From 82bd5c87748babf1ed64f118695063eae2c69f82 Mon Sep 17 00:00:00 2001 From: Pragnesh Date: Sat, 2 Dec 2023 21:25:27 +0530 Subject: [PATCH] if ssh_key_id is specified skip generating temp ssh key --- builder/digitalocean/builder.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/builder/digitalocean/builder.go b/builder/digitalocean/builder.go index 899dee4..0532410 100644 --- a/builder/digitalocean/builder.go +++ b/builder/digitalocean/builder.go @@ -102,19 +102,24 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) state.Put("hook", hook) state.Put("ui", ui) + // Only generate the temp key pair if one is not already provided + genTempKeyPair := b.config.SSHKeyID == 0 || b.config.Comm.SSHPrivateKeyFile == "" + // Build the steps steps := []multistep.Step{ - &communicator.StepSSHKeyGen{ - CommConf: &b.config.Comm, - SSHTemporaryKeyPair: b.config.Comm.SSH.SSHTemporaryKeyPair, - }, + multistep.If(genTempKeyPair, + &communicator.StepSSHKeyGen{ + CommConf: &b.config.Comm, + SSHTemporaryKeyPair: b.config.Comm.SSH.SSHTemporaryKeyPair, + }, + ), multistep.If(b.config.PackerDebug && b.config.Comm.SSHPrivateKeyFile == "", &communicator.StepDumpSSHKey{ Path: fmt.Sprintf("do_%s.pem", b.config.PackerBuildName), SSH: &b.config.Comm.SSH, }, ), - &stepCreateSSHKey{}, + multistep.If(genTempKeyPair, new(stepCreateSSHKey)), new(stepCreateDroplet), new(stepDropletInfo), &communicator.StepConnect{ @@ -123,9 +128,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) SSHConfig: b.config.Comm.SSHConfigFunc(), }, new(commonsteps.StepProvision), - &commonsteps.StepCleanupTempKeys{ - Comm: &b.config.Comm, - }, + multistep.If(genTempKeyPair, + &commonsteps.StepCleanupTempKeys{ + Comm: &b.config.Comm, + }, + ), new(stepShutdown), new(stepPowerOff), &stepSnapshot{