From 52f9805ff44ac3f161f084cfab9edead992b8c81 Mon Sep 17 00:00:00 2001 From: shinny-taojiachun Date: Tue, 23 Apr 2024 11:12:09 +0800 Subject: [PATCH] =?UTF-8?q?detach=20key=E5=89=8D=E5=85=88=E5=85=B3?= =?UTF-8?q?=E6=9C=BA=EF=BC=8C=E9=81=BF=E5=85=8D=E5=88=9B=E5=BB=BA=E9=95=9C?= =?UTF-8?q?=E5=83=8F=E8=BF=87=E7=A8=8B=E4=B8=AD=E5=8F=91=E7=94=9F=E9=87=8D?= =?UTF-8?q?=E5=90=AF=E5=AF=BC=E8=87=B4=E6=9C=8D=E5=8A=A1=E6=84=8F=E5=A4=96?= =?UTF-8?q?=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cvm/step_detach_temp_key_pair.go | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/builder/tencentcloud/cvm/step_detach_temp_key_pair.go b/builder/tencentcloud/cvm/step_detach_temp_key_pair.go index b2125164..53f96bdf 100644 --- a/builder/tencentcloud/cvm/step_detach_temp_key_pair.go +++ b/builder/tencentcloud/cvm/step_detach_temp_key_pair.go @@ -24,13 +24,31 @@ func (s *stepDetachTempKeyPair) Run(ctx context.Context, state multistep.StateBa keyId := state.Get("temporary_key_pair_id").(string) instance := state.Get("instance").(*cvm.Instance) + // 生成镜像前先关机,避免创建镜像过程中发生重启导致服务意外启动 + Say(state, *instance.InstanceName, "Trying to stop instance") + + stopReq := cvm.NewStopInstancesRequest() + stopReq.InstanceIds = []*string{instance.InstanceId} + err := Retry(ctx, func(ctx context.Context) error { + _, e := client.StopInstances(stopReq) + return e + }) + if err != nil { + return Halt(state, err, "Failed to stop instance") + } + Message(state, "Waiting for instance stop", "") + err = WaitForInstance(ctx, client, *instance.InstanceId, "STOPPED", 1800) + if err != nil { + return Halt(state, err, "Failed to wait for instance to be stopped") + } + Say(state, keyId, "Trying to detach keypair") req := cvm.NewDisassociateInstancesKeyPairsRequest() req.KeyIds = []*string{&keyId} req.InstanceIds = []*string{instance.InstanceId} - req.ForceStop = common.BoolPtr(true) - err := Retry(ctx, func(ctx context.Context) error { + req.ForceStop = common.BoolPtr(false) + err = Retry(ctx, func(ctx context.Context) error { _, e := client.DisassociateInstancesKeyPairs(req) return e }) @@ -39,7 +57,7 @@ func (s *stepDetachTempKeyPair) Run(ctx context.Context, state multistep.StateBa } Message(state, "Waiting for keypair detached", "") - err = WaitForInstance(ctx, client, *instance.InstanceId, "RUNNING", 1800) + err = WaitForInstance(ctx, client, *instance.InstanceId, "STOPPED", 1800) if err != nil { return Halt(state, err, "Failed to wait for keypair detached") }