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 |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 7 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 8 | is_redhat = is_debian = None |
| 9 | root_password = None |
| 10 | |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 11 | requirements = [ |
| 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 Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 26 | def install(install_path=None): |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 27 | if os.getuid() != 0: |
| 28 | raise Exception, "Please run this script as root" |
| 29 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 30 | install_pre_requisites() |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 31 | |
| 32 | if os.environ.get('SUDO_UID'): |
| 33 | os.setuid(int(os.environ.get('SUDO_UID'))) |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 34 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 35 | if not install_path: |
| 36 | install_path = os.getcwd() |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 37 | setup_folders(install_path) |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 38 | install_erpnext(install_path) |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 39 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 40 | post_install(install_path) |
| 41 | |
| 42 | def 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 | |
| 56 | def 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 Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 68 | if not (python_version and int(python_version.split(".")[0])==2 and int(python_version.split(".")[1]) >= 7): |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 69 | 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 Doshi | b1b564c | 2013-07-29 11:44:26 +0530 | [diff] [blame] | 74 | is_redhat = distribution in ("redhat", "centos", "centos linux", "fedora") |
Nabin Hait | ada7099 | 2013-08-20 10:58:07 +0530 | [diff] [blame] | 75 | is_debian = distribution in ("debian", "ubuntu", "elementary os", "linuxmint") |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 76 | |
| 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 | |
| 82 | def install_using_yum(): |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 83 | 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] | 84 | |
| 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 | |
| 113 | def update_config_for_redhat(): |
| 114 | import re |
| 115 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 116 | # 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 | |
| 121 | def install_using_apt(): |
Anand Doshi | b1b564c | 2013-07-29 11:44:26 +0530 | [diff] [blame] | 122 | exec_in_shell("apt-get update") |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 123 | 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] | 124 | 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 | |
| 137 | def update_config_for_debian(): |
Pratik Vyas | f4081c8 | 2013-10-28 14:43:00 +0530 | [diff] [blame] | 138 | for service in ("mysql", "ntpd"): |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 139 | exec_in_shell("service %s restart" % service) |
| 140 | |
| 141 | def install_python_modules(): |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 142 | print "-"*80 |
| 143 | print "Installing Python Modules: (This may take some time)" |
| 144 | print python_modules |
| 145 | print "-"*80 |
| 146 | |
Anand Doshi | e7d77ca | 2013-08-14 15:12:46 +0530 | [diff] [blame] | 147 | if not exec_in_shell("which pip"): |
| 148 | exec_in_shell("easy_install pip") |
| 149 | |
| 150 | exec_in_shell("pip install --upgrade pip") |
Anand Doshi | 3a6f4f8 | 2013-09-12 19:10:05 +0530 | [diff] [blame] | 151 | exec_in_shell("pip install --upgrade setuptools") |
Anand Doshi | e7d77ca | 2013-08-14 15:12:46 +0530 | [diff] [blame] | 152 | exec_in_shell("pip install --upgrade virtualenv") |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 153 | exec_in_shell("pip install -r {}".format(' '.join(requirements))) |
Anand Doshi | 08352ca | 2013-07-17 08:24:50 +0530 | [diff] [blame] | 154 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 155 | def 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 Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 171 | # setup paths |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 172 | sys.path = [".", "lib", "app"] + sys.path |
| 173 | import wnf |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 174 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 175 | # install database, run patches, update schema |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 176 | # setup_db(install_path, root_password, db_name) |
| 177 | wnf.install(db_name, root_password=root_password) |
| 178 | |
| 179 | # setup_cron(install_path) |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 180 | |
| 181 | def get_root_password(): |
| 182 | # ask for root mysql password |
| 183 | import getpass |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 184 | root_pwd = None |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 185 | root_pwd = getpass.getpass("MySQL Root user's Password: ") |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 186 | return root_pwd |
| 187 | |
| 188 | def test_root_connection(root_pwd): |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 189 | out = exec_in_shell("mysql -u root %s -e 'exit'" % \ |
| 190 | (("-p"+root_pwd) if root_pwd else "").replace('$', '\$').replace(' ', '\ ')) |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 191 | if "access denied" in out.lower(): |
| 192 | raise Exception("Incorrect MySQL Root user's password") |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 193 | |
| 194 | def setup_folders(install_path): |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 195 | app = os.path.join(install_path, "app") |
| 196 | if not os.path.exists(app): |
| 197 | print "Cloning erpnext" |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 198 | exec_in_shell("cd %s && git clone https://github.com/webnotes/erpnext.git app && cd app && git checkout wsgi" % install_path) |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 199 | exec_in_shell("cd app && git config core.filemode false") |
Anand Doshi | 7d3e75d | 2013-07-24 19:01:02 +0530 | [diff] [blame] | 200 | if not os.path.exists(app): |
| 201 | raise Exception, "Couldn't clone erpnext repository" |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 202 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 203 | lib = os.path.join(install_path, "lib") |
| 204 | if not os.path.exists(lib): |
| 205 | print "Cloning wnframework" |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 206 | exec_in_shell("cd %s && git clone https://github.com/webnotes/wnframework.git lib && cd lib && git checkout wsgi" % install_path) |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 207 | exec_in_shell("cd lib && git config core.filemode false") |
Anand Doshi | 7d3e75d | 2013-07-24 19:01:02 +0530 | [diff] [blame] | 208 | if not os.path.exists(lib): |
| 209 | raise Exception, "Couldn't clone wnframework repository" |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 210 | |
| 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 | |
| 217 | def setup_conf(install_path, db_name): |
| 218 | import os, string, random, re |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 219 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 220 | # 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 Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 236 | def post_install(install_path): |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 237 | print |
| 238 | print "-"*80 |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 239 | print "To start the development server, run lib/wnf.py --serve" |
Anand Doshi | cf2cf38 | 2013-08-09 19:39:09 +0530 | [diff] [blame] | 240 | print "-"*80 |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 241 | print "Installation complete" |
Pratik Vyas | fe44d27 | 2013-10-24 23:59:53 +0530 | [diff] [blame] | 242 | print "Open your browser and go to http://localhost:8000" |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 243 | print "Login using username = Administrator and password = admin" |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 244 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 245 | def exec_in_shell(cmd): |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 246 | # using Popen instead of os.system - as recommended by python docs |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 247 | from subprocess import Popen |
| 248 | import tempfile |
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 | 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 Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 254 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 255 | stdout.seek(0) |
| 256 | out = stdout.read() |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 257 | if out: out = out.decode('utf-8') |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 258 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 259 | stderr.seek(0) |
| 260 | err = stderr.read() |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 261 | if err: err = err.decode('utf-8') |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 262 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 263 | 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 | |
| 271 | if __name__ == "__main__": |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 272 | install() |