sonic-screwdriver 1.4.0 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/bin/commit_docs.sh +26 -0
  3. data/.circleci/config.yml +72 -0
  4. data/.gitignore +2 -1
  5. data/CHANGELOG.md +29 -3
  6. data/Gemfile +3 -3
  7. data/Guardfile +17 -10
  8. data/LICENSE.txt +2 -2
  9. data/README.md +25 -28
  10. data/Rakefile +9 -2
  11. data/docs/_config.yml +3 -0
  12. data/docs/_docs/help.md +1 -1
  13. data/docs/_docs/install-bastion.md +5 -15
  14. data/docs/_docs/install.md +3 -13
  15. data/docs/_docs/next-steps.md +1 -1
  16. data/docs/_docs/settings.md +42 -56
  17. data/docs/_docs/tutorial-ecs-exec.md +16 -20
  18. data/docs/_docs/tutorial-ecs-sh.md +73 -0
  19. data/docs/_docs/tutorial-execute.md +106 -38
  20. data/docs/_docs/tutorial-ssh.md +15 -19
  21. data/docs/_docs/why-ec2-run-command.md +1 -1
  22. data/docs/_includes/commands.html +5 -5
  23. data/docs/_includes/content.html +5 -0
  24. data/docs/_includes/css/main.css +15 -9
  25. data/docs/_includes/css/sonic.css +7 -5
  26. data/docs/_includes/example.html +4 -4
  27. data/docs/_includes/footer.html +6 -4
  28. data/docs/_includes/reference.md +1 -0
  29. data/docs/_includes/subnav.html +2 -1
  30. data/docs/_reference/sonic-completion.md +44 -0
  31. data/docs/_reference/sonic-completion_script.md +25 -0
  32. data/docs/_reference/sonic-ecs-exec.md +30 -0
  33. data/docs/_reference/sonic-ecs-help.md +21 -0
  34. data/docs/_reference/sonic-ecs-sh.md +35 -0
  35. data/docs/_reference/sonic-ecs.md +25 -0
  36. data/docs/_reference/sonic-execute.md +85 -0
  37. data/docs/_reference/sonic-list.md +40 -0
  38. data/docs/_reference/sonic-ssh.md +86 -0
  39. data/docs/_reference/sonic-version.md +21 -0
  40. data/docs/bin/web +1 -1
  41. data/docs/img/tutorials/ec2-console-run-command.png +0 -0
  42. data/docs/quick-start.md +17 -22
  43. data/docs/reference.md +12 -0
  44. data/{bin → exe}/sonic +3 -3
  45. data/lib/bash_scripts/docker-exec.sh +1 -0
  46. data/lib/bash_scripts/docker-run.sh +8 -1
  47. data/lib/sonic.rb +11 -3
  48. data/lib/sonic/{aws_services.rb → aws_service.rb} +6 -1
  49. data/lib/sonic/base_command.rb +82 -0
  50. data/lib/sonic/checks.rb +2 -2
  51. data/lib/sonic/cli.rb +41 -29
  52. data/lib/sonic/command.rb +8 -22
  53. data/lib/sonic/completer.rb +161 -0
  54. data/lib/sonic/completer/script.rb +6 -0
  55. data/lib/sonic/completer/script.sh +10 -0
  56. data/lib/sonic/core.rb +15 -0
  57. data/lib/sonic/default/settings.yml +9 -16
  58. data/lib/sonic/docker.rb +30 -2
  59. data/lib/sonic/ecs.rb +22 -0
  60. data/lib/sonic/execute.rb +203 -51
  61. data/lib/sonic/help.rb +9 -0
  62. data/lib/sonic/help/command/send.md +10 -0
  63. data/lib/sonic/help/completion.md +22 -0
  64. data/lib/sonic/help/completion_script.md +3 -0
  65. data/lib/sonic/help/ecs/exec.md +8 -0
  66. data/lib/sonic/help/ecs/sh.md +13 -0
  67. data/lib/sonic/help/execute.md +59 -0
  68. data/lib/sonic/help/list.md +17 -0
  69. data/lib/sonic/help/ssh.md +60 -0
  70. data/lib/sonic/list.rb +5 -2
  71. data/lib/sonic/setting.rb +47 -0
  72. data/lib/sonic/ssh.rb +42 -23
  73. data/lib/sonic/ssh/identifier_detector.rb +7 -3
  74. data/lib/sonic/ui.rb +2 -2
  75. data/lib/sonic/version.rb +1 -1
  76. data/sonic.gemspec +14 -9
  77. data/spec/lib/cli_spec.rb +11 -11
  78. data/spec/lib/sonic/execute_spec.rb +1 -2
  79. data/spec/spec_helper.rb +18 -10
  80. metadata +115 -19
  81. data/Gemfile.lock +0 -134
  82. data/docs/_docs/tutorial-ecs-run.md +0 -100
  83. data/lib/sonic/cli/help.rb +0 -152
  84. data/lib/sonic/settings.rb +0 -115
@@ -1,100 +0,0 @@
1
- ---
2
- title: ECS Run
3
- ---
4
-
5
- The nice thing about the previous `ecs-exec` command we covered is that it allows you to get into the actual running container and debug with the exact environment that is on production. The cavaet with doing this is that we are affecting a live process in actual use. If you do something inadvertently wrong on the server it could affect users. Sometimes it is nice to start up a new container with the exact same environment as the other running containers but be isolated so you cannot affect live requests.
6
-
7
- The `sonic ecs-run` command is similar to the `sonic 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.
8
-
9
- ### sonic ecs-run
10
-
11
- ```sh
12
- sonic ecs-run [ECS_SERVICE] --cluster [ECS_CLUSTER]
13
- ```
14
-
15
- Here's an example:
16
-
17
- ```sh
18
- sonic ecs-run hi-web-stag
19
- ```
20
-
21
- You see something like this:
22
-
23
- ```sh
24
- $ sonic ecs-run hi-web-stag
25
- Running: scp -r /tmp/sonic ec2-user@34.211.195.71:/tmp/sonic > /dev/null
26
- Warning: Permanently added '34.211.195.71' (ECDSA) to the list of known hosts.
27
- => ssh -t ec2-user@34.211.195.71 bash /tmp/sonic/bash_scripts/docker-run.sh
28
- Warning: Permanently added '34.211.195.71' (ECDSA) to the list of known hosts.
29
- + exec bundle exec rails server -b 0.0.0.0
30
- => Booting WEBrick
31
- => Rails 4.2.3 application starting in development on http://0.0.0.0:3000
32
- => Run `rails server -h` for more startup options
33
- => Ctrl-C to shutdown server
34
- [2017-06-14 19:01:30] INFO WEBrick 1.3.1
35
- [2017-06-14 19:01:30] INFO ruby 2.3.3 (2016-11-21) [x86_64-linux]
36
- [2017-06-14 19:01:30] INFO WEBrick::HTTPServer#start: pid=1 port=3000
37
- ^C[2017-06-14 19:01:34] INFO going to shutdown ...
38
- [2017-06-14 19:01:34] INFO WEBrick::HTTPServer#start done.
39
- Exiting
40
- Connection to 34.211.195.71 closed.
41
- $
42
- ```
43
-
44
- In the above output a WEBrick server gets started. The reason this happens is because the Dockerfile default `CMD` in this project happens to start a webserver. Most of the time you probably want to start shell for debugging. To start a bash shell just tack the bash command at the end.
45
-
46
- ```sh
47
- $ sonic ecs-run hi-web-stag bash
48
- Running: scp -r /tmp/sonic ec2-user@34.211.195.71:/tmp/sonic > /dev/null
49
- Warning: Permanently added '34.211.195.71' (ECDSA) to the list of known hosts.
50
- => ssh -t ec2-user@34.211.195.71 bash /tmp/sonic/bash_scripts/docker-run.sh bash
51
- Warning: Permanently added '34.211.195.71' (ECDSA) to the list of known hosts.
52
- root@56a495dbd5cd:/app#
53
- ```
54
-
55
- You are now in a docker container running exactly the same environment as the other running containers with the `hi-web-stag` service. While this looks similiar to the `ecs-exec` command this container is a brand new process and is isolated from any live request. You can do whatever you want in this container and experiment to your heart's content.
56
-
57
- We can prove that this is a brand new docker container that is outside of ECS' knowledge. Let's ssh into the same instance and take a look at all the running docker containers in another terminal.
58
-
59
- ```sh
60
- $ sonic ssh hi-web-stag docker ps
61
- => ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ec2-user@34.211.195.71 docker ps
62
- Warning: Permanently added '34.211.195.71' (ECDSA) to the list of known hosts.
63
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
64
- 29e7c1253c46 tongueroo/hi:ufo-2017-06-13T14-48-08-0a9eea5 "bash" 54 seconds ago Up 53 seconds 3000/tcp cocky_goldstine
65
- fc4035f90bdc tongueroo/hi:ufo-2017-06-13T14-48-08-0a9eea5 "bin/web" About an hour ago Up About an hour 0.0.0.0:32768->3000/tcp ecs-hi-web-stag-11-web-9eb081978abad89a9701
66
- bf646ae7789a amazon/amazon-ecs-agent:latest "/agent" About an hour ago Up About an hour ecs-agent
67
- $
68
- ```
69
-
70
- The output shows that there is this extra runnning container called `cocky_goldstine`. This name does not look like the typical ECS managed running docker container: `ecs-hi-web-stag-11-web-9eb081978abad89a9701`. This is how we know that this is a container outside of ECS control.
71
-
72
- ```sh
73
- $ sonic ecs-run hi-web-stag bash
74
- Running: scp -r /tmp/sonic ec2-user@34.211.195.71:/tmp/sonic > /dev/null
75
- Warning: Permanently added '34.211.195.71' (ECDSA) to the list of known hosts.
76
- => ssh -t ec2-user@34.211.195.71 bash /tmp/sonic/bash_scripts/docker-run.sh bash
77
- Warning: Permanently added '34.211.195.71' (ECDSA) to the list of known hosts.
78
- root@29e7c1253c46:/app# exit
79
- exit
80
- Connection to 34.211.195.71 closed.
81
- $
82
- ```
83
-
84
- Let's exit out of the first terminal where you ran the original `sonic ecs-run` command and then list the running containers again.
85
-
86
- ```sh
87
- $ sonic ssh hi-web-stag docker ps
88
- => ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ec2-user@34.211.195.71 docker ps
89
- Warning: Permanently added '34.211.195.71' (ECDSA) to the list of known hosts.
90
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
91
- fc4035f90bdc tongueroo/hi:ufo-2017-06-13T14-48-08-0a9eea5 "bin/web" About an hour ago Up About an hour 0.0.0.0:32768->3000/tcp ecs-hi-web-stag-11-web-9eb081978abad89a9701
92
- bf646ae7789a amazon/amazon-ecs-agent:latest "/agent" About an hour ago Up About an hour ecs-agent
93
- $
94
- ```
95
-
96
- Zapped! The `cocky_goldstine` container that was created with `sonic ecs-run` is no more.
97
-
98
- <a id="prev" class="btn btn-basic" href="{% link _docs/tutorial-ecs-exec.md %}">Back</a>
99
- <a id="next" class="btn btn-primary" href="{% link _docs/tutorial-execute.md %}">Next Step</a>
100
- <p class="keyboard-tip">Pro tip: Use the <- and -> arrow keys to move back and forward.</p>
@@ -1,152 +0,0 @@
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 Sonic
14
- class CLI < Command
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 things. Examples of valid identifiers:
20
-
21
- * EC2 instance id. Example: i-067c5e3f026c1e801
22
- * EC2 tag value. Any tag value is search, the tag key does not matter only the tag value matters. Example: hi-web-prod
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
- When using ecs identifiers the `--cluster` option is required or can be set in ~/.sonic/settings.yml.
28
-
29
- Examples:
30
-
31
- $ sonic ssh i-067c5e3f026c1e801
32
- $ sonic ssh hi-web-prod
33
- $ sonic ssh --cluster my-cluster my-ecs-service
34
- $ sonic ssh 7fbc8c75-4675-4d39-a5a4-0395ff8cd474
35
- $ sonic ssh 1ed12abd-645c-4a05-9acf-739b9d790170
36
-
37
- Sonic ssh builds up the ssh command and calls it. For example, the following commands:
38
-
39
- sonic ssh i-027363802c6ff314f
40
-
41
- Translates to:
42
-
43
- ssh ec2-user@ec2-52-24-216-170.us-west-2.compute.amazonaws.com
44
-
45
- You can also tack on any command to be run at the end of the command. Example:
46
-
47
- $ sonic ssh i-027363802c6ff314f uptime
48
- => ssh ec2-user@ec2-52-24-216-170.us-west-2.compute.amazonaws.com uptime
49
- 15:57:02 up 18:21, 0 users, load average: 0.00, 0.01, 0.00
50
-
51
- Specifying pem keys:
52
-
53
- The recommended way to specify custom private keys is to use ssh-agent as covered here: https://blog.boltops.com/2017/09/21/3-ssh-tips-ssh-agent-tunnel-and-escaping-from-the-dead
54
-
55
- But you can also specify the pem key to use with the -i option. Example:
56
-
57
- $ sonic ssh -i ~/.ssh/id_rsa-custom ec2-user@ec2-52-24-216-170.us-west-2.compute.amazonaws.com
58
-
59
- Retry option:
60
-
61
- For newly launched instances, the instance's ssh access might not be quite ready. Typically, you must press up enter repeatedly until the instance is ready. Sonic ssh has a retry option that automates this. Example:
62
-
63
- $ sonic ssh -r i-027363802c6ff314f
64
-
65
- Bastion Host Support
66
-
67
- Sonic ssh also supports a bastion host.
68
-
69
- $ sonic ssh --bastion bastion.domain.com i-027363802c6ff314f
70
- $ sonic ssh --bastion user@bastion.domain.com i-027363802c6ff314f
71
-
72
- Here's what the output looks like:
73
-
74
- $ sonic ssh --bastion 34.211.223.3 i-0f7f833131a51ce35 uptime
75
- => ssh -At ec2-user@34.211.223.3 ssh ec2-user@10.10.110.135 uptime
76
- 17:57:59 up 37 min, 0 users, load average: 0.00, 0.02, 0.00
77
- Connection to 34.211.223.3 closed.
78
- $
79
- EOL
80
- end
81
-
82
- def ecs_exec
83
- <<-EOL
84
- Ssh into an ECS container instance, finds a running docker container associated
85
- with the service and docker exec's into it.
86
-
87
- Examples:
88
-
89
- $ sonic ecs-exec my-service --cluster my-cluster
90
-
91
- You can use a variety of identifiers. These include the ECS service name and task id.
92
- EOL
93
- end
94
-
95
- def ecs_run
96
- <<-EOL
97
- Ssh into an ECS container instance and runs a docker container using the same
98
- environment and image as the specified running service.
99
-
100
- Examples:
101
-
102
- $ sonic ecs-run --cluster my-cluster my-service
103
- $ sonic ecs-run --cluster my-cluster my-service
104
-
105
- # If there are flags in the command that you want to pass down to the docker
106
- run command you will need to put the command in single quotes. This is due to
107
- the way Thor (what this tool uses) parses options.
108
-
109
- $ sonic ecs-run --cluster prod-hi hi-web-prod 'rake -T'
110
- EOL
111
- end
112
-
113
- def execute
114
- <<-EOL
115
- Run as a command across a list of servers. A filter must be provided. The filter can be a mix of instance ids and ec2 tags. This command can also take a path to a file. To specify a path to a file use file:// at the beginning of your file.
116
-
117
- Examples:
118
-
119
- $ sonic execute hi-web-prod uptime
120
- $ sonic execute hi-web-prod,hi-worker-prod,hi-clock-prod uptime
121
- $ sonic execute i-030033c20c54bf149,i-030033c20c54bf150 uname -a
122
- $ sonic execute i-030033c20c54bf149 file://hello.sh
123
-
124
- You cannot mix instance ids and tag names in the filter.
125
- EOL
126
- end
127
-
128
- def list
129
- <<-EOL
130
- List ec2 servers. A filter must be provided. The filter can be a mix of instance ids and ec2 tags. sonic list will auto-detect the what type of filter it is filter appropriately. The filter for listing is optional.
131
-
132
- Examples:
133
-
134
- $ sonic list
135
- $ sonic list hi-web-prod
136
- $ sonic list hi-web-prod,hi-clock-prod
137
- $ sonic list i-09482b1a6e330fbf7
138
-
139
- Example Output:
140
-
141
- $ sonic list --header i-09482b1a6e330fbf7
142
- Instance Id Public IP Private IP Type
143
- i-09482b1a6e330fbf7 54.202.152.168 172.31.21.108 t2.small
144
- $
145
-
146
- You cannot mix instance ids and tag names in the filter.
147
- EOL
148
- end
149
- end
150
- end
151
- end
152
- end
@@ -1,115 +0,0 @@
1
- require 'yaml'
2
-
3
- module Sonic
4
- class Settings
5
- def initialize(project_root=nil)
6
- @project_root = project_root || '.'
7
- end
8
-
9
- def data
10
- return @data if @data
11
-
12
- project_file = "#{@project_root}/.sonic/settings.yml"
13
- project = File.exist?(project_file) ? YAML.load_file(project_file) : {}
14
-
15
- user_file = "#{home}/.sonic/settings.yml"
16
- user = File.exist?(user_file) ? YAML.load_file(user_file) : {}
17
-
18
- default_file = File.expand_path("../default/settings.yml", __FILE__)
19
- default = YAML.load_file(default_file)
20
-
21
- @data = default.merge(user.merge(project))
22
- ensure_default_cluster!(@data)
23
- ensure_default_bastion!(data)
24
- @data
25
- end
26
-
27
- # Public: Returns default cluster based on the ECS service name.
28
- #
29
- # service - ECS service
30
- # count - The Integer number of times to duplicate the text.
31
- #
32
- # The settings.yml format:
33
- #
34
- # service_cluster:
35
- # default: stag
36
- # hi-web-prod: prod
37
- # hi-clock-prod: prod
38
- # hi-worker-prod: prod
39
- # hi-web-stag: stag
40
- # hi-clock-stag: stag
41
- # hi-worker-stag: stag
42
- #
43
- # Examples
44
- #
45
- # default_cluster('hi-web-prod')
46
- # # => 'prod'
47
- # default_cluster('whatever')
48
- # # => 'stag'
49
- #
50
- # Returns the ECS cluster name.
51
- def default_cluster(service)
52
- service_cluster = data["service_cluster"]
53
- service_cluster[service] || service_cluster["default"]
54
- end
55
-
56
- # When user's .sonic/settings.yml lack the default cluster, we add it on.
57
- # Otherwise the user get confusing and scary aws-sdk-core/param_validator errors:
58
- # Example: https://gist.github.com/sonic/67b9a68a77363b908d1c36047bc2709a
59
- def ensure_default_cluster!(data)
60
- unless data["service_cluster"]["default"]
61
- data["service_cluster"]["default"] = "default"
62
- end
63
- data
64
- end
65
-
66
- # Public: Returns default bastion host.
67
- #
68
- # cluster - cluster provided by user
69
- #
70
- # The settings.yml format:
71
- #
72
- # bastion:
73
- # default: bastion.mydomain.com
74
- # prod: bastion.mydomain.com
75
- # stag: bastion-stag.mydomain.com
76
- #
77
- # Examples
78
- #
79
- # default_bastion('stag')
80
- # # => 'bastion-stag.mydomain.com'
81
- # default_bastion('whatever')
82
- # # => 'bastion.mydomain.com'
83
- #
84
- # Returns the bastion host that is mapped to the cluster
85
- def default_bastion(cluster)
86
- bastion = data["bastion"]
87
- bastion[cluster] || bastion["default"]
88
- end
89
-
90
- # When user's .sonic/settings.yml lack the default cluster, we add it on.
91
- def ensure_default_bastion!(data)
92
- unless data["bastion"] && data["bastion"].has_key?("default")
93
- data["bastion"] ||= {}
94
- data["bastion"]["default"] = nil
95
- end
96
- data
97
- end
98
-
99
- # By default bypass strict host key checking for convenience.
100
- # But user can overrride this.
101
- def host_key_check_options
102
- if data["host_key_check"] == true
103
- []
104
- else
105
- # disables host key checking
106
- %w[-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null]
107
- end
108
- end
109
-
110
- def home
111
- # hack but fast
112
- ENV['TEST'] ? "spec/fixtures/home" : ENV['HOME']
113
- end
114
- end
115
- end