Пример #1
0
  def do_show( self, args ):
    """
        Show list of components with various related information

        usage:

          show software      - show components for which software is available
          show installed     - show components installed in the host with runit system
          show setup         - show components set up for automatic running in the host
          show project       - show project to install or upgrade
          show status        - show status of the installed components
          show database      - show status of the databases
          show mysql         - show status of the MySQL server
          show log  <system> <service|agent> [nlines]
                             - show last <nlines> lines in the component log file
          show info          - show version of software and setup
          show host          - show host related parameters
          show errors [*|<system> <service|agent>]
                             - show error count for the given component or all the components
                               in the last hour and day
    """

    argss = args.split()
    if not argss:
      print self.do_show.__doc__
      return

    option = argss[0]
    del argss[0]

    if option == 'software':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getSoftwareComponents()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        print
        pprint.pprint( result['Value'] )
    elif option == 'installed':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getInstalledComponents()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        print
        pprint.pprint( result['Value'] )
    elif option == 'setup':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getSetupComponents()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        print
        pprint.pprint( result['Value'] )
    elif option == 'project':
      result = SystemAdministratorClient( self.host, self.port ).getProject()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        print "Current project is %s" % result[ 'Value' ]
    elif option == 'status':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getOverallStatus()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        fields = ["System",'Name','Module','Type','Setup','Installed','Runit','Uptime','PID']
        records = []
        rDict = result['Value']
        for compType in rDict:
          for system in rDict[compType]:
            components = rDict[compType][system].keys()
            components.sort()
            for component in components:
              record = []
              if rDict[compType][system][component]['Installed']:
                module = str( rDict[compType][system][component]['Module'] )
                record += [ system,component,module,compType.lower()[:-1]]
                if rDict[compType][system][component]['Setup']:
                  record += ['Setup']
                else:
                  record += ['NotSetup']
                if rDict[compType][system][component]['Installed']:
                  record += ['Installed']
                else:
                  record += ['NotInstalled']
                record += [str( rDict[compType][system][component]['RunitStatus'] )]
                record += [str( rDict[compType][system][component]['Timeup'] )]
                record += [str( rDict[compType][system][component]['PID'] )]
                records.append(record)  
        printTable(fields,records)        
    elif option == 'database' or option == 'databases':
      client = SystemAdministratorClient( self.host, self.port )
      if not InstallTools.mysqlPassword:
        InstallTools.mysqlPassword = "******"
      InstallTools.getMySQLPasswords()
      result = client.getDatabases( InstallTools.mysqlRootPwd )
      if not result['OK']:
        self.__errMsg( result['Message'] )
        return
      resultSW = client.getAvailableDatabases()
      if not resultSW['OK']:
        self.__errMsg( resultSW['Message'] )
        return

      sw = resultSW['Value']
      installed = result['Value']
      print
      for db in sw:
        if db in installed:
          print db.rjust( 25 ), ': Installed'
        else:
          print db.rjust( 25 ), ': Not installed'
      if not sw:
        print "No database found"
    elif option == 'mysql':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getMySQLStatus()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      elif result['Value']:
        print
        for par, value in result['Value'].items():
          print par.rjust( 28 ), ':', value
      else:
        print "No MySQL database found"
    elif option == "log":
      self.getLog( argss )
    elif option == "info":
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getInfo()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        print
        print "Setup:", result['Value']['Setup']
        print "DIRAC version:", result['Value']['DIRAC']
        if result['Value']['Extensions']:
          for e, v in result['Value']['Extensions'].items():
            print "%s version" % e, v
        print
    elif option == "host":
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getHostInfo()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:   
        print   
        print "Host info:"
        print
        
        fields = ['Parameter','Value']
        records = []
        for key,value in result['Value'].items():
          records.append( [key, str(value) ] )
          
        printTable( fields, records )  
            
    elif option == "errors":
      self.getErrors( argss )
    else:
      print "Unknown option:", option
    def do_show(self, args):
        """
        Show list of components with various related information

        usage:

          show software      - show components for which software is available
          show installed     - show components installed in the host with runit system
          show setup         - show components set up for automatic running in the host
          show project       - show project to install or upgrade
          show status        - show status of the installed components
          show database      - show status of the databases
          show mysql         - show status of the MySQL server
          show log  <system> <service|agent> [nlines]
                             - show last <nlines> lines in the component log file
          show info          - show version of software and setup
          show host          - show host related parameters
          show errors [*|<system> <service|agent>]
                             - show error count for the given component or all the components
                               in the last hour and day
    """

        argss = args.split()
        if not argss:
            print self.do_show.__doc__
            return

        option = argss[0]
        del argss[0]

        if option == 'software':
            client = SystemAdministratorClient(self.host, self.port)
            result = client.getSoftwareComponents()
            if not result['OK']:
                self.__errMsg(result['Message'])
            else:
                print
                pprint.pprint(result['Value'])
        elif option == 'installed':
            client = SystemAdministratorClient(self.host, self.port)
            result = client.getInstalledComponents()
            if not result['OK']:
                self.__errMsg(result['Message'])
            else:
                print
                pprint.pprint(result['Value'])
        elif option == 'setup':
            client = SystemAdministratorClient(self.host, self.port)
            result = client.getSetupComponents()
            if not result['OK']:
                self.__errMsg(result['Message'])
            else:
                print
                pprint.pprint(result['Value'])
        elif option == 'project':
            result = SystemAdministratorClient(self.host,
                                               self.port).getProject()
            if not result['OK']:
                self.__errMsg(result['Message'])
            else:
                print "Current project is %s" % result['Value']
        elif option == 'status':
            client = SystemAdministratorClient(self.host, self.port)
            result = client.getOverallStatus()
            if not result['OK']:
                self.__errMsg(result['Message'])
            else:
                fields = [
                    "System", 'Name', 'Module', 'Type', 'Setup', 'Installed',
                    'Runit', 'Uptime', 'PID'
                ]
                records = []
                rDict = result['Value']
                for compType in rDict:
                    for system in rDict[compType]:
                        components = rDict[compType][system].keys()
                        components.sort()
                        for component in components:
                            record = []
                            if rDict[compType][system][component]['Installed']:
                                module = str(rDict[compType][system][component]
                                             ['Module'])
                                record += [
                                    system, component, module,
                                    compType.lower()[:-1]
                                ]
                                if rDict[compType][system][component]['Setup']:
                                    record += ['Setup']
                                else:
                                    record += ['NotSetup']
                                if rDict[compType][system][component][
                                        'Installed']:
                                    record += ['Installed']
                                else:
                                    record += ['NotInstalled']
                                record += [
                                    str(rDict[compType][system][component]
                                        ['RunitStatus'])
                                ]
                                record += [
                                    str(rDict[compType][system][component]
                                        ['Timeup'])
                                ]
                                record += [
                                    str(rDict[compType][system][component]
                                        ['PID'])
                                ]
                                records.append(record)
                printTable(fields, records)
        elif option == 'database' or option == 'databases':
            client = SystemAdministratorClient(self.host, self.port)
            if not InstallTools.mysqlPassword:
                InstallTools.mysqlPassword = "******"
            InstallTools.getMySQLPasswords()
            result = client.getDatabases(InstallTools.mysqlRootPwd)
            if not result['OK']:
                self.__errMsg(result['Message'])
                return
            resultSW = client.getAvailableDatabases()
            if not resultSW['OK']:
                self.__errMsg(resultSW['Message'])
                return

            sw = resultSW['Value']
            installed = result['Value']
            print
            for db in sw:
                if db in installed:
                    print db.rjust(25), ': Installed'
                else:
                    print db.rjust(25), ': Not installed'
            if not sw:
                print "No database found"
        elif option == 'mysql':
            client = SystemAdministratorClient(self.host, self.port)
            result = client.getMySQLStatus()
            if not result['OK']:
                self.__errMsg(result['Message'])
            elif result['Value']:
                print
                for par, value in result['Value'].items():
                    print par.rjust(28), ':', value
            else:
                print "No MySQL database found"
        elif option == "log":
            self.getLog(argss)
        elif option == "info":
            client = SystemAdministratorClient(self.host, self.port)
            result = client.getInfo()
            if not result['OK']:
                self.__errMsg(result['Message'])
            else:
                print
                print "Setup:", result['Value']['Setup']
                print "DIRAC version:", result['Value']['DIRAC']
                if result['Value']['Extensions']:
                    for e, v in result['Value']['Extensions'].items():
                        print "%s version" % e, v
                print
        elif option == "host":
            client = SystemAdministratorClient(self.host, self.port)
            result = client.getHostInfo()
            if not result['OK']:
                self.__errMsg(result['Message'])
            else:
                print
                print "Host info:"
                print

                fields = ['Parameter', 'Value']
                records = []
                for key, value in result['Value'].items():
                    records.append([key, str(value)])

                printTable(fields, records)

        elif option == "errors":
            self.getErrors(argss)
        else:
            print "Unknown option:", option
Пример #3
0
  def do_install( self, args ):
    """
        Install various DIRAC components

        usage:

          install mysql
          install db <database>
          install service <system> <service> [-m <ModuleName>] [-p <Option>=<Value>] [-p <Option>=<Value>] ...
          install agent <system> <agent> [-m <ModuleName>] [-p <Option>=<Value>] [-p <Option>=<Value>] ...
          install executor <system> <executor> [-m <ModuleName>] [-p <Option>=<Value>] [-p <Option>=<Value>] ...
    """
    argss = args.split()
    if not argss:
      gLogger.notice( self.do_install.__doc__ )
      return

    # Retrieve user installing the component
    result = getProxyInfo()
    if not result[ 'OK' ]:
      self.__errMsg( result[ 'Message'] )
    user = result[ 'Value' ][ 'username' ]

    option = argss[0]
    del argss[0]
    if option == "mysql":
      gLogger.notice( "Installing MySQL database, this can take a while ..." )
      client = SystemAdministratorClient( self.host, self.port )
      if InstallTools.mysqlPassword == 'LocalConfig':
        InstallTools.mysqlPassword = ''
      InstallTools.getMySQLPasswords()
      result = client.installMySQL( InstallTools.mysqlRootPwd, InstallTools.mysqlPassword )
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        gLogger.notice( "MySQL:", result['Value'] )
        gLogger.notice( "You might need to restart SystemAdministrator service to take new settings into account" )
    elif option == "db":
      if not argss:
        gLogger.notice( self.do_install.__doc__ )
        return
      database = argss[0]
      client = SystemAdministratorClient( self.host, self.port )

      result = client.getAvailableDatabases()
      if not result['OK']:
        self.__errMsg( "Can not get database list: %s" % result['Message'] )
        return
      if not result['Value'].has_key( database ):
        self.__errMsg( "Unknown database %s: " % database )
        return
      system = result['Value'][database]['System']
      setup = gConfig.getValue( '/DIRAC/Setup', '' )
      if not setup:
        self.__errMsg( "Unknown current setup" )
        return
      instance = gConfig.getValue( '/DIRAC/Setups/%s/%s' % ( setup, system ), '' )
      if not instance:
        self.__errMsg( "No instance defined for system %s" % system )
        self.__errMsg( "\tAdd new instance with 'add instance %s <instance_name>'" % system )
        return

      if not InstallTools.mysqlPassword:
        InstallTools.mysqlPassword = '******'
      InstallTools.getMySQLPasswords()
      result = client.installDatabase( database, InstallTools.mysqlRootPwd )
      if not result['OK']:
        self.__errMsg( result['Message'] )
        return
      extension, system = result['Value']

      result = client.getHostInfo()
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return
      else:
        cpu = result[ 'Value' ][ 'CPUModel' ]
      hostname = self.host
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return

      if database != 'InstalledComponentsDB':
        result = MonitoringUtilities.monitorInstallation( 'DB', system.replace( 'System', '' ), database, cpu = cpu, hostname = hostname )
        if not result['OK']:
          self.__errMsg( result['Message'] )
          return
      # result = client.addDatabaseOptionsToCS( system, database )
      InstallTools.mysqlHost = self.host
      result = client.getInfo()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      hostSetup = result['Value']['Setup']
      result = InstallTools.addDatabaseOptionsToCS( gConfig, system, database, hostSetup, overwrite = True )
      if not result['OK']:
        self.__errMsg( result['Message'] )
        return
      gLogger.notice( "Database %s from %s/%s installed successfully" % ( database, extension, system ) )
    elif option in self.runitComponents:
      if len( argss ) < 2:
        gLogger.notice( self.do_install.__doc__ )
        return

      system = argss[0]
      del argss[0]
      component = argss[0]
      del argss[0]
      
      specialOptions = {}
      module = ''
      for i in range(len(argss)):
        if argss[i] == "-m":
          specialOptions['Module'] = argss[i+1]
          module = argss[i+1]
        if argss[i] == "-p":
          opt,value = argss[i+1].split('=')
          specialOptions[opt] = value           
      if module == component:
        module = ''
      
      client = SystemAdministratorClient( self.host, self.port )
      # First need to update the CS
      # result = client.addDefaultOptionsToCS( option, system, component )
      InstallTools.host = self.host
      result = client.getInfo()
      if not result['OK']:
        self.__errMsg( result['Message'] )
        return
      hostSetup = result['Value']['Setup']
      
      # Install Module section if not yet there
      if module:
        result = InstallTools.addDefaultOptionsToCS( gConfig, option, system, module, 
                                                     getCSExtensions(), hostSetup )
        # Add component section with specific parameters only
        result = InstallTools.addDefaultOptionsToCS( gConfig, option, system, component, 
                                                     getCSExtensions(), hostSetup, specialOptions, 
                                                     addDefaultOptions = False )
      else:  
        # Install component section
        result = InstallTools.addDefaultOptionsToCS( gConfig, option, system, component, 
                                                     getCSExtensions(), hostSetup, specialOptions )
    
      if not result['OK']:
        self.__errMsg( result['Message'] )
        return
      # Then we can install and start the component
      result = client.setupComponent( option, system, component, module )
      if not result['OK']:
        self.__errMsg( result['Message'] )
        return
      compType = result['Value']['ComponentType']
      runit = result['Value']['RunitStatus']
      gLogger.notice( "%s %s_%s is installed, runit status: %s" % ( compType, system, component, runit ) )

      # And register it in the database
      result = client.getHostInfo()
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return
      else:
        cpu = result[ 'Value' ][ 'CPUModel' ]
      hostname = self.host
      if component == 'ComponentMonitoring':
        result = MonitoringUtilities.monitorInstallation( 'DB', system, 'InstalledComponentsDB', cpu = cpu, hostname = hostname )
        if not result['OK']:
          self.__errMsg( 'Error registering installation into database: %s' % result[ 'Message' ] )
          return
      result = MonitoringUtilities.monitorInstallation( option, system, component, module, cpu = cpu, hostname = hostname )
      if not result['OK']:
        self.__errMsg( 'Error registering installation into database: %s' % result[ 'Message' ] )
        return
    else:
      gLogger.notice( "Unknown option:", option )
Пример #4
0
  def do_uninstall( self, args ):
    """
        Uninstall DIRAC component

        usage:

          uninstall db <database>
          uninstall <-f ForceLogUninstall> <system> <component>
    """
    argss = args.split()
    if not argss:
      gLogger.notice( self.do_uninstall.__doc__ )
      return

    # Retrieve user uninstalling the component
    result = getProxyInfo()
    if not result[ 'OK' ]:
      self.__errMsg( result[ 'Message'] )
    user = result[ 'Value' ][ 'username' ]

    option = argss[0]
    if option == 'db':
      component = argss[1]
      client = SystemAdministratorClient( self.host, self.port )

      result = client.getHostInfo()
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return
      else:
        cpu = result[ 'Value' ][ 'CPUModel' ]
      hostname = self.host
      result = client.getAvailableDatabases()
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return
      system = result[ 'Value' ][ component ][ 'System' ]
      result = MonitoringUtilities.monitorUninstallation( system , component, hostname = hostname, cpu = cpu )
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return

      result = client.uninstallDatabase( component )
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
      else:
        gLogger.notice( "Successfully uninstalled %s" % ( component ) )
    else:
      if option == '-f':
        force = True
        del argss[0]
      else:
        force = False

      if len( argss ) != 2:
        gLogger.notice( self.do_uninstall.__doc__ )
        return

      system, component = argss
      client = SystemAdministratorClient( self.host, self.port )

      monitoringClient = ComponentMonitoringClient()
      result = monitoringClient.getInstallations( { 'Instance': component, 'UnInstallationTime': None },
                                                  { 'System': system },
                                                  { 'HostName': self.host }, True )
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return
      if len( result[ 'Value' ] ) < 1:
        self.__errMsg( "Given component does not exist" )
        return
      if len( result[ 'Value' ] ) > 1:
        self.__errMsg( "Too many components match" )
        return

      removeLogs = False
      if force:
        removeLogs = True
      else:
        if result[ 'Value' ][0][ 'Component' ][ 'Type' ] in self.runitComponents:
          result = promptUser( 'Remove logs?', ['y', 'n'], 'n' )
          if result[ 'OK' ]:
            removeLogs = result[ 'Value' ] == 'y'

      result = client.uninstallComponent( system, component, removeLogs )
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
      else:
        gLogger.notice( "Successfully uninstalled %s/%s" % ( system, component ) )

      result = client.getHostInfo()
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return
      else:
        cpu = result[ 'Value' ][ 'CPUModel' ]
      hostname = self.host
      result = MonitoringUtilities.monitorUninstallation( system, component, hostname = hostname, cpu = cpu )
      if not result[ 'OK' ]:
        return result
Пример #5
0
  def do_uninstall( self, args ):
    """
        Uninstall DIRAC component

        usage:

          uninstall db <database>
          uninstall <-f ForceLogUninstall> <system> <component>
    """
    argss = args.split()
    if not argss:
      gLogger.notice( self.do_uninstall.__doc__ )
      return
    
    option = argss[0]
    if option == 'db':
      component = argss[1]
      client = SystemAdministratorClient( self.host, self.port )

      result = client.uninstallDatabase( component )
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
      else:
        gLogger.notice( "Successfully uninstalled %s" % ( component ) )

      result = client.getHostInfo()
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return
      else:
        cpu = result[ 'Value' ][ 'CPUModel' ]
      hostname = self.host
      result = client.getAvailableDatabases()
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return
      system = result[ 'Value' ][ component ][ 'System' ]
      result = MonitoringUtilities.monitorUninstallation( system , component, hostname = hostname, cpu = cpu )
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return
    else:
      if option == '-f':
        force = True
        del argss[0]
      else:
        force = False

      if len( argss ) != 2:
        gLogger.notice( self.do_uninstall.__doc__ )
        return

      system, component = argss
      client = SystemAdministratorClient( self.host, self.port )

      monitoringClient = ComponentMonitoringClient()
      result = monitoringClient.getInstallations( { 'Instance': component, 'UnInstallationTime': None },
                                                  { 'System': system },
                                                  { 'HostName': self.host }, True )
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return
      if len( result[ 'Value' ] ) < 1:
        self.__errMsg( "Given component does not exist" )
        return
      if len( result[ 'Value' ] ) > 1:
        self.__errMsg( "Too many components match" )
        return

      removeLogs = False
      if force:
        removeLogs = True
      else:
        if result[ 'Value' ][0][ 'Component' ][ 'Type' ] in self.runitComponents:
          result = promptUser( 'Remove logs?', ['y', 'n'], 'n' )
          if result[ 'OK' ]:
            removeLogs = result[ 'Value' ] == 'y'

      result = client.uninstallComponent( system, component, removeLogs )
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
      else:
        gLogger.notice( "Successfully uninstalled %s/%s" % ( system, component ) )

      result = client.getHostInfo()
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return
      else:
        cpu = result[ 'Value' ][ 'CPUModel' ]
      hostname = self.host
      result = MonitoringUtilities.monitorUninstallation( system, component, hostname = hostname, cpu = cpu )
      if not result[ 'OK' ]:
        return result
Пример #6
0
  def do_show( self, args ):
    """
        Show list of components with various related information

        usage:

          show software      - show components for which software is available
          show installed     - show components installed in the host with runit system
          show setup         - show components set up for automatic running in the host
          show project       - show project to install or upgrade
          show status        - show status of the installed components
          show database      - show status of the databases
          show mysql         - show status of the MySQL server
          show log  <system> <service|agent> [nlines]
                             - show last <nlines> lines in the component log file
          show info          - show version of software and setup
          show doc <type> <system> <name>
                             - show documentation for a given service or agent
          show host          - show host related parameters
          show hosts         - show all available hosts
          show installations [ list | current | -n <Name> | -h <Host> | -s <System> | -m <Module> | -t <Type> | -itb <InstallationTime before>
                              | -ita <InstallationTime after> | -utb <UnInstallationTime before> | -uta <UnInstallationTime after> ]*
                             - show all the installations of components that match the given parameters
          show errors [*|<system> <service|agent>]
                             - show error count for the given component or all the components
                               in the last hour and day
    """

    argss = args.split()
    if not argss:
      gLogger.notice( self.do_show.__doc__ )
      return

    option = argss[0]
    del argss[0]

    if option == 'software':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getSoftwareComponents()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        gLogger.notice( '' )
        pprint.pprint( result['Value'] )
    elif option == 'installed':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getInstalledComponents()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        gLogger.notice( '' )
        pprint.pprint( result['Value'] )
    elif option == 'setup':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getSetupComponents()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        gLogger.notice( '' )
        pprint.pprint( result['Value'] )
    elif option == 'project':
      result = SystemAdministratorClient( self.host, self.port ).getProject()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        gLogger.notice( "Current project is %s" % result[ 'Value' ] )
    elif option == 'status':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getOverallStatus()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        fields = ["System",'Name','Module','Type','Setup','Installed','Runit','Uptime','PID']
        records = []
        rDict = result['Value']
        for compType in rDict:
          for system in rDict[compType]:
            components = rDict[compType][system].keys()
            components.sort()
            for component in components:
              record = []
              if rDict[compType][system][component]['Installed']:
                module = str( rDict[compType][system][component]['Module'] )
                record += [ system,component,module,compType.lower()[:-1]]
                if rDict[compType][system][component]['Setup']:
                  record += ['Setup']
                else:
                  record += ['NotSetup']
                if rDict[compType][system][component]['Installed']:
                  record += ['Installed']
                else:
                  record += ['NotInstalled']
                record += [str( rDict[compType][system][component]['RunitStatus'] )]
                record += [str( rDict[compType][system][component]['Timeup'] )]
                record += [str( rDict[compType][system][component]['PID'] )]
                records.append(record)  
        printTable(fields,records)        
    elif option == 'database' or option == 'databases':
      client = SystemAdministratorClient( self.host, self.port )
      if not InstallTools.mysqlPassword:
        InstallTools.mysqlPassword = "******"
      InstallTools.getMySQLPasswords()
      result = client.getDatabases( InstallTools.mysqlRootPwd )
      if not result['OK']:
        self.__errMsg( result['Message'] )
        return
      resultSW = client.getAvailableDatabases()
      if not resultSW['OK']:
        self.__errMsg( resultSW['Message'] )
        return

      sw = resultSW['Value']
      installed = result['Value']
      gLogger.notice( '' )
      for db in sw:
        if db in installed:
          gLogger.notice( db.rjust( 25 ), ': Installed' )
        else:
          gLogger.notice( db.rjust( 25 ), ': Not installed' )
      if not sw:
        gLogger.notice( "No database found" )
    elif option == 'mysql':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getMySQLStatus()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      elif result['Value']:
        gLogger.notice( '' )
        for par, value in result['Value'].items():
          gLogger.notice( ( par.rjust( 28 ), ':', value ) )
      else:
        gLogger.notice( "No MySQL database found" )
    elif option == "log":
      self.getLog( argss )
    elif option == "info":
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getInfo()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        gLogger.notice( '' )
        gLogger.notice( "Setup:", result['Value']['Setup'] )
        gLogger.notice( "DIRAC version:", result['Value']['DIRAC'] )
        if result['Value']['Extensions']:
          for e, v in result['Value']['Extensions'].items():
            gLogger.notice( "%s version" % e, v )
        gLogger.notice( '' )
    elif option == "host":
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getHostInfo()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:   
        gLogger.notice( '' )
        gLogger.notice( "Host info:" )
        gLogger.notice( '' )
        
        fields = ['Parameter','Value']
        records = []
        for key, value in result['Value'].items():
          records.append( [key, str( value ) ] )
          
        printTable( fields, records )  
    elif option == "hosts":
      client = ComponentMonitoringClient()
      result = client.getHosts( {}, False, False )
      if not result[ 'OK' ]:
        self.__errMsg( 'Error retrieving the list of hosts: %s' % ( result[ 'Message' ] ) )
      else:
        hostList = result[ 'Value' ]
        gLogger.notice( '' )
        gLogger.notice( ' ' + 'Host'.center( 32 ) + ' ' + 'CPU'.center( 34 ) + ' ' )
        gLogger.notice( ( '-' * 69 ) )
        for element in hostList:
          gLogger.notice( '|' + element[ 'HostName' ].center( 32 ) + '|' + element[ 'CPU' ].center( 34 ) + '|' )
        gLogger.notice( ( '-' * 69 ) )
        gLogger.notice( '' )
    elif option == "errors":
      self.getErrors( argss )
    elif option == "installations":
      self.getInstallations( argss )
    elif option == "doc":
      if len( argss ) > 2:
        if argss[0] in [ 'service', 'agent' ]:
          compType = argss[0]
          compSystem = argss[1]
          compModule = argss[2]
          client = SystemAdministratorClient( self.host, self.port )
          result = client.getComponentDocumentation( compType, compSystem, compModule )
          if result[ 'OK' ]:
            gLogger.notice( result[ 'Value' ] )
          else:
            self.__errMsg( result[ 'Message' ] )
        else:
          gLogger.notice( self.do_show.__doc__ )
      else:
        gLogger.notice( self.do_show.__doc__ )
    else:
      gLogger.notice( "Unknown option:", option )
Пример #7
0
  def do_install( self, args ):
    """
        Install various DIRAC components

        usage:

          install mysql
          install db <database>
          install service <system> <service> [-m <ModuleName>] [-p <Option>=<Value>] [-p <Option>=<Value>] ...
          install agent <system> <agent> [-m <ModuleName>] [-p <Option>=<Value>] [-p <Option>=<Value>] ...
          install executor <system> <executor> [-m <ModuleName>] [-p <Option>=<Value>] [-p <Option>=<Value>] ...
    """
    argss = args.split()
    if not argss:
      gLogger.notice( self.do_install.__doc__ )
      return

    option = argss[0]
    del argss[0]
    if option == "mysql":
      gLogger.notice( "Installing MySQL database, this can take a while ..." )
      client = SystemAdministratorClient( self.host, self.port )
      if InstallTools.mysqlPassword == 'LocalConfig':
        InstallTools.mysqlPassword = ''
      InstallTools.getMySQLPasswords()
      result = client.installMySQL( InstallTools.mysqlRootPwd, InstallTools.mysqlPassword )
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        gLogger.notice( "MySQL:", result['Value'] )
        gLogger.notice( "You might need to restart SystemAdministrator service to take new settings into account" )
    elif option == "db":
      if not argss:
        gLogger.notice( self.do_install.__doc__ )
        return
      database = argss[0]
      client = SystemAdministratorClient( self.host, self.port )

      result = client.getAvailableDatabases()
      if not result['OK']:
        self.__errMsg( "Can not get database list: %s" % result['Message'] )
        return
      if not result['Value'].has_key( database ):
        self.__errMsg( "Unknown database %s: " % database )
        return
      system = result['Value'][database]['System']
      setup = gConfig.getValue( '/DIRAC/Setup', '' )
      if not setup:
        self.__errMsg( "Unknown current setup" )
        return
      instance = gConfig.getValue( '/DIRAC/Setups/%s/%s' % ( setup, system ), '' )
      if not instance:
        self.__errMsg( "No instance defined for system %s" % system )
        self.__errMsg( "\tAdd new instance with 'add instance %s <instance_name>'" % system )
        return

      if not InstallTools.mysqlPassword:
        InstallTools.mysqlPassword = '******'
      InstallTools.getMySQLPasswords()
      result = client.installDatabase( database, InstallTools.mysqlRootPwd )
      if not result['OK']:
        self.__errMsg( result['Message'] )
        return
      extension, system = result['Value']

      result = client.getHostInfo()
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return
      else:
        cpu = result[ 'Value' ][ 'CPUModel' ]
      hostname = self.host
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return

      if database != 'InstalledComponentsDB':
        result = MonitoringUtilities.monitorInstallation( 'DB', system.replace( 'System', '' ), database, cpu = cpu, hostname = hostname )
        if not result['OK']:
          self.__errMsg( result['Message'] )
          return
      # result = client.addDatabaseOptionsToCS( system, database )
      InstallTools.mysqlHost = self.host
      result = client.getInfo()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      hostSetup = result['Value']['Setup']
      result = InstallTools.addDatabaseOptionsToCS( gConfig, system, database, hostSetup, overwrite = True )
      if not result['OK']:
        self.__errMsg( result['Message'] )
        return
      gLogger.notice( "Database %s from %s/%s installed successfully" % ( database, extension, system ) )
    elif option in self.runitComponents:
      if len( argss ) < 2:
        gLogger.notice( self.do_install.__doc__ )
        return

      system = argss[0]
      del argss[0]
      component = argss[0]
      del argss[0]
      
      specialOptions = {}
      module = ''
      for i in range(len(argss)):
        if argss[i] == "-m":
          specialOptions['Module'] = argss[i+1]
          module = argss[i+1]
        if argss[i] == "-p":
          opt,value = argss[i+1].split('=')
          specialOptions[opt] = value           
      if module == component:
        module = ''
      
      client = SystemAdministratorClient( self.host, self.port )
      # First need to update the CS
      # result = client.addDefaultOptionsToCS( option, system, component )
      InstallTools.host = self.host
      result = client.getInfo()
      if not result['OK']:
        self.__errMsg( result['Message'] )
        return
      hostSetup = result['Value']['Setup']
      
      # Install Module section if not yet there
      if module:
        result = InstallTools.addDefaultOptionsToCS( gConfig, option, system, module, 
                                                     getCSExtensions(), hostSetup )
        # Add component section with specific parameters only
        result = InstallTools.addDefaultOptionsToCS( gConfig, option, system, component, 
                                                     getCSExtensions(), hostSetup, specialOptions, 
                                                     addDefaultOptions = False )
      else:  
        # Install component section
        result = InstallTools.addDefaultOptionsToCS( gConfig, option, system, component, 
                                                     getCSExtensions(), hostSetup, specialOptions )
    
      if not result['OK']:
        self.__errMsg( result['Message'] )
        return
      # Then we can install and start the component
      result = client.setupComponent( option, system, component, module )
      if not result['OK']:
        self.__errMsg( result['Message'] )
        return
      compType = result['Value']['ComponentType']
      runit = result['Value']['RunitStatus']
      gLogger.notice( "%s %s_%s is installed, runit status: %s" % ( compType, system, component, runit ) )

      # And register it in the database
      result = client.getHostInfo()
      if not result[ 'OK' ]:
        self.__errMsg( result[ 'Message' ] )
        return
      else:
        cpu = result[ 'Value' ][ 'CPUModel' ]
      hostname = self.host
      if component == 'ComponentMonitoring':
        result = MonitoringUtilities.monitorInstallation( 'DB', system, 'InstalledComponentsDB', cpu = cpu, hostname = hostname )
        if not result['OK']:
          self.__errMsg( 'Error registering installation into database: %s' % result[ 'Message' ] )
          return
      result = MonitoringUtilities.monitorInstallation( option, system, component, module, cpu = cpu, hostname = hostname )
      if not result['OK']:
        self.__errMsg( 'Error registering installation into database: %s' % result[ 'Message' ] )
        return
    else:
      gLogger.notice( "Unknown option:", option )
Пример #8
0
  def do_show( self, args ):
    """
        Show list of components with various related information

        usage:

          show software      - show components for which software is available
          show installed     - show components installed in the host with runit system
          show setup         - show components set up for automatic running in the host
          show project       - show project to install or upgrade
          show status        - show status of the installed components
          show database      - show status of the databases
          show mysql         - show status of the MySQL server
          show log  <system> <service|agent> [nlines]
                             - show last <nlines> lines in the component log file
          show info          - show version of software and setup
          show host          - show host related parameters
          show hosts         - show all available hosts
          show installations [ list | current | -n <Name> | -h <Host> | -s <System> | -m <Module> | -t <Type> | -itb <InstallationTime before>
                              | -ita <InstallationTime after> | -utb <UnInstallationTime before> | -uta <UnInstallationTime after> ]*
                             - show all the installations of components that match the given parameters
          show errors [*|<system> <service|agent>]
                             - show error count for the given component or all the components
                               in the last hour and day
    """

    argss = args.split()
    if not argss:
      gLogger.notice( self.do_show.__doc__ )
      return

    option = argss[0]
    del argss[0]

    if option == 'software':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getSoftwareComponents()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        gLogger.notice( '' )
        pprint.pprint( result['Value'] )
    elif option == 'installed':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getInstalledComponents()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        gLogger.notice( '' )
        pprint.pprint( result['Value'] )
    elif option == 'setup':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getSetupComponents()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        gLogger.notice( '' )
        pprint.pprint( result['Value'] )
    elif option == 'project':
      result = SystemAdministratorClient( self.host, self.port ).getProject()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        gLogger.notice( "Current project is %s" % result[ 'Value' ] )
    elif option == 'status':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getOverallStatus()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        fields = ["System",'Name','Module','Type','Setup','Installed','Runit','Uptime','PID']
        records = []
        rDict = result['Value']
        for compType in rDict:
          for system in rDict[compType]:
            components = rDict[compType][system].keys()
            components.sort()
            for component in components:
              record = []
              if rDict[compType][system][component]['Installed']:
                module = str( rDict[compType][system][component]['Module'] )
                record += [ system,component,module,compType.lower()[:-1]]
                if rDict[compType][system][component]['Setup']:
                  record += ['Setup']
                else:
                  record += ['NotSetup']
                if rDict[compType][system][component]['Installed']:
                  record += ['Installed']
                else:
                  record += ['NotInstalled']
                record += [str( rDict[compType][system][component]['RunitStatus'] )]
                record += [str( rDict[compType][system][component]['Timeup'] )]
                record += [str( rDict[compType][system][component]['PID'] )]
                records.append(record)  
        printTable(fields,records)        
    elif option == 'database' or option == 'databases':
      client = SystemAdministratorClient( self.host, self.port )
      if not InstallTools.mysqlPassword:
        InstallTools.mysqlPassword = "******"
      InstallTools.getMySQLPasswords()
      result = client.getDatabases( InstallTools.mysqlRootPwd )
      if not result['OK']:
        self.__errMsg( result['Message'] )
        return
      resultSW = client.getAvailableDatabases()
      if not resultSW['OK']:
        self.__errMsg( resultSW['Message'] )
        return

      sw = resultSW['Value']
      installed = result['Value']
      gLogger.notice( '' )
      for db in sw:
        if db in installed:
          gLogger.notice( db.rjust( 25 ), ': Installed' )
        else:
          gLogger.notice( db.rjust( 25 ), ': Not installed' )
      if not sw:
        gLogger.notice( "No database found" )
    elif option == 'mysql':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getMySQLStatus()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      elif result['Value']:
        gLogger.notice( '' )
        for par, value in result['Value'].items():
          gLogger.notice( ( par.rjust( 28 ), ':', value ) )
      else:
        gLogger.notice( "No MySQL database found" )
    elif option == "log":
      self.getLog( argss )
    elif option == "info":
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getInfo()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:
        gLogger.notice( '' )
        gLogger.notice( "Setup:", result['Value']['Setup'] )
        gLogger.notice( "DIRAC version:", result['Value']['DIRAC'] )
        if result['Value']['Extensions']:
          for e, v in result['Value']['Extensions'].items():
            gLogger.notice( "%s version" % e, v )
        gLogger.notice( '' )
    elif option == "host":
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getHostInfo()
      if not result['OK']:
        self.__errMsg( result['Message'] )
      else:   
        gLogger.notice( '' )
        gLogger.notice( "Host info:" )
        gLogger.notice( '' )
        
        fields = ['Parameter','Value']
        records = []
        for key, value in result['Value'].items():
          records.append( [key, str( value ) ] )
          
        printTable( fields, records )  
    elif option == "hosts":
      client = ComponentMonitoringClient()
      result = client.getHosts( {}, False, False )
      if not result[ 'OK' ]:
        self.__errMsg( 'Error retrieving the list of hosts: %s' % ( result[ 'Message' ] ) )
      else:
        hostList = result[ 'Value' ]
        gLogger.notice( '' )
        gLogger.notice( ' ' + 'Host'.center( 32 ) + ' ' + 'CPU'.center( 34 ) + ' ' )
        gLogger.notice( ( '-' * 69 ) )
        for element in hostList:
          gLogger.notice( '|' + element[ 'HostName' ].center( 32 ) + '|' + element[ 'CPU' ].center( 34 ) + '|' )
        gLogger.notice( ( '-' * 69 ) )
        gLogger.notice( '' )
    elif option == "errors":
      self.getErrors( argss )
    elif option == "installations":
      self.getInstallations( argss )
    else:
      gLogger.notice( "Unknown option:", option )
Пример #9
0
  def do_uninstall( self, args ):
    """
        Uninstall a DIRAC component or host along with all its components

        usage:

          uninstall db <database>
          uninstall host <hostname>
          uninstall <-f ForceLogUninstall> <system> <component>
    """
    argss = args.split()
    if not argss:
      gLogger.notice( self.do_uninstall.__doc__ )
      return

    # Retrieve user uninstalling the component
    result = getProxyInfo()
    if not result[ 'OK' ]:
      self._errMsg( result[ 'Message'] )
    user = result[ 'Value' ][ 'username' ]

    option = argss[0]
    if option == 'db':
      del argss[0]
      if not argss:
        gLogger.notice( self.do_uninstall.__doc__ )
        return
      component = argss[0]
      client = SystemAdministratorClient( self.host, self.port )

      result = client.getHostInfo()
      if not result[ 'OK' ]:
        self._errMsg( result[ 'Message' ] )
        return
      else:
        cpu = result[ 'Value' ][ 'CPUModel' ]
      hostname = self.host
      result = client.getAvailableDatabases()
      if not result[ 'OK' ]:
        self._errMsg( result[ 'Message' ] )
        return
      system = result[ 'Value' ][ component ][ 'System' ]
      result = MonitoringUtilities.monitorUninstallation( system , component, hostname = hostname, cpu = cpu )
      if not result[ 'OK' ]:
        self._errMsg( result[ 'Message' ] )
        return

      result = client.uninstallDatabase( component )
      if not result[ 'OK' ]:
        self._errMsg( result[ 'Message' ] )
      else:
        gLogger.notice( "Successfully uninstalled %s" % ( component ) )
    elif option == 'host':
      del argss[0]
      if not argss:
        gLogger.notice( self.do_uninstall.__doc__ )
        return
      hostname = argss[0]

      client = ComponentMonitoringClient()
      result = client.hostExists( { 'HostName': hostname } )
      if not result[ 'OK' ]:
        self._errMsg( result[ 'Message' ] )
      else:
        if not result[ 'Value' ]:
          self._errMsg( 'Given host does not exist' )
        else:
          result = client.getHosts( {'HostName': hostname }, True, False )
          if not result[ 'OK' ]:
            self._errMsg( result[ 'Message' ] )
          else:
            host = result[ 'Value' ][0]
            # Remove every installation associated with the host
            for installation in host[ 'Installations' ]:
              result = client.removeInstallations( installation, {}, { 'HostName': hostname } )
              if not result[ 'OK' ]:
                self._errMsg( result[ 'Message' ] )
                break
            # Finally remove the host
            result = client.removeHosts( { 'HostName': hostname } )
            if not result[ 'OK' ]:
              self._errMsg( result[ 'Message' ] )
            else:
              gLogger.notice( 'Host %s was successfully removed' % hostname )
    else:
      if option == '-f':
        force = True
        del argss[0]
      else:
        force = False

      if len( argss ) != 2:
        gLogger.notice( self.do_uninstall.__doc__ )
        return

      system, component = argss
      client = SystemAdministratorClient( self.host, self.port )

      monitoringClient = ComponentMonitoringClient()
      result = monitoringClient.getInstallations( { 'Instance': component, 'UnInstallationTime': None },
                                                  { 'System': system },
                                                  { 'HostName': self.host }, True )
      if not result[ 'OK' ]:
        self._errMsg( result[ 'Message' ] )
        return
      if len( result[ 'Value' ] ) < 1:
        self._errMsg( "Given component does not exist" )
        return
      if len( result[ 'Value' ] ) > 1:
        self._errMsg( "Too many components match" )
        return

      removeLogs = False
      if force:
        removeLogs = True
      else:
        if result[ 'Value' ][0][ 'Component' ][ 'Type' ] in self.runitComponents:
          result = promptUser( 'Remove logs?', ['y', 'n'], 'n' )
          if result[ 'OK' ]:
            removeLogs = result[ 'Value' ] == 'y'

      result = client.uninstallComponent( system, component, removeLogs )
      if not result[ 'OK' ]:
        self._errMsg( result[ 'Message' ] )
      else:
        gLogger.notice( "Successfully uninstalled %s/%s" % ( system, component ) )

      result = client.getHostInfo()
      if not result[ 'OK' ]:
        self._errMsg( result[ 'Message' ] )
        return
      else:
        cpu = result[ 'Value' ][ 'CPUModel' ]
      hostname = self.host
      result = MonitoringUtilities.monitorUninstallation( system, component, hostname = hostname, cpu = cpu )
      if not result[ 'OK' ]:
        return result
Пример #10
0
  def do_show( self, args ):
    """
        Show list of components with various related information

        usage:

          show software      - show components for which software is available
          show installed     - show components installed in the host with runit system
          show setup         - show components set up for automatic running in the host
          show project       - show project to install or upgrade
          show status        - show status of the installed components
          show database      - show status of the databases
          show mysql         - show status of the MySQL server
          show log  <system> <service|agent> [nlines]
                             - show last <nlines> lines in the component log file
          show info          - show version of software and setup
          show doc <type> <system> <name>
                             - show documentation for a given service or agent
          show host          - show host related parameters
          show hosts         - show all available hosts
          show ports [host]  - show all ports used by a host. If no host is given, the host currently connected to is used
          show installations [ list | current | -n <Name> | -h <Host> | -s <System> | -m <Module> | -t <Type> | -itb <InstallationTime before>
                              | -ita <InstallationTime after> | -utb <UnInstallationTime before> | -uta <UnInstallationTime after> ]*
                             - show all the installations of components that match the given parameters
          show profile <system> <component> [ -s <size> | -h <host> | -id <initial date DD/MM/YYYY> | -it <initial time hh:mm>
                              | -ed <end date DD/MM/YYYY | -et <end time hh:mm> ]*
                             - show <size> log lines of profiling information for a component in the machine <host>
          show errors [*|<system> <service|agent>]
                             - show error count for the given component or all the components
                               in the last hour and day
    """

    argss = args.split()
    if not argss:
      gLogger.notice( self.do_show.__doc__ )
      return

    option = argss[0]
    del argss[0]

    if option == 'software':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getSoftwareComponents()
      if not result['OK']:
        self._errMsg( result['Message'] )
      else:
        gLogger.notice( '' )
        pprint.pprint( result['Value'] )
    elif option == 'installed':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getInstalledComponents()
      if not result['OK']:
        self._errMsg( result['Message'] )
      else:
        gLogger.notice( '' )
        pprint.pprint( result['Value'] )
    elif option == 'setup':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getSetupComponents()
      if not result['OK']:
        self._errMsg( result['Message'] )
      else:
        gLogger.notice( '' )
        pprint.pprint( result['Value'] )
    elif option == 'project':
      result = SystemAdministratorClient( self.host, self.port ).getProject()
      if not result['OK']:
        self._errMsg( result['Message'] )
      else:
        gLogger.notice( "Current project is %s" % result[ 'Value' ] )
    elif option == 'status':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getOverallStatus()
      if not result['OK']:
        self._errMsg( result['Message'] )
      else:
        fields = ["System",'Name','Module','Type','Setup','Installed','Runit','Uptime','PID']
        records = []
        rDict = result['Value']
        for compType in rDict:
          for system in rDict[compType]:
            components = rDict[compType][system].keys()
            components.sort()
            for component in components:
              record = []
              if rDict[compType][system][component]['Installed']:
                module = str( rDict[compType][system][component]['Module'] )
                record += [ system,component,module,compType.lower()[:-1]]
                if rDict[compType][system][component]['Setup']:
                  record += ['Setup']
                else:
                  record += ['NotSetup']
                if rDict[compType][system][component]['Installed']:
                  record += ['Installed']
                else:
                  record += ['NotInstalled']
                record += [str( rDict[compType][system][component]['RunitStatus'] )]
                record += [str( rDict[compType][system][component]['Timeup'] )]
                record += [str( rDict[compType][system][component]['PID'] )]
                records.append(record)
        printTable(fields,records)
    elif option == 'database' or option == 'databases':
      client = SystemAdministratorClient( self.host, self.port )
      if not gComponentInstaller.mysqlPassword:
        gComponentInstaller.mysqlPassword = "******"
      gComponentInstaller.getMySQLPasswords()
      result = client.getDatabases( gComponentInstaller.mysqlRootPwd )
      if not result['OK']:
        self._errMsg( result['Message'] )
        return
      resultSW = client.getAvailableDatabases()
      if not resultSW['OK']:
        self._errMsg( resultSW['Message'] )
        return

      sw = resultSW['Value']
      installed = result['Value']
      gLogger.notice( '' )
      for db in sw:
        if db in installed:
          gLogger.notice( db.rjust( 25 ), ': Installed' )
        else:
          gLogger.notice( db.rjust( 25 ), ': Not installed' )
      if not sw:
        gLogger.notice( "No database found" )
    elif option == 'mysql':
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getMySQLStatus()
      if not result['OK']:
        self._errMsg( result['Message'] )
      elif result['Value']:
        gLogger.notice( '' )
        for par, value in result['Value'].items():
          gLogger.notice( ( par.rjust( 28 ), ':', value ) )
      else:
        gLogger.notice( "No MySQL database found" )
    elif option == "log":
      self.getLog( argss )
    elif option == "info":
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getInfo()
      if not result['OK']:
        self._errMsg( result['Message'] )
      else:
        gLogger.notice( '' )
        gLogger.notice( "Setup:", result['Value']['Setup'] )
        gLogger.notice( "DIRAC version:", result['Value']['DIRAC'] )
        if result['Value']['Extensions']:
          for e, v in result['Value']['Extensions'].items():
            gLogger.notice( "%s version" % e, v )
        gLogger.notice( '' )
    elif option == "host":
      client = SystemAdministratorClient( self.host, self.port )
      result = client.getHostInfo()
      if not result['OK']:
        self._errMsg( result['Message'] )
      else:
        gLogger.notice( '' )
        gLogger.notice( "Host info:" )
        gLogger.notice( '' )

        fields = ['Parameter','Value']
        records = []
        for parameter in result['Value'].iteritems():
          if parameter[0] == 'Extension':
            extensions = parameter[1].split( ',' )
            for extension in extensions:
              extensionName, extensionVersion = extension.split( ':' )
              records.append( [ '%sVersion' % extensionName, str( extensionVersion ) ] )
          else:
            records.append( [ parameter[0], str( parameter[1] ) ] )

        printTable( fields, records )
    elif option == "hosts":
      client = ComponentMonitoringClient()
      result = client.getHosts( {}, False, False )
      if not result[ 'OK' ]:
        self._errMsg( 'Error retrieving the list of hosts: %s' % ( result[ 'Message' ] ) )
      else:
        hostList = result[ 'Value' ]
        gLogger.notice( '' )
        gLogger.notice( ' ' + 'Host'.center( 32 ) + ' ' + 'CPU'.center( 34 ) + ' ' )
        gLogger.notice( ( '-' * 69 ) )
        for element in hostList:
          gLogger.notice( '|' + element[ 'HostName' ].center( 32 ) + '|' + element[ 'CPU' ].center( 34 ) + '|' )
        gLogger.notice( ( '-' * 69 ) )
        gLogger.notice( '' )
    elif option == "ports":
      if not argss:
        client = SystemAdministratorClient( self.host )
      else:
        hostname = argss[0]
        del argss[0]

        client = ComponentMonitoringClient()
        result = client.hostExists( { 'HostName': hostname } )
        if not result[ 'OK' ]:
          self._errMsg( result[ 'Message' ] )
          return
        else:
          if not result[ 'Value' ]:
            self._errMsg( 'Given host does not exist' )
            return

        client = SystemAdministratorClient( hostname )

      result = client.getUsedPorts()
      if not result[ 'OK' ]:
        self._errMsg( result[ 'Message' ] )
        return
      pprint.pprint( result[ 'Value' ] )
    elif option == "errors":
      self.getErrors( argss )
    elif option == "installations":
      self.getInstallations( argss )
    elif option == "doc":
      if len( argss ) > 2:
        if argss[0] in [ 'service', 'agent' ]:
          compType = argss[0]
          compSystem = argss[1]
          compModule = argss[2]
          client = SystemAdministratorClient( self.host, self.port )
          result = client.getComponentDocumentation( compType, compSystem, compModule )
          if result[ 'OK' ]:
            gLogger.notice( result[ 'Value' ] )
          else:
            self._errMsg( result[ 'Message' ] )
        else:
          gLogger.notice( self.do_show.__doc__ )
      else:
        gLogger.notice( self.do_show.__doc__ )
    elif option == "profile":
      if len( argss ) > 1:
        system = argss[0]
        del argss[0]
        component = argss[0]
        del argss[0]

        component = '%s_%s' % ( system, component )

        argDict = { '-s': None, '-h': self.host, '-id': None, '-it': '00:00', '-ed': None, '-et': '00:00' }
        key = None
        for arg in argss:
          if not key:
            key = arg
          else:
            argDict[ key ] = arg
            key = None

        size = None
        try:
          if argDict[ '-s' ]:
            size = int( argDict[ '-s' ] )
        except ValueError as _ve:
          self._errMsg( 'Argument \'size\' must be an integer' )
          return
        host = argDict[ '-h' ]
        initialDate = argDict[ '-id' ]
        initialTime = argDict[ '-it' ]
        endingDate = argDict[ '-ed' ]
        endingTime = argDict[ '-et' ]

        if initialDate:
          initialDate = '%s %s' % ( initialDate, initialTime )
        else:
          initialDate = ''
        if endingDate:
          endingDate = '%s %s' % ( endingDate, endingTime )
        else:
          endingDate = ''

        client = MonitoringClient()
        if size:
          result = client.getLimitedData( host, component, size )
        else:
          result = client.getDataForAGivenPeriod( host, component, initialDate, endingDate )

        if result[ 'OK' ]:
          text = ''
          headers = [result['Value'][0].keys()]
          for header in headers:
            text += str( header ).ljust( 15 )
          gLogger.notice( text )
          for record in result[ 'Value' ]:
            for metric in record.itervalues():
              text += str( metric ).ljust( 15 )
            gLogger.notice( text )
        else:
          self._errMsg( result[ 'Message' ] )
      else:
        gLogger.notice( self.do_show.__doc__ )
    else:
      gLogger.notice( "Unknown option:", option )
Пример #11
0
  def do_install(self, args):
    """
        Install various DIRAC components

        usage:

          install db <databaseName>
          install service <system> <service> [-m <ModuleName>] [-p <Option>=<Value>] [-p <Option>=<Value>] ...
          install agent <system> <agent> [-m <ModuleName>] [-p <Option>=<Value>] [-p <Option>=<Value>] ...
          install executor <system> <executor> [-m <ModuleName>] [-p <Option>=<Value>] [-p <Option>=<Value>] ...
    """
    argss = args.split()
    if not argss:
      gLogger.notice(self.do_install.__doc__)
      return

    option = argss[0]
    del argss[0]

    # Databases
    if option == "db":
      if not argss:
        gLogger.notice(self.do_install.__doc__)
        return
      database = argss[0]
      client = SystemAdministratorClient(self.host, self.port)

      result = client.getAvailableDatabases()
      if not result['OK']:
        self._errMsg("Can not get database list: %s" % result['Message'])
        return
      if database not in result['Value']:
        self._errMsg("Unknown database %s: " % database)
        return
      system = result['Value'][database]['System']
      dbType = result['Value'][database]['Type']
      setup = gConfig.getValue('/DIRAC/Setup', '')
      if not setup:
        self._errMsg("Unknown current setup")
        return
      instance = gConfig.getValue('/DIRAC/Setups/%s/%s' % (setup, system), '')
      if not instance:
        self._errMsg("No instance defined for system %s" % system)
        self._errMsg("\tAdd new instance with 'add instance %s <instance_name>'" % system)
        return

      if dbType == 'MySQL':
        if not gComponentInstaller.mysqlPassword:
          gComponentInstaller.mysqlPassword = '******'
        gComponentInstaller.getMySQLPasswords()
        result = client.installDatabase(database, gComponentInstaller.mysqlRootPwd)
        if not result['OK']:
          self._errMsg(result['Message'])
          return
        extension, system = result['Value']

        result = client.getHostInfo()
        if not result['OK']:
          self._errMsg(result['Message'])
          return
        else:
          cpu = result['Value']['CPUModel']
        hostname = self.host
        if not result['OK']:
          self._errMsg(result['Message'])
          return

        if database != 'InstalledComponentsDB':
          result = MonitoringUtilities.monitorInstallation(
              'DB', system.replace('System', ''), database, cpu=cpu, hostname=hostname)
          if not result['OK']:
            self._errMsg(result['Message'])
            return

        gComponentInstaller.mysqlHost = self.host
        result = client.getInfo()
        if not result['OK']:
          self._errMsg(result['Message'])
        hostSetup = result['Value']['Setup']

      result = gComponentInstaller.addDatabaseOptionsToCS(gConfig, system, database, hostSetup, overwrite=True)
      if not result['OK']:
        self._errMsg(result['Message'])
        return
      gLogger.notice("Database %s from %s/%s installed successfully" % (database, extension, system))

    # DIRAC components
    elif option in self.runitComponents:
      if len(argss) < 2:
        gLogger.notice(self.do_install.__doc__)
        return

      system = argss[0]
      del argss[0]
      component = argss[0]
      del argss[0]

      specialOptions = {}
      module = ''

      for i in range(len(argss)):
        if argss[i] == "-m":
          specialOptions['Module'] = argss[i + 1]
          module = argss[i + 1]
        if argss[i] == "-p":
          opt, value = argss[i + 1].split('=')
          specialOptions[opt] = value
      if module == component:
        module = ''

      client = SystemAdministratorClient(self.host, self.port)
      # First need to update the CS
      # result = client.addDefaultOptionsToCS( option, system, component )
      gComponentInstaller.host = self.host
      result = client.getInfo()
      if not result['OK']:
        self._errMsg(result['Message'])
        return
      hostSetup = result['Value']['Setup']

      # Install Module section if not yet there
      if module:
        result = gComponentInstaller.addDefaultOptionsToCS(gConfig, option, system, module,
                                                           getCSExtensions(), hostSetup)
        # in case of Error we must stop, this can happen when the module name is wrong...
        if not result['OK']:
          self._errMsg(result['Message'])
          return
        # Add component section with specific parameters only
        result = gComponentInstaller.addDefaultOptionsToCS(gConfig, option, system, component,
                                                           getCSExtensions(), hostSetup, specialOptions,
                                                           addDefaultOptions=True)
      else:
        # Install component section
        result = gComponentInstaller.addDefaultOptionsToCS(gConfig, option, system, component,
                                                           getCSExtensions(), hostSetup, specialOptions)

      if not result['OK']:
        self._errMsg(result['Message'])
        return
      # Then we can install and start the component
      result = client.setupComponent(option, system, component, module)
      if not result['OK']:
        self._errMsg(result['Message'])
        return
      compType = result['Value']['ComponentType']
      runit = result['Value']['RunitStatus']
      gLogger.notice("%s %s_%s is installed, runit status: %s" % (compType, system, component, runit))

      # And register it in the database
      result = client.getHostInfo()
      if not result['OK']:
        self._errMsg(result['Message'])
        return
      else:
        cpu = result['Value']['CPUModel']
      hostname = self.host
      if component == 'ComponentMonitoring':
        # Make sure that the service is running before trying to use it
        nTries = 0
        maxTries = 5
        mClient = ComponentMonitoringClient()
        result = mClient.ping()
        while not result['OK'] and nTries < maxTries:
          time.sleep(3)
          result = mClient.ping()
          nTries = nTries + 1

        if not result['OK']:
          self._errMsg("ComponentMonitoring service taking too long to start, won't be logged into the database")
          return

        result = MonitoringUtilities.monitorInstallation(
            'DB', system, 'InstalledComponentsDB', cpu=cpu, hostname=hostname)
        if not result['OK']:
          self._errMsg('Error registering installation into database: %s' % result['Message'])
          return

      result = MonitoringUtilities.monitorInstallation(option, system, component, module, cpu=cpu, hostname=hostname)
      if not result['OK']:
        self._errMsg('Error registering installation into database: %s' % result['Message'])
        return
    else:
      gLogger.notice("Unknown option:", option)