Skip to content

Commit

Permalink
add startup service and script to apply jinja template
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesreid1 committed May 14, 2020
1 parent 809257c commit 4fb4795
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
47 changes: 47 additions & 0 deletions apply_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os, re, sys
from jinja2 import Environment, FileSystemLoader, select_autoescape

"""
Apply Default Values to Jinja Templates
"""

TEMPLATE_FILES= ['service/apollo.service.j2']
REAL_FILES = [t[:-3] for t in TEMPLATE_FILES]

# Should existing files be overwritten
OVERWRITE = False

REQ_ENV_VARS = [
'APOLLO_PATH', # path to the apollo repo
'APOLLO_USER', # the user to run the service as
]

def usage():
print(sys.argv[0], "usage:")
print("")
print("You must define the following environment variables")
print("to render the Jinja templates:")
for env_var in REQ_ENV_VARS:
print(" "*4,env_var)

for env_var in REQ_ENV_VARS:
if env_var not in os.environ:
usage()
raise Exception(f"Error: required env var not defined: {env_var}")

for template_file, real_file in zip(TEMPLATE_FILES, REAL_FILES):
template_dir = os.path.basepath(template_file)
env = Environment(loader=FileSystemLoader(template_dir))
render_kwargs = {}
for env_var in REQ_ENV_VARS:
render_kwargs[env_var.lower()] = os.environ[env_var]
content = env.get_template(template_file).render(**render_kwargs)


# Write to file
if os.path.exists(rfile) and not OVERWRITE:
raise Exception("Error: file %s already exists!"%(rfile))
else:
with open(rfile,'w') as f:
f.write(content)

38 changes: 38 additions & 0 deletions service/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# uptime service

Directory containing files useful for creating a bot startup service
(on Linux, of course).

## Jinja Template

This directory contains a Jinja template for a systemd service
called `apollo.service` that can be installed to run on startup.
The Jinja template contains one parameter that is the location of
the uptime repo on disk.

To render the template using a user-sepcified path:

* Edit `render_template.py` to specify path of uptime repo
* Run `render_template.py` to create `apollo.service`

## Virtual Environment

To run the startup service, you must first create a virtualenv
in the `vp/` folder of the `uptime` repo. To do this, run this
command from the `uptime` repo:

```
virtualenv -p python3.6 vp && source vp/bin/activate'
```

Now install the required software:

```
pip install -r requirements.txt
```

Now the python exeutable at `{{ apollo_path }}/vp/bin/python` will
have the necessary libraries installed and can be used by the
service script.


14 changes: 14 additions & 0 deletions service/apollo.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Unit]
Description=apollo space junk twitter bot

[Service]
Restart=always
User={{ apollo_user }}
Group=wheel
ExecStart={{ apollo_path }}/vp/bin/python -u {{ apollo_path }}/bot/ApolloBotFlock.py
ExecStop=/usr/bin/killall {{ apollo_path }}/vp/bin/python

[Install]
WantedBy=default.target


0 comments on commit 4fb4795

Please sign in to comment.