diff --git a/pkg/infra/terraform/init.go b/pkg/infra/terraform/init.go index b9d992ffa9a4fed0be73c5ce0ef49ecdd71cf9ff..e52a13bd90a7a3ee2f0bb85a65ff40f38f378e63 100644 --- a/pkg/infra/terraform/init.go +++ b/pkg/infra/terraform/init.go @@ -20,6 +20,7 @@ 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" @@ -35,7 +36,7 @@ func unpack(workingDir string, platform string, target string) (err error) { } // terraform init -func tfInit(workingDir string, platform string, target string, terraformBinary string, providers []prov.Provider) (err error) { +func TFInit(workingDir string, platform string, target string, terraformBinary string, providers []prov.Provider) (err error) { err = unpack(workingDir, platform, target) if err != nil { return errors.Wrap(err, "failed to unpack terraform modules") diff --git a/pkg/infra/terraform/logger.go b/pkg/infra/terraform/logger.go index bfb609d2d3eb569e926b5fd632b66ab69eee167d..54add90bd6e1daa6d645206dae61d1cf1f201a2c 100644 --- a/pkg/infra/terraform/logger.go +++ b/pkg/infra/terraform/logger.go @@ -16,12 +16,22 @@ limitations under the License. package terraform +import ( + "github.com/sirupsen/logrus" +) + type printfer struct { + logger *logrus.Logger + level logrus.Level } func newPrintfer() *printfer { - return &printfer{} + return &printfer{ + logger: logrus.StandardLogger(), + level: logrus.DebugLevel, + } } func (p *printfer) Printf(format string, ifs ...interface{}) { + p.logger.Logf(p.level, format, ifs...) } diff --git a/pkg/infra/terraform/providers/providers.go b/pkg/infra/terraform/providers/providers.go new file mode 100644 index 0000000000000000000000000000000000000000..986ee8aa947bbe9f3e5489758a4358ae1c29e847 --- /dev/null +++ b/pkg/infra/terraform/providers/providers.go @@ -0,0 +1,35 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package providers + +import "fmt" + +var ( + OpenStack = provider("openstack") +) + +type Provider struct { + Name string + Source string +} + +func provider(name string) Provider { + return Provider{ + Name: name, + Source: fmt.Sprintf("nkd/local/%s", name), + } +} diff --git a/pkg/infra/terraform/stage.go b/pkg/infra/terraform/stage.go new file mode 100644 index 0000000000000000000000000000000000000000..1fbe53d9ccfdcda0cf26e00528ae8f4b92a5de29 --- /dev/null +++ b/pkg/infra/terraform/stage.go @@ -0,0 +1,32 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package terraform + +import "gitee.com/openeuler/nestos-kubernetes-deployer/pkg/infra/terraform/providers" + +type Stage interface { + Name() string + + // terraform state file + StateFilename() string + + // outputs file + OutputsFilename() string + + // the list of providers that are used for the stage + Providers() []providers.Provider +} diff --git a/pkg/infra/terraform/terraform.go b/pkg/infra/terraform/terraform.go index c4a8922bfaca18cb19099dbd90fef7c452c7f7b6..e961f8d351fc436fed86f6f220ee7eed330c356d 100644 --- a/pkg/infra/terraform/terraform.go +++ b/pkg/infra/terraform/terraform.go @@ -22,7 +22,9 @@ import ( "path/filepath" "github.com/hashicorp/terraform-exec/tfexec" + "github.com/openshift/installer/pkg/lineprinter" "github.com/pkg/errors" + "github.com/sirupsen/logrus" ) func newTFExec(workingDir string, terraformBinary string) (*tfexec.Terraform, error) { @@ -32,9 +34,25 @@ func newTFExec(workingDir string, terraformBinary string) (*tfexec.Terraform, er return nil, err } - // TODO 日志打印 - tf.SetStdout(os.Stdout) - tf.SetStderr(os.Stderr) + // If the log path is not set, terraform will not receive debug logs. + if path, ok := os.LookupEnv("TEREFORM_LOG_PATH"); ok { + if err := tf.SetLog(os.Getenv("TEREFORM_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(path) //nolint:errcheck + } + } + + // Add terraform info logs to the installer log + lpPrint := &lineprinter.LinePrinter{Print: (&lineprinter.Trimmer{WrappedPrint: logrus.Print}).Print} + lpError := &lineprinter.LinePrinter{Print: (&lineprinter.Trimmer{WrappedPrint: logrus.Error}).Print} + defer lpPrint.Close() + defer lpError.Close() + + tf.SetStdout(lpPrint) + tf.SetStderr(lpError) tf.SetLogger(newPrintfer()) return tf, nil @@ -42,7 +60,7 @@ 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 { + if err := TFInit(workingDir, platform, stage.Name(), terraformBinary, stage.Providers()); err != nil { return err } @@ -51,18 +69,13 @@ func TFApply(workingDir string, platform string, stage Stage, terraformBinary st return errors.Wrap(err, "failed to create a new tfexec.") } - // TODO 日志打印 - tf.SetStdout(os.Stdout) - tf.SetStderr(os.Stderr) - tf.SetLogger(newPrintfer()) - err = tf.Apply(context.Background(), applyOpts...) return errors.Wrap(err, "failed to apply Terraform.") } // 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 { + if err := TFInit(workingDir, platform, stage.Name(), terraformBinary, stage.Providers()); err != nil { return err } @@ -71,11 +84,6 @@ func TFDestroy(workingDir string, platform string, stage Stage, terraformBinary return errors.Wrap(err, "failed to create a new tfexec.") } - // TODO 日志打印 - tf.SetStdout(os.Stdout) - tf.SetStderr(os.Stderr) - tf.SetLogger(newPrintfer()) - return errors.Wrap( tf.Destroy(context.Background(), destroyOpts...), "failed doing terraform destroy.",