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( models.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_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')
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 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 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_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)
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)
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( models.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)
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_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)
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'
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 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
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]
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( models.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)