"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "devstack/files/schema/influxdb_setup.py" between
monasca-api-3.1.0.tar.gz and monasca-api-4.0.0.tar.gz

About: OpenStack Monasca API is a RESTful API server that is designed with a layered architecture and supports Monitoring as a Service (MONaaS).
The "Ussuri" series (latest release).

influxdb_setup.py  (monasca-api-3.1.0):influxdb_setup.py  (monasca-api-4.0.0)
#!/usr/bin/env python
# #
# (C) Copyright 2015,2016 Hewlett Packard Enterprise Development LP # (C) Copyright 2015,2016 Hewlett Packard Enterprise Development LP
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
skipping to change at line 25 skipping to change at line 23
# implied. # implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# #
"""A simple script to setup influxdb user and roles. At some point this should """A simple script to setup influxdb user and roles. At some point this should
become a more full featured module. Also this assumes that none of the become a more full featured module. Also this assumes that none of the
python based influxdb clients are available on this system. 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
from six.moves import urllib
import six.moves.urllib.parse as urlparse
ADMIN = 'root' ADMIN = 'root'
ADMIN_PASS = 'root' ADMIN_PASS = 'root'
DBNAME = 'mon' DBNAME = 'mon'
USERS = {} USERS = {}
USERS['mon_api'] = 'password' USERS['mon_api'] = 'password'
USERS['mon_persister'] = 'password' USERS['mon_persister'] = 'password'
URL = 'http://127.0.0.1:8086' URL = 'http://127.0.0.1:8086'
SHARDSPACE_NAME = 'persister_all' SHARDSPACE_NAME = 'persister_all'
REPLICATION = 1 REPLICATION = 1
RETENTION = '90d' RETENTION = '90d'
import json
import sys
import time
import six.moves.urllib.parse as urlparse
import urllib2
def format_response(req): def format_response(req):
try: try:
json_value = json.loads(req.read()) json_value = json.loads(req.read())
if (len(json_value['results'][0]) > 0 and if (len(json_value['results'][0]) > 0 and
'series' in json_value['results'][0] and 'series' in json_value['results'][0] and
'values' in json_value['results'][0]['series'][0]): 'values' in json_value['results'][0]['series'][0]):
return json_value['results'][0]['series'][0]['values'] return json_value['results'][0]['series'][0]['values']
else: else:
return [] return []
except KeyError: except KeyError:
print "Query returned a non-successful result: {0}".format(json_value['r esults']) print("Query returned a non-successful result: {0}".format(json_value['r esults']))
raise raise
def influxdb_get(uri, query, db=None): def influxdb_get(uri, query, db=None):
"""Runs a query via HTTP GET and returns the response as a Python list.""" """Runs a query via HTTP GET and returns the response as a Python list."""
getparams = {"q": query} getparams = {"q": query}
if db: if db:
getparams['db'] = db getparams['db'] = db
try: try:
params = urlparse.urlencode(getparams) params = urlparse.urlencode(getparams)
uri = "{}&{}".format(uri,params) uri = "{}&{}".format(uri, params)
req = urllib2.urlopen(uri) req = urllib.request.urlopen(uri)
return format_response(req) return format_response(req)
except KeyError: except KeyError:
sys.exit(1) sys.exit(1)
def influxdb_get_post(uri, query, db=None): def influxdb_get_post(uri, query, db=None):
"""Runs a query using HTTP GET or POST and returns the response as a Python list. """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, At some InfluxDB release several ops changed from using GET to POST. For example,
CREATE DATABASE. To maintain backward compatibility, this function first trys the CREATE DATABASE. To maintain backward compatibility, this function first trys the
query using POST and if that fails it retries again using GET.""" query using POST and if that fails it retries again using GET.
"""
query_params = {"q": query} query_params = {"q": query}
if db: if db:
query_params['db'] = db query_params['db'] = db
try: try:
encoded_params = urlparse.urlencode(query_params) encoded_params = safe_encode(urlparse.urlencode(query_params))
try: try:
req = urllib2.urlopen(uri, encoded_params) req = urllib.request.urlopen(uri, encoded_params)
return format_response(req) return format_response(req)
except urllib2.HTTPError: except urllib.error.HTTPError:
uri = "{}&{}".format(uri, encoded_params) uri = "{}&{}".format(uri, encoded_params)
req = urllib2.urlopen(uri) req = urllib.request.urlopen(uri)
return format_response(req) return format_response(req)
except KeyError: except KeyError:
sys.exit(1) sys.exit(1)
def main(argv=None): def main(argv=None):
"""If necessary, create the database, retention policy, and users""" """If necessary, create the database, retention policy, and users"""
auth_str = '?u=%s&p=%s' % (ADMIN, ADMIN_PASS) auth_str = '?u=%s&p=%s' % (ADMIN, ADMIN_PASS)
api_uri = "{0}/query{1}".format(URL, auth_str) api_uri = "{0}/query{1}".format(URL, auth_str)
# List current databases # List current databases
dbs = influxdb_get(uri=api_uri, query="SHOW DATABASES") dbs = influxdb_get(uri=api_uri, query="SHOW DATABASES")
if [DBNAME] not in dbs: 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 )) influxdb_get_post(uri=api_uri, query="CREATE DATABASE {0}".format(DBNAME ))
print "...created!" print("...created!")
# Check retention policy # Check retention policy
policies = influxdb_get(uri=api_uri, policies = influxdb_get(uri=api_uri,
query="SHOW RETENTION POLICIES ON {0}".format(DBNAME )) query="SHOW RETENTION POLICIES ON {0}".format(DBNAME ))
if not any(pol[0] == SHARDSPACE_NAME for pol in policies): if not any(pol[0] == SHARDSPACE_NAME for pol in policies):
# Set retention policy # Set retention policy
policy = "CREATE RETENTION POLICY {0} ON {1} DURATION {2} REPLICATION {3 policy = ("CREATE RETENTION POLICY {0} ON {1} DURATION {2} "
} DEFAULT".format(SHARDSPACE_NAME, "REPLICATION {3} DEFAULT".format(SHARDSPACE_NAME,
DBNAME,
DBNAME, RETENTION,
REPLICATION)
RETENTION, )
REPLICATION)
influxdb_get_post(uri=api_uri, db=DBNAME, query=policy) influxdb_get_post(uri=api_uri, db=DBNAME, query=policy)
# Create the users # Create the users
users = influxdb_get(uri=api_uri, query="SHOW USERS", db=DBNAME) users = influxdb_get(uri=api_uri, query="SHOW USERS", db=DBNAME)
for name, password in USERS.items(): for name, password in USERS.items():
if not any(user[0] == name for user in users): if not any(user[0] == name for user in users):
influxdb_get_post(uri=api_uri, influxdb_get_post(uri=api_uri,
query=unicode("CREATE USER {0} WITH PASSWORD '{1}' query=safe_decode("CREATE USER {0} WITH PASSWORD '
".format(name, password)), {1}'"
.format(name, password)),
db=DBNAME) db=DBNAME)
if __name__ == "__main__": if __name__ == "__main__":
sys.exit(main()) sys.exit(main())
 End of changes. 15 change blocks. 
31 lines changed or deleted 30 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)