ac2550 | ead1051 | 2018-10-05 13:50:23 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ### |
| 3 | # ============LICENSE_START======================================================= |
| 4 | # ONAP CLAMP |
| 5 | # ================================================================================ |
| 6 | # Copyright (C) 2018 AT&T Intellectual Property. All rights |
| 7 | # reserved. |
| 8 | # ================================================================================ |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | # you may not use this file except in compliance with the License. |
| 11 | # You may obtain 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, |
| 17 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | # See the License for the specific language governing permissions and |
| 19 | # limitations under the License. |
| 20 | # ============LICENSE_END============================================ |
| 21 | # =================================================================== |
| 22 | # |
| 23 | ### |
| 24 | |
| 25 | import json |
| 26 | import logging |
| 27 | import os |
| 28 | import sys |
| 29 | |
| 30 | import requests |
| 31 | |
| 32 | PER_PAGE = 1000 |
| 33 | |
| 34 | def parse_args(args): |
| 35 | """ Parse arguments given to this script""" |
| 36 | import argparse |
| 37 | parser = argparse.ArgumentParser( |
| 38 | description=('Description of the script')) |
| 39 | parser.add_argument('-v', '--verbose', dest='log_level', action='store_const', |
| 40 | const=logging.DEBUG, default=logging.INFO, |
| 41 | help='Use verbose logging') |
| 42 | parser.add_argument('-C', '--configuration_path', |
| 43 | default='./default', |
| 44 | help='Path of the configuration to be backed up.') |
| 45 | parser.add_argument('-f', '--force', action='store_const', |
| 46 | const=True, default=False, |
| 47 | help=('If the save folder already exists, overwrite files' |
| 48 | ' matching a configuration item that should be written.' |
| 49 | ' Files already in the folder that do not match are' |
| 50 | ' left as-is.')) |
| 51 | parser.add_argument('-H', '--kibana-host', default='http://localhost:5601', |
| 52 | help='Kibana endpoint.') |
| 53 | |
| 54 | return parser.parse_args(args) |
| 55 | |
| 56 | def get_logger(args): |
| 57 | """Creates the logger based on the provided arguments""" |
| 58 | logging.basicConfig() |
| 59 | logger = logging.getLogger(__name__) |
| 60 | logger.setLevel(args.log_level) |
| 61 | return logger |
| 62 | |
| 63 | def main(): |
| 64 | """ This script dumps the kibana configuration from Kibana""" |
| 65 | args = parse_args(sys.argv[1:]) |
| 66 | |
| 67 | base_config_path = args.configuration_path |
| 68 | |
| 69 | # get list of all objects available |
| 70 | url = "%s/api/saved_objects/" % (args.kibana_host.rstrip("/"),) |
| 71 | saved_objects_req = requests.get(url, |
| 72 | params={'per_page': PER_PAGE}) |
| 73 | |
| 74 | saved_objects = saved_objects_req.json()['saved_objects'] |
| 75 | |
| 76 | for obj in saved_objects: |
| 77 | |
| 78 | obj_folder = os.path.sep.join((base_config_path, obj['type'])) |
| 79 | |
| 80 | if not os.path.exists(obj_folder): |
| 81 | os.makedirs(obj_folder) |
| 82 | |
| 83 | filename = "%s/%s-%s.json" % (obj_folder, obj['type'], obj['id']) |
| 84 | with open(filename, 'w') as file: |
| 85 | json.dump(obj, fp=file) |
| 86 | |
| 87 | |
| 88 | if __name__ == "__main__": |
| 89 | main() |