Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 2 | from __future__ import unicode_literals |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 3 | import os, sys |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 4 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 5 | apache_user = None |
| 6 | is_redhat = is_debian = None |
| 7 | root_password = None |
| 8 | |
| 9 | def install(install_path=None): |
| 10 | install_pre_requisites() |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 11 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 12 | if not install_path: |
| 13 | install_path = os.getcwd() |
| 14 | install_erpnext(install_path) |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 15 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 16 | post_install(install_path) |
| 17 | |
| 18 | def install_pre_requisites(): |
| 19 | global is_redhat, is_debian |
| 20 | is_redhat, is_debian = validate_install() |
| 21 | if is_redhat: |
| 22 | install_using_yum() |
| 23 | elif is_debian: |
| 24 | install_using_apt() |
| 25 | |
| 26 | install_python_modules() |
| 27 | |
| 28 | print "-"*80 |
| 29 | print "Pre-requisites Installed" |
| 30 | print "-"*80 |
| 31 | |
| 32 | def validate_install(): |
| 33 | import platform |
| 34 | |
| 35 | # check os |
| 36 | operating_system = platform.system() |
| 37 | print "Operating System =", operating_system |
| 38 | if operating_system != "Linux": |
| 39 | raise Exception, "Sorry! This installer works only for Linux based Operating Systems" |
| 40 | |
| 41 | # check python version |
| 42 | python_version = sys.version.split(" ")[0] |
| 43 | print "Python Version =", python_version |
| 44 | if not (python_version and int(python_version.split(".")[0])==2 and int(python_version.split(".")[1]) >= 6): |
| 45 | raise Exception, "Hey! ERPNext needs Python version to be 2.6+" |
| 46 | |
| 47 | # check distribution |
| 48 | distribution = platform.linux_distribution()[0].lower().replace('"', '') |
| 49 | print "Distribution = ", distribution |
| 50 | is_redhat = distribution in ("redhat", "centos", "fedora") |
| 51 | is_debian = distribution in ("debian", "ubuntu", "elementary os") |
| 52 | |
| 53 | if not (is_redhat or is_debian): |
| 54 | raise Exception, "Sorry! This installer works only with yum or apt-get package management" |
| 55 | |
| 56 | return is_redhat, is_debian |
| 57 | |
| 58 | def install_using_yum(): |
| 59 | packages = "python python-setuptools MySQL-python httpd git memcached ntp vim-enhanced screen" |
| 60 | |
| 61 | print "-"*80 |
| 62 | print "Installing Packages: (This may take some time)" |
| 63 | print packages |
| 64 | print "-"*80 |
| 65 | exec_in_shell("yum install -y %s" % packages) |
| 66 | |
| 67 | if not exec_in_shell("which mysql"): |
| 68 | packages = "mysql mysql-server mysql-devel" |
| 69 | print "Installing Packages:", packages |
| 70 | exec_in_shell("yum install -y %s" % packages) |
| 71 | exec_in_shell("service mysqld restart") |
| 72 | |
| 73 | # set a root password post install |
| 74 | global root_password |
| 75 | print "Please create a password for root user of MySQL" |
| 76 | root_password = (get_root_password() or "erpnext").strip() |
| 77 | exec_in_shell('mysqladmin -u root password "%s"' % (root_password,)) |
| 78 | print "Root password set as", root_password |
| 79 | |
| 80 | # install htop |
| 81 | if not exec_in_shell("which htop"): |
| 82 | try: |
| 83 | 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") |
| 84 | except: |
| 85 | pass |
| 86 | |
| 87 | update_config_for_redhat() |
| 88 | |
| 89 | def update_config_for_redhat(): |
| 90 | import re |
| 91 | |
| 92 | global apache_user |
| 93 | apache_user = "apache" |
| 94 | |
| 95 | # update memcache user |
| 96 | with open("/etc/sysconfig/memcached", "r") as original: |
| 97 | memcached_conf = original.read() |
| 98 | with open("/etc/sysconfig/memcached", "w") as modified: |
| 99 | modified.write(re.sub('USER.*', 'USER="%s"' % apache_user, memcached_conf)) |
| 100 | |
| 101 | # set to autostart on startup |
| 102 | for service in ("mysqld", "httpd", "memcached", "ntpd"): |
| 103 | exec_in_shell("chkconfig --level 2345 %s on" % service) |
| 104 | exec_in_shell("service %s restart" % service) |
| 105 | |
| 106 | def install_using_apt(): |
| 107 | packages = "python python-setuptools python-mysqldb apache2 git memcached ntp vim screen htop" |
| 108 | print "-"*80 |
| 109 | print "Installing Packages: (This may take some time)" |
| 110 | print packages |
| 111 | print "-"*80 |
| 112 | exec_in_shell("apt-get install -y %s" % packages) |
| 113 | |
| 114 | if not exec_in_shell("which mysql"): |
| 115 | packages = "mysql-server libmysqlclient-dev" |
| 116 | print "Installing Packages:", packages |
| 117 | exec_in_shell("apt-get install -y %s" % packages) |
| 118 | |
| 119 | update_config_for_debian() |
| 120 | |
| 121 | def update_config_for_debian(): |
| 122 | global apache_user |
| 123 | apache_user = "www-data" |
| 124 | |
| 125 | # update memcache user |
| 126 | with open("/etc/memcached.conf", "r") as original: |
| 127 | memcached_conf = original.read() |
| 128 | with open("/etc/memcached.conf", "w") as modified: |
| 129 | modified.write(memcached_conf.replace("-u memcache", "-u %s" % apache_user)) |
| 130 | |
| 131 | exec_in_shell("a2enmod rewrite") |
| 132 | |
| 133 | for service in ("mysql", "apache2", "memcached", "ntpd"): |
| 134 | exec_in_shell("service %s restart" % service) |
| 135 | |
| 136 | def install_python_modules(): |
Anand Doshi | 08352ca | 2013-07-17 08:24:50 +0530 | [diff] [blame] | 137 | python_modules = "pytz python-dateutil jinja2 markdown2 termcolor python-memcached requests chardet dropbox google-api-python-client pygeoip" |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 138 | |
| 139 | print "-"*80 |
| 140 | print "Installing Python Modules: (This may take some time)" |
| 141 | print python_modules |
| 142 | print "-"*80 |
| 143 | |
| 144 | exec_in_shell("easy_install pip") |
| 145 | exec_in_shell("pip install -q %s" % python_modules) |
Anand Doshi | 08352ca | 2013-07-17 08:24:50 +0530 | [diff] [blame] | 146 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 147 | def install_erpnext(install_path): |
| 148 | print |
| 149 | print "-"*80 |
| 150 | print "Installing ERPNext" |
| 151 | print "-"*80 |
| 152 | |
| 153 | # ask for details |
| 154 | global root_password |
| 155 | if not root_password: |
| 156 | root_password = get_root_password() |
| 157 | test_root_connection(root_password) |
| 158 | |
| 159 | db_name = raw_input("ERPNext Database Name: ") |
| 160 | if not db_name: |
| 161 | raise Exception, "Sorry! You must specify ERPNext Database Name" |
| 162 | |
| 163 | # install folders and conf |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 164 | setup_folders(install_path) |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 165 | setup_conf(install_path, db_name) |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 166 | |
| 167 | # setup paths |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 168 | sys.path.extend([".", "lib", "app"]) |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 169 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 170 | # install database, run patches, update schema |
| 171 | setup_db(install_path, root_password, db_name) |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 172 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 173 | setup_cron(install_path) |
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 | setup_apache_conf(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): |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 191 | app = os.path.join(install_path, "app") |
| 192 | if not os.path.exists(app): |
| 193 | print "Cloning erpnext" |
Anand Doshi | 08352ca | 2013-07-17 08:24:50 +0530 | [diff] [blame] | 194 | 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] | 195 | exec_in_shell("cd app && git config core.filemode false") |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 196 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 197 | lib = os.path.join(install_path, "lib") |
| 198 | if not os.path.exists(lib): |
| 199 | print "Cloning wnframework" |
Anand Doshi | 08352ca | 2013-07-17 08:24:50 +0530 | [diff] [blame] | 200 | 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] | 201 | exec_in_shell("cd lib && git config core.filemode false") |
| 202 | |
| 203 | public = os.path.join(install_path, "public") |
| 204 | for p in [public, os.path.join(public, "files"), os.path.join(public, "backups"), |
| 205 | os.path.join(install_path, "logs")]: |
| 206 | if not os.path.exists(p): |
| 207 | os.mkdir(p) |
| 208 | |
| 209 | def setup_conf(install_path, db_name): |
| 210 | import os, string, random, re |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 211 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 212 | # generate db password |
| 213 | char_range = string.ascii_letters + string.digits |
| 214 | db_password = "".join((random.choice(char_range) for n in xrange(16))) |
| 215 | |
| 216 | # make conf file |
| 217 | with open(os.path.join(install_path, "lib", "conf", "conf.py"), "r") as template: |
| 218 | conf = template.read() |
| 219 | |
| 220 | conf = re.sub("db_name.*", 'db_name = "%s"' % (db_name,), conf) |
| 221 | conf = re.sub("db_password.*", 'db_password = "%s"' % (db_password,), conf) |
| 222 | |
| 223 | with open(os.path.join(install_path, "conf.py"), "w") as conf_file: |
| 224 | conf_file.write(conf) |
| 225 | |
| 226 | return db_password |
| 227 | |
| 228 | def setup_db(install_path, root_password, db_name): |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 229 | from webnotes.install_lib.install import Installer |
| 230 | inst = Installer("root", root_password) |
Anand Doshi | 08352ca | 2013-07-17 08:24:50 +0530 | [diff] [blame] | 231 | inst.import_from_db(db_name, verbose=1) |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 232 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 233 | # run patches and sync |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 234 | exec_in_shell("./lib/wnf.py --patch_sync_build") |
| 235 | |
| 236 | def setup_cron(install_path): |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 237 | erpnext_cron_entries = [ |
| 238 | "*/3 * * * * cd %s && python lib/wnf.py --run_scheduler >> /var/log/erpnext-sch.log 2>&1" % install_path, |
| 239 | "0 */6 * * * cd %s && python lib/wnf.py --backup >> /var/log/erpnext-backup.log 2>&1" % install_path |
| 240 | ] |
| 241 | |
| 242 | for row in erpnext_cron_entries: |
| 243 | try: |
| 244 | existing_cron = exec_in_shell("crontab -l") |
| 245 | if row not in existing_cron: |
| 246 | exec_in_shell('{ crontab -l; echo "%s"; } | crontab' % row) |
| 247 | except: |
| 248 | exec_in_shell('echo "%s" | crontab' % row) |
| 249 | |
| 250 | def setup_apache_conf(install_path): |
| 251 | apache_conf_content = """Listen 8080 |
| 252 | NameVirtualHost *:8080 |
| 253 | <VirtualHost *:8080> |
| 254 | ServerName localhost |
| 255 | DocumentRoot %s/public/ |
| 256 | |
| 257 | AddHandler cgi-script .cgi .xml .py |
| 258 | AddType application/vnd.ms-fontobject .eot |
| 259 | AddType font/ttf .ttf |
| 260 | AddType font/otf .otf |
| 261 | AddType application/x-font-woff .woff |
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 | <Directory %s/public/> |
| 264 | # directory specific options |
| 265 | Options -Indexes +FollowSymLinks +ExecCGI |
| 266 | |
| 267 | # directory's index file |
| 268 | DirectoryIndex web.py |
| 269 | |
| 270 | AllowOverride all |
| 271 | Order Allow,Deny |
| 272 | Allow from all |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 273 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 274 | # rewrite rule |
| 275 | RewriteEngine on |
| 276 | RewriteCond %%{REQUEST_FILENAME} !-f |
| 277 | RewriteCond %%{REQUEST_FILENAME} !-d |
| 278 | RewriteCond %%{REQUEST_FILENAME} !-l |
| 279 | RewriteRule ^([^/]+)$ /web.py?page=$1 [QSA,L] |
| 280 | </Directory> |
| 281 | </VirtualHost>""" % (install_path, install_path) |
| 282 | |
| 283 | new_apache_conf_path = os.path.join(install_path, os.path.basename(install_path)+".conf") |
| 284 | with open(new_apache_conf_path, "w") as apache_conf_file: |
| 285 | apache_conf_file.write(apache_conf_content) |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 286 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 287 | def post_install(install_path): |
| 288 | global apache_user |
| 289 | exec_in_shell("chown -R %s %s" % (apache_user, install_path)) |
| 290 | |
| 291 | apache_conf_filename = os.path.basename(install_path)+".conf" |
| 292 | if is_redhat: |
| 293 | os.symlink(os.path.join(install_path, apache_conf_filename), |
| 294 | os.path.join("/etc/httpd/conf.d", apache_conf_filename)) |
| 295 | exec_in_shell("service httpd restart") |
| 296 | |
| 297 | elif is_debian: |
| 298 | os.symlink(os.path.join(install_path, apache_conf_filename), |
| 299 | os.path.join("/etc/apache2/sites-enabled", apache_conf_filename)) |
| 300 | exec_in_shell("service apache2 restart") |
| 301 | |
| 302 | print |
| 303 | print "-"*80 |
| 304 | print "Installation complete" |
| 305 | print "Open your browser and go to http://localhost:8080" |
| 306 | print "Login using username = Administrator and password = admin" |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 307 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 308 | def exec_in_shell(cmd): |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 309 | # using Popen instead of os.system - as recommended by python docs |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 310 | from subprocess import Popen |
| 311 | import tempfile |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 312 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 313 | with tempfile.TemporaryFile() as stdout: |
| 314 | with tempfile.TemporaryFile() as stderr: |
| 315 | p = Popen(cmd, shell=True, stdout=stdout, stderr=stderr) |
| 316 | p.wait() |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 317 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 318 | stdout.seek(0) |
| 319 | out = stdout.read() |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 320 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 321 | stderr.seek(0) |
| 322 | err = stderr.read() |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 323 | |
Rushabh Mehta | 54c1545 | 2013-07-16 12:05:41 +0530 | [diff] [blame] | 324 | if err and any((kw in err.lower() for kw in ["traceback", "error", "exception"])): |
| 325 | print out |
| 326 | raise Exception, err |
| 327 | else: |
| 328 | print "." |
| 329 | |
| 330 | return out |
| 331 | |
| 332 | if __name__ == "__main__": |
Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 333 | install() |