lib-oar-properties.rb 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/ruby

require 'pp'
require 'erb'
require 'fileutils'
require 'pathname'
require 'json'
require 'time'
require 'yaml'
require 'hashdiff'

class MissingProperty <  StandardError; end

MiB = 1024**2

# Get node properties from the reference repo hash
17
# See also: https://www.grid5000.fr/mediawiki/index.php/Reference_Repository
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
def get_node_properties(cluster_uid, cluster, node_uid, node)
  h = {} # ouput

  main_network_adapter = node['network_interfaces'].values.find{ |na| na['enabled'] && na['mounted'] && na['interface'] =~ /ethernet/i && !na['management'] }
  main_network_adapter = node['network_interfaces'].values.find{ |na| na['enabled'] && na['mounted'] }
  raise MissingProperty, "Node #{node_uid} does not have a main network_adapter" unless main_network_adapter

#  h['host']            = main_network_adapter['network_address']
#TODO  raise MissingProperty, "Node #{node_uid} has no network_address" unless h['host']

  h['ip']              = main_network_adapter['ip']
  raise MissingProperty, "Node #{node_uid} has no IP" unless h['ip']
  h['cluster']         = cluster_uid
  h['nodemodel']       = cluster['model']
  h['switch']          = main_network_adapter['switch']
  h['besteffort']      = node['supported_job_types']['besteffort']
  h['deploy']          = node['supported_job_types']['deploy']
  h['ip_virtual']      = node['supported_job_types']['virtual'] == 'ivt'
  h['virtual']         = node['supported_job_types']['virtual']
  h['cpuarch']         = node['architecture']['platform_type']
  h['cpucore']         = node['architecture']['smt_size']/node['architecture']['smp_size']
  h['cputype']         = [node['processor']['model'], node['processor']['version']].join(' ')
  h['cpufreq']         = node['processor']['clock_speed']/1_000_000_000.0
  h['disktype']        = (node['block_devices'].first[1] || {})['interface']
42
  h['ethnb']           = node['network_interfaces'].values.select{|na| na['interface'] =~ /ethernet/i}.select{|nb| nb['mounted'] == true || nb['mountable'] == true}.length
43

44
  eth10g               = node['network_interfaces'].values.select{|na| na['interface'] =~ /ethernet/i}.select{|nb| nb['mounted'] == true || nb['mountable'] == true}
45 46
  h['eth10g']          = eth10g.detect{|na| na['rate'] == 10_000_000_000}.nil? ? false : true

47
  ib10g                = node['network_interfaces'].values.detect{|na| na['interface'] =~ /infiniband/i && ( na['mounted'] == true || na['mountable'] == true ) && na['rate'] == 10_000_000_000}
48 49 50
  h['ib10g']           = ib10g ? true : false
  h['ib10gmodel']      = ib10g ? ib10g['version'] : 'none'

51
  ib20g                = node['network_interfaces'].values.detect{|na| na['interface'] =~ /infiniband/i && ( na['mounted'] == true || na['mountable'] == true ) && na['rate'] == 20_000_000_000}
52 53 54
  h['ib20g']           = ib20g ? true : false
  h['ib20gmodel']      = ib20g ? ib20g['version'] : 'none'

55
  ib40g                = node['network_interfaces'].values.detect{|na| na['interface'] =~ /infiniband/i && ( na['mounted'] == true || na['mountable'] == true ) && na['rate'] == 40_000_000_000}
56 57 58
  h['ib40g']           = ib40g ? true : false
  h['ib40gmodel']      = ib40g ? ib40g['version'] : 'none'

59
  ib56g                = node['network_interfaces'].values.detect{|na| na['interface'] =~ /infiniband/i && ( na['mounted'] == true || na['mountable'] == true ) && na['rate'] == 56_000_000_000}
60 61 62
  h['ib56g']           = ib56g ? true : false
  h['ib56gmodel']      = ib56g ? ib56g['version'] : 'none'

63
  myri10g              = node['network_interfaces'].values.detect{|na| na['interface'] =~ /myri/i && ( na['mounted'] == true || na['mountable'] == true ) && na['rate'] == 10_000_000_000}
64 65 66
  h['myri10g']         = myri10g ? true : false
  h['myri10gmodel']    = myri10g ? myri10g['version'] : 'none'

67
  myri2g               = node['network_interfaces'].values.detect{|na| na['interface'] =~ /myri/i && ( na['mounted'] == true || na['mountable'] == true ) && na['rate'] == 2_000_000_000}
68 69 70 71 72 73 74 75 76 77 78 79 80 81
  h['myri2g']          = myri2g ? true : false
  h['myri2gmodel']     = myri2g ? myri2g['version'] : 'none'

  h['memcore']         = node['main_memory']['ram_size']/node['architecture']['smt_size']/MiB
  h['memcpu']          = node['main_memory']['ram_size']/node['architecture']['smp_size']/MiB
  h['memnode']         = node['main_memory']['ram_size']/MiB

  node['gpu']  ||= {}
  h['gpu']             = case node['gpu']['gpu'] when true; true; when false; false when nil; false; else node['gpu']['gpu'].upcase end
  h['gpu_count']       = node['gpu']['gpu_count']
  h['gpu_model']       = node['gpu']['gpu_model']

  node['monitoring'] ||= {}

82
  h['wattmeter'] = case node['monitoring']['wattmeter'] when true; true; when false; false when nil; false; else node['monitoring']['wattmeter'].upcase end
83 84 85 86 87

  # h['rconsole'] = node['monitoring']['rconsole']

  h['cluster_priority'] = (cluster['priority'] || Time.parse(cluster['created_at'].to_s).strftime('%Y%m')).to_i
  
88 89 90 91 92
  h['production'] = false # default
  h['production'] = node['supported_job_types']['queues'].include?('production') if node['supported_job_types'] && node['supported_job_types'].has_key?('queues')

  h['max_walltime'] = 0 # default
  h['max_walltime'] = node['supported_job_types']['max_walltime'] if node['supported_job_types'] && node['supported_job_types'].has_key?('max_walltime')
93
  
94
  return h
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
end
  
#
#
#
def get_nodelist_properties(site_uid, site)
  properties = {} # output
  
  site['clusters'].each do |cluster_uid, cluster|

    cluster['nodes'].each do |node_uid, node|

      begin
        properties[node_uid] = get_node_properties(cluster_uid, cluster, node_uid, node)

      rescue MissingProperty => e
        # puts "Error while processing node #{node_uid}: #{e}"
      end

    end
  end

  return properties
end

def diff_node_properties(a, b)
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
  a ||= {}
  b ||= {}

  # default OAR at resource creation:
  #  available_upto: '2147483647'
  #  besteffort: 'YES'
  #  core: ~
  #  cpu: ~
  #  cpuset: 0
  #  deploy: 'NO'
  #  desktop_computing: 'NO'
  #  drain: 'NO'
  #  expiry_date: 0
  #  finaud_decision: 'YES'
  #  host: ~
  #  last_available_upto: 0
  #  last_job_date: 0
  #  network_address: server
  #  next_finaud_decision: 'NO'
  #  next_state: UnChanged
  #  resource_id: 9
  #  scheduler_priority: 0
  #  state: Suspected
  #  state_num: 3
  #  suspended_jobs: 'NO'
  #  type: default
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

  ignore_keys = [
                 "slash_16",
                 "slash_17",
                 "slash_18",
                 "slash_19",
                 "slash_20",
                 "slash_21",
                 "slash_22",
                 "available_upto",
                 "chunks",
                 "comment", # TODO
                 "core",
                 "cpu",
                 "cpuset",
                 "desktop_computing",
                 "drain",
                 "expiry_date",
                 "finaud_decision",
                 "grub",
                 "host", # TODO
168
                 "jobs", # This property exists when a job is running
169 170 171 172 173 174 175 176 177 178 179 180
                 "last_available_upto",
                 "last_job_date",
                 "maintenance",
                 "mic", # TODO
                 "network_address", # TODO
                 "next_finaud_decision",
                 "next_state",
                 "rconsole", # TODO
                 "resource_id",
                 "scheduler_priority",
                 "state",
                 "state_num",
181
                 "switch", # TODO
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
                 "subnet_address",
                 "subnet_prefix",
                 "suspended_jobs",
                 "thread",
                 "type", # TODO
                 "vlan",
                 "wattmeter" # TODO
                ]
  
  ignore_keys.each { |key| a.delete(key) }
  ignore_keys.each { |key| b.delete(key) }

  return HashDiff.diff(a, b)

end

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
def oarcmd_script_header()
  return <<EOF
set -eu

echo '================================================================================'

EOF
end

def oarcmd_create_node_header()
  return <<EOF
nodelist=$(oarnodes -l)

list_contains () { 
    [[ "$1" =~ (^|[[:space:]])"$2"($|[[:space:]]) ]] && return 0 || return 1
}

EOF

end

def oarcmd_create_node(host, properties, node_hash) # host = grifffon-1.nancy.grid5000.fr; properties, node_hash: input of the reference API for the node
  node_uid, site_uid, grid_uid = host.split(".")
  cluster_uid, node_number     = node_uid.split("-")

  command  = "echo; echo 'Adding host #{host}:'\n"
  command += 'list_contains "$nodelist" "' + host + '" && '
  command += "echo '=> host already exist'\n"
  command += 'list_contains "$nodelist" "' + host + '" || '
  command += "sudo oar_resources_add -a --hosts 1 --host0 #{node_number} --host-prefix #{cluster_uid}- --host-suffix .#{site_uid}.#{grid_uid}.fr --cpus #{node_hash['architecture']['smp_size']} --cores #{properties['cpucore']}"
  command += ' | sudo bash'
  
  return command + "\n"
end
232 233

def oarcmd_set_node_properties(host, properties)
234 235
  #return "# #{host}: OK" if properties.size == 0
  return "" if properties.size == 0
236

237 238
  command  = "echo; echo 'Setting properties for #{host}:'; echo\n"
  command += "sudo oarnodesetting -h #{host} -p "
239 240 241 242 243 244 245 246 247

  command +=
    properties.to_a.map{ |(k,v)|
    v = "YES" if v == true
    v = "NO"  if v == false
    
    ! v.nil? ? "#{k}=#{v.inspect.gsub("'", "\\'").gsub("\"", "'")}" : nil
  }.compact.join(' -p ')
  
248
  return command + "\n"
249
end
250 251 252 253
# '

# Get the OAR properties from the OAR scheduler
# This is only needed for the -d option
254
def oarcmd_get_nodelist_properties(site_uid, filename=nil, options)
255 256 257 258
  oarnodes_yaml = ""

  if filename and File.exist?(filename)
    # Read oar properties from file
259
    puts "Read 'oarnodes -Y' from #{filename}" if options[:verbose]
260 261 262
    oarnodes_yaml = File.open(filename, 'rb') { |f| f.read }
  else
    # Download the oar properties from the oar server
263
    puts "Downloading 'oarnodes -Y' from " + options[:ssh][:host].gsub("%s", site_uid) + "..." if options[:verbose]
264

265
    Net::SSH.start(options[:ssh][:host].gsub("%s", site_uid), options[:ssh][:user], options[:ssh][:params]) { |ssh|
266 267
      # capture all stderr and stdout output from a remote process
      oarnodes_yaml = ssh.exec!('oarnodes -Y')
268
    }
269
    puts "... done" if options[:verbose]
270

271 272
    if filename
      # Cache the file
273
      puts "Save 'oarnodes -Y' as #{filename}" if options[:verbose]
274 275 276 277 278 279 280 281
      File.write(filename, oarnodes_yaml)
    end
  end

  # Load the YAML file into an hashtable
  h = YAML.load(oarnodes_yaml)

  # Format convertion: use host as keys of the hash (instead of id)
282
  h = h.map {|k, v| v['type'] == 'default' ? [v['host'].split('.').first, v] : [nil, nil] }.to_h
283

284 285 286
  return h
end

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

def ssh_exec(site_uid, cmds, options)
  # The following is equivalent to : "cat cmds | bash"
  #res = ""
  c = Net::SSH.start(options[:ssh][:host].gsub("%s", site_uid), options[:ssh][:user], options[:ssh][:params])
  c.open_channel { |channel|
    channel.exec('bash') { |ch, success|
      channel.on_data { |ch, data|
        puts data #if options[:verbose] # ssh cmd output
      }
      
      cmds.each { |cmd| 
        channel.send_data cmd 
      }
      channel.eof!
    }
  }
  c.loop
end