Debian Codegraph @

lib/puppet/provider/nova_cell_v2/nova_manage.rb

raw
1 require File.join(File.dirname(__FILE__), '..','..','..', 'puppet/provider/nova')
2 require 'uri'
3
4 Puppet::Type.type(:nova_cell_v2).provide(
5 :nova_manage,
6 :parent => Puppet::Provider::Nova
7 ) do
8
9 desc "Manage nova cellv2 cells"
10
11 mk_resource_methods
12
13 def initialize(value={})
14 super(value)
15 @property_flush = {}
16 end
17
18 def self.instances
19 cells_list = nova_manage_request("cell_v2", "list_cells", "--verbose")
20
21 cells_list.split("\n")[3..-2].collect do |cell|
22 $name, $uuid, $transport_url, $database_connection = cell.split('|')[1..-1].map{ |x| x.strip}
23 default_transport_url = defaults(is_cell0($uuid))[:transport_url]
24 default_database_connection = defaults(is_cell0($uuid))[:database_connection]
25
26 if $transport_url == default_transport_url
27 $transport_url = 'default'
28 end
29 if $database_connection == default_database_connection
30 $database_connection = 'default'
31 end
32 new(
33 :name => $name,
34 :uuid => $uuid,
35 :transport_url => $transport_url,
36 :database_connection => $database_connection,
37 :ensure => :present
38 )
39 end
40 end
41
42 def self.prefetch(resources)
43 instances.each do |prov|
44 if resource = resources[prov.name]
45 resource.provider = prov
46 end
47 end
48 end
49
50 @cell0_uuid = '00000000-0000-0000-0000-000000000000'
51
52 def self.is_cell0(uuid)
53 uuid == @cell0_uuid
54 end
55
56 def self.defaults(cell0=false)
57 conf = nova_conf
58 if cell0
59 database_uri = URI.parse(conf['database']['connection'].strip)
60 database_uri.path += '_cell0'
61 {
62 :transport_url => 'none:///',
63 :database_connection => database_uri.to_s
64 }
65 else
66 {
67 :transport_url => conf['DEFAULT']['transport_url'].strip,
68 :database_connection => conf['database']['connection'].strip,
69 }
70 end
71 end
72
73 def is_cell0?
74 self.class.is_cell0(@property_hash[:uuid])
75 end
76
77 def exists?
78 @property_hash[:ensure] == :present
79 end
80
81 def create
82 optional_opts = []
83 unless resource[:name] == 'None'
84 optional_opts.push('--name').push(resource[:name])
85 end
86 {
87 :transport_url => '--transport-url',
88 :database_connection => '--database_connection'
89 }.each do |param, opt|
90 if resource[param] and resource[param] != 'default'
91 optional_opts.push(opt).push(resource[param])
92 end
93 end
94 cell_uuid = nova_manage_request('cell_v2', 'create_cell',
95 optional_opts, "--verbose"
96 )
97 @property_hash = {
98 :uuid => cell_uuid.strip(),
99 :ensure => :present,
100 :transport_url => resource[:transport_url],
101 :database_connection => resource[:database_connection]
102 }
103 end
104
105 def transport_url=(value)
106 @property_flush[:transport_url] = value
107 end
108
109 def database_connection=(value)
110 @property_flush[:database_connection] = value
111 end
112
113 def destroy
114 @property_hash.clear
115 end
116
117 def flush
118 @property_hash.update(@property_flush)
119 if @property_flush[:ensure] == :absent
120 nova_manage_request("cell_v2", "delete_cell", "--cell_uuid", @property_hash[:uuid])
121 elsif @property_flush[:transport_url] or @property_flush[:database_connection]
122 opts = []
123 if not @property_flush[:transport_url]
124 # Must pass this even when not changed or it will change to the value in nova.conf
125 @property_flush[:transport_url] = @property_hash[:transport_url]
126 end
127 if @property_flush[:transport_url] == 'default'
128 @property_flush[:transport_url] = self.class.defaults(is_cell0?)[:transport_url]
129 end
130 opts.push('--transport-url').push(@property_flush[:transport_url])
131
132 if not @property_flush[:database_connection]
133 # Must pass this even when not changed or it will change to the value in nova.conf
134 @property_flush[:database_connection] = @property_hash[:database_connection]
135 end
136 if @property_flush[:database_connection] == 'default'
137 @property_flush[:database_connection] = self.class.defaults(is_cell0?)[:database_connection]
138 end
139 opts.push('--database_connection').push(@property_flush[:database_connection])
140
141 nova_manage_request("cell_v2", "update_cell", "--cell_uuid", @property_hash[:uuid], opts)
142 end
143 @property_flush = {}
144 end
145 end