blob: a10ce10680588cf9c9d2b97ef2bac8511c92ab97 [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")
Anand Doshi7d3e75d2013-07-24 19:01:02 +0530196 if not os.path.exists(app):
197 raise Exception, "Couldn't clone erpnext repository"
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530198
Rushabh Mehta54c15452013-07-16 12:05:41 +0530199 lib = os.path.join(install_path, "lib")
200 if not os.path.exists(lib):
201 print "Cloning wnframework"
Anand Doshi08352ca2013-07-17 08:24:50 +0530202 exec_in_shell("cd %s && git clone https://github.com/webnotes/wnframework.git lib" % install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530203 exec_in_shell("cd lib && git config core.filemode false")
Anand Doshi7d3e75d2013-07-24 19:01:02 +0530204 if not os.path.exists(lib):
205 raise Exception, "Couldn't clone wnframework repository"
Rushabh Mehta54c15452013-07-16 12:05:41 +0530206
207 public = os.path.join(install_path, "public")
208 for p in [public, os.path.join(public, "files"), os.path.join(public, "backups"),
209 os.path.join(install_path, "logs")]:
210 if not os.path.exists(p):
211 os.mkdir(p)
212
213def setup_conf(install_path, db_name):
214 import os, string, random, re
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530215
Rushabh Mehta54c15452013-07-16 12:05:41 +0530216 # generate db password
217 char_range = string.ascii_letters + string.digits
218 db_password = "".join((random.choice(char_range) for n in xrange(16)))
219
220 # make conf file
221 with open(os.path.join(install_path, "lib", "conf", "conf.py"), "r") as template:
222 conf = template.read()
223
224 conf = re.sub("db_name.*", 'db_name = "%s"' % (db_name,), conf)
225 conf = re.sub("db_password.*", 'db_password = "%s"' % (db_password,), conf)
226
227 with open(os.path.join(install_path, "conf.py"), "w") as conf_file:
228 conf_file.write(conf)
229
230 return db_password
231
232def setup_db(install_path, root_password, db_name):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530233 from webnotes.install_lib.install import Installer
234 inst = Installer("root", root_password)
Anand Doshi08352ca2013-07-17 08:24:50 +0530235 inst.import_from_db(db_name, verbose=1)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530236
Rushabh Mehta54c15452013-07-16 12:05:41 +0530237 # run patches and sync
Rushabh Mehta54c15452013-07-16 12:05:41 +0530238 exec_in_shell("./lib/wnf.py --patch_sync_build")
239
240def setup_cron(install_path):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530241 erpnext_cron_entries = [
242 "*/3 * * * * cd %s && python lib/wnf.py --run_scheduler >> /var/log/erpnext-sch.log 2>&1" % install_path,
243 "0 */6 * * * cd %s && python lib/wnf.py --backup >> /var/log/erpnext-backup.log 2>&1" % install_path
244 ]
245
246 for row in erpnext_cron_entries:
247 try:
248 existing_cron = exec_in_shell("crontab -l")
249 if row not in existing_cron:
250 exec_in_shell('{ crontab -l; echo "%s"; } | crontab' % row)
251 except:
252 exec_in_shell('echo "%s" | crontab' % row)
253
254def setup_apache_conf(install_path):
255 apache_conf_content = """Listen 8080
256NameVirtualHost *:8080
257<VirtualHost *:8080>
258 ServerName localhost
259 DocumentRoot %s/public/
260
261 AddHandler cgi-script .cgi .xml .py
262 AddType application/vnd.ms-fontobject .eot
263 AddType font/ttf .ttf
264 AddType font/otf .otf
265 AddType application/x-font-woff .woff
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530266
Rushabh Mehta54c15452013-07-16 12:05:41 +0530267 <Directory %s/public/>
268 # directory specific options
269 Options -Indexes +FollowSymLinks +ExecCGI
270
271 # directory's index file
272 DirectoryIndex web.py
273
274 AllowOverride all
275 Order Allow,Deny
276 Allow from all
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530277
Rushabh Mehta54c15452013-07-16 12:05:41 +0530278 # rewrite rule
279 RewriteEngine on
280 RewriteCond %%{REQUEST_FILENAME} !-f
281 RewriteCond %%{REQUEST_FILENAME} !-d
282 RewriteCond %%{REQUEST_FILENAME} !-l
283 RewriteRule ^([^/]+)$ /web.py?page=$1 [QSA,L]
284 </Directory>
285</VirtualHost>""" % (install_path, install_path)
286
287 new_apache_conf_path = os.path.join(install_path, os.path.basename(install_path)+".conf")
288 with open(new_apache_conf_path, "w") as apache_conf_file:
289 apache_conf_file.write(apache_conf_content)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530290
Rushabh Mehta54c15452013-07-16 12:05:41 +0530291def post_install(install_path):
292 global apache_user
293 exec_in_shell("chown -R %s %s" % (apache_user, install_path))
294
295 apache_conf_filename = os.path.basename(install_path)+".conf"
296 if is_redhat:
297 os.symlink(os.path.join(install_path, apache_conf_filename),
298 os.path.join("/etc/httpd/conf.d", apache_conf_filename))
299 exec_in_shell("service httpd restart")
300
301 elif is_debian:
302 os.symlink(os.path.join(install_path, apache_conf_filename),
303 os.path.join("/etc/apache2/sites-enabled", apache_conf_filename))
304 exec_in_shell("service apache2 restart")
305
306 print
307 print "-"*80
308 print "Installation complete"
309 print "Open your browser and go to http://localhost:8080"
310 print "Login using username = Administrator and password = admin"
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530311
Rushabh Mehta54c15452013-07-16 12:05:41 +0530312def exec_in_shell(cmd):
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530313 # using Popen instead of os.system - as recommended by python docs
Rushabh Mehta54c15452013-07-16 12:05:41 +0530314 from subprocess import Popen
315 import tempfile
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530316
Rushabh Mehta54c15452013-07-16 12:05:41 +0530317 with tempfile.TemporaryFile() as stdout:
318 with tempfile.TemporaryFile() as stderr:
319 p = Popen(cmd, shell=True, stdout=stdout, stderr=stderr)
320 p.wait()
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530321
Rushabh Mehta54c15452013-07-16 12:05:41 +0530322 stdout.seek(0)
323 out = stdout.read()
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530324
Rushabh Mehta54c15452013-07-16 12:05:41 +0530325 stderr.seek(0)
326 err = stderr.read()
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530327
Rushabh Mehta54c15452013-07-16 12:05:41 +0530328 if err and any((kw in err.lower() for kw in ["traceback", "error", "exception"])):
329 print out
330 raise Exception, err
331 else:
332 print "."
333
334 return out
335
336if __name__ == "__main__":
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530337 install()