bundle_dashboard_v1.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright: © 2021 Netdata Inc.
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5. '''Bundle the v1 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. import sys
  11. from pathlib import Path
  12. os.chdir(Path(__file__).parent.absolute())
  13. BASEPATH = Path('v1')
  14. URLTEMPLATE = 'https://github.com/netdata/dashboard/releases/download/{0}/dashboard.tar.gz'
  15. MAKEFILETEMPLATE = '''
  16. # Auto-generated by bundle_dashboard.py
  17. # Copyright: © 2021 Netdata Inc.
  18. # SPDX-License-Identifier: GPL-3.0-or-later
  19. MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
  20. dist_noinst_DATA = \\
  21. $(srcdir)/README.md
  22. dist_web_DATA = \\
  23. {0} \\
  24. $(NULL)
  25. webv1dir=$(webdir)/v1
  26. dist_webv1_DATA = \\
  27. index.html \\
  28. $(NULL)
  29. webcssdir=$(webdir)/css
  30. dist_webcss_DATA = \\
  31. {1} \\
  32. $(NULL)
  33. webfontsdir=$(webdir)/fonts
  34. dist_webfonts_DATA = \\
  35. {2} \\
  36. $(NULL)
  37. webimagesdir=$(webdir)/images
  38. dist_webimages_DATA = \\
  39. {3} \\
  40. $(NULL)
  41. weblibdir=$(webdir)/lib
  42. dist_weblib_DATA = \\
  43. {4} \\
  44. $(NULL)
  45. webstaticcssdir=$(webdir)/static/css
  46. dist_webstaticcss_DATA = \\
  47. {5} \\
  48. $(NULL)
  49. webstaticjsdir=$(webdir)/static/js
  50. dist_webstaticjs_DATA = \\
  51. {6} \\
  52. $(NULL)
  53. webstaticmediadir=$(webdir)/static/media
  54. dist_webstaticmedia_DATA = \\
  55. {7} \\
  56. $(NULL)
  57. '''
  58. def copy_dashboard(tag):
  59. '''Fetch and bundle the dashboard code.'''
  60. print('Preparing target directory')
  61. shutil.rmtree(BASEPATH)
  62. BASEPATH.mkdir()
  63. print('::group::Fetching dashboard release tarball')
  64. subprocess.check_call('curl -L -o dashboard.tar.gz ' + URLTEMPLATE.format(tag), shell=True)
  65. print('::endgroup::')
  66. print('::group::Extracting dashboard release tarball')
  67. subprocess.check_call('tar -xvzf dashboard.tar.gz -C ' + str(BASEPATH) + ' --strip-components=1', shell=True)
  68. print('::endgroup::')
  69. print('Copying README.md')
  70. BASEPATH.joinpath('README.md').symlink_to('../.dashboard-notice.md')
  71. print('Removing dashboard release tarball')
  72. BASEPATH.joinpath('..', 'dashboard.tar.gz').unlink()
  73. def genfilelist(path):
  74. '''Generate a list of files for the Makefile.'''
  75. files = [f for f in path.iterdir() if f.is_file() and f.name != 'README.md']
  76. files = [Path(*f.parts[1:]) for f in files]
  77. files.sort()
  78. return ' \\\n '.join([("$(srcdir)/" + str(f)) for f in files])
  79. def write_makefile():
  80. '''Write out the makefile for the dashboard code.'''
  81. print('Generating Makefile')
  82. MAKEFILEDATA = MAKEFILETEMPLATE.format(
  83. genfilelist(BASEPATH),
  84. genfilelist(BASEPATH.joinpath('css')),
  85. genfilelist(BASEPATH.joinpath('fonts')),
  86. genfilelist(BASEPATH.joinpath('images')),
  87. genfilelist(BASEPATH.joinpath('lib')),
  88. genfilelist(BASEPATH.joinpath('static', 'css')),
  89. genfilelist(BASEPATH.joinpath('static', 'js')),
  90. genfilelist(BASEPATH.joinpath('static', 'media')),
  91. )
  92. BASEPATH.joinpath('Makefile.am').write_text(MAKEFILEDATA)
  93. def list_changed_files():
  94. '''Create a list of changed files, and set it in an environment variable.'''
  95. if 'GITHUB_ENV' in os.environ:
  96. print('Generating file list for commit.')
  97. subprocess.check_call('echo "COMMIT_FILES<<EOF" >> $GITHUB_ENV', shell=True)
  98. subprocess.check_call('git status --porcelain=v1 --no-renames --untracked-files=all | rev | cut -d \' \' -f 1 | rev >> $GITHUB_ENV', shell=True)
  99. subprocess.check_call('echo "EOF" >> $GITHUB_ENV', shell=True)
  100. copy_dashboard(sys.argv[1])
  101. write_makefile()
  102. list_changed_files()