def execute_git_push(self, transformationInput: TransformationInput):
        properties = GetProperties()
        commands = properties.get_git_push_command_file()
        runCommandLine = RunCommandLine()
        createRepository = CreateRepository()
        _target_area = createRepository.get_targetArea_Dir()
        original_cur_dir = os.getcwd()
        result = None
        try:
            os.chdir(_target_area)
            for command in commands:
                command = command.strip()
                if command.find("<%message%>") != -1:
                    command = command.replace(
                        "<%message%>", "\"Automated check in at " +
                        str(int(time.time())) + "\"")
                return_code = runCommandLine.run_command_line(command, "run")
                if return_code != 0:
                    raise subprocess.CalledProcessError
                else:
                    result = "success"
        except Exception as e:
            print("Error occurred while executing command : " + command)
            print(e)
            os.chdir(original_cur_dir)
            result = str(e)

        os.chdir(original_cur_dir)
        return result
Beispiel #2
0
    def check_rules_against_dependencies_aws(self,
                                             dependency_scan_results_model):
        getProperties = GetProperties()
        source_dict = dependency_scan_results_model.get_extra_dependencies_in_source_dict(
        )
        target_dict = dependency_scan_results_model.get_extra_dependencies_in_target_dict(
        )
        supported_source_list = getProperties.get_supported_environment_source_dependencies(
        )
        for key in source_dict:
            if key not in supported_source_list:
                return False
        for key in target_dict:
            if key not in supported_source_list:
                return False
        return True

        return True
Beispiel #3
0
 def check_rules_against_dependencies_google(self,
                                             dependency_scan_results_model):
     getProperties = GetProperties()
     source_dict = dependency_scan_results_model.get_extra_dependencies_in_source_dict(
     )
     target_dict = dependency_scan_results_model.get_extra_dependencies_in_target_dict(
     )
     supported_source_list = getProperties.get_supported_environment_source_dependencies(
     )
     for key in source_dict:
         if key == 'NO extra dependencies in source':
             return True
         if key not in supported_source_list:
             return source_dict[key]
     for key in target_dict:
         if key not in supported_source_list:
             return source_dict[key]
     return 'True'
    def execute_fetch_github_url(self,
                                 transformationInput: TransformationInput):
        properties = GetProperties()
        commands = properties.get_git_clone_commands()
        runCommandLine = RunCommandLine()
        createRepository = CreateRepository()
        _staging_area = createRepository.get_stagingArea_Dir()

        result = None
        result = self.fetchFilesFromGithub(
            _staging_area, commands, result, runCommandLine,
            transformationInput.get_source_github_url())

        if result.find("Error") == 0:
            raise Exception(result)

        _target_area = createRepository.get_targetArea_Dir()
        result = self.fetchFilesFromGithub(
            _target_area, commands, result, runCommandLine,
            transformationInput.get_target_github_url())

        if result.find("Error") == 0:
            raise Exception(result)
        return result
Beispiel #5
0
    def transform(self, transformationInput):
        print(transformationInput.get_source_github_url())
        print(transformationInput.get_targetcloudprovider())

        # Create report file which will be used to add status at each step and their result
        reporting = Reporting(transformation_input=transformationInput)

        # 1. Create a staging area
        createRepository = CreateRepository()

        try:
            _stagingArea = createRepository.get_stagingArea_Dir()
            _targetARea = createRepository.get_targetArea_Dir()
        except Exception as e:
            error = "Error occurred while creating staging area : " + e
            return reporting.add_to_report(error=error)

        reporting.add_to_report(error="Success : Staging and Target area created")

        commandLineExecutor = CommandLineExecutor()
        # 2. Fetch git URL
        try:
            commandLineExecutor.execute("FetchGitHub", transformationInput)
        except Exception as e:
            error = "Error occurred while fetching Git URL : " + e
            return reporting.add_to_report(error=error)

        reporting.add_to_report(error="Success : Git hub fetch URL Success")


        # 3. Scan : compare POM.xmls of targetArea and staging area : if answer is Yes
        # Then copy src folder to target and push changes
        # Target repo should be existing and connected to cloud build
        dependencyScanning = DependencyScanning()
        try:
            dependency_scan_results_model = dependencyScanning.scan_dependencies(_stagingArea, _targetARea)
        except Exception as e:
            error = "Error occurred while scanning dependencies : " + e
            return reporting.add_to_report(error=error)

        reporting.add_to_report(error="Success : Scanning dependencies done")

        # 4. Dependencies failed and the code cannot be transformed
        if not dependency_scan_results_model.get_dependencies_satisfied():
            jsonObject = json.dumps(dependency_scan_results_model.get_missing_required_dependencies_in_target())
            error = "Missing dependencies dependencies found during scaning: " + jsonObject
            return reporting.add_to_report(error=error)
            return jsonObject

        ruleEngine = RuleEngine()
        try:
            isSourceSupported = ruleEngine.check_rules_against_dependencies_google(dependency_scan_results_model)
        except Exception as e:
            error = "Error occurred while checking rules against dependencies : " + e
            return reporting.add_to_report(error=error)

        reporting.add_to_report(error="Success : Rules checked against dependencies")

        try:

            if str(isSourceSupported) != 'True':
                getProperties = GetProperties()
                can_transform_unsupported_dependency = getProperties. \
                    get_transform_unsupported_dependency(unsupported_dependency=isSourceSupported)
                if not can_transform_unsupported_dependency:
                    return "Unsupported dependency cannot be transformed!"
                else:
                    # 5. Transform unsupported dependencies
                    isTransformed = ruleEngine.transform_unsupported_dependencies(unsupported_dependency=isSourceSupported,
                                                                  staging_area=_stagingArea)
                    if not isTransformed:
                        return "Unsupported dependency transformation failed."
        except Exception as e:
            error = "Error occurred while transforming unsupported dependencies : " + e
            return reporting.add_to_report(error=error)

        reporting.add_to_report(error="Success : Unsupported dependencies checked")

        # 6. Copy functionality : transform code
        transform_code = TransformCode()
        try:
            updated_dir = transform_code.TransformCode(_stagingArea, _targetARea)
        except Exception as e:
            error = "Error occurred while transforming code by copying into template folder : " + e
            return reporting.add_to_report(error=error)

        reporting.add_to_report(error="Success : Transformation done by copying src to template folder")

        # 7. Check if the solution is Google, then copy ServletInitalizer
        try:
            if str(transformationInput.get_targetcloudprovider()).lower() == "google":
                update_google_project = UpdateGoogleProject()
                update_google_project.copy_servlet_initializer(_targetARea)
        except Exception as e:
            error = "Error occurred while copying ServletInitalizer for Google dependencies : " + e
            return reporting.add_to_report(error=error)

        reporting.add_to_report(error="Success : Google dependency checked for ServletInitalizer")

        # 8. Deploy by pushing the target to its git branch
        try:
            if transformationInput.get_is_deploy() == "true":
                commandLineExecutor.execute("gitpush", transformationInput)
        except Exception as e:
            error = "Error occurred while Git push : " + e
            return reporting.add_to_report(error=error)

        reporting.add_to_report(error="Success : Code pushed to Git, check Code Build pipeline!")

        return os.path.realpath(updated_dir)
Beispiel #6
0
 def get_lines_to_be_added_from_model(self):
     get_properties = GetProperties()
     return get_properties.get_lines_to_be_added_to_model()
Beispiel #7
0
 def get_repo_lines_to_be_updated(self):
     getProperties = GetProperties()
     return getProperties.get_update_imports_statement_repository()