-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_salt_formula.py
61 lines (52 loc) · 1.6 KB
/
gen_salt_formula.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
import os
import sys
import argparse
# import os.path as op
parser = argparse.ArgumentParser(
description="Generate a salt formula skeleton")
parser.add_argument('--state', dest='is_state', type=bool, default=False)
parser.add_argument('formula_name', nargs='+', type=str)
args = parser.parse_args()
formula_name = ('_').join(args.formula_name)
if len(formula_name) == 0:
print("Please provide a formula name")
sys.exit(1)
# root_path = os.getcwd()
if args.is_state:
path_names = ['{0}/files'.format(formula_name)]
else:
path_names = ['{0}/{0}'.format(formula_name)]
os.makedirs(formula_name, mode=0o755, exist_ok=True)
for path in path_names:
os.makedirs(path, mode=0o755, exist_ok=True)
if args.is_state:
file_names = ['{0}/init.sls', '{0}/map.jinja']
else:
file_names = ['{0}/pillar.example', '{0}/README.rst', '{0}/VERSION',
'{0}/{0}/init.sls', '{0}/{0}/map.jinja']
for fname in file_names:
os.mknod(fname.format(formula_name), mode=0o644)
if not args.is_state:
with open('{0}/VERSION'.format(formula_name), 'w') as version:
version.write('0.0.1\n')
with open(file_names[-1].format(formula_name), 'w') as pkg:
pkg.write(
'''{{% set {0} = salt['grains.filter_by']({{
'Debian': {{
'pkgs': [
]
}},
'RedHat': {{
'pkgs': [
]
}},
'Arch': {{
'pkgs': [
]
}}
}}, merge=salt['pillar.get']('{0}:lookup')) %}}
'''.format(formula_name))
with open(file_names[-2].format(formula_name), 'w') as init:
init.write(
'''{{% from "{0}/map.jinja" import {0} with context %}}
'''.format(formula_name))