def list_all_containers(): port = START_PORT for i in range(NUM_WORKER_NODES): logger.info("Containers on Node co_node_{}: ".format(port)) (out, err) = CLIUtils.run(LIST_POD_CONTAINERS_CMD.format(port)) logger.info('list of containers: %s', out) port = port + 1
def start_container_on_node(spec): (out, err) = CLIUtils.run( START_POD_CONTAINER_SELF_CMD.format(spec["node"], spec["container"]["port"], spec["container"]["image"], spec["container"]["name"])) logger.debug('started container id: {}'.format(out))
def callback(ch, method, properties, body): with open('na.log', 'a+') as f: print(dir(body)) print(f'{body} are received') f.write(f'{body} are received\n') payload = json.loads(body.decode('utf-8')) f.write("state in payload: {}\n".format(payload["state"])) f.write("_id in payload: {}\n".format(payload["_id"])) if payload['state'] == "fail": #delete the container if running f.write("Received failed state. Stopping container\n") self._stop_container(payload['name']) else: # check the db for the state of the container f.write("checking the state in db: {} \n".format(self._check_container_state_in_db(payload["_id"]))) if self._check_container_state_in_db(payload["_id"]) == 'queue': print("Bringing up the container {}".format(payload['image'])) f.write("Bringing up the container {}\n".format(payload['image'])) cmd = "docker run --rm --name={} -d {}".format(payload["name"], payload['image']) out, err = CLIUtils.run(cmd) f.write("Executed cmd: {}".format(cmd)) f.write("Out put : {}\n".format(out)) f.write("Err: {}\n".format(err)) if self._check_container_state_in_db(payload["_id"]) == 'queue': f.write("Updating container state in db: {}\n".format(payload['name'])) out = self.update_container(payload["_id"], "running", datetime.datetime.utcnow()) f.write("update container out put : {}\n".format(out)) f.write("Updating service current_state in db: {}\n".format(payload['name'])) out = self.update_config_details(payload["service_map"]) f.write("update config details out put: {}\n".format(out.raw_result)) else: # clean the stale container f.write("stopping container on node as the scheduler has marked it stale in db: {}\n".format(payload['name'])) self._stop_container(payload["name"])
from cli_utils import CLIUtils out, err = CLIUtils.run("docker stop test-nginx") print(out)
def list_containers_on_node(node): logger.info("Containers on Node co_node_{}: ".format(node)) (out, err) = CLIUtils.run(LIST_POD_CONTAINERS_CMD.format(node)) logger.info('list of containers: %s', out)
def _stop_container(self, name): cmd = "docker stop {}".format(name) CLIUtils.run_nb(cmd)
} } num_nodes = 5 num_replicas = 3 port = START_PORT # for i in range(int(num_replicas)): # cont_port = START_PORT # image_name = "nginx" # # for i in range(int(num_replicas)): # # cont_name = "{}_{}".format(image_name, cont_port) # # (out, err) = CLIUtils.run(START_POD_CONTAINER_CMD.format(port, cont_port, image_name, cont_name)) # # logger.debug('started container id: {}'.format(out)) # # cont_port = cont_port + 1 # cont_name = "{}_{}".format(image_name, cont_port) # (out, err) = CLIUtils.run(START_POD_CONTAINER_CMD.format(port, cont_port, image_name, cont_name)) # logger.debug('started container id: {}'.format(out)) # port = port + 1 (out, err) = CLIUtils.run( START_POD_CONTAINER_CMD.format(cont_spec["node"], cont_spec["container"]["port"], cont_spec["container"]["image"], cont_spec["container"]["name"])) logger.debug('started container id: {}'.format(out)) port = START_PORT for i in range(int(num_nodes)): logger.info("Containers on Node co_node_{}: ".format(port)) (out, err) = CLIUtils.run(LIST_POD_CONTAINERS_CMD.format(port)) logger.info('list of containers: %s', out) port = port + 1
port = START_PORT for i in range(int(num_nodes)): node_name = "co_node_{}".format(port) na = NodeAgent(node_name) na.nodes.insert_one({ "name": node_name, "description": "worker node", "heart_beat_time": datetime.datetime.utcnow(), "free_mem": 10, "free_cpu": 4, "free_disk": 128 }) logger.debug('added worker node container') # create queues channel.queue_declare(node_name) # bind the queue to exchange channel.queue_bind(node_name, 'co_topic', routing_key=node_name) # start node agent cmd = "python3 /NodeAgent.py -n {} -c &".format(node_name) d_cmd = "docker exec {} {}".format(node_name, cmd) print(d_cmd) CLIUtils.run_nb(d_cmd) # start node agent poller cmd = "python3 /NodeAgent.py -n {} -p &".format(node_name) d_cmd = "docker exec {} {}".format(node_name, cmd) print(d_cmd) CLIUtils.run_nb(d_cmd) port = port + 1