-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path001-update-grafana-compose-extend.ts
57 lines (48 loc) · 1.67 KB
/
001-update-grafana-compose-extend.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
import { type Context } from '../context.js';
import { parse, stringify } from 'yaml';
export default async function migrate(context: Context) {
// Check if docker-compose.yaml exists
if (!context.doesFileExist('./docker-compose.yaml')) {
return context;
}
// Read and parse the docker-compose file
const composeContent = context.getFile('./docker-compose.yaml');
if (!composeContent) {
return context;
}
const composeData = parse(composeContent);
// Check if grafana service exists with the specified build context
if (composeData?.services?.grafana?.build?.context !== './.config') {
return context;
}
// Check if base compose file exists
if (!context.doesFileExist('./.config/docker-compose-base.yaml')) {
return context;
}
// Preserve build args if they exist
const existingBuildArgs = composeData.services.grafana.build.args;
const preservedArgs: Record<string, string> = {};
if (existingBuildArgs?.grafana_image || existingBuildArgs?.grafana_version) {
if (existingBuildArgs.grafana_image) {
preservedArgs['grafana_image'] = existingBuildArgs.grafana_image;
}
if (existingBuildArgs.grafana_version) {
preservedArgs['grafana_version'] = existingBuildArgs.grafana_version;
}
}
// Update the grafana service configuration
composeData.services.grafana = {
extends: {
file: '.config/docker-compose-base.yaml',
service: 'grafana',
},
...(Object.keys(preservedArgs).length > 0 && {
build: {
args: preservedArgs,
},
}),
};
// Write the updated compose file
context.updateFile('./docker-compose.yaml', stringify(composeData, { lineWidth: 0 }));
return context;
}