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 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_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 state_update(self): """Perform a state update.""" try: sess = DB.session db_proc.put_zone(session=sess, zon=self.zone, put=self.cell) except Exception: LOG.error(traceback.format_exc()) else: db_proc.commit_session(session=sess)
def state_update(self): """Perform a state update.""" try: sess = DB.session db_proc.put_zone(session=sess, zon=self.zone, put=self.cell) except Exception: LOG.error(traceback.format_exc()) else: db_proc.commit_session(session=sess)
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_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 delete_user(self): """Delete a user in the Tribble DB. :return: ``str`` """ try: sess = DB.session user_query = db_proc.get_user_id(user_name=self.user) db_proc.delete_item(session=sess, item=user_query) except Exception as exp: self.log.error(traceback.format_exc()) return 'Failed to delete user\nERROR : %s' % exp else: db_proc.commit_session(session=sess) self.log.warn('User %s was deleted' % self.user)
def _node_post(self, info): """Delete an instance from both the cloud provider and from the DB. :param info: ``object`` """ try: sess = DB.session new_instance = db_proc.post_instance(ins=info, put=self.packet) sess = db_proc.add_item(session=sess, item=new_instance) except Exception as exp: self.zone_status.error(error_msg=exp) LOG.critical('Issues when Posting Instances %s' % exp) else: db_proc.commit_session(session=sess) LOG.info('Instance posted ID:%s NAME:%s' % (info.id, info.name))
def delete_user(self): """Delete a user in the Tribble DB. :return: ``str`` """ try: sess = DB.session user_query = db_proc.get_user_id(user_name=self.user) db_proc.delete_item(session=sess, item=user_query) except Exception as exp: self.log.error(traceback.format_exc()) return 'Failed to delete user\nERROR : %s' % exp else: db_proc.commit_session(session=sess) self.log.warn('User %s was deleted' % self.user)
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 _node_post(self, info): """Delete an instance from both the cloud provider and from the DB. :param info: ``object`` """ try: sess = DB.session new_instance = db_proc.post_instance(ins=info, put=self.packet) sess = db_proc.add_item( session=sess, item=new_instance ) except Exception as exp: self.zone_status.error(error_msg=exp) LOG.critical('Issues when Posting Instances %s' % exp) else: db_proc.commit_session(session=sess) LOG.info('Instance posted ID:%s NAME:%s' % (info.id, info.name))
def create_user(self): """Create a user in the Tribble DB. :return: ``str`` """ try: sess = DB.session new_user = db_proc.post_user(admin=self.args.get('admin', 0), user=self.user, encrypted=rosetta.encrypt( password=self.key, plaintext=self.psw)) db_proc.add_item(session=sess, item=new_user) db_proc.commit_session(session=sess) except Exception as exp: return 'Failed to create user\nERROR : %s' % exp else: self.log.info('User %s was created' % self.user)
def reset_user(self): """Reset a user in the Tribble DB. :return: ``str`` """ try: sess = DB.session user_query = db_proc.get_user_id(self.user) if not user_query: return 'No User Found.' else: user_query.updated_at = datetime.datetime.utcnow() user_query.dcsecret = rosetta.encrypt(password=self.key, plaintext=self.psw) db_proc.commit_session(session=sess) except Exception: self.log.error(traceback.format_exc()) else: self.log.warn('User %s was reset' % self.user)
def delete_schematic_resource(self): """Perform a Schematic delete. This will delete a provided Schematic as well as it's configuration management row. """ try: sess = DB.session config = db_proc.get_configmanager(skm=self.schematic) db_proc.delete_item(session=sess, item=self.schematic) db_proc.delete_item(session=sess, item=config) except AttributeError as exp: msg = 'Issues while removing Schematic => %s', exp LOG.info(msg) self.cell['zone_state'] = 'DELETE FAILED' self.cell['zone_msg'] = msg self.state_update() else: db_proc.commit_session(session=sess)
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 delete_schematic_resource(self): """Perform a Schematic delete. This will delete a provided Schematic as well as it's configuration management row. """ try: sess = DB.session config = db_proc.get_configmanager(skm=self.schematic) db_proc.delete_item(session=sess, item=self.schematic) db_proc.delete_item(session=sess, item=config) except AttributeError as exp: msg = 'Issues while removing Schematic => %s', exp LOG.info(msg) self.cell['zone_state'] = 'DELETE FAILED' self.cell['zone_msg'] = msg self.state_update() else: db_proc.commit_session(session=sess)
def create_user(self): """Create a user in the Tribble DB. :return: ``str`` """ try: sess = DB.session new_user = db_proc.post_user( admin=self.args.get('admin', 0), user=self.user, encrypted=rosetta.encrypt( password=self.key, plaintext=self.psw ) ) db_proc.add_item(session=sess, item=new_user) db_proc.commit_session(session=sess) except Exception as exp: return 'Failed to create user\nERROR : %s' % exp else: self.log.info('User %s was created' % self.user)
def delete_zone_resource(self): """Perform a zone resource delete. This will delete a zone as well as it's keys. """ try: instances = db_proc.get_instances(zon=self.zone) if instances: self.cell['zone_state'] = 'DELETE FAILED' self.cell['zone_msg'] = ( 'Found Instance when they should have all been deleted') self.state_update() else: sess = DB.session key = db_proc.get_instanceskeys(zon=self.zone) db_proc.delete_item(session=sess, item=self.zone) db_proc.delete_item(session=sess, item=key) db_proc.commit_session(session=sess) except AttributeError as exp: LOG.error('No Zone To Delete as No Zone was Found ==> %s', exp)
def _node_remove(self, ids): """Delete an instance from both the cloud provider and from the DB. :param ids: ``list`` """ try: sess = DB.session schematic = db_proc.get_schematic_id( sid=self.packet['schematic_id'], uid=self.packet['auth_id']) zone = db_proc.get_zones_by_id(skm=schematic, zid=self.packet['zone_id']) instances = db_proc.get_instance_ids(zon=zone, ids=ids) for instance in instances: db_proc.delete_item(session=sess, item=instance) except Exception as exp: self.zone_status.error(error_msg=exp) LOG.critical('Issues when Removing Instances %s' % exp) else: db_proc.commit_session(session=sess) self._remove_user_data()
def delete_zone_resource(self): """Perform a zone resource delete. This will delete a zone as well as it's keys. """ try: instances = db_proc.get_instances(zon=self.zone) if instances: self.cell['zone_state'] = 'DELETE FAILED' self.cell['zone_msg'] = ( 'Found Instance when they should have all been deleted' ) self.state_update() else: sess = DB.session key = db_proc.get_instanceskeys(zon=self.zone) db_proc.delete_item(session=sess, item=self.zone) db_proc.delete_item(session=sess, item=key) db_proc.commit_session(session=sess) except AttributeError as exp: LOG.error('No Zone To Delete as No Zone was Found ==> %s', exp)
def reset_user(self): """Reset a user in the Tribble DB. :return: ``str`` """ try: sess = DB.session user_query = db_proc.get_user_id(self.user) if not user_query: return 'No User Found.' else: user_query.updated_at = datetime.datetime.utcnow() user_query.dcsecret = rosetta.encrypt( password=self.key, plaintext=self.psw ) db_proc.commit_session(session=sess) except Exception: self.log.error(traceback.format_exc()) else: self.log.warn('User %s was reset' % self.user)
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 _node_remove(self, ids): """Delete an instance from both the cloud provider and from the DB. :param ids: ``list`` """ try: sess = DB.session schematic = db_proc.get_schematic_id( sid=self.packet['schematic_id'], uid=self.packet['auth_id'] ) zone = db_proc.get_zones_by_id( skm=schematic, zid=self.packet['zone_id'] ) instances = db_proc.get_instance_ids(zon=zone, ids=ids) for instance in instances: db_proc.delete_item(session=sess, item=instance) except Exception as exp: self.zone_status.error(error_msg=exp) LOG.critical('Issues when Removing Instances %s' % exp) else: db_proc.commit_session(session=sess) self._remove_user_data()
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)