vagrant-compose 0.2.1 → 0.2.2
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 +4 -4
- data/lib/vagrant/compose/util/cluster.rb +203 -196
- data/lib/vagrant/compose/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5cf1494be11d93a996a0c39fa92a1c52f27dbd5
|
4
|
+
data.tar.gz: 7245362caf0bad80f39bca9ca32caf5bf91bdd40
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f83e4c1a6f546027c274ecf619f93ebc1aa286c777f87d2230215077d503d4651056f03cbe5608a2a4bb1d02b84445fdeb4f4a2cc6d796dae5a99ba03803438
|
7
|
+
data.tar.gz: b1afbe56b35f74a62367bf01ea3714428f69942c972625e93966bb41cf9049d684ce9b808b31fdc4e00b2c742d359170b523a9a4d7f9a5870604e4fba0e02fb1
|
@@ -1,207 +1,214 @@
|
|
1
1
|
require_relative "node_group"
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
nodes
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
# NB. tiene in considerazione anche l'eventualità che un gruppo possa essere composto da nodi appartenenti a diversi node_groups
|
72
|
-
ansible_groups= {}
|
73
|
-
nodes.each do |node|
|
74
|
-
node.ansible_groups.each do |ansible_group|
|
75
|
-
ansible_groups[ansible_group] = [] unless ansible_groups.has_key? (ansible_group)
|
76
|
-
ansible_groups[ansible_group] << node
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
## Fase2: Configurazione provisioning del cluster via Ansible
|
81
|
-
# Ogni nodo diventerà una vm su cui sarà fatto il provisioning, ovvero un host nell'inventory di ansible
|
82
|
-
# Ad ogni gruppo corrispondono nodi con caratteristiche simili
|
83
|
-
|
84
|
-
# genearazione inventory file per ansible, aka ansible_groups in Vagrant (NB. 1 group = 1 gruppo ansible)
|
85
|
-
ansible_groups_provision = {}
|
86
|
-
ansible_groups.each do |ansible_group, ansible_group_nodes|
|
87
|
-
ansible_groups_provision[ansible_group] = []
|
88
|
-
ansible_group_nodes.each do |node|
|
89
|
-
ansible_groups_provision[ansible_group] << node.hostname if @multimachine_filter.empty? or @multimachine_filter == node.boxname #filter ansible groups if vagrant command on one node
|
90
|
-
end
|
91
|
-
end
|
92
|
-
ansible_groups_provision['all_groups:children'] = ansible_groups.keys
|
93
|
-
|
94
|
-
# Oltre alla creazione del file di inventory per ansible, contenente gruppi e host, è supportata:
|
95
|
-
# - la creazione di file ansible_group_vars, ovvero di file preposti a contenere una serie di variabili - specifico di ogni gruppo di host -
|
96
|
-
# per condizionare il provisioning ansible sulla base delle caratteristiche del cluster specifico
|
97
|
-
# - la creazione di file ansible_host_vars, ovvero di file preposti a contenere una serie di variabili - specifico di ogni host -
|
98
|
-
# per condizionare il provisioning ansible sulla base delle caratteristiche del cluster specifico
|
99
|
-
|
100
|
-
# La generazione delle variabili utilizza una serie di VariableProvisioner, uno o più d'uno per ogni gruppo di hosts, configurati durante la
|
101
|
-
# definizione del cluster.
|
102
|
-
|
103
|
-
context = {}
|
104
|
-
|
105
|
-
#genearazione context (NB. 1 group = 1 gruppo host ansible)
|
106
|
-
ansible_groups.each do |ansible_group, ansible_group_nodes|
|
107
|
-
|
108
|
-
# genero le variabili per il group
|
109
|
-
provisioners = @ansible_context_vars[ansible_group]
|
110
|
-
unless provisioners.nil?
|
111
|
-
# se necessario, normalizzo provisioner in array provisioners
|
112
|
-
provisioners = [ provisioners ] if not provisioners.respond_to?('each')
|
113
|
-
# per tutti i provisioners abbinati al ruolo
|
114
|
-
provisioners.each do |provisioner|
|
115
|
-
begin
|
116
|
-
vars = provisioner.call(context, ansible_group_nodes)
|
117
|
-
|
118
|
-
#TODO: gestire conflitto (n>=2 gruppi che generano la stessa variabile - con valori diversi)
|
119
|
-
context = context.merge(vars)
|
120
|
-
rescue Exception => e
|
121
|
-
raise VagrantPlugins::Compose::Errors::ContextVarExpressionError, :message => e.message, :ansible_group => ansible_group
|
3
|
+
|
4
|
+
module VagrantPlugins
|
5
|
+
module Compose
|
6
|
+
|
7
|
+
# Definisce un cluster, ovvero l'insieme di 1..n gruppi di nodi con caratteristiche simili.
|
8
|
+
class Cluster
|
9
|
+
|
10
|
+
attr_reader :name
|
11
|
+
attr_reader :multimachine_filter
|
12
|
+
attr_accessor :box
|
13
|
+
attr_accessor :domain
|
14
|
+
attr_accessor :ansible_playbook_path
|
15
|
+
attr_accessor :ansible_context_vars
|
16
|
+
attr_accessor :ansible_group_vars
|
17
|
+
attr_accessor :ansible_host_vars
|
18
|
+
|
19
|
+
# Costruttore di una istanza di cluster.
|
20
|
+
def initialize(name)
|
21
|
+
@group_index = 0
|
22
|
+
@node_groups = {}
|
23
|
+
@ansible_context_vars = {}
|
24
|
+
@ansible_group_vars = {}
|
25
|
+
@ansible_host_vars = {}
|
26
|
+
@multimachine_filter = ""
|
27
|
+
@ansible_playbook_path = File.join(Dir.pwd, 'provisioning')
|
28
|
+
|
29
|
+
@name = name
|
30
|
+
@box = 'ubuntu/trusty64'
|
31
|
+
@domain = 'vagrant'
|
32
|
+
end
|
33
|
+
|
34
|
+
# Metodo per la creazione di un gruppo di nodi; in fase di creazione, il blocco inizializza
|
35
|
+
# i valori/le expressioni da utilizzarsi nella valorizzazione degli attributi dei nodi in fase di compose.
|
36
|
+
#
|
37
|
+
# Oltre alla creazione dei nodi, il metodo prevede anche l'esecuzione di un blocco di codice per
|
38
|
+
# la configurazione del gruppo di nodi stesso.
|
39
|
+
def nodes(instances, name, &block)
|
40
|
+
raise RuntimeError, "Nodes #{name} already exists in this cluster." unless not @node_groups.has_key?(name)
|
41
|
+
|
42
|
+
@node_groups[name] = NodeGroup.new(@group_index, instances, name)
|
43
|
+
@node_groups[name].box = @box
|
44
|
+
@node_groups[name].boxname = lambda { |group_index, group_name, node_index| return "#{group_name}#{node_index + 1}" }
|
45
|
+
@node_groups[name].hostname = lambda { |group_index, group_name, node_index| return "#{group_name}#{node_index + 1}" }
|
46
|
+
@node_groups[name].aliases = []
|
47
|
+
@node_groups[name].ip = lambda { |group_index, group_name, node_index| return "172.31.#{group_index}.#{100 + node_index + 1}" }
|
48
|
+
@node_groups[name].cpus = 1
|
49
|
+
@node_groups[name].memory = 256
|
50
|
+
@node_groups[name].ansible_groups = []
|
51
|
+
@node_groups[name].attributes = {}
|
52
|
+
|
53
|
+
@group_index += 1
|
54
|
+
|
55
|
+
block.call(@node_groups[name]) if block_given?
|
56
|
+
end
|
57
|
+
|
58
|
+
# Prepara il provisioning del cluster
|
59
|
+
def compose
|
60
|
+
|
61
|
+
@multimachine_filter = ARGV.length > 1 ? ARGV[1] : "" # detect if running vagrant up/provision MACHINE
|
62
|
+
|
63
|
+
## Fase1: Creazione dei nodi
|
64
|
+
|
65
|
+
# sviluppa i vari gruppi di nodi, creando i singoli nodi
|
66
|
+
nodes = []
|
67
|
+
|
68
|
+
@node_groups.each do |key, group|
|
69
|
+
group.compose(@name, @domain, nodes.size) do |node|
|
70
|
+
nodes << node
|
122
71
|
end
|
123
|
-
end
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
72
|
+
end
|
73
|
+
|
74
|
+
# sviluppa i gruppi abbinando a ciascono i nodi creati
|
75
|
+
# NB. tiene in considerazione anche l'eventualità che un gruppo possa essere composto da nodi appartenenti a diversi node_groups
|
76
|
+
ansible_groups= {}
|
77
|
+
nodes.each do |node|
|
78
|
+
node.ansible_groups.each do |ansible_group|
|
79
|
+
ansible_groups[ansible_group] = [] unless ansible_groups.has_key? (ansible_group)
|
80
|
+
ansible_groups[ansible_group] << node
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
## Fase2: Configurazione provisioning del cluster via Ansible
|
85
|
+
# Ogni nodo diventerà una vm su cui sarà fatto il provisioning, ovvero un host nell'inventory di ansible
|
86
|
+
# Ad ogni gruppo corrispondono nodi con caratteristiche simili
|
87
|
+
|
88
|
+
# genearazione inventory file per ansible, aka ansible_groups in Vagrant (NB. 1 group = 1 gruppo ansible)
|
89
|
+
ansible_groups_provision = {}
|
90
|
+
ansible_groups.each do |ansible_group, ansible_group_nodes|
|
91
|
+
ansible_groups_provision[ansible_group] = []
|
92
|
+
ansible_group_nodes.each do |node|
|
93
|
+
ansible_groups_provision[ansible_group] << node.hostname if @multimachine_filter.empty? or @multimachine_filter == node.boxname #filter ansible groups if vagrant command on one node
|
94
|
+
end
|
95
|
+
end
|
96
|
+
ansible_groups_provision['all_groups:children'] = ansible_groups.keys
|
97
|
+
|
98
|
+
# Oltre alla creazione del file di inventory per ansible, contenente gruppi e host, è supportata:
|
99
|
+
# - la creazione di file ansible_group_vars, ovvero di file preposti a contenere una serie di variabili - specifico di ogni gruppo di host -
|
100
|
+
# per condizionare il provisioning ansible sulla base delle caratteristiche del cluster specifico
|
101
|
+
# - la creazione di file ansible_host_vars, ovvero di file preposti a contenere una serie di variabili - specifico di ogni host -
|
102
|
+
# per condizionare il provisioning ansible sulla base delle caratteristiche del cluster specifico
|
103
|
+
|
104
|
+
# La generazione delle variabili utilizza una serie di VariableProvisioner, uno o più d'uno per ogni gruppo di hosts, configurati durante la
|
105
|
+
# definizione del cluster.
|
106
|
+
|
107
|
+
context = {}
|
108
|
+
|
109
|
+
#genearazione context (NB. 1 group = 1 gruppo host ansible)
|
110
|
+
ansible_groups.each do |ansible_group, ansible_group_nodes|
|
111
|
+
|
112
|
+
# genero le variabili per il group
|
113
|
+
provisioners = @ansible_context_vars[ansible_group]
|
114
|
+
unless provisioners.nil?
|
115
|
+
# se necessario, normalizzo provisioner in array provisioners
|
116
|
+
provisioners = [ provisioners ] if not provisioners.respond_to?('each')
|
117
|
+
# per tutti i provisioners abbinati al ruolo
|
118
|
+
provisioners.each do |provisioner|
|
119
|
+
begin
|
120
|
+
vars = provisioner.call(context, ansible_group_nodes)
|
121
|
+
|
122
|
+
#TODO: gestire conflitto (n>=2 gruppi che generano la stessa variabile - con valori diversi)
|
123
|
+
context = context.merge(vars)
|
124
|
+
rescue Exception => e
|
125
|
+
raise VagrantPlugins::Compose::Errors::ContextVarExpressionError, :message => e.message, :ansible_group => ansible_group
|
126
|
+
end
|
127
|
+
end
|
151
128
|
end
|
152
|
-
end
|
153
|
-
end
|
129
|
+
end
|
154
130
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
131
|
+
# cleanup ansible_group_vars files
|
132
|
+
# TODO: make variable public
|
133
|
+
@ansible_group_vars_path = File.join(@ansible_playbook_path, 'group_vars')
|
134
|
+
# TODO: make safe
|
135
|
+
FileUtils.mkdir_p(@ansible_group_vars_path) unless File.exists?(@ansible_group_vars_path)
|
136
|
+
Dir.foreach(@ansible_group_vars_path) {|f| fn = File.join(@ansible_group_vars_path, f); File.delete(fn) if f.end_with?(".yml")}
|
137
|
+
|
138
|
+
#generazione ansible_group_vars file (NB. 1 group = 1 gruppo host ansible)
|
139
|
+
ansible_groups.each do |ansible_group, ansible_group_nodes|
|
140
|
+
ansible_group_vars = {}
|
141
|
+
# genero le variabili per il group
|
142
|
+
provisioners = @ansible_group_vars[ansible_group]
|
143
|
+
unless provisioners.nil?
|
144
|
+
# se necessario, normalizzo provisioner in array provisioners
|
145
|
+
provisioners = [ provisioners ] if not provisioners.respond_to?('each')
|
146
|
+
# per tutti i provisioners abbinati al ruolo
|
147
|
+
provisioners.each do |provisioner|
|
148
|
+
begin
|
149
|
+
vars = provisioner.call(context, ansible_group_nodes)
|
150
|
+
|
151
|
+
#TODO: gestire conflitto (n>=2 gruppi che generano la stessa variabile - con valori diversi)
|
152
|
+
ansible_group_vars = ansible_group_vars.merge(vars)
|
153
|
+
rescue Exception => e
|
154
|
+
raise VagrantPlugins::Compose::Errors::GroupVarExpressionError, :message => e.message, :ansible_group => ansible_group
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# crea il file (se sono state generate delle variabili)
|
160
|
+
unless ansible_group_vars.empty?
|
161
|
+
# TODO: make safe
|
162
|
+
File.open(File.join(@ansible_group_vars_path,"#{ansible_group}.yml") , 'w+') do |file|
|
163
|
+
file.puts YAML::dump(ansible_group_vars)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
# cleanup ansible_host_vars files (NB. 1 nodo = 1 host)
|
169
|
+
# TODO: make variable public
|
170
|
+
@ansible_host_vars_path = File.join(@ansible_playbook_path, 'host_vars')
|
171
|
+
# TODO: make safe
|
172
|
+
FileUtils.mkdir_p(@ansible_host_vars_path) unless File.exists?(@ansible_host_vars_path)
|
173
|
+
Dir.foreach(@ansible_host_vars_path) {|f| fn = File.join(@ansible_host_vars_path, f); File.delete(fn) if f.end_with?(".yml")}
|
174
|
+
|
175
|
+
#generazione ansible_host_vars file
|
176
|
+
nodes.each do |node|
|
177
|
+
# genero le variabili per il nodo; il nodo, può essere abbinato a diversi gruppi
|
178
|
+
ansible_host_vars = {}
|
179
|
+
node.ansible_groups.each do |ansible_group|
|
180
|
+
# genero le variabili per il gruppo
|
181
|
+
provisioners = @ansible_host_vars[ansible_group]
|
182
|
+
unless provisioners.nil?
|
183
|
+
# se necessario, normalizzo provisioner in array provisioners
|
184
|
+
provisioners = [ provisioners ] if not provisioners.respond_to?('each')
|
185
|
+
# per tutti i provisioners abbinati al gruppo
|
186
|
+
provisioners.each do |provisioner|
|
187
|
+
begin
|
188
|
+
vars = provisioner.call(context, node)
|
189
|
+
|
190
|
+
#TODO: gestire conflitto (n>=2 gruppi che generano la stessa variabile - con valori diversi)
|
191
|
+
ansible_host_vars = ansible_host_vars.merge(vars)
|
192
|
+
rescue Exception => e
|
193
|
+
raise VagrantPlugins::Compose::Errors::HostVarExpressionError, :message => e.message, :host => node.hostname, :ansible_group => ansible_group
|
194
|
+
end
|
195
|
+
end
|
190
196
|
end
|
191
|
-
end
|
197
|
+
end
|
198
|
+
|
199
|
+
# crea il file (se sono state generate delle variabili)
|
200
|
+
unless ansible_host_vars.empty?
|
201
|
+
# TODO: make safe
|
202
|
+
File.open(File.join(@ansible_host_vars_path,"#{node.hostname}.yml") , 'w+') do |file|
|
203
|
+
file.puts YAML::dump(ansible_host_vars)
|
204
|
+
end
|
205
|
+
end
|
192
206
|
end
|
193
|
-
end
|
194
207
|
|
195
|
-
|
196
|
-
unless ansible_host_vars.empty?
|
197
|
-
# TODO: make safe
|
198
|
-
File.open(File.join(@ansible_host_vars_path,"#{node.hostname}.yml") , 'w+') do |file|
|
199
|
-
file.puts YAML::dump(ansible_host_vars)
|
200
|
-
end
|
208
|
+
return nodes, ansible_groups_provision
|
201
209
|
end
|
202
|
-
end
|
210
|
+
end
|
203
211
|
|
204
|
-
|
205
|
-
|
212
|
+
end
|
213
|
+
end
|
206
214
|
|
207
|
-
end
|