def update_input(self, array_name, params): self.logger.debug("~~~~ %s ~~~~" % inspect.stack()[0][3]) if len(params) > 0: self.logger.info("~" * 50) self.logger.info("Updating parameters for array: %s" % array_name) self.logger.info(" ~~~~ PARAMETERS ~~~~") for k, v in params.items(): self.logger.info("%s: %s" % (k, v)) self.logger.info("~" * 50) filters = {"nickname": array_name} array_id = self.get_id(filters) update_data = dict( map(lambda (key, value): ("server[parameters][" + str(key) + "]", value), params.items()) ) url = "/".join([self.base_url + "servers", str(array_id)]) http_code = ResumatorRightScale.make_request(self, url, "PUT", update_data) if http_code == 204: self.logger.info("%s: Updated inputs" % array_name) return True self.logger.error("HTTP CODE: %d: %s: Unable to update inputs!" % (http_code, array_name)) return False self.logger.error("No params sent") return False
def run_recipe(self, array_name, recipe, inputs={}): self.logger.debug("~~~~ %s ~~~~" % inspect.stack()[0][3]) filters = {"nickname": array_name} array_id = self.get_id(filters) url = "/".join([self.base_url + "server_arrays", str(array_id), "run_executable"]) post_data = {"recipe": recipe} post_data = dict(map(lambda (key, value): ("server[" + str(key) + "]", value), post_data.items())) if inputs: inputs = dict(map(lambda (key, value): ("server[parameters][" + str(key) + "]", value), inputs.items())) post_data = dict(post_data.items() + inputs.items()) self.logger.debug("post data: %s" % post_data) self.logger.info("Running recipe: %s on %s" % (recipe, array_name)) http_code = ResumatorRightScale.make_request(self, url, "POST", post_data) if http_code == 201: self.logger.info("Successfully executed %s on %s" % (recipe, array_name)) return True self.logger.error("HTTP CODE: %d: %s failed on %s" % (http_code, recipe, array_name)) return False
def run_rightscript(self, server_name, script_data, inputs={}): self.logger.debug("~~~~ %s ~~~~" % inspect.stack()[0][3]) filters = {'nickname': server_name} server_id = self.get_id(filters) url = '/'.join([self.base_url + 'servers', str(server_id), 'run_script']) rs = RightScript(self.account_id, self.username, self.password) post_data = {'right_script_href': rs.get_href(script_data)} post_data = dict(map(lambda (key, value): ("server[" + str(key) + "]", value), post_data.items())) if inputs: inputs = dict(map(lambda (key, value): ("server[parameters][" + str(key) + "]", value), inputs.items())) post_data = dict(post_data.items() + inputs.items()) self.logger.debug("post data: %s" % post_data) self.logger.info("Running rightscript: \"%s\" on %s" % (', '.join(["%s: %s" % (key, val) for (key, val) in script_data.items()]), server_name)) http_code = ResumatorRightScale.make_request(self, url, "POST", post_data) if http_code == 201: self.logger.info("Successfully executed %s on %s" % (script_data['name'], server_name)) return True self.logger.error("HTTP CODE: %d: %s failed on %s" % (http_code, script_data['name'], server_name)) return False
def get_deployments(self): http_code = ResumatorRightScale.make_request(self, self.url) content = json.loads(self.response_body) if http_code == 200 and content: return content self.logger.error("HTTP CODE: %d: Unable to get deployments" % http_code) return []
def get_settings(self, server_id): self.logger.debug("~~~~ %s ~~~~" % inspect.stack()[0][3]) # Ugh... you can't use servers.js here, you can only get back json by setting the format on the query string. url = '/'.join([self.base_url + 'servers', str(server_id), "settings?format=%s" % self.resp_format]) http_code = ResumatorRightScale.make_request(self, url) contents = json.loads(self.response_body) if http_code == 200 and contents: return contents self.logger.error("HTTP CODE: %d: Unable to pull settings for server id: %d" % (http_code, server_id)) return {}
def create(self, data): deployment_name = (data['nickname'] if 'nickname' in data else 'unknown') if len(data) > 0: data = dict(map(lambda (key, value): ("deployment[" + str(key) + "]", value), data.items())) http_code = ResumatorRightScale.make_request(self, self.url, "POST", data) if http_code == 201: self.logger.info("Created Deployment \"%s\"" % deployment_name) return True self.logger.error("HTTP CODE: %d: Unable to create deployment (%s)" % (http_code, deployment_name)) return False
def delete(self, array_name): self.logger.debug("~~~~ %s ~~~~" % inspect.stack()[0][3]) filters = {"nickname": array_name} array_id = self.get_id(filters) url = "/".join([self.base_url + "server_arrays", str(array_id)]) http_code = ResumatorRightScale.make_request(self, url, "DELETE") if http_code == 200: self.logger.info("Deleted Server Array: %s" % array_name) return True self.logger.error("HTTP CODE: %d: Unable to delete server array: %s" % (http_code, array_name)) return False
def terminate(self, server_name): self.logger.debug("~~~~ %s ~~~~" % inspect.stack()[0][3]) filters = {'nickname': server_name} server_id = self.get_id(filters) url = '/'.join([self.base_url + 'servers', str(server_id), 'stop']) http_code = ResumatorRightScale.make_request(self, url, "POST") if http_code == 201: self.logger.info("Terminated: %s" % server_name) return True self.logger.error("HTTP CODE: %d: Unable to terminate: %s" % (http_code, server_name)) return False
def terminate(self, array_name): self.logger.debug("~~~~ %s ~~~~" % inspect.stack()[0][3]) filters = {"nickname": array_name} array_id = self.get_id(filters) url = "/".join([self.base_url + "server_arrays", str(array_id), "terminate_all"]) http_code = ResumatorRightScale.make_request(self, url, "POST") if http_code == 201: self.logger.info("Terminated: %s" % array_name) return True self.logger.error("HTTP CODE: %d: Unable to terminate: %s" % (http_code, array_name)) return False
def launch(self, array_name): self.logger.debug("~~~~ %s ~~~~" % inspect.stack()[0][3]) filters = {"nickname": array_name} array_id = self.get_id(filters) url = "/".join([self.base_url + "server_arrays", str(array_id), "launch"]) self.logger.info("Launching Array: %s" % array_name) http_code = ResumatorRightScale.make_request(self, url, "POST") if http_code == 201: self.logger.info("%s launched" % array_name) return True self.logger.error("HTTP CODE: %d: Server Array launch failed for: %s" % (http_code, array_name)) return False
def get_instances(self, filters={}): self.logger.debug("~~~~ %s ~~~~" % inspect.stack()[0][3]) array_id = self.get_id(filters) url = "/".join([self.base_url + "server_arrays", str(array_id), "instances?format=%s" % self.resp_format]) http_code = ResumatorRightScale.make_request(self, url) contents = json.loads(self.response_body) if http_code == 200 and contents: return contents elif http_code == 200 and not contents: self.logger.warn("No instances returned for %s" % filters["nickname"]) return [] self.logger.error("HTTP CODE: %d: Unable to get instances." % http_code) return []
def launch(self, server_name): self.logger.debug("~~~~ %s ~~~~" % inspect.stack()[0][3]) filters = {'nickname': server_name} server_id = self.get_id(filters) url = '/'.join([self.base_url + 'servers', str(server_id), 'start']) self.logger.info("Launching Server: %s" % server_name) http_code = ResumatorRightScale.make_request(self, url, "POST") if http_code == 201: self.logger.info("%s launched" % server_name) return True self.logger.error("HTTP CODE: %d: Server launch failed for: %s" % (http_code, server_name)) return False
def get_details(self, filters): try: key_id = self.ssh_keys[filters['aws_key_name']] except KeyError: self.logger.error("Unable to get ssh key id by: %s" % filters) return False url = '/'.join([self.base_url + 'ec2_ssh_keys', str(key_id), "?format=%s" % self.resp_format]) http_code = ResumatorRightScale.make_request(self, url) contents = json.loads(self.response_body) if http_code == 200 and contents: return contents self.logger.error("HTTP CODE: %d: Unable to get details for ssh key: %s" % (http_code, filters['aws_key_name'])) return {}
def run_rightscript(self, array_name, script_data, inputs={}, all_servers=True): self.logger.debug("~~~~ %s ~~~~" % inspect.stack()[0][3]) filters = {"nickname": array_name} array_id = self.get_id(filters) url = "/".join( [ self.base_url + "server_arrays", str(array_id), ("run_script_on_all" if all_servers else "run_script") + "?format=%s" % self.resp_format, ] ) rs = RightScript(self.account_id, self.username, self.password) ## we could also do this with the ServerTemplate module and pass a name, but ## in this case I think it makes more sense to pass in the href because we want to send ## the server template that is being used by the array. server_template_href = script_data.pop("server_template_href", None) post_data = {"right_script_href": rs.get_href(script_data), "server_template_hrefs": server_template_href} post_data = dict(map(lambda (key, value): ("server_array[" + str(key) + "]", value), post_data.items())) if inputs: inputs = dict( map(lambda (key, value): ("server_array[parameters][" + str(key) + "]", value), inputs.items()) ) post_data = dict(post_data.items() + inputs.items()) self.logger.debug("post data: %s" % post_data) self.logger.info( 'Running rightscript: "%s" on %s' % (", ".join(["%s: %s" % (key, val) for (key, val) in script_data.items()]), array_name) ) http_code = ResumatorRightScale.make_request(self, url, "POST", post_data) if http_code == 201: self.logger.info("Successfully executed %s on %s" % (script_data["name"], array_name)) return True self.logger.error("HTTP CODE: %d: %s failed on %s" % (http_code, script_data["name"], array_name)) return False
def __array_activation(self, array_name, activate=True): self.logger.debug("~~~~ %s ~~~~" % inspect.stack()[0][3]) filters = {"nickname": array_name} array_id = self.get_id(filters) url = "/".join([self.base_url + "server_arrays", str(array_id)]) _act = "true" if activate else "false" _verb = "enable" if activate else "disable" _past = "Enabled" if activate else "Disabled" post_data = {"server_array[active]": _act} http_code = ResumatorRightScale.make_request(self, url, "PUT", post_data) if http_code == 204: self.logger.info("%s Server Array: %s" % (_past, array_name)) return True self.logger.error("Unable to %s %s" % (_verb, array_name)) return False
def create(self, server_data, cloud_id=1): self.logger.debug("~~~~ %s ~~~~" % inspect.stack()[0][3]) server_name = (server_data['nickname'] if 'nickname' in server_data else 'unknown') st = ServerTemplate(self.account_id, self.username, self.password) ssh = SshKey(self.account_id, self.username, self.password) template_data = server_data.pop('server_template', None) key_data = server_data.pop('ssh_key', None) security_groups = server_data.pop('security_groups', None) server_data['server_template_href'] = st.get_href(template_data) server_data['ec2_ssh_key_href'] = ssh.get_href(key_data) _ebs_optimized = ('Yes' if 'ebs_optimized' in server_data and (server_data['ebs_optimized'] == 1 or server_data['ebs_optimized'] == 'true') else 'No') self.logger.info("~" * 50) self.logger.info(" Creating Server: %s" % server_name) self.logger.info(" Server Template: %s" % ', '.join(["%s: %s" % (key, val) for (key, val) in template_data.items()])) self.logger.info(" Security Groups: %s" % ', '.join(security_groups)) self.logger.info(" Instance Type: %s" % server_data['instance_type']) #self.logger.info(" Pricing: %s" % server_data['pricing']) self.logger.info(" EBS Optimized: %s" % _ebs_optimized) self.logger.info(" SSH Key: %s" % key_data['aws_key_name']) self.logger.info(" Cloud Id: %d" % cloud_id) self.logger.info("~" * 50) server_data = dict(map(lambda (key, value): ("server[" + str(key) + "]", value), server_data.items())) if cloud_id != 1: self.logger.warn("Deviating from default cloud id of 1!") server_data['cloud_id'] = cloud_id self.logger.debug("Server post data: %s" % server_data) http_code = ResumatorRightScale.make_request(self, self.url, "POST", server_data) if http_code == 201: self.logger.info("Created server '%s'" % server_name) return self.add_security_groups(server_name, security_groups) self.logger.error("HTTP CODE: %d: Unable to create '%s'" % (http_code, server_name)) return False
def delete(self, deployment_name): filters = {'nickname': deployment_name} deployment_id = self.get_id(filters) if deployment_id == 0: self.logger.error("%s: Cannot delete deployment 0" % inspect.stack()[0][3]) return False ## Delete cannot have the .js on the end of deployments... url = '/'.join([self.base_url + 'deployments', str(deployment_id)]) http_code = ResumatorRightScale.make_request(self, url, "DELETE") if http_code == 200: self.logger.info("Deleted Deployment: %s" % deployment_name) return True self.logger.error("HTTP CODE: %d: Unable to delete deployment: %s" % (http_code, deployment_name)) return False
def __init__(self, account_id, username, password): ResumatorRightScale.__init__(self, account_id, username, password) self.url = self.base_url + "server_templates.%s" % self.resp_format
def __init__(self, account_id, username, password): ResumatorRightScale.__init__(self, account_id, username, password) self.url = self.base_url + "ec2_ssh_keys.%s" % self.resp_format self.ssh_keys = {} self.__set_keys()
def __init__(self, account_id, username, password): ResumatorRightScale.__init__(self, account_id, username, password) self.url = self.base_url + "ec2_security_groups.%s" % self.resp_format
def __init__(self, account_id, username, password): ResumatorRightScale.__init__(self, account_id, username, password) self.url = self.base_url + "deployments.%s" % self.resp_format
def create(self, server_data, parameters={}, cloud_id=1): self.logger.debug("~~~~ %s ~~~~" % inspect.stack()[0][3]) array_name = server_data["nickname"] if "nickname" in server_data else "unknown" st = ServerTemplate(self.account_id, self.username, self.password) ssh = SshKey(self.account_id, self.username, self.password) elasticity_data = server_data.pop("elasticity", None) security_groups = server_data.pop("security_groups", None) ssh_key_data = server_data.pop("ssh_key", None) template_data = server_data.pop("server_template", None) array_enabled = ( "Yes" if "active" in server_data and (server_data["active"] == "true" or server_data["active"] == "true") else "No" ) self.logger.info("~" * 50) self.logger.info("Creating Server Array: %s" % array_name) self.logger.info(" Enabled: %s" % array_enabled) self.logger.info( " Server Template: %s" % ", ".join(["%s: %s" % (key, val) for (key, val) in template_data.items()]) ) self.logger.info(" Array Type: %s" % server_data["array_type"]) self.logger.info(" Security Groups: %s" % ", ".join(security_groups)) self.logger.info(" Instance Type: %s" % server_data["instance_type"]) # self.logger.info(" Pricing: %s" % server_data['pricing']) self.logger.info(" SSH Key: %s" % ssh_key_data["aws_key_name"]) self.logger.info(" Cloud Id: %d" % cloud_id) self.logger.info( " Elasticity: %s" % ", ".join(["%s: %s" % (key, val) for (key, val) in elasticity_data.items()]) ) if parameters: self.logger.info(" ~~~~ PARAMETERS ~~~~") for k, v in parameters.items(): self.logger.info("%s: %s" % (k, v)) self.logger.info("~" * 50) server_data["server_template_href"] = st.get_href(template_data) server_data["ec2_ssh_key_href"] = ssh.get_href(ssh_key_data) server_data = dict(map(lambda (key, value): ("server_array[" + str(key) + "]", value), server_data.items())) elasticity_data = dict( map(lambda (key, value): ("server_array[elasticity][" + str(key) + "]", value), elasticity_data.items()) ) server_data = dict(server_data.items() + elasticity_data.items()) if parameters: param_data = dict( map(lambda (key, value): ("server_array[parameters][" + str(key) + "]", value), parameters.items()) ) server_data = dict(server_data.items() + param_data.items()) if cloud_id != 1: self.logger.warn("Deviating from default cloud id of 1!") server_data["cloud_id"] = cloud_id self.logger.debug("Server array post data: %s" % server_data) http_code = ResumatorRightScale.make_request(self, self.url, "POST", server_data) if http_code == 201: self.logger.info("Created server array '%s'" % array_name) return self.add_security_groups(array_name, security_groups) self.logger.error("HTTP CODE: %d: Unable to create '%s'" % (http_code, array_name)) return False
def __init__(self, account_id, username, password): ResumatorRightScale.__init__(self, account_id, username, password) self.url = self.base_url + "ec2_ebs_volumes.%s" % self.resp_format