Beispiel #1
0
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
Beispiel #2
0
def _create_instance(image_id, mem, port, ip, update_lb=False):
    global current_port
    if ip != "localhost":
        url = 'http://' + ip + ':5001' + '/run'
        form_data = {'image_id' : image_id, 'network' : image_id, 'mem' : str(mem), 'port' : port}
        params = urllib.urlencode(form_data)
        response = urllib2.urlopen(url, params)
        result = json.load(response)
        main_lb_ip = docker.get_ip_by_name(main_lb_image_label)
        created = True
        lb_port = result['lb_port']
        container_id = result['container_id']
        update_main_load_balancer(image_id, ip, str(lb_port))
    else:
        container_id = docker.run('--network', image_id, '-m', str(mem) + 'M', '--device=/dev/isgx','-d', '--cpus=0.5', '-v', image_id + ':' + docker.default_volume_path, image_id)
        #if there are instances with this image, remove the current load balancer, reverse one port number in the load balancer port counter and the recreate the load balancer.
        instances = docker.get_instance_names_by_id(haproxy_prefix + image_id)
        if instances:
            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) 
            current_port -= 1
            lb_port = _add_load_balancer(image_id, port, ip=load_balancer_ip)
        else:
            lb_port = _add_load_balancer(image_id, port)
        update_main_load_balancer(image_id, "127.0.0.1", str(lb_port))
   
    return {container_id: ip}
Beispiel #3
0
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
Beispiel #4
0
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)
Beispiel #5
0
    

#PRIVATE; ADD_LOAD_BALANCER: used by CREATE to add a load balancer to a given cluster by id
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:
<<<<<<< HEAD
        created = docker.run('--network=' + image_id, '-d', '--cpus=0.5', '-p', str(current_port) + ":" + str(current_port), lb_image_label)
    else:
        networkID = docker.get_network_name_by_id(image_id)
        created = docker.run('--network=' + image_id, '--ip', ip, '-d',  '--cpus=0.5', '-p', str(current_port) + ":" + str(current_port), lb_image_label)
=======
         created = docker.run('--network=' + id, '-d', '-p', '80:80', lb_image_label)
    else:
         networkID = docker.get_network_name_by_id(id)
         created = docker.run('--network=' + id, '--ip', ip, '-d', '-p', '80:80', lb_image_label)
>>>>>>> a93139078caac6f5316b15c2b42f663c2736e747
    if created:
        current_port += 1
        print('[INFO] Load balancer created succesfully')
        return current_port - 1  

def _add_to_main_load_balancer(image_id, lb_ip, container_ips, port):