blob: c95a03d3e588d95e949a5b5decbcd89f9651328e [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# 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
Anand Doshi0ebe35b2013-11-18 12:06:33 +053012requirements = [
13 "chardet",
14 "cssmin",
15 "dropbox",
16 "google-api-python-client",
17 "gunicorn",
18 "httplib2",
19 "jinja2",
20 "markdown2",
21 "markupsafe",
22 "mysql-python",
23 "pygeoip",
24 "python-dateutil",
25 "python-memcached",
26 "pytz==2013d",
27 "requests",
28 "six",
29 "slugify",
30 "termcolor",
Pratik Vyas374461b2013-11-28 16:57:33 +053031 "werkzeug",
32 "semantic_version",
33 "gitpython==0.3.2.RC1"
Pratik Vyasfe44d272013-10-24 23:59:53 +053034]
35
Pratik Vyas18515d32013-11-11 02:52:08 +053036def install(install_path):
Pratik Vyasfe44d272013-10-24 23:59:53 +053037 setup_folders(install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +053038 install_erpnext(install_path)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +053039
Rushabh Mehta54c15452013-07-16 12:05:41 +053040 post_install(install_path)
41
42def install_pre_requisites():
43 global is_redhat, is_debian
44 is_redhat, is_debian = validate_install()
45 if is_redhat:
46 install_using_yum()
47 elif is_debian:
48 install_using_apt()
49
50 install_python_modules()
51
52 print "-"*80
53 print "Pre-requisites Installed"
54 print "-"*80
55
56def validate_install():
57 import platform
58
59 # check os
60 operating_system = platform.system()
61 print "Operating System =", operating_system
62 if operating_system != "Linux":
63 raise Exception, "Sorry! This installer works only for Linux based Operating Systems"
64
65 # check python version
66 python_version = sys.version.split(" ")[0]
67 print "Python Version =", python_version
Pratik Vyasfe44d272013-10-24 23:59:53 +053068 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 +053069 raise Exception, "Hey! ERPNext needs Python version to be 2.7+"
Rushabh Mehta54c15452013-07-16 12:05:41 +053070
71 # check distribution
72 distribution = platform.linux_distribution()[0].lower().replace('"', '')
73 print "Distribution = ", distribution
Luis Perez5651d982013-11-07 14:53:49 -060074 is_redhat = distribution in ("redhat", "red hat enterprise linux server", "centos", "centos linux", "fedora")
Nabin Haitada70992013-08-20 10:58:07 +053075 is_debian = distribution in ("debian", "ubuntu", "elementary os", "linuxmint")
Rushabh Mehta54c15452013-07-16 12:05:41 +053076
77 if not (is_redhat or is_debian):
78 raise Exception, "Sorry! This installer works only with yum or apt-get package management"
79
80 return is_redhat, is_debian
81
82def install_using_yum():
Pratik Vyasfe44d272013-10-24 23:59:53 +053083 packages = "python python-setuptools gcc python-devel MySQL-python git memcached ntp vim-enhanced screen"
Rushabh Mehta54c15452013-07-16 12:05:41 +053084
85 print "-"*80
86 print "Installing Packages: (This may take some time)"
87 print packages
88 print "-"*80
89 exec_in_shell("yum install -y %s" % packages)
90
91 if not exec_in_shell("which mysql"):
92 packages = "mysql mysql-server mysql-devel"
93 print "Installing Packages:", packages
94 exec_in_shell("yum install -y %s" % packages)
95 exec_in_shell("service mysqld restart")
96
97 # set a root password post install
98 global root_password
99 print "Please create a password for root user of MySQL"
100 root_password = (get_root_password() or "erpnext").strip()
101 exec_in_shell('mysqladmin -u root password "%s"' % (root_password,))
102 print "Root password set as", root_password
103
104 # install htop
105 if not exec_in_shell("which htop"):
106 try:
107 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")
108 except:
109 pass
110
111 update_config_for_redhat()
112
113def update_config_for_redhat():
114 import re
115
Rushabh Mehta54c15452013-07-16 12:05:41 +0530116 # set to autostart on startup
Pratik Vyas18515d32013-11-11 02:52:08 +0530117 for service in ("mysqld", "memcached", "ntpd"):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530118 exec_in_shell("chkconfig --level 2345 %s on" % service)
119 exec_in_shell("service %s restart" % service)
120
121def install_using_apt():
Anand Doshib1b564c2013-07-29 11:44:26 +0530122 exec_in_shell("apt-get update")
Pratik Vyasfe44d272013-10-24 23:59:53 +0530123 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 +0530124 print "-"*80
125 print "Installing Packages: (This may take some time)"
126 print packages
127 print "-"*80
128 exec_in_shell("apt-get install -y %s" % packages)
Pratik Vyas4ec768b2013-10-28 21:57:59 +0530129 global root_password
130 if not root_password:
131 root_password = get_root_password()
132 exec_in_shell("echo mysql-server mysql-server/root_password password %s | sudo debconf-set-selections" % root_password)
133 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 +0530134
135 if not exec_in_shell("which mysql"):
136 packages = "mysql-server libmysqlclient-dev"
137 print "Installing Packages:", packages
138 exec_in_shell("apt-get install -y %s" % packages)
139
140 update_config_for_debian()
141
142def update_config_for_debian():
Pratik Vyasf4081c82013-10-28 14:43:00 +0530143 for service in ("mysql", "ntpd"):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530144 exec_in_shell("service %s restart" % service)
145
146def install_python_modules():
Rushabh Mehta54c15452013-07-16 12:05:41 +0530147 print "-"*80
148 print "Installing Python Modules: (This may take some time)"
Rushabh Mehta54c15452013-07-16 12:05:41 +0530149 print "-"*80
150
Anand Doshie7d77ca2013-08-14 15:12:46 +0530151 if not exec_in_shell("which pip"):
152 exec_in_shell("easy_install pip")
153
154 exec_in_shell("pip install --upgrade pip")
Anand Doshi3a6f4f82013-09-12 19:10:05 +0530155 exec_in_shell("pip install --upgrade setuptools")
Anand Doshie7d77ca2013-08-14 15:12:46 +0530156 exec_in_shell("pip install --upgrade virtualenv")
Pratik Vyas4ec768b2013-10-28 21:57:59 +0530157 exec_in_shell("pip install {}".format(' '.join(requirements)))
Anand Doshi08352ca2013-07-17 08:24:50 +0530158
Rushabh Mehta54c15452013-07-16 12:05:41 +0530159def install_erpnext(install_path):
160 print
161 print "-"*80
162 print "Installing ERPNext"
163 print "-"*80
164
165 # ask for details
166 global root_password
167 if not root_password:
168 root_password = get_root_password()
169 test_root_connection(root_password)
170
171 db_name = raw_input("ERPNext Database Name: ")
172 if not db_name:
173 raise Exception, "Sorry! You must specify ERPNext Database Name"
174
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530175 # setup paths
Pratik Vyasfe44d272013-10-24 23:59:53 +0530176 sys.path = [".", "lib", "app"] + sys.path
177 import wnf
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530178
Rushabh Mehta54c15452013-07-16 12:05:41 +0530179 # install database, run patches, update schema
Pratik Vyasfe44d272013-10-24 23:59:53 +0530180 # setup_db(install_path, root_password, db_name)
181 wnf.install(db_name, root_password=root_password)
182
Pratik Vyas18515d32013-11-11 02:52:08 +0530183 setup_cron(install_path)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530184
185def get_root_password():
186 # ask for root mysql password
187 import getpass
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530188 root_pwd = None
Rushabh Mehta54c15452013-07-16 12:05:41 +0530189 root_pwd = getpass.getpass("MySQL Root user's Password: ")
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530190 return root_pwd
191
192def test_root_connection(root_pwd):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530193 out = exec_in_shell("mysql -u root %s -e 'exit'" % \
194 (("-p"+root_pwd) if root_pwd else "").replace('$', '\$').replace(' ', '\ '))
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530195 if "access denied" in out.lower():
196 raise Exception("Incorrect MySQL Root user's password")
Rushabh Mehta54c15452013-07-16 12:05:41 +0530197
198def setup_folders(install_path):
Pratik Vyas18515d32013-11-11 02:52:08 +0530199 os.chdir(install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530200 app = os.path.join(install_path, "app")
201 if not os.path.exists(app):
202 print "Cloning erpnext"
Pratik Vyasf64dfa12013-10-31 11:43:51 +0530203 exec_in_shell("cd %s && git clone https://github.com/webnotes/erpnext.git app" % install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530204 exec_in_shell("cd app && git config core.filemode false")
Anand Doshi7d3e75d2013-07-24 19:01:02 +0530205 if not os.path.exists(app):
206 raise Exception, "Couldn't clone erpnext repository"
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530207
Rushabh Mehta54c15452013-07-16 12:05:41 +0530208 lib = os.path.join(install_path, "lib")
209 if not os.path.exists(lib):
210 print "Cloning wnframework"
Pratik Vyasf64dfa12013-10-31 11:43:51 +0530211 exec_in_shell("cd %s && git clone https://github.com/webnotes/wnframework.git lib" % install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530212 exec_in_shell("cd lib && git config core.filemode false")
Anand Doshi7d3e75d2013-07-24 19:01:02 +0530213 if not os.path.exists(lib):
214 raise Exception, "Couldn't clone wnframework repository"
Rushabh Mehta54c15452013-07-16 12:05:41 +0530215
216 public = os.path.join(install_path, "public")
217 for p in [public, os.path.join(public, "files"), os.path.join(public, "backups"),
218 os.path.join(install_path, "logs")]:
219 if not os.path.exists(p):
220 os.mkdir(p)
221
222def setup_conf(install_path, db_name):
223 import os, string, random, re
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530224
Rushabh Mehta54c15452013-07-16 12:05:41 +0530225 # generate db password
226 char_range = string.ascii_letters + string.digits
227 db_password = "".join((random.choice(char_range) for n in xrange(16)))
228
229 # make conf file
230 with open(os.path.join(install_path, "lib", "conf", "conf.py"), "r") as template:
231 conf = template.read()
232
233 conf = re.sub("db_name.*", 'db_name = "%s"' % (db_name,), conf)
234 conf = re.sub("db_password.*", 'db_password = "%s"' % (db_password,), conf)
235
236 with open(os.path.join(install_path, "conf.py"), "w") as conf_file:
237 conf_file.write(conf)
238
239 return db_password
240
Rushabh Mehta54c15452013-07-16 12:05:41 +0530241def post_install(install_path):
Pratik Vyas18515d32013-11-11 02:52:08 +0530242 pass
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530243
Rushabh Mehta54c15452013-07-16 12:05:41 +0530244def exec_in_shell(cmd):
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530245 # using Popen instead of os.system - as recommended by python docs
Rushabh Mehta54c15452013-07-16 12:05:41 +0530246 from subprocess import Popen
247 import tempfile
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530248
Rushabh Mehta54c15452013-07-16 12:05:41 +0530249 with tempfile.TemporaryFile() as stdout:
250 with tempfile.TemporaryFile() as stderr:
251 p = Popen(cmd, shell=True, stdout=stdout, stderr=stderr)
252 p.wait()
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530253
Rushabh Mehta54c15452013-07-16 12:05:41 +0530254 stdout.seek(0)
255 out = stdout.read()
Nabin Hait096d3632013-10-17 17:01:14 +0530256 if out: out = out.decode('utf-8')
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530257
Rushabh Mehta54c15452013-07-16 12:05:41 +0530258 stderr.seek(0)
259 err = stderr.read()
Nabin Hait096d3632013-10-17 17:01:14 +0530260 if err: err = err.decode('utf-8')
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530261
Rushabh Mehta54c15452013-07-16 12:05:41 +0530262 if err and any((kw in err.lower() for kw in ["traceback", "error", "exception"])):
263 print out
264 raise Exception, err
265 else:
266 print "."
267
268 return out
269
Pratik Vyas18515d32013-11-11 02:52:08 +0530270def parse_args():
271 parser = argparse.ArgumentParser()
272 parser.add_argument('--create_user', default=False, action='store_true')
273 parser.add_argument('--username', default='erpnext')
274 parser.add_argument('--password', default='erpnext')
275 parser.add_argument('--no_install_prerequisites', default=False, action='store_true')
276 return parser.parse_args()
277
278def create_user(username, password):
279 import subprocess, pwd
280 p = subprocess.Popen("useradd -m -d /home/{username} -s {shell} {username}".format(username=username, shell=os.environ.get('SHELL')).split())
281 p.wait()
282 p = subprocess.Popen("passwd {username}".format(username=username).split(), stdin=subprocess.PIPE)
283 p.communicate('{password}\n{password}\n'.format(password=password))
284 p.wait()
285 return pwd.getpwnam(username).pw_uid
286
287def setup_cron(install_path):
Jev Bjorsellffd851e2013-11-17 16:30:25 -0800288 erpnext_cron_entries = [
Anand Doshi0ebe35b2013-11-18 12:06:33 +0530289 "*/3 * * * * cd %s && python2.7 lib/wnf.py --run_scheduler >> erpnext-sch.log 2>&1" % install_path,
290 "0 */6 * * * cd %s && python2.7 lib/wnf.py --backup >> erpnext-backup.log 2>&1" % install_path
Jev Bjorsellffd851e2013-11-17 16:30:25 -0800291 ]
292 for row in erpnext_cron_entries:
293 try:
294 existing_cron = exec_in_shell("crontab -l")
295 if row not in existing_cron:
296 exec_in_shell('{ crontab -l; echo "%s"; } | crontab' % row)
297 except:
298 exec_in_shell('echo "%s" | crontab' % row)
Pratik Vyas18515d32013-11-11 02:52:08 +0530299
Rushabh Mehta54c15452013-07-16 12:05:41 +0530300if __name__ == "__main__":
Pratik Vyas18515d32013-11-11 02:52:08 +0530301 args = parse_args()
302 install_path = os.getcwd()
303 if os.getuid() != 0 and args.create_user and not args.no_install_prequisites:
304 raise Exception, "Please run this script as root"
305
306 if args.create_user:
307 uid = create_user(args.username, args.password)
308 install_path = '/home/{username}/erpnext'.format(username=args.username)
309
310 if not args.no_install_prerequisites:
311 install_pre_requisites()
312
313 if os.environ.get('SUDO_UID') and not args.create_user:
314 os.setuid(int(os.environ.get('SUDO_UID')))
315
316 if os.getuid() == 0 and args.create_user:
317 os.setuid(uid)
318 if install_path:
319 os.mkdir(install_path)
320
321 install(install_path=install_path)
322 print
323 print "-"*80
324 print "Installation complete"
325 print "To start the development server,"
326 print "Login as {username} with password {password}".format(username=args.username, password=args.password)
327 print "cd {}".format(install_path)
328 print "./lib/wnf.py --serve"
329 print "-"*80
330 print "Open your browser and go to http://localhost:8000"
331 print "Login using username = Administrator and password = admin"