blob: db1f40265e107885f019a1547394cfa3d5d4f837 [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 +05308is_redhat = is_debian = None
9root_password = None
10
Pratik Vyasfe44d272013-10-24 23:59:53 +053011requirements = [
12 "MySQL-python",
13 "pytz==2013b",
14 "python-dateutil",
15 "jinja2",
16 "markdown2",
17 "termcolor",
18 "python-memcached",
19 "requests",
20 "chardet",
21 "dropbox",
22 "google-api-python-client ",
23 "pygeoip"
24]
25
Rushabh Mehta54c15452013-07-16 12:05:41 +053026def install(install_path=None):
Pratik Vyasfe44d272013-10-24 23:59:53 +053027 if os.getuid() != 0:
28 raise Exception, "Please run this script as root"
29
Rushabh Mehta54c15452013-07-16 12:05:41 +053030 install_pre_requisites()
Pratik Vyasfe44d272013-10-24 23:59:53 +053031
32 if os.environ.get('SUDO_UID'):
33 os.setuid(int(os.environ.get('SUDO_UID')))
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +053034
Rushabh Mehta54c15452013-07-16 12:05:41 +053035 if not install_path:
36 install_path = os.getcwd()
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):
Rushabh Mehta54c15452013-07-16 12:05:41 +053069 raise Exception, "Hey! ERPNext needs Python version to be 2.6+"
70
71 # check distribution
72 distribution = platform.linux_distribution()[0].lower().replace('"', '')
73 print "Distribution = ", distribution
Anand Doshib1b564c2013-07-29 11:44:26 +053074 is_redhat = distribution in ("redhat", "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 # update memcache user
117 with open("/etc/sysconfig/memcached", "r") as original:
118 memcached_conf = original.read()
119 with open("/etc/sysconfig/memcached", "w") as modified:
120 modified.write(re.sub('USER.*', 'USER="%s"' % apache_user, memcached_conf))
121
122 # set to autostart on startup
123 for service in ("mysqld", "httpd", "memcached", "ntpd"):
124 exec_in_shell("chkconfig --level 2345 %s on" % service)
125 exec_in_shell("service %s restart" % service)
126
127def install_using_apt():
Anand Doshib1b564c2013-07-29 11:44:26 +0530128 exec_in_shell("apt-get update")
Pratik Vyasfe44d272013-10-24 23:59:53 +0530129 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 +0530130 print "-"*80
131 print "Installing Packages: (This may take some time)"
132 print packages
133 print "-"*80
134 exec_in_shell("apt-get install -y %s" % packages)
135
136 if not exec_in_shell("which mysql"):
137 packages = "mysql-server libmysqlclient-dev"
138 print "Installing Packages:", packages
139 exec_in_shell("apt-get install -y %s" % packages)
140
141 update_config_for_debian()
142
143def update_config_for_debian():
Pratik Vyasf4081c82013-10-28 14:43:00 +0530144 for service in ("mysql", "ntpd"):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530145 exec_in_shell("service %s restart" % service)
146
147def install_python_modules():
Rushabh Mehta54c15452013-07-16 12:05:41 +0530148 print "-"*80
149 print "Installing Python Modules: (This may take some time)"
150 print python_modules
151 print "-"*80
152
Anand Doshie7d77ca2013-08-14 15:12:46 +0530153 if not exec_in_shell("which pip"):
154 exec_in_shell("easy_install pip")
155
156 exec_in_shell("pip install --upgrade pip")
Anand Doshi3a6f4f82013-09-12 19:10:05 +0530157 exec_in_shell("pip install --upgrade setuptools")
Anand Doshie7d77ca2013-08-14 15:12:46 +0530158 exec_in_shell("pip install --upgrade virtualenv")
Pratik Vyasfe44d272013-10-24 23:59:53 +0530159 exec_in_shell("pip install -r {}".format(' '.join(requirements)))
Anand Doshi08352ca2013-07-17 08:24:50 +0530160
Rushabh Mehta54c15452013-07-16 12:05:41 +0530161def install_erpnext(install_path):
162 print
163 print "-"*80
164 print "Installing ERPNext"
165 print "-"*80
166
167 # ask for details
168 global root_password
169 if not root_password:
170 root_password = get_root_password()
171 test_root_connection(root_password)
172
173 db_name = raw_input("ERPNext Database Name: ")
174 if not db_name:
175 raise Exception, "Sorry! You must specify ERPNext Database Name"
176
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530177 # setup paths
Pratik Vyasfe44d272013-10-24 23:59:53 +0530178 sys.path = [".", "lib", "app"] + sys.path
179 import wnf
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530180
Rushabh Mehta54c15452013-07-16 12:05:41 +0530181 # install database, run patches, update schema
Pratik Vyasfe44d272013-10-24 23:59:53 +0530182 # setup_db(install_path, root_password, db_name)
183 wnf.install(db_name, root_password=root_password)
184
185 # setup_cron(install_path)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530186
187def get_root_password():
188 # ask for root mysql password
189 import getpass
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530190 root_pwd = None
Rushabh Mehta54c15452013-07-16 12:05:41 +0530191 root_pwd = getpass.getpass("MySQL Root user's Password: ")
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530192 return root_pwd
193
194def test_root_connection(root_pwd):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530195 out = exec_in_shell("mysql -u root %s -e 'exit'" % \
196 (("-p"+root_pwd) if root_pwd else "").replace('$', '\$').replace(' ', '\ '))
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530197 if "access denied" in out.lower():
198 raise Exception("Incorrect MySQL Root user's password")
Rushabh Mehta54c15452013-07-16 12:05:41 +0530199
200def setup_folders(install_path):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530201 app = os.path.join(install_path, "app")
202 if not os.path.exists(app):
203 print "Cloning erpnext"
Pratik Vyasfe44d272013-10-24 23:59:53 +0530204 exec_in_shell("cd %s && git clone https://github.com/webnotes/erpnext.git app && cd app && git checkout wsgi" % install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530205 exec_in_shell("cd app && git config core.filemode false")
Anand Doshi7d3e75d2013-07-24 19:01:02 +0530206 if not os.path.exists(app):
207 raise Exception, "Couldn't clone erpnext repository"
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530208
Rushabh Mehta54c15452013-07-16 12:05:41 +0530209 lib = os.path.join(install_path, "lib")
210 if not os.path.exists(lib):
211 print "Cloning wnframework"
Pratik Vyasfe44d272013-10-24 23:59:53 +0530212 exec_in_shell("cd %s && git clone https://github.com/webnotes/wnframework.git lib && cd lib && git checkout wsgi" % install_path)
Rushabh Mehta54c15452013-07-16 12:05:41 +0530213 exec_in_shell("cd lib && git config core.filemode false")
Anand Doshi7d3e75d2013-07-24 19:01:02 +0530214 if not os.path.exists(lib):
215 raise Exception, "Couldn't clone wnframework repository"
Rushabh Mehta54c15452013-07-16 12:05:41 +0530216
217 public = os.path.join(install_path, "public")
218 for p in [public, os.path.join(public, "files"), os.path.join(public, "backups"),
219 os.path.join(install_path, "logs")]:
220 if not os.path.exists(p):
221 os.mkdir(p)
222
223def setup_conf(install_path, db_name):
224 import os, string, random, re
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530225
Rushabh Mehta54c15452013-07-16 12:05:41 +0530226 # generate db password
227 char_range = string.ascii_letters + string.digits
228 db_password = "".join((random.choice(char_range) for n in xrange(16)))
229
230 # make conf file
231 with open(os.path.join(install_path, "lib", "conf", "conf.py"), "r") as template:
232 conf = template.read()
233
234 conf = re.sub("db_name.*", 'db_name = "%s"' % (db_name,), conf)
235 conf = re.sub("db_password.*", 'db_password = "%s"' % (db_password,), conf)
236
237 with open(os.path.join(install_path, "conf.py"), "w") as conf_file:
238 conf_file.write(conf)
239
240 return db_password
241
Rushabh Mehta54c15452013-07-16 12:05:41 +0530242def post_install(install_path):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530243 print
244 print "-"*80
Pratik Vyasfe44d272013-10-24 23:59:53 +0530245 print "To start the development server, run lib/wnf.py --serve"
Anand Doshicf2cf382013-08-09 19:39:09 +0530246 print "-"*80
Rushabh Mehta54c15452013-07-16 12:05:41 +0530247 print "Installation complete"
Pratik Vyasfe44d272013-10-24 23:59:53 +0530248 print "Open your browser and go to http://localhost:8000"
Rushabh Mehta54c15452013-07-16 12:05:41 +0530249 print "Login using username = Administrator and password = admin"
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530250
Rushabh Mehta54c15452013-07-16 12:05:41 +0530251def exec_in_shell(cmd):
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530252 # using Popen instead of os.system - as recommended by python docs
Rushabh Mehta54c15452013-07-16 12:05:41 +0530253 from subprocess import Popen
254 import tempfile
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530255
Rushabh Mehta54c15452013-07-16 12:05:41 +0530256 with tempfile.TemporaryFile() as stdout:
257 with tempfile.TemporaryFile() as stderr:
258 p = Popen(cmd, shell=True, stdout=stdout, stderr=stderr)
259 p.wait()
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530260
Rushabh Mehta54c15452013-07-16 12:05:41 +0530261 stdout.seek(0)
262 out = stdout.read()
Nabin Hait096d3632013-10-17 17:01:14 +0530263 if out: out = out.decode('utf-8')
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530264
Rushabh Mehta54c15452013-07-16 12:05:41 +0530265 stderr.seek(0)
266 err = stderr.read()
Nabin Hait096d3632013-10-17 17:01:14 +0530267 if err: err = err.decode('utf-8')
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530268
Rushabh Mehta54c15452013-07-16 12:05:41 +0530269 if err and any((kw in err.lower() for kw in ["traceback", "error", "exception"])):
270 print out
271 raise Exception, err
272 else:
273 print "."
274
275 return out
276
277if __name__ == "__main__":
Nabin Hait096d3632013-10-17 17:01:14 +0530278 install()