Skip to content

Commit

Permalink
Merge pull request #2977 from mitre/configure-magma-env-file
Browse files Browse the repository at this point in the history
Have server.py automatically configure Magma .env when build flag set
  • Loading branch information
elegantmoose committed May 15, 2024
2 parents 31318e8 + 9d071c9 commit 4f77a69
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
from app.utility.config_generator import ensure_local_config


MAGMA_PATH = "./plugins/magma"


def setup_logger(level=logging.DEBUG):
format = "%(message)s"
datefmt = "%Y-%m-%d %H:%M:%S"
Expand Down Expand Up @@ -139,11 +142,19 @@ async def enable_cors(request, response):

async def start_vue_dev_server():
await asyncio.create_subprocess_shell(
"npm run dev", stdout=sys.stdout, stderr=sys.stderr, cwd="./plugins/magma/"
"npm run dev", stdout=sys.stdout, stderr=sys.stderr, cwd=MAGMA_PATH
)
logging.info("VueJS development server is live.")


def configure_magma_env_file():
logging.info("Setting VueJS environment file.")
host = BaseWorld.get_config("host")
port = BaseWorld.get_config("port")
with open(f"{MAGMA_PATH}/.env", "w") as fp:
fp.write(f"VITE_CALDERA_URL=http://{host}:{port}")


def _get_parser():

def list_str(values):
Expand Down Expand Up @@ -251,19 +262,26 @@ def list_str(values):
app_svc.register_subapp("/api/v2", app.api.v2.make_app(app_svc.get_services()))
init_swagger_documentation(app_svc.application)
if args.uiDevHost:
if not os.path.exists("./plugins/magma/dist"):
if not os.path.exists(f"{MAGMA_PATH}/dist"):
logging.info("Building VueJS front-end.")
subprocess.run(["npm", "run", "build"], cwd="plugins/magma", check=True)
subprocess.run(["npm", "run", "build"], cwd=MAGMA_PATH, check=True)
logging.info("VueJS front-end build complete.")
app_svc.application.on_response_prepare.append(enable_cors)

if args.build:
logging.info("Building VueJS front-end.")
subprocess.run(["npm", "install"], cwd="plugins/magma", check=True)
subprocess.run(["npm", "run", "build"], cwd="plugins/magma", check=True)
logging.info("VueJS front-end build complete.")
if len(os.listdir(MAGMA_PATH)) > 0:
configure_magma_env_file()
logging.info("Building VueJS front-end.")
subprocess.run(["npm", "install"], cwd=MAGMA_PATH, check=True)
subprocess.run(["npm", "run", "build"], cwd=MAGMA_PATH, check=True)
logging.info("VueJS front-end build complete.")
else:
logging.warning(
f"[bright_yellow]The `--build` flag was supplied, but the Caldera v5 Vue UI is not present."
f" The Vue UI should be located in {MAGMA_PATH}. Use `--recursive` when cloning Caldera.[/bright_yellow]"
)
else:
if not os.path.exists("./plugins/magma/dist"):
if not os.path.exists(f"{MAGMA_PATH}/dist"):
logging.warning(
"[bright_yellow]Built Caldera v5 Vue components not detected, and `--build` flag not supplied."
" If attempting to start Caldera v5 for the first time, the `--build` flag must be"
Expand Down

0 comments on commit 4f77a69

Please sign in to comment.