nickstenning / honcho

Honcho: a python clone of Foreman. For managing Procfile-based applications.
http://pypi.python.org/pypi/honcho
MIT License
1.6k stars 146 forks source link

Export: Add custom template support #115

Closed msabramo closed 9 years ago

msabramo commented 9 years ago

This adds the ability to use a custom template when exporting to supervisord.

Fixes: GH-88

Example:
$ honcho export supervisord . -t my_supervisord_template.conf.jinja2

$ cat export_test.conf
# *****************************************************
# This is a custom supervisord template for honcho
# blah blah blah
# *****************************************************

[program:export_test-ansvc]
MONKEY=YES
command=/bin/sh -c 'bin/ansvc start'
autostart=true
autorestart=true
stopsignal=QUIT
stdout_logfile=/var/log/export_test/ansvc-0.log
stderr_logfile=/var/log/export_test/ansvc-0.error.log
user=marca
directory=/Users/marca/dev/git-repos/honcho/export_test
environment=PORT="5000"

[group:export_test]
programs=export_test-ansvc
msabramo commented 9 years ago

Rebased to fix merge conflict.

msabramo commented 9 years ago

@nickstenning, @slafs: What do you guys think of this?

slafs commented 9 years ago

For me this doesn't feel right. I think the responsibility for deciding whether to use rendering with provided full_path or with the builtin path is oddly placed. Don't have much time right now. Let me think about it for a while and I'll get back to you.

nickstenning commented 9 years ago

I'm not really happy with a flag like this that purports to be general but in practice only works for one export format. Part of the reason for allowing exporters to expose themselves to honcho through setuptools entrypoints is to allow exactly this kind of customisation, but in a more general way.

I appreciate that this is simpler, but if it doesn't generalise it's actually adding complexity to the user interface. In addition, I think you'll find that with #119 merged, it will be much simpler to write custom exporters.

msabramo commented 9 years ago

I think I have the same feeling that it's not quite right and could be better. I think maybe the choosing of the template is happening at too low of a level. And the Export classes shouldn't be concerned with it. You should just give them a template and they should render it. After all they are exporters and not template choosers.

Maybe the choice of template should happen further up the stack?

This might intersect a bit with another idea I had - to allow the specified template to be an asset spec (package resource) in addition to a plain ol' absolute file path. This would allow one to pip install honcho-export-supervisord-fancy and then reference the template with something like -t honcho-export-supervisord-fancy:templates/cool1.jinja2

If that existed, then the default honcho supervisord template could just be a default value that is a resource spec

nickstenning commented 9 years ago

The problem is that some exporters use multiple templates. Have a look at the upstart exporter to see what I mean.

msabramo commented 9 years ago

Ah yes I remember the multiple template thing now. I punted on that. Not sure whether to pass a dict of templates or a directory, which seems more CLI-friendly.

msabramo commented 9 years ago

This needs to be reworked in light of the merging of #119.

msabramo commented 9 years ago

My current thought is to allow -t to take a directory full of templates for the exporters that have multiple templates like the upstart exporter...

Thoughts...?

msabramo commented 9 years ago

Right now this only works for supervisor; I need to implement upstart support

msabramo commented 9 years ago

Or I suppose we could add an error message that says Custom templates not supported for upstart yet.

msabramo commented 9 years ago

Problem with this:

honcho export supervisord out --template=my_supervisord_template.conf.jinja2

will fail because it interprets the template argument as being relative to the honcho package unless it starts with a slash. Workaround: honcho export supervisord out --template=$(pwd)/my_supervisord_template.conf.jinja2, but I think it's better to change this so honcho-relative paths are assets specs that start with "honcho:" -- e.g.: "honcho:supervisord/supervisord.conf"

msabramo commented 9 years ago

Or add "." to the filesystem search path.

msabramo commented 9 years ago

Apparently, foreman uses a directory for templates, although this wasn't at all obvious from help or docs:

https://github.com/ddollar/foreman/pull/363

nickstenning commented 9 years ago

Do you want to rework this so the templates directory is configurable?

msabramo commented 9 years ago

@nickstenning: Just curious why you closed this? I haven't gotten around to updating this yet, but I wonder if you closed it, because you don't think it's a good idea? Are you open to another PR?

nickstenning commented 9 years ago

No, it's a fine idea. Feel free to open a PR when you get a chance to update the code.

msabramo commented 9 years ago

Ok, cool, just checking before I spend the time. Thanks!

msabramo commented 9 years ago

OK, I just updated this so that --template is a directory. I'm not quite sure how to test this in an automated way, but I did do manual testing.

[marca@marca-mac2 export_test]$ ls -l upstart_template.d
total 24
-rw-r--r--+ 1 marca  staff  353 Feb 23 06:01 master.conf
-rw-r--r--+ 1 marca  staff  380 Feb 23 06:02 process.conf
-rw-r--r--+ 1 marca  staff  119 Feb 23 06:02 process_master.conf
[marca@marca-mac2 export_test]$ honcho export upstart upstart-export --template upstart_template.d
2015-02-23 07:59:43 [20837] [INFO] Writing 'upstart-export/export_test.conf'
2015-02-23 07:59:43 [20837] [INFO] Writing 'upstart-export/export_test-ansvc.conf'
2015-02-23 07:59:43 [20837] [INFO] Writing 'upstart-export/export_test-ansvc-1.conf'

[marca@marca-mac2 export_test]$ honcho export upstart upstart-export
2015-02-23 08:00:16 [20841] [INFO] Writing 'upstart-export/export_test.conf'
2015-02-23 08:00:16 [20841] [INFO] Writing 'upstart-export/export_test-ansvc.conf'
2015-02-23 08:00:16 [20841] [INFO] Writing 'upstart-export/export_test-ansvc-1.conf'

[marca@marca-mac2 export_test]$ ls -l supervisor-template.d
total 8
-rw-r--r--+ 1 marca  staff  776 Dec 23 08:09 supervisord.conf
[marca@marca-mac2 export_test]$ honcho export supervisord out --template=supervisor-template.d
2015-02-23 08:00:39 [20847] [INFO] Writing 'out/export_test.conf'

[marca@marca-mac2 export_test]$ honcho export supervisord out
2015-02-23 08:01:05 [20855] [INFO] Writing 'out/export_test.conf'
msabramo commented 9 years ago

See https://github.com/nickstenning/honcho/pull/140