def restart(self, timeout=600): ''' Restart this instance of Tomcat. Restarting will only work if Tomcat is launched by a supported wrapper which exposes restarting functionality via JMX. >>> t.restart() ''' def started(): try: return self.server_status() == 'STARTED' except: return False def apps_started(apps): try: all_restarted = False max_attempts = 5 attempts = 0 while not all_restarted: current_apps = map( lambda x: (x['baseName'], x['stateName'] == 'STARTED'), self.list_webapps().values()) original_apps = map(lambda x: (x, True), apps) all_restarted = all_restarted or set( original_apps).issubset(set(current_apps)) attempts += 1 if attempts >= max_attempts: break return all_restarted except: return False apps = map(lambda x: x['baseName'], self.list_webapps().values()) if not self.can_restart(): raise TomcatError('{0} does not support remote restarting'.format( self.name)) self._restarter.restart() if not wait_until(lambda: not started(), timeout): raise TomcatError('Timed out waiting for {0} to shut down'.format( self.name)) if not wait_until(started, timeout): raise TomcatError('Timed out waiting for {0} to boot up'.format( self.name)) if not wait_until(lambda: apps_started(apps), timeout): raise TomcatError( 'Timed out waiting for applications ({0}) to boot up'.format( apps))
def run_command(self, command, *args, **opts): if len(self.members) <= 0: raise TomcatError("Cluster has no members") hosts = opts.setdefault('hosts', self.members.keys()) threads = opts.setdefault('threads', min(self.member_count(), self.max_threads)) abort_on_error = opts.setdefault('abort_on_error', False) if abort_on_error: abort = Value('b', 0) def run_cmd(host): try: if abort_on_error and abort.value: raise TomcatError('Aborted') self.log.debug("Performing %s%s on %s", command, args, host) self._run_progress_callback(event=events.CMD_START, command=command, args=args, node=host) rv = getattr(self.members[host], command)(*args) self._run_progress_callback(event=events.CMD_END, command=command, args=args, node=host) except Exception as e: if abort_on_error: abort.value = True rv = e return (host, rv) pool = ThreadPool(processes=threads) return ClusterCommandResults(pool.map(run_cmd, hosts))
def parse_warfile(filename): m = re.match('^(?P<ctx>(?P<path>.+?)(##(?P<ver>.+?))?)\\.war$', '/' + os.path.basename(filename), flags=re.I) if m == None: raise TomcatError("Invalid WAR file name: '{0}'".format(filename)) return (m.group('ctx'), m.group('path'), m.group('ver'))
def _do_get(self, request, timeout=None): if timeout is None: timeout=self.timeout request = urllib2.Request('%s?%s' % (self.baseurl, request)) cmd_url = request.get_full_url() request.add_header("Authorization", self.auth_header) self.log.debug("JMXProxy request: %s", cmd_url) try: result = urllib2.urlopen(request, None, timeout) except Exception as e: raise TomcatError('Error communicating with {0}: {1}'.format(cmd_url, e)) rv = result.read().replace('\r','') self.log.debug("JMXProxy response: %s", rv) if not rv.startswith('OK'): raise TomcatError(rv) return rv
def expire_sessions(self, app, vhost='*'): ''' Forcefully expire ALL active sessions in a webapp >>> t.expire_sessions('/manager') ''' sessions = self.list_sessions(app, vhost) if len(sessions) <= 0: raise TomcatError( "Unable to find context '{0}' from vhost '{1}'".format( app, vhost)) mgrs = self.find_managers(app, vhost) for ctx, ids in sessions.iteritems(): for id in ids: if not ctx in mgrs: raise TomcatError( "Unable to find manager for context '{0}' from vhost '{1}'" .format(app, vhost)) self._expire_session(mgrs[ctx]['objectName'], id)
def restart(self, timeout=600): ''' Restart this instance of Tomcat. Restarting will only work if Tomcat is launched by a supported wrapper which exposes restarting functionality via JMX. >>> t.restart() ''' def started(): try: return self.server_status() == 'STARTED' except: return False if not self.can_restart(): raise TomcatError('{0} does not support remote restarting'.format( self.name)) self._restarter.restart() if not wait_until(lambda: not started(), timeout): raise TomcatError('Timed out waiting for {0} to shut down'.format( self.name)) if not wait_until(started, timeout): raise TomcatError('Timed out waiting for {0} to boot up'.format( self.name))
def run_cmd(host): try: if abort_on_error and abort.value: raise TomcatError('Aborted') self.log.debug("Performing %s%s on %s", command, args, host) self._run_progress_callback(event=events.CMD_START, command=command, args=args, node=host) rv = getattr(self.members[host], command)(*args) self._run_progress_callback(event=events.CMD_END, command=command, args=args, node=host) except Exception as e: if abort_on_error: abort.value = True rv = e return (host, rv)
def add_member(self, t): member_id = '{0}:{1}'.format(t.host, t.port) if member_id in self.members: raise TomcatError('{0} already exists'.format(member_id)) self.members[member_id] = t