aboutsummaryrefslogtreecommitdiffstats
path: root/app/install/calipso-installer.py
blob: bccddaeda44a2950992aff0599544c2b2f7d9307 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
###############################################################################
# Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems)   #
# and others                                                                  #
#                                                                             #
# All rights reserved. This program and the accompanying materials            #
# are made available under the terms of the Apache License, Version 2.0       #
# which accompanies this distribution, and is available at                    #
# http://www.apache.org/licenses/LICENSE-2.0                                  #
###############################################################################
from pymongo import MongoClient, ReturnDocument
from pymongo.errors import ConnectionFailure
from urllib.parse import quote_plus
import docker
import argparse
import dockerpycreds
# note : not used, useful for docker api security if used
import time
import json


class MongoComm:
    # deals with communication from host/installer server to mongoDB, includes methods for future use
    try:

        def __init__(self, host, user, password, port):
            self.uri = "mongodb://%s:%s@%s:%s/%s" % (
                quote_plus(user), quote_plus(password), host, port, "calipso")
            self.client = MongoClient(self.uri)

        def find(self, coll, key, val):
            collection = self.client.calipso[coll]
            doc = collection.find({key: val})
            return doc

        def get(self, coll, doc_name):
            collection = self.client.calipso[coll]
            doc = collection.find_one({"name": doc_name})
            return doc

        def insert(self, coll, doc):
            collection = self.client.calipso[coll]
            doc_id = collection.insert(doc)
            return doc_id

        def remove_doc(self, coll, doc):
            collection = self.client.calipso[coll]
            collection.remove(doc)

        def remove_coll(self, coll):
            collection = self.client.calipso[coll]
            collection.remove()

        def find_update(self, coll, key, val, data):
            collection = self.client.calipso[coll]
            collection.find_one_and_update(
                {key: val},
                {"$set": data},
                upsert=True
            )

        def update(self, coll, doc, upsert=False):
            collection = self.client.calipso[coll]
            doc_id = collection.update_one({'_id': doc['_id']},{'$set': doc}, upsert=upsert)
            return doc_id

    except ConnectionFailure:
        print("MongoDB Server not available")


DockerClient = docker.from_env()   # using local host docker environment parameters

# use the below example for installer against a remote docker host:
# DockerClient = docker.DockerClient(base_url='tcp://korlev-calipso-testing.cisco.com:2375')


def copy_file(filename):
    c = MongoComm(args.hostname, args.dbuser, args.dbpassword, args.dbport)
    txt = open('db/'+filename+'.json')
    data = json.load(txt)
    c.remove_coll(filename)
    doc_id = c.insert(filename, data)
    print("Copied", filename, "mongo doc_ids:\n\n", doc_id, "\n\n")
    time.sleep(1)


C_MONGO_CONFIG = "/local_dir/calipso_mongo_access.conf"
H_MONGO_CONFIG = "/home/calipso/calipso_mongo_access.conf"
PYTHONPATH = "/home/scan/calipso_prod/app"
C_LDAP_CONFIG = "/local_dir/ldap.conf"
H_LDAP_CONFIG = "/home/calipso/ldap.conf"

# functions to check and start calipso containers:
def start_mongo(dbport):
    if not DockerClient.containers.list(all=True, filters={"name": "calipso-mongo"}):
        print("\nstarting container calipso-mongo, please wait...\n")
        image = DockerClient.images.list(all=True, name="korenlev/calipso:mongo")
        if image:
            print(image, "exists...not downloading...")
        else:
            print("image korenlev/calipso:mongo missing, hold on while downloading first...\n")
            image = DockerClient.images.pull("korenlev/calipso:mongo")
            print("Downloaded", image, "\n\n")
        mongocontainer = DockerClient.containers.run('korenlev/calipso:mongo', detach=True, name="calipso-mongo",
                                                     ports={'27017/tcp': dbport, '28017/tcp': 28017},
                                                     restart_policy={"Name": "always"})
        # wait a bit till mongoDB is up before starting to copy the json files from 'db' folder:
        time.sleep(5)
        enable_copy = input("create initial calipso DB ? (copy json files from 'db' folder to mongoDB -"
                            " 'c' to copy, 'q' to skip):")
        if enable_copy == "c":
            print("\nstarting to copy json files to mongoDB...\n\n")
            print("-----------------------------------------\n\n")
            time.sleep(1)
            copy_file("attributes_for_hover_on_data")
            copy_file("clique_constraints")
            copy_file("clique_types")
            copy_file("cliques")
            copy_file("constants")
            copy_file("environments_config")
            copy_file("inventory")
            copy_file("link_types")
            copy_file("links")
            copy_file("messages")
            copy_file("meteor_accounts_loginServiceConfiguration")
            copy_file("users")
            copy_file("monitoring_config")
            copy_file("monitoring_config_templates")
            copy_file("network_agent_types")
            copy_file("roles")
            copy_file("scans")
            copy_file("scheduled_scans")
            copy_file("statistics")
            copy_file("supported_environments")

            # note : 'messages', 'roles', 'users' and some of the 'constants' are filled by calipso-ui at runtime
            # some other docs are filled later by scanning, logging and monitoring
        else:
            return
    else:
        print("container named calipso-mongo already exists, please deal with it using docker...\n")
        return


def start_listen():
    if not DockerClient.containers.list(all=True, filters={"name": "calipso-listen"}):
        print("\nstarting container calipso-listen...\n")
        image = DockerClient.images.list(all=True, name="korenlev/calipso:listen")
        if image:
            print(image, "exists...not downloading...")
        else:
            print("image korenlev/calipso:listen missing, hold on while downloading first...\n")
            image = DockerClient.images.pull("korenlev/calipso:listen")
            print("Downloaded", image, "\n\n")
        listencontainer = DockerClient.containers.run('korenlev/calipso:listen', detach=True, name="calipso-listen",
                                                      ports={'22/tcp': 50022},
                                                      restart_policy={"Name": "always"},
                                                      environment=["PYTHONPATH=" + PYTHONPATH,
                                                                   "MONGO_CONFIG=" + C_MONGO_CONFIG],
                                                      volumes={'/home/calipso': {'bind': '/local_dir', 'mode': 'rw'}})
    else:
        print("container named calipso-listen already exists, please deal with it using docker...\n")
        return


def start_ldap():
    if not DockerClient.containers.list(all=True, filters={"name": "calipso-ldap"}):
        print("\nstarting container calipso-ldap...\n")
        image = DockerClient.images.list(all=True, name="korenlev/calipso:ldap")
        if image:
            print(image, "exists...not downloading...")
        else:
            print("image korenlev/calipso:ldap missing, hold on while downloading first...\n")
            image = DockerClient.images.pull("korenlev/calipso:ldap")
            print("Downloaded", image, "\n\n")
        ldapcontainer = DockerClient.containers.run('korenlev/calipso:ldap', detach=True, name="calipso-ldap",
                                                    ports={'389/tcp': 389, '389/udp': 389},
                                                    restart_policy={"Name": "always"},
                                                    volumes={'/home/calipso/': {'bind': '/local_dir/', 'mode': 'rw'}})
    else:
        print("container named calipso-ldap already exists, please deal with it using docker...\n")
        return


def start_api():
    if not DockerClient.containers.list(all=True, filters={"name": "calipso-api"}):
        print("\nstarting container calipso-api...\n")
        image = DockerClient.images.list(all=True, name="korenlev/calipso:api")
        if image:
            print(image, "exists...not downloading...")
        else:
            print("image korenlev/calipso:api missing, hold on while downloading first...\n")
            image = DockerClient.images.pull("korenlev/calipso:api")
            print("Downloaded", image, "\n\n")
        apicontainer = DockerClient.containers.run('korenlev/calipso:api', detach=True, name="calipso-api",
                                                   ports={'8000/tcp': 8000, '22/tcp': 40022},
                                                   restart_policy={"Name": "always"},
                                                   environment=["PYTHONPATH=" + PYTHONPATH,
                                                                "MONGO_CONFIG=" + C_MONGO_CONFIG,
                                                                "LDAP_CONFIG=" + C_LDAP_CONFIG,
                                                                "LOG_LEVEL=DEBUG"],
                                                   volumes={'/home/calipso/': {'bind': '/local_dir/', 'mode': 'rw'}})
    else:
        print("container named calipso-api already exists, please deal with it using docker...\n")
        return


def start_scan():
    if not DockerClient.containers.list(all=True, filters={"name": "calipso-scan"}):
        print("\nstarting container calipso-scan...\n")
        image = DockerClient.images.list(all=True, name="korenlev/calipso:scan")
        if image:
            print(image, "exists...not downloading...")
        else:
            print("image korenlev/calipso:scan missing, hold on while downloading first...\n")
            image = DockerClient.images.pull("korenlev/calipso:scan")
            print("Downloaded", image, "\n\n")
        scancontainer = DockerClient.containers.run('korenlev/calipso:scan', detach=True, name="calipso-scan",
                                                    ports={'22/tcp': 30022},
                                                    restart_policy={"Name": "always"},
                                                    environment=["PYTHONPATH=" + PYTHONPATH,
                                                                 "MONGO_CONFIG=" + C_MONGO_CONFIG],
                                                    volumes={'/home/calipso/': {'bind': '/local_dir/', 'mode': 'rw'}})
    else:
        print("container named calipso-scan already exists, please deal with it using docker...\n")
        return


def start_sensu():
    if not DockerClient.containers.list(all=True, filters={"name": "calipso-sensu"}):
        print("\nstarting container calipso-sensu...\n")
        image = DockerClient.images.list(all=True, name="korenlev/calipso:sensu")
        if image:
            print(image, "exists...not downloading...")
        else:
            print("image korenlev/calipso:sensu missing, hold on while downloading first...\n")
            image = DockerClient.images.pull("korenlev/calipso:sensu")
            print("Downloaded", image, "\n\n")
        sensucontainer = DockerClient.containers.run('korenlev/calipso:sensu', detach=True, name="calipso-sensu",
                                                     ports={'22/tcp': 20022, '3000/tcp': 3000, '4567/tcp': 4567,
                                                            '5671/tcp': 5671, '15672/tcp': 15672},
                                                     restart_policy={"Name": "always"},
                                                     environment=["PYTHONPATH=" + PYTHONPATH],
                                                     volumes={'/home/calipso/': {'bind': '/local_dir/', 'mode': 'rw'}})
    else:
        print("container named calipso-sensu already exists, please deal with it using docker...\n")
        return


def start_ui(host, dbuser, dbpassword, webport, dbport):
    if not DockerClient.containers.list(all=True, filters={"name": "calipso-ui"}):
        print("\nstarting container calipso-ui...\n")
        image = DockerClient.images.list(all=True, name="korenlev/calipso:ui")
        if image:
            print(image, "exists...not downloading...")
        else:
            print("image korenlev/calipso:ui missing, hold on while downloading first...\n")
            image = DockerClient.images.pull("korenlev/calipso:ui")
            print("Downloaded", image, "\n\n")
        uicontainer = DockerClient.containers.run('korenlev/calipso:ui', detach=True, name="calipso-ui",
                                                  ports={'3000/tcp': webport},
                                                  restart_policy={"Name": "always"},
                                                  environment=["ROOT_URL=http://{}:{}".format(host, str(webport)),
                                                               "MONGO_URL=mongodb://{}:{}@{}:{}/calipso".format
                                                               (dbuser, dbpassword, host, str(dbport)),
                                                               "LDAP_CONFIG=" + C_LDAP_CONFIG])
    else:
        print("container named calipso-ui already exists, please deal with it using docker...\n")
        return


# function to check and stop calipso containers:

def container_stop(container_name):
    if DockerClient.containers.list(all=True, filters={"name": container_name}):
        print("fetching container name", container_name, "...\n")
        c = DockerClient.containers.get(container_name)
        if c.status != "running":
            print(container_name, "is not running...")
            time.sleep(1)
            print("removing container name", c.name, "...\n")
            c.remove()
        else:
            print("killing container name", c.name, "...\n")
            c.kill()
            time.sleep(1)
            print("removing container name", c.name, "...\n")
            c.remove()
    else:
        print("no container named", container_name, "found...")


# parser for getting optional command arguments:
parser = argparse.ArgumentParser()
parser.add_argument("--hostname", help="Hostname or IP address of the server (default=172.17.0.1)",type=str,
                    default="172.17.0.1", required=False)
parser.add_argument("--webport", help="Port for the Calipso WebUI (default=80)",type=int,
                    default="80", required=False)
parser.add_argument("--dbport", help="Port for the Calipso MongoDB (default=27017)",type=int,
                    default="27017", required=False)
parser.add_argument("--dbuser", help="User for the Calipso MongoDB (default=calipso)",type=str,
                    default="calipso", required=False)
parser.add_argument("--dbpassword", help="Password for the Calipso MongoDB (default=calipso_default)",type=str,
                    default="calipso_default", required=False)
args = parser.parse_args()

container = ""
action = ""
container_names = ["all", "calipso-mongo", "calipso-scan", "calipso-listen", "calipso-ldap", "calipso-api",
                     "calipso-sensu", "calipso-ui"]
container_actions = ["stop", "start"]
while action not in container_actions:
    action = input("Action? (stop, start, or 'q' to quit):\n")
    if action == "q":
        exit()
while container not in container_names:
    container = input("Container? (all, calipso-mongo, calipso-scan, calipso-listen, calipso-ldap, calipso-api, "
                      "calipso-sensu, calipso-ui or 'q' to quit):\n")
    if container == "q":
        exit()

# starting the containers per arguments:
if action == "start":
    # building /home/calipso/calipso_mongo_access.conf and /home/calipso/ldap.conf files, per the arguments:
    calipso_mongo_access_text = "server " + args.hostname + "\nuser " + args.dbuser + "\npassword " + \
                                args.dbpassword + "\nauth_db calipso"
    ldap_text = "user admin" + "\npassword password" + "\nurl ldap://" + args.hostname + ":389" + \
                "\nuser_id_attribute CN" + "\nuser_pass_attribute userpassword" + \
                "\nuser_objectclass inetOrgPerson" + \
                "\nuser_tree_dn OU=Users,DC=openstack,DC=org" + "\nquery_scope one" + \
                "\ntls_req_cert allow" + \
                "\ngroup_member_attribute member"
    print("creating default", H_MONGO_CONFIG, "file...\n")
    calipso_mongo_access_file = open(H_MONGO_CONFIG, "w+")
    time.sleep(1)
    calipso_mongo_access_file.write(calipso_mongo_access_text)
    calipso_mongo_access_file.close()
    print("creating default", H_LDAP_CONFIG, "file...\n")
    ldap_file = open(H_LDAP_CONFIG, "w+")
    time.sleep(1)
    ldap_file.write(ldap_text)
    ldap_file.close()

    if container == "calipso-mongo" or container == "all":
        start_mongo(args.dbport)
        time.sleep(1)
    if container == "calipso-listen" or container == "all":
        start_listen()
        time.sleep(1)
    if container == "calipso-ldap" or container == "all":
        start_ldap()
        time.sleep(1)
    if container == "calipso-api" or container == "all":
        start_api()
        time.sleep(1)
    if container == "calipso-scan" or container == "all":
        start_scan()
        time.sleep(1)
    if container == "calipso-sensu" or container == "all":
        start_sensu()
        time.sleep(1)
    if container == "calipso-ui" or container == "all":
        start_ui(args.hostname, args.dbuser, args.dbpassword, args.webport, args.dbport)
        time.sleep(1)

# stopping the containers per arguments:
if action == "stop":
    if container == "calipso-mongo" or container == "all":
        container_stop("calipso-mongo")
    if container == "calipso-listen" or container == "all":
        container_stop("calipso-listen")
    if container == "calipso-ldap" or container == "all":
        container_stop("calipso-ldap")
    if container == "calipso-api" or container == "all":
        container_stop("calipso-api")
    if container == "calipso-scan" or container == "all":
        container_stop("calipso-scan")
    if container == "calipso-sensu" or container == "all":
        container_stop("calipso-sensu")
    if container == "calipso-ui" or container == "all":
        container_stop("calipso-ui")