You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.4 KiB

  1. import requests
  2. from datetime import datetime, timedelta as td
  3. from django.core.management.base import BaseCommand
  4. from hc.api.models import Check, DEFAULT_TIMEOUT
  5. PROJECT_ID_TO_API_KEY_MAP = {
  6. 2: 'eB-PDSWREOVFjjxZr1ena3hOqB9EglWX',
  7. 1: 'RZ9BZERmgS40paCpfsGw9zomL1VIxlYe',
  8. 3: '5IVMol_f50_6DjsGCImPIcawsNzEZoxb',
  9. }
  10. PROJECT_ID_TO_INTEGRATION_ID_MAP = {
  11. 2: 'https://hooks.slack.com/services/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',
  12. 1: 'https://hooks.slack.com/services/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',
  13. 3: 'https://hooks.slack.com/services/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',
  14. }
  15. API_CHECKS_URL = 'https://healthchecks.io/api/v1/checks/'
  16. class Command(BaseCommand):
  17. help = "Migrates healthchecks from healthchecks.io to healthchecks.squadplatform.com"
  18. def handle(self, *args, **options):
  19. self.stdout.write("migration started\n")
  20. for project_id, api_key in PROJECT_ID_TO_API_KEY_MAP.items():
  21. self.stdout.write('migrating Project ' + str(project_id))
  22. checks = self.fetch_checks_from_api(api_key)
  23. self.stdout.write('Checks fetched')
  24. self.create_checks_in_db(checks, project_id)
  25. self.stdout.write('Created checks in DB')
  26. def fetch_checks_from_api(self, api_key: str):
  27. response = requests.get(API_CHECKS_URL, headers={'X-Api-Key': api_key})
  28. if response.ok:
  29. return response.json()['checks']
  30. raise Exception('API request failed for api-key', api_key)
  31. def create_checks_in_db(self, checks: [dict], project_id: int):
  32. checks_to_create = []
  33. for check in checks:
  34. checks_to_create.append(Check(
  35. name=check['name'],
  36. tags=check['tags'],
  37. desc=check['desc'],
  38. grace=td(seconds=check['grace']),
  39. n_pings=check['n_pings'],
  40. last_ping=check['last_ping'],
  41. timeout=td(seconds=check['timeout']) if 'timeout' in check else DEFAULT_TIMEOUT,
  42. tz=check['tz'] if 'tz' in check else 'UTC',
  43. schedule=check['schedule'] if 'schedule' in check else "* * * * *",
  44. code=check['ping_url'][check['ping_url'].find('.com/')+5:],
  45. kind='simple' if 'timeout' in check else 'cron',
  46. project_id=project_id,
  47. ))
  48. Check.objects.bulk_create(checks_to_create)