-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add startup service and script to apply jinja template
- Loading branch information
1 parent
809257c
commit 4fb4795
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|