Rushabh Mehta | a8f9aa0 | 2013-06-21 17:55:43 +0530 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | from __future__ import unicode_literals |
| 3 | import os, commands, sys |
| 4 | |
| 5 | def install(): |
| 6 | # get required details |
| 7 | root_pwd = get_root_password() |
| 8 | db_name, db_pwd = get_new_db_details() |
| 9 | |
| 10 | # install path |
| 11 | install_path = os.getcwd() |
| 12 | |
| 13 | setup_folders(install_path) |
| 14 | |
| 15 | setup_conf(install_path, db_name, db_pwd) |
| 16 | |
| 17 | # setup paths |
| 18 | sys.path.append('.') |
| 19 | sys.path.append('lib') |
| 20 | sys.path.append('app') |
| 21 | |
| 22 | setup_db(install_path, root_pwd, db_name) |
| 23 | |
| 24 | apply_patches(install_path) |
| 25 | |
| 26 | show_remaining_steps() |
| 27 | |
| 28 | def setup_folders(path): |
| 29 | execute_in_shell("git clone git://github.com/webnotes/wnframework.git lib", verbose=1) |
| 30 | execute_in_shell("git clone git://github.com/webnotes/erpnext.git app", verbose=1) |
| 31 | public = os.path.join(path, "public") |
| 32 | os.mkdir(public) |
| 33 | os.mkdir(os.path.join(public, "files")) |
| 34 | os.mkdir(os.path.join(public, "backups")) |
| 35 | os.mkdir(os.path.join(path, "logs")) |
| 36 | |
| 37 | def setup_conf(path, db_name, db_pwd): |
| 38 | # read template conf file |
| 39 | with open(os.path.join(path, 'lib', 'conf', 'conf.py'), 'r') as template: |
| 40 | content = template.read() |
| 41 | |
| 42 | # manipulate content |
| 43 | import re |
| 44 | |
| 45 | # set new_dbname, new_dbpassword, files_path, backup_path, log_file_name |
| 46 | content = re.sub("db_name.*", "db_name = '%s'" % db_name, content) |
| 47 | content = re.sub("db_password.*", "db_password = '%s'" % db_pwd, content) |
| 48 | |
| 49 | # write conf file |
| 50 | with open(os.path.join(path, 'conf.py'), 'w') as new_conf: |
| 51 | new_conf.write(content) |
| 52 | |
| 53 | def setup_db(path, root_pwd, db_name): |
| 54 | source = os.path.join(path, 'app', "master.sql") |
| 55 | execute_in_shell("gunzip -c %s.gz > %s" % (source, source), verbose=1) |
| 56 | |
| 57 | from webnotes.install_lib.install import Installer |
| 58 | inst = Installer('root', root_pwd) |
| 59 | inst.import_from_db(db_name, source_path=source, verbose = 1) |
| 60 | execute_in_shell("rm %s" % source) |
| 61 | |
| 62 | def apply_patches(path): |
| 63 | # need to build before patches, once, so that all-web.js and all-web.css exists |
| 64 | execute_in_shell("./lib/wnf.py -b", verbose=1) |
| 65 | execute_in_shell("./lib/wnf.py --patch_sync_build", verbose=1) |
| 66 | |
| 67 | # set filemode false |
| 68 | execute_in_shell("cd app && git config core.filemode false", verbose=1) |
| 69 | execute_in_shell("cd lib && git config core.filemode false", verbose=1) |
| 70 | |
| 71 | def get_root_password(): |
| 72 | # ask for root mysql password |
| 73 | import getpass |
| 74 | |
| 75 | root_pwd = None |
| 76 | while not root_pwd: |
| 77 | root_pwd = getpass.getpass("MySQL Root user's Password: ") |
| 78 | |
| 79 | test_root_connection(root_pwd) |
| 80 | |
| 81 | return root_pwd |
| 82 | |
| 83 | def test_root_connection(root_pwd): |
| 84 | err, out = execute_in_shell("mysql -u root -p%s -e 'exit'" % \ |
| 85 | root_pwd.replace('$', '\$').replace(' ', '\ ')) |
| 86 | if "access denied" in out.lower(): |
| 87 | raise Exception("Incorrect MySQL Root user's password") |
| 88 | |
| 89 | def get_new_db_details(): |
| 90 | return get_input("New ERPNext Database Name: "), \ |
| 91 | get_input("New ERPNext Database's Password: ") |
| 92 | |
| 93 | def get_input(msg): |
| 94 | val = None |
| 95 | while not val: |
| 96 | val = raw_input(msg) |
| 97 | return val |
| 98 | |
| 99 | def show_remaining_steps(): |
| 100 | steps_remaining = """ |
| 101 | Notes: |
| 102 | ------ |
| 103 | |
| 104 | sample apache conf file |
| 105 | #----------------------------------------------------------- |
| 106 | SetEnv PYTHON_EGG_CACHE /var/www |
| 107 | |
| 108 | # you can change 99 to any other port |
| 109 | |
| 110 | Listen 99 |
| 111 | NameVirtualHost *:99 |
| 112 | <VirtualHost *:99> |
| 113 | ServerName localhost |
| 114 | DocumentRoot {path to erpnext's folder}/public |
| 115 | AddHandler cgi-script .cgi .xml .py |
| 116 | |
| 117 | <Directory {path to erpnext's folder}/public/> |
| 118 | # directory specific options |
| 119 | Options -Indexes +FollowSymLinks +ExecCGI |
| 120 | |
| 121 | # directory's index file |
| 122 | DirectoryIndex web.py |
| 123 | |
| 124 | # rewrite rule |
| 125 | RewriteEngine on |
| 126 | |
| 127 | # condition 1: |
| 128 | # ignore login-page.html, app.html, blank.html, unsupported.html |
| 129 | RewriteCond %{REQUEST_URI} ^((?!app\.html|blank\.html|unsupported\.html).)*$ |
| 130 | |
| 131 | # condition 2: if there are no slashes |
| 132 | # and file is .html or does not containt a . |
| 133 | RewriteCond %{REQUEST_URI} ^(?!.+/)((.+\.html)|([^.]+))$ |
| 134 | |
| 135 | # rewrite if both of the above conditions are true |
| 136 | RewriteRule ^(.+)$ web.py?page=$1 [NC,L] |
| 137 | |
| 138 | AllowOverride all |
| 139 | Order Allow,Deny |
| 140 | Allow from all |
| 141 | </Directory> |
| 142 | </VirtualHost> |
| 143 | #----------------------------------------------------------- |
| 144 | |
| 145 | To Do: |
| 146 | |
| 147 | * Configure apache/http conf file to point to public folder |
| 148 | * chown recursively all files in your folder to apache user |
| 149 | * login using: user="Administrator" and password="admin" |
| 150 | |
| 151 | """ |
| 152 | |
| 153 | print steps_remaining |
| 154 | |
| 155 | def execute_in_shell(cmd, verbose=0): |
| 156 | # using Popen instead of os.system - as recommended by python docs |
| 157 | from subprocess import Popen, PIPE |
| 158 | p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) |
| 159 | |
| 160 | # get err and output |
| 161 | err, out = p.stderr.read(), p.stdout.read() |
| 162 | |
| 163 | if verbose: |
| 164 | if err: print err |
| 165 | if out: print out |
| 166 | |
| 167 | return err, out |
| 168 | |
| 169 | if __name__=="__main__": |
| 170 | install() |