Skip to content

Instantly share code, notes, and snippets.

@aimed
Last active May 10, 2023 12:12
Show Gist options
  • Save aimed/4228b86740a35a0ee55cf6494694d3a5 to your computer and use it in GitHub Desktop.
Save aimed/4228b86740a35a0ee55cf6494694d3a5 to your computer and use it in GitHub Desktop.
copy-sandbox-env

copy-sandbox-env

Run using

npx GIST_URL ./deploy/PROJECT_NAME
#!/usr/bin/env node
// @ts-check
const fs = require('fs')
const path = require('path')
const yaml = require('yaml')
const dotenv = require('dotenv')
const { execSync } = require('child_process')
const dotEnvStringify = require('dotenv-stringify')
const deploymentPath = process.argv[process.argv.length - 1]
try {
copySandboxEnv()
console.log('Copied sandbox env 💿')
} catch (error) {
console.error('🚨', error.message)
process.exit(1)
}
function copySandboxEnv() {
const existing = readDotEnv('.env')
const values = readYaml('values.yaml')['forto-app'].app.env
const sandbox = readYaml('values.sandbox.yaml')['forto-app'].app.env
const secrets = readSops('secrets.sandbox.yaml')['forto-app'].app.secretenv
const merged = { ...values, ...sandbox, ...secrets, ...existing }
const env = replaceClusterURLs(merged)
writeEnv('.env', env)
}
function readDotEnv(path) {
return fs.existsSync(path) ? dotenv.parse(fs.readFileSync(path).toString()) : {}
}
/**
* @param {string} path
* @param {Record<string, any>} env
*/
function writeEnv(path, env) {
const envString = dotEnvStringify(env)
fs.writeFileSync(path, envString)
}
/**
* @param {string} file
* @returns {Record<string, any>} parsed content
*/
function readYaml(file) {
return yaml.parse(fs.readFileSync(inDeploymentFolder(file)).toString())
}
function inDeploymentFolder(file) {
return path.resolve(deploymentPath, file)
}
function readSops(file) {
try {
execSync('saml2aws login', { stdio: 'pipe' })
} catch (error) {
throw new Error('Login using "saml2aws login" first')
}
const decrypted = execSync(`sops -d ${inDeploymentFolder(file)}`, {
env: { AWS_PROFILE: 'sandbox-Developer', ...process.env },
})
return yaml.parse(decrypted.toString())
}
/**
* @param {Record<string, any>} params
* @returns {Record<string, string>}
*/
function replaceClusterURLs(params) {
return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, replaceClusterURL(value.toString())]))
}
/**
* @param {string} value
* @returns string
*/
function replaceClusterURL(value) {
if (value.endsWith('.api.svc.cluster.local.')) {
return value.toString().replace('http://', 'https://').replace('.api.svc.cluster.local.', '.forto.io')
}
return value
}
{
"version": "0.1.0",
"license": "MIT",
"main": "./index.js",
"bin": "./index.js",
"files": [
"./index.js"
],
"engines": {
"node": ">=10"
},
"scripts": {
"start": "node index.js"
},
"peerDependencies": {},
"prettier": {
"printWidth": 80,
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
},
"name": "copy-sandbox-env",
"author": "Maximilian Taeschner",
"devDependencies": {},
"dependencies": {
"dotenv": "^10.0.0",
"dotenv-stringify": "^2.0.6",
"yaml": "^1.10.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment