-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.py
executable file
·143 lines (107 loc) · 4.64 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
'''
config.py
Created by Christian Klein on 2010-03-10.
Copyright (c) 2010 Christian Klein. All rights reserved.
service/config.py is meant as a replacement for service-config
to create the directory structures for daemontools (http://cr.yp.to/daemontools).
'''
import sys
import os
import pwd
from optparse import OptionParser
def get_ids(username):
'''Get user id for given username'''
passwd = pwd.getpwnam(username)
return passwd.pw_uid, passwd.pw_gid
def makelog(directory, loguser):
'''Create log directory'''
uid, gid = get_ids(loguser)
fname = os.path.join(directory, 'main')
os.makedirs(fname)
os.chown(fname, uid, gid)
os.chmod(fname, 02755)
fname = os.path.join(directory, 'status')
open(fname, 'w')
os.chown(fname, uid, gid)
os.chmod(fname, 0644);
fname = os.path.join(directory, 'run')
open(fname, 'w').write('#!/bin/sh\nexec\nsetuidgid %s\nmultilog t ./main\n' % loguser)
os.chown(fname, uid, gid)
os.chmod(fname, 0755)
def create(templates):
'''
Create the directory structure
This function creates the directory structure for each defined template.
The path stored in template['directory'] will be created, with all
parent directories if they do not exist yet.
In the given directory, a script 'run' will be created that will be executed
by supervise (http://cr.yp.to/daemontools/supervise.html).
The run script will be build using template['run_prologue'] and
template['run'].
The value of 'run_prologue' is a helper for executing stuff before
the actual command is executed, like setting PATH which is not possible
with the envdir program.
Additionally, the subdirectories 'env' and 'log' will be created.
The key value pairs of template['env'] will be stored in the 'env' directory
with key being the filename of a file with the value as content.
'env' is to be used as a directory for envdir (http://cr.yp.to/daemontools/envdir.html).
The subdirectory 'log' is a directory structure for logging with
multilog (http://cr.yp.to/daemontools/multilog.html).
'''
for name, template in templates.items():
fmtvars = template.get('vars', {})
try:
directory = template['directory']
for subdir in 'env', 'log':
os.makedirs(os.path.join(directory, subdir), 02755)
makelog(os.path.join(directory, 'log'), template.get('loguser', template.get('user')))
# environment variables:
uid, gid = get_ids(template['user'])
template['env'].update({'UID': str(uid), 'GID': str(gid)})
for variable, value in template['env'].items():
filename = os.path.join(directory, 'env', variable % fmtvars)
open(filename, 'w').write(value)
os.chmod(filename, 0644)
# create run script:
fname = os.path.join(directory, 'run')
run = open(fname, 'w')
run.write('#!/bin/sh\n')
if 'run_prologue' in template:
run.write(template['run_prologue'] % fmtvars)
if not template['run_prologue'].endswith('\\n'):
run.write('\\n')
run.write('exec 2>&1\nexec envdir %s/env \\\n' % directory)
run.write(template['run'] % fmtvars)
if not template['run'].endswith('\\n'):
run.write('\\n')
os.chmod(fname, 0755)
except (KeyError, OSError), exception:
sys.stderr.write("Could not create service directories: %s\n" % exception)
# tear down
#print "tearing down", directory
pass
def load_yaml(filename):
from yaml import load
from yaml import Loader as Loader
return load(open(filename), Loader=Loader)
def load_json(filename):
import simplejson as json
return json.load(open(filename))
def load_python(filename):
lvars = {}
execfile(filename, lvars)
return lvars['TEMPLATES']
def main():
parser = OptionParser()
parser.add_option('-f', '--format', type='choice', choices=['python', 'yaml', 'json'],
default='python', help='Format of template file [default: %default]')
parser.add_option('-s', '--service', action="store_true", help="Create service directories")
options, args = parser.parse_args(sys.argv)
if len(args) != 2:
parser.error('No template file given')
templates = globals().get('load_%s' % options.format)(args[1])
if options.service:
create(templates)
if __name__ == '__main__':
main()