]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/mgr/dashboard: build frontend/dist in build
authorKefu Chai <kchai@redhat.com>
Sun, 11 Jul 2021 00:30:06 +0000 (08:30 +0800)
committerKefu Chai <kchai@redhat.com>
Thu, 22 Jul 2021 08:33:33 +0000 (16:33 +0800)
to enable us to build out of source, we should build the frontend
artifacts out of src. in this change:

* cmake:
  - install frontend/dist from build
  - drop rules to exclude unused frontend artifacts.
  - pass `--output-path` option to ng
  - copy `frontend/package.json` to build so that the frontend
    can find it
* pybind/mgr/dashboard/module.py: fall back to build directory
  if frontend/dist is not found.
* pybind/mgr/dashboard/__init__.py: use frontend/dist when performing
  unit test.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/pybind/mgr/CMakeLists.txt
src/pybind/mgr/dashboard/CMakeLists.txt
src/pybind/mgr/dashboard/__init__.py
src/pybind/mgr/dashboard/module.py

index fd629880697e49875a77ebf73b07d7ad45b1031c..264c6ed40b490fa7c0ec8a4b57b466eaf00a64bd 100644 (file)
@@ -6,17 +6,23 @@ set(mgr_module_install_excludes
   PATTERN "constraints*.txt" EXCLUDE
   PATTERN "tests/*" EXCLUDE)
 
-if(WITH_MGR_DASHBOARD_FRONTEND)
-  add_subdirectory(dashboard)
-endif()
-
 install(DIRECTORY dashboard
   DESTINATION ${CEPH_INSTALL_DATADIR}/mgr
   ${mgr_module_install_excludes}
-  REGEX "cypress.*" EXCLUDE
-  PATTERN "frontend/node_modules" EXCLUDE
+  PATTERN "frontend/*" EXCLUDE
   PATTERN ".*" EXCLUDE)
 
+if(WITH_MGR_DASHBOARD_FRONTEND)
+  # build from source
+  add_subdirectory(dashboard)
+else()
+  # prebuilt
+  install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/dashboard/frontend/dist
+    DESTINATION ${CEPH_INSTALL_DATADIR}/mgr/dashboard/frontend)
+  install(FILES dashboard/frontend/package.json
+    DESTINATION ${CEPH_INSTALL_DATADIR}/mgr/dashboard/frontend)
+endif()
+
 if(WITH_MGR_ROOK_CLIENT)
   add_subdirectory(rook)
 endif()
index 8771b3a76f678df239a21e98785592b89341e5bf..b8d558f0ce87eaa5da2395a0985801ea82899110 100644 (file)
@@ -102,27 +102,45 @@ execute_process(
     OUTPUT_VARIABLE default_lang
     OUTPUT_STRIP_TRAILING_WHITESPACE)
 
+set(frontend_dist_dir "${CMAKE_CURRENT_BINARY_DIR}/frontend/dist")
+set(npm_args "--output-path ${frontend_dist_dir}")
 if(NOT CMAKE_BUILD_TYPE STREQUAL Debug)
-  set(npm_args "--prod --progress=false")
+  string(APPEND npm_args " --prod --progress=false")
 else()
-  set(npm_args "--progress=false")
+  string(APPEND npm_args " --progress=false")
 endif()
 
 add_npm_command(
-  OUTPUT "${CMAKE_SOURCE_DIR}/src/pybind/mgr/dashboard/frontend/dist"
+  OUTPUT "${frontend_dist_dir}"
   COMMAND DASHBOARD_FRONTEND_LANGS="${DASHBOARD_FRONTEND_LANGS}" npm run build:localize -- ${npm_args}
   DEPENDS ${frontend_src} frontend/node_modules
   WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/pybind/mgr/dashboard/frontend
   COMMENT "dashboard frontend is being created"
   ${nodeenv}
 )
+
+add_custom_command(
+  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/frontend/package.json
+  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/frontend/package.json
+  COMMAND ${CMAKE_COMMAND} -E copy_if_different
+    ${CMAKE_CURRENT_SOURCE_DIR}/frontend/package.json
+    ${CMAKE_CURRENT_BINARY_DIR}/frontend/package.json)
+
 add_custom_target(mgr-dashboard-frontend-build
   ALL
-  DEPENDS frontend/dist mgr-dashboard-frontend-deps
+  DEPENDS
+    ${frontend_dist_dir}
+    ${CMAKE_CURRENT_BINARY_DIR}/frontend/package.json
+    mgr-dashboard-frontend-deps
   WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/pybind/mgr/dashboard/frontend)
 
 add_dependencies(tests mgr-dashboard-frontend-build)
 
+install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/frontend/dist
+  DESTINATION ${CEPH_INSTALL_DATADIR}/mgr/dashboard/frontend)
+install(FILES ${CMAKE_CURRENT_BINARY_DIR}/frontend/package.json
+  DESTINATION ${CEPH_INSTALL_DATADIR}/mgr/dashboard/frontend)
+
 if(WITH_TESTS)
   include(AddCephTest)
   add_tox_test(mgr-dashboard TOX_ENVS py3 lint check openapi-check)
index cd36ecafa7cec43f2925bb5f0e298019f7e24014..d51b9d1d04ac09bea96a3bf6b1eab71ce8952e03 100644 (file)
@@ -37,7 +37,8 @@ else:
     import logging
     logging.basicConfig(level=logging.DEBUG)
     logging.root.handlers[0].setLevel(logging.DEBUG)
-    os.environ['PATH'] = '{}:{}'.format(os.path.abspath('../../../../build/bin'),
+    build_dir = os.path.abspath('../../../../build')
+    os.environ['PATH'] = '{}:{}'.format(os.path.join(build_dir, 'bin'),
                                         os.environ['PATH'])
     import sys
 
@@ -48,7 +49,10 @@ else:
     from tests import mock  # type: ignore
 
     mgr = mock.Mock()
-    mgr.get_frontend_path.side_effect = lambda: os.path.abspath("./frontend/dist")
+    mgr.get_frontend_path.side_effect = \
+        lambda: os.path.join(build_dir,
+                             'src/pybind/mgr/dashboard',
+                             'frontend/dist')
 
 # DO NOT REMOVE: required for ceph-mgr to load a module
 from .module import Module, StandbyModule  # noqa: F401
index c950dfbcaf60613782bd08361e4600eb857f7188..802c5411137a892804d9ce3b6f73282d08860408 100644 (file)
@@ -303,7 +303,15 @@ class Module(MgrModule, CherryPyConfig):
     @classmethod
     def get_frontend_path(cls):
         current_dir = os.path.dirname(os.path.abspath(__file__))
-        return os.path.join(current_dir, 'frontend/dist')
+        path = os.path.join(current_dir, 'frontend/dist')
+        if os.path.exists(path):
+            return path
+        else:
+            path = os.path.join(current_dir,
+                                '../../../../build',
+                                'src/pybind/mgr/dashboard',
+                                'frontend/dist')
+            return os.path.abspath(path)
 
     def serve(self):