lib/vagrant/compose/declarative/cluster.rb
# Implements cluster creation from a playbook file
Implements cluster creation from a playbook file
Implements cluster creation from a playbook file
def from (file)
          playbook = YAML.load(pycompose (file))
          @name                   = playbook.keys[0]
          @box                    = playbook[@name]['box']
          @domain                 = playbook[@name]['domain']
          @ansible_playbook_path  = playbook[@name]['ansible_playbook_path']
          nodes = []
          playbook[@name]['nodes'].each do |node|
            boxname = node.keys[0]
            box             = node[boxname]['box']
            hostname        = node[boxname]['hostname']
            aliases         = node[boxname]['aliases']
            fqdn            = node[boxname]['fqdn']
            ip              = node[boxname]['ip']
            cpus            = node[boxname]['cpus']
            memory          = node[boxname]['memory']
            ansible_groups  = node[boxname]['ansible_groups']
            attributes      = node[boxname]['attributes']
            index           = node[boxname]['index']
            group_index     = node[boxname]['group_index']
            nodes << VagrantPlugins::Compose::Node.new(box, boxname, hostname, fqdn, aliases, ip, cpus, memory, ansible_groups, attributes, index, group_index)
          end
          ansible_groups = {}
          if playbook[@name].key?("ansible")
            ansible = playbook[@name]['ansible']
            ansible_groups = ansible['inventory']
            ansible_group_vars_path = File.join(@ansible_playbook_path, 'group_vars')
            if 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")}
            end
            if ansible.key?("group_vars")
              ansible['group_vars'].each do |group, vars|
                unless vars.empty?
                  FileUtils.mkdir_p(ansible_group_vars_path) unless File.exists?(ansible_group_vars_path)
                  fileName = group.gsub(':', '_')
                  File.open(File.join(ansible_group_vars_path,"#{fileName}.yml") , 'w+') do |file|
                    file.puts YAML::dump(vars)
                  end
                end
              end
            end
            ansible_host_vars_path = File.join(@ansible_playbook_path, 'host_vars')
            if 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")}
            end
            if ansible.key?("host_vars")
              ansible['host_vars'].each do |host, vars|
                unless vars.empty?
                  FileUtils.mkdir_p(ansible_host_vars_path) unless File.exists?(ansible_host_vars_path)
                  File.open(File.join(ansible_host_vars_path,"#{host}.yml") , 'w+') do |file|
                    file.puts YAML::dump(vars)
                  end
                end
              end
            end
          end
          return nodes, ansible_groups 
        end
def from ( file ) playbook = YAML . load ( pycompose ( file ) ) @name = playbook . keys [ 0 ] @box = playbook [ @name ] [ 'box' ] @domain = playbook [ @name ] [ 'domain' ] @ansible_playbook_path = playbook [ @name ] [ 'ansible_playbook_path' ] nodes = [ ] playbook [ @name ] [ 'nodes' ] . each do | node | boxname = node . keys [ 0 ] box = node [ boxname ] [ 'box' ] hostname = node [ boxname ] [ 'hostname' ] aliases = node [ boxname ] [ 'aliases' ] fqdn = node [ boxname ] [ 'fqdn' ] ip = node [ boxname ] [ 'ip' ] cpus = node [ boxname ] [ 'cpus' ] memory = node [ boxname ] [ 'memory' ] ansible_groups = node [ boxname ] [ 'ansible_groups' ] attributes = node [ boxname ] [ 'attributes' ] index = node [ boxname ] [ 'index' ] group_index = node [ boxname ] [ 'group_index' ] nodes << VagrantPlugins :: Compose :: Node . new ( box , boxname , hostname , fqdn , aliases , ip , cpus , memory , ansible_groups , attributes , index , group_index ) end ansible_groups = { } if playbook [ @name ] . key? ( "ansible" ) ansible = playbook [ @name ] [ 'ansible' ] ansible_groups = ansible [ 'inventory' ] ansible_group_vars_path = File . join ( @ansible_playbook_path , 'group_vars' ) if 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" ) } end if ansible . key? ( "group_vars" ) ansible [ 'group_vars' ] . each do | group , vars | unless vars . empty? FileUtils . mkdir_p ( ansible_group_vars_path ) unless File . exists? ( ansible_group_vars_path ) fileName = group . gsub ( ':' , '_' ) File . open ( File . join ( ansible_group_vars_path , "#{fileName}.yml" ) , 'w+' ) do | file | file . puts YAML :: dump ( vars ) end end end end ansible_host_vars_path = File . join ( @ansible_playbook_path , 'host_vars' ) if 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" ) } end if ansible . key? ( "host_vars" ) ansible [ 'host_vars' ] . each do | host , vars | unless vars . empty? FileUtils . mkdir_p ( ansible_host_vars_path ) unless File . exists? ( ansible_host_vars_path ) File . open ( File . join ( ansible_host_vars_path , "#{host}.yml" ) , 'w+' ) do | file | file . puts YAML :: dump ( vars ) end end end end end return nodes , ansible_groups end
Implements cluster creation from a playbook file
Implements cluster creation from a playbook file
# calls vagrant-playbook utility for executing the playbook file. # extract cluster attributes # extract nodes # extract ansible inventory, ansible_group_vars, ansible_host_vars # extract ansible inventory # cleanup ansible_group_vars files # TODO: make safe #generazione ansible_group_vars file (NB. 1 group = 1 gruppo host ansible) # crea il file (se sono state generate delle variabili) # TODO: make safe # cleanup ansible_host_vars files (NB. 1 nodo = 1 host) # TODO: make safe #generazione ansible_host_vars file  # crea il file (se sono state generate delle variabili) # TODO: make safe
file
<not_specific>