From 96ff0e452f4f5319485178c32c788620e9218d0f Mon Sep 17 00:00:00 2001
From: Alain Takoudjou <alain.takoudjou@nexedi.com>
Date: Sat, 1 Aug 2015 16:54:11 +0200
Subject: [PATCH] kvm: add monitor stack

---
 software/kvm/common.cfg                       |  9 ++--
 .../instance-kvm-cluster-input-schema.json    |  6 +++
 .../kvm/instance-kvm-cluster.cfg.jinja2.in    |  1 +
 software/kvm/instance-kvm-input-schema.json   |  6 +++
 software/kvm/instance-kvm.cfg.jinja2          | 50 +++++++++++++++++--
 software/kvm/instance.cfg.in                  |  2 +
 6 files changed, 68 insertions(+), 6 deletions(-)

diff --git a/software/kvm/common.cfg b/software/kvm/common.cfg
index 0c695b965..13dbdfb70 100644
--- a/software/kvm/common.cfg
+++ b/software/kvm/common.cfg
@@ -12,9 +12,11 @@ extends =
   ../../component/openssl/buildout.cfg
   ../../component/dcron/buildout.cfg
   ../../component/netcat/buildout.cfg
+  ../../component/pycurl/buildout.cfg
   ../../stack/slapos.cfg
   ../../stack/nodejs.cfg
   ../../stack/resilient/buildout.cfg
+  ../../stack/monitor/buildout.cfg
 
 # stacks are listed from most generic to most specific,
 # to avoid versioning issues
@@ -51,6 +53,7 @@ eggs =
   cns.recipe.symlink
   collective.recipe.template
   plone.recipe.command
+  ${pycurl:egg}
 
 [http-proxy]
 # https://github.com/nodejitsu/node-http-proxy
@@ -87,7 +90,7 @@ command =
 [template]
 recipe = slapos.recipe.template
 url = ${:_profile_base_location_}/instance.cfg.in
-md5sum = 98116c0ccac67ca97e5bd1ce2b9008a2
+md5sum = 41eb12b66bd1af86b1b98b2166e86417
 output = ${buildout:directory}/template.cfg
 mode = 0644
 
@@ -95,7 +98,7 @@ mode = 0644
 recipe = hexagonit.recipe.download
 url = ${:_profile_base_location_}/instance-kvm.cfg.jinja2
 mode = 644
-md5sum = d1598672b2957cf89b63c86c754c7afb
+md5sum = 6e4f3819c4cdaf27896f3762a53a3f18
 download-only = true
 on-update = true
 
@@ -103,7 +106,7 @@ on-update = true
 recipe = hexagonit.recipe.download
 url = ${:_profile_base_location_}/instance-kvm-cluster.cfg.jinja2.in
 mode = 644
-md5sum = 5a864099760e3a37fa4604044d708657
+md5sum = 6ac9d1b06398b57cc1c8d63a9c492415
 download-only = true
 on-update = true
 
diff --git a/software/kvm/instance-kvm-cluster-input-schema.json b/software/kvm/instance-kvm-cluster-input-schema.json
index 25e2d8197..f858d9e4f 100644
--- a/software/kvm/instance-kvm-cluster-input-schema.json
+++ b/software/kvm/instance-kvm-cluster-input-schema.json
@@ -294,6 +294,12 @@
               "title": "Text content to send to this virtual machine.",
               "description": "Text content which will be written in a file 'data' of http server of this virtual machine instance. The file will be available via URL: http://10.0.2.100/data in the VM.",
               "type": "string"
+            },
+            "enable-monitor": {
+              "title": "Deploy monitoring tools",
+              "description": "Deploy monitor instance to this kvm instance. It help to check instance status, log and promise results.",
+              "type": "boolean",
+              "default": true
             }
           },
           "type": "object"
diff --git a/software/kvm/instance-kvm-cluster.cfg.jinja2.in b/software/kvm/instance-kvm-cluster.cfg.jinja2.in
index e521b82b9..5eb399d70 100644
--- a/software/kvm/instance-kvm-cluster.cfg.jinja2.in
+++ b/software/kvm/instance-kvm-cluster.cfg.jinja2.in
@@ -64,6 +64,7 @@ config-httpd-port = {{ dumps(kvm_parameter_dict.get('httpd-port', 8081)) }}
 {% if kvm_parameter_dict.get('data-to-vm', '') -%}
 config-data-to-vm = {{ dumps(kvm_parameter_dict.get('data-to-vm', '')) }}
 {% endif -%}
+config-enable-monitor = True
 
 # Enable simple http server on ipv6 so all VMs will access it
 config-document-host = ${apache-conf:ip}
diff --git a/software/kvm/instance-kvm-input-schema.json b/software/kvm/instance-kvm-input-schema.json
index abc78e17e..8233cc41b 100644
--- a/software/kvm/instance-kvm-input-schema.json
+++ b/software/kvm/instance-kvm-input-schema.json
@@ -135,6 +135,12 @@
       "description": "List of rules for NAT of QEMU user mode network stack, as comma-separated list of ports. For each port specified, it will redirect port x of the VM (example: 80) to the port x + 10000 of the public IPv6 (example: 10080). Defaults to \"22 80 443\". Ignored if \"use-tap\" parameter is enabled.",
       "type": "string"
     },
+    "enable-monitor": {
+      "title": "Deploy monitoring tools",
+      "description": "Deploy monitor instance to this kvm instance. It help to check instance status, log and promise results.",
+      "type": "boolean",
+      "default": true
+    },
     "enable-http-server": {
       "title": "Enable local http server",
       "description": "Set if local http server which serve files to the vm should be deployed. If set to true, get file into the vm with URL: http://10.0.2.100/FILE.",
diff --git a/software/kvm/instance-kvm.cfg.jinja2 b/software/kvm/instance-kvm.cfg.jinja2
index 3e5d6698c..7bfebf5a3 100644
--- a/software/kvm/instance-kvm.cfg.jinja2
+++ b/software/kvm/instance-kvm.cfg.jinja2
@@ -2,6 +2,13 @@
 {% set use_tap = slapparameter_dict.get('use-tap', 'False').lower() -%}
 {% set use_nat = slapparameter_dict.get('use-nat', 'True').lower() -%}
 {% set name = slapparameter_dict.get('name', 'localhost') -%}
+{% set monitor = slapparameter_dict.get('enable-monitor', 'True').lower()  -%}
+{% set extends_list = [] -%}
+
+{% if monitor -%}
+{% do extends_list.append(template_monitor) -%}
+{% endif -%}
+{% do extends_list.append(logrotate_cfg) -%}
 #############################
 #
 # Instanciate kvm
@@ -19,6 +26,24 @@ parts =
   cron
 #  cron-entry-monitor
   frontend-promise
+{% if monitor -%}
+# monitor parts
+  cron-entry-monitor
+  cron-entry-rss
+  deploy-index
+  deploy-status-history-cgi
+  deploy-status-cgi
+#  deploy-logfile-cgi
+#  deploy-resource-consumption-monitoring-cgi
+  setup-static-files
+  public-symlink
+  cgi-httpd-wrapper
+  cgi-httpd-graceful-wrapper
+  monitor-promise
+  monitor-instance-log-access
+  monitor-access-log
+  monitor-access-public
+{% endif -%}
 {% if slapparameter_dict.get('document-host', '') %}
   cluster-url-path
 {% endif -%}
@@ -36,11 +61,13 @@ parts =
 {% if slapparameter_dict.get('authorized-key', '') %}
   get-authorized-key
 {%   endif -%}
-  
+
+{% endif -%}
+
 extends = 
-  {{ logrotate_cfg }}
+#  Add extends list
+ {{ extends_list | join('\n  ') }}
 #  {{ template_httpd_cfg }}
-{% endif -%}
 
 eggs-directory = {{ eggs_directory }}
 develop-eggs-directory = {{ develop_eggs_directory }}
@@ -295,6 +322,20 @@ hostname = ${httpd:host}
 port = ${httpd:port}
 {% endif %}
 
+{% if monitor == 'true' -%}
+[monitor-access-log]
+< = monitor-directory-access
+source = ${directory:log}
+
+[monitor-access-public]
+< = monitor-directory-access
+source = ${directory:public}
+
+[monitor-parameters]
+port = 8026
+
+{% endif -%}
+
 [publish-connection-information]
 recipe = slapos.cookbook:publish
 ipv6 = ${slap-network-information:global-ipv6}
@@ -329,6 +370,9 @@ tap-ipv4 = ${slap-network-information:tap-ipv4}
 7_info = Get the publick key file in your VM with the command: wget {{ kvm_http }}/authorized_keys
 {%   endif %}
 {% endif %}
+{% if monitor -%}
+monitor_url = ${monitor-parameters:url}
+{% endif -%}
 
 {% if use_tap == 'true' and tap_network_dict.has_key('ipv4') -%}
 1_info = Use these configurations below to configure interface {{ iface }} in your VM.
diff --git a/software/kvm/instance.cfg.in b/software/kvm/instance.cfg.in
index 25fdf0983..2cb24b6fc 100644
--- a/software/kvm/instance.cfg.in
+++ b/software/kvm/instance.cfg.in
@@ -69,6 +69,7 @@ extra-context =
   raw logrotate_cfg ${template-logrotate-base:rendered}
   raw template_content ${template-content:location}/${template-content:filename}
   raw template_httpd_cfg ${template-httpd:rendered}
+  raw template_monitor ${monitor-template:output}
 
 [dynamic-template-kvm]
 recipe = slapos.recipe.template:jinja2
@@ -96,6 +97,7 @@ context =
     raw sixtunnel_executable_location ${6tunnel:location}/bin/6tunnel
     raw template_httpd_cfg ${template-httpd:rendered}
     raw template_content ${template-content:location}/${template-content:filename}
+    raw template_monitor ${monitor-template:output}
     raw websockify_executable_location ${buildout:directory}/bin/websockify
 template-parts-destination = ${template-parts:destination}
 template-replicated-destination = ${template-replicated:destination}
-- 
2.30.9