Beispiel #1
0
    def _prepare_database(self, config_locals):
        """Prepare database."""
        with database.session() as session:
            adapters = {}
            for adapter_config in config_locals['ADAPTERS']:
                adapter = Adapter(**adapter_config)
                session.add(adapter)
                adapters[adapter_config['name']] = adapter

            roles = {}
            for role_config in config_locals['ROLES']:
                role = Role(**role_config)
                session.add(role)
                roles[role_config['name']] = role

            switches = {}
            for switch_config in config_locals['SWITCHES']:
                switch = Switch(**switch_config)
                session.add(switch)
                switches[switch_config['ip']] = switch

            machines = {}
            for switch_ip, machine_configs in (
                config_locals['MACHINES_BY_SWITCH'].items()
            ):
                for machine_config in machine_configs:
                    machine = Machine(**machine_config)
                    machines[machine_config['mac']] = machine
                    machine.switch = switches[switch_ip]
                    session.add(machine)

            clusters = {}
            for cluster_config in config_locals['CLUSTERS']:
                adapter_name = cluster_config['adapter']
                del cluster_config['adapter']
                cluster = Cluster(**cluster_config)
                clusters[cluster_config['name']] = cluster
                cluster.adapter = adapters[adapter_name]
                cluster.state = ClusterState(
                    state="INSTALLING", progress=0.0, message='')
                session.add(cluster)

            hosts = {}
            for cluster_name, host_configs in (
                config_locals['HOSTS_BY_CLUSTER'].items()
            ):
                for host_config in host_configs:
                    mac = host_config['mac']
                    del host_config['mac']
                    host = ClusterHost(**host_config)
                    hosts['%s.%s' % (
                        host_config['hostname'], cluster_name)] = host
                    host.machine = machines[mac]
                    host.cluster = clusters[cluster_name]
                    host.state = HostState(
                        state="INSTALLING", progress=0.0, message='')
                    session.add(host)
def prepare_database(config):
    with database.session() as session:
        adapters = {}
        for adapter_config in config["ADAPTERS"]:
            adapter = Adapter(**adapter_config)
            session.add(adapter)
            adapters[adapter_config["name"]] = adapter

        roles = {}
        for role_config in config["ROLES"]:
            role = Role(**role_config)
            session.add(role)
            roles[role_config["name"]] = role

        switches = {}
        for switch_config in config["SWITCHES"]:
            switch = Switch(**switch_config)
            session.add(switch)
            switches[switch_config["ip"]] = switch

        machines = {}
        for switch_ip, machine_configs in config["MACHINES_BY_SWITCH"].items():
            for machine_config in machine_configs:
                machine = Machine(**machine_config)
                machines[machine_config["mac"]] = machine
                machine.switch = switches[switch_ip]
                session.add(machine)

        clusters = {}
        for cluster_config in config["CLUSTERS"]:
            adapter_name = cluster_config["adapter"]
            del cluster_config["adapter"]
            cluster = Cluster(**cluster_config)
            clusters[cluster_config["name"]] = cluster
            cluster.adapter = adapters[adapter_name]
            cluster.state = ClusterState(state="INSTALLING", progress=0.0, message="")
            session.add(cluster)

        hosts = {}
        for cluster_name, host_configs in config["HOSTS_BY_CLUSTER"].items():
            for host_config in host_configs:
                mac = host_config["mac"]
                del host_config["mac"]
                host = ClusterHost(**host_config)
                hosts["%s.%s" % (host_config["hostname"], cluster_name)] = host
                host.machine = machines[mac]
                host.cluster = clusters[cluster_name]
                host.state = HostState(state="INSTALLING", progress=0.0, message="")
                session.add(host)