/
manager_bare_metal.go
150 lines (133 loc) · 3.2 KB
/
manager_bare_metal.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
142
143
144
145
146
147
148
149
150
package create
import (
"errors"
"os"
"github.com/joyent/triton-kubernetes/state"
"github.com/manifoldco/promptui"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
const (
bareMetalRancherTerraformModulePath = "terraform/modules/bare-metal-rancher"
)
// This struct represents the definition of a Terraform .tf file.
// Marshalled into json this struct can be passed directly to Terraform.
type bareMetalManagerTerraformConfig struct {
baseManagerTerraformConfig
Host string `json:"host"`
BastionHost string `json:"bastion_host,omitempty"`
SSHUser string `json:"ssh_user,omitempty"`
KeyPath string `json:"key_path,omitempty"`
}
func newBareMetalManager(currentState state.State, name string) error {
nonInteractiveMode := viper.GetBool("non-interactive")
baseConfig, err := getBaseManagerTerraformConfig(bareMetalRancherTerraformModulePath, name)
if err != nil {
return err
}
cfg := bareMetalManagerTerraformConfig{
baseManagerTerraformConfig: baseConfig,
}
host := ""
if viper.IsSet("host") {
host = viper.GetString("host")
} else if nonInteractiveMode {
return errors.New("host must be specified")
} else {
prompt := promptui.Prompt{
Label: "Host/IP for cluster manager",
Validate: func(input string) error {
if input == "" {
return errors.New("Invalid host/ip")
}
return nil
},
}
result, err := prompt.Run()
if err != nil {
return err
}
host = result
}
cfg.Host = host
ssh_user := ""
if viper.IsSet("ssh_user") {
ssh_user = viper.GetString("ssh_user")
} else if nonInteractiveMode {
return errors.New("ssh_user must be specified")
} else {
prompt := promptui.Prompt{
Label: "SSH User",
Default: "ubuntu",
Validate: func(input string) error {
if input == "" {
return errors.New("Invalid SSH User")
}
return nil
},
}
result, err := prompt.Run()
if err != nil {
return err
}
ssh_user = result
}
cfg.SSHUser = ssh_user
bastion_host := ""
if viper.IsSet("bastion_host") {
bastion_host = viper.GetString("bastion_host")
} else if nonInteractiveMode {
return errors.New("bastion_host must be specified")
} else {
prompt := promptui.Prompt{
Label: "Bastion Host",
Default: "None",
Validate: func(input string) error {
if input == "" {
return errors.New("Invalid Bastion Host")
}
return nil
},
}
result, err := prompt.Run()
if err != nil {
return err
}
if result != "None" {
bastion_host = result
}
}
cfg.BastionHost = bastion_host
key_path := ""
if viper.IsSet("key_path") {
key_path = viper.GetString("key_path")
} else if nonInteractiveMode {
return errors.New("key_path must be specified")
} else {
prompt := promptui.Prompt{
Label: "Key Path",
Default: "~/.ssh/id_rsa",
Validate: func(input string) error {
expandedPath, err := homedir.Expand(input)
if err != nil {
return err
}
_, err = os.Stat(expandedPath)
if err != nil {
if os.IsNotExist(err) {
return errors.New("File not found")
}
}
return nil
},
}
result, err := prompt.Run()
if err != nil {
return err
}
key_path = result
}
cfg.KeyPath = key_path
currentState.SetManager(&cfg)
return nil
}