ec2-ssh 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +37 -37
- data/lib/ec2-ssh.rb +1 -1
- data/lib/ec2-ssh/aws.rb +11 -24
- data/lib/ec2-ssh/cli.rb +11 -20
- data/lib/ec2-ssh/ssh.rb +10 -34
- data/lib/ec2-ssh/utils.rb +10 -10
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1937a7b40c0d0eca79b23921c42e33aab59f5d22
|
4
|
+
data.tar.gz: 89a9a42e2726cda0b87bffc40dec71d3430fff9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10708e39a4966fa9e944775fa06ba878dd41c49250b0bfeca699d9b0d3759dfd5253d059d1610b6a67b8901b253a7fc7226709c4e69abab697c4732778e397f5
|
7
|
+
data.tar.gz: 4a54d77fb72beaea0d336e4e0e49c4189029d6e3b7f616acb6b9763fc86f3abf7f33bf34d687afd11edcd07a890514f4277ea02edd27a5cf5924c1a38ebbe16f
|
data/README.md
CHANGED
@@ -1,20 +1,51 @@
|
|
1
|
-
|
1
|
+
````bash
|
2
|
+
Usage:
|
3
|
+
ec2-ssh connect
|
4
|
+
|
5
|
+
Options:
|
6
|
+
[--cmd=CMD] # commmand to run on remote servers
|
7
|
+
[--profile=PROFILE] # Aws cli profile name as listed in ~/aws/credentials
|
8
|
+
# Default: default
|
9
|
+
[--region=REGION] # region
|
10
|
+
# Default: us-east-1
|
11
|
+
u, [--user=USER] # run as user
|
12
|
+
# Default: ec2-user
|
13
|
+
p, [--parallel=PARALLEL] # run in parallel
|
14
|
+
s, [--sequence=SEQUENCE] # run in sequence
|
15
|
+
g, [--groups=GROUPS] # run in groups
|
16
|
+
l, [--groups-limit=N] # limit
|
17
|
+
w, [--wait=N] # wait
|
18
|
+
[--as=AS] # filter by autoscale groups
|
19
|
+
[--tag-key=TAG_KEY] # tag key to filter instances by
|
20
|
+
# Default: Name
|
21
|
+
[--tag-value=TAG_VALUE] # tag value to filter instances by
|
22
|
+
t, [--terminal=TERMINAL] # open terminal tabs for all servers
|
23
|
+
c, [--capture-output=CAPTURE_OUTPUT] # capture output
|
24
|
+
[--upload=source,destination] # upload a file - source,destination (make sure seperate these by comma)
|
25
|
+
[--download=source,destination] # download a file - source,destination (make sure seperate these by comma)
|
26
|
+
|
27
|
+
Connect to autoscale instance (random instance), Pass --cmd='whatever' to run a cmd on the server (use ; to seperate commands)
|
28
|
+
````
|
29
|
+
|
30
|
+
This is a utility for connecting on ec2 to autoscale groups and run multiple commands either parallel sequential or in groups
|
2
31
|
|
3
32
|
(based on sshkit https://github.com/capistrano/sshkit)
|
4
33
|
|
5
34
|
use it where you need to upload / download / capture output/ exec commands
|
6
35
|
|
7
|
-
````txt
|
8
36
|
seperate commands with a semicolon
|
9
37
|
|
38
|
+
default values are:
|
39
|
+
````txt
|
10
40
|
default region: us-east-1
|
11
41
|
default user: ec2-user
|
12
|
-
default connnection
|
42
|
+
default connnection: parallel
|
43
|
+
default tag key: Name
|
13
44
|
````
|
14
45
|
|
15
|
-
|
46
|
+
--groups flag was designed to relieve problems (such as mass Git checkouts) where you rely on a contested resource that you don't want to DDOS by hitting it too hard.
|
16
47
|
|
17
|
-
|
48
|
+
--sequence flag is intended to be used for rolling restarts, amongst other similar use-cases.
|
18
49
|
|
19
50
|
if the instance is on a private subnet (given you have a VPN connection) it will fallback to connecting using the internal IP address
|
20
51
|
|
@@ -42,7 +73,7 @@ $ ec2-ssh --tag-value ElasticSearch --profile otherAccountName --cmd 'touch /tmp
|
|
42
73
|
````
|
43
74
|
-------------------
|
44
75
|
|
45
|
-
connect by selecting autoscale group
|
76
|
+
connect interactively by selecting from a list of autoscale group names
|
46
77
|
|
47
78
|
````bash
|
48
79
|
$ ec2-ssh --as --cmd 'touch /tmp/test'
|
@@ -56,34 +87,3 @@ simply selct the number corresponding to the autoscale group (limit of AWS SDk i
|
|
56
87
|
|
57
88
|
-------------------
|
58
89
|
|
59
|
-
|
60
|
-
##Possible command line options
|
61
|
-
````bash
|
62
|
-
Usage:
|
63
|
-
ec2-ssh connect
|
64
|
-
|
65
|
-
Options:
|
66
|
-
[--cmd=CMD] # commmand to run on remote servers
|
67
|
-
[--profile=PROFILE] # Aws cli profile name as listed in ~/aws/credentials
|
68
|
-
# Default: default
|
69
|
-
[--region=REGION] # region
|
70
|
-
# Default: us-east-1
|
71
|
-
u, [--user=USER] # run as user
|
72
|
-
# Default: ec2-user
|
73
|
-
p, [--parallel=PARALLEL] # run in parallel
|
74
|
-
s, [--sequence=SEQUENCE] # run in sequence
|
75
|
-
g, [--groups=GROUPS] # run in groups
|
76
|
-
l, [--groups-limit=N] # limit
|
77
|
-
w, [--wait=N] # wait
|
78
|
-
[--as=AS] # filter by autoscale groups
|
79
|
-
[--tag-key=TAG_KEY] # tag key to filter instances by
|
80
|
-
# Default: Name
|
81
|
-
[--tag-value=TAG_VALUE] # tag value to filter instances by
|
82
|
-
t, [--terminal=TERMINAL] # open terminal tabs for all servers
|
83
|
-
c, [--capture-output=CAPTURE_OUTPUT] # capture output
|
84
|
-
[--upload=source,destination] # upload a file - source,destination (make sure seperate these by comma)
|
85
|
-
[--download=source,destination] # download a file - source,destination (make sure seperate these by comma)
|
86
|
-
[--pty=PTY] # enable pty for sudo operations
|
87
|
-
|
88
|
-
Connect to autoscale instance (random instance), Pass --cmd='whatever' to run a cmd on the server (use ; to seperate commands)
|
89
|
-
````
|
data/lib/ec2-ssh.rb
CHANGED
data/lib/ec2-ssh/aws.rb
CHANGED
@@ -7,40 +7,33 @@ module Ec2Ssh::Cli::Aws
|
|
7
7
|
@as = Aws::AutoScaling::Client.new()
|
8
8
|
@ec2 = Aws::EC2::Client.new()
|
9
9
|
|
10
|
-
say "Currently running
|
10
|
+
say "Currently running user is: #{Aws::IAM::CurrentUser.new.arn}"
|
11
11
|
end
|
12
12
|
|
13
|
-
def get_auto_scale_groups
|
14
|
-
say "Fetching AutoScale
|
15
|
-
@as_groups = @as.describe_auto_scaling_groups
|
13
|
+
def get_auto_scale_groups
|
14
|
+
say "Fetching AutoScale groups from #{@region} please wait..."
|
15
|
+
@as_groups = @as.describe_auto_scaling_groups.auto_scaling_groups
|
16
16
|
|
17
17
|
as_group_names = @as_groups.inject([]) {|acc, asg| acc << asg.auto_scaling_group_name; acc }
|
18
18
|
|
19
19
|
as_selection = {}
|
20
|
-
|
21
|
-
if grep_pattern
|
22
|
-
as_groups = as_group_names.grep(grep_pattern)
|
23
|
-
else
|
24
|
-
as_groups = as_group_names
|
25
|
-
end
|
26
|
-
as_groups.each_with_index.inject(as_selection) {|acc, pair|
|
20
|
+
as_group_names.each_with_index.inject(as_selection) {|acc, pair|
|
27
21
|
element, index = pair
|
28
22
|
as_selection[index] = element
|
29
23
|
acc
|
30
|
-
}
|
31
|
-
|
24
|
+
}
|
25
|
+
|
32
26
|
say "AutoScale Group in #{options[:region]}:\n"
|
33
27
|
as_selection.each {|k,v| say "#{k}: #{v}"}
|
34
28
|
|
35
29
|
selected_as = ask("Which server group do you want to ssh to?", color = :yellow)
|
36
|
-
|
30
|
+
|
37
31
|
get_instances('aws:autoscaling:groupName', as_selection[selected_as.to_i])
|
38
32
|
end
|
39
33
|
|
40
34
|
def get_instances(tag_key, tag_value)
|
41
35
|
@all_servers = []
|
42
36
|
|
43
|
-
say "Fetching instances with #{tag_key}: #{tag_value}", color = :white
|
44
37
|
response = @ec2.describe_instances({
|
45
38
|
filters: [
|
46
39
|
{
|
@@ -58,17 +51,11 @@ module Ec2Ssh::Cli::Aws
|
|
58
51
|
})
|
59
52
|
|
60
53
|
if !response.reservations.empty?
|
61
|
-
response.reservations.each {|r| r.instances.inject(@all_servers){|acc, k|
|
62
|
-
if k.public_ip_address.nil?
|
63
|
-
say "Could not find public ip address for instance: #{k.instance_id}, falling back to private ip address", color = :yellow
|
64
|
-
acc << k.private_ip_address; acc
|
65
|
-
else
|
66
|
-
acc << k.public_ip_address; acc
|
67
|
-
end
|
68
|
-
}}
|
54
|
+
response.reservations.each {|r| r.instances.inject(@all_servers){|acc, k| acc << k.public_ip_address; acc}}
|
69
55
|
else
|
70
56
|
say "could not find any instances with the tag #{tag_key}: #{tag_value} on #{@region}"
|
71
57
|
end
|
72
|
-
|
58
|
+
|
59
|
+
say "All servers: #{@all_servers.size}"
|
73
60
|
end
|
74
61
|
end
|
data/lib/ec2-ssh/cli.rb
CHANGED
@@ -18,7 +18,7 @@ class Ec2Ssh::Cli < Thor
|
|
18
18
|
|
19
19
|
desc "connect", "Connect to autoscale instance (random instance), Pass --cmd='whatever' to run a cmd on the server (use ; to seperate commands)"
|
20
20
|
method_option :cmd, :desc => 'commmand to run on remote servers'
|
21
|
-
method_option :profile, :desc => '
|
21
|
+
method_option :profile, :desc => 'aws cli profile', :default => 'default'
|
22
22
|
method_option :region, :desc => "region", :default => 'us-east-1'
|
23
23
|
method_option :user, :aliases => 'u', :desc => 'run as user', :default => 'ec2-user'
|
24
24
|
method_option :parallel, :aliases => 'p', :desc => 'run in parallel'
|
@@ -26,32 +26,24 @@ class Ec2Ssh::Cli < Thor
|
|
26
26
|
method_option :groups, :aliases => 'g', :desc => 'run in groups'
|
27
27
|
method_option :groups_limit, :aliases => 'l', :desc => 'limit', :type => :numeric
|
28
28
|
method_option :wait, :aliases => 'w', :desc => 'wait', :type => :numeric
|
29
|
-
method_option :as, :desc => '
|
30
|
-
method_option :grep, :desc => 'grep autoscale name when list is fecthed'
|
29
|
+
method_option :as, :desc => 'display interactive choices for autoscale groups'
|
31
30
|
method_option :tag_key, :desc => 'tag key to filter instances by', :default => 'Name'
|
32
31
|
method_option :tag_value, :desc => 'tag value to filter instances by'
|
33
32
|
method_option :terminal, :aliases => 't', :desc => 'open terminal tabs for all servers'
|
34
|
-
method_option :capture_output, :aliases => 'c', :desc => 'capture output'
|
35
|
-
method_option :upload, :desc => 'upload a file - source,destination (make sure seperate these by comma)', :banner => 'source,destination'
|
36
|
-
method_option :download, :desc => 'download a file - source,destination (make sure seperate these by comma)', :banner => 'source,destination'
|
37
|
-
method_option :pty, :desc => 'enable pty for sudo operations', :type => :boolean, :default => false
|
38
33
|
def connect
|
39
34
|
extend Aws
|
40
35
|
extend Ssh
|
41
36
|
extend Utils
|
42
|
-
set_ssh(options[:user], options[:pty])
|
43
|
-
aws_init(options[:profile], options[:region])
|
44
37
|
|
38
|
+
set_ssh(options[:user])
|
39
|
+
aws_init(options[:profile], options[:region])
|
40
|
+
|
45
41
|
if options[:as]
|
46
42
|
get_auto_scale_groups
|
47
43
|
elsif options[:tag_value]
|
48
44
|
get_instances(options[:tag_key].chomp, options[:tag_value].chomp)
|
49
|
-
else
|
50
|
-
say "No value provided for filtering instances", color = :red
|
51
|
-
Ec2Ssh::Cli.start(%w{help connect})
|
52
|
-
exit
|
53
45
|
end
|
54
|
-
|
46
|
+
|
55
47
|
if options[:terminal]
|
56
48
|
open_in_terminal
|
57
49
|
else
|
@@ -61,18 +53,17 @@ class Ec2Ssh::Cli < Thor
|
|
61
53
|
exit 1
|
62
54
|
end
|
63
55
|
|
64
|
-
mode = :parallel if options[:parallel]
|
56
|
+
mode = :parallel if options[:parallel] || mode.nil?
|
65
57
|
mode = :groups if options[:groups]
|
66
58
|
mode = :sequence if options[:sequence]
|
67
59
|
|
68
|
-
mode = :parallel if mode.nil?
|
69
60
|
dsl_options = {}
|
70
61
|
dsl_options[:in] = mode
|
71
62
|
dsl_options[:wait] = options[:wait] if options[:wait]
|
72
63
|
dsl_options[:limit] = options[:groups_limit] if options[:groups_limit]
|
73
|
-
|
74
|
-
say "
|
75
|
-
ssh_to(options[:user], dsl_options, options[:cmd]
|
64
|
+
|
65
|
+
say "user: #{options[:user]}\ndsl_options: #{dsl_options}\ncmd: #{options[:cmd]}", color = :cyan
|
66
|
+
ssh_to(options[:user], dsl_options, options[:cmd])
|
76
67
|
end
|
77
68
|
end
|
78
69
|
|
@@ -80,6 +71,6 @@ class Ec2Ssh::Cli < Thor
|
|
80
71
|
desc "version", "version"
|
81
72
|
def version
|
82
73
|
say Ec2Ssh::ABOUT, color = :green
|
83
|
-
end
|
74
|
+
end
|
84
75
|
|
85
76
|
end
|
data/lib/ec2-ssh/ssh.rb
CHANGED
@@ -1,47 +1,23 @@
|
|
1
|
+
require 'sshkit'
|
2
|
+
require 'sshkit/dsl'
|
3
|
+
include SSHKit::DSL
|
4
|
+
|
1
5
|
module Ec2Ssh::Cli::Ssh
|
2
|
-
def set_ssh(user
|
6
|
+
def set_ssh(user)
|
3
7
|
ENV['SSHKIT_COLOR'] = 'TRUE'
|
4
8
|
SSHKit.config.output_verbosity = Logger::DEBUG
|
5
|
-
|
9
|
+
SSHKit::Backend::Netssh.configure { |ssh|
|
10
|
+
ssh.ssh_options = {
|
6
11
|
:user => user,
|
7
12
|
:paranoid => false,
|
8
13
|
:forward_agent => true,
|
9
14
|
:user_known_hosts_file => '/dev/null'
|
15
|
+
}
|
10
16
|
}
|
11
|
-
|
12
|
-
SSHKit::Backend::Netssh.configure { |ssh|
|
13
|
-
ssh.ssh_options = ssh_options
|
14
|
-
ssh.pty = true if pty
|
15
|
-
}
|
16
|
-
|
17
|
-
|
18
17
|
end
|
19
18
|
|
20
|
-
def ssh_to(user, dsl_options, cmd
|
19
|
+
def ssh_to(user, dsl_options, cmd)
|
21
20
|
say "Running #{cmd} via ssh in #{dsl_options}", color = :cyan
|
22
|
-
on
|
23
|
-
|
24
|
-
if upload
|
25
|
-
file = upload.split(',').map { |e| e.strip }
|
26
|
-
source = File.expand_path(file.first)
|
27
|
-
destination = file.last
|
28
|
-
puts "uploading #{source} to #{destination}"
|
29
|
-
upload! source, destination
|
30
|
-
end
|
31
|
-
|
32
|
-
if download
|
33
|
-
file = download.split(',').map { |e| e.strip }
|
34
|
-
source = file.first
|
35
|
-
destination = File.expand_path(file.last)
|
36
|
-
puts "downloading #{source} to #{destination}.#{host}"
|
37
|
-
download! source, "#{destination}.#{host}"
|
38
|
-
end
|
39
|
-
|
40
|
-
if capture_output
|
41
|
-
puts capture cmd
|
42
|
-
elsif upload.nil? && download.nil?
|
43
|
-
execute cmd
|
44
|
-
end
|
45
|
-
end
|
21
|
+
on(@all_servers, in: dsl_options[:in]) { |host| execute cmd }
|
46
22
|
end
|
47
23
|
end
|
data/lib/ec2-ssh/utils.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
module Ec2Ssh::Cli::Utils
|
2
2
|
def open_in_terminal
|
3
3
|
@all_servers.each do |server|
|
4
|
-
say "Opening #{server} in a new tab", color = :cyan
|
5
4
|
`osascript <<-eof
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
5
|
+
tell application "iTerm"
|
6
|
+
tell current window
|
7
|
+
create tab with default profile
|
8
|
+
tell the current tab
|
9
|
+
activate current session
|
10
|
+
tell the last session
|
11
|
+
set name to "#{server}"
|
12
|
+
write text "ssh -o StrictHostKeyChecking=no ec2-user@#{server}"
|
13
|
+
end tell
|
14
14
|
end tell
|
15
15
|
end tell
|
16
|
-
|
16
|
+
end tell
|
17
17
|
eof`
|
18
18
|
end
|
19
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ec2-ssh
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ami Mahloof
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
110
|
version: 1.3.6
|
111
111
|
requirements: []
|
112
112
|
rubyforge_project:
|
113
|
-
rubygems_version: 2.4
|
113
|
+
rubygems_version: 2.6.4
|
114
114
|
signing_key:
|
115
115
|
specification_version: 4
|
116
116
|
summary: run commmands on multiple servers by tag or by autoscale group
|