Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable black linting #246

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Lint

on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: psf/black@stable
50 changes: 34 additions & 16 deletions grafana_backup/api_checks.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,53 @@
from grafana_backup.commons import print_horizontal_line
from grafana_backup.dashboardApi import health_check, auth_check, uid_feature_check, paging_feature_check, contact_point_check
from grafana_backup.dashboardApi import (
health_check,
auth_check,
uid_feature_check,
paging_feature_check,
contact_point_check,
)


def main(settings):
grafana_url = settings.get('GRAFANA_URL')
http_get_headers = settings.get('HTTP_GET_HEADERS')
verify_ssl = settings.get('VERIFY_SSL')
client_cert = settings.get('CLIENT_CERT')
debug = settings.get('DEBUG')
api_health_check = settings.get('API_HEALTH_CHECK')
api_auth_check = settings.get('API_AUTH_CHECK')
grafana_url = settings.get("GRAFANA_URL")
http_get_headers = settings.get("HTTP_GET_HEADERS")
verify_ssl = settings.get("VERIFY_SSL")
client_cert = settings.get("CLIENT_CERT")
debug = settings.get("DEBUG")
api_health_check = settings.get("API_HEALTH_CHECK")
api_auth_check = settings.get("API_AUTH_CHECK")

if api_health_check:
(status, json_resp) = health_check(grafana_url,
http_get_headers, verify_ssl, client_cert, debug)
(status, json_resp) = health_check(
grafana_url, http_get_headers, verify_ssl, client_cert, debug
)
if not status == 200:
return (status, json_resp, None, None, None)

if api_auth_check:
(status, json_resp) = auth_check(grafana_url,
http_get_headers, verify_ssl, client_cert, debug)
(status, json_resp) = auth_check(
grafana_url, http_get_headers, verify_ssl, client_cert, debug
)
if not status == 200:
return (status, json_resp, None, None, None)

dashboard_uid_support, datasource_uid_support = uid_feature_check(
grafana_url, http_get_headers, verify_ssl, client_cert, debug)
grafana_url, http_get_headers, verify_ssl, client_cert, debug
)
if isinstance(dashboard_uid_support, str):
raise Exception(dashboard_uid_support)
if isinstance(datasource_uid_support, str):
raise Exception(datasource_uid_support)

paging_support = paging_feature_check(
grafana_url, http_get_headers, verify_ssl, client_cert, debug)
grafana_url, http_get_headers, verify_ssl, client_cert, debug
)
if isinstance(paging_support, str):
raise Exception(paging_support)

is_contact_point_available = contact_point_check(
grafana_url, http_get_headers, verify_ssl, client_cert, debug)
grafana_url, http_get_headers, verify_ssl, client_cert, debug
)

print_horizontal_line()
if status == 200:
Expand All @@ -45,4 +56,11 @@ def main(settings):
print("[Pre-Check] Server status is NOT OK !!: {0}".format(json_resp))
print_horizontal_line()

return (status, json_resp, dashboard_uid_support, datasource_uid_support, paging_support, is_contact_point_available)
return (
status,
json_resp,
dashboard_uid_support,
datasource_uid_support,
paging_support,
is_contact_point_available,
)
31 changes: 23 additions & 8 deletions grafana_backup/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,33 @@


def main(args, settings):
backup_dir = settings.get('BACKUP_DIR')
timestamp = settings.get('TIMESTAMP')
backup_dir = settings.get("BACKUP_DIR")
timestamp = settings.get("TIMESTAMP")

archive_file = '{0}/{1}.tar.gz'.format(backup_dir, timestamp)
archive_file = "{0}/{1}.tar.gz".format(backup_dir, timestamp)
backup_files = list()

for folder_name in ['folders', 'datasources', 'dashboards', 'alert_channels', 'organizations', 'users', 'snapshots',
'dashboard_versions', 'annotations', 'library-elements', 'teams', 'team_members', 'alert_rules', 'contact_points', 'notification_policies']:
backup_path = '{0}/{1}/{2}'.format(backup_dir, folder_name, timestamp)
for folder_name in [
"folders",
"datasources",
"dashboards",
"alert_channels",
"organizations",
"users",
"snapshots",
"dashboard_versions",
"annotations",
"library-elements",
"teams",
"team_members",
"alert_rules",
"contact_points",
"notification_policies",
]:
backup_path = "{0}/{1}/{2}".format(backup_dir, folder_name, timestamp)

for file_path in glob(backup_path):
print('backup {0} at: {1}'.format(folder_name, file_path))
print("backup {0} at: {1}".format(folder_name, file_path))
backup_files.append(file_path)

if os.path.exists(archive_file):
Expand All @@ -27,4 +42,4 @@ def main(args, settings):
tar.add(file_path)
shutil.rmtree(os.path.abspath(os.path.join(file_path, os.pardir)))
tar.close()
print('\ncreated archive at: {0}'.format(archive_file))
print("\ncreated archive at: {0}".format(archive_file))
14 changes: 9 additions & 5 deletions grafana_backup/azure_storage_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@


def main(args, settings):
arg_archive_file = args.get('<archive_file>', None)
arg_archive_file = args.get("<archive_file>", None)

azure_storage_container_name = settings.get('AZURE_STORAGE_CONTAINER_NAME')
azure_storage_connection_string = settings.get('AZURE_STORAGE_CONNECTION_STRING')
azure_storage_container_name = settings.get("AZURE_STORAGE_CONTAINER_NAME")
azure_storage_connection_string = settings.get("AZURE_STORAGE_CONNECTION_STRING")

try:
blob_service_client = BlobServiceClient.from_connection_string(azure_storage_connection_string)
container_client = blob_service_client.get_blob_client(container=azure_storage_container_name, blob=arg_archive_file)
blob_service_client = BlobServiceClient.from_connection_string(
azure_storage_connection_string
)
container_client = blob_service_client.get_blob_client(
container=azure_storage_container_name, blob=arg_archive_file
)
azure_storage_bytes = container_client.download_blob().readall()
azure_storage_data = io.BytesIO(azure_storage_bytes)
print("Download from Azure Storage was successful")
Expand Down
22 changes: 13 additions & 9 deletions grafana_backup/azure_storage_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@


def main(args, settings):
azure_storage_container_name = settings.get('AZURE_STORAGE_CONTAINER_NAME')
azure_storage_connection_string = settings.get('AZURE_STORAGE_CONNECTION_STRING')
azure_storage_container_name = settings.get("AZURE_STORAGE_CONTAINER_NAME")
azure_storage_connection_string = settings.get("AZURE_STORAGE_CONNECTION_STRING")

backup_dir = settings.get('BACKUP_DIR')
timestamp = settings.get('TIMESTAMP')
backup_dir = settings.get("BACKUP_DIR")
timestamp = settings.get("TIMESTAMP")

azure_file_name = '{0}.tar.gz'.format(timestamp)
archive_file = '{0}/{1}'.format(backup_dir, azure_file_name)
azure_file_name = "{0}.tar.gz".format(timestamp)
archive_file = "{0}/{1}".format(backup_dir, azure_file_name)

try:
blob_service_client = BlobServiceClient.from_connection_string(azure_storage_connection_string)
container_client = blob_service_client.get_blob_client(container=azure_storage_container_name, blob=azure_file_name)
with open(archive_file, 'rb') as data:
blob_service_client = BlobServiceClient.from_connection_string(
azure_storage_connection_string
)
container_client = blob_service_client.get_blob_client(
container=azure_storage_container_name, blob=azure_file_name
)
with open(archive_file, "rb") as data:
container_client.upload_blob(data)
print("Upload to Azure Storage was successful")
except FileNotFoundError: # noqa: F821
Expand Down
26 changes: 13 additions & 13 deletions grafana_backup/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from grafana_backup.constants import (PKG_NAME, PKG_VERSION, JSON_CONFIG_PATH)
from grafana_backup.constants import PKG_NAME, PKG_VERSION, JSON_CONFIG_PATH
from grafana_backup.save import main as save
from grafana_backup.restore import main as restore
from grafana_backup.delete import main as delete
Expand Down Expand Up @@ -29,17 +29,17 @@

--no-archive Skip archive creation and do not delete unarchived files
(used for troubleshooting purposes)
""".format(PKG_NAME, PKG_VERSION)
""".format(
PKG_NAME, PKG_VERSION
)


args = docopt(docstring, help=False,
version='{0} {1}'.format(PKG_NAME, PKG_VERSION))
args = docopt(docstring, help=False, version="{0} {1}".format(PKG_NAME, PKG_VERSION))


def main():
arg_config = args.get('--config', False)
default_config = '{0}/conf/grafanaSettings.json'.format(
os.path.dirname(__file__))
arg_config = args.get("--config", False)
default_config = "{0}/conf/grafanaSettings.json".format(os.path.dirname(__file__))

if arg_config:
settings = conf(arg_config)
Expand All @@ -48,25 +48,25 @@ def main():
elif os.path.isfile(default_config):
settings = conf(default_config)

if args.get('save', None):
if args.get("save", None):
save(args, settings)
sys.exit()
elif args.get('restore', None):
elif args.get("restore", None):
restore(args, settings)
sys.exit()
elif args.get('delete', None):
elif args.get("delete", None):
delete(args, settings)
sys.exit()
elif args.get('tools', None):
elif args.get("tools", None):
tools(args, settings)
sys.exit()
elif args.get('--help', None):
elif args.get("--help", None):
print(docstring)
sys.exit()
else:
print(docstring)
sys.exit()


if __name__ == '__main__':
if __name__ == "__main__":
main()
22 changes: 13 additions & 9 deletions grafana_backup/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@


def print_horizontal_line():
print('')
print("")
print("########################################")
print('')
print("")


def log_response(resp):
Expand All @@ -13,22 +13,26 @@ def log_response(resp):
try:
print("[DEBUG] resp body: {0}".format(resp.json()))
except ValueError:
print("[DEBUG] resp body: {0}".format(to_python2_and_3_compatible_string(resp.text)))
print(
"[DEBUG] resp body: {0}".format(
to_python2_and_3_compatible_string(resp.text)
)
)
return resp


def to_python2_and_3_compatible_string(some_string):
if sys.version_info[0] > 2:
return some_string
else:
return some_string.encode('utf8')
return some_string.encode("utf8")


def load_config(path=None):
config = None

try:
with open(path, 'r') as f:
with open(path, "r") as f:
config = json.load(f)
f.closed
except IOError as e:
Expand All @@ -41,12 +45,12 @@ def load_config(path=None):
def save_json(file_name, data, folder_path, extension, pretty_print):
pattern = "^db/|^uid/"
if re.match(pattern, file_name):
file_name = re.sub(pattern, '', file_name)
file_name = re.sub(pattern, "", file_name)

file_path = folder_path + '/' + file_name + '.' + extension
with open(u"{0}".format(file_path), 'w') as f:
file_path = folder_path + "/" + file_name + "." + extension
with open("{0}".format(file_path), "w") as f:
if pretty_print:
f.write(json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')))
f.write(json.dumps(data, sort_keys=True, indent=4, separators=(",", ": ")))
else:
f.write(json.dumps(data))
# Return file_path for showing in the console message
Expand Down
27 changes: 19 additions & 8 deletions grafana_backup/create_alert_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@


def main(args, settings, file_path):
grafana_url = settings.get('GRAFANA_URL')
http_post_headers = settings.get('HTTP_POST_HEADERS')
verify_ssl = settings.get('VERIFY_SSL')
client_cert = settings.get('CLIENT_CERT')
debug = settings.get('DEBUG')
grafana_url = settings.get("GRAFANA_URL")
http_post_headers = settings.get("HTTP_POST_HEADERS")
verify_ssl = settings.get("VERIFY_SSL")
client_cert = settings.get("CLIENT_CERT")
debug = settings.get("DEBUG")

with open(file_path, 'r') as f:
with open(file_path, "r") as f:
data = f.read()

alert_channel = json.loads(data)
result = create_alert_channel(json.dumps(alert_channel), grafana_url, http_post_headers, verify_ssl, client_cert, debug)
print("create alert_channel: {0}, status: {1}, msg: {2}".format(alert_channel['name'], result[0], result[1]))
result = create_alert_channel(
json.dumps(alert_channel),
grafana_url,
http_post_headers,
verify_ssl,
client_cert,
debug,
)
print(
"create alert_channel: {0}, status: {1}, msg: {2}".format(
alert_channel["name"], result[0], result[1]
)
)
Loading