/
setup.ts
139 lines (130 loc) · 4.39 KB
/
setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { existsSync, readdirSync } from 'fs'
import { fileURLToPath } from 'url'
import { $, chalk } from 'zx'
import { decrypt } from './crypt'
import { env } from './envalid'
import { hfValues } from './hf'
import { BasicArguments, loadYaml, parser, terminal } from './utils'
import { askYesNo } from './zx-enhance'
chalk.level = 2
const dirname = fileURLToPath(import.meta.url)
let otomiImageTag: string
let otomiClusterOwner: string
let otomiK8sVersion: string
/**
* Check whether the environment matches the configuration for the kubernetes context
* @returns
*/
const checkKubeContext = async (): Promise<void> => {
if (env.CI) return
const debug = terminal('checkKubeContext')
debug.info('Validating kube context')
const values: any = await hfValues()
const currentContext = (await $`kubectl config current-context`).stdout.trim()
const k8sContext = values?.cluster?.k8sContext
debug.debug('currentContext: ', currentContext)
debug.debug('k8sContext: ', k8sContext)
debug.info(`Using kube context: ${currentContext}`)
if (k8sContext !== currentContext) {
let fixContext = false
if (!(parser.argv as BasicArguments).setContext) {
fixContext = await askYesNo(
`Warning: Your current kubernetes context (${currentContext}) does not match cluster context: ${k8sContext}. Would you like to switch kube context to cluster first?`,
{ defaultYes: true },
)
}
if (fixContext || (parser.argv as BasicArguments).setContext) {
await $`kubectl config use ${k8sContext}`
}
}
}
/**
* Check the ENV_DIR parameter and whether or not the folder is populated
* @returns
*/
const checkEnvDir = (): boolean => {
const debug = terminal('checkEnvDir')
if (dirname.includes('otomi-core') && !env.ENV_DIR) {
debug.error('The ENV_DIR environment variable is not set')
process.exit(1)
}
debug.debug(`ENV_DIR: ${env.ENV_DIR}`)
return readdirSync(env.ENV_DIR).length > 0
}
export type PrepareEnvironmentOptions = {
skipEnvDirCheck?: boolean
skipKubeContextCheck?: boolean
skipDecrypt?: boolean
skipAllPreChecks?: boolean
}
let clusterFile: any
export const scriptName = process.env.OTOMI_CALLER_COMMAND ?? 'otomi'
/**
* Find the cluster kubernetes version in the values
* @returns String of the kubernetes version on the cluster
*/
export const getK8sVersion = (): string => {
if (otomiK8sVersion) return otomiK8sVersion
if (!clusterFile) {
clusterFile = loadYaml(`${env.ENV_DIR}/env/cluster.yaml`)
}
otomiK8sVersion = clusterFile.cluster?.k8sVersion
return otomiK8sVersion
}
/**
* Find what image tag is defined in configuration for otomi
* @returns string
*/
export const getImageTag = (): string => {
if (otomiImageTag) return otomiImageTag
const file = `${env.ENV_DIR}/env/settings.yaml`
if (!existsSync(file)) return process.env.OTOMI_TAG ?? 'master'
const settingsFile = loadYaml(file)
otomiImageTag = settingsFile?.otomi?.version ?? 'master'
return otomiImageTag
}
/**
* Find the customer name that is defined in configuration for otomi
* @returns string
*/
export const getClusterOwner = (): string => {
if (otomiClusterOwner) return otomiClusterOwner
if (!clusterFile) {
clusterFile = loadYaml(`${env.ENV_DIR}/env/cluster.yaml`)
}
otomiClusterOwner = clusterFile.cluster?.owner
return otomiClusterOwner
}
/**
* Prepare environment when running an otomi command
*/
export const prepareEnvironment = async (options?: PrepareEnvironmentOptions): Promise<void> => {
if (options?.skipAllPreChecks) return
const debug = terminal('prepareEnvironment')
debug.info('Checking environment')
if (!options?.skipEnvDirCheck && checkEnvDir()) {
if (!env.CI && !options?.skipKubeContextCheck) await checkKubeContext()
if (!options?.skipDecrypt) await decrypt()
}
}
/**
* If ran within otomi-core, stop execution as it should not be ran within that folder.
* @param command that is executed
*/
export const exitIfInCore = (command: string): void => {
if (dirname.includes('otomi-core') || env.ENV_DIR.includes('otomi-core')) {
const debug = terminal('exitIfInCore')
debug.error(`'otomi ${command}' should not be ran from otomi-core`)
process.exit(1)
}
}
/**
* Cleanup trap on exit - any handler function MUST be synchronous
* @param handler cleanup function set per command
*/
export const cleanupHandler = (handler: () => any): void => {
process.on('exit', (code) => {
handler()
process.exit(code)
})
}