Updating for consistent use of chalk and logs, as well as adding an option to update docker-compose and envoy files.

This commit is contained in:
mike12345567 2021-02-26 17:08:28 +00:00
parent b22684163c
commit 3669e59bc3
5 changed files with 37 additions and 19 deletions

View File

@ -3,3 +3,4 @@ docker-compose.yaml
envoy.yaml envoy.yaml
hosting.properties hosting.properties
build/ build/
docker-error.log

View File

@ -1,12 +1,11 @@
const Command = require("../structures/Command") const Command = require("../structures/Command")
const { CommandWords } = require("../constants") const { CommandWords } = require("../constants")
const { lookpath } = require("lookpath") const { lookpath } = require("lookpath")
const { downloadFile, logErrorToFile } = require("../utils") const { downloadFile, logErrorToFile, success, info } = require("../utils")
const { confirmation } = require("../questions") const { confirmation } = require("../questions")
const fs = require("fs") const fs = require("fs")
const compose = require("docker-compose") const compose = require("docker-compose")
const envFile = require("./makeEnv") const envFile = require("./makeEnv")
const chalk = require("chalk")
const BUDIBASE_SERVICES = ["app-service", "worker-service"] const BUDIBASE_SERVICES = ["app-service", "worker-service"]
const ERROR_FILE = "docker-error.log" const ERROR_FILE = "docker-error.log"
@ -15,6 +14,15 @@ const FILE_URLS = [
"https://raw.githubusercontent.com/Budibase/budibase/master/hosting/envoy.yaml" "https://raw.githubusercontent.com/Budibase/budibase/master/hosting/envoy.yaml"
] ]
async function downloadFiles() {
const promises = []
for (let url of FILE_URLS) {
const fileName = url.split("/").slice(-1)[0]
promises.push(downloadFile(url, `./${fileName}`))
}
await Promise.all(promises)
}
async function checkDockerConfigured() { async function checkDockerConfigured() {
const error = "docker/docker-compose has not been installed, please follow instructions at: https://docs.budibase.com/self-hosting/hosting-methods/docker-compose#installing-docker" const error = "docker/docker-compose has not been installed, please follow instructions at: https://docs.budibase.com/self-hosting/hosting-methods/docker-compose#installing-docker"
const docker = await lookpath("docker") const docker = await lookpath("docker")
@ -48,28 +56,25 @@ async function init() {
console.log("Stopping.") console.log("Stopping.")
return return
} }
const promises = [] await downloadFiles()
for (let url of FILE_URLS) {
const fileName = url.split("/").slice(-1)[0]
promises.push(downloadFile(url, `./${fileName}`))
}
await Promise.all(promises)
await envFile.make() await envFile.make()
} }
async function start() { async function start() {
await checkDockerConfigured() await checkDockerConfigured()
checkInitComplete() checkInitComplete()
console.log(info("Starting services, this may take a moment."))
const port = envFile.get("MAIN_PORT") const port = envFile.get("MAIN_PORT")
await handleError(async () => { await handleError(async () => {
await compose.upAll({cwd: "./", log: false}) await compose.upAll({cwd: "./", log: false})
}) })
console.log(chalk.green(`Services started, please go to http://localhost:${port} for next steps.`)) console.log(success(`Services started, please go to http://localhost:${port} for next steps.`))
} }
async function status() { async function status() {
await checkDockerConfigured() await checkDockerConfigured()
checkInitComplete() checkInitComplete()
console.log(info("Budibase status"))
await handleError(async () => { await handleError(async () => {
const response = await compose.ps() const response = await compose.ps()
console.log(response.out) console.log(response.out)
@ -79,23 +84,31 @@ async function status() {
async function stop() { async function stop() {
await checkDockerConfigured() await checkDockerConfigured()
checkInitComplete() checkInitComplete()
console.log(info("Stopping services, this may take a moment."))
await handleError(async () => { await handleError(async () => {
await compose.stop() await compose.stop()
}) })
console.log(success("Services have been stopped successfully."))
} }
async function update() { async function update() {
await checkDockerConfigured() await checkDockerConfigured()
checkInitComplete() checkInitComplete()
if (await confirmation("Do you wish to update you docker-compose.yaml and envoy.yaml?")) {
await downloadFiles()
}
await handleError(async () => { await handleError(async () => {
const status = await compose.ps() const status = await compose.ps()
const parts = status.out.split("\n") const parts = status.out.split("\n")
const isUp = parts[2] && parts[2].indexOf("Up") !== -1 const isUp = parts[2] && parts[2].indexOf("Up") !== -1
await compose.stop() if (isUp) {
console.log(chalk.cyan("Beginning update, this may take a few minutes.")) console.log(info("Stopping services, this may take a moment."))
await compose.stop()
}
console.log(info("Beginning update, this may take a few minutes."))
await compose.pullMany(BUDIBASE_SERVICES, {log: true}) await compose.pullMany(BUDIBASE_SERVICES, {log: true})
if (isUp) { if (isUp) {
console.log(chalk.green("Update complete, restarting services...")) console.log(success("Update complete, restarting services..."))
await start() await start()
} }
}) })

View File

@ -1,5 +1,5 @@
const { string, number } = require("../questions") const { string, number } = require("../questions")
const { getSuccess } = require("../utils") const { success } = require("../utils")
const fs = require("fs") const fs = require("fs")
const path = require("path") const path = require("path")
const randomString = require("randomstring") const randomString = require("randomstring")
@ -36,7 +36,7 @@ module.exports.make = async () => {
const hostingPort = await number("Please enter the port on which you want your installation to run: ", 10000) const hostingPort = await number("Please enter the port on which you want your installation to run: ", 10000)
const fileContents = getContents(hostingPort, hostingKey) const fileContents = getContents(hostingPort, hostingKey)
fs.writeFileSync(FILE_PATH, fileContents) fs.writeFileSync(FILE_PATH, fileContents)
console.log(getSuccess("Configuration has been written successfully - please check .env file for more details.")) console.log(success("Configuration has been written successfully - please check .env file for more details."))
} }
module.exports.get = property => { module.exports.get = property => {

View File

@ -1,7 +1,7 @@
const { const {
getSubHelpDescription, getSubHelpDescription,
getHelpDescription, getHelpDescription,
getError, error,
} = require("../utils") } = require("../utils")
class Command { class Command {
@ -52,11 +52,11 @@ class Command {
} }
} }
if (!executed) { if (!executed) {
console.log(getError(`Unknown ${this.command} option.`)) console.log(error(`Unknown ${this.command} option.`))
command.help() command.help()
} }
} catch (err) { } catch (err) {
console.log(getError(err)) console.log(error(err))
} }
}) })
} }

View File

@ -29,14 +29,18 @@ exports.getSubHelpDescription = string => {
return chalk.green(string) return chalk.green(string)
} }
exports.getError = error => { exports.error = error => {
return chalk.red(`Error - ${error}`) return chalk.red(`Error - ${error}`)
} }
exports.getSuccess = success => { exports.success = success => {
return chalk.green(success) return chalk.green(success)
} }
exports.info = info => {
return chalk.cyan(info)
}
exports.logErrorToFile = (file, error) => { exports.logErrorToFile = (file, error) => {
fs.writeFileSync(path.resolve(`./${file}`), `Budiase Error\n${error}`) fs.writeFileSync(path.resolve(`./${file}`), `Budiase Error\n${error}`)
} }