/
gen-drone.ts
93 lines (80 loc) · 2.44 KB
/
gen-drone.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
import { writeFileSync } from 'fs'
import { Argv } from 'yargs'
import { env } from '../common/envalid'
import { hfValues } from '../common/hf'
import { getClusterOwner, getImageTag, prepareEnvironment } from '../common/setup'
import {
BasicArguments,
getFilename,
getParsedArgs,
gucci,
OtomiDebugger,
rootDir,
setParsedArgs,
terminal,
} from '../common/utils'
export interface Arguments extends BasicArguments {
dryRun?: boolean
}
const cmdName = getFilename(import.meta.url)
const debug: OtomiDebugger = terminal(cmdName)
export const genDrone = async (): Promise<void> => {
const argv: Arguments = getParsedArgs()
const allValues = await hfValues()
if (!allValues.charts?.drone?.enabled) {
return
}
const receiver = allValues.alerts?.drone ?? 'slack'
const branch = allValues.charts?.['otomi-api']?.git?.branch ?? 'main'
const key = receiver === 'slack' ? 'url' : 'lowPrio'
const channel = receiver === 'slack' ? allValues.alerts?.[receiver]?.channel ?? 'dev-mon' : undefined
const webhook = allValues.alerts?.[receiver]?.[key]
if (!webhook) throw new Error(`Could not find webhook url in 'alerts.${receiver}.${key}'`)
const cluster = allValues.cluster?.name
const globalPullSecret = allValues.otomi?.globalPullSecret
const provider = allValues.alerts.drone
const imageTag = getImageTag()
const pullPolicy = imageTag.startsWith('v') ? 'if-not-exists' : 'always'
const obj = {
imageTag,
branch,
cluster,
channel,
customer: getClusterOwner(),
globalPullSecret,
provider,
webhook,
pullPolicy,
}
const output = (await gucci(`${rootDir}/tpl/.drone.yml.gotmpl`, obj)) as string
// TODO: Remove when validate-values can validate subpaths
if (!output) {
debug.warn('Something went wrong trying to template using gucci')
return
}
if (argv.dryRun) {
debug.log(output)
} else {
writeFileSync(`${env.ENV_DIR}/.drone.yml`, output)
debug.log(`gen-drone is done and the configuration is written to: ${env.ENV_DIR}/.drone.yml`)
}
}
export const module = {
command: cmdName,
describe: undefined,
builder: (parser: Argv): Argv =>
parser.options({
'dry-run': {
alias: ['d'],
boolean: true,
default: false,
hidden: true,
},
}),
handler: async (argv: Arguments): Promise<void> => {
setParsedArgs(argv)
await prepareEnvironment({ skipKubeContextCheck: true })
await genDrone()
},
}
export default module