From 0e3a1aa747dde4d9089b2257d28bfa4e16b2f3d3 Mon Sep 17 00:00:00 2001
From: aviau <alexandre.viau@savoirfairelinux.com>
Date: Mon, 22 Jun 2015 12:10:21 -0400
Subject: [PATCH] surveil-init: Added help text and more options

Change-Id: I305e2af73f7940c4b3ffeda7ad4cb92ae0e0eae1
---
 Dockerfile                                  |  2 +-
 doc/source/tutorials/installing_surveil.rst |  4 +--
 docker-compose.yml                          |  2 +-
 surveil/cmd/init.py                         | 28 +++++++++++++--------
 4 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index 0fb9aaf..0145fcc 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -51,5 +51,5 @@ ENV SURVEIL_OS_TENANT_NAME=admin
 
 CMD cd /opt/surveil && \
     ./setup.sh && \
-    ((sleep 40 && surveil-init) &) && \
+    ((sleep 40 && surveil-init --influxdb --packs --mongodb) &) && \
     surveil-api
diff --git a/doc/source/tutorials/installing_surveil.rst b/doc/source/tutorials/installing_surveil.rst
index 79b6b0a..732c239 100644
--- a/doc/source/tutorials/installing_surveil.rst
+++ b/doc/source/tutorials/installing_surveil.rst
@@ -40,9 +40,9 @@ Launch all surveil services with the following command: ::
     systemctl start surveil-full.target
 
 
-The surveil-init command will create a database in InfluxDB and initialize the MongoDB database: ::
+The surveil-init command will flush existing MongoDB Alignak config, create an InfluxDB database and upload configuration templates to Alignak: ::
 
-    surveil-init --influxdb
+    surveil-init --mongodb --influxdb --packs
 
 The surveil-webui-init command will pre-create data sources in Grafana: ::
 
diff --git a/docker-compose.yml b/docker-compose.yml
index 3a1392a..a1f9a4f 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -10,7 +10,7 @@ surveil:
     PBR_VERSION: "DEV"
   volumes:
    - ./surveil:/opt/surveil/surveil
-  command: bash -c "cd /opt/surveil && ./setup.sh && /opt/surveil/env/bin/python setup.py develop && ((sleep 40 && surveil-init --demo) &) && sleep 20 && surveil-api --reload"
+  command: bash -c "cd /opt/surveil && ./setup.sh && /opt/surveil/env/bin/python setup.py develop && ((sleep 40 && surveil-init --influxdb --packs --mongodb --demo) &) && sleep 20 && surveil-api --reload"
 #  privileged: true # Fedora users might want to uncomment this if they face permission issues
 
 alignak:
diff --git a/surveil/cmd/init.py b/surveil/cmd/init.py
index 39068a7..6a35b0d 100644
--- a/surveil/cmd/init.py
+++ b/surveil/cmd/init.py
@@ -30,21 +30,35 @@ def main():
     parser.add_option('-d', '--demo',
                       default=False,
                       dest='demo',
+                      help="Create fake hosts in Alignak",
                       action="store_true")
     parser.add_option('-i', '--influxdb',
                       default=False,
                       dest='influxdb',
+                      help="Pre-create the InfluxDB database",
+                      action='store_true')
+    parser.add_option('-m', '--mongodb',
+                      default=False,
+                      dest='mongodb',
+                      help="Drop the existing Alignak MongoDB database",
+                      action='store_true')
+    parser.add_option('-p', '--packs',
+                      default=False,
+                      dest='packs',
+                      help="Upload/Update configuration packs to MongoDB",
                       action='store_true')
     opts, _ = parser.parse_args(sys.argv)
 
     # Create a basic config in mongodb
     mongo = pymongo.MongoClient(config.surveil_api_config['mongodb_uri'])
 
-    if opts.demo is True:
+    if opts.mongodb is True:
         # Drop the current shinken config
+        print("Dropping existing Alignak MongoDB database...")
         mongo.drop_database('shinken')
 
     if opts.influxdb is True:
+        print("Pre-creating InfluxDB database...")
         # Create the InfluxDB database
         influx_client = influxdb.InfluxDBClient.from_DSN(
             config.surveil_api_config['influxdb_uri']
@@ -54,11 +68,8 @@ def main():
         if not any(db['name'] == influx_client._database for db in databases):
             influx_client.create_database(influx_client._database)
 
-    if mongo.surveil.init.count() == 0:
-        # Mark packs as uploaded
-        print("Uploading packs...")
-        mongo.surveil.init.insert({"source": "surveil-init script"})
-
+    if opts.packs:
+        print ("Uploading packs...")
         # Load the shinken packs
         subprocess.call(
             [
@@ -108,16 +119,13 @@ def main():
             ]
         )
 
-    else:
-        print("Skipping pack upload...")
-
     cli_surveil = sc.Client('http://localhost:8080/v2',
                             auth_url='http://localhost:8080/v2/auth',
                             version='2_0')
 
     # if --demo is specified, you get more hosts.
     if opts.demo is True:
-
+        print("Creating demo configuration...")
         # shinken's ws-arbiter
         cli_surveil.config.hosts.create(
             use="generic-host",