예제 #1
0
    def installPackage( self, package, hostId ):
        # hostId can be either numeric (the actual id) or it can be
        # the name. In the latter case, try to find the host by its
        # name
        host = None
        try:
            if hostId.isdigit():
                host = self.hosts[ int( hostId )]
            else:
                for next in self.hosts.values():
                    if hostId == next.hostname:
                        host = next
                        break
            if not host: 
                raise Exception('Host not found')
        except:
            raise Exception( 'Host %s not found' % hostId )
            
        
        self.log.info('Installing package %s on host %s' %( package, host.hostname ))
        command = 'screen -S "install" sudo apt-get -y install "%s"\r\nexit\r\n' % package

        channel = host.invokeShell()
        screenReader = ScreenReader(self.name, channel, self.log)
        screenReader.start()        
        channel.send( command )
        screenReader.join( 60.0 )
        log = screenReader.getBuffer()

        self.log.info( 'Package %s installed' % package )

        escapedPackageName = package.replace( "'", "\'" )
        startScript = '/opt/webportal/%s/start.sh' % escapedPackageName
        stopScript  = '/opt/webportal/%s/stop.sh'  % escapedPackageName
        command = 'screen -S installer\r\n[ -e \'%s\' ]; echo "Start$?"\r\n \
[ -e \'%s\' ]; echo "Stop$?"\r\nexit\r\nexit\r\n' % ( startScript, stopScript )
        
        channel = host.invokeShell()
        screenReader = ScreenReader(self.name, channel, self.log)
        screenReader.start()        
        channel.send( command )
        screenReader.join( 30.0 )
        output = screenReader.getBuffer()
        startCommand = startScript if output.find( 'Start0' ) >= 0 else ''
        stopCommand  = stopScript  if output.find( 'Stop0'  ) >= 0 else ''
        self.log.info( 'StartScript found: %s, StopScript found: %s' % ( startCommand, \
            stopCommand ))
        #self.log.info( output )

        return log, startCommand, stopCommand
예제 #2
0
	def start(self, globalVars=None):
		if not self.component or not self.component.host:
			raise AttributeError('Host is not set for %s' % str(self))

		if not self.isAlive():
			self.clearScreenReaders()

			if not self.canStart():
				self.log.info('Cannot start Action "%s", no commands found' % (self.name))
				return
			
			self.log.info('Starting Action "%s". "%d" Commands / "%d" dependencies found' % (self.name, len(self._startCommands), len(self.dependencies)))

			if len(self.dependencies):
				self.log.info('Action depends on: %s' % list('%s>%s' % (item.component.getName(), item.name) for item in self.dependencies))
				for dep in self.dependencies:
					dep.start()

			for cmd in self._startCommands.values():
				channel = self.component.host.invokeShell()
				command = self.createScreenCmd( cmd, globalVars )
				hideLog = cmd.hideLog
				self.startChannels.append(channel)

				screen_name = self.name + '_run'
				screenReader = ScreenReader( screen_name, channel, self.log, notifyStop=self.screenReaderStopped)
				self.screenReaders['hide' if hideLog else 'show'].append(screenReader)
				screenReader.start()

				self.log.debug('Running start command "%s" by Action "%s"' % (command.replace('\n','\\n'), self.name))
				channel.send(command)

				self.constantlyCheckState( globalVars )
				# if blocking is enabled for this command, wait for the screenreader to quit
				if cmd.blocking and screenReader and screenReader.isAlive():
					self.log.debug('Command requires blocking. Action "%s" joined the screenReader Thread. Waiting for it to finish' % self.name)
					screenReader.join( 5.0 )


			# notify EventHandler after all cmds run
			if self.isAlive():
				EventHistory.actionRun(self, self.component)
			
			return True

		else:
			self.log.debug('Could not start action "%s", Action still active' % self.name)
			return False
예제 #3
0
	def constantlyCheckState( self, globalVars=None ):
		if not self.statusFile: 
			return
		loopCmd = 'while true; do %s; sleep 10; done' % self.statusFile
		self.stateCommand = ShellCommand( -1, loopCmd, False, False )
		self.stateChannel = self.component.host.invokeShell()
		command = self.createScreenCmd( self.stateCommand, globalVars )

		self.log.debug('Constantly checking file "%s" for Action "%s"' % (self.statusFile, self.name))

		screen_name = self.name + '_watchdog'
		screenReader = ScreenReader( screen_name, self.stateChannel, self.log, 
			notifyNewline=self.notifyNewStatusLine, notifyStop=self.screenReaderStopped )
		self.screenReaders[ 'show' ].append( screenReader )
		screenReader.start()
	
		self.stateChannel.send(command)
예제 #4
0
    def loadParameters( self, component, action ):
        host = component.host
        channel = host.invokeShell()
        channel.send( 'screen -S load_parameters\r\n' )
        screenReader = ScreenReader( 'load_parameters', channel, self.log )
        screenReader.start()

        channel.send( action.parameterFile + ' load\r\n' )
        channel.send( 'exit\r\n' )
        screenReader.join( 15.0 )
        rawData = screenReader.getBuffer()
        try:
            start = rawData.lower().index( '<configuration>' )
            end = rawData.lower().index( '</configuration>' ) + len( '</configuration>' )
            return rawData[ start:end ]
        except ValueError,e:
            raise ValueError( 'Invalid xml format' )
예제 #5
0
	def stop(self, force=False, globalVars=None ):
		if not self.component or not self.component.host:
			raise AttributeError('Host is not set for %s' % str(self))

		if not self.isAlive() and not force:
			self.log.debug('Action "%s"::stop skipped because component is not alive')
			return False

		self.killStateChannel() 

		if not self.canStop():
			self.log.info('Cannot stop Action "%s" (force=%s), no commands found' % (self.name, str(force)))
			return
		
		self.log.info('Stopping Action "%s" (force=%s). "%d" Commands found' % (self.name, str(force), len(self._stopCommands)))

		for cmd in self._stopCommands.values():
			channel = self.component.host.invokeShell()
			command = self.createScreenCmd( cmd, globalVars )
			hideLog = cmd.hideLog
			
			self.log.debug('Running stop command "%s" by Action "%s"' % (command.replace('\n','\\n'), self.name))

			screen_name = self.name + '_stop'
			screenReader = ScreenReader( screen_name, channel, self.log, notifyStop=self.screenReaderStopped)
			self.screenReaders['hide' if hideLog else 'show'].append(screenReader)
			screenReader.start()
		
			channel.send(command)

			# if blocking is enabled for this command, wait for the screenreader to quit
			if cmd.blocking and screenReader and screenReader.isAlive():
				self.log.debug('Command requires blocking. Action "%s" joined the screenReader Thread. Waiting for it to finish' % self.name)
				screenReader.join( 5.0 )

		# Wait for all readers (start and stop) to finish
		for reader in self.screenReaders['hide']+self.screenReaders['show']:
			if reader and reader.isAlive():
				self.log.debug('Action "%s"::stop joined the screenReader Thread. Waiting for it to finish' % self.name)
				reader.join( 5.0 )


		return True
예제 #6
0
    def saveParameters( self, component, action, data ):
        host = component.host
        channel = host.invokeShell()
        channel.send( 'screen -S save_parameters\r\n' )
        screenReader = ScreenReader( 'save_parameters', channel, self.log )
        screenReader.start()

        xml = '<configuration>'
        nodes = []
        for key,value in data.iteritems():
            nodes.append( '<param name=\\"%s\\" value=\\"%s\\" />' % (
                self.escapeXMLValue( key ), self.escapeXMLValue( value )))
        xml += ''.join( nodes )
        xml += '</configuration>'

        command = action.parameterFile + ' save "%s"\r\n' % xml
        self.log.debug( 'Sending command %s' % command )
        channel.send( command )
        channel.send( 'exit\r\n' )
        screenReader.join( 10.0 )
        rawData = screenReader.getBuffer()

        return True