pec 0.1.5 → 0.1.6

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: 12573783d7f7d2956311eac8ddafc10dd576ef54
4
- data.tar.gz: c995f4586be47905890d741abe3c419733deff28
3
+ metadata.gz: 3777a334f52b99460286a3728200d1115a9912cb
4
+ data.tar.gz: a77aa53e221c37903bb3e20c91eefd6a5d139d54
5
5
  SHA512:
6
- metadata.gz: 532ce626a2742c53c8cd53869141b66b7cb455112a2a74a4e8b1208dac528512727aacbd696dede2975d7e0cf590b104a330824c2541b62853dceafc6a6465db
7
- data.tar.gz: 912b3adf04d3d12b5d36cc5a0f4a410827bdbfb33a414f4f554287807b4ba0d68462847e0f591be05d8ab048c932f8b4ec47a8fa89a57e1ee1bfb474d8e18e9a
6
+ metadata.gz: 553934f57c16268267a986f769650a7429ba59aa49b4fe0b20573fad64250b82b28890988a03ffb6250c86e2c6d560c65ac59863f597c2013439c6fe083c02c3
7
+ data.tar.gz: b09690c07338509b24963ac254161adda7e548b0808f05293ff5849b49f9947b95536e542b29e131137a0bec4e49ecb431be3749c479d4bfba6f15727dcaf935
data/Gemfile CHANGED
@@ -7,7 +7,7 @@ gem 'thor'
7
7
  gem 'ruby-ip'
8
8
  gem 'activesupport'
9
9
  gem "bundler"
10
-
10
+ gem "colorator"
11
11
  group :development, :test do
12
12
  gem "rspec"
13
13
  gem "rake"
data/README.md CHANGED
@@ -13,7 +13,7 @@ OpenStackにおいて複数サーバの起動一括起動停止や、 DHCPサー
13
13
 
14
14
  ## Usage
15
15
 
16
- 定義ファイル作成
16
+ セットアップ・定義ファイル作成
17
17
 
18
18
  $ pec init
19
19
 
@@ -21,7 +21,7 @@ OpenStackにおいて複数サーバの起動一括起動停止や、 DHCPサー
21
21
  create - /Pec.yaml
22
22
  create - /user_datas/web_server.yaml.sample
23
23
  ```
24
-
24
+
25
25
  Pec.yamlに基づきホストを作成します。
26
26
  ホスト名が指定された場合はそのホストのみ作成、削除します。
27
27
 
@@ -29,21 +29,13 @@ Pec.yamlに基づきホストを作成します。
29
29
 
30
30
  $ pec destroy <hostname>
31
31
 
32
- ### Configure
33
- #### ~/.fog
34
- ```
35
- % cat ~/.fog
36
- default:
37
- openstack_auth_url: "http://your-openstack-endpoint/v2.0/tokens"
38
- openstack_username: "admin"
39
- openstack_tenant: "admin"
40
- openstack_api_key: "admin-no-password"
41
- ```
42
-
32
+ $ pec status <hostname>
43
33
 
34
+ ### Configure
44
35
  #### Pec.yaml
45
36
  ```
46
37
  pyama-test001:
38
+ tenant: your_tenant
47
39
  image: centos-7.1_chef-12.3_puppet-3.7
48
40
  flavor: m1.small
49
41
  networks:
@@ -75,10 +67,11 @@ pyama-test002:
75
67
 
76
68
  | 項目名 | 説明 | 必須 | 例示 |
77
69
  | -------------- | ---------------------------------------------- | ---- | ------------------------------- |
78
- | instance_name| インスタンス名 | ○ | pyama-test001 |
70
+ | instance_name | インスタンス名 | ○ | pyama-test001 |
71
+ | tenant | テナント名 | ○ | your_tenant |
79
72
  | image | イメージ名 | ○ | centos-7.1_chef-12.3_puppet-3.7 |
80
73
  | flavor | フレーバー名 | ○ | m1.small |
81
- | networks | ネットワーク定義 | - | [] |
74
+ | networks | ネットワーク定義 | - | [] |
82
75
  | security_group | セキュリティグループ名 | - | [default,ssh] |
83
76
  | templates | `user_data`のテンプレート.`./user_datas`に配置 | - | [base.yaml,webserver.yaml] |
84
77
  | user_data | cloud-init記法に準拠 | - | - |
data/lib/pec.rb CHANGED
@@ -1,9 +1,17 @@
1
1
  require 'fog'
2
2
  require 'ip'
3
+ require 'colorator'
3
4
  require "pec/version"
4
5
  require "pec/query"
5
6
  require "pec/errors"
6
- require "pec/vm_director"
7
+ require "pec/init"
8
+ require "pec/resource"
9
+ require "pec/resource/openstack"
10
+ require "pec/director"
11
+ require "pec/director/helper"
12
+ require "pec/director/make_director"
13
+ require "pec/director/destroy_director"
14
+ require "pec/director/vm_status_director"
7
15
  require "pec/configure"
8
16
  require "pec/configure/sample"
9
17
  require "pec/configure/host"
@@ -12,6 +20,7 @@ require "pec/configure/user_data"
12
20
  require "pec/compute/server"
13
21
  require "pec/compute/flavor"
14
22
  require "pec/compute/image"
23
+ require "pec/compute/tenant"
15
24
  require "pec/compute/security_group"
16
25
  require "pec/network/port"
17
26
  require "pec/network/subnet"
data/lib/pec/cli.rb CHANGED
@@ -5,84 +5,26 @@ module Pec
5
5
 
6
6
  desc 'init', 'create sample config'
7
7
  def init
8
- dirname = "user_datas"
9
- unless FileTest.exist?(dirname)
10
- FileUtils.mkdir_p(dirname)
11
- puts "create directry user_datas"
12
- end
13
- unless File.exist?("Pec.yaml")
14
- open("Pec.yaml","w") do |e|
15
- YAML.dump(Pec::Configure::Sample.pec_file, e)
16
- end
17
- puts "create configure file Pec.yaml"
18
- end
19
- open("#{dirname}/web_server.yaml.sample","w") do |e|
20
- YAML.dump(Pec::Configure::Sample.user_data, e)
21
- end if FileTest.exist?(dirname)
22
-
8
+ Pec::Init.create_fog_config
9
+ Pec::Init.create_template_dir
10
+ Pec::Init.create_sample_config
23
11
  end
24
12
 
25
13
  desc 'up', 'create vm by Pec.yaml'
26
14
  def up(host_name = nil)
27
- config = Pec::Configure.new("Pec.yaml")
28
- director = Pec::VmDirector.new
29
-
30
- config.filter_by_host(host_name).each do |host|
31
- begin
32
- director.make(host)
33
- rescue Pec::Errors::Error => e
34
- pec_create_err_message(e, host)
35
- rescue Excon::Errors::Error => e
36
- excon_err_message(e)
37
- end
38
- end if config
39
- rescue Errno::ENOENT => e
40
- puts e
41
- rescue Pec::Errors::Configure => e
42
- pec_config_err_message
15
+ Pec::Director.execute("make", host_name)
43
16
  end
44
17
 
45
18
  option :force , type: :boolean, aliases: "-f"
46
19
  desc "destroy", "delete vm"
47
20
  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|
52
- begin
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
56
- rescue Pec::Errors::Error => e
57
- pec_delete_err_message(e, host)
58
- rescue Excon::Errors::Error => e
59
- excon_err_message(e)
60
- end
61
- end if config
62
- rescue Errno::ENOENT => e
63
- puts e
64
- rescue Pec::Errors::Configure => e
65
- pec_config_err_message
21
+ Pec::Director.execute("destroy", host_name, options)
66
22
  end
67
23
 
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
24
+ desc "status", "vm status"
25
+ def status(host_name = nil)
26
+ say("Current machine stasus:", :yellow)
27
+ Pec::Director.execute("vm_status", host_name)
86
28
  end
87
29
  end
88
30
  end
@@ -1,7 +1,7 @@
1
1
  module Pec
2
2
  class Compute
3
3
  class Flavor
4
- include Query
4
+ extend Query
5
5
  end
6
6
  end
7
7
  end
@@ -1,7 +1,7 @@
1
1
  module Pec
2
2
  class Compute
3
3
  class Image
4
- include Query
4
+ extend Query
5
5
  end
6
6
  end
7
7
  end
@@ -1,12 +1,7 @@
1
1
  module Pec
2
2
  class Compute
3
3
  class Security_Group
4
- include Query
5
- def add_security_group(server_id, security_groups)
6
- security_groups.each do |sg_name|
7
- response = Fog::Compute[:openstack].add_security_group(server_id, sg_name)
8
- end if security_groups
9
- end
4
+ extend Query
10
5
  end
11
6
  end
12
7
  end
@@ -1,37 +1,31 @@
1
- require 'json'
2
1
  module Pec
3
2
  class Compute
4
3
  class Server
5
- include Query
6
- def create(name, image_ref, flavor_ref, ports, options)
7
- networks = ports.map do |port|
8
- raise(Pec::Errors::Port, "port-id:#{port.id} ip-addr:#{port.ip_address} in used") if port.used?
9
- puts "#{name}: assingn ip #{port.ip_address}"
10
- { port_id: port.id }
11
- end if ports
4
+ extend Query
5
+ class << self
6
+ def create(name, image_ref, flavor_ref, options)
12
7
 
13
- options.merge!({ 'nics' => networks })
8
+ response = Pec::Resource.get.create_server(name, image_ref, flavor_ref, options)
14
9
 
15
- response = Fog::Compute[:openstack].create_server(name, image_ref, flavor_ref, options)
10
+ if response[:status] == 202
11
+ puts "success create for server_name:#{name}".blue
12
+ end
16
13
 
17
- if response[:status] == 202
18
- puts "success create for server_name:#{name}"
14
+ response.data[:body]["server"]["id"]
19
15
  end
20
16
 
21
- response.data[:body]["server"]["id"]
22
- end
23
-
24
- def exists?(server_name)
25
- fetch(server_name)
26
- end
17
+ def exists?(server_name)
18
+ fetch(server_name)
19
+ end
27
20
 
28
- def destroy!(server_name)
29
- server = fetch(server_name)
30
- raise(Pec::Errors::Host, "server_name:#{server_name} is not fond!") unless server
31
- response = Fog::Compute[:openstack].delete_server(server["id"]) if server
21
+ def destroy!(server_name)
22
+ server = fetch(server_name)
23
+ raise(Pec::Errors::Host, "server_name:#{server_name} is not fond!") unless server
24
+ response = Pec::Resource.get.delete_server(server["id"]) if server
32
25
 
33
- if response && response[:status] == 204
34
- puts "server_name:#{server_name} is deleted!"
26
+ if response && response[:status] == 204
27
+ puts "server_name:#{server_name} is deleted!".green
28
+ end
35
29
  end
36
30
  end
37
31
  end
@@ -0,0 +1,12 @@
1
+ module Pec
2
+ class Compute
3
+ class Tenant
4
+ extend Query
5
+ class << self
6
+ def get_name(id)
7
+ list.find {|p| p["id"] == id }["name"]
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,7 +1,7 @@
1
1
  module Pec
2
2
  class Configure
3
3
  class Host
4
- attr_reader :name, :image, :flavor,:security_group, :user_data, :networks, :templates
4
+ attr_reader :name, :image, :flavor,:security_group, :user_data, :networks, :templates, :tenant
5
5
  def initialize(config)
6
6
  @name = config[0];
7
7
  @image = config[1]["image"];
@@ -9,6 +9,7 @@ module Pec
9
9
  @security_group = config[1]["security_group"];
10
10
  @user_data = config[1]["user_data"];
11
11
  @templates = config[1]["templates"]
12
+ @tenant = config[1]["tenant"]
12
13
  end
13
14
 
14
15
  def append_network(network)
@@ -27,7 +28,7 @@ module Pec
27
28
  end
28
29
 
29
30
  def check_require_key(config)
30
- err = %w(image flavor).find {|r| !config[1].key?(r) || config[1][r].nil? }
31
+ err = %w(image flavor tenant).find {|r| !config[1].key?(r) || config[1][r].nil? }
31
32
  raise(Pec::Errors::Host,"skip! #{config[0]}: #{err} is required!") unless err.nil?
32
33
  true
33
34
  end
@@ -5,6 +5,7 @@ module Pec
5
5
  def pec_file
6
6
  {
7
7
  "sever_name" => {
8
+ "tenant" => "your_tenant",
8
9
  "image" => "centos-7",
9
10
  "flavor" => "m1.small",
10
11
  "networks" => [
@@ -21,7 +21,6 @@ module Pec
21
21
  def make_port_content(config, ports)
22
22
  config.networks.map do |ether|
23
23
  ifcfg_content = {}
24
-
25
24
  ifcfg_content["bootproto"] = ether.bootproto
26
25
  ifcfg_content["name"] = ether.options["name"] || ether.name
27
26
  ifcfg_content["device"] = ether.options["device"] || ether.name
@@ -0,0 +1,53 @@
1
+ module Pec
2
+ class Director
3
+ class << self
4
+
5
+ def execute(action, host_name, options=nil)
6
+ config = Pec::Configure.new("Pec.yaml")
7
+ director = assign_director(action, options)
8
+
9
+ config.filter_by_host(host_name).each do |host|
10
+ begin
11
+ director.execute!(host) if director.do_it?(host)
12
+ rescue Pec::Errors::Error => e
13
+ director.err_message(e, host)
14
+ rescue Excon::Errors::Error => e
15
+ excon_err_message(e)
16
+ end
17
+ end if config
18
+
19
+ rescue Pec::Errors::Configure => e
20
+ config_load_err_message
21
+ rescue Pec::Errors::Error => e
22
+ err_message(e)
23
+ rescue Errno::ENOENT => e
24
+ err_messag(e)
25
+ end
26
+
27
+ def assign_director(action, options)
28
+ case
29
+ when action == "make"
30
+ Pec::Director::MakeDirector.new
31
+ when action == "destroy"
32
+ Pec::Director::DestroyDirector.new(options)
33
+ when action == "vm_status"
34
+ Pec::Director::VmStatusDirector.new
35
+ else
36
+ raise
37
+ end
38
+ end
39
+
40
+ def err_message(e)
41
+ puts e.to_s.magenta
42
+ end
43
+
44
+ def config_load_err_message
45
+ puts "can't load configfile".magenta
46
+ end
47
+
48
+ def excon_err_message(e)
49
+ JSON.parse(e.response[:body]).each { |e,m| puts "#{e}:#{m["message"]}".magenta }
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,23 @@
1
+ module Pec
2
+ class Director
3
+ class DestroyDirector
4
+ def initialize(command_options)
5
+ @command_options = command_options
6
+ end
7
+
8
+ def execute!(host)
9
+ Pec::Resource.set_tenant(host.tenant)
10
+ Pec::Compute::Server.destroy!(host.name)
11
+ end
12
+
13
+ def do_it?(host)
14
+ @command_options[:force] || Thor.new.yes?("#{host.name}: Are you sure you want to destroy the '#{host.name}' VM? [y/N]")
15
+ end
16
+
17
+ def err_message(e, host)
18
+ puts e.to_s.magenta
19
+ puts "can't destroy server:#{host.name}".magenta if host
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,47 @@
1
+ module Pec
2
+ class Director
3
+ class Helper
4
+ class << self
5
+
6
+ def ports_assign(host)
7
+ host.networks.map do |ether|
8
+ begin
9
+ ip = IP.new(ether.ip_address)
10
+ rescue ArgumentError => e
11
+ raise(Pec::Errors::Port, "ip:#{ether.ip_address} #{e}")
12
+ end
13
+
14
+ port_subnet = Pec::Network::Subnet.fetch(ip.network.to_s)
15
+ raise(Pec::Errors::Subnet, "subnet:#{ip.network.to_s} is not fond!") unless port_subnet
16
+
17
+ port = Pec::Network::Port.new.assign(ether.name, ip, port_subnet, get_security_group_id(host.security_group))
18
+ raise(Pec::Errors::Port, "ip addess:#{ip.to_addr} can't create port!") unless port
19
+
20
+ puts "#{host.name}: assingn ip #{port.ip_address}".green
21
+ port
22
+ end if host.networks
23
+ end
24
+
25
+ def get_nics(ports)
26
+ { 'nics' => ports.map { |port| { port_id: port.id } }}
27
+ end
28
+
29
+ def get_security_group_id(security_groups)
30
+ security_groups.map do |sg_name|
31
+ sg = Pec::Compute::Security_Group.fetch(sg_name)
32
+ raise(Pec::Errors::SecurityGroup, "security_group:#{sg_name} is not found!") unless sg
33
+ sg["id"]
34
+ end if security_groups
35
+ end
36
+
37
+ def parse_from_addresses(addresses)
38
+ addresses.map do |net, ethers|
39
+ ethers.map do |ether|
40
+ ether["addr"]
41
+ end
42
+ end.flatten
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,35 @@
1
+ module Pec
2
+ class Director
3
+ class MakeDirector
4
+ def execute!(host)
5
+ Pec::Resource.set_tenant(host.tenant)
6
+ make(host)
7
+ end
8
+
9
+ def do_it?(host)
10
+ true
11
+ end
12
+
13
+ def make(host)
14
+ if Pec::Compute::Server.exists?(host.name)
15
+ puts "skip create server! name:#{host.name} is exists!".yellow
16
+ return true
17
+ end
18
+
19
+ ports = Pec::Director::Helper.ports_assign(host)
20
+ flavor_ref = Pec::Compute::Flavor.get_ref(host.flavor)
21
+ image_ref = Pec::Compute::Image.get_ref(host.image)
22
+ options = { "user_data" => Pec::Configure::UserData.make(host, ports) }
23
+
24
+ options.merge!(Pec::Director::Helper.get_nics(ports))
25
+
26
+ Pec::Compute::Server.create(host.name, image_ref, flavor_ref, options)
27
+ end
28
+
29
+ def err_message(e, host)
30
+ puts e.to_s.magenta
31
+ puts "can't create server:#{host.name}".magenta if host
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,38 @@
1
+ module Pec
2
+ class Director
3
+ class VmStatusDirector
4
+ def execute!(host)
5
+ Pec::Resource.set_tenant(host.tenant)
6
+ show_summary(host)
7
+ end
8
+
9
+ def do_it?(host)
10
+ true
11
+ end
12
+
13
+ def show_summary(host)
14
+ server = Pec::Compute::Server.fetch(host.name)
15
+ status = "uncreated"
16
+ compute_node = ""
17
+ tenant_name = ""
18
+ flavor = ""
19
+ ip_address = ""
20
+ if server
21
+ detail = Pec::Resource.get.get_server_details(server["id"])
22
+ status = detail["status"]
23
+ compute_node = detail["OS-EXT-SRV-ATTR:host"]
24
+ flavor = detail["flavor"]["id"]
25
+ tenant_name = Pec::Compute::Tenant.get_name(detail["tenant_id"])
26
+ ip_address = Pec::Director::Helper.parse_from_addresses(detail["addresses"]).join(",")
27
+ end
28
+
29
+ puts sprintf(" %-30s |%-10s | %-10s | %-10s | %-30s | %-48s", host.name, status, tenant_name, flavor, compute_node, ip_address)
30
+ end
31
+
32
+ def err_message(e, host)
33
+ puts e.magenta
34
+ puts "can't create server:#{host.name}".magenta if host
35
+ end
36
+ end
37
+ end
38
+ end
data/lib/pec/errors.rb CHANGED
@@ -9,5 +9,6 @@ module Pec
9
9
  class UserData < Error; end
10
10
  class Configure < Error; end
11
11
  class SecurityGroup < Error; end
12
+ class Resource < Error; end
12
13
  end
13
14
  end
data/lib/pec/init.rb ADDED
@@ -0,0 +1,42 @@
1
+ module Pec
2
+ class Init
3
+ class << self
4
+ def create_template_dir
5
+ dirname = "user_datas"
6
+ unless FileTest.exist?(dirname)
7
+ FileUtils.mkdir_p(dirname)
8
+ open("#{dirname}/web_server.yaml.sample","w") do |e|
9
+ YAML.dump(Pec::Configure::Sample.user_data, e)
10
+ end if FileTest.exist?(dirname)
11
+ puts "create directry user_datas".green
12
+ end
13
+ end
14
+
15
+ def create_sample_config
16
+ unless File.exist?("Pec.yaml")
17
+ open("Pec.yaml","w") do |e|
18
+ YAML.dump(Pec::Configure::Sample.pec_file, e)
19
+ end
20
+ puts "create configure file Pec.yaml".green
21
+ end
22
+ end
23
+
24
+ def create_fog_config
25
+ thor = Thor.new
26
+ if !File.exist?(File.expand_path("~/.fog")) || thor.yes?("Do you want to overwrite the existing ~/.fog? [y/N]")
27
+ thor.say("Start Configure by OpenStack", :yellow)
28
+ params = {}
29
+
30
+ params = %w(auth_uri username api_key tenant).inject({}) do |user_input, c|
31
+ user_input["openstack_#{c}"] = thor.ask("openstack #{c}:")
32
+ user_input
33
+ end
34
+
35
+ thor.say("Configure Complete!", :blue) if open(File.expand_path("~/.fog"), "w") do |e|
36
+ YAML.dump({ "default" => params }, e)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -2,38 +2,59 @@ require 'json'
2
2
  module Pec
3
3
  class Network
4
4
  class Port
5
- @@use_ip_list = []
5
+ extend Query
6
6
  attr_reader :name, :subnet
7
- include Query
8
- def initialize(name, ip_addr, subnet, security_groups)
7
+ @@use_ip_list = []
8
+
9
+ def assign(name, ip, subnet, security_group_ids)
9
10
  @name = name
10
11
  @subnet = subnet
11
- @security_groups = security_groups
12
- @config = fetch(ip_addr)
13
- end
14
12
 
15
- def assign!(ip)
16
13
  # dhcp ip recycle
17
14
  if request_any_address?(ip)
18
- @config = fetch_free_port
19
- ip = IP.new("#{@config["fixed_ips"][0]["ip_address"]}/#{ip.pfxlen}") unless @config.nil?
15
+ @port = fetch_free_port
16
+ ip = IP.new("#{@port["fixed_ips"][0]["ip_address"]}/#{ip.pfxlen}") unless @port.nil?
20
17
  end
21
-
22
18
  case
23
19
  when exists? && !used?
24
- replace(ip)
20
+ recreate(ip, subnet, security_group_ids)
25
21
  when !exists?
26
- create(ip)
22
+ create(ip, subnet, security_group_ids)
27
23
  when used?
28
24
  raise(Pec::Errors::Port, "ip:#{ip.to_addr} is used!")
29
25
  end
26
+ self
27
+ end
28
+
29
+ def create(ip, subnet, security_group_ids)
30
+ options = { security_groups: security_group_ids }
31
+ options.merge!({ fixed_ips: [{ subnet_id: subnet["id"], ip_address: ip.to_addr}]}) if ip.to_s != subnet["cidr"]
32
+ response = Pec::Resource.get.create_port(subnet["network_id"], options)
33
+ if response
34
+ @port = response.data[:body]["port"]
35
+ @@use_ip_list << response.data[:body]["port"]["fixed_ips"][0]["ip_address"]
36
+ response.data[:body]["port"]["id"]
37
+ end
38
+ end
39
+
40
+ def delete(ip)
41
+ target_port = fetch_by_ip(ip.to_addr)
42
+ response = Pec::Resource.get.delete_port(target_port["id"]) if target_port
43
+ end
44
+
45
+ def recreate(ip, subnet, security_group_ids)
46
+ create(ip, subnet, security_group_ids) if delete(ip)
30
47
  end
31
48
 
32
49
  def request_any_address?(ip)
33
50
  ip.to_s == subnet["cidr"]
34
51
  end
35
52
 
36
- def fetch(ip_addr)
53
+ def list
54
+ Pec::Resource.get.port_list
55
+ end
56
+
57
+ def fetch_by_ip(ip_addr)
37
58
  list.find {|p| p["fixed_ips"][0]["ip_address"] == ip_addr }
38
59
  end
39
60
 
@@ -47,52 +68,31 @@ module Pec
47
68
  end
48
69
 
49
70
  def exists?
50
- !@config.nil?
71
+ !@port.nil?
51
72
  end
52
73
 
53
74
  def used?
54
- @config && !@config["device_owner"].empty?
75
+ @port && !@port["device_owner"].empty?
55
76
  end
56
77
 
57
78
  def id
58
- @config["id"]
79
+ @port["id"]
59
80
  end
60
81
 
61
82
  def mac_address
62
- @config["mac_address"]
83
+ @port["mac_address"]
63
84
  end
64
85
 
65
86
  def ip_address
66
- @config["fixed_ips"][0]["ip_address"]
87
+ @port["fixed_ips"][0]["ip_address"]
67
88
  end
68
89
 
69
90
  def network_id
70
- @config["network_id"]
91
+ @port["network_id"]
71
92
  end
72
93
 
73
94
  def netmask
74
- IP.new(@config["fixed_ips"][0]["ip_address"]).netmask.to_s
75
- end
76
-
77
- def create(ip)
78
- options = { security_groups: @security_groups }
79
- options.merge!({ fixed_ips: [{ subnet_id: @subnet["id"], ip_address: ip.to_addr}]}) if ip.to_s != subnet["cidr"]
80
- response = Fog::Network[:openstack].create_port(@subnet["network_id"], options)
81
-
82
- if response
83
- @config = response.data[:body]["port"]
84
- @@use_ip_list << response.data[:body]["port"]["fixed_ips"][0]["ip_address"]
85
- response.data[:body]["port"]["id"]
86
- end
87
- end
88
-
89
- def delete(ip)
90
- port = fetch(ip.to_addr)
91
- response = Fog::Network[:openstack].delete_port(port["id"]) if port
92
- end
93
-
94
- def replace(ip)
95
- create(ip) if delete(ip)
95
+ IP.new(@port["fixed_ips"][0]["ip_address"]).netmask.to_s
96
96
  end
97
97
  end
98
98
  end
@@ -1,11 +1,13 @@
1
1
  module Pec
2
2
  class Network
3
3
  class Subnet
4
- include Query
5
- def fetch(cidr)
6
- subnet = list.find {|p| p["cidr"] == cidr }
7
- raise(Pec::Errors::Subnet, "cidr:#{cidr} is not fond!") unless subnet
8
- subnet
4
+ extend Query
5
+ class << self
6
+ def fetch(cidr)
7
+ subnet = list.find {|p| p["cidr"] == cidr }
8
+ raise(Pec::Errors::Subnet, "cidr:#{cidr} is not fond!") unless subnet
9
+ subnet
10
+ end
9
11
  end
10
12
  end
11
13
  end
data/lib/pec/query.rb CHANGED
@@ -1,22 +1,9 @@
1
1
  require 'active_support/core_ext/string/inflections'
2
- require 'fog'
3
2
  module Pec
4
3
  module Query
5
- def get_adapter
6
- case
7
- when self.class.name.include?('Network')
8
- Fog::Network[:openstack]
9
- when self.class.name.include?('Compute')
10
- Fog::Compute[:openstack]
11
- else
12
- raise
13
- end
14
- end
15
-
16
4
  def list
17
- name = self.class.name.demodulize.downcase+"s"
18
- @_list ||= Hash.new
19
- @_list[name] ||= get_adapter.send("list_#{name}").data[:body][name]
5
+ class_name = self.name.demodulize.downcase
6
+ Pec::Resource.get.send("#{class_name}_list")
20
7
  end
21
8
 
22
9
  def fetch(name)
@@ -0,0 +1,20 @@
1
+ require 'singleton'
2
+ module Pec
3
+ class Resource
4
+ include Singleton
5
+ class << self
6
+ @@_resource = {}
7
+ @@_tenant = nil
8
+ def get
9
+ raise(Pec::Errors::Resource, "Please be tenant is always set") unless @@_tenant
10
+ unless ENV['PEC_TEST']
11
+ @@_resource[@@_tenant] ||= Pec::Resource::OpenStack.new(@@_tenant)
12
+ end
13
+ end
14
+
15
+ def set_tenant(tenant)
16
+ @@_tenant = tenant
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,60 @@
1
+ module Pec
2
+ class Resource
3
+ class OpenStack
4
+ def initialize(tenant)
5
+ tenant_hash = { openstack_tenant: tenant }
6
+ @network = Fog::Network::OpenStack.new(tenant_hash)
7
+ @compute = Fog::Compute::OpenStack.new(tenant_hash)
8
+ end
9
+
10
+ def port_list
11
+ @_ports ||= @network.list_ports[:body]['ports']
12
+ end
13
+
14
+ def subnet_list
15
+ @_subnets ||= @network.list_subnets[:body]['subnets']
16
+ end
17
+
18
+ def server_list
19
+ @_servers ||= @compute.list_servers[:body]['servers']
20
+ end
21
+
22
+ def security_group_list
23
+ @_security_groups ||= @compute.list_security_groups[:body]['security_groups']
24
+ end
25
+
26
+ def image_list
27
+ @_images ||= @compute.list_images[:body]['images']
28
+ end
29
+
30
+ def flavor_list
31
+ @_flavors ||= @compute.list_flavors[:body]['flavors']
32
+ end
33
+
34
+ def tenant_list
35
+ @_tenants ||= @compute.list_tenants[:body]['tenants']
36
+ end
37
+
38
+ def create_server(name, image_ref, flavor_ref, options)
39
+ @compute.create_server(name, image_ref, flavor_ref, options)
40
+ end
41
+
42
+ def delete_server(server_id)
43
+ @compute.delete_server(server_id)
44
+ end
45
+
46
+ def get_server_details(server_id)
47
+ @compute.get_server_details(server_id)[:body]['server']
48
+ end
49
+
50
+ def create_port(network_id, options)
51
+ @network.create_port(network_id, options)
52
+ end
53
+
54
+ def delete_port(port_id)
55
+ @network.delete_port(port_id)
56
+ end
57
+
58
+ end
59
+ end
60
+ end
data/lib/pec/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pec
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
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.1.5
4
+ version: 0.1.6
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-06-11 00:00:00.000000000 Z
11
+ date: 2015-06-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: openstac vm booter.
14
14
  email:
@@ -36,17 +36,25 @@ files:
36
36
  - lib/pec/compute/image.rb
37
37
  - lib/pec/compute/security_group.rb
38
38
  - lib/pec/compute/server.rb
39
+ - lib/pec/compute/tenant.rb
39
40
  - lib/pec/configure.rb
40
41
  - lib/pec/configure/ethernet.rb
41
42
  - lib/pec/configure/host.rb
42
43
  - lib/pec/configure/sample.rb
43
44
  - lib/pec/configure/user_data.rb
45
+ - lib/pec/director.rb
46
+ - lib/pec/director/destroy_director.rb
47
+ - lib/pec/director/helper.rb
48
+ - lib/pec/director/make_director.rb
49
+ - lib/pec/director/vm_status_director.rb
44
50
  - lib/pec/errors.rb
51
+ - lib/pec/init.rb
45
52
  - lib/pec/network/port.rb
46
53
  - lib/pec/network/subnet.rb
47
54
  - lib/pec/query.rb
55
+ - lib/pec/resource.rb
56
+ - lib/pec/resource/openstack.rb
48
57
  - lib/pec/version.rb
49
- - lib/pec/vm_director.rb
50
58
  - pec.gemspec
51
59
  homepage: http://ten-snapon.com
52
60
  licenses:
@@ -1,54 +0,0 @@
1
- module Pec
2
- class VmDirector
3
- def initialize
4
- @subnet = Pec::Network::Subnet.new
5
- @flavor = Pec::Compute::Flavor.new
6
- @image = Pec::Compute::Image.new
7
- @security_group = Pec::Compute::Security_Group.new
8
- @compute = Pec::Compute::Server.new
9
- end
10
-
11
- def make(config)
12
- if @compute.exists?(config.name)
13
- puts "skip create server! name:#{config.name} is exists!"
14
- return true
15
- end
16
-
17
- ports = get_ports(config)
18
- flavor_ref = @flavor.get_ref(config.flavor)
19
- image_ref = @image.get_ref(config.image)
20
- options = { "user_data" => Pec::Configure::UserData.make(config, ports) }
21
-
22
- @compute.create(config.name, image_ref, flavor_ref, ports, options)
23
- end
24
-
25
- def destroy!(server_name)
26
- @compute.destroy!(host.name)
27
- end
28
-
29
- def get_ports(config)
30
- config.networks.map do |ether|
31
- begin
32
- ip = IP.new(ether.ip_address)
33
- rescue ArgumentError => e
34
- raise(Pec::Errors::Port, "ip:#{ether.ip_address} #{e}")
35
- end
36
-
37
- subnet = @subnet.fetch(ip.network.to_s)
38
- raise(Pec::Errors::Subnet, "subnet:#{ip.network.to_s} is not fond!") unless subnet
39
-
40
- port = Pec::Network::Port.new(ether.name, ip.to_addr, subnet, get_security_group_id(config.security_group))
41
- raise(Pec::Errors::Port, "ip addess:#{ip.to_addr} can't create port!") unless port.assign!(ip)
42
- port
43
- end if config.networks
44
- end
45
-
46
- def get_security_group_id(security_groups)
47
- security_groups.map do |name|
48
- sg = @security_group.fetch(name)
49
- raise(Pec::Errors::SecurityGroup, "security_group:#{name} is not found!") unless sg
50
- sg["id"]
51
- end if security_groups
52
- end
53
- end
54
- end