1
0
mirror of https://github.com/openshift/installer.git synced 2026-02-06 00:48:45 +01:00

pkg/terraform: Modify some helper functions for the new binary layout.

This assumes that we ship the installer binary with the same dir
as the terraform templates.
This commit is contained in:
Yifan Gu
2018-09-19 17:51:16 -07:00
committed by Alex Crawford
parent c60cabd1ee
commit ff5a57b016
5 changed files with 66 additions and 43 deletions

View File

@@ -40,11 +40,11 @@ func DestroyWorkflow(clusterDir string, contOnErr bool) Workflow {
}
func destroyAssetsStep(m *metadata) error {
return runDestroyStep(m, assetsStep)
return runDestroyStep(m, terraform.AssetsStep)
}
func destroyInfraStep(m *metadata) error {
return runDestroyStep(m, infraStep)
return runDestroyStep(m, terraform.InfraStep)
}
func destroyWorkersStep(m *metadata) error {
@@ -132,7 +132,13 @@ func runDestroyStep(m *metadata, step string, extraArgs ...string) error {
// there is no statefile, therefore nothing to destroy for this step
return nil
}
templateDir, err := findStepTemplates(step, m.cluster.Platform)
dir, err := baseLocation()
if err != nil {
return err
}
templateDir, err := terraform.FindStepTemplates(dir, step, m.cluster.Platform)
if err != nil {
return err
}

View File

@@ -30,22 +30,27 @@ func InstallWorkflow(clusterDir string) Workflow {
}
func installAssetsStep(m *metadata) error {
return runInstallStep(m, assetsStep)
return runInstallStep(m, terraform.AssetsStep)
}
func installInfraStep(m *metadata) error {
return runInstallStep(m, infraStep)
return runInstallStep(m, terraform.InfraStep)
}
func runInstallStep(m *metadata, step string, extraArgs ...string) error {
templateDir, err := findStepTemplates(step, m.cluster.Platform)
dir, err := baseLocation()
if err != nil {
return err
}
templateDir, err := terraform.FindStepTemplates(dir, step, m.cluster.Platform)
if err != nil {
return err
}
if err := terraform.Init(m.clusterDir, templateDir); err != nil {
return err
}
return terraform.Apply(m.clusterDir, step, templateDir, extraArgs...)
_, err = terraform.Apply(m.clusterDir, step, templateDir, extraArgs...)
return err
}
func generateIgnConfigStep(m *metadata) error {

View File

@@ -13,43 +13,11 @@ import (
)
const (
assetsStep = "assets"
bootstrapStep = "bootstrap"
binaryPrefix = "installer"
configFileName = "config.yaml"
infraStep = "infra"
internalFileName = "internal.yaml"
stepsBaseDir = "steps"
)
// returns the directory containing templates for a given step. If platform is
// specified, it looks for a subdirectory with platform first, falling back if
// there are no platform-specific templates for that step
func findStepTemplates(stepName string, platform config.Platform) (string, error) {
base, err := baseLocation()
if err != nil {
return "", fmt.Errorf("error looking up step %s templates: %v", stepName, err)
}
stepDir := filepath.Join(base, stepsBaseDir, stepName)
for _, path := range []string{
filepath.Join(stepDir, string(platform)),
stepDir} {
stat, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
continue
}
return "", fmt.Errorf("invalid path for %q templates: %s", base, err)
}
if !stat.IsDir() {
return "", fmt.Errorf("invalid path for %q templates", base)
}
return path, nil
}
return "", os.ErrNotExist
}
func generateClusterConfigMaps(m *metadata) error {
clusterGeneratedPath := filepath.Join(m.clusterDir, generatedPath)
if err := os.MkdirAll(clusterGeneratedPath, os.ModeDir|0755); err != nil {

View File

@@ -9,4 +9,5 @@ go_library(
],
importpath = "github.com/openshift/installer/pkg/terraform",
visibility = ["//visibility:public"],
deps = ["//pkg/types/config:go_default_library"],
)

View File

@@ -3,7 +3,24 @@ package terraform
import (
"fmt"
"os"
"path"
"path/filepath"
"github.com/openshift/installer/pkg/types/config"
)
const (
// AssetsStep is the name of the step that generates the assets.
// This is deprecated.
// TODO(yifan) Remove this when removing the asset step.
AssetsStep = "assets"
// BootstrapStep is the name of the step that runs the bootstrap.
BootstrapStep = "bootstrap"
// InfraStep is the name of the step that sets up infra.
InfraStep = "infra"
stepsBaseDir = "steps"
)
func terraformExec(clusterDir string, args ...string) error {
@@ -21,16 +38,18 @@ func terraformExec(clusterDir string, args ...string) error {
}
// Apply runs "terraform apply" with the given clusterDir, templateDir and extra args.
// It outputs the tfstate file.
func Apply(clusterDir string, state string, templateDir string, extraArgs ...string) error {
// It returns the absolute path of the tfstate file.
func Apply(clusterDir string, state string, templateDir string, extraArgs ...string) (string, error) {
stateFileName := fmt.Sprintf("%s.tfstate", state)
defaultArgs := []string{
"apply",
"-auto-approve",
fmt.Sprintf("-state=%s.tfstate", state),
fmt.Sprintf("-state=%s", stateFileName),
}
extraArgs = append(extraArgs, templateDir)
args := append(defaultArgs, extraArgs...)
return terraformExec(clusterDir, args...)
return path.Join(clusterDir, stateFileName), terraformExec(clusterDir, args...)
}
// Destroy runs "terraform destory" with the given clusterDir, templateDir, state file
@@ -57,3 +76,27 @@ func HasStateFile(stateDir string, stateFile string) bool {
_, err := os.Stat(stepStateFile)
return !os.IsNotExist(err)
}
// FindStepTemplates returns the directory containing templates for a given step.
// If platform is specified, it looks for a subdirectory with platform first,
// falling back if there are no platform-specific templates for that step.
func FindStepTemplates(dir, stepName string, platform config.Platform) (string, error) {
stepDir := filepath.Join(dir, stepsBaseDir, stepName)
for _, path := range []string{
filepath.Join(stepDir, string(platform)),
stepDir} {
stat, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
continue
}
return "", fmt.Errorf("invalid path for %q templates: %s", path, err)
}
if !stat.IsDir() {
return "", fmt.Errorf("invalid path for %q templates", path)
}
return path, nil
}
return "", os.ErrNotExist
}