Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 4 | #!/usr/bin/env python |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 5 | from __future__ import unicode_literals |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 6 | import os, sys |
Pratik Vyas | 18515d3 | 2013-11-11 02:52:08 +0530 | [diff] [blame] | 7 | import argparse |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 8 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 9 | is_redhat = is_debian = None |
| 10 | root_password = None |
| 11 | |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 12 | requirements = [ |
| 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 Vyas | 4ec768b | 2013-10-28 21:57:59 +0530 | [diff] [blame] | 23 | "Werkzeug", |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 24 | "google-api-python-client ", |
| 25 | "pygeoip" |
| 26 | ] |
| 27 | |
Pratik Vyas | 18515d3 | 2013-11-11 02:52:08 +0530 | [diff] [blame] | 28 | def install(install_path): |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 29 | setup_folders(install_path) |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 30 | install_erpnext(install_path) |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 31 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 32 | post_install(install_path) |
| 33 | |
| 34 | def 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 | |
| 48 | def 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 Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 60 | if not (python_version and int(python_version.split(".")[0])==2 and int(python_version.split(".")[1]) >= 7): |
Pratik Vyas | 18515d3 | 2013-11-11 02:52:08 +0530 | [diff] [blame] | 61 | raise Exception, "Hey! ERPNext needs Python version to be 2.7+" |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 62 | |
| 63 | # check distribution |
| 64 | distribution = platform.linux_distribution()[0].lower().replace('"', '') |
| 65 | print "Distribution = ", distribution |
Luis Perez | 5651d98 | 2013-11-07 14:53:49 -0600 | [diff] [blame] | 66 | is_redhat = distribution in ("redhat", "red hat enterprise linux server", "centos", "centos linux", "fedora") |
Nabin Hait | ada7099 | 2013-08-20 10:58:07 +0530 | [diff] [blame] | 67 | is_debian = distribution in ("debian", "ubuntu", "elementary os", "linuxmint") |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 68 | |
| 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 | |
| 74 | def install_using_yum(): |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 75 | packages = "python python-setuptools gcc python-devel MySQL-python git memcached ntp vim-enhanced screen" |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 76 | |
| 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 | |
| 105 | def update_config_for_redhat(): |
| 106 | import re |
| 107 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 108 | # set to autostart on startup |
Pratik Vyas | 18515d3 | 2013-11-11 02:52:08 +0530 | [diff] [blame] | 109 | for service in ("mysqld", "memcached", "ntpd"): |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 110 | exec_in_shell("chkconfig --level 2345 %s on" % service) |
| 111 | exec_in_shell("service %s restart" % service) |
| 112 | |
| 113 | def install_using_apt(): |
Anand Doshi | b1b564c | 2013-07-29 11:44:26 +0530 | [diff] [blame] | 114 | exec_in_shell("apt-get update") |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 115 | packages = "python python-setuptools python-dev build-essential python-pip python-mysqldb git memcached ntp vim screen htop" |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 116 | 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 Vyas | 4ec768b | 2013-10-28 21:57:59 +0530 | [diff] [blame] | 121 | 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 Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 126 | |
| 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 | |
| 134 | def update_config_for_debian(): |
Pratik Vyas | f4081c8 | 2013-10-28 14:43:00 +0530 | [diff] [blame] | 135 | for service in ("mysql", "ntpd"): |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 136 | exec_in_shell("service %s restart" % service) |
| 137 | |
| 138 | def install_python_modules(): |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 139 | print "-"*80 |
| 140 | print "Installing Python Modules: (This may take some time)" |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 141 | print "-"*80 |
| 142 | |
Anand Doshi | e7d77ca | 2013-08-14 15:12:46 +0530 | [diff] [blame] | 143 | if not exec_in_shell("which pip"): |
| 144 | exec_in_shell("easy_install pip") |
| 145 | |
| 146 | exec_in_shell("pip install --upgrade pip") |
Anand Doshi | 3a6f4f8 | 2013-09-12 19:10:05 +0530 | [diff] [blame] | 147 | exec_in_shell("pip install --upgrade setuptools") |
Anand Doshi | e7d77ca | 2013-08-14 15:12:46 +0530 | [diff] [blame] | 148 | exec_in_shell("pip install --upgrade virtualenv") |
Pratik Vyas | 4ec768b | 2013-10-28 21:57:59 +0530 | [diff] [blame] | 149 | exec_in_shell("pip install {}".format(' '.join(requirements))) |
Anand Doshi | 08352ca | 2013-07-17 08:24:50 +0530 | [diff] [blame] | 150 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 151 | def 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 Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 167 | # setup paths |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 168 | sys.path = [".", "lib", "app"] + sys.path |
| 169 | import wnf |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 170 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 171 | # install database, run patches, update schema |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 172 | # setup_db(install_path, root_password, db_name) |
| 173 | wnf.install(db_name, root_password=root_password) |
| 174 | |
Pratik Vyas | 18515d3 | 2013-11-11 02:52:08 +0530 | [diff] [blame] | 175 | setup_cron(install_path) |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 176 | |
| 177 | def get_root_password(): |
| 178 | # ask for root mysql password |
| 179 | import getpass |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 180 | root_pwd = None |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 181 | root_pwd = getpass.getpass("MySQL Root user's Password: ") |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 182 | return root_pwd |
| 183 | |
| 184 | def test_root_connection(root_pwd): |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 185 | out = exec_in_shell("mysql -u root %s -e 'exit'" % \ |
| 186 | (("-p"+root_pwd) if root_pwd else "").replace('$', '\$').replace(' ', '\ ')) |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 187 | if "access denied" in out.lower(): |
| 188 | raise Exception("Incorrect MySQL Root user's password") |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 189 | |
| 190 | def setup_folders(install_path): |
Pratik Vyas | 18515d3 | 2013-11-11 02:52:08 +0530 | [diff] [blame] | 191 | os.chdir(install_path) |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 192 | app = os.path.join(install_path, "app") |
| 193 | if not os.path.exists(app): |
| 194 | print "Cloning erpnext" |
Pratik Vyas | f64dfa1 | 2013-10-31 11:43:51 +0530 | [diff] [blame] | 195 | exec_in_shell("cd %s && git clone https://github.com/webnotes/erpnext.git app" % install_path) |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 196 | exec_in_shell("cd app && git config core.filemode false") |
Anand Doshi | 7d3e75d | 2013-07-24 19:01:02 +0530 | [diff] [blame] | 197 | if not os.path.exists(app): |
| 198 | raise Exception, "Couldn't clone erpnext repository" |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 199 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 200 | lib = os.path.join(install_path, "lib") |
| 201 | if not os.path.exists(lib): |
| 202 | print "Cloning wnframework" |
Pratik Vyas | f64dfa1 | 2013-10-31 11:43:51 +0530 | [diff] [blame] | 203 | exec_in_shell("cd %s && git clone https://github.com/webnotes/wnframework.git lib" % install_path) |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 204 | exec_in_shell("cd lib && git config core.filemode false") |
Anand Doshi | 7d3e75d | 2013-07-24 19:01:02 +0530 | [diff] [blame] | 205 | if not os.path.exists(lib): |
| 206 | raise Exception, "Couldn't clone wnframework repository" |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 207 | |
| 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 | |
| 214 | def setup_conf(install_path, db_name): |
| 215 | import os, string, random, re |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 216 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 217 | # 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 Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 233 | def post_install(install_path): |
Pratik Vyas | 18515d3 | 2013-11-11 02:52:08 +0530 | [diff] [blame] | 234 | pass |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 235 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 236 | def exec_in_shell(cmd): |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 237 | # using Popen instead of os.system - as recommended by python docs |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 238 | from subprocess import Popen |
| 239 | import tempfile |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 240 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 241 | 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 Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 245 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 246 | stdout.seek(0) |
| 247 | out = stdout.read() |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 248 | if out: out = out.decode('utf-8') |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 249 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 250 | stderr.seek(0) |
| 251 | err = stderr.read() |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 252 | if err: err = err.decode('utf-8') |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 253 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 254 | 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 Vyas | 18515d3 | 2013-11-11 02:52:08 +0530 | [diff] [blame] | 262 | def 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 | |
| 270 | def 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 | |
| 279 | def 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 Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 292 | if __name__ == "__main__": |
Pratik Vyas | 18515d3 | 2013-11-11 02:52:08 +0530 | [diff] [blame] | 293 | 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" |