ec2-list 0.2.2 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9972d4bc4b4d7eef77ab975b36502cb033b7c5bd
4
- data.tar.gz: 58ae1fbf6bc46b50d3745565d0a7f844048925bc
3
+ metadata.gz: c5fd967d53d4b91259043495c085b0d364943163
4
+ data.tar.gz: 586e5a1d8bb9dfdaf5f789d92289eb2d5ba2dcaa
5
5
  SHA512:
6
- metadata.gz: d255fcc8398b01d4dcc415be42a9462d5d5e7627e8c3e966662d71818c511d4a09240e20fde891fafd50dafa9a9e76857af774cdad8d711a3a87d4692d0c669f
7
- data.tar.gz: 88637671372a8c04ebfd4d77b917854760272e3596940c6beefc26030e32753d646eb9615f3db9e9fc1e07394526d871dcb6410772ba6a3b571b3d9544d169a7
6
+ metadata.gz: 67de679649c0b8eb170353bd014a9b35a52e63246ffd9928dfac9b6bc3bd95bcb473a3c026aca4c705ce24c15f616ed64386a09fa73f4c1b2931bf37bbbc0d7f
7
+ data.tar.gz: 37fa58cdf9c7423799f2f44e02c5892be6b2daecf8d8919cbc5cde8093f10e1ab45ca3fee133dcec58c59c3ef0ce4297576218e630fc33e70aad167b60f0b0a1
data/lib/ec2list/cli.rb CHANGED
@@ -29,7 +29,7 @@ module Ec2list
29
29
  private
30
30
 
31
31
  def display_columns
32
- [:id, :type, :tag, :status, :fqdn, :ip_addr, :since]
32
+ ec2_list.display_columns
33
33
  end
34
34
 
35
35
  def ec2_list
@@ -1,3 +1,3 @@
1
1
  module Ec2list
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/ec2list.rb CHANGED
@@ -18,27 +18,19 @@ module Ec2list
18
18
  end
19
19
 
20
20
  def result
21
- if @tag
22
- instance_list.select { |instance| instance[:tag].include?(@tag) }
23
- else
24
- instance_list
25
- end
21
+ instances.map { |instance|
22
+ display_columns.zip(display_columns.map { |col| instance.send(col) }).to_h
23
+ }
24
+ end
25
+
26
+ def display_columns
27
+ %i(id type tag status fqdn ip_addr since)
26
28
  end
27
29
 
28
30
  def instance_list
29
- instances.map { |instance|
30
- {
31
- id: instance.instance_id,
32
- type: instance.instance_type,
33
- status: instance.state.name,
34
- since: since_about(Time.now - instance.launch_time),
35
- tag: instance.tags.find { |tag| tag.key == 'Name' }.value.gsub(' ', '_'),
36
- fqdn: instance.public_dns_name,
37
- ip_addr: instance.public_ip_address
38
- }
39
- }.sort_by { |x| x[:tag] }
31
+ instances.sort_by(&:tag)
40
32
  end
41
-
33
+
42
34
  def values(keys)
43
35
  result.map { |x| keys.map { |k| x[k] }.join (' ') }.compact.sort
44
36
  end
@@ -46,9 +38,17 @@ module Ec2list
46
38
  def reservations
47
39
  ec2.describe_instances(filters: [filter]).reservations
48
40
  end
49
-
41
+
50
42
  def instances
51
- ec2.describe_instances(filters: [filter]).reservations.map { |x| x.instances }.flatten
43
+ if @tag
44
+ all_instances.select { |instance| instance.cont?(@tag) }
45
+ else
46
+ all_instances
47
+ end
48
+ end
49
+
50
+ def all_instances
51
+ reservations.map { |x| x.instances }.flatten.map { |ec2| Instance.new ec2 }
52
52
  end
53
53
 
54
54
  def filter
@@ -61,16 +61,47 @@ module Ec2list
61
61
  {}
62
62
  end
63
63
  end
64
+ end
65
+
66
+ class Instance
67
+ attr_accessor :id, :type, :status, :since, :tags, :fqdn, :ip_addr
68
+
69
+ def initialize(ec2)
70
+ @id = ec2.instance_id
71
+ @type = ec2.instance_type
72
+ @status = ec2.state.name
73
+ @since = since_about(Time.now - ec2.launch_time)
74
+ @tags = ec2.tags || []
75
+ @fqdn = ec2.public_dns_name
76
+ @ip_addr = ec2.public_ip_address
77
+ end
78
+
79
+ def name
80
+ name_tag = tags.find { |tag| tag.key == 'Name' }
81
+ if name_tag
82
+ name_tag.value.gsub(' ', '_')
83
+ else
84
+ nil
85
+ end
86
+ end
87
+
88
+ def tag
89
+ name
90
+ end
91
+
92
+ def cont?(tag)
93
+ name && name.include?(tag)
94
+ end
64
95
 
65
96
  private
66
97
 
67
- def since_about(second)
68
- if second > 86400
69
- "#{second.quo(86400).to_i}d"
70
- elsif second > 3600
71
- "#{second.quo(3600).to_i}h"
98
+ def since_about(since)
99
+ if since > 86400
100
+ "#{since.quo(86400).to_i}d"
101
+ elsif since > 3600
102
+ "#{since.quo(3600).to_i}h"
72
103
  else
73
- "#{second.quo(60).to_i}m"
104
+ "#{since.quo(60).to_i}m"
74
105
  end
75
106
  end
76
107
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ec2-list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jun Yokoyama
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-09 00:00:00.000000000 Z
11
+ date: 2016-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  version: '0'
119
119
  requirements: []
120
120
  rubyforge_project:
121
- rubygems_version: 2.4.5
121
+ rubygems_version: 2.5.1
122
122
  signing_key:
123
123
  specification_version: 4
124
124
  summary: display list of ec2 instances.