| 1 | # This file is part of the "Cleverbox" program. |
|---|
| 2 | # |
|---|
| 3 | # Cleverbox is free software: you can redistribute it and/or modify |
|---|
| 4 | # it under the terms of the GNU General Public License as published by |
|---|
| 5 | # the Free Software Foundation, either version 3 of the License, or |
|---|
| 6 | # (at your option) any later version. |
|---|
| 7 | # |
|---|
| 8 | # Cleverbox is distributed in the hope that it will be useful, |
|---|
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | # GNU General Public License for more details. |
|---|
| 12 | # |
|---|
| 13 | # You should have received a copy of the GNU General Public License |
|---|
| 14 | # along with Cleverbox. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 15 | # |
|---|
| 16 | # Copyright 2008, Tristan Rivoallan |
|---|
| 17 | |
|---|
| 18 | import logging, os, shutil, stat |
|---|
| 19 | import ConfigParser |
|---|
| 20 | from pkg_resources import parse_version |
|---|
| 21 | from cleverbox.utils import filesystem |
|---|
| 22 | |
|---|
| 23 | layout = ('clients-available', 'clients-enabled', |
|---|
| 24 | 'projects-available', 'projects-enabled', |
|---|
| 25 | 'profiles/default') |
|---|
| 26 | |
|---|
| 27 | profile_files = ('project.apache.conf', 'trac-defaults.ini', 'permissions.ini') |
|---|
| 28 | |
|---|
| 29 | class Environment: |
|---|
| 30 | |
|---|
| 31 | path = '' |
|---|
| 32 | |
|---|
| 33 | def __init__(self, envdir): |
|---|
| 34 | self.path = envdir |
|---|
| 35 | self.config = ConfigParser.SafeConfigParser() |
|---|
| 36 | self.config.read(self.get_path('cleverbox.ini')) |
|---|
| 37 | |
|---|
| 38 | def create(self, version, parameters): |
|---|
| 39 | """ |
|---|
| 40 | Creates a cleverbox environment. |
|---|
| 41 | """ |
|---|
| 42 | |
|---|
| 43 | #Parameters sanity check |
|---|
| 44 | try: |
|---|
| 45 | parameters['general']['assets_dir'] |
|---|
| 46 | parameters['general']['apache_group'] |
|---|
| 47 | parameters['general']['clients_root'] |
|---|
| 48 | parameters['general']['ssh_group'] |
|---|
| 49 | parameters['trac']['tracadmin_path'] |
|---|
| 50 | |
|---|
| 51 | except KeyError, e: |
|---|
| 52 | raise Exception, "Supplied parameters are not complete." |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | # Do not do anything if base directories is not empty |
|---|
| 56 | if (len(os.listdir(self.path))): |
|---|
| 57 | raise IOError, '"%s" directory is not empty' % self.path |
|---|
| 58 | |
|---|
| 59 | # Create cleverbox.ini |
|---|
| 60 | for (section_name, section_directives) in parameters.items(): |
|---|
| 61 | self.config.add_section(section_name) |
|---|
| 62 | for (k, v) in section_directives.items(): |
|---|
| 63 | self.config.set(section_name, k, v) |
|---|
| 64 | |
|---|
| 65 | # Write all to disk |
|---|
| 66 | try: |
|---|
| 67 | # Create directory structure |
|---|
| 68 | print |
|---|
| 69 | logging.info(" Creating directory layout") |
|---|
| 70 | print |
|---|
| 71 | |
|---|
| 72 | env_dirs = [] |
|---|
| 73 | for dirname in layout: |
|---|
| 74 | env_dirs.append(self.get_path(dirname)) |
|---|
| 75 | map(os.makedirs, env_dirs) |
|---|
| 76 | |
|---|
| 77 | # Create VERSION file |
|---|
| 78 | try: |
|---|
| 79 | print |
|---|
| 80 | logging.info(" Creating VERSION file") |
|---|
| 81 | print |
|---|
| 82 | |
|---|
| 83 | fd = open(self.get_path('VERSION'), 'w') |
|---|
| 84 | fd.write(version) |
|---|
| 85 | finally: |
|---|
| 86 | fd.close() |
|---|
| 87 | |
|---|
| 88 | # Create data directory |
|---|
| 89 | os.makedirs(self.config.get('general', 'clients_root')) |
|---|
| 90 | |
|---|
| 91 | print |
|---|
| 92 | logging.info(' Created data directory in "%s"' % self.config.get('general', 'clients_root')) |
|---|
| 93 | print |
|---|
| 94 | |
|---|
| 95 | # Create default configuration profile |
|---|
| 96 | print |
|---|
| 97 | logging.info(" Creating default configuration profile") |
|---|
| 98 | print |
|---|
| 99 | |
|---|
| 100 | for filename in profile_files: |
|---|
| 101 | shutil.copy( |
|---|
| 102 | os.path.join(parameters['general']['assets_dir'], filename), |
|---|
| 103 | os.path.join(self.get_path('profiles'), 'default')) |
|---|
| 104 | |
|---|
| 105 | # Write cleverbox.ini to disk |
|---|
| 106 | self.config.write(open(self.get_path('cleverbox.ini'), 'w+')) |
|---|
| 107 | |
|---|
| 108 | # Set permissions |
|---|
| 109 | permissions = {self.get_path('VERSION') : (0640, 'root', self.config.get('general', 'apache_group')), |
|---|
| 110 | self.get_path('cleverbox.ini') : (0640, 'root', self.config.get('general', 'ssh_group')), |
|---|
| 111 | self.get_path('clients-available') : (0750, 'root', self.config.get('general', 'apache_group')), |
|---|
| 112 | self.get_path('clients-enabled') : (0750, 'root', self.config.get('general', 'apache_group')), |
|---|
| 113 | self.get_path('projects-available') : (0750, 'root', self.config.get('general', 'apache_group')), |
|---|
| 114 | self.get_path('projects-enabled') : (0750, 'root', self.config.get('general', 'apache_group')), |
|---|
| 115 | self.get_path('profiles') : (0750, 'root', self.config.get('general', 'ssh_group')), |
|---|
| 116 | self.get_path('profiles/default') : (0750, 'root', self.config.get('general', 'ssh_group')), |
|---|
| 117 | self.get_path('profiles/default/trac-defaults.ini') : (0640, 'root', self.config.get('general', 'ssh_group')), |
|---|
| 118 | self.get_path('profiles/default/project.apache.conf') : (0640, 'root', self.config.get('general', 'ssh_group')), |
|---|
| 119 | self.get_path('profiles/default/permissions.ini') : (0640, 'root', self.config.get('general', 'ssh_group')), |
|---|
| 120 | self.config.get('general', 'clients_root') : (0750, 'root', self.config.get('general', 'apache_group'))} |
|---|
| 121 | |
|---|
| 122 | filesystem.set_permissions(permissions) |
|---|
| 123 | |
|---|
| 124 | except Exception, exception: |
|---|
| 125 | logging.error(exception) |
|---|
| 126 | logging.error(" Environment couldn't be initialized in %(env_dir)s" % {'env_dir' : self.path}) |
|---|
| 127 | logging.info(" Rolling back changes") |
|---|
| 128 | |
|---|
| 129 | for entry in os.listdir(self.path): |
|---|
| 130 | path = os.path.join(self.path, entry) |
|---|
| 131 | if filesystem.is_directory(path): |
|---|
| 132 | shutil.rmtree(path) |
|---|
| 133 | else: |
|---|
| 134 | os.remove(path) |
|---|
| 135 | |
|---|
| 136 | # we remove clients_root directory |
|---|
| 137 | shutil.rmtree(self.config.get('general', 'clients_root')); |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | def needs_upgrade(self, target_version): |
|---|
| 141 | """ |
|---|
| 142 | Checks if environment needs to be upgraded. |
|---|
| 143 | """ |
|---|
| 144 | return parse_version(self.get_version()) < parse_version(target_version) |
|---|
| 145 | |
|---|
| 146 | def upgrade(self): |
|---|
| 147 | """ |
|---|
| 148 | Upgrades environment using available upgrade scripts. |
|---|
| 149 | """ |
|---|
| 150 | from cleverbox.upgrades import upgrades |
|---|
| 151 | i = 1 |
|---|
| 152 | while i: |
|---|
| 153 | try: |
|---|
| 154 | upgrade_func = getattr(upgrades, 'do_upgrade_' + str(i)) |
|---|
| 155 | upgrade_func.__call__(self.envname, self.get_version()) |
|---|
| 156 | except AttributeError, e: |
|---|
| 157 | # No more upgrade scripts |
|---|
| 158 | break |
|---|
| 159 | i = i + 1 |
|---|
| 160 | |
|---|
| 161 | def get_version(self): |
|---|
| 162 | """ |
|---|
| 163 | Returns environment current version. |
|---|
| 164 | """ |
|---|
| 165 | return open(os.path.join(self.path, 'VERSION')).read().strip() |
|---|
| 166 | |
|---|
| 167 | def get_path(self, filename = ''): |
|---|
| 168 | """ |
|---|
| 169 | Returns path to requested filename. |
|---|
| 170 | """ |
|---|
| 171 | return os.path.join(self.path, filename) |
|---|