def loadCachedConfig(self): config_obj = {} cached_config_path = FileSystem.join(self.tmpCacheDir, self.configFileName) if FileSystem.fileExists(cached_config_path): print('File exists load config.') config_obj = self.parseConfig(cached_config_path) config_obj['init'] = False elif self.gitInitUrl: print('No cached configuration, download from github') if self.clone(self.gitInitUrl): config_obj = self.parseConfig(FileSystem.join(self.tmpRepoDir, self.configFileName)) config_obj['init'] = True return config_obj
def __init__(self, type_of_event, branch, url, keypair, git_init_url=None): self.type = type_of_event self.branch = branch self.url = url self.tmpDir = '/tmp/ferrymang' self.tmpRepoDir = FileSystem.join(self.tmpDir, 'repository') self.tmpCacheDir = FileSystem.join(self.tmpDir, 'cache') self.gitInitUrl = git_init_url or '' self.Keypair = Keypair('git', keypair['pubkpath'], keypair['prvkpath'], keypair['pkpasswd']) FileSystem.createDirectory(self.tmpDir) self.config = self.loadCachedConfig() self.dispatch()
def runScripts(self, script_name): if self.config['applications']: for key in self.config['applications']: application_path = self.rootAndResolve(self.config['applications'][key]['path']) script = self.config['applications'][key][script_name] if script.get('path') is not None: script_path = FileSystem.join(self.config['applications'][key]['path'], script['path']) script_path = self.rootAndResolve(script_path) if FileSystem.fileExists(script_path): print('Running script', script_path) if subprocess.call('cd '+application_path+' && '+script_path+' '+script['parameters'], shell=True): print('Ran script successfully', script_path) else: print('Ran script but unsuccessfully', script_path) else: print('Failed to run a script; unavailable script', script_path) elif script.get('commands') is not None: for command in script['commands']: command = command.replace('{path}', application_path) print('Running command "'+command+'"') if subprocess.call('cd '+application_path+' && '+command, shell=True): print('Ran command successfully', command) else: print('Ran command but unsuccessfully', command)
def clean(self): self.runScripts('stop') self.deleteApplicationsRoots() FileSystem.delete(FileSystem.join(self.tmpCacheDir, self.configFileName)) return
def deploy(self): self.clean() if not self.config['init']: self.clone(self.url) # Read current version's config self.config = self.parseConfig(FileSystem.join(self.tmpRepoDir, self.configFileName)) # Copy current config to config cache if FileSystem.delete(self.tmpCacheDir) and FileSystem.createDirectory(self.tmpCacheDir): if FileSystem.copy(FileSystem.join(self.tmpRepoDir, self.configFileName), self.tmpCacheDir): print('Cached configuration file.') # Create root directory if configured if self.config['root']: if FileSystem.createDirectory(self.config['root']): print('Created root directory as configured.') if self.config['actions']: for actions in self.config['actions']: if actions['type'] == 'move': to_path = FileSystem.join(self.config['root'], actions['to']) to_path = FileSystem.resolve(to_path) if actions['to'][-1:] == '/' and not FileSystem.dirExists(to_path): FileSystem.createDirectory(to_path) from_path = FileSystem.join(self.tmpRepoDir, actions['from']) from_path = FileSystem.resolve(from_path) print('ACTION: MOVING.', from_path, to_path) FileSystem.move(from_path, to_path) print('OK: Moved.') elif actions['type'] == 'mkdir': path = FileSystem.join(self.config['root'], actions['path']) print('ACTION: MKDIR: ' + path) if not FileSystem.exists(path): if FileSystem.createDirectory(path): print('OK: Directory created.') else: print('ERROR: Could not create directory.') else: print('ERROR: Already exists. Nothing to do.') elif actions['type'] == 'delete': path = FileSystem.join(self.config['root'], actions['path']) if not FileSystem.exists(path): print('Path marked for deletion does not exits. Nothing to do.') else: print('ACTION: DELETING ' + path) if FileSystem.delete(path): print('OK: File or directory deleted') else: print('ERROR: Could not delete path: '+path) self.runScripts('start') # Delete TMP folder if FileSystem.delete(self.tmpRepoDir): print('Successfully deleted temporary repository directory.') return True
def rootAndResolve(self, path): path = FileSystem.join(self.config['root'], path) return FileSystem.resolve(path)