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 = generics_utils.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 test_check_bundle_should_failed_when_tag_softwarefile_and_bootorder(self, mock_method): #Given jsonPath = "tests/integration/data/bundle/bundleFilesTagSoftwareFileKeyBootOrder.json" #When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) #Then mock_method.assert_called_with("There is the attribute [bootOrder] or [bootType] for file 'directoryTest' but is not tagged as 'bootscript'", "ERROR")
def test_check_bundle_should_failed_when_tag_bootscript_and_rights(self, mock_method): #Given jsonPath = "tests/integration/data/bundle/bundleFilesTagBootScriptKeyRights.json" #When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) #Then mock_method.assert_called_with("There is the attribute [ownerGroup], [rights] or [symlink] for file 'cleanup_tmp.sh' but is not tagged as 'softwarefile'", "ERROR")
def test_check_bundle_should_failed_when_tag_ospkg_in_directory(self, mock_method): #Given jsonPath = "tests/integration/data/bundle/bundleFilesTagOSPkgInDirectory.json" #When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) #Then mock_method.assert_called_with("The file 'iotop-0.6-2.el7.noarch.rpm, with tag 'ospkg' must be in the first level files section", "ERROR")
def test_check_bundle_should_failed_when_tag_ospkg_in_directory(self, mock_method): #Given jsonPath = "tests/integration/data/bundle/bundleFilesTagOSPkgInDirectory.json" #When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) #Then mock_method.assert_called_with("The file 'iotop-0.6-2.el7.noarch.rpm, with tag 'ospkg' must be in the first level files section", "ERROR")
def test_check_bundle_should_failed_when_tag_ospkg_and_two_oses(self, mock_method): #Given jsonPath = "tests/integration/data/bundle/bundleFilesTagOSPkgWithTwoOses.json" #When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) #Then mock_method.assert_called_with("The file 'iotop-0.6-2.el7.noarch.rpm' is tagged 'ospkg' but there is more than one os in [oses] attribute", "ERROR")
def test_check_bundle_should_succeed(self): #Given jsonPath = "tests/integration/data/bundle/bundleFull.json" #When bundle = generics_utils.check_json_syntax(jsonPath) bundle = bundle_utils.check_bundle(bundle) #Then self.assertIsNotNone(bundle)
def test_check_bundle_should_failed_when_no_files(self, mock_method): #Given jsonPath = "tests/integration/data/bundle/bundleWithoutFiles.json" #When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) #Then mock_method.assert_called_with("no attribute [files] for [bundle]", "ERROR")
def test_check_bundle_should_failed_when_files_no_source(self, mock_method): #Given jsonPath = "tests/integration/data/bundle/bundleFilesWithoutSource.json" #When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) #Then mock_method.assert_called_with("There is no attribute [source] for a [file]", "ERROR")
def test_check_bundle_should_failed_when_no_files(self, mock_method): #Given jsonPath = "tests/integration/data/bundle/bundleWithoutFiles.json" #When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) #Then mock_method.assert_called_with("no attribute [files] for [bundle]", "ERROR")
def test_check_bundle_should_failed_when_files_no_name(self, mock_method): #Given jsonPath = find_relative_path_for("tests/integration/data/bundle/bundleFilesWithoutName.json") #When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) #Then mock_method.assert_called_with("There is no attribute [name] for a [file]", "ERROR")
def test_check_bundle_should_failed_when_no_version(self, mock_method): #Given jsonPath = find_relative_path_for("tests/integration/data/bundle/bundleWithoutVersion.json") #When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) #Then mock_method.assert_called_with("no attribute [version] for [bundle]", "ERROR")
def test_check_bundle_should_succeed_when_empty_restrictionRule(self): # Given jsonPath = find_relative_path_for("tests/integration/data/bundle/bundleWithEmptyRestrictionRule.json") # When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) # Then self.assertIsNotNone(bundle)
def test_check_bundle_should_succeed_when_no_restrictionRule(self): # Given jsonPath = "tests/integration/data/bundle/bundleWithoutRestrictionRule.json" # When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) # Then self.assertIsNotNone(bundle)
def test_check_bundle_should_failed_when_tag_softwarefile_and_bootorder(self, mock_method): #Given jsonPath = "tests/integration/data/bundle/bundleFilesTagSoftwareFileKeyBootOrder.json" #When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) #Then mock_method.assert_called_with("There is the attribute [bootOrder] or [bootType] for file 'directoryTest' but is not tagged as 'bootscript'", "ERROR")
def test_check_bundle_should_failed_when_files_no_source(self, mock_method): #Given jsonPath = "tests/integration/data/bundle/bundleFilesWithoutSource.json" #When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) #Then mock_method.assert_called_with("There is no attribute [source] for a [file]", "ERROR")
def test_check_bundle_should_failed_when_tag_bootscript_and_rights(self, mock_method): #Given jsonPath = "tests/integration/data/bundle/bundleFilesTagBootScriptKeyRights.json" #When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) #Then mock_method.assert_called_with("There is the attribute [ownerGroup], [rights] or [symlink] for file 'cleanup_tmp.sh' but is not tagged as 'softwarefile'", "ERROR")
def test_check_bundle_should_succeed(self): #Given jsonPath = "tests/integration/data/bundle/bundleFull.json" #When bundle = generics_utils.check_json_syntax(jsonPath) bundle = bundle_utils.check_bundle(bundle) #Then self.assertIsNotNone(bundle)
def test_check_bundle_should_succeed_when_empty_restrictionRule(self): # Given jsonPath = "tests/integration/data/bundle/bundleWithEmptyRestrictionRule.json" # When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) # Then self.assertIsNotNone(bundle)
def test_check_bundle_should_succeed_when_no_restrictionRule(self): # Given jsonPath = find_relative_path_for( "tests/integration/data/bundle/bundleWithoutRestrictionRule.json") # When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) # Then self.assertIsNotNone(bundle)
def load_data(file): isJson = check_extension_is_json(file) if isJson: print "you provided a json file, checking the syntax..." data = generics_utils.check_json_syntax(file) else: print "you provided a yaml file, checking the syntax..." data = generics_utils.check_yaml_syntax(file) return data
def load_data(file): isJson = check_extension_is_json(file) if isJson: print "you provided a json file, checking the syntax..." data = generics_utils.check_json_syntax(file) else: print "you provided a yaml file, checking the syntax..." data = generics_utils.check_yaml_syntax(file) return data
def test_check_bundle_should_failed_when_files_no_name(self, mock_method): #Given jsonPath = find_relative_path_for( "tests/integration/data/bundle/bundleFilesWithoutName.json") #When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) #Then mock_method.assert_called_with( "There is no attribute [name] for a [file]", "ERROR")
def test_check_bundle_should_failed_when_no_version(self, mock_method): #Given jsonPath = find_relative_path_for( "tests/integration/data/bundle/bundleWithoutVersion.json") #When bundle = generics_utils.check_json_syntax(jsonPath) bundle_utils.check_bundle(bundle) #Then mock_method.assert_called_with("no attribute [version] for [bundle]", "ERROR")
def validate_deployment(file): try: isJson = check_extension_is_json(file) if isJson: data = generics_utils.check_json_syntax(file) else: data = generics_utils.check_yaml_syntax(file) if data is None: return if "provisioner" not in data: printer.out("There is no provisioner in the file", printer.ERROR) return data["provisioner"] except ValueError as e: printer.out("JSON parsing error: " + str(e), printer.ERROR) printer.out("Syntax of deployment file [" + file + "]: FAILED") except IOError as e: printer.out("unknown error deployment json file", printer.ERROR)
def validate_deployment(file): try: isJson = check_extension_is_json(file) if isJson: data = generics_utils.check_json_syntax(file) else: data = generics_utils.check_yaml_syntax(file) if data is None: return if "provisioner" not in data: printer.out("There is no provisioner in the file", printer.ERROR) return data["provisioner"] except ValueError as e: printer.out("JSON parsing error: " + str(e), printer.ERROR) printer.out("Syntax of deployment file [" + file + "]: FAILED") except IOError as e: printer.out("unknown error deployment json file", printer.ERROR)
def validate_configurations_file(file, isJson): if isJson: data = generics_utils.check_json_syntax(file) else: data = generics_utils.check_yaml_syntax(file) if data is None: return #check manadatory fields if "stack" in data: stack=check_mandatory_stack(data["stack"]) if stack is None: return #else: # print "No stack section find in the template file" # return if "builders" in data: check_mandatory_builders(data["builders"]) return data
def validate_configurations_file(file, isJson): if isJson: data = generics_utils.check_json_syntax(file) else: data = generics_utils.check_yaml_syntax(file) if data is None: return #check manadatory fields if "stack" in data: stack=check_mandatory_stack(data["stack"]) if stack is None: return if "bundles" in data["stack"]: for bundle in data["stack"]["bundles"]: bundle = check_bundle(bundle) if bundle is None: return if "builders" in data: check_mandatory_builders(data["builders"]) return data
def validate_configurations_file(file, isJson): if isJson: data = generics_utils.check_json_syntax(file) else: data = generics_utils.check_yaml_syntax(file) if data is None: return #check manadatory fields if "stack" in data: stack = check_mandatory_stack(data["stack"]) if stack is None: return if "bundles" in data["stack"]: for bundle in data["stack"]["bundles"]: bundle = check_bundle(bundle) if bundle is None: return if "builders" in data: check_mandatory_builders(data["builders"]) return data
def validate_bundle(file): try: isJson = check_extension_is_json(file) if isJson: print "you provided a json file, checking..." data = generics_utils.check_json_syntax(file) else: print "you provided a yaml file, checking..." data = generics_utils.check_yaml_syntax(file) if data is None: return data = check_bundle(data) if data is None: return return data except ValueError as e: printer.out("JSON parsing error: "+str(e), printer.ERROR) printer.out("Syntax of bundle file ["+file+"]: FAILED") except IOError as e: printer.out("unknown error bundle json file", printer.ERROR)
def validate_bundle(file): try: isJson = check_extension_is_json(file) if isJson: print "you provided a json file, checking..." data = generics_utils.check_json_syntax(file) else: print "you provided a yaml file, checking..." data = generics_utils.check_yaml_syntax(file) if data is None: return data = check_bundle(data) if data is None: return return data except ValueError as e: printer.out("JSON parsing error: " + str(e), printer.ERROR) printer.out("Syntax of bundle file [" + file + "]: FAILED") except IOError as e: printer.out("unknown error bundle json file", printer.ERROR)
def validate_json_file(file): try: data = generics_utils.check_json_syntax(file) if data is None: return #check manadatory fields if "stack" in data: stack=check_mandatory_stack(data["stack"]) if stack is None: return #else: # print "No stack section find in the template file" # return if "builders" in data: check_mandatory_builders(data["builders"]) return data except ValueError as e: printer.out("JSON parsing error: "+str(e), printer.ERROR) printer.out("Syntax of template file ["+file+"]: FAILED") except IOError as e: printer.out("unknown error template json file", printer.ERROR)
def do_build(self, args): try: # add arguments doParser = self.arg_build() try: doArgs = doParser.parse_args(args.split()) except SystemExit as e: return # -- file = generics_utils.get_file(doArgs.file) if file is None: return 2 data = generics_utils.check_json_syntax(file) if "builders" in data: builders = hammr_utils.check_mandatory_builders(data["builders"]) builders = hammr_utils.check_mandatory_generate_scan(builders) else: printer.out("no builder section found", printer.ERROR) return if builders is None: return try: myScannedInstances = self.api.Users(self.login).Scannedinstances.Getall(Includescans="true") if myScannedInstances is None or not hasattr(myScannedInstances, 'get_scannedInstance'): printer.out("scan not found", printer.ERROR) return else: myScan = None for myScannedInstance in myScannedInstances.get_scannedInstance(): for scan in myScannedInstance.scans.scan: if str(scan.dbId) == doArgs.id: myScan = scan myRScannedInstance = myScannedInstance break if myScan is not None: break if myScan is not None and myScan.status.complete and not myScan.status.error and not myScan.status.cancelled: i = 1 for builder in builders: printer.out( "Generating '" + builder["type"] + "' image (" + str(i) + "/" + str(len(builders)) + ")") format_type = builder["type"] myimage = image() myinstallProfile = installProfile() if "swapSize" in builder["installation"]: myinstallProfile.swapSize = builder["installation"]["swapSize"] myinstallProfile.diskSize = builder["installation"]["diskSize"] if format_type in generate_utils.CLOUD_FORMATS: func = getattr(generate_utils, "generate_" + generics_utils.remove_special_chars(format_type), None) if func: myimage, myimageFormat, myinstallProfile = func(myimage, builder, myinstallProfile, self.api, self.login) else: printer.out("Builder type unknown: " + format_type, printer.ERROR) return elif format_type in generate_utils.VIRTUAL_FORMATS: func = getattr(generate_utils, "generate_" + generics_utils.remove_special_chars(format_type), None) if func: myimage, myimageFormat, myinstallProfile = func(myimage, builder, myinstallProfile) else: printer.out("Builder type unknown: " + format_type, printer.ERROR) return elif format_type in generate_utils.PHYSICAL_FORMATS: func = getattr(generate_utils, "generate_" + generics_utils.remove_special_chars(format_type), None) if func: myimage, myimageFormat, myinstallProfile = func(myimage, builder, myinstallProfile) else: printer.out("Builder type unknown: " + format_type, printer.ERROR) return else: printer.out("Builder type unknown: " + format_type, printer.ERROR) return myimage.format = myimageFormat myimage.installProfile = myinstallProfile rImage = self.api.Users(self.login).Scannedinstances(myRScannedInstance.dbId).Scans( myScan.dbId).Images().Generate(myimage) status = rImage.status statusWidget = progressbar_widget.Status() statusWidget.status = status widgets = [Bar('>'), ' ', statusWidget, ' ', ReverseBar('<')] progress = ProgressBar(widgets=widgets, maxval=100).start() while not (status.complete or status.error or status.cancelled): statusWidget.status = status progress.update(status.percentage) status = self.api.Users(self.login).Scannedinstances(myRScannedInstance.dbId).Scans( myScan.dbId).Images(Sitid=rImage.dbId).Status.Get() time.sleep(2) statusWidget.status = status progress.finish() if status.error: printer.out("Generation '" + builder[ "type"] + "' error: " + status.message + "\n" + status.errorMessage, printer.ERROR) if status.detailedError: printer.out(status.detailedErrorMsg) elif status.cancelled: printer.out("Generation '" + builder["type"] + "' canceled: " + status.message, printer.ERROR) else: printer.out("Generation '" + builder["type"] + "' ok", printer.OK) i += 1 else: printer.out("Impossible to generate this scan", printer.ERROR) except KeyError as e: printer.out("unknown error template json file", printer.ERROR) except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_build() except KeyboardInterrupt: printer.out("\n") if generics_utils.query_yes_no("Do you want to cancel the job ?"): if 'myRScannedInstance' in locals() and 'myScan' in locals() and 'rImage' in locals() \ and hasattr(myRScannedInstance, 'dbId') and hasattr(myScan, 'dbId') and hasattr(rImage, 'dbId'): self.api.Users(self.login).Scannedinstances(myRScannedInstance.dbId).Scans(myScan.dbId).Images( Sitid=rImage.dbId).Status.Cancel() else: print "Exiting command" except Exception as e: return handle_uforge_exception(e)
def do_create(self, args): try: # add arguments doParser = self.arg_create() doArgs = doParser.parse_args(args.split()) #if the help command is called, parse_args returns None object if not doArgs: return 2 # -- file = generics_utils.get_file(doArgs.file) if file is None: return 2 data = generics_utils.check_json_syntax(file) if data is None: return 2 if "builders" in data: accounts_file_type = "builders" iterables = check_mandatory_create_account(data["builders"], accounts_file_type) elif "accounts" in data: accounts_file_type = "accounts" iterables = check_mandatory_create_account(data["accounts"], accounts_file_type) else: printer.out("Error: no builders or accounts section found", printer.ERROR) return 2 if iterables is None: return 2 try: for iterable in iterables: myCredAccount = None if "account" in iterable: account_type = iterable["account"]["type"] elif "type" in iterable: account_type = iterable["type"] targetPlatform = account_utils.get_target_platform_object(self.api, self.login, account_type) if targetPlatform is None: printer.out("Platform type unknown: "+account_type, printer.ERROR) return 2 func = getattr(account_utils, generics_utils.remove_special_chars(targetPlatform.type), None) if func: if accounts_file_type == "builders" and "account" in iterable: myCredAccount = func(iterable["account"]) elif accounts_file_type == "accounts": myCredAccount = func(iterable) else: pass # DO NOTHING - no account in builder to create # TODO # the account type must be in the account part, if no builder part (independant file) if myCredAccount is not None: myCredAccount = account_utils.assign_target_platform_account(myCredAccount, targetPlatform.name) printer.out("Create account for '" + account_type + "'...") self.api.Users(self.login).Accounts.Create(body=myCredAccount, element_name="ns1:credAccount") printer.out("Account create successfully for [" + account_type + "]", printer.OK) return 0 except KeyError as e: printer.out("unknown error template json file", printer.ERROR) except IOError as e: printer.out("File error: " + str(e), printer.ERROR) except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_create() except Exception as e: return handle_uforge_exception(e)