awsssh 2.0.1

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.
Files changed (6) hide show
  1. checksums.yaml +15 -0
  2. data/README.md +43 -0
  3. data/awsssh.gemspec +16 -0
  4. data/bin/awsssh +5 -0
  5. data/lib/awsssh.rb +179 -0
  6. metadata +64 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NzVmZmEzNDNkNDZjNzMzZDEwNjZmOGZiYjA1NGZkN2UzYTM2Nzc2Yg==
5
+ data.tar.gz: !binary |-
6
+ YmIzNjlkMzE2ZDVhZjBhZGRjMDk3ZmZlZmQxMjE2YTVmYjc5N2E4ZQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OTg3NzFjOGJjN2UxM2NiZmU4ODdiNTU1MmQyMTlhZmRlM2Q4YzY2YWE2OWQw
10
+ YTQzMjliMzVhMThkNzEwZDQ4NDNkNDIzNTM1MWYxOWIwNDIyZjUyZWE3Njg2
11
+ Nzk3MGQ3YWQ3YTNjODc5YjQ5NmVkZmI3ZDBhMjBhNWYwYWMzYWI=
12
+ data.tar.gz: !binary |-
13
+ M2M1ZjkyYTM4ZmQyZDU3NzQ5NzgyZTgyYjlmZmJkMDhlMjc5ZDRiOGIwOWE2
14
+ ODkxNTVlZTI2YzQ2ZTEwZjA1YmZiYjM2ZmNjODQ2NjE2Yjk2YWI5MDAxYzhj
15
+ YzEzZjE4NjY3NDQwODBiOWJhMmM5YWJiOGVmMDc0MWM4MWUyM2M=
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # AWSSSH
2
+
3
+ Im [Mitarbeiterhandbuch](https://employeeapps.infopark.de/manual/03_Security) sind SSH-Configurationen vorgegeben, mit denen man alle AWS-EC2s abfängt.
4
+ Da ich aber nicht jedesmal nachschauen will, wie der Public-DNS Name einer instance ist, will ich das weiterhin in meiner ssh config pflegen und habe dort einträge wie:
5
+
6
+ ```Host sihl-live1
7
+ Hostname ec2-54-194-243-71.eu-west-1.compute.amazonaws.com```
8
+
9
+ da ein Aufruf von `$ ssh sihl-live1` dann aber die einstellung ignoriert der umweg über dieses script.
10
+
11
+ ## Installation
12
+
13
+ 1. Herunterladen
14
+ 2. an die Stelle verschieben, an der es am ende liegen soll
15
+ 3. wenn bei 2. nicht /usr/* angegeben ist `rake install` ausführen. Das erstellt ein Link in `/usr/local/bin`
16
+
17
+ ### Configurationen
18
+ 1. Das zum Umschalten der AWS Config muss das script über `awscfg <kundenname>` aufrufbar sein.
19
+ 2. Die Konfigurationsdateien müssen unter `/Users/<username>/.aws/` liegen.
20
+ 3. Die Konfigurationsdateien müssen den Namen `aws_config_<kundenname>` heißen.
21
+
22
+ ## Aufruf
23
+
24
+ `awsssh HOST`
25
+
26
+ **Beispiel**<br>
27
+ `awsssh sihl-live1`
28
+
29
+ ## Hilfe
30
+
31
+ `awsssh --help`<br>
32
+ Zeigt die Hilfe an
33
+
34
+ ## Inhalte
35
+
36
+ `Gemfile`: zum installieren aller benötigten gems<br>
37
+ `README.md`: diese README<br>
38
+ `Rakefile`: für die installation<br>
39
+ `awsssh.rb`: Erstellt die verbidnung (Programmlogik)<br>
40
+
41
+ ## Kontakt
42
+
43
+ Sebastian Thiele <[mailto:sebastian.thiele@infopark.de]>
data/awsssh.gemspec ADDED
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'awsssh'
3
+ s.version = '2.0.1'
4
+ s.date = '2014-02-27'
5
+ s.summary = "Connects you with OpsWorks EC2"
6
+ s.description = "This will connects you with an EC3 instace"
7
+ s.authors = ["Sebastian Thiele"]
8
+ s.email = [%w(Sebastian.Thiele infopark.de).join('@')]
9
+ s.license = "MIT"
10
+ s.files = `git ls-files`.split("\n")
11
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ s.homepage = "https://github.com/infopark/support/tree/master/scripte/ruby/awsssh"
13
+
14
+
15
+ s.add_runtime_dependency "net-ssh", "2.7.0"
16
+ end
data/bin/awsssh ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "awsssh"
4
+
5
+ Awsssh.new.do_start()
data/lib/awsssh.rb ADDED
@@ -0,0 +1,179 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'net/ssh'
4
+ require 'json'
5
+
6
+ class Awsssh
7
+
8
+ def do_start
9
+ if ARGV[0] == "--list-accounts"
10
+ list_accounts
11
+ elsif ARGV[0] == "--list-servers" && !ARGV[1].nil?
12
+ list_servers(ARGV[1])
13
+ elsif !ARGV[0].nil? && ARGV[0][0,2] != "--"
14
+ connect ARGV[0]
15
+ else
16
+ help
17
+ end
18
+ end
19
+
20
+ CONFIG_DIR = "/Users/#{ENV['USER']}/.aws/"
21
+ CONF_FILE = "aws_config_"
22
+ ##
23
+ # Renders the Help
24
+ #
25
+ # * *Returns* :
26
+ # - [String]
27
+ # usage: awsssh [<instance-name>|parameters]
28
+ # <instance-name> Name of the instance
29
+ # parameters:
30
+ # --help This help
31
+ # --list-accounts List all known AWS Accounts
32
+ # --list-servers <account> List all Server for an AWS Account
33
+
34
+ def help
35
+ length = 30
36
+ puts "usage: awsssh [<instance-name>|parameters]"
37
+ printf "\t %-#{length}s Name of the instance\n", "<instance-name>"
38
+ puts "parameters:"
39
+ printf "\t %-#{length}s This help\n", "--help"
40
+ printf "\t %-#{length}s List all known AWS Accounts\n", "--list-accounts"
41
+ printf "\t %-#{length}s List all Server for an AWS Account\n", "--list-servers <account>"
42
+ end
43
+
44
+
45
+ ##
46
+ # List stacks for a account
47
+ #
48
+ # * *Args* :
49
+ # - +account+ -> Account name
50
+ #
51
+ # * *Returns* :
52
+ # - [Array] StackIDs
53
+
54
+ def list_stacks(account)
55
+ awscfg(account)
56
+ stacks = JSON.parse(`aws opsworks describe-stacks`)
57
+ stack_ids = []
58
+ stacks['Stacks'].each do |stack|
59
+ stack_ids << stack['StackId']
60
+ end
61
+ return stack_ids
62
+ end
63
+
64
+
65
+ ##
66
+ # Read Stack
67
+ #
68
+ # * *Args* :
69
+ # - +stackid+ -> Stack ID
70
+ # - +account+ -> Account name
71
+ #
72
+ # * *Returns* :
73
+ # - Stecks JSON
74
+ #
75
+
76
+ def read_stack(stackid, account)
77
+ # `awscfg #{account}`
78
+ awscfg(account)
79
+ JSON.parse(`aws opsworks describe-instances --stack-id #{stackid}`)
80
+ end
81
+
82
+ ##
83
+ # Lists all AWS Accounts
84
+ #
85
+ # * *Returns* :
86
+ # - [String]
87
+ # This are the known AWS Accounts:
88
+ # - account
89
+
90
+ def list_accounts
91
+ length = 30
92
+ puts "This are the known AWS Accounts:"
93
+ config_files = Dir.entries(CONFIG_DIR)
94
+ config_files.each do |file|
95
+ if file[0,CONF_FILE.length] == CONF_FILE
96
+ file_part = file.split("_")
97
+ unless file_part[2].nil?
98
+ printf "\t- %-#{length}s\n", file_part[2]
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ ##
105
+ # Server Name
106
+ #
107
+ # * *Args* :
108
+ # - +stack+ -> Stack as JSON
109
+ #
110
+ # * *Returns* :
111
+ # - [String]
112
+ # - <servername> (<status>)
113
+
114
+ def server_name(stack)
115
+ stack["Instances"].each do |instance|
116
+ printf "\t- %-20s %s\n", instance["Hostname"], instance["Status"]
117
+ end
118
+ end
119
+
120
+ ##
121
+ # List all Servers for a given AWS Account
122
+ #
123
+ # * *Args* :
124
+ # - +account+ -> AWS Account name
125
+ #
126
+
127
+ def list_servers(account)
128
+ stacks = list_stacks account
129
+ stacks.each do |stack_id|
130
+ stack = read_stack stack_id, account
131
+ server_name stack
132
+ end
133
+ end
134
+
135
+ ##
136
+ # Establish the connection
137
+ #
138
+ # * *Args* :
139
+ # - +server+ -> Server name
140
+ #
141
+
142
+ def connect(server)
143
+ host = server.split("-")
144
+ public_dns = nil
145
+ stack_ids = list_stacks host[0]
146
+ stack_ids.each do |stack_id|
147
+ stack = read_stack(stack_id, host[0])
148
+ stack["Instances"].each do |i|
149
+ if i["Hostname"] == server
150
+ public_dns = i["PublicDns"]
151
+ break
152
+ end
153
+ end
154
+ break unless public_dns.nil?
155
+ end
156
+
157
+ if public_dns.nil?
158
+ puts "Server '#{server}' not found. Try ssh"
159
+ exec "ssh #{server}"
160
+ exit -1
161
+ end
162
+
163
+ puts "Connecting to #{server} (#{public_dns})"
164
+ exec "ssh #{public_dns}"
165
+
166
+ end
167
+
168
+ def awscfg(account)
169
+ if File.exists?(CONFIG_DIR + CONF_FILE + account)
170
+ `awscfg #{account}`
171
+ else
172
+ puts "No config #{CONF_FILE}#{account} found"
173
+ exit -1
174
+ end
175
+ end
176
+
177
+
178
+
179
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: awsssh
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Thiele
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-ssh
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.0
27
+ description: This will connects you with an EC3 instace
28
+ email:
29
+ - Sebastian.Thiele@infopark.de
30
+ executables:
31
+ - awsssh
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - README.md
36
+ - awsssh.gemspec
37
+ - bin/awsssh
38
+ - lib/awsssh.rb
39
+ homepage: https://github.com/infopark/support/tree/master/scripte/ruby/awsssh
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.2.1
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Connects you with OpsWorks EC2
63
+ test_files: []
64
+ has_rdoc: