From e61cb295ff2d5c73f2630f179be8e78198ccb0ee Mon Sep 17 00:00:00 2001
From: Alexandre MERLIN <alexandre.merlin@inria.fr>
Date: Wed, 6 Apr 2022 15:51:25 +0200
Subject: [PATCH] [gen] add environments wiki generator

---
 lib/refrepo/gen/wiki.rb                       |  5 +++
 .../gen/wiki/generators/environments.rb       | 43 +++++++++++++++++++
 2 files changed, 48 insertions(+)
 create mode 100644 lib/refrepo/gen/wiki/generators/environments.rb

diff --git a/lib/refrepo/gen/wiki.rb b/lib/refrepo/gen/wiki.rb
index 03582c6f63..ed15811bb9 100644
--- a/lib/refrepo/gen/wiki.rb
+++ b/lib/refrepo/gen/wiki.rb
@@ -8,6 +8,7 @@ require 'refrepo/gen/wiki/generators/site_hardware'
 require 'refrepo/gen/wiki/generators/site_network'
 require 'refrepo/gen/wiki/generators/status'
 require 'refrepo/gen/wiki/generators/group_storage'
+require 'refrepo/gen/wiki/generators/environments'
 require 'refrepo/gen/wiki/generators/kwollect_metrics'
 require 'refrepo/gen/wiki/generators/oarsub_simplifier_aliases'
 
@@ -38,6 +39,10 @@ module RefRepo::Gen::Wiki
       :gen => GroupStorageGenerator,
       :page => 'Generated/GroupStorage'
     },
+    'environments' => {
+      :gen => EnvironmentsGenerator,
+      :page => 'Generated/Environments'
+    },
     'kwollect_metrics' => {
       :gen => KwollectMetricsGenerator,
       :page => 'Generated/KwollectMetrics'
diff --git a/lib/refrepo/gen/wiki/generators/environments.rb b/lib/refrepo/gen/wiki/generators/environments.rb
new file mode 100644
index 0000000000..ee103ee11a
--- /dev/null
+++ b/lib/refrepo/gen/wiki/generators/environments.rb
@@ -0,0 +1,43 @@
+# coding: utf-8
+
+class EnvironmentsGenerator < WikiGenerator
+
+  OSVERSION_SORT_ORDER = ['debian9','debian10','debian11','debiantesting','centos7','centos8','rocky8','centosstream8','ubuntu1804','ubuntu2004']
+  VARIANT_SORT_ORDER = ['min','base', 'xen', 'nfs', 'big', 'std']
+
+  def initialize(page_name)
+    super(page_name)
+  end
+
+  def generate_content(_options)
+
+    table_columns = ["Name"]
+    table_data = []
+    envs = []
+
+    G5K::SITES.each do |site|
+      envs += RefRepo::Utils::get_api("sites/#{site}/internal/kadeployapi/environments?username=deploy&last")
+    end
+    envs.select!{|x| x['visibility'] == 'public'}
+    # The creation date can be a little different on each site, we remove it from hash before removing dupplicate
+    envs.each{|x| x.delete('created_at')}
+    envs.uniq!
+    table_columns += envs.map{|x| x['arch']}.uniq.sort
+    envs = envs.group_by{|x| x['name']}.map{|k,v| [k,v.map{|x| x['arch']}]}
+    envs.each do |env|
+      tarch = table_columns[1..3].map{|l| env[1].include?(l) ? '[[Image:Check.png]]' : '[[Image:NoStarted.png]]' }
+      table_data << [env[0]] + tarch
+    end
+
+    # Sort by OS version and variant
+    table_data.sort_by! { |row|
+      [OSVERSION_SORT_ORDER.index(row[0].split('-')[0]) || 100, VARIANT_SORT_ORDER.index(row[0].split('-')[1]) || 100]
+    }
+
+    # Table construction
+    table_options = 'class="wikitable sortable" style="text-align: center;"'
+    @generated_content = MW.generate_table(table_options, table_columns, table_data)
+    @generated_content += MW.italic(MW.small(generated_date_string))
+    @generated_content += MW::LINE_FEED
+  end
+end
-- 
GitLab