budibase/packages/server/scripts/dev/manage.js

133 lines
3.6 KiB
JavaScript
Raw Normal View History

2021-03-22 18:05:00 +01:00
#!/usr/bin/env node
const compose = require("docker-compose")
const path = require("path")
const fs = require("fs")
const isWsl = require("is-wsl")
const { processStringSync } = require("@budibase/string-templates")
function isLinux() {
return !isWsl && process.platform !== "darwin" && process.platform !== "win32"
}
2021-03-22 18:05:00 +01:00
// This script wraps docker-compose allowing you to manage your dev infrastructure with simple commands.
const CONFIG = {
cwd: path.resolve(process.cwd(), "../../hosting"),
config: "docker-compose.dev.yaml",
log: true,
}
const Commands = {
Up: "up",
Down: "down",
Nuke: "nuke",
}
async function init() {
2022-02-01 11:02:37 +01:00
// generate nginx file, always do this incase it has changed
const hostingPath = path.join(process.cwd(), "..", "..", "hosting")
2022-02-01 11:02:37 +01:00
const nginxHbsPath = path.join(hostingPath, "nginx.dev.conf.hbs")
const nginxOutputPath = path.join(hostingPath, ".generated-nginx.dev.conf")
const contents = fs.readFileSync(nginxHbsPath, "utf8")
const config = {
address: isLinux() ? "172.17.0.1" : "host.docker.internal",
}
2022-02-01 11:02:37 +01:00
fs.writeFileSync(nginxOutputPath, processStringSync(contents, config))
const envFilePath = path.join(process.cwd(), ".env")
if (!fs.existsSync(envFilePath)) {
const envFileJson = {
PORT: 4001,
MINIO_URL: "http://localhost:4004",
2022-02-18 18:36:23 +01:00
COUCH_DB_URL: "http://budibase:budibase@localhost:4005",
REDIS_URL: "localhost:6379",
WORKER_URL: "http://localhost:4002",
INTERNAL_API_KEY: "budibase",
2022-03-03 14:37:04 +01:00
ACCOUNT_PORTAL_URL: "http://localhost:10001",
ACCOUNT_PORTAL_API_KEY: "budibase",
JWT_SECRET: "testsecret",
REDIS_PASSWORD: "budibase",
MINIO_ACCESS_KEY: "budibase",
MINIO_SECRET_KEY: "budibase",
COUCH_DB_PASSWORD: "budibase",
COUCH_DB_USER: "budibase",
SELF_HOSTED: 1,
DISABLE_ACCOUNT_PORTAL: "",
MULTI_TENANCY: "",
DISABLE_THREADING: 1,
2022-09-28 09:56:45 +02:00
SERVICE: "app-service",
DEPLOYMENT_ENVIRONMENT: "development",
BB_ADMIN_USER_EMAIL: "",
BB_ADMIN_USER_PASSWORD: "",
PLUGINS_DIR: "",
TENANT_FEATURE_FLAGS: "*:LICENSING,*:USER_GROUPS",
}
let envFile = ""
Object.keys(envFileJson).forEach(key => {
envFile += `${key}=${envFileJson[key]}\n`
})
fs.writeFileSync(envFilePath, envFile)
}
}
2021-03-22 18:05:00 +01:00
async function up() {
console.log("Spinning up your budibase dev environment... 🔧✨")
await init()
2021-03-23 12:01:33 +01:00
await compose.upAll(CONFIG)
2022-09-28 09:56:45 +02:00
// We always ensure to restart the proxy service in case of nginx conf changes
await compose.restartOne("proxy-service", CONFIG)
2021-03-22 18:05:00 +01:00
}
async function down() {
console.log("Spinning down your budibase dev environment... 🌇")
2021-03-23 12:01:33 +01:00
await compose.stop(CONFIG)
2021-03-22 18:05:00 +01:00
}
async function nuke() {
console.log(
"Clearing down your budibase dev environment, including all containers and volumes... 💥"
)
await compose.down({
...CONFIG,
// stop containers, delete volumes
commandOptions: ["-v", "--remove-orphans"],
})
2021-03-22 18:05:00 +01:00
}
const managementCommand = process.argv.slice(2)[0]
if (
!managementCommand ||
2021-05-04 12:32:22 +02:00
!Object.values(Commands).some(command => managementCommand === command)
) {
throw new Error(
"You must supply either an 'up', 'down' or 'nuke' commmand to manage the budibase development environment."
)
}
2021-03-22 18:05:00 +01:00
let command
switch (managementCommand) {
case Commands.Up:
command = up
break
case Commands.Down:
command = down
break
case Commands.Nuke:
command = nuke
break
default:
command = up
}
command()
.then(() => {
console.log("Done! 🎉")
})
2021-05-04 12:32:22 +02:00
.catch(err => {
2021-03-23 12:01:33 +01:00
console.error(
"Something went wrong while managing budibase dev environment:",
err.message
)
2021-03-22 18:05:00 +01:00
})