Newer
Older
Mikel
committed
import json, argparse
from dashboardApi import import_grafana_settings, search_datasource
from commons import print_horizontal_line
arthur.yueh
committed
from datetime import datetime
arthur.yueh
committed
parser.add_argument('path', help='folder path to save datasources')
Mikel
committed
parser.add_argument('conf_filename', default="grafanaSettings", help='The settings file name in the conf directory'
' (for example: the server name we want to backup/restore)')
arthur.yueh
committed
folder_path = args.path
Mikel
committed
import_grafana_settings(args.conf_filename)
arthur.yueh
committed
log_file = 'datasources_{0}.txt'.format(datetime.today().strftime('%Y%m%d%H%M'))
arthur.yueh
committed
def save_datasource(file_name, datasource_setting):
file_path = folder_path + '/' + file_name + '.datasource'
with open(file_path, 'w') as f:
f.write(json.dumps(datasource_setting))
arthur.yueh
committed
print("datasource:{0} is saved to {1}".format(file_name, file_path))
arthur.yueh
committed
def get_all_datasources_and_save():
arthur.yueh
committed
status_code_and_content = search_datasource()
if status_code_and_content[0] == 200:
datasources = status_code_and_content[1]
arthur.yueh
committed
print("There are {0} datasources:".format(len(datasources)))
for datasource in datasources:
print(datasource)
save_datasource(datasource['name'], datasource)
else:
Mikel
committed
print("query datasource failed, status: {0}, msg: {1}".format(status_code_and_content[0],
status_code_and_content[1]))
arthur.yueh
committed
datasources = get_all_datasources_and_save()