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
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
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 {
|
2022-09-26 09:52:31 -04:00
|
|
|
*onErr = multierr.Append(*onErr, fmt.Errorf("on rollback: %w", err)) // TODO: print the error, or return it?
|
2022-03-22 11:03:15 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, "Rollback succeeded.")
|
|
|
|
}
|
|
|
|
|
|
|
|
type rollbackerGCP struct {
|
|
|
|
client gcpclient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rollbackerGCP) rollback(ctx context.Context) error {
|
|
|
|
var err error
|
2022-08-01 10:51:34 -04:00
|
|
|
err = multierr.Append(err, r.client.TerminateLoadBalancers(ctx))
|
2022-03-22 11:03:15 -04:00
|
|
|
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 {
|
2022-08-25 09:12:08 -04:00
|
|
|
return r.client.TerminateResourceGroupResources(ctx)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-09-26 09:52:31 -04:00
|
|
|
|
|
|
|
type rollbackerQEMU struct {
|
|
|
|
client qemuclient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rollbackerQEMU) rollback(ctx context.Context) error {
|
|
|
|
var err error
|
|
|
|
err = multierr.Append(err, r.client.DestroyCluster(ctx))
|
|
|
|
err = multierr.Append(err, r.client.CleanUpWorkspace())
|
|
|
|
return err
|
|
|
|
}
|