mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-12-27 00:19:36 -05:00
2d8fcd9bf4
Co-authored-by: Malte Poll <mp@edgeless.systems> Co-authored-by: katexochen <katexochen@users.noreply.github.com> Co-authored-by: Daniel Weiße <dw@edgeless.systems> Co-authored-by: Thomas Tendyck <tt@edgeless.systems> Co-authored-by: Benedict Schlueter <bs@edgeless.systems> Co-authored-by: leongross <leon.gross@rub.de> Co-authored-by: Moritz Eckert <m1gh7ym0@gmail.com>
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package cmd
|
|
|
|
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 retrun it?
|
|
return
|
|
}
|
|
fmt.Fprintln(w, "Rollback succeeded.")
|
|
}
|
|
|
|
type rollbackerGCP struct {
|
|
client gcpclient
|
|
}
|
|
|
|
func (r *rollbackerGCP) rollback(ctx context.Context) error {
|
|
var err error
|
|
err = multierr.Append(err, r.client.TerminateInstances(ctx))
|
|
err = multierr.Append(err, r.client.TerminateFirewall(ctx))
|
|
err = multierr.Append(err, r.client.TerminateVPCs(ctx))
|
|
return err
|
|
}
|
|
|
|
type rollbackerAzure struct {
|
|
client azureclient
|
|
}
|
|
|
|
func (r *rollbackerAzure) rollback(ctx context.Context) error {
|
|
return r.client.TerminateResourceGroup(ctx)
|
|
}
|
|
|
|
type rollbackerAWS struct {
|
|
client ec2client
|
|
}
|
|
|
|
func (r *rollbackerAWS) rollback(ctx context.Context) error {
|
|
var err error
|
|
err = multierr.Append(err, r.client.TerminateInstances(ctx))
|
|
err = multierr.Append(err, r.client.DeleteSecurityGroup(ctx))
|
|
return err
|
|
}
|