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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7e40fd385f8f00a463c8502e04fb54f7d4f63562
4
- data.tar.gz: 01a91853581553bbaf0de49276d53da6d1d10ce0
3
+ metadata.gz: a5cf1494be11d93a996a0c39fa92a1c52f27dbd5
4
+ data.tar.gz: 7245362caf0bad80f39bca9ca32caf5bf91bdd40
5
5
  SHA512:
6
- metadata.gz: a15df2dd6f627fafe2a1de6165fbc668fc468dd86b7ccf7cdeac251206248707f73d390904c58c6d23e935a0c9820d689a20b8fe56ba5ea2b90ab2a7c0e7ff7b
7
- data.tar.gz: 60e3cb500b327c773dc930977f3af95e693f1de186747d4d1c8b9ae6161f19f4bbb19aa2ae8c45ebf306934ba5ccd1ac21b231bb321bbe0ae61d83cc20cbbdf4
6
+ metadata.gz: 5f83e4c1a6f546027c274ecf619f93ebc1aa286c777f87d2230215077d503d4651056f03cbe5608a2a4bb1d02b84445fdeb4f4a2cc6d796dae5a99ba03803438
7
+ data.tar.gz: b1afbe56b35f74a62367bf01ea3714428f69942c972625e93966bb41cf9049d684ce9b808b31fdc4e00b2c742d359170b523a9a4d7f9a5870604e4fba0e02fb1
@@ -1,207 +1,214 @@
1
1
  require_relative "node_group"
2
2
 
3
- # Definisce un cluster, ovvero l'insieme di 1..n gruppi di nodi con caratteristiche simili.
4
- class Cluster
5
-
6
- attr_reader :name
7
- attr_reader :multimachine_filter
8
- attr_accessor :box
9
- attr_accessor :domain
10
- attr_accessor :ansible_playbook_path
11
- attr_accessor :ansible_context_vars
12
- attr_accessor :ansible_group_vars
13
- attr_accessor :ansible_host_vars
14
-
15
- # Costruttore di una istanza di cluster.
16
- def initialize(name)
17
- @group_index = 0
18
- @node_groups = {}
19
- @ansible_context_vars = {}
20
- @ansible_group_vars = {}
21
- @ansible_host_vars = {}
22
- @multimachine_filter = ""
23
- @ansible_playbook_path = File.join(Dir.pwd, 'provisioning')
24
-
25
- @name = name
26
- @box = 'ubuntu/trusty64'
27
- @domain = 'vagrant'
28
- end
29
-
30
- # Metodo per la creazione di un gruppo di nodi; in fase di creazione, il blocco inizializza
31
- # i valori/le expressioni da utilizzarsi nella valorizzazione degli attributi dei nodi in fase di compose.
32
- #
33
- # Oltre alla creazione dei nodi, il metodo prevede anche l'esecuzione di un blocco di codice per
34
- # la configurazione del gruppo di nodi stesso.
35
- def nodes(instances, name, &block)
36
- raise RuntimeError, "Nodes #{name} already exists in this cluster." unless not @node_groups.has_key?(name)
37
-
38
- @node_groups[name] = NodeGroup.new(@group_index, instances, name)
39
- @node_groups[name].box = @box
40
- @node_groups[name].boxname = lambda { |group_index, group_name, node_index| return "#{group_name}#{node_index + 1}" }
41
- @node_groups[name].hostname = lambda { |group_index, group_name, node_index| return "#{group_name}#{node_index + 1}" }
42
- @node_groups[name].aliases = []
43
- @node_groups[name].ip = lambda { |group_index, group_name, node_index| return "172.31.#{group_index}.#{100 + node_index + 1}" }
44
- @node_groups[name].cpus = 1
45
- @node_groups[name].memory = 256
46
- @node_groups[name].ansible_groups = []
47
- @node_groups[name].attributes = {}
48
-
49
- @group_index += 1
50
-
51
- block.call(@node_groups[name]) if block_given?
52
- end
53
-
54
- # Prepara il provisioning del cluster
55
- def compose
56
-
57
- @multimachine_filter = ARGV.length > 1 ? ARGV[1] : "" # detect if running vagrant up/provision MACHINE
58
-
59
- ## Fase1: Creazione dei nodi
60
-
61
- # sviluppa i vari gruppi di nodi, creando i singoli nodi
62
- nodes = []
63
-
64
- @node_groups.each do |key, group|
65
- group.compose(@name, @domain, nodes.size) do |node|
66
- nodes << node
67
- end
68
- end
69
-
70
- # sviluppa i gruppi abbinando a ciascono i nodi creati
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
- end
125
- end
126
-
127
- # cleanup ansible_group_vars files
128
- # TODO: make variable public
129
- @ansible_group_vars_path = File.join(@ansible_playbook_path, 'group_vars')
130
- # TODO: make safe
131
- FileUtils.mkdir_p(@ansible_group_vars_path) unless File.exists?(@ansible_group_vars_path)
132
- Dir.foreach(@ansible_group_vars_path) {|f| fn = File.join(@ansible_group_vars_path, f); File.delete(fn) if f.end_with?(".yml")}
133
-
134
- #generazione ansible_group_vars file (NB. 1 group = 1 gruppo host ansible)
135
- ansible_groups.each do |ansible_group, ansible_group_nodes|
136
- ansible_group_vars = {}
137
- # genero le variabili per il group
138
- provisioners = @ansible_group_vars[ansible_group]
139
- unless provisioners.nil?
140
- # se necessario, normalizzo provisioner in array provisioners
141
- provisioners = [ provisioners ] if not provisioners.respond_to?('each')
142
- # per tutti i provisioners abbinati al ruolo
143
- provisioners.each do |provisioner|
144
- begin
145
- vars = provisioner.call(context, ansible_group_nodes)
146
-
147
- #TODO: gestire conflitto (n>=2 gruppi che generano la stessa variabile - con valori diversi)
148
- ansible_group_vars = ansible_group_vars.merge(vars)
149
- rescue Exception => e
150
- raise VagrantPlugins::Compose::Errors::GroupVarExpressionError, :message => e.message, :ansible_group => ansible_group
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
- # crea il file (se sono state generate delle variabili)
156
- unless ansible_group_vars.empty?
157
- # TODO: make safe
158
- File.open(File.join(@ansible_group_vars_path,"#{ansible_group}.yml") , 'w+') do |file|
159
- file.puts YAML::dump(ansible_group_vars)
160
- end
161
- end
162
- end
163
-
164
- # cleanup ansible_host_vars files (NB. 1 nodo = 1 host)
165
- # TODO: make variable public
166
- @ansible_host_vars_path = File.join(@ansible_playbook_path, 'host_vars')
167
- # TODO: make safe
168
- FileUtils.mkdir_p(@ansible_host_vars_path) unless File.exists?(@ansible_host_vars_path)
169
- Dir.foreach(@ansible_host_vars_path) {|f| fn = File.join(@ansible_host_vars_path, f); File.delete(fn) if f.end_with?(".yml")}
170
-
171
- #generazione ansible_host_vars file
172
- nodes.each do |node|
173
- # genero le variabili per il nodo; il nodo, può essere abbinato a diversi gruppi
174
- ansible_host_vars = {}
175
- node.ansible_groups.each do |ansible_group|
176
- # genero le variabili per il gruppo
177
- provisioners = @ansible_host_vars[ansible_group]
178
- unless provisioners.nil?
179
- # se necessario, normalizzo provisioner in array provisioners
180
- provisioners = [ provisioners ] if not provisioners.respond_to?('each')
181
- # per tutti i provisioners abbinati al gruppo
182
- provisioners.each do |provisioner|
183
- begin
184
- vars = provisioner.call(context, node)
185
-
186
- #TODO: gestire conflitto (n>=2 gruppi che generano la stessa variabile - con valori diversi)
187
- ansible_host_vars = ansible_host_vars.merge(vars)
188
- rescue Exception => e
189
- raise VagrantPlugins::Compose::Errors::HostVarExpressionError, :message => e.message, :host => node.hostname, :ansible_group => ansible_group
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
- # crea il file (se sono state generate delle variabili)
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
- return nodes, ansible_groups_provision
205
- end
212
+ end
213
+ end
206
214
 
207
- end
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module Compose
3
- VERSION = "0.2.1"
3
+ VERSION = "0.2.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-compose
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabrizio Pandini