Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can’t use j2 templates inside a directory in netpalm/backend/plugins/jinja2_templates #18

Open
nabapal opened this issue May 13, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@nabapal
Copy link

nabapal commented May 13, 2020

This feature is useful to organised the jinja2 templates.

@Minal1409
Copy link

Yes this feature will be useful as we have morethan 100 templates. Some way is required to group this templates

@rhwendt
Copy link
Contributor

rhwendt commented Nov 5, 2020

You can load any templates you want by editing the config.json

❯ grep j2 config.json
    "jinja2_config_templates": "netpalm/backend/plugins/extensibles/j2_config_templates/",
    "jinja2_service_templates": "netpalm/backend/plugins/extensibles/j2_service_templates/",
    "webhook_jinja2_templates": "netpalm/backend/plugins/extensibles/j2_webhook_templates/",

This can be tricky if you're using netpalm as a dependency. In our case we are cloning into our main project.

For example our project looks like this from the root folder.

├── netpalm
│   ├── CODE_OF_CONDUCT.md
│   ├── config
│   ├── CONTRIBUTING.md
│   ├── docker-compose.ci.yml
│   ├── docker-compose.dev.yml
│   ├── docker-compose.yml
│   ├── dockerfiles
│   ├── gunicorn.conf.py
│   ├── LICENSE
│   ├── netpalm
│   ├── netpalm.postman_collection.json
│   ├── NOTICE
│   ├── pytest.ini
│   ├── README.md
│   ├── redis_gen_new_certs.sh
│   ├── RUNNING_TESTS.md
│   ├── static
│   ├── tests
│   └── worker.py
├── README.md
├── templates
│   ├── config
│   └── service

Our build does some ugly text replace in the netpalm dockerfiles before we build it. I'm sure this could be done easier.

    - name: change the context of netpalm controller
      replace:
        path: /conductor/netpalm/docker-compose.yml
        regexp: "^          context: ."
        replace: "          context: .."

    - name: edit the context of netpalm controller dockerfile
      lineinfile:
        path: /conductor/netpalm/docker-compose.yml
        insertafter: "^          context: .."
        regexp: "{{ item.regexp }}"
        line: "{{ item.line }}"
      loop:
        - { regexp: "^          dockerfile: ./dockerfiles/netpalm_controller_dockerfile",
            line: "          dockerfile: ./netpalm/dockerfiles/netpalm_controller_dockerfile" }
        - { regexp: "^          dockerfile: ./dockerfiles/netpalm_pinned_worker_dockerfile",
            line: "          dockerfile: ./netpalm/dockerfiles/netpalm_pinned_worker_dockerfile" }
        - { regexp: "^          dockerfile: ./dockerfiles/netpalm_fifo_worker_dockerfile",
            line: "          dockerfile: ./netpalm/dockerfiles/netpalm_fifo_worker_dockerfile" }

    - name: edit the dockerfile for netpalm and workers
      lineinfile:
        path: "{{ item.path }}"
        regexp: "{{ item.regexp }}"
        line: "{{ item.line }}"
      loop:
       - { regexp: "^ADD netpalm/requirements.txt /code/",
           line: "ADD ./netpalm/netpalm/requirements.txt /code/",
           path: "/conductor/netpalm/dockerfiles/netpalm_controller_dockerfile" }
       - { regexp: "^ADD netpalm/worker_requirements.txt /code/",
           line: "ADD ./netpalm/netpalm/worker_requirements.txt /code/",
           path: "/conductor/netpalm/dockerfiles/netpalm_pinned_worker_dockerfile" }
       - { regexp: "^ADD netpalm/worker_requirements.txt /code/",
           line: "ADD ./netpalm/netpalm/worker_requirements.txt /code/",
           path: "/conductor/netpalm/dockerfiles/netpalm_fifo_worker_dockerfile" }
       - { regexp: "^ADD . /code",
           line: "ADD ./netpalm /code",
           path: "/conductor/netpalm/dockerfiles/netpalm_controller_dockerfile" }
       - { regexp: "^ADD . /code",
           line: "ADD ./netpalm /code",
           path: "/conductor/netpalm/dockerfiles/netpalm_pinned_worker_dockerfile" }
       - { regexp: "^ADD . /code",
           line: "ADD ./netpalm /code",
           path: "/conductor/netpalm/dockerfiles/netpalm_fifo_worker_dockerfile" }

    - name: add the templates netpalm controller and workers
      lineinfile:
        path: "{{ item.path }}"
        line: "ADD ./templates /code/templates"
        insertafter: "^ADD ./netpalm /code"
      loop:
        - { path: "/conductor/netpalm/dockerfiles/netpalm_controller_dockerfile" }
        - { path: "/conductor/netpalm/dockerfiles/netpalm_pinned_worker_dockerfile" }
        - { path: "/conductor/netpalm/dockerfiles/netpalm_fifo_worker_dockerfile" }

    - name: Update netpalm templates directory in netpalm config
      lineinfile:
        path: /conductor/netpalm/config/config.json
        regexp: "{{ item.regex }}"
        line: "{{ item.line }}"
      loop:
        - { regex: '"jinja2_service_templates":',
            line: '    "jinja2_service_templates": "/code/templates/service/",' }
        - { regex: '"jinja2_config_templates":',
            line: '    "jinja2_config_templates": "/code/templates/config/",' }

@tbotnz tbotnz added the enhancement New feature or request label Dec 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants