├── LICENSE.txt ├── README.md └── rails-console-ecs /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 mnc 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rails-console-ecs 2 | This is simple shell script for executing rails console in a ECS Task. 3 | You can exec `rails c` command from outside of ECS Cluster by executing only one command. 4 | 5 | ## How it works 6 | 7 | 1. Run a Task using a latest Task Definition in a specific ECS Cluster 8 | 2. Get private IP by AWS CLI 9 | 3. Get docker container ID via SSH 10 | 4. Exec `rails c` command in the container via SSH 11 | 5. Stop the Task 12 | 13 | ## Usage 14 | 15 | ``` 16 | Usage: 17 | rails-console-ecs [options] 18 | 19 | Options: 20 | -v Version 21 | -h Show help of this command 22 | -c Ecs cluster which a task runs in. use RAILS_C_ECS_CLUSTER by default 23 | -d Ecs task definition. use RAILS_C_TASK_DEFINITION by default 24 | -e Rails env. use RAILS_C_ENV by default 25 | -r Region. use RAILS_C_REGION by default 26 | ``` 27 | 28 | ## Installation 29 | 30 | ### Requirement 31 | - [aws-cli](https://github.com/aws/aws-cli) 32 | - [jq](https://github.com/stedolan/jq) 33 | 34 | ### Install rails-console-ecs 35 | 36 | #### Linux 37 | 38 | ```shell 39 | curl https://raw.githubusercontent.com/mnc/rails-console-ecs/master/rails-console-ecs | sudo tee /usr/bin/rails-console-ecs 40 | sudo chmod +x /usr/bin/rails-console-ecs 41 | ``` 42 | 43 | #### Mac 44 | 45 | ```shell 46 | curl https://raw.githubusercontent.com/mnc/rails-console-ecs/master/rails-console-ecs | sudo tee /usr/local/bin/rails-console-ecs 47 | sudo chmod +x /usr/local/bin/rails-console-ecs 48 | ``` 49 | 50 | ## LICENSE 51 | MIT 52 | -------------------------------------------------------------------------------- /rails-console-ecs: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | VERSION='0.1.0' 6 | 7 | usage_exit() { 8 | cat <&2 64 | } 65 | 66 | cluster=${opt_c:-$RAILS_C_ECS_CLUSTER} 67 | task_definition=${opt_d:-$RAILS_C_TASK_DEFINITION} 68 | rails_env=${opt_e:-$RAILS_C_ENV} 69 | region=${opt_r:-$RAILS_C_REGION} 70 | 71 | container_instance_arn=$(aws ecs list-container-instances \ 72 | --region "$region" \ 73 | --cluster "$cluster" \ 74 | --query 'containerInstanceArns[0]' \ 75 | --output text) 76 | 77 | run_task_result=$(aws ecs run-task \ 78 | --region "$region" \ 79 | --task-definition "$task_definition" \ 80 | --cluster "$cluster") 81 | 82 | run_task_failures=$(echo "$run_task_result" | jq '.failures[]') 83 | [ -n "$run_task_failures" ] && abort "$run_task_failures" 84 | 85 | task_arn=$(echo "$run_task_result" | jq -r '.tasks[0].taskArn') 86 | aws ecs wait tasks-running --cluster "$cluster" --tasks "$task_arn" --region "$region" 87 | 88 | instance_id=$(aws ecs describe-container-instances --cluster "$cluster" \ 89 | --container-instances "$container_instance_arn" \ 90 | --region "$region" \ 91 | --query 'containerInstances[0].ec2InstanceId' \ 92 | --output text) 93 | 94 | private_ip=$(aws ec2 describe-instances --instance-id "$instance_id" \ 95 | --region "$region" \ 96 | --query 'Reservations[0].Instances[0].PrivateIpAddress' \ 97 | --output text) 98 | 99 | docker_id=$(ssh -t "$private_ip" "curl http://localhost:51678/v1/tasks" | \ 100 | jq -r ".Tasks[] | select(.Arn==\"$task_arn\").Containers[].DockerId") 101 | 102 | ssh -t "$private_ip" "docker exec -it \"$docker_id\" bash -c 'cd /app; bin/rails c $rails_env'" 103 | 104 | aws ecs stop-task --region "$region" --task "$task_arn" --cluster "$cluster" > /dev/null 105 | aws ecs wait tasks-stopped --cluster "$cluster" --tasks "$task_arn" --region "$region" 106 | 107 | exit 0 108 | --------------------------------------------------------------------------------