-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathdeploy.go
More file actions
162 lines (147 loc) · 5.11 KB
/
deploy.go
File metadata and controls
162 lines (147 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2026 Google LLC
//
// 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 cmd defines command line utilities for gcluster
package cmd
import (
"fmt"
"hpc-toolkit/pkg/config"
"hpc-toolkit/pkg/logging"
"hpc-toolkit/pkg/modulewriter"
"hpc-toolkit/pkg/shell"
"path/filepath"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
func addDeployFlags(c *cobra.Command) *cobra.Command {
return addJsonOutputFlag(
addGroupSelectionFlags(
addAutoApproveFlag(
addArtifactsDirFlag(
addCreateFlags(c)))))
}
func init() {
rootCmd.AddCommand(deployCmd)
}
var (
deployCmd = addDeployFlags(&cobra.Command{
Use: "deploy (<DEPLOYMENT_DIRECTORY> | <BLUEPRINT_FILE>)",
Short: "deploy all resources in a Toolkit deployment directory.",
Long: "deploy all resources in a Toolkit deployment directory.",
Args: cobra.MatchAll(cobra.ExactArgs(1), checkExists),
ValidArgsFunction: filterYaml,
Run: runDeployCmd,
SilenceUsage: true,
})
)
func runDeployCmd(cmd *cobra.Command, args []string) {
var deplRoot string
if checkDir(cmd, args) != nil { // arg[0] is BLUEPRINT_FILE
deplRoot = doCreate(args[0])
} else { // arg[0] is DEPLOYMENT_DIRECTORY
deplRoot = args[0]
// check that no "create" flags were specified
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if f.Changed && createCmd.LocalFlags().Lookup(f.Name) != nil {
checkErr(fmt.Errorf("cannot specify flag %q with DEPLOYMENT_DIRECTORY provided", f.Name), nil)
}
})
}
doDeploy(deplRoot)
}
func doDeploy(deplRoot string) {
artDir := getArtifactsDir(deplRoot)
checkErr(shell.CheckWritableDir(artDir), nil)
bp, ctx := artifactBlueprintOrDie(artDir)
groups := bp.Groups
checkErr(validateGroupSelectionFlags(bp), ctx)
checkErr(validateRuntimeDependencies(deplRoot, groups), ctx)
checkErr(shell.ValidateDeploymentDirectory(groups, deplRoot), ctx)
for ig, group := range groups {
if !isGroupSelected(group.Name) {
logging.Info("skipping group %q", group.Name)
continue
}
groupDir := filepath.Join(deplRoot, string(group.Name))
checkErr(shell.ImportInputs(groupDir, artDir, bp), ctx)
switch group.Kind() {
case config.PackerKind:
// Packer groups are enforced to have length 1
subPath, e := modulewriter.DeploymentSource(group.Modules[0])
checkErr(e, ctx)
moduleDir := filepath.Join(groupDir, subPath)
checkErr(deployPackerGroup(moduleDir, getApplyBehavior()), ctx)
case config.TerraformKind:
checkErr(deployTerraformGroup(groupDir, artDir, getApplyBehavior(), getOutputFormat()), ctx)
default:
checkErr(
config.BpError{
Err: fmt.Errorf("group %q is an unsupported kind %q", groupDir, group.Kind()),
Path: config.Root.Groups.At(ig).Name}, ctx)
}
}
logging.Info("\n###############################")
printAdvancedInstructionsMessage(deplRoot)
}
func validateRuntimeDependencies(deplDir string, groups []config.Group) error {
for ig, group := range groups {
var err error
switch group.Kind() {
case config.PackerKind:
err = shell.ConfigurePacker()
case config.TerraformKind:
groupDir := filepath.Join(deplDir, string(group.Name))
_, err = shell.ConfigureTerraform(groupDir)
default:
err = config.BpError{
Path: config.Root.Groups.At(ig).Name,
Err: fmt.Errorf("group %s is an unsupported kind %q", group.Name, group.Kind().String())}
}
if err != nil {
return err
}
}
return nil
}
func deployPackerGroup(moduleDir string, applyBehavior shell.ApplyBehavior) error {
if err := shell.ConfigurePacker(); err != nil {
return err
}
c := shell.ProposedChanges{
Summary: fmt.Sprintf("Proposed change: use packer to build image in %s", moduleDir),
Full: fmt.Sprintf("Proposed change: use packer to build image in %s", moduleDir),
}
buildImage := applyBehavior == shell.AutomaticApply || shell.ApplyChangesChoice(c)
if buildImage {
logging.Info("initializing packer module at %s", moduleDir)
if err := shell.ExecPackerCmd(moduleDir, false, "init", "."); err != nil {
return err
}
logging.Info("validating packer module at %s", moduleDir)
if err := shell.ExecPackerCmd(moduleDir, false, "validate", "."); err != nil {
return err
}
logging.Info("building image using packer module at %s", moduleDir)
if err := shell.ExecPackerCmd(moduleDir, true, "build", "."); err != nil {
return err
}
}
return nil
}
func deployTerraformGroup(groupDir string, artifactsDir string, applyBehavior shell.ApplyBehavior, outputFormat shell.OutputFormat) error {
tf, err := shell.ConfigureTerraform(groupDir)
if err != nil {
return err
}
return shell.ExportOutputs(tf, artifactsDir, applyBehavior, outputFormat)
}