root / snf-image-host / decode-config.py @ 9a4838f0
History | View | Annotate | Download (1.7 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
|
3 |
# Copyright (C) 2012 GRNET S.A.
|
4 |
#
|
5 |
# This program is free software; you can redistribute it and/or modify
|
6 |
# it under the terms of the GNU General Public License as published by
|
7 |
# the Free Software Foundation; either version 2 of the License, or
|
8 |
# (at your option) any later version.
|
9 |
#
|
10 |
# This program is distributed in the hope that it will be useful, but
|
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 |
# General Public License for more details.
|
14 |
#
|
15 |
# You should have received a copy of the GNU General Public License
|
16 |
# along with this program; if not, write to the Free Software
|
17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
18 |
# 02110-1301, USA.
|
19 |
|
20 |
"""Decode a json encoded string with properties
|
21 |
|
22 |
This program decodes a json encoded properties string and outputs it in a
|
23 |
bash sourcable way. The properties are passed to the program through a JSON
|
24 |
string either read from a file or from standard input and are outputed to a
|
25 |
target file.
|
26 |
"""
|
27 |
|
28 |
import sys |
29 |
import os |
30 |
import subprocess |
31 |
import json |
32 |
import random |
33 |
import string |
34 |
from StringIO import StringIO |
35 |
|
36 |
|
37 |
def main(): |
38 |
options = sys.argv[1:]
|
39 |
|
40 |
prefix = ''.join(random.choice(string.ascii_uppercase) for x in range(8)) |
41 |
|
42 |
config = json.load(sys.stdin) |
43 |
|
44 |
for key, value in config.items(): |
45 |
if str(key).upper() in options: |
46 |
os.environ[prefix + str(key).upper()] = value
|
47 |
|
48 |
p = subprocess.Popen(['bash', '-c', 'set'], stdout=subprocess.PIPE) |
49 |
output = StringIO(p.communicate()[0])
|
50 |
for line in iter(output): |
51 |
if line.startswith(prefix):
|
52 |
print line[len(prefix):] |
53 |
|
54 |
return 0 |
55 |
|
56 |
|
57 |
if __name__ == "__main__": |
58 |
sys.exit(main()) |
59 |
|
60 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|