def get_aggregate_filter(aggregate_filter): ''' Determining if the filter is by region or type from the string format ''' filter_region = "." not in aggregate_filter # Aggregate by instance region if filter_region: region = aggregate_filter aggregate_data = Instance.objects( records__timestamp__gte=DEFAULT_AGGREGATE_TIMESPAN, instance_zone__contains=region) result = region_response_from_data(aggregate_data) return jsonify(result) # Aggregate by instance type else: instance_type = aggregate_filter aggregate_data = Instance.objects( records__timestamp__gte=DEFAULT_AGGREGATE_TIMESPAN, instance_type__contains=instance_type) result = type_response_from_data(aggregate_data) return jsonify(result)
def main(): print "Querying AWS" session = boto3.Session() # will be created with EC2 role timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') inventory_manager = InventoryManager(session) inventory = inventory_manager.list_inventory() for item in inventory: # Using \t as a delimiter in case of awk print (timestamp + "\t" + item["name"] + "\t" + item["type"] + "\t" + item["zone"]) if USE_DB: instance = None # Retrieving the instance if it exists try: instance = Instance.objects.get(instance_id = item["id"]) # Creating the instance if it doesn't except DoesNotExist: instance = Instance( instance_id = item["id"], instance_name = item["name"], instance_type = item["type"], instance_zone = item["zone"], instance_account = item["account"] ) instance.records.append(InstanceRecord( timestamp = timestamp, instance_state = item["state"] )) instance.save()
def test_instance_duplicate(self): instance = Instance( instance_id = "TestID", instance_name = "TestName", instance_type = "TestType", instance_zone = "TestZone", instance_account = "TestAccount" ) instance.save()
def create(self, request): instance = Instance( name=request['name'], description=request['description'], type_=request['type'], identifier_mode=request['identifierMode'], save=request['save'] ) instance.save() return jsonify(instance.to_json())
def setUpClass(self): ''' Creating a test client for Flask ''' self.app = app.test_client() self.app.testing = True # Clearing test DB Instance.drop_collection() ''' Populating database with random test data ''' MAX_INSTANCES = 60 self.zones = ["eu-west-1a", "eu-west-1b", "eu-west-1c", "eu-west-2a"] self.types = ["t2.micro", "t2.nano", "m4.xlarge"] to_create = {} # Creating random information from sample regions and types for index in range(0, MAX_INSTANCES): random_zone = self.zones[random.randint(0, len(self.zones) - 1)] random_type = self.types[random.randint(0, len(self.types) - 1)] if random_zone not in to_create: to_create[random_zone] = {} if random_type not in to_create[random_zone]: to_create[random_zone][random_type] = 1 else: to_create[random_zone][random_type] += 1 # Writing instance records to database based on random information instance_id_counter = 0 for zone in to_create: for itype in to_create[zone]: for count in range(0, to_create[zone][itype]): instance = Instance(instance_type=itype, instance_id="i-" + str(instance_id_counter), instance_zone=zone, instance_account="testaccount") instance.save() instance_id_counter += 1 # Spreading instance records across 12 months self.timestamps = [] for i in range(-11, 1): self.timestamps.append(monthdelta(LOAD_TIME, i)) # Going through all instances and creating records between two # randomly-selected timestamps for instance in Instance.objects(): start = random.randint(0, len(self.timestamps) - 2) end = random.randint(start + 1, len(self.timestamps) - 1) for timestamp_index in range(start, end + 1): instance.records.append( InstanceRecord(timestamp=self.timestamps[timestamp_index], instance_state="RUNNING")) instance.save()
def update(self, request): instance = Instance( id_=request['id'], name=request['name'], description=request['description'], type_=request['type'], identifier_mode=request['identifierMode'], save=request['save'], client_model=request['clientModel'], server_model=request['serverModel'] ) Instance.update(instance) return jsonify(instance.to_json())
def test_aggregate_type(self): for instance_type in self.types: ''' Calculating expected result ''' results = Instance.objects( instance_type__contains=instance_type, records__timestamp__gte=(LOAD_TIME - timedelta(days=1))) expected = {str(instance_type): {}} for instance in results: instance_region = instance["instance_zone"][:-1] if instance_region not in expected[instance_type]: expected[instance["instance_type"]][instance_region] = 1 else: expected[instance["instance_type"]][instance_region] += 1 ''' Making call and validating output ''' raw_response = self.app.get('/aggregate/' + instance_type) response = json.loads(raw_response.data) # Region must be present in response self.assertIn(instance_type, response) for region in expected[instance_type]: # Instance type must be present in expected region self.assertIn(region, response[instance_type]) # Counts must match self.assertEquals(expected[instance_type][region], response[instance_type][region])
def delete(self, id_): model_type = request.form['model'] instance = Instance.get_by_id(id_) folder = folder = f'{PATH}/{model_type}' if instance: if model_type == 'client' and instance.client_model: self.delete_folder(folder, instance.name) instance.client_model = False elif model_type == 'server' and instance.server_model: self.delete_folder(folder, instance.name) instance.server_model = False Instance.update(instance) return jsonify({ "error": False, "message": "Model Deleted"})
def login(): if 'user' in session: return redirect(url_for('dashboard')) if request.method == 'GET': return render_template('login.html') if not 'username' in request.form or not 'password' in request.form: return render_template('login.html', error='Could not log you in') username = request.form['username'] password = request.form['password'] stuff = requests.put(CTFD_URL + '/kws_login', data={ 'username': username, 'password': password, }).json() if not stuff['ok']: return render_template('login.html', error='Could not log you in') u = User.query.filter_by(name=request.form['username']).first() if not u: u = User(username, password) db.session.add(u) db.session.commit() db.session.refresh(u) i = Instance('Instance', stuff['ip'], SHARED_SECRET, u) db.session.add(i) db.session.commit() session['user'] = u.id return redirect(url_for('dashboard'))
def test_aggregate(self): ''' Calculating expected result ''' results = Instance.objects(records__timestamp__gte=(LOAD_TIME - timedelta(days=1))) expected = {} for instance in results: # We have zone but we want region instance_region = instance["instance_zone"][:-1] if instance_region not in expected: expected[instance_region] = {} if instance["instance_type"] not in expected[instance_region]: expected[instance_region][instance["instance_type"]] = 1 else: expected[instance_region][instance["instance_type"]] += 1 ''' Making call and validating output ''' raw_response = self.app.get('/aggregate') response = json.loads(raw_response.data) for expected_region in expected: # Region must be present in response self.assertIn(expected_region, response) for itype in expected[expected_region]: # Instance type must be present in expected region self.assertIn(itype, response[expected_region]) # Counts must match self.assertEquals(expected[expected_region][itype], response[expected_region][itype])
def post(self): #get data from request. data = json.loads(self.request.body) lab_id = data['lab_id'] target_function = data['target_function'] target_instances = data['target_instances'] #create lab key lab_key = ndb.Key('Lab', int(lab_id)) #get lab entity from datastore lab = lab_key.get() gce_project = gce.GceProject(oauth_decorator.credentials, project_id=lab.project_id, zone_name=lab.lab_zone) if target_instances == "ALL": instances = Instance.query(Instance.lab == lab_key).fetch() else: instances = [] for i in target_instances: try: instances.append(ndb.Key('Instance', int(i)).get()) except: logging.debug("Key error?") if target_function == 'START': start_instances(gce_project, lab, instances) elif target_function == 'STOP': stop_instances(gce_project, lab, instances) elif target_function == "DELETE": delete_instances(gce_project, lab, instances)
def get(self): args = request.headers.get('userInfo') aws_keys = json.loads(args) instances = get_instances(aws_keys['aws_access_key_id'], aws_keys['aws_secret_access_key']) for i in instances: instance_id = i.get('instance_id') availability_zone = i.get('availability_zone') instance_state = i.get('instance_state') try: with DATABASE.atomic(): Instance.create(instance_id=instance_id) except IntegrityError: pass return jsonify({"data": instances})
def create_instances(cloudconfig, lab_id, lab_slice, topo, create_sec_group_job_id): try: openstack = Openstack(cloudconfig.detail['openstackAuthURL'], cloudconfig.detail['openstackProject'], cloudconfig.detail['openstackUser'], cloudconfig.detail['openstackPassword']) sec_group_id = queue.fetch_job(create_sec_group_job_id).result # Prepare and save the instance models for s in topo['instances']: links = _extract_links(s, topo) configurations, password = _extract_configurations( lab_id, lab_slice, s, topo) Instance.insert(name=s['name'], status='deploying', x=s['x'], y=s['y'], gid=s['gid'], slice_id=lab_slice.id, image=s['image'], flavor=s['flavor'], links=links, configurations=configurations, password=password) # Actually deployment threads = [] for instance in Instance.fetchall(slice_id=lab_slice.id): t = CreateInstanceThread(openstack, lab_id, instance, lab_slice, sec_group_id) t.start() threads.append(t) for t in threads: t.join() except Exception as ex: error_type = 'Create instances error' error_msgs = [error_type + ': ' + str(ex)] lab = Lab.fetchone(id=lab_id) lab.update(status='deployfailed', error_msgs=lab.error_msgs + error_msgs) raise Exception( error_type ) # Raise exception to not execute the next job in the dependency link
def update_allowed_address_pairs(cloudconfig, lab_id, lab_slice, topo): try: openstack = Openstack(cloudconfig.detail['openstackAuthURL'], cloudconfig.detail['openstackProject'], cloudconfig.detail['openstackUser'], cloudconfig.detail['openstackPassword']) mac_regex = '^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$' ip_cidr_regex = '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4]' \ '[0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-' \ 'f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-' \ '5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5' \ ']|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}((' \ '(:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0' \ '-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]' \ '{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0' \ '-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d' \ '|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1' \ ',4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d' \ '|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1' \ '\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(\/[0-9]{1,2})*$' for link in topo['links']: address_pairs = [] network = NetworkNode.fetchone(gid=link['network']['gid'], slice_id=lab_slice.id) if link['target']['type'].lower() == 'instance': device = Instance.fetchone(gid=link['target']['gid'], slice_id=lab_slice.id) elif link['target']['type'].lower() == 'router': device = Router.fetchone(gid=link['target']['gid'], slice_id=lab_slice.id) else: continue for raw_address_pair in link.get('allowedAddressPairs', []): mac_address, ip_address, _ = (raw_address_pair + ',,').split( ',', 2) if ip_address.strip() == '': ip_address = mac_address mac_address = '' if (mac_address == '' or re.match( mac_regex, mac_address.strip())) and re.match( ip_cidr_regex, ip_address.strip()): address_pair = {'ip_address': ip_address} if mac_address != '': address_pair['mac_address'] = mac_address address_pairs.append(address_pair) if address_pairs: openstack.update_allowed_address_pairs( network, device.cloud_attrs['id'], address_pairs) except Exception as ex: error_type = 'Update allowed address pairs error' error_msgs = [error_type + ': ' + str(ex)] lab = Lab.fetchone(id=lab_id) lab.update(status='deployfailed', error_msgs=lab.error_msgs + error_msgs) raise Exception( error_type ) # Raise exception to not execute the next job in the dependency link
def test_instance(self): instance = Instance( instance_id = "TestID", instance_name = "TestName", instance_type = "TestType", instance_zone = "TestZone", instance_account = "TestAccount" ) instance.save() query_result = Instance.objects(instance_id = "TestID") # Only one item should have been inserted self.assertEquals(1, query_result.count()) # The only item should be identical to the one we've inserted self.assertEquals(instance, query_result[0])
def create_instance(self, service_name, instance_id, image_name, environment, hostname, command, volumes): instance_name = service_name + '_{}'.format(instance_id) logger.info('Create a docker instance: {}'.format(instance_name)) # cmd_data = ["nginx", # "-g", # "daemon off;"] # labels_data = { # "com.example.vendor": "Acme", # "com.example.license": "GPL", # "com.example.version": "1.0"} # HostConfig = {"NetworkMode": "bridge"} try: print image_name, command, instance_name, hostname, volumes r = swarm_client.create_container( image=image_name, command=command, name=instance_name, # environment=environment, hostname=hostname, volumes=volumes, ) except Exception as ex: logger.error("Error{}".format(ex)) return None, ex # service = Service.objects.get(service_name=service_name) container_id = r.get('Id') # created_at = datetime_to_timestamp(timezone.now()) created_at = (timezone.now()) try: """ create agent info in db. """ a = swarm_client.inspect_container(container_id) node_name = a.get('Node').get('Name') node_ip = a.get('Node').get('IP') agent_obj = service_DBclient.create_agent_info(node_name, node_ip) except Exception as ex: agent_obj = None logger.debug('Did not get agent info:{}'.format(ex)) b = Instance(name=instance_name, created_at=created_at, instance_id=instance_id, continer_id=container_id, command=command, hostname=hostname, volumes=volumes, environment=environment, host=agent_obj) # service = service, # host='hostname' # b.save() # container_id = json.loads(r.text).get('Id') logger.info('Create docker continer :{}'.format(instance_name)) return b, container_id
def get_aggregate(): # Retrieving records for all instances in the last day aggregate_data = Instance.objects( records__timestamp__gte=DEFAULT_AGGREGATE_TIMESPAN) result = region_response_from_data(aggregate_data) return jsonify(result)
def get_by_ip(self, ip): print('Searching for device with IP: ' + ip) device = Device.get_by_ip(ip) if device: instance = Instance.get_by_id(device.instance_id) return jsonify({ "device" : device.to_json(), "instance": instance.to_json() if instance else None }) return jsonify({ "error": "True", "message": "Device IP not registered"})
def put_instance(instance): i = Instance.query.filter_by(ip=instance['ip']).first() if i is not None: app.logger.info('Updating instance with ip %s ..', instance['ip']) instance['timestamp'] = datetime.datetime.now() i.update(instance) else: app.logger.info('Creating instance with ip %s ..', instance['ip']) db.session.add(Instance(**instance)) db.session.commit() return NoContent, (200 if i is not None else 201)
def handle_instance(): if request.method == 'POST': if request.is_json: data = request.get_json() print("--------------------------------------", file=sys.stderr) print(data, file=sys.stderr) new_instance = Instance(name=data['name'], ip=data['ip'], fqdn=data['fqdn'], user=data['user'], password=data['password'], ssh_key=data['ssh_key']) db.session.add(new_instance) db.session.commit() return {"message": f"Instance {new_instance.name} has been created successfully."} else: return {"error": "The request payload is not in JSON format"}
def save(self, id_, request): try: file = request.files['file'] model_type = file.filename instance = Instance.get_by_id(id_) model_name = instance.name folder = f'{PATH}/{model_type}' self.delete_folder(folder, model_name) self.unzip_files(file, folder, model_name) if model_type == 'client': instance.client_model = True elif model_type == 'server': instance.server_model = True TFModel.load_model_v2(model_name, folder) Instance.update(instance) return jsonify({ "error": False, "message": "Model Saved"}) except Exception as e: return jsonify({ "error": True, "message": e})
def getInstanceInfo(uri): print("Getting information on {}".format(uri)) instancedata = getjson(uri, 'instance') instance = Instance.query.filter_by(uri=uri).first() if instance is None: instance = Instance(pending=True) print("Adding instance to DB") db.session.add(instance) for k, v in instancedata.items(): setattr(instance, k, v) db.session.commit() return instance
def get_instance(session_key): ''' Return instance for session_key ''' logger = create_instance.get_logger() logger.info('Find heroku instance for session_key: %s' % session_key) try: inst = Instance.objects.get(session_key=session_key) app = cloud.apps.get(inst.app) if not app: inst.delete() raise Instance.DoesNotExist() except Instance.DoesNotExist: inst = create_instance(session_key) return inst
def delete(self, id_): instance = Instance.get_by_id(id_) if instance.id_ == 1: logging.info('Cannot delete Instance ID = 1') return jsonify({"error": True, "message": f'Instance ID 1 cannot be deleted'}) devices = Device.get_by_instance_id(id_) for d in devices: d.instance_id = 1 d.update(d) if instance: instance.delete(id_) # Deletar as pastas dos modelos relacionadas com o nome return ('Instance ID ' + str(id_) + ' Deleted') else: return jsonify({"error": True, "message": f'Instance ID {id_} Not Found'}) return 'Not Found'
def test_aggregate_region(self): ''' Extracting regions from zones ''' regions = [] for zone in self.zones: if zone[:-1] not in regions: regions.append(zone[:-1]) ''' Testing against all regions ''' for region in regions: ''' Calculating expected result ''' results = Instance.objects( instance_zone__contains=region, records__timestamp__gte=(LOAD_TIME - timedelta(days=1))) expected = {str(region): {}} for instance in results: if instance["instance_type"] not in expected[region]: expected[region][instance["instance_type"]] = 1 else: expected[region][instance["instance_type"]] += 1 ''' Making call and validating output ''' raw_response = self.app.get('/aggregate/' + region) response = json.loads(raw_response.data) # Region must be present in response self.assertIn(region, response) for itype in expected[region]: # Instance type must be present in expected region self.assertIn(itype, response[region]) # Counts must match self.assertEquals(expected[region][itype], response[region][itype])
def delete_instances(cloudconfig, lab_id, lab_slice): try: openstack = Openstack(cloudconfig.detail['openstackAuthURL'], cloudconfig.detail['openstackProject'], cloudconfig.detail['openstackUser'], cloudconfig.detail['openstackPassword']) instances = Instance.fetchall(slice_id=lab_slice.id) threads = [] for instance in instances: t = DeleteInstanceThread(openstack, lab_id, instance) t.start() threads.append(t) for t in threads: t.join() except Exception as ex: error_type = 'Delete instances error' error_msgs = [error_type + ': ' + str(ex)] lab = Lab.fetchone(id=lab_id) lab.update(status='destroyfailed', error_msgs=lab.error_msgs + error_msgs) raise Exception( error_type ) # Raise exception to not execute the next job in the dependency link
def post(self): #get data from request. data = json.loads(self.request.body) lab_id = data['lab_id'] #create lab key lab_key = ndb.Key('Lab', int(lab_id)) #get lab entity from datastore lab = lab_key.get() project_id = lab.project_id lab_zone = lab.lab_zone #get expected instances from datastore query = Instance.query(Instance.lab == lab_key).fetch() #get active instances for project, add to dictionary gce_project = gce.GceProject(oauth_decorator.credentials, project_id=project_id, zone_name=lab_zone) instances = gce_appengine.GceAppEngine().run_gce_request(self, gce_project.list_instances, 'Error listing instances: ', # filter='name eq ^%s.*' % 'test-instance', maxResults='500') status_dict = {} ip_dict = {} for instance in instances: status_dict[instance.name] = instance.status if instance.status == 'RUNNING': ip_dict[instance.name] = instance.network_interfaces[0][u'accessConfigs'][0]['natIP'] logging.debug(status_dict) #compare expected instances with active instances and produce list for passing to client side js function instance_list = [] for n in range(len(query)): instance_name = query[n].name pass_phrase = memcache.get(instance_name) if pass_phrase is not None: logging.debug("Did read from memcache!") #TODO desired_state = query[n].desired_state if desired_state == "RUNNING": if instance_name in status_dict: instance_state = status_dict[query[n].name] elif (datetime.now() - query[n].request_timestamp) < timedelta(seconds=120): instance_state = "REQUEST PENDING" else: instance_state = "ERROR STARTING INSTANCE" ## could call start function here elif desired_state == "TERMINATED": if instance_name not in status_dict: instance_state = "TERMINATED" elif status_dict[query[n].name] == "STOPPING": instance_state = "STOPPING" elif (datetime.now() - query[n].request_timestamp) < timedelta(seconds=120): instance_state = "REQUEST PENDING" else: instance_state = "ERROR STOPPING INSTANCE" ## could call stop function here # if instance_name in status_dict: # instance_state = status_dict[query[n].name] # else: # instance_state = "TERMINATED" ip_address = 'Not assigned' if instance_state == 'RUNNING': ip_address = ip_dict[instance.name] instance_list.append({"address": ip_address, "id": query[n].key.id(), "state": instance_state, "name": instance_name, "pass": pass_phrase}) self.response.out.write(json.dumps(instance_list))
def post(self): #get data from form. lab_name = self.request.get('lab-name') project_id = self.request.get('project-id') lab_zone = self.request.get('lab-zone') instance_image = self.request.get('instance-image') machine_type = self.request.get('machine-type') number_students = int(self.request.get('total-students')) #get credentials stored in datastore user_id = users.get_current_user().user_id() credentials = oauth2client.StorageByKeyName( oauth2client.CredentialsModel, user_id, 'credentials').get() #configure project object gce_project = gce.GceProject(credentials, project_id=project_id, zone_name=lab_zone) #create lab entity in datastore lab = Lab(name=lab_name, project_id=project_id, lab_zone=lab_zone, machine_type=machine_type, instance_image=instance_image) lab.put() #create instance objects instances = [] for n in range(number_students): # set the username to the name of the instance username = '******' % (lab_name, n) pass_phrase = security.generate_random_string(length=8) if not memcache.add(username, pass_phrase): logging.error('Failed to set memcache') metadata_items = [ { 'key': 'user', 'value': username }, { 'key': 'pass', 'value': pass_phrase }, { 'key': 'startup-script-url', 'value': 'gs://startup-scripts-compute/startup.sh' } ] instance = Instance(name="%s-%s" % (lab_name, n), lab=lab.key, metadata=metadata_items, desired_state="RUNNING", request_timestamp=datetime.now()) instance.put() instances.append(instance) start_instances(gce_project, lab, instances) self.redirect('/lab/%s' % lab.key.id())
def test_required(self): # ID is mandatory instance = Instance( instance_name = "TestName", instance_type = "TestType", instance_zone = "TestZone", instance_account = "TestAccount" ) with self.assertRaises(ValidationError) as ve: instance.save() # Type is mandatory instance = Instance( instance_id = "TestID", instance_name = "TestName", instance_zone = "TestZone", instance_account = "TestAccount" ) with self.assertRaises(ValidationError) as ve: instance.save() # Zone is mandatory instance = Instance( instance_id = "TestID", instance_name = "TestName", instance_type = "TestType", instance_account = "TestAccount" ) with self.assertRaises(ValidationError) as ve: instance.save() # Account is mandatory instance = Instance( instance_id = "TestID", instance_name = "TestName", instance_type = "TestType", instance_zone = "TestZone" ) with self.assertRaises(ValidationError) as ve: instance.save() # Name is not mandatory instance = Instance( instance_id = "TestID", instance_type = "TestType", instance_zone = "TestZone", instance_account = "TestAccount" ) instance.save()
def get(self): instances = Instance.get() response = [] for i in instances: response.append(i.to_json()) return jsonify(response)
def get_by_id(self, id_): instance = Instance.get_by_id(id_) if instance: return jsonify(instance.to_json()) return jsonify({"error": True, "message": 'Instance not found'})
def load_instance(instance_name): ins = Instance.select().where(Instance.name == instance_name) return ins.first()
def save_model(self, filename): model = os.path.join(InstanceController.MODEL_PATH, filename) self.algo_instance.save(model) logger.info("Model saved") return model if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("name", help="Name of instance") parser.add_argument("model_path", help="Command: stop, serve, train") parser.add_argument("command", help="Command: stop, serve, train") args = parser.parse_args() if (args.command == "serve"): i = Instance.select().where(Instance.name == args.name).get() ins = InstanceController(json.loads(i.config)) ins.start() ins.load_model(args.model_path) logger.info("Model: " + args.model_path) ins.serve() if (args.command == "train"): i = Instance.select().where(Instance.name == args.name).get() ins = InstanceController(json.loads(i.config)) ins.start() viz = ins.train() path = ins.save_model(args.model_path) m = TrainedModel(instance_id=i.id, path=path, visualization=viz) m.save() i.state = "TRAINED" i.pid = 0