bundle_dashboard_v2.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright: © 2023 Netdata Inc.
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5. '''Bundle the v2 dashboard code into the agent repo.
  6. This is designed to be run as part of a GHA workflow, but will work fine outside of one.'''
  7. import os
  8. import shutil
  9. import subprocess
  10. from pathlib import Path
  11. os.chdir(Path(__file__).parent.absolute())
  12. BASEDIR = 'v2'
  13. BASEPATH = Path(BASEDIR)
  14. TMPPATH = Path('tmp')
  15. URLSRC = 'https://app.netdata.cloud/agent.tar.gz'
  16. MAKEFILETEMPLATE = '''
  17. # Auto-generated by bundle_dashboard_v2.py
  18. # Copyright: © 2023 Netdata Inc.
  19. # SPDX-License-Identifier: GPL-3.0-or-later
  20. MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
  21. dist_noinst_DATA = \\
  22. $(srcdir)/README.md
  23. webv2dir=$(webdir)/v2
  24. dist_webv2_DATA = \\
  25. {0} \\
  26. $(NULL)
  27. webv2staticdir=$(webv2dir)/static
  28. dist_webv2static_DATA = \\
  29. {1} \\
  30. $(NULL)
  31. webv2staticemailimgdir=$(webv2staticdir)/email/img
  32. dist_webv2staticemailimg_DATA = \\
  33. {2} \\
  34. $(NULL)
  35. webv2staticimgdir=$(webv2staticdir)/img
  36. dist_webv2staticimg_DATA = \\
  37. {3} \\
  38. $(NULL)
  39. webv2staticimglogososdir=$(webv2staticimgdir)/logos/os
  40. dist_webv2staticimglogosos_DATA = \\
  41. {4} \\
  42. $(NULL)
  43. webv2staticimglogosservicesdir=$(webv2staticimgdir)/logos/services
  44. dist_webv2staticimglogosservices_DATA = \\
  45. {5} \\
  46. $(NULL)
  47. webv2staticimgmaildir=$(webv2staticimgdir)/mail
  48. dist_webv2staticimgmail_DATA = \\
  49. {6} \\
  50. $(NULL)
  51. webv2staticsitepagesholding503dir=$(webv2staticdir)/site/pages/holding-page-503
  52. dist_webv2staticsitepagesholding503_DATA = \\
  53. {7} \\
  54. $(NULL)
  55. '''
  56. def copy_dashboard():
  57. '''Fetch and bundle the dashboard code.'''
  58. print('Preparing target directory')
  59. shutil.rmtree(BASEPATH)
  60. TMPPATH.mkdir()
  61. print('::group::Fetching dashboard release tarball')
  62. subprocess.check_call(f'curl -L -o agent.tar { URLSRC }', shell=True)
  63. print('::endgroup::')
  64. print('::group::Extracting dashboard release tarball')
  65. subprocess.check_call(f"tar -xvf agent.tar -C { TMPPATH } --strip-components=1 --exclude='*.br' --exclude='*.gz'", shell=True)
  66. print('::endgroup::')
  67. print('Copying files')
  68. (TMPPATH / 'agent' / BASEDIR).rename(BASEPATH)
  69. (TMPPATH / 'agent' / 'index.html').rename(Path('./index.html'))
  70. (TMPPATH / 'agent' / 'registry-access.html').rename('./registry-access.html')
  71. (TMPPATH / 'agent' / 'registry-alert-redirect.html').rename('./registry-alert-redirect.html')
  72. (TMPPATH / 'agent' / 'registry-hello.html').rename('./registry-hello.html')
  73. shutil.copytree(TMPPATH / 'agent' / 'static', Path('./static'), dirs_exist_ok=True)
  74. shutil.rmtree(TMPPATH)
  75. print('Copying README.md')
  76. BASEPATH.joinpath('README.md').symlink_to('../.dashboard-v2-notice.md')
  77. print('Removing dashboard release tarball')
  78. BASEPATH.joinpath('..', 'agent.tar').unlink()
  79. def genfilelist(path):
  80. '''Generate a list of files for the Makefile.'''
  81. files = [f for f in path.iterdir() if f.is_file() and f.name != 'README.md']
  82. files = [Path(*f.parts[1:]) for f in files]
  83. files.sort()
  84. return ' \\\n '.join([("$(srcdir)/" + str(f)) for f in files])
  85. def write_makefile():
  86. '''Write out the makefile for the dashboard code.'''
  87. print('Generating Makefile')
  88. MAKEFILEDATA = MAKEFILETEMPLATE.format(
  89. genfilelist(BASEPATH),
  90. genfilelist(BASEPATH.joinpath('static')),
  91. genfilelist(BASEPATH.joinpath('static', 'email', 'img')),
  92. genfilelist(BASEPATH.joinpath('static', 'img')),
  93. genfilelist(BASEPATH.joinpath('static', 'img', 'logos', 'os')),
  94. genfilelist(BASEPATH.joinpath('static', 'img', 'logos', 'services')),
  95. genfilelist(BASEPATH.joinpath('static', 'img', 'mail')),
  96. genfilelist(BASEPATH.joinpath('static', 'site', 'pages', 'holding-page-503')),
  97. )
  98. BASEPATH.joinpath('Makefile.am').write_text(MAKEFILEDATA)
  99. def list_changed_files():
  100. '''Create a list of changed files, and set it in an environment variable.'''
  101. if 'GITHUB_ENV' in os.environ:
  102. print('Generating file list for commit.')
  103. subprocess.check_call('echo "COMMIT_FILES<<EOF" >> $GITHUB_ENV', shell=True)
  104. subprocess.check_call('git status --porcelain=v1 --no-renames --untracked-files=all | rev | cut -d \' \' -f 1 | rev >> $GITHUB_ENV', shell=True)
  105. subprocess.check_call('echo "EOF" >> $GITHUB_ENV', shell=True)
  106. copy_dashboard()
  107. write_makefile()
  108. list_changed_files()