def __init__(self, only_config=False):
        self.service_defs = {}
        self.services = {}

        if only_config:
            self.load_defaults_and_settings(only_config=True)
            return

        self.yumbase = yum.YumBase()
        is_root = os.geteuid() == 0
        self.yumbase.preconf.init_plugins = is_root
        self.yumbase.preconf.errorlevel = 0
        self.yumbase.preconf.debuglevel = 0
        self.yumbase.conf.cache = not(is_root)
        if is_root:
            try:
                locking.try_to_acquire_yum_lock(self.yumbase)
            except locking.CannotAcquireYumLockException as e:
                sys.stderr.write("Could not acquire yum lock : '%s'" % str(e))
                sys.exit(1)
        self.yumdeps = YumDeps(self.yumbase)

        self.load_defaults_and_settings(only_config=False)

        for name, service in self.services.iteritems():
            init_script = '/etc/init.d/%s' % name
            service_artefact = self.yumdeps.get_service_artefact(init_script)
            service['name'] = name
            if os.path.exists(init_script):
                service['init_script'] = init_script
            else:
                service['state_handling'] = 'serverside'
            if service_artefact:
                service['service_artefact'] = service_artefact
                toplevel_artefacts = self.yumdeps.get_all_whatrequires(
                    service_artefact)
                service['toplevel_artefacts'] = toplevel_artefacts
                service.setdefault('needs_artefacts', []).extend(
                    map(self.yumdeps.strip_version, filter(
                        self.artefacts_filter, self.yumdeps.get_all_requires([service_artefact]))))
                service['needs_artefacts'].extend(map(self.yumdeps.strip_version, filter(
                    self.artefacts_filter, toplevel_artefacts)))

        self.add_services_ignore()
        self.add_services_states()
        self.add_services_extra()

        self.handled_artefacts = [
            a for a in filter(self.artefacts_filter, self.yumdeps.requires.keys())]

        self.current_artefacts = self.yumdeps.requires.keys()

        self.next_artefacts = self.updates = {}
        self.yumdeps.load_all_updates()
        for a in filter(self.artefacts_filter, self.yumdeps.all_updates.keys()):
            self.updates[a] = self.yumdeps.all_updates[a]

        if len(self.updates):
            self.state = 'update_needed'
        else:
            self.state = 'uptodate'

        self.lockstate = self.get_lock_state()

        self.host = self.hostname = socket.gethostname().split('.', 1)[0]
        self.fqdn = socket.getfqdn()
        f = open('/proc/uptime', 'r')
        self.uptime = float(f.readline().split()[0])
        self.running_kernel = 'kernel/' + platform.uname()[2]
        self.latest_kernel = self.determine_latest_kernel()
        self.reboot_required_to_activate_latest_kernel = (self.running_kernel != self.latest_kernel
                                                          if self.latest_kernel else False)
        self.reboot_required_after_next_update = self.next_artefacts_need_reboot()
        if hasattr(self, 'settings') and self.settings.get('ssh_poll_max_seconds'):
            self.ssh_poll_max_seconds = self.settings.get(
                'ssh_poll_max_seconds')

        now = datetime.datetime.now()
        self.date = str(now)
        self.epoch = round(float(now.strftime('%s')))
        self.interface = {}
        for interface in netifaces.interfaces():
            if interface == 'lo':
                continue
            self.interface[interface] = []
            try:
                for link in netifaces.ifaddresses(interface)[netifaces.AF_INET]:
                    self.interface[interface].append(link['addr'])
            except:
                pass
            self.interface[interface] = ' '.join(self.interface[interface])

        self.pwd = os.getcwd()

        self.structure_keys = [key for key in self.__dict__.keys()
                               if key not in ['yumbase',
                                              'yumdeps',
                                              'service_defs',
                                              'artefacts_filter']]
Exemple #2
0
    def __init__(self, only_config=False):
        # Redirect stdout because some yum plugins will print to it and
        # break the json
        old_stdout = sys.stdout
        try:
            sys.stdout = open(os.devnull, "w")

            self.service_defs = {}
            self.services = {}

            if only_config:
                self.load_defaults_and_settings(only_config=True)
                return

            # As the called script executes the yadt-status.py with sudo,
            # this will nearly always be True
            is_root = os.geteuid() == 0
            self.yumbase = Status.initialize_yumbase(is_root)
            if is_root:
                try:
                    locking.try_to_acquire_yum_lock(self.yumbase)
                except locking.CannotAcquireYumLockException as e:
                    sys.stderr.write("Could not acquire yum lock : '%s'" % str(e))
                    sys.exit(1)
            self.yumdeps = YumDeps(self.yumbase)
            self.load_defaults_and_settings(only_config=False)
            self.setup_services()
            self.add_services_ignore()
            self.add_services_states()
            self.add_services_extra()
            self.handled_artefacts = [
                a for a in filter(self.artefacts_filter, self.yumdeps.requires.keys())]
            self.current_artefacts = self.yumdeps.requires.keys()
            self.next_artefacts = self.updates = {}
            self.yumdeps.load_all_updates()
            for a in filter(self.artefacts_filter, self.yumdeps.all_updates.keys()):
                self.updates[a] = self.yumdeps.all_updates[a]
            self.state = 'update_needed' if self.updates else 'uptodate'
            self.lockstate = self.get_lock_state()
            self.host = self.hostname = socket.gethostname().split('.', 1)[0]
            self.fqdn = socket.getfqdn()
            with open('/proc/uptime', 'r') as f:
                self.uptime = float(f.readline().split()[0])
            self.running_kernel = 'kernel/' + platform.uname()[2]
            self.latest_kernel = self.determine_latest_kernel()
            self.reboot_required_to_activate_latest_kernel = (self.running_kernel != self.latest_kernel
                                                              if self.latest_kernel else False)
            self.reboot_required_after_next_update = self.next_artefacts_need_reboot()
            if hasattr(self, 'settings') and self.settings.get('ssh_poll_max_seconds'):
                self.ssh_poll_max_seconds = self.settings.get(
                    'ssh_poll_max_seconds')

            now = datetime.datetime.now()
            self.date = str(now)
            self.epoch = round(float(now.strftime('%s')))
            self.interface = Status.setup_interfaces()
            self.pwd = os.getcwd()

            self.structure_keys = [key for key in self.__dict__.keys()
                                   if key not in ['yumbase',
                                                  'yumdeps',
                                                  'service_defs',
                                                  'artefacts_filter']]
        finally:
            sys.stdout = old_stdout
Exemple #3
0
    def __init__(self, only_config=False):
        self.service_defs = {}
        self.services = {}

        if only_config:
            self.load_defaults_and_settings(only_config=True)
            return

        self.yumbase = yum.YumBase()
        is_root = os.geteuid() == 0
        self.yumbase.preconf.init_plugins = is_root
        self.yumbase.preconf.errorlevel = 0
        self.yumbase.preconf.debuglevel = 0
        self.yumbase.conf.cache = not (is_root)
        if is_root:
            try:
                locking.try_to_acquire_yum_lock(self.yumbase)
            except locking.CannotAcquireYumLockException as e:
                sys.stderr.write("Could not acquire yum lock : '%s'" % str(e))
                sys.exit(1)
        self.yumdeps = YumDeps(self.yumbase)

        self.load_defaults_and_settings(only_config=False)

        for name, service in self.services.iteritems():
            init_script = '/etc/init.d/%s' % name
            service_artefact = self.yumdeps.get_service_artefact(init_script)
            service['name'] = name
            if os.path.exists(init_script):
                service['init_script'] = init_script
            else:
                service['state_handling'] = 'serverside'
            if service_artefact:
                service['service_artefact'] = service_artefact
                toplevel_artefacts = self.yumdeps.get_all_whatrequires(
                    service_artefact)
                service['toplevel_artefacts'] = toplevel_artefacts
                service.setdefault('needs_artefacts', []).extend(
                    map(
                        self.yumdeps.strip_version,
                        filter(
                            self.artefacts_filter,
                            self.yumdeps.get_all_requires([service_artefact
                                                           ]))))
                service['needs_artefacts'].extend(
                    map(self.yumdeps.strip_version,
                        filter(self.artefacts_filter, toplevel_artefacts)))

        self.add_services_ignore()
        self.add_services_states()
        self.add_services_extra()

        self.handled_artefacts = [
            a for a in filter(self.artefacts_filter,
                              self.yumdeps.requires.keys())
        ]

        self.current_artefacts = self.yumdeps.requires.keys()

        self.next_artefacts = self.updates = {}
        self.yumdeps.load_all_updates()
        for a in filter(self.artefacts_filter,
                        self.yumdeps.all_updates.keys()):
            self.updates[a] = self.yumdeps.all_updates[a]

        if len(self.updates):
            self.state = 'update_needed'
        else:
            self.state = 'uptodate'

        self.lockstate = self.get_lock_state()

        self.host = self.hostname = socket.gethostname().split('.', 1)[0]
        self.fqdn = socket.getfqdn()
        f = open('/proc/uptime', 'r')
        self.uptime = float(f.readline().split()[0])
        self.running_kernel = 'kernel/' + platform.uname()[2]
        self.latest_kernel = self.determine_latest_kernel()
        self.reboot_required_to_activate_latest_kernel = (
            self.running_kernel != self.latest_kernel
            if self.latest_kernel else False)
        self.reboot_required_after_next_update = self.next_artefacts_need_reboot(
        )
        if hasattr(self,
                   'settings') and self.settings.get('ssh_poll_max_seconds'):
            self.ssh_poll_max_seconds = self.settings.get(
                'ssh_poll_max_seconds')

        now = datetime.datetime.now()
        self.date = str(now)
        self.epoch = round(float(now.strftime('%s')))
        self.interface = {}
        for interface in netifaces.interfaces():
            if interface == 'lo':
                continue
            self.interface[interface] = []
            try:
                for link in netifaces.ifaddresses(interface)[
                        netifaces.AF_INET]:
                    self.interface[interface].append(link['addr'])
            except:
                pass
            self.interface[interface] = ' '.join(self.interface[interface])

        self.pwd = os.getcwd()

        self.structure_keys = [
            key for key in self.__dict__.keys() if key not in
            ['yumbase', 'yumdeps', 'service_defs', 'artefacts_filter']
        ]