|
| 1 | +#!/usr/bin/ruby |
| 2 | + |
| 3 | +# This script is used to script manual changes over nodes yaml files |
| 4 | +# It copy files from input/ for a specific site and cluster and apply custom modifications and copy back transformed files to input/ |
| 5 | + |
| 6 | +require 'optparse' |
| 7 | +require 'pp' |
| 8 | +require 'erb' |
| 9 | +require 'fileutils' |
| 10 | +require 'pathname' |
| 11 | +require 'yaml' |
| 12 | +require '../lib/hash/hash' |
| 13 | + |
| 14 | +if RUBY_VERSION < "2.1" |
| 15 | + puts "This script requires ruby >= 2.1" |
| 16 | + exit |
| 17 | +end |
| 18 | + |
| 19 | +puts 'Manual postprocessing of existing yaml files.' |
| 20 | + |
| 21 | +#TODO use non-relative file paths everywhere... |
| 22 | + |
| 23 | +options = { |
| 24 | + :site => "", |
| 25 | + :clusters => [], |
| 26 | + :rm_output => false |
| 27 | +} |
| 28 | + |
| 29 | +OptionParser.new do |opts| |
| 30 | + opts.banner = "Usage: [options]" |
| 31 | + |
| 32 | + opts.separator "" |
| 33 | + |
| 34 | + opts.separator "Filters:" |
| 35 | + |
| 36 | + opts.on('-s', '--site a', String, 'Select site(s)') do |s| |
| 37 | + options[:site] = s |
| 38 | + end |
| 39 | + |
| 40 | + opts.on('-c', '--clusters a,b,c', Array, 'Select clusters(s). Default: none') do |s| |
| 41 | + options[:clusters] = s |
| 42 | + end |
| 43 | + |
| 44 | + opts.on('--clean', 'Remove content from output/ after execution') do |s| |
| 45 | + options[:rm_output] = true |
| 46 | + end |
| 47 | + |
| 48 | +end.parse! |
| 49 | + |
| 50 | +if options[:site].empty? || options[:clusters].empty? |
| 51 | + puts "Not copying anything, post-processing file from 'output'" |
| 52 | +else |
| 53 | + options[:clusters].each{ |cluster_uid| |
| 54 | + node_files = Dir["../../input/grid5000/sites/#{options[:site]}/clusters/#{cluster_uid}/nodes/*.yaml"] |
| 55 | + node_files.each{|f| |
| 56 | + filename = [f.split("/").last.split(".").first, options[:site], "grid5000", "fr", "yaml"].join(".") |
| 57 | + puts "Copying #{filename} to output/" |
| 58 | + FileUtils.cp(f, "./output/#{filename}") |
| 59 | + } |
| 60 | + } |
| 61 | +end |
| 62 | + |
| 63 | +##Modify node yaml hash here |
| 64 | +def modify_node_data(hash, node_uid, cluster_uid, site_uid) |
| 65 | + |
| 66 | + #example content: |
| 67 | + hash["network_adapters"].each{ |na_id, na| |
| 68 | + if (na_id == "ib0" && na["firmware_version"] != "2.7.700") |
| 69 | + puts "#{node_uid}" + na.inspect |
| 70 | + na["firmware_version"] = "2.7.700" |
| 71 | + # na["mac"] = "80:00:02:0a:fe:" + na["mac"].split(":")[5..20].join(":") |
| 72 | + end |
| 73 | + } |
| 74 | + hash |
| 75 | +end |
| 76 | + |
| 77 | +list_of_yaml_files = Dir['output/*.y*ml'].sort_by { |x| -x.count('/') } |
| 78 | +list_of_yaml_files.each { |filename| |
| 79 | + begin |
| 80 | + file = filename.split("/")[1] |
| 81 | + node_uid = file.split(".")[0] |
| 82 | + site_uid = file.split(".")[1] |
| 83 | + cluster_uid = node_uid.split("-")[0] |
| 84 | + |
| 85 | + hash = YAML::load_file(filename) |
| 86 | + if hash == false |
| 87 | + puts "Unable to load YAML file #{filename}" |
| 88 | + next |
| 89 | + end |
| 90 | + |
| 91 | + hash = hash[node_uid] |
| 92 | + |
| 93 | + # Adapt content of modify_node_data |
| 94 | + hash = modify_node_data(hash, node_uid, cluster_uid, site_uid) |
| 95 | + # |
| 96 | + |
| 97 | + hash = {node_uid => hash} |
| 98 | + |
| 99 | + new_filename = Pathname("../../input/grid5000/sites/#{site_uid}/clusters/#{cluster_uid}/nodes/" + node_uid + ".yaml") |
| 100 | + |
| 101 | + new_filename.dirname.mkpath() |
| 102 | + |
| 103 | + write_yaml(new_filename, hash) |
| 104 | + |
| 105 | + contents = File.read(new_filename) |
| 106 | + File.open(new_filename, 'w') { |file| |
| 107 | + file.write("# Generated by g5k-checks (g5k-checks -m api)\n") |
| 108 | + file.write(contents) |
| 109 | + } |
| 110 | + |
| 111 | + rescue Exception => e |
| 112 | + puts "#{node_uid} - #{e.class}: #{e.message}" |
| 113 | + puts e.backtrace |
| 114 | + end |
| 115 | +} |
| 116 | + |
| 117 | +if options[:rm_output] |
| 118 | + puts "Cleaning up output directory" |
| 119 | + FileUtils.rm Dir.glob("./output/*") |
| 120 | +end |
0 commit comments