Ticket #2905: install.py

File install.py, 11.0 KB (added by alexarsh5@…, 16 years ago)

django app install procedure for fresh ubuntu

Line 
1import commands
2import os
3from string import find
4from optparse import OptionParser
5import shutil
6import sys
7import urllib2
8
9###################
10#Install functions#
11###################
12
13#Install Python
14def install_python():
15 print "Checking your python version..."
16 version = commands.getoutput('python --version')
17 if version.find('Python 2.5') == 0:
18 print "Your python version is OK"
19 else:
20 print "Your python version is %s and required version is 2.5.X" % version
21 print "Updating python..."
22 try:
23 os.system("sudo apt-get install python2.5 > /dev/null")
24 except:
25 sys.exit("Failed to install Python. Try to install it manually and rerun the script.")
26 print "Python 2.5 was installed"
27
28#Install Postgres
29def install_postgres(options):
30 if not options.local:
31 print "Checking your postgres version..."
32 if commands.getoutput('sudo apt-show-versions | grep postgresql-8.3'):
33 print "Your postgres version is OK"
34 else:
35 print "Your postgres version is not 8.3"
36 print "Installing postgres... (This can take some time)"
37 try:
38 print "Installing postgresql(1/4)..."
39 os.system("sudo apt-get -y install postgresql-8.3 > /dev/null")
40 print "Installing postgresql-client(2/4)..."
41 os.system("sudo apt-get -y install postgresql-client-8.3 > /dev/null")
42 print "Installing postgresql-contrib(3/4)..."
43 os.system("sudo apt-get -y install postgresql-contrib-8.3 > /dev/null")
44 print "Installing pgadmin3(4/4)..."
45 os.system("sudo apt-get -y install pgadmin3 > /dev/null")
46 except:
47 print "Failed to install Postgres. Try to install it manually and rerun the script."
48 exit()
49 print "Postgres was installed"
50 print "Creating postgres user..."
51 try:
52 os.system("echo \"ALTER USER postgres WITH PASSWORD 'qwerty123';\\q\" | sudo su postgres -c psql template1 > /dev/null")
53 os.system("sudo passwd -d postgres > /dev/null")
54 os.system("echo \"qwerty123\nqwerty123\" | sudo su postgres -c passwd 2>1& /dev/null")
55 except:
56 sys.exit("Failed to create postgres user. Try to create it manually and rerun the script.")
57 print "Postgres user created"
58
59#Install Psycopg2
60def install_psycopg2(options):
61 if not options.local:
62 print "Checking if you have psycopg2..."
63 if commands.getoutput("sudo apt-show-versions | grep psycopg2"):
64 print "Psycopg2 is installed"
65 else:
66 print "Installing psycopg2..."
67 try:
68 os.system("sudo apt-get -y install python2.5-psycopg2 > /dev/null")
69 except:
70 sys.exit("Failed to install psycopg2. Try to install it manually and rerun the script.")
71 print "Psycopg2 was installed"
72
73#Install Django
74def install_django(dvd, output):
75 print "Checking your django version..."
76 if commands.getoutput('django-admin.py --version').startswith("1.0.2"):
77 print "Your django version is OK"
78 else:
79 print "Installing django..."
80 try:
81 os.chdir("%s/linux" % dvd)
82 shutil.copy("Django-1.0.2-final.tar.gz", "%s/Django-1.0.2-final.tar.gz" % output)
83 os.chdir(output)
84 if not os.path.exists("%s/Django-1.0.2-final" % output):
85 os.system("sudo tar -zxvf Django-1.0.2-final.tar.gz > /dev/null")
86 os.chdir("%s/Django-1.0.2-final" % output)
87 os.system("sudo python2.5 setup.py install > /dev/null")
88 os.unlink("%s/Django-1.0.2-final.tar.gz" % output)
89 except:
90 sys.exit("Failed to install django. Try to install it manually and rerun the script.")
91 print "Django was installed"
92
93#Install Apache2
94def install_apache2():
95 print "Checking if you have apache2..."
96 if commands.getoutput("sudo apt-show-versions | grep apache2"):
97 print "Apache2 is installed"
98 else:
99 print "Installing apache2..."
100 try:
101 os.system("sudo apt-get -y install apache2 > /dev/null")
102 except:
103 sys.exit("Failed to install apache2. Try to install it manually and rerun the script.")
104 print "Apache2 was installed"
105
106#Install mod_python
107def install_mod_python():
108 print "Checking if you have mod_python..."
109 if commands.getoutput("sudo apt-show-versions | grep mod-python"):
110 print "Mod_python is installed"
111 else:
112 print "Installing mod_python..."
113 try:
114 os.system("sudo apt-get -y install libapache2-mod-python > /dev/null")
115 except:
116 sys.exit("Failed to install mod_python. Try to install it manually and rerun the script.")
117 print "Mod_python was installed"
118
119#Install python_imaging
120def install_python_imaging():
121 print "Checking if you have python_imaging..."
122 if commands.getoutput("sudo apt-show-versions | grep python-imaging"):
123 print "Python_imaging is installed"
124 else:
125 print "Installing python_imaging..."
126 try:
127 os.system("sudo apt-get -y install python-imaging > /dev/null")
128 except:
129 sys.exit("Failed to install python_imaging. Try to install it manually and rerun the script.")
130 print "Python_imaging was installed"
131
132#Edit settings.py
133def edit_settings(options, output):
134 if not options.local:
135 try:
136 file = open("%s/mx30/settings.py" % output, "r")
137 text = file.read()
138 file.close()
139 text = text.replace("DATABASE_ENGINE = 'sqlite3'", "DATABASE_ENGINE = 'postgresql_psycopg2'")
140 text = text.replace("peer_center.db", "peer_center")
141 text = text.replace("DATABASE_USER = ''", "DATABASE_USER = 'postgres'")
142 text = text.replace("DATABASE_PASSWORD = ''", "DATABASE_PASSWORD = 'qwerty123'")
143 text = text.replace("DATABASE_HOST = ''", "DATABASE_HOST = 'localhost'")
144 file = open("settings.py", "w")
145 file.write(text)
146 file.close()
147 except:
148 sys.exit("Failed to edit settings")
149 print "setting.py is OK"
150
151#Copying mx30 directory, media, admin media and configure settings
152def copy_and_configure(options, dvd, output):
153 print "Copying mx30 directory..."
154 os.chdir(dvd)
155 try:
156 if not os.path.exists("%s/mx30" % output):
157 shutil.copytree("mx30", "%s/mx30" % output)
158 os.chdir("%s/mx30" % output)
159 os.system("sudo chmod -R 777 *")
160 print "mx30 directory was copied"
161 else:
162 print "You have mx30 directory in %s already" % output
163 except:
164 sys.exit("Failed to copy mx30 directory")
165 edit_settings(options, output)
166 try:
167 if not os.path.exists("/var/www/media"):
168 shutil.move("%s/mx30/media" % output, "/var/www/media")
169 os.rename("/var/www/media", "/var/www/site_media")
170 else:
171 print "You have media directory already"
172 except:
173 sys.exit("Failed to move media directory")
174 try:
175 if not os.path.exists("/var/www/media"):
176 shutil.copytree("%s/Django-1.0.2-final/django/contrib/admin/media" % output, "/var/www/media")
177 else:
178 print "You have admin media directory already"
179 except:
180 sys.exit("Failed to copy admin media directory")
181 print "Media directories are OK"
182
183#Create database and load the data
184def create_db(output):
185 try:
186 os.system("echo \"qwerty123\n\" | sudo python %s/mx30/scripts/peer_center_install.pyc %s/mx30 > /dev/null" % (output, output))
187 except:
188 sys.exit("Failed to create the database and load the data.")
189 print "DB was created and data was loaded"
190
191#Configure ip and prefix
192def configure_ip_and_prefix(options, output):
193 try:
194 if options.ip:
195 os.system("sudo python %s/mx30/scripts/mx_config.pyc -i %s --prefix=/peergw" % (output, options.ip.split(":")[0]))
196 else:
197 os.system("sudo python %s/mx30/scripts/mx_config.pyc -i localhost --prefix=/peergw" % output)
198 except:
199 sys.exit("Failed to configure ip and prefix")
200 print "IP and prefix were configured"
201
202#Configure and restart apache
203def configure_and_restart_apache(output):
204 django_in_apache = commands.getoutput('sudo cat /etc/apache2/httpd.conf | grep "DJANGO_SETTINGS_MODULE"')
205 if django_in_apache:
206 print "Your apache is configured already"
207 else:
208 try:
209 shutil.copy("/etc/apache2/httpd.conf", "/etc/apache2/httpd_orig.conf")
210 file = open("%s/mx30/scripts/apache_conf.txt" % output, 'r')
211 text = file.read()
212 file.close()
213 file = open("/etc/apache2/httpd.conf", 'w')
214 file.write(text.replace("c:/mxhw", output))
215 file.close()
216 os.system('sudo echo "Listen 7777" >> /etc/apache2/httpd.conf')
217 except:
218 sys.exit("Failed to configue apache")
219 try:
220 os.system('sudo /etc/init.d/apache2 restart')
221 except:
222 sys.exit("Failed to restart apache")
223 print "Apache was configured and restarted"
224
225###############
226#Main function#
227###############
228
229def main():
230
231 ########################
232 #Parsing script options#
233 ########################
234
235 parser = OptionParser()
236 usage = "usage: mx_config [options] -i <install dvd location> -o <desired install location>"
237 parser.add_option("-i", "--input", dest="input", help="mx30 directory location for the installation")
238 parser.add_option("-o", "--output", dest="output", help="desired install directory location")
239 parser.add_option("-l", action="store_true", dest="local", help="Local installation")
240 parser.add_option("-I", "--ip", dest="ip", help="your ip")
241 (options, args) = parser.parse_args()
242
243 if not os.getuid()==0:
244 sys.exit("You must be root to run this script")
245 req = urllib2.Request('http://www.google.com')
246 try:
247 urllib2.urlopen(req)
248 except:
249 sys.exit("You must be connected to the internet to run this script")
250 if not options.input:
251 sys.exit("You should provide install dvd location. Use '-i' option.")
252 if not options.output:
253 sys.exit("You should provide the desired install location. Use '-o' option.")
254
255 dvd = os.path.abspath(options.input.split("mx30")[0])
256 output = os.path.abspath(options.output.split("mx30")[0])
257
258 ###################
259 #Install procedure#
260 ###################
261
262 print "Updating apt-get... (This can take some time)"
263 os.system("sudo apt-get update > /dev/null")
264 os.system("sudo apt-get -y install apt-show-versions > /dev/null")
265 print "Finished updating apt-get"
266 print "Copying files..."
267 shutil.copytree(dvd, "%s/dvd" % output)
268 dvd = os.path.abspath("%s/dvd" % output)
269 os.system("sudo chmod 777 -R %s" % dvd)
270 print "Finished copying files"
271 os.chdir(dvd)
272 install_python()
273 install_postgres(options)
274 install_psycopg2(options)
275 install_django(dvd, output)
276 install_apache2()
277 install_mod_python()
278 install_python_imaging()
279 copy_and_configure(options, dvd, output)
280 create_db(output)
281 configure_ip_and_prefix(options, output)
282 configure_and_restart_apache(output)
283 print "Deleting temporary files..."
284 shutil.rmtree(dvd)
285
286 if options.ip:
287 print "The installation was finished successfully. You application is running on: %s/peergw" % options.ip.split(":")[0]
288 else:
289 print "The installation was finished successfully. You application is running on: localhost/peergw"
290
291if __name__ == "__main__":
292 main()
Back to Top