-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment.py
executable file
·154 lines (130 loc) · 5.2 KB
/
Assignment.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
#! /usr/bin/env python
import apt
import os,sys
import socket
import urllib2
import zipfile
import shutil
import getpass
import subprocess
import re
import grp
import pwd
def sanitycheck():
if not os.getuid()== 0:
sys.exit('Script must be run as root')
def chkPackageInstStatus(cache,packageName):
return cache[packageName].is_installed
def installPackage(cache,packageName):
pkg = cache[packageName]
print '%s is trusted:%s'%(packageName,pkg.candidate.origins[0].trusted)
pkg.mark_install()
print '%s is marked for install:%s'%(packageName,pkg.marked_install)
print '%s is %s:'%(packageName, pkg.candidate.summary)
def updateSourcesList():
with open("/etc/apt/sources.list.d/ondrej-php5-precise.list", "w") as php5Source:
php5Source.write("deb http://ppa.launchpad.net/ondrej/php5/ubuntu precise main\n")
php5Source.write("deb-src http://ppa.launchpad.net/ondrej/php5/ubuntu precise main")
def updateCache(cache):
cache.update()
cache.open(None)
cache.commit(apt.progress.TextFetchProgress(),apt.progress.InstallProgress())
return cache
def domainConf(dmn):
localip=socket.gethostbyname(socket.gethostname())
with open("/etc/hosts", "r") as hosts:
domains=hosts.readlines()
for domain in domains:
if dmn in domain:
print "Domain Seems to be already configure\nProceeding"
configured=True
break
else:
configured=False
if not configured:
with open("/etc/hosts", "a") as hosts:
hosts.write("%s %s"%(localip,dmn))
def nginxConf(dmn):
if not os.path.exists("/etc/nginx/sites-available/%s"%dmn):
with open("/etc/nginx/sites-available/%s"%(dmn), "w") as conf:
conf.write("""server{
server_name %s *.%s;
access_log /var/log/nginx/%s.access.log;
error_log /var/log/nginx/%s.error.log;
index index.php;
root /var/www/%s/htdocs;
location / {
try_files \$uri \$uri/ /index.php?\$args;
}
location ~ \.php$ {
include fastcgi_params;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
}
}"""%(dmn,dmn,dmn,dmn,dmn))
os.symlink("/etc/nginx/sites-available/%s"%dmn, "/etc/nginx/sites-enabled/%s"%dmn)
def wpconf(dmn):
print "Fetching latest version of Wordprerss"
f = urllib2.urlopen("http://wordpress.org/latest.zip")
with open("latest.zip", "wb") as code:
code.write(f.read())
zf=zipfile.ZipFile("latest.zip")
zf.extractall("/var/www/%s/"%dmn)
shutil.copytree("/var/www/%s/wordpress"%dmn, "/var/www/%s/htdocs/"%dmn)
shutil.rmtree("/var/www/%s/wordpress"%dmn)
uname=raw_input("Please Enter the Username of an existing user for database login:")
password=getpass.getpass(prompt="Please Enter the password of an existing user for database login:")
os.system("mysql -u %s -p%s -e 'create database examplecom_db'"%(uname,password))
salt = urllib2.urlopen("https://api.wordpress.org/secret-key/1.1/salt/")
with open("/var/www/"+dmn+"/htdocs/wp-config-sample.php", "r") as sample:
lines = sample.readlines()
with open("/var/www/"+dmn+"/htdocs/wp-config.php", "w") as config:
for line in lines:
config.write(re.sub(r'database_name_here', 'examplecom_db', line))
with open("/var/www/%s/htdocs/wp-config.php"%dmn, "r") as config:
lines = config.readlines()
with open("/var/www/%s/htdocs/wp-config.php"%dmn, "w") as config:
for line in lines:
config.write(re.sub(r'username_here', uname, line))
with open("/var/www/%s/htdocs/wp-config.php"%dmn, "r") as config:
lines = config.readlines()
with open("/var/www/%s/htdocs/wp-config.php"%dmn, "w") as config:
for line in lines:
config.write(re.sub(r'password_here', password, line))
with open("/var/www/%s/htdocs/wp-config.php"%dmn, "r") as config:
block = config.read()
with open("/var/www/%s/htdocs/wp-config.php"%dmn, "w") as config:
config.write(re.sub(r'/\*\*\#@\+.*?\#@-\*/', salt.read(), block,flags=re.DOTALL))
os.system("chown -R www-data:www-data /var/www/%s"%dmn)
def restart(service):
p = subprocess.Popen(['service', service, 'restart'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err =p.communicate()
print out
print err
def main():
sanitycheck()
cache=apt.Cache()
dmn=raw_input("Please Enter Domain Name:")
reqPackages=("php5-cgi","php5-mysql","php5-fpm","nginx","mysql-server")
for version in cache["php5-cgi"].versions:
if version.version.split("-")[0]>="5.4":
hasslistver=True;
else:
hasslistver=False;
if hasslistver == False:
updateSourcesList()
cache=updateCache(cache)
for package in reqPackages:
print "Checking %s Install Status:"%package
if not chkPackageInstStatus(cache,package):
installPackage(cache,package)
cache.commit()
domainConf(dmn)
nginxConf(dmn)
wpconf(dmn)
restart("php5-fpm")
restart("mysql")
restart("nginx")
print "Open %s in your Browser"%dmn
if __name__ == "__main__":
main()