1
+ const fs = require ( 'fs' ) ;
2
+
3
+ const ENV_FILE_PATH = '.env.secrets' ;
4
+ const DEVTRON_HOST = 'https://dev-staging.devtron.info' ;
5
+
6
+ // Function to read environment variables from .env.secrets file
7
+ const readEnvFile = ( ) => {
8
+ try {
9
+ const envContent = fs . readFileSync ( ENV_FILE_PATH , 'utf8' ) ;
10
+ const envVars = { } ;
11
+ envContent . split ( '\n' ) . forEach ( line => {
12
+ const [ key , value ] = line . split ( '=' ) ;
13
+ if ( key && value ) {
14
+ envVars [ key . trim ( ) ] = value . trim ( ) ;
15
+ }
16
+ } ) ;
17
+ return envVars ;
18
+ } catch ( error ) {
19
+ console . error ( `Error reading ${ ENV_FILE_PATH } :` , error . message ) ;
20
+ return { } ;
21
+ }
22
+ } ;
23
+
24
+ const writeIngressHostToEnv = async ( token , namespace ) => {
25
+ const cookie = `argocd.token=${ token } ` ;
26
+
27
+ const ingressBody = {
28
+ appId : "" ,
29
+ clusterId : 1 ,
30
+ k8sRequest : {
31
+ resourceIdentifier : {
32
+ groupVersionKind : {
33
+ Group : "networking.k8s.io" ,
34
+ Version : "v1" ,
35
+ Kind : "Ingress"
36
+ } ,
37
+ namespace,
38
+ }
39
+ }
40
+ } ;
41
+
42
+ const { result : { data : [ ingress ] } } = await fetch ( `${ DEVTRON_HOST } /orchestrator/k8s/resource/list` , {
43
+ "headers" : {
44
+ "cookie" : cookie ,
45
+ } ,
46
+ "body" : JSON . stringify ( ingressBody ) ,
47
+ "method" : "POST"
48
+ } ) . then ( response => response . json ( ) ) ;
49
+
50
+ const ingressDetailBody = {
51
+ appId : "" ,
52
+ clusterId : 1 ,
53
+ k8sRequest : {
54
+ resourceIdentifier : {
55
+ groupVersionKind : {
56
+ Group : "networking.k8s.io" ,
57
+ Version : "v1" ,
58
+ Kind : "Ingress"
59
+ } ,
60
+ namespace,
61
+ name : ingress . name
62
+ }
63
+ }
64
+ } ;
65
+
66
+ const { result : { manifestResponse : { manifest : { spec : { rules : [ { host } ] } } } } } = await fetch ( `${ DEVTRON_HOST } /orchestrator/k8s/resource` , {
67
+ "headers" : {
68
+ "cookie" : cookie ,
69
+ } ,
70
+ "body" : JSON . stringify ( ingressDetailBody ) ,
71
+ "method" : "POST"
72
+ } ) . then ( response => response . json ( ) ) ;
73
+
74
+ // Read existing .env file or create empty string if it doesn't exist
75
+ let envContent = '' ;
76
+ try {
77
+ envContent = fs . readFileSync ( ENV_FILE_PATH , 'utf8' ) ;
78
+ } catch ( error ) {
79
+ // File doesn't exist, will create it
80
+ }
81
+
82
+ // Check if VITE_TARGET_URL already exists and update or add it
83
+ if ( envContent . includes ( 'VITE_TARGET_URL=' ) ) {
84
+ // Replace existing value
85
+ envContent = envContent . replace ( / V I T E _ T A R G E T _ U R L = .* $ / m, `VITE_TARGET_URL=https://${ host } ` ) ;
86
+ console . log ( 'VITE_TARGET_URL updated in .env.secrets file' ) ;
87
+ } else {
88
+ // Add new entry
89
+ const newLine = envContent . length > 0 && ! envContent . endsWith ( '\n' ) ? '\n' : '' ;
90
+ envContent += `${ newLine } VITE_TARGET_URL=https://${ host } \n` ;
91
+ console . log ( 'VITE_TARGET_URL added to .env.secrets file' ) ;
92
+ }
93
+
94
+ fs . writeFileSync ( ENV_FILE_PATH , envContent ) ;
95
+ } ;
96
+
97
+ const main = async ( ) => {
98
+ if ( ! process . argv [ 2 ] ) {
99
+ console . error ( 'Please provide a namespace as an argument.' ) ;
100
+ process . exit ( 1 ) ;
101
+ }
102
+
103
+ const namespace = process . argv [ 2 ] ;
104
+
105
+ // Read token from .env.secrets file
106
+ const envVars = readEnvFile ( ) ;
107
+ const token = envVars . DEV_STAGING_TOKEN ;
108
+
109
+ if ( ! token ) {
110
+ console . error ( 'DEV_STAGING_TOKEN not found in .env.secrets file' ) ;
111
+ process . exit ( 1 ) ;
112
+ }
113
+
114
+ const cookie = `argocd.token=${ token } `
115
+
116
+ const secretsBody = {
117
+ appId : "" ,
118
+ clusterId : 1 ,
119
+ k8sRequest : {
120
+ resourceIdentifier : {
121
+ groupVersionKind : {
122
+ Group : "" ,
123
+ Version : "v1" ,
124
+ Kind : "Secret"
125
+ } ,
126
+ namespace,
127
+ name : "orchestrator-secret"
128
+ }
129
+ }
130
+ }
131
+
132
+ const { result : { manifestResponse : { manifest : { data : { ADMIN_PASSWORD } } } } } = await fetch ( `${ DEVTRON_HOST } /orchestrator/k8s/resource` , {
133
+ "headers" : {
134
+ "cookie" : cookie ,
135
+ } ,
136
+ "body" : JSON . stringify ( secretsBody ) ,
137
+ "method" : "POST"
138
+ } ) . then ( response => response . json ( ) ) ;
139
+
140
+ // Read existing .env file or create empty string if it doesn't exist
141
+ let envContent = '' ;
142
+ try {
143
+ envContent = fs . readFileSync ( ENV_FILE_PATH , 'utf8' ) ;
144
+ } catch ( error ) {
145
+ // File doesn't exist, will create it
146
+ }
147
+
148
+ // Decode the base64 encoded ADMIN_PASSWORD
149
+ const decodedPassword = Buffer . from ( ADMIN_PASSWORD , 'base64' ) . toString ( 'utf8' ) ;
150
+
151
+ // Check if VITE_ADMIN_PASSWORD already exists and update or add it
152
+ if ( envContent . includes ( 'VITE_ADMIN_PASSWORD=' ) ) {
153
+ // Replace existing value
154
+ envContent = envContent . replace ( / V I T E _ A D M I N _ P A S S W O R D = .* $ / m, `VITE_ADMIN_PASSWORD=${ decodedPassword } ` ) ;
155
+ console . log ( 'VITE_ADMIN_PASSWORD updated in .env.secrets file' ) ;
156
+ } else {
157
+ // Add new entry
158
+ const newLine = envContent . length > 0 && ! envContent . endsWith ( '\n' ) ? '\n' : '' ;
159
+ envContent += `${ newLine } VITE_ADMIN_PASSWORD=${ decodedPassword } \n` ;
160
+ console . log ( 'VITE_ADMIN_PASSWORD added to .env.secrets file' ) ;
161
+ }
162
+
163
+ fs . writeFileSync ( ENV_FILE_PATH , envContent ) ;
164
+
165
+ await writeIngressHostToEnv ( token , namespace ) ;
166
+ }
167
+
168
+ main ( )
0 commit comments