Adding in single compose configuration to CLI.

This commit is contained in:
Michael Drury 2022-09-28 00:04:26 +01:00
parent ba303bf758
commit b66df050dc
4 changed files with 149 additions and 2284 deletions

View File

@ -13,7 +13,7 @@ const {
const { confirmation } = require("../questions")
const fs = require("fs")
const compose = require("docker-compose")
const makeEnv = require("./makeEnv")
const makeFiles = require("./makeFiles")
const axios = require("axios")
const { captureEvent } = require("../events")
const yaml = require("yaml")
@ -45,7 +45,7 @@ async function checkDockerConfigured() {
}
function checkInitComplete() {
if (!fs.existsSync(makeEnv.filePath)) {
if (!fs.existsSync(makeFiles.filePath)) {
throw "Please run the hosting --init command before any other hosting command."
}
}
@ -77,12 +77,12 @@ async function init(type) {
type,
})
await downloadFiles()
const config = isQuick ? makeEnv.QUICK_CONFIG : {}
const config = isQuick ? makeFiles.QUICK_CONFIG : {}
if (type === InitTypes.DIGITAL_OCEAN) {
try {
const output = await axios.get(DO_USER_DATA_URL)
const response = parseEnv(output.data)
for (let [key, value] of Object.entries(makeEnv.ConfigMap)) {
for (let [key, value] of Object.entries(makeFiles.ConfigMap)) {
if (response[key]) {
config[value] = response[key]
}
@ -91,7 +91,7 @@ async function init(type) {
// don't need to handle error, just don't do anything
}
}
await makeEnv.make(config)
await makeFiles.makeEnv(config)
}
async function start() {
@ -102,7 +102,7 @@ async function start() {
"Starting services, this may take a moment - first time this may take a few minutes to download images."
)
)
const port = makeEnv.get("MAIN_PORT")
const port = makeFiles.getEnvProperty("MAIN_PORT")
await handleError(async () => {
// need to log as it makes it more clear
await compose.upAll({ cwd: "./", log: true })
@ -177,9 +177,12 @@ async function watchPlugins(pluginPath) {
}
const dockerYaml = fs.readFileSync(dockerFilePath, "utf8")
const parsedYaml = yaml.parse(dockerYaml)
let service
if (parsedYaml["services"]["app-service"]) {
service = parsedYaml["services"]["app-service"]
let service,
serviceList = Object.keys(parsedYaml.services)
if (parsedYaml.services["app-service"]) {
service = parsedYaml.services["app-service"]
} else if (serviceList.length === 1) {
service = parsedYaml.services[serviceList[0]]
}
if (!service) {
console.log(

View File

@ -1,66 +0,0 @@
const { number } = require("../questions")
const { success } = require("../utils")
const fs = require("fs")
const path = require("path")
const randomString = require("randomstring")
const FILE_PATH = path.resolve("./.env")
function getContents(port) {
return `
# Use the main port in the builder for your self hosting URL, e.g. localhost:10000
MAIN_PORT=${port}
# This section contains all secrets pertaining to the system
JWT_SECRET=${randomString.generate()}
MINIO_ACCESS_KEY=${randomString.generate()}
MINIO_SECRET_KEY=${randomString.generate()}
COUCH_DB_PASSWORD=${randomString.generate()}
COUCH_DB_USER=${randomString.generate()}
REDIS_PASSWORD=${randomString.generate()}
INTERNAL_API_KEY=${randomString.generate()}
# This section contains variables that do not need to be altered under normal circumstances
APP_PORT=4002
WORKER_PORT=4003
MINIO_PORT=4004
COUCH_DB_PORT=4005
REDIS_PORT=6379
WATCHTOWER_PORT=6161
BUDIBASE_ENVIRONMENT=PRODUCTION`
}
module.exports.filePath = FILE_PATH
module.exports.ConfigMap = {
MAIN_PORT: "port",
}
module.exports.QUICK_CONFIG = {
key: "budibase",
port: 10000,
}
module.exports.make = async (inputs = {}) => {
const hostingPort =
inputs.port ||
(await number(
"Please enter the port on which you want your installation to run: ",
10000
))
const fileContents = getContents(hostingPort)
fs.writeFileSync(FILE_PATH, fileContents)
console.log(
success(
"Configuration has been written successfully - please check .env file for more details."
)
)
}
module.exports.get = property => {
const props = fs.readFileSync(FILE_PATH, "utf8").split(property)
if (props[0].charAt(0) === "=") {
property = props[0]
} else {
property = props[1]
}
return property.split("=")[1].split("\n")[0]
}

View File

@ -0,0 +1,107 @@
const { number } = require("../questions")
const { success } = require("../utils")
const fs = require("fs")
const path = require("path")
const randomString = require("randomstring")
const yaml = require("yaml")
const SINGLE_IMAGE = "budibase/budibase:latest"
const VOL_NAME = "budibase_data"
const COMPOSE_PATH = path.resolve("./docker-compose.yaml")
const ENV_PATH = path.resolve("./.env")
function getSingleCompose(port) {
const singleComposeObj = {
version: "3",
services: {
budibase: {
restart: "unless-stopped",
image: SINGLE_IMAGE,
ports: [`${port}:80`],
environment: {
JWT_SECRET: randomString.generate(),
MINIO_ACCESS_KEY: randomString.generate(),
MINIO_SECRET_KEY: randomString.generate(),
REDIS_PASSWORD: randomString.generate(),
COUCHDB_USER: "admin",
COUCH_DB_PASSWORD: randomString.generate(),
INTERNAL_API_KEY: randomString.generate(),
},
volumes: [`${VOL_NAME}:/data`],
},
},
volumes: {
[VOL_NAME]: {
driver: "local",
},
},
}
return yaml.stringify(singleComposeObj)
}
function getEnv(port) {
return `
# Use the main port in the builder for your self hosting URL, e.g. localhost:10000
MAIN_PORT=${port}
# This section contains all secrets pertaining to the system
JWT_SECRET=${randomString.generate()}
MINIO_ACCESS_KEY=${randomString.generate()}
MINIO_SECRET_KEY=${randomString.generate()}
COUCH_DB_PASSWORD=${randomString.generate()}
COUCH_DB_USER=${randomString.generate()}
REDIS_PASSWORD=${randomString.generate()}
INTERNAL_API_KEY=${randomString.generate()}
# This section contains variables that do not need to be altered under normal circumstances
APP_PORT=4002
WORKER_PORT=4003
MINIO_PORT=4004
COUCH_DB_PORT=4005
REDIS_PORT=6379
WATCHTOWER_PORT=6161
BUDIBASE_ENVIRONMENT=PRODUCTION`
}
module.exports.filePath = ENV_PATH
module.exports.ConfigMap = {
MAIN_PORT: "port",
}
module.exports.QUICK_CONFIG = {
key: "budibase",
port: 10000,
}
async function make(path, contentsFn, inputs = {}) {
const port =
inputs.port ||
(await number(
"Please enter the port on which you want your installation to run: ",
10000
))
const fileContents = contentsFn(port)
fs.writeFileSync(path, fileContents)
console.log(
success(
`Configuration has been written successfully - please check ${path} for more details.`
)
)
}
module.exports.makeEnv = async (inputs = {}) => {
return make(ENV_PATH, getEnv, inputs)
}
module.exports.makeSingleCompose = async (inputs = {}) => {
return make(COMPOSE_PATH, getSingleCompose, inputs)
}
module.exports.getEnvProperty = property => {
const props = fs.readFileSync(ENV_PATH, "utf8").split(property)
if (props[0].charAt(0) === "=") {
property = props[0]
} else {
property = props[1]
}
return property.split("=")[1].split("\n")[0]
}

File diff suppressed because it is too large Load Diff