@@ -4,10 +4,8 @@ import fetch from 'node-fetch';
4
4
import yaml from 'js-yaml' ;
5
5
import path from 'path' ;
6
6
7
- const MEDIC_REPO_NAME = 'medic' ;
8
- const MEDIC_REPO_URL = 'https://docs.communityhealthtoolkit.org/helm-charts' ;
9
- const CHT_CHART_NAME = `${ MEDIC_REPO_NAME } /cht-chart-4x` ;
10
- const DEFAULT_CHART_VERSION = '1.1.*' ;
7
+ import config from './config.js' ;
8
+ const { MEDIC_REPO_NAME , MEDIC_REPO_URL , CHT_CHART_NAME , DEFAULT_CHART_VERSION , IMAGE_TAG_API_URL } = config ;
11
9
12
10
import { fileURLToPath } from 'url' ;
13
11
import { obtainCertificateAndKey , createSecret } from './certificate.js' ;
@@ -16,7 +14,7 @@ import { UserRuntimeError } from './error.js';
16
14
const __filename = fileURLToPath ( import . meta. url ) ;
17
15
const __dirname = path . dirname ( __filename ) ;
18
16
19
- const readFile = function ( f ) {
17
+ const readFile = ( f ) => {
20
18
try {
21
19
return yaml . load ( fs . readFileSync ( f , 'utf8' ) ) ;
22
20
} catch ( err ) {
@@ -25,22 +23,22 @@ const readFile = function(f) {
25
23
}
26
24
} ;
27
25
28
- const prepare = function ( f ) {
26
+ const prepare = ( f ) => {
29
27
const values = readFile ( f ) ;
30
28
const environment = values . environment || '' ;
31
29
const scriptPath = path . join ( __dirname , 'prepare.sh' ) ;
32
30
child_process . execSync ( `${ scriptPath } ${ environment } ` , { stdio : 'inherit' } ) ; //NoSONAR
33
31
} ;
34
32
35
- const loadValues = function ( f ) {
33
+ const loadValues = ( f ) => {
36
34
if ( ! f ) {
37
35
console . error ( 'No values file provided. Please specify a values file using -f <file>' ) ;
38
36
process . exit ( 1 ) ;
39
37
}
40
38
return readFile ( f ) ;
41
39
} ;
42
40
43
- const determineNamespace = function ( values ) {
41
+ const determineNamespace = ( values ) => {
44
42
const namespace = values . namespace || '' ;
45
43
if ( ! namespace ) {
46
44
console . error ( 'Namespace is not specified.' ) ;
@@ -49,43 +47,48 @@ const determineNamespace = function(values) {
49
47
return namespace ;
50
48
} ;
51
49
52
- const getImageTag = async function ( chtversion ) {
53
- const response = await fetch ( `https://staging.dev.medicmobile.org/_couch/builds_4/ medic:medic:${ chtversion } ` ) ;
50
+ const getImageTag = async ( chtVersion ) => {
51
+ const response = await fetch ( `${ IMAGE_TAG_API_URL } / medic:medic:${ chtVersion } ` ) ;
54
52
const data = await response . json ( ) ;
55
- const tag = data . tags && data . tags [ 0 ] ;
53
+ const tag = data . tags ?. [ 0 ] ;
56
54
if ( ! tag ) {
57
55
return Promise . reject ( new UserRuntimeError ( 'cht image tag not found' ) ) ;
58
56
}
59
57
return tag . image . split ( ':' ) . pop ( ) ;
60
58
} ;
61
59
62
- const getChartVersion = function ( values ) {
60
+ const getChartVersion = ( values ) => {
63
61
return values . cht_chart_version || DEFAULT_CHART_VERSION ;
64
62
} ;
65
63
66
64
const helmCmd = ( action , positionalArgs , params ) => {
67
- const flagsArray = Object . entries ( params ) . map ( ( [ key , value ] ) => {
68
- if ( value === true ) {
69
- return `--${ key } ` ;
70
- }
71
- if ( value ) {
72
- return `--${ key } ${ value } ` ;
73
- }
74
- return '' ; //If value is falsy, don't include the flag
75
- } ) . filter ( Boolean ) ;
65
+ const flagsArray = Object
66
+ . entries ( params )
67
+ . map ( ( [ key , value ] ) => {
68
+ if ( value === true ) {
69
+ return `--${ key } ` ;
70
+ }
71
+ if ( value ) {
72
+ return `--${ key } ${ value } ` ;
73
+ }
74
+ return '' ; //If value is falsy, don't include the flag
75
+ } )
76
+ . filter ( Boolean ) ;
76
77
77
78
const command = `helm ${ action } ${ positionalArgs . join ( ' ' ) } ${ flagsArray . join ( ' ' ) } ` ;
78
- return child_process . execSync ( command , { stdio : 'inherit' } ) ; //NoSONAR
79
+ return child_process . execSync ( command , { stdio : 'inherit' } ) ;
79
80
} ;
80
81
81
- const helmInstallOrUpdate = function ( valuesFile , namespace , values , imageTag ) {
82
+ const helmInstallOrUpdate = ( valuesFile , namespace , values , imageTag ) => {
82
83
const chartVersion = getChartVersion ( values ) ;
83
84
ensureMedicHelmRepo ( ) ;
84
85
const projectName = values . project_name || '' ;
85
86
const namespaceExists = checkNamespaceExists ( namespace ) ;
86
87
87
88
try {
88
- const releaseExists = child_process . execSync ( `helm list -n ${ namespace } ` ) . toString ( ) //NoSONAR
89
+ const releaseExists = child_process
90
+ . execSync ( `helm list -n ${ namespace } ` )
91
+ . toString ( )
89
92
. includes ( projectName ) ;
90
93
91
94
const commonOpts = {
@@ -121,29 +124,32 @@ const helmInstallOrUpdate = function(valuesFile, namespace, values, imageTag) {
121
124
}
122
125
} ;
123
126
124
- const checkNamespaceExists = function ( namespace ) {
127
+ const checkNamespaceExists = ( namespace ) => {
125
128
try {
126
- const result = child_process . execSync ( `kubectl get namespace ${ namespace } ` ) . toString ( ) ; //NoSONAR
127
- return result . includes ( namespace ) ; //NoSONAR
129
+ const result = child_process . execSync ( `kubectl get namespace ${ namespace } ` ) . toString ( ) ;
130
+ return result . includes ( namespace ) ;
128
131
} catch ( err ) {
129
132
return false ;
130
133
}
131
134
} ;
132
135
133
- const ensureMedicHelmRepo = function ( ) {
136
+ const ensureMedicHelmRepo = ( ) => {
134
137
try {
135
138
const repoList = child_process . execSync ( `helm repo list -o json` ) . toString ( ) ;
136
139
const repos = JSON . parse ( repoList ) ;
137
140
const medicRepo = repos . find ( repo => repo . name === MEDIC_REPO_NAME ) ;
141
+
138
142
if ( ! medicRepo ) {
139
143
console . log ( `Helm repo ${ MEDIC_REPO_NAME } not found, adding..` ) ;
140
- child_process . execSync ( `helm repo add ${ MEDIC_REPO_NAME } ${ MEDIC_REPO_URL } ` , { stdio : 'inherit' } ) ; //NoSONAR
144
+ child_process . execSync ( `helm repo add ${ MEDIC_REPO_NAME } ${ MEDIC_REPO_URL } ` , { stdio : 'inherit' } ) ;
141
145
return ;
142
- } else if ( medicRepo . url . replace ( / \/ $ / , '' ) !== MEDIC_REPO_URL ) {
146
+ }
147
+
148
+ if ( medicRepo . url . replace ( / \/ $ / , '' ) !== MEDIC_REPO_URL ) {
143
149
throw new UserRuntimeError ( `Medic repo found but url not matching '${ MEDIC_REPO_URL } ', see: helm repo list` ) ;
144
150
}
145
151
// Get the latest
146
- child_process . execSync ( `helm repo update ${ MEDIC_REPO_NAME } ` , { stdio : 'inherit' } ) ; //NoSONAR
152
+ child_process . execSync ( `helm repo update ${ MEDIC_REPO_NAME } ` , { stdio : 'inherit' } ) ;
147
153
} catch ( err ) {
148
154
console . error ( err . message ) ;
149
155
if ( err . stack ) {
@@ -153,7 +159,7 @@ const ensureMedicHelmRepo = function() {
153
159
}
154
160
} ;
155
161
156
- const install = async function ( f ) {
162
+ const install = async ( f ) => {
157
163
prepare ( f ) ;
158
164
const values = loadValues ( f ) ;
159
165
const namespace = determineNamespace ( values ) ;
0 commit comments