Fix python3 compatibility issue

Make grafana-init.py and influxdb_setup.py scripts compatible with  python3

Change-Id: Id2909094473d82844072a299b9e0dab2d4893557
This commit is contained in:
Adrian Czarnecki 2019-11-07 12:00:02 +01:00
parent 159bc64f41
commit 1941530472
3 changed files with 33 additions and 22 deletions

View File

@ -1,4 +1,3 @@
#!/usr/bin/env python
# coding=utf-8
# (C) Copyright 2017 Hewlett Packard Enterprise Development LP

View File

@ -1,5 +1,3 @@
#!/usr/bin/env python
#
# (C) Copyright 2015,2016 Hewlett Packard Enterprise Development LP
#
@ -22,6 +20,14 @@
python based influxdb clients are available on this system.
"""
import json
import sys
from oslo_utils.encodeutils import safe_decode
from oslo_utils.encodeutils import safe_encode
import six.moves.urllib.parse as urlparse
from six.moves import urllib
ADMIN = 'root'
ADMIN_PASS = 'root'
DBNAME = 'mon'
@ -35,12 +41,6 @@ SHARDSPACE_NAME = 'persister_all'
REPLICATION = 1
RETENTION = '90d'
import json
import sys
import time
import six.moves.urllib.parse as urlparse
import urllib2
def format_response(req):
try:
@ -52,9 +52,10 @@ def format_response(req):
else:
return []
except KeyError:
print "Query returned a non-successful result: {0}".format(json_value['results'])
print("Query returned a non-successful result: {0}".format(json_value['results']))
raise
def influxdb_get(uri, query, db=None):
"""Runs a query via HTTP GET and returns the response as a Python list."""
@ -64,13 +65,14 @@ def influxdb_get(uri, query, db=None):
try:
params = urlparse.urlencode(getparams)
uri = "{}&{}".format(uri,params)
req = urllib2.urlopen(uri)
uri = "{}&{}".format(uri, params)
req = urllib.request.urlopen(uri)
return format_response(req)
except KeyError:
sys.exit(1)
def influxdb_get_post(uri, query, db=None):
"""Runs a query using HTTP GET or POST and returns the response as a Python list.
At some InfluxDB release several ops changed from using GET to POST. For example,
@ -82,20 +84,20 @@ def influxdb_get_post(uri, query, db=None):
query_params['db'] = db
try:
encoded_params = urlparse.urlencode(query_params)
encoded_params = safe_encode(urlparse.urlencode(query_params))
try:
req = urllib2.urlopen(uri, encoded_params)
req = urllib.request.urlopen(uri, encoded_params)
return format_response(req)
except urllib2.HTTPError:
except urllib.error.HTTPError:
uri = "{}&{}".format(uri, encoded_params)
req = urllib2.urlopen(uri)
req = urllib.request.urlopen(uri)
return format_response(req)
except KeyError:
sys.exit(1)
def main(argv=None):
"""If necessary, create the database, retention policy, and users"""
auth_str = '?u=%s&p=%s' % (ADMIN, ADMIN_PASS)
@ -104,9 +106,9 @@ def main(argv=None):
# List current databases
dbs = influxdb_get(uri=api_uri, query="SHOW DATABASES")
if [DBNAME] not in dbs:
print "Creating database '{}'".format(DBNAME)
print("Creating database '{}'".format(DBNAME))
influxdb_get_post(uri=api_uri, query="CREATE DATABASE {0}".format(DBNAME))
print "...created!"
print("...created!")
# Check retention policy
policies = influxdb_get(uri=api_uri,
@ -124,7 +126,8 @@ def main(argv=None):
for name, password in USERS.items():
if not any(user[0] == name for user in users):
influxdb_get_post(uri=api_uri,
query=unicode("CREATE USER {0} WITH PASSWORD '{1}'".format(name, password)),
query=safe_decode("CREATE USER {0} WITH PASSWORD '{1}'"
.format(name, password)),
db=DBNAME)
if __name__ == "__main__":

View File

@ -593,7 +593,11 @@ function install_schema_metric_database_influxdb {
sudo cp -f "${MONASCA_API_DIR}"/devstack/files/schema/influxdb_setup.py $MONASCA_SCHEMA_DIR/influxdb_setup.py
sudo chmod 0750 $MONASCA_SCHEMA_DIR/influxdb_setup.py
sudo chown root:root $MONASCA_SCHEMA_DIR/influxdb_setup.py
sudo $MONASCA_SCHEMA_DIR/influxdb_setup.py
if python3_enabled; then
sudo python3 $MONASCA_SCHEMA_DIR/influxdb_setup.py
else
sudo python $MONASCA_SCHEMA_DIR/influxdb_setup.py
fi
}
function install_schema_metric_database_vertica {
@ -1241,7 +1245,12 @@ function init_monasca_grafana {
sudo chown -R root:root "${DASHBOARDS_DIR}"
sudo chmod -R 0644 "${DASHBOARDS_DIR}"
sudo python "${MONASCA_API_DIR}"/devstack/files/grafana/grafana-init.py
if python3_enabled; then
sudo python3 "${MONASCA_API_DIR}"/devstack/files/grafana/grafana-init.py
else
sudo python "${MONASCA_API_DIR}"/devstack/files/grafana/grafana-init.py
fi
sudo rm -rf "${DASHBOARDS_DIR}"
}