def create_new_instance(self, topology_json, config_txt): created = False while not created: inst_id = "gpi-" + hex(random.randint(1,2**31-1))[2:].rjust(8,"0") inst_dir = "%s/%s" % (self.instances_dir, inst_id) if not os.path.exists(inst_dir): os.makedirs(inst_dir) created = True configf = open("%s/provision.conf" % inst_dir, "w") configf.write(config_txt) configf.close() # We don't do anything with it. Just use it to raise an exception # if there is anything wrong with the configuration file GPConfig("%s/provision.conf" % inst_dir) topology = Topology.from_json_string(topology_json) topology.set_property("id", inst_id) topology.set_property("state", Topology.STATE_NEW) topology.save("%s/topology.json" % inst_dir) inst = Instance(inst_id, inst_dir) return inst
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] api = API(self.opt.dir) (status_code, message, topology_json) = api.instance(inst_id) if status_code != API.STATUS_SUCCESS: self._print_error("Could not access instance.", message) return 1 else: t = Topology.from_json_string(topology_json) if not t.domains.has_key(self.opt.domain): self._print_error("Could not add user", "Domain '%s' does not exist" % self.opt.domain) return 1 domain = t.domains[self.opt.domain] user = User() user.set_property("id", self.opt.login) user.set_property("password_hash", self.opt.passwd) user.set_property("ssh_pkey", self.opt.ssh) if self.opt.admin != None: user.set_property("admin", self.opt.admin) else: user.set_property("admin", False) user.set_property("certificate", self.opt.certificate) domain.add_to_array("users", user) topology_json = t.to_json_string() print "Adding new user to", print Fore.WHITE + Style.BRIGHT + inst_id + Fore.RESET + Style.RESET_ALL + "...", status_code, message = api.instance_update(inst_id, topology_json, [], []) 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 "Added user in " + Fore.WHITE + Style.BRIGHT + "%i minutes and %s seconds" % (minutes, seconds) return 0 elif status_code == API.STATUS_FAIL: self._print_error("Could not update topology.", message) return 1
def update_topology(self, topology_json): try: topology_file = "%s/topology.json" % self.instance_dir new_topology = Topology.from_json_string(topology_json) new_topology._json_file = topology_file except ObjectValidationException, ove: message = "Error in topology file: %s" % ove return (False, message, None)
def __load_topology(self): topology_file = "%s/topology.json" % self.instance_dir f = open (topology_file, "r") json_string = f.read() topology = Topology.from_json_string(json_string) topology._json_file = topology_file f.close() return topology
def run(self): SIGINTWatcher(self.cleanup_after_kill) t_start = time.time() self.parse_options() if len(self.args) <= 2: print "You must specify an instance id and at least one host." print "For example: %s [options] gpi-37a8bf17 simple-wn3" % self.name exit(1) inst_id = self.args[1] hosts = self.args[2:] api = API(self.opt.dir) (status_code, message, topology_json) = api.instance(inst_id) if status_code != API.STATUS_SUCCESS: self._print_error("Could not access instance.", message) exit(1) else: t = Topology.from_json_string(topology_json) if not t.domains.has_key(self.opt.domain): self._print_error("Could not remove hosts", "Domain '%s' does not exist" % self.opt.domain) exit(1) removed = [] nodes = t.domains[self.opt.domain].nodes for host in hosts: if host in nodes: nodes.pop(host) removed.append(host) remaining = set(removed) ^ set(hosts) for r in remaining: print Fore.YELLOW + Style.BRIGHT + "Warning" + Fore.RESET + Style.RESET_ALL + ":", print "Host %s does not exist." % r topology_json = t.to_json_string() if len(removed) > 0: print "Removing hosts %s from" % list(removed), print Fore.WHITE + Style.BRIGHT + inst_id + Fore.RESET + Style.RESET_ALL + "...", status_code, message = api.instance_update(inst_id, topology_json, [], []) if status_code == API.STATUS_SUCCESS: print Fore.GREEN + Style.BRIGHT + "done!" t_end = time.time() delta = t_end - t_start minutes = int(delta / 60) seconds = int(delta - (minutes * 60)) print "Removed hosts in " + Fore.WHITE + Style.BRIGHT + "%i minutes and %s seconds" % (minutes, seconds) elif status_code == API.STATUS_FAIL: self._print_error("Could not update topology.", message) exit(1)
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] api = API(self.opt.dir) (status_code, message, topology_json) = api.instance(inst_id) if status_code != API.STATUS_SUCCESS: self._print_error("Could not access instance.", message) return 1 else: t = Topology.from_json_string(topology_json) if not t.domains.has_key(self.opt.domain): self._print_error("Could not add host", "Domain '%s' does not exist" % self.opt.domain) return 1 domain = t.domains[self.opt.domain] node = Node() node.set_property("id", self.opt.id) node.set_property("run_list", self.opt.runlist.split(",")) if self.opt.depends != None: node.add_to_array("depends", "node:%s" % self.opt.depends) domain.add_to_array("nodes", (node)) topology_json = t.to_json_string() print "Adding new host to", print Fore.WHITE + Style.BRIGHT + inst_id + Fore.RESET + Style.RESET_ALL + "...", status_code, message = api.instance_update(inst_id, topology_json, [], []) 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 "Added host in " + Fore.WHITE + Style.BRIGHT + "%i minutes and %s seconds" % (minutes, seconds) return 0 elif status_code == API.STATUS_FAIL: self._print_error("Could not update topology.", message) return 1
def check_instance_state(api, inst_id, state): (status_code, message, topology_json) = api.instance(inst_id) assert status_code == API.STATUS_SUCCESS, message topology = Topology.from_json_string(topology_json) assert topology.state == state
def run(self): self.parse_options() if len(self.args) != 2: print "You must specify an instance id." print "For example: %s [options] gpi-37a8bf17" % self.name exit(1) inst_id = self.args[1] api = API(self.opt.dir) (status_code, message, topology_json) = api.instance(inst_id) if status_code != API.STATUS_SUCCESS: self._print_error("Could not access instance.", message) exit(1) else: if self.opt.verbose or self.opt.debug: print topology_json else: topology = Topology.from_json_string(topology_json) reset = Fore.RESET + Style.RESET_ALL print Fore.WHITE + Style.BRIGHT + inst_id + reset + ": " + self.__colorize_topology_state(topology.state) print for domain in topology.domains.values(): print "Domain " + Fore.CYAN + "'%s'" % domain.id # Find "column" widths and get values while we're at it node_width = state_width = hostname_width = ip_width = 0 rows = [] for node in domain.get_nodes(): if len(node.id) > node_width: node_width = len(node.id) if node.has_property("state"): state = node.state else: state = Node.STATE_NEW state_str = Node.state_str[state] if len(state_str) > state_width: state_width = len(state_str) if node.has_property("hostname"): hostname = node.hostname else: hostname = "" if len(hostname) > hostname_width: hostname_width = len(hostname) if node.has_property("ip"): ip = node.ip else: ip = "" if len(ip) > ip_width: ip_width = len(ip) rows.append((node.id, state, state_str, hostname, ip)) for (node_id, state, state_str, hostname, ip) in rows: node_id_pad = self.__pad(node_id, Fore.WHITE + Style.BRIGHT + node_id + Fore.RESET + Style.RESET_ALL, node_width + 2) state_pad = self.__pad(state_str, self.__colorize_node_state(state), state_width + 2) hostname_pad = self.__pad(hostname, "", hostname_width + 2) ip_pad = self.__pad(ip, "", ip_width) print " " + node_id_pad + state_pad + hostname_pad + ip_pad print
def run(self): t_start = time.time() self.parse_options() if len(self.args) <= 2: print "You must specify an instance id and at least one host." print "For example: %s [options] gpi-37a8bf17 simple-wn3" % self.name return 1 inst_id = self.args[1] users = self.args[2:] api = API(self.opt.dir) (status_code, message, topology_json) = api.instance(inst_id) if status_code != API.STATUS_SUCCESS: self._print_error("Could not access instance.", message) return 1 else: t = Topology.from_json_string(topology_json) if not t.domains.has_key(self.opt.domain): self._print_error("Could not remove users", "Domain '%s' does not exist" % self.opt.domain) return 1 removed = [] domain_users = t.domains[self.opt.domain].users for user in users: if user in domain_users: domain_users.pop(user) removed.append(user) remaining = set(removed) ^ set(users) for r in remaining: print Fore.YELLOW + Style.BRIGHT + "Warning" + Fore.RESET + Style.RESET_ALL + ":", print "User %s does not exist." % r topology_json = t.to_json_string() if len(removed) > 0: print "Removing users %s from" % list(removed), print Fore.WHITE + Style.BRIGHT + inst_id + Fore.RESET + Style.RESET_ALL + "...", status_code, message = api.instance_update(inst_id, topology_json, [], []) 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 "Removed users in " + Fore.WHITE + Style.BRIGHT + "%i minutes and %s seconds" % ( minutes, seconds, ) return 0 elif status_code == API.STATUS_FAIL: self._print_error("Could not update topology.", message) return 1