2022-09-05 09:06:08 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-13 13:01:38 +02:00
|
|
|
package cloudcmd
|
2022-03-22 16:03:15 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-07 12:56:25 +01:00
|
|
|
"errors"
|
2022-03-22 16:03:15 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-04-14 14:15:07 +02:00
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
2023-08-24 16:38:02 +02:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2022-03-22 16:03:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// rollbacker does a rollback.
|
|
|
|
type rollbacker interface {
|
2023-08-24 16:38:02 +02:00
|
|
|
rollback(ctx context.Context, w io.Writer, logLevel terraform.LogLevel) error
|
2022-03-22 16:03:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// rollbackOnError calls rollback on the rollbacker if the handed error is not nil,
|
|
|
|
// and writes logs to the writer w.
|
2023-04-14 14:15:07 +02:00
|
|
|
func rollbackOnError(w io.Writer, onErr *error, roll rollbacker, logLevel terraform.LogLevel) {
|
2022-03-22 16:03:15 +01:00
|
|
|
if *onErr == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "An error occurred: %s\n", *onErr)
|
|
|
|
fmt.Fprintln(w, "Attempting to roll back.")
|
2023-08-24 16:38:02 +02:00
|
|
|
if err := roll.rollback(context.Background(), w, logLevel); err != nil {
|
2023-06-01 12:33:06 +02:00
|
|
|
*onErr = errors.Join(*onErr, fmt.Errorf("on rollback: %w", err)) // TODO(katexochen): print the error, or return it?
|
2022-03-22 16:03:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, "Rollback succeeded.")
|
|
|
|
}
|
|
|
|
|
2022-09-27 09:22:29 +02:00
|
|
|
type rollbackerTerraform struct {
|
2023-10-31 12:46:40 +01:00
|
|
|
client tfDestroyer
|
2022-09-26 15:52:31 +02:00
|
|
|
}
|
|
|
|
|
2023-08-24 16:38:02 +02:00
|
|
|
func (r *rollbackerTerraform) rollback(ctx context.Context, w io.Writer, logLevel terraform.LogLevel) error {
|
2023-04-14 14:15:07 +02:00
|
|
|
if err := r.client.Destroy(ctx, logLevel); err != nil {
|
2023-08-24 16:38:02 +02:00
|
|
|
fmt.Fprintf(w, "Could not destroy the resources. Please delete the %q directory manually if no resources were created\n",
|
|
|
|
constants.TerraformWorkingDir)
|
2023-02-07 15:19:59 +01:00
|
|
|
return err
|
2022-10-26 15:57:00 +02:00
|
|
|
}
|
2023-02-07 15:19:59 +01:00
|
|
|
return r.client.CleanUpWorkspace()
|
2022-09-26 15:52:31 +02:00
|
|
|
}
|
2022-10-05 09:11:30 +02:00
|
|
|
|
|
|
|
type rollbackerQEMU struct {
|
2023-10-31 12:46:40 +01:00
|
|
|
client tfDestroyer
|
2023-10-23 10:20:25 +02:00
|
|
|
libvirt libvirtRunner
|
2022-10-05 09:11:30 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 10:20:25 +02:00
|
|
|
func (r *rollbackerQEMU) rollback(ctx context.Context, w io.Writer, logLevel terraform.LogLevel) error {
|
|
|
|
tfErr := r.client.Destroy(ctx, logLevel)
|
|
|
|
libvirtErr := r.libvirt.Stop(ctx)
|
|
|
|
if err := errors.Join(tfErr, libvirtErr); err != nil {
|
2023-08-24 16:38:02 +02:00
|
|
|
fmt.Fprintf(w, "Could not destroy the resources. Please delete the %q directory manually if no resources were created\n",
|
|
|
|
constants.TerraformWorkingDir)
|
2023-10-23 10:20:25 +02:00
|
|
|
return err
|
2022-10-26 15:57:00 +02:00
|
|
|
}
|
2023-02-07 15:19:59 +01:00
|
|
|
return r.client.CleanUpWorkspace()
|
2022-10-05 09:11:30 +02:00
|
|
|
}
|