From 22c367b3781476613c1b14a4d7260571ef74e7dd Mon Sep 17 00:00:00 2001
From: Lucas Nussbaum <lucas.nussbaum@loria.fr>
Date: Fri, 16 Nov 2018 23:12:57 +0100
Subject: [PATCH] [dev] migrate input/schema

---
 Rakefile                                      |  9 +-
 lib/refrepo/valid/input/schema.rb             | 56 +++++++++++
 .../input/{ => schemas}/schema-cluster.yaml   |  0
 .../input/{ => schemas}/schema-global.yaml    |  0
 .../schema-network_equipments.yaml            |  0
 .../input/{ => schemas}/schema-node.yaml      |  0
 .../input/{ => schemas}/schema-site.yaml      |  0
 .../input/yaml-input-schema-validator.rb      | 95 -------------------
 8 files changed, 63 insertions(+), 97 deletions(-)
 create mode 100644 lib/refrepo/valid/input/schema.rb
 rename lib/refrepo/valid/input/{ => schemas}/schema-cluster.yaml (100%)
 rename lib/refrepo/valid/input/{ => schemas}/schema-global.yaml (100%)
 rename lib/refrepo/valid/input/{ => schemas}/schema-network_equipments.yaml (100%)
 rename lib/refrepo/valid/input/{ => schemas}/schema-node.yaml (100%)
 rename lib/refrepo/valid/input/{ => schemas}/schema-site.yaml (100%)
 delete mode 100644 lib/refrepo/valid/input/yaml-input-schema-validator.rb

diff --git a/Rakefile b/Rakefile
index 9d7ea037d34..9cf5a3664fd 100644
--- a/Rakefile
+++ b/Rakefile
@@ -58,9 +58,14 @@ namespace :valid do
     exit(ret)
   end
 
-  desc "Check input data schema validity"
+  desc "Check input data schema validity -- parameters: SITE={grenoble,..} CLUSTER={yeti,..}"
   task "schema" do
-    invoke_script "#{VALIDATORS_DIR}/yaml-input-schema-validator.rb"
+    require 'refrepo/valid/input/schema'
+    options = {}
+    options[:sites] = ( ENV['SITE'] ? ENV['SITE'].split(',') : G5K_SITES )
+    options[:clusters] = ( ENV['CLUSTER'] ? ENV['CLUSTER'].split(',') : [] )
+    ret = yaml_input_schema_validator(options)
+    exit(ret)
   end
 
   desc "Check OAR properties -- parameters: [SITE={grenoble,...}] [CLUSTER={yeti,...}] [VERBOSE=1]"
diff --git a/lib/refrepo/valid/input/schema.rb b/lib/refrepo/valid/input/schema.rb
new file mode 100644
index 00000000000..ac866d3e1fd
--- /dev/null
+++ b/lib/refrepo/valid/input/schema.rb
@@ -0,0 +1,56 @@
+#!/usr/bin/ruby
+
+require 'fileutils'
+require 'pathname'
+
+require 'refrepo/input_loader'
+
+require_relative "./lib/schema_validator"
+
+def run_validator(uid, data, schema)
+  validator = HashValidator.validate(data, schema, strict = true)
+  if ! validator.valid?
+    errors = {uid => validator.errors}
+    puts errors.to_yaml
+    return false
+  end
+  return true
+end
+
+def yaml_input_schema_validator(options)
+  global_hash = load_yaml_file_hierarchy
+  sites = options[:sites]
+  clusters = options[:clusters]
+  schema_global  = load_yaml_schema(File.expand_path("./schemas/schema-global.yaml", File.dirname(__FILE__)))
+  schema_site    = load_yaml_schema(File.expand_path("./schemas/schema-site.yaml", File.dirname(__FILE__)))
+  schema_cluster = load_yaml_schema(File.expand_path("./schemas/schema-cluster.yaml", File.dirname(__FILE__)))
+  schema_node    = load_yaml_schema(File.expand_path("./schemas/schema-node.yaml", File.dirname(__FILE__)))
+  schema_network_equipments = load_yaml_schema(File.expand_path("./schemas/schema-network_equipments.yaml", File.dirname(__FILE__)))
+
+  r = true
+
+  r &= run_validator('global', global_hash, schema_global) #
+
+  global_hash["sites"].each do |site_uid, site|
+    next if sites and not sites.include?(site_uid)
+
+    r &= run_validator(site_uid, site, schema_site) #
+
+    site['networks'].each do |network_equipment_uid, network_equipment|
+      r &= run_validator(site_uid, network_equipment, schema_network_equipments)
+    end
+
+    site["clusters"].each do |cluster_uid, cluster|
+      next if clusters and not clusters.empty? and not clusters.include?(cluster_uid)      
+      
+      r &= run_validator(cluster_uid, cluster, schema_cluster) #
+
+      cluster["nodes"].each do |node_uid, node|
+        next if node == nil || node["status"] == "retired"
+
+        r &= run_validator(node_uid, node, schema_node) #
+      end
+    end
+  end
+  return r
+end
diff --git a/lib/refrepo/valid/input/schema-cluster.yaml b/lib/refrepo/valid/input/schemas/schema-cluster.yaml
similarity index 100%
rename from lib/refrepo/valid/input/schema-cluster.yaml
rename to lib/refrepo/valid/input/schemas/schema-cluster.yaml
diff --git a/lib/refrepo/valid/input/schema-global.yaml b/lib/refrepo/valid/input/schemas/schema-global.yaml
similarity index 100%
rename from lib/refrepo/valid/input/schema-global.yaml
rename to lib/refrepo/valid/input/schemas/schema-global.yaml
diff --git a/lib/refrepo/valid/input/schema-network_equipments.yaml b/lib/refrepo/valid/input/schemas/schema-network_equipments.yaml
similarity index 100%
rename from lib/refrepo/valid/input/schema-network_equipments.yaml
rename to lib/refrepo/valid/input/schemas/schema-network_equipments.yaml
diff --git a/lib/refrepo/valid/input/schema-node.yaml b/lib/refrepo/valid/input/schemas/schema-node.yaml
similarity index 100%
rename from lib/refrepo/valid/input/schema-node.yaml
rename to lib/refrepo/valid/input/schemas/schema-node.yaml
diff --git a/lib/refrepo/valid/input/schema-site.yaml b/lib/refrepo/valid/input/schemas/schema-site.yaml
similarity index 100%
rename from lib/refrepo/valid/input/schema-site.yaml
rename to lib/refrepo/valid/input/schemas/schema-site.yaml
diff --git a/lib/refrepo/valid/input/yaml-input-schema-validator.rb b/lib/refrepo/valid/input/yaml-input-schema-validator.rb
deleted file mode 100644
index 2a9a0c20ddd..00000000000
--- a/lib/refrepo/valid/input/yaml-input-schema-validator.rb
+++ /dev/null
@@ -1,95 +0,0 @@
-#!/usr/bin/ruby
-
-if RUBY_VERSION < "2.1"
-  puts "This script requires ruby >= 2.1"
-  exit
-end
-
-require 'fileutils'
-require 'pathname'
-
-$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '../../lib')))
-require 'refrepo/input_loader'
-
-require_relative "./lib/schema_validator"
-
-def run_validator(uid, data, schema)
-  validator = HashValidator.validate(data, schema, strict = true)
-  if ! validator.valid?
-    errors = {uid => validator.errors}
-    puts errors.to_yaml
-    return false
-  end
-  return true
-end
-
-def yaml_input_schema_validator(global_hash, sites = nil, clusters = nil)
-  schema_global  = load_yaml_schema(File.expand_path("./schema-global.yaml", File.dirname(__FILE__)))
-  schema_site    = load_yaml_schema(File.expand_path("./schema-site.yaml", File.dirname(__FILE__)))
-  schema_cluster = load_yaml_schema(File.expand_path("./schema-cluster.yaml", File.dirname(__FILE__)))
-  schema_node    = load_yaml_schema(File.expand_path("./schema-node.yaml", File.dirname(__FILE__)))
-  schema_network_equipments = load_yaml_schema(File.expand_path("./schema-network_equipments.yaml", File.dirname(__FILE__)))
-
-  r = true
-
-  r &= run_validator('global', global_hash, schema_global) #
-
-  global_hash["sites"].each do |site_uid, site|
-    next if sites and not sites.include?(site_uid)
-
-    r &= run_validator(site_uid, site, schema_site) #
-
-    site['networks'].each do |network_equipment_uid, network_equipment|
-      r &= run_validator(site_uid, network_equipment, schema_network_equipments)
-    end
-
-    site["clusters"].each do |cluster_uid, cluster|
-      next if clusters and not clusters.include?(cluster_uid)      
-      
-      r &= run_validator(cluster_uid, cluster, schema_cluster) #
-
-      cluster["nodes"].each do |node_uid, node|
-        next if node == nil || node["status"] == "retired"
-
-        r &= run_validator(node_uid, node, schema_node) #
-      end
-    end
-  end
-  return r
-end
-
-if __FILE__ == $0
-  require 'optparse'
-
-  options = {}
-
-  OptionParser.new do |opts|
-    opts.banner = "Usage: yaml-input-schema-validator.rb [options]"
-
-    ###
-
-    opts.separator ""
-    opts.separator "Filters:"
-
-    opts.on('-s', '--sites a,b,c', Array, 'Select site(s)',
-            "Default: all sites") do |s|
-      options[:sites] = s
-    end
-
-    opts.on('-c', '--clusters a,b,c', Array, 'Select clusters(s). Default: all') do |s|
-      options[:clusters] = s
-    end
-
-    # Print an options summary.
-    opts.on_tail("-h", "--help", "Show this message") do
-      puts opts
-      exit
-    end
-  end.parse!
-
-  puts "Checking input data:\n\n"
-  global_hash = load_yaml_file_hierarchy(File.expand_path("../../input/grid5000/", File.dirname(__FILE__)))
-  r = yaml_input_schema_validator(global_hash, options[:sites], options[:clusters])
-  puts 'OK' if r
-  exit r
-end
-- 
GitLab