Esempio n. 1
0
    def singularity_start(self, container_path):
        """Starts the container to execute commands or run the runscript
        with the supplied args inside the container.

        Args:
            container_path (str): The container image to run/execute.

        Returns:
            int: The container process returncode.
        """
        env = self.prepare_environment(set_env=True)

        volumes = [
            '{}:{}'.format(env['HOME'], env['HOME']),
            '{}:{}'.format(env['HOME'], '/github/home'),
            '{}:{}'.format(env['GITHUB_WORKSPACE'],
                           env['GITHUB_WORKSPACE']),
            '{}:{}'.format(env['GITHUB_WORKSPACE'], '/github/workspace'),
            '{}:{}'.format(env['GITHUB_EVENT_PATH'],
                           '/github/workflow/event.json')
        ]

        args = self.action.get('args', None)
        runs = self.action.get('runs', None)
        ecode = None

        if runs:
            info = '{}[{}] singularity exec {} {}'.format(
                self.msg_prefix, self.action['name'],
                container_path, runs)
            commands = runs
            start = s_client.execute
        else:
            info = '{}[{}] singularity run {} {}'.format(
                self.msg_prefix, self.action['name'],
                container_path, args)
            commands = args
            start = s_client.run

        log.info(info)
        if not self.dry_run:
            output = start(container_path, commands, bind=volumes,
                           stream=True, options=[
                               '--userns',
                               '--pwd', env['GITHUB_WORKSPACE']])
            try:
                for line in output:
                    log.action_info(line)
                ecode = 0
            except subprocess.CalledProcessError as ex:
                ecode = ex.returncode
        else:
            ecode = 0

        self.remove_environment()
        return ecode
Esempio n. 2
0
    def docker_start(self):
        log.info('{}[{}] docker start '.format(self.msg_prefix,
                                               self.action['name']))
        if self.dry_run:
            return 0
        self.container.start()
        cout = self.container.logs(stream=True)
        for line in cout:
            log.action_info(pu.decode(line).strip('\n'))

        return self.container.wait()['StatusCode']
Esempio n. 3
0
    def singularity_start(self):
        """Starts a singularity instance based on the image.
        """
        env_vars = self.action.get('env', {})

        for s in self.action.get('secrets', []):
            env_vars.update({s: os.environ[s]})

        for e, v in self.env.items():
            env_vars.update({e: v})

        env_vars.update({'HOME': os.environ['HOME']})

        # sets the env variables
        for k, v in env_vars.items():
            sclient.setenv(k, v)
        args = self.action.get('args', None)
        runs = self.action.get('runs', None)

        ecode = None
        bind_list = [self.workspace, os.environ['HOME']]

        if runs:
            info = '{}[{}] singularity exec {} {}'.format(
                self.msg_prefix, self.action['name'], self.image_name, runs)
            commands = runs
            start = sclient.execute
        else:
            info = '{}[{}] singularity run {} {}'.format(
                self.msg_prefix, self.action['name'], self.image_name, args)
            commands = args
            start = sclient.run

        log.info(info)
        if not self.dry_run:
            output = start(self.image_name,
                           commands,
                           contain=True,
                           bind=bind_list,
                           stream=True)

            try:
                for line in output:
                    log.action_info(line)
                ecode = 0
            except subprocess.CalledProcessError as ex:
                ecode = ex.returncode
        else:
            ecode = 0
        return ecode
Esempio n. 4
0
    def docker_start(self):
        """Start the container process.

        Args:
            None

        Returns:
          int: The returncode of the container process.
        """
        log.info('{}[{}] docker start '.format(self.msg_prefix,
                                               self.action['name']))
        if self.dry_run:
            return 0
        self.container.start()
        cout = self.container.logs(stream=True)
        for line in cout:
            log.action_info(pu.decode(line).strip('\n'))

        return self.container.wait()['StatusCode']
Esempio n. 5
0
    def host_start(self, cmd):
        """Start the execution of the command on the host machine.

        Args:
          cmd(str): The command to execute.

        Returns:
          int: The return code of the process.
        """
        log.info('{}[{}] {}'.format(self.msg_prefix, self.action['name'],
                                    ' '.join(cmd)))

        if self.dry_run:
            return 0

        ecode = 0

        try:
            log.debug('Executing: {}'.format(' '.join(cmd)))
            p = Popen(' '.join(cmd),
                      stdout=PIPE,
                      stderr=STDOUT,
                      shell=True,
                      universal_newlines=True,
                      preexec_fn=os.setsid)

            popper.cli.process_list.append(p.pid)

            log.debug('Reading process output')

            for line in iter(p.stdout.readline, ''):
                line_decoded = pu.decode(line)
                log.action_info(line_decoded[:-1])

            p.wait()
            ecode = p.poll()
            log.debug('Code returned by process: {}'.format(ecode))

        except CalledProcessError as ex:
            msg = "Command '{}' failed: {}".format(cmd, ex)
            ecode = ex.returncode
            log.action_info(msg)
        finally:
            log.action_info()

        os.chdir(self.cwd)
        return ecode