Show
Ignore:
Timestamp:
09/12/07 21:51:47 (5 years ago)
Author:
trivoallan
Message:

cleverbox :

  • implemented project unit tests
  • stricter permissions for created projects files
  • fixed trac.ini string substitution
Location:
cleverbox/trunk/cleverbox
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • cleverbox/trunk/cleverbox/environment.py

    r200 r201  
    140140        return open(os.path.join(self.path, 'VERSION')).read().strip() 
    141141 
    142     def get_path(self, filename): 
     142    def get_path(self, filename = ''): 
    143143        """ 
    144144        Returns path to requested filename. 
  • cleverbox/trunk/cleverbox/project.py

    r200 r201  
    7777                            '%s-%s' % (client_name, project_name)) 
    7878    os.symlink(target, linkname) 
    79      
     79 
    8080    print styles.style.SUCCESSNORESET('') 
    8181    logging.info("  Project '%s' has been enabled. Apache needs to be reloaded." % project_name) 
     
    156156        _fix_perms(environment, collected_infos, parameters) 
    157157 
     158        try: 
     159            enable_project = parameters['enable'] 
     160        except KeyError: 
     161            # -- Enable client ? 
     162            print "Project has been created. Do you want to enable it ?" 
     163            print 
     164            enable_project = raw_input('Enable project ? [y/N]> ').strip() or 'n' 
     165 
     166        if str(enable_project).find('y') == 0: 
     167            enable(environment, client_name, project_name) 
     168 
    158169    except Exception, e: 
    159         print "An error occured :" 
     170        print "An error occured : " 
    160171        print e 
    161172        traceback.print_exc() 
    162  
    163     try: 
    164         enable_project = parameters['enable'] 
    165     except KeyError: 
    166         # -- Enable client ? 
    167         print "Project has been created. Do you want to enable it ?" 
    168         print 
    169         enable_project = raw_input('Enable project ? [y/N]> ').strip() or 'n' 
    170  
    171     if str(enable_project).find('y') == 0: 
    172         enable(environment, client_name, project_name) 
     173        print "TODO : rollback changes" 
     174 
    173175 
    174176def _create_dirs(environment, infos, parameters): 
     
    300302 
    301303def _fix_perms(environment, infos, parameters): 
    302     """ 
    303     chown -R dev:www-data /$CLIENTSROOT/$CLIENTNAME/var/svn/$PROJECTNAME 
    304     chmod g+w /$CLIENTSROOT/$CLIENTNAME/var/svn/$PROJECTNAME 
    305     chown -R www-data:dev /$CLIENTSROOT/$CLIENTNAME/var/trac/$PROJECTNAME/db 
    306     chown dev:www-data /$CLIENTSROOT/$CLIENTNAME/var/trac/conf/trac.ini 
    307     chmod g+w /$CLIENTSROOT/$CLIENTNAME/var/trac/conf/trac.ini 
    308     """ 
    309  
    310     # Trac DB 
    311     trac_db_path   = os.path.join(environment.config.get('general', 'clients_root'), 
    312                                   infos['client'], 
    313                                   'var/trac', 
    314                                   infos['short_name'], 
    315                                   'db') 
    316     filesystem.chowntree(trac_db_path,  
    317                          filesystem.get_uid_from_name(environment.config.get('general', 'apache_user')), 
    318                          filesystem.get_gid_from_name(environment.config.get('general', 'ssh_group'))) 
    319  
    320     # Trac attachments 
    321     trac_attachments_path = os.path.join( environment.config.get('general', 'clients_root'), 
    322                                          infos['client'], 
    323                                          'var/trac', 
    324                                          infos['short_name'], 
    325                                          'attachments' ) 
    326  
    327     filesystem.chowntree(trac_attachments_path , 
    328                          filesystem.get_uid_from_name(environment.config.get('general', 'apache_user')), 
    329                          filesystem.get_gid_from_name(environment.config.get('general', 'ssh_group'))) 
    330  
    331     # Trac conf 
    332     trac_ini_path = os.path.join(environment.config.get('general', 'clients_root'), 
    333                                  infos['client'], 
    334                                  'var','trac', 
    335                                  infos['short_name'], 
    336                                  'conf','trac.ini' ) 
    337     os.chmod( trac_ini_path, 0777 )     
    338  
    339     # Subversion repository 
    340     svn_repos_path = os.path.join( environment.config.get('general', 'clients_root'), 
    341                                   infos['client'], 
    342                                   'var/svn', 
    343                                   infos['short_name'] ) 
    344  
    345     filesystem.chowntree(svn_repos_path,  
    346                          filesystem.get_uid_from_name(environment.config.get('general', 'apache_user')),  
    347                          filesystem.get_gid_from_name(environment.config.get('general', 'ssh_group'))) 
    348      
    349     os.chmod( svn_repos_path, 0775 ) 
     304 
     305    clients_root = environment.config.get('general', 'clients_root') 
     306    client_name = infos['client'] 
     307    project_name = infos['short_name'] 
     308     
     309    permissions = { 
     310        os.path.join(environment.get_path(), 'projects-available', client_name + '-' + project_name)   : (0640, 'root', 'root'), 
     311        os.path.join(clients_root, client_name, 'htdocs', project_name)                                : (0770, 'root', 'www-data'), 
     312        os.path.join(clients_root, client_name, 'var', 'svn', project_name)                            : (0750, 'root', 'www-data'), 
     313        os.path.join(clients_root, client_name, 'var', 'svn', project_name, 'db')                      : (0770, 'root', 'www-data'), 
     314        os.path.join(clients_root, client_name, 'var', 'trac', project_name)                           : (0750, 'root', 'www-data'), 
     315        os.path.join(clients_root, client_name, 'var', 'trac', project_name, 'db')                     : (0770, 'root', 'www-data'), 
     316        os.path.join(clients_root, client_name, 'var', 'trac', project_name, 'db', 'trac.db')          : (0640, 'root', 'www-data'), 
     317        os.path.join(clients_root, client_name, 'var', 'trac', project_name, 'attachments')            : (0770, 'root', 'www-data'), 
     318        os.path.join(clients_root, client_name, 'var', 'trac', project_name, 'conf', 'trac.ini')       : (0640, 'root', 'www-data'), 
     319    } 
     320 
     321    filesystem.set_permissions(permissions) 
    350322 
    351323    print "  Perms fixed\n" 
     
    372344            # Perform variable substituion in each default option 
    373345            # It is probably not very good in term of performances, but we'll wait until someone complains to fix it 
    374             default_option = tracdefaults_config.get(section, option) % {'client_name'      : infos['client'], 
    375                                                                          'project_name'     : infos['short_name'], 
    376                                                                          'clients_root'     : environment.config.get('general', 'clients_root'), 
    377                                                                          'authbackend_pass' : environment.config.get('general', 'authbackend_pass'), 
    378                                                                          'trac_install_dir' : environment.config.get('trac', 'lib_dir'), 
    379                                                                          'domain_name'      : environment.config.get('general', 'domain')} 
     346            default_option = tracdefaults_config.get(section, option) 
    380347            tracproject_config.set(section, option, default_option) 
    381  
     348             
     349    # Add cleverbox section 
     350    # The variables in this section can be used for string substitution across the file 
     351    cleverbox_options = {'client_name'      : infos['client'], 
     352                         'project_name'     : infos['short_name'], 
     353                         'clients_root'     : environment.config.get('general', 'clients_root'), 
     354                         'authbackend_pass' : environment.config.get('general', 'authbackend_pass'), 
     355                         'trac_install_dir' : environment.config.get('trac', 'lib_dir'), 
     356                         'domain_name'      : environment.config.get('general', 'domain')}  
     357    tracproject_config.add_section('cleverbox') 
     358    for (option, value) in cleverbox_options.items(): 
     359        tracproject_config.set('cleverbox', option, value) 
     360 
     361    # Write file to disk 
    382362    fp = open(project_config_path, 'w+') 
    383363    tracproject_config.write(fp) 
  • cleverbox/trunk/cleverbox/tests/client.py

    r200 r201  
    2323 
    2424    # client add 
    25     def test_client_add_generate_expected_layout(self): 
     25    def test_client_add_generates_expected_layout(self): 
    2626        client_name = 'testclient' 
    2727        client.add(self.env, client_name, {'enable' : 'y'})  
  • cleverbox/trunk/cleverbox/tests/environment.py

    r195 r201  
    1313 
    1414    def tearDown(self): 
     15        pass 
    1516        shutil.rmtree(self.env.path) 
    1617        shutil.rmtree(self.env.config.get('general', 'clients_root')) 
  • cleverbox/trunk/cleverbox/tests/project.py

    r200 r201  
    99logging.basicConfig(stream=open('/dev/null', 'w+')) 
    1010 
     11default_parameters = project_parameters = {'full_name'   : 'Test Project', 
     12                                           'profile'     : 'default', 
     13                                           'admin_login' : 'admin', 
     14                                           'enable'      : 'y'} 
    1115class ProjectTestCase(unittest.TestCase): 
    1216     
     
    2529         
    2630        project_name = 'testproject' 
    27         project_parameters = {'full_name'   : 'Test Project', 
    28                               'profile'     : 'default', 
    29                               'admin_login' : 'admin', 
    30                               'enable'      : 'y'} 
    31         project.add(self.env, client_name, project_name, project_parameters) 
     31        project.add(self.env, client_name, project_name, default_parameters) 
    3232        assert project.exists(self.env, client_name, project_name), 'project.exists() returns true if project already exists' 
    3333         
     34    def test_add_creates_expected_layout(self): 
     35 
     36        # Fixtures 
     37        client_name = 'testclient' 
     38        client.add(self.env, client_name, {'enable' : 'y'}) 
     39        project_name = 'testproject'  
     40        project.add(self.env, client_name, project_name, default_parameters) 
     41 
     42        # Specify expected layout 
     43        clients_root = self.env.config.get('general', 'clients_root') 
     44        expected = { 
     45            os.path.join(self.env.path, 'projects-available', client_name + '-' + project_name)         : ('S_ISREG', '0640', 'root', 'root'), 
     46            # Control constant should be S_ISLNK, but it does not seem to recognize the generated symlink as such 
     47            os.path.join(self.env.path, 'projects-enabled', client_name + '-' + project_name)           : ('S_ISREG', '0640', 'root', 'root'), 
     48            os.path.join(clients_root, client_name, 'htdocs', project_name)                             : ('S_ISDIR', '0770', 'root', 'www-data'), 
     49            os.path.join(clients_root, client_name, 'var', 'svn', project_name)                         : ('S_ISDIR', '0750', 'root', 'www-data'), 
     50            os.path.join(clients_root, client_name, 'var', 'svn', project_name, 'db')                   : ('S_ISDIR', '0770', 'root', 'www-data'), 
     51            os.path.join(clients_root, client_name, 'var', 'trac', project_name)                        : ('S_ISDIR', '0750', 'root', 'www-data'), 
     52            os.path.join(clients_root, client_name, 'var', 'trac', project_name, 'db')                  : ('S_ISDIR', '0770', 'root', 'www-data'), 
     53            os.path.join(clients_root, client_name, 'var', 'trac', project_name, 'db', 'trac.db')       : ('S_ISREG', '0640', 'root', 'www-data'), 
     54            os.path.join(clients_root, client_name, 'var', 'trac', project_name, 'attachments')         : ('S_ISDIR', '0770', 'root', 'www-data'), 
     55            os.path.join(clients_root, client_name, 'var', 'trac', project_name, 'conf', 'trac.ini')    : ('S_ISREG', '0640', 'root', 'www-data'), 
     56        } 
     57         
     58        cleverbox.tests.validate_layout(expected) 
     59     
     60    def test_enable_creates_symlink(self): 
     61         
     62        # Fixtures 
     63        client_name = 'testclient' 
     64        client.add(self.env, client_name, {'enable' : 'n'}) 
     65        project_name = 'testproject'  
     66        default_parameters['enable'] = False 
     67        project.add(self.env, client_name, project_name, default_parameters) 
     68         
     69        # Enable project 
     70        project.enable(self.env, client_name, project_name) 
     71         
     72        # Validate 
     73        open(self.env.get_path(os.path.join('projects-enabled', client_name + '-' + project_name))) 
     74 
     75    def test_disable_deletes_symlink(self): 
     76 
     77        # Fixtures 
     78        client_name = 'testclient' 
     79        client.add(self.env, client_name, {'enable' : 'n'}) 
     80        project_name = 'testproject'  
     81        project.add(self.env, client_name, project_name, default_parameters) 
     82         
     83        # Enable project 
     84        project.disable(self.env, client_name, project_name) 
     85         
     86        # Validate 
     87        try: 
     88            open(self.env.get_path(os.path.join('projects-enabled', client_name + '-' + project_name))) 
     89            self.fail('project.disable() deletes projects-enabled symlink') 
     90        except: 
     91            pass 
    3492         
    3593