@@ -4,6 +4,7 @@ package nodecmd
4
4
5
5
import (
6
6
"fmt"
7
+ "os"
7
8
"os/exec"
8
9
"strings"
9
10
@@ -225,51 +226,52 @@ func createEC2Instances(ec2Svc map[string]*awsAPI.AwsCloud,
225
226
if err != nil {
226
227
return instanceIDs , elasticIPs , sshCertPath , keyPairName , err
227
228
}
228
- if ! keyPairExists {
229
- switch {
230
- case useSSHAgent :
231
- ux .Logger .PrintToUser ("Using ssh agent identity %s to create key pair %s in AWS[%s]" , sshIdentity , keyPairName [region ], region )
232
- if err := ec2Svc [region ].UploadSSHIdentityKeyPair (regionConf [region ].Prefix , sshIdentity ); err != nil {
233
- return instanceIDs , elasticIPs , sshCertPath , keyPairName , err
234
- }
235
- case ! useSSHAgent && certInSSHDir :
236
- ux .Logger .PrintToUser ("Default Key Pair named %s already exists on your .ssh directory but not on AWS" , regionConf [region ].Prefix )
237
- ux .Logger .PrintToUser ("We need to create a new Key Pair in AWS as we can't find Key Pair named %s in AWS[%s]" , regionConf [region ].Prefix , region )
238
- keyPairName [region ], err = promptKeyPairName (ec2Svc [region ])
239
- if err != nil {
240
- return instanceIDs , elasticIPs , sshCertPath , keyPairName , err
241
- }
242
- if err := ec2Svc [region ].CreateAndDownloadKeyPair (regionConf [region ].Prefix , privKey ); err != nil {
243
- return instanceIDs , elasticIPs , sshCertPath , keyPairName , err
244
- }
245
- case ! useSSHAgent && ! certInSSHDir :
246
- ux .Logger .PrintToUser (fmt .Sprintf ("Creating new key pair %s in AWS[%s]" , keyPairName , region ))
247
- if err := ec2Svc [region ].CreateAndDownloadKeyPair (regionConf [region ].Prefix , privKey ); err != nil {
248
- return instanceIDs , elasticIPs , sshCertPath , keyPairName , err
229
+ if replaceKeyPair && ! forMonitoring {
230
+ // delete existing key pair on AWS console and download the newly created key pair file
231
+ // in .ssh dir (will overwrite existing file in .ssh dir)
232
+ if keyPairExists {
233
+ if err := ec2Svc [region ].DeleteKeyPair (regionConf [region ].Prefix ); err != nil {
234
+ return instanceIDs , elasticIPs , sshCertPath , keyPairName , fmt .Errorf ("unable to delete existing key pair %s in AWS console due to %w" , regionConf [region ].Prefix , err )
249
235
}
250
236
}
237
+ if err = os .RemoveAll (privKey ); err != nil {
238
+ return instanceIDs , elasticIPs , sshCertPath , keyPairName , fmt .Errorf ("unable to delete existing key pair file %s in .ssh dir due to %w" , privKey , err )
239
+ }
240
+ if err := ec2Svc [region ].CreateAndDownloadKeyPair (regionConf [region ].Prefix , privKey ); err != nil {
241
+ return instanceIDs , elasticIPs , sshCertPath , keyPairName , err
242
+ }
251
243
} else {
252
- // keypair exists
253
- switch {
254
- case useSSHAgent :
255
- ux .Logger .PrintToUser ("Using existing key pair %s in AWS[%s] via ssh-agent" , keyPairName [region ], region )
256
- case ! useSSHAgent && certInSSHDir :
257
- ux . Logger . PrintToUser ( "Using existing key pair %s in AWS[%s]" , keyPairName [ region ], region )
258
- case ! useSSHAgent && ! certInSSHDir :
259
- if replaceKeyPair {
260
- // delete default key pair in .ssh dir and recreate default key pair
261
- // in both AWS console and store it in .ssh dir
262
- privKey , err = app . GetSSHCertFilePath ( keyPairName [region ] + constants . CertSuffix )
244
+ if ! keyPairExists {
245
+ switch {
246
+ case useSSHAgent :
247
+ ux .Logger .PrintToUser ("Using ssh agent identity %s to create key pair %s in AWS[%s]" , sshIdentity , keyPairName [region ], region )
248
+ if err := ec2Svc [ region ]. UploadSSHIdentityKeyPair ( regionConf [ region ]. Prefix , sshIdentity ); err != nil {
249
+ return instanceIDs , elasticIPs , sshCertPath , keyPairName , err
250
+ }
251
+ case ! useSSHAgent && certInSSHDir :
252
+ ux . Logger . PrintToUser ( "Default Key Pair named %s already exists on your .ssh directory but not on AWS" , regionConf [ region ]. Prefix )
253
+ ux . Logger . PrintToUser ( "We need to create a new Key Pair in AWS as we can't find Key Pair named %s in AWS[%s]" , regionConf [ region ]. Prefix , region )
254
+ keyPairName [ region ] , err = promptKeyPairName ( ec2Svc [region ])
263
255
if err != nil {
264
256
return instanceIDs , elasticIPs , sshCertPath , keyPairName , err
265
257
}
266
- if err := ec2Svc [region ].DeleteKeyPair (regionConf [region ].Prefix ); err != nil {
258
+ if err := ec2Svc [region ].CreateAndDownloadKeyPair (regionConf [region ].Prefix , privKey ); err != nil {
267
259
return instanceIDs , elasticIPs , sshCertPath , keyPairName , err
268
260
}
269
- if err := ec2Svc [region ].CreateAndDownloadKeyPair (keyPairName [region ], privKey ); err != nil {
261
+ case ! useSSHAgent && ! certInSSHDir :
262
+ ux .Logger .PrintToUser (fmt .Sprintf ("Creating new key pair %s in AWS[%s]" , keyPairName , region ))
263
+ if err := ec2Svc [region ].CreateAndDownloadKeyPair (regionConf [region ].Prefix , privKey ); err != nil {
270
264
return instanceIDs , elasticIPs , sshCertPath , keyPairName , err
271
265
}
272
- } else {
266
+ }
267
+ } else {
268
+ // keypair exists
269
+ switch {
270
+ case useSSHAgent :
271
+ ux .Logger .PrintToUser ("Using existing key pair %s in AWS[%s] via ssh-agent" , keyPairName [region ], region )
272
+ case ! useSSHAgent && certInSSHDir :
273
+ ux .Logger .PrintToUser ("Using existing key pair %s in AWS[%s]" , keyPairName [region ], region )
274
+ case ! useSSHAgent && ! certInSSHDir :
273
275
ux .Logger .PrintToUser ("Default Key Pair named %s already exists in AWS[%s]" , keyPairName [region ], region )
274
276
ux .Logger .PrintToUser ("We need to create a new Key Pair in AWS as we can't find Key Pair named %s in your .ssh directory" , keyPairName [region ])
275
277
keyPairName [region ], err = promptKeyPairName (ec2Svc [region ])
0 commit comments