bundle_dashboard_v2.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. CMAKETEMPLATE = '''
  17. install(FILES {0} DESTINATION ${{WEB_DEST}}/v2)
  18. install(FILES {1} DESTINATION ${{WEB_DEST}}/v2/static)
  19. install(FILES {2} DESTINATION ${{WEB_DEST}}/v2/static/email/img)
  20. install(FILES {3} DESTINATION ${{WEB_DEST}}/v2/static/img)
  21. install(FILES {4} DESTINATION ${{WEB_DEST}}/v2/static/img/logos/os)
  22. install(FILES {5} DESTINATION ${{WEB_DEST}}/v2/static/img/logos/services)
  23. install(FILES {6} DESTINATION ${{WEB_DEST}}/v2/static/img/mail)
  24. install(FILES {7} DESTINATION ${{WEB_DEST}}/v2/static/site/pages/holding-page-503)
  25. '''
  26. def copy_dashboard():
  27. '''Fetch and bundle the dashboard code.'''
  28. print('Preparing target directory')
  29. shutil.rmtree(BASEPATH)
  30. TMPPATH.mkdir()
  31. print('::group::Fetching dashboard release tarball')
  32. subprocess.check_call(f'curl -L -o agent.tar { URLSRC }', shell=True)
  33. print('::endgroup::')
  34. print('::group::Extracting dashboard release tarball')
  35. subprocess.check_call(f"tar -xvf agent.tar -C { TMPPATH } --strip-components=1 --exclude='*.br' --exclude='*.gz'", shell=True)
  36. print('::endgroup::')
  37. print('Copying files')
  38. (TMPPATH / 'agent' / BASEDIR).rename(BASEPATH)
  39. (TMPPATH / 'agent' / 'index.html').rename(Path('./index.html'))
  40. (TMPPATH / 'agent' / 'registry-access.html').rename('./registry-access.html')
  41. (TMPPATH / 'agent' / 'registry-alert-redirect.html').rename('./registry-alert-redirect.html')
  42. (TMPPATH / 'agent' / 'registry-hello.html').rename('./registry-hello.html')
  43. shutil.copytree(TMPPATH / 'agent' / 'static', Path('./static'), dirs_exist_ok=True)
  44. shutil.rmtree(TMPPATH)
  45. print('Copying README.md')
  46. BASEPATH.joinpath('README.md').symlink_to('../.dashboard-v2-notice.md')
  47. print('Removing dashboard release tarball')
  48. BASEPATH.joinpath('..', 'agent.tar').unlink()
  49. def genfilelist(path):
  50. '''Generate a list of files for the Makefile.'''
  51. files = [f for f in path.iterdir() if f.is_file() and f.name != 'README.md']
  52. files = [Path(*f.parts[1:]) for f in files]
  53. files.sort()
  54. return '\n'.join([("web/gui/v2/" + str(f)) for f in files])
  55. def write_cmakefile():
  56. '''Write out the makefile for the dashboard code.'''
  57. print('Generating cmake file')
  58. output = CMAKETEMPLATE.format(
  59. genfilelist(BASEPATH),
  60. genfilelist(BASEPATH.joinpath('static')),
  61. genfilelist(BASEPATH.joinpath('static', 'email', 'img')),
  62. genfilelist(BASEPATH.joinpath('static', 'img')),
  63. genfilelist(BASEPATH.joinpath('static', 'img', 'logos', 'os')),
  64. genfilelist(BASEPATH.joinpath('static', 'img', 'logos', 'services')),
  65. genfilelist(BASEPATH.joinpath('static', 'img', 'mail')),
  66. genfilelist(BASEPATH.joinpath('static', 'site', 'pages', 'holding-page-503')),
  67. )
  68. BASEPATH.joinpath('dashboard_v2.cmake').write_text(output)
  69. def list_changed_files():
  70. '''Create a list of changed files, and set it in an environment variable.'''
  71. if 'GITHUB_ENV' in os.environ:
  72. print('Generating file list for commit.')
  73. subprocess.check_call('echo "COMMIT_FILES<<EOF" >> $GITHUB_ENV', shell=True)
  74. subprocess.check_call('git status --porcelain=v1 --no-renames --untracked-files=all | rev | cut -d \' \' -f 1 | rev >> $GITHUB_ENV', shell=True)
  75. subprocess.check_call('echo "EOF" >> $GITHUB_ENV', shell=True)
  76. copy_dashboard()
  77. write_cmakefile()
  78. list_changed_files()