def zones_get(sid): """Return a list of zones. Method is accessible with GET /v1/schematics/<sid>/zones :param sid: ``str`` # schematic ID :return json, status: ``tuple`` """ parsed_data = utils.zone_basic_handler(sid=sid) if parsed_data[0] is False: return utils.return_msg(msg=parsed_data[1], status=parsed_data[2]) else: _success, schematic, zones, user_id = parsed_data LOG.debug('%s %s %s %s', _success, schematic, zones, user_id) try: return_zones = [] for zone in zones: dzone = utils.pop_ts(zone.__dict__) instances = db_proc.get_instances(zon=zone) if instances: dzone['instance_quantity'] = len(instances) return_zones.append(dzone) except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='Unexpected Error', status=500) else: return utils.return_msg(msg=return_zones, status=200)
def zone_put(sid=None, zid=None): """Update a Zone. Method is accessible with PUT /v1/schematics/<sid>/zones/<zid> :param sid: ``str`` # schematic ID :param zid: ``str`` # Zone ID :return json, status: ``tuple`` """ parsed_data = utils.zone_data_handler(sid=sid) if parsed_data[0] is False: return utils.return_msg(msg=parsed_data[1], status=parsed_data[2]) else: _success, schematic, payload, user_id = parsed_data LOG.debug('%s %s %s %s', _success, schematic, payload, user_id) zone = db_proc.get_zones_by_id(skm=schematic, zid=zid) if not zone: return utils.return_msg(msg='no zones found', status=404) try: sess = DB.session sess = db_proc.put_zone(session=sess, zon=zone, put=payload) except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='unexpected error', status=500) else: db_proc.commit_session(session=sess) return utils.return_msg(msg='updates received', status=201)
def reset_zone_state(sid=None, zid=None): r"""Reset the state of a zone to active. This method will reset the state of an existing zone no matter the current state. The new state after invoking this method will be set to "ACTIVE RESET" Method is accessible with POST /v1/schematics/<sid>/zones :param sid: ``str`` # schematic ID :param zid: ``str`` # Zone ID :return json, status: ``tuple`` """ parsed_data = utils.zone_basic_handler(sid=sid, zid=zid) if parsed_data[0] is False: return utils.return_msg(msg=parsed_data[1], status=parsed_data[2]) else: _success, schematic, zone, user_id = parsed_data LOG.debug('%s %s %s %s', _success, schematic, zone, user_id) cell = {'zone_state': 'ACTIVE RESET'} try: sess = DB.session db_proc.put_zone(session=sess, zon=zone, put=cell) except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='unexpected error', status=500) else: db_proc.commit_session(session=sess) return utils.return_msg( msg='Zone State for %s has been Reset' % zid, status=200 )
def zone_purge(sid=None, zid=None): """purge a Zone. This is used to remove all indication of a zone without attempting to disconnect or otherwise clean up the zone or any of its may be attached instances. Method is accessible with DELETE /v1/schematics/<sid>/zones/<zid>/purge :param sid: ``str`` # schematic ID :param zid: ``str`` # Zone ID :return json, status: ``tuple`` """ parsed_data = utils.zone_basic_handler(sid=sid, zid=zid) if parsed_data[0] is False: return utils.return_msg(msg=parsed_data[1], status=parsed_data[2]) else: _success, schematic, zone, user_id = parsed_data LOG.debug('%s %s %s %s', _success, schematic, zone, user_id) try: sess = DB.session db_proc.delete_item(session=sess, item=zone) except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='unexpected error', status=500) else: db_proc.commit_session(session=sess) return utils.return_msg( msg='zone %s was purged' % zone.id, status=203 )
def zone_put(sid=None, zid=None): """Update a Zone. Method is accessible with PUT /v1/schematics/<sid>/zones/<zid> :param sid: ``str`` # schematic ID :param zid: ``str`` # Zone ID :return json, status: ``tuple`` """ parsed_data = utils.zone_data_handler(sid=sid) if parsed_data[0] is False: return utils.return_msg(msg=parsed_data[1], status=parsed_data[2]) else: _success, schematic, payload, user_id = parsed_data LOG.debug('%s %s %s %s', _success, schematic, payload, user_id) zone = db_proc.get_zones_by_id(skm=schematic, zid=zid) if not zone: return utils.return_msg(msg='no zones found', status=404) try: sess = DB.session sess = db_proc.put_zone( session=sess, zon=zone, put=payload ) except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='unexpected error', status=500) else: db_proc.commit_session(session=sess) return utils.return_msg(msg='updates received', status=201)
def schematics_list(): """Return a list of Schematics. Method is accessible with GET /v1/schematics :return json, status: ``tuple`` """ user_id = utils.auth_mech(rdata=flask.request.headers) if not user_id: return utils.return_msg(msg='missing information', status=400) schematics = db_proc.get_schematics(uid=user_id) if not schematics: return utils.return_msg(msg='no schematic(s) found', status=404) try: schematics_list = [ utils.build_schematic_list(schematic) for schematic in schematics if schematic ] except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='unexpected error', status=500) else: if not schematics_list: return utils.return_msg(msg='no schematic(s) found', status=404) else: return utils.return_msg(msg=schematics_list, status=200)
def reset_zone_state(sid=None, zid=None): r"""Reset the state of a zone to active. This method will reset the state of an existing zone no matter the current state. The new state after invoking this method will be set to "ACTIVE RESET" Method is accessible with POST /v1/schematics/<sid>/zones :param sid: ``str`` # schematic ID :param zid: ``str`` # Zone ID :return json, status: ``tuple`` """ parsed_data = utils.zone_basic_handler(sid=sid, zid=zid) if parsed_data[0] is False: return utils.return_msg(msg=parsed_data[1], status=parsed_data[2]) else: _success, schematic, zone, user_id = parsed_data LOG.debug('%s %s %s %s', _success, schematic, zone, user_id) cell = {'zone_state': 'ACTIVE RESET'} try: sess = DB.session db_proc.put_zone(session=sess, zon=zone, put=cell) except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='unexpected error', status=500) else: db_proc.commit_session(session=sess) return utils.return_msg(msg='Zone State for %s has been Reset' % zid, status=200)
def zone_purge(sid=None, zid=None): """purge a Zone. This is used to remove all indication of a zone without attempting to disconnect or otherwise clean up the zone or any of its may be attached instances. Method is accessible with DELETE /v1/schematics/<sid>/zones/<zid>/purge :param sid: ``str`` # schematic ID :param zid: ``str`` # Zone ID :return json, status: ``tuple`` """ parsed_data = utils.zone_basic_handler(sid=sid, zid=zid) if parsed_data[0] is False: return utils.return_msg(msg=parsed_data[1], status=parsed_data[2]) else: _success, schematic, zone, user_id = parsed_data LOG.debug('%s %s %s %s', _success, schematic, zone, user_id) try: sess = DB.session db_proc.delete_item(session=sess, item=zone) except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='unexpected error', status=500) else: db_proc.commit_session(session=sess) return utils.return_msg(msg='zone %s was purged' % zone.id, status=203)
def schematic_get(sid=None): """Return a of Schematic. Method is accessible with GET /v1/schematics/<sid> :param sid: ``str`` # schematic ID :return json, status: ``tuple`` """ if not sid: return utils.return_msg(msg='missing information', status=400) user_id = utils.auth_mech(rdata=flask.request.headers) if not user_id: return utils.return_msg(msg='missing information', status=400) schematic = db_proc.get_schematic_id(sid=sid, uid=user_id) if not schematic: return utils.return_msg(msg='no schematic found', status=404) else: _schematic = utils.pop_ts(temp=schematic.__dict__) config = db_proc.get_configmanager(skm=schematic) if config: _config = utils.pop_ts(temp=config.__dict__) _schematic['config_manager'] = _config return utils.return_msg(msg=_schematic, status=200)
def zone_post(sid=None): """Post a Zone. Method is accessible with POST /v1/schematics/<sid>/zones :param sid: ``str`` # schematic ID :return json, status: ``tuple`` """ parsed_data = utils.zone_data_handler(sid=sid, check_for_zone=True) if parsed_data[0] is False: return utils.return_msg(msg=parsed_data[1], status=parsed_data[2]) else: _success, schematic, payload, user_id = parsed_data LOG.debug('%s %s %s %s', _success, schematic, payload, user_id) config = db_proc.get_configmanager(skm=schematic) try: sess = DB.session for _zn in payload['zones']: ssh_user = _zn.get('ssh_user') pub = _zn.get('ssh_key_pub') pri = _zn.get('ssh_key_pri') key_name = _zn.get('key_name') ssh_key = db_proc.post_instanceskeys( pub=pub, pri=pri, sshu=ssh_user, key_name=key_name ) db_proc.add_item(session=sess, item=ssh_key) zone = db_proc.post_zones( skm=schematic, zon=_zn, ssh=ssh_key ) db_proc.add_item(session=sess, item=zone) packet = utils.build_cell( job='build', schematic=schematic, zone=zone, sshkey=ssh_key, config=config ) rpc.default_publisher(message=packet) except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='Unexpected Error', status=500) else: db_proc.commit_session(session=sess) msg = 'Application requests have been recieved for Schematic %s' % sid return utils.return_msg(msg=msg, status=200)
def schematic_post(): """Post a Schematic, Method is accessible with POST /v1/schematics :return json, status: ``tuple`` """ auth = utils.auth_mech(hdata=flask.request.data, rdata=flask.request.headers) if not auth: return utils.return_msg(msg='Authentication or Data Type Failure', status=401) else: user_id, payload = auth if not all([user_id, payload]): build_response = 'missing information %s %s' % (user_id, payload) return utils.return_msg(msg=build_response, status=400) config_type = payload.get('config_type') try: sess = DB.session if config_type is not None: if _config_check(config_type).validate_plugin() is False: raise tribble.DeadOnArival('Plugin "%s" was not found.' % config_type) con = db_proc.post_configmanager(post=payload) db_proc.add_item(session=sess, item=con) schematic = db_proc.post_schematic(con=con, uid=user_id, post=payload) db_proc.add_item(session=sess, item=schematic) if 'zones' in payload: _zone_builder(session=sess, schematic=schematic, con=con, payload=payload) except tribble.DeadOnArival as exp: return utils.return_msg(msg='%s' % exp, status=405) except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='Unexpected Error', status=500) else: LOG.debug(payload) build_response = ( 'Application requests have been recieved and Schematic %s has' ' been built' % schematic.id) db_proc.commit_session(session=sess) return utils.return_msg(msg=build_response, status=200)
def zone_post(sid=None): """Post a Zone. Method is accessible with POST /v1/schematics/<sid>/zones :param sid: ``str`` # schematic ID :return json, status: ``tuple`` """ parsed_data = utils.zone_data_handler(sid=sid, check_for_zone=True) if parsed_data[0] is False: return utils.return_msg(msg=parsed_data[1], status=parsed_data[2]) else: _success, schematic, payload, user_id = parsed_data LOG.debug('%s %s %s %s', _success, schematic, payload, user_id) config = db_proc.get_configmanager(skm=schematic) try: sess = DB.session for _zn in payload['zones']: ssh_user = _zn.get('ssh_user') pub = _zn.get('ssh_key_pub') pri = _zn.get('ssh_key_pri') key_name = _zn.get('key_name') ssh_key = db_proc.post_instanceskeys(pub=pub, pri=pri, sshu=ssh_user, key_name=key_name) db_proc.add_item(session=sess, item=ssh_key) zone = db_proc.post_zones(skm=schematic, zon=_zn, ssh=ssh_key) db_proc.add_item(session=sess, item=zone) packet = utils.build_cell(job='build', schematic=schematic, zone=zone, sshkey=ssh_key, config=config) rpc.default_publisher(message=packet) except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='Unexpected Error', status=500) else: db_proc.commit_session(session=sess) msg = 'Application requests have been recieved for Schematic %s' % sid return utils.return_msg(msg=msg, status=200)
def index(): """Return 200 response on GET '/' :return json, status: tuple """ state = {'Version': info.__version__, 'Application': info.__appname__} LOG.debug('%s %s', flask.request.method, flask.request.path) return utils.return_msg(msg=state, status=200)
def instance_delete(sid=None, zid=None, iid=None): """Delete an Instance from a Zone. :param sid: ``str`` # schematic ID :param zid: ``str`` # Zone ID :param iid: ``str`` # Instance ID :return json, status: ``tuple`` """ if not all([sid, zid, iid]): check_all = [check for check in sid, zid, iid if not check] return utils.return_msg( msg='missing Information %s' % check_all, status=400 ) user_id = utils.auth_mech(rdata=flask.request.headers) if not user_id: return utils.return_msg(msg='missing information', status=400) schematic = db_proc.get_schematic_id(sid=sid, uid=user_id) if not schematic: return utils.return_msg(msg='no schematic found', status=404) zone = db_proc.get_zones_by_id(skm=schematic, zid=zid) if not zone: return utils.return_msg(msg='No Zone Found', status=404) elif zone.zone_state == 'BUILDING': build_response = ("Instance Delete can not be performed because Zone" " %s has a Pending Status" % zone.id) return utils.return_msg(msg=build_response, status=200) instances = db_proc.get_instances(zon=zone) instance_id = [ instance.instance_id for instance in instances if instance.instance_id == iid ] if not instance_id: return utils.return_msg(msg='No Instance Found', status=404) try: config = db_proc.get_configmanager(skm=schematic) cell = utils.build_cell( job='instance_delete', schematic=schematic, zone=zone, config=config ) cell['uuids'] = instance_id except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='Unexpected Error', status=500) else: rpc.default_publisher(message=cell) return utils.return_msg(msg='Deletes Recieved', status=203)
def zone_delete(sid=None, zid=None): """Delete a Zone. Method is accessible with DELETE /v1/schematics/<sid>/zones/<zid> :param sid: ``str`` # schematic ID :param zid: ``str`` # Zone ID :return json, status: ``tuple`` """ parsed_data = utils.zone_basic_handler(sid=sid, zid=zid) if parsed_data[0] is False: return utils.return_msg(msg=parsed_data[1], status=parsed_data[2]) else: _success, schematic, zone, user_id = parsed_data if zone.zone_state == 'BUILDING': build_response = ( 'Zone Delete can not be performed because Zone "%s" has a' ' Pending Status' % zone.id ) return utils.return_msg(msg=build_response, status=200) LOG.debug('%s %s %s %s', _success, schematic, zone, user_id) try: config = db_proc.get_configmanager(skm=schematic) instances = db_proc.get_instances(zon=zone) packet = utils.build_cell( job='zone_delete', schematic=schematic, zone=zone, config=config ) packet['uuids'] = [instance.instance_id for instance in instances] rpc.default_publisher(message=packet) sess = DB.session zone_status.ZoneState(cell=packet).delete() except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='unexpected error', status=500) else: db_proc.commit_session(session=sess) return utils.return_msg(msg='deletes received', status=203)
def instance_delete(sid=None, zid=None, iid=None): """Delete an Instance from a Zone. :param sid: ``str`` # schematic ID :param zid: ``str`` # Zone ID :param iid: ``str`` # Instance ID :return json, status: ``tuple`` """ if not all([sid, zid, iid]): check_all = [check for check in sid, zid, iid if not check] return utils.return_msg(msg='missing Information %s' % check_all, status=400) user_id = utils.auth_mech(rdata=flask.request.headers) if not user_id: return utils.return_msg(msg='missing information', status=400) schematic = db_proc.get_schematic_id(sid=sid, uid=user_id) if not schematic: return utils.return_msg(msg='no schematic found', status=404) zone = db_proc.get_zones_by_id(skm=schematic, zid=zid) if not zone: return utils.return_msg(msg='No Zone Found', status=404) elif zone.zone_state == 'BUILDING': build_response = ("Instance Delete can not be performed because Zone" " %s has a Pending Status" % zone.id) return utils.return_msg(msg=build_response, status=200) instances = db_proc.get_instances(zon=zone) instance_id = [ instance.instance_id for instance in instances if instance.instance_id == iid ] if not instance_id: return utils.return_msg(msg='No Instance Found', status=404) try: config = db_proc.get_configmanager(skm=schematic) cell = utils.build_cell(job='instance_delete', schematic=schematic, zone=zone, config=config) cell['uuids'] = instance_id except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='Unexpected Error', status=500) else: rpc.default_publisher(message=cell) return utils.return_msg(msg='Deletes Recieved', status=203)
def schematic_put(sid=None): """Update a schematic. if a zone is in the put data add the zone to the schematic. The addition of a zone on a put will not build the zone automatically. Method is accessible with PUT /v1/schematics/<sid> :param sid: ``str`` # schematic ID :return json, status: ``tuple`` """ if not sid: return utils.return_msg(msg='missing information', status=400) auth = utils.auth_mech(hdata=flask.request.data, rdata=flask.request.headers) if not auth: return utils.return_msg(msg='Authentication or Data Type Failure', status=401) else: user_id, payload = auth schematic = db_proc.get_schematic_id(sid=sid, uid=user_id) if not schematic: return utils.return_msg(msg='no schematic found', status=404) if not all([user_id, payload]): build_response = 'missing information %s %s' % (user_id, payload) return utils.return_msg(msg=build_response, status=400) config_type = payload.get('config_type') try: sess = DB.session if config_type is not None: if _config_check(config_type).validate_plugin() is False: raise tribble.DeadOnArival('Plugin "%s" was not found.' % config_type) con = db_proc.get_configmanager(skm=schematic) db_proc.put_schematic_id(session=sess, skm=schematic, put=payload) db_proc.put_configmanager(session=sess, con=con, put=payload) if 'zones' in payload: _zone_builder(session=sess, schematic=schematic, con=con, payload=payload) except tribble.DeadOnArival as exp: return utils.return_msg(msg='%s' % exp, status=405) except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='Unexpected Error', status=500) else: LOG.debug(payload) db_proc.commit_session(session=sess) return utils.return_msg(msg='Updates Recieved', status=201)
def zone_delete(sid=None, zid=None): """Delete a Zone. Method is accessible with DELETE /v1/schematics/<sid>/zones/<zid> :param sid: ``str`` # schematic ID :param zid: ``str`` # Zone ID :return json, status: ``tuple`` """ parsed_data = utils.zone_basic_handler(sid=sid, zid=zid) if parsed_data[0] is False: return utils.return_msg(msg=parsed_data[1], status=parsed_data[2]) else: _success, schematic, zone, user_id = parsed_data if zone.zone_state == 'BUILDING': build_response = ( 'Zone Delete can not be performed because Zone "%s" has a' ' Pending Status' % zone.id) return utils.return_msg(msg=build_response, status=200) LOG.debug('%s %s %s %s', _success, schematic, zone, user_id) try: config = db_proc.get_configmanager(skm=schematic) instances = db_proc.get_instances(zon=zone) packet = utils.build_cell(job='zone_delete', schematic=schematic, zone=zone, config=config) packet['uuids'] = [instance.instance_id for instance in instances] rpc.default_publisher(message=packet) sess = DB.session zone_status.ZoneState(cell=packet).delete() except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='unexpected error', status=500) else: db_proc.commit_session(session=sess) return utils.return_msg(msg='deletes received', status=203)
def zone_get(sid, zid): """Return a zone. Method is accessible with GET /v1/schematics/<sid>/zones/<zid> :param sid: ``str`` # schematic ID :param zid: ``str`` # Zone ID :return json, status: ``tuple`` """ parsed_data = utils.zone_basic_handler(sid=sid, zid=zid) if parsed_data[0] is False: return utils.return_msg(msg=parsed_data[1], status=parsed_data[2]) else: _success, schematic, zone, user_id = parsed_data _zone = utils.pop_ts(temp=zone.__dict__) instances = db_proc.get_instances(zon=zone) if instances: _zone['instances'] = [ utils.pop_ts(temp=instance.__dict__) for instance in instances ] LOG.debug('%s %s %s %s', _success, schematic, zone, user_id) return utils.return_msg(msg=_zone, status=200)
def schematic_delete(sid=None): """Delete a Schematic. Method is accessible with GET /v1/schematics/<sid> :param sid: ``str`` # schematic ID :return json, status: ``tuple`` """ if not sid: return utils.return_msg(msg='missing information', status=400) user_id = utils.auth_mech(rdata=flask.request.headers) if not user_id: return utils.return_msg(msg='missing information', status=400) schematic = db_proc.get_schematic_id(sid=sid, uid=user_id) if not schematic: return utils.return_msg(msg='no schematic found', status=404) try: config = db_proc.get_configmanager(skm=schematic) zones = db_proc.get_zones(skm=schematic) if zones: zone_ids = [zone.id for zone in zones] build_response = ('can not delete the schematic, you have an' ' active zone(s): %s' % zone_ids) return utils.return_msg(msg=build_response, status=405) sess = DB.session db_proc.delete_item(session=sess, item=schematic) db_proc.delete_item(session=sess, item=config) except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='unexpected error', status=500) else: db_proc.commit_session(session=sess) return utils.return_msg(msg='deletes received', status=203)
def redeploy_zone(sid=None, zid=None): """Redploy a zone. This method will interate over an existing zone and ensure that all things known in the zone are built and in an active state. Method is accessible with POST /v1/schematics/<sid>/zones :param sid: ``str`` # schematic ID :param zid: ``str`` # Zone ID :return json, status: ``tuple`` """ parsed_data = utils.zone_basic_handler(sid=sid, zid=zid) if parsed_data[0] is False: return utils.return_msg(msg=parsed_data[1], status=parsed_data[2]) else: _success, schematic, zone, user_id = parsed_data LOG.debug('%s %s %s %s', _success, schematic, zone, user_id) config = db_proc.get_configmanager(skm=schematic) key = db_proc.get_instanceskeys(zon=zone) ints = db_proc.get_instances(zon=zone) base_qty = int(zone.quantity) numr_qty = len(ints) if base_qty > numr_qty: difference = base_qty - numr_qty packet = utils.build_cell( job='redeploy_build', schematic=schematic, zone=zone, sshkey=key, config=config ) packet['quantity'] = difference LOG.debug(packet) rpc.default_publisher(message=packet) msg = 'Building %s Instances for Zone %s' % (difference, zone.id) return utils.return_msg(msg=msg, status=200) elif base_qty < numr_qty: difference = numr_qty - base_qty packet = utils.build_cell( job='redeploy_delete', schematic=schematic, zone=zone, sshkey=key, config=config ) instances = [ins.instance_id for ins in ints] remove_instances = instances[:difference] packet['uuids'] = remove_instances LOG.debug(packet) remove_ids = [ ins for ins in ints if ins.instance_id in remove_instances ] try: sess = DB.session for instance_id in remove_ids: db_proc.delete_item(session=sess, item=instance_id) except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='Unexpected Error', status=500) else: rpc.default_publisher(message=packet) db_proc.commit_session(session=sess) msg = 'Removing %s Instances for Zone %s' % (difference, zone.id) return utils.return_msg(msg=msg, status=200) else: return utils.return_msg(msg='nothing to do', status=200)
def redeploy_zone(sid=None, zid=None): """Redploy a zone. This method will interate over an existing zone and ensure that all things known in the zone are built and in an active state. Method is accessible with POST /v1/schematics/<sid>/zones :param sid: ``str`` # schematic ID :param zid: ``str`` # Zone ID :return json, status: ``tuple`` """ parsed_data = utils.zone_basic_handler(sid=sid, zid=zid) if parsed_data[0] is False: return utils.return_msg(msg=parsed_data[1], status=parsed_data[2]) else: _success, schematic, zone, user_id = parsed_data LOG.debug('%s %s %s %s', _success, schematic, zone, user_id) config = db_proc.get_configmanager(skm=schematic) key = db_proc.get_instanceskeys(zon=zone) ints = db_proc.get_instances(zon=zone) base_qty = int(zone.quantity) numr_qty = len(ints) if base_qty > numr_qty: difference = base_qty - numr_qty packet = utils.build_cell(job='redeploy_build', schematic=schematic, zone=zone, sshkey=key, config=config) packet['quantity'] = difference LOG.debug(packet) rpc.default_publisher(message=packet) msg = 'Building %s Instances for Zone %s' % (difference, zone.id) return utils.return_msg(msg=msg, status=200) elif base_qty < numr_qty: difference = numr_qty - base_qty packet = utils.build_cell(job='redeploy_delete', schematic=schematic, zone=zone, sshkey=key, config=config) instances = [ins.instance_id for ins in ints] remove_instances = instances[:difference] packet['uuids'] = remove_instances LOG.debug(packet) remove_ids = [ ins for ins in ints if ins.instance_id in remove_instances ] try: sess = DB.session for instance_id in remove_ids: db_proc.delete_item(session=sess, item=instance_id) except Exception: LOG.error(traceback.format_exc()) return utils.return_msg(msg='Unexpected Error', status=500) else: rpc.default_publisher(message=packet) db_proc.commit_session(session=sess) msg = 'Removing %s Instances for Zone %s' % (difference, zone.id) return utils.return_msg(msg=msg, status=200) else: return utils.return_msg(msg='nothing to do', status=200)