def check_sample_api(s): instances_dir = create_instances_dir() config_txt, topology_json = load_config_file(s, dummy = True) api = API(instances_dir) (status_code, message, inst_id) = api.instance_create(topology_json, config_txt) assert status_code == API.STATUS_SUCCESS check_instance_state(api, inst_id, Topology.STATE_NEW) (status_code, message, topologies_json) = api.instance_list(None) assert status_code == API.STATUS_SUCCESS insts = json.loads(topologies_json) assert len(insts) == 1 assert insts[0]["id"] == inst_id (status_code, message, topologies_json) = api.instance_list([inst_id]) assert status_code == API.STATUS_SUCCESS, message insts = json.loads(topologies_json) assert len(insts) == 1 assert insts[0]["id"] == inst_id (status_code, message) = api.instance_start(inst_id, [], []) assert status_code == API.STATUS_SUCCESS, message check_instance_state(api, inst_id, Topology.STATE_RUNNING) (status_code, message) = api.instance_stop(inst_id) assert status_code == API.STATUS_SUCCESS, message check_instance_state(api, inst_id, Topology.STATE_STOPPED) (status_code, message) = api.instance_start(inst_id, [], []) assert status_code == API.STATUS_SUCCESS, message check_instance_state(api, inst_id, Topology.STATE_RUNNING) (status_code, message) = api.instance_update(inst_id, None, [], []) assert status_code == API.STATUS_SUCCESS, message check_instance_state(api, inst_id, Topology.STATE_RUNNING) (status_code, message) = api.instance_terminate(inst_id) assert status_code == API.STATUS_SUCCESS, message check_instance_state(api, inst_id, Topology.STATE_TERMINATED) remove_instances_dir(instances_dir)
def run(self): t_start = time.time() self.parse_options() if len(self.args) != 2: print "You must specify an instance id." print "For example: %s [options] gpi-37a8bf17" % self.name return 1 inst_id = self.args[1] if self.opt.extra_files != None: self._check_exists_file(self.opt.extra_files) extra_files = parse_extra_files_files(self.opt.extra_files) else: extra_files = [] if self.opt.run != None: self._check_exists_file(self.opt.run) run_cmds = [l.strip() for l in open(self.opt.run).readlines()] else: run_cmds = [] print "Starting instance", print Fore.WHITE + Style.BRIGHT + inst_id + Fore.RESET + Style.RESET_ALL + "...", api = API(self.opt.dir) status_code, message = api.instance_start(inst_id, extra_files, run_cmds) if status_code == API.STATUS_SUCCESS: print Fore.GREEN + Style.BRIGHT + "done!" self._set_last_gpi(inst_id) t_end = time.time() delta = t_end - t_start minutes = int(delta / 60) seconds = int(delta - (minutes * 60)) print "Started instance in " + Fore.WHITE + Style.BRIGHT + "%i minutes and %s seconds" % (minutes, seconds) return 0 elif status_code == API.STATUS_FAIL: print self._print_error("Could not start instance.", message) return 1