pec 0.1.4 → 0.1.5
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 +4 -4
- data/.travis.yml +3 -0
- data/Gemfile +1 -3
- data/README.md +4 -2
- data/lib/pec/cli.rb +38 -18
- data/lib/pec/configure.rb +5 -1
- data/lib/pec/configure/ethernet.rb +5 -2
- data/lib/pec/configure/user_data.rb +12 -14
- data/lib/pec/errors.rb +1 -0
- data/lib/pec/network/port.rb +3 -8
- data/lib/pec/version.rb +1 -1
- data/lib/pec/vm_director.rb +6 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12573783d7f7d2956311eac8ddafc10dd576ef54
|
4
|
+
data.tar.gz: c995f4586be47905890d741abe3c419733deff28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 532ce626a2742c53c8cd53869141b66b7cb455112a2a74a4e8b1208dac528512727aacbd696dede2975d7e0cf590b104a330824c2541b62853dceafc6a6465db
|
7
|
+
data.tar.gz: 912b3adf04d3d12b5d36cc5a0f4a410827bdbfb33a414f4f554287807b4ba0d68462847e0f591be05d8ab048c932f8b4ec47a8fa89a57e1ee1bfb474d8e18e9a
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# Pec
|
2
2
|
[](https://travis-ci.org/pyama86/pec)
|
3
|
+
[](https://codeclimate.com/github/pyama86/pec)
|
4
|
+
[](https://codeclimate.com/github/pyama86/pec/coverage)
|
3
5
|
|
4
|
-
OpenStack
|
5
|
-
|
6
|
+
OpenStackにおいて複数サーバの起動一括起動停止や、 DHCPサーバがない状況でのIP自動採番を実現します。
|
7
|
+
作って壊してが驚くほどかんたんに。
|
6
8
|
|
7
9
|
## Install
|
8
10
|
|
data/lib/pec/cli.rb
CHANGED
@@ -24,45 +24,65 @@ module Pec
|
|
24
24
|
|
25
25
|
desc 'up', 'create vm by Pec.yaml'
|
26
26
|
def up(host_name = nil)
|
27
|
-
config = Pec::Configure.new
|
28
|
-
config.load("Pec.yaml")
|
29
|
-
|
27
|
+
config = Pec::Configure.new("Pec.yaml")
|
30
28
|
director = Pec::VmDirector.new
|
31
29
|
|
32
|
-
config.each do |host|
|
33
|
-
next if !host_name.nil? && host.name != host_name
|
34
|
-
|
30
|
+
config.filter_by_host(host_name).each do |host|
|
35
31
|
begin
|
36
32
|
director.make(host)
|
37
33
|
rescue Pec::Errors::Error => e
|
38
|
-
|
39
|
-
puts "can't create server:#{host.name}"
|
34
|
+
pec_create_err_message(e, host)
|
40
35
|
rescue Excon::Errors::Error => e
|
41
|
-
|
36
|
+
excon_err_message(e)
|
42
37
|
end
|
43
38
|
end if config
|
44
39
|
rescue Errno::ENOENT => e
|
45
40
|
puts e
|
46
41
|
rescue Pec::Errors::Configure => e
|
47
|
-
|
42
|
+
pec_config_err_message
|
48
43
|
end
|
49
44
|
|
50
45
|
option :force , type: :boolean, aliases: "-f"
|
51
46
|
desc "destroy", "delete vm"
|
52
|
-
def destroy(
|
53
|
-
config = Pec::Configure.new
|
54
|
-
|
55
|
-
|
56
|
-
|
47
|
+
def destroy(host_name = nil)
|
48
|
+
config = Pec::Configure.new("Pec.yaml")
|
49
|
+
director = Pec::VmDirector.new
|
50
|
+
|
51
|
+
config.filter_by_host(host_name).each do |host|
|
57
52
|
begin
|
58
|
-
|
53
|
+
if options[:force] || yes?("#{host.name}: Are you sure you want to destroy the '#{host.name}' VM? [y/N]")
|
54
|
+
director.destroy!(host.name)
|
55
|
+
end
|
59
56
|
rescue Pec::Errors::Error => e
|
60
|
-
|
61
|
-
|
57
|
+
pec_delete_err_message(e, host)
|
58
|
+
rescue Excon::Errors::Error => e
|
59
|
+
excon_err_message(e)
|
62
60
|
end
|
63
61
|
end if config
|
64
62
|
rescue Errno::ENOENT => e
|
65
63
|
puts e
|
64
|
+
rescue Pec::Errors::Configure => e
|
65
|
+
pec_config_err_message
|
66
|
+
end
|
67
|
+
|
68
|
+
no_tasks do
|
69
|
+
def pec_create_err_message(e, host)
|
70
|
+
puts e
|
71
|
+
puts "can't create server:#{host.name}"
|
72
|
+
end
|
73
|
+
|
74
|
+
def pec_delete_err_message(e, host)
|
75
|
+
puts e
|
76
|
+
puts "can't create server:#{host.name}"
|
77
|
+
end
|
78
|
+
|
79
|
+
def pec_config_err_message
|
80
|
+
puts "configure can't load"
|
81
|
+
end
|
82
|
+
|
83
|
+
def excon_err_message(e)
|
84
|
+
JSON.parse(e.response[:body]).each { |e,m| puts "#{e}:#{m["message"]}" }
|
85
|
+
end
|
66
86
|
end
|
67
87
|
end
|
68
88
|
end
|
data/lib/pec/configure.rb
CHANGED
@@ -3,7 +3,7 @@ module Pec
|
|
3
3
|
class Configure
|
4
4
|
include Enumerable
|
5
5
|
|
6
|
-
def
|
6
|
+
def initialize(file_name)
|
7
7
|
YAML.load_file(file_name).to_hash.each do |config|
|
8
8
|
host = Pec::Configure::Host.load(config)
|
9
9
|
@configure ||= []
|
@@ -13,6 +13,10 @@ module Pec
|
|
13
13
|
raise(Pec::Errors::Configure, e)
|
14
14
|
end
|
15
15
|
|
16
|
+
def filter_by_host(host_name)
|
17
|
+
@configure.select {|h| host_name.nil? || host_name == h.name}
|
18
|
+
end
|
19
|
+
|
16
20
|
def each
|
17
21
|
@configure.each do |config|
|
18
22
|
yield config
|
@@ -11,14 +11,17 @@ module Pec
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
+
def find_port(ports)
|
15
|
+
ports.find { |p| p.name == @name }
|
16
|
+
end
|
17
|
+
|
14
18
|
class << self
|
15
19
|
def load(name, config)
|
16
20
|
self.new(config) if check_require_key(name, config) && check_network_key(name, config)
|
17
21
|
end
|
18
22
|
|
19
23
|
def check_require_key(name, config)
|
20
|
-
|
21
|
-
raise(Pec::Errors::Ethernet, "skip! #{name}: #{err} is required!") unless err.nil?
|
24
|
+
raise(Pec::Errors::Ethernet, "skip! #{name}: bootproto is required!") if config[1]["bootproto"].nil?
|
22
25
|
true
|
23
26
|
end
|
24
27
|
|
@@ -20,26 +20,24 @@ module Pec
|
|
20
20
|
|
21
21
|
def make_port_content(config, ports)
|
22
22
|
config.networks.map do |ether|
|
23
|
-
|
24
|
-
%w(name device).each do |k|
|
25
|
-
port_content[k] = ether.name unless ether.options.key?(k)
|
26
|
-
end
|
23
|
+
ifcfg_content = {}
|
27
24
|
|
28
|
-
|
29
|
-
|
30
|
-
|
25
|
+
ifcfg_content["bootproto"] = ether.bootproto
|
26
|
+
ifcfg_content["name"] = ether.options["name"] || ether.name
|
27
|
+
ifcfg_content["device"] = ether.options["device"] || ether.name
|
28
|
+
ifcfg_content["type"] = ether.options['type'] ||'Ethernet'
|
29
|
+
ifcfg_content["onboot"] = ether.options['onboot'] || 'yes'
|
30
|
+
ifcfg_content["hwaddr"] = ether.find_port(ports).mac_address
|
31
31
|
path = ether.options['path'] || "/etc/sysconfig/network-scripts/ifcfg-#{ether.name}"
|
32
32
|
|
33
|
-
port = ports.find {|p| p.name == ether.name}
|
34
|
-
port_content["hwaddr"] = port.mac_address
|
35
|
-
|
36
33
|
if ether.bootproto == "static"
|
37
|
-
|
38
|
-
|
34
|
+
ifcfg_content["netmask"] = ether.find_port(ports).netmask
|
35
|
+
ifcfg_content["ipaddr"] = ether.find_port(ports).ip_address
|
39
36
|
end
|
40
|
-
|
37
|
+
ifcfg_content.merge!(ether.options)
|
38
|
+
|
41
39
|
{
|
42
|
-
'content' =>
|
40
|
+
'content' => ifcfg_content.map {|k,v| "#{k.upcase}=#{v}"}.join("\n"),
|
43
41
|
'owner' => "root:root",
|
44
42
|
'path' => path,
|
45
43
|
'permissions' => "0644"
|
data/lib/pec/errors.rb
CHANGED
data/lib/pec/network/port.rb
CHANGED
@@ -15,11 +15,8 @@ module Pec
|
|
15
15
|
def assign!(ip)
|
16
16
|
# dhcp ip recycle
|
17
17
|
if request_any_address?(ip)
|
18
|
-
|
19
|
-
|
20
|
-
ip = IP.new("#{free["fixed_ips"][0]["ip_address"]}/#{ip.pfxlen}")
|
21
|
-
@config = free
|
22
|
-
end
|
18
|
+
@config = fetch_free_port
|
19
|
+
ip = IP.new("#{@config["fixed_ips"][0]["ip_address"]}/#{ip.pfxlen}") unless @config.nil?
|
23
20
|
end
|
24
21
|
|
25
22
|
case
|
@@ -79,9 +76,7 @@ module Pec
|
|
79
76
|
|
80
77
|
def create(ip)
|
81
78
|
options = { security_groups: @security_groups }
|
82
|
-
if ip.to_s != subnet["cidr"]
|
83
|
-
options.merge!({ fixed_ips: [{ subnet_id: @subnet["id"], ip_address: ip.to_addr}]})
|
84
|
-
end
|
79
|
+
options.merge!({ fixed_ips: [{ subnet_id: @subnet["id"], ip_address: ip.to_addr}]}) if ip.to_s != subnet["cidr"]
|
85
80
|
response = Fog::Network[:openstack].create_port(@subnet["network_id"], options)
|
86
81
|
|
87
82
|
if response
|
data/lib/pec/version.rb
CHANGED
data/lib/pec/vm_director.rb
CHANGED
@@ -22,6 +22,10 @@ module Pec
|
|
22
22
|
@compute.create(config.name, image_ref, flavor_ref, ports, options)
|
23
23
|
end
|
24
24
|
|
25
|
+
def destroy!(server_name)
|
26
|
+
@compute.destroy!(host.name)
|
27
|
+
end
|
28
|
+
|
25
29
|
def get_ports(config)
|
26
30
|
config.networks.map do |ether|
|
27
31
|
begin
|
@@ -42,7 +46,8 @@ module Pec
|
|
42
46
|
def get_security_group_id(security_groups)
|
43
47
|
security_groups.map do |name|
|
44
48
|
sg = @security_group.fetch(name)
|
45
|
-
|
49
|
+
raise(Pec::Errors::SecurityGroup, "security_group:#{name} is not found!") unless sg
|
50
|
+
sg["id"]
|
46
51
|
end if security_groups
|
47
52
|
end
|
48
53
|
end
|