def add_cluster(self, req, cluster_meta): """ Adds a new cluster to Daisy. :param req: The WSGI/Webob Request object :param image_meta: Mapping of metadata about cluster :raises HTTPBadRequest if x-cluster-name is missing """ self._enforce(req, 'add_cluster') cluster_name = cluster_meta["name"] if not cluster_name: raise ValueError('cluster name is null!') # target_systems = cluster_meta['target_systems'] # if not target_systems: # raise ValueError('target_systems is null!') if not cluster_meta.get('target_systems', None): cluster_meta['target_systems'] = "os" cluster_name_split = cluster_name.split('_') for cluster_name_info in cluster_name_split: if not cluster_name_info.isalnum(): raise ValueError( 'cluster name must be numbers or letters or underscores !') try: params = {"filters": dict(name=cluster_name)} cluster_name_repeat = \ registry.get_clusters_detail(req.context, **params) except Exception: pass else: if cluster_name_repeat: msg = _('cluster name [%s] is in use!') % cluster_name raise HTTPBadRequest(explanation=msg) if cluster_meta.get('nodes', None): orig_keys = list(cluster_meta['nodes']) for host_id in orig_keys: self._raise_404_if_host_deleted(req, host_id) node = registry.get_host_metadata(req.context, host_id) if node['status'] == 'in-cluster': msg = _("Forbidden to add host %s with status " "'in-cluster' in another cluster") % host_id raise HTTPForbidden(explanation=msg) if node.get('interfaces', None): interfaces = node['interfaces'] input_host_pxe_info = [ interface for interface in interfaces if interface.get('is_deployment', None) == 1 ] if not input_host_pxe_info and node.get('os_status', None) != 'active': msg = _( "The host %s has more than one dhcp server, " "please choose one interface for deployment") % \ host_id raise HTTPServerError(explanation=msg) print cluster_name print cluster_meta cluster_meta = registry.add_cluster_metadata(req.context, cluster_meta) return {'cluster_meta': cluster_meta}
def get_host_meta_or_404(self, request, host_id): """ Grabs the host metadata for an host with a supplied identifier or raises an HTTPNotFound (404) response :param request: The WSGI/Webob Request object :param host_id: The opaque host identifier :raises HTTPNotFound if host does not exist """ context = request.context try: return registry.get_host_metadata(context, host_id) except exception.NotFound: msg = "Host with identifier %s not found" % host_id LOG.debug(msg) raise webob.exc.HTTPNotFound(msg, request=request, content_type='text/plain') except exception.Forbidden: msg = "Forbidden host access" LOG.debug(msg) raise webob.exc.HTTPForbidden(msg, request=request, content_type='text/plain')
def add_cluster(self, req, cluster_meta): """ Adds a new cluster to Daisy. :param req: The WSGI/Webob Request object :param image_meta: Mapping of metadata about cluster :raises HTTPBadRequest if x-cluster-name is missing """ self._enforce(req, 'add_cluster') cluster_name = cluster_meta["name"] if not cluster_name: raise ValueError('cluster name is null!') # target_systems = cluster_meta['target_systems'] # if not target_systems: # raise ValueError('target_systems is null!') if not cluster_meta.get('target_systems', None): cluster_meta['target_systems'] = "os" cluster_name_split = cluster_name.split('_') for cluster_name_info in cluster_name_split: if not cluster_name_info.isalnum(): raise ValueError( 'cluster name must be numbers or letters or underscores !') try: params = {"filters": dict(name=cluster_name)} cluster_name_repeat = \ registry.get_clusters_detail(req.context, **params) except Exception: pass else: if cluster_name_repeat: msg = _('cluster name [%s] is in use!') % cluster_name raise HTTPBadRequest(explanation=msg) if cluster_meta.get('nodes', None): orig_keys = list(cluster_meta['nodes']) for host_id in orig_keys: self._raise_404_if_host_deleted(req, host_id) node = registry.get_host_metadata(req.context, host_id) if node['status'] == 'in-cluster': msg = _("Forbidden to add host %s with status " "'in-cluster' in another cluster") % host_id raise HTTPForbidden(explanation=msg) if node.get('interfaces', None): interfaces = node['interfaces'] input_host_pxe_info = [ interface for interface in interfaces if interface.get( 'is_deployment', None) == 1] if not input_host_pxe_info and node.get( 'os_status', None) != 'active': msg = _( "The host %s has more than one dhcp server, " "please choose one interface for deployment") % \ host_id raise HTTPServerError(explanation=msg) print cluster_name print cluster_meta cluster_meta = registry.add_cluster_metadata(req.context, cluster_meta) return {'cluster_meta': cluster_meta}
def get_host_detail(req, host_id): try: host_detail = registry.get_host_metadata(req.context, host_id) except exception.Invalid as e: raise HTTPBadRequest(explanation=e.msg, request=req) except exception.NotFound: msg = "Host with identifier %s not found", host_id LOG.debug(msg) raise HTTPNotFound(msg) return host_detail
def get_host_detail(req, host_id): try: host_detail = registry.get_host_metadata(req.context, host_id) except exception.Invalid as e: raise HTTPBadRequest(explanation=e.msg, request=req) except exception.NotFound: msg = "Host with identifier %s not found", host_id LOG.debug(msg) raise HTTPNotFound(msg) return host_detail
def upgrade_os(req, hosts_list): upgrade_hosts = [] max_parallel_os_upgrade_number = int(CONF.max_parallel_os_upgrade_number) while hosts_list: host_meta = {} threads = [] if len(hosts_list) > max_parallel_os_upgrade_number: upgrade_hosts = hosts_list[:max_parallel_os_upgrade_number] hosts_list = hosts_list[max_parallel_os_upgrade_number:] else: upgrade_hosts = hosts_list hosts_list = [] new_os_file = check_tfg_exist() for host_info in upgrade_hosts: host_id = host_info.keys()[0] host_ip = host_info.values()[0] host_detail = daisy_cmn.get_host_detail(req, host_id) target_host_os = _get_host_os_version(host_ip, host_detail['root_pwd']) if _cmp_os_version(new_os_file, target_host_os, host_ip) == 0: host_meta['os_progress'] = 10 host_meta['os_status'] = host_os_status['UPDATING'] host_meta['messages'] = "os updating,begin copy iso" update_db_host_status(req, host_id, host_meta) t = threading.Thread(target=os_thread_bin, args=(req, host_ip, host_id)) t.setDaemon(True) t.start() threads.append(t) else: LOG.warn(_("new os version is lower than or equal to that of " "host %s, don't need to upgrade!" % host_ip)) try: for t in threads: t.join() except: LOG.warn(_("Join update thread %s failed!" % t)) else: for host_info in upgrade_hosts: update_failed_flag = False host_id = host_info.keys()[0] host_ip = host_info.values()[0] host = registry.get_host_metadata(req.context, host_id) if host['os_status'] == host_os_status['UPDATE_FAILED'] or host['os_status'] == host_os_status['INIT']: update_failed_flag = True raise exception.ThreadBinException("%s update tfg failed! %s" % (host_ip, host['messages'])) if not update_failed_flag: host_meta = {} host_meta['os_progress'] = 100 host_meta['os_status'] = host_os_status['ACTIVE'] host_meta['messages'] = "os upgrade successfully" update_db_host_status(req, host_id,host_meta)
def _get_hosts_id_by_mgnt_ips(req, cluster_id, ips): params = {'cluster_id': cluster_id} hosts = registry.get_hosts_detail(req.context, **params) hosts_needed = [] for host in hosts: host_info = registry.get_host_metadata(req.context, host['id']) for interface in host_info['interfaces']: if interface.get('assigned_networks', None): assigned_networks = interface['assigned_networks'] for assigned_network in assigned_networks: if assigned_network['type'] == 'MANAGEMENT' and\ assigned_network['ip'] in ips: hosts_needed.append(host) hosts_id_needed = [host_needed['id'] for host_needed in hosts_needed] return hosts_id_needed
def _get_hosts_id_by_mgnt_ips(req, cluster_id, ips): params = {'cluster_id': cluster_id} hosts = registry.get_hosts_detail(req.context, **params) hosts_needed = [] for host in hosts: host_info = registry.get_host_metadata(req.context, host['id']) for interface in host_info['interfaces']: if interface.get('assigned_networks', None): assigned_networks = interface['assigned_networks'] for assigned_network in assigned_networks: if assigned_network['type'] == 'MANAGEMENT' and\ assigned_network['ip'] in ips: hosts_needed.append(host) hosts_id_needed = [host_needed['id'] for host_needed in hosts_needed] return hosts_id_needed
def get_host_meta_or_404(self, request, host_id): """ Grabs the host metadata for an host with a supplied identifier or raises an HTTPNotFound (404) response :param request: The WSGI/Webob Request object :param host_id: The opaque host identifier :raises HTTPNotFound if host does not exist """ context = request.context try: return registry.get_host_metadata(context, host_id) except exception.NotFound: msg = "Host with identifier %s not found" % host_id LOG.debug(msg) raise webob.exc.HTTPNotFound( msg, request=request, content_type='text/plain') except exception.Forbidden: msg = "Forbidden host access" LOG.debug(msg) raise webob.exc.HTTPForbidden(msg, request=request, content_type='text/plain')
def get_host_detail(req, host_id): try: host_detail = registry.get_host_metadata(req.context, host_id) except exception.Invalid as e: raise HTTPBadRequest(explanation=e.msg, request=req) return host_detail
def get_cluster_kolla_config(req, cluster_id): LOG.info(_("get kolla config from database...")) mgt_ip_list = set() kolla_config = {} controller_ip_list = [] computer_ip_list = [] storage_ip_list = [] mgt_macname_list = [] pub_macname_list = [] dat_macname_list = [] ext_macname_list = [] sto_macname_list = [] hbt_macname_list = [] enable_dvs_list = [] vlans_id = {} openstack_version = '3.0.0' docker_namespace = 'kolla' host_name_ip = {} host_name_ip_list = [] version_flag = False version_path = daisy_kolla_ver_path for parent, dirnames, filenames in os.walk(version_path): for filename in filenames: if filename.endswith('.version'): filename = version_path + filename for line in open(filename): if 'tag' in line: version_flag = True kolla_openstack_version = line.strip() openstack_version = kolla_openstack_version.split( "= ")[1] if version_flag == False: version_path = kolla_file + '/kolla-ansible/ansible/group_vars/' for parent, dirnames, filenames in os.walk(version_path): for filename in filenames: if filename == 'all.yml': filename = version_path + filename for line in open(filename): if 'openstack_release:' in line: version_flag = True kolla_openstack_version = line.strip() openstack_version = kolla_openstack_version.split( ": ")[1].strip('\"') LOG.info(_("openstack version is %s"), openstack_version) docker_registry_ip = _get_local_ip() docker_registry = docker_registry_ip + ':4000' LOG.info(_("get cluster network detail...")) cluster_networks = daisy_cmn.get_cluster_networks_detail(req, cluster_id) for network in cluster_networks: vlans_id.update({network.get('network_type'): network.get('vlan_id')}) all_roles = get_roles_detail(req) roles = [ role for role in all_roles if (role['cluster_id'] == cluster_id and role['deployment_backend'] == daisy_cmn.kolla_backend_name) ] for role in roles: if role['name'] == 'CONTROLLER_LB': kolla_vip = role['vip'] role_hosts = get_hosts_of_role(req, role['id']) for role_host in role_hosts: host_detail = get_host_detail(req, role_host['host_id']) deploy_host_cfg = get_controller_node_cfg( req, host_detail, cluster_networks) mgt_ip = deploy_host_cfg['mgtip'] host_name_ip = { deploy_host_cfg['host_name']: deploy_host_cfg['mgtip'] } controller_ip_list.append(mgt_ip) mgt_macname = deploy_host_cfg['mgt_macname'] pub_macname = deploy_host_cfg['pub_macname'] sto_macname = deploy_host_cfg['sto_macname'] hbt_macname = deploy_host_cfg.get('hbt_macname') mgt_macname_list.append(mgt_macname) pub_macname_list.append(pub_macname) sto_macname_list.append(sto_macname) hbt_macname_list.append(hbt_macname) if host_name_ip not in host_name_ip_list: host_name_ip_list.append(host_name_ip) if len(set(mgt_macname_list)) != 1 or \ len(set(pub_macname_list)) != 1 or \ len(set(sto_macname_list)) != 1 or \ len(set(hbt_macname_list)) > 1: msg = (_("hosts interface name of public and \ management and storage and heartbeat \ must be same!")) LOG.error(msg) raise HTTPForbidden(msg) kolla_config.update({'Version': openstack_version}) kolla_config.update({'Namespace': docker_namespace}) kolla_config.update({'VIP': kolla_vip}) kolla_config.update({'IntIfMac': mgt_macname}) kolla_config.update({'PubIfMac': pub_macname}) kolla_config.update({'StoIfMac': sto_macname}) kolla_config.update({'HbtIfMac': hbt_macname}) kolla_config.update({'LocalIP': docker_registry}) kolla_config.update({'Controller_ips': controller_ip_list}) kolla_config.update({'Network_ips': controller_ip_list}) #kolla_config.update({'Storage_ips': controller_ip_list}) kolla_config.update({'vlans_id': vlans_id}) if role['name'] == 'COMPUTER': role_hosts = get_hosts_of_role(req, role['id']) for role_host in role_hosts: host_detail = get_host_detail(req, role_host['host_id']) deploy_host_cfg = get_computer_node_cfg( req, host_detail, cluster_networks) mgt_ip = deploy_host_cfg['mgtip'] host_name_ip = { deploy_host_cfg['host_name']: deploy_host_cfg['mgtip'] } computer_ip_list.append(mgt_ip) if host_name_ip not in host_name_ip_list: host_name_ip_list.append(host_name_ip) dat_macname = deploy_host_cfg['dat_macname'] dat_macname_list.append(dat_macname) ext_macname = deploy_host_cfg['ext_macname'] ext_macname_list.append(ext_macname) if len(set(dat_macname_list)) != 1 or \ len(set(ext_macname_list)) != 1: msg = (_("computer hosts interface name of dataplane \ and external must be same!")) LOG.error(msg) raise HTTPForbidden(msg) kolla_config.update({'Computer_ips': computer_ip_list}) kolla_config.update({'TulIfMac': dat_macname}) kolla_config.update({'ExtIfMac': ext_macname}) mgt_ip_list = set(controller_ip_list + computer_ip_list) for ctl_host_ip in controller_ip_list: if len(storage_ip_list) > 2: break storage_ip_list.append(ctl_host_ip) for com_host_ip in computer_ip_list: if com_host_ip not in controller_ip_list: if len(storage_ip_list) > 2: break storage_ip_list.append(com_host_ip) kolla_config.update({'Storage_ips': storage_ip_list}) LOG.info("update enable_dvs config") params = {'cluster_id': cluster_id} hosts = registry.get_hosts_detail(req.context, **params) for host in hosts: host_detail = registry.get_host_metadata(req.context, host['id']) enable_dvs = False isolcpus = '' for interface in host_detail['interfaces']: vswitch_type = interface.get("vswitch_type") if vswitch_type == 'dvs': enable_dvs = True isolcpus = host_detail['isolcpus'] break enable_dvs_list.append(enable_dvs) if len(set(enable_dvs_list)) != 1: msg = (_("hosts interface vswitch type must be same!")) LOG.error(msg) raise HTTPForbidden(msg) else: isolcpus_hex = convert_string_to_hex(isolcpus) kolla_config.update({'enable_dvs': enable_dvs}) kolla_config.update({'ovs_dpdk_pmd_coremask': isolcpus_hex}) return (kolla_config, mgt_ip_list, host_name_ip_list)
def push_config(self): """ Push config to remote host. :param req: http req :param role_id: host role id :return: """ self.role_info = registry.get_role_metadata(self.context, self.role_id) if not self.role_info or not self.role_info.get('config_set_id'): LOG.error("<<<config_clushshell:push_config,get_role_metadata failed.>>>") return config_set = registry.get_config_set_metadata(self.context, self.role_info['config_set_id']) if not config_set or not config_set.has_key('config'): LOG.info("<<<config_clushshell:push_config,get_config_set_metadata failed.>>>") return config_set['config'] = \ [config for config in config_set['config'] if config.has_key('config_version') and config.has_key('running_version') and config['config_version'] != config['running_version']] if not config_set['config']: LOG.info('<<<No config need to be modified, within the scope of the hosts in role_id:%s.>>>' % self.role_id) return self.role_hosts = registry.get_role_host_metadata(self.context, self.role_id) current_count = 0 all_host_config_sets = [] for role_host in self.role_hosts: host = registry.get_host_metadata(self.context, role_host['host_id']) #change by 10166727--------start------------- host_ip=[] for interface in host['interfaces']: find_flag=interface['ip'].find(':') if find_flag<0: host_ip=[interface['ip']] else: ip_list_tmp=interface['ip'].split(",") for ip_list in ip_list_tmp: if ip_list.split(':')[0] == "MANAGEMENT": host_ip=[str(ip_list.split(':')[1])] #change by 10166727--------end--------------- if not host_ip: continue host_ip = host_ip[0] if 0 != subprocess.call('/var/lib/daisy/tecs/trustme.sh %s %s' % (host_ip, 'ossdbg1'), shell=True, stderr=subprocess.STDOUT): raise Exception("trustme.sh error!") if not config_set.has_key("config"): continue self._openstack_set_config(host_ip, config_set) all_host_config_sets.append(config_set) registry.update_configs_metadata_by_role_hosts(self.context, all_host_config_sets) LOG.debug("Update config for host:%s successfully!" % host_ip) self._host_service_restart(host_ip) current_count +=1 self.role_info['config_set_update_progress'] = round(current_count*1.0/len(self.role_hosts), 2)*100 registry.update_role_metadata(self.context, self.role_id, self.role_info)
def get_host_detail(req, host_id): try: host_detail = registry.get_host_metadata(req.context, host_id) except exception.Invalid as e: raise HTTPBadRequest(explanation=e.msg, request=req) return host_detail
def update_cluster(self, req, id, cluster_meta): """ Updates an existing cluster with the registry. :param request: The WSGI/Webob Request object :param id: The opaque cluster identifier :retval Returns the updated cluster information as a mapping """ self._enforce(req, 'update_cluster') if 'nodes' in cluster_meta: orig_keys = list(cluster_meta['nodes']) for host_id in orig_keys: self._raise_404_if_host_deleted(req, host_id) node = registry.get_host_metadata(req.context, host_id) if node['status'] == 'in-cluster': host_cluster = registry.get_host_clusters( req.context, host_id) if host_cluster[0]['cluster_id'] != id: msg = _("Forbidden to add host %s with status " "'in-cluster' in another cluster") % host_id raise HTTPForbidden(explanation=msg) if node.get('interfaces', None): interfaces = node['interfaces'] input_host_pxe_info = [ interface for interface in interfaces if interface.get( 'is_deployment', None) == 1] if not input_host_pxe_info and node.get( 'os_status', None) != 'active': msg = _( "The host %s has more than one dhcp server, " "please choose one interface for deployment") % \ host_id raise HTTPServerError(explanation=msg) if 'networks' in cluster_meta: orig_keys = list(cluster_meta['networks']) for network_id in orig_keys: self._raise_404_if_network_deleted(req, network_id) orig_cluster_meta = self.get_cluster_meta_or_404(req, id) # Do not allow any updates on a deleted cluster. # Fix for LP Bug #1060930 if orig_cluster_meta['deleted']: msg = _("Forbidden to update deleted cluster.") raise HTTPForbidden(explanation=msg, request=req, content_type="text/plain") try: cluster_meta = registry.update_cluster_metadata(req.context, id, cluster_meta) except exception.Invalid as e: msg = (_("Failed to update cluster metadata. Got error: %s") % utils.exception_to_str(e)) LOG.warning(msg) raise HTTPBadRequest(explanation=msg, request=req, content_type="text/plain") except exception.NotFound as e: msg = (_("Failed to find cluster to update: %s") % utils.exception_to_str(e)) LOG.warning(msg) raise HTTPNotFound(explanation=msg, request=req, content_type="text/plain") except exception.Forbidden as e: msg = (_("Forbidden to update cluster: %s") % utils.exception_to_str(e)) LOG.warning(msg) raise HTTPForbidden(explanation=msg, request=req, content_type="text/plain") except (exception.Conflict, exception.Duplicate) as e: LOG.warning(utils.exception_to_str(e)) raise HTTPConflict(body=_('Cluster operation conflicts'), request=req, content_type='text/plain') else: self.notifier.info('cluster.update', cluster_meta) return {'cluster_meta': cluster_meta}
def push_role_configs(self, role_id, push_status): """ Push config to remote host. :param req: http req :param role_id: host role id :return: """ role_info = registry.get_role_metadata(self.context, role_id) if not role_info.get('config_set_id'): LOG.info("<<<No config_set configed for role '%s'>>>" % role_info['name']) return config_set = registry.get_config_set_metadata( self.context, role_info['config_set_id']) if not config_set: LOG.info("<<<Get config_set failed for role '%s'.>>>" % role_info['name']) return else: if 'config' not in config_set: LOG.info("<<<No configs get for role '%s'.>>>" % role_info['name']) return config_set['config'] = [config for config in config_set['config'] if config.get('config_version', 0) != config.get('running_version', 0)] if not config_set['config']: LOG.info("<<<No config need to push for role '%s'.>>>" % role_info['name']) return self.role_hosts = registry.get_role_host_metadata( self.context, role_id) total_host_count = 0 if push_status: for r_host in self.role_hosts: if r_host['status'] == push_status: total_host_count += 1 else: total_host_count = len(self.role_hosts) if total_host_count > 0: LOG.info("Begin to push config for role '%s'" % role_info['name']) else: return current_count = 0 # all_host_config_sets = [] restart_failed_services = {} for role_host in self.role_hosts: host = registry.get_host_metadata( self.context, role_host['host_id']) if push_status and role_host['status'] != push_status: LOG.debug("<<<Status of host '%s' is not '%s'," " don't push configs.>>>" % (role_host['host_id'], push_status)) continue host_management_ip = '' for interface in host['interfaces']: if ('assigned_networks' in interface and interface['assigned_networks']): for assigned_network in interface['assigned_networks']: if (assigned_network['name'] == 'MANAGEMENT' and 'ip' in assigned_network): host_management_ip = assigned_network['ip'] if not host_management_ip: msg = "Can't find management ip for host %s"\ % role_host['host_id'] raise HTTPBadRequest(explanation=msg) root_passwd = 'ossdbg1' daisy_cmn.trust_me([host_management_ip], root_passwd) self._openstack_set_config(host_management_ip, config_set) failed_service = self._service_restart(role_info['service_name'], host_management_ip) if failed_service: if host_management_ip not in restart_failed_services: restart_failed_services.update({host_management_ip: []}) restart_failed_services[host_management_ip].extend( failed_service) current_count += 1 role_info['config_set_update_progress'] =\ round(current_count * 1.0 / total_host_count, 2) * 100 registry.update_role_metadata( self.context, role_id, role_info) all_config_sets = [] for config in config_set['config']: config['running_version'] = config['config_version'] all_config_sets.append(config_set) registry.update_configs_metadata_by_role_hosts( self.context, all_config_sets) if restart_failed_services: raise HTTPBadRequest("Services '%s' restart failed" % str(restart_failed_services))
def template_to_host(self, req, host_template): if not host_template.get('cluster_name', None): msg = "cluster name is null" raise HTTPNotFound(explanation=msg) params = {'filters':{'cluster_name':host_template['cluster_name']}} templates = registry.host_template_lists_metadata(req.context, **params) hosts_param = [] host_template_used = {} if templates and templates[0]: hosts_param = json.loads(templates[0]['hosts']) for host in hosts_param: if host['name'] == host_template['host_template_name']: host_template_used = host break if not host_template_used: msg = "not host_template %s" % host_template['host_template_name'] raise HTTPNotFound(explanation=msg, request=req, content_type="text/plain") if host_template.get('host_id', None): self.get_host_meta_or_404(req, host_template['host_id']) else: msg="host_id is not null" raise HTTPBadRequest(explanation = msg) host_id = host_template['host_id'] params = {'filters':{'name': host_template['cluster_name']}} clusters = registry.get_clusters_detail(req.context, **params) if clusters and clusters[0]: host_template_used['cluster'] = clusters[0]['id'] if host_template_used.has_key('role') and host_template_used['role']: role_id_list = [] host_role_list = [] if host_template_used.has_key('cluster'): params = self._get_query_params(req) role_list = registry.get_roles_detail(req.context, **params) for role_name in role_list: if role_name['cluster_id'] == host_template_used['cluster']: host_role_list = list(host_template_used['role']) if role_name['name'] in host_role_list: role_id_list.append(role_name['id']) host_template_used['role'] = role_id_list if host_template_used.has_key('name'): host_template_used.pop('name') if host_template_used.has_key('dmi_uuid'): host_template_used.pop('dmi_uuid') if host_template_used.has_key('ipmi_user'): host_template_used.pop('ipmi_user') if host_template_used.has_key('ipmi_passwd'): host_template_used.pop('ipmi_passwd') if host_template_used.has_key('ipmi_addr'): host_template_used.pop('ipmi_addr') host_template_interfaces = host_template_used.get('interfaces', None) if host_template_interfaces: template_ether_interface = [interface for interface in host_template_interfaces if interface['type'] == "ether" ] orig_host_meta = registry.get_host_metadata(req.context, host_id) orig_host_interfaces = orig_host_meta.get('interfaces', None) temp_orig_host_interfaces = [ interface for interface in orig_host_interfaces if interface['type'] == "ether" ] if len(temp_orig_host_interfaces) != len(template_ether_interface): msg = (_('host_id %s does not match the host_id host_template ' '%s.') % (host_id, host_template['host_template_name'])) raise HTTPBadRequest(explanation = msg) interface_match_flag = 0 for host_template_interface in host_template_interfaces: if host_template_interface['type'] == 'ether': for orig_host_interface in orig_host_interfaces: if orig_host_interface['pci'] == host_template_interface['pci']: interface_match_flag += 1 host_template_interface['mac'] = orig_host_interface['mac'] if host_template_interface.has_key('ip'): host_template_interface.pop('ip') if interface_match_flag != len(template_ether_interface): msg = (_('host_id %s does not match the host ' 'host_template %s.') % (host_id, host_template['host_template_name'])) raise HTTPBadRequest(explanation=msg) host_template_used['interfaces'] = str(host_template_interfaces) host_template = registry.update_host_metadata(req.context, host_id, host_template_used) return {"host_template": host_template}
def template_to_host(self, req, host_template): if not host_template.get('cluster_name', None): msg = "cluster name is null" raise HTTPNotFound(explanation=msg) if host_template.get('host_id', None): host_id = host_template['host_id'] orig_host_meta = self.get_host_meta_or_404(req, host_id) else: msg = "host id which need to template instantiate can't be null" raise HTTPBadRequest(explanation=msg) path = os.path.join( os.path.abspath(os.path.dirname(os.path.realpath(__file__))), 'ext') for root, dirs, names in os.walk(path): filename = 'router.py' if filename in names: ext_name = root.split(path)[1].strip('/') ext_func = "%s.api.hosts" % ext_name extension = importutils.import_module('daisy.api.v1.ext.%s' % ext_func) if 'template_to_host' in dir(extension): extension.template_to_host(req, orig_host_meta) params = {'filters': {'cluster_name': host_template['cluster_name']}} templates = registry.host_template_lists_metadata( req.context, **params) hosts_param = [] host_template_used = {} if templates and templates[0]: hosts_param = json.loads(templates[0]['hosts']) for host in hosts_param: if host['name'] == host_template['host_template_name']: host_template_used = host break if not host_template_used: msg = "not host_template %s" % host_template['host_template_name'] raise HTTPNotFound(explanation=msg, request=req, content_type="text/plain") params = {'filters': {'name': host_template['cluster_name']}} clusters = registry.get_clusters_detail(req.context, **params) if clusters and clusters[0]: host_template_used['cluster'] = clusters[0]['id'] if 'role' in host_template_used and host_template_used['role']: role_id_list = [] host_role_list = [] if 'cluster' in host_template_used: params = self._get_query_params(req) role_list = registry.get_roles_detail(req.context, **params) for role_name in role_list: if role_name['cluster_id'] == host_template_used[ 'cluster']: host_role_list = list(host_template_used['role']) if role_name['name'] in host_role_list: role_id_list.append(role_name['id']) host_template_used['role'] = role_id_list ignore_common_key_list = ["name", "dmi_uuid", "ipmi_addr"] for ignore_key in ignore_common_key_list: if host_template_used.get(ignore_key, None): host_template_used.pop(ignore_key) ssh_host_flag = self._judge_ssh_host(req, host_template_used['cluster'], host_id) ignore_ssh_key_list = [ "root_disk", "root_lv_size", "swap_lv_size", "isolcpus", "os_version_file", "os_version_id", "root_pwd", "hugepages", "hugepagesize", "discover_mode" ] if ssh_host_flag: for ignore_key in ignore_ssh_key_list: if host_template_used.get(ignore_key, None): host_template_used.pop(ignore_key) daisy_cmn.add_ssh_host_to_cluster_and_assigned_network( req, host_template_used['cluster'], host_id) # ssh host add cluster and assigned network,must get new host data. orig_host_meta = registry.get_host_metadata(req.context, host_id) else: if not host_template_used.get("root_disk", None): raise HTTPBadRequest( explanation="ssh host template can't be used by pxe host") host_template_used['os_status'] = "init" host_template_used['messages'] = "" host_template_used['os_progress'] = 0 host_template_used['description'] = "" host_template_interfaces = host_template_used.get('interfaces', None) if host_template_interfaces: template_ether_interface = [ interface for interface in host_template_interfaces if interface['type'] == "ether" ] template_bond_interface = [ interface for interface in host_template_interfaces if interface['type'] == "bond" ] orig_host_interfaces = orig_host_meta.get('interfaces', None) temp_orig_host_interfaces = [ interface for interface in orig_host_interfaces if interface['type'] == "ether" ] if len(temp_orig_host_interfaces) != len(template_ether_interface): msg = (_('host_id %s number of interface ' 'does not match host template' '%s.') % (host_id, host_template['host_template_name'])) raise HTTPBadRequest(explanation=msg) interface_match_flag = 0 host_template_interfaces = \ filter(lambda interface: 'vlan' != interface['type'], host_template_interfaces) for host_template_interface in host_template_interfaces: if host_template_interface['type'] == 'ether': for orig_host_interface in orig_host_interfaces: if orig_host_interface['name'] ==\ host_template_interface['name']: interface_match_flag += 1 host_template_interface['mac'] =\ orig_host_interface['mac'] if 'ip' in host_template_interface and\ ssh_host_flag: host_template_interface['ip'] =\ orig_host_interface['ip'] else: host_template_interface.pop('ip') if orig_host_interface.get('assigned_networks', None) and ssh_host_flag: host_template_interface['assigned_networks']\ = orig_host_interface['assigned_networks'] if host_template_interface['type'] == 'bond': for orig_host_interface in orig_host_interfaces: if orig_host_interface['name'] ==\ host_template_interface['name']: if ssh_host_flag: interface_match_flag += 1 interface_list = ["mac", "slave1", "slave2", "ip"] for interface_key in interface_list: if host_template_interface.get( interface_key, None) and ssh_host_flag: host_template_interface[interface_key]\ = orig_host_interface[interface_key] if 'ip' in host_template_interface and\ not ssh_host_flag: host_template_interface.pop('ip') if orig_host_interface.get('assigned_networks', None) and ssh_host_flag: host_template_interface['assigned_networks']\ = orig_host_interface['assigned_networks'] if host_template_interface['type'] == 'vlan': host_template_interfaces.remove(host_template_interface) if ssh_host_flag: vlan_interfaces = [] for orig_host_interface in orig_host_interfaces: if orig_host_interface['type'] == 'vlan': vlan_interfaces.append(orig_host_interface) host_template_interfaces.extend(vlan_interfaces) if interface_match_flag != (len(template_ether_interface) + len(template_bond_interface)): msg = (_('ssh discover host_id ' 'interface %s does not match the ' 'host_template %s.') % (host_id, host_template['host_template_name'])) raise HTTPBadRequest(explanation=msg) else: if interface_match_flag != len(template_ether_interface): msg = (_('host_id %s interface does not match the ' 'host_template %s.') % (host_id, host_template['host_template_name'])) raise HTTPBadRequest(explanation=msg) host_template_used['interfaces'] = host_template_interfaces try: host_template = registry.update_host_metadata( req.context, host_id, host_template_used) except Exception as e: raise HTTPBadRequest(e.message) return {"host_template": host_template}
def get_cluster_kolla_config(req, cluster_id): LOG.info(_("get kolla config from database...")) mgt_ip_list = set() kolla_config = {} controller_ip_list = [] computer_ip_list = [] storage_ip_list = [] mgt_macname_list = [] pub_macname_list = [] dat_macname_list = [] ext_macname_list = [] sto_macname_list = [] hbt_macname_list = [] enable_dvs_list = [] vlans_id = {} openstack_version = '3.0.0' docker_namespace = 'kolla' host_name_ip = {} host_name_ip_list = [] version_flag = False version_path = daisy_kolla_ver_path for parent, dirnames, filenames in os.walk(version_path): for filename in filenames: if filename.endswith('.version'): filename = version_path + filename for line in open(filename): if 'tag' in line: version_flag = True kolla_openstack_version = line.strip() openstack_version = kolla_openstack_version.split( "= ")[1] if version_flag == False: version_path = kolla_file + '/kolla-ansible/ansible/group_vars/' for parent, dirnames, filenames in os.walk(version_path): for filename in filenames: if filename == 'all.yml': filename = version_path + filename for line in open(filename): if 'openstack_release:' in line: version_flag = True kolla_openstack_version = line.strip() openstack_version = kolla_openstack_version.split( ": ")[1].strip('\"') LOG.info(_("openstack version is %s"), openstack_version) docker_registry_ip = _get_local_ip() docker_registry = docker_registry_ip + ':4000' LOG.info(_("get cluster network detail...")) cluster_networks = daisy_cmn.get_cluster_networks_detail(req, cluster_id) for network in cluster_networks: vlans_id.update({network.get('network_type'): network.get('vlan_id')}) all_roles = get_roles_detail(req) roles = [role for role in all_roles if (role['cluster_id'] == cluster_id and role['deployment_backend'] == daisy_cmn.kolla_backend_name)] for role in roles: if role['name'] == 'CONTROLLER_LB': kolla_vip = role['vip'] role_hosts = get_hosts_of_role(req, role['id']) for role_host in role_hosts: host_detail = get_host_detail( req, role_host['host_id']) deploy_host_cfg = get_controller_node_cfg( req, host_detail, cluster_networks) mgt_ip = deploy_host_cfg['mgtip'] host_name_ip = { deploy_host_cfg['host_name']: deploy_host_cfg['mgtip']} controller_ip_list.append(mgt_ip) mgt_macname = deploy_host_cfg['mgt_macname'] pub_macname = deploy_host_cfg['pub_macname'] sto_macname = deploy_host_cfg['sto_macname'] hbt_macname = deploy_host_cfg.get('hbt_macname') mgt_macname_list.append(mgt_macname) pub_macname_list.append(pub_macname) sto_macname_list.append(sto_macname) hbt_macname_list.append(hbt_macname) if host_name_ip not in host_name_ip_list: host_name_ip_list.append(host_name_ip) if len(set(mgt_macname_list)) != 1 or \ len(set(pub_macname_list)) != 1 or \ len(set(sto_macname_list)) != 1 or \ len(set(hbt_macname_list)) > 1: msg = (_("hosts interface name of public and \ management and storage and heartbeat \ must be same!")) LOG.error(msg) raise HTTPForbidden(msg) kolla_config.update({'Version': openstack_version}) kolla_config.update({'Namespace': docker_namespace}) kolla_config.update({'VIP': kolla_vip}) kolla_config.update({'IntIfMac': mgt_macname}) kolla_config.update({'PubIfMac': pub_macname}) kolla_config.update({'StoIfMac': sto_macname}) kolla_config.update({'HbtIfMac': hbt_macname}) kolla_config.update({'LocalIP': docker_registry}) kolla_config.update({'Controller_ips': controller_ip_list}) kolla_config.update({'Network_ips': controller_ip_list}) #kolla_config.update({'Storage_ips': controller_ip_list}) kolla_config.update({'vlans_id': vlans_id}) if role['name'] == 'COMPUTER': role_hosts = get_hosts_of_role(req, role['id']) for role_host in role_hosts: host_detail = get_host_detail( req, role_host['host_id']) deploy_host_cfg = get_computer_node_cfg( req, host_detail, cluster_networks) mgt_ip = deploy_host_cfg['mgtip'] host_name_ip = { deploy_host_cfg['host_name']: deploy_host_cfg['mgtip']} computer_ip_list.append(mgt_ip) if host_name_ip not in host_name_ip_list: host_name_ip_list.append(host_name_ip) dat_macname = deploy_host_cfg['dat_macname'] dat_macname_list.append(dat_macname) ext_macname = deploy_host_cfg['ext_macname'] ext_macname_list.append(ext_macname) if len(set(dat_macname_list)) != 1 or \ len(set(ext_macname_list)) != 1: msg = (_("computer hosts interface name of dataplane \ and external must be same!")) LOG.error(msg) raise HTTPForbidden(msg) kolla_config.update({'Computer_ips': computer_ip_list}) kolla_config.update({'TulIfMac': dat_macname}) kolla_config.update({'ExtIfMac': ext_macname}) mgt_ip_list = set(controller_ip_list + computer_ip_list) for ctl_host_ip in controller_ip_list: if len(storage_ip_list) > 2: break storage_ip_list.append(ctl_host_ip) for com_host_ip in computer_ip_list: if com_host_ip not in controller_ip_list: if len(storage_ip_list) > 2: break storage_ip_list.append(com_host_ip) kolla_config.update({'Storage_ips': storage_ip_list}) LOG.info("update enable_dvs config") params = {'cluster_id': cluster_id} hosts = registry.get_hosts_detail(req.context, **params) for host in hosts: host_detail = registry.get_host_metadata(req.context, host['id']) enable_dvs = False isolcpus = '' for interface in host_detail['interfaces']: vswitch_type = interface.get("vswitch_type") if vswitch_type == 'dvs': enable_dvs = True isolcpus = host_detail['isolcpus'] break enable_dvs_list.append(enable_dvs) if len(set(enable_dvs_list)) != 1: msg = (_("hosts interface vswitch type must be same!")) LOG.error(msg) raise HTTPForbidden(msg) else: isolcpus_hex = convert_string_to_hex(isolcpus) kolla_config.update({'enable_dvs': enable_dvs}) kolla_config.update({'ovs_dpdk_pmd_coremask': isolcpus_hex}) return (kolla_config, mgt_ip_list, host_name_ip_list)
def update_cluster(self, req, id, cluster_meta): """ Updates an existing cluster with the registry. :param request: The WSGI/Webob Request object :param id: The opaque cluster identifier :retval Returns the updated cluster information as a mapping """ self._enforce(req, 'update_cluster') if 'nodes' in cluster_meta: orig_keys = list(cluster_meta['nodes']) for host_id in orig_keys: self._raise_404_if_host_deleted(req, host_id) node = registry.get_host_metadata(req.context, host_id) if node['status'] == 'in-cluster': host_cluster = registry.get_host_clusters( req.context, host_id) if host_cluster[0]['cluster_id'] != id: msg = _("Forbidden to add host %s with status " "'in-cluster' in another cluster") % host_id raise HTTPForbidden(explanation=msg) if node.get('interfaces', None): interfaces = node['interfaces'] input_host_pxe_info = [ interface for interface in interfaces if interface.get('is_deployment', None) == 1 ] if not input_host_pxe_info and node.get('os_status', None) != 'active': msg = _( "The host %s has more than one dhcp server, " "please choose one interface for deployment") % \ host_id raise HTTPServerError(explanation=msg) if 'networks' in cluster_meta: orig_keys = list(cluster_meta['networks']) for network_id in orig_keys: self._raise_404_if_network_deleted(req, network_id) orig_cluster_meta = self.get_cluster_meta_or_404(req, id) # Do not allow any updates on a deleted cluster. # Fix for LP Bug #1060930 if orig_cluster_meta['deleted']: msg = _("Forbidden to update deleted cluster.") raise HTTPForbidden(explanation=msg, request=req, content_type="text/plain") try: cluster_meta = registry.update_cluster_metadata( req.context, id, cluster_meta) except exception.Invalid as e: msg = (_("Failed to update cluster metadata. Got error: %s") % utils.exception_to_str(e)) LOG.warning(msg) raise HTTPBadRequest(explanation=msg, request=req, content_type="text/plain") except exception.NotFound as e: msg = (_("Failed to find cluster to update: %s") % utils.exception_to_str(e)) LOG.warning(msg) raise HTTPNotFound(explanation=msg, request=req, content_type="text/plain") except exception.Forbidden as e: msg = (_("Forbidden to update cluster: %s") % utils.exception_to_str(e)) LOG.warning(msg) raise HTTPForbidden(explanation=msg, request=req, content_type="text/plain") except (exception.Conflict, exception.Duplicate) as e: LOG.warning(utils.exception_to_str(e)) raise HTTPConflict(body=_('Cluster operation conflicts'), request=req, content_type='text/plain') else: self.notifier.info('cluster.update', cluster_meta) return {'cluster_meta': cluster_meta}