Пример #1
0
    def __init__(self, deployment):
        if deployment:
            self.uuid = str(uuid.uuid4())
            self.deployment = deployment
            self.deployment_uuid = deployment.uuid
            self.storage_path = os.path.join(get_path(), self.__tablename__,
                                             self.uuid)
            self.plugin = None
            self.test_artifact = deployment.test_artifact

            plugins_available = ModuleLoader().load('plugins')
            for p in plugins_available:
                if p.plugin_type == self.test_artifact.plugin:
                    self.plugin = p

            if not self.plugin:
                raise LookupError(
                    f"Could not find matching plugin for '{self.test_artifact.plugin}'."
                )

            db_session.add(self)
            db_session.commit()

            if not os.path.exists(self.fq_storage_path):
                os.makedirs(self.fq_storage_path)
        else:
            raise Exception(f'Linked entities do not exist.')
Пример #2
0
    def __init__(self, uuid, name, repository_url, storage_path):
        self.uuid = uuid
        self.name = name
        self.repository_url = repository_url
        self.storage_path = storage_path
        self.che_env = is_che_env()

        current_app.logger.info(f"CHE environment: {self.che_env}")

        if not path.exists(self.fq_storage_path):
            makedirs(self.fq_storage_path)
            current_app.logger.info(
                f"Created directory path {self.fq_storage_path}")

        if self.che_env:
            src_path = path.join(get_dir_prefix(), self.repository_url)
            current_app.logger.info(
                f'Copying directory tree from  {src_path} into {self.fq_storage_path}'
            )
            copytree(src_path, self.fq_storage_path, dirs_exist_ok=True)
        else:
            current_app.logger.info(
                f'Cloning repository {self.repository_url} into {self.fq_storage_path}'
            )
            git.Git(self.fq_storage_path).clone(self.repository_url,
                                                self.fq_storage_path)

        db_session.add(self)
        db_session.commit()
Пример #3
0
    def __init__(self, execution):
        self.uuid = str(uuid.uuid4())
        self.execution_uuid = execution.uuid
        self.storage_path = os.path.join(self.__tablename__, self.uuid)

        if execution and os.path.isfile(execution.fq_result_storage_path):
            if not os.path.exists(self.fq_storage_path):
                os.makedirs(self.fq_storage_path)

            # Potentially post-process the results before storing them.
            shutil.copy2(execution.fq_result_storage_path,
                         self.fq_result_storage_path)
            db_session.add(self)
            db_session.commit()
            current_app.logger.info(f'AutoUndeploy is set to {AutoUndeploy}.')
            if AutoUndeploy:
                current_app.logger.info(f'Initiating undeployment.')
                linked_execution: Execution = Execution.get_by_uuid(
                    self.execution_uuid)
                linked_execution.undeploy()
            else:
                current_app.logger.info(
                    f'NOT initiating undeployment due to disabled AutoUndeploy.'
                )
        else:
            raise Exception(f'Linked entities do not exist.')
Пример #4
0
    def __init__(self, project, sut_tosca_path, ti_tosca_path, policy, plugin):
        self.uuid = str(uuid.uuid4())
        self.project_uuid = project.uuid
        self.sut_tosca_path = sut_tosca_path
        self.ti_tosca_path = ti_tosca_path
        self.storage_path = os.path.join(BasePath, self.__tablename__,
                                         self.uuid)
        self.policy = policy
        self.plugin = plugin

        if not os.path.exists(self.fq_storage_path):
            os.makedirs(self.fq_storage_path)

        self.commit_hash = project.commit_hash

        # Copy repository excluding the '.git' directory
        src_dir = project.fq_storage_path
        if os.path.isdir(src_dir) and os.path.isdir(self.fq_storage_path):
            copytree(src_dir,
                     self.fq_storage_path,
                     ignore=ignore_patterns('.git'),
                     dirs_exist_ok=True)

        db_session.add(self)
        db_session.commit()
Пример #5
0
 def __init__(self, testartifact):
     self.uuid = str(uuid.uuid4())
     self.testartifact_uuid = testartifact.uuid
     if testartifact:
         db_session.add(self)
         db_session.commit()
     else:
         raise Exception(f'Linked entities do not exist.')
Пример #6
0
    def deploy(self):
        test_artifact = TestArtifact.get_by_uuid(self.testartifact_uuid)
        sut_csar_path = os.path.join(test_artifact.fq_storage_path,
                                     test_artifact.sut_tosca_path)
        ti_csar_path = os.path.join(test_artifact.fq_storage_path,
                                    test_artifact.ti_tosca_path)

        # Deployment of SuT
        with Csar(sut_csar_path, extract_dir=self.sut_storage_path,
                  keep=True) as sut_csar:
            if DropPolicies:
                sut_csar.drop_policies()
            entry_definition = sut_csar.tosca_entry_point

            current_app.logger.\
                info(f'Deploying SuT {str(entry_definition)} with opera in folder {str(self.sut_storage_path)}.')
            subprocess.call(['opera', 'deploy', entry_definition],
                            cwd=self.sut_storage_path)

        # Deployment of TI
        with Csar(ti_csar_path, extract_dir=self.ti_storage_path,
                  keep=True) as ti_csar:
            if DropPolicies:
                ti_csar.drop_policies()
            entry_definition = ti_csar.tosca_entry_point

            if entry_definition:
                current_app.logger.\
                    info(f'Deploying TI {str(entry_definition)} with opera in folder {str(self.ti_storage_path)}.')
                subprocess.call(['opera', 'deploy', entry_definition],
                                cwd=self.ti_storage_path)

        time.sleep(30)

        envFaasScenario = os.getenv('CTT_FAAS_ENABLED')
        faas_mode = False
        if envFaasScenario:
            if envFaasScenario == "1":
                faas_mode = True
        elif FaasScenario:
            faas_mode = True

        if faas_mode:
            # FaaS scenario
            deployed_systems = Deployment.deployment_workaround(
                exclude_sut=True)
            self.sut_hostname = self.__test_artifact.policy_yaml['properties'][
                'hostname']
            self.ti_hostname = deployed_systems['ti']
        else:
            deployed_systems = Deployment.deployment_workaround(
                exclude_sut=False)
            self.sut_hostname = deployed_systems['sut']
            self.ti_hostname = deployed_systems['ti']

        db_session.add(self)
        db_session.commit()
Пример #7
0
 def delete_by_uuid(cls, del_uuid):
     execution = Execution.query.filter_by(uuid=del_uuid)
     if execution:
         from models.result import Result
         linked_results = Result.query.filter_by(execution_uuid=del_uuid)
         for result in linked_results:
             Result.delete_by_uuid(result.uuid)
         execution.delete()
         # rmtree(self.fq_storage_path)
         db_session.commit()
Пример #8
0
    def __init__(self, deployment):
        self.uuid = str(uuid.uuid4())
        self.deployment_uuid = deployment.uuid
        self.storage_path = os.path.join(BasePath, self.__tablename__,
                                         self.uuid)

        if deployment:
            db_session.add(self)
            db_session.commit()
        else:
            raise Exception(f'Linked entities do not exist.')
Пример #9
0
    def delete_by_uuid(cls, del_uuid):
        deployment = Deployment.query.filter_by(uuid=del_uuid)
        if deployment:
            from models.execution import Execution
            linked_executions = Execution.query.filter_by(deployment_uuid=del_uuid)
            for result in linked_executions:
                Execution.delete_by_uuid(result.uuid)

            deployment.delete()
            # rmtree(self.fq_storage_path)
            db_session.commit()
Пример #10
0
 def delete_by_uuid(cls, del_uuid):
     project = Project.query.filter_by(uuid=del_uuid)
     if project:
         folder_to_delete = project.first().fq_storage_path
         from models.testartifact import TestArtifact
         linked_testartifacts = TestArtifact.query.filter_by(project_uuid=del_uuid)
         for result in linked_testartifacts:
             TestArtifact.delete_by_uuid(result.uuid)
         project.delete()
         rmtree(folder_to_delete)
         db_session.commit()
Пример #11
0
 def delete_by_uuid(cls, del_uuid):
     testartifact = TestArtifact.query.filter_by(uuid=del_uuid)
     if testartifact:
         folder_to_delete = testartifact.first().fq_storage_path
         from models.deployment import Deployment
         linked_deployments = Deployment.query.filter_by(testartifact_uuid=del_uuid)
         for result in linked_deployments:
             Deployment.delete_by_uuid(result.uuid)
         testartifact.delete()
         rmtree(folder_to_delete)
         db_session.commit()
Пример #12
0
    def __init__(self, execution):
        self.uuid = str(uuid.uuid4())
        self.execution_uuid = execution.uuid
        self.storage_path = os.path.join(BasePath, self.__tablename__,
                                         self.uuid)

        self.fq_storage_path = os.path.join(BasePath, self.storage_path)

        if execution:
            db_session.add(self)
            db_session.commit()
        else:
            raise Exception(f'Linked entities do not exist.')
Пример #13
0
 def create(cls, deployment_uuid):
     linked_deployment = Deployment.get_by_uuid(deployment_uuid)
     execution = Execution(linked_deployment)
     config_uuid = execution.configure()
     if config_uuid:
         exec_uuid = execution.execute(config_uuid)
         if exec_uuid:
             execution.get_results(exec_uuid)
             db_session.add(execution)
             db_session.commit()
     else:
         current_app.logger.info(
             "Execution could not be triggered. No config_uuid provided.")
     return execution
Пример #14
0
    def __init__(self, execution):
        self.uuid = str(uuid.uuid4())
        self.execution_uuid = execution.uuid
        self.storage_path = os.path.join(self.__tablename__, self.uuid)

        if execution and os.path.isfile(execution.fq_result_storage_path):
            if not os.path.exists(self.fq_storage_path):
                os.makedirs(self.fq_storage_path)

            # Potentially post-process the results before storing them.
            shutil.copy2(execution.fq_result_storage_path, self.fq_result_storage_path)
            db_session.add(self)
            db_session.commit()
        else:
            raise Exception(f'Linked entities do not exist.')
Пример #15
0
    def __init__(self, uuid, name, repository_url, storage_path):
        self.uuid = uuid
        self.name = name
        self.repository_url = repository_url
        self.storage_path = storage_path

        if not path.exists(self.fq_storage_path):
            makedirs(self.fq_storage_path)
            current_app.logger.info(f"Created directory path {self.fq_storage_path}")

        current_app.logger.info(f"Cloning repository {self.repository_url} into {self.fq_storage_path}")
        git.Git(self.fq_storage_path).clone(self.repository_url, self.fq_storage_path)

        db_session.add(self)
        db_session.commit()
Пример #16
0
    def __init__(self, testartifact):
        self.uuid = str(uuid.uuid4())
        self.testartifact_uuid = testartifact.uuid
        self.__test_artifact = testartifact
        self.sut_hostname = 'localhost'
        self.ti_hostname = 'localhost'

        if testartifact:
            db_session.add(self)
            db_session.commit()
        else:
            raise Exception(f'Linked entities do not exist.')

        if not os.path.exists(self.storage_path):
            os.makedirs(self.storage_path)
Пример #17
0
 def delete_by_uuid(cls, del_uuid):
     project = Project.query.filter_by(uuid=del_uuid)
     project_entity = project.first()
     if project and project_entity:
         folder_to_delete = project_entity.fq_storage_path
         from models.testartifact import TestArtifact
         linked_testartifacts = TestArtifact.query.filter_by(
             project_uuid=del_uuid)
         for result in linked_testartifacts:
             TestArtifact.delete_by_uuid(result.uuid)
         project.delete()
         rmtree(folder_to_delete)
         db_session.commit()
     else:
         warning_msg = f'{cls.__name__} with UUID {del_uuid} does not exist. So not deleted.'
         current_app.logger.warning(warning_msg)
Пример #18
0
    def __init__(self, project, sut_tosca_path, sut_inputs_path,
                 ti_tosca_path, ti_inputs_path, tmp_dir, policy, plugin):
        self.uuid = str(uuid.uuid4())
        self.project_uuid = project.uuid
        self.storage_path = os.path.join(get_path(), self.__tablename__, self.uuid)
        self.policy = policy
        self.plugin = plugin

        if not os.path.exists(self.fq_storage_path):
            os.makedirs(self.fq_storage_path)

        self.commit_hash = project.commit_hash

        # Copy repository excluding the '.git' directory
        src_dir = project.fq_storage_path
        if os.path.isdir(src_dir) and os.path.isdir(self.fq_storage_path):
            copytree(src_dir, self.fq_storage_path, ignore=ignore_patterns('.git'), dirs_exist_ok=True)

        # Set default paths for the artifacts and copy them into the testartifact folder in the RepoDir of RadonCTT
        self.sut_tosca_path = os.path.join(RepoDir, TestArtifact.sut_default_file_name)
        sut_tosca_path_fq = os.path.join(self.fq_storage_path, self.sut_tosca_path)
        os.makedirs(os.path.dirname(sut_tosca_path_fq), exist_ok=True)
        copy(sut_tosca_path, sut_tosca_path_fq)

        self.ti_tosca_path = os.path.join(RepoDir, TestArtifact.ti_default_file_name)
        ti_tosca_path_fq = os.path.join(self.fq_storage_path, self.ti_tosca_path)
        os.makedirs(os.path.dirname(ti_tosca_path_fq), exist_ok=True)
        copy(ti_tosca_path, ti_tosca_path_fq)

        if sut_inputs_path:
            self.sut_inputs_path = os.path.join(RepoDir, TestArtifact.sut_inputs_default_file_name)
            sut_inputs_path_fq = os.path.join(self.fq_storage_path, self.sut_inputs_path)
            os.makedirs(os.path.dirname(sut_inputs_path_fq), exist_ok=True)
            copy(sut_inputs_path, sut_inputs_path_fq)

        if ti_inputs_path:
            self.ti_inputs_path = os.path.join(RepoDir, TestArtifact.ti_inputs_default_file_name)
            ti_inputs_path_fq = os.path.join(self.fq_storage_path, self.ti_inputs_path)
            os.makedirs(os.path.dirname(ti_inputs_path_fq), exist_ok=True)
            copy(ti_inputs_path, ti_inputs_path_fq)

        db_session.add(self)
        db_session.commit()

        rmtree(tmp_dir, ignore_errors=True)
Пример #19
0
    def __init__(self, uuid, name, repository_url, storage_path):
        self.uuid = uuid
        self.name = name
        self.repository_url = repository_url
        self.storage_path = storage_path
        self.che_env = is_che_env()
        self.auto_undeploy = AutoUndeploy

        current_app.logger.info(f"CHE environment: {self.che_env}")
        current_app.logger.info(f"Auto Undeploy: {self.auto_undeploy}")

        if not path.exists(self.fq_storage_path):
            makedirs(self.fq_storage_path)
            current_app.logger.info(
                f"Created directory path {self.fq_storage_path}")

        if self.che_env:
            src_path = path.join(get_dir_prefix(), self.repository_url)
            current_app.logger.info(
                f'Copying directory tree from  {src_path} into {self.fq_storage_path}'
            )
            copytree(src_path, self.fq_storage_path, dirs_exist_ok=True)
        else:
            current_app.logger.info(
                f'Cloning repository {self.repository_url} into {self.fq_storage_path}'
            )

            # If credentials are provided, we inject them into the URL
            credentials = git_credentials()
            repo_url = self.repository_url
            if credentials:
                current_app.logger.info(
                    f"Using provided credentials with user '{credentials['username']}'"
                )
                parsed_url = urlparse(self.repository_url)
                repo_url = f"{parsed_url.scheme}://{credentials['username']}:{credentials['password']}@" \
                           f"{parsed_url.netloc}{parsed_url.path}"

            git.Git(self.fq_storage_path).clone(repo_url, self.fq_storage_path)

        db_session.add(self)
        db_session.commit()
Пример #20
0
    def deploy(self):
        test_artifact = TestArtifact.get_by_uuid(self.testartifact_uuid)
        sut_csar_path = os.path.join(test_artifact.fq_storage_path,
                                     test_artifact.sut_tosca_path)
        ti_csar_path = os.path.join(test_artifact.fq_storage_path,
                                    test_artifact.ti_tosca_path)

        if test_artifact.sut_inputs_path:
            sut_inputs_path = os.path.join(test_artifact.fq_storage_path,
                                           test_artifact.sut_inputs_path)
        else:
            sut_inputs_path = None

        if test_artifact.ti_inputs_path:
            ti_inputs_path = os.path.join(test_artifact.fq_storage_path,
                                          test_artifact.ti_inputs_path)
        else:
            ti_inputs_path = None

        # Deployment of SuT
        with Csar(sut_csar_path, extract_dir=self.sut_storage_path,
                  keep=True) as sut_csar:
            if DropPolicies:
                sut_csar.drop_policies()
            entry_definition = sut_csar.tosca_entry_point

            current_app.logger.\
                info(f'Deploying SuT {str(entry_definition)} with opera in folder {str(self.sut_storage_path)}.')
            try:
                if sut_inputs_path:
                    subprocess.call([
                        'opera', 'deploy', '-p', self.sut_storage_path, '-i',
                        sut_inputs_path, entry_definition
                    ],
                                    cwd=self.sut_storage_path)
                else:
                    subprocess.call([
                        'opera', 'deploy', '-p', self.sut_storage_path,
                        entry_definition
                    ],
                                    cwd=self.sut_storage_path)
                opera_outputs = subprocess.check_output(
                    ['opera', 'outputs', '-p', self.sut_storage_path],
                    cwd=self.sut_storage_path)
                current_app.logger.info(
                    f'Opera returned output {opera_outputs}.')
            except OperationError:
                subprocess.call(
                    ['opera', 'undeploy', '-p', self.sut_storage_path])

        # Deployment of TI
        with Csar(ti_csar_path, extract_dir=self.ti_storage_path,
                  keep=True) as ti_csar:
            if DropPolicies:
                ti_csar.drop_policies()
            entry_definition = ti_csar.tosca_entry_point

            if entry_definition:
                current_app.logger.\
                    info(f'Deploying TI {str(entry_definition)} with opera in folder {str(self.ti_storage_path)}.')

                try:
                    if ti_inputs_path:
                        subprocess.call([
                            'opera', 'deploy', '-p', self.ti_storage_path,
                            '-i', ti_inputs_path, entry_definition
                        ],
                                        cwd=self.ti_storage_path)
                    else:
                        subprocess.call([
                            'opera', 'deploy', '-p', self.ti_storage_path,
                            entry_definition
                        ],
                                        cwd=self.ti_storage_path)
                    opera_outputs = subprocess.check_output(
                        ['opera', 'outputs', '-p', self.ti_storage_path],
                        cwd=self.ti_storage_path)
                    current_app.logger.info(
                        f'Opera returned output {opera_outputs}.')
                    opera_yaml_outputs = yaml.safe_load(opera_outputs)
                except OperationError:
                    subprocess.call(
                        ['opera', 'undeploy', '-p', self.ti_storage_path])

        time.sleep(30)

        # FaaS scenario
        # deployed_systems = Deployment.deployment_workaround(exclude_sut=True)
        self.sut_hostname = self.__test_artifact.policy_yaml['properties'][
            'hostname']
        current_app.logger.info(f'SUT hostname {self.sut_hostname}.')
        self.ti_hostname = opera_yaml_outputs['public_address']['value']
        current_app.logger.info(f'TI hostname {self.ti_hostname}.')
        # self.ti_hostname = deployed_systems['ti']

        db_session.add(self)
        db_session.commit()
Пример #21
0
    def deploy(self):
        test_artifact = TestArtifact.get_by_uuid(self.testartifact_uuid)
        sut_csar_path = os.path.join(test_artifact.fq_storage_path, test_artifact.sut_tosca_path)
        ti_csar_path = os.path.join(test_artifact.fq_storage_path, test_artifact.ti_tosca_path)
        sut_success: bool = False

        if test_artifact.sut_inputs_path:
            sut_inputs_path = os.path.join(test_artifact.fq_storage_path, test_artifact.sut_inputs_path)
        else:
            sut_inputs_path = None

        if test_artifact.ti_inputs_path:
            ti_inputs_path = os.path.join(test_artifact.fq_storage_path, test_artifact.ti_inputs_path)
        else:
            ti_inputs_path = None

        # Deployment of SuT
        with Csar(sut_csar_path, extract_dir=self.sut_storage_path, keep=True) as sut_csar:
            if DropPolicies:
                sut_csar.drop_policies()
            entry_definition = sut_csar.tosca_entry_point

            if not is_test_mode():
                current_app.logger. \
                    info(f'Deploying SuT {str(entry_definition)} with opera in folder {str(self.sut_storage_path)}.')

                return_code: int
                try:
                    if sut_inputs_path:
                        return_code = subprocess.call(['opera', 'deploy',
                                                       '-p', self.sut_storage_path,
                                                       '-i', sut_inputs_path,
                                                       entry_definition],
                                                      cwd=self.sut_storage_path)
                    else:
                        return_code = subprocess.call(['opera', 'deploy',
                                                       '-p', self.sut_storage_path,
                                                       entry_definition],
                                                      cwd=self.sut_storage_path)

                    if return_code == 0:
                        sut_success = True
                        opera_outputs = subprocess.check_output(['opera', 'outputs',
                                                                 '-p', self.sut_storage_path],
                                                                cwd=self.sut_storage_path)
                        current_app.logger.info(f'Opera returned output {opera_outputs}.')
                        opera_yaml_outputs_sut = yaml.safe_load(opera_outputs)
                    else:
                        error_message: str = f'Deployment of SUT failed with Opera return code ${return_code}.'
                        current_app.logger.info(error_message)
                        raise OperationError(error_message)
                except OperationError:
                    subprocess.call(['opera', 'undeploy',
                                     '-p', self.sut_storage_path])
            else:
                current_app.logger.info(f'Deployment of SUT skipped due to test mode being enabled.')
                sut_success = True

        # Deployment of TI
        with Csar(ti_csar_path, extract_dir=self.ti_storage_path, keep=True) as ti_csar:
            if DropPolicies:
                ti_csar.drop_policies()
            entry_definition = ti_csar.tosca_entry_point

            if entry_definition:
                current_app.logger. \
                    info(f'Deploying TI {str(entry_definition)} with opera in folder {str(self.ti_storage_path)}.')

                if sut_success and (not is_test_mode()):
                    return_code: int

                    try:
                        if ti_inputs_path:
                            return_code = subprocess.call(['opera', 'deploy',
                                                           '-p', self.ti_storage_path,
                                                           '-i', ti_inputs_path,
                                                           entry_definition],
                                                          cwd=self.ti_storage_path)
                        else:
                            return_code = subprocess.call(['opera', 'deploy',
                                                           '-p', self.ti_storage_path,
                                                           entry_definition],
                                                          cwd=self.ti_storage_path)

                        if return_code == 0:
                            opera_outputs = subprocess.check_output(['opera', 'outputs',
                                                                     '-p', self.ti_storage_path],
                                                                    cwd=self.ti_storage_path)
                            current_app.logger.info(f'Opera returned output {opera_outputs}.')
                            opera_yaml_outputs_ti = yaml.safe_load(opera_outputs)
                            time.sleep(30)
                        else:
                            error_message: str = f'Deployment of TI failed with Opera return code ${return_code}.'
                            current_app.logger.info(error_message)
                            raise OperationError(error_message)
                    except OperationError:
                        subprocess.call(['opera', 'undeploy',
                                         '-p', self.ti_storage_path])

                    # Set SUT hostname to the value of the opera output of the SUT
                    # with the name from the policy field for hostname
                    policy_hostname: str = self.__test_artifact.policy_yaml['properties']['hostname']
                    if policy_hostname in opera_yaml_outputs_sut and 'value' in opera_yaml_outputs_sut[policy_hostname]:
                        self.sut_hostname = opera_yaml_outputs_sut[policy_hostname]['value']
                    else:
                        # Set hostname to field set in the policy field for hostname
                        self.sut_hostname = policy_hostname
                    current_app.logger.info(f'SUT hostname {self.sut_hostname}.')

                    # Set TI hostname to the 'public_address' value of the opera output
                    self.ti_hostname = opera_yaml_outputs_ti['public_address']['value']
                    current_app.logger.info(f'TI hostname {self.ti_hostname}.')

                elif not sut_success:
                    current_app.logger.warning(f'Deployment of TI skipped due to failure of SUT deployment.')
                elif is_test_mode():
                    current_app.logger.info(f'Deployment of TI skipped due to test mode being enabled.')

        db_session.add(self)
        db_session.commit()
Пример #22
0
 def delete_by_uuid(cls, del_uuid):
     result = Result.query.filter_by(uuid=del_uuid)
     if result:
         result.delete()
         # rmtree(self.fq_storage_path)
         db_session.commit()