Exemple #1
0
    def execute(self, data):
        if self.definition.remote:
            remote = self.definition.remote
            return self.execute_remote(
                data,
                resolve_variable_spec(data, remote.get('host', 'localhost')),
                resolve_variable_spec(data, remote.get('user', 'root')))
        self.log.debug("Running %s", self.definition.name)
        input_data = filter_by_channel(self.definition.inputs, data)
        params = [
            str(resolve_variable_spec(data, a))
            for a in self.definition.arguments
        ]
        executable = self.definition.executable

        env = os.environ.copy()
        env.update(get_environment_extension())
        env.update(self._environ)
        p = Popen([executable] + params,
                  stdin=PIPE,
                  stdout=PIPE,
                  stderr=PIPE,
                  env=env,
                  cwd=self.definition.base_path)
        stdin = self.handle_stdin(input_data)
        out, err = p.communicate(stdin)
        self.handle_stderr(err, data)
        self.handle_stdout(out, data)
        p.wait()
        self.handle_return_code(p.returncode, data)
        return p.returncode == 0
Exemple #2
0
    def execute(self, data):
        restricted = filter_by_channel(self.definition.inputs, data)
        restricted.update({
            i['name']: resolve_variable_spec(data, i['source'])
            for i in self.definition.extended_inputs if 'source' in i
        })
        restricted.update({
            i['name']: i['value']
            for i in self.definition.extended_inputs if 'value' in i
        })

        actor = must_get_actor(self.definition.extended.name)
        ret = actor.execute(restricted)
        if ret:
            for output in self.definition.extended_outputs:
                restricted[output['name']] = resolve_variable_spec(
                    restricted, output['source'])
            data.update(filter_by_channel(self.definition.outputs, restricted))

        return ret
Exemple #3
0
        def execute(self, data):
            extends = ExtendsActorDefinition(definition)
            restricted = {
                n['name']: data[n['name']]
                for n in extends.required_inputs
            }
            restricted.update({
                i['name']: resolve_variable_spec(restricted, i['source'])
                for i in extends.inputs if 'source' in i
            })
            restricted.update({
                i['name']: i['value']
                for i in extends.inputs if 'value' in i
            })

            ret = super(ExtendsActor, self).execute(restricted)
            if ret:
                for output in extends.output:
                    data[output['name']] = resolve_variable_spec(
                        restricted, output['source'])

            return ret
Exemple #4
0
    def execute(self, data):
        input_data = filter_by_channel(self.definition.inputs, data)
        params = [resolve_variable_spec(data, a) for a in self.definition.executor.arguments]
        executable = self.definition.executor.executable

        env = os.environ.copy()
        env.update(get_environment_extension())
        p = Popen([executable] + params, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env)
        stdin = self.handle_stdin(input_data)
        out, err = p.communicate(stdin)
        self.handle_stderr(err, data)
        self.handle_stdout(out, data)
        p.wait()
        self.handle_return_code(p.returncode, data)
        return p.returncode == 0
Exemple #5
0
    def execute_remote(self, data, address, user, sync_repo=True):
        self.log.debug("Running %s REMOTELY", self.definition.name)
        actor_relative_path = os.path.relpath(self.definition.base_path, get_loaded_path())
        actor_remote_path = os.path.normpath(os.path.join(_ACTOR_REMOTE_PATH, actor_relative_path))
        input_data = filter_by_channel(self.definition.inputs, data)
        actor_input = self.handle_stdin(input_data)
        params = [str(resolve_variable_spec(data, a)) for a in self.definition.arguments]
        executable = self.definition.executable
        playbook = os.path.normpath(os.path.join(get_loaded_path(), '../playbooks/remote-execute-actor.yaml'))

        quoted_remote_command = ' '.join(shlex_quote(element) for element in [executable] + params)

        with self._in_out_temp_files() as (input_file, output_file):
            if actor_input:
                with open(input_file.name, 'w') as f:
                    f.write(actor_input)

            command_template = '''
                ansible-playbook
                        {playbook}
                        -i {host},
                        -u {user}
                        -e actor_repository="{actor_repo_path}"
                        -e sync_repo={sync_repo}
                        -e actor_output_file="{actor_output_file}"
                        -e actor_input_file="{actor_input_file}"
                        -e actor_command="'{actor_command}'"
                        -e actor_name="{actor_name}"
                        -e actor_cwd="{actor_cwd}"
                        -e actor_remote_repo_path="{remote_repo_path}"
            '''
            command = command_template.format(
                playbook=playbook,
                user=user,
                host=address,
                # Repo information
                actor_repo_path=get_loaded_path(),
                remote_repo_path=_ACTOR_REMOTE_PATH,
                sync_repo='True' if sync_repo else 'False',
                # actor configuration
                actor_name=self.definition.name,
                actor_command=quoted_remote_command,
                actor_cwd=actor_remote_path,
                actor_input_file=input_file.name,
                actor_output_file=output_file.name)

            ansible_process = Popen(shlex.split(command), stdout=PIPE, stderr=PIPE)
            playbook_output = ansible_process.communicate()

            self.log.debug("PLAYBOOK STANDARD OUTPUT:\n------------------------\n%s\n----------------------",
                           playbook_output[0])
            self.log.debug("PLAYBOOK STANDARD ERROR:\n------------------------\n%s\n----------------------",
                           playbook_output[1])

            if ansible_process.returncode:
                return False

            with open(output_file.name, 'r') as f:
                actor_output = json.load(f)

            self.handle_stdout(actor_output['stdout'], data)
            self.handle_stderr(actor_output['stderr'], data)
            self.handle_return_code(actor_output['rc'], data)

            return actor_output['rc'] == 0