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.")
|
|
|
|
}
|
|
|
|
|
2022-09-27 03:22:29 -04:00
|
|
|
type rollbackerTerraform struct {
|
|
|
|
client terraformClient
|
2022-09-26 09:52:31 -04:00
|
|
|
}
|
|
|
|
|
2022-09-27 03:22:29 -04:00
|
|
|
func (r *rollbackerTerraform) rollback(ctx context.Context) error {
|
2022-09-26 09:52:31 -04:00
|
|
|
var err error
|
|
|
|
err = multierr.Append(err, r.client.DestroyCluster(ctx))
|
2022-10-26 09:57:00 -04:00
|
|
|
if err == nil {
|
|
|
|
err = multierr.Append(err, r.client.CleanUpWorkspace())
|
|
|
|
}
|
2022-09-26 09:52:31 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-10-05 03:11:30 -04:00
|
|
|
|
|
|
|
type rollbackerQEMU struct {
|
2022-11-15 08:00:44 -05:00
|
|
|
client terraformClient
|
|
|
|
libvirt libvirtRunner
|
|
|
|
createdWorkspace bool
|
2022-10-05 03:11:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rollbackerQEMU) rollback(ctx context.Context) error {
|
|
|
|
var err error
|
2022-11-15 08:00:44 -05:00
|
|
|
if r.createdWorkspace {
|
|
|
|
err = multierr.Append(err, r.client.DestroyCluster(ctx))
|
|
|
|
}
|
2022-10-05 03:11:30 -04:00
|
|
|
err = multierr.Append(err, r.libvirt.Stop(ctx))
|
2022-10-26 09:57:00 -04:00
|
|
|
if err == nil {
|
|
|
|
err = r.client.CleanUpWorkspace()
|
|
|
|
}
|
2022-10-05 03:11:30 -04:00
|
|
|
return err
|
|
|
|
}
|