def _stop(self, kwargs, class_file, pClass): self.logger.debug("_stop(kwargs=%s, class_file=%s, pClass=%s)" % (kwargs, class_file, pClass)) if not exists(class_file): self.logger.error("class_file '%s' does not exist" % class_file) return HttpErrorResponse( AgentException(AgentException.E_CONFIG_NOT_EXIST).message) try: try: fd = open(class_file, 'r') p = pickle.load(fd) self.logger.debug('dump file %s loaded' % class_file) fd.close() except Exception as e: ex = AgentException(AgentException.E_CONFIG_READ_FAILED, detail=e) self.logger.exception(ex.message) return HttpErrorResponse(ex.message) if 'drain' in kwargs: p.stop(kwargs['drain']) else: p.stop() self.logger.debug("Removing class_file '%s'" % class_file) remove(class_file) return HttpJsonResponse() except Exception as e: ex = AgentException(AgentException.E_UNKNOWN, detail=e) self.logger.exception(e) return HttpErrorResponse(ex.message)
def _httpproxy_get_params(self, kwargs): ret = {} if 'port' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'port') if not isinstance(kwargs['port'], int): raise AgentException(AgentException.E_ARGS_INVALID, detail='Invalid "port" value') ret['port'] = int(kwargs.pop('port')) if 'code_version' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'code_version') ret['code_version'] = kwargs.pop('code_version') if 'web_list' in kwargs: web_list = kwargs.pop('web_list') else: web_list = [] if len(web_list) == 0: raise AgentException(AgentException.E_ARGS_INVALID, detail='At least one web_list is required') ret['web_list'] = web_list if 'fpm_list' in kwargs: ret['fpm_list'] = kwargs.pop('fpm_list') if 'tomcat_list' in kwargs: ret['tomcat_list'] = kwargs.pop('tomcat_list') if 'tomcat_servlets' in kwargs: ret['tomcat_servlets'] = kwargs.pop('tomcat_servlets') ret['cdn'] = kwargs.get('cdn', None) return ret
def _create(self, post_params, class_file, pClass): if exists(class_file): return HttpErrorResponse( AgentException(AgentException.E_CONFIG_EXISTS).message) try: if type(post_params) != dict: raise TypeError() self.logger.debug('Creating class') p = pClass(**post_params) self.logger.debug('Created class') except (ValueError, TypeError) as e: ex = AgentException(AgentException.E_ARGS_INVALID, detail=str(e)) self.logger.exception(e) return HttpErrorResponse(ex.message) except Exception as e: ex = AgentException(AgentException.E_UNKNOWN, detail=e) self.logger.exception(e) return HttpErrorResponse(ex.message) else: try: self.logger.debug('Openning file %s' % class_file) fd = open(class_file, 'w') pickle.dump(p, fd) fd.close() except Exception as e: ex = AgentException(AgentException.E_CONFIG_COMMIT_FAILED, detail=e) self.logger.exception(ex.message) return HttpErrorResponse(ex.message) else: self.logger.debug('Created class file') return HttpJsonResponse()
def _update(self, post_params, class_file, pClass): try: if type(post_params) != dict: raise TypeError() fd = open(class_file, 'r') p = pickle.load(fd) fd.close() p.configure(**post_params) p.restart() except (ValueError, TypeError) as e: self.logger.exception(e) ex = AgentException(AgentException.E_ARGS_INVALID) return HttpErrorResponse(ex.message) except Exception as e: self.logger.exception(e) ex = AgentException(AgentException.E_UNKNOWN, detail=e) return HttpErrorResponse(ex.message) else: try: fd = open(class_file, 'w') pickle.dump(p, fd) fd.close() except Exception as e: self.logger.exception(ex.message) ex = AgentException(AgentException.E_CONFIG_COMMIT_FAILED, detail=e) return HttpErrorResponse(ex.message) else: return HttpJsonResponse()
def _MRC_get_params(self, kwargs): ret = {} if 'dir_serviceHost' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'dir service host') ret['dir_serviceHost'] = kwargs.pop('dir_serviceHost') if 'uuid' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'uuid') ret['uuid'] = kwargs.pop('uuid') return ret
def _slave_get_params(self, kwargs): ret = {} if 'slaves' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'slaves') ret = kwargs.pop('slaves') if len(kwargs) != 0: raise AgentException(AgentException.E_ARGS_UNEXPECTED, kwargs.keys()) return ret
def _master_get_params(self, kwargs): ret = {} if 'master_server_id' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'master_server_id') ret['master_server_id'] = kwargs.pop('master_server_id') if len(kwargs) != 0: raise AgentException(AgentException.E_ARGS_UNEXPECTED, kwargs.keys()) ret['config'] = self.config_parser return ret
def updatePHPCode(self, kwargs): if 'filetype' not in kwargs: return HttpErrorResponse( AgentException(AgentException.E_ARGS_MISSING, 'filetype').message) filetype = kwargs.pop('filetype') if 'codeVersionId' not in kwargs: return HttpErrorResponse( AgentException(AgentException.E_ARGS_MISSING, 'codeVersionId').message) codeVersionId = kwargs.pop('codeVersionId') if 'file' not in kwargs: return HttpErrorResponse( AgentException(AgentException.E_ARGS_MISSING, 'file').message) file = kwargs.pop('file') if filetype != 'git' and not isinstance(file, FileUploadField): return HttpErrorResponse( AgentException(AgentException.E_ARGS_INVALID, detail='"file" should be a file').message) if len(kwargs) != 0: return HttpErrorResponse( AgentException(AgentException.E_ARGS_UNEXPECTED, kwargs.keys()).message) if filetype == 'zip': source = zipfile.ZipFile(file.file, 'r') elif filetype == 'tar': source = tarfile.open(fileobj=file.file) elif filetype == 'git': source = git.DEFAULT_CODE_REPO else: return HttpErrorResponse('Unknown archive type ' + str(filetype)) if not exists(join(self.VAR_CACHE, 'www')): makedirs(join(self.VAR_CACHE, 'www')) target_dir = join(self.VAR_CACHE, 'www', codeVersionId) if exists(target_dir): rmtree(target_dir) if filetype == 'git': target_dir = join(self.VAR_CACHE, 'www') self.logger.debug("git_enable_revision('%s', '%s', '%s')" % (target_dir, source, codeVersionId)) git.git_enable_revision(target_dir, source, codeVersionId) else: source.extractall(target_dir) # Fix session handlers self.fix_session_handlers(target_dir) return HttpJsonResponse()
def _load_dump(self, f): if not exists(self.master_file): return HttpErrorResponse( AgentException(AgentException.E_CONFIG_NOT_EXIST).message) try: fd = open(self.master_file, 'r') p = pickle.load(fd) p.load_dump(f) fd.close() except Exception as e: ex = AgentException(AgentException.E_CONFIG_READ_FAILED, role.MySQLMaster.__name__, self.master_file, detail=e) self.logger.exception(ex.message) raise
def createTomcat(self, kwargs): """Create Tomcat6""" ret = {} if 'tomcat_port' not in kwargs: return HttpErrorResponse( AgentException(AgentException.E_ARGS_MISSING, 'tomcat_port').message) ret['tomcat_port'] = kwargs.pop('tomcat_port') if len(kwargs) != 0: return HttpErrorResponse( AgentException(AgentException.E_ARGS_UNEXPECTED, kwargs.keys()).message) with self.tomcat_lock: return self._create(ret, self.tomcat_file, role.Tomcat6)
def stopHttpProxy(self, kwargs): """KILL the HttpProxy""" if len(kwargs) != 0: return HttpErrorResponse( AgentException(AgentException.E_ARGS_UNEXPECTED, kwargs.keys()).message) with self.httpproxy_lock: return self._stop(kwargs, self.httpproxy_file, self.HttpProxy)
def stopTomcat(self, kwargs): """KILL Tomcat6""" if len(kwargs) != 0: return HttpErrorResponse( AgentException(AgentException.E_ARGS_UNEXPECTED, kwargs.keys()).message) with self.tomcat_lock: return self._stop(kwargs, self.tomcat_file, role.Tomcat6)
def stopWebServer(self, kwargs): """KILL the WebServer""" if len(kwargs) != 0: return HttpErrorResponse( AgentException(AgentException.E_ARGS_UNEXPECTED, kwargs.keys()).message) with self.web_lock: return self._stop(kwargs, self.webserver_file, self.WebServer)
def getHttpProxyState(self, kwargs): """GET state of HttpProxy""" if len(kwargs) != 0: return HttpErrorResponse( AgentException(AgentException.E_ARGS_UNEXPECTED, kwargs.keys()).message) with self.httpproxy_lock: return self._get(kwargs, self.httpproxy_file, self.HttpProxy)
def stopPHP(self, kwargs): """KILL the PHPProcessManager""" if len(kwargs) != 0: return HttpErrorResponse( AgentException(AgentException.E_ARGS_UNEXPECTED, kwargs.keys()).message) with self.php_lock: return self._stop(kwargs, self.php_file, role.PHPProcessManager)
def set_password(self, kwargs): """Create a replication master""" self.logger.debug('Updating password') try: if 'username' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'username') username = kwargs.pop('username') if 'password' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'password') password = kwargs.pop('password') if len(kwargs) != 0: raise AgentException(AgentException.E_ARGS_UNEXPECTED, kwargs.keys()) self._set_password(username, password) return HttpJsonResponse() except AgentException as e: return HttpErrorResponse(e.message)
def _get(self, get_params, class_file, pClass): if not exists(class_file): return HttpErrorResponse( AgentException(AgentException.E_CONFIG_NOT_EXIST).message) try: fd = open(class_file, 'r') p = pickle.load(fd) fd.close() except Exception as e: ex = AgentException(AgentException.E_CONFIG_READ_FAILED, pClass.__name__, class_file, detail=e) self.logger.exception(ex.message) return HttpErrorResponse(ex.message) else: return HttpJsonResponse({'return': p.status()})
def getPHPState(self, kwargs): """GET state of PHPProcessManager""" if len(kwargs) != 0: return HttpErrorResponse( AgentException(AgentException.E_ARGS_UNEXPECTED, kwargs.keys()).message) with self.php_lock: return self._get(kwargs, self.php_file, role.PHPProcessManager)
def getTomcatState(self, kwargs): """GET state of Tomcat6""" if len(kwargs) != 0: return HttpErrorResponse( AgentException(AgentException.E_ARGS_UNEXPECTED, kwargs.keys()).message) with self.tomcat_lock: return self._get(kwargs, self.tomcat_file, role.Tomcat6)
def _webserver_get_params(self, kwargs): ret = {} if 'port' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'port') if not isinstance(kwargs['port'], int): raise AgentException(AgentException.E_ARGS_INVALID, detail='Invalid "port" value') ret['port'] = int(kwargs.pop('port')) if 'code_versions' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'code_versions') ret['code_versions'] = kwargs.pop('code_versions') if len(kwargs) != 0: raise AgentException(AgentException.E_ARGS_UNEXPECTED, kwargs.keys()) return ret
def load_dump(self, kwargs): self.logger.debug('Uploading mysql dump ') self.logger.debug(kwargs) #TODO: archive the dump? if 'mysqldump_file' not in kwargs: return HttpErrorResponse( AgentException(AgentException.E_ARGS_MISSING, 'mysqldump_file').message) file = kwargs.pop('mysqldump_file') if not isinstance(file, FileUploadField): return HttpErrorResponse( AgentException( AgentException.E_ARGS_INVALID, detail='"mysqldump_file" should be a file').message) try: self._load_dump(file.file) except AgentException as e: return HttpErrorResponse(e.message) else: return HttpJsonResponse()
def _stop(self, get_params, class_file, pClass): if not exists(class_file): return HttpErrorResponse( AgentException(AgentException.E_CONFIG_NOT_EXIST).message) try: try: fd = open(class_file, 'r') p = pickle.load(fd) fd.close() except Exception as e: ex = AgentException(AgentException.E_CONFIG_READ_FAILED, detail=e) self.logger.exception(ex.message) return HttpErrorResponse(ex.message) p.stop() remove(class_file) return HttpJsonResponse() except Exception as e: ex = AgentException(AgentException.E_UNKNOWN, detail=e) self.logger.exception(e) return HttpErrorResponse(ex.message)
def setup_slave(self, kwargs): """Create a replication Slave""" self.logger.debug('slave in setup_slave ') if 'master_host' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'master_host') params = { "master_host": kwargs["master_host"], "config": self.config_parser } self.logger.debug(params) with self.slave_lock: return self._create(params, self.slave_file, role.MySQLSlave)
def set_snapshot(self, kwargs): if 'archive_url' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'archive_url') self.logger.info('set_snapshot: restoring archive') archive_cmd = "wget --ca-certificate /etc/cpsagent/certs/ca_cert.pem " archive_cmd += kwargs['archive_url'] + " -O - | tar xz -C /" self.logger.debug('set_snapshot: %s' % archive_cmd) out, err = run_cmd(archive_cmd) self.logger.debug('set_snapshot: stdout %s' % out) self.logger.debug('set_snapshot: stderr %s' % err) self.logger.info('set_snapshot: archive restored successfully') return HttpJsonResponse()
def _slave_get_setup_params(self, kwargs): ret = {} if 'mysqldump_file' not in kwargs: return HttpErrorResponse( AgentException(AgentException.E_ARGS_MISSING, 'mysqldump_file').message) file = kwargs.pop('mysqldump_file') if not isinstance(file, FileUploadField): return HttpErrorResponse( AgentException( AgentException.E_ARGS_INVALID, detail='"mysqldump_file" should be a file').message) ret['mysqldump_file'] = file.file if 'master_host' not in kwargs: return HttpErrorResponse( AgentException(AgentException.E_ARGS_MISSING, 'master_host').message) ret['master_host'] = kwargs.pop('master_host') if 'master_log_file' not in kwargs: return HttpErrorResponse( AgentException(AgentException.E_ARGS_MISSING, 'master_log_file').message) ret['master_log_file'] = kwargs.pop('master_log_file') if 'master_log_pos' not in kwargs: return HttpErrorResponse( AgentException(AgentException.E_ARGS_MISSING, 'master_log_pos').message) ret['master_log_pos'] = kwargs.pop('master_log_pos') if 'slave_server_id' not in kwargs: return HttpErrorResponse( AgentException(AgentException.E_ARGS_MISSING, 'slave_server_id').message) ret['slave_server_id'] = kwargs.pop('slave_server_id') if len(kwargs) != 0: return HttpErrorResponse( AgentException(AgentException.E_ARGS_UNEXPECTED, kwargs.keys()).message) ret['config'] = self.config_parser return ret
def _php_get_params(self, kwargs): ret = {} if 'port' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'port') if not isinstance(kwargs['port'], int): raise AgentException(AgentException.E_ARGS_INVALID, detail='Invalid "port" value') ret['port'] = int(kwargs.pop('port')) if 'scalaris' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'scalaris') ret['scalaris'] = kwargs.pop('scalaris') if 'configuration' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'configuration') if not isinstance(kwargs['configuration'], dict): raise AgentException(AgentException.E_ARGS_INVALID, detail='invalid "configuration" object') ret['configuration'] = kwargs.pop('configuration') if len(kwargs) != 0: raise AgentException(AgentException.E_ARGS_UNEXPECTED, kwargs.keys()) return ret
def _DIR_get_params(self, kwargs): ret = {} if 'uuid' not in kwargs: raise AgentException(AgentException.E_ARGS_MISSING, 'uuid') ret['uuid'] = kwargs.pop('uuid') return ret