kadeployg5k.rb 4.44 KB
Newer Older
1 2
#!/usr/bin/ruby

3
# This script generates:
4
# - kadeployg5k/files/<site_uid>/server_conf[_dev]/clusters.conf from input/
5 6
# - kadeployg5k/files/<site_uid>/server_conf[_dev]/<cluster_uid>-cluster.conf from conf/kadeployg5k.yaml and template/kadeployg5k.yaml.Erb

7 8
require 'pp'
require 'erb'
9
require 'pathname'
10 11 12 13
require '../lib/input_loader'
require '../lib/hash/hash.rb'

global_hash = load_yaml_file_hierarchy("../input/grid5000/")
14
$output_dir = ENV['puppet_repo'] || 'output'
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

# Compute cluster prefix
# input: cluster_list = ['graoully', 'graphene', 'griffon', ...]
# output: prefix_hash = {'grao' => 'graoully', 'graphe' => 'graphene', ...}
def cluster_prefix(cluster_list)

  # Shrink cluster names. Start with 3 characters
  # prefix_hash = {'gra' => ['graoully', 'graphene', ...], 'gri' => ['griffon']}
  prefix_hash = cluster_list.group_by { |x| x[0, 3] } 

  # Add characters until each prefix is unique
  loop do
    prefix_hash.clone.each { |k, v|
      next if v.size == 1
      prefix_hash.merge!(v.group_by { |x| x[0, k.length+1] })
      prefix_hash.delete(k)
    }
    break if prefix_hash.keys.size == cluster_list.size # no prefix duplicates
  end

  # Inverse key <=> value
  prefix_hash = Hash[prefix_hash.map {|k, v| [v[0], k] }]

end

40
# Extract the node ip from the node hash
41 42 43 44 45 46 47 48
def get_ip(node)
  node['network_adapters'].each { |device, network_adapter|
    if network_adapter['mounted'] && /^eth[0-9]$/.match(device)
      return network_adapter['ip']
    end
  }
end

49
# There is two kadeploy servers : kadeploy and kadeploy-dev
50
['', '-dev'].each {|suffix|
51

52 53
  global_hash['sites'].each { |site_uid, site|

54 55 56 57
    #
    # Generate site/<site_uid>/servers_conf[_dev]/clusters.conf
    #
    
58 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
    clusters_conf = { 'clusters'=> [] } # output clusters.conf
    prefix = cluster_prefix(site['clusters'].keys)

    site['clusters'].sort.each { |cluster_uid, cluster|

      # clusters:
      # - name: griffon
      #   prefix: gri
      #   conf_file: /etc/kadeploy3/griffon-cluster.conf
      #   nodes:
      #   - name: griffon-[1-92].nancy.grid5000.fr
      #     address: 172.16.65.[1-92]

      cluster_conf = {}
      cluster_conf['name']      = cluster_uid
      cluster_conf['prefix']    = prefix[cluster_uid]
      cluster_conf['conf_file'] = "/etc/kadeploy3#{suffix}/#{cluster_uid}-cluster.conf"
      cluster_conf['nodes']     = []

      # init
      first = last = c_uid = -1
      first_ip = ['0', '0', '0', 0]

      # group nodes by range (griffon-[1-92] -> 172.16.65.[1-92])
      cluster['nodes'].each_sort_by_node_uid { |node_uid, node|
        c, id = node_uid.scan(/^([^\d]*)(\d*)$/).first
        id = id.to_i
        ip = get_ip(node).split('.')
        ip[3] = ip[3].to_i

        if c == c_uid && id == last + 1 && ip[0..2] == first_ip[0..2] && ip[3] == first_ip[3] + id - first
          # extend range
          last = id
        else
          if c_uid != -1
            node = {}
            node['name']    = "#{c_uid}[#{first}-#{last}].#{site_uid}.grid5000.fr"
            node['address'] = "#{first_ip[0..2].join('.')}.[#{first_ip[3]}-#{first_ip[3]+last-first}]"
            cluster_conf['nodes'] << node
          end

          # new range
          first = last = id
          first_ip = ip
          c_uid = c
        end
      }
      # last range
      if c_uid != -1
        node = {}
        node['name']    = "#{c_uid}[#{first}-#{last}].#{site_uid}.grid5000.fr"
        node['address'] = "#{first_ip[0..2].join('.')}.[#{first_ip[3]}-#{first_ip[3]+last-first}]"
        cluster_conf['nodes'] << node
      end

      clusters_conf['clusters'] << cluster_conf

    } # site['clusters'].each

117 118
    output_file = Pathname("#{$output_dir}/modules/kadeployg5k/files/#{site_uid}/server_conf#{suffix.tr('-', '_')}/clusters.conf")
    output_file.dirname.mkpath()
119
    write_yaml(output_file, clusters_conf)
120
    
121 122 123 124 125 126
    #
    # Generate <cluster_uid>-cluster.conf files
    #

    # Load 'conf/kadeployg5k.yaml' data and fill up the kadeployg5k.conf.erb template for each cluster
    
127
    conf = YAML::load(ERB.new(File.read("./conf/kadeployg5k#{suffix}.yaml")).result(binding))
128 129

    site['clusters'].each { |cluster_uid, cluster|
130

131
      data = data = conf[site_uid][cluster_uid]
132
      output = ERB.new(File.read('templates/kadeployg5k.conf.erb')).result(binding)
133

134 135 136
      output_file = Pathname("#{$output_dir}/modules/kadeployg5k/files/#{site_uid}/server_conf#{suffix.tr('-', '_')}/#{cluster_uid}-cluster.conf")
      output_file.dirname.mkpath()
      File.write(output_file, output)
137 138 139 140 141
      
    }
    
  }
}