def add_product(self, product_name, product_description, attributes, metadatas): """ Add a prodduct to the SDC @param product_name: the name of the product @param product_description: the description of the product @param attributes: the attributes of thw product @param metadatas: the metadata of teh product @return: (None if all OK / an error on failure), (The product / None of error) """ my_url = "%s/%s" % (self.sdc_url, "catalog/product") loggers.set_info_log(my_url) product = productrelease.Product(product_name, product_description) if attributes is not None: attributes_list = productrelease.process_attributes(attributes) for att in attributes_list: product.add_attribute(att) metadatas_list = productrelease.process_attributes(metadatas) for meta in metadatas_list: product.add_metadata(meta) payload = product.to_product_xml() print(EL.tostring(payload)) print(my_url) response = http.post(my_url, self.header2, EL.tostring(payload)) if response.status() is not 200: error = 'error to add the product sdc ' + str(response.status()) loggers.set_error_log(error) return error, None else: self.products.append(product) return None, product
def add_node_run_list(self, software): """ add the software to install into the list of the node @param software: The software @return: None if all OK or an error on failure """ puppet_master_url = models.Data.objects.get(key="puppet_master_url") software_name = software[0] version = software[1] uri = '%s/%s/%s/%s/%s/%s' % (puppet_master_url, 'install', self.tenant, self.name, software_name, version) loggers.set_info_log(uri) response = http.post(uri, self.headers, None) if response.status() != 200: msg = "Error adding the software to puppet" loggers.set_error_log(str(response.status) + ": " + msg + ": " + response.read()) return "Error" uri = '%s/%s/%s' % (puppet_master_url, 'generate', self.name) loggers.set_info_log(uri) response = http.post(uri, self.headers, "") if response.status() != 200: msg = "Error adding the software to puppet" loggers.set_error_log(str(response.status) + ": " + msg + ": " + response.read()) return "Error" return None
def add_floating_ip(self, vm_id): """ Add a floating IP to a VM @param vm_id: the id of the VM @return: the floating IP and the id of the floating ip """ pools = self.get_pool() if pools is None: msg = "No exists any pools or cannot obtain them" return None, msg my_url = "%s/%s" % (self.url_openstack, "os-floating-ips") response = None for pol in pools: payload = '{ "pool": "' + pol + '"}' response = http.post(my_url, self.header, payload) if response.status is 200: loggers.set_info_log("IP from pool: " + pol) break if response.status != 200: msg = "Error: cannot create a floating IP in any pool" return None, msg floating = json.loads(response.read()) floating_ip = floating['floating_ip']['ip'] floating_ip_id = floating['floating_ip']['id'] loggers.set_info_log(floating_ip) r = self.associate_floating_ip(vm_id, floating_ip) if r is not None: return r return floating_ip, floating_ip_id
def add_floating_ip(self, vm_id): """ Add a floating IP to a VM @param vm_id: the id of the VM @return: the floating IP and the id of the floating ip """ pools = self.get_pool() if pools is None: msg = "No exists any pools or cannot obtain them" return None, msg my_url = "%s/%s" % (self.url_openstack, "os-floating-ips") response = None for pol in pools: payload = '{ "pool": "' + pol + '"}' response = http.post(my_url, self.header, payload) if response.status is 200: loggers.set_info_log("IP from pool: " + pol) break if response.status != 200: msg = "Error: cannot create a floating IP in any pool" return None, msg floating = json.loads(response.read()) floating_ip = floating['floating_ip']['ip'] floating_ip_id = floating['floating_ip']['id'] loggers.set_info_log(floating_ip) r = self.associate_floating_ip(vm_id, floating_ip) if r is not None: return r return floating_ip, floating_ip_id
def add_product(self, product_name, product_description, attributes, metadatas): """ Add a prodduct to the SDC @param product_name: the name of the product @param product_description: the description of the product @param attributes: the attributes of thw product @param metadatas: the metadata of teh product @return: (None if all OK / an error on failure), (The product / None of error) """ my_url = "%s/%s" % (self.sdc_url, "catalog/product") loggers.set_info_log(my_url) product = productrelease.Product(product_name, product_description) if attributes is not None: attributes_list = productrelease.process_attributes(attributes) for att in attributes_list: product.add_attribute(att) metadatas_list = productrelease.process_attributes(metadatas) for meta in metadatas_list: product.add_metadata(meta) payload = product.to_product_xml() print(EL.tostring(payload)) print(my_url) response = http.post(my_url, self.header2, EL.tostring(payload)) if response.status() is not 200: error = 'error to add the product sdc ' + str(response.status()) loggers.set_error_log(error) return error, None else: self.products.append(product) return None, product
def add_node_run_list(self, software): """ add the software to install into the list of the node @param software: The software @return: None if all OK or an error on failure """ puppet_master_url = models.Data.objects.get(key="puppet_master_url") software_name = software[0] version = software[1] uri = '%s/%s/%s/%s/%s/%s' % (puppet_master_url, 'install', self.tenant, self.name, software_name, version) loggers.set_info_log(uri) response = http.post(uri, self.headers, None) if response.status() != 200: msg = "Error adding the software to puppet" loggers.set_error_log( str(response.status) + ": " + msg + ": " + response.read()) return "Error" uri = '%s/%s/%s' % (puppet_master_url, 'generate', self.name) loggers.set_info_log(uri) response = http.post(uri, self.headers, "") if response.status() != 200: msg = "Error adding the software to puppet" loggers.set_error_log( str(response.status) + ": " + msg + ": " + response.read()) return "Error" return None
def delete_vm(self, vm_id): """ Delete a VM from its id @param vm_id: the id of the VM @return: None if all OK or an error on failure """ the_url = "%s/%s/%s" % (self.url_openstack, "servers", vm_id) response = http.delete(the_url, self.header) if response.status != 204: return 'error deleting vm' + str(response.status) + response.reason else: loggers.set_info_log('Deleting VM ........') return None
def delete_vm(self, vm_id): """ Delete a VM from its id @param vm_id: the id of the VM @return: None if all OK or an error on failure """ the_url = "%s/%s/%s" % (self.url_openstack, "servers", vm_id) response = http.delete(the_url, self.header) if response.status != 204: return 'error deleting vm' + str(response.status) + response.reason else: loggers.set_info_log('Deleting VM ........') return None
def update_master_server(self): """ Add the software into puppet-master or chef-server @return: None if all OK or an error on failure """ puppet_master_url = models.Data.objects.get(key="puppet_master_url") if self.repo == 'svn': uri = '%s/%s/%s' % ( puppet_master_url, 'download/svn', self.name) loggers.set_info_log("update puppet_master url: " + uri) elif self.repo == 'git': uri = u'{0:s}/{1:s}/{2:s}'.format(puppet_master_url, 'download/git', self.name) loggers.set_info_log("update puppet_master url: " + uri) else: msg = "The repository is not svn or git. " loggers.set_error_log(msg) return msg payload = "url=" + self.cookbook_url loggers.set_info_log("update puppet_master payload: " + payload) response = http.post(uri, self.headers, payload) if response.status() is not 200: msg = "Error downloading the puppet module into the puppet master" loggers.set_error_log(str(response.status) + ": " + msg + ": " + response.read()) return msg loggers.set_info_log(str( response.status) + ": Correctly download the module into the " "puppet master") return None
def rem_floating_ip(self, floating_ip, server_id, fip_id): """ Disassociate a IP from a VM and elease the floating IP @param floating_ip: The floating IP @param server_id: the VM id where the floating id is associated @param fip_id: the floating ip id @return: The flaoting ip if all OK or a mgs error on failure """ the_url = "%s/%s/%s/%s" % ( self.url_openstack, "servers", server_id, "action") payload = '{ "removeFloatingIp": {"address": "' + floating_ip + '" } }' loggers.set_info_log(the_url) loggers.set_info_log(payload) response = http.post(the_url, self.header, payload) loggers.set_info_log(response.status) if response.status != 202: msg = "Error: Cannot un-assign the floating IP" return None, msg the_url = "%s/%s/%s" % (self.url_openstack, "os-floating-ips", fip_id) response = http.delete(the_url, self.header) loggers.set_info_log("Deleted the floating IP") if response.status != 202: msg = "Error deleting the floating IP" return None, msg return floating_ip, None
def rem_floating_ip(self, floating_ip, server_id, fip_id): """ Disassociate a IP from a VM and elease the floating IP @param floating_ip: The floating IP @param server_id: the VM id where the floating id is associated @param fip_id: the floating ip id @return: The flaoting ip if all OK or a mgs error on failure """ the_url = "%s/%s/%s/%s" % (self.url_openstack, "servers", server_id, "action") payload = '{ "removeFloatingIp": {"address": "' + floating_ip + '" } }' loggers.set_info_log(the_url) loggers.set_info_log(payload) response = http.post(the_url, self.header, payload) loggers.set_info_log(response.status) if response.status != 202: msg = "Error: Cannot un-assign the floating IP" return None, msg the_url = "%s/%s/%s" % (self.url_openstack, "os-floating-ips", fip_id) response = http.delete(the_url, self.header) loggers.set_info_log("Deleted the floating IP") if response.status != 202: msg = "Error deleting the floating IP" return None, msg return floating_ip, None
def update_master_server(self): """ Add the software into puppet-master or chef-server @return: None if all OK or an error on failure """ puppet_master_url = models.Data.objects.get(key="puppet_master_url") if self.repo == 'svn': uri = '%s/%s/%s' % (puppet_master_url, 'download/svn', self.name) loggers.set_info_log("update puppet_master url: " + uri) elif self.repo == 'git': uri = u'{0:s}/{1:s}/{2:s}'.format(puppet_master_url, 'download/git', self.name) loggers.set_info_log("update puppet_master url: " + uri) else: msg = "The repository is not svn or git. " loggers.set_error_log(msg) return msg payload = "url=" + self.cookbook_url loggers.set_info_log("update puppet_master payload: " + payload) response = http.post(uri, self.headers, payload) if response.status() is not 200: msg = "Error downloading the puppet module into the puppet master" loggers.set_error_log( str(response.status) + ": " + msg + ": " + response.read()) return msg loggers.set_info_log( str(response.status) + ": Correctly download the module into the " "puppet master") return None
def delete_node_client(self): """ Delete the node from chef-server or puppet-master @return: None if all OK or an error on failure """ puppet_master_url = models.Data.objects.get(key="puppet_master_url") uri = '%s/%s/%s' % (puppet_master_url, 'delete/node', self.name) loggers.set_info_log("delete node: puppet_master url: " + uri) response = http.delete(uri, self.headers) if response.status() != 200: msg = "Error deleting node from puppet" loggers.set_error_log( str(response.status) + ": " + msg + ": " + response.read()) return "Error" return None
def delete_node_client(self): """ Delete the node from chef-server or puppet-master @return: None if all OK or an error on failure """ puppet_master_url = models.Data.objects.get(key="puppet_master_url") uri = '%s/%s/%s' % (puppet_master_url, 'delete/node', self.name) loggers.set_info_log("delete node: puppet_master url: " + uri) response = http.delete(uri, self.headers) if response.status() != 200: msg = "Error deleting node from puppet" loggers.set_error_log(str(response.status) + ": " + msg + ": " + response.read()) return "Error" return None
def get_vm(self, vm_id): """ Obtain data about a vm @param vm_id: the id of the VM @return: the info or None if cannot find the VM """ the_url = "%s/%s/%s" % (self.url_openstack, "servers", vm_id) response = http.get(the_url, self.header) if response.status is not 200 and response.status is not 202: loggers.set_error_log( str(response.status) + ': error to add the product') return None else: info = response.read() loggers.set_info_log(info) return info
def get_vm(self, vm_id): """ Obtain data about a vm @param vm_id: the id of the VM @return: the info or None if cannot find the VM """ the_url = "%s/%s/%s" % (self.url_openstack, "servers", vm_id) response = http.get(the_url, self.header) if response.status is not 200 and response.status is not 202: loggers.set_error_log( str(response.status) + ': error to add the product') return None else: info = response.read() loggers.set_info_log(info) return info
def associate_floating_ip(self, vm_id, floating_ip): """ Associate a floating IP to a VM @param vm_id: the VM id @param floating_ip: The floating IP @return: None if all OK or an error on failure """ my_url = "%s/%s/%s/%s" % ( self.url_openstack, "servers", vm_id, "action") payload = '{ "addFloatingIp": {"address": "' + floating_ip + '" } }' loggers.set_info_log(my_url) response = http.post(my_url, self.header, payload) loggers.set_info_log(response.read()) if response.status is not 202: msg = "Error: Cannot assign the floating IP to the VM" return msg return None
def associate_floating_ip(self, vm_id, floating_ip): """ Associate a floating IP to a VM @param vm_id: the VM id @param floating_ip: The floating IP @return: None if all OK or an error on failure """ my_url = "%s/%s/%s/%s" % (self.url_openstack, "servers", vm_id, "action") payload = '{ "addFloatingIp": {"address": "' + floating_ip + '" } }' loggers.set_info_log(my_url) response = http.post(my_url, self.header, payload) loggers.set_info_log(response.read()) if response.status is not 202: msg = "Error: Cannot assign the floating IP to the VM" return msg return None
def remove_master_server(self): """ Remove the software from puppet-master or chef-server @return: None if all OK or an error on failure """ puppet_master_url = models.Data.objects.get(key="puppet_master_url") uri = u'{0:s}/{1:s}/{2:s}'.format(puppet_master_url, 'delete/module', self.name) response = http.delete(uri, self.headers) if response.status() != 200: msg = "Error deleting the puppet module from the puppet master" loggers.set_error_log( str(response.status) + ": " + msg + ": " + response.read()) return msg loggers.set_info_log( str(response.status) + ": Correctly deleting the module from the " "puppet master") return None
def remove_master_server(self): """ Remove the software from puppet-master or chef-server @return: None if all OK or an error on failure """ puppet_master_url = models.Data.objects.get(key="puppet_master_url") uri = u'{0:s}/{1:s}/{2:s}'.format(puppet_master_url, 'delete/module', self.name) response = http.delete(uri, self.headers) if response.status() != 200: msg = "Error deleting the puppet module from the puppet master" loggers.set_error_log(str(response.status) + ": " + msg + ": " + response.read()) return msg loggers.set_info_log( str(response.status) + ": Correctly deleting the module from the " "puppet master") return None
def connect_ssh(self, ip): """ Go into a VM to check the software installation @param ip: The IP of the VM @return: None if all OK or an error on failure """ loggers.set_info_log("En el ssh") ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: ssh.connect(ip, username=self.sdc_user, password=str( models.Data.objects.get(key="ubuntu_password"))) except Exception: try: ssh.connect( ip, username=self.sdc_user, password=str( models.Data.objects.get(key="centos_password"))) except Exception: msg = 'Error connecting to the Chef-server for recipe ' \ 'execution' loggers.set_error_log(msg) return msg if self.manager == 'chef': stdin, stdout, stderr = ssh.exec_command('chef-client') stdin.flush() else: ssh.exec_command('service puppet stop') stdin, stdout, stderr = ssh.exec_command('servicepuppet agent -t') stdin.flush() result = '' for line in stdout: result += line.strip('\n') loggers.set_info_log(line.strip('\n')) ssh.close() if "FATAL" in result: msg = 'ERROR to execute the recipe' loggers.set_error_log(msg) return msg return None
def get_product_info(self, product_name): """ return the information from a product @param product_name: the product name @return: the product description """ the_url = "%s/%s/%s" % (self.sdc_url, "catalog/product", product_name) response = http.get(the_url, self.header) if response.status() != 200: loggers.set_error_log('error to get the product ' + product_name + ' ' + str(response.status())) return None else: data = json.loads(response.read()) loggers.set_info_log(data) try: description = data['description'] except Exception: return None return description
def get_product_release(self, product_name): """ Obtain the product release from a product @param product_name: the name of the product @return: The version of the software """ my_url = "%s/%s/%s/%s" % ( self.sdc_url, "catalog/product", product_name, "release") response = http.get(my_url, self.header) if response.status() != 200: error = 'error to get the product ' + product_name + ' ' + str( response.status()) loggers.set_error_log(error) return None else: data = json.loads(response.read()) loggers.set_info_log(data) if data is None: return None return data['productRelease']['version']
def get_product_release(self, product_name): """ Obtain the product release from a product @param product_name: the name of the product @return: The version of the software """ my_url = "%s/%s/%s/%s" % (self.sdc_url, "catalog/product", product_name, "release") response = http.get(my_url, self.header) if response.status() != 200: error = 'error to get the product ' + product_name + ' ' + str( response.status()) loggers.set_error_log(error) return None else: data = json.loads(response.read()) loggers.set_info_log(data) if data is None: return None return data['productRelease']['version']
def get_product_info(self, product_name): """ return the information from a product @param product_name: the product name @return: the product description """ the_url = "%s/%s/%s" % (self.sdc_url, "catalog/product", product_name) response = http.get(the_url, self.header) if response.status() != 200: loggers.set_error_log('error to get the product ' + product_name + ' ' + str(response.status())) return None else: data = json.loads(response.read()) loggers.set_info_log(data) try: description = data['description'] except Exception: return None return description
def deploy_vm(self): """ Deploy a VM @return: The Ip of a VM and its id. Return the error if something wrong """ the_url = "%s/%s" % (self.url_openstack, "servers") loggers.set_info_log("Deploy VM url: " + the_url) payload = '{"server": ' \ '{"name": " ' + self.name + '", "imageRef": "' \ + self.so + '", "flavorRef": "2"}}' response = http.post(the_url, self.header, payload) if response.status is not 200 and response.status is not 202: msg = str(response.status) + '. Error deploying the VM: ' + str( response.reason) return None, msg else: var = response.read() server = json.loads(var) vm_id = server['server']['id'] ip = self.while_till_deployed(vm_id) return ip, vm_id
def get_products(self): """ Obtain a list of the product's name @return:List od products """ my_url = "%s/%s" % (self.sdc_url, "catalog/product") loggers.set_info_log(my_url) response = http.get(my_url, self.header) if response.status() != 200: msg = "Error obtaining the token" loggers.set_error_log(msg) return "Error", msg else: data = json.loads(response.read()) products = data["product"] my_products = [] for product in products: var = product['name'] my_products.append(var) loggers.set_info_log(my_products) return None, my_products
def deploy_vm(self): """ Deploy a VM @return: The Ip of a VM and its id. Return the error if something wrong """ the_url = "%s/%s" % (self.url_openstack, "servers") loggers.set_info_log("Deploy VM url: " + the_url) payload = '{"server": ' \ '{"name": " ' + self.name + '", "imageRef": "' \ + self.so + '", "flavorRef": "2"}}' response = http.post(the_url, self.header, payload) if response.status is not 200 and response.status is not 202: msg = str(response.status) + '. Error deploying the VM: ' + str( response.reason) return None, msg else: var = response.read() server = json.loads(var) vm_id = server['server']['id'] ip = self.while_till_deployed(vm_id) return ip, vm_id
def get_products(self): """ Obtain a list of the product's name @return:List od products """ my_url = "%s/%s" % (self.sdc_url, "catalog/product") loggers.set_info_log(my_url) response = http.get(my_url, self.header) if response.status() != 200: msg = "Error obtaining the token" loggers.set_error_log(msg) return "Error", msg else: data = json.loads(response.read()) products = data["product"] my_products = [] for product in products: var = product['name'] my_products.append(var) loggers.set_info_log(my_products) return None, my_products
def connect_ssh(self, ip): """ Go into a VM to check the software installation @param ip: The IP of the VM @return: None if all OK or an error on failure """ loggers.set_info_log("En el ssh") ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: ssh.connect(ip, username=self.sdc_user, password=str( models.Data.objects.get(key="ubuntu_password"))) except Exception: try: ssh.connect(ip, username=self.sdc_user, password=str( models.Data.objects.get(key="centos_password"))) except Exception: msg = 'Error connecting to the Chef-server for recipe ' \ 'execution' loggers.set_error_log(msg) return msg if self.manager == 'chef': stdin, stdout, stderr = ssh.exec_command('chef-client') stdin.flush() else: ssh.exec_command('service puppet stop') stdin, stdout, stderr = ssh.exec_command('servicepuppet agent -t') stdin.flush() result = '' for line in stdout: result += line.strip('\n') loggers.set_info_log(line.strip('\n')) ssh.close() if "FATAL" in result: msg = 'ERROR to execute the recipe' loggers.set_error_log(msg) return msg return None
def test(self): """ Test the software into a VM @return: """ loggers.set_info_log("En el test de openstack") loggers.set_info_log("*************") loggers.set_info_log(self.name) loggers.set_info_log(self.so) loggers.set_info_log(self.cookbook) loggers.set_info_log("*************") chef_puppet = None software_install = '' if self.manager == 'chef': chef_puppet = chef_management.MINode(self.name) software_install = self.cookbook + '::' + self.version + "_install" if self.manager == 'pupet': chef_puppet = puppet_management.MINode(self.name, self.tenant) software_install = [self.cookbook, self.version] if self.token is None: msg = "Error: Cannot obtained the token" loggers.set_error_log(msg) return msg ip, server_id = self.deploy_vm() if ip is None: msg = server_id + ". Image id: " + self.so loggers.set_error_log(msg) return msg loggers.set_info_log("Correctly deployed VM withb image id: " + self.so) fip, fip_id = self.add_floating_ip(server_id) if fip is None: self.delete_vm(server_id) msg = "Error, cannot find floating IP: " + fip_id loggers.set_error_log(msg) return msg time.sleep(60) r = chef_puppet.add_node_run_list(software_install) if r is not None: self.rem_floating_ip(fip, server_id, fip_id) self.delete_vm(server_id) msg = 'Error installing software in a VM with image id: ' + \ self.so + '. Error: ' + r loggers.set_error_log(msg) return msg r = self.connect_ssh(fip) if r is not None: self.rem_floating_ip(fip, server_id, fip_id) self.delete_vm(server_id) chef_puppet.delete_node_client() msg = "Error testing the software: " + r loggers.set_error_log(msg) return msg loggers.set_info_log("Antes del delete") r = self.delete_vm(server_id) chef_puppet.delete_node_client() if r is not None: msg = "Error deleting the testing VM: " + r loggers.set_error_log(msg) return msg self.rem_floating_ip(fip, server_id, fip_id) return None
def test(self): """ Test the software into a VM @return: """ loggers.set_info_log("En el test de openstack") loggers.set_info_log("*************") loggers.set_info_log(self.name) loggers.set_info_log(self.so) loggers.set_info_log(self.cookbook) loggers.set_info_log("*************") chef_puppet = None software_install = '' if self.manager == 'chef': chef_puppet = chef_management.MINode(self.name) software_install = self.cookbook + '::' + self.version + "_install" if self.manager == 'pupet': chef_puppet = puppet_management.MINode(self.name, self.tenant) software_install = [self.cookbook, self.version] if self.token is None: msg = "Error: Cannot obtained the token" loggers.set_error_log(msg) return msg ip, server_id = self.deploy_vm() if ip is None: msg = server_id + ". Image id: " + self.so loggers.set_error_log(msg) return msg loggers.set_info_log( "Correctly deployed VM withb image id: " + self.so) fip, fip_id = self.add_floating_ip(server_id) if fip is None: self.delete_vm(server_id) msg = "Error, cannot find floating IP: " + fip_id loggers.set_error_log(msg) return msg time.sleep(60) r = chef_puppet.add_node_run_list(software_install) if r is not None: self.rem_floating_ip(fip, server_id, fip_id) self.delete_vm(server_id) msg = 'Error installing software in a VM with image id: ' + \ self.so + '. Error: ' + r loggers.set_error_log(msg) return msg r = self.connect_ssh(fip) if r is not None: self.rem_floating_ip(fip, server_id, fip_id) self.delete_vm(server_id) chef_puppet.delete_node_client() msg = "Error testing the software: " + r loggers.set_error_log(msg) return msg loggers.set_info_log("Antes del delete") r = self.delete_vm(server_id) chef_puppet.delete_node_client() if r is not None: msg = "Error deleting the testing VM: " + r loggers.set_error_log(msg) return msg self.rem_floating_ip(fip, server_id, fip_id) return None