@@ -21,8 +21,8 @@ getAwsCredential()
21
21
const aws = AwsAdapter . getInstance ( ) ;
22
22
return aws . verifyAccessPermission ( ) ;
23
23
} )
24
- . then ( data => console . log ( data ) )
25
- . catch ( err => console . log ( err ) ) ;
24
+ . then ( ( data ) => console . log ( data ) )
25
+ . catch ( ( err ) => console . log ( err ) ) ;
26
26
27
27
export function generateDockerfile ( options ) {
28
28
let base_image_url = options [ 'base_image_url' ] ;
@@ -72,25 +72,39 @@ ${pip}`;
72
72
return dockerfileContent ;
73
73
}
74
74
75
- export async function getCredential ( keyname ) {
76
- const credentials = await keytar . findCredentials ( keyname ) ;
75
+ export async function getCredential (
76
+ keyName : string
77
+ ) : Promise < { account : string ; password : string } > {
78
+ const credentials = await keytar . findCredentials ( keyName ) ;
77
79
if ( ! credentials || credentials . length === 0 ) {
78
- return null ;
80
+ return { account : '' , password : '' } ;
79
81
}
80
- console . log ( '[Get ' + keyname + ' Credential]' , credentials [ 0 ] ) ;
82
+ console . log ( '[Get ' + keyName + ' Credential]' , credentials [ 0 ] ) ;
81
83
return credentials [ 0 ] ;
82
84
}
83
85
84
- export async function saveCredential ( keyname , account , password ) {
85
- const existCredential = await getCredential ( keyname ) ;
86
- if ( existCredential ) {
87
- await keytar . deletePassword ( keyname , existCredential . account ) ;
86
+ export async function saveCredential (
87
+ keyName : string ,
88
+ account : string ,
89
+ password : string
90
+ ) : Promise < { account : string ; password : string } > {
91
+ const existCredential = await getCredential ( keyName ) ;
92
+ if ( existCredential . account ) {
93
+ await keytar . deletePassword ( keyName , existCredential . account ) ;
88
94
}
89
- await keytar . setPassword ( keyname , account , password ) ;
90
- console . log ( '[' + keyname + ' Credential Saved]' ) ;
95
+ await keytar . setPassword ( keyName , account , password ) ;
96
+ console . log ( '[' + keyName + ' Credential Saved]' ) ;
91
97
return { account, password } ;
92
98
}
93
99
100
+ export async function deleteCredential ( keyName : string ) {
101
+ const existCredential = await getCredential ( keyName ) ;
102
+ if ( existCredential . account ) {
103
+ await keytar . deletePassword ( keyName , existCredential . account ) ;
104
+ }
105
+ console . log ( '[' + keyName + ' Credential Deleted]' ) ;
106
+ }
107
+
94
108
export function writeDockerfile ( dockerfileContent ) {
95
109
fs . writeFileSync (
96
110
path . join ( config . workingDir , 'Dockerfile' ) ,
@@ -149,6 +163,14 @@ const handlers = {
149
163
}
150
164
await saveCredential ( awsRegionKeyName , 'region' , region ) ;
151
165
} ,
166
+ 'delete-dockerhub-credential' : async ( ) =>
167
+ await deleteCredential ( dockerHubCredentialKeyName ) ,
168
+ 'delete-primehub-credential' : async ( ) =>
169
+ await deleteCredential ( primeHubCredentialKeyName ) ,
170
+ 'delete-aws-credential' : async ( ) => {
171
+ await deleteCredential ( awsCredentialKeyName ) ;
172
+ await deleteCredential ( awsRegionKeyName ) ;
173
+ } ,
152
174
'build-status' : async ( ) => {
153
175
return handlers . build_status ;
154
176
} ,
@@ -234,54 +256,74 @@ const handlers = {
234
256
const [ username , password ] = plaintext . split ( ':' ) ;
235
257
return { username, password } ;
236
258
}
237
- const { repoName, tag } = extractTrueName ( image_name ) ;
238
- const ecrRegistry = await AwsAdapter . getInstance ( ) . createEcrRepository (
239
- repoName
240
- ) ;
241
- const ecrToken = await AwsAdapter . getInstance ( ) . getAuthorizationToken ( ) ;
242
- const authToken = ecrToken . authorizationData [ 0 ] . authorizationToken ;
243
- const endpoint = ecrToken . authorizationData [ 0 ] . proxyEndpoint ;
244
- const { username, password } = extractAuthPassword ( authToken ) ;
245
- const auth = {
246
- username : username ,
247
- password : password ,
248
- serveraddress : endpoint ,
249
- } ;
250
259
251
- // Generate Image Tag for ECR
252
- const ecrImageName = `${ ecrRegistry . repositoryUri } :${ tag } ` ;
253
- await docker
254
- . getImage ( image_name )
255
- . tag ( { repo : ecrRegistry . repositoryUri , tag : tag } ) ;
260
+ try {
261
+ const { repoName, tag } = extractTrueName ( image_name ) ;
262
+ const ecrRegistry = await AwsAdapter . getInstance ( ) . createEcrRepository (
263
+ repoName
264
+ ) ;
265
+ const ecrToken = await AwsAdapter . getInstance ( ) . getAuthorizationToken ( ) ;
266
+ const authToken = ecrToken . authorizationData [ 0 ] . authorizationToken ;
267
+ const endpoint = ecrToken . authorizationData [ 0 ] . proxyEndpoint ;
268
+ const { username, password } = extractAuthPassword ( authToken ) ;
269
+ const auth = {
270
+ username : username ,
271
+ password : password ,
272
+ serveraddress : endpoint ,
273
+ } ;
256
274
257
- console . log ( `[Push Image] ${ ecrImageName } ` , ecrRegistry ) ;
258
- const push_stream = await docker . getImage ( ecrImageName ) . push ( {
259
- authconfig : auth ,
260
- } ) ;
275
+ // Generate Image Tag for ECR
276
+ const ecrImageName = `${ ecrRegistry . repositoryUri } :${ tag } ` ;
277
+ await docker
278
+ . getImage ( image_name )
279
+ . tag ( { repo : ecrRegistry . repositoryUri , tag : tag } ) ;
261
280
262
- const log_ipc_name = `push-log-${ ecrImageName } ` ;
263
- docker . modem . followProgress (
264
- push_stream ,
265
- ( err , output ) => {
266
- handlers . build_status = 'finished' ;
267
- console . log ( 'push finished' , err , output ) ;
268
- send ( log_ipc_name , {
269
- stage : 'finished' ,
270
- error : err ,
271
- output : output ,
281
+ console . log ( `[Push Image] ${ ecrImageName } ` , ecrRegistry ) ;
282
+ const push_stream = await docker . getImage ( ecrImageName ) . push ( {
283
+ authconfig : auth ,
284
+ } ) ;
285
+
286
+ const log_ipc_name = `push-log-${ ecrImageName } ` ;
287
+ docker . modem . followProgress (
288
+ push_stream ,
289
+ ( err , output ) => {
290
+ handlers . build_status = 'finished' ;
291
+ console . log ( 'push finished' , err , output ) ;
292
+ send ( log_ipc_name , {
293
+ stage : 'finished' ,
294
+ error : err ,
295
+ output : output ,
296
+ } ) ;
297
+ } ,
298
+ ( event ) => {
299
+ console . log ( event ) ;
300
+ send ( log_ipc_name , {
301
+ stage : 'progressing' ,
302
+ output : event ,
303
+ } ) ;
304
+ handlers . build_events . push ( event ) ;
305
+ }
306
+ ) ;
307
+ return log_ipc_name ;
308
+ } catch ( error ) {
309
+ async function sleep ( time : number ) : Promise < void > {
310
+ return new Promise < void > ( ( res , rej ) => {
311
+ setTimeout ( res , time ) ;
272
312
} ) ;
273
- } ,
274
- ( event ) => {
275
- console . log ( event ) ;
276
- send ( log_ipc_name , {
277
- stage : 'progressing' ,
278
- output : event ,
313
+ }
314
+ async function throwErrorByIpc ( ipc_name : string , error ) {
315
+ await sleep ( 1000 ) ;
316
+ send ( ipc_name , {
317
+ stage : 'finished' ,
318
+ error : error ,
319
+ output : [ { error : `[AWS] ${ error . message } ` } ] ,
279
320
} ) ;
280
- handlers . build_events . push ( event ) ;
281
321
}
282
- ) ;
283
-
284
- return log_ipc_name ;
322
+ const error_ipc_name = `push-log-${ image_name } ` ;
323
+ console . log ( 'push error' , error . message ) ;
324
+ throwErrorByIpc ( error_ipc_name , error ) ;
325
+ return error_ipc_name ;
326
+ }
285
327
} ,
286
328
'get-image-detail' : async ( { image_name } ) => {
287
329
const image = await docker . getImage ( image_name ) ;
@@ -302,7 +344,7 @@ const handlers = {
302
344
return { repoName, tag : tag || 'latest' } ;
303
345
}
304
346
const credential = await getCredential ( dockerHubCredentialKeyName ) ;
305
- if ( ! credential ) {
347
+ if ( ! credential . account || ! credential . password ) {
306
348
return null ;
307
349
}
308
350
const auth = {
0 commit comments