2 # -*- coding: utf-8 -*-
5 # Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
6 # This file is part of openmano
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact with: nfvlabs@tid.es
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
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
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
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")
57 if __name__
== "__main__":
58 # Read parameters and configuration file
60 # load parameters and configuration
61 opts
, args
= getopt
.getopt(sys
.argv
[1:], "vhc:",
62 ["config=", "help", "version"])
63 config_file
= 'openmanod.cfg'
66 if o
in ("-v", "--version"):
67 print("openmanoconfig.py version " + __version__
+ ' ' + version_date
)
68 print("(c) Copyright Telefonica")
70 elif o
in ("-h", "--help"):
73 elif o
in ("-c", "--config"):
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"]))
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'])
88 tenants
= mydb
.get_rows(FROM
="nfvo_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
="")
97 environ
["OPENMANO_TENANT"] = tenants
[0]["name"]
98 print("export OPENMANO_TENANT='{}'".format(tenants
[0]["name"]))
100 dcs
= mydb
.get_rows(FROM
="datacenters")
102 print("#No datacenter found", file=sys
.stderr
)
104 print("#Found several datacenters export OPENMANO_DATACENTER=", file=sys
.stderr
, end
="")
106 print(" '{}'".format(dc
["name"]), file=sys
.stderr
, end
="")
109 environ
["OPENMANO_DATACENTER"] = dcs
[0]["name"]
110 print("export OPENMANO_DATACENTER='{}'".format(dcs
[0]["name"]))
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
)
119 except db_base_Exception
as e
:
120 print("#"+str(e
), file=sys
.stderr
)