Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
cwdcwd committed Jan 10, 2022
2 parents 41b0e26 + 9dce3f6 commit 206fb83
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 24 deletions.
21 changes: 0 additions & 21 deletions frontend.dev

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@opentorc/config-wrapper",
"version": "1.1.3",
"version": "1.2.0",
"description": "config management wrapper",
"main": "./src/index.js",
"bin": {
Expand Down
64 changes: 63 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const fs = require('fs/promises')
const arg = require('arg')
const inquirer = require('inquirer')
const chalk = require('chalk')

let pkg = require('../package.json');
const configWrapper = require('./index')
const configWrapper = require('./index');
const { mkdir } = require('fs');


function parseArgumentsIntoOptions(rawArgs) {
Expand All @@ -13,13 +15,15 @@ function parseArgumentsIntoOptions(rawArgs) {
'--oldprefix': String,
'--newprefix': String,
'--env': String,
'--folder': String,
'--service': String,
'--overwrite': Boolean,
'--encrypt': Boolean,
'--help': Boolean,
'-o': '--outfile',
'-i': '--infile',
'-e': '--env',
'-f': '--folder',
'-s': '--service',
'-h': '--help'
},{
Expand All @@ -37,6 +41,7 @@ function parseArgumentsIntoOptions(rawArgs) {
overwrite: args['--overwrite'] || false,
encrypt: args['--encrypt'] || false,
env: args['--env'],
folder: args['--folder'] || '',
help: args['--help'] || false,
command: args._[0] || ''
}
Expand Down Expand Up @@ -64,6 +69,9 @@ function displayHelp() {
{bold.blue * --service} aws application service
{bold.blue * --overwrite} optional flag to overwrite existing parameters
{bold.blue * --encrypt} optional flag to encrypt the parameters
{underline.green exportAllParams:} export all parameters from AWS Parameter Store to hierachical folders
{bold.blue * --folder} folder to save parameters to
{bold.blue * --env} optional aws application environment
`)
}

Expand Down Expand Up @@ -107,6 +115,48 @@ async function putToAWSFromFile(config) {
const results = await configWrapper.awsManager.setParametersByService(params, env, service)
console.log(chalk.green(`Saved ${results.length} parameters to AWS for "/torc/${env}/${service}"`))
}
async function exportAllParams(config) {
// console.log(config)
const rootFolder = config?.folder || './params'
await fs.mkdir(rootFolder, { recursive: true })
let params = {}
const allParams = await configWrapper.awsManager.getAllOrgParams(true)

if (config?.env) {
Object.keys(allParams).forEach((key) => {
if (key === config.env) {
console.log(`adding params for: ${key}`)
params[key] = allParams[key]
}
})
} else {
params = allParams
}

// console.log(params)
const envs = Object.keys(params)

for (let i = 0; i < envs.length; ++i) {
const env = envs[i]
await fs.mkdir(`${rootFolder}/${env}`)
const services = Object.keys(params[env])

for (let j = 0; j < services.length; ++j) {
const service = services[j]
const aEnvVars = []
const vars = Object.keys(params[env][service])

for (let k = 0; k < vars.length; ++k) {
const key = vars[k]
const param = params[env][service][key]
aEnvVars.push(`${key}=${param.value}`)
}

console.log(`writing ${aEnvVars.length} params for ${env}/${service} to ${rootFolder}/${env}/${service}.env`)
await fs.writeFile(`${rootFolder}/${env}/${service}.env`, aEnvVars.join('\n'))
}
}
}

async function promptForMissingOptions(options) {
let commandFunc = null
Expand Down Expand Up @@ -205,6 +255,18 @@ async function promptForMissingOptions(options) {
}
break
}
case 'exportAllParams': {
commandFunc = exportAllParams
if (!options.folder) {
questions.push({
type: 'input',
name: 'folder',
message: 'Folder: ',
default: './params',
})
}
break
}
default: {
commandFunc = displayHelp
return { commandFunc }
Expand Down
99 changes: 98 additions & 1 deletion src/lib/awsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,107 @@ async function setParametersByService(params, env, service) {
return data
}

async function getEnvironments() {
console.log(`Getting environments descending from ${BASE_PATH}`)
var config = {
Path: BASE_PATH,
Recursive: true
};

const envs = {}
let nextToken = null

do {
let params = await ssm.getParametersByPath(config).promise()

for (let i = 0; i < params.Parameters.length; i++) {
const param = params.Parameters[i]
const name = param.Name.split('/')
const env = name[2]
envs[env] = envs[env]? envs[env]+1 : 1
}

params = await ssm.getParametersByPath(config).promise()
nextToken = params.NextToken
config.NextToken = nextToken
} while (nextToken)

return envs
}

async function getServicesForEnvironment(env) {
const Path = BASE_PATH + '/' + env
console.log(`Getting services descending from the environment ${Path}`)
var config = {
Path,
Recursive: true
};

const svcs = {}
let nextToken = null

do {
let params = await ssm.getParametersByPath(config).promise()

for (let i = 0; i < params.Parameters.length; i++) {
const param = params.Parameters[i]
const name = param.Name.split('/')
const svc = name[3]
svcs[svc] = svcs[svc] ? svcs[svc] + 1 : 1
}

params = await ssm.getParametersByPath(config).promise()
nextToken = params.NextToken
config.NextToken = nextToken
} while (nextToken)

return svcs
}

async function getAllOrgParams(isEncrypted) {
console.log(`Getting all parameters under ${BASE_PATH}`)
var config = {
Path: BASE_PATH,
Recursive: true,
WithDecryption: isEncrypted
};

const convertedParams = {}
let nextToken = null

do {
let params = await ssm.getParametersByPath(config).promise()

for (let i = 0; i < params.Parameters.length; i++) {
const param = restructureParam(params.Parameters[i])
const name = params.Parameters[i].Name.split('/')

if (!convertedParams[name[2]]) {
convertedParams[name[2]] = {}
}

if (!convertedParams[name[2]][name[3]]) {
convertedParams[name[2]][name[3]] = {}
}

convertedParams[name[2]][name[3]][param.name] = param
}

params = await ssm.getParametersByPath(config).promise()
nextToken = params.NextToken
config.NextToken = nextToken
} while (nextToken)

return convertedParams
}

module.exports = {
constructParamPath,
getParameter,
getParametersByService,
setParameter,
setParametersByService
setParametersByService,
getEnvironments,
getServicesForEnvironment,
getAllOrgParams
}
30 changes: 30 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,36 @@ describe('awsManager', async () => {
}
})
})

describe('getEnvironments', async () => {
it('should get all the environments', async () => {
const envs = await awsManager.getEnvironments()
console.log(envs)
should.exist(envs.test)
envs.test.should.equal(4)
})
})

describe('getServicesForEnvironment', async () => {
it('should get all the services in an environment', async () => {
const svcs = await awsManager.getServicesForEnvironment('test')
console.log(svcs)
should.exist(svcs['config-wrapper'])
svcs['config-wrapper'].should.equal(4)
})
})

describe('getAllOrgParams', async () => {
it('should get all the org parameters', async () => {
const params = await awsManager.getAllOrgParams()
console.log(params)
should.exist(params.test)
should.exist(params.test['config-wrapper'])
Object.keys(params.test['config-wrapper']).should.have.lengthOf(4)
params.test['config-wrapper'].should.have.property('testParam01')
params.test['config-wrapper'].should.have.property('secretParam01')
})
})
})


Expand Down

0 comments on commit 206fb83

Please sign in to comment.