Skip to content

Commit 1f0995a

Browse files
nimajalaliniravpatel27
authored andcommitted
Azure HA (#143)
* azure ha for gcm using rke * use lookup interpolation for external data source output
1 parent f566b9d commit 1f0995a

File tree

319 files changed

+42192
-98
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

319 files changed

+42192
-98
lines changed

Gopkg.lock

+51-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

create/manager_azure.go

+146-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import (
2020
)
2121

2222
const (
23-
azureRancherTerraformModulePath = "terraform/modules/azure-rancher"
23+
azureRancherTerraformModulePath = "terraform/modules/azure-rancher"
24+
azureRancherHATerraformModulePath = "terraform/modules/azure-rke"
2425
)
2526

2627
// This struct represents the definition of a Terraform .tf file.
@@ -45,19 +46,158 @@ type azureManagerTerraformConfig struct {
4546
AzureSSHUser string `json:"azure_ssh_user"`
4647
AzurePublicKeyPath string `json:"azure_public_key_path"`
4748
AzurePrivateKeyPath string `json:"azure_private_key_path"`
49+
50+
// Used for HA deployments
51+
FQDN string `json:"fqdn,omitempty"`
52+
TLSPrivateKeyPath string `json:"tls_private_key_path,omitempty"`
53+
TLSCertPath string `json:"tls_cert_path,omitempty"`
4854
}
4955

5056
func newAzureManager(currentState state.State, name string) error {
5157
nonInteractiveMode := viper.GetBool("non-interactive")
5258

53-
baseConfig, err := getBaseManagerTerraformConfig(azureRancherTerraformModulePath, name)
54-
if err != nil {
55-
return err
59+
highlyAvailable := false
60+
if viper.IsSet("ha") {
61+
highlyAvailable = viper.GetBool("ha")
62+
} else if !nonInteractiveMode {
63+
haOptions := []struct {
64+
Name string
65+
Value bool
66+
}{
67+
{"No", false},
68+
{"Yes", true},
69+
}
70+
71+
prompt := promptui.Select{
72+
Label: "Would you like to make this deployment highly available",
73+
Items: haOptions,
74+
Templates: &promptui.SelectTemplates{
75+
Label: "{{ . }}?",
76+
Active: fmt.Sprintf("%s {{ .Name | underline }}", promptui.IconSelect),
77+
Inactive: " {{.Name}}",
78+
Selected: " Highly Available? {{.Name}}",
79+
},
80+
}
81+
82+
i, _, err := prompt.Run()
83+
if err != nil {
84+
return err
85+
}
86+
87+
highlyAvailable = haOptions[i].Value
5688
}
5789

58-
cfg := azureManagerTerraformConfig{
59-
baseManagerTerraformConfig: baseConfig,
90+
cfg := azureManagerTerraformConfig{}
91+
92+
// HA specific configuration
93+
if highlyAvailable {
94+
if viper.IsSet("fqdn") {
95+
cfg.FQDN = viper.GetString("fqdn")
96+
} else if nonInteractiveMode {
97+
return errors.New("fqdn must be specified")
98+
} else {
99+
prompt := promptui.Prompt{
100+
Label: "Fully Qualified Domain Name",
101+
Validate: func(input string) error {
102+
if len(input) == 0 {
103+
return errors.New("Invalid Fully Qualified Domain Name")
104+
}
105+
return nil
106+
},
107+
Default: "rancher.example.com",
108+
}
109+
110+
result, err := prompt.Run()
111+
if err != nil {
112+
return err
113+
}
114+
115+
cfg.FQDN = result
116+
}
117+
118+
if viper.IsSet("tls_private_key_path") {
119+
cfg.TLSPrivateKeyPath = viper.GetString("tls_private_key_path")
120+
} else if nonInteractiveMode {
121+
return errors.New("tls_private_key_path must be specified")
122+
} else {
123+
prompt := promptui.Prompt{
124+
Label: "TLS Private Key Path",
125+
Validate: func(input string) error {
126+
expandedPath, err := homedir.Expand(input)
127+
if err != nil {
128+
return err
129+
}
130+
131+
_, err = os.Stat(expandedPath)
132+
if err != nil {
133+
if os.IsNotExist(err) {
134+
return errors.New("File not found")
135+
}
136+
}
137+
return nil
138+
},
139+
}
140+
141+
result, err := prompt.Run()
142+
if err != nil {
143+
return err
144+
}
145+
146+
expandedTLSPrivateKeyPath, err := homedir.Expand(result)
147+
if err != nil {
148+
return err
149+
}
150+
151+
cfg.TLSPrivateKeyPath = expandedTLSPrivateKeyPath
152+
}
153+
154+
if viper.IsSet("tls_cert_path") {
155+
cfg.TLSPrivateKeyPath = viper.GetString("tls_cert_path")
156+
} else if nonInteractiveMode {
157+
return errors.New("tls_cert_path must be specified")
158+
} else {
159+
prompt := promptui.Prompt{
160+
Label: "TLS Certificate Path",
161+
Validate: func(input string) error {
162+
expandedPath, err := homedir.Expand(input)
163+
if err != nil {
164+
return err
165+
}
166+
167+
_, err = os.Stat(expandedPath)
168+
if err != nil {
169+
if os.IsNotExist(err) {
170+
return errors.New("File not found")
171+
}
172+
}
173+
return nil
174+
},
175+
}
176+
177+
result, err := prompt.Run()
178+
if err != nil {
179+
return err
180+
}
181+
182+
expandedTLSCertPath, err := homedir.Expand(result)
183+
if err != nil {
184+
return err
185+
}
186+
187+
cfg.TLSCertPath = expandedTLSCertPath
188+
}
189+
}
190+
191+
terraformModulePath := azureRancherTerraformModulePath
192+
if highlyAvailable {
193+
terraformModulePath = azureRancherHATerraformModulePath
194+
}
195+
196+
baseConfig, err := getBaseManagerTerraformConfig(terraformModulePath, name)
197+
if err != nil {
198+
return err
60199
}
200+
cfg.baseManagerTerraformConfig = baseConfig
61201

62202
// Azure Subscription ID
63203
if viper.IsSet("azure_subscription_id") {

get/cluster.go

+1-29
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package get
33
import (
44
"errors"
55
"fmt"
6-
"io/ioutil"
7-
"os"
86
"sort"
97

108
"github.com/joyent/triton-kubernetes/backend"
@@ -106,33 +104,7 @@ func GetCluster(remoteBackend backend.Backend) error {
106104
selectedClusterKey = clusters[value]
107105
}
108106

109-
// Create a temporary directory
110-
tempDir, err := ioutil.TempDir("", "triton-kubernetes-")
111-
if err != nil {
112-
return err
113-
}
114-
defer os.RemoveAll(tempDir)
115-
116-
// Save the terraform config to the temporary directory
117-
jsonPath := fmt.Sprintf("%s/%s", tempDir, "main.tf.json")
118-
err = ioutil.WriteFile(jsonPath, state.Bytes(), 0644)
119-
if err != nil {
120-
return err
121-
}
122-
123-
// Use temporary directory as working directory
124-
shellOptions := shell.ShellOptions{
125-
WorkingDir: tempDir,
126-
}
127-
128-
// Run terraform init
129-
err = shell.RunShellCommand(&shellOptions, "terraform", "init", "-force-copy")
130-
if err != nil {
131-
return err
132-
}
133-
134-
// Run terraform output
135-
err = shell.RunShellCommand(&shellOptions, "terraform", "output", "-module", selectedClusterKey)
107+
err = shell.RunTerraformOutputWithState(state, selectedClusterKey)
136108
if err != nil {
137109
return err
138110
}

get/manager.go

+1-29
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package get
33
import (
44
"errors"
55
"fmt"
6-
"io/ioutil"
7-
"os"
86

97
"github.com/joyent/triton-kubernetes/backend"
108
"github.com/joyent/triton-kubernetes/shell"
@@ -60,33 +58,7 @@ func GetManager(remoteBackend backend.Backend) error {
6058
return err
6159
}
6260

63-
// Create a temporary directory
64-
tempDir, err := ioutil.TempDir("", "triton-kubernetes-")
65-
if err != nil {
66-
return err
67-
}
68-
defer os.RemoveAll(tempDir)
69-
70-
// Save the terraform config to the temporary directory
71-
jsonPath := fmt.Sprintf("%s/%s", tempDir, "main.tf.json")
72-
err = ioutil.WriteFile(jsonPath, state.Bytes(), 0644)
73-
if err != nil {
74-
return err
75-
}
76-
77-
// Use temporary directory as working directory
78-
shellOptions := shell.ShellOptions{
79-
WorkingDir: tempDir,
80-
}
81-
82-
// Run terraform init
83-
err = shell.RunShellCommand(&shellOptions, "terraform", "init", "-force-copy")
84-
if err != nil {
85-
return err
86-
}
87-
88-
// Run terraform output
89-
err = shell.RunShellCommand(&shellOptions, "terraform", "output", "-module", "cluster-manager")
61+
err = shell.RunTerraformOutputWithState(state, "cluster-manager")
9062
if err != nil {
9163
return err
9264
}

shell/run_shell_cmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"os/exec"
66
)
77

8-
func RunShellCommand(options *ShellOptions, command string, args ...string) error {
8+
func runShellCommand(options *ShellOptions, command string, args ...string) error {
99
cmd := exec.Command(command, args...)
1010
cmd.Stdin = os.Stdin
1111
cmd.Stdout = os.Stdout

0 commit comments

Comments
 (0)