def query_yes_no(question, default="yes"): """Ask a yes/no question via raw_input() and return their answer. "question" is a string that is presented to the user. "default" is the presumed answer if the user just hits <Enter>. It must be "yes" (the default), "no" or None (meaning an answer is required of the user). The "answer" return value is one of "yes" or "no". """ valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False} if default == None: prompt = " [y/n] " elif default == "yes": prompt = " [Y/n] " elif default == "no": prompt = " [y/N] " else: raise ValueError("invalid default answer: '%s'" % default) while True: printer.out(question + prompt) choice = raw_input().lower() if default is not None and choice == '': return valid[default] elif choice in valid: return valid[choice] else: printer.out("Please respond with 'yes' or 'no' "\ "(or 'y' or 'n').\n")
def generate_vagrant(image, builder, installProfile): installProfile = get_memory_amount(builder, installProfile, True) if installProfile==2: return None,None,None if "publicBaseBox" in builder and builder["publicBaseBox"]=="true": #private vagrant if not "osUser" in builder: printer.out("osUser not found in vagrant builder", printer.ERROR) return None,None,None if not "sshKey" in builder: printer.out("sshKey not found in vagrant builder", printer.ERROR) return None,None,None #create ssh key for the user sshkey = generics_utils.create_user_ssh_key(self.api, self.login, builder["sshKey"]) if sshkey==2: return None,None,None sshKeys = sshKeys() sshKeys.add_sshKey(sshkey) vagrantUser = osUser() vagrantUser.name = builder["osUser"] vagrantUser.sshKeys = sshKeys installProfile.osusers.add_osUser(vagrantUser) else: pass image.compress=True myimageFormat = imageFormat(name=VIRTUAL_FORMATS[builder["type"]]) return image,myimageFormat,installProfile
def publish_abiquo(pimage, builder): #doing field verification if not "enterprise" in builder: printer.out("enterprise in abiquo builder not found", printer.ERROR) return if not "datacenter" in builder: printer.out("datacenter in abiquo builder not found", printer.ERROR) return if not "productName" in builder: printer.out("productName in abiquo builder not found", printer.ERROR) return if not "category" in builder: printer.out("category in abiquo builder not found", printer.ERROR) return if not "description" in builder: printer.out("description in abiquo builder not found", printer.ERROR) return pimage.credAccount.datacenterName = builder["datacenter"] pimage.credAccount.displayName = builder["productName"] pimage.credAccount.category = builder["category"] pimage.credAccount.organizationName = builder["enterprise"] pimage.credAccount.description = builder["description"] return pimage
def check_mandatory_create_account(iterables, type): #iterables can be builders or accounts for iterable in iterables: if type=="builders": if "account" in iterable: if not "type" in iterable and not "type" in iterable["account"]: printer.out("no attribute type in builder", printer.ERROR) return if "file" in iterable["account"]: file = get_file(iterable["account"]["file"]) if file is None: return 2 data = check_json_syntax(file) if data is None: return 2 if "accounts" in data: return check_mandatory_create_account(data["accounts"], "accounts") if type=="accounts": if not "type" in iterable: printer.out("no attribute type in accounts", printer.ERROR) return #TODO return iterables
def susecloud(myCredAccount, account): # doing field verification if not "username" in account: printer.out("username in susecloud account not found", printer.ERROR) return if not "password" in account: printer.out("catalogName in susecloud account not found", printer.ERROR) return if not "endpoint" in account: printer.out("endpoint for susecloud account not found", printer.ERROR) return if not "keystoneEndpoint" in account: printer.out("keystoneEndpoint for susecloud account not found", printer.ERROR) return if not "name" in account: printer.out("name for susecloud account not found", printer.ERROR) return myCredAccount.type_ = "susecloud" myCredAccount.login = account["username"] myCredAccount.password = account["password"] myCredAccount.serverUrl = account["endpoint"] myCredAccount.keystoneUrl = account["keystoneEndpoint"] myCredAccount.name = account["name"] return myCredAccount
def flexiant(myCredAccount, account): # doing field verification if not "password" in account: printer.out("password in flexiant account not found", printer.ERROR) return if not "username" in account: printer.out("username in flexiant account not found", printer.ERROR) return if not "wsdlURL" in account: printer.out("wsdlURL for flexiant account not found", printer.ERROR) return if not "name" in account: printer.out("name for flexiant account not found", printer.ERROR) return myCredAccount.type_ = "flexiant" myCredAccount.login = account["username"] myCredAccount.password = account["password"] try: myCredAccount.userUUID = (myCredAccount.login).split("/")[1] except: printer.out(account["username"] + " is not a valid Flexiant username", printer.ERROR) return myCredAccount.wsdlLocation = account["wsdlURL"] myCredAccount.name = account["name"] return myCredAccount
def query_yes_no(question, default="yes"): """Ask a yes/no question via raw_input() and return their answer. "question" is a string that is presented to the user. "default" is the presumed answer if the user just hits <Enter>. It must be "yes" (the default), "no" or None (meaning an answer is required of the user). The "answer" return value is one of "yes" or "no". """ valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False} if default == None: prompt = " [y/n] " elif default == "yes": prompt = " [Y/n] " elif default == "no": prompt = " [y/N] " else: raise ValueError("invalid default answer: '%s'" % default) while True: printer.out(question + prompt) choice = raw_input().lower() if default is not None and choice == "": return valid[default] elif choice in valid: return valid[choice] else: printer.out("Please respond with 'yes' or 'no' " "(or 'y' or 'n').\n")
def start(self): output_file = open(self.output_file_name, 'wb') try: response = requests.get(self.url, stream=True, verify=self.verify) response.raise_for_status() self.read_chunk(response, output_file, report_hook=True) except requests.exceptions.HTTPError as e: printer.out("Error getting URL: " + self.url, printer.ERROR) raise e
def start(self): file = open(self.dest_file_name, 'wb') try: response = urllib2.urlopen(self.url) bytes = self.chunk_read(response, file, report_hook=True) self.progress_finish() except urllib2.HTTPError as e: printer.out("Error getting URL: " + self.url, printer.ERROR) raise e
def start(self): file = open(self.dest_file_name, 'wb') try: response = urllib2.urlopen(self.url) bytes = self.chunk_read(response, file, report_hook=True) self.progress_finish() except urllib2.HTTPError as e: printer.out("Error getting URL: "+self.url, printer.ERROR) raise e
def get_memory_amount(builder, installProfile, is_mandatory): if "hardwareSettings" in builder and "memory" in builder["hardwareSettings"]: installProfile.memorySize = builder["hardwareSettings"]["memory"] return installProfile else: if is_mandatory: printer.out("Error: no hardwareSettings part for builder ["+builder["type"]+"]", printer.ERROR) return 2 else: return installProfile
def publish_ami(pimage, builder): #doing field verification if not "s3bucket" in builder: printer.out("s3bucket in AMI builder not found", printer.ERROR) return if not "region" in builder: printer.out("region in AMI builder not found", printer.ERROR) return pimage.credAccount.bucket = builder["s3bucket"] pimage.publishLocation = builder["region"] return pimage
def publish_azure(pimage, builder): #doing field verification if not "storageAccount" in builder: printer.out("region in Microsoft Azure not found", printer.ERROR) return pimage.credAccount.bucket = builder["storageAccount"] if "location" in builder: pimage.credAccount.zoneName = builder["location"] pimage.publishLocation = builder["location"] return pimage
def vcenter(myCredAccount, account): # doing field verification if not "hostname" in account: printer.out("hostname in vcenter account not found", printer.ERROR) return if not "username" in account: printer.out("username in vcenter account not found", printer.ERROR) return if not "password" in account: printer.out("password in vcenter account not found", printer.ERROR) return if not "name" in account: printer.out("name in vcenter account not found", printer.ERROR) return if "proxyHostname" in account: myCredAccount.proxyHost = account["proxyHostname"] if "proxyPort" in account: myCredAccount.proxyPort = account["proxyPort"] if "port" in account: port = int(account["port"]) else: port = 443 myCredAccount.type_ = "vsphere" myCredAccount.name = account["name"] myCredAccount.login = account["username"] myCredAccount.password = account["password"] myCredAccount.hostname = account["hostname"] myCredAccount.port = port return myCredAccount
def check_json_syntax(file): try: printer.out("Validating the template file [" + file + "] ...") json_data = open(file) data = json.load(json_data) json_data.close() printer.out("Syntax of template file [" + file + "] is ok", printer.OK) return data except ValueError as e: printer.out("Syntax of template file [" + file + "] FAILED", printer.ERROR) printer.out("JSON parsing error: " + str(e)) return except IOError as e: printer.out("File error: " + str(e), printer.ERROR) return
def publish_cloudstack(pimage, builder): #doing field verification if not "imageName" in builder: printer.out("imageName in cloudstack builder not found", printer.ERROR) return if not "zone" in builder: printer.out("zone in cloudstack builder not found", printer.ERROR) return if "publicImage" in builder: pimage.credAccount.publicImage = True if (builder["publicImage"]=="true") else False if "featured" in builder: pimage.credAccount.featuredEnabled = True if (builder["featured"]=="true") else False pimage.credAccount.displayName = builder["imageName"] pimage.credAccount.zoneName = builder["zone"] return pimage
def publish_openstack(pimage, builder): #doing field verification if not "imageName" in builder: printer.out("imageName in openstack builder not found", printer.ERROR) return if not "tenant" in builder: printer.out("tenant in openstack builder not found", printer.ERROR) return if "description" in builder: pimage.credAccount.description = builder["description"] pimage.credAccount.displayName = builder["imageName"] pimage.credAccount.tenantName = builder["tenant"] if "publicImage" in builder: pimage.credAccount.publicImage = True if (builder["publicImage"]=="true") else False #if "paraVirtualMode" in builder: # pimage.credAccount. = True if (builder["paraVirtualMode"]=="true") else False return pimage
def read_chunk(self, response, output_file, chunk_size=8192, report_hook=False): total_size = int(response.headers['Content-Length']) current_chunk = 0 printer.out("Ready to download " + size(total_size), printer.INFO) widgets = ['Status: ', Percentage(), ' ', Bar('>'), ' ', ETA()] progress_bar = ProgressBar(widgets=widgets, maxval=100).start() for chunk in response.iter_content(chunk_size): output_file.write(chunk) current_chunk += 1 if report_hook: percent = int(current_chunk * chunk_size * 100 / total_size) if percent > 100: percent = 100 progress_bar.update(percent) progress_bar.finish()
def generate_ami(image, builder, installProfile, api, login): if not "account" in builder: printer.out("Account not found in builder", printer.ERROR) return None,None,None if not "name" in builder["account"]: printer.out("Account anme not found in builder", printer.ERROR) return None,None,None accounts = api.Users(login).Accounts.Getall() if accounts is None or not hasattr(accounts, 'get_credAccount'): printer.out("No accounts available", printer.ERROR) return None,None,None for account in accounts.get_credAccount(): if account.name == builder["account"]["name"]: image.credAccount = account break if "disableRootLogin" in builder: myrootUser = osUser() if builder["disableRootLogin"]=="true": val=True elif builder["disableRootLogin"]=="false": val=False else: printer.out("Unknown value for 'disableRootLogin' in builder [ami]", printer.ERROR) return None,None,None myrootUser.disablePasswordLogin = val installProfile.rootUser = myrootUser if "updateAWSTools" in builder: image.update if "ebs" in builder: if "installation" in builder and "diskSize" in builder["installation"]: installProfile.ebsVolumeSize = builder["installation"]["diskSize"] else: printer.out("No disksize set for ebs volume in builder [ami]", printer.ERROR) return None,None,None image.compress=False myimageFormat = imageFormat(name=CLOUD_FORMATS[builder["type"]]) return image,myimageFormat,installProfile
def azure(myCredAccount, account): # doing field verification if not "rsaPrivateKey" in account: printer.out("rsaPrivateKey in azure account not found", printer.ERROR) return if not "certKey" in account: printer.out("certKey in azure account not found", printer.ERROR) return if not "subscriptionId" in account: printer.out("subscriptionId for azure account not found", printer.ERROR) return if not "name" in account: printer.out("name for azure account not found", printer.ERROR) return myCredAccount.type_ = "azure" myCredAccount.accountNumber = account["subscriptionId"] myCredAccount.name = account["name"] myCertificates = certificates() myCredAccount.certificates = myCertificates try: myCertificate = certificate() with open(account["rsaPrivateKey"], "r") as myfile: myCertificate.certStr = myfile.read() myCertificate.type_ = "azureRSAKey" myCertificate.name = ntpath.basename(account["rsaPrivateKey"]) myCertificates.add_certificate(myCertificate) myCertificate = certificate() with open(account["certKey"], "r") as myfile: myCertificate.certStr = myfile.read() myCertificate.type_ = "azureCertKey" myCertificate.name = ntpath.basename(account["certKey"]) myCertificates.add_certificate(myCertificate) except IOError as e: printer.out("File error: " + str(e), printer.ERROR) return return myCredAccount
def read_chunk(self, response, output_file, chunk_size=8192, report_hook=False): total_size = int(response.headers['Content-Length']) current_chunk = 0 printer.out("Ready to download " + size(total_size), printer.INFO) widgets = ['Status: ', Percentage(), ' ', Bar('>'), ' ', ETA()] progress_bar = ProgressBar(widgets=widgets, maxval=100).start() for chunk in response.iter_content(chunk_size): output_file.write(chunk) current_chunk += 1 if report_hook: percent = int(current_chunk * chunk_size * 100 / total_size) progress_bar.update(percent) progress_bar.finish()
def generate_eucalyptus(image, builder, installProfile, api, login): if not "account" in builder: printer.out("Account not found in builder", printer.ERROR) return None,None,None accounts = api.Users(login).Accounts.Getall() if accounts is None or not accounts.get_credAccount(): printer.out("No accounts available", printer.ERROR) return None,None,None else: for account in accounts.get_credAccount(): if account.name == builder["account"]["name"]: image.credAccount = account break if "disableRootLogin" in builder: myrootUser = osUser() if builder["disableRootLogin"]=="true": val=True elif builder["disableRootLogin"]=="false": val=False else: printer.out("Unknown value for 'disableRootLogin' in builder [ami]", printer.ERROR) return None,None,None myrootUser.disablePasswordLogin = val installProfile.rootUser = myrootUser image.compress=False myimageFormat = imageFormat(name=CLOUD_FORMATS[builder["type"]]) return image,myimageFormat,installProfile
def chunk_read(self, response, file, chunk_size=8192, report_hook=False): # inspired by http://stackoverflow.com/questions/2028517/python-urllib2-progress-hook total_size = response.info().getheader('Content-Length').strip() total_size = int(total_size) bytes = 0 printer.out("Ready to download "+size(total_size), printer.INFO) #widgets = ['Status: ', Percentage(), ' ', Bar('>'), ' ', ETA(), ' ', FileTransferSpeed()] widgets = ['Status: ', Percentage(), ' ', Bar('>'), ' ', ETA()] self.pbar = ProgressBar(widgets=widgets, maxval=100).start() while True: chunk = response.read(chunk_size) file.write(chunk) bytes += len(chunk) if not chunk: break if report_hook: self.progress_update((bytes/chunk_size), chunk_size, total_size) return bytes
def check_mandatory_generate_scan(builders): for builder in builders: if not "installation" in builder: printer.out("no attribute installation in builder", printer.ERROR) return if not "diskSize" in builder["installation"]: printer.out("no attribute diskSize in the installation part of builder", printer.ERROR) return if not "hardwareSettings" in builder: printer.out("no attribute hardwareSettings in builder", printer.ERROR) return if not "memory" in builder["hardwareSettings"]: printer.out("no attribute diskSize in the memory part of hardwareSettings", printer.ERROR) return return builders
def chunk_read(self, response, file, chunk_size=8192, report_hook=False): # inspired by http://stackoverflow.com/questions/2028517/python-urllib2-progress-hook total_size = response.info().getheader('Content-Length').strip() total_size = int(total_size) bytes = 0 printer.out("Ready to download " + size(total_size), printer.INFO) #widgets = ['Status: ', Percentage(), ' ', Bar('>'), ' ', ETA(), ' ', FileTransferSpeed()] widgets = ['Status: ', Percentage(), ' ', Bar('>'), ' ', ETA()] self.pbar = ProgressBar(widgets=widgets, maxval=100).start() while True: chunk = response.read(chunk_size) file.write(chunk) bytes += len(chunk) if not chunk: break if report_hook: self.progress_update((bytes / chunk_size), chunk_size, total_size) return bytes
def gce(myCredAccount, account): # doing field verification if not "username" in account: printer.out("username in gce account not found", printer.ERROR) return if not "certPassword" in account: printer.out("certPassword in gce account not found", printer.ERROR) return if not "cert" in account: printer.out("cert in gce account not found", printer.ERROR) return if not "name" in account: printer.out("name for gce account not found", printer.ERROR) return myCredAccount.type_ = "google" myCredAccount.login = account["username"] myCredAccount.password = account["certPassword"] myCredAccount.name = account["name"] myCertificates = certificates() myCredAccount.certificates = myCertificates try: myCertificate = certificate() with open(account["cert"], "r") as myfile: myCertificate.certStr = myfile.read() myCertificate.type_ = "googleCertificate" myCertificate.name = ntpath.basename(account["cert"]) myCertificates.add_certificate(myCertificate) except IOError as e: printer.out("File error: " + str(e), printer.ERROR) return return myCredAccount
def check_yaml_syntax(file): try: yaml_data=open(file) data = yaml.load(yaml_data) yaml_data.close() return data except ValueError as e: printer.out("Syntax of template file ["+file+"] FAILED", printer.ERROR) printer.out("YAML parsing error: "+str(e)) return except IOError as e: printer.out("File error: "+str(e), printer.ERROR) return
def publish_vcenter(pimage, builder): #doing field verification if not "datacenterName" in builder: printer.out("datacenterName in vcenter builder not found", printer.ERROR) return if not "cluster" in builder: printer.out("cluster in vcenter builder not found", printer.ERROR) return if not "datastore" in builder: printer.out("datastore in vcenter builder not found", printer.ERROR) return if not "imageName" in builder: printer.out("imageName in vcenter builder not found", printer.ERROR) return pimage.credAccount.clusterName = builder["cluster"] pimage.credAccount.datacenterName = builder["datacenterName"] pimage.credAccount.datastore = builder["datastore"] pimage.credAccount.displayName = builder["imageName"] return pimage
def nimbula(myCredAccount, account): # doing field verification if not "password" in account: printer.out("password in nimbula account not found", printer.ERROR) return if not "username" in account: printer.out("username in nimbula account not found", printer.ERROR) return if not "endpoint" in account: printer.out("endpoint for nimbula account not found", printer.ERROR) return if not "name" in account: printer.out("name for nimbula account not found", printer.ERROR) return myCredAccount.type_ = "nimbula" myCredAccount.login = account["username"] myCredAccount.password = account["password"] myCredAccount.serverUrl = account["endpoint"] myCredAccount.name = account["name"] return myCredAccount
def abiquo(myCredAccount, account): # doing field verification if not "password" in account: printer.out("password in abiquo account not found", printer.ERROR) return if not "username" in account: printer.out("username in abiquo account not found", printer.ERROR) return if not "hostname" in account: printer.out("hostname for abiquo account not found", printer.ERROR) return if not "name" in account: printer.out("name for abiquo account not found", printer.ERROR) return myCredAccount.type_ = "abiquo" myCredAccount.login = account["username"] myCredAccount.password = account["password"] myCredAccount.hostname = account["hostname"] myCredAccount.name = account["name"] return myCredAccount
def cloudstack(myCredAccount, account): # doing field verification if not "publicKey" in account: printer.out("publicKey in cloudstack account not found", printer.ERROR) return if not "secretKey" in account: printer.out("secretKey in cloudstack account not found", printer.ERROR) return if not "endpoint" in account: printer.out("endpoint for cloudstack account not found", printer.ERROR) return if not "name" in account: printer.out("name for cloudstack account not found", printer.ERROR) return myCredAccount.type_ = "cloudcom" myCredAccount.publicAPIKey = account["publicKey"] myCredAccount.secretAPIKey = account["secretKey"] myCredAccount.serverUrl = account["endpoint"] myCredAccount.name = account["name"] return myCredAccount
def publish_vcd(pimage, builder): #doing field verification if not "orgName" in builder: printer.out("orgName in vcd builder not found", printer.ERROR) return if not "catalogName" in builder: printer.out("catalogName in vcd builder not found", printer.ERROR) return if not "imageName" in builder: printer.out("imageName in vcd builder not found", printer.ERROR) return pimage.credAccount.organizationName = builder["orgName"] pimage.credAccount.catalogId = builder["catalogName"] pimage.credAccount.displayName = builder["imageName"] return pimage
def create_user_ssh_key(api, login, sshKey): if not "name" in sshKey: printer.out("sshKey name not found in builder", printer.ERROR) return 2 if not "publicKey" in sshKey: printer.out("publicKey in sshKey not found in builder", printer.ERROR) return 2 mySshKey = sshKey() mySshKey.name=sshKey["name"] mySshKey.publicKey=sshKey["publicKey"] key = self.api.Users(login).Sshkeys().Create(mySshKey) if key is None: printer.out("Impossible to create sshKey ["+mySshKey.name+"]", printer.ERROR) return 2 return key
def publish_nimbula(pimage, builder): #doing field verification if not "imageListName" in builder: printer.out("imageListName in nimbula builder not found", printer.ERROR) return if not "imageVersion" in builder: printer.out("imageVersion in nimbula builder not found", printer.ERROR) return if not "description" in builder: printer.out("description in nimbula builder not found", printer.ERROR) return pimage.credAccount.imageVersion = builder["imageVersion"] pimage.credAccount.description = builder["description"] pimage.credAccount.listName = builder["imageListName"] return pimage
def print_uforge_exception(e): if len(e.args) >= 1 and type(e.args[0]) is UForgeError: printer.out(get_uforge_exception(e), printer.ERROR) else: traceback.print_exc()