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 setUp(self): super(TestAdapterAPI, self).setUp() with database.session() as session: adapters = [ Adapter(name='Centos_openstack', os='Centos', target_system='openstack'), Adapter(name='Ubuntu_openstack', os='Ubuntu', target_system='openstack') ] session.add_all(adapters) roles = [ Role(name='Control', target_system='openstack'), Role(name='Compute', target_system='openstack'), Role(name='Master', target_system='hadoop') ] session.add_all(roles)
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 sync_from_installers(): manager = config_manager.ConfigManager() adapters = manager.getAdapters() target_systems = set() roles_per_target_system = {} for adapter in adapters: target_systems.add(adapter['target_system']) for target_system in target_systems: roles_per_target_system[target_system] = manager.getRoles( target_system) with database.session() as session: session.query(Adapter).delete() session.query(Role).delete() for adapter in adapters: session.add(Adapter(**adapter)) for target_system, roles in roles_per_target_system.items(): for role in roles: session.add(Role(**role))
def setupDb(): """setup database.""" SECURITY_CONFIG = { "security": { "server_credentials": { "username": "******", "password": "******"}, "service_credentials": { "username": "******", "password": "******"}, "console_credentials": { "username": "******", "password": "******"} } } NET_CONFIG = { "networking": { "interfaces": { "management": { "ip_start": "10.120.8.100", "ip_end": "10.120.8.200", "netmask": "255.255.255.0", "gateway": "", "nic": "eth0", "promisc": 1 }, "tenant": { "ip_start": "192.168.10.100", "ip_end": "192.168.10.200", "netmask": "255.255.255.0", "gateway": "", "nic": "eth1", "promisc": 0 }, "public": { "ip_start": "12.145.68.100", "ip_end": "12.145.68.200", "netmask": "255.255.255.0", "gateway": "", "nic": "eth2", "promisc": 0 }, "storage": { "ip_start": "172.29.8.100", "ip_end": "172.29.8.200", "netmask": "255.255.255.0", "gateway": "", "nic": "eth3", "promisc": 0 } }, "global": { "nameservers": "8.8.8.8", "search_path": "ods.com", "gateway": "192.168.1.1", "proxy": "http://127.0.0.1:3128", "ntp_server": "127.0.0.1" } } } PAR_CONFIG = { "partition": "/home 20%;/tmp 10%;/var 30%;" } HOST_CONFIG = { "networking": { "interfaces": { "management": { "ip": "%s" }, "tenant": { "ip": "%s" } } }, "roles": ["base"] } print "Setting up DB ..." with database.session() as session: # populate switch_config switch_config = SwitchConfig(ip='192.168.1.10', filter_port='1') session.add(switch_config) # populate role table role = Role(name='compute', target_system='openstack') session.add(role) # Populate one adapter to DB adapter = Adapter(name='Centos_openstack', os='Centos', target_system='openstack') session.add(adapter) #Populate switches info to DB switches = [Switch(ip="192.168.2.1", credential={"version": "2c", "community": "public"}, vendor="huawei", state="under_monitoring"), Switch(ip="192.168.2.2", credential={"version": "2c", "community": "public"}, vendor="huawei", state="under_monitoring"), Switch(ip="192.168.2.3", credential={"version": "2c", "community": "public"}, vendor="huawei", state="under_monitoring"), Switch(ip="192.168.2.4", credential={"version": "2c", "community": "public"}, vendor="huawei", state="under_monitoring")] session.add_all(switches) # Populate machines info to DB machines = [ Machine(mac='00:0c:27:88:0c:a1', port='1', vlan='1', switch_id=1), Machine(mac='00:0c:27:88:0c:a2', port='2', vlan='1', switch_id=1), Machine(mac='00:0c:27:88:0c:a3', port='3', vlan='1', switch_id=1), Machine(mac='00:0c:27:88:0c:b1', port='1', vlan='1', switch_id=2), Machine(mac='00:0c:27:88:0c:b2', port='2', vlan='1', switch_id=2), Machine(mac='00:0c:27:88:0c:b3', port='3', vlan='1', switch_id=2), Machine(mac='00:0c:27:88:0c:c1', port='1', vlan='1', switch_id=3), Machine(mac='00:0c:27:88:0c:c2', port='2', vlan='1', switch_id=3), Machine(mac='00:0c:27:88:0c:c3', port='3', vlan='1', switch_id=3), Machine(mac='00:0c:27:88:0c:d1', port='1', vlan='1', switch_id=4), Machine(mac='00:0c:27:88:0c:d2', port='2', vlan='1', switch_id=4), ] session.add_all(machines) # Popluate clusters into DB """ a. cluster #1: a new machine will be added to it. b. cluster #2: a failed machine needs to be re-deployed. c. cluster #3: a new cluster with 3 hosts will be deployed. """ clusters_networking_config = [ {"networking": {"interfaces": {"management": {"ip_start": "10.120.1.100", "ip_end": "10.120.1.200"}, "tenant": {"ip_start": "192.168.1.100", "ip_end": "192.168.1.200"}, "public": {"ip_start": "12.145.1.100", "ip_end": "12.145.1.200"}, "storage": {"ip_start": "172.29.1.100", "ip_end": "172.29.1.200"}}}}, {"networking": {"interfaces": {"management": {"ip_start": "10.120.2.100", "ip_end": "10.120.2.200"}, "tenant": {"ip_start": "192.168.2.100", "ip_end": "192.168.2.200"}, "public": {"ip_start": "12.145.2.100", "ip_end": "12.145.2.200"}, "storage": {"ip_start": "172.29.2.100", "ip_end": "172.29.2.200"}}}} ] cluster_names = ['cluster_01', 'cluster_02'] for name, networking_config in zip(cluster_names, clusters_networking_config): nconfig = copy.deepcopy(NET_CONFIG) util.merge_dict(nconfig, networking_config) c = Cluster( name=name, adapter_id=1, security_config=json.dumps(SECURITY_CONFIG['security']), networking_config=json.dumps(nconfig['networking']), partition_config=json.dumps(PAR_CONFIG['partition'])) session.add(c) # Populate hosts to each cluster host_mips = ['10.120.1.100', '10.120.1.101', '10.120.1.102', '10.120.2.100', '10.120.2.101', '10.120.2.102'] host_tips = ['192.168.1.100', '192.168.1.101', '192.168.1.102', '192.168.2.100', '192.168.2.101', '192.168.2.102'] hosts_config = [] for mip, tip in zip(host_mips, host_tips): config = copy.deepcopy(HOST_CONFIG) config['networking']['interfaces']['management']['ip'] = mip config['networking']['interfaces']['tenant']['ip'] = tip hosts_config.append(json.dumps(config)) hosts = [ ClusterHost(hostname='host_01', machine_id=1, cluster_id=1, config_data=hosts_config[0]), ClusterHost(hostname='host_02', machine_id=2, cluster_id=1, config_data=hosts_config[1]), ClusterHost(hostname='host_03', machine_id=3, cluster_id=1, config_data=hosts_config[2]), ClusterHost(hostname='host_01', machine_id=4, cluster_id=2, config_data=hosts_config[3]), ClusterHost(hostname='host_02', machine_id=5, cluster_id=2, config_data=hosts_config[4]), ClusterHost(hostname='host_03', machine_id=6, cluster_id=2, config_data=hosts_config[5]) ] session.add_all(hosts) # Populate cluster state and host state cluster_states = [ ClusterState(id=1, state="READY", progress=1.0, message="Successfully!"), ClusterState(id=2, state="ERROR", progress=0.5, message="Failed!") ] session.add_all(cluster_states) host_states = [ HostState(id=1, state="READY", progress=1.0, message="Successfully!"), HostState(id=2, state="READY", progress=1.0, message="Successfully!"), HostState(id=3, state="READY", progress=1.0, message="Successfully!"), HostState(id=4, state="ERROR", progress=0.5, message="Failed!"), HostState(id=5, state="READY", progress=1.0, message="Successfully!"), HostState(id=6, state="ERROR", progress=1.0, message="Failed!") ] session.add_all(host_states)