예제 #1
0
    def creator(_, config):
        """Creator function for creating an instance of a Packer image script."""
        packer_script = render(config.script,
                               model=config.model,
                               env=config.env,
                               variables=config.variables,
                               item=config.item)
        filename = "packer.dry.run.see.comment"

        if not config.dry_run:
            # writing Packer file (JSON)
            filename = write_temporary_file(packer_script, 'packer-', '.json')
            packer_script = ''

        # rendering the Bash script for generating the Packer image
        template_file = os.path.join(os.path.dirname(__file__),
                                     'templates/packer-image.sh.j2')

        with open(template_file) as handle:
            template = handle.read()
            config.script = render(template,
                                   debug=config.debug,
                                   packer_content=packer_script,
                                   packer_filename=filename)

        return Packer(config)
예제 #2
0
 def test_valid_loader(self):
     """Testing Loader used the right way."""
     yaml_file = os.path.join(os.path.dirname(__file__),
                              'data/loader_main.yaml')
     document = Adapter(Loader.load(yaml_file))
     render(document.some, model=document.model)
     assert_that(render(document.some, model=document.model),
                 equal_to('hello world!'))
예제 #3
0
    def creator(entry, config):
        """Creator function for creating an instance of a Bash."""
        template_file = os.path.join(os.path.dirname(__file__),
                                     'templates/docker-container.sh.j2')

        with open(template_file) as handle:
            template = handle.read()
            # all fields are re-rendered via the Bash script
            wrapped_script = render(
                template,
                recursive=False,
                container={
                    'image':
                    'centos:7' if 'image' not in entry else entry['image'],
                    'remove':
                    True
                    if 'remove' not in entry else str(entry['remove']).lower(),
                    'background':
                    False if 'background' not in entry else str(
                        entry['background']).lower(),
                    'mount':
                    False
                    if 'mount' not in entry else str(entry['mount']).lower(),
                    'network':
                    '' if 'network' not in entry else entry['network'],
                    'script':
                    config.script
                })

            config.script = wrapped_script

        return Container(config)
예제 #4
0
    def creator(entry, config):
        """Creator function for creating an instance of an Ansible script."""
        ansible_playbook = "ansible.playbook.dry.run.see.comment"
        ansible_inventory = "ansible.inventory.dry.run.see.comment"

        ansible_playbook_content = render(config.script,
                                          model=config.model,
                                          env=config.env,
                                          variables=config.variables,
                                          item=config.item)
        ansible_inventory_content = render(entry['inventory'],
                                           model=config.model,
                                           env=config.env,
                                           variables=config.variables,
                                           item=config.item)

        if not config.dry_run:
            ansible_playbook = write_temporary_file(ansible_playbook_content,
                                                    'ansible-play-', '.yaml')
            ansible_playbook_content = ''
            ansible_inventory = write_temporary_file(
                ansible_inventory_content, prefix='ansible-inventory-')
            ansible_inventory_content = ''

        # rendering the Bash script for running the Ansible playbook
        template_file = os.path.join(os.path.dirname(__file__),
                                     'templates/ansible.sh.j2')
        with open(template_file) as handle:
            template = handle.read()
            config.script = render(
                template,
                debug=config.debug,
                ansible_playbook_content=ansible_playbook_content,
                ansible_playbook=ansible_playbook,
                ansible_inventory_content=ansible_inventory_content,
                ansible_inventory=ansible_inventory,
                limit=entry['limit'])

        return Ansible(config)
예제 #5
0
    def creator(entry, config):
        """Creator function for creating an instance of a Docker image script."""
        # writing Dockerfile
        dockerfile = render(config.script,
                            model=config.model,
                            env=config.env,
                            variables=config.variables,
                            item=config.item)
        filename = "dockerfile.dry.run.see.comment"

        if not config.dry_run:
            temp = tempfile.NamedTemporaryFile(prefix="dockerfile-",
                                               mode='w+t',
                                               delete=False)
            temp.writelines(dockerfile)
            temp.close()
            filename = temp.name
            dockerfile = ''

        # rendering the Bash script for generating the Docker image
        name = entry['name'] + "-%s" % os.getpid() if entry[
            'unique'] else entry['name']
        tag = render(entry['tag'],
                     model=config.model,
                     env=config.env,
                     item=config.item)
        template_file = os.path.join(os.path.dirname(__file__),
                                     'templates/docker-image.sh.j2')

        with open(template_file) as handle:
            template = handle.read()
            config.script = render(template,
                                   name=name,
                                   tag=tag,
                                   dockerfile_content=dockerfile,
                                   dockerfile_filename=filename)

        return Image(config)
예제 #6
0
    def creator(entry, config):
        """Preparing and creating script."""
        script = render(config.script,
                        model=config.model,
                        env=config.env,
                        item=config.item)

        temp = tempfile.NamedTemporaryFile(prefix="script-",
                                           suffix=".py",
                                           mode='w+t',
                                           delete=False)
        temp.writelines(script)
        temp.close()

        language = 'python' if 'type' not in entry else entry['type']
        template_file = os.path.join(os.path.dirname(__file__),
                                     'templates/%s-script.sh.j2' % language)

        with open(template_file) as handle:
            template = handle.read()
            config.script = render(template, script=temp.name)

        return Script(config)
예제 #7
0
파일: bash.py 프로젝트: jbenden/pipeline
    def create_file_for(self, script):
        """
        Create a temporary, executable bash file.

        It also does render given script (string) with the model and
        the provided environment variables and optional also an item
        when using the B{with} field.

        Args:
            script (str): either pather and filename or Bash code.

        Returns:
            str: path and filename of a temporary file.
        """
        temp = tempfile.NamedTemporaryFile(
            prefix="pipeline-script-",
            mode='w+t',
            suffix=".sh",
            delete=False,
            dir=self.get_temporary_scripts_path())

        self.update_environment_variables(temp.name)
        rendered_script = render(script,
                                 model=self.config.model,
                                 env=self.env,
                                 item=self.config.item,
                                 variables=self.config.variables)
        if rendered_script is None:
            self.success = False
            temp.close()
            os.remove(temp.name)
            return None

        to_file_map = {2: lambda s: s.encode('utf-8'), 3: lambda s: s}

        if all(ord(ch) < 128
               for ch in rendered_script) and os.path.isfile(rendered_script):
            with open(rendered_script) as handle:
                content = str(handle.read())
                temp.writelines(content)
        else:
            temp.write(u"#!/bin/bash\n%s" % self.render_bash_options())
            temp.write(to_file_map[sys.version_info.major](rendered_script))
        temp.close()
        # make Bash script executable
        os.chmod(temp.name, 0o700)
        return temp.name
예제 #8
0
파일: tasks.py 프로젝트: jbenden/pipeline
    def can_process_shell(self, entry):
        """:return: True when shell can be executed."""
        count = 0
        condition = render(entry['when'],
                           variables=self.pipeline.variables,
                           model=self.pipeline.model,
                           env=self.get_merged_env(include_os=True))

        if Condition.evaluate("" if condition is None else condition):
            if len(self.pipeline.options.tags) == 0:
                return True

            if 'tags' in entry:
                for tag in self.pipeline.options.tags:
                    if tag in entry['tags']:
                        count += 1

        return count > 0
예제 #9
0
파일: tasks.py 프로젝트: ipgur/pipeline
    def prepare_shell_data(self, shells, key, entry):
        """Prepare one shell or docker task."""
        if self.can_process_shell(entry):
            if key in ['python']:
                entry['type'] = key

            if 'with' in entry and isinstance(entry['with'], str):
                rendered_with = ast.literal_eval(
                    render(entry['with'],
                           variables=self.pipeline.variables,
                           model=self.pipeline.model,
                           env=self.get_merged_env(include_os=True)))
            elif 'with' in entry:
                rendered_with = entry['with']
            else:
                rendered_with = ['']

            for item in rendered_with:
                shells.append({
                    'id':
                    self.next_task_id,
                    'creator':
                    key,
                    'entry':
                    entry,
                    'model':
                    self.pipeline.model,
                    'env':
                    self.get_merged_env(),
                    'item':
                    item,
                    'dry_run':
                    self.pipeline.options.dry_run,
                    'debug':
                    self.pipeline.options.debug,
                    'strict':
                    self.pipeline.options.strict,
                    'variables':
                    self.pipeline.variables,
                    'temporary_scripts_path':
                    self.pipeline.options.temporary_scripts_path
                })
                self.next_task_id += 1
예제 #10
0
def generate_html(store):
    """
    Generating HTML report.

    Args:
        store (Store): report data.

    Returns:
        str: rendered HTML template.
    """
    spline = {
        'version': VERSION,
        'url': 'https://github.com/Nachtfeuer/pipeline',
        'generated': datetime.now().strftime("%A, %d. %B %Y - %I:%M:%S %p")
    }

    html_template_file = os.path.join(os.path.dirname(__file__), 'templates/report.html.j2')
    with open(html_template_file) as handle:
        html_template = handle.read()
        return render(html_template, spline=spline, store=store)
예제 #11
0
파일: bash.py 프로젝트: ipgur/pipeline
    def process(self):
        """Running the Bash code."""
        temp_filename = self.create_file_for(self.config.script)

        if len(self.config.title) > 0:
            self.logger.info(render(self.config.title, model=self.config.model, env=self.env,
                                    item=self.config.item, variables=self.config.variables))

        if temp_filename is not None:
            try:
                for line in self.process_file(temp_filename):
                    yield line
            finally:
                # removing script
                os.remove(temp_filename)

        if not self.config.internal:
            if self.exit_code == 0:
                self.event.succeeded()
            else:
                self.event.failed(exit_code=self.exit_code)
예제 #12
0
 def test_render_simple(self):
     """Testing simple rendering without usage nested templates."""
     model = {"message": "hello world!"}
     given = render("{{model.message}}", model=model)
     assert_that(given, equal_to('hello world!'))
예제 #13
0
 def test_render_failed(self):
     """Testing failure in rendering."""
     # undefined variable 'env' (UndefinedError exception)
     assert_that(render('{{ env.missing }}'), equal_to(None))
     # syntax error (TemplateSyntaxError exception)
     assert_that(render('{% for %}'), equal_to(None))
예제 #14
0
 def test_render_nested(self):
     """Testing using nested templates."""
     model = {"message": "hello world!", "template": "{{ model.message }}"}
     given = render("{{ model.template|render(model=model) }}", model=model)
     assert_that(given, equal_to('hello world!'))