| tierno | 44528e4 | 2016-10-11 12:06:25 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | ## |
| 5 | # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. |
| 6 | # This file is part of openmano |
| 7 | # All Rights Reserved. |
| 8 | # |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | # not use this file except in compliance with the License. You may obtain |
| 11 | # a copy of the License at |
| 12 | # |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | # |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | # License for the specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # |
| 21 | # For those usages not covered by the Apache License, Version 2.0 please |
| 22 | # contact with: nfvlabs@tid.es |
| 23 | ## |
| 24 | |
| 25 | |
| 26 | """ |
| 27 | Read openmanod.cfg file and creates envioronment variables for openmano client |
| 28 | Call it wusing execution quotes, or copy paste the output to set your shell envioronment |
| 29 | It read database to look for a ninc tenant / datacenter |
| 30 | """ |
| 31 | |
| 32 | from __future__ import print_function |
| 33 | from os import environ |
| 34 | from openmanod import load_configuration |
| 35 | #from socket import gethostname |
| 36 | from db_base import db_base_Exception |
| 37 | import nfvo_db |
| 38 | import getopt |
| 39 | import sys |
| 40 | |
| 41 | |
| 42 | __author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes" |
| 43 | __date__ ="$26-aug-2014 11:09:29$" |
| 44 | __version__="0.0.1-r509" |
| 45 | version_date="Oct 2016" |
| 46 | database_version="0.16" #expected database schema version |
| 47 | |
| 48 | |
| 49 | def usage(): |
| 50 | print("Usage: ", sys.argv[0], "[options]") |
| 51 | print(" -v|--version: prints current version") |
| 52 | print(" -c|--config [configuration_file]: loads the configuration file (default: openmanod.cfg)") |
| 53 | print(" -h|--help: shows this help") |
| 54 | return |
| 55 | |
| 56 | |
| 57 | if __name__ == "__main__": |
| 58 | # Read parameters and configuration file |
| 59 | try: |
| 60 | # load parameters and configuration |
| 61 | opts, args = getopt.getopt(sys.argv[1:], "vhc:", |
| 62 | ["config=", "help", "version"]) |
| 63 | config_file = 'openmanod.cfg' |
| 64 | |
| 65 | for o, a in opts: |
| 66 | if o in ("-v", "--version"): |
| 67 | print("openmanoconfig.py version " + __version__ + ' ' + version_date) |
| 68 | print("(c) Copyright Telefonica") |
| 69 | exit() |
| 70 | elif o in ("-h", "--help"): |
| 71 | usage() |
| 72 | exit() |
| 73 | elif o in ("-c", "--config"): |
| 74 | config_file = a |
| 75 | else: |
| 76 | assert False, "Unhandled option" |
| 77 | global_config = load_configuration(config_file) |
| 78 | if global_config["http_host"] == "0.0.0.0": |
| 79 | global_config["http_host"] = "localhost" #gethostname() |
| 80 | environ["OPENMANO_HOST"]=global_config["http_host"] |
| 81 | print("export OPENMANO_HOST='{}'".format(global_config["http_host"])) |
| 82 | environ["OPENMANO_PORT"] = str(global_config["http_port"]) |
| 83 | print("export OPENMANO_PORT={}".format(global_config["http_port"])) |
| 84 | |
| 85 | mydb = nfvo_db.nfvo_db(); |
| 86 | mydb.connect(global_config['db_host'], global_config['db_user'], global_config['db_passwd'], global_config['db_name']) |
| 87 | try: |
| 88 | tenants = mydb.get_rows(FROM="nfvo_tenants") |
| 89 | if not tenants: |
| 90 | print("#No tenant found", file=sys.stderr) |
| 91 | elif len(tenants) > 1: |
| 92 | print("#Found several tenants export OPENMANO_TENANT=", file=sys.stderr, end="") |
| 93 | for tenant in tenants: |
| 94 | print(" '{}'".format(tenant["name"]), file=sys.stderr, end="") |
| 95 | print("") |
| 96 | else: |
| 97 | environ["OPENMANO_TENANT"] = tenants[0]["name"] |
| 98 | print("export OPENMANO_TENANT='{}'".format(tenants[0]["name"])) |
| 99 | |
| 100 | dcs = mydb.get_rows(FROM="datacenters") |
| 101 | if not dcs: |
| 102 | print("#No datacenter found", file=sys.stderr) |
| 103 | elif len(dcs) > 1: |
| 104 | print("#Found several datacenters export OPENMANO_DATACENTER=", file=sys.stderr, end="") |
| 105 | for dc in dcs: |
| 106 | print(" '{}'".format(dc["name"]), file=sys.stderr, end="") |
| 107 | print("") |
| 108 | else: |
| 109 | environ["OPENMANO_DATACENTER"] = dcs[0]["name"] |
| 110 | print("export OPENMANO_DATACENTER='{}'".format(dcs[0]["name"])) |
| 111 | |
| 112 | except db_base_Exception as e: |
| 113 | print("#DATABASE is not a MANO one or it is a '0.0' version. Try to upgrade to version '{}' with \ |
| 114 | './database_utils/migrate_mano_db.sh'".format(database_version), file=sys.stderr) |
| 115 | exit(-1) |
| 116 | |
| 117 | |
| 118 | |
| 119 | except db_base_Exception as e: |
| 120 | print("#"+str(e), file=sys.stderr) |
| 121 | exit(-1) |
| 122 | |
| 123 | except SystemExit: |
| 124 | pass |