-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
185 lines (143 loc) · 5.2 KB
/
main.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
import os, datetime
import argparse
from libraries import Vhost
import yaml
import pymssql, _mssql, uuid, decimal
from pprint import pprint, pformat
import settings
# per pyinstaller
resource_path = os.environ.get("_MEIPASS2", os.path.abspath(".") )
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=settings.DESCRIPTION
)
parser.add_argument('config_file', metavar='CONFIG_FILE',
help='File di configurazione')
parser.add_argument("-o", '--output_dir',
help='Directory in cui salvare il file/la directory generata, percorso assoluto',
default=settings.DEFAULT_OUTPUT_DIR)
parser.add_argument('--write_to_netposition',
action="store_true",
help='Scrive in NetPosition user e password generati',
default=False)
args = parser.parse_args()
output_dir = args.output_dir
if not os.path.exists(output_dir):
os.makedirs(output_dir)
try:
stream = file(args.config_file, 'r') # 'document.yaml' contains a single YAML document.
except IOError as e:
print "IO error, %s: %s " % (e.strerror, e.filename)
exit()
yaml_result = yaml.load(stream)
vhosts = []
yaml_defaults = yaml_result.get('defaults', {}) if type(yaml_result.get('defaults', {})) is dict else {}
for yaml_vhost in yaml_result['vhosts']:
vhosts.append(Vhost.yaml( dict(yaml_defaults.items() + yaml_vhost.items()) ) )
#altre operazioni necessarie
for vhost in vhosts:
vhost.generate_strings(resource_path)
print "PARSED: "
for index, vhost in enumerate(vhosts):
print "#" + unicode(index) + ":" + repr(vhost)
#user
print "Writing configurations..."
config_filename = os.path.basename(os.path.splitext(args.config_file)[0])
output_name = config_filename + "-" + datetime.datetime.now().strftime("%Y-%m-%d.%H-%M")
if len(vhosts) < 5:
f = open(output_dir + "\\" + output_name + ".txt", "w")
for vhost in vhosts:
f.write('\n')
f.write(vhost.user_string)
f.write('\n')
for vhost in vhosts:
f.write('\n')
f.write(vhost.logrotate_string)
f.write('\n')
for vhost in vhosts:
f.write(vhost.samba_string)
f.write('\n')
for vhost in vhosts:
f.write(vhost.vhost_string)
f.write('\n')
for vhost in vhosts:
f.write('\n')
f.write(vhost.cmd_string)
f.write('\n')
for vhost in vhosts:
if vhost.dns_giasone_string:
f.write('\n')
f.write(vhost.dns_giasone_string)
f.write('\n')
for vhost in vhosts:
if vhost.dns_castore_string:
f.write('\n')
f.write(vhost.dns_castore_string)
f.close()
else:
dir = output_dir + "\\" + output_name
if not os.path.exists(dir):
os.makedirs(dir)
f = open(dir + r"\users.txt", "w")
for vhost in vhosts:
f.write('\n')
f.write(vhost.user_string)
f.close()
f = open(dir + r"\logrotation.txt", "w")
for vhost in vhosts:
f.write('\n')
f.write(vhost.logrotate_string)
f.close()
f = open(dir + r"\samba.txt", "w")
for vhost in vhosts:
f.write('\n')
f.write(vhost.samba_string)
f.close()
f = open(dir + r"\vhosts.txt", "w")
for vhost in vhosts:
f.write('\n')
f.write(vhost.vhost_string)
f.close()
f = open(dir + r"\mkdir-default-dirs.txt", "w")
for vhost in vhosts:
f.write('\n')
f.write(vhost.cmd_string)
f.close()
f = open(dir + r"\dns-giasone.txt", "w")
for vhost in vhosts:
if vhost.dns_giasone_string:
f.write('\n')
f.write(vhost.dns_giasone_string)
f.close()
f = open(dir + r"\dns-castore.txt", "w")
for vhost in vhosts:
if vhost.dns_castore_string:
f.write('\n')
f.write(vhost.dns_castore_string)
f.close()
if args.write_to_netposition:
print "Writing on SQL server..."
conn = pymssql.connect(host='CASTORE', user='python', password='python', database='NetPosition', as_dict=True)
cur = conn.cursor()
descrizione = ">> python vhost generator @ " + datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S")
for vhost in vhosts:
cur.execute('SELECT * FROM DominiSingoli WHERE URL=%s and Username=%s', (vhost.name, vhost.user.name ))
row = cur.fetchone()
if row is not None:
cur.execute('UPDATE DominiSingoli SET Password=%s, Descrizione=%s where IdDominioSingolo = %s', (vhost.user.password, descrizione, row['IdDominioSingolo'] ))
else:
cur.execute('INSERT INTO DominiSingoli (URL, Username, Password, Descrizione) \
VALUES (%s, %s, %s, %s)', (vhost.name, vhost.user.name, vhost.user.password, descrizione ))
conn.commit()
conn.close()
############## TODO ################
### creazione script dns ###########
#su giasone
#dnscmd /ZoneAdd furla-branch2.local /Primary /file furla-branch2.local.dns
#dnscmd /RecordAdd furla-branch2.local dev2 A 192.168.2.111
#dnscmd /RecordAdd furla-branch2.local @ NS castore.netidea.local
#su castore
#dnscmd /zoneadd furla-branch2.local /secondary 192.168.2.100 furla-branch2.local.dns
#i file sono qui
#%WinDir%\System32\dns