blob: 78f9459f109b6af21e3e19d6ead23d99ca2a8dbb [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
Pratik Vyas18515d32013-11-11 02:52:08 +05307import argparse
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +05308
Rushabh Mehta54c15452013-07-16 12:05:41 +05309is_redhat = is_debian = None
10root_password = None
11
Pratik Vyasfe44d272013-10-24 23:59:53 +053012requirements = [
13 "MySQL-python",
14 "pytz==2013b",
15 "python-dateutil",
16 "jinja2",
17 "markdown2",
18 "termcolor",
19 "python-memcached",
20 "requests",
21 "chardet",
22 "dropbox",
Pratik Vyas4ec768b2013-10-28 21:57:59 +053023 "Werkzeug",
Pratik Vyasfe44d272013-10-24 23:59:53 +053024 "google-api-python-client ",
25 "pygeoip"
26]
27
Pratik Vyas18515d32013-11-11 02:52:08 +053028def install(install_path):
Pratik Vyasfe44d272013-10-24 23:59:53 +053029 setup_folders(install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +053030 install_erpnext(install_path)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +053031
Rushabh Mehta54c15452013-07-16 12:05:41 +053032 post_install(install_path)
33
34def install_pre_requisites():
35 global is_redhat, is_debian
36 is_redhat, is_debian = validate_install()
37 if is_redhat:
38 install_using_yum()
39 elif is_debian:
40 install_using_apt()
41
42 install_python_modules()
43
44 print "-"*80
45 print "Pre-requisites Installed"
46 print "-"*80
47
48def validate_install():
49 import platform
50
51 # check os
52 operating_system = platform.system()
53 print "Operating System =", operating_system
54 if operating_system != "Linux":
55 raise Exception, "Sorry! This installer works only for Linux based Operating Systems"
56
57 # check python version
58 python_version = sys.version.split(" ")[0]
59 print "Python Version =", python_version
Pratik Vyasfe44d272013-10-24 23:59:53 +053060 if not (python_version and int(python_version.split(".")[0])==2 and int(python_version.split(".")[1]) >= 7):
Pratik Vyas18515d32013-11-11 02:52:08 +053061 raise Exception, "Hey! ERPNext needs Python version to be 2.7+"
Rushabh Mehta54c15452013-07-16 12:05:41 +053062
63 # check distribution
64 distribution = platform.linux_distribution()[0].lower().replace('"', '')
65 print "Distribution = ", distribution
Anand Doshib1b564c2013-07-29 11:44:26 +053066 is_redhat = distribution in ("redhat", "centos", "centos linux", "fedora")
Nabin Haitada70992013-08-20 10:58:07 +053067 is_debian = distribution in ("debian", "ubuntu", "elementary os", "linuxmint")
Rushabh Mehta54c15452013-07-16 12:05:41 +053068
69 if not (is_redhat or is_debian):
70 raise Exception, "Sorry! This installer works only with yum or apt-get package management"
71
72 return is_redhat, is_debian
73
74def install_using_yum():
Pratik Vyasfe44d272013-10-24 23:59:53 +053075 packages = "python python-setuptools gcc python-devel MySQL-python git memcached ntp vim-enhanced screen"
Rushabh Mehta54c15452013-07-16 12:05:41 +053076
77 print "-"*80
78 print "Installing Packages: (This may take some time)"
79 print packages
80 print "-"*80
81 exec_in_shell("yum install -y %s" % packages)
82
83 if not exec_in_shell("which mysql"):
84 packages = "mysql mysql-server mysql-devel"
85 print "Installing Packages:", packages
86 exec_in_shell("yum install -y %s" % packages)
87 exec_in_shell("service mysqld restart")
88
89 # set a root password post install
90 global root_password
91 print "Please create a password for root user of MySQL"
92 root_password = (get_root_password() or "erpnext").strip()
93 exec_in_shell('mysqladmin -u root password "%s"' % (root_password,))
94 print "Root password set as", root_password
95
96 # install htop
97 if not exec_in_shell("which htop"):
98 try:
99 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")
100 except:
101 pass
102
103 update_config_for_redhat()
104
105def update_config_for_redhat():
106 import re
107
Rushabh Mehta54c15452013-07-16 12:05:41 +0530108 # set to autostart on startup
Pratik Vyas18515d32013-11-11 02:52:08 +0530109 for service in ("mysqld", "memcached", "ntpd"):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530110 exec_in_shell("chkconfig --level 2345 %s on" % service)
111 exec_in_shell("service %s restart" % service)
112
113def install_using_apt():
Anand Doshib1b564c2013-07-29 11:44:26 +0530114 exec_in_shell("apt-get update")
Pratik Vyasfe44d272013-10-24 23:59:53 +0530115 packages = "python python-setuptools python-dev build-essential python-pip python-mysqldb git memcached ntp vim screen htop"
Rushabh Mehta54c15452013-07-16 12:05:41 +0530116 print "-"*80
117 print "Installing Packages: (This may take some time)"
118 print packages
119 print "-"*80
120 exec_in_shell("apt-get install -y %s" % packages)
Pratik Vyas4ec768b2013-10-28 21:57:59 +0530121 global root_password
122 if not root_password:
123 root_password = get_root_password()
124 exec_in_shell("echo mysql-server mysql-server/root_password password %s | sudo debconf-set-selections" % root_password)
125 exec_in_shell("echo mysql-server mysql-server/root_password_again password %s | sudo debconf-set-selections" % root_password)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530126
127 if not exec_in_shell("which mysql"):
128 packages = "mysql-server libmysqlclient-dev"
129 print "Installing Packages:", packages
130 exec_in_shell("apt-get install -y %s" % packages)
131
132 update_config_for_debian()
133
134def update_config_for_debian():
Pratik Vyasf4081c82013-10-28 14:43:00 +0530135 for service in ("mysql", "ntpd"):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530136 exec_in_shell("service %s restart" % service)
137
138def install_python_modules():
Rushabh Mehta54c15452013-07-16 12:05:41 +0530139 print "-"*80
140 print "Installing Python Modules: (This may take some time)"
Rushabh Mehta54c15452013-07-16 12:05:41 +0530141 print "-"*80
142
Anand Doshie7d77ca2013-08-14 15:12:46 +0530143 if not exec_in_shell("which pip"):
144 exec_in_shell("easy_install pip")
145
146 exec_in_shell("pip install --upgrade pip")
Anand Doshi3a6f4f82013-09-12 19:10:05 +0530147 exec_in_shell("pip install --upgrade setuptools")
Anand Doshie7d77ca2013-08-14 15:12:46 +0530148 exec_in_shell("pip install --upgrade virtualenv")
Pratik Vyas4ec768b2013-10-28 21:57:59 +0530149 exec_in_shell("pip install {}".format(' '.join(requirements)))
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
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530167 # setup paths
Pratik Vyasfe44d272013-10-24 23:59:53 +0530168 sys.path = [".", "lib", "app"] + sys.path
169 import wnf
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530170
Rushabh Mehta54c15452013-07-16 12:05:41 +0530171 # install database, run patches, update schema
Pratik Vyasfe44d272013-10-24 23:59:53 +0530172 # setup_db(install_path, root_password, db_name)
173 wnf.install(db_name, root_password=root_password)
174
Pratik Vyas18515d32013-11-11 02:52:08 +0530175 setup_cron(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):
Pratik Vyas18515d32013-11-11 02:52:08 +0530191 os.chdir(install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530192 app = os.path.join(install_path, "app")
193 if not os.path.exists(app):
194 print "Cloning erpnext"
Pratik Vyasf64dfa12013-10-31 11:43:51 +0530195 exec_in_shell("cd %s && git clone https://github.com/webnotes/erpnext.git app" % install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530196 exec_in_shell("cd app && git config core.filemode false")
Anand Doshi7d3e75d2013-07-24 19:01:02 +0530197 if not os.path.exists(app):
198 raise Exception, "Couldn't clone erpnext repository"
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530199
Rushabh Mehta54c15452013-07-16 12:05:41 +0530200 lib = os.path.join(install_path, "lib")
201 if not os.path.exists(lib):
202 print "Cloning wnframework"
Pratik Vyasf64dfa12013-10-31 11:43:51 +0530203 exec_in_shell("cd %s && git clone https://github.com/webnotes/wnframework.git lib" % install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530204 exec_in_shell("cd lib && git config core.filemode false")
Anand Doshi7d3e75d2013-07-24 19:01:02 +0530205 if not os.path.exists(lib):
206 raise Exception, "Couldn't clone wnframework repository"
Rushabh Mehta54c15452013-07-16 12:05:41 +0530207
208 public = os.path.join(install_path, "public")
209 for p in [public, os.path.join(public, "files"), os.path.join(public, "backups"),
210 os.path.join(install_path, "logs")]:
211 if not os.path.exists(p):
212 os.mkdir(p)
213
214def setup_conf(install_path, db_name):
215 import os, string, random, re
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530216
Rushabh Mehta54c15452013-07-16 12:05:41 +0530217 # generate db password
218 char_range = string.ascii_letters + string.digits
219 db_password = "".join((random.choice(char_range) for n in xrange(16)))
220
221 # make conf file
222 with open(os.path.join(install_path, "lib", "conf", "conf.py"), "r") as template:
223 conf = template.read()
224
225 conf = re.sub("db_name.*", 'db_name = "%s"' % (db_name,), conf)
226 conf = re.sub("db_password.*", 'db_password = "%s"' % (db_password,), conf)
227
228 with open(os.path.join(install_path, "conf.py"), "w") as conf_file:
229 conf_file.write(conf)
230
231 return db_password
232
Rushabh Mehta54c15452013-07-16 12:05:41 +0530233def post_install(install_path):
Pratik Vyas18515d32013-11-11 02:52:08 +0530234 pass
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530235
Rushabh Mehta54c15452013-07-16 12:05:41 +0530236def exec_in_shell(cmd):
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530237 # using Popen instead of os.system - as recommended by python docs
Rushabh Mehta54c15452013-07-16 12:05:41 +0530238 from subprocess import Popen
239 import tempfile
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530240
Rushabh Mehta54c15452013-07-16 12:05:41 +0530241 with tempfile.TemporaryFile() as stdout:
242 with tempfile.TemporaryFile() as stderr:
243 p = Popen(cmd, shell=True, stdout=stdout, stderr=stderr)
244 p.wait()
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530245
Rushabh Mehta54c15452013-07-16 12:05:41 +0530246 stdout.seek(0)
247 out = stdout.read()
Nabin Hait096d3632013-10-17 17:01:14 +0530248 if out: out = out.decode('utf-8')
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530249
Rushabh Mehta54c15452013-07-16 12:05:41 +0530250 stderr.seek(0)
251 err = stderr.read()
Nabin Hait096d3632013-10-17 17:01:14 +0530252 if err: err = err.decode('utf-8')
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530253
Rushabh Mehta54c15452013-07-16 12:05:41 +0530254 if err and any((kw in err.lower() for kw in ["traceback", "error", "exception"])):
255 print out
256 raise Exception, err
257 else:
258 print "."
259
260 return out
261
Pratik Vyas18515d32013-11-11 02:52:08 +0530262def parse_args():
263 parser = argparse.ArgumentParser()
264 parser.add_argument('--create_user', default=False, action='store_true')
265 parser.add_argument('--username', default='erpnext')
266 parser.add_argument('--password', default='erpnext')
267 parser.add_argument('--no_install_prerequisites', default=False, action='store_true')
268 return parser.parse_args()
269
270def create_user(username, password):
271 import subprocess, pwd
272 p = subprocess.Popen("useradd -m -d /home/{username} -s {shell} {username}".format(username=username, shell=os.environ.get('SHELL')).split())
273 p.wait()
274 p = subprocess.Popen("passwd {username}".format(username=username).split(), stdin=subprocess.PIPE)
275 p.communicate('{password}\n{password}\n'.format(password=password))
276 p.wait()
277 return pwd.getpwnam(username).pw_uid
278
279def setup_cron(install_path):
280 erpnext_cron_entries = [
281 "*/3 * * * * cd %s && python lib/wnf.py --run_scheduler >> erpnext-sch.log 2>&1" % install_path,
282 "0 */6 * * * cd %s && python lib/wnf.py --backup >> erpnext-backup.log 2>&1" % install_path
283 ]
284 for row in erpnext_cron_entries:
285 try:
286 existing_cron = exec_in_shell("crontab -l")
287 if row not in existing_cron:
288 exec_in_shell('{ crontab -l; echo "%s"; } | crontab' % row)
289 except:
290 exec_in_shell('echo "%s" | crontab' % row)
291
Rushabh Mehta54c15452013-07-16 12:05:41 +0530292if __name__ == "__main__":
Pratik Vyas18515d32013-11-11 02:52:08 +0530293 args = parse_args()
294 install_path = os.getcwd()
295 if os.getuid() != 0 and args.create_user and not args.no_install_prequisites:
296 raise Exception, "Please run this script as root"
297
298 if args.create_user:
299 uid = create_user(args.username, args.password)
300 install_path = '/home/{username}/erpnext'.format(username=args.username)
301
302 if not args.no_install_prerequisites:
303 install_pre_requisites()
304
305 if os.environ.get('SUDO_UID') and not args.create_user:
306 os.setuid(int(os.environ.get('SUDO_UID')))
307
308 if os.getuid() == 0 and args.create_user:
309 os.setuid(uid)
310 if install_path:
311 os.mkdir(install_path)
312
313 install(install_path=install_path)
314 print
315 print "-"*80
316 print "Installation complete"
317 print "To start the development server,"
318 print "Login as {username} with password {password}".format(username=args.username, password=args.password)
319 print "cd {}".format(install_path)
320 print "./lib/wnf.py --serve"
321 print "-"*80
322 print "Open your browser and go to http://localhost:8000"
323 print "Login using username = Administrator and password = admin"