forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
141 lines (117 loc) · 4.53 KB
/
generate.go
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
package cmd
import (
"os"
"path"
log "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/Azure/acs-engine/pkg/acsengine"
"github.com/Azure/acs-engine/pkg/api"
"io/ioutil"
)
const (
generateName = "generate"
generateShortDescription = "Generate an Azure Resource Manager template"
generateLongDescription = "Generates an Azure Resource Manager template, parameters file and other assets for a cluster"
)
type generateCmd struct {
apimodelPath string
outputDirectory string // can be auto-determined from clusterDefinition
caCertificatePath string
caPrivateKeyPath string
classicMode bool
noPrettyPrint bool
parametersOnly bool
// derived
containerService *api.ContainerService
apiVersion string
}
func newGenerateCmd() *cobra.Command {
gc := generateCmd{}
generateCmd := &cobra.Command{
Use: generateName,
Short: generateShortDescription,
Long: generateLongDescription,
RunE: func(cmd *cobra.Command, args []string) error {
gc.validate(cmd, args)
return gc.run()
},
}
f := generateCmd.Flags()
f.StringVar(&gc.apimodelPath, "api-model", "", "")
f.StringVar(&gc.outputDirectory, "output-directory", "", "output directory (derived from FQDN if absent)")
f.StringVar(&gc.caCertificatePath, "ca-certificate-path", "", "path to the CA certificate to use for Kubernetes PKI assets")
f.StringVar(&gc.caPrivateKeyPath, "ca-private-key-path", "", "path to the CA private key to use for Kubernetes PKI assets")
f.BoolVar(&gc.classicMode, "classic-mode", false, "enable classic parameters and outputs")
f.BoolVar(&gc.noPrettyPrint, "no-pretty-print", false, "skip pretty printing the output")
f.BoolVar(&gc.parametersOnly, "parameters-only", false, "only output parameters files")
return generateCmd
}
func (gc *generateCmd) validate(cmd *cobra.Command, args []string) {
var caCertificateBytes []byte
var caKeyBytes []byte
var err error
if gc.apimodelPath == "" {
if len(args) > 0 {
gc.apimodelPath = args[0]
} else if len(args) > 1 {
cmd.Usage()
log.Fatalln("too many arguments were provided to 'generate'")
} else {
cmd.Usage()
log.Fatalln("--api-model was not supplied, nor was one specified as a positional argument")
}
}
if _, err := os.Stat(gc.apimodelPath); os.IsNotExist(err) {
log.Fatalf("specified api model does not exist (%s)", gc.apimodelPath)
}
gc.containerService, gc.apiVersion, err = api.LoadContainerServiceFromFile(gc.apimodelPath)
if err != nil {
log.Fatalf("error parsing the api model: %s", err.Error())
}
if gc.outputDirectory == "" {
gc.outputDirectory = path.Join("_output", gc.containerService.Properties.MasterProfile.DNSPrefix)
}
// consume gc.caCertificatePath and gc.caPrivateKeyPath
if (gc.caCertificatePath != "" && gc.caPrivateKeyPath == "") || (gc.caCertificatePath == "" && gc.caPrivateKeyPath != "") {
log.Fatal("--ca-certificate-path and --ca-private-key-path must be specified together")
}
if gc.caCertificatePath != "" {
if caCertificateBytes, err = ioutil.ReadFile(gc.caCertificatePath); err != nil {
log.Fatal("failed to read CA certificate file:", err)
}
if caKeyBytes, err = ioutil.ReadFile(gc.caPrivateKeyPath); err != nil {
log.Fatal("failed to read CA private key file:", err)
}
prop := gc.containerService.Properties
if prop.CertificateProfile == nil {
prop.CertificateProfile = &api.CertificateProfile{}
}
prop.CertificateProfile.CaCertificate = string(caCertificateBytes)
prop.CertificateProfile.SetCAPrivateKey(string(caKeyBytes))
}
}
func (gc *generateCmd) run() error {
log.Infoln("Generating assets...")
templateGenerator, err := acsengine.InitializeTemplateGenerator(gc.classicMode)
if err != nil {
log.Fatalln("failed to initialize template generator: %s", err.Error())
}
certsGenerated := false
template, parameters, certsGenerated, err := templateGenerator.GenerateTemplate(gc.containerService)
if err != nil {
log.Fatalf("error generating template %s: %s", gc.apimodelPath, err.Error())
os.Exit(1)
}
if !gc.noPrettyPrint {
if template, err = acsengine.PrettyPrintArmTemplate(template); err != nil {
log.Fatalf("error pretty printing template: %s \n", err.Error())
}
if parameters, err = acsengine.PrettyPrintJSON(parameters); err != nil {
log.Fatalf("error pretty printing template parameters: %s \n", err.Error())
}
}
if err = acsengine.WriteArtifacts(gc.containerService, gc.apiVersion, template, parameters, gc.outputDirectory, certsGenerated, gc.parametersOnly); err != nil {
log.Fatalf("error writing artifacts: %s \n", err.Error())
}
return nil
}