Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

custom msg-templates for c7n_mailer #1127

Closed
kennyg opened this issue May 4, 2017 · 12 comments
Closed

custom msg-templates for c7n_mailer #1127

kennyg opened this issue May 4, 2017 · 12 comments

Comments

@kennyg
Copy link

kennyg commented May 4, 2017

can you add support for customized msg-templates for c7n_mailer? it appears the tool is hard-coded to look for a default template regardless of what's specified in the .yml config.

@kapilt
Copy link
Collaborator

kapilt commented May 5, 2017

two sides of this, customizing templates at the delivery / rendering side in the mailer, and specifying the notification template.

right now we just upload the msg-templates folder from the current working directory that the mailer deploy command is invoked from. you can drop custom templates in that folder.

wrt to sending side and picking a named template, the notify action can has a template name field/attibute that can be specified.

@kennyg
Copy link
Author

kennyg commented May 5, 2017

I tried to specify the template name in the configuration, i.e. mydefault.html, but was unable to see my changes reflected unless I overwrote the default.html template. It appears the render function looks for a default template, unless i'm missing something.
https://github.com/capitalone/cloud-custodian/blob/master/tools/c7n_mailer/c7n_mailer/sqs_message_processor.py#L262

@JohnTheodore
Copy link
Contributor

JohnTheodore commented May 9, 2017

@kennyg I believe that code snippet tries to get the template value from the policy, and if it doesn't exist.. it uses the 'default' file.

data['action'].get('template', 'default')

  actions:
    - type: notify
      priority_header: '1'
      template: kennyg_template
      subject: "some subject"
      to:
        - resource-owner
      transport:
        type: sqs
        queue: {CC_SQS_URL}

^ note that it looks for 'template'.

Would you dump the commands you're using the deploy the lambda function? your policy with the action? and the name of your template?

@jantman
Copy link
Contributor

jantman commented May 15, 2017

I was able to get this working. The catch is that sqs_message_processor uses the Jinja FileSystemLoader, so it's looking for templates in c7n-mailer/tools/c7n_mailer/msg-templates/ inside the source tree.

I'm using cloud-custodian from git pinned to a specific hash, so to install the mailer, this is what worked for me:

virtualenv --no-site-packages -p python2.7 .
. ./bin/activate
pip install -e 'git+https://github.com/capitalone/cloud-custodian.git@34d74e0dce6a2eb8599311947a4b2fea3036d#egg=c7n_mailer&subdirectory=tools/c7n_mailer'
pip install -r src/c7n-mailer/tools/c7n_mailer/requirements.txt
# my custom templates are in ./templates/; copy them into the source tree
cp templates/* src/c7n-mailer/tools/c7n_mailer/msg-templates/
PYTHONPATH=$PYTHONPATH:.:. ; . ./bin/activate && c7n-mailer -c /app/mailer.yml

@JohnTheodore
Copy link
Contributor

yea, the pathing is hard coded in that weird way. a PR to fix that sounds reasonable!

@davidclin
Copy link

davidclin commented Apr 23, 2018

I installed the mailer following the directions provided in the mailer README.md and had a similar question regarding using customized msg-templates. At the moment, I've been tinkering with modifying the default.html.j2 and default.j2 templates (eg: adding a simple "hello" text line) but not seeing my change in the email rendered. I have a feeling I'm not editing the right file because when I remove the default.html.j2 and default.j2 files entirely (expecting some failure error due to absence of template file, the mailer runs to completion and sends an email back with output but without my additional text.

The directory I've been making changes to the msg-template files is:

(custodian) [ec2-user@ip-x-x-x-x msg-templates]$ pwd
/home/ec2-user/cloud-custodian/tools/c7n_mailer/msg-templates

I've tried modifying the default.html.j2 and default.j2 respectively and tried the following lines to my policy:

HTML-based

actions:
      - type: notify
        template: default.html
        template_format: 'html'
        priority_header: '5'
        subject: 'test notification'
        to:
          - event-owner
        owner_absent_contact:
          - 
        transport:
          type: sqs
          queue: 

Text-based

actions:
      - type: notify
        template: default
        template_format: 'text'
        priority_header: '5'
        subject: 'test notification'
        ... (same as HTML-based)

jantman mentions something about the templates being in the "source tree" but I have no idea where or how to find that in my present virtualenv. :(

@jantman
Copy link
Contributor

jantman commented Apr 25, 2018

@davidclin The Makefile that I used is available in a blog post here. The gist of it is:

  1. I have a git repo that I clone which includes a templates/ directory.
  2. I create a virtualenv in that git clone, and install c7n as an editable git clone (pip install -e git+https://github.com/capitalone/cloud-custodian.git)
  3. The c7n source is installed at src/c7n in my git clone
  4. The templates directory that the mailer uses is at src/c7n/tools/c7n_mailer/msg-templates/, so that's where it reads the templates from (and where I put mine).

DISCLAIMER: We're currently using 0.8.26.0, so I'm not sure what refactoring may have happened since then.

The shorter path to arrive at the above is: install c7n in your virtualenv, and then search the virtualenv for default.html.j2. Wherever that is, it's what you replace or overwrite.

@davidclin
Copy link

@jantman Thank you!!! I took the path of least resistance and installed c7n in my custodian virtualenv -- and I'm able to modify the default.html.j2 file now! Haven't had a chance to try out custom templates yet, but will try later next week.

@davidclin
Copy link

Was able to get custom templates working using the option mentioned above of installing c7n in the custodian virtualenv.

Everything is working great and I'm able to create customized templates for my policies.

The trick was finding the path where the default.html.j2 file lived.

Easily solved issueing:

find ~/ -type f -name "default.html.j2"

@jainankurp
Copy link

I am using Azure Storage queue for sending emails, after installing the c7n-mailer this the location of my templates. "/cloud-custodian\tools\c7n_mailer\c7n_mailer\msg-templates"
I tried updating directly "default.html.j2" and default.j2", no matter what i do it doesn't respecting custom templates and only sending with original templates (which actually doesn't exist as i have updated them). Can someone advise where should i change to get custom templates working?

below is my action sample from policy.
actions:
- type: notify
template: default
priority_header: '1'
subject: test mailer.
to:
- foo@example.com
transport:
type: asq
queue: https://mailerstorage.queue.core.windows.net/ccqueue

Thanks in Advance!

@cbm-mmaireaux
Copy link

Hello,
Got news on how to add custom templates to a c7n-mailer?
Regards

@amansingh14
Copy link

c7n-mailer -c mailer/mailer.yml --templates templates/ --update-lambda

you can use above comment while deploying the mailer

@cloud-custodian cloud-custodian locked and limited conversation to collaborators Jun 1, 2022
@castrojo castrojo converted this issue into discussion #7313 Jun 1, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

8 participants