blob: 1a802aefdaa8d9927227df126dd3c7c46b0f096a [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2# License: GNU General Public License v3. See license.txt
3
Rushabh Mehta54c15452013-07-16 12:05:41 +05304#!/usr/bin/env python
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +05305from __future__ import unicode_literals
Rushabh Mehta54c15452013-07-16 12:05:41 +05306import os, sys
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +05307
Rushabh Mehta54c15452013-07-16 12:05:41 +05308apache_user = None
9is_redhat = is_debian = None
10root_password = None
11
12def install(install_path=None):
13 install_pre_requisites()
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +053014
Rushabh Mehta54c15452013-07-16 12:05:41 +053015 if not install_path:
16 install_path = os.getcwd()
17 install_erpnext(install_path)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +053018
Rushabh Mehta54c15452013-07-16 12:05:41 +053019 post_install(install_path)
20
21def install_pre_requisites():
22 global is_redhat, is_debian
23 is_redhat, is_debian = validate_install()
24 if is_redhat:
25 install_using_yum()
26 elif is_debian:
27 install_using_apt()
28
29 install_python_modules()
30
31 print "-"*80
32 print "Pre-requisites Installed"
33 print "-"*80
34
35def validate_install():
36 import platform
37
38 # check os
39 operating_system = platform.system()
40 print "Operating System =", operating_system
41 if operating_system != "Linux":
42 raise Exception, "Sorry! This installer works only for Linux based Operating Systems"
43
44 # check python version
45 python_version = sys.version.split(" ")[0]
46 print "Python Version =", python_version
47 if not (python_version and int(python_version.split(".")[0])==2 and int(python_version.split(".")[1]) >= 6):
48 raise Exception, "Hey! ERPNext needs Python version to be 2.6+"
49
50 # check distribution
51 distribution = platform.linux_distribution()[0].lower().replace('"', '')
52 print "Distribution = ", distribution
Anand Doshib1b564c2013-07-29 11:44:26 +053053 is_redhat = distribution in ("redhat", "centos", "centos linux", "fedora")
Rushabh Mehta54c15452013-07-16 12:05:41 +053054 is_debian = distribution in ("debian", "ubuntu", "elementary os")
55
56 if not (is_redhat or is_debian):
57 raise Exception, "Sorry! This installer works only with yum or apt-get package management"
58
59 return is_redhat, is_debian
60
61def install_using_yum():
62 packages = "python python-setuptools MySQL-python httpd git memcached ntp vim-enhanced screen"
63
64 print "-"*80
65 print "Installing Packages: (This may take some time)"
66 print packages
67 print "-"*80
68 exec_in_shell("yum install -y %s" % packages)
69
70 if not exec_in_shell("which mysql"):
71 packages = "mysql mysql-server mysql-devel"
72 print "Installing Packages:", packages
73 exec_in_shell("yum install -y %s" % packages)
74 exec_in_shell("service mysqld restart")
75
76 # set a root password post install
77 global root_password
78 print "Please create a password for root user of MySQL"
79 root_password = (get_root_password() or "erpnext").strip()
80 exec_in_shell('mysqladmin -u root password "%s"' % (root_password,))
81 print "Root password set as", root_password
82
83 # install htop
84 if not exec_in_shell("which htop"):
85 try:
86 exec_in_shell("cd /tmp && rpm -i --force http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm && yum install -y htop")
87 except:
88 pass
89
90 update_config_for_redhat()
91
92def update_config_for_redhat():
93 import re
94
95 global apache_user
96 apache_user = "apache"
97
98 # update memcache user
99 with open("/etc/sysconfig/memcached", "r") as original:
100 memcached_conf = original.read()
101 with open("/etc/sysconfig/memcached", "w") as modified:
102 modified.write(re.sub('USER.*', 'USER="%s"' % apache_user, memcached_conf))
103
104 # set to autostart on startup
105 for service in ("mysqld", "httpd", "memcached", "ntpd"):
106 exec_in_shell("chkconfig --level 2345 %s on" % service)
107 exec_in_shell("service %s restart" % service)
108
109def install_using_apt():
Anand Doshib1b564c2013-07-29 11:44:26 +0530110 exec_in_shell("apt-get update")
Rushabh Mehta54c15452013-07-16 12:05:41 +0530111 packages = "python python-setuptools python-mysqldb apache2 git memcached ntp vim screen htop"
112 print "-"*80
113 print "Installing Packages: (This may take some time)"
114 print packages
115 print "-"*80
116 exec_in_shell("apt-get install -y %s" % packages)
117
118 if not exec_in_shell("which mysql"):
119 packages = "mysql-server libmysqlclient-dev"
120 print "Installing Packages:", packages
121 exec_in_shell("apt-get install -y %s" % packages)
122
123 update_config_for_debian()
124
125def update_config_for_debian():
126 global apache_user
127 apache_user = "www-data"
128
129 # update memcache user
130 with open("/etc/memcached.conf", "r") as original:
131 memcached_conf = original.read()
132 with open("/etc/memcached.conf", "w") as modified:
133 modified.write(memcached_conf.replace("-u memcache", "-u %s" % apache_user))
134
135 exec_in_shell("a2enmod rewrite")
136
137 for service in ("mysql", "apache2", "memcached", "ntpd"):
138 exec_in_shell("service %s restart" % service)
139
140def install_python_modules():
Anand Doshi08352ca2013-07-17 08:24:50 +0530141 python_modules = "pytz python-dateutil jinja2 markdown2 termcolor python-memcached requests chardet dropbox google-api-python-client pygeoip"
Rushabh Mehta54c15452013-07-16 12:05:41 +0530142
143 print "-"*80
144 print "Installing Python Modules: (This may take some time)"
145 print python_modules
146 print "-"*80
147
148 exec_in_shell("easy_install pip")
149 exec_in_shell("pip install -q %s" % python_modules)
Anand Doshi08352ca2013-07-17 08:24:50 +0530150
Rushabh Mehta54c15452013-07-16 12:05:41 +0530151def install_erpnext(install_path):
152 print
153 print "-"*80
154 print "Installing ERPNext"
155 print "-"*80
156
157 # ask for details
158 global root_password
159 if not root_password:
160 root_password = get_root_password()
161 test_root_connection(root_password)
162
163 db_name = raw_input("ERPNext Database Name: ")
164 if not db_name:
165 raise Exception, "Sorry! You must specify ERPNext Database Name"
166
167 # install folders and conf
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530168 setup_folders(install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530169 setup_conf(install_path, db_name)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530170
171 # setup paths
Rushabh Mehta54c15452013-07-16 12:05:41 +0530172 sys.path.extend([".", "lib", "app"])
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530173
Rushabh Mehta54c15452013-07-16 12:05:41 +0530174 # install database, run patches, update schema
175 setup_db(install_path, root_password, db_name)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530176
Rushabh Mehta54c15452013-07-16 12:05:41 +0530177 setup_cron(install_path)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530178
Rushabh Mehta54c15452013-07-16 12:05:41 +0530179 setup_apache_conf(install_path)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530180
181def get_root_password():
182 # ask for root mysql password
183 import getpass
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530184 root_pwd = None
Rushabh Mehta54c15452013-07-16 12:05:41 +0530185 root_pwd = getpass.getpass("MySQL Root user's Password: ")
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530186 return root_pwd
187
188def test_root_connection(root_pwd):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530189 out = exec_in_shell("mysql -u root %s -e 'exit'" % \
190 (("-p"+root_pwd) if root_pwd else "").replace('$', '\$').replace(' ', '\ '))
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530191 if "access denied" in out.lower():
192 raise Exception("Incorrect MySQL Root user's password")
Rushabh Mehta54c15452013-07-16 12:05:41 +0530193
194def setup_folders(install_path):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530195 app = os.path.join(install_path, "app")
196 if not os.path.exists(app):
197 print "Cloning erpnext"
Anand Doshi08352ca2013-07-17 08:24:50 +0530198 exec_in_shell("cd %s && git clone https://github.com/webnotes/erpnext.git app" % install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530199 exec_in_shell("cd app && git config core.filemode false")
Anand Doshi7d3e75d2013-07-24 19:01:02 +0530200 if not os.path.exists(app):
201 raise Exception, "Couldn't clone erpnext repository"
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530202
Rushabh Mehta54c15452013-07-16 12:05:41 +0530203 lib = os.path.join(install_path, "lib")
204 if not os.path.exists(lib):
205 print "Cloning wnframework"
Anand Doshi08352ca2013-07-17 08:24:50 +0530206 exec_in_shell("cd %s && git clone https://github.com/webnotes/wnframework.git lib" % install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530207 exec_in_shell("cd lib && git config core.filemode false")
Anand Doshi7d3e75d2013-07-24 19:01:02 +0530208 if not os.path.exists(lib):
209 raise Exception, "Couldn't clone wnframework repository"
Rushabh Mehta54c15452013-07-16 12:05:41 +0530210
211 public = os.path.join(install_path, "public")
212 for p in [public, os.path.join(public, "files"), os.path.join(public, "backups"),
213 os.path.join(install_path, "logs")]:
214 if not os.path.exists(p):
215 os.mkdir(p)
216
217def setup_conf(install_path, db_name):
218 import os, string, random, re
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530219
Rushabh Mehta54c15452013-07-16 12:05:41 +0530220 # generate db password
221 char_range = string.ascii_letters + string.digits
222 db_password = "".join((random.choice(char_range) for n in xrange(16)))
223
224 # make conf file
225 with open(os.path.join(install_path, "lib", "conf", "conf.py"), "r") as template:
226 conf = template.read()
227
228 conf = re.sub("db_name.*", 'db_name = "%s"' % (db_name,), conf)
229 conf = re.sub("db_password.*", 'db_password = "%s"' % (db_password,), conf)
230
231 with open(os.path.join(install_path, "conf.py"), "w") as conf_file:
232 conf_file.write(conf)
233
234 return db_password
235
236def setup_db(install_path, root_password, db_name):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530237 from webnotes.install_lib.install import Installer
238 inst = Installer("root", root_password)
Anand Doshi08352ca2013-07-17 08:24:50 +0530239 inst.import_from_db(db_name, verbose=1)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530240
Rushabh Mehta54c15452013-07-16 12:05:41 +0530241 # run patches and sync
Rushabh Mehta54c15452013-07-16 12:05:41 +0530242 exec_in_shell("./lib/wnf.py --patch_sync_build")
243
244def setup_cron(install_path):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530245 erpnext_cron_entries = [
246 "*/3 * * * * cd %s && python lib/wnf.py --run_scheduler >> /var/log/erpnext-sch.log 2>&1" % install_path,
247 "0 */6 * * * cd %s && python lib/wnf.py --backup >> /var/log/erpnext-backup.log 2>&1" % install_path
248 ]
249
250 for row in erpnext_cron_entries:
251 try:
252 existing_cron = exec_in_shell("crontab -l")
253 if row not in existing_cron:
254 exec_in_shell('{ crontab -l; echo "%s"; } | crontab' % row)
255 except:
256 exec_in_shell('echo "%s" | crontab' % row)
257
258def setup_apache_conf(install_path):
259 apache_conf_content = """Listen 8080
260NameVirtualHost *:8080
261<VirtualHost *:8080>
262 ServerName localhost
263 DocumentRoot %s/public/
264
265 AddHandler cgi-script .cgi .xml .py
266 AddType application/vnd.ms-fontobject .eot
267 AddType font/ttf .ttf
268 AddType font/otf .otf
269 AddType application/x-font-woff .woff
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530270
Rushabh Mehta54c15452013-07-16 12:05:41 +0530271 <Directory %s/public/>
272 # directory specific options
273 Options -Indexes +FollowSymLinks +ExecCGI
274
275 # directory's index file
276 DirectoryIndex web.py
277
278 AllowOverride all
279 Order Allow,Deny
280 Allow from all
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530281
Rushabh Mehta54c15452013-07-16 12:05:41 +0530282 # rewrite rule
283 RewriteEngine on
284 RewriteCond %%{REQUEST_FILENAME} !-f
285 RewriteCond %%{REQUEST_FILENAME} !-d
286 RewriteCond %%{REQUEST_FILENAME} !-l
287 RewriteRule ^([^/]+)$ /web.py?page=$1 [QSA,L]
288 </Directory>
289</VirtualHost>""" % (install_path, install_path)
290
291 new_apache_conf_path = os.path.join(install_path, os.path.basename(install_path)+".conf")
292 with open(new_apache_conf_path, "w") as apache_conf_file:
293 apache_conf_file.write(apache_conf_content)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530294
Rushabh Mehta54c15452013-07-16 12:05:41 +0530295def post_install(install_path):
296 global apache_user
297 exec_in_shell("chown -R %s %s" % (apache_user, install_path))
298
299 apache_conf_filename = os.path.basename(install_path)+".conf"
300 if is_redhat:
301 os.symlink(os.path.join(install_path, apache_conf_filename),
302 os.path.join("/etc/httpd/conf.d", apache_conf_filename))
303 exec_in_shell("service httpd restart")
304
305 elif is_debian:
306 os.symlink(os.path.join(install_path, apache_conf_filename),
307 os.path.join("/etc/apache2/sites-enabled", apache_conf_filename))
308 exec_in_shell("service apache2 restart")
309
310 print
311 print "-"*80
Anand Doshicf2cf382013-08-09 19:39:09 +0530312 print "To change url domain, run: lib/wnf.py --domain example.com"
313 print "-"*80
Rushabh Mehta54c15452013-07-16 12:05:41 +0530314 print "Installation complete"
315 print "Open your browser and go to http://localhost:8080"
316 print "Login using username = Administrator and password = admin"
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530317
Rushabh Mehta54c15452013-07-16 12:05:41 +0530318def exec_in_shell(cmd):
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530319 # using Popen instead of os.system - as recommended by python docs
Rushabh Mehta54c15452013-07-16 12:05:41 +0530320 from subprocess import Popen
321 import tempfile
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530322
Rushabh Mehta54c15452013-07-16 12:05:41 +0530323 with tempfile.TemporaryFile() as stdout:
324 with tempfile.TemporaryFile() as stderr:
325 p = Popen(cmd, shell=True, stdout=stdout, stderr=stderr)
326 p.wait()
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530327
Rushabh Mehta54c15452013-07-16 12:05:41 +0530328 stdout.seek(0)
329 out = stdout.read()
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530330
Rushabh Mehta54c15452013-07-16 12:05:41 +0530331 stderr.seek(0)
332 err = stderr.read()
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530333
Rushabh Mehta54c15452013-07-16 12:05:41 +0530334 if err and any((kw in err.lower() for kw in ["traceback", "error", "exception"])):
335 print out
336 raise Exception, err
337 else:
338 print "."
339
340 return out
341
342if __name__ == "__main__":
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530343 install()