Anand Doshi | 17fb35f | 2012-05-10 12:39:25 +0530 | [diff] [blame] | 1 | #!/usr/bin/python |
Anand Doshi | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 2 | from __future__ import unicode_literals |
Anand Doshi | 17fb35f | 2012-05-10 12:39:25 +0530 | [diff] [blame] | 3 | import os, commands |
| 4 | |
| 5 | # ask for root mysql password |
| 6 | import getpass |
| 7 | |
| 8 | root_pwd = None |
| 9 | while not root_pwd: |
| 10 | root_pwd = getpass.getpass("MySQL Root user's Password: ") |
| 11 | |
| 12 | # test root connection |
| 13 | op = commands.getoutput("mysql -u root -p%s -e 'exit'" % \ |
| 14 | root_pwd.replace('$', '\$').replace(' ', '\ ')) |
| 15 | if "access denied" in op.lower(): |
| 16 | raise Exception("Incorrect MySQL Root user's password") |
| 17 | |
| 18 | # ask for new dbname |
| 19 | new_dbname = None |
| 20 | while not new_dbname: |
| 21 | new_dbname = raw_input("New ERPNext Database Name: ") |
| 22 | |
| 23 | # ask for new dbpassword |
| 24 | new_dbpassword = None |
| 25 | while not new_dbpassword: |
| 26 | new_dbpassword = raw_input("New ERPNext Database's Password: ") |
| 27 | |
| 28 | # get erpnext path |
| 29 | erpnext_path = os.path.dirname(os.path.abspath(__file__)) |
| 30 | os.chdir(erpnext_path) |
| 31 | |
| 32 | # setup backups |
| 33 | if not os.path.exists(os.path.join(erpnext_path, 'backups')): |
| 34 | os.makedirs('backups') |
| 35 | os.symlink(os.path.join(erpnext_path, 'backups'), |
| 36 | os.path.join(erpnext_path, 'public', 'backups')) |
| 37 | |
| 38 | # setup files |
| 39 | if not os.path.exists(os.path.join(erpnext_path, 'files')): |
| 40 | os.makedirs('files') |
| 41 | os.symlink(os.path.join(erpnext_path, 'files'), |
| 42 | os.path.join(erpnext_path, 'public', 'files')) |
| 43 | |
| 44 | # setup logs |
| 45 | if not os.path.exists(os.path.join(erpnext_path, 'logs')): |
| 46 | os.makedirs('logs') |
| 47 | os.system('touch logs/error_log.txt') |
| 48 | |
| 49 | # setup lib -- framework repo with read only access |
| 50 | # change this if you have your own fork |
| 51 | if not os.path.exists(os.path.join(erpnext_path, 'lib')): |
Anand Doshi | c655f47 | 2012-07-12 19:08:48 +0530 | [diff] [blame] | 52 | os.system('git clone https://github.com/webnotes/wnframework.git lib') |
Anand Doshi | 17fb35f | 2012-05-10 12:39:25 +0530 | [diff] [blame] | 53 | |
| 54 | # setup symlinks in public |
| 55 | if not os.path.exists(os.path.join(erpnext_path, 'public', 'js', 'lib')): |
| 56 | os.symlink(os.path.join(erpnext_path, 'lib', 'js', 'lib'), |
| 57 | os.path.join(erpnext_path, 'public', 'js', 'lib')) |
| 58 | if not os.path.exists(os.path.join(erpnext_path, 'public', 'images', 'lib')): |
Anand Doshi | 8ec5ddf | 2012-05-10 12:56:10 +0530 | [diff] [blame] | 59 | os.symlink(os.path.join(erpnext_path, 'lib', 'images'), |
Anand Doshi | 17fb35f | 2012-05-10 12:39:25 +0530 | [diff] [blame] | 60 | os.path.join(erpnext_path, 'public', 'images', 'lib')) |
| 61 | |
| 62 | # extract master |
| 63 | if os.path.exists(os.path.join(erpnext_path, 'data', 'master.sql.gz')): |
| 64 | os.system('gunzip data/master.sql.gz') |
| 65 | |
| 66 | # setup conf |
| 67 | if not os.path.exists(os.path.join(erpnext_path, 'conf.py')): |
| 68 | # read template conf file |
| 69 | with open(os.path.join(erpnext_path, 'lib', 'conf', 'conf.py'), 'r') as template: |
| 70 | content = template.read() |
| 71 | |
| 72 | # manipulate content |
| 73 | import re |
| 74 | |
| 75 | # set new_dbname, new_dbpassword, modules_path, files_path, backup_path, log_file_name |
| 76 | content = re.sub("db_name.*", "db_name = '%s'" % new_dbname, content) |
| 77 | content = re.sub("db_password.*", "db_password = '%s'" % new_dbpassword, content) |
| 78 | content = re.sub("modules_path.*", "modules_path = '%s'" % \ |
| 79 | os.path.join(erpnext_path, 'erpnext'), content) |
| 80 | content = re.sub("files_path.*", "files_path = '%s'" % \ |
| 81 | os.path.join(erpnext_path, 'files'), content) |
| 82 | content = re.sub("backup_path.*", "backup_path = '%s'" % \ |
| 83 | os.path.join(erpnext_path, 'backups'), content) |
| 84 | content = re.sub("log_file_name.*", "log_file_name = '%s'" % \ |
| 85 | os.path.join(erpnext_path, 'logs', 'error_log.txt'), content) |
| 86 | |
| 87 | |
| 88 | # write conf file |
| 89 | with open(os.path.join(erpnext_path, 'conf.py'), 'w') as new_conf: |
| 90 | new_conf.write(content) |
| 91 | |
| 92 | # install db |
| 93 | import sys |
| 94 | sys.path.append(erpnext_path) |
Anand Doshi | 45b187d | 2012-05-10 13:10:06 +0530 | [diff] [blame] | 95 | sys.path.append(os.path.join(erpnext_path, 'lib', 'py')) |
Anand Doshi | 17fb35f | 2012-05-10 12:39:25 +0530 | [diff] [blame] | 96 | import conf |
| 97 | sys.path.append(conf.modules_path) |
| 98 | |
| 99 | from webnotes.install_lib.install import Installer |
| 100 | inst = Installer('root', root_pwd) |
| 101 | inst.import_from_db(new_dbname, source_path=os.path.join(erpnext_path, 'data', 'master.sql'), verbose = 1) |
| 102 | |
| 103 | # apply patches |
| 104 | os.chdir(erpnext_path) |
Anand Doshi | 9b88baa | 2012-07-12 18:52:54 +0530 | [diff] [blame] | 105 | os.system("lib/wnf.py --update origin master") |
Anand Doshi | 79af963 | 2012-05-14 16:36:10 +0530 | [diff] [blame] | 106 | |
Anand Doshi | 3fa25fa | 2012-05-10 15:15:43 +0530 | [diff] [blame] | 107 | # set filemode false |
| 108 | os.system("git config core.filemode false") |
| 109 | os.chdir(os.path.join(erpnext_path, 'lib')) |
| 110 | os.system("git config core.filemode false") |
| 111 | |
Anand Doshi | 17fb35f | 2012-05-10 12:39:25 +0530 | [diff] [blame] | 112 | steps_remaining = """ |
Anand Doshi | 54e79bb | 2012-07-12 19:50:53 +0530 | [diff] [blame] | 113 | Notes: |
| 114 | ------ |
| 115 | |
| 116 | sample apache conf file |
| 117 | #----------------------------------------------------------- |
| 118 | SetEnv PYTHON_EGG_CACHE /var/www |
| 119 | |
| 120 | # you can change 99 to any other port |
| 121 | |
| 122 | Listen 99 |
| 123 | NameVirtualHost *:99 |
| 124 | <VirtualHost *:99> |
| 125 | ServerName localhost |
| 126 | DocumentRoot {path to erpnext's folder}/public |
| 127 | AddHandler cgi-script .cgi .xml .py |
| 128 | |
| 129 | <Directory {path to erpnext's folder}/public/> |
| 130 | # directory specific options |
Anand Doshi | af1f437 | 2012-07-12 22:06:27 +0530 | [diff] [blame] | 131 | Options -Indexes +FollowSymLinks +ExecCGI |
Anand Doshi | 54e79bb | 2012-07-12 19:50:53 +0530 | [diff] [blame] | 132 | |
| 133 | # directory's index file |
| 134 | DirectoryIndex web.py |
| 135 | |
| 136 | # rewrite rule |
| 137 | RewriteEngine on |
| 138 | |
| 139 | # condition 1: |
| 140 | # ignore login-page.html, app.html, blank.html, unsupported.html |
| 141 | RewriteCond %{REQUEST_URI} ^((?!app\.html|blank\.html|unsupported\.html).)*$ |
| 142 | |
| 143 | # condition 2: if there are no slashes |
| 144 | # and file is .html or does not containt a . |
| 145 | RewriteCond %{REQUEST_URI} ^(?!.+/)((.+\.html)|([^.]+))$ |
| 146 | |
| 147 | # rewrite if both of the above conditions are true |
| 148 | RewriteRule ^(.+)$ web.py?page=$1 [NC,L] |
| 149 | |
| 150 | AllowOverride all |
| 151 | Order Allow,Deny |
| 152 | Allow from all |
| 153 | </Directory> |
| 154 | </VirtualHost> |
| 155 | #----------------------------------------------------------- |
| 156 | |
Anand Doshi | 17fb35f | 2012-05-10 12:39:25 +0530 | [diff] [blame] | 157 | To Do: |
| 158 | |
| 159 | * Configure apache/http conf file to point to public folder |
| 160 | * chown recursively all files in your folder to apache user |
Anand Doshi | a385175 | 2012-05-10 15:57:05 +0530 | [diff] [blame] | 161 | * login using: user="Administrator" and password="admin" |
Anand Doshi | 54e79bb | 2012-07-12 19:50:53 +0530 | [diff] [blame] | 162 | |
Anand Doshi | 17fb35f | 2012-05-10 12:39:25 +0530 | [diff] [blame] | 163 | """ |
| 164 | |
| 165 | print steps_remaining |
| 166 | |