ec2-ssh 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 72f253fec1905d28a541b4a90f5539f850690ff1
4
+ data.tar.gz: 5497db4af38b1ca7df990f23c18adbca843578f5
5
+ SHA512:
6
+ metadata.gz: d0b7adbbe570f304bed85d7ae171a1f871375061eeb64a914b590684b40b32e39f53cb6adcc4481f9414895f7aadc1e79e9bd09b12e7c21a2e366d4f95bb9935
7
+ data.tar.gz: 52078a2cd777a8477180f8e4a71fba99379c318b358e4a83a087fdfed5ddad0a5ffa2e2ff743d59c7529a78ccc37f6dfe4713d50e2400ba5553bb00b6e326ce1
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ *.gem
3
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015-2016 Glide Talk, LTD.
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
data/bin/ec2-ssh ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib]))
3
+ require 'thor'
4
+ require 'ec2-ssh'
5
+
6
+ def help
7
+ Ec2Ssh::Cli.start(%w{help connect})
8
+ end
9
+
10
+
11
+ begin
12
+ ENV["THOR_DEBUG"] = "1"
13
+ Ec2Ssh::Cli.start
14
+ rescue Thor::RequiredArgumentMissingError => e
15
+ puts "\e[31mMissing Arguments: #{e}\e[0m\n\n"
16
+ help
17
+ rescue Thor::InvocationError => e
18
+ puts "\e[31m#{e.to_s.gsub(/Usage:.+"/, '').chomp} but there's no such option\e[0m\n\n"
19
+ help
20
+ end
21
+
data/ec2-ssh.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ $:.push File.expand_path("../lib/ec2-ssh", __FILE__)
3
+
4
+ require File.expand_path('../lib/ec2-ssh', __FILE__)
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "ec2-ssh"
8
+ s.version = Ec2Ssh::VERSION
9
+ s.authors = ["Ami Mahloof"]
10
+ s.email = "ami.mahloof@gmail.com"
11
+ s.homepage = "https://github.com/innovia/ec2-ssh"
12
+ s.summary = "run commmands on multiple servers by tag or by autoscale group"
13
+ s.description = "EC2 SSH Utility"
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.files = `git ls-files`.split($\).reject{|n| n =~ %r[png|gif\z]}.reject{|n| n =~ %r[^(test|spec|features)/]}
16
+ s.add_runtime_dependency 'thor', '~> 0.19', '>= 0.19.1'
17
+ s.add_runtime_dependency 'aws-sdk', '~> 2.0.45', '>= 2.0.45'
18
+ s.add_runtime_dependency 'sshkit', '~> 1.7.1', '>= 1.7.1'
19
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ s.extra_rdoc_files = ['README.md', 'LICENSE']
21
+ s.license = 'MIT'
22
+ end
@@ -0,0 +1,61 @@
1
+ module Ec2Ssh::Cli::Aws
2
+ def aws_init(profile, region)
3
+ ENV['AWS_PROFILE'] = profile
4
+ Aws.config.update({region: region})
5
+
6
+ @region = region
7
+ @as = Aws::AutoScaling::Client.new()
8
+ @ec2 = Aws::EC2::Client.new()
9
+
10
+ say "Currently running user is: #{Aws::IAM::CurrentUser.new.arn}"
11
+ end
12
+
13
+ def get_auto_scale_groups
14
+ say "Fetching AutoScale Groups - please wait..."
15
+ @as_groups = @as.describe_auto_scaling_groups.auto_scaling_groups
16
+
17
+ as_group_names = @as_groups.inject([]) {|acc, asg| acc << asg.auto_scaling_group_name; acc }
18
+
19
+ as_selection = {}
20
+ as_group_names.each_with_index.inject(as_selection) {|acc, pair|
21
+ element, index = pair
22
+ as_selection[index] = element
23
+ acc
24
+ }
25
+
26
+ say "AutoScale Group in #{options[:region]}:\n"
27
+ as_selection.each {|k,v| say "#{k}: #{v}"}
28
+
29
+ selected_as = ask("Which server group do you want to ssh to?", color = :yellow)
30
+
31
+ get_instances('aws:autoscaling:groupName', as_selection[selected_as.to_i])
32
+ end
33
+
34
+ def get_instances(tag_key, tag_value)
35
+ @all_servers = []
36
+
37
+ response = @ec2.describe_instances({
38
+ filters: [
39
+ {
40
+ name: 'instance-state-code',
41
+ values: ['16']
42
+
43
+ },{
44
+ name: 'tag-key',
45
+ values: ["#{tag_key}"]
46
+ },{
47
+ name: 'tag-value',
48
+ values: ["#{tag_value}"]
49
+ }
50
+ ]
51
+ })
52
+
53
+ if !response.reservations.empty?
54
+ response.reservations.each {|r| r.instances.inject(@all_servers){|acc, k| acc << k.public_ip_address; acc}}
55
+ else
56
+ say "could not find any instances with the tag #{tag_key}: #{tag_value} on #{@region}"
57
+ end
58
+
59
+ say "All servers: #{@all_servers.size}"
60
+ end
61
+ end
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib]))
4
+
5
+ require 'aws-sdk'
6
+ require 'thor'
7
+ require 'pp'
8
+ require 'sshkit'
9
+ require 'sshkit/dsl'
10
+
11
+ class Ec2Ssh::Cli < Thor
12
+ include Thor::Actions
13
+ autoload :Aws, 'ec2-ssh/aws'
14
+ autoload :Ssh, 'ec2-ssh/ssh'
15
+ autoload :Utils, 'ec2-ssh/utils'
16
+
17
+ default_task :connect
18
+
19
+ desc "connect", "Connect to autoscale instance (random instance), Pass --cmd='whatever' to run a cmd on the server (use ; to seperate commands)"
20
+ method_option :cmd, :desc => 'commmand to run on remote servers', :required => true
21
+ method_option :profile, :desc => 'aws cli profile', :default => 'default'
22
+ method_option :region, :desc => "region", :default => 'us-east-1'
23
+ method_option :user, :aliases => 'u', :desc => 'run as user', :default => 'ec2-user'
24
+ method_option :parallel, :aliases => 'p', :desc => 'run in parallel'
25
+ method_option :sequence, :aliases => 's', :desc => 'run in sequence'
26
+ method_option :groups, :aliases => 'g', :desc => 'run in groups'
27
+ method_option :groups_limit, :aliases => 'l', :desc => 'limit', :type => :numeric
28
+ method_option :wait, :aliases => 'w', :desc => 'wait', :type => :numeric
29
+ method_option :as, :desc => 'get autoscale groups'
30
+ method_option :tag_key, :desc => 'tag key to filter instances by', :default => 'Name'
31
+ method_option :tag_value, :desc => 'tag value to filter instances by'
32
+ method_option :terminal, :aliases => 't', :desc => 'open terminal tabs for all servers'
33
+ def connect
34
+ extend Aws
35
+ extend Ssh
36
+ extend Utils
37
+ set_ssh(options[:user])
38
+ aws_init(options[:profile], options[:region])
39
+
40
+ if options[:as]
41
+ get_auto_scale_groups
42
+ elsif options[:tag_value]
43
+ get_instances(options[:tag_key].chomp, options[:tag_value].chomp)
44
+ end
45
+
46
+ if options[:terminal]
47
+ open_in_terminal
48
+ else
49
+
50
+ if options[:parallel] && options[:sequence]
51
+ say "You can't run both in sequence and in parallel at the same time"
52
+ exit 1
53
+ end
54
+
55
+ mode = :parallel if options[:parallel]
56
+ mode = :groups if options[:groups]
57
+ mode = :sequence if options[:sequence]
58
+
59
+ mode = :parallel if mode.nil?
60
+ dsl_options = {}
61
+ dsl_options[:in] = mode
62
+ dsl_options[:wait] = options[:wait] if options[:wait]
63
+ dsl_options[:limit] = options[:groups_limit] if options[:groups_limit]
64
+
65
+ say "dsl opts: #{dsl_options}", color = :cyan
66
+ ssh_to(options[:user], dsl_options, options[:cmd])
67
+ end
68
+ end
69
+
70
+ map ["-v", "--version"] => :version
71
+ desc "version", "version"
72
+ def version
73
+ say Ec2Ssh::ABOUT, color = :green
74
+ end
75
+
76
+ end
@@ -0,0 +1,21 @@
1
+ module Ec2Ssh::Cli::Ssh
2
+ def set_ssh(user)
3
+ ENV['SSHKIT_COLOR'] = 'TRUE'
4
+ SSHKit.config.output_verbosity = Logger::DEBUG
5
+ SSHKit::Backend::Netssh.configure { |ssh|
6
+ ssh.ssh_options = {
7
+ :user => user,
8
+ :paranoid => false,
9
+ :forward_agent => true,
10
+ :user_known_hosts_file => '/dev/null'
11
+ }
12
+ }
13
+ end
14
+
15
+ def ssh_to(user, dsl_options, cmd)
16
+ say "Running #{cmd} via ssh in #{dsl_options}", color = :cyan
17
+ on @all_servers, dsl_options do |host|
18
+ execute cmd
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module Ec2Ssh::Cli::Utils
2
+ def open_in_terminal
3
+ @all_servers.each do |server|
4
+ `osascript <<-eof
5
+ tell application "iTerm"
6
+ make new terminal
7
+ tell the current terminal
8
+ activate current session
9
+ launch session "Default Session"
10
+ tell the last session
11
+ set name to "#{server}"
12
+ write text "ssh ec2-user@#{server}"
13
+ end tell
14
+ end tell
15
+ end tell
16
+ eof`
17
+ end
18
+ end
19
+ end
data/lib/ec2-ssh.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Ec2Ssh
2
+ VERSION = "1.0.0"
3
+ ABOUT = "ec2-ssh v#{VERSION} (c) #{Time.now.strftime("2015-%Y")} @innovia"
4
+
5
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib]))
6
+
7
+ autoload :Cli, 'ec2-ssh/cli'
8
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ec2-ssh
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ami Mahloof
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-04 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.19'
20
+ - - '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 0.19.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.19'
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.19.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: aws-sdk
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ~>
38
+ - !ruby/object:Gem::Version
39
+ version: 2.0.45
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: 2.0.45
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ~>
48
+ - !ruby/object:Gem::Version
49
+ version: 2.0.45
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: 2.0.45
53
+ - !ruby/object:Gem::Dependency
54
+ name: sshkit
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ version: 1.7.1
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: 1.7.1
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.7.1
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: 1.7.1
73
+ description: EC2 SSH Utility
74
+ email: ami.mahloof@gmail.com
75
+ executables:
76
+ - ec2-ssh
77
+ extensions: []
78
+ extra_rdoc_files:
79
+ - README.md
80
+ - LICENSE
81
+ files:
82
+ - .gitignore
83
+ - LICENSE
84
+ - README.md
85
+ - Rakefile
86
+ - bin/ec2-ssh
87
+ - ec2-ssh.gemspec
88
+ - lib/ec2-ssh.rb
89
+ - lib/ec2-ssh/aws.rb
90
+ - lib/ec2-ssh/cli.rb
91
+ - lib/ec2-ssh/ssh.rb
92
+ - lib/ec2-ssh/utils.rb
93
+ homepage: https://github.com/innovia/ec2-ssh
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: 1.3.6
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.4.2
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: run commmands on multiple servers by tag or by autoscale group
117
+ test_files: []