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_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 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 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 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 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 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 add_product_release(self, product, product_name, version): """ Add a product release to the SDC @param product: A product into SDC @param product_name: The name of the product @param version: the version of the release @return: None if all OK / an error on failure """ my_url = "%s/%s/%s/%s" % (self.sdc_url, "catalog/product", product_name, "release") product_release = productrelease.ProductRelease(product, version) payload = product_release.to_product_xml() response = http.post(my_url, self.header2, EL.tostring(payload)) if response.status() != 200: return ('Error to add the product release to sdc ' + str(response.status())) else: self.products.append(product) return None
def add_product_release(self, product, product_name, version): """ Add a product release to the SDC @param product: A product into SDC @param product_name: The name of the product @param version: the version of the release @return: None if all OK / an error on failure """ my_url = "%s/%s/%s/%s" % ( self.sdc_url, "catalog/product", product_name, "release") product_release = productrelease.ProductRelease(product, version) payload = product_release.to_product_xml() response = http.post(my_url, self.header2, EL.tostring(payload)) if response.status() != 200: return ( 'Error to add the product release to sdc ' + str( response.status())) else: self.products.append(product) return None
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