2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
package cloudcmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-10-05 03:11:30 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/libvirt"
|
2022-09-26 09:52:31 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
2022-11-14 12:18:58 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2022-04-13 07:01:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Terminator deletes cloud provider resources.
|
|
|
|
type Terminator struct {
|
2022-10-26 09:57:00 -04:00
|
|
|
newTerraformClient func(ctx context.Context) (terraformClient, error)
|
2022-10-05 03:11:30 -04:00
|
|
|
newLibvirtRunner func() libvirtRunner
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTerminator create a new cloud terminator.
|
|
|
|
func NewTerminator() *Terminator {
|
|
|
|
return &Terminator{
|
2022-10-26 09:57:00 -04:00
|
|
|
newTerraformClient: func(ctx context.Context) (terraformClient, error) {
|
2022-11-14 12:18:58 -05:00
|
|
|
return terraform.New(ctx, constants.TerraformWorkingDir)
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-10-05 03:11:30 -04:00
|
|
|
newLibvirtRunner: func() libvirtRunner {
|
|
|
|
return libvirt.New()
|
|
|
|
},
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 04:29:28 -04:00
|
|
|
// Terminate deletes the could provider resources.
|
2022-10-26 09:57:00 -04:00
|
|
|
func (t *Terminator) Terminate(ctx context.Context) (retErr error) {
|
|
|
|
defer func() {
|
|
|
|
if retErr == nil {
|
|
|
|
retErr = t.newLibvirtRunner().Stop(ctx)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cl, err := t.newTerraformClient(ctx)
|
2022-10-06 05:52:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer cl.RemoveInstaller()
|
|
|
|
|
|
|
|
return t.terminateTerraform(ctx, cl)
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
2022-09-26 09:52:31 -04:00
|
|
|
|
2022-09-27 03:22:29 -04:00
|
|
|
func (t *Terminator) terminateTerraform(ctx context.Context, cl terraformClient) error {
|
2023-02-13 02:42:54 -05:00
|
|
|
if err := cl.Destroy(ctx); err != nil {
|
2022-09-26 09:52:31 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-10-05 03:11:30 -04:00
|
|
|
return cl.CleanUpWorkspace()
|
|
|
|
}
|