def slave_status(self): ret = self.fetchdict("SHOW SLAVE STATUS") LOG.debug('slave status: %s' % str(ret)) if ret: return ret else: raise ServiceError( 'SHOW SLAVE STATUS returned empty set. Slave is not started?')
def create(self): if self.exists(): raise ServiceError('Unable to create user %s@%s: already exists.') LOG.debug( 'Creating user %s on host %s with password %s and privileges %s' % (self.login, self.host, self.password, self.privileges)) self.cli.create_user(self.login, self.host, self.password, self.privileges) return self
def create_redis_conf_copy(port=DEFAULT_PORT): if not os.path.exists(DEFAULT_CONF_PATH): raise ServiceError('Default redis config %s does not exist' % DEFAULT_CONF_PATH) dst = get_redis_conf_path(port) if not os.path.exists(dst): LOG.debug('Copying %s to %s.' % (DEFAULT_CONF_PATH,dst)) shutil.copy(DEFAULT_CONF_PATH, dst) else: LOG.debug('%s already exists.' % dst)
def stop(self, reason=None): if self.running: if self.pid: LOG.info('Stopping redis server on port %s (pid %s). Reason: %s' % (self.port, self.pid, reason)) os.kill(int(self.pid), signal.SIGTERM) wait_until(lambda: not self.running) else: #XXX: rare case when process is alive but scalarizr is unable to get PID raise ServiceError("Cannot stop redis process: PID file not found.")
def create_redis_conf_copy(port=__redis__['defaults']['port']): if not os.path.exists(__redis__['defaults']['redis.conf']): raise ServiceError('Default redis config %s does not exist' % __redis__['defaults']['redis.conf']) dst = get_redis_conf_path(port) if not os.path.exists(dst): LOG.debug('Copying %s to %s.' % (__redis__['defaults']['redis.conf'],dst)) shutil.copy(__redis__['defaults']['redis.conf'], dst) chown_r(dst, 'redis') else: LOG.debug('%s already exists.' % dst)
def stop(self, reason=None): if self.running: if self.pid: LOG.debug("Waiting until redis service is ready to shut down") wait_until(lambda: not self._is_aof_rewrite_running) # http://redis.io/commands/shutdown LOG.info('Stopping redis server on port %s (pid %s). Reason: %s' % (self.port, self.pid, reason)) os.kill(int(self.pid), signal.SIGTERM) #self.cli.execute("SHUTDOWN SAVE") wait_until(lambda: not self.running) LOG.debug("Redis process terminated.") else: #XXX: rare case when process is alive but scalarizr is unable to get PID raise ServiceError("Cannot stop redis process: PID file not found.")
def init_processes(self, num, ports=None, passwords=None): ports = ports or [] passwords = passwords or [] if not __redis__["use_password"]: # Ignoring passwords from HostInitResponse if use_password=0 passwords = [None for password in passwords] if len(ports) < num: diff = num - len(ports) LOG.debug("Passed ports: %s. Need to find %s more." % (str(ports), diff)) additional_ports = [ port for port in get_available_ports() if port not in ports ] if len(additional_ports) < diff: raise ServiceError('Not enough free ports') LOG.debug("Found available ports: %s" % str(additional_ports)) ports += additional_ports[:diff] if len(passwords) < len(ports): diff = len(ports) - len(passwords) if __redis__["use_password"]: LOG.debug("Generating %s additional passwords for ports %s" % (diff, ports[-diff:])) additional_passwords = [ cryptotool.pwgen(20) for port in ports[-diff:] ] LOG.debug("Generated passwords: %s" % str(additional_passwords)) passwords += additional_passwords else: LOG.debug( "Setting %s additional empty passwords for ports %s" % (diff, ports[-diff:])) passwords += [None for port in ports[-diff:]] assert len(ports) == len(passwords) creds = dict(zip(ports, passwords)) LOG.debug("Initializing redis processes: %s" % str(creds)) for port, password in creds.items(): if port not in self.ports: create_redis_conf_copy(port) redis_process = Redis(port, password) self.instances.append(redis_process) LOG.debug('Total of redis processes: %d' % len(self.instances))
def _fetch(self, query, cursor_type=None, fetch_one=False): conn = self.get_connection() cur = conn.cursor(cursor_type) LOG.debug(query) try: cur.execute(query) except (pymysql.err.Error, pymysql.err.OperationalError, socket.error, IOError), e: #catching mysqld restarts (e.g. sgt) if type(e) == pymysql.err.Error or e.args[0] in (2013, 32, errno.EPIPE): try: conn = self.get_connection(force=True) cur = conn.cursor(cursor_type) cur.execute(query) except socket.error, err: if err.args[0] == 32: raise ServiceError( 'Scalarizr was unable to connect to mysql with user %s: (%s)' % (self.user, str(err)))
def exists(self): result = False try: result = self.cli.user_exists(self.login, self.host) except pymysql.err.OperationalError, e: raise ServiceError(str(e))
def get_instance(self, port=None): for instance in self.instances: if instance.port == port: return instance raise ServiceError('Redis instance with port %s not found' % port)