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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d29ffb28f864e94df722cb8da0c152b670446b07
4
- data.tar.gz: 6fbfda8c8af3a68833f7b30bd34a5a92dfa48cb5
3
+ metadata.gz: 12573783d7f7d2956311eac8ddafc10dd576ef54
4
+ data.tar.gz: c995f4586be47905890d741abe3c419733deff28
5
5
  SHA512:
6
- metadata.gz: 7079d41c4aac4497a19a4c4e6df31080b57a6e0d98e729c883f95b188ad79b318ff31f4e1304a1f4dfae6164f920c913b3a53495696a3337a675d65f859eb56f
7
- data.tar.gz: 7afb5135a78958e7b513dbc485fa2d6da50abcb885bd7a25951cabc897d50c9f18e73eba5f30af7ea3fa702e3b44df3f83d845ec44538fe1b88464d589903eb6
6
+ metadata.gz: 532ce626a2742c53c8cd53869141b66b7cb455112a2a74a4e8b1208dac528512727aacbd696dede2975d7e0cf590b104a330824c2541b62853dceafc6a6465db
7
+ data.tar.gz: 912b3adf04d3d12b5d36cc5a0f4a410827bdbfb33a414f4f554287807b4ba0d68462847e0f591be05d8ab048c932f8b4ec47a8fa89a57e1ee1bfb474d8e18e9a
@@ -3,3 +3,6 @@ rvm:
3
3
  - 2.0.0
4
4
  - 2.1.6
5
5
  - 2.2.2
6
+ addons:
7
+ code_climate:
8
+ repo_token: c969d1896316cbd26ab70d3b7fef4f4e168df83b849fa7f16a1885569c86f29c
data/Gemfile CHANGED
@@ -12,6 +12,4 @@ group :development, :test do
12
12
  gem "rspec"
13
13
  gem "rake"
14
14
  end
15
- group :test do
16
- gem 'coveralls', :require => false
17
- end
15
+ gem "codeclimate-test-reporter", group: :test, require: nil
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Pec
2
2
  [![Build Status](https://travis-ci.org/pyama86/pec.svg?branch=master)](https://travis-ci.org/pyama86/pec)
3
+ [![Code Climate](https://codeclimate.com/github/pyama86/pec/badges/gpa.svg)](https://codeclimate.com/github/pyama86/pec)
4
+ [![Test Coverage](https://codeclimate.com/github/pyama86/pec/badges/coverage.svg)](https://codeclimate.com/github/pyama86/pec/coverage)
3
5
 
4
- OpenStackにおいて複数サーバの起動や、
5
- DHCPサーバがない状況でのIP自動採番を実現します。
6
+ OpenStackにおいて複数サーバの起動一括起動停止や、 DHCPサーバがない状況でのIP自動採番を実現します。
7
+ 作って壊してが驚くほどかんたんに。
6
8
 
7
9
  ## Install
8
10
 
@@ -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
- puts e
39
- puts "can't create server:#{host.name}"
34
+ pec_create_err_message(e, host)
40
35
  rescue Excon::Errors::Error => e
41
- JSON.parse(e.response[:body]).each { |e,m| puts "#{e}:#{m["message"]}" }
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
- puts "configure can't load"
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(name = nil)
53
- config = Pec::Configure.new
54
- config.load("Pec.yaml")
55
- config.each do |host|
56
- next if !name.nil? && host.name != name
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
- Pec::Compute::Server.new.destroy!(host.name) if options[:force] || yes?("#{host.name}: Are you sure you want to destroy the '#{host.name}' VM? [y/N]")
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
- puts e
61
- puts "can't create server:#{host.name}"
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
@@ -3,7 +3,7 @@ module Pec
3
3
  class Configure
4
4
  include Enumerable
5
5
 
6
- def load(file_name)
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
- err = %w(bootproto).find {|k| !config[1].key?(k)}
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
- port_content = {}
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
- port_content["bootproto"] = ether.bootproto
29
- port_content["type"] = ether.options['type'] ||'Ethernet'
30
- port_content["onboot"] = ether.options['onboot'] || 'yes'
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
- port_content["netmask"] = port.netmask
38
- port_content["ipaddr"] = port.ip_address
34
+ ifcfg_content["netmask"] = ether.find_port(ports).netmask
35
+ ifcfg_content["ipaddr"] = ether.find_port(ports).ip_address
39
36
  end
40
- port_content.merge!(ether.options)
37
+ ifcfg_content.merge!(ether.options)
38
+
41
39
  {
42
- 'content' => port_content.map {|k,v| "#{k.upcase}=#{v}"}.join("\n"),
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"
@@ -8,5 +8,6 @@ module Pec
8
8
  class Query < Error; end
9
9
  class UserData < Error; end
10
10
  class Configure < Error; end
11
+ class SecurityGroup < Error; end
11
12
  end
12
13
  end
@@ -15,11 +15,8 @@ module Pec
15
15
  def assign!(ip)
16
16
  # dhcp ip recycle
17
17
  if request_any_address?(ip)
18
- free = fetch_free_port
19
- if free
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
@@ -1,3 +1,3 @@
1
1
  module Pec
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -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
- sg["id"] if sg
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - kazuhiko yamashita