From 1e380ce251c2e433ee4e8bbe8d5edfc0a3d301af Mon Sep 17 00:00:00 2001 From: jianli-97 Date: Thu, 13 Jul 2023 15:15:03 +0800 Subject: [PATCH] modify terraform functions --- pkg/infra/terraform/init.go | 29 +++++-------------- pkg/infra/terraform/terraform.go | 49 ++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 43 deletions(-) diff --git a/pkg/infra/terraform/init.go b/pkg/infra/terraform/init.go index e52a13b..cbd3ad3 100644 --- a/pkg/infra/terraform/init.go +++ b/pkg/infra/terraform/init.go @@ -20,37 +20,22 @@ import ( "context" "path/filepath" - prov "gitee.com/openeuler/nestos-kubernetes-deployer/pkg/infra/terraform/providers" "github.com/hashicorp/terraform-exec/tfexec" - "github.com/openshift/installer/data" "github.com/pkg/errors" ) -func unpack(workingDir string, platform string, target string) (err error) { - err = data.Unpack(workingDir, filepath.Join(platform, target)) - if err != nil { - return err - } - - return nil -} - // terraform init -func TFInit(workingDir string, platform string, target string, terraformBinary string, providers []prov.Provider) (err error) { - err = unpack(workingDir, platform, target) +func TFInit(dir string, terraformDir string) error { + tf, err := newTFExec(dir, terraformDir) if err != nil { - return errors.Wrap(err, "failed to unpack terraform modules") + return errors.Wrap(err, "failed to create a new tfexec") } - tf, err := newTFExec(workingDir, terraformBinary) + // 使用本地terraform插件 + err = tf.Init(context.Background(), tfexec.PluginDir(filepath.Join(terraformDir, "plugins"))) if err != nil { - return errors.Wrap(err, "failed to create a new tfexec.") + return errors.Wrap(err, "failed to init terraform") } - // 如果想导入本地已有插件,需构建相应目录 - // 如openstack插件所需目录为plugins/registry.terraform.io/terraform-provider-openstack/openstack/1.51.1/linux_arm64/terraform-provider-openstack_v1.51.1 - return errors.Wrap( - tf.Init(context.Background(), tfexec.PluginDir(filepath.Join(terraformBinary, "plugins"))), - "failed doing terraform init.", - ) + return nil } diff --git a/pkg/infra/terraform/terraform.go b/pkg/infra/terraform/terraform.go index ba540da..f114dda 100644 --- a/pkg/infra/terraform/terraform.go +++ b/pkg/infra/terraform/terraform.go @@ -20,6 +20,7 @@ import ( "context" "os" "path/filepath" + "runtime" "github.com/hashicorp/terraform-exec/tfexec" "github.com/openshift/installer/pkg/lineprinter" @@ -27,21 +28,21 @@ import ( "github.com/sirupsen/logrus" ) -func newTFExec(workingDir string, terraformBinary string) (*tfexec.Terraform, error) { - execPath := filepath.Join(terraformBinary, "bin", "terraform") - tf, err := tfexec.NewTerraform(workingDir, execPath) +func newTFExec(dir string, terraformDir string) (*tfexec.Terraform, error) { + tfPath := filepath.Join(terraformDir, "bin", runtime.GOOS+"_"+runtime.GOARCH, "terraform") + tf, err := tfexec.NewTerraform(dir, tfPath) if err != nil { return nil, err } // If the log path is not set, terraform will not receive debug logs. - if logPath, ok := os.LookupEnv("TEREFORM_LOG_PATH"); ok { - if err := tf.SetLog(os.Getenv("TEREFORM_LOG")); err != nil { + if logPath, ok := os.LookupEnv("TERRAFORM_LOG_PATH"); ok { + if err := tf.SetLog(os.Getenv("TERRAFORM_LOG")); err != nil { logrus.Infof("Skipping setting terraform log levels: %v", err) } else { - tf.SetLogCore(os.Getenv("TEREFORM_LOG_CORE")) //nolint:errcheck - tf.SetLogProvider(os.Getenv("TEREFORM_LOG_PROVIDER")) //nolint:errcheck - tf.SetLogPath(logPath) //nolint:errcheck + tf.SetLogCore(os.Getenv("TERRAFORM_LOG_CORE")) //nolint:errcheck + tf.SetLogProvider(os.Getenv("TERRAFORM_LOG_PROVIDER")) //nolint:errcheck + tf.SetLogPath(logPath) //nolint:errcheck } } @@ -59,33 +60,39 @@ func newTFExec(workingDir string, terraformBinary string) (*tfexec.Terraform, er } // terraform apply -func TFApply(workingDir string, platform string, stage Stage, terraformBinary string, applyOpts ...tfexec.ApplyOption) error { - if err := TFInit(workingDir, platform, stage.Name(), terraformBinary, stage.Providers()); err != nil { +func TFApply(dir string, terraformDir string, applyOpts ...tfexec.ApplyOption) error { + if err := TFInit(dir, terraformDir); err != nil { return err } - tf, err := newTFExec(workingDir, terraformBinary) + tf, err := newTFExec(dir, terraformDir) if err != nil { - return errors.Wrap(err, "failed to create a new tfexec.") + return errors.Wrap(err, "failed to create a new tfexec") } err = tf.Apply(context.Background(), applyOpts...) - return errors.Wrap(err, "failed to apply Terraform.") + if err != nil { + return errors.Wrap(err, "failed to apply Terraform") + } + + return nil } // terraform destroy -func TFDestroy(workingDir string, platform string, stage Stage, terraformBinary string, destroyOpts ...tfexec.DestroyOption) error { - if err := TFInit(workingDir, platform, stage.Name(), terraformBinary, stage.Providers()); err != nil { +func TFDestroy(dir string, terraformDir string, destroyOpts ...tfexec.DestroyOption) error { + if err := TFInit(dir, terraformDir); err != nil { return err } - tf, err := newTFExec(workingDir, terraformBinary) + tf, err := newTFExec(dir, terraformDir) + if err != nil { + return errors.Wrap(err, "failed to destroy a new tfexec") + } + + err = tf.Destroy(context.Background(), destroyOpts...) if err != nil { - return errors.Wrap(err, "failed to create a new tfexec.") + return errors.Wrap(err, "failed to destroy terraform") } - return errors.Wrap( - tf.Destroy(context.Background(), destroyOpts...), - "failed doing terraform destroy.", - ) + return nil } -- Gitee