2021-03-23 06:05:00 +13:00
|
|
|
#!/usr/bin/env node
|
|
|
|
const compose = require("docker-compose")
|
|
|
|
const path = require("path")
|
2021-03-24 04:37:11 +13:00
|
|
|
const fs = require("fs")
|
2021-04-08 04:13:19 +12:00
|
|
|
const { processStringSync } = require("@budibase/string-templates")
|
|
|
|
|
|
|
|
function isLinux() {
|
|
|
|
return process.platform !== "darwin" && process.platform !== "win32"
|
|
|
|
}
|
2021-03-23 06:05:00 +13: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",
|
|
|
|
}
|
|
|
|
|
2021-03-24 04:37:11 +13:00
|
|
|
async function init() {
|
2021-04-08 04:13:19 +12:00
|
|
|
// generate envoy file, always do this incase it has changed
|
|
|
|
const hostingPath = path.join(process.cwd(), "..", "..", "hosting")
|
|
|
|
const envoyHbsPath = path.join(hostingPath, "envoy.dev.yaml.hbs")
|
2021-04-08 04:52:22 +12:00
|
|
|
const envoyOutputPath = path.join(hostingPath, ".generated-envoy.dev.yaml")
|
2021-04-08 04:13:19 +12:00
|
|
|
const contents = fs.readFileSync(envoyHbsPath, "utf8")
|
|
|
|
const config = {
|
|
|
|
address: isLinux() ? "172.17.0.1" : "host.docker.internal",
|
2021-03-24 04:37:11 +13:00
|
|
|
}
|
2021-04-08 04:13:19 +12:00
|
|
|
fs.writeFileSync(envoyOutputPath, processStringSync(contents, config))
|
|
|
|
|
2021-03-24 04:37:11 +13:00
|
|
|
const envFilePath = path.join(process.cwd(), ".env")
|
|
|
|
const envFileJson = {
|
|
|
|
PORT: 4001,
|
|
|
|
MINIO_URL: "http://localhost:10000/",
|
|
|
|
COUCH_DB_URL: "http://budibase:budibase@localhost:10000/db/",
|
2021-05-01 02:44:37 +12:00
|
|
|
REDIS_URL: "http://localhost:10000/cache/",
|
2021-03-24 04:37:11 +13:00
|
|
|
WORKER_URL: "http://localhost:4002",
|
|
|
|
JWT_SECRET: "testsecret",
|
|
|
|
MINIO_ACCESS_KEY: "budibase",
|
|
|
|
MINIO_SECRET_KEY: "budibase",
|
|
|
|
COUCH_DB_PASSWORD: "budibase",
|
|
|
|
COUCH_DB_USER: "budibase",
|
|
|
|
SELF_HOSTED: 1,
|
|
|
|
}
|
|
|
|
let envFile = ""
|
|
|
|
Object.keys(envFileJson).forEach(key => {
|
|
|
|
envFile += `${key}=${envFileJson[key]}\n`
|
|
|
|
})
|
|
|
|
fs.writeFileSync(envFilePath, envFile)
|
|
|
|
}
|
|
|
|
|
2021-03-23 06:05:00 +13:00
|
|
|
async function up() {
|
|
|
|
console.log("Spinning up your budibase dev environment... 🔧✨")
|
2021-03-24 04:37:11 +13:00
|
|
|
await init()
|
2021-03-24 00:01:33 +13:00
|
|
|
await compose.upAll(CONFIG)
|
2021-03-23 06:05:00 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
async function down() {
|
|
|
|
console.log("Spinning down your budibase dev environment... 🌇")
|
2021-03-24 00:01:33 +13:00
|
|
|
await compose.stop(CONFIG)
|
2021-03-23 06:05:00 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
async function nuke() {
|
|
|
|
console.log(
|
|
|
|
"Clearing down your budibase dev environment, including all containers and volumes... 💥"
|
|
|
|
)
|
2021-04-09 05:18:53 +12:00
|
|
|
await compose.down({
|
|
|
|
...CONFIG,
|
|
|
|
// stop containers, delete volumes
|
|
|
|
commandOptions: ["-v", "--remove-orphans"],
|
|
|
|
})
|
2021-03-23 06:05:00 +13:00
|
|
|
}
|
|
|
|
|
2021-03-23 08:35:29 +13:00
|
|
|
const managementCommand = process.argv.slice(2)[0]
|
|
|
|
|
|
|
|
if (
|
|
|
|
!managementCommand ||
|
|
|
|
!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-23 06:05:00 +13: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-03-24 00:01:33 +13:00
|
|
|
.catch(err => {
|
|
|
|
console.error(
|
|
|
|
"Something went wrong while managing budibase dev environment:",
|
|
|
|
err.message
|
|
|
|
)
|
2021-03-23 06:05:00 +13:00
|
|
|
})
|