From 434a28e80f12cfd9d7bef8073ad751ee30172269 Mon Sep 17 00:00:00 2001
From: Alexandre Marangone <amarango@redhat.com>
Date: Mon, 30 Oct 2017 09:57:29 -0700
Subject: [PATCH] mgr: refactor mgr module enablement

Cherry-picked from https://github.com/ceph/ceph-helm/commit/a6d6b2eef3a700efbcdc17dda099d1e38abc1ce0

Change-Id: Ie6b7e94e80d625c212331cf74c0f796bb3e99858
Signed-off-by: Alexandre Marangone <amarango@redhat.com>
Signed-off-by: Ganesh Maharaj Mahalingam <ganesh.mahalingam@intel.com>
---
 ceph/templates/bin/_start_mgr.sh.tpl          | 29 +++++++++++++++----
 .../bin/_variables_entrypoint.sh.tpl          |  2 --
 ceph/values.yaml                              | 23 +++++++++++++++
 3 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/ceph/templates/bin/_start_mgr.sh.tpl b/ceph/templates/bin/_start_mgr.sh.tpl
index dabf49fef7..998ee2dc64 100644
--- a/ceph/templates/bin/_start_mgr.sh.tpl
+++ b/ceph/templates/bin/_start_mgr.sh.tpl
@@ -33,11 +33,30 @@ log "SUCCESS"
 
 ceph -v
 
-if [[ "$MGR_DASHBOARD" == 1 ]]; then
-    ceph ${CLI_OPTS} mgr module enable dashboard --force
-    ceph ${CLI_OPTS} config-key put mgr/dashboard/server_addr "$MGR_IP"
-    ceph ${CLI_OPTS} config-key put mgr/dashboard/server_port "$MGR_PORT"
-fi
+# Env. variables matching the pattern "<module>_" will be
+# found and parsed for config-key settings by
+#  ceph config-key set mgr/<module>/<key> <value>
+MODULES_TO_DISABLE=`ceph mgr dump | python -c "import json, sys; print ' '.join(json.load(sys.stdin)['modules'])"`
+
+for module in ${ENABLED_MODULES}; do
+    # This module may have been enabled in the past
+    # remove it from the disable list if present
+    MODULES_TO_DISABLE=${MODULES_TO_DISABLE/$module/}
+
+    options=`env | grep ^${module}_ || true`
+    for option in ${options}; do
+        #strip module name
+        option=${option/${module}_/}
+        key=`echo $option | cut -d= -f1`
+        value=`echo $option | cut -d= -f2`
+        ceph ${CLI_OPTS} config-key set mgr/$module/$key $value
+    done
+    ceph ${CLI_OPTS} mgr module enable ${module} --force
+done
+
+for module in $MODULES_TO_DISABLE; do
+  ceph ${CLI_OPTS} mgr module disable ${module}
+done
 
 log "SUCCESS"
 # start ceph-mgr
diff --git a/ceph/templates/bin/_variables_entrypoint.sh.tpl b/ceph/templates/bin/_variables_entrypoint.sh.tpl
index d5bc4733ce..5d464c22a4 100644
--- a/ceph/templates/bin/_variables_entrypoint.sh.tpl
+++ b/ceph/templates/bin/_variables_entrypoint.sh.tpl
@@ -44,9 +44,7 @@ ALL_SCENARIOS="osd osd_directory osd_directory_single osd_ceph_disk osd_ceph_dis
 : ${RGW_REMOTE_CGI_HOST:=0.0.0.0}
 : ${RGW_USER:="cephnfs"}
 : ${MGR_NAME:=${HOSTNAME}}
-: ${MGR_DASHBOARD:=1}
 : ${MGR_IP:=0.0.0.0}
-: ${MGR_PORT:=7000}
 : ${RBD_POOL_PG:=128}
 
 # This is ONLY used for the CLI calls, e.g: ceph $CLI_OPTS health
diff --git a/ceph/values.yaml b/ceph/values.yaml
index b18a95434f..0d7cdf55ca 100644
--- a/ceph/values.yaml
+++ b/ceph/values.yaml
@@ -316,6 +316,29 @@ bootstrap:
     }
     #ensure_pool volumes 8 cinder
 
+# Uncomment below to enable mgr modules
+# For a list of available modules:
+#  http://docs.ceph.com/docs/master/mgr/
+# This overrides mgr_initial_modules (default: restful, status)
+# Any module not listed here will be disabled
+ceph_mgr_enabled_modules:
+  - restful
+  - status
+
+# You can configure your mgr modules
+# below. Each module has its own set
+# of key/value. Refer to the doc
+# above for more info. For example:
+#ceph_mgr_modules_config:
+#  dashboard:
+#    port: 7000
+#  localpool:
+#    failure_domain: host
+#    subtree: rack
+#    pg_num: "128"
+#    num_rep: "3"
+#    min_size: "2"
+
 # if you change provision_storage_class to false
 # it is presumed you manage your own storage
 # class definition externally