awssh 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4fd2f1b3cf3b3eb5719170a5049b1164e417dc29
4
- data.tar.gz: a28e7d706464b1278bf00cc1f81e889f5c3371ad
3
+ metadata.gz: 7813fbd643c47e30330aa13e8df8c830694833a2
4
+ data.tar.gz: fa612f8c10bfc39b2b111ff8c89e702594c61071
5
5
  SHA512:
6
- metadata.gz: 248175e0a1006d5e1434fce9dcf8261c0b939126f11b11e07ea1323ca15fbe83bead4b1059651b6a771f7e162e36a53a7da7db5ea7311b33f4b3fc1816e388b5
7
- data.tar.gz: 1b7a8a62366d290db7249103227dc92e6ceb56ed95248cf2c7ed911c1af933c36bf6e453ac553fbb1718d19555a95932bba200592ea3b39557034f429be7d133
6
+ metadata.gz: bc4a46a8e7e69672c8165af90b92ea1c5f9b7dcfc3c76bbeac97737318ca60424dc27fd1a4ee12011bae3b63aaa8e394d1411e4453ce5fb45893638a6bc8cd80
7
+ data.tar.gz: 77f3cd627766fe012459567f412ee9f6030ecb01501cd1ded42fa1c121435b92a80b5fc088522609040beb4062c45305b690bc1f2160bcc5a422d21fba466b35
@@ -6,6 +6,7 @@ module Awssh
6
6
  config: '~/.awssh',
7
7
  multi: false,
8
8
  test: false,
9
+ list: false,
9
10
  }
10
11
  @config = {
11
12
  multi: 'csshX',
@@ -14,7 +15,9 @@ module Awssh
14
15
  user: nil,
15
16
  key: 'AWS ACCESS KEY ID',
16
17
  secret: 'AWS SECRET ACCESS KEY',
17
- domain: 'example.com'
18
+ domain: 'example.com',
19
+ cache: '~/.awssh.cache',
20
+ expires: 1.day
18
21
  }.stringify_keys
19
22
 
20
23
  @config_file = File.expand_path(@options[:config])
@@ -58,6 +61,14 @@ module Awssh
58
61
  opts.on('-u', '--user', 'override user setting') do |u|
59
62
  @config['user'] = u
60
63
  end
64
+ opts.on('-l', '--list', 'just list servers') do |l|
65
+ @options[:list] = true
66
+ end
67
+ opts.on('-U', '--update', 'just update the cache') do |u|
68
+ @options[:update] = true
69
+ get_servers
70
+ exit 0
71
+ end
61
72
  end.parse!(argv)
62
73
 
63
74
  @search = argv
@@ -70,22 +81,22 @@ module Awssh
70
81
  end
71
82
 
72
83
  def connect
73
- @servers = get_servers
84
+ @servers = find_servers
74
85
 
75
- puts "found: #{@servers.count}" if @options[:verbose]
76
- @servers.each do |s|
77
- puts "- #{s}"
78
- end if @options[:verbose]
79
-
80
- if @servers.count == 0
81
- puts "no servers found"
82
- exit 1
86
+ if @options[:verbose] || @options[:list]
87
+ print_list
83
88
  end
89
+ return if @options[:list]
84
90
 
85
91
  if @servers.count > 1 && !@options[:multi]
92
+ print_list
86
93
  puts "more than one server found, and multi is false"
87
94
  puts "set the -m flag to connect to more than one matched server"
88
- puts "servers: #{@servers.join(',')}" unless @options[:verbose]
95
+ exit 1
96
+ end
97
+
98
+ if @servers.count == 0
99
+ puts "no servers found"
89
100
  exit 1
90
101
  end
91
102
 
@@ -97,31 +108,62 @@ module Awssh
97
108
 
98
109
  private
99
110
 
100
- def get_servers
101
- @fog = Fog::Compute.new(provider: 'AWS', aws_access_key_id: @config['key'], aws_secret_access_key: @config['secret'], region: @config["region"])
102
- list = @fog.servers.all
111
+ def print_list
112
+ puts "found: #{@servers.count}"
113
+ @servers.each do |s|
114
+ puts "- #{s}"
115
+ end
116
+ end
117
+
118
+ def find_servers
119
+ servers = []
120
+ get_servers.each do |n|
121
+ fail = false
122
+ @search.each do |v|
123
+ if v =~ /^\^/
124
+ fail = true if n =~ /#{v.gsub(/^\^/, '')}/
125
+ else
126
+ fail = true unless n =~ /#{v}/
127
+ end
128
+ end
129
+ next if fail
130
+ servers << n
131
+ end
132
+ servers
133
+ end
103
134
 
135
+ def get_servers
136
+ list = get_cache do
137
+ server_names
138
+ end
104
139
  puts "total servers: #{list.count}" if @options[:verbose]
140
+ list.sort
141
+ end
105
142
 
106
- servers = []
107
- list.each do |s|
108
- n = s.tags['Name']
109
- if n
110
- fail = false
111
- @search.each do |v|
112
- if v =~ /^\^/
113
- fail = true if n =~ /#{v.gsub(/^\^/, '')}/
114
- else
115
- fail = true unless n =~ /#{v}/
143
+ def get_cache
144
+ if @config['cache']
145
+ file = File.expand_path(@config['cache'])
146
+ if File.exists?(file)
147
+ unless @options[:update]
148
+ if Time.now - File.mtime(file) < @config['expires']
149
+ return YAML.load_file(file)
116
150
  end
117
151
  end
118
- next if fail
119
- servers << n
120
- else
121
- puts "server #{s.id} does not have a name"
122
152
  end
153
+ puts "updating cache ..."
154
+ list = yield
155
+ File.open(file, "w+") { |f| f.write list.to_yaml }
156
+ return list
123
157
  end
124
- servers.sort
158
+ list = yield
159
+ return list
160
+ end
161
+
162
+ def server_names
163
+ puts "requesting servers ..."
164
+ @fog = Fog::Compute.new(provider: 'AWS', aws_access_key_id: @config['key'], aws_secret_access_key: @config['secret'], region: @config["region"])
165
+ list = @fog.servers.all
166
+ list.inject([]) { |a, e| a << e.tags['Name'] }
125
167
  end
126
168
 
127
169
  def get_command(servers)
@@ -2,7 +2,7 @@ module Awssh
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 5
5
+ TINY = 6
6
6
  TAG = nil
7
7
  LIST = [MAJOR, MINOR, TINY, TAG].compact
8
8
  STRING = LIST.join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awssh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shawn Catanzarite