def add_network_hosts(network_id, network_name, hosts): """ add hosts to vlan :param network_id:id of network :param network_name: Network={"id":"uuid","name":"vlan140","hosts":[host1,host2]} :param hosts: the hosts assign to network """ vlan_hosts = { "vlan_id": network_id, "vlan_name": network_name, "hosts": hosts } try: if vlan_hosts and isinstance(vlan_hosts, dict): hosts = vlan_hosts["hosts"] new_hosts = [(str(uuid4()), vlan_hosts["vlan_id"], vlan_hosts["vlan_name"], host) for host in hosts if host] for new_host in new_hosts: yield dbpools.execute_commit( dbpools.get_pool(dbpools.LOCAL_DB), sql= "insert into vlan_hosts (id, vlan_id, vlan_name, host_id) VALUES (%s,%s,%s,%s)", param=new_host) except Exception, e: LOG.error("insert vlan hosts error: %s" % e) raise e
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 force_delete_volume_from_db(volume_id): """ clear volume attachment info from cinder db :param volume_id: id of volume :return: """ try: db = dbpools.get_cinder() yield dbpools.execute_commit( db, "update volumes set status='available', attach_status='detached' " "where id = %s", (volume_id)) yield dbpools.execute_commit( db, "delete from volume_attachment where volume_id = %s", (volume_id)) except Exception as e: LOG.error("force delete volume from db error: %s" % e) raise e
def update_volume_image_metadata(volume_id, key, value): try: db = dbpools.get_cinder() query_sql = "select * from volume_glance_metadata where volume_id = %s and `key` = %s" cur = yield db.execute(query_sql, [volume_id, key]) res = cur.fetchone() if res: yield dbpools.execute_commit( db, "update volume_glance_metadata set `value` = %s where volume_id = %s and `key` = %s", (value, volume_id, key)) else: yield dbpools.execute_commit( db, "insert into volume_glance_metadata (created_at,deleted,volume_id,`key`,`value`) VALUES (NOW(),0,%s,%s,%s)", (volume_id, key, value)) except Exception, e: LOG.error(e.message) raise VolumeOperationFailed()
def __update_server_state(vm_id): try: db = dbpools.get_nova() yield dbpools.execute_commit( db, "update instances set vm_state = 'stopped' ,power_state = 4 where uuid = %s", (vm_id) ) except Exception, e: LOG.error("host down update vm state stopped error: %s" % e)
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 __activate_user(token_id, user_id): db = dbpools.get_pool(dbpools.COMMON_DB) cur = yield db.execute("select token from token where user_id = %s ", (user_id,)) old_token = cur.fetchone() if old_token: yield __delete_token(old_token['token']) yield dbpools.execute_commit( db, "insert into token (token, user_id) values (%s, %s)", (token_id, user_id) )
def delete_tenant_subnets(tenant_id, subnet_ids=None): """ delete tenant of vlan record :param subnet_ids: ids of vlan, default None :param tenant_id: id of tenant """ db = dbpools.get_pool(dbpools.LOCAL_DB) try: if subnet_ids: str_subnet_ids = ','.join(["'%s'" % subnet for subnet in subnet_ids]) yield dbpools.execute_commit(db, sql="delete from vlan_subnet_tenant where subnet_id in (%s) " "and tenant_id = %%s" % str_subnet_ids, param=[tenant_id]) else: yield dbpools.execute_commit(db, sql="delete from vlan_subnet_tenant where tenant_id = %s", param=[tenant_id]) except Exception, e: LOG.error("delete subnet tenant error: %s" % e) raise e
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_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_tenant_subnet_ips(subnet_id, tenant_id, new_ippools): try: db = dbpools.get_pool(dbpools.LOCAL_DB) sql = "UPDATE vlan_subnet_tenant SET ippools=%s " \ "WHERE subnet_id=%s and tenant_id=%s " \ yield dbpools.execute_commit(db, sql=sql, param=(json.dumps(new_ippools), subnet_id, tenant_id)) except Exception, e: LOG.error("update_tenant subnet 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 __force_del_detach_volume(volume_id): """ force detach specific volume from specific server :param volume_id: The :id: volume """ try: try: db = dbpools.get_nova() yield dbpools.execute_commit( db, "delete from block_device_mapping where volume_id = %s", (volume_id)) except Exception, e: LOG.error("force delete server attach volume from db error: %s" % e) raise e except Exception as e: LOG.error("force del server detach volume error: %s" % e)
def __delete_token(token_id): yield dbpools.execute_commit( dbpools.get_common(), "delete from token where token = %s", (token_id,) ) try: session = yield openstack.get_session() yield openstack.connect_request( session=session, type=openstack.TYPE_IDENTITY, url="/tokens/%s" % token_id, method=openstack.METHOD_DELETE ) except Exception, e: LOG.error("this token has deleted %s" % e)
def __update_server_host(vm_id, new_host): """ update tenant' quota settings :param vm_id: id of virtual machine :param new_host: migrate to host """ try: db = dbpools.get_nova() yield dbpools.execute_commit( db, "update instances set `host`=%s, node=%s, launched_on=%s" " where uuid = %s", (new_host, new_host, new_host, vm_id) ) except Exception, e: LOG.error("update instance migrate to host: %s, error: %s" % (new_host, e)) raise MigrateFailed()
def update_vlan_host(vlan_id=None, **vlan_host): """ update host of vlan record :param vlan_id: id of vlan :param vlan_host: {"vlan_name":"vlan130", "des":"desc"} """ try: if vlan_id: update_params = ', '.join(['%s=%%s' % k for k in vlan_host.keys()]) update_values = vlan_host.values() update_values.extend([vlan_id]) if update_params: yield dbpools.execute_commit( dbpools.get_pool(dbpools.LOCAL_DB), sql="update vlan_hosts set %s %s" % (update_params, "where vlan_id = %s"), param=update_values) except Exception, e: LOG.error("update vlan host error: %s" % e) raise e
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(id): db = dbpools.get_local() sql = "delete from taskflow where id = %s" yield dbpools.execute_commit(db, sql, [id])
def del_expires_token(): yield dbpools.execute_commit( dbpools.get_keystone(), "delete from token where expires < utc_timestamp() limit 1000" )
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])