def clean_cluster_config(self, clusterid):
     """clean cluster config."""
     session = database.current_session()
     session.query(Cluster).filter_by(
         id=clusterid).delete(synchronize_session='fetch')
     session.query(ClusterState).filter_by(
         id=clusterid).delete(synchronize_session='fetch')
    def _get_host_progress(cls, hostid):
        """Get Host Progress from database.

        .. notes::
           The function should be called in database session.
        """
        session = database.current_session()
        host = session.query(
            ClusterHost
        ).filter_by(id=hostid).first()
        if not host:
            logging.error(
                'there is no host for %s in ClusterHost', hostid)
            return None, None, None

        if not host.state:
            logging.error('there is no related HostState for %s',
                          hostid)
            return host.fullname, None, None

        return (
            host.fullname,
            host.state.state,
            Progress(host.state.progress,
                     host.state.message,
                     host.state.severity))
Example #3
0
 def clean_cluster_config(self, clusterid):
     """clean cluster config."""
     session = database.current_session()
     session.query(Cluster).filter_by(id=clusterid).delete(
         synchronize_session='fetch')
     session.query(ClusterState).filter_by(id=clusterid).delete(
         synchronize_session='fetch')
Example #4
0
 def updateHostConfig(self, hostid, config):
     '''update hsot config to db.'''
     session = database.current_session()
     host = session.query(ClusterHost).filter_by(id=hostid).first()
     if not host:
         return
     filtered_config = self.HOST_FILTER.filter(config)
     host.config = filtered_config
Example #5
0
    def get_cluster_config(self, clusterid):
        """Get cluster config from db."""
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if not cluster:
            return {}

        return self.GET_CLUSTER_FILTER.filter(cluster.config)
 def clean_host_config(self, hostid):
     """clean host config."""
     self.clean_host_installing_progress(hostid)
     session = database.current_session()
     session.query(ClusterHost).filter_by(
         id=hostid).delete(synchronize_session='fetch')
     session.query(HostState).filter_by(
         id=hostid).delete(synchronize_session='fetch')
Example #7
0
    def update_cluster_config(self, clusterid, config):
        """Update cluster config to db."""
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if not cluster:
            return

        cluster.config = self.UPDATE_CLUSTER_FILTER.filter(config)
Example #8
0
 def updateHostConfig(self, hostid, config):
     '''update hsot config to db.'''
     session = database.current_session()
     host = session.query(ClusterHost).filter_by(id=hostid).first()
     if not host:
         return
     filtered_config = self.HOST_FILTER.filter(config)
     host.config = filtered_config
Example #9
0
def update_switch_ips(switch_ips):
    """get updated switch ips."""
    session = database.current_session()
    switches = session.query(Switch).all()
    if switch_ips:
        return [switch.ip for switch in switches if switch.ip in switch_ips]
    else:
        return [switch.ip for switch in switches]
    def get_cluster_config(self, clusterid):
        """Get cluster config from db."""
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if not cluster:
            return {}

        return self.GET_CLUSTER_FILTER.filter(cluster.config)
    def get_host_config(self, hostid):
        """Get host config from db."""
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if not host:
            return {}

        return self.GET_HOST_FILTER.filter(host.config)
Example #12
0
    def get_host_config(self, hostid):
        """Get host config from db."""
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if not host:
            return {}

        return self.GET_HOST_FILTER.filter(host.config)
Example #13
0
 def getHostConfig(self, hostid):
     '''get host config from db.'''
     session = database.current_session()
     host = session.query(ClusterHost).filter_by(id=hostid).first()
     if host:
         return host.config
     else:
         return {}
    def update_cluster_config(self, clusterid, config):
        """Update cluster config to db."""
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if not cluster:
            return

        cluster.config = self.UPDATE_CLUSTER_FILTER.filter(config)
Example #15
0
 def getClusterConfig(self, clusterid):
     '''get cluster config from db.'''
     session = database.current_session()
     cluster = session.query(Cluster).filter_by(id=clusterid).first()
     if cluster:
         return cluster.config
     else:
         return {}
    def update_host_config(self, hostid, config):
        """Update host config to db."""
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if not host:
            return

        host.config = self.UPDATE_HOST_FILTER.filter(config)
 def get_host_config(self, hostid):
     """Get host config from db."""
     session = database.current_session()
     host = session.query(ClusterHost).filter_by(id=hostid).first()
     if host:
         return host.config
     else:
         return {}
 def get_cluster_config(self, clusterid):
     """Get cluster config from db."""
     session = database.current_session()
     cluster = session.query(Cluster).filter_by(id=clusterid).first()
     if cluster:
         return cluster.config
     else:
         return {}
Example #19
0
 def clean_host_config(self, hostid):
     """clean host config."""
     self.clean_host_installing_progress(hostid)
     session = database.current_session()
     session.query(ClusterHost).filter_by(id=hostid).delete(
         synchronize_session='fetch')
     session.query(HostState).filter_by(id=hostid).delete(
         synchronize_session='fetch')
Example #20
0
    def update_host_config(self, hostid, config):
        """Update host config to db."""
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if not host:
            return

        host.config = self.UPDATE_HOST_FILTER.filter(config)
    def _update_host_progress(cls, hostid, progress):
        """Update host progress to database.

        .. note::
           The function should be called in database session.
        """
        session = database.current_session()
        host = session.query(
            ClusterHost).filter_by(id=hostid).first()
        if not host:
            logging.error(
                'there is no host for %s in ClusterHost', hostid)
            return

        if not host.state:
            logging.error(
                'there is no related HostState for %s', hostid)
            return

        if host.state.state != 'INSTALLING':
            logging.error(
                'host %s is not in INSTALLING state',
                hostid)
            return

        if host.state.progress > progress.progress:
            logging.error(
                'host %s progress is not increased '
                'from %s to %s',
                hostid, host.state, progress)
            return

        if (
            host.state.progress == progress.progress and
            host.state.message == progress.message
        ):
            logging.info(
                'ignore update host %s progress %s to %s',
                hostid, progress, host.state)
            return

        host.state.progress = progress.progress
        host.state.message = progress.message
        if progress.severity:
            host.state.severity = progress.severity

        if host.state.progress >= 1.0:
            host.state.state = 'READY'

        if host.state.severity == 'ERROR':
            host.state.state = 'ERROR'

        if host.state.state != 'INSTALLING':
            host.mutable = True

        logging.debug(
            'update host %s state %s',
            hostid, host.state)
Example #22
0
def poll_switch(ip_addr, req_obj='mac', oper="SCAN"):
    """ Query switch and return expected result

        :param str ip_addr: switch ip address
        :param str req_obj: the object requested to query from switch
        :param str oper : the operation to query the switch(SCAN, GET, SET)
    """
    if not ip_addr:
        logging.error('No switch IP address is provided!')
        return

    #Retrieve vendor info from switch table
    session = database.current_session()
    switch = session.query(Switch).filter_by(ip=ip_addr).first()
    logging.info("pollswitch: %s", switch)
    if not switch:
        logging.error('no switch found for %s', ip_addr)
        return

    credential = switch.credential
    logging.error("pollswitch: credential %r", credential)
    vendor = switch.vendor
    hdmanager = HDManager()

    if not vendor or not hdmanager.is_valid_vendor(ip_addr,
                                                   credential, vendor):
        # No vendor found or vendor doesn't match queried switch.
        logging.debug('no vendor or vendor had been changed for switch %s',
                      switch)
        vendor = hdmanager.get_vendor(ip_addr, credential)
        logging.debug('[pollswitch] credential %r', credential)
        if not vendor:
            logging.error('no vendor found or match switch %s', switch)
            return
        switch.vendor = vendor

    # Start to poll switch's mac address.....
    logging.debug('hdmanager learn switch from %s %s %s %s %s',
                  ip_addr, credential, vendor, req_obj, oper)
    results = hdmanager.learn(ip_addr, credential, vendor, req_obj, oper)
    logging.info("pollswitch %s result: %s", switch, results)
    if not results:
        logging.error('no result learned from %s %s %s %s %s',
                      ip_addr, credential, vendor, req_obj, oper)
        return

    for entry in results:
        mac = entry['mac']
        machine = session.query(Machine).filter_by(mac=mac).first()
        if not machine:
            machine = Machine(mac=mac)
            machine.port = entry['port']
            machine.vlan = entry['vlan']
            machine.switch = switch

    logging.debug('update switch %s state to under monitoring', switch)
    switch.state = 'under_monitoring'
Example #23
0
    def update_adapters(self, adapters, roles_per_target_system):
        """Update adapter config to db."""
        session = database.current_session()
        session.query(Adapter).delete()
        session.query(Role).delete()
        for adapter in adapters:
            session.add(Adapter(**adapter))

        for _, roles in roles_per_target_system.items():
            for role in roles:
                session.add(Role(**role))
    def update_adapters(self, adapters, roles_per_target_system):
        """Update adapter config to db."""
        session = database.current_session()
        session.query(Adapter).delete()
        session.query(Role).delete()
        for adapter in adapters:
            session.add(Adapter(**adapter))

        for _, roles in roles_per_target_system.items():
            for role in roles:
                session.add(Role(**role))
Example #25
0
def update_switch_ips(switch_ips):
    """get updated switch ips."""
    session = database.current_session()
    switches = session.query(Switch).all()
    if switch_ips:
        return [
            switch.ip for switch in switches
            if switch.ip in switch_ips
        ]
    else:
        return [switch.ip for switch in switches]
Example #26
0
def update_cluster_hosts(cluster_hosts, cluster_filter=None, host_filter=None):
    """get updated clusters and hosts per cluster from cluster hosts."""
    session = database.current_session()
    os_versions = {}
    target_systems = {}
    updated_cluster_hosts = {}
    clusters = session.query(Cluster).all()
    for cluster in clusters:
        if cluster_hosts and (cluster.id not in cluster_hosts
                              and str(cluster.id) not in cluster_hosts
                              and cluster.name not in cluster_hosts):
            logging.debug('ignore cluster %s sinc it is not in %s', cluster.id,
                          cluster_hosts)
            continue

        adapter = cluster.adapter
        if not cluster.adapter:
            logging.error('there is no adapter for cluster %s', cluster.id)
            continue

        if cluster_filter and not cluster_filter(cluster):
            logging.debug('filter cluster %s', cluster.id)
            continue

        updated_cluster_hosts[cluster.id] = []
        os_versions[cluster.id] = adapter.os
        target_systems[cluster.id] = adapter.target_system

        if cluster.id in cluster_hosts:
            hosts = cluster_hosts[cluster.id]
        elif str(cluster.id) in cluster_hosts:
            hosts = cluster_hosts[str(cluster.id)]
        elif cluster.name in cluster_hosts:
            hosts = cluster_hosts[cluster.name]
        else:
            hosts = []

        if not hosts:
            hosts = [host.id for host in cluster.hosts]

        for host in cluster.hosts:
            if (host.id not in hosts and str(host.id) not in hosts
                    and host.hostname not in hosts):
                logging.debug('ignore host %s which is not in %s', host.id,
                              hosts)
                continue

            if host_filter and not host_filter(host):
                logging.debug('filter host %s', host.id)
                continue

            updated_cluster_hosts[cluster.id].append(host.id)

    return (updated_cluster_hosts, os_versions, target_systems)
Example #27
0
    def _update_cluster_progress(cls, clusterid):
        """Update cluster installing progress to database.

        .. note::
           The function should be called in the database session.
        """
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if not cluster:
            logging.error('there is no cluster for %s in Cluster', clusterid)
            return

        if not cluster.state:
            logging.error('there is no ClusterState for %s', clusterid)

        if cluster.state.state != 'INSTALLING':
            logging.error('cluster %s is not in INSTALLING state', clusterid)
            return

        cluster_progress = 0.0
        cluster_messages = {}
        cluster_severities = set([])
        hostids = []
        for host in cluster.hosts:
            if host.state:
                hostids.append(host.id)
                cluster_progress += host.state.progress
                if host.state.message:
                    cluster_messages[host.hostname] = host.state.message

                if host.state.severity:
                    cluster_severities.add(host.state.severity)

        cluster.state.progress = cluster_progress / len(hostids)
        cluster.state.message = '\n'.join([
            '%s: %s' % (hostname, message)
            for hostname, message in cluster_messages.items()
        ])
        for severity in ['ERROR', 'WARNING', 'INFO']:
            if severity in cluster_severities:
                cluster.state.severity = severity
                break

        if cluster.state.progress >= 1.0:
            cluster.state.state = 'READY'

        if cluster.state.severity == 'ERROR':
            cluster.state.state = 'ERROR'

        if cluster.state.state != 'INSTALLING':
            cluster.mutable = True

        logging.debug('update cluster %s state %s', clusterid, cluster.state)
    def clean_cluster_installing_progress(self, clusterid):
        """clean cluster installing progress."""
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if not cluster:
            return

        if cluster.state and cluster.state.state != 'UNINITIALIZED':
            cluster.mutable = False
            cluster.state.state = 'INSTALLING'
            cluster.state.progress = 0.0
            cluster.state.message = ''
            cluster.state.severity = 'INFO'
Example #29
0
    def clean_cluster_installing_progress(self, clusterid):
        """clean cluster installing progress."""
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if not cluster:
            return

        if cluster.state and cluster.state.state != 'UNINITIALIZED':
            cluster.mutable = False
            cluster.state.state = 'INSTALLING'
            cluster.state.progress = 0.0
            cluster.state.message = ''
            cluster.state.severity = 'INFO'
Example #30
0
 def update_switch_and_machines(self, switches, switch_machines):
     """update switches and machines."""
     session = database.current_session()
     session.query(Switch).delete(synchronize_session='fetch')
     session.query(Machine).delete(synchronize_session='fetch')
     for switch_data in switches:
         switch = Switch(**switch_data)
         logging.info('add switch %s', switch)
         session.add(switch)
         for machine_data in switch_machines.get(switch.ip, []):
             machine = Machine(**machine_data)
             logging.info('add machine %s under %s', machine, switch)
             machine.switch = switch
             session.add(machine)
    def reinstall_cluster(self, clusterid):
        """reinstall cluster."""
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if not cluster:
            return

        if not cluster.state:
            cluster.state = ClusterState()

        cluster.state.state = 'INSTALLING'
        cluster.mutable = False
        cluster.state.progress = 0.0
        cluster.state.message = ''
        cluster.state.severity = 'INFO'
Example #32
0
    def update_switch_filters(self, switch_filters):
        """update switch filters."""
        session = database.current_session()
        switch_filter_tuples = set([])
        session.query(SwitchConfig).delete(synchronize_session='fetch')
        for switch_filter in switch_filters:
            switch_filter_tuple = tuple(switch_filter.values())
            if switch_filter_tuple in switch_filter_tuples:
                logging.debug('ignore adding switch filter: %s', switch_filter)
                continue
            else:
                logging.debug('add switch filter: %s', switch_filter)
                switch_filter_tuples.add(switch_filter_tuple)

            session.add(SwitchConfig(**switch_filter))
Example #33
0
    def reinstall_cluster(self, clusterid):
        """reinstall cluster."""
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if not cluster:
            return

        if not cluster.state:
            cluster.state = ClusterState()

        cluster.state.state = 'INSTALLING'
        cluster.mutable = False
        cluster.state.progress = 0.0
        cluster.state.message = ''
        cluster.state.severity = 'INFO'
 def update_switch_and_machines(
     self, switches, switch_machines
 ):
     """update switches and machines."""
     session = database.current_session()
     session.query(Switch).delete(synchronize_session='fetch')
     session.query(Machine).delete(synchronize_session='fetch')
     for switch_data in switches:
         switch = Switch(**switch_data)
         logging.info('add switch %s', switch)
         session.add(switch)
         for machine_data in switch_machines.get(switch.ip, []):
             machine = Machine(**machine_data)
             logging.info('add machine %s under %s', machine, switch)
             machine.switch = switch
             session.add(machine)
    def update_switch_filters(self, switch_filters):
        """update switch filters."""
        session = database.current_session()
        switch_filter_tuples = set([])
        session.query(SwitchConfig).delete(synchronize_session='fetch')
        for switch_filter in switch_filters:
            switch_filter_tuple = tuple(switch_filter.values())
            if switch_filter_tuple in switch_filter_tuples:
                logging.debug('ignore adding switch filter: %s',
                              switch_filter)
                continue
            else:
                logging.debug('add switch filter: %s', switch_filter)
                switch_filter_tuples.add(switch_filter_tuple)

            session.add(SwitchConfig(**switch_filter))
def trigger_install(clusterid):
    """Deploy a given cluster.

    :param clusterid: the id of the cluster to deploy.
    :type clusterid: int

    .. note::
        The function should be called in database session.
    """
    session = database.current_session()
    cluster = session.query(Cluster).filter_by(id=clusterid).first()
    if not cluster:
        logging.error('no cluster found for %s', clusterid)
        return

    adapter = cluster.adapter
    if not adapter:
        logging.error('no proper adapter found for cluster %s', cluster.id)
        return

    if not cluster.state:
        cluster.state = ClusterState()

    if cluster.state.state and cluster.state.state != 'UNINITIALIZED':
        logging.error('ignore installing cluster %s since the state is %s',
                      cluster.id, cluster.state)
        return

    cluster.state.state = 'INSTALLING'
    hostids = [host.id for host in cluster.hosts]
    update_hostids = []
    for host in cluster.hosts:
        if not host.state:
            host.state = HostState()
        elif host.state.state and host.state.state != 'UNINITIALIZED':
            logging.info('ignore installing host %s sinc the state is %s',
                         host.id, host.state)
            continue

        host.state.state = 'INSTALLING'
        update_hostids.append(host.id)

    manager = ConfigManager()
    manager.update_cluster_and_host_configs(
        clusterid, hostids, update_hostids,
        adapter.os, adapter.target_system)
    manager.sync()
Example #37
0
def trigger_install(clusterid):
    '''trigger installer to start install for a given cluster.

    Args:
        clusterid: int, the id of the cluster.

    Returns:
        None
    '''
    manager = ConfigManager()
    session = database.current_session()
    cluster = session.query(Cluster).filter_by(id=clusterid).first()
    if not cluster:
        logging.error('no cluster found for %s', clusterid)
        return

    if not cluster.state:
        cluster.state = ClusterState()

    if cluster.state.state and cluster.state.state != 'UNINITIALIZED':
        logging.error('ignore installing cluster %s since the state is %s',
                      cluster.id, cluster.state)
        return

    cluster.state.state = 'INSTALLING'
    session.flush()
    adapter = cluster.adapter
    if not adapter:
        logging.error('no proper adapter found for cluster %s', cluster.id)
        return

    hostids = [host.id for host in cluster.hosts]
    update_hostids = []
    for host in cluster.hosts:
        if not host.state:
            host.state = HostState()
        elif host.state.state and host.state.state != 'UNINITIALIZED':
            logging.info('ignore installing host %s sinc eth state is %s',
                         host.id, host.state)
            continue

        host.state.state = 'INSTALLING'
        update_hostids.append(host.id)

    manager.updateClusterAndHostConfigs(clusterid, hostids, update_hostids,
                                        adapter.os, adapter.target_system)
    manager.sync()
Example #38
0
def trigger_install(clusterid):
    """Deploy a given cluster.

    :param clusterid: the id of the cluster to deploy.
    :type clusterid: int

    .. note::
        The function should be called in database session.
    """
    session = database.current_session()
    cluster = session.query(Cluster).filter_by(id=clusterid).first()
    if not cluster:
        logging.error('no cluster found for %s', clusterid)
        return

    adapter = cluster.adapter
    if not adapter:
        logging.error('no proper adapter found for cluster %s', cluster.id)
        return

    if not cluster.state:
        cluster.state = ClusterState()

    if cluster.state.state and cluster.state.state != 'UNINITIALIZED':
        logging.error('ignore installing cluster %s since the state is %s',
                      cluster.id, cluster.state)
        return

    cluster.state.state = 'INSTALLING'
    hostids = [host.id for host in cluster.hosts]
    update_hostids = []
    for host in cluster.hosts:
        if not host.state:
            host.state = HostState()
        elif host.state.state and host.state.state != 'UNINITIALIZED':
            logging.info('ignore installing host %s sinc the state is %s',
                         host.id, host.state)
            continue

        host.state.state = 'INSTALLING'
        update_hostids.append(host.id)

    manager = ConfigManager()
    manager.update_cluster_and_host_configs(clusterid, hostids, update_hostids,
                                            adapter.os, adapter.target_system)
    manager.sync()
Example #39
0
    def _update_host_progress(cls, hostid, progress):
        """Update host progress to database.

        .. note::
           The function should be called in database session.
        """
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if not host:
            logging.error('there is no host for %s in ClusterHost', hostid)
            return

        if not host.state:
            logging.error('there is no related HostState for %s', hostid)
            return

        if host.state.state != 'INSTALLING':
            logging.error('host %s is not in INSTALLING state', hostid)
            return

        if host.state.progress > progress.progress:
            logging.error('host %s progress is not increased '
                          'from %s to %s', hostid, host.state, progress)
            return

        if (host.state.progress == progress.progress
                and host.state.message == progress.message):
            logging.info('ignore update host %s progress %s to %s', hostid,
                         progress, host.state)
            return

        host.state.progress = progress.progress
        host.state.message = progress.message
        if progress.severity:
            host.state.severity = progress.severity

        if host.state.progress >= 1.0:
            host.state.state = 'READY'

        if host.state.severity == 'ERROR':
            host.state.state = 'ERROR'

        if host.state.state != 'INSTALLING':
            host.mutable = True

        logging.debug('update host %s state %s', hostid, host.state)
Example #40
0
def trigger_install(clusterid):
    '''trigger installer to start install for a given cluster.

    Args:
        clusterid: int, the id of the cluster.

    Returns:
        None
    '''
    manager = ConfigManager()
    session = database.current_session()
    cluster = session.query(Cluster).filter_by(id=clusterid).first()
    if not cluster:
        logging.error('no cluster found for %s', clusterid)
        return

    adapter = cluster.adapter
    if not adapter:
        logging.error('no proper adapter found for cluster %s', cluster.id)
        return

    if not cluster.state:
        cluster.state = ClusterState()

    if cluster.state.state and cluster.state.state != 'UNINITIALIZED':
        logging.error('ignore installing cluster %s since the state is %s',
                      cluster.id, cluster.state)
        return

    cluster.state.state = 'INSTALLING'
    hostids = [host.id for host in cluster.hosts]
    update_hostids = []
    for host in cluster.hosts:
        if not host.state:
            host.state = HostState()
        elif host.state.state and host.state.state != 'UNINITIALIZED':
            logging.info('ignore installing host %s sinc the state is %s',
                         host.id, host.state)
            continue

        host.state.state = 'INSTALLING'
        update_hostids.append(host.id)

    manager.updateClusterAndHostConfigs(clusterid, hostids, update_hostids,
                                        adapter.os, adapter.target_system)
    manager.sync()
Example #41
0
    def clean_host_installing_progress(self, hostid):
        """clean host intalling progress."""
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if not host:
            return

        log_dir = os.path.join(setting.INSTALLATION_LOGDIR, host.fullname, '')
        session.query(LogProgressingHistory).filter(
            LogProgressingHistory.pathname.startswith(log_dir)).delete(
                synchronize_session='fetch')
        if host.state and host.state.state != 'UNINITIALIZED':
            host.mutable = False
            host.state.state = 'INSTALLING'
            host.state.progress = 0.0
            host.state.message = ''
            host.state.severity = 'INFO'
Example #42
0
    def _get_host_progress(cls, hostid):
        """Get Host Progress from database.

        .. notes::
           The function should be called in database session.
        """
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if not host:
            logging.error('there is no host for %s in ClusterHost', hostid)
            return None, None, None

        if not host.state:
            logging.error('there is no related HostState for %s', hostid)
            return host.fullname, None, None

        return (host.fullname, host.state.state,
                Progress(host.state.progress, host.state.message,
                         host.state.severity))
    def clean_host_installing_progress(self, hostid):
        """clean host intalling progress."""
        session = database.current_session()
        host = session.query(ClusterHost).filter_by(id=hostid).first()
        if not host:
            return

        log_dir = os.path.join(
            setting.INSTALLATION_LOGDIR,
            host.fullname,
            '')
        session.query(LogProgressingHistory).filter(
            LogProgressingHistory.pathname.startswith(
                log_dir)).delete(synchronize_session='fetch')
        if host.state and host.state.state != 'UNINITIALIZED':
            host.mutable = False
            host.state.state = 'INSTALLING'
            host.state.progress = 0.0
            host.state.message = ''
            host.state.severity = 'INFO'
    def get_switch_and_machines(self):
        """get switches and machines."""
        session = database.current_session()
        switches = session.query(Switch).all()
        switches_data = []
        switch_machines_data = {}
        for switch in switches:
            switches_data.append({
                'ip': switch.ip,
                'vendor_info': switch.vendor_info,
                'credential': switch.credential,
                'state': switch.state,
            })
            switch_machines_data[switch.ip] = []
            for machine in switch.machines:
                switch_machines_data[switch.ip].append({
                    'mac': machine.mac,
                    'port': machine.port,
                    'vlan': machine.vlan,
                })

        return switches_data, switch_machines_data
Example #45
0
    def get_switch_and_machines(self):
        """get switches and machines."""
        session = database.current_session()
        switches = session.query(Switch).all()
        switches_data = []
        switch_machines_data = {}
        for switch in switches:
            switches_data.append({
                'ip': switch.ip,
                'vendor_info': switch.vendor_info,
                'credential': switch.credential,
                'state': switch.state,
            })
            switch_machines_data[switch.ip] = []
            for machine in switch.machines:
                switch_machines_data[switch.ip].append({
                    'mac': machine.mac,
                    'port': machine.port,
                    'vlan': machine.vlan,
                })

        return switches_data, switch_machines_data
 def get_clusters(self):
     """get clusters."""
     session = database.current_session()
     clusters = session.query(Cluster).all()
     return [cluster.id for cluster in clusters]
 def get_cluster_hosts(self, clusterid):
     """get cluster hosts."""
     session = database.current_session()
     hosts = session.query(ClusterHost).filter_by(
         cluster_id=clusterid).all()
     return [host.id for host in hosts]
Example #48
0
    def test_session(self):
        with database.session() as session:
            self.assertEqual(database.current_session(), session)
            self.assertTrue(database.in_session())

        self.assertFalse(database.in_session())
Example #49
0
def poll_switch(ip_addr, req_obj='mac', oper="SCAN"):
    """Query switch and return expected result

    .. note::
       When polling switch succeeds, for each mac it got from polling switch,
       A Machine record associated with the switch is added to the database.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param req_obj: the object requested to query from switch.
    :type req_obj: str
    :param oper: the operation to query the switch.
    :type oper: str, should be one of ['SCAN', 'GET', 'SET']

    .. note::
       The function should be called inside database session scope.

    """
    if not ip_addr:
        logging.error('No switch IP address is provided!')
        return

    #Retrieve vendor info from switch table
    session = database.current_session()
    switch = session.query(Switch).filter_by(ip=ip_addr).first()
    logging.info("pollswitch: %s", switch)
    if not switch:
        logging.error('no switch found for %s', ip_addr)
        return

    credential = switch.credential
    logging.error("pollswitch: credential %r", credential)
    vendor = switch.vendor
    hdmanager = HDManager()

    if not vendor or not hdmanager.is_valid_vendor(ip_addr,
                                                   credential, vendor):
        # No vendor found or vendor doesn't match queried switch.
        logging.debug('no vendor or vendor had been changed for switch %s',
                      switch)
        vendor = hdmanager.get_vendor(ip_addr, credential)
        logging.debug('[pollswitch] credential %r', credential)
        if not vendor:
            logging.error('no vendor found or match switch %s', switch)
            return
        switch.vendor = vendor

    # Start to poll switch's mac address.....
    logging.debug('hdmanager learn switch from %s %s %s %s %s',
                  ip_addr, credential, vendor, req_obj, oper)
    results = hdmanager.learn(ip_addr, credential, vendor, req_obj, oper)
    logging.info("pollswitch %s result: %s", switch, results)
    if not results:
        logging.error('no result learned from %s %s %s %s %s',
                      ip_addr, credential, vendor, req_obj, oper)
        return

    for entry in results:
        mac = entry['mac']
        machine = session.query(Machine).filter_by(mac=mac).first()
        if not machine:
            machine = Machine(mac=mac)
            machine.port = entry['port']
            machine.vlan = entry['vlan']
            machine.switch = switch

    logging.debug('update switch %s state to under monitoring', switch)
    switch.state = 'under_monitoring'
Example #50
0
def trigger_install(clusterid, hostids=[]):
    """Deploy a given cluster.

    :param clusterid: the id of the cluster to deploy.
    :type clusterid: int
    :param hostids: the ids of the hosts to deploy.
    :type hostids: list of int

    .. note::
        The function should be called in database session.
    """
    logging.debug('trigger install cluster %s hosts %s',
                  clusterid, hostids)
    session = database.current_session()
    cluster = session.query(Cluster).filter_by(id=clusterid).first()
    if not cluster:
        logging.error('no cluster found for %s', clusterid)
        return

    adapter = cluster.adapter
    if not adapter:
        logging.error('no proper adapter found for cluster %s', cluster.id)
        return

    if cluster.mutable:
        logging.error('ignore installing cluster %s since it is mutable',
                      cluster)
        return

    if not cluster.state:
        cluster.state = ClusterState()

    cluster.state.state = 'INSTALLING'
    cluster.state.progress = 0.0
    cluster.state.message = ''
    cluster.state.severity = 'INFO'

    all_hostids = [host.id for host in cluster.hosts]
    update_hostids = []
    for host in cluster.hosts:
        if host.id not in hostids:
            logging.info('ignore installing %s since it is not in %s',
                         host, hostids)
            continue

        if host.mutable:
            logging.error('ignore installing %s since it is mutable',
                          host)
            continue

        log_dir = os.path.join(
            setting.INSTALLATION_LOGDIR,
            '%s.%s' % (host.hostname, clusterid))
        logging.info('clean log dir %s', log_dir)
        shutil.rmtree(log_dir, True)
        session.query(LogProgressingHistory).filter(
            LogProgressingHistory.pathname.startswith(
                '%s/' % log_dir)).delete(
            synchronize_session='fetch')

        if not host.state:
            host.state = HostState()

        host.state.state = 'INSTALLING'
        host.state.progress = 0.0
        host.state.message = ''
        host.state.severity = 'INFO'
        update_hostids.append(host.id)

    os.system('service rsyslog restart')

    manager = ConfigManager()
    manager.update_cluster_and_host_configs(
        clusterid, all_hostids, update_hostids,
        adapter.os, adapter.target_system)
    manager.sync()
Example #51
0
 def get_clusters(self):
     """get clusters."""
     session = database.current_session()
     clusters = session.query(Cluster).all()
     return [cluster.id for cluster in clusters]
Example #52
0
def poll_switch(ip_addr, req_obj='mac', oper="SCAN"):
    """Query switch and return expected result

    .. note::
       When polling switch succeeds, for each mac it got from polling switch,
       A Machine record associated with the switch is added to the database.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param req_obj: the object requested to query from switch.
    :type req_obj: str
    :param oper: the operation to query the switch.
    :type oper: str, should be one of ['SCAN', 'GET', 'SET']

    .. note::
       The function should be called inside database session scope.

    """
    if not ip_addr:
        logging.error('No switch IP address is provided!')
        return

    #Retrieve vendor info from switch table
    session = database.current_session()
    switch = session.query(Switch).filter_by(ip=ip_addr).first()
    logging.info("pollswitch: %s", switch)
    if not switch:
        logging.error('no switch found for %s', ip_addr)
        return

    credential = switch.credential
    logging.error("pollswitch: credential %r", credential)
    vendor = switch.vendor
    hdmanager = HDManager()

    if not vendor or not hdmanager.is_valid_vendor(ip_addr, credential,
                                                   vendor):
        # No vendor found or vendor doesn't match queried switch.
        logging.debug('no vendor or vendor had been changed for switch %s',
                      switch)
        vendor = hdmanager.get_vendor(ip_addr, credential)
        logging.debug('[pollswitch] credential %r', credential)
        if not vendor:
            logging.error('no vendor found or match switch %s', switch)
            return
        switch.vendor = vendor

    # Start to poll switch's mac address.....
    logging.debug('hdmanager learn switch from %s %s %s %s %s', ip_addr,
                  credential, vendor, req_obj, oper)
    results = hdmanager.learn(ip_addr, credential, vendor, req_obj, oper)
    logging.info("pollswitch %s result: %s", switch, results)
    if not results:
        logging.error('no result learned from %s %s %s %s %s', ip_addr,
                      credential, vendor, req_obj, oper)
        return

    for entry in results:
        mac = entry['mac']
        machine = session.query(Machine).filter_by(mac=mac).first()
        if not machine:
            machine = Machine(mac=mac)
            machine.port = entry['port']
            machine.vlan = entry['vlan']
            machine.switch = switch

    logging.debug('update switch %s state to under monitoring', switch)
    switch.state = 'under_monitoring'
Example #53
0
    def test_session(self):
        with database.session() as session:
            self.assertEqual(database.current_session(), session)
            self.assertTrue(database.in_session())

        self.assertFalse(database.in_session())
    def _update_cluster_progress(cls, clusterid):
        """Update cluster installing progress to database.

        .. note::
           The function should be called in the database session.
        """
        session = database.current_session()
        cluster = session.query(
            Cluster).filter_by(id=clusterid).first()
        if not cluster:
            logging.error(
                'there is no cluster for %s in Cluster',
                clusterid)
            return

        if not cluster.state:
            logging.error(
                'there is no ClusterState for %s',
                clusterid)

        if cluster.state.state != 'INSTALLING':
            logging.error('cluster %s is not in INSTALLING state',
                          clusterid)
            return

        cluster_progress = 0.0
        cluster_messages = {}
        cluster_severities = set([])
        hostids = []
        for host in cluster.hosts:
            if host.state:
                hostids.append(host.id)
                cluster_progress += host.state.progress
                if host.state.message:
                    cluster_messages[host.hostname] = host.state.message

                if host.state.severity:
                    cluster_severities.add(host.state.severity)

        cluster.state.progress = cluster_progress / len(hostids)
        cluster.state.message = '\n'.join(
            [
                '%s: %s' % (hostname, message)
                for hostname, message in cluster_messages.items()
            ]
        )
        for severity in ['ERROR', 'WARNING', 'INFO']:
            if severity in cluster_severities:
                cluster.state.severity = severity
                break

        if cluster.state.progress >= 1.0:
            cluster.state.state = 'READY'

        if cluster.state.severity == 'ERROR':
            cluster.state.state = 'ERROR'

        if cluster.state.state != 'INSTALLING':
            cluster.mutable = True

        logging.debug(
            'update cluster %s state %s',
            clusterid, cluster.state)
Example #55
0
def update_cluster_hosts(cluster_hosts,
                         cluster_filter=None, host_filter=None):
    """get updated clusters and hosts per cluster from cluster hosts."""
    session = database.current_session()
    os_versions = {}
    target_systems = {}
    updated_cluster_hosts = {}
    clusters = session.query(Cluster).all()
    for cluster in clusters:
        if cluster_hosts and (
            cluster.id not in cluster_hosts and
            str(cluster.id) not in cluster_hosts and
            cluster.name not in cluster_hosts
        ):
            logging.debug('ignore cluster %s sinc it is not in %s',
                          cluster.id, cluster_hosts)
            continue

        adapter = cluster.adapter
        if not cluster.adapter:
            logging.error('there is no adapter for cluster %s',
                          cluster.id)
            continue

        if cluster_filter and not cluster_filter(cluster):
            logging.debug('filter cluster %s', cluster.id)
            continue

        updated_cluster_hosts[cluster.id] = []
        os_versions[cluster.id] = adapter.os
        target_systems[cluster.id] = adapter.target_system

        if cluster.id in cluster_hosts:
            hosts = cluster_hosts[cluster.id]
        elif str(cluster.id) in cluster_hosts:
            hosts = cluster_hosts[str(cluster.id)]
        elif cluster.name in cluster_hosts:
            hosts = cluster_hosts[cluster.name]
        else:
            hosts = []

        if not hosts:
            hosts = [host.id for host in cluster.hosts]

        for host in cluster.hosts:
            if (
                host.id not in hosts and
                str(host.id) not in hosts and
                host.hostname not in hosts
            ):
                logging.debug('ignore host %s which is not in %s',
                              host.id, hosts)
                continue

            if host_filter and not host_filter(host):
                logging.debug('filter host %s', host.id)
                continue

            updated_cluster_hosts[cluster.id].append(host.id)

    return (updated_cluster_hosts, os_versions, target_systems)
Example #56
0
 def get_cluster_hosts(self, clusterid):
     """get cluster hosts."""
     session = database.current_session()
     hosts = session.query(ClusterHost).filter_by(
         cluster_id=clusterid).all()
     return [host.id for host in hosts]