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 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))