Class: VagrantPlugins::Compose::Cluster
- Inherits:
-
Object
- Object
- VagrantPlugins::Compose::Cluster
- Defined in:
- lib/vagrant/compose/util/cluster.rb
Overview
Definisce un cluster, ovvero l’insieme di 1..n gruppi di nodi con caratteristiche simili.
Instance Attribute Summary collapse
-
#ansible_context_vars ⇒ Object
Returns the value of attribute ansible_context_vars.
-
#ansible_group_vars ⇒ Object
Returns the value of attribute ansible_group_vars.
-
#ansible_host_vars ⇒ Object
Returns the value of attribute ansible_host_vars.
-
#ansible_playbook_path ⇒ Object
Returns the value of attribute ansible_playbook_path.
-
#box ⇒ Object
Returns the value of attribute box.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#multimachine_filter ⇒ Object
readonly
Returns the value of attribute multimachine_filter.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#compose ⇒ Object
Prepara il provisioning del cluster.
-
#initialize(name) ⇒ Cluster
constructor
Costruttore di una istanza di cluster.
-
#nodes(instances, name, &block) ⇒ Object
Metodo per la creazione di un gruppo di nodi; in fase di creazione, il blocco inizializza i valori/le expressioni da utilizzarsi nella valorizzazione degli attributi dei nodi in fase di compose.
Constructor Details
#initialize(name) ⇒ Cluster
Costruttore di una istanza di cluster.
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/vagrant/compose/util/cluster.rb', line 20 def initialize(name) @group_index = 0 @node_groups = {} @ansible_context_vars = {} @ansible_group_vars = {} @ansible_host_vars = {} @multimachine_filter = "" @ansible_playbook_path = File.join(Dir.pwd, 'provisioning') @name = name @box = 'ubuntu/trusty64' @domain = 'vagrant' end |
Instance Attribute Details
#ansible_context_vars ⇒ Object
Returns the value of attribute ansible_context_vars.
15 16 17 |
# File 'lib/vagrant/compose/util/cluster.rb', line 15 def ansible_context_vars @ansible_context_vars end |
#ansible_group_vars ⇒ Object
Returns the value of attribute ansible_group_vars.
16 17 18 |
# File 'lib/vagrant/compose/util/cluster.rb', line 16 def ansible_group_vars @ansible_group_vars end |
#ansible_host_vars ⇒ Object
Returns the value of attribute ansible_host_vars.
17 18 19 |
# File 'lib/vagrant/compose/util/cluster.rb', line 17 def ansible_host_vars @ansible_host_vars end |
#ansible_playbook_path ⇒ Object
Returns the value of attribute ansible_playbook_path.
14 15 16 |
# File 'lib/vagrant/compose/util/cluster.rb', line 14 def ansible_playbook_path @ansible_playbook_path end |
#box ⇒ Object
Returns the value of attribute box.
12 13 14 |
# File 'lib/vagrant/compose/util/cluster.rb', line 12 def box @box end |
#domain ⇒ Object
Returns the value of attribute domain.
13 14 15 |
# File 'lib/vagrant/compose/util/cluster.rb', line 13 def domain @domain end |
#multimachine_filter ⇒ Object (readonly)
Returns the value of attribute multimachine_filter.
11 12 13 |
# File 'lib/vagrant/compose/util/cluster.rb', line 11 def multimachine_filter @multimachine_filter end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/vagrant/compose/util/cluster.rb', line 10 def name @name end |
Instance Method Details
#compose ⇒ Object
Prepara il provisioning del cluster
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 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 151 152 153 154 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/vagrant/compose/util/cluster.rb', line 59 def compose @multimachine_filter = ARGV.length > 1 ? ARGV[1] : "" # detect if running vagrant up/provision MACHINE ## Fase1: Creazione dei nodi # sviluppa i vari gruppi di nodi, creando i singoli nodi nodes = [] @node_groups.each do |key, group| group.compose(@name, @domain, nodes.size) do |node| nodes << node end end # sviluppa i gruppi abbinando a ciascono i nodi creati # NB. tiene in considerazione anche l'eventualità che un gruppo possa essere composto da nodi appartenenti a diversi node_groups ansible_groups= {} nodes.each do |node| node.ansible_groups.each do |ansible_group| ansible_groups[ansible_group] = [] unless ansible_groups.has_key? (ansible_group) ansible_groups[ansible_group] << node end end ## Fase2: Configurazione provisioning del cluster via Ansible # Ogni nodo diventerà una vm su cui sarà fatto il provisioning, ovvero un host nell'inventory di ansible # Ad ogni gruppo corrispondono nodi con caratteristiche simili # genearazione inventory file per ansible, aka ansible_groups in Vagrant (NB. 1 group = 1 gruppo ansible) ansible_groups_provision = {} ansible_groups.each do |ansible_group, ansible_group_nodes| ansible_groups_provision[ansible_group] = [] ansible_group_nodes.each do |node| 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 end end ansible_groups_provision['all_groups:children'] = ansible_groups.keys # Oltre alla creazione del file di inventory per ansible, contenente gruppi e host, è supportata: # - la creazione di file ansible_group_vars, ovvero di file preposti a contenere una serie di variabili - specifico di ogni gruppo di host - # per condizionare il provisioning ansible sulla base delle caratteristiche del cluster specifico # - la creazione di file ansible_host_vars, ovvero di file preposti a contenere una serie di variabili - specifico di ogni host - # per condizionare il provisioning ansible sulla base delle caratteristiche del cluster specifico # La generazione delle variabili utilizza una serie di VariableProvisioner, uno o più d'uno per ogni gruppo di hosts, configurati durante la # definizione del cluster. context = {} #genearazione context (NB. 1 group = 1 gruppo host ansible) ansible_groups.each do |ansible_group, ansible_group_nodes| # genero le variabili per il group provisioners = @ansible_context_vars[ansible_group] unless provisioners.nil? # se necessario, normalizzo provisioner in array provisioners provisioners = [ provisioners ] if not provisioners.respond_to?('each') # per tutti i provisioners abbinati al ruolo provisioners.each do |provisioner| begin vars = provisioner.call(context, ansible_group_nodes) #TODO: gestire conflitto (n>=2 gruppi che generano la stessa variabile - con valori diversi) context = context.merge(vars) rescue Exception => e raise VagrantPlugins::Compose::Errors::ContextVarExpressionError, :message => e., :ansible_group => ansible_group end end end end # cleanup ansible_group_vars files # TODO: make variable public @ansible_group_vars_path = File.join(@ansible_playbook_path, 'group_vars') # TODO: make safe FileUtils.mkdir_p(@ansible_group_vars_path) unless File.exists?(@ansible_group_vars_path) Dir.foreach(@ansible_group_vars_path) {|f| fn = File.join(@ansible_group_vars_path, f); File.delete(fn) if f.end_with?(".yml")} #generazione ansible_group_vars file (NB. 1 group = 1 gruppo host ansible) ansible_groups.each do |ansible_group, ansible_group_nodes| ansible_group_vars = {} # genero le variabili per il group provisioners = @ansible_group_vars[ansible_group] unless provisioners.nil? # se necessario, normalizzo provisioner in array provisioners provisioners = [ provisioners ] if not provisioners.respond_to?('each') # per tutti i provisioners abbinati al ruolo provisioners.each do |provisioner| begin vars = provisioner.call(context, ansible_group_nodes) #TODO: gestire conflitto (n>=2 gruppi che generano la stessa variabile - con valori diversi) ansible_group_vars = ansible_group_vars.merge(vars) rescue Exception => e raise VagrantPlugins::Compose::Errors::GroupVarExpressionError, :message => e., :ansible_group => ansible_group end end end # crea il file (se sono state generate delle variabili) unless ansible_group_vars.empty? # TODO: make safe File.open(File.join(@ansible_group_vars_path,"#{ansible_group}.yml") , 'w+') do |file| file.puts YAML::dump(ansible_group_vars) end end end # cleanup ansible_host_vars files (NB. 1 nodo = 1 host) # TODO: make variable public @ansible_host_vars_path = File.join(@ansible_playbook_path, 'host_vars') # TODO: make safe FileUtils.mkdir_p(@ansible_host_vars_path) unless File.exists?(@ansible_host_vars_path) Dir.foreach(@ansible_host_vars_path) {|f| fn = File.join(@ansible_host_vars_path, f); File.delete(fn) if f.end_with?(".yml")} #generazione ansible_host_vars file nodes.each do |node| # genero le variabili per il nodo; il nodo, può essere abbinato a diversi gruppi ansible_host_vars = {} node.ansible_groups.each do |ansible_group| # genero le variabili per il gruppo provisioners = @ansible_host_vars[ansible_group] unless provisioners.nil? # se necessario, normalizzo provisioner in array provisioners provisioners = [ provisioners ] if not provisioners.respond_to?('each') # per tutti i provisioners abbinati al gruppo provisioners.each do |provisioner| begin vars = provisioner.call(context, node) #TODO: gestire conflitto (n>=2 gruppi che generano la stessa variabile - con valori diversi) ansible_host_vars = ansible_host_vars.merge(vars) rescue Exception => e raise VagrantPlugins::Compose::Errors::HostVarExpressionError, :message => e., :host => node.hostname, :ansible_group => ansible_group end end end end # crea il file (se sono state generate delle variabili) unless ansible_host_vars.empty? # TODO: make safe File.open(File.join(@ansible_host_vars_path,"#{node.hostname}.yml") , 'w+') do |file| file.puts YAML::dump(ansible_host_vars) end end end return nodes, ansible_groups_provision end |
#nodes(instances, name, &block) ⇒ Object
Metodo per la creazione di un gruppo di nodi; in fase di creazione, il blocco inizializza i valori/le expressioni da utilizzarsi nella valorizzazione degli attributi dei nodi in fase di compose.
Oltre alla creazione dei nodi, il metodo prevede anche l’esecuzione di un blocco di codice per la configurazione del gruppo di nodi stesso.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/vagrant/compose/util/cluster.rb', line 39 def nodes(instances, name, &block) raise RuntimeError, "Nodes #{name} already exists in this cluster." unless not @node_groups.has_key?(name) @node_groups[name] = NodeGroup.new(@group_index, instances, name) @node_groups[name].box = @box @node_groups[name].boxname = lambda { |group_index, group_name, node_index| return "#{group_name}#{node_index + 1}" } @node_groups[name].hostname = lambda { |group_index, group_name, node_index| return "#{group_name}#{node_index + 1}" } @node_groups[name].aliases = [] @node_groups[name].ip = lambda { |group_index, group_name, node_index| return "172.31.#{group_index}.#{100 + node_index + 1}" } @node_groups[name].cpus = 1 @node_groups[name].memory = 256 @node_groups[name].ansible_groups = [] @node_groups[name].attributes = {} @group_index += 1 block.call(@node_groups[name]) if block_given? end |