def run_profile(profile_name, tag_name): registry = config.get_env(profile_name, 'docker_registry') vol_mapping = config.get_env(profile_name, 'vol_mapping') port_mapping = config.get_env(profile_name, 'port_mapping') env_vars = config.get_env(profile_name, 'env_vars') command = "docker run -d " command += '--name ' + profile_name command += ' -p ' + port_mapping command += ' -v ' + vol_mapping command += ' -e ' + env_vars tag = 'latest' if tag_name == '': logger.log_bl('\nGetting top 5 available tags of ' + profile_name + ' from Amazon ECR registry...') tag_list = utils.cmd_exec( "aws ecr describe-images --repository-name " + profile_name + " --output text --query 'sort_by(imageDetails,& imagePushedAt)[*].imageTags[*]' | tr '\t' '\n' | tail -5" ) tag_list = list(reversed(tag_list.split('\n'))) tag = click.prompt(logger.style('\nSelect an tag to deploy: ?'), type=click.Choice(tag_list)) logger.log_g('\nYou have selected tag: [ ' + tag + ' ] for your application') command += ' ' + registry + '/' + profile_name + ':' + tag logger.debug('final command : ' + command) logger.log_y('\nKilling old container if exist') utils.cmd_exec('docker rm -f ' + profile_name) utils.cmd_exec(command) logger.log_g("\nSuccessfully started " + profile_name + " application . Please check logs using: ") logger.log_cy("\n docker logs -f " + profile_name + " \n")
def add_build_tag(search_pattern, tag_name): logger.log_c("\nSearching pattern: ", search_pattern) logger.log_c("Adding tag : " + tag_name) image_name = utils.cmd_exec("docker images | cut -d ' ' -f1 | grep " + search_pattern + " | head -1") logger.log_y("Found this image name from the given pattern : " + image_name) add_tag_command = utils.join_me( ['docker tag ', image_name, ' ', image_name, ':', tag_name]) utils.cmd_exec(add_tag_command) logger.log_y('Added Tag : ' + tag_name + ' into the images\n') images_list = utils.cmd_exec('docker images') logger.log_c("Updated Images list :\n") logger.log(images_list)
def deploy(cluster_name, service_name, image_version): task_family = get_task_family(cluster_name, service_name) logger.log_c("Deploying Task Family : " , task_family) logger.log_c("Cluster Name : " , cluster_name) logger.log_c("Service Name : " , service_name) task_family = task_family.split(":")[0] last_task_def_name = get_latest_task_definations(task_family) task_def_json = get_task_defination_json(last_task_def_name) task_def_json = update_task_image_version(task_def_json, image_version) older_revision = json.dumps(task_def_json['revision']) logger.log_c("Last Task Version Deployed : " , older_revision) new_revision = str(int(older_revision) + 1); logger.log_c("Deploying new Task version : " , new_revision) task_def_json = clean_task_defination(task_def_json) logger.debug('Updated JSON of Task definations : ' + json.dumps(task_def_json, indent=4)) register_task(task_family, task_def_json) utils.cmd_exec("aws ecs update-service --cluster " + cluster_name + " --service " + service_name + " --task-definition " + task_family + ":" + new_revision) logger.log_c("Successfully deployed task version " , new_revision)
def test_machine(): url = get_slack_url() usage = utils.cmd_exec( "docker exec -it fe0df5cdc63f vmstat -s | grep 'memory'") message = raw_input("Enter the message you want to send : ") data = { 'text': '>>> This is `sent` \n from *dokr* plugin \n>' + message + ' \n Memory Left on your device : ' + usage } request.post(url, data)
def push_image(search_pattern): logger.log_c("\nSearching images matching this pattern : " + search_pattern) image_name = utils.cmd_exec("docker images | cut -d ' ' -f1 | grep " + search_pattern + " | head -1") logger.log_y("\nFound this image name from the given pattern : " + image_name) if image_name == '': logger.log_r("No such found to push !!! Exiting !!!") exit() image_list_str = "docker image inspect -f '{{join .RepoTags \"\\n\" }}' " + image_name logger.debug("\nFinding images tags") image_list = utils.cmd_exec(image_list_str) logger.debug("\nFound Following images : \n\n" + image_list) tag_list = utils.cmd_exec(image_list_str).split("\n") logger.log_c('\nTotal Tags found : ', str(len(tag_list))) for tag in tag_list: logger.log_c('\n--------------------------Pushing image :' + tag + '-----------------------------------') utils.cmd_exec('docker push ' + tag)
def find_ip(instanceToSearch): logger.log_c("Fetching instance list") ec2_instances = utils.cmd_exec("aws ec2 describe-instances") ec2_instances = json.loads(ec2_instances) found_instance = {}; logger.debug("Searching the pattern : " + instanceToSearch) reservations = ec2_instances['Reservations']; for reservation in reservations: instances = reservation['Instances'] for instance in instances: tags = instance['Tags']; for tag in tags: tagKey = json.dumps(tag['Key']); tagValue = json.dumps(tag['Value']); if instanceToSearch in tagValue : logger.debug("Found instance : " + tagValue) found_instance = instance; break ip_address = found_instance['NetworkInterfaces'][0]['Association']['PublicIp']; logger.log_c("Ip Address of " + instanceToSearch + ": " , ip_address)
def login_ecs(): logger.log_y("Logging into ECS") login = utils.cmd_exec("aws ecr get-login | sed 's/-e none//g'") utils.cmd_exec(login) logger.log_g("Login successful")
def get_task_family(cluster_name, service_name): service_json = utils.cmd_exec("aws ecs describe-services --cluster " + cluster_name + " --service " + service_name) service_json = json.loads(service_json) return service_json['services'][0]['deployments'][0]['taskDefinition'].split("/")[1]
def register_task(task_family, task_def_json): utils.cmd_exec("aws ecs register-task-definition --family " + task_family + " --cli-input-json \"" + json.dumps(task_def_json).replace('"', '\\"').replace('\n', '\\n') + "\"")
def tasks_in_cluster(cluster_name, service_name): tasks = utils.cmd_exec("aws ecs list-tasks --cluster " + cluster_name + " --service-name " + service_name) tasks = json.loads(tasks) return tasks['taskArns']
def services_in_cluster(service_name): services = utils.cmd_exec("aws ecs list-services --cluster " + service_name) services = json.loads(services) return services['serviceArns']
def cluster_list(): clusters = utils.cmd_exec("aws ecs list-clusters") clusters = json.loads(clusters) return clusters['clusterArns']
def get_task_defination_json(task_def_name): task_defination_json = utils.cmd_exec("aws ecs describe-task-definition --task-definition " + task_def_name); return json.loads(task_defination_json)
def get_latest_task_definations(family_prefix): task_definations = utils.cmd_exec("aws ecs list-task-definitions --family-prefix " + family_prefix); return json.loads(task_definations)['taskDefinitionArns'][-1]
def clean_all(): logger.log_c('\nCleaning docker system') logger.log_y(utils.cmd_exec("docker system prune -a -f"))
def clean_up(search_pattern): logger.log_c('\nCleaning Old Images matching pattern : ', search_pattern) logger.debug('\n---------- Cleaning Old Images ---- \n') utils.cmd_exec("docker images -a | grep " + search_pattern + " | awk '{print $3}' | xargs docker rmi -f") logger.log_g("\nCleaned images successfully")