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
|
#!/usr/bin/python
#
# flufig
#
# flufig is a simple flumotion configuration generator using
# pyhton jinja2 template engine and simplejson. flufig generates
# planet.xml and worker.xml files from configuration templates and
# an easy representation of the flow structure written in json.
#
#
# Copyright (C) 2012 Christian Pointner <equinox@spreadspace.org>
# Michael Gebetsroither <michael@mgeb.org>
#
# This file is part of flufig.
#
# flufig is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# any later version.
#
# flufig is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with flufig. If not, see <http://www.gnu.org/licenses/>.
#
import string
import random
import sys
import simplejson as json
from exceptions import *
from jinja2 import Environment, FileSystemLoader
def rand_string(size=8, chars=string.ascii_lowercase + string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range(size))
if len(sys.argv) <= 2:
raise SystemExit("No template name and or config file given")
cf = open(sys.argv[2], 'r')
config = json.load(cf);
cf.close();
data = { 'globals' : config['globals'], 'atmosphere' : {}, 'flow' : {} }
machines = {}
idx = 0
for streamer in config['streamer']:
worker = 'streamer%i'%(idx)
port = streamer['config']['port']
found = False
for machine in config['globals']['machines'].keys():
if worker in config['globals']['machines'][machine]:
if machine in machines:
if 'porter' in machines[machine]:
if port in machines[machine]['porter']:
print "Porter: machine %s already uses port %i" % (machine, port)
sys.exit(1)
else:
machines[machine]['porter'] = {}
else:
machines[machine] = { 'porter': {} }
found = True
machines[machine]['porter'][port] = {
'socket-path': "porter%i-%s"%(idx, rand_string()),
'username': rand_string(size=12),
'password': rand_string(size=12),
}
data['atmosphere']['porter-%s-%i'%(machine, port)] = {
'type': "porter",
'desc': "Porter for %s on port %i"%(machine, port),
'worker': worker,
'properties': {
'port': port,
'socket-path': machines[machine]['porter'][port]['socket-path'],
'username': machines[machine]['porter'][port]['username'],
'password': machines[machine]['porter'][port]['password'],
},
}
if not found:
print "Streamer %i has no machine assigned" % (idx)
sys.exit(1)
idx+=1
env = Environment(loader=FileSystemLoader('../templates/%s/' % (sys.argv[1])))
template = env.get_template('planet.xml')
planet = template.render(globals=data['globals'], atmosphere=data['atmosphere'], flow=data['flow'])
sys.stdout.write(planet.encode("utf8"))
|