/
account.py
executable file
·343 lines (158 loc) · 6.82 KB
/
account.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#/bin/python
#Account Registration Script
#Version 1.1
#Author Eldo Roshi
import sys
import subprocess
import os
import crypt
import io
import MySQLdb
import digitalocean
from datetime import datetime
import dns.zone
from dns.exception import DNSException
from dns.rdataclass import *
from dns.rdatatype import *
from urlparse import urlparse
IP_ADDRESS = "1.1.1.1"
TOKEN = "trytofindthistoken"
class AccountCreation:
def __init__(account, name, username, password, domain, email, theme):
account.name = name
account.password = password
account.username = username
account.domain = urlparse(domain).hostname
account.email = email
account.theme = theme
#Create User Function
def CreateUser(account):
password = crypt.crypt(account.password, "1987")
adduser = subprocess.Popen(["useradd", "-d", "/home/" + account.username, "-m", account.username, "-p", password])
return "user created"
#Create public html directory
def publichtml_dir(account):
try:
createdir = subprocess.check_output(["mkdir", "/home/" + account.username + "/public_html"])
return "Public Folder Created"
except subprocess.CalledProcessError:
return "Error Folder can't be created"
#Create Virtual Hosting
def VirtualHosting(account):
try:
f = open("/etc/apache2/sites-available/sites.conf", "a+")
f.write("<VirtualHost *:80> \n")
f.write("\tServerAdmin " + account.email + "\n")
f.write("\tServerName " + account.domain + "\n")
f.write("\tServerAlias " + "www." + account.domain + "\n")
f.write("\tDocumentRoot " + "/home/" + account.username + "/public_html \n")
f.write("\t<Directory /home/" + account.username + "/public_html> \n")
f.write("\t\tOptions Indexes FollowSymLinks MultiViews \n")
f.write("\t\tAllowOverride All \n")
f.write("\t\tRequire all granted \n")
f.write("\t</Directory> \n")
f.write("\tErrorLog ${APACHE_LOG_DIR}/error.log \n")
f.write("\tCustomLog ${APACHE_LOG_DIR}/access.log combined \n")
f.write("</VirtualHost> \n")
f.close()
except IOError:
return "Virtual host file can't be written"
#Create domain in Digital Ocean
def createdomain(account):
domain = digitalocean.Domain(name=account.domain, ip_address=IP_ADDRESS, token= TOKEN).create()
#Add Dns Zones
def dnszone (account):
zone_file = "/etc/bind/db.local"
domain = "localhost"
try:
zone = dns.zone.from_file(zone_file, domain)
for (name, ttl, rdata) in zone.iterate_rdatas(SOA):
print rdata
serial = rdata.serial
new_serial = datetime.now().strftime("%Y%m%d%H%M%S")
print "Changing SOA serial from %s to %s" % (serial, new_serial)
rdata.serial = int(new_serial)
rdata.mname = dns.name.Name(("ns1", "codeless-hosting", "co", ""))
print "Adding record of type A:", account.domain
NS_add = "@"
target = dns.name.Name(("ns1", "codeless-hosting", "co", ""))
print "Adding record of type NS:", NS_add
rdataset = zone.find_rdataset(NS_add, rdtype=NS, create=True)
rdata = dns.rdtypes.ANY.NS.NS(IN, NS, target)
rdataset.add(rdata, ttl=86400)
rdataset = zone.find_rdataset("www", rdtype=A, create=True)
rdata = dns.rdtypes.IN.A.A(IN, A, address="192.168.1.30")
rdataset.add(rdata, ttl=86400)
new_zone_file = "/etc/bind/zones/db.%s" % account.domain
zone.to_file(new_zone_file)
except DNSException, e:
print e
#Populate the named.conf.local zone
def addzone (account):
f = open("/etc/bind/named.conf.local", "a+")
f.write("\t zone \""+ account.domain + "\" { \n")
f.write("\t\t type master;\n")
f.write("\t\tfile \"/etc/bind/zones/db." + account.domain + "\"; \n")
f.write("};")
f.close()
def loadconfig (account):
try:
apachereload = subprocess.Popen(["apache2ctl", "graceful"])
print "restarted"
except subprocess.CalledProcessError:
print "Some error in the sites.conf file"
namedconfig = subprocess.check_output(["named-checkconf"])
if "" in namedconfig :
zonereload = subprocess.Popen(["rndc", "reload"])
zonereload.wait()
print zonereload.communicate()
def createdb(account):
db = MySQLdb.connect("localhost", "root", "eldo2014")
cur = db.cursor()
try:
db_name = account.username + "db"
createdb = "CREATE DATABASE IF NOT EXISTS %s;" %(db_name)
results = cur.execute(createdb)
return "Database Creation", results
createuser = "CREATE USER '%s'@'%s'" %(account.username, "localhost")
results = cur.execute(createuser)
return "User Creation", results
setpassword ="SET PASSWORD FOR '%s'@'%s' = PASSWORD('%s')" %(account.username, "localhost", account.password)
results = cur.execute(setpassword)
return "Set the user password", results
give_priviledges = "GRANT ALL ON %s.* TO '%s'@'%s';" %(db_name, account.username, "localhost")
results = cur.execute(give_priviledges)
return "Set user priviledges", results
except MySQLdb.Error, e:
return e
#Installation of Wordpress Core
def downloadWP(account):
try:
download = subprocess.check_output(["wp", "core", "download" , "--path=/home/"+ account.username + "/public_html", "--url="+ account.domain, "--user="+account.email, "--allow-root" ])
return "Wordpress Download Complete"
except subprocess.CalledProcessError:
return "Error during the process of wordpress core download"
#Install and configure wordpress
def installWP(account):
try:
config = subprocess.check_output(["wp", "core", "config", "--path=/home/"+ account.username + "/public_html", "--dbname="+ account.username + "db", "--dbuser="+ account.username, "--dbpass="+ account.password, "--allow-root" ])
installation = subprocess.check_output(["wp", "core", "install", "--path=/home/" + account.username + "/public_html", "--title=" + account.name , "--url=" + account.domain , "--admin_user=" + account.username, "--admin_password=" + account.password, "--admin_email=" + account.email, "--allow-root" ])
return "Installation its ok"
except subprocess.CalledProcessError:
return "Error during installation process"
def installtheme(account):
try:
theme = subprocess.check_output(["wp", "theme", "install", "--path=/home/"+ account.username + "/public_html", account.theme, "--activate", "--allow-root"])
return "Theme was Installed"
except subprocess.CalledProcessError:
return "Theme installation error"
#x = AccountCreation("unisol", "unisol", "unicorn", "unisol.com", "email@unisol.com", "twentyfifteen" )
#x.CreateUser()
#x.VirtualHosting()
#x.dnszone()
#x.addzone()
#x.loadconfig()
#x.publichtml_dir()
#x.createdb()
#x.downloadWP()
#x.installWP()