blob: 786b3a26d093a06fc34d17b2a6712111830c5db1 [file] [log] [blame]
Rushabh Mehta54c15452013-07-16 12:05:41 +05301#!/usr/bin/env python
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +05302from __future__ import unicode_literals
Rushabh Mehta54c15452013-07-16 12:05:41 +05303import os, sys
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +05304
Rushabh Mehta54c15452013-07-16 12:05:41 +05305apache_user = None
6is_redhat = is_debian = None
7root_password = None
8
9def install(install_path=None):
10 install_pre_requisites()
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +053011
Rushabh Mehta54c15452013-07-16 12:05:41 +053012 if not install_path:
13 install_path = os.getcwd()
14 install_erpnext(install_path)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +053015
Rushabh Mehta54c15452013-07-16 12:05:41 +053016 post_install(install_path)
17
18def install_pre_requisites():
19 global is_redhat, is_debian
20 is_redhat, is_debian = validate_install()
21 if is_redhat:
22 install_using_yum()
23 elif is_debian:
24 install_using_apt()
25
26 install_python_modules()
27
28 print "-"*80
29 print "Pre-requisites Installed"
30 print "-"*80
31
32def validate_install():
33 import platform
34
35 # check os
36 operating_system = platform.system()
37 print "Operating System =", operating_system
38 if operating_system != "Linux":
39 raise Exception, "Sorry! This installer works only for Linux based Operating Systems"
40
41 # check python version
42 python_version = sys.version.split(" ")[0]
43 print "Python Version =", python_version
44 if not (python_version and int(python_version.split(".")[0])==2 and int(python_version.split(".")[1]) >= 6):
45 raise Exception, "Hey! ERPNext needs Python version to be 2.6+"
46
47 # check distribution
48 distribution = platform.linux_distribution()[0].lower().replace('"', '')
49 print "Distribution = ", distribution
50 is_redhat = distribution in ("redhat", "centos", "fedora")
51 is_debian = distribution in ("debian", "ubuntu", "elementary os")
52
53 if not (is_redhat or is_debian):
54 raise Exception, "Sorry! This installer works only with yum or apt-get package management"
55
56 return is_redhat, is_debian
57
58def install_using_yum():
59 packages = "python python-setuptools MySQL-python httpd git memcached ntp vim-enhanced screen"
60
61 print "-"*80
62 print "Installing Packages: (This may take some time)"
63 print packages
64 print "-"*80
65 exec_in_shell("yum install -y %s" % packages)
66
67 if not exec_in_shell("which mysql"):
68 packages = "mysql mysql-server mysql-devel"
69 print "Installing Packages:", packages
70 exec_in_shell("yum install -y %s" % packages)
71 exec_in_shell("service mysqld restart")
72
73 # set a root password post install
74 global root_password
75 print "Please create a password for root user of MySQL"
76 root_password = (get_root_password() or "erpnext").strip()
77 exec_in_shell('mysqladmin -u root password "%s"' % (root_password,))
78 print "Root password set as", root_password
79
80 # install htop
81 if not exec_in_shell("which htop"):
82 try:
83 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")
84 except:
85 pass
86
87 update_config_for_redhat()
88
89def update_config_for_redhat():
90 import re
91
92 global apache_user
93 apache_user = "apache"
94
95 # update memcache user
96 with open("/etc/sysconfig/memcached", "r") as original:
97 memcached_conf = original.read()
98 with open("/etc/sysconfig/memcached", "w") as modified:
99 modified.write(re.sub('USER.*', 'USER="%s"' % apache_user, memcached_conf))
100
101 # set to autostart on startup
102 for service in ("mysqld", "httpd", "memcached", "ntpd"):
103 exec_in_shell("chkconfig --level 2345 %s on" % service)
104 exec_in_shell("service %s restart" % service)
105
106def install_using_apt():
107 packages = "python python-setuptools python-mysqldb apache2 git memcached ntp vim screen htop"
108 print "-"*80
109 print "Installing Packages: (This may take some time)"
110 print packages
111 print "-"*80
112 exec_in_shell("apt-get install -y %s" % packages)
113
114 if not exec_in_shell("which mysql"):
115 packages = "mysql-server libmysqlclient-dev"
116 print "Installing Packages:", packages
117 exec_in_shell("apt-get install -y %s" % packages)
118
119 update_config_for_debian()
120
121def update_config_for_debian():
122 global apache_user
123 apache_user = "www-data"
124
125 # update memcache user
126 with open("/etc/memcached.conf", "r") as original:
127 memcached_conf = original.read()
128 with open("/etc/memcached.conf", "w") as modified:
129 modified.write(memcached_conf.replace("-u memcache", "-u %s" % apache_user))
130
131 exec_in_shell("a2enmod rewrite")
132
133 for service in ("mysql", "apache2", "memcached", "ntpd"):
134 exec_in_shell("service %s restart" % service)
135
136def install_python_modules():
Anand Doshi08352ca2013-07-17 08:24:50 +0530137 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 +0530138
139 print "-"*80
140 print "Installing Python Modules: (This may take some time)"
141 print python_modules
142 print "-"*80
143
144 exec_in_shell("easy_install pip")
145 exec_in_shell("pip install -q %s" % python_modules)
Anand Doshi08352ca2013-07-17 08:24:50 +0530146
Rushabh Mehta54c15452013-07-16 12:05:41 +0530147def install_erpnext(install_path):
148 print
149 print "-"*80
150 print "Installing ERPNext"
151 print "-"*80
152
153 # ask for details
154 global root_password
155 if not root_password:
156 root_password = get_root_password()
157 test_root_connection(root_password)
158
159 db_name = raw_input("ERPNext Database Name: ")
160 if not db_name:
161 raise Exception, "Sorry! You must specify ERPNext Database Name"
162
163 # install folders and conf
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530164 setup_folders(install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530165 setup_conf(install_path, db_name)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530166
167 # setup paths
Rushabh Mehta54c15452013-07-16 12:05:41 +0530168 sys.path.extend([".", "lib", "app"])
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530169
Rushabh Mehta54c15452013-07-16 12:05:41 +0530170 # install database, run patches, update schema
171 setup_db(install_path, root_password, db_name)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530172
Rushabh Mehta54c15452013-07-16 12:05:41 +0530173 setup_cron(install_path)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530174
Rushabh Mehta54c15452013-07-16 12:05:41 +0530175 setup_apache_conf(install_path)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530176
177def get_root_password():
178 # ask for root mysql password
179 import getpass
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530180 root_pwd = None
Rushabh Mehta54c15452013-07-16 12:05:41 +0530181 root_pwd = getpass.getpass("MySQL Root user's Password: ")
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530182 return root_pwd
183
184def test_root_connection(root_pwd):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530185 out = exec_in_shell("mysql -u root %s -e 'exit'" % \
186 (("-p"+root_pwd) if root_pwd else "").replace('$', '\$').replace(' ', '\ '))
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530187 if "access denied" in out.lower():
188 raise Exception("Incorrect MySQL Root user's password")
Rushabh Mehta54c15452013-07-16 12:05:41 +0530189
190def setup_folders(install_path):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530191 app = os.path.join(install_path, "app")
192 if not os.path.exists(app):
193 print "Cloning erpnext"
Anand Doshi08352ca2013-07-17 08:24:50 +0530194 exec_in_shell("cd %s && git clone https://github.com/webnotes/erpnext.git app" % install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530195 exec_in_shell("cd app && git config core.filemode false")
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530196
Rushabh Mehta54c15452013-07-16 12:05:41 +0530197 lib = os.path.join(install_path, "lib")
198 if not os.path.exists(lib):
199 print "Cloning wnframework"
Anand Doshi08352ca2013-07-17 08:24:50 +0530200 exec_in_shell("cd %s && git clone https://github.com/webnotes/wnframework.git lib" % install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530201 exec_in_shell("cd lib && git config core.filemode false")
202
203 public = os.path.join(install_path, "public")
204 for p in [public, os.path.join(public, "files"), os.path.join(public, "backups"),
205 os.path.join(install_path, "logs")]:
206 if not os.path.exists(p):
207 os.mkdir(p)
208
209def setup_conf(install_path, db_name):
210 import os, string, random, re
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530211
Rushabh Mehta54c15452013-07-16 12:05:41 +0530212 # generate db password
213 char_range = string.ascii_letters + string.digits
214 db_password = "".join((random.choice(char_range) for n in xrange(16)))
215
216 # make conf file
217 with open(os.path.join(install_path, "lib", "conf", "conf.py"), "r") as template:
218 conf = template.read()
219
220 conf = re.sub("db_name.*", 'db_name = "%s"' % (db_name,), conf)
221 conf = re.sub("db_password.*", 'db_password = "%s"' % (db_password,), conf)
222
223 with open(os.path.join(install_path, "conf.py"), "w") as conf_file:
224 conf_file.write(conf)
225
226 return db_password
227
228def setup_db(install_path, root_password, db_name):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530229 from webnotes.install_lib.install import Installer
230 inst = Installer("root", root_password)
Anand Doshi08352ca2013-07-17 08:24:50 +0530231 inst.import_from_db(db_name, verbose=1)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530232
Rushabh Mehta54c15452013-07-16 12:05:41 +0530233 # run patches and sync
Rushabh Mehta54c15452013-07-16 12:05:41 +0530234 exec_in_shell("./lib/wnf.py --patch_sync_build")
235
236def setup_cron(install_path):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530237 erpnext_cron_entries = [
238 "*/3 * * * * cd %s && python lib/wnf.py --run_scheduler >> /var/log/erpnext-sch.log 2>&1" % install_path,
239 "0 */6 * * * cd %s && python lib/wnf.py --backup >> /var/log/erpnext-backup.log 2>&1" % install_path
240 ]
241
242 for row in erpnext_cron_entries:
243 try:
244 existing_cron = exec_in_shell("crontab -l")
245 if row not in existing_cron:
246 exec_in_shell('{ crontab -l; echo "%s"; } | crontab' % row)
247 except:
248 exec_in_shell('echo "%s" | crontab' % row)
249
250def setup_apache_conf(install_path):
251 apache_conf_content = """Listen 8080
252NameVirtualHost *:8080
253<VirtualHost *:8080>
254 ServerName localhost
255 DocumentRoot %s/public/
256
257 AddHandler cgi-script .cgi .xml .py
258 AddType application/vnd.ms-fontobject .eot
259 AddType font/ttf .ttf
260 AddType font/otf .otf
261 AddType application/x-font-woff .woff
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530262
Rushabh Mehta54c15452013-07-16 12:05:41 +0530263 <Directory %s/public/>
264 # directory specific options
265 Options -Indexes +FollowSymLinks +ExecCGI
266
267 # directory's index file
268 DirectoryIndex web.py
269
270 AllowOverride all
271 Order Allow,Deny
272 Allow from all
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530273
Rushabh Mehta54c15452013-07-16 12:05:41 +0530274 # rewrite rule
275 RewriteEngine on
276 RewriteCond %%{REQUEST_FILENAME} !-f
277 RewriteCond %%{REQUEST_FILENAME} !-d
278 RewriteCond %%{REQUEST_FILENAME} !-l
279 RewriteRule ^([^/]+)$ /web.py?page=$1 [QSA,L]
280 </Directory>
281</VirtualHost>""" % (install_path, install_path)
282
283 new_apache_conf_path = os.path.join(install_path, os.path.basename(install_path)+".conf")
284 with open(new_apache_conf_path, "w") as apache_conf_file:
285 apache_conf_file.write(apache_conf_content)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530286
Rushabh Mehta54c15452013-07-16 12:05:41 +0530287def post_install(install_path):
288 global apache_user
289 exec_in_shell("chown -R %s %s" % (apache_user, install_path))
290
291 apache_conf_filename = os.path.basename(install_path)+".conf"
292 if is_redhat:
293 os.symlink(os.path.join(install_path, apache_conf_filename),
294 os.path.join("/etc/httpd/conf.d", apache_conf_filename))
295 exec_in_shell("service httpd restart")
296
297 elif is_debian:
298 os.symlink(os.path.join(install_path, apache_conf_filename),
299 os.path.join("/etc/apache2/sites-enabled", apache_conf_filename))
300 exec_in_shell("service apache2 restart")
301
302 print
303 print "-"*80
304 print "Installation complete"
305 print "Open your browser and go to http://localhost:8080"
306 print "Login using username = Administrator and password = admin"
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530307
Rushabh Mehta54c15452013-07-16 12:05:41 +0530308def exec_in_shell(cmd):
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530309 # using Popen instead of os.system - as recommended by python docs
Rushabh Mehta54c15452013-07-16 12:05:41 +0530310 from subprocess import Popen
311 import tempfile
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530312
Rushabh Mehta54c15452013-07-16 12:05:41 +0530313 with tempfile.TemporaryFile() as stdout:
314 with tempfile.TemporaryFile() as stderr:
315 p = Popen(cmd, shell=True, stdout=stdout, stderr=stderr)
316 p.wait()
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530317
Rushabh Mehta54c15452013-07-16 12:05:41 +0530318 stdout.seek(0)
319 out = stdout.read()
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530320
Rushabh Mehta54c15452013-07-16 12:05:41 +0530321 stderr.seek(0)
322 err = stderr.read()
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530323
Rushabh Mehta54c15452013-07-16 12:05:41 +0530324 if err and any((kw in err.lower() for kw in ["traceback", "error", "exception"])):
325 print out
326 raise Exception, err
327 else:
328 print "."
329
330 return out
331
332if __name__ == "__main__":
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530333 install()