Skip to content

Commit dd1145b

Browse files
author
Maurice Faber
committed
fix: cli values too early
1 parent 19d9333 commit dd1145b

12 files changed

+35
-50
lines changed

.cspell.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
"millis",
3434
"mkilled",
3535
"nindent",
36+
"nothrow",
3637
"nslookup",
37-
"RAGRS",
38+
"rAGRS",
3839
"jwks",
3940
"pids",
4041
"ragzrs",

binzx/otomi

-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ else
291291
-v /tmp/otomi:/tmp/otomi \
292292
$(check_volume_path $HOME/.kube "/home/app/.kube") \
293293
$([ -n "$KUBECONFIG" ] && check_volume_path $KUBECONFIG "/home/app/.kube/config") \
294-
$(check_volume_path $HOME/.ssh "/home/app/.ssh") \
295294
$(check_volume_path $helm_config "/home/app/.config/helm") \
296295
$(check_volume_path $HOME/.config/gcloud "/home/app/.config/gcloud") \
297296
$(check_volume_path $HOME/.aws "/home/app/.aws") \

src/ci-tests.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
getFilename,
1313
getParsedArgs,
1414
OtomiDebugger,
15+
rootDir,
1516
setParsedArgs,
16-
startingDir,
1717
terminal,
1818
} from './common/utils'
1919
import { basicOptions } from './common/yargs-opts'
@@ -33,8 +33,8 @@ const setup = (): void => {
3333

3434
export const ciTests = async (): Promise<void> => {
3535
const argv: Arguments = getParsedArgs()
36-
if (!existsSync(`${startingDir}/env`)) symlinkSync(`${startingDir}/tests/fixtures`, `${startingDir}/env`)
37-
debug.log(`Running CI tests with values from ${`${startingDir}/tests/fixtures/`}`)
36+
if (!existsSync(`${rootDir}/env`)) symlinkSync(`${rootDir}/tests/fixtures`, `${rootDir}/env`)
37+
debug.log(`Running CI tests with values from ${`${rootDir}/tests/fixtures/`}`)
3838

3939
const xCommand = 'opa test policies -v'
4040
debug.info(xCommand)

src/cmd/apply.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
getParsedArgs,
1010
logLevelString,
1111
OtomiDebugger,
12+
rootDir,
1213
setParsedArgs,
13-
startingDir,
1414
terminal,
1515
waitTillAvailable,
1616
} from '../common/utils'
@@ -48,7 +48,7 @@ const commitOnFirstRun = async () => {
4848
if ((await nothrow($`git ls-remote`)).stdout.trim().length !== 0) return
4949
await commit()
5050
await nothrow($`kubectl -n otomi create cm otomi-status --from-literal=status='Installed'`)
51-
cd(startingDir)
51+
cd(rootDir)
5252
}
5353

5454
const applyAll = async () => {

src/cmd/bootstrap.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ import { fileURLToPath } from 'url'
55
// import isURL from 'validator/es/lib/isURL'
66
import { Argv } from 'yargs'
77
import { $, cd, nothrow } from 'zx'
8-
import { genSops } from './gen-sops'
98
import { decrypt, encrypt } from '../common/crypt'
109
import { env } from '../common/envalid'
1110
import { hfValues } from '../common/hf'
12-
import { getImageTag, prepareEnvironment, rootDir } from '../common/setup'
11+
import { getImageTag, prepareEnvironment } from '../common/setup'
1312
import {
1413
BasicArguments,
15-
currDir,
1614
generateSecrets,
1715
getFilename,
1816
isChart,
1917
loadYaml,
2018
OtomiDebugger,
19+
rootDir,
2120
setParsedArgs,
2221
terminal,
2322
} from '../common/utils'
2423
import { writeValues } from '../common/values'
24+
import { genSops } from './gen-sops'
2525

2626
export const getChartValues = (): any | undefined => {
2727
return loadYaml(env.VALUES_INPUT)
@@ -50,8 +50,6 @@ const generateLooseSchema = () => {
5050
}
5151

5252
export const bootstrapValues = async (): Promise<void> => {
53-
const originalValues = isChart() ? getChartValues() : await hfValues(true)
54-
5553
const hasOtomi = existsSync(`${env.ENV_DIR}/bin/otomi`)
5654

5755
const binPath = `${env.ENV_DIR}/bin`
@@ -100,6 +98,7 @@ export const bootstrapValues = async (): Promise<void> => {
10098
)
10199

102100
// Done, write chart values if we got any
101+
const originalValues = isChart() ? getChartValues() : await hfValues(true)
103102
if (isChart()) await writeValues(originalValues)
104103

105104
// Generate passwords and merge with values and give the priority to the current existing passwords. (don't change passwords everytime)
@@ -144,7 +143,6 @@ export const bootstrapGit = async (): Promise<void> => {
144143
} else {
145144
// scenario 1 or 2 or 4(2 will only be called upon first otomi commit)
146145
debug.info('Initializing values repo.')
147-
const cwd = await currDir()
148146
cd(env.ENV_DIR)
149147

150148
const values = await hfValues(true)
@@ -196,7 +194,9 @@ export const bootstrapGit = async (): Promise<void> => {
196194
await $`git config --local user.email ${email}`
197195
await $`git checkout -b ${branch}`
198196
await $`git remote add origin ${remote}`
199-
cd(cwd)
197+
if (existsSync(`${env.ENV_DIR}/.sops.yaml`)) await nothrow($`git config --local diff.sopsdiffer.textconv "sops -d"`)
198+
199+
cd(rootDir)
200200
debug.log(`Done bootstrapping git`)
201201
}
202202
}

src/cmd/commit.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import { encrypt } from '../common/crypt'
55
import { env } from '../common/envalid'
66
import { hfValues } from '../common/hf'
77
import { prepareEnvironment } from '../common/setup'
8-
import { currDir, getFilename, OtomiDebugger, setParsedArgs, terminal } from '../common/utils'
8+
import { getFilename, OtomiDebugger, rootDir, setParsedArgs, terminal } from '../common/utils'
99
import { Arguments as HelmArgs } from '../common/yargs-opts'
10-
import { bootstrapGit } from './bootstrap'
1110
import { Arguments as DroneArgs, genDrone } from './gen-drone'
1211
import { pull } from './pull'
1312
import { validateValues } from './validate-values'
@@ -20,10 +19,10 @@ interface Arguments extends HelmArgs, DroneArgs {}
2019
export const preCommit = async (): Promise<void> => {
2120
const pcDebug = terminal('Pre Commit')
2221
pcDebug.info('Check for cluster diffs')
23-
await nothrow($`git config --local diff.sopsdiffer.textconv "sops -d"`)
24-
const settingsDiff = (await $`git diff env/settings.yaml`).stdout.trim()
25-
const secretDiff = (await $`git diff env/secrets.settings.yaml`).stdout.trim()
26-
22+
cd(env.ENV_DIR)
23+
const settingsDiff = (await nothrow($`git diff env/settings.yaml`)).stdout.trim()
24+
const secretDiff = (await nothrow($`git diff env/secrets.settings.yaml`)).stdout.trim()
25+
cd(rootDir)
2726
const versionChanges = settingsDiff.includes('+ version:')
2827
const secretSlackChanges = secretDiff.includes('+ url: https://hooks.slack.com/')
2928
const secretMsTeamsLowPrioChanges = secretDiff.includes('+ lowPrio: https://')
@@ -36,7 +35,6 @@ export const gitPush = async (branch: string): Promise<boolean> => {
3635
const gitDebug = terminal('gitPush')
3736
gitDebug.info('Starting git push.')
3837

39-
const cwd = await currDir()
4038
cd(env.ENV_DIR)
4139
try {
4240
await $`git push -u origin ${branch} -f`
@@ -46,7 +44,7 @@ export const gitPush = async (branch: string): Promise<boolean> => {
4644
gitDebug.error(error)
4745
return false
4846
} finally {
49-
cd(cwd)
47+
cd(rootDir)
5048
}
5149
}
5250

@@ -55,7 +53,6 @@ export const commit = async (): Promise<void> => {
5553

5654
debug.info('Preparing values')
5755

58-
const cwd = await currDir()
5956
cd(env.ENV_DIR)
6057

6158
const values = await hfValues()
@@ -68,6 +65,7 @@ export const commit = async (): Promise<void> => {
6865
await $`git commit -m 'otomi commit' --no-verify`
6966
} catch (e) {
7067
debug.error(e.stdout)
68+
debug.error(e.stderr)
7169
debug.log('Something went wrong trying to commit. Did you make any changes?')
7270
}
7371

@@ -93,7 +91,7 @@ export const commit = async (): Promise<void> => {
9391
debug.error('Pushing the values failed, please read the above error message and manually try again')
9492
process.exit(1)
9593
} finally {
96-
cd(cwd)
94+
cd(rootDir)
9795
}
9896
}
9997

@@ -108,8 +106,8 @@ export const module = {
108106

109107
if (!env.CI && existsSync(`${env.ENV_DIR}/.git`)) {
110108
await pull()
111-
} else {
112-
await bootstrapGit()
109+
// } else {
110+
// await bootstrapGit()
113111
}
114112
await commit()
115113
},

src/cmd/gen-drone.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
getParsedArgs,
1010
gucci,
1111
OtomiDebugger,
12+
rootDir,
1213
setParsedArgs,
13-
startingDir,
1414
terminal,
1515
} from '../common/utils'
1616

@@ -54,7 +54,7 @@ export const genDrone = async (): Promise<void> => {
5454
pullPolicy,
5555
}
5656

57-
const output = (await gucci(`${startingDir}/tpl/.drone.yml.gotmpl`, obj)) as string
57+
const output = (await gucci(`${rootDir}/tpl/.drone.yml.gotmpl`, obj)) as string
5858

5959
// TODO: Remove when validate-values can validate subpaths
6060
if (!output) {

src/cmd/gen-sops.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
gucci,
1111
loadYaml,
1212
OtomiDebugger,
13+
rootDir,
1314
setParsedArgs,
14-
startingDir,
1515
terminal,
1616
} from '../common/utils'
1717

@@ -42,7 +42,7 @@ export const genSops = async (): Promise<void> => {
4242
}
4343

4444
const targetPath = `${env.ENV_DIR}/.sops.yaml`
45-
const templatePath = `${startingDir}/tpl/.sops.yaml.gotmpl`
45+
const templatePath = `${rootDir}/tpl/.sops.yaml.gotmpl`
4646
const kmsProvider = providerMap[provider] as string
4747
const kmsKeys = settingsVals.kms.sops[provider].keys as string
4848

src/cmd/pull.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import { $, cd } from 'zx'
33
import { env } from '../common/envalid'
44
import { hfValues } from '../common/hf'
55
import { prepareEnvironment, scriptName } from '../common/setup'
6-
import { currDir, getFilename, OtomiDebugger, setParsedArgs, terminal } from '../common/utils'
6+
import { getFilename, OtomiDebugger, rootDir, setParsedArgs, terminal } from '../common/utils'
77
import { Arguments as HelmArgs } from '../common/yargs-opts'
8-
import { bootstrapValues } from './bootstrap'
98

109
type Arguments = HelmArgs
1110

@@ -16,21 +15,18 @@ export const pull = async (): Promise<void> => {
1615
const allValues = await hfValues()
1716
const branch = allValues.charts?.['otomi-api']?.git?.branch ?? 'main'
1817
debug.info('Pulling latest values')
19-
const cwd = await currDir()
2018
cd(env.ENV_DIR)
2119
try {
2220
await $`git fetch`
23-
await $`if git log >/dev/null; then git merge origin/${branch}; fi`
21+
await $`if git log; then git merge origin/${branch}; fi`
2422
} catch (error) {
2523
debug.error(error.stdout)
2624
debug.warn(
2725
`An error occured when trying to pull (maybe not problematic).\nIf you see merge conflicts then please resolve these and run \`otomi commit\` again.`,
2826
)
2927
} finally {
30-
cd(cwd)
28+
cd(rootDir)
3129
}
32-
33-
await bootstrapValues()
3430
}
3531

3632
export const module = {

src/cmd/validate-values.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,7 @@ import { Argv } from 'yargs'
44
import { chalk } from 'zx'
55
import { hfValues } from '../common/hf'
66
import { prepareEnvironment } from '../common/setup'
7-
import {
8-
getFilename,
9-
getParsedArgs,
10-
loadYaml,
11-
OtomiDebugger,
12-
setParsedArgs,
13-
startingDir,
14-
terminal,
15-
} from '../common/utils'
7+
import { getFilename, getParsedArgs, loadYaml, OtomiDebugger, rootDir, setParsedArgs, terminal } from '../common/utils'
168
import { Arguments, helmOptions } from '../common/yargs-opts'
179

1810
const cmdName = getFilename(import.meta.url)
@@ -43,7 +35,7 @@ export const validateValues = async (): Promise<void> => {
4335

4436
try {
4537
debug.info('Loading values-schema.yaml')
46-
const valuesSchema = loadYaml(`${startingDir}/values-schema.yaml`) as Record<string, unknown>
38+
const valuesSchema = loadYaml(`${rootDir}/values-schema.yaml`) as Record<string, unknown>
4739
debug.debug('Initializing Ajv')
4840
const ajv = new Ajv({ allErrors: true, strict: false, strictTypes: false, verbose: true })
4941
debug.debug('Compiling Ajv validation')

src/common/setup.ts

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const dirname = fileURLToPath(import.meta.url)
1313
let otomiImageTag: string
1414
let otomiClusterOwner: string
1515
let otomiK8sVersion: string
16-
export const rootDir = process.cwd()
1716

1817
/**
1918
* Check whether the environment matches the configuration for the kubernetes context

src/common/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { env } from './envalid'
1515
$.verbose = false // https://github.com/google/zx#verbose - don't need to print the SHELL executed commands
1616
$.prefix = 'set -euo pipefail;' // https://github.com/google/zx/blob/main/index.mjs#L103
1717

18-
export const startingDir = process.cwd()
18+
export const rootDir = process.cwd()
1919
export const currDir = async (): Promise<string> => (await $`pwd`).stdout.trim()
2020
export const parser = yargs(process.argv.slice(3))
2121
export const getFilename = (path: string): string => fileURLToPath(path).split('/').pop()?.split('.')[0] as string
@@ -356,7 +356,7 @@ export const extract = (schema: Record<string, any>, leaf: string, mapValue = (v
356356
let valuesSchema: Record<string, unknown>
357357
export const getValuesSchema = async (): Promise<Record<string, unknown>> => {
358358
if (valuesSchema) return valuesSchema
359-
const schema = loadYaml(`${startingDir}/values-schema.yaml`)
359+
const schema = loadYaml(`${rootDir}/values-schema.yaml`)
360360
const derefSchema = await $RefParser.dereference(schema as $RefParser.JSONSchema)
361361
valuesSchema = omit(derefSchema, ['definitions', 'properties.teamConfig'])
362362

0 commit comments

Comments
 (0)