oar-properties.rb 3.46 KB
Newer Older
1 2 3 4 5 6 7 8
#!/usr/bin/ruby

require 'pp'
require 'erb'
require 'fileutils'
require 'pathname'
require 'json'
require 'time'
9 10 11 12
require 'yaml'
require 'hashdiff'
require 'optparse'
require 'net/ssh'
13

14
require '../oar-properties/lib/lib-oar-properties'
15 16
require '../lib/input_loader'

17 18 19
options = {}
options[:sites] = %w{grenoble lille luxembourg lyon nantes reims rennes sophia}
options[:diff]  = false
20

21 22
OptionParser.new do |opts|
  opts.banner = "Usage: oar-properties.rb [options]"
23

24 25 26 27 28 29 30
  opts.separator ""
  opts.separator "Example: ruby oar-properties.rb -v -s nancy -d oarnodes-%s.yaml -o cmd-%s.sh"

  opts.separator ""
  opts.separator "Filters:"

  ###
31

32 33 34
  opts.on('-s', '--sites a,b,c', Array, 'Select site(s)',
                                        "Default: "+options[:sites].join(", ")) do |s|
    options[:sites] = s
35
  end
36 37 38

  opts.on('-c', '--clusters a,b,c', Array, 'Select clusters(s). Default: all') do |s|
    options[:clusters] = s
39 40
  end

41 42
  opts.on('-n', '--nodes a,b,c', Array, 'Select nodes(s). Default: all') do |n|
    options[:nodes] = n
43 44
  end

45
  ###
46

47 48
  opts.separator ""
  opts.separator "Output options:"
49

50 51 52
  opts.on('-o', '--output', 'Output oarnodesetting command into a file. Default: stdout') do |o|
    options[:output] = o
  end
53

54 55 56 57 58 59 60 61 62 63 64 65 66
  opts.on('-e', '--exec', 'Directly apply the changes to the OAR server') do |e|
    options[:exec] = e
  end
  
  opts.on("-d", "--diff [YAML filename]", 
          "Only generates the minimal list of commands needed to update the site configuration",
          "The optional YAML file is suppose to be the output of the 'oarnodes -Y' command.",
          "If the file does not exist, the script will get the data from the OAR server and save the result on disk for future use.",
          "If no filename is specified, the script will simply connect to the OAR server.",
          "You can use the '%s' placeholder for 'site'. Ex: oarnodes-%s.yaml") do |d|
    d = true if d == nil
    options[:diff] = d
  end
67
    
68 69 70 71 72 73 74 75 76 77 78
#  opts.on("-n", "--dry-run", "Perform a trial run with no changes made") do |n|
#    options[:dryrun] = n
#  end

  ###

  opts.separator ""
  opts.separator "Common options:"

  opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
    options[:verbose] = v
79
  end
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
  
  # Print an options summary.
  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
end.parse!

pp options

# Option
site_uid = 'nancy'

nodelist_properties = {}

#
# Get the OAR properties from the reference-repo
#

global_hash = load_yaml_file_hierarchy('../../input/grid5000/')
nodelist_properties["ref"] = get_nodelist_properties(site_uid, global_hash["sites"][site_uid])

#
# Get the current OAR properties from the OAR scheduler
# This is only needed for the -d option
#

nodelist_properties["oar"] = {}

if options[:diff]
  filename = options[:diff].is_a?(String) ? options[:diff].gsub("%s", site_uid) : nil
  nodelist_properties["oar"] = oarcmd_get_nodelist_properties(site_uid, filename)
112
end
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132


#
# Diff
#

node_properties = {}
node_properties["ref"] = nodelist_properties["ref"]["graphene-1"]
node_properties["oar"] = nodelist_properties["oar"]["graphene-1"]

diff      = diff_node_properties(node_properties["ref"], node_properties["oar"])
diff_keys = diff.map{ |hashdiff_array| hashdiff_array[1] }

node_properties["to_be_updated"] = node_properties["ref"].select { |key, value| diff_keys.include?(key) }

#
# Example
#
puts oarcmd_set_node_properties("graphene-1", node_properties["oar"])
puts oarcmd_set_node_properties("graphene-1", node_properties["to_be_updated"])