vagrant-compose 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 03a7541ea3a7eab57a665a0d510467c444c1683b
4
- data.tar.gz: c78fe102cb943bf9f724379a762a6e268c471bda
3
+ metadata.gz: 9dc0b6deae71c2ef6434438ec073f8ccf334b128
4
+ data.tar.gz: 0a79dee2c550ca94fac13c9aabd496d934fc3670
5
5
  SHA512:
6
- metadata.gz: 67e92a734dc62de90c13123df626f02830f5a328f6f7619708ef891ade36b4f890ec2f827ade85dbdf1ed91cce89bd51713d4403e038424ee8a3e78534f12e8c
7
- data.tar.gz: 0640c723ad84731e00651c5f50ebad963e7fec8931057e12b0ae2542c334099300f97c33d079ae802d38068d94f8e469b5844d09c0ac3dedb64099c17efe09fa
6
+ metadata.gz: d9fc532a3c232cc18db1bc6a51927a445efa4842d2aa74d991592ac8cee667ed73b9c08b95ed29fa947e16b615b1940e3405883b6d7f08bc84e0b2e1a02986f2
7
+ data.tar.gz: fff140445bdccf9a7f27bb3e5c4517d2e7419a35d18c3189b51a95c38ddb0653bdff47d0c90e0839749fe2804ef8fc19709a883f32fe2e4228cdaf64f5403ade
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
1
  # 0.1.0 (December 27, 2015)
2
2
 
3
- * Initial release.
3
+ * Initial release.
4
+
5
+ # 0.2.0 (December 31, 2015)
6
+
7
+ breaking changes!
8
+ * nodes instances number defined into node method (instances attributes removed)
9
+ * cluster.nodes return only nodes (before nodes with index were returned)
10
+
11
+ other changes:
12
+ * Improved documentation.
13
+ * cluster domain now is optional
14
+ * nodes code block now is optional
data/README.md CHANGED
@@ -1,3 +1,438 @@
1
1
  # vagrant-compose
2
- A Vagrant plugin that helps building complex multi-machine scenarios
2
+
3
+ A Vagrant plugin that helps building complex multi-machine scenarios.
4
+
5
+ Complex multi-machine scenarios includes several set of nodes, each one with different characteristic, software stacks and configuration.
6
+
7
+ For instance, if you are setting up an environment for testing [Consul](https://consul.io/), your cluster will be composed by:
8
+
9
+ - consul server nodes
10
+ - consul agent nodes
11
+ - nodes simulating other datacenter
12
+ - ...
13
+
14
+ On top of that, a Consul cluster can be composed in several different ways, implementing high availability or not, merging roles/functions on the same server or keeping role/function separated etc. etc.
15
+
16
+ Vagrant-compose streamline the definition of complex multi-machine scenarios, providing also support for a straight forward provisioning of nodes with Ansible.
17
+
18
+ > So far, the plugin is designed for working with Ansible provisioning, but it can be easily used/extended for supporting other provisioning systems supported by Vagrant.
19
+
20
+ ## Installation
21
+
22
+ Install the plugin following the typical Vagrant procedure:
23
+
24
+ ```
25
+ $ vagrant plugin install vagrant-compose
26
+ ```
27
+
28
+ ## Quick start
29
+
30
+ Create the following `Vagrantfile` for implementing a multi-machine scenario that defines a cluster named `test` with 3 `consul-server` nodes.
31
+
32
+ ``` ruby
33
+ Vagrant.configure(2) do |config|
34
+ #cluster definition
35
+ config.cluster.compose('test') do |c|
36
+ c.nodes(3, 'consul-server')
37
+ end
38
+
39
+ #cluster creation
40
+ config.cluster.nodes.each do |node, index|
41
+ config.vm.define "#{node.boxname}" do |node_vm|
42
+ node_vm.vm.box = "#{node.box}"
43
+ end
44
+ end
45
+ end
46
+ ```
47
+
48
+ The first part of the `Vagrantfile` contains the definition of the `test` cluster:
49
+
50
+ ``` ruby
51
+ config.cluster.compose('test') do |c|
52
+ ...
53
+ end
54
+ ```
55
+
56
+ Please note that the cluster definition, is followed by a block of code that allows to configure the cluster itself; in this example the configuration consists in defining a set of 3 `consul-server` nodes.
57
+
58
+ ``` ruby
59
+ c.nodes(3, 'consul-server')
60
+ ```
61
+
62
+ When the definition of the cluster is completed, behind the scene vagrant-compose take care of composing the cluster, and the resulting list of nodes will be available in the `config.cluster.nodes` variable.
63
+
64
+ The second part of the `Vagrantfile` creates the cluster by defining a vm in VirtualBox for each node in the cluster:
65
+
66
+ ``` ruby
67
+ config.cluster.nodes.each do |node, index|
68
+ config.vm.define "#{node.boxname}" do |node_vm|
69
+ node_vm.vm.box = "#{node.box}"
70
+ end
71
+ end
72
+ ```
73
+
74
+ If you run `vagrant up` you will get a 3 node cluster with following machines, based on `ubuntu/trusty64` base box (default).
75
+
76
+ - `test-consul-server1`
77
+ - `test-consul-server2`
78
+ - `test-consul-server3`
79
+
80
+ Done !
81
+
82
+ Of course, real-word scenarios are more complex; it is necessary to get more control in configuring the cluster topology and machine attributes, and finally you need also to implement automatic provisioning of software stack installed in the machines.
83
+
84
+ See following chapters for more details.
85
+
86
+ ## Configuring the cluster
87
+
88
+ There are several options to customise the cluster definition.
89
+
90
+ ### Defining cluster attributes
91
+
92
+ Cluster attributes apply to all the node in the cluster.
93
+
94
+ You can set set cluster attributes in the block of code that is passed as a second parameter to the `cluster.compose` method, as show in the following example:
95
+
96
+ ``` ruby
97
+ config.cluster.compose('test') do |c|
98
+ c.box = "centos/7"
99
+ ...
100
+ end
101
+ ```
102
+
103
+ Following cluster attributes are available:
104
+
105
+ - **box**, [String], default = 'ubuntu/trusty64'
106
+
107
+ Sets the base box for nodes, a.k.a the image that will be used to spin up the machine; please note that the base box can be customised also for each set of nodes (see Defining set of nodes).
108
+
109
+
110
+ - **domain**, [String], default = 'vagrant'
111
+
112
+ Sets the domain used for computing the nodes in the cluster; if the `domain` value is set to `nil` or `““` (empty string), the fully qualified name and the hostname of each nodes will be the same.
113
+
114
+ ### Defining set of nodes
115
+
116
+ A cluster can be composed by one or more set of nodes.
117
+
118
+ Each set of nodes represent a group of one or more nodes with similar characteristics. For instance, in a cluster defined for testing [Consul](https://consul.io/), you will get at least two set of nodes:
119
+
120
+ - Consul server nodes
121
+ - Consul agent nodes
122
+
123
+ Set of nodes can be defined in the block of code that is passed as a second parameter to the `cluster.compose` method, by using the `nodes` method as show in the following example:
124
+
125
+ ``` ruby
126
+ config.cluster.compose('test') do |c|
127
+ ...
128
+ c.nodes(3, 'zookeeper')
129
+ ...
130
+ end
131
+ ```
132
+
133
+ The first parameter of the `nodes` method is the number of nodes in the set, while the second parameter is the name of the set; `nodes` accepts an optional third parameter, allowing to define a block of code where it is possible to customise several attributes of the set of nodes itself:
134
+
135
+ ``` ruby
136
+ config.cluster.compose('test') do |c|
137
+ ...
138
+ c.nodes(3, 'zookeeper') do |n|
139
+ n.box = "centos/7"
140
+ end
141
+ ...
142
+ end
143
+ ```
144
+
145
+ Please note that all the available attributes can be set to:
146
+
147
+ - A literal value, like for instance `"centos/7". This value will be inherited - without changes - by all nodes in the set.
148
+
149
+ - A block of code, afterwards value_generator, that will be executed when building the nodes in the set. When calling the block of code, three parameters will be given:
150
+
151
+ - **group_index**, [integer (zero based)], uniquely assigned to each set of nodes
152
+ - **group_name**, [String], with the name of the set of nodes
153
+ - **node_index**, [integer (zero based)], uniquely assigned to each node in the set
154
+
155
+ An example of value_generator is the following lambda expression, that computes the host-name for each node in the cluster (`test-consul-server1`, `test-consul-server2`, etc. etc.):
156
+
157
+ ``` ruby
158
+ lambda { |group_index, group_name, node_index|
159
+ return "#{group_name}#{node_index + 1}"
160
+ }
161
+ ```
162
+
163
+
164
+
165
+ Following set of nodes attributes are available:
166
+
167
+ - **box**, [String|String_Generator], default = `cluster.box`
168
+
169
+ Sets the base box used for creating nodes in this set.
170
+
171
+ - **boxname**, [String|String_Generator], default = `"#{group_name}#{node_index + 1}"`
172
+
173
+ Sets the box name (a.k.a. the name of the machine in VirtualBox/VMware) for each node in this set.
174
+
175
+ Note: when generating nodes, the resulting boxname will be automatically prefixed by `"#{cluster_name}-"`.
176
+
177
+ - **hostname**, [String|String_Generator], default = `"#{group_name}#{node_index + 1}"`
178
+
179
+ Sets the hostname for each node in this set.
180
+
181
+ Note: when generating nodes, the resulting hostname will be automatically prefixed by `"#{cluster_name}-"`; additionally the **fqdn** attribute will be computed by concatenating `".#{cluster.domain}"`, if defined (if `domain` is not defined, fqdn will be the same of hostname).
182
+
183
+
184
+ - **aliases**, [Array(String)|Array(String)_Generator], default = `[]`
185
+
186
+ Allows to provide aliases for each node in this set.
187
+
188
+ Note: when generating nodes, aliases will be automatically concatenated into a string, comma separated.
189
+
190
+
191
+ - **ip**, [String|String_Generator], default = `"172.31.#{group_index}.#{100 + node_index + 1}"`
192
+
193
+ Sets the ip for for each node in this set.
194
+
195
+
196
+ - **cpus**, [Integer|Integer_Generator], default = `1`
197
+
198
+ Sets the number of cpus for each node in this set.
199
+
200
+
201
+ - **memory**, [Integer|Integer_Generator], default = `256` (MB)
202
+
203
+ Sets the memory allocated for each node in this set.
204
+
205
+
206
+ - **attributes**, [Hash(String, obj)|Hash(String, obj)_Generator], default = `{}`
207
+
208
+ Allows to provide customisable additional attributes for each node in this set.
209
+
210
+ > Please note that some attribute, like boxname, hostname, ip, *must* be different for each node in the set (and in the cluster).
211
+ >
212
+ > Use value_generators for those attributes.
213
+
214
+ ### Composing nodes
215
+
216
+ By executing the code blocks provided to `cluster.compose` method, and also inner code blocks provided to `nodes` calls, the vagrant-compose plugin can compose the cluster topology, as a sum of all the nodes generated by each set.
217
+
218
+ The resulting list of nodes is stored in the `config.cluster.nodes` variable; each node has following attributes assigned using value/value generators:
219
+
220
+ - **box**
221
+ - **boxname**
222
+ - **hostname**
223
+ - **fqdn**
224
+
225
+
226
+ - **aliases**
227
+
228
+
229
+ - **ip**
230
+
231
+
232
+ - **cpus**
233
+
234
+
235
+ - **memory**
236
+
237
+
238
+ - **attributes**
239
+
240
+
241
+
242
+ Two additional attributes will be automatically set for each node:
243
+
244
+ - **index**, [integer (zero based)], uniquely assigned to each node in the cluster
245
+ - **group_index**, [integer (zero based)], uniquely assigned to each node in a set of nodes
246
+
247
+ ## Creating nodes
248
+
249
+ Given the list of nodes stored in the `config.cluster.nodes` variable, it is possible to create a multi-machine environment by iterating over the list:
250
+
251
+ ``` ruby
252
+ config.cluster.nodes.each do |node|
253
+ ...
254
+ end
255
+ ```
256
+
257
+ Within the cycle you can instruct vagrant to create machines based on attributes of the current node; for instance, you can define a VM in VirtualBox (default Vagrant provider); the example uses the [vagrant-hostmanager](https://github.com/smdahlen/vagrant-hostmanager) plugin to set the hostname into the guest machine:
258
+
259
+ ``` ruby
260
+ config.cluster.nodes.each do |node|
261
+ config.vm.define "#{node.boxname}" do |node_vm|
262
+ node_vm.vm.box = "#{node.box}"
263
+ node_vm.vm.network :private_network, ip: "#{node.ip}"
264
+ node_vm.vm.hostname = "#{node.fqdn}"
265
+ node_vm.hostmanager.aliases = node.aliases unless node.aliases.empty?
266
+ node_vm.vm.provision :hostmanager
267
+
268
+ node_vm.vm.provider "virtualbox" do |vb|
269
+ vb.name = "#{node.boxname}"
270
+ vb.memory = node.memory
271
+ vb.cpus = node.cpus
272
+ end
273
+ end
274
+ end
275
+ ```
276
+
277
+ > In order to increase performance of node creation, you can leverage on support for linked clones introduced by Vagrant 1.8.1. Add the following line to the above script:
278
+ >
279
+ > vb.linked_clone = true if Vagrant::VERSION =~ /^1.8/
280
+
281
+ Hostmanager requires following additional settings before the `config.cluster.nodes.each` command:
282
+
283
+ ``` ruby
284
+ config.hostmanager.enabled = false
285
+ config.hostmanager.manage_host = true
286
+ config.hostmanager.include_offline = true
287
+ ```
288
+
289
+ ## Configuring ansible provisioning
290
+
291
+ The vagrant-compose plugin provides support for a straight forward provisioning of nodes in the cluster implemented with Ansible.
292
+
293
+ ### Defining ansible_groups
294
+
295
+ Each set of nodes, and therefore all the nodes within the set, can be assigned to one or more ansible_groups.
296
+
297
+ In the following example, `consul-agent` nodes will be part of `consul` and `docker` ansible_groups.
298
+
299
+ ``` ruby
300
+ c.nodes(3, 'consul-agent') do |n|
301
+ ...
302
+ n.ansible_groups = ['consul', 'docker']
303
+ end
304
+ ```
305
+
306
+ This configuration is used by the `cluster.compose` method in order to define an **inventory file** where nodes (hosts in ansible "") clustered in group; the resulting list of ansible_groups, each with its own list of host is stored in the `config.cluster.ansible_groups` variable.
307
+
308
+ Ansible playbook will use groups for providing different software stack to different machines.
309
+
310
+ Please note that the possibility to assign a node to one or more groups introduces an high degree of flexibility; for instance, it is easy to change the topology of the cluster above for instance when it is required to implement an http load balancer based on consul service discovery:
311
+
312
+ ``` ruby
313
+ c.nodes(3, 'consul-agent') do |n|
314
+ ...
315
+ n.ansible_groups = ['consul', 'docker', 'registrator']
316
+ end
317
+ c.nodes(1, 'load-balancer') do |n|
318
+ ...
319
+ n.ansible_groups = ['consul', 'docker', 'consul-template', 'nginx']
320
+ end
321
+ ```
322
+
323
+ As you can see, `consul` and `docker` ansible_groups now include both nodes from `consul-agent` and `load-balancer` node set; vice versa, other groups like `registrator`, `consul-template`, `nginx` contain node only from one of the two nodes set.
324
+
325
+ Ansible playbook can leverage on groups for providing machines with the required software stacks.
326
+
327
+ ### Defining group vars
328
+
329
+ In Ansible, the inventory file is usually integrated with a set of variables containing settings that will influence playbooks behaviour for all the host in a group.
330
+
331
+ The vagrant-compose plugin allows you to define one or more group_vars generator for each ansible_groups; group_vars generators are code block that will be instantiated during `cluster.compose` with two input parameters:
332
+
333
+ - **context_vars** see below
334
+ - **nodes**, list of nodes in the ansible_group
335
+
336
+ Expected output type is `Hash(String, Obj)`.
337
+
338
+ For instance, when building a [Consul](https://consul.io/) cluster, all the `consul-server` nodes have to be configured with the same `bootstrap_expect` parameter, that must be set to the number of `consul-server` nodes in the cluster:
339
+
340
+ ``` ruby
341
+ config.cluster.compose('test') do |c|
342
+ ...
343
+ c.ansible_group_vars['consul-server'] = lambda { |context, nodes|
344
+ return { 'consul_bootstrap_expect' => nodes.length }
345
+ }
346
+ ...
347
+ end
348
+ ```
349
+
350
+ Ansible group vars will be stored into yaml files saved into `{cluster.ansible_playbook_path}\group_vars` folder.
351
+
352
+ The variable `cluster.ansible_playbook_path` defaults to the current directory (the directory of the Vagrantfile) + `/provisioning`; this value can be changed like any other cluster attributes (see Defining cluster attributes).
353
+
354
+ ### Defining host vars
355
+
356
+ While group vars will influence playbooks behaviour for all hosts in a group, in Ansible host vars will influence playbooks behaviour for a specific host.
357
+
358
+ The vagrant-compose plugin allows to define one or more host_vars generator for each ansible_groups; host_vars generators are code block that will be instantiated during `cluster.compose` with two input parameters:
359
+
360
+ - **context_vars** see below
361
+ - **node**, one node in the ansible_group
362
+
363
+ Expected output type is `Hash(String, Obj)`.
364
+
365
+ For instance, when building a [Consul](https://consul.io/) cluster, all the `consul-server` nodes should be configured with the ip to which Consul will bind client interfaces:
366
+
367
+ ``` ruby
368
+ config.cluster.compose('test') do |c|
369
+ ...
370
+ c.ansible_host_vars['consul-server'] = lambda { |context, node|
371
+ return { 'consul_client_ip' => node.ip }
372
+ }
373
+ ...
374
+ end
375
+ ```
376
+
377
+ Ansible host vars will be stored into yaml files saved into `{cluster.ansible_playbook_path}\host_vars` folder.
378
+
379
+ ### Context vars
380
+
381
+ Group vars and host var generation by design can operate only with the set of information that comes with a groups of nodes or a single node.
382
+
383
+ However, sometimes, it is necessary to share some information across group of nodes, like for instance providing information about zookeeper nodes to mesos master nodes.
384
+
385
+ This can be achieved by setting one or more context_vars generator for each ansible_groups.
386
+
387
+ > Context_vars generator are always executed before group_vars and host_vars generators; the resulting context, is given in input to group_vars and host_vars generators.
388
+
389
+ For instance, when building a [Consul](https://consul.io/) cluster, all the `consul-agent` nodes should be configured with the ip - the list of ip - to be used when joining the cluster; such list can be generated from the list of nodes in the `consul-server` set of nodes, and stored in a context_vars:
390
+
391
+ ``` ruby
392
+ config.cluster.compose('test') do |c|
393
+ ...
394
+ c.context_vars['consul-server'] = lambda { |context, nodes|
395
+ return { 'consul-serverIPs' => nodes.map { |n| n.ip }.to_a }
396
+ }
397
+ ...
398
+ end
399
+ ```
400
+
401
+ Then, you can use the above context var when generating group_vars for nodes in the `consul-agent` group.
402
+
403
+ ``` ruby
404
+ config.cluster.compose('test') do |c|
405
+ ...
406
+ c.context_vars['consul-server'] = lambda { |context, nodes|
407
+ return { 'serverIPs' => nodes.map { |n| n.ip }.to_a }
408
+ }
409
+ c.group_vars['consul-agent'] = lambda { |context, nodes|
410
+ return { 'consul_joins' => context['consul-serverIPs'] }
411
+ }
412
+ ...
413
+ end
414
+ ```
415
+
416
+ ## Creating nodes (with provisioning)
417
+
418
+ Given `config.cluster.ansible_groups` variable, generated group_vars and host_vars files, and of course an ansible playbook, it is possible to integrate provisioning into the node creation sequence.
419
+
420
+ NB. The example uses ansible parallel execution (all nodes are provisioned together in parallel after completing node creation).
421
+
422
+ ``` ruby
423
+ config.cluster.nodes.each do |node|
424
+ config.vm.define "#{node.boxname}" do |node_vm|
425
+ ...
426
+ if node.index == config.cluster.nodes.size - 1
427
+ node_vm.vm.provision "ansible" do |ansible|
428
+ ansible.limit = 'all' # enable parallel provisioning
429
+ ansible.playbook = "provisioning/playbook.yml"
430
+ ansible.groups = config.cluster.ansible_groups
431
+ end
432
+ end
433
+ end
434
+ end
435
+
436
+
437
+ ```
3
438
 
data/Rakefile CHANGED
@@ -18,6 +18,4 @@ Bundler::GemHelper.install_tasks
18
18
  RSpec::Core::RakeTask.new
19
19
 
20
20
  # Default task is to run the unit tests
21
- task :default => "spec"
22
-
23
- print "zzz"
21
+ task :default => "spec"
@@ -29,7 +29,6 @@ module VagrantPlugins
29
29
  puts "==> cluster #{@cluster.name} with #{nodes.size} nodes"
30
30
  @nodes.each do |node|
31
31
  puts " #{node.boxname} accessible as #{node.fqdn} #{node.aliases} #{node.ip} => [#{node.box}, #{node.cpus} cpus, #{node.memory} memory]"
32
-
33
32
  end
34
33
  puts " ansible_groups filtered by #{@cluster.multimachine_filter}" if not @cluster.multimachine_filter.empty?
35
34
  end
@@ -14,7 +14,7 @@ class Cluster
14
14
 
15
15
  # Costruttore di una istanza di cluster.
16
16
  def initialize(name)
17
- @group_uid = 0
17
+ @group_index = 0
18
18
  @node_groups = {}
19
19
  @ansible_context_vars = {}
20
20
  @ansible_group_vars = {}
@@ -32,23 +32,23 @@ class Cluster
32
32
  #
33
33
  # Oltre alla creazione dei nodi, il metodo prevede anche l'esecuzione di un blocco di codice per
34
34
  # la configurazione del gruppo di nodi stesso.
35
- def nodes(name, &block)
35
+ def nodes(instances, name, &block)
36
36
  raise RuntimeError, "Nodes #{name} already exists in this cluster." unless not @node_groups.has_key?(name)
37
37
 
38
- @node_groups[name] = NodeGroup.new(@group_uid, name)
38
+ @node_groups[name] = NodeGroup.new(@group_index, instances, name)
39
39
  @node_groups[name].box = @box
40
- @node_groups[name].boxname = lambda { |group_uid, group_name, node_index| return "#{group_name}#{node_index + 1}" }
41
- @node_groups[name].hostname = lambda { |group_uid, group_name, node_index| return "#{group_name}#{node_index + 1}" }
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
42
  @node_groups[name].aliases = []
43
- @node_groups[name].ip = lambda { |group_uid, group_name, node_index| return "172.31.#{group_uid}.#{100 + node_index + 1}" }
43
+ @node_groups[name].ip = lambda { |group_index, group_name, node_index| return "172.31.#{group_index}.#{100 + node_index + 1}" }
44
44
  @node_groups[name].cpus = 1
45
45
  @node_groups[name].memory = 256
46
46
  @node_groups[name].ansible_groups = []
47
47
  @node_groups[name].attributes = {}
48
48
 
49
- @group_uid += 1
49
+ @group_index += 1
50
50
 
51
- block.call(@node_groups[name])
51
+ block.call(@node_groups[name]) if block_given?
52
52
  end
53
53
 
54
54
  # Prepara il provisioning del cluster
@@ -201,7 +201,7 @@ class Cluster
201
201
  end
202
202
  end
203
203
 
204
- return nodes.map.with_index, ansible_groups_provision
204
+ return nodes, ansible_groups_provision
205
205
  end
206
206
 
207
207
  end
@@ -1,30 +1,36 @@
1
- # Definisce un nodo, ovvero uno delle istanze di nodi che compongono il cluster
2
- class Node
3
- attr_reader :box
4
- attr_reader :boxname
5
- attr_reader :hostname
6
- attr_reader :fqdn
7
- attr_reader :aliases
8
- attr_reader :ip
9
- attr_reader :cpus
10
- attr_reader :memory
11
- attr_reader :ansible_groups
12
- attr_reader :attributes
13
- attr_reader :index
14
- attr_reader :group_index
1
+ module VagrantPlugins
2
+ module Compose
15
3
 
16
- def initialize(box, boxname, hostname, fqdn, aliases, ip, cpus, memory, ansible_groups, attributes, index, group_index)
17
- @box = box
18
- @boxname = boxname
19
- @hostname = hostname
20
- @fqdn = fqdn
21
- @aliases = aliases
22
- @ip = ip
23
- @cpus = cpus
24
- @memory = memory
25
- @ansible_groups = ansible_groups
26
- @attributes = attributes
27
- @index = index
28
- @group_index = group_index
4
+ # Definisce un nodo, ovvero uno delle istanze di nodi che compongono il cluster
5
+ class Node
6
+ attr_reader :box
7
+ attr_reader :boxname
8
+ attr_reader :hostname
9
+ attr_reader :fqdn
10
+ attr_reader :aliases
11
+ attr_reader :ip
12
+ attr_reader :cpus
13
+ attr_reader :memory
14
+ attr_reader :ansible_groups
15
+ attr_reader :attributes
16
+ attr_reader :index
17
+ attr_reader :group_index
18
+
19
+ def initialize(box, boxname, hostname, fqdn, aliases, ip, cpus, memory, ansible_groups, attributes, index, group_index)
20
+ @box = box
21
+ @boxname = boxname
22
+ @hostname = hostname
23
+ @fqdn = fqdn
24
+ @aliases = aliases
25
+ @ip = ip
26
+ @cpus = cpus
27
+ @memory = memory
28
+ @ansible_groups = ansible_groups
29
+ @attributes = attributes
30
+ @index = index
31
+ @group_index = group_index
32
+ end
33
+ end
34
+
29
35
  end
30
36
  end
@@ -1,58 +1,65 @@
1
1
  require_relative "node"
2
2
 
3
- # Definisce un node group, ovvero un insieme di nodi con caratteristiche omogenee.
4
- # i singoli nodi del gruppo, sono generati in fase di compose tramite delle espressioni
5
- # che generano i valori degli attributi che caratterizzano ogni nodo
6
- class NodeGroup
7
- attr_reader :uid, :name
8
- attr_accessor :instances
9
- attr_accessor :box
10
- attr_accessor :boxname
11
- attr_accessor :hostname
12
- attr_accessor :aliases
13
- attr_accessor :ip
14
- attr_accessor :cpus
15
- attr_accessor :memory
16
- attr_accessor :ansible_groups
17
- attr_accessor :attributes
3
+ module VagrantPlugins
4
+ module Compose
18
5
 
19
- def initialize(uid, name)
20
- @uid = uid
21
- @name = name
22
- @instances = 1
23
- end
6
+ # Definisce un node group, ovvero un insieme di nodi con caratteristiche omogenee.
7
+ # i singoli nodi del gruppo, sono generati in fase di compose tramite delle espressioni
8
+ # che generano i valori degli attributi che caratterizzano ogni nodo
9
+ class NodeGroup
10
+ attr_reader :uid
11
+ attr_reader :name
12
+ attr_reader :instances
13
+ attr_accessor :box
14
+ attr_accessor :boxname
15
+ attr_accessor :hostname
16
+ attr_accessor :aliases
17
+ attr_accessor :ip
18
+ attr_accessor :cpus
19
+ attr_accessor :memory
20
+ attr_accessor :ansible_groups
21
+ attr_accessor :attributes
24
22
 
25
- # compone il gruppo, generando le istanze dei vari nodi
26
- def compose(cluster_name, cluster_domain, cluster_offset)
27
- node_index = 0
28
- while node_index < @instances
29
- box = generate(:box, @box, node_index)
30
- boxname = "#{cluster_name}-#{generate(:boxname, @boxname, node_index)}"
31
- hostname = "#{cluster_name}-#{generate(:hostname, @hostname, node_index)}"
32
- aliases = generate(:aliases, @aliases, node_index).join(',')
33
- fqdn = "#{hostname}.#{cluster_domain}"
34
- ip = generate(:ip, @ip, node_index)
35
- cpus = generate(:cpus, @cpus, node_index)
36
- memory = generate(:memory, @memory, node_index)
37
- ansible_groups = generate(:ansible_groups, @ansible_groups, node_index)
38
- attributes = generate(:attributes, @attributes, node_index)
39
- yield Node.new(box, boxname, hostname, fqdn, aliases, ip, cpus, memory, ansible_groups, attributes, cluster_offset + node_index, node_index)
23
+ def initialize(index, instances, name)
24
+ @index = index
25
+ @name = name
26
+ @instances = instances
27
+ end
40
28
 
41
- node_index += 1
42
- end
43
- end
29
+ # compone il gruppo, generando le istanze dei vari nodi
30
+ def compose(cluster_name, cluster_domain, cluster_offset)
31
+ node_index = 0
32
+ while node_index < @instances
33
+ box = generate(:box, @box, node_index)
34
+ boxname = "#{cluster_name}-#{generate(:boxname, @boxname, node_index)}"
35
+ hostname = "#{cluster_name}-#{generate(:hostname, @hostname, node_index)}"
36
+ aliases = generate(:aliases, @aliases, node_index).join(',')
37
+ fqdn = cluster_domain.empty? "#{hostname}" : "#{hostname}.#{cluster_domain}"
38
+ ip = generate(:ip, @ip, node_index)
39
+ cpus = generate(:cpus, @cpus, node_index)
40
+ memory = generate(:memory, @memory, node_index)
41
+ ansible_groups = generate(:ansible_groups, @ansible_groups, node_index)
42
+ attributes = generate(:attributes, @attributes, node_index)
43
+ yield Node.new(box, boxname, hostname, fqdn, aliases, ip, cpus, memory, ansible_groups, attributes, cluster_offset + node_index, node_index)
44
+
45
+ node_index += 1
46
+ end
47
+ end
44
48
 
45
- # funzione di utilità per l'esecuzione delle espressioni che generano
46
- # i valori degli attributi
47
- def generate(var, generator, node_index)
48
- unless generator.respond_to? :call
49
- return generator
50
- else
51
- begin
52
- return generator.call(@uid, @name, node_index)
53
- rescue Exception => e
54
- raise VagrantPlugins::Compose::Errors::AttributeExpressionError, :message => e.message, :attribute => var, :node_index => node_index, :node_group_name => name
49
+ # funzione di utilità per l'esecuzione delle espressioni che generano
50
+ # i valori degli attributi
51
+ def generate(var, generator, node_index)
52
+ unless generator.respond_to? :call
53
+ return generator
54
+ else
55
+ begin
56
+ return generator.call(@index, @name, node_index)
57
+ rescue Exception => e
58
+ raise VagrantPlugins::Compose::Errors::AttributeExpressionError, :message => e.message, :attribute => var, :node_index => node_index, :node_group_name => name
59
+ end
60
+ end
55
61
  end
56
- end
62
+ end
63
+
57
64
  end
58
65
  end
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module Compose
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-compose
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabrizio Pandini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-30 00:00:00.000000000 Z
11
+ date: 2015-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -75,7 +75,6 @@ files:
75
75
  - lib/vagrant/compose/version.rb
76
76
  - lib/vagrant/compose.rb
77
77
  - LICENCE
78
- - provisioning/group_vars/zookeeper.yml
79
78
  - Rakefile
80
79
  - README.md
81
80
  - vagrant-compose.gemspec
@@ -1,2 +0,0 @@
1
- ---
2
- x: 1