def get_task_flow(task_id=None, name=None, resource=None, **params): result = [] db = dbpools.get_local() sql = "select * from taskflow where 1=1" if task_id: sql = sql + " and task_id = '" + task_id + "'" if name: sql = sql + " and name = '" + name + "'" if resource: sql = sql + " and resource = '" + resource + "'" cur = yield db.execute(sql) tasks = cur.fetchall() if tasks: for task in tasks: resp = { "id": task.get("id"), "task_id": task.get("task_id"), "status": task.get("status"), "name": task.get("name"), "resource": task.get("resource"), "updated_at": task.get("updated_at") } pro = simplejson.loads(task.get("param")) mes = simplejson.loads(task.get("message")) res = resp.copy() res.update(pro) res.update(mes) flag = True for k, v in params.items(): if k not in res or v != res.get(k): flag = False if flag: result.append(res) raise gen.Return(result)
def insert_tenant_subnets(tenant_id=None, subnet_ids=None): """ insert vlans of tenant :param tenant_id: id of tenant :param subnet_ids: ids of networks """ try: new_tenants = [] if tenant_id and subnet_ids and \ isinstance(subnet_ids, (list, tuple)): subnet_info_all = yield get_subnet_db(subnet_ids=subnet_ids) subnet_dict={} for subnet_info in subnet_info_all: subnet_dict[subnet_info["id"]] = subnet_info["network_id"] for subnet_id in subnet_ids: vlan_id= subnet_dict[subnet_id] new_tenants.append((str(uuid4()), vlan_id, subnet_id, tenant_id, subnet_id, tenant_id)) if new_tenants: for new_tenant in new_tenants: yield dbpools.execute_commit(dbpools.get_local(), sql="insert into vlan_subnet_tenant (id,vlan_id, subnet_id, tenant_id) " "select %s, %s, %s, %s from dual where not exists " "(select * from vlan_subnet_tenant where subnet_id = %s " "and tenant_id = %s)", param=new_tenant) except Exception, e: print e LOG.error("insert subnet tenants error: %s" % e) raise e
def clean_rubbish(): """clean task""" try: db = dbpools.get_local() sql = "update taskflow set status=4, updated_at=now() where status=3" yield dbpools.update(db, sql) except Exception, e: pass
def get_expire_task(interval): db = dbpools.get_local() sql = "select * from taskflow where status = 0 and TIMESTAMPDIFF(SECOND,updated_at,NOW()) > %s" try: cur = yield db.execute(sql, (interval, )) result = cur.fetchall() except Exception, e: print e
def __get_from_db(tenant_id, name): db = dbpools.get_local() cur = yield db.execute( "select tenant_id, quota_name, quota_limit, quota_used, " "unix_timestamp(dt_created) as dt_created, unix_timestamp(dt_updated) as dt_updated " "from tenant_quotas where tenant_id = %s and quota_name = %s", (tenant_id, name) ) raise gen.Return(cur.fetchone())
def delete_quotas(tenant_id): try: yield dbpools.execute_commit( dbpools.get_local(), 'delete from tenant_quotas where tenant_id = %s', (tenant_id, ) ) except Exception, e: LOG.error(e.message) raise TenantQuotaOperationFailed()
def insert_task_flow(task_id, name, resource, message='', **params): task_count = yield get_task_flow_count(name, resource) if not task_count: db = dbpools.get_local() sql = "insert into taskflow (task_id, `name`,resource, updated_at, message, param) " \ "values (%s, %s, %s, now(), %s, %s)" yield dbpools.execute_commit(db, sql, [ task_id, name, resource, simplejson.dumps(message), simplejson.dumps(params) ])
def get_task_flow_count(name, resource): db = dbpools.get_local() sql = "select count(*) as count from taskflow where 1=1" if name: sql = sql + " and name = '" + name + "'" if resource: sql = sql + " and resource = '" + resource + "'" cur = yield db.execute(sql) task_count = cur.fetchone() result = task_count.get("count") raise gen.Return(result)
def insert_tenant_hosts(tenant_id, tenant_hosts): tenant_hosts = json.dumps(tenant_hosts) try: yield dbpools.execute_commit( dbpools.get_local(), sql="insert into tenant_hosts (id,tenant_id,hosts) " "select %s, %s,%s from dual where not EXISTS " "(SELECT * from tenant_hosts where tenant_id =%s)", param=(str(uuid4()), tenant_id, tenant_id, tenant_hosts)) except Exception, e: LOG.error("insert the tenant_hosts error :%s" % e) raise e
def update_quota_used(tenant_id, name, used): __check_quota_name(name) quota = yield __get_from_db(tenant_id, name) if not quota: yield update_quota_limit(tenant_id, name) try: yield dbpools.execute_commit( dbpools.get_local(), 'update tenant_quotas set dt_updated=utc_timestamp(), quota_used=%s ' 'where tenant_id = %s and quota_name = %s', (used, tenant_id, name) ) except Exception, e: LOG.error(e.message) raise TenantQuotaOperationFailed()
def update_quota_limit(tenant_id, name, limit=-1): __check_quota_name(name) quota = yield __get_from_db(tenant_id, name) try: db = dbpools.get_local() if quota: yield dbpools.execute_commit( db, 'update tenant_quotas set dt_updated=utc_timestamp(), quota_limit=%s ' 'where tenant_id = %s and quota_name = %s', (limit, tenant_id, name) ) else: yield dbpools.execute_commit( db, 'insert into tenant_quotas (tenant_id, quota_name, quota_limit, dt_created) ' 'values (%s, %s, %s, utc_timestamp())', (tenant_id, name, limit) ) except Exception, e: LOG.error(e.message) raise TenantQuotaOperationFailed()
def update_task_flow_status(id, status): db = dbpools.get_local() sql = "update taskflow set status = %s, updated_at= now() where status != %s and id = %s" row = yield dbpools.update(db, sql, [status, status, id]) raise gen.Return(row)
def update_task_flow(id, **update_params): db = dbpools.get_local() sql = "update taskflow set %s where id = %%s" % \ ', '.join(["%s = '%s'" % (k, v) for k, v in update_params.items()]) yield dbpools.execute_commit(db, sql, [id])
def delete_task_flow_by_resource(resource): db = dbpools.get_local() sql = "delete from taskflow where resource = %s" yield dbpools.execute_commit(db, sql, [resource])
def delete_task_flow(id): db = dbpools.get_local() sql = "delete from taskflow where id = %s" yield dbpools.execute_commit(db, sql, [id])