pec 0.7.9 → 0.8.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: 6e289abbff98834305ea24c1bb20d3858d548fc9
4
- data.tar.gz: 6a1187c24347f8783d03f84179c543a317a46b8c
3
+ metadata.gz: 4c127f2ce57f24e553a4a7a97e2e17a9b59e1d2f
4
+ data.tar.gz: 06ebdf5437d8f2bdb49525ce4b8adbec4863d830
5
5
  SHA512:
6
- metadata.gz: 7e51d042af1b21821fd38a505d01ad2c82b9e970866b284dd0479f020bc54616b08656883788c0c38b97e2928e73169e4287830a80e6f1133e8ac35b30b3e31b
7
- data.tar.gz: 9ae349b52e0cb7cc154cabb69c40dbda8a406f9ab2e44eb98a52bd9d65025e3a376c339b59999e97c46960818877a297344647f7940c792581c096d9cf26c688
6
+ metadata.gz: bc8b80525f965822e16a933ce72a4bc49db5cc0d382d0f232081f9083b383ba1fc051b974c287823412128bafd3c63e4b171b98c44694b5c49c89f4abda4a938
7
+ data.tar.gz: 66bfed05fd07cf6700ff9137545934c0288dfbc2e87769e5f34884b8c7e33de71ce47155b497b53ac86f6e83225a684508e113d7e9b235dd9da12aa83e0104ea
data/.gitignore CHANGED
@@ -9,6 +9,7 @@
9
9
  /tmp/
10
10
  /vendor
11
11
  /*.yaml
12
+ /*.erb
12
13
  *.sample
13
14
  /user_datas
14
15
  .ruby-version
data/README.md CHANGED
@@ -31,7 +31,8 @@ create - /user_data/web_server.yaml.sample
31
31
  $ pec config <hostname_regex> <hostname_regex>...
32
32
 
33
33
  ### Configure
34
- #### Pec.yaml
34
+ #### Pec.yaml or Pec.yaml.erb
35
+ support format yaml,erb
35
36
  ```
36
37
  # merge of yaml
37
38
  _default_: &def
@@ -72,7 +73,7 @@ pyama-test002:
72
73
  # include config
73
74
  includes:
74
75
  - path/to/a.yaml
75
- - path/to/b.yaml
76
+ - path/to/b.yaml.erb
76
77
 
77
78
  ```
78
79
  ##### Detail
data/lib/pec.rb CHANGED
@@ -6,6 +6,7 @@ require 'thor'
6
6
  require 'ip'
7
7
  require 'colorator'
8
8
  require "pec/core"
9
+ require "pec/config_file"
9
10
  require "pec/version"
10
11
  require "pec/logger"
11
12
  require "pec/configure"
@@ -31,21 +32,14 @@ module Pec
31
32
  @_last_tenant = _tenant_name if _tenant_name != @_last_tenant
32
33
  end
33
34
 
34
- def self.load_config(config_name=nil)
35
+ def self.load_config(config_name="Pec.yaml")
35
36
  @_configure ||= []
36
- config_name ||= 'Pec.yaml'
37
- merge_config(config_name).to_hash.reject {|k,v| k[0].match(/\_/) || k.match(/^includes$/) }.each do |host|
37
+ ConfigFile.new(config_name).load.to_hash.reject {|k,v| k[0].match(/\_/) || k.match(/^includes$/) }.each do |host|
38
38
  @_configure << Pec::Configure.new(host)
39
39
  end
40
- end
41
-
42
- def self.merge_config(config_name)
43
- base_config = YAML.load_file(config_name)
44
- if include_files = base_config.to_hash.find{|k,v| k.match(/^includes$/) && !v.nil? }
45
- YAML.load(File.read(config_name) + include_files[1].map {|f|File.read(f)}.join("\n"))
46
- else
47
- base_config
48
- end
40
+ rescue => e
41
+ Pec::Logger.critical "configure error!"
42
+ raise e
49
43
  end
50
44
 
51
45
  def self.configure
@@ -53,11 +47,11 @@ module Pec
53
47
  @_configure
54
48
  end
55
49
 
56
- def self.servers(hosts, options, fetch=false)
50
+ def self.servers(hosts, options, not_fetch)
57
51
  self.configure.each do |config|
58
52
  next if hosts.size > 0 && hosts.none? {|name| config.name.match(/^#{name}/)}
59
53
  Pec.init_yao(config.tenant)
60
- server = fetch_server(config) if fetch
54
+ server = fetch_server(config) unless not_fetch
61
55
  yield(server, config)
62
56
  end
63
57
  end
@@ -102,6 +96,15 @@ module Pec
102
96
  end
103
97
  end
104
98
 
99
+ def self.processor_matching(source, klass)
100
+ source.keys.each do |k|
101
+ Object.const_get(klass.to_s).constants.each do |c|
102
+ object = Object.const_get("#{klass.to_s}::#{c}")
103
+ yield object if k.to_s == object.kind.to_s
104
+ end
105
+ end
106
+ end
107
+
105
108
  def self.config_reset
106
109
  @_configure = nil
107
110
  end
@@ -1,9 +1,8 @@
1
1
  module Pec::Command
2
2
  class Base
3
- @fetch = false
4
3
  def self.run(host_name, options)
5
4
  before_do
6
- Pec.servers(host_name, options, @fetch) do |server,config|
5
+ Pec.servers(host_name, options, not_fetch) do |server,config|
7
6
  task(host_name, options, server, config)
8
7
  end
9
8
  rescue => e
@@ -15,7 +14,9 @@ module Pec::Command
15
14
  Pec::Logger.info("\t" + e.backtrace.join("\n\t"))
16
15
  end
17
16
 
17
+ def self.not_fetch; end
18
18
  def self.task(host_name, options, server, config); end
19
19
  def self.before_do; end
20
+
20
21
  end
21
22
  end
@@ -3,5 +3,9 @@ module Pec::Command
3
3
  def self.task(host_name, options, server, config)
4
4
  puts YAML.dump(config.inspect[0] => config.inspect[1])
5
5
  end
6
+
7
+ def self.not_fetch
8
+ true
9
+ end
6
10
  end
7
11
  end
@@ -1,6 +1,5 @@
1
1
  module Pec::Command
2
2
  class Destroy < Base
3
- @fetch = true
4
3
  def self.task(host_name, options, server, config)
5
4
  unless server
6
5
  Pec::Logger.notice "not be created #{config.name}"
@@ -1,6 +1,5 @@
1
1
  module Pec::Command
2
2
  class Halt < Base
3
- @fetch = true
4
3
  def self.task(host_name, options, server, config)
5
4
  case
6
5
  when server.nil?
@@ -10,5 +10,9 @@ module Pec::Command
10
10
  def self.before_do
11
11
  Thor.new.say("vm list:", :yellow)
12
12
  end
13
+
14
+ def self.not_fetch
15
+ true
16
+ end
13
17
  end
14
18
  end
@@ -1,6 +1,5 @@
1
1
  module Pec::Command
2
2
  class Status < Base
3
- @fetch = true
4
3
  def self.task(host_name, options, server, config)
5
4
  if server
6
5
  puts sprintf(
@@ -1,6 +1,5 @@
1
1
  module Pec::Command
2
2
  class Up < Base
3
- @fetch = true
4
3
  def self.task(host_name, options, server, config)
5
4
  case
6
5
  when server.nil?
@@ -8,12 +7,16 @@ module Pec::Command
8
7
  attribute = {name: config.name}
9
8
 
10
9
  begin
11
- processor_matching(config, Pec::Handler) do |klass|
12
- attribute.deep_merge!(klass.build(config))
10
+ Pec.processor_matching(config, Pec::Handler) do |klass|
11
+ if attr = klass.build(config)
12
+ attribute.deep_merge!(attr)
13
+ end
13
14
  end
14
15
 
15
- processor_matching(attribute, Pec::Handler) do |klass|
16
- attribute.deep_merge!(klass.post_build(config, attribute))
16
+ Pec.processor_matching(attribute, Pec::Handler) do |klass|
17
+ if attr = klass.post_build(config, attribute)
18
+ attribute.deep_merge!(attr)
19
+ end
17
20
  end
18
21
 
19
22
  Yao::Server.create(attribute)
@@ -22,8 +25,8 @@ module Pec::Command
22
25
  Pec::Logger.critical(e)
23
26
  Pec::Logger.warning "recovery start #{config.name}"
24
27
 
25
- processor_matching(config, Pec::Handler) do |klass|
26
- attribute.deep_merge!(klass.recover(attribute))
28
+ Pec.processor_matching(config, Pec::Handler) do |klass|
29
+ klass.recover(attribute)
27
30
  end
28
31
  Pec::Logger.warning "recovery success! #{config.name}"
29
32
  end
@@ -36,13 +39,5 @@ module Pec::Command
36
39
  end
37
40
  end
38
41
 
39
- def self.processor_matching(source, klass)
40
- source.keys.each do |k|
41
- Object.const_get(klass.to_s).constants.each do |c|
42
- object = Object.const_get("#{klass.to_s}::#{c}")
43
- yield object if k.to_s == object.kind.to_s
44
- end
45
- end
46
- end
47
42
  end
48
43
  end
@@ -0,0 +1,32 @@
1
+ require 'erb'
2
+ module Pec
3
+ class ConfigFile
4
+ attr_accessor :config_name
5
+ def initialize(config_name)
6
+ self.config_name = File.exist?("#{config_name}.erb") ? "#{config_name}.erb" : config_name
7
+ end
8
+
9
+ def load
10
+ base = read_file(config_name)
11
+ include_files = YAML.load(base).to_hash.find{|k,v| k.match(/^includes$/) && !v.nil? }
12
+ inc = include_files ? include_files[1].map {|f| read_file(f)}.join("\n") : ""
13
+ YAML.load(base + inc)
14
+ end
15
+
16
+ def read_file(file_name)
17
+ if File.exist?(file_name)
18
+ case
19
+ when file_name.match(/.erb$/)
20
+ erb = ERB.new(File.read(file_name), nil, '%-')
21
+ erb.result
22
+ when file_name.match(/.yaml$/) || file_name.match(/.yml$/)
23
+ File.read(file_name)
24
+ else
25
+ raise "not match file type must be yaml or erb"
26
+ end
27
+ else
28
+ raise "not file exiets! #{file_name}"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -24,10 +24,14 @@ module Pec::Handler
24
24
  end
25
25
 
26
26
  def recover(attribute)
27
+ return unless attribute[:networks]
28
+
27
29
  Pec::Logger.notice "start port recovery"
28
30
  attribute[:networks].each do |port|
29
- Yao::Port.destroy(port[:port])
30
- Pec::Logger.notice "port delete id:#{port[:port]}"
31
+ if port[:port]
32
+ Yao::Port.destroy(port[:port])
33
+ Pec::Logger.notice "port delete id:#{port[:port]}"
34
+ end
31
35
  end
32
36
  Pec::Logger.notice "complete port recovery"
33
37
  end
@@ -58,13 +62,9 @@ module Pec::Handler
58
62
  security_group(host)
59
63
  ) if host.security_group
60
64
 
61
- network[CONFIG].keys.each do |k|
62
- Pec::Handler::Networks.constants.each do |c|
63
- if Object.const_get("Pec::Handler::Networks::#{c}").kind == k &&
64
- ops = Object.const_get("Pec::Handler::Networks::#{c}").build(network)
65
- attribute.deep_merge!(ops)
66
- end
67
- end
65
+ Pec.processor_matching(network[CONFIG], Pec::Handler::Networks) do |klass|
66
+ ops = klass.build(network)
67
+ attribute.deep_merge!(ops) if ops
68
68
  end
69
69
 
70
70
  attribute
@@ -11,14 +11,8 @@ module Pec::Handler
11
11
  end
12
12
 
13
13
  def self.post_build(host, attribute)
14
- attribute.keys.each do |k|
15
- Pec::Handler::UserData.constants.each do |c|
16
- klass = Object.const_get("Pec::Handler::UserData::#{c}")
17
-
18
- if klass.kind.to_s == k.to_s
19
- attribute = klass.post_build(host, attribute)
20
- end
21
- end
14
+ Pec.processor_matching(attribute, Pec::Handler::UserData) do |klass|
15
+ attribute = klass.post_build(host, attribute)
22
16
  end
23
17
  attribute[:user_data] = Base64.encode64("#cloud-config\n" + attribute[:user_data].to_yaml) if attribute[:user_data]
24
18
  attribute
@@ -8,12 +8,11 @@ module Pec::Handler
8
8
 
9
9
  class << self
10
10
  def post_build(host, attribute)
11
- _nic = Pec::Handler::UserData::Nic.constants.reject {|c|c.to_s.downcase == "base"}.find do |c|
12
- host.os_type && Object.const_get("Pec::Handler::UserData::Nic::#{c}").os_type.include?(host.os_type)
13
- end
14
-
15
- nic = if _nic
16
- Object.const_get("Pec::Handler::UserData::Nic::#{_nic}")
11
+ nic = if host.os_type
12
+ os = Pec::Handler::UserData::Nic.constants.find do |c|
13
+ Object.const_get("Pec::Handler::UserData::Nic::#{c}").os_type.include?(host.os_type)
14
+ end
15
+ Object.const_get("Pec::Handler::UserData::Nic::#{os}")
17
16
  else
18
17
  Pec::Handler::UserData::Nic::Rhel
19
18
  end
@@ -35,5 +35,6 @@ module Pec::Handler
35
35
  raise "undfined method ifcfg_config"
36
36
  end
37
37
  end
38
+ self.os_type = []
38
39
  end
39
40
  end
data/lib/pec/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pec
2
- VERSION = "0.7.9"
2
+ VERSION = "0.8.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.9
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kazuhiko yamashita
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-27 00:00:00.000000000 Z
11
+ date: 2015-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -152,6 +152,7 @@ files:
152
152
  - lib/pec/command/status.rb
153
153
  - lib/pec/command/up.rb
154
154
  - lib/pec/config_error.rb
155
+ - lib/pec/config_file.rb
155
156
  - lib/pec/configure.rb
156
157
  - lib/pec/core.rb
157
158
  - lib/pec/handler.rb