bolts-ssh 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d929ba149173bde3cd4c3ff0ed2fba832b6bad07
4
+ data.tar.gz: 9f583744991efe725fcd415dc88454725ba51076
5
+ SHA512:
6
+ metadata.gz: afc159445edad238166bbf36dd1a449d0bcde0654c3e8d774e675834ae744536688b066ee81c6607324870ade2ed063f39bb7dae5fc56218ff92e4953fa85930
7
+ data.tar.gz: c7ebd163ca1322304896108bf381d1d9d6e22d0e525de1c00a5a4ab36c0c439509818641f8a611d53d3929febbbcc59c4952eb09b886cf722e874f30a6fbf521
@@ -0,0 +1,16 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ InstalledFiles
7
+ _yardoc
8
+ coverage
9
+ doc/
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ spec/reports
14
+ test/tmp
15
+ test/version_tmp
16
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bolts.gemspec
4
+ gemspec
5
+
6
+ gem "codeclimate-test-reporter", group: :test, require: nil
@@ -0,0 +1,106 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bolts-ssh (0.0.1)
5
+ aws-sdk
6
+ colorize
7
+ hashie
8
+ thor
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ aws-sdk (2.9.17)
14
+ aws-sdk-resources (= 2.9.17)
15
+ aws-sdk-core (2.9.17)
16
+ aws-sigv4 (~> 1.0)
17
+ jmespath (~> 1.0)
18
+ aws-sdk-resources (2.9.17)
19
+ aws-sdk-core (= 2.9.17)
20
+ aws-sigv4 (1.0.0)
21
+ byebug (9.0.6)
22
+ codeclimate-test-reporter (1.0.8)
23
+ simplecov (<= 0.13)
24
+ coderay (1.1.1)
25
+ colorize (0.8.1)
26
+ diff-lcs (1.3)
27
+ docile (1.1.5)
28
+ ffi (1.9.18)
29
+ formatador (0.2.5)
30
+ guard (2.14.1)
31
+ formatador (>= 0.2.4)
32
+ listen (>= 2.7, < 4.0)
33
+ lumberjack (~> 1.0)
34
+ nenv (~> 0.1)
35
+ notiffany (~> 0.0)
36
+ pry (>= 0.9.12)
37
+ shellany (~> 0.0)
38
+ thor (>= 0.18.1)
39
+ guard-bundler (2.1.0)
40
+ bundler (~> 1.0)
41
+ guard (~> 2.2)
42
+ guard-compat (~> 1.1)
43
+ guard-compat (1.2.1)
44
+ guard-rspec (4.7.3)
45
+ guard (~> 2.1)
46
+ guard-compat (~> 1.1)
47
+ rspec (>= 2.99.0, < 4.0)
48
+ hashie (3.5.5)
49
+ jmespath (1.3.1)
50
+ json (2.1.0)
51
+ listen (3.1.5)
52
+ rb-fsevent (~> 0.9, >= 0.9.4)
53
+ rb-inotify (~> 0.9, >= 0.9.7)
54
+ ruby_dep (~> 1.2)
55
+ lumberjack (1.0.12)
56
+ method_source (0.8.2)
57
+ nenv (0.3.0)
58
+ notiffany (0.1.1)
59
+ nenv (~> 0.1)
60
+ shellany (~> 0.0)
61
+ pry (0.10.4)
62
+ coderay (~> 1.1.0)
63
+ method_source (~> 0.8.1)
64
+ slop (~> 3.4)
65
+ rake (12.0.0)
66
+ rb-fsevent (0.9.8)
67
+ rb-inotify (0.9.8)
68
+ ffi (>= 0.5.0)
69
+ rspec (3.6.0)
70
+ rspec-core (~> 3.6.0)
71
+ rspec-expectations (~> 3.6.0)
72
+ rspec-mocks (~> 3.6.0)
73
+ rspec-core (3.6.0)
74
+ rspec-support (~> 3.6.0)
75
+ rspec-expectations (3.6.0)
76
+ diff-lcs (>= 1.2.0, < 2.0)
77
+ rspec-support (~> 3.6.0)
78
+ rspec-mocks (3.6.0)
79
+ diff-lcs (>= 1.2.0, < 2.0)
80
+ rspec-support (~> 3.6.0)
81
+ rspec-support (3.6.0)
82
+ ruby_dep (1.5.0)
83
+ shellany (0.0.1)
84
+ simplecov (0.13.0)
85
+ docile (~> 1.1.0)
86
+ json (>= 1.8, < 3)
87
+ simplecov-html (~> 0.10.0)
88
+ simplecov-html (0.10.0)
89
+ slop (3.6.0)
90
+ thor (0.19.4)
91
+
92
+ PLATFORMS
93
+ ruby
94
+
95
+ DEPENDENCIES
96
+ bolts-ssh!
97
+ bundler
98
+ byebug
99
+ codeclimate-test-reporter
100
+ guard
101
+ guard-bundler
102
+ guard-rspec
103
+ rake
104
+
105
+ BUNDLED WITH
106
+ 1.14.6
@@ -0,0 +1,12 @@
1
+ guard 'rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { "spec/bolts_spec.rb" }
4
+ watch(%r{^lib/bolts/(.+)\.rb$}) { "spec/bolts_spec.rb" }
5
+ watch('spec/spec_helper.rb') { "spec/bolts_spec.rb" }
6
+ watch(%r{^lib/bolts/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ end
8
+
9
+ guard 'bundler' do
10
+ watch('Gemfile')
11
+ watch(/^.+\.gemspec/)
12
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tung Nguyen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,196 @@
1
+ # Bolts
2
+
3
+ Bolts contains is a group of commands that help debug EC2 instance and ECS containers quickly.
4
+
5
+ After I exhaust debugging an ECS service with CloudWatch Logs I usually take it to the the next step. I ssh into an instance with a running task or docker container and poke around to figure out the issue.
6
+
7
+ In order to find the instance with the service's docker container I click around on the ECS console website until I find the container instance's DNS name and then paste it to the terminal. The process is not complicated but it is tedious. For example, the typical process is:
8
+
9
+ 1. Click on the cluster
10
+ 2. Click on the service
11
+ 3. Click on the tasks tab
12
+ 4. Click on the one of the tasks
13
+ 5. Click on the container instance
14
+ 6. Highlight and copy the DNS name
15
+ 7. Paste the DNS name into the terminal to build up the ssh ec2-user@[dnsname] command
16
+ 8. Ssh into the machine
17
+ 9. Find the docker container with "docker ps"
18
+ 10. Run docker exec -ti [container_id] bash
19
+ 11. Finally, debug the actual problem
20
+
21
+ By the time I get into the container, I need to remind my brain on what the original issue was. This tool automates that process so you do not waste your precious finger energy clicking on links and use it to focus on better things like fixing the actual issue.
22
+
23
+ ## Install
24
+
25
+ Locally on your machine.
26
+
27
+ ```
28
+ gem install bolts-ssh
29
+ ```
30
+ Set up your AWS credentials at `~/.aws/credentials` and `~/.aws/config`. This is the [AWS standard way of setting up credentials](https://aws.amazon.com/blogs/security/a-new-and-standardized-way-to-manage-credentials-in-the-aws-sdks/).
31
+
32
+ Note that the gem is named `bolts-ssh` but the command is `bolts`.
33
+
34
+ ## Requirements
35
+
36
+ * [jq](https://stedolan.github.io/jq/manual/) - a lightweight and flexible command-line JSON processor
37
+
38
+ If you are also using the `exec` and `run` commands, then you will need to ensure that [jq](https://stedolan.github.io/jq/) is installed on all of your ECS container instances. If you are only using the `bolts ssh` command then you do not need the jq dependency.
39
+
40
+ ## Usage
41
+
42
+ ### bolts ssh
43
+
44
+ To ssh into the host or container instance where an ECS service called `my-service` is running, simply run:
45
+
46
+ ```
47
+ $ bolts ssh my-service --cluster my-cluster
48
+ # now you are on the container instance
49
+ $ docker ps
50
+ $ curl -s http://localhost:51678/v1/meta | jq .
51
+ ```
52
+
53
+ The `my-service` can possible run on multiple container instances. The `bolts` command chooses the first container instance that it finds. If you need to ssh into a specific container instance, use `bolts ssh` instead.
54
+
55
+ You can also use the instance id, container instance arn or task arn to ssh into the machine. Examples:
56
+
57
+ ```
58
+ $ bolts ssh my-ecs-service --cluster my-cluster # cluster is required or ~/.bolts/settings.yml
59
+ $ bolts ssh i-067c5e3f026c1e801
60
+ $ bolts ssh 7fbc8c75-4675-4d39-a5a4-0395ff8cd474
61
+ $ bolts ssh 1ed12abd-645c-4a05-9acf-739b9d790170
62
+ ```
63
+
64
+ ### bolts ecs-exec
65
+
66
+ `bolts ecs-exec` will hop one more level and get you all the way into a live container for a service. To quickly get yourself into a docker exec bash shell for a service:
67
+
68
+ ```
69
+ $ bolts ecs-exec SERVICE bash
70
+ $ bolts ecs-exec SERVICE # same as above, defaults to bash shell
71
+ ```
72
+
73
+ This ultimately runs the following command after it ssh into the container instance:
74
+
75
+ ```
76
+ $ docker run -ti SERVICE_CONTAINER bash
77
+ ```
78
+
79
+ Here are examples to show what is possible:
80
+
81
+ ```
82
+ $ bolts ecs-exec my-service bash
83
+ # You're in the docker container now
84
+ $ ls # check out some files to make sure you're the right place
85
+ $ ps auxxx | grep puma # is the web process up?
86
+ $ env # are the environment variables properly set?
87
+ $ bundle exec rails c # start up a rails console to debug
88
+ ```
89
+
90
+ You can also pass in bundle exec rails console if you want to get to that as quickly as possible.
91
+
92
+ ```
93
+ $ bolts ecs-exec my-service 'bundle exec rails console'
94
+ # You're a rails console in the docker container now
95
+ > User.count
96
+ ```
97
+
98
+ You must put commands with spaces in quotes.
99
+
100
+ You can also use the container instance id or instance id in place of the service name:
101
+
102
+ ```
103
+ $ bolts ecs-exec 9f1dadc7-4f67-41da-abec-ec08810bfbc9 bash
104
+ $ i-006a097bb10643e20
105
+ ```
106
+
107
+ ### bolts ecs-run
108
+
109
+ The `bolts ecs-run` command is similar to the `bolts ecs-exec` command except it'll run a brand new container with the same environment variables as the task associated with the service. This allows you to debug in a container with the exact environment variables as the running tasks/containers without affecting the live service. So this is safer since you will not be able to mess up a live container that is in service.
110
+
111
+ This also allows you to run one off commands like a rake task. Here's an example:
112
+
113
+ ```
114
+ bolts ecs-run my-service bundle exec 'rake do:something'
115
+ ```
116
+
117
+ The default command opens up a bash shell.
118
+
119
+ ```
120
+ bolts ecs-run my-service # start a bash shell
121
+ bolts ecs-run my-service bash # same thing
122
+ ```
123
+
124
+ ## Settings
125
+
126
+ A `~/.bolts/settings.yml` file is support that maps services to clusters. This is very useful if you get tired of typing the `--cluster` option every time. Here is an example `~/.bolts/settings.yml`:
127
+
128
+ ```yaml
129
+ service_cluster:
130
+ default: my-default-cluster
131
+ hi-web-prod: prod
132
+ hi-clock-prod: prod
133
+ hi-worker-prod: prod
134
+ hi-web-stag: stag
135
+ hi-clock-stag: stag
136
+ hi-worker-stag: stag
137
+ ```
138
+
139
+ This results in shorter commands:
140
+
141
+ ```
142
+ bolts ssh hi-web-prod
143
+ bolts ssh hi-clock-prod
144
+ bolts ssh hi-worker-stag
145
+ ```
146
+
147
+ Instead of what you normally would have to type:
148
+
149
+ ```
150
+ bolts ssh hi-web-prod --cluster prod
151
+ bolts ssh hi-clock-prod --cluster prod
152
+ bolts ssh hi-worker-prod --cluster stag
153
+ ```
154
+
155
+ ## Help and CLI Options
156
+
157
+ ```
158
+ bolts help
159
+ bolts help ssh
160
+ bolts help ecs-exec
161
+ bolts help ecs-run
162
+ ```
163
+
164
+ ## Internals
165
+
166
+ The process that I outline about click around is close to the logic that actually takes place in the tool but is slightly different. Here's an overview of what actually happens internally for those who are interested.
167
+
168
+ I thought it would be possible to map the container instance info from `aws ecs describe-services` but it is not possible. But we can map to the container instance DNS name starting from `aws ecs list-tasks`.
169
+
170
+ Steps:
171
+
172
+ 1. list-tasks: list all the tasks for their task_arns (scoped to service). this is all tasks on the service. We already know the service name!
173
+ 2. describe-tasks: Using the task_arns from list-tasks. This will provide the container instance scoped the service since list-tasks was scoped to service. Keep the task arn for step 7. Also describe-task-definition and capture env vars and image for step 8b.
174
+ 3. describe-container-instances: Using container\_instance\_arn from step 2. This will provide the instance_id to ssh into.
175
+ 4. ec2 describe-instances to get the dns name or IP address.
176
+ 5. Copy over files with required info over to the server with scp.
177
+ 6. ssh into the machine with IP address.
178
+ 7. Use the ecs metadata and pass it the task_arn from step 2. This will provide the map to the container id.
179
+ 8. Run docker command
180
+ * a) docker exec -ti CONTAINER_ID `options[:command]`
181
+ * b) docker run `options[:run_options]` IMAGE `options[:command]`
182
+
183
+ In order to pass info over from your local machine to the container instance a file is generated and copied in step 5. File contains:
184
+
185
+ * Options all the way from the original cli call like command to run. This is in json form.
186
+ A bash script is also copied.
187
+ * Bash script gets the container id using the task_arn. It also will run the docker exec or run command.
188
+ * So bash script does steps 7 and 8a or 8b.
189
+
190
+ ## Contributing
191
+
192
+ 1. Fork it
193
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
194
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
195
+ 4. Push to the branch (`git push origin my-new-feature`)
196
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ task :default => :spec
5
+
6
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift(File.expand_path('../../lib', __FILE__))
4
+ require 'bolts'
5
+ require 'bolts/cli'
6
+
7
+ Bolts::CLI.start(ARGV)
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bolts/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bolts-ssh"
8
+ spec.version = Bolts::VERSION
9
+ spec.authors = ["Tung Nguyen"]
10
+ spec.email = ["tongueroo@gmail.com"]
11
+ spec.description = %q{A collection of tools commands to debug EC2 and ECS quickly}
12
+ spec.summary = %q{Ssh into an EC2 instance quickly. Docker run or docker exec with running ECS container quickly.}
13
+ spec.homepage = "https://github.com/bolts/bolts"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "thor"
22
+ spec.add_dependency "hashie"
23
+ spec.add_dependency "colorize"
24
+ spec.add_dependency "aws-sdk"
25
+
26
+ spec.add_development_dependency "bundler"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "guard"
29
+ spec.add_development_dependency "guard-bundler"
30
+ spec.add_development_dependency "guard-rspec"
31
+ spec.add_development_dependency "byebug"
32
+ end
@@ -0,0 +1,15 @@
1
+ #!/bin/bash -xe
2
+
3
+ TASK_ARN=$(cat /tmp/bolts/task-arn.txt)
4
+ CONTAINER_ID=$(curl -s http://localhost:51678/v1/tasks | jq ".Tasks[] | select(.Arn == \"$TASK_ARN\")" | jq -r '.Containers[].DockerId' | head -1)
5
+
6
+ # Got all the info in memory now we need os we can clean the files
7
+ rm -rf /tmp/bolts
8
+
9
+ if [ $1 ]; then
10
+ COMMAND=$@
11
+ else
12
+ COMMAND=bash
13
+ fi
14
+
15
+ exec docker exec -ti $CONTAINER_ID $COMMAND
@@ -0,0 +1,15 @@
1
+ #!/bin/bash -x
2
+
3
+ CONTAINER_IMAGE=$(cat /tmp/bolts/docker-image.txt)
4
+
5
+ # Got all the info in memory now we need os we can clean the files
6
+ # rm -rf /tmp/bolts
7
+ # Cannot remove files until we are done using the --env-file
8
+
9
+ # Maybe do this later. But will need to have bolts path be unique.
10
+ # at -f "/tmp/bolts/seppuku.sh" now + 1.minute
11
+
12
+ cp /tmp/bolts/env-file.txt ~/
13
+ rm -rf /tmp/bolts
14
+
15
+ exec docker run -ti --env-file ~/env-file.txt $CONTAINER_IMAGE "$@"
@@ -0,0 +1,12 @@
1
+ $:.unshift(File.expand_path("../", __FILE__))
2
+ require "bolts/version"
3
+ require "aws-sdk"
4
+
5
+ module Bolts
6
+ autoload :Settings, 'bolts/settings'
7
+ autoload :Defaults, 'bolts/defaults'
8
+ autoload :CLI, 'bolts/cli'
9
+ autoload :AwsServices, 'bolts/aws_services'
10
+ autoload :Ssh, 'bolts/ssh'
11
+ autoload :Docker, 'bolts/docker'
12
+ end
@@ -0,0 +1,11 @@
1
+ module Bolts
2
+ module AwsServices
3
+ def ecs
4
+ @ecs ||= Aws::ECS::Client.new
5
+ end
6
+
7
+ def ec2
8
+ @ec2 ||= Aws::EC2::Client.new
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ require 'thor'
2
+ require 'bolts/cli/help'
3
+
4
+ module Bolts
5
+ class CLI < Thor
6
+ class_option :verbose, type: :boolean
7
+ class_option :noop, type: :boolean
8
+ class_option :cluster, desc: "ECS Cluster to use. Default cluster is default"
9
+
10
+ desc "ssh IDENTIFER", "ssh into a instance using identifier. identifer can be be service name or instance id"
11
+ long_desc Help.ssh
12
+ def ssh(service, *command)
13
+ Ssh.new(service, options.merge(command: command)).run
14
+ end
15
+
16
+ desc "ecs-exec SERVICE", "docker exec into running docker container associated with the service on a container instance"
17
+ long_desc Help.ecs_exec
18
+ def ecs_exec(service, *command)
19
+ Docker.new(service, options.merge(command: command)).exec
20
+ end
21
+
22
+ # Cannot name the command run because that is a reserved Thor keyword :(
23
+ desc "ecs-run SERVICE", "docker run with the service on a container instance"
24
+ long_desc Help.ecs_run
25
+ def ecs_run(service, *command)
26
+ Docker.new(service, options.merge(command: command)).run
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,67 @@
1
+ # https://github.com/erikhuda/thor/issues/398
2
+ class Thor
3
+ module Shell
4
+ class Basic
5
+ def print_wrapped(message, options = {})
6
+ pretty_message = message.split("\n").map{|l| " #{l}"}.join("\n")
7
+ stdout.puts pretty_message
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ module Bolts
14
+ class CLI < Thor
15
+ class Help
16
+ class << self
17
+ def ssh
18
+ <<-EOL
19
+ Ssh quicky into an EC2 instance using an identifier. The identifier can be many
20
+ different things:
21
+
22
+ * EC2 instance id. Example: i-067c5e3f026c1e801
23
+ * ECS service. Example: my-ecs-service
24
+ * ECS container instance id. Example: 7fbc8c75-4675-4d39-a5a4-0395ff8cd474
25
+ * ECS task id. Example: 1ed12abd-645c-4a05-9acf-739b9d790170
26
+
27
+ Examples:
28
+
29
+ $ bolts ssh my-ecs-service --cluster my-cluster # cluster is required or ~/.bolts/settings.yml
30
+ $ bolts ssh i-067c5e3f026c1e801
31
+ $ bolts ssh 7fbc8c75-4675-4d39-a5a4-0395ff8cd474
32
+ $ bolts ssh 1ed12abd-645c-4a05-9acf-739b9d790170
33
+ EOL
34
+ end
35
+
36
+ def ecs_exec
37
+ <<-EOL
38
+ Ssh into an ECS container instance, finds a running docker container associated
39
+ with the service and docker exec's into it.
40
+
41
+ Examples:
42
+
43
+ $ bolts ecs-exec my-service --cluster my-cluster
44
+ EOL
45
+ end
46
+
47
+ def ecs_run
48
+ <<-EOL
49
+ Ssh into an ECS container instance and runs a docker container using the same
50
+ environment and image as the specified running service.
51
+
52
+ Examples:
53
+
54
+ $ bolts ecs-run my-service --cluster my-cluster
55
+ $ bolts ecs-run my-service --cluster my-cluster
56
+
57
+ # If there are flags in the command that you want to pass down to the docker
58
+ run command you will need to put the command in single quotes. This is due to
59
+ the way Thor (what this tool uses) parses options.
60
+
61
+ $ bolts ecs-run --cluster prod-hi hi-web-prod 'rake -T'
62
+ EOL
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,32 @@
1
+ module Bolts
2
+ # To include this module must have this in initialize:
3
+ #
4
+ # def initialize(optiions, ...)
5
+ # @options = options
6
+ # ...
7
+ # end
8
+ #
9
+ # So @options must be set
10
+ module Defaults
11
+ # user: 123456789.dkr.ecr.us-east-1.amazonaws.com/bolts
12
+ # service_cluster:
13
+ # default: prod-lo
14
+ # hi-web-prod: prod-hi
15
+ # hi-clock-prod: prod-lo
16
+ # hi-worker-prod: prod-lo
17
+ #
18
+ # Assumes that @service is set in the class that the Defaults module is included in.
19
+ def default_cluster
20
+ service_cluster = settings.data["service_cluster"]
21
+ service_cluster[@service] || service_cluster["default"]
22
+ end
23
+
24
+ def default_user
25
+ settings.data["user"] || "ec2-user"
26
+ end
27
+
28
+ def settings
29
+ @settings ||= Settings.new(@options[:project_root])
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,80 @@
1
+ require 'fileutils'
2
+
3
+ module Bolts
4
+ class Docker < Ssh
5
+ def setup
6
+ check_service_exists!
7
+ check_tasks_running!
8
+ create_container_data
9
+ black_hole = " > /dev/null" unless @options[:verbose]
10
+ black_hold = ''
11
+ execute("scp -r #{data_path} #{ssh_host}:#{data_path} #{black_hole}")
12
+ FileUtils.rm_rf(data_path) # clean up locally
13
+ # the docker-exec.sh cleans up after itself and blows away /tmp/bolts
14
+ end
15
+
16
+ def exec
17
+ setup
18
+ docker_exec = "/tmp/bolts/bash_scripts/docker-exec.sh"
19
+ kernel_exec("ssh", "-t", ssh_host, "bash #{docker_exec}")
20
+ end
21
+
22
+ # I cannot name this run like 'docker run' because run is a keyword in Thor.
23
+ def run
24
+ setup
25
+ docker_run = "/tmp/bolts/bash_scripts/docker-run.sh"
26
+ # args = ["ssh", ssh_host, "bash #{docker_run} #{options[:docker_options]}"].compact
27
+ args = ["ssh", "-t", ssh_host, "bash", docker_run, @options[:docker_command]].compact
28
+ kernel_exec(*args)
29
+ end
30
+
31
+ def data_path
32
+ "/tmp/bolts"
33
+ end
34
+
35
+ def execute(command)
36
+ puts "Running: #{command}"
37
+ system(command)
38
+ end
39
+
40
+ # Data that is needed in order to run a new docker container that mimics the
41
+ # docker container that is already running:
42
+ # * task_arn
43
+ # * env_vars
44
+ # * image
45
+ def create_container_data
46
+ # For container env_vars and image info.
47
+ task_definition_arn = task.task_definition_arn # task is a method in the superclass: Ssh
48
+ response = ecs.describe_task_definition(task_definition: task_definition_arn)
49
+ task_definition = response.to_h[:task_definition]
50
+ container_definition = task_definition[:container_definitions].first # assumes care about the first container definition
51
+ env_file_data = env_file_data(container_definition[:environment])
52
+
53
+ bolts_folder = "/tmp/bolts"
54
+ FileUtils.mkdir_p(bolts_folder) unless File.exist?(bolts_folder)
55
+ IO.write("/tmp/bolts/task-arn.txt", task_arns.first)
56
+ IO.write("/tmp/bolts/docker-image.txt", container_definition[:image])
57
+ IO.write("/tmp/bolts/env-file.txt", env_file_data)
58
+ FileUtils.cp_r(bash_scripts, "/tmp/bolts")
59
+ end
60
+
61
+ # environment - [{:name=>"AUTH_TOKEN", :value=>"xxx"}, {:name=>"RAILS_LOG_TO_STDOUT", :value=>"1"}]
62
+ #
63
+ # Returns String with a simple form, the docker --env-file format
64
+ #
65
+ # AUTH_TOKEN=xxx
66
+ # RAILS_LOG_TO_STDOUT=1
67
+ def env_file_data(environment)
68
+ variables = []
69
+ environment.each do |item|
70
+ variables << "#{item[:name]}=#{item[:value]}"
71
+ end
72
+ variables.join("\n")
73
+ end
74
+
75
+ def bash_scripts
76
+ File.expand_path("../../bash_scripts", __FILE__)
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1,41 @@
1
+ require 'yaml'
2
+
3
+ module Bolts
4
+ class Settings
5
+ def initialize(project_root='.')
6
+ @project_root = project_root
7
+ end
8
+
9
+ def data
10
+ return @data if @data
11
+
12
+ @data = defaults
13
+ if File.exist?(settings_path)
14
+ custom_data = YAML.load_file(settings_path)
15
+ @data.merge!(custom_data)
16
+ ensure_default_cluster(@data)
17
+ end
18
+ @data
19
+ end
20
+
21
+ def defaults
22
+ {
23
+ "user" => "ec2-user",
24
+ "service_cluster" => {"default" => "default"}
25
+ }
26
+ end
27
+
28
+ # When user's .bolts/settings.yml lack the default cluster, we add it on.
29
+ # Otherwise the user get confusing and scary aws-sdk-core/param_validator errors:
30
+ # Example: https://gist.github.com/bolts/67b9a68a77363b908d1c36047bc2709a
31
+ def ensure_default_cluster(data)
32
+ unless @data["service_cluster"]["default"]
33
+ @data["service_cluster"]["default"] = "default"
34
+ end
35
+ end
36
+
37
+ def settings_path
38
+ "#{ENV['HOME']}/.bolts/settings.yml"
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,148 @@
1
+ require 'colorize'
2
+
3
+ module Bolts
4
+ class Ssh
5
+ include Defaults
6
+ include AwsServices
7
+
8
+ def initialize(identifier, options)
9
+ @options = options
10
+ @identifier = identifier
11
+ @service = @identifier # always set service even though sometimes it is not the identifier
12
+ @cluster = options[:cluster] || default_cluster
13
+ @user = options[:user] || default_user
14
+ end
15
+
16
+ def run
17
+ exit_on_specs
18
+ check_cluster_exists! unless instance_id?
19
+ kernel_exec("ssh", ssh_host)
20
+ end
21
+
22
+ # hack to test CLI call logic up to this class
23
+ def exit_on_specs
24
+ if ENV['TEST']
25
+ puts "ssh into ..."
26
+ exit 0
27
+ end
28
+ end
29
+
30
+ # used by child Classes
31
+ def ssh_host
32
+ @ssh_host ||= build_ssh_host
33
+ end
34
+
35
+ def build_ssh_host
36
+ ec2_instance_id = if instance_id?
37
+ @identifier
38
+ elsif container_instance_or_task_arn?
39
+ find_by_container_instance(@identifier) ||
40
+ find_by_task(@identifier)
41
+ else # service name
42
+ check_service_exists!
43
+ check_tasks_running!
44
+ container_instance_arn = task.container_instance_arn
45
+ find_by_container_instance(container_instance_arn)
46
+ end
47
+ instance_hostname(ec2_instance_id)
48
+ end
49
+
50
+ def find_by_task(task_arn)
51
+ response = ecs.describe_tasks(
52
+ cluster: @cluster,
53
+ tasks: [task_arn])
54
+ task = response.tasks.first
55
+ unless task
56
+ puts "Unable to find a #{task_arn.green} container instance or task in the #{@cluster.green} cluster."
57
+ exit 1
58
+ end
59
+ find_by_container_instance(task.container_instance_arn)
60
+ end
61
+
62
+ def find_by_container_instance(container_instance_arn)
63
+ response = ecs.describe_container_instances(
64
+ cluster: @cluster,
65
+ container_instances: [container_instance_arn])
66
+ container_instance = response.container_instances.first
67
+ unless container_instance
68
+ return false
69
+ end
70
+ ec2_instance_id = container_instance.ec2_instance_id
71
+ end
72
+
73
+ def check_cluster_exists!
74
+ cluster = ecs.describe_clusters(clusters: [@cluster]).clusters.first
75
+ unless cluster
76
+ puts "The #{@cluster.green} cluster does not exist. Are you sure you specified the right cluster?"
77
+ exit 1
78
+ end
79
+ end
80
+
81
+ def check_service_exists!
82
+ service = ecs.describe_services(services: [@service], cluster: @cluster).services.first
83
+ unless service
84
+ puts "The #{@service.green} service does not exist in #{@cluster.green} cluster. Are you sure you specified the right service and cluster?"
85
+ exit 1
86
+ end
87
+ end
88
+
89
+ def check_tasks_running!
90
+ if task_arns.empty?
91
+ puts "Unable to find a running task that belongs to the #{@service} service on the #{@cluster} cluster."
92
+ puts "There must be a running task in order for bolts to look up an container instance."
93
+ exit 1
94
+ end
95
+ end
96
+
97
+ def instance_hostname(ec2_instance_id)
98
+ instance = ec2.describe_instances(instance_ids: [ec2_instance_id]).reservations[0].instances[0]
99
+ # struct Aws::EC2::Types::Instance
100
+ # http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Types/Instance.html
101
+ host = instance.public_dns_name
102
+ "#{@user}@#{host}"
103
+ end
104
+
105
+ def task_arns
106
+ @task_arns ||= ecs.list_tasks(cluster: @cluster, service_name: @service).task_arns
107
+ end
108
+
109
+ # Only need one container instance to ssh into so we'll just use the first.
110
+ # Useful to have this in a method for subclasses like Bolts::Exec.
111
+ def task
112
+ @task ||= ecs.describe_tasks(cluster: @cluster, tasks: [task_arns.first]).tasks.first
113
+ end
114
+
115
+
116
+ # Will use Kernel.exec so that the ssh process takes over this ruby process.
117
+ def kernel_exec(*args)
118
+ # append the optional command that can be provided to the ssh command
119
+ full_command = args + @options[:command]
120
+ puts "Running: #{full_command.join(' ')}"
121
+ # https://ruby-doc.org/core-2.3.1/Kernel.html#method-i-exec
122
+ # Using 2nd form
123
+ Kernel.exec(*full_command)
124
+ end
125
+
126
+ # Examples:
127
+ #
128
+ # Container instance ids:
129
+ # b748e59a-b679-42a7-b713-afb12294935b
130
+ # 9f1dadc7-4f67-41da-abec-ec08810bfbc9
131
+ #
132
+ # Task ids:
133
+ # 222c9e66-780b-4755-8c16-8670988e8011
134
+ # 6358f9c2-b231-4f5b-a59b-15bf19d52a15
135
+ #
136
+ # Container instance and task ids have the same format
137
+ def container_instance_or_task_arn?
138
+ @identifier =~ /.{8}-.{4}-.{4}-.{4}-.{12}/
139
+ end
140
+
141
+ # Examples:
142
+ # i-006a097bb10643e20
143
+ def instance_id?
144
+ @identifier =~ /i-.{17}/
145
+ end
146
+
147
+ end
148
+ end
@@ -0,0 +1,3 @@
1
+ module Bolts
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ # to run specs with what's remembered from vcr
4
+ # $ rake
5
+ #
6
+ # to run specs with new fresh data from aws api calls
7
+ # $ rake clean:vcr ; time rake
8
+ describe Bolts::CLI do
9
+ before(:all) do
10
+ @args = "--cluster default"
11
+ end
12
+
13
+ describe "bolts" do
14
+ it "should hello world" do
15
+ out = execute("bin/bolts ssh my-service #{@args}")
16
+ expect(out).to include("ssh into")
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ ENV['TEST'] = '1'
2
+
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+
6
+ require "pp"
7
+
8
+ root = File.expand_path('../../', __FILE__)
9
+ require "#{root}/lib/bolts"
10
+
11
+ module Helpers
12
+ def execute(cmd)
13
+ puts "Running: #{cmd}" if ENV['DEBUG']
14
+ out = `#{cmd}`
15
+ puts out if ENV['DEBUG']
16
+ out
17
+ end
18
+ end
19
+
20
+ RSpec.configure do |c|
21
+ c.include Helpers
22
+ end
metadata ADDED
@@ -0,0 +1,211 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bolts-ssh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tung Nguyen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hashie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: colorize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: aws-sdk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-bundler
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: guard-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: byebug
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: A collection of tools commands to debug EC2 and ECS quickly
154
+ email:
155
+ - tongueroo@gmail.com
156
+ executables:
157
+ - bolts
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - ".gitignore"
162
+ - ".rspec"
163
+ - Gemfile
164
+ - Gemfile.lock
165
+ - Guardfile
166
+ - LICENSE.txt
167
+ - README.md
168
+ - Rakefile
169
+ - bin/bolts
170
+ - bolts.gemspec
171
+ - lib/bash_scripts/docker-exec.sh
172
+ - lib/bash_scripts/docker-run.sh
173
+ - lib/bolts.rb
174
+ - lib/bolts/aws_services.rb
175
+ - lib/bolts/cli.rb
176
+ - lib/bolts/cli/help.rb
177
+ - lib/bolts/defaults.rb
178
+ - lib/bolts/docker.rb
179
+ - lib/bolts/settings.rb
180
+ - lib/bolts/ssh.rb
181
+ - lib/bolts/version.rb
182
+ - spec/lib/cli_spec.rb
183
+ - spec/spec_helper.rb
184
+ homepage: https://github.com/bolts/bolts
185
+ licenses:
186
+ - MIT
187
+ metadata: {}
188
+ post_install_message:
189
+ rdoc_options: []
190
+ require_paths:
191
+ - lib
192
+ required_ruby_version: !ruby/object:Gem::Requirement
193
+ requirements:
194
+ - - ">="
195
+ - !ruby/object:Gem::Version
196
+ version: '0'
197
+ required_rubygems_version: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ requirements: []
203
+ rubyforge_project:
204
+ rubygems_version: 2.5.2
205
+ signing_key:
206
+ specification_version: 4
207
+ summary: Ssh into an EC2 instance quickly. Docker run or docker exec with running
208
+ ECS container quickly.
209
+ test_files:
210
+ - spec/lib/cli_spec.rb
211
+ - spec/spec_helper.rb