diff --git a/packages/cli/package.json b/packages/cli/package.json index 5c58c80560..3f1b485f05 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -14,6 +14,7 @@ "chalk": "^4.1.0", "commander": "^7.1.0", "docker-compose": "^0.23.6", + "envfile": "^6.14.0", "inquirer": "^8.0.0", "lookpath": "^1.1.0", "pkg": "^4.4.9", diff --git a/packages/cli/src/constants.js b/packages/cli/src/constants.js index 40c63d218d..c8da56fa5c 100644 --- a/packages/cli/src/constants.js +++ b/packages/cli/src/constants.js @@ -5,4 +5,5 @@ exports.CommandWords = { exports.InitTypes = { QUICK: "quick", + DIGITAL_OCEAN: "do", } diff --git a/packages/cli/src/hosting/index.js b/packages/cli/src/hosting/index.js index f5c2552c64..8dbccde010 100644 --- a/packages/cli/src/hosting/index.js +++ b/packages/cli/src/hosting/index.js @@ -6,6 +6,8 @@ const { confirmation } = require("../questions") const fs = require("fs") const compose = require("docker-compose") const envFile = require("./makeEnv") +const { parse } = require("envfile") +const axios = require("axios") const BUDIBASE_SERVICES = ["app-service", "worker-service"] const ERROR_FILE = "docker-error.log" @@ -13,6 +15,7 @@ const FILE_URLS = [ "https://raw.githubusercontent.com/Budibase/budibase/master/hosting/docker-compose.yaml", "https://raw.githubusercontent.com/Budibase/budibase/master/hosting/envoy.yaml", ] +const DO_USER_DATA_URL = "http://169.254.169.254/metadata/v1/user-data" async function downloadFiles() { const promises = [] @@ -50,8 +53,8 @@ async function handleError(func) { } } -async function init(info) { - const isQuick = info === InitTypes.QUICK +async function init(type) { + const isQuick = type === InitTypes.QUICK || type === InitTypes.DIGITAL_OCEAN await checkDockerConfigured() if (!isQuick) { const shouldContinue = await confirmation( @@ -64,6 +67,18 @@ async function init(info) { } await downloadFiles() const config = isQuick ? envFile.QUICK_CONFIG : {} + if (type === InitTypes.DIGITAL_OCEAN) { + try { + const response = parse(await axios.get(DO_USER_DATA_URL)) + for (let [key, value] of Object.entries(envFile.ConfigMap)) { + if (response[key]) { + config[value] = response[key] + } + } + } catch (err) { + // don't need to handle error, just don't do anything + } + } await envFile.make(config) } diff --git a/packages/cli/src/hosting/makeEnv.js b/packages/cli/src/hosting/makeEnv.js index b84e50eb6b..c8359dd5e4 100644 --- a/packages/cli/src/hosting/makeEnv.js +++ b/packages/cli/src/hosting/makeEnv.js @@ -30,6 +30,10 @@ BUDIBASE_ENVIRONMENT=PRODUCTION` } module.exports.filePath = FILE_PATH +module.exports.ConfigMap = { + HOSTING_KEY: "key", + MAIN_PORT: "port", +} module.exports.QUICK_CONFIG = { key: "budibase", port: 10000, diff --git a/packages/cli/yarn.lock b/packages/cli/yarn.lock index c03030576c..00cf6284e3 100644 --- a/packages/cli/yarn.lock +++ b/packages/cli/yarn.lock @@ -391,6 +391,11 @@ enquirer@^2.3.5: dependencies: ansi-colors "^4.1.1" +envfile@^6.14.0: + version "6.14.0" + resolved "https://registry.yarnpkg.com/envfile/-/envfile-6.14.0.tgz#8dab1ab915a4c6567087a6bb9e732b1f164c2b30" + integrity sha512-JxpcaOgJQB/x0XFmNiqFu0BLK22PlI3F0d95of5SOqcLvBlGC0ug7MHYDWnmZqmx9svIue8v0cVcqAygPTyXHQ== + escape-string-regexp@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"