-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
192 lines (154 loc) · 5.04 KB
/
main.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
// NewConfig returns a new decoded Config struct
func NewConfig(configPath string) (*Config, error) {
// Create config structure
config := &Config{}
// Open config file
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer file.Close()
// Init new YAML decode
d := yaml.NewDecoder(file)
// Start YAML decoding from file
if err := d.Decode(&config); err != nil {
return nil, err
}
readConfig = config
return config, nil
}
// replaceMastersSchedulable is a dirty hack
func replaceMastersSchedulable(filePath string) {
input, err := ioutil.ReadFile(filePath)
check(err)
lines := strings.Split(string(input), "\n")
for i, line := range lines {
if strings.Contains(line, "mastersSchedulable: true") {
lines[i] = " mastersSchedulable: false"
}
}
output := strings.Join(lines, "\n")
err = ioutil.WriteFile(filePath, []byte(output), 0644)
check(err)
}
// PreflightSetup is to write the install-config.yaml file for openshift-install
func PreflightSetup(config Config) {
// Remove generation directory if it exists
log.Printf("Cleaning up asset directory %s\n", config.PilotLight.AssetDirectory)
err := os.RemoveAll(config.PilotLight.AssetDirectory)
check(err)
absoluteBinPath, err := filepath.Abs(config.PilotLight.AssetDirectory + "/bin/")
check(err)
absoluteConfPath, err := filepath.Abs(config.PilotLight.AssetDirectory + "/conf/")
check(err)
absoluteWWWRootPath, err := filepath.Abs(config.PilotLight.AssetDirectory + "/www-root/")
check(err)
// Create generation directory
CreateDirectory(config.PilotLight.AssetDirectory)
// Create bin directory
CreateDirectory(absoluteBinPath)
// Create conf directory
CreateDirectory(absoluteConfPath)
// Create www-root directory
CreateDirectory(absoluteWWWRootPath)
// Copy over the install-config.yml file
CopyFile(config.PilotLight.InstallConfigPath, absoluteConfPath+"/install-config.yaml", BUFFERSIZE)
os := "linux"
if runtime.GOOS == "darwin" {
os = "mac"
}
// Download the openshift-install package
err = DownloadFile(absoluteBinPath+"/openshift-install-"+os+".tar.gz", "https://mirror.openshift.com/pub/openshift-v4/clients/ocp/latest/openshift-install-"+os+".tar.gz")
check(err)
// Unzip the openshift-install package
Untar(absoluteBinPath, absoluteBinPath+"/openshift-install-"+os+".tar.gz")
// Remove unneeded files
DeleteFile(absoluteBinPath + "/openshift-install-" + os + ".tar.gz")
DeleteFile(absoluteBinPath + "/README.md")
// Create bootstrap files
log.Println("Creating bootstrap files...")
d1 := []byte("#!/bin/sh\n" + absoluteBinPath + "/openshift-install create manifests --dir=" + absoluteConfPath)
err = ioutil.WriteFile(absoluteBinPath+"/create_manifests.sh", d1, 0755)
check(err)
d2 := []byte("#!/bin/sh\n" + absoluteBinPath + "/openshift-install create ignition-configs --dir=" + absoluteConfPath)
err = ioutil.WriteFile(absoluteBinPath+"/create_ignition_configs.sh", d2, 0755)
check(err)
// Run Manifest creation command
log.Println("Creating manifests...")
cmd := exec.Command(absoluteBinPath + "/create_manifests.sh")
err = cmd.Start()
check(err)
// wait for command to finish
cmd.Wait()
log.Println("Manifest files created!")
// Edit manifest file to disable scheduling workloads on Control Plane nodes
if config.PilotLight.MastersSchedulable == false {
log.Println("Setting Control Plane to not run workloads...")
//ModifySchedulerYAMLFile("cluster-scheduler-02-config", "yml", absoluteConfPath+"/manifests/")
replaceMastersSchedulable(absoluteConfPath + "/manifests/cluster-scheduler-02-config.yml")
}
// Create ignition files
log.Println("Creating ignition configs...")
cmd = exec.Command(absoluteBinPath + "/create_ignition_configs.sh")
err = cmd.Start()
check(err)
log.Println("Preflight complete!")
}
// ModifySchedulerYAMLFile opens a YAML file
func ModifySchedulerYAMLFile(fileName string, fileExt string, filePath string) {
if fileName == "" {
fileName = "config"
}
if fileExt == "" {
fileExt = "yml"
}
if filePath == "" {
filePath = "."
}
viper.SetConfigName(fileName)
viper.SetConfigType(fileExt)
viper.AddConfigPath(filePath)
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.Fatal(err)
}
SchC := SchedulerConfig{}
err = viper.Unmarshal(&SchC)
if err != nil {
log.Fatalf("unable to decode into struct, %v", err)
}
// Change value in map and marshal back into yaml
viper.Set("spec.mastersSchedulable", false)
_, err = yaml.Marshal(&SchC)
if err != nil {
log.Fatalf("error: %v", err)
}
viper.WriteConfig()
}
// Func main should be as small as possible and do as little as possible by convention
func main() {
// Generate our config based on the config supplied
// by the user in the flags
cfgPath, err := ParseFlags()
if err != nil {
log.Fatal(err)
}
cfg, err := NewConfig(cfgPath)
if err != nil {
log.Fatal(err)
}
// Run the server
cfg.RunHTTPServer()
}