pec 0.4.4 → 0.5.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.
@@ -1,35 +0,0 @@
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
- options = Pec::Director::Helper.set_nics(options, ports)
24
- options = Pec::Director::Helper.set_availability_zone(options, host)
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
@@ -1,41 +0,0 @@
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
- availability_zone = ""
17
- compute_node = ""
18
- tenant_name = ""
19
- flavor = ""
20
- ip_address = ""
21
- if server
22
- detail = Pec::Resource.get.get_server_details(server["id"])
23
- status = detail["status"]
24
- availability_zone = detail["OS-EXT-AZ:availability_zone"]
25
- compute_node = detail["OS-EXT-SRV-ATTR:host"]
26
- tenant_name = Pec::Compute::Tenant.get_name(detail["tenant_id"])
27
- flavor = Pec::Compute::Flavor.get_name(detail["flavor"]["id"])
28
- ip_address = Pec::Director::Helper.parse_from_addresses(detail["addresses"]).join(",")
29
- end
30
-
31
- puts sprintf(" %-35s %-10s %-10s %-10s %-10s %-35s %-48s",
32
- host.name, status, tenant_name, flavor, availability_zone, compute_node, ip_address)
33
- end
34
-
35
- def err_message(e, host)
36
- puts e.magenta
37
- puts "can't create server:#{host.name}".magenta if host
38
- end
39
- end
40
- end
41
- end
@@ -1,104 +0,0 @@
1
- require 'json'
2
- module Pec
3
- class Network
4
- class Port
5
- extend Query
6
- class << self
7
- @@use_ip_list = []
8
-
9
- def assign(name, ip, subnet, security_group_ids)
10
- ip = get_free_port_ip(ip, subnet) if request_any_address?(ip, subnet)
11
- port_state = Pec::Network::PortState.new(name, fetch_by_ip(ip.to_addr))
12
-
13
- assign_port = case
14
- when port_state.exists? && !port_state.used?
15
- recreate(ip, subnet, security_group_ids)
16
- when !port_state.exists?
17
- create(ip, subnet, security_group_ids)
18
- when port_state.used?
19
- raise(Pec::Errors::Port, "ip:#{ip.to_addr} is used!")
20
- end
21
- Pec::Network::PortState.new(name, assign_port)
22
- end
23
-
24
- def create(ip, subnet, security_group_ids)
25
- options = set_security_group(security_group_ids)
26
- options = set_fixed_ip(options, subnet, ip)
27
- response = Pec::Resource.get.create_port(subnet["network_id"], options)
28
- raise(Pec::Errors::Port, "ip:#{ip.to_addr} is not created!") unless response[:status] == 201
29
- append_assigned_ip(response)
30
- port_from_response(response)
31
- end
32
-
33
- def delete(ip)
34
- target_port = fetch_by_ip(ip.to_addr)
35
- response = Pec::Resource.get.delete_port(target_port["id"]) if target_port
36
- raise(Pec::Errors::Host, "ip:#{ip.to_addr} response err status:#{response[:status]}") unless response[:status] == 204
37
- true
38
- end
39
-
40
- def recreate(ip, subnet, security_group_ids)
41
- create(ip, subnet, security_group_ids) if delete(ip)
42
- end
43
-
44
- def set_security_group(security_group_ids)
45
- { security_groups: security_group_ids }
46
- end
47
-
48
- def set_fixed_ip(options, subnet, ip)
49
- ip.to_s != subnet["cidr"] ? options.merge({ fixed_ips: [{ subnet_id: subnet["id"], ip_address: ip.to_addr}]}) : options
50
- end
51
-
52
- def append_assigned_ip(response)
53
- @@use_ip_list << ip_from_port(port_from_response(response))
54
- end
55
-
56
- def assigned_ip?(port)
57
- @@use_ip_list.include?(ip_from_port(port))
58
- end
59
-
60
- def get_free_port_ip(ip, subnet)
61
- port = get_free_port(subnet)
62
- port ? IP.new("#{ip_from_port(port)}/#{ip.pfxlen}") : ip
63
- end
64
-
65
- def fetch_by_ip(ip_addr)
66
- list.find {|p| ip_from_port(p) == ip_addr }
67
- end
68
-
69
- def port_from_response(response)
70
- response.data[:body]["port"]
71
- end
72
-
73
- def ip_from_port(port)
74
- port["fixed_ips"][0]["ip_address"]
75
- end
76
-
77
- def get_free_port(subnet)
78
- list.find do |p|
79
- same_subnet?(p, subnet) &&
80
- unused?(p) &&
81
- admin_state_up?(p) &&
82
- !assigned_ip?(p)
83
- end
84
- end
85
-
86
- def request_any_address?(ip, subnet)
87
- ip.to_s == subnet["cidr"]
88
- end
89
-
90
- def same_subnet?(port, subnet)
91
- port["fixed_ips"][0]["subnet_id"] == subnet["id"]
92
- end
93
-
94
- def unused?(port)
95
- port["device_owner"].empty?
96
- end
97
-
98
- def admin_state_up?(port)
99
- port["admin_state_up"]
100
- end
101
- end
102
- end
103
- end
104
- end
@@ -1,40 +0,0 @@
1
- require 'json'
2
- module Pec
3
- class Network
4
- class PortState
5
- attr_reader :device_name
6
- def initialize(device_name, port)
7
- @device_name = device_name
8
- @port = port
9
- end
10
-
11
- def exists?
12
- !@port.nil?
13
- end
14
-
15
- def used?
16
- @port && !@port["device_owner"].empty?
17
- end
18
-
19
- def id
20
- @port["id"]
21
- end
22
-
23
- def mac_address
24
- @port["mac_address"]
25
- end
26
-
27
- def ip_address
28
- @port["fixed_ips"][0]["ip_address"]
29
- end
30
-
31
- def network_id
32
- @port["network_id"]
33
- end
34
-
35
- def netmask(cidr)
36
- IP.new("#{@port["fixed_ips"][0]["ip_address"]}/#{cidr}").netmask.to_s
37
- end
38
- end
39
- end
40
- end
@@ -1,7 +0,0 @@
1
- module Pec
2
- class Network
3
- class Security_Group
4
- extend Query
5
- end
6
- end
7
- end
@@ -1,14 +0,0 @@
1
- module Pec
2
- class Network
3
- class Subnet
4
- extend Query
5
- class << self
6
- def fetch_by_cidr(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
11
- end
12
- end
13
- end
14
- end
@@ -1,24 +0,0 @@
1
- require 'active_support/core_ext/string/inflections'
2
- module Pec
3
- module Query
4
- def list
5
- class_name = self.name.demodulize.downcase
6
- Pec::Resource.get.send("#{class_name}_list")
7
- end
8
-
9
- def fetch(name)
10
- list.find {|s| s["name"] == name}
11
- end
12
-
13
- def get_ref(name)
14
- class_name = self.class.name.demodulize.downcase
15
- response = fetch(name)
16
- raise(Pec::Errors::Query, "#{class_name}:#{name} ref is not fond!") unless response
17
- response["links"][0]["href"]
18
- end
19
-
20
- def get_name(id)
21
- list.find {|p| p["id"] == id }["name"]
22
- end
23
- end
24
- end
@@ -1,26 +0,0 @@
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
- else
13
- @@_resource[@@_tenant] ||= Pec::Resource::Mock.new(@@_tenant)
14
- end
15
- end
16
-
17
- def set_tenant(tenant)
18
- @@_tenant = tenant
19
- end
20
-
21
- def get_tenant
22
- @@_tenant
23
- end
24
- end
25
- end
26
- end
@@ -1,172 +0,0 @@
1
- module Pec
2
- class Resource
3
- class Mock
4
- def initialize(tenant)
5
- tenant_hash = { "openstack_tenant" => tenant }
6
- end
7
-
8
- def port_list
9
- 1.upto(10).map do |c|
10
- {
11
- "id" => c,
12
- "fixed_ips" => [
13
- { "subnet_id" => c,
14
- "ip_address" => "#{c}." * 3 + "#{c}"
15
- }
16
- ],
17
- "network_id" => c,
18
- "device_owner" => c % 2 == 0 ? c.to_s : "",
19
- "admin_state_up" => c % 2 == 0 ? "True" : "False"
20
- }
21
- end
22
- end
23
-
24
- def subnet_list
25
- 1.upto(10).map do |c|
26
- {
27
- "id" => c,
28
- "cidr" => "#{c}." * 3 + "0/24",
29
- "network_id" => c
30
- }
31
- end
32
- end
33
-
34
- def server_list
35
- 10.upto(20).map do |c|
36
- {
37
- "id" => c,
38
- "name" => c,
39
- "status" => c %2 == 0 ? "Active" : "SHUTOFF"
40
- }
41
- end
42
- end
43
-
44
- def security_group_list
45
- 1.upto(10).map do |c|
46
- {
47
- "id" => c,
48
- "name" => c
49
- }
50
- end
51
- end
52
-
53
- def image_list
54
- ref_list
55
- end
56
-
57
- def flavor_list
58
- ref_list
59
- end
60
-
61
- def ref_list
62
- 1.upto(10).map do |c|
63
- {
64
- "id" => c,
65
- "name" => c,
66
- "links" => [
67
- "href" => c
68
- ]
69
- }
70
- end
71
-
72
- end
73
-
74
- def tenant_list
75
- 1.upto(10).map do |c|
76
- {
77
- "id" => c,
78
- "name" => c
79
- }
80
- end
81
- end
82
-
83
- def create_server(name, image_ref, flavor_ref, options)
84
- object = Object.new
85
- object.set_value(name, 202)
86
- object
87
- end
88
-
89
- def delete_server(server_id)
90
- object = Object.new
91
- object.set_value(server_id, 204)
92
- object
93
- end
94
-
95
- def get_server_details(server_id)
96
- if server_id.to_i % 2 == 0
97
- {
98
- "status" => "active",
99
- "OS-EXT-SRV-ATTR:host" => "#{server_id}.compute.node",
100
- "flavor" => {
101
- "id" => server_id
102
- },
103
- "tenant_id" => server_id,
104
- "addresses" => {
105
- "test_net" => [
106
- "addr" => server_id
107
- ]
108
- }
109
- }
110
- else
111
- {
112
- "status" => "uncreated",
113
- "OS-EXT-SRV-ATTR:host" => "#{server_id}.compute.node",
114
- "flavor" => {
115
- "id" => server_id
116
- },
117
- "tenant_id" => server_id,
118
- "addresses" => {
119
- "test_net" => {
120
- "addr" => server_id
121
- }
122
- }
123
- }
124
- end
125
- end
126
- def create_port(network_id, options)
127
- object = Object.new
128
- object.set_value(network_id, 201)
129
- object
130
- end
131
-
132
- def delete_port(port_id)
133
- object = Object.new
134
- object.set_value(port_id, 204)
135
- object
136
- end
137
- end
138
- end
139
- end
140
- class Object
141
- attr_reader :status
142
- def set_value(value, status)
143
- @value = value
144
- @status = @value.to_i % 2 == 0 ? 999 : status
145
- end
146
-
147
- def [](key)
148
- @status
149
- end
150
-
151
- def data
152
- {
153
- :body => {
154
- "port" => {
155
- "id" => @value,
156
- "fixed_ips" => [
157
- {
158
- "subnet_id" => @value,
159
- "ip_address" => "#{@value}." * 3 + "#{@value}"
160
- }
161
- ],
162
- "network_id" => @value,
163
- "device_owner" => @value.to_i % 2 == 0 ? @value : "",
164
- "admin_state_up" => @value.to_i % 2 == 0 ? "True" : "False"
165
- },
166
- "server" => {
167
- "id" => @value
168
- }
169
- }
170
- }
171
- end
172
- end