@@ -100,32 +100,37 @@ func waitVaultToBeRunning(dryRun bool) {
100
100
log .Printf ("[#99] Dry-run mode, waitVaultToBeRunning skipped." )
101
101
return
102
102
}
103
- config := configs .ReadConfig ()
104
- x := 50
105
- for i := 0 ; i < x ; i ++ {
106
- _ , _ , err := pkg .ExecShellReturnStrings (config .KubectlClientPath , "--kubeconfig" , config .KubeConfigPath , "get" , "namespace/vault" )
107
- if err != nil {
108
- log .Println ("Waiting vault to be born" )
109
- time .Sleep (10 * time .Second )
110
- } else {
111
- log .Println ("vault namespace found, continuing" )
112
- time .Sleep (25 * time .Second )
113
- break
103
+ token := viper .GetString ("vault.token" )
104
+ if len (token ) == 0 {
105
+ config := configs .ReadConfig ()
106
+ x := 50
107
+ for i := 0 ; i < x ; i ++ {
108
+ _ , _ , err := pkg .ExecShellReturnStrings (config .KubectlClientPath , "--kubeconfig" , config .KubeConfigPath , "get" , "namespace/vault" )
109
+ if err != nil {
110
+ log .Println ("Waiting vault to be born" )
111
+ time .Sleep (10 * time .Second )
112
+ } else {
113
+ log .Println ("vault namespace found, continuing" )
114
+ time .Sleep (25 * time .Second )
115
+ break
116
+ }
114
117
}
115
- }
116
118
117
- //! failing
118
- x = 50
119
- for i := 0 ; i < x ; i ++ {
120
- _ , _ , err := pkg .ExecShellReturnStrings (config .KubectlClientPath , "--kubeconfig" , config .KubeConfigPath , "-n" , "vault" , "get" , "pods" , "-l" , "app.kubernetes.io/instance=vault" )
121
- if err != nil {
122
- log .Println ("Waiting vault pods to create" )
123
- time .Sleep (10 * time .Second )
124
- } else {
125
- log .Println ("vault pods found, continuing" )
126
- time .Sleep (15 * time .Second )
127
- break
119
+ //! failing
120
+ x = 50
121
+ for i := 0 ; i < x ; i ++ {
122
+ _ , _ , err := pkg .ExecShellReturnStrings (config .KubectlClientPath , "--kubeconfig" , config .KubeConfigPath , "-n" , "vault" , "get" , "pods" , "-l" , "app.kubernetes.io/instance=vault" )
123
+ if err != nil {
124
+ log .Println ("Waiting vault pods to create" )
125
+ time .Sleep (10 * time .Second )
126
+ } else {
127
+ log .Println ("vault pods found, continuing" )
128
+ time .Sleep (15 * time .Second )
129
+ break
130
+ }
128
131
}
132
+ } else {
133
+ log .Println ("vault token arleady exists, skipping vault health checks waitVaultToBeRunning" )
129
134
}
130
135
}
131
136
@@ -134,45 +139,49 @@ func loopUntilPodIsReady(dryRun bool) {
134
139
log .Printf ("[#99] Dry-run mode, loopUntilPodIsReady skipped." )
135
140
return
136
141
}
142
+ token := viper .GetString ("vault.token" )
143
+ if len (token ) == 0 {
137
144
138
- x := 50
139
- url := "http://localhost:8200/v1/sys/health"
140
- for i := 0 ; i < x ; i ++ {
141
- log .Println ("vault is not ready yet, sleeping and checking again" )
142
- time .Sleep (10 * time .Second )
145
+ x := 50
146
+ url := "http://localhost:8200/v1/sys/health"
147
+ for i := 0 ; i < x ; i ++ {
148
+ log .Println ("vault is not ready yet, sleeping and checking again" )
149
+ time .Sleep (10 * time .Second )
143
150
144
- req , _ := http .NewRequest ("GET" , url , nil )
151
+ req , _ := http .NewRequest ("GET" , url , nil )
145
152
146
- req .Header .Add ("Content-Type" , "application/json" )
153
+ req .Header .Add ("Content-Type" , "application/json" )
147
154
148
- res , err := http .DefaultClient .Do (req )
149
- if err != nil {
150
- log .Println ("error with http request Do, vault is not available" , err )
151
- continue
152
- }
155
+ res , err := http .DefaultClient .Do (req )
156
+ if err != nil {
157
+ log .Println ("error with http request Do, vault is not available" , err )
158
+ continue
159
+ }
153
160
154
- defer res .Body .Close ()
155
- body , err := ioutil .ReadAll (res .Body )
156
- if err != nil {
157
- log .Println ("vault is availbale but the body is not what is expected " , err )
158
- continue
159
- }
160
- log .Println (string (body ))
161
+ defer res .Body .Close ()
162
+ body , err := ioutil .ReadAll (res .Body )
163
+ if err != nil {
164
+ log .Println ("vault is availbale but the body is not what is expected " , err )
165
+ continue
166
+ }
161
167
162
- var responseJson map [string ]interface {}
168
+ var responseJson map [string ]interface {}
163
169
164
- if err := json .Unmarshal (body , & responseJson ); err != nil {
165
- log .Printf ("vault is availbale but the body is not what is expected %s" , err )
166
- continue
167
- }
170
+ if err := json .Unmarshal (body , & responseJson ); err != nil {
171
+ log .Printf ("vault is availbale but the body is not what is expected %s" , err )
172
+ continue
173
+ }
168
174
169
- _ , ok := responseJson ["initialized" ]
170
- if ok {
171
- log .Printf ("vault is initialized and is in the expected state" )
172
- return
175
+ _ , ok := responseJson ["initialized" ]
176
+ if ok {
177
+ log .Printf ("vault is initialized and is in the expected state" )
178
+ return
179
+ }
180
+ log .Panic ("vault was never initialized" )
173
181
}
182
+ } else {
183
+ log .Println ("vault token arleady exists, skipping vault health checks loopUntilPodIsReady" )
174
184
}
175
- log .Panic ("vault was never initialized" )
176
185
}
177
186
178
187
type VaultInitResponse struct {
@@ -206,41 +215,46 @@ func initializeVaultAndAutoUnseal(dryRun bool) {
206
215
return
207
216
}
208
217
209
- time . Sleep ( time . Second * 10 )
210
- url := "http://127.0.0.1:8200/v1/sys/init"
218
+ token := viper . GetString ( "vault.token" )
219
+ if len ( token ) == 0 {
211
220
212
- payload := strings .NewReader ("{\n \t \" stored_shares\" : 3,\n \t \" recovery_threshold\" : 3,\n \t \" recovery_shares\" : 5\n }" )
221
+ time .Sleep (time .Second * 10 )
222
+ url := "http://127.0.0.1:8200/v1/sys/init"
213
223
214
- req , err := http .NewRequest ("POST" , url , payload )
215
- if err != nil {
216
- log .Panic (err )
217
- }
224
+ payload := strings .NewReader ("{\n \t \" stored_shares\" : 3,\n \t \" recovery_threshold\" : 3,\n \t \" recovery_shares\" : 5\n }" )
218
225
219
- req .Header .Add ("Content-Type" , "application/json" )
226
+ req , err := http .NewRequest ("POST" , url , payload )
227
+ if err != nil {
228
+ log .Panic (err )
229
+ }
220
230
221
- res , err := http .DefaultClient .Do (req )
222
- if err != nil {
223
- log .Println ("error in Do http client request" , err )
224
- }
231
+ req .Header .Add ("Content-Type" , "application/json" )
225
232
226
- defer res .Body .Close ()
227
- body , err := ioutil .ReadAll (res .Body )
228
- if err != nil {
229
- log .Panic (err )
230
- }
233
+ res , err := http .DefaultClient .Do (req )
234
+ if err != nil {
235
+ log .Println ("error in Do http client request" , err )
236
+ }
231
237
232
- log .Println (string (body ))
238
+ defer res .Body .Close ()
239
+ body , err := ioutil .ReadAll (res .Body )
240
+ if err != nil {
241
+ log .Panic (err )
242
+ }
233
243
244
+ log .Println (string (body ))
234
245
235
- vaultResponse := VaultUnsealResponse {}
236
- err = json .Unmarshal (body , & vaultResponse )
237
- if err != nil {
238
- log .Panic (err )
239
- }
246
+ vaultResponse := VaultUnsealResponse {}
247
+ err = json .Unmarshal (body , & vaultResponse )
248
+ if err != nil {
249
+ log .Panic (err )
250
+ }
240
251
241
- viper .Set ("vault.token" , vaultResponse .RootToken )
242
- viper .Set ("vault.unseal-keys" , vaultResponse )
243
- viper .WriteConfig ()
252
+ viper .Set ("vault.token" , vaultResponse .RootToken )
253
+ viper .Set ("vault.unseal-keys" , vaultResponse )
254
+ viper .WriteConfig ()
255
+ } else {
256
+ log .Println ("vault token already exists, continuing" )
257
+ }
244
258
}
245
259
246
260
func waitGitlabToBeReady (dryRun bool ) {
0 commit comments