def show_containers(): # Connection to Docker Daemon c = connection.get() # Showing containers containers = c.containers() for container in containers: print(container.get('Id'))
def stats(cluster_name, stats_type): # Connection to Docker Daemon c = connection.get() container_list = localstore.load_object(cluster_name + ".pkl") containers = c.containers() if container_list is not None: # Checking all containers stats for container in container_list: containerid = c.inspect_container(container).get("Id") containerName = c.inspect_container(container).get("Name") print("Container Name:" + str(containerName) + " Container Id:" + str(containerid) ) # Get snapshot of statistics. Stats can be taken in stream format also. stats_obj = c.stats(containerid, False, False) if stats_type == "io": print(stats_obj.get("blkio_stats")) if stats_type == "precpu": print(stats_obj.get("precpu_stats")) if stats_type == "read": print(stats_obj.get("read")) if stats_type == "memory": print(stats_obj.get("memory_stats")) if stats_type == "pid": print(stats_obj.get("pids_stats")) if stats_type == "network": print(stats_obj.get("networks")) if stats_type == "cpu": print(stats_obj.get("cpu_stats")) else: print("Cluster [%s] is not running" % cluster_name)
def stats(cluster_name, stats_type): # Connection to Docker Daemon c = connection.get() container_list = localstore.load_object(cluster_name + ".pkl") containers = c.containers() if container_list is not None: # Checking all containers stats for container in container_list: containerid = c.inspect_container(container).get("Id") containerName = c.inspect_container(container).get("Name") print("Container Name:" + str(containerName) + " Container Id:" + str(containerid)) # Get snapshot of statistics. Stats can be taken in stream format also. stats_obj = c.stats(containerid, False, False) if stats_type == "io": print(stats_obj.get("blkio_stats")) if stats_type == "precpu": print(stats_obj.get("precpu_stats")) if stats_type == "read": print(stats_obj.get("read")) if stats_type == "memory": print(stats_obj.get("memory_stats")) if stats_type == "pid": print(stats_obj.get("pids_stats")) if stats_type == "network": print(stats_obj.get("networks")) if stats_type == "cpu": print(stats_obj.get("cpu_stats")) else: print("Cluster [%s] is not running" % cluster_name)
def list_apps(userSession): url = connection.build_url('application/list') r = connection.get(url, params={'user_token': userSession.Token}, headers={'Accept': 'application/json'}) if r.status_code != 200: raise ApplicationInvocationError("Requesting applications failed: %s" % r.content) # TODO: parse resources listOfApplications = r.json() return listOfApplications
def list_free_apps(): url = connection.build_url('application/list') r = connection.get(url, params={'type': 'free'}, headers={'Accept': 'application/json'}) if r.status_code != 200: raise ApplicationInvocationError("Requesting free applications failed: %s" % r.content) # TODO: parse resources listOfApplications = r.json() return listOfApplications
def consolidate_logs(cluster_name, logs): # Connection to Docker Daemon c = connection.get() container_list = localstore.load_object(cluster_name + ".pkl") if container_list is not None: # Checking all containers logs for container in container_list: logs_stream = c.logs(container, stream=True, stdout=True, stderr=True) container_name = c.inspect_container(container).get("Name") print("Staring logging for: ", container_name) thread= Thread(target=threaded_function, args=(logs_stream,logs, container_name)) thread.setDaemon(True) thread.start() else: print("Cluster [%s] is not running" % cluster_name)
def start_cluster(cluster_name, size, image): c = connection.get() container_list = [] # Simple iteration for starting containers for i in range(0, size): cname = "%s-node%s" % (cluster_name, i) try: container = c.create_container(image, name=cname, ports=[8080]) container_list.append(container) c.start(container, publish_all_ports=True) except Exception as err: print("Unexpected error:", sys.exc_info()[0]) print('Handling run-time error:', err) # Saving locally cluster nodes localstore.save_object(container_list, cluster_name + ".pkl")
def validate_containers(cluster_name): # Connection to Docker Daemon c = connection.get() container_list = localstore.load_object(cluster_name + ".pkl") # Initialization checker = True if container_list is not None: # Checking all containers status for container in container_list: checker = checker * c.inspect_container(container).get("State").get("Running") if checker: print("Validation Success: All cluster [%s] nodes are up"%cluster_name) else: print("Validation Failure: Not all cluster [%s] nodes are up" %cluster_name) else: print("Cluster [%s] is not running"%cluster_name)
def stop_cluster(cluster_name): # Connection to Docker Daemon c = connection.get() # Loading containers from localstore container_list = localstore.load_object(cluster_name + ".pkl") if container_list is not None: for container in container_list: # We need to stop the containers try: print("Stopping container: " + str(container)) c.stop(container, 10) c.remove_container(container) except: print("Unexpected error:", sys.exc_info()[0]) # Delete local store of cluster nodes os.remove(cluster_name+".pkl") else: print("Cluster [%s] is not running" % cluster_name)
def stop_cluster(cluster_name): # Connection to Docker Daemon c = connection.get() # Loading containers from localstore container_list = localstore.load_object(cluster_name + ".pkl") if container_list is not None: for container in container_list: # We need to stop the containers try: print("Stopping container: " + str(container)) c.stop(container, 10) c.remove_container(container) except: print("Unexpected error:", sys.exc_info()[0]) # Delete local store of cluster nodes os.remove(cluster_name + ".pkl") else: print("Cluster [%s] is not running" % cluster_name)
def validate_containers(cluster_name): # Connection to Docker Daemon c = connection.get() container_list = localstore.load_object(cluster_name + ".pkl") # Initialization checker = True if container_list is not None: # Checking all containers status for container in container_list: checker = checker * c.inspect_container(container).get( "State").get("Running") if checker: print("Validation Success: All cluster [%s] nodes are up" % cluster_name) else: print("Validation Failure: Not all cluster [%s] nodes are up" % cluster_name) else: print("Cluster [%s] is not running" % cluster_name)
def consolidate_logs(cluster_name, logs): # Connection to Docker Daemon c = connection.get() container_list = localstore.load_object(cluster_name + ".pkl") if container_list is not None: # Checking all containers logs for container in container_list: logs_stream = c.logs(container, stream=True, stdout=True, stderr=True) container_name = c.inspect_container(container).get("Name") print("Staring logging for: ", container_name) thread = Thread(target=threaded_function, args=(logs_stream, logs, container_name)) thread.setDaemon(True) thread.start() else: print("Cluster [%s] is not running" % cluster_name)