2023-07-24 04:30:53 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package upgrade
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
)
|
|
|
|
|
2023-07-26 11:29:03 -04:00
|
|
|
// IAMMigrateCmd is a terraform migration command for IAM. Which is used for the tfMigrationClient.
|
2023-07-24 04:30:53 -04:00
|
|
|
type IAMMigrateCmd struct {
|
2023-08-04 07:53:51 -04:00
|
|
|
tf tfIAMClient
|
|
|
|
upgradeID string
|
|
|
|
iamWorkspace string
|
|
|
|
upgradeWorkspace string
|
|
|
|
csp cloudprovider.Provider
|
|
|
|
logLevel terraform.LogLevel
|
2023-07-24 04:30:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewIAMMigrateCmd creates a new IAMMigrateCmd.
|
2023-08-04 07:53:51 -04:00
|
|
|
func NewIAMMigrateCmd(ctx context.Context, iamWorkspace, upgradeWorkspace, upgradeID string, csp cloudprovider.Provider, logLevel terraform.LogLevel) (*IAMMigrateCmd, error) {
|
|
|
|
tfClient, err := terraform.New(ctx, filepath.Join(upgradeWorkspace, upgradeID, constants.TerraformIAMUpgradeWorkingDir))
|
2023-07-24 04:30:53 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("setting up terraform client: %w", err)
|
|
|
|
}
|
|
|
|
return &IAMMigrateCmd{
|
2023-08-04 07:53:51 -04:00
|
|
|
tf: tfClient,
|
|
|
|
upgradeID: upgradeID,
|
|
|
|
iamWorkspace: iamWorkspace,
|
|
|
|
upgradeWorkspace: upgradeWorkspace,
|
|
|
|
csp: csp,
|
|
|
|
logLevel: logLevel,
|
2023-07-24 04:30:53 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the name of the command.
|
|
|
|
func (c *IAMMigrateCmd) String() string {
|
|
|
|
return "iam migration"
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpgradeID returns the upgrade ID.
|
|
|
|
func (c *IAMMigrateCmd) UpgradeID() string {
|
|
|
|
return c.upgradeID
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckTerraformMigrations checks whether Terraform migrations are possible in the current workspace.
|
|
|
|
func (c *IAMMigrateCmd) CheckTerraformMigrations(file file.Handler) error {
|
2023-08-04 07:53:51 -04:00
|
|
|
return checkTerraformMigrations(file, c.upgradeWorkspace, c.upgradeID, constants.TerraformIAMUpgradeBackupDir)
|
2023-07-24 04:30:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Plan prepares the upgrade workspace and plans the Terraform migrations for the Constellation upgrade, writing the plan to the outWriter.
|
|
|
|
func (c *IAMMigrateCmd) Plan(ctx context.Context, file file.Handler, outWriter io.Writer) (bool, error) {
|
|
|
|
templateDir := filepath.Join("terraform", "iam", strings.ToLower(c.csp.String()))
|
|
|
|
if err := terraform.PrepareIAMUpgradeWorkspace(file,
|
|
|
|
templateDir,
|
2023-08-04 07:53:51 -04:00
|
|
|
c.iamWorkspace,
|
|
|
|
filepath.Join(c.upgradeWorkspace, c.upgradeID, constants.TerraformIAMUpgradeWorkingDir),
|
|
|
|
filepath.Join(c.upgradeWorkspace, c.upgradeID, constants.TerraformIAMUpgradeBackupDir),
|
2023-07-24 04:30:53 -04:00
|
|
|
); err != nil {
|
|
|
|
return false, fmt.Errorf("preparing terraform workspace: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-04 07:53:51 -04:00
|
|
|
hasDiff, err := c.tf.Plan(ctx, c.logLevel)
|
2023-07-24 04:30:53 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("terraform plan: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasDiff {
|
2023-08-04 07:53:51 -04:00
|
|
|
if err := c.tf.ShowPlan(ctx, c.logLevel, outWriter); err != nil {
|
2023-07-24 04:30:53 -04:00
|
|
|
return false, fmt.Errorf("terraform show plan: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasDiff, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply applies the Terraform IAM migrations for the Constellation upgrade.
|
|
|
|
func (c *IAMMigrateCmd) Apply(ctx context.Context, fileHandler file.Handler) error {
|
2023-08-21 04:26:53 -04:00
|
|
|
if _, err := c.tf.ApplyIAM(ctx, c.csp, c.logLevel); err != nil {
|
2023-07-24 04:30:53 -04:00
|
|
|
return fmt.Errorf("terraform apply: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-04 07:53:51 -04:00
|
|
|
if err := fileHandler.RemoveAll(c.iamWorkspace); err != nil {
|
2023-07-24 04:30:53 -04:00
|
|
|
return fmt.Errorf("removing old terraform directory: %w", err)
|
|
|
|
}
|
2023-08-04 07:53:51 -04:00
|
|
|
if err := fileHandler.CopyDir(
|
|
|
|
filepath.Join(c.upgradeWorkspace, c.upgradeID, constants.TerraformIAMUpgradeWorkingDir),
|
|
|
|
c.iamWorkspace,
|
|
|
|
); err != nil {
|
2023-07-24 04:30:53 -04:00
|
|
|
return fmt.Errorf("replacing old terraform directory with new one: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-04 07:53:51 -04:00
|
|
|
if err := fileHandler.RemoveAll(filepath.Join(c.upgradeWorkspace, c.upgradeID, constants.TerraformIAMUpgradeWorkingDir)); err != nil {
|
2023-07-24 04:30:53 -04:00
|
|
|
return fmt.Errorf("removing terraform upgrade directory: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|