mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-10-01 01:36:09 -04:00
e66cb84d6e
* Dont clean up workspace if rollback fails * Remove dependency on CSP from terminate Signed-off-by: Daniel Weiße <dw@edgeless.systems>
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
/*
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
package cloudcmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
"go.uber.org/multierr"
|
|
)
|
|
|
|
// rollbacker does a rollback.
|
|
type rollbacker interface {
|
|
rollback(ctx context.Context) error
|
|
}
|
|
|
|
// rollbackOnError calls rollback on the rollbacker if the handed error is not nil,
|
|
// and writes logs to the writer w.
|
|
func rollbackOnError(ctx context.Context, w io.Writer, onErr *error, roll rollbacker) {
|
|
if *onErr == nil {
|
|
return
|
|
}
|
|
fmt.Fprintf(w, "An error occurred: %s\n", *onErr)
|
|
fmt.Fprintln(w, "Attempting to roll back.")
|
|
if err := roll.rollback(ctx); err != nil {
|
|
*onErr = multierr.Append(*onErr, fmt.Errorf("on rollback: %w", err)) // TODO: print the error, or return it?
|
|
return
|
|
}
|
|
fmt.Fprintln(w, "Rollback succeeded.")
|
|
}
|
|
|
|
type rollbackerTerraform struct {
|
|
client terraformClient
|
|
}
|
|
|
|
func (r *rollbackerTerraform) rollback(ctx context.Context) error {
|
|
var err error
|
|
err = multierr.Append(err, r.client.DestroyCluster(ctx))
|
|
if err == nil {
|
|
err = multierr.Append(err, r.client.CleanUpWorkspace())
|
|
}
|
|
return err
|
|
}
|
|
|
|
type rollbackerQEMU struct {
|
|
client terraformClient
|
|
libvirt libvirtRunner
|
|
}
|
|
|
|
func (r *rollbackerQEMU) rollback(ctx context.Context) error {
|
|
var err error
|
|
err = multierr.Append(err, r.client.DestroyCluster(ctx))
|
|
err = multierr.Append(err, r.libvirt.Stop(ctx))
|
|
if err == nil {
|
|
err = r.client.CleanUpWorkspace()
|
|
}
|
|
return err
|
|
}
|