def generate_container_names(self, component_type, count, cluster): names, container_numbers = [], [] mid_name = _name.get(component_type) zk_opers = Container_ZkOpers() exists = zk_opers.check_containerCluster_exists(cluster) if not exists: for i in range(int(count)): container_name = 'd-%s-%s-n-%s' % (mid_name, cluster, str(i + 1)) names.append(container_name) else: containers = zk_opers.retrieve_container_list(cluster) for container in containers: container_value = zk_opers.retrieve_container_node_value( cluster, container) container_name = container_value.get('containerName') container_prefix, container_number = re.findall( '(.*-n-)(\d+)', container_name)[0] container_numbers.append(int(container_number)) max_number = max(container_numbers) if max_number < 4: max_number = 4 for i in range(int(count)): max_number += 1 add_container_name = container_prefix + str(max_number) names.append(add_container_name) return names
def get_container_name_from_zk(self, cluster, container_node): zkOper = Container_ZkOpers() container_info = zkOper.retrieve_container_node_value( cluster, container_node) inspect = container_info.get('inspect') con = Container_Model(inspect=inspect) return con.name()
def __get_params(self): """ two containers may be with a host_ip """ params, container_info, container_nodes = {}, {}, [] zkOper = Container_ZkOpers() if self.containers: for container in self.containers: container_node = self.container_opers.get_container_node_from_container_name( self.cluster, container) container_nodes.append(container_node) else: container_nodes = zkOper.retrieve_container_list(self.cluster) self.container_nodes = container_nodes for container_node in self.container_nodes: container_name_list = [] container_info = zkOper.retrieve_container_node_value( self.cluster, container_node) container_name = container_info.get('containerName') host_ip = container_info.get('hostIp') container_name_list.append(container_name) if host_ip in params: container_name_list.extend(params[host_ip]) params[host_ip] = container_name_list return params
def retrieve_container_status_from_containerName(self, container_name): cluster = get_containerClusterName_from_containerName(container_name) container_node = self.get_container_node_from_container_name( cluster, container_name) zkOper = Container_ZkOpers() status_value = zkOper.retrieve_container_status_value( cluster, container_node) return status_value
def write_container_status_by_containerName(self, container_name, record): containerClusterName = get_containerClusterName_from_containerName( container_name) container_node = self.get_container_node_from_container_name( containerClusterName, container_name) zkOper = Container_ZkOpers() zkOper.write_container_status(containerClusterName, container_node, record)
def __exclude_servers(self, cluster): host_ip_list = [] zk_opers = Container_ZkOpers() container_list = zk_opers.retrieve_container_list(cluster) for container in container_list: container_value = zk_opers.retrieve_container_node_value( cluster, container) host_ip = container_value.get('hostIp') host_ip_list.append(host_ip) return host_ip_list
def __update_cluser_info_to_zk(self, cluster, network_mode, component_type, component_container_cluster_config): sum_count = component_container_cluster_config.sum_count _container_cluster_info = {} _container_cluster_info.setdefault('containerClusterName', cluster) _container_cluster_info.setdefault('containerCount', sum_count) _container_cluster_info.setdefault('start_flag', Status.failed) zkOper = Container_ZkOpers() zkOper.write_container_cluster_info(_container_cluster_info)
def update_zk_info_when_process_complete(self, cluster, create_result='failed', error_msg=''): zkOper = Container_ZkOpers() _container_cluster_info = zkOper.retrieve_container_cluster_info( cluster) _container_cluster_info['start_flag'] = create_result _container_cluster_info['error_msg'] = error_msg _container_cluster_info['containerClusterName'] = cluster zkOper.write_container_cluster_info(_container_cluster_info)
def write_container_node_value_by_containerName(self, container_name, container_props): """only write container value and not write status value """ cluster = get_containerClusterName_from_containerName(container_name) container_node = self.get_container_node_from_container_name( cluster, container_name) zkOper = Container_ZkOpers() zkOper.write_container_node_value(cluster, container_node, container_props)
def __is_containers_started(self, container_cluster_name, sum_count): zkOper = Container_ZkOpers() container_list = zkOper.retrieve_container_list(container_cluster_name) if len(container_list) != sum_count: logging.info('container length:%s, sum_count :%s' % (len(container_list), sum_count)) return False logging.info('sum_count is created: %s' % sum_count) status = self.component_container_cluster_validator.container_cluster_status_validator( container_cluster_name) logging.info('cluster status: %s' % status) return status.get('status') == Status.started
def __create_cluser_info_to_zk(self, network_mode, component_type, component_container_cluster_config): containerCount = component_container_cluster_config.nodeCount containerClusterName = component_container_cluster_config.container_cluster_name use_ip = 'bridge' != network_mode _container_cluster_info = { 'containerCount': containerCount, 'containerClusterName': containerClusterName, 'type': component_type, 'isUseIp': use_ip } zkOper = Container_ZkOpers() zkOper.write_container_cluster_info(_container_cluster_info)
def do_when_remove_cluster(self): def check(): for container_node in self.container_nodes: container_status = zk_opers.retrieve_container_status_value( self.cluster, container_node) if container_status.get('status') != Status.destroyed: return return True zk_opers = Container_ZkOpers() ret = handleTimeout(check, (50, 4)) if not ret: raise CommonException( 'remove containers %s in containerCluster:%s failed' % (self.containers, self.cluster)) for container_node in self.container_nodes: logging.info('do delete container node :%s info in zookeeper' % container_node) zk_opers.delete_container_node(self.cluster, container_node) cluster_info = zk_opers.retrieve_container_cluster_info(self.cluster) node_count = cluster_info.get('containerCount') _node_count = int(node_count) - len(self.containers) cluster_info.update({'containerCount': _node_count}) cluster_info.update({'start_flag': Status.succeed}) zk_opers.write_container_cluster_info(cluster_info)
def get_container_node_from_container_name(self, cluster, container_name): con_node = '' zkOper = Container_ZkOpers() cluster_info = zkOper.retrieve_container_cluster_info(cluster) use_ip = cluster_info.get('isUseIp') if use_ip: container_node_list = zkOper.retrieve_container_list(cluster) for container_node in container_node_list: container_info = zkOper.retrieve_container_node_value( cluster, container_node) inspect = container_info.get('inspect') con = Container_Model(inspect=inspect) con_name = con.name() if container_name == con_name: con_node = container_node break else: con_node = container_name return con_node
def do_when_remove_cluster(self): zkOper = Container_ZkOpers() cluster_info = zkOper.retrieve_container_cluster_info(self.cluster) use_ip = cluster_info.get('isUseIp') if use_ip: container_ip_list = zkOper.retrieve_container_list(self.cluster) logging.info('container_ip_list:%s' % str(container_ip_list)) zkOper.recover_ips_to_pool(container_ip_list)
def write_container_node_info_to_zk(self, container_stat, containerProps): inspect = containerProps.get('inspect') is_use_ip = containerProps.get('isUseIp') con = Container_Model(inspect=inspect) container_name = con.name() cluster = con.cluster(container_name) logging.info('get container cluster :%s' % cluster) if is_use_ip: container_node = con.ip() logging.info('get container ip :%s' % container_node) if not (container_node and cluster): raise CommonException( 'get container ip or cluster name failed, not write this info, inspect:%s' % (inspect)) container_node = container_node else: container_node = container_name zkOper = Container_ZkOpers() zkOper.write_container_node_info(cluster, container_node, container_stat, containerProps)
def retrieve_cluster_NIC(self): zkOper = Container_ZkOpers() uuid = zkOper.getClusterUUID() uuid_info = zkOper.retrieve_uuid_info(uuid) NIC = uuid_info.get('NIC') if not NIC: NIC = getNIC() uuid_info.update({'NIC': NIC}) zkOper.writeClusterInfo(uuid, uuid_info) return NIC
def container_cluster_status_validator(self, cluster): zkOper = Container_ZkOpers() container_node_list = zkOper.retrieve_container_list(cluster) status_list, result = [], {} for container_node in container_node_list: status_node_value = zkOper.retrieve_container_status_value( cluster, container_node) status_list.append(status_node_value.get('status')) status = self.__get_cluster_status(status_list) result.setdefault('status', status) if status == Status.destroyed: logging.info('delete containerCluster: %s record in zookeeper' % cluster) zkOper.delete_container_cluster(cluster) return result
def __sum_count(self, cluster, node_count): zk_oper = Container_ZkOpers() cluster_info = zk_oper.retrieve_container_cluster_info(cluster) container_count = cluster_info.get('containerCount') return int(node_count) + int(container_count)
def get_host_ip_from_zk(self, cluster, container_node): zkOper = Container_ZkOpers() container_info = zkOper.retrieve_container_node_value( cluster, container_node) return container_info.get('hostIp')