blob: ca95baf8f75f93a8d4206432f404ceee8c9b8fb0 [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 # set to autostart on startup
117 for service in ("mysqld", "httpd", "memcached", "ntpd"):
118 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)
129
130 if not exec_in_shell("which mysql"):
131 packages = "mysql-server libmysqlclient-dev"
132 print "Installing Packages:", packages
133 exec_in_shell("apt-get install -y %s" % packages)
134
135 update_config_for_debian()
136
137def update_config_for_debian():
Pratik Vyasf4081c82013-10-28 14:43:00 +0530138 for service in ("mysql", "ntpd"):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530139 exec_in_shell("service %s restart" % service)
140
141def install_python_modules():
Rushabh Mehta54c15452013-07-16 12:05:41 +0530142 print "-"*80
143 print "Installing Python Modules: (This may take some time)"
144 print python_modules
145 print "-"*80
146
Anand Doshie7d77ca2013-08-14 15:12:46 +0530147 if not exec_in_shell("which pip"):
148 exec_in_shell("easy_install pip")
149
150 exec_in_shell("pip install --upgrade pip")
Anand Doshi3a6f4f82013-09-12 19:10:05 +0530151 exec_in_shell("pip install --upgrade setuptools")
Anand Doshie7d77ca2013-08-14 15:12:46 +0530152 exec_in_shell("pip install --upgrade virtualenv")
Pratik Vyasfe44d272013-10-24 23:59:53 +0530153 exec_in_shell("pip install -r {}".format(' '.join(requirements)))
Anand Doshi08352ca2013-07-17 08:24:50 +0530154
Rushabh Mehta54c15452013-07-16 12:05:41 +0530155def install_erpnext(install_path):
156 print
157 print "-"*80
158 print "Installing ERPNext"
159 print "-"*80
160
161 # ask for details
162 global root_password
163 if not root_password:
164 root_password = get_root_password()
165 test_root_connection(root_password)
166
167 db_name = raw_input("ERPNext Database Name: ")
168 if not db_name:
169 raise Exception, "Sorry! You must specify ERPNext Database Name"
170
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530171 # setup paths
Pratik Vyasfe44d272013-10-24 23:59:53 +0530172 sys.path = [".", "lib", "app"] + sys.path
173 import wnf
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530174
Rushabh Mehta54c15452013-07-16 12:05:41 +0530175 # install database, run patches, update schema
Pratik Vyasfe44d272013-10-24 23:59:53 +0530176 # setup_db(install_path, root_password, db_name)
177 wnf.install(db_name, root_password=root_password)
178
179 # setup_cron(install_path)
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530180
181def get_root_password():
182 # ask for root mysql password
183 import getpass
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530184 root_pwd = None
Rushabh Mehta54c15452013-07-16 12:05:41 +0530185 root_pwd = getpass.getpass("MySQL Root user's Password: ")
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530186 return root_pwd
187
188def test_root_connection(root_pwd):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530189 out = exec_in_shell("mysql -u root %s -e 'exit'" % \
190 (("-p"+root_pwd) if root_pwd else "").replace('$', '\$').replace(' ', '\ '))
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530191 if "access denied" in out.lower():
192 raise Exception("Incorrect MySQL Root user's password")
Rushabh Mehta54c15452013-07-16 12:05:41 +0530193
194def setup_folders(install_path):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530195 app = os.path.join(install_path, "app")
196 if not os.path.exists(app):
197 print "Cloning erpnext"
Pratik Vyasfe44d272013-10-24 23:59:53 +0530198 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 +0530199 exec_in_shell("cd app && git config core.filemode false")
Anand Doshi7d3e75d2013-07-24 19:01:02 +0530200 if not os.path.exists(app):
201 raise Exception, "Couldn't clone erpnext repository"
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530202
Rushabh Mehta54c15452013-07-16 12:05:41 +0530203 lib = os.path.join(install_path, "lib")
204 if not os.path.exists(lib):
205 print "Cloning wnframework"
Pratik Vyasfe44d272013-10-24 23:59:53 +0530206 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 +0530207 exec_in_shell("cd lib && git config core.filemode false")
Anand Doshi7d3e75d2013-07-24 19:01:02 +0530208 if not os.path.exists(lib):
209 raise Exception, "Couldn't clone wnframework repository"
Rushabh Mehta54c15452013-07-16 12:05:41 +0530210
211 public = os.path.join(install_path, "public")
212 for p in [public, os.path.join(public, "files"), os.path.join(public, "backups"),
213 os.path.join(install_path, "logs")]:
214 if not os.path.exists(p):
215 os.mkdir(p)
216
217def setup_conf(install_path, db_name):
218 import os, string, random, re
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530219
Rushabh Mehta54c15452013-07-16 12:05:41 +0530220 # generate db password
221 char_range = string.ascii_letters + string.digits
222 db_password = "".join((random.choice(char_range) for n in xrange(16)))
223
224 # make conf file
225 with open(os.path.join(install_path, "lib", "conf", "conf.py"), "r") as template:
226 conf = template.read()
227
228 conf = re.sub("db_name.*", 'db_name = "%s"' % (db_name,), conf)
229 conf = re.sub("db_password.*", 'db_password = "%s"' % (db_password,), conf)
230
231 with open(os.path.join(install_path, "conf.py"), "w") as conf_file:
232 conf_file.write(conf)
233
234 return db_password
235
Rushabh Mehta54c15452013-07-16 12:05:41 +0530236def post_install(install_path):
Rushabh Mehta54c15452013-07-16 12:05:41 +0530237 print
238 print "-"*80
Pratik Vyasfe44d272013-10-24 23:59:53 +0530239 print "To start the development server, run lib/wnf.py --serve"
Anand Doshicf2cf382013-08-09 19:39:09 +0530240 print "-"*80
Rushabh Mehta54c15452013-07-16 12:05:41 +0530241 print "Installation complete"
Pratik Vyasfe44d272013-10-24 23:59:53 +0530242 print "Open your browser and go to http://localhost:8000"
Rushabh Mehta54c15452013-07-16 12:05:41 +0530243 print "Login using username = Administrator and password = admin"
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530244
Rushabh Mehta54c15452013-07-16 12:05:41 +0530245def exec_in_shell(cmd):
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530246 # using Popen instead of os.system - as recommended by python docs
Rushabh Mehta54c15452013-07-16 12:05:41 +0530247 from subprocess import Popen
248 import tempfile
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530249
Rushabh Mehta54c15452013-07-16 12:05:41 +0530250 with tempfile.TemporaryFile() as stdout:
251 with tempfile.TemporaryFile() as stderr:
252 p = Popen(cmd, shell=True, stdout=stdout, stderr=stderr)
253 p.wait()
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530254
Rushabh Mehta54c15452013-07-16 12:05:41 +0530255 stdout.seek(0)
256 out = stdout.read()
Nabin Hait096d3632013-10-17 17:01:14 +0530257 if out: out = out.decode('utf-8')
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530258
Rushabh Mehta54c15452013-07-16 12:05:41 +0530259 stderr.seek(0)
260 err = stderr.read()
Nabin Hait096d3632013-10-17 17:01:14 +0530261 if err: err = err.decode('utf-8')
Rushabh Mehtaa8f9aa02013-06-21 17:55:43 +0530262
Rushabh Mehta54c15452013-07-16 12:05:41 +0530263 if err and any((kw in err.lower() for kw in ["traceback", "error", "exception"])):
264 print out
265 raise Exception, err
266 else:
267 print "."
268
269 return out
270
271if __name__ == "__main__":
Nabin Hait096d3632013-10-17 17:01:14 +0530272 install()