def _add_load_balancer(container_params, ip=None): global current_port lb_image_label = haproxy_prefix + container_params['image_id'] lb_dockerfile_path = haproxy_dockerfile_dir + container_params['image_id'] ip_list = docker.get_ip_list_by_id(container_params['image_id']) if len(ip_list) == 0 and ip: return None haproxy.add_nodes(container_params['image_id'], container_params['port'], ip_list) ### DEBUG ### docker.build('-t', lb_image_label, lb_dockerfile_path) if not ip: current_port += 1 created = docker.run('--network=' + container_params['image_id'], '-d', '-p', str(current_port) + ':' + str(current_port), lb_image_label) else: networkID = docker.get_network_name_by_id(container_params['image_id']) created = docker.run('--network=' + container_params['image_id'], '--ip', str(ip), '-d', '-p', str(current_port) + ':' + str(current_port), lb_image_label) if created: print('[INFO] Load balancer created succesfully') if not ip: print '[DEBUG] Adding load balancer. The port used was ' + str( current_port) return current_port - 1 else: print '[DEBUG] Updating load balancer. The port used was ' + str( current_port) return current_port
def write_to_log(id): ip_list = docker.get_ip_list_by_id(id) with open(os.path.join(log_path, id + '.log'), 'w+') as log: data = id + empty_line for ip in ip_list: data = data + ip + empty_line log.write(data) log.close()
def _add_to_main_load_balancer(image_id, lb_ip, container_ips, port): current_ip = docker.get_ip_list_by_id(main_lb_image_label) haproxy.add_lb_to_main_lb(lb_ip, container_ips, port) docker.build('-t', main_lb_image_label, main_lb_dockerfile_path) main_load_balancer_instance = docker.get_instance_names_by_id(main_lb_image_label)[0] docker.stop(main_load_balancer_instance) eocker.rm(main_load_balancer_instance) created = docker.run('--network=' + main_lb_image_label, '--ip', currrent_ip, '-d', main_lb_image_label) return True
def _add_load_balancer(image_id, port, ip=None): global current_port lb_image_label = haproxy_prefix + image_id lb_dockerfile_path = haproxy_dockerfile_dir + image_id ip_list = docker.get_ip_list_by_id(image_id) if not ip_list: return None haproxy.add_nodes(image_id, port, ip_list) docker.build('-t', lb_image_label, lb_dockerfile_path) if not ip:
def update_load_balancer(container_params): image_id = container_params['image_id'] load_balancer_instance = docker.get_instance_names_by_id(haproxy_prefix + image_id)[0] load_balancer_ip = docker.get_ip_by_name(load_balancer_instance) _remove_current_load_balancer(load_balancer_instance, image_id) load_balancer_unavailable = True lb_port = _add_load_balancer(container_params, ip=load_balancer_ip) while load_balancer_unavailable: load_balancer_list = docker.get_instance_names_by_id(haproxy_prefix + image_id) if load_balancer_list: load_balancer_instance = load_balancer_list[0] load_balancer_unavailable = False else: lb_port = _add_load_balancer(container_params) container_ips = docker.get_ip_list_by_id(container_params['image_id']) return lb_port
def _create_service_dispatcher(id, cluster, volume): service_dispatcher_dir = service_dispatcher_dockerfile_dir + id if not os.path.exists(service_dispatcher_dir): os.makedirs(service_dispatcher_dir) section = merger.get_section() dockerfile_dir = (definition.ROOT_PATH + '/dockerfiles/' + id).encode("ascii") dockerfile_path = dockerfile_dir + '/Dockerfile' merger.insert_section(dockerfile_path, section) print("[LOG] section inserted") instance_metadata_path = service_dispatcher_dir + "/meta.json" cluster['volume_size'] = volume['volume_size'] del cluster['package'] merger.write_data(instance_metadata_path, json.dumps(cluster)) shutil.copy(service_dispatcher_dockerfile_dir + "Dockerfile", service_dispatcher_dir) manager_address = socket.gethostbyname(socket.gethostname()) docker.build('-t', service_dispatcher_id, service_dispatcher_dockerfile_dir) docker.run('-e', "ID=" + id, '-e', 'MANAGER=' + manager_address, '-d', service_dispatcher_id) return docker.get_ip_list_by_id(service_dispatcher_id)