From 83208355d40b85b82d47b1132b9f22ccdd559d55 Mon Sep 17 00:00:00 2001 From: Radon Rosborough Date: Thu, 18 Mar 2021 19:03:28 -0700 Subject: [PATCH] Load language configurations into server --- .dockerignore | 1 + .gitignore | 3 +++ Makefile | 6 +++--- backend/langs.js | 25 ++++++++----------------- docker/app/Dockerfile | 2 ++ tools/config.js => lib/yaml.js | 0 tools/build-lang-image.js | 12 +++++++----- tools/generate-build-script.js | 2 +- tools/make-foreach.js | 2 +- tools/plan-publish.js | 2 +- tools/write-all-build-scripts.js | 2 +- 11 files changed, 28 insertions(+), 29 deletions(-) rename tools/config.js => lib/yaml.js (100%) diff --git a/.dockerignore b/.dockerignore index 0cf0b25..782411e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -5,5 +5,6 @@ **/.lsp-repl-history **/.terraform **/build +**/build-docker **/node_modules **/out diff --git a/.gitignore b/.gitignore index 84d2869..382d670 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,8 @@ .lsp-repl-history .terraform build +# Separate directory for things that are ignored by Git but not by +# Docker. +build-docker node_modules out diff --git a/Makefile b/Makefile index 405ab6e..04b2536 100644 --- a/Makefile +++ b/Makefile @@ -256,11 +256,11 @@ publish: # Full synchronization and prod deployment ### Miscellaneous -## Run this every time you update .gitignore. +## Run this every time you update .gitignore or .dockerignore.in. -dockerignore: # Update .dockerignore from .gitignore +dockerignore: # Update .dockerignore from .gitignore and .dockerignore.in echo "# This file is generated by 'make dockerignore', do not edit." > .dockerignore - cat .gitignore | sed 's#^#**/#' >> .dockerignore + cat .gitignore | sed 's/#.*//' | grep . | sed 's#^#**/#' >> .dockerignore ## You need to be inside a 'make env' shell whenever you are running ## manual commands (Docker, Terraform, Packer, etc.) directly, as diff --git a/backend/langs.js b/backend/langs.js index c1f35d3..63716b0 100644 --- a/backend/langs.js +++ b/backend/langs.js @@ -3,6 +3,7 @@ import path from "path"; import debounce from "debounce"; +import { getLangs, readLangConfig } from "../lib/yaml.js"; import { log } from "./util.js"; // Map from language IDs to language configuration objects. This is @@ -16,24 +17,14 @@ export let aliases = {}; // global langs variable in this module. Never throw an error. If // there is a problem then just leave the languages as they previously // were. -async function readLangsFromDisk() { +async function updateLangsFromDisk() { try { const newLangs = {}; const newAliases = {}; - for (const filename of await fs.readdir("/opt/riju/langs")) { - if (path.parse(filename).ext !== ".json") { - continue; - } - const id = path.parse(filename).name; - const langConfig = JSON.parse( - await fs.readFile(`/opt/riju/langs/${filename}`, "utf-8") - ); - if (langConfig.id !== id) { - log.error( - "Language config ${filename} has mismatched language ID ${id}, ignoring" - ); - continue; - } + for (const langConfig of await Promise.all( + (await getLangs()).map(readLangConfig) + )) { + const { id } = langConfig; newLangs[id] = langConfig; newAliases[id] = id; for (const alias of langConfig.aliases || []) { @@ -52,6 +43,6 @@ async function readLangsFromDisk() { } } -export const langsPromise = readLangsFromDisk().then(() => langs); +export const langsPromise = updateLangsFromDisk().then(() => langs); -fsOrig.watch("/opt/riju/langs", debounce(readLangsFromDisk, 200)); +fsOrig.watch("langs", debounce(updateLangsFromDisk, 200)); diff --git a/docker/app/Dockerfile b/docker/app/Dockerfile index 04f0cfa..9d27f1d 100644 --- a/docker/app/Dockerfile +++ b/docker/app/Dockerfile @@ -18,7 +18,9 @@ RUN make frontend COPY frontend/pages ./frontend/pages/ COPY frontend/styles ./frontend/styles/ +COPY lib ./lib/ COPY backend ./backend/ +COPY langs ./langs/ FROM ubuntu:rolling diff --git a/tools/config.js b/lib/yaml.js similarity index 100% rename from tools/config.js rename to lib/yaml.js diff --git a/tools/build-lang-image.js b/tools/build-lang-image.js index e7c8e4b..3e5ab36 100644 --- a/tools/build-lang-image.js +++ b/tools/build-lang-image.js @@ -4,7 +4,7 @@ import http from "http"; import { Command } from "commander"; import express from "express"; -import { readLangConfig } from "./config.js"; +import { readLangConfig } from "../lib/yaml.js"; import { getLocalImageLabel } from "./docker-util.js"; import { hashDockerfile } from "./hash-dockerfile.js"; import { getDebHash, runCommand } from "./util.js"; @@ -67,7 +67,9 @@ async function main() { process.exit(0); } -main().catch((err) => { - console.error(err); - process.exit(1); -}); +if (process.argv[1] === url.fileURLToPath(import.meta.url)) { + main().catch((err) => { + console.error(err); + process.exit(1); + }); +} diff --git a/tools/generate-build-script.js b/tools/generate-build-script.js index d6dc8fc..df6dfb6 100644 --- a/tools/generate-build-script.js +++ b/tools/generate-build-script.js @@ -5,7 +5,7 @@ import url from "url"; import { Command } from "commander"; import YAML from "yaml"; -import { readLangConfig, readSharedDepConfig } from "./config.js"; +import { readLangConfig, readSharedDepConfig } from "../lib/yaml.js"; // Given a language config object, return the text of a Bash script // that will build the (unpacked) riju-lang-foo Debian package into diff --git a/tools/make-foreach.js b/tools/make-foreach.js index c52a7ac..56fc1e5 100644 --- a/tools/make-foreach.js +++ b/tools/make-foreach.js @@ -1,7 +1,7 @@ import process from "process"; import url from "url"; -import { getPackages } from "./config.js"; +import { getPackages } from "../lib/yaml.js"; import { runCommand } from "./util.js"; // Parse command-line arguments, run main functionality, and exit. diff --git a/tools/plan-publish.js b/tools/plan-publish.js index d8149c6..fa389dc 100644 --- a/tools/plan-publish.js +++ b/tools/plan-publish.js @@ -7,7 +7,7 @@ import { Command } from "commander"; import _ from "lodash"; import { v4 as getUUID } from "uuid"; -import { getLangs, getPackages, readLangConfig } from "./config.js"; +import { getLangs, getPackages, readLangConfig } from "../lib/yaml.js"; import { getLocalImageDigest, getLocalImageLabel, diff --git a/tools/write-all-build-scripts.js b/tools/write-all-build-scripts.js index 14573f1..06868b5 100644 --- a/tools/write-all-build-scripts.js +++ b/tools/write-all-build-scripts.js @@ -7,7 +7,7 @@ import nodePath from "path"; import process from "process"; import url from "url"; -import { getPackages } from "./config.js"; +import { getPackages } from "../lib/yaml.js"; import { generateBuildScript } from "./generate-build-script.js"; // Parse command-line arguments, run main functionality, and exit.