eipmap 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +71 -0
- data/Rakefile +2 -0
- data/bin/eipmap +114 -0
- data/eipmap.gemspec +25 -0
- data/lib/eipmap/client.rb +109 -0
- data/lib/eipmap/driver.rb +71 -0
- data/lib/eipmap/dsl/context/domain.rb +19 -0
- data/lib/eipmap/dsl/context.rb +42 -0
- data/lib/eipmap/dsl/converter.rb +61 -0
- data/lib/eipmap/dsl.rb +9 -0
- data/lib/eipmap/exporter.rb +50 -0
- data/lib/eipmap/ext/string_ext.rb +25 -0
- data/lib/eipmap/logger.rb +28 -0
- data/lib/eipmap/version.rb +3 -0
- data/lib/eipmap.rb +17 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a2f1c0b232fc62fb36c85f964b4d3f94c15180e4
|
4
|
+
data.tar.gz: 97f4786d8f7e010c992f722c86f8c35bfd3bd649
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 210bd9e16c09a217d9fe01b975c4a17587772a197a1a36d3b2354d45a28a6317cc16492a4047c2746e1e3535d41cc9e5422c3ed1828b92872788b6f5c5f095b1
|
7
|
+
data.tar.gz: 401a8d6b544f153daea65898a8d698ea6d090e2d25d2b5a3444fd6d539544aa525d0e0c2a154566f8305e5db46d4ae0a3c63310891fdff38d987661722557752
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Genki Sugawara
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# Eipmap
|
2
|
+
|
3
|
+
Eipmap is a tool to manage Elastic IP Addresses (EIP).
|
4
|
+
|
5
|
+
It defines the state of EIP using DSL, and updates EIP according to DSL.
|
6
|
+
|
7
|
+
[![Gem Version](https://badge.fury.io/rb/eipmap.svg)](http://badge.fury.io/rb/eipmap)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'eipmap'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install eipmap
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```sh
|
28
|
+
export AWS_ACCESS_KEY_ID='...'
|
29
|
+
export AWS_SECRET_ACCESS_KEY='...'
|
30
|
+
export AWS_REGION='us-east-1'
|
31
|
+
eipmap -e -o EIPfile # export EIP
|
32
|
+
vi EIPfile
|
33
|
+
eipmap -a --dry-run
|
34
|
+
eipmap -a # apply `EIPfile` to EIP
|
35
|
+
```
|
36
|
+
|
37
|
+
## Help
|
38
|
+
|
39
|
+
```
|
40
|
+
Usage: eipmap [options]
|
41
|
+
-p, --profile PROFILE_NAME
|
42
|
+
--credentials-path PATH
|
43
|
+
-k, --access-key ACCESS_KEY
|
44
|
+
-s, --secret-key SECRET_KEY
|
45
|
+
-r, --region REGION
|
46
|
+
-a, --apply
|
47
|
+
-f, --file FILE
|
48
|
+
--dry-run
|
49
|
+
--allow-reassociation
|
50
|
+
-e, --export
|
51
|
+
-o, --output FILE
|
52
|
+
--no-color
|
53
|
+
--debug
|
54
|
+
```
|
55
|
+
|
56
|
+
## EIPfile example
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
require 'other/eipfile'
|
60
|
+
|
61
|
+
domain "standard" do
|
62
|
+
ip "54.256.256.1"
|
63
|
+
ip "54.256.256.2", :instance_id=>"i-12345678"
|
64
|
+
end
|
65
|
+
|
66
|
+
domain "vpc" do
|
67
|
+
ip "54.256.256.11", :network_interface_id=>"eni-12345678", :private_ip_address=>"10.0.1.1"
|
68
|
+
ip "54.256.256.12", :network_interface_id=>"eni-12345678" #, :private_ip_address=>"10.0.1.2" (optional)
|
69
|
+
ip "54.256.256.13"
|
70
|
+
end
|
71
|
+
```
|
data/Rakefile
ADDED
data/bin/eipmap
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$: << File.expand_path("#{File.dirname __FILE__}/../lib")
|
3
|
+
require 'rubygems'
|
4
|
+
require 'eipmap'
|
5
|
+
require 'optparse'
|
6
|
+
|
7
|
+
Version = Eipmap::VERSION
|
8
|
+
DEFAULT_FILENAME = 'EIPfile'
|
9
|
+
|
10
|
+
mode = nil
|
11
|
+
file = DEFAULT_FILENAME
|
12
|
+
output_file = '-'
|
13
|
+
|
14
|
+
options = {
|
15
|
+
:dry_run => false,
|
16
|
+
:color => true,
|
17
|
+
:debug => false,
|
18
|
+
}
|
19
|
+
|
20
|
+
ARGV.options do |opt|
|
21
|
+
begin
|
22
|
+
access_key = nil
|
23
|
+
secret_key = nil
|
24
|
+
region = nil
|
25
|
+
profile_name = nil
|
26
|
+
credentials_path = nil
|
27
|
+
|
28
|
+
opt.on('-p', '--profile PROFILE_NAME') {|v| profile_name = v }
|
29
|
+
opt.on('' , '--credentials-path PATH') {|v| credentials_path = v }
|
30
|
+
opt.on('-k', '--access-key ACCESS_KEY') {|v| access_key = v }
|
31
|
+
opt.on('-s', '--secret-key SECRET_KEY') {|v| secret_key = v }
|
32
|
+
opt.on('-r', '--region REGION') {|v| region = v }
|
33
|
+
opt.on('-a', '--apply') { mode = :apply }
|
34
|
+
opt.on('-f', '--file FILE') {|v| file = v }
|
35
|
+
opt.on('', '--dry-run') { options[:dry_run] = true }
|
36
|
+
opt.on('', '--allow-reassociation') { options[:allow_reassociation] = true }
|
37
|
+
opt.on('-e', '--export') { mode = :export }
|
38
|
+
opt.on('-o', '--output FILE') {|v| output_file = v }
|
39
|
+
opt.on('' , '--no-color') { options[:color] = false }
|
40
|
+
opt.on('' , '--debug') { options[:debug] = true }
|
41
|
+
opt.parse!
|
42
|
+
|
43
|
+
aws_opts = {}
|
44
|
+
|
45
|
+
if access_key and secret_key
|
46
|
+
aws_opts.update(
|
47
|
+
:access_key_id => access_key,
|
48
|
+
:secret_access_key => secret_key
|
49
|
+
)
|
50
|
+
elsif profile_name or credentials_path
|
51
|
+
credentials_opts = {}
|
52
|
+
credentials_opts[:profile_name] = profile_name if profile_name
|
53
|
+
credentials_opts[:path] = credentials_path if credentials_path
|
54
|
+
credentials = Aws::SharedCredentials.new(credentials_opts)
|
55
|
+
aws_opts[:credentials] = credentials
|
56
|
+
elsif (access_key and !secret_key) or (!access_key and secret_key) or mode.nil?
|
57
|
+
puts opt.help
|
58
|
+
exit 1
|
59
|
+
end
|
60
|
+
|
61
|
+
aws_opts[:region] = region if region
|
62
|
+
Aws.config.update(aws_opts)
|
63
|
+
rescue => e
|
64
|
+
$stderr.puts("[ERROR] #{e.message}")
|
65
|
+
exit 1
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
String.colorize = options[:color]
|
70
|
+
|
71
|
+
if options[:debug]
|
72
|
+
Aws.config.update(
|
73
|
+
:http_wire_trace => true,
|
74
|
+
:logger => Eipmap::Logger.instance
|
75
|
+
)
|
76
|
+
end
|
77
|
+
|
78
|
+
begin
|
79
|
+
logger = Eipmap::Logger.instance
|
80
|
+
logger.set_debug(options[:debug])
|
81
|
+
client = Eipmap::Client.new(options)
|
82
|
+
|
83
|
+
case mode
|
84
|
+
when :export
|
85
|
+
if output_file == '-'
|
86
|
+
logger.info('# Export Logs')
|
87
|
+
puts client.export
|
88
|
+
else
|
89
|
+
logger.info("Export Logs to `#{output_file}`")
|
90
|
+
open(output_file, 'wb') {|f| f.puts client.export }
|
91
|
+
end
|
92
|
+
when :apply
|
93
|
+
unless File.exist?(file)
|
94
|
+
raise "No EIPfile found (looking for: #{file})"
|
95
|
+
end
|
96
|
+
|
97
|
+
msg = "Apply `#{file}` to Elastic IP Addresses"
|
98
|
+
msg << ' (dry-run)' if options[:dry_run]
|
99
|
+
logger.info(msg)
|
100
|
+
|
101
|
+
updated = client.apply(file)
|
102
|
+
|
103
|
+
logger.info('No change'.intense_blue) unless updated
|
104
|
+
else
|
105
|
+
raise 'must not happen'
|
106
|
+
end
|
107
|
+
rescue => e
|
108
|
+
if options[:debug]
|
109
|
+
raise e
|
110
|
+
else
|
111
|
+
$stderr.puts("[ERROR] #{e.message}".red)
|
112
|
+
exit 1
|
113
|
+
end
|
114
|
+
end
|
data/eipmap.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'eipmap/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'eipmap'
|
8
|
+
spec.version = Eipmap::VERSION
|
9
|
+
spec.authors = ['Genki Sugawara']
|
10
|
+
spec.email = ['sgwr_dts@yahoo.co.jp']
|
11
|
+
spec.summary = %q{Eipmap is a tool to manage Elastic IP Addresses (EIP).}
|
12
|
+
spec.description = %q{Eipmap is a tool to manage Elastic IP Addresses (EIP). It defines the state of EIP using DSL, and updates EIP according to DSL.}
|
13
|
+
spec.homepage = 'https://github.com/winebarrel/eipmap'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'aws-sdk-core', '~> 2.0.2'
|
22
|
+
spec.add_dependency "term-ansicolor"
|
23
|
+
spec.add_development_dependency 'bundler'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
class Eipmap::Client
|
2
|
+
include Eipmap::Logger::Helper
|
3
|
+
|
4
|
+
def initialize(options = {})
|
5
|
+
@options = options
|
6
|
+
@ec2 = Aws::EC2::Client.new
|
7
|
+
@driver = Eipmap::Driver.new(@ec2, options)
|
8
|
+
end
|
9
|
+
|
10
|
+
def export
|
11
|
+
exported = Eipmap::Exporter.export(@ec2, @options)
|
12
|
+
Eipmap::DSL.convert(exported, @options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def apply(file)
|
16
|
+
walk(file)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def walk(file)
|
22
|
+
expected = load_file(file)
|
23
|
+
actual = Eipmap::Exporter.export(@ec2, @options)
|
24
|
+
updated = false
|
25
|
+
|
26
|
+
expected.each do |domain, expected_ips|
|
27
|
+
actual_ips = actual.delete(domain)
|
28
|
+
|
29
|
+
if actual_ips
|
30
|
+
result = walk_ips(domain, expected_ips, actual_ips)
|
31
|
+
updated ||= result
|
32
|
+
else
|
33
|
+
expected_ips.each do |ip, attrs|
|
34
|
+
warn_not_allocated(domain, ip)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
actual.each do |domain, ips|
|
40
|
+
ips.each do |ip, attrs|
|
41
|
+
warn_not_defined(domain, ip)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
updated
|
46
|
+
end
|
47
|
+
|
48
|
+
def walk_ips(domain, expected_ips, actual_ips)
|
49
|
+
updated = false
|
50
|
+
|
51
|
+
expected_ips.sort_by {|k, v| v.length }.each do |ip, expected_attrs|
|
52
|
+
actual_attrs = actual_ips.delete(ip)
|
53
|
+
|
54
|
+
if actual_attrs
|
55
|
+
result = walk_ip(domain, ip, expected_attrs, actual_attrs)
|
56
|
+
updated ||= result
|
57
|
+
else
|
58
|
+
warn_not_allocated(domain, ip)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
actual_ips.each do |ip, attrs|
|
63
|
+
warn_not_defined(domain, ip)
|
64
|
+
end
|
65
|
+
|
66
|
+
updated
|
67
|
+
end
|
68
|
+
|
69
|
+
def walk_ip(domain, ip, expected_attrs, actual_attrs)
|
70
|
+
return false if association_is_equal?(expected_attrs, actual_attrs)
|
71
|
+
@driver.update_association(domain, ip, expected_attrs, actual_attrs)
|
72
|
+
end
|
73
|
+
|
74
|
+
def association_is_equal?(expected_attrs, actual_attrs)
|
75
|
+
if expected_attrs[:network_interface_id]
|
76
|
+
result = (expected_attrs[:network_interface_id] == actual_attrs[:network_interface_id])
|
77
|
+
|
78
|
+
if expected_attrs[:private_ip_address]
|
79
|
+
result &= (expected_attrs[:private_ip_address] == actual_attrs[:private_ip_address])
|
80
|
+
end
|
81
|
+
|
82
|
+
result
|
83
|
+
elsif expected_attrs[:instance_id]
|
84
|
+
expected_attrs[:instance_id] == actual_attrs[:instance_id]
|
85
|
+
else
|
86
|
+
not actual_attrs[:network_interface_id] and not actual_attrs[:instance_id]
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def load_file(file)
|
91
|
+
if file.kind_of?(String)
|
92
|
+
open(file) do |f|
|
93
|
+
Eipmap::DSL.parse(f.read, file)
|
94
|
+
end
|
95
|
+
elsif file.respond_to?(:read)
|
96
|
+
Eipmap::DSL.parse(file.read, file.path)
|
97
|
+
else
|
98
|
+
raise TypeError, "can't convert #{file} into File"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def warn_not_allocated(domain, ip)
|
103
|
+
log(:warn, "#{domain} > #{ip}: IP address that are not allocated is defined", :color => :yellow)
|
104
|
+
end
|
105
|
+
|
106
|
+
def warn_not_defined(domain, ip)
|
107
|
+
log(:warn, "#{domain} > #{ip}: undefined IP address exist", :color => :yellow)
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
class Eipmap::Driver
|
2
|
+
include Eipmap::Logger::Helper
|
3
|
+
|
4
|
+
def initialize(ec2, options = {})
|
5
|
+
@ec2 = ec2
|
6
|
+
@options = options
|
7
|
+
end
|
8
|
+
|
9
|
+
def update_association(domain, ip, expected_attrs, actual_attrs)
|
10
|
+
if expected_attrs[:network_interface_id] or expected_attrs[:instance_id]
|
11
|
+
associate_address(domain, ip, expected_attrs, actual_attrs)
|
12
|
+
else
|
13
|
+
disassociate_address(domain, ip, expected_attrs, actual_attrs)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def associate_address(domain, ip, expected_attrs, actual_attrs)
|
20
|
+
params = {:dry_run => @options[:dry_run]}
|
21
|
+
log_info = {}
|
22
|
+
|
23
|
+
if (interface_id = expected_attrs[:network_interface_id])
|
24
|
+
params[:network_interface_id] = interface_id
|
25
|
+
log_info[:network_interface_id] = interface_id
|
26
|
+
params[:allocation_id] = actual_attrs[:allocation_id]
|
27
|
+
params[:allow_reassociation] = @options[:allow_reassociation]
|
28
|
+
|
29
|
+
if (private_ip = expected_attrs[:private_ip_address])
|
30
|
+
params[:private_ip_address] = private_ip
|
31
|
+
log_info[:private_ip_address] = private_ip
|
32
|
+
end
|
33
|
+
else
|
34
|
+
params[:public_ip] = ip
|
35
|
+
instance_id = expected_attrs[:instance_id]
|
36
|
+
params[:instance_id] = instance_id
|
37
|
+
log_info[:instance_id] = instance_id
|
38
|
+
end
|
39
|
+
|
40
|
+
log(:info, "#{domain} > #{ip}: Associate to #{log_info.inspect}", :color => :green)
|
41
|
+
|
42
|
+
unless_dry_run do
|
43
|
+
@ec2.associate_address(params)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def disassociate_address(domain, ip, expected_attrs, actual_attrs)
|
48
|
+
params = {:dry_run => @options[:dry_run]}
|
49
|
+
|
50
|
+
if actual_attrs[:association_id]
|
51
|
+
params[:association_id] = actual_attrs[:association_id]
|
52
|
+
else
|
53
|
+
params[:public_ip] = ip
|
54
|
+
end
|
55
|
+
|
56
|
+
log(:info, "#{domain} > #{ip}: Dissociate", :color => :red)
|
57
|
+
|
58
|
+
unless_dry_run do
|
59
|
+
@ec2.disassociate_address(params)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def unless_dry_run
|
64
|
+
if @options[:dry_run]
|
65
|
+
false
|
66
|
+
else
|
67
|
+
yield
|
68
|
+
true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Eipmap::DSL::Context::Domain
|
2
|
+
def initialize(domain, &block)
|
3
|
+
@domain = domain
|
4
|
+
@result = {}
|
5
|
+
instance_eval(&block)
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :result
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def ip(ip_address, options = {})
|
13
|
+
if @result[ip_address]
|
14
|
+
raise "#{@domain} > #{ip_address}: already defined"
|
15
|
+
end
|
16
|
+
|
17
|
+
@result[ip_address] = options
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class Eipmap::DSL::Context
|
2
|
+
def self.eval(dsl, path, options = {})
|
3
|
+
self.new(path, options) {
|
4
|
+
eval(dsl, binding, path)
|
5
|
+
}
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :result
|
9
|
+
|
10
|
+
def initialize(path, options = {}, &block)
|
11
|
+
@path = path
|
12
|
+
@options = options
|
13
|
+
@result = {}
|
14
|
+
instance_eval(&block)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def require(file)
|
20
|
+
eipfile = (file =~ %r|\A/|) ? file : File.expand_path(File.join(File.dirname(@path), file))
|
21
|
+
|
22
|
+
if File.exist?(eipfile)
|
23
|
+
instance_eval(File.read(eipfile), eipfile)
|
24
|
+
elsif File.exist?(eipfile + '.rb')
|
25
|
+
instance_eval(File.read(eipfile + '.rb'), eipfile + '.rb')
|
26
|
+
else
|
27
|
+
Kernel.require(file)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def domain(name, &block)
|
32
|
+
@result[name] ||= {}
|
33
|
+
|
34
|
+
Eipmap::DSL::Context::Domain.new(name, &block).result.each do |ip, attrs|
|
35
|
+
if @result[name][ip]
|
36
|
+
raise "#{name} > #{ip}: already defined"
|
37
|
+
end
|
38
|
+
|
39
|
+
@result[name][ip] = attrs
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
class Eipmap::DSL::Converter
|
2
|
+
def self.convert(exported, options = {})
|
3
|
+
self.new(exported, options).convert
|
4
|
+
end
|
5
|
+
|
6
|
+
def initialize(exported, options = {})
|
7
|
+
@exported = exported
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
def convert
|
12
|
+
output_domains(@exported)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def output_domains(domains)
|
18
|
+
domains.each.sort_by {|k, v| k }.map {|domain, ips|
|
19
|
+
output_domain(domain, ips)
|
20
|
+
}.join("\n")
|
21
|
+
end
|
22
|
+
|
23
|
+
def output_domain(domain, ips)
|
24
|
+
return nil if ips.empty?
|
25
|
+
|
26
|
+
<<-EOS
|
27
|
+
domain #{domain.inspect} do
|
28
|
+
#{output_ips(ips)}
|
29
|
+
end
|
30
|
+
EOS
|
31
|
+
end
|
32
|
+
|
33
|
+
def output_ips(ips)
|
34
|
+
ips.sort_by {|k, v| k }.map {|ip, attrs|
|
35
|
+
output_ip(ip, attrs)
|
36
|
+
}.join.strip
|
37
|
+
end
|
38
|
+
|
39
|
+
def output_ip(ip, attrs)
|
40
|
+
args = [ip.inspect]
|
41
|
+
ip_options = {}
|
42
|
+
|
43
|
+
if attrs[:network_interface_id]
|
44
|
+
ip_options[:network_interface_id] = attrs[:network_interface_id]
|
45
|
+
elsif attrs[:instance_id]
|
46
|
+
ip_options[:instance_id] = attrs[:instance_id]
|
47
|
+
end
|
48
|
+
|
49
|
+
if attrs[:private_ip_address]
|
50
|
+
ip_options[:private_ip_address] = attrs[:private_ip_address]
|
51
|
+
end
|
52
|
+
|
53
|
+
unless ip_options.empty?
|
54
|
+
args << ip_options.inspect.sub(/\A\{/, '').sub(/\}\z/, '')
|
55
|
+
end
|
56
|
+
|
57
|
+
<<-EOS
|
58
|
+
ip #{args.join(', ')}
|
59
|
+
EOS
|
60
|
+
end
|
61
|
+
end
|
data/lib/eipmap/dsl.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
class Eipmap::Exporter
|
2
|
+
def self.export(ec2, options = {})
|
3
|
+
self.new(ec2, options).export
|
4
|
+
end
|
5
|
+
|
6
|
+
def initialize(ec2, options = {})
|
7
|
+
@ec2 = ec2
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
def export
|
12
|
+
result = {}
|
13
|
+
|
14
|
+
@ec2.describe_addresses.each do |response|
|
15
|
+
response.addresses.each do |address|
|
16
|
+
export_address(address, result)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
return result
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def export_address(address, result)
|
26
|
+
domain = address.domain
|
27
|
+
public_ip = address.public_ip
|
28
|
+
result[domain] ||= {}
|
29
|
+
result[domain][public_ip] = {}
|
30
|
+
|
31
|
+
[
|
32
|
+
:instance_id,
|
33
|
+
:allocation_id,
|
34
|
+
:association_id,
|
35
|
+
:network_interface_id,
|
36
|
+
:private_ip_address,
|
37
|
+
].each do |key|
|
38
|
+
value = empty_to_nil(address[key])
|
39
|
+
result[domain][public_ip][key] = value if value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def empty_to_nil(str)
|
44
|
+
if str.nil? or str.empty?
|
45
|
+
nil
|
46
|
+
else
|
47
|
+
str
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class String
|
2
|
+
@@colorize = false
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def colorize=(value)
|
6
|
+
@@colorize = value
|
7
|
+
end
|
8
|
+
|
9
|
+
def colorize
|
10
|
+
@@colorize
|
11
|
+
end
|
12
|
+
end # of class methods
|
13
|
+
|
14
|
+
Term::ANSIColor::Attribute.named_attributes.map do |attribute|
|
15
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
16
|
+
def #{attribute.name}
|
17
|
+
if @@colorize
|
18
|
+
Term::ANSIColor.send(#{attribute.name.inspect}, self)
|
19
|
+
else
|
20
|
+
self
|
21
|
+
end
|
22
|
+
end
|
23
|
+
EOS
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class Eipmap::Logger < ::Logger
|
2
|
+
include Singleton
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
super($stdout)
|
6
|
+
|
7
|
+
self.formatter = proc do |severity, datetime, progname, msg|
|
8
|
+
"#{msg}\n"
|
9
|
+
end
|
10
|
+
|
11
|
+
self.level = Logger::INFO
|
12
|
+
end
|
13
|
+
|
14
|
+
def set_debug(value)
|
15
|
+
self.level = value ? Logger::DEBUG : Logger::INFO
|
16
|
+
end
|
17
|
+
|
18
|
+
module Helper
|
19
|
+
def log(level, message, options = {})
|
20
|
+
options = (@options || {}).merge(options)
|
21
|
+
message = "[#{level.to_s.upcase}] #{message}" unless level == :info
|
22
|
+
message << ' (dry-run)' if options[:dry_run]
|
23
|
+
message = message.send(options[:color]) if options[:color]
|
24
|
+
logger = options[:logger] || Eipmap::Logger.instance
|
25
|
+
logger.send(level, message)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/eipmap.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'singleton'
|
3
|
+
|
4
|
+
require 'aws-sdk-core'
|
5
|
+
require 'term/ansicolor'
|
6
|
+
|
7
|
+
module Eipmap; end
|
8
|
+
require 'eipmap/logger'
|
9
|
+
require 'eipmap/client'
|
10
|
+
require 'eipmap/driver'
|
11
|
+
require 'eipmap/dsl'
|
12
|
+
require 'eipmap/dsl/context'
|
13
|
+
require 'eipmap/dsl/context/domain'
|
14
|
+
require 'eipmap/dsl/converter'
|
15
|
+
require 'eipmap/exporter'
|
16
|
+
require 'eipmap/ext/string_ext'
|
17
|
+
require 'eipmap/version'
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: eipmap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Genki Sugawara
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: aws-sdk-core
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.0.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.0.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: term-ansicolor
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: Eipmap is a tool to manage Elastic IP Addresses (EIP). It defines the
|
70
|
+
state of EIP using DSL, and updates EIP according to DSL.
|
71
|
+
email:
|
72
|
+
- sgwr_dts@yahoo.co.jp
|
73
|
+
executables:
|
74
|
+
- eipmap
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- .gitignore
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/eipmap
|
84
|
+
- eipmap.gemspec
|
85
|
+
- lib/eipmap.rb
|
86
|
+
- lib/eipmap/client.rb
|
87
|
+
- lib/eipmap/driver.rb
|
88
|
+
- lib/eipmap/dsl.rb
|
89
|
+
- lib/eipmap/dsl/context.rb
|
90
|
+
- lib/eipmap/dsl/context/domain.rb
|
91
|
+
- lib/eipmap/dsl/converter.rb
|
92
|
+
- lib/eipmap/exporter.rb
|
93
|
+
- lib/eipmap/ext/string_ext.rb
|
94
|
+
- lib/eipmap/logger.rb
|
95
|
+
- lib/eipmap/version.rb
|
96
|
+
homepage: https://github.com/winebarrel/eipmap
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
metadata: {}
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 2.4.1
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: Eipmap is a tool to manage Elastic IP Addresses (EIP).
|
120
|
+
test_files: []
|