def remove_instance_from_cluster(image_id): global server_pointer ip = available_nodes[server_pointer].get_ip() #if the choosen node is not local, send a request to the chosen node to remove an instance with the image_id if ip != "localhost": url = 'http://' + ip + ':5001' + '/remove' form_data = {'image_id' : image_id} params = urllib.urlencode(form_data) response = urllib2.urlopen(url, params) result = json.load(response) #update the main load balancer to remove the container from the chosen instance main_lb_ip = docker.get_ip_by_name(main_lb_image_label) removed = True lb_port = result['lb_port'] container_id = result['container_id'] update_main_load_balancer(image_id, ip, str(lb_port)) else: #if the chosen node is the local node, remove an instance with with the image_id straight from docker removables = docker.get_instance_ids_by_id(image_id) removed = docker.rm('-f', removables[-1]) #update main load balancer to remove the container from the chosen instance lb_port = _add_load_balancer(image_id, port) update_main_load_balancer(image_id, "127.0.0.1", str(lb_port)) #update the server pointer to the previous server. If it goes down to zero, set it to the last one on the list. _remove_container_from_record(image_id, container_id) server_pointer -= 1 if server_pointer == 0: server_pointer = len(available_nodes) - 1
def remove_image(image_id): global server_pointer ip = available_nodes[server_pointer].get_ip() #if the choosen node is not local, send a request to the chosen node to remove an instance with the image_id if ip != "localhost": url = 'http://' + ip + ':5001' + '/remove_image' form_data = {'image_id' : image_id} params = urllib.urlencode(form_data) response = urllib2.urlopen(url, params) result = json.load(response) else: #if the chosen node is the local node, remove an instance with with the image_id straight from docker removables = docker.get_instance_ids_by_id(image_id) for index, removable in enumerate(removables): removed = [docker.rm('-f', removables[index])] #update main load balancer to remove the container from the chosen instance lb_port = _add_load_balancer(image_id, None) update_main_load_balancer(image_id, "127.0.0.1", str(lb_port)) #update the server pointer to the previous server. If it goes down to zero, set it to the last one on the list. for removable in removables: _remove_container_from_record(image_id, removable) server_pointer -= 1 if server_pointer == 0: server_pointer = len(available_nodes) - 1 success_containers = docker.rmi(image_id) success_load_balancer = docker.rmi(haproxy_prefix + image_id) success_volume = docker.remove_volume(image_id) success_network = docker.remove_network(image_id) merger.remove_app_data(image_id) success = success_containers and success_load_balancer and success_volume and success_network return str(success) + '\n', 200
def get_cpu_stats(cluster_id): available_nodes = app.get_available_nodes() stats_dict = {} stats_lines = [] #GETTING DOCKER CPU STATS FROM ALL INSTANCES IN ALL NODES for node in available_nodes: if node.get_ip() != "localhost": url = 'http://' + node.get_ip() + ":5001/cpu_stats/" + cluster_id node_stats_table = urllib2.urlopen(url).read() node_stats_lines = node_stats_table.split('\n') stats_dict.update(stats_string_to_dict(node_stats_lines)) else: node_stats_table = docker.stats_cpu() node_stats_lines = node_stats_table.split('\n') #TURNING STATS STRING LINES INTO {INSTANCE_ID -> CPU_STAT} DICTIONARY all_stats_dict = stats_string_to_dict(node_stats_lines) cluster_instances_ids = docker.get_instance_ids_by_id(cluster_id) #REMOVING STATS FROM INSTANCES THAT ARE NOT FROM THE DESIRED CLUSTER for key in all_stats_dict: if key in cluster_instances_ids: stats_dict[key] = all_stats_dict[key] print "[INFO] current instances for " + cluster_id + " are: " + str( stats_dict) stats = stats_dict.values() index = 0 #REMOVING PERCENTAGE CHARACTER FROM CPU STATS VALUES AND TURNING THEM INTO INTS for stat in stats: stat = stat[:-2] stat = float(stat) stats[index] = stat index += 1 return stats
def get_cpu_stats(image_id): # image_id = request.form['image_id'] ancestor_image_instances_ids = docker.get_instance_ids_by_id(image_id) cluster_instances_ids = [] for instance in ancestor_image_instances_ids: if docker.get_image(instance).strip('\n').strip('"') == image_id: cluster_instances_ids.append(instance) stats = docker.stats_cpu() stats_list = stats.split('\n') match_lines = [] for cluster_instance in cluster_instances_ids: for stats_line in stats_list: if cluster_instance in stats_line: match_lines.append(stats_line) match_stats = '\n'.join(match_lines) print "[DEBUG] image_id stats are " + match_stats print "[DEBUG] current_port is " + str(current_port) return match_stats