def initialize(self, name, user): """ Utility to create a project on the filesystem. #. Create a unix user for the project. #. Create home directory for code. #. Create a bare git repository for code. """ from cannula.utils import shell project = self.get(name) user = api.users.get(user) if not user.has_perm('add', project.group): raise PermissionError("You can not create projects in this group") # TODO: Create unix user for project log.info("Creating project directories for: %s", project) if not os.path.isdir(project.repo_dir): os.makedirs(project.repo_dir) if not os.path.isdir(project.project_dir): os.makedirs(project.project_dir) # Create the git repo args = {'git_cmd': CANNULA_GIT_CMD, 'repo': project.repo_dir} shell(self.git_init_cmd % args) ctx = { 'project': project, 'cannula_admin': CANNULA_CMD, # The current setting file 'settings': os.environ['DJANGO_SETTINGS_MODULE'], # The current python executable (could be in a virtualenv) 'python': sys.executable } # Update git config in new repo write_file(project.git_config, 'git/git-config.txt', ctx) # Write out post-recieve hook and make it executable write_file(project.post_receive, 'git/post-receive.sh', ctx) shell('chmod 755 %s' % project.post_receive) # Write out a description file write_file(project.git_description, 'git/description.txt', ctx) log.info("Project %s initialized", project) api.log.create("Project %s initialized" % project, user=user, group=project.group, project=project)
def initialize(self, name, user): """ Utility to create a project on the filesystem. #. Create a unix user for the project. #. Create home directory for code. #. Create a bare git repository for code. """ from cannula.utils import shell project = self.get(name) user = api.users.get(user) if not user.has_perm('add', project.group): raise PermissionError("You can not create projects in this group") # TODO: Create unix user for project log.info("Creating project directories for: %s", project) if not os.path.isdir(project.repo_dir): os.makedirs(project.repo_dir) if not os.path.isdir(project.project_dir): os.makedirs(project.project_dir) # Create the git repo args = { 'git_cmd': CANNULA_GIT_CMD, 'repo': project.repo_dir } shell(self.git_init_cmd % args) ctx = { 'project': project, 'cannula_admin': CANNULA_CMD, # The current setting file 'settings': os.environ['DJANGO_SETTINGS_MODULE'], # The current python executable (could be in a virtualenv) 'python': sys.executable} # Update git config in new repo write_file(project.git_config, 'git/git-config.txt', ctx) # Write out post-recieve hook and make it executable write_file(project.post_receive, 'git/post-receive.sh', ctx) shell('chmod 755 %s' % project.post_receive) # Write out a description file write_file(project.git_description, 'git/description.txt', ctx) log.info("Project %s initialized", project) api.log.create("Project %s initialized" % project, user=user, group=project.group, project=project)
def test_deploy(self): from cannula.utils import shell, call_subprocess # Fake a remote push g1 = self.api.groups.create('testy', 'abby') p1 = self.api.projects.create(name='test', user='******', group=g1) self.api.projects.initialize(p1, user='******') self.assertTrue(os.path.isfile(p1.post_receive)) # Persist the data in the test db so that external commands (git) can # see the data as well. transaction.commit() cmd = "%s push %s master" % (conf.CANNULA_GIT_CMD, p1.repo_dir) _, cannula_cmd = shell('which cannulactl') self.assertTrue(os.path.isfile('/tmp/cannula_test.db')) env = { 'C_USER': '******', 'DJANGO_SETTINGS_MODULE': 'cannula.test_settings', 'CANNULA_BASE': self.base_dir, 'CANNULA_CMD': cannula_cmd.strip(), 'REPO': 'testy/test.git', } # call_subprocess(cmd, cwd=self.dummy_project, env=env) yaml_file = os.path.join(p1.project_dir, 'app.yaml') self.assertTrue(os.path.isfile(yaml_file))
def restart(self): """Restart the proxy service.""" if self.supervisor_managed: return api.proc.restart('proxy-server') # Else restart the proxy manually code, output = shell(self.restart_cmd % self.context) if code != 0: raise Exception(output)
def stop(self): """Stop the proxy service.""" if self.api.supervisor_managed: return api.proc.stop('proxy-server') # Else stop the proxy manually code, output = shell(self.stop_cmd % self.context) if code != 0: raise Exception(output)
def initialize(interactive, verbosity): levels = {0: logging.ERROR, 1: logging.INFO, 2: logging.DEBUG} logger = logging.getLogger('cannula') logger.setLevel(levels.get(int(verbosity))) logger.debug("Running Cannula Initialize") from cannula.conf import conf_dict, write_config from cannula.utils import shell config = conf_dict() # save a copy to see if user altered it. original = config.copy() create_dirs = ['cannula_base'] for d in create_dirs: config = create_directory(config, d, interactive, logger) # Cannula settings module config = set_option( config, 'cannula_settings', interactive, logger, message= "\nSet %s to (%s) \nor if you are brave you can change this here: ") # try to find cannulactl command and prompt user for new setting status, cmd = shell('which cannulactl') if status == 0: config['cannula_cmd'] = cmd.strip() config = set_option(config, 'cannula_cmd', interactive, logger) # Cannula ssh command, run by authorized keys base = config.get('cannula_base') ssh_cmd = os.path.join(base, 'bin', 'cannula.sh') config['cannula_ssh_cmd'] = ssh_cmd config = write_file(config, 'cannula_ssh_cmd', 'cannula/canner.sh', interactive, logger, perm='755') # Make sure we have a semi random secret key if not config.get('django_secret_key'): config['django_secret_key'] = ''.join([ choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for _ in range(50) ]) config = set_option(config, 'django_secret_key', interactive, logger) # Configure the database config = setup_database(config, interactive, logger) # Save the configuration if config != original: write_config(config)
def initialize(interactive, verbosity): levels = {0: logging.ERROR, 1: logging.INFO, 2: logging.DEBUG} logger = logging.getLogger('cannula') logger.setLevel(levels.get(int(verbosity))) logger.debug("Running Cannula Initialize") from cannula.conf import conf_dict, write_config from cannula.utils import shell config = conf_dict() # save a copy to see if user altered it. original = config.copy() create_dirs = ['cannula_base'] for d in create_dirs: config = create_directory(config, d, interactive, logger) # Cannula settings module config = set_option(config, 'cannula_settings', interactive, logger, message="\nSet %s to (%s) \nor if you are brave you can change this here: ") # try to find cannulactl command and prompt user for new setting status, cmd = shell('which cannulactl') if status == 0: config['cannula_cmd'] = cmd.strip() config = set_option(config, 'cannula_cmd', interactive, logger) # Cannula ssh command, run by authorized keys base = config.get('cannula_base') ssh_cmd = os.path.join(base, 'bin', 'cannula.sh') config['cannula_ssh_cmd'] = ssh_cmd config = write_file(config, 'cannula_ssh_cmd', 'cannula/canner.sh', interactive, logger, perm='755') # Make sure we have a semi random secret key if not config.get('django_secret_key'): config['django_secret_key'] = ''.join([choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for _ in range(50)]) config = set_option(config, 'django_secret_key', interactive, logger) # Configure the database config = setup_database(config, interactive, logger) # Save the configuration if config != original: write_config(config)
def startup(self): status, output = shell('%(cmd)s -c %(main_conf)s' % self.context) if status > 0: raise Exception(output)
def shutdown(self): status, output = shell('%(ctl_cmd)s -c %(main_conf)s shutdown' % self.context) if status > 0: logging.error(output)
def _exec(self, cmd, **kwargs): options = {'git': CANNULA_GIT_CMD} options.update(kwargs) return shell(cmd % options, cwd=self.directory)