Repository navigation
Expand file tree
/
Copy pathroot.go
More file actions
470 lines (412 loc) · 16.9 KB
/
Copy pathroot.go
File metadata and controls
470 lines (412 loc) · 16.9 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
163
8000
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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
import (
"encoding/json"
goflag "flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/docker/machine/libmachine/log"
"github.com/golang/glog"
"github.com/minishift/minishift/cmd/minishift/cmd/addon"
cmdAddon "github.com/minishift/minishift/cmd/minishift/cmd/addon"
configCmd "github.com/minishift/minishift/cmd/minishift/cmd/config"
"github.com/minishift/minishift/cmd/minishift/cmd/dns"
hostfolderCmd "github.com/minishift/minishift/cmd/minishift/cmd/hostfolder"
"github.com/minishift/minishift/cmd/minishift/cmd/image"
cmdImage "github.com/minishift/minishift/cmd/minishift/cmd/image"
cmdOpenshift "github.com/minishift/minishift/cmd/minishift/cmd/openshift"
cmdProfile "github.com/minishift/minishift/cmd/minishift/cmd/profile"
cmdUtil "github.com/minishift/minishift/cmd/minishift/cmd/util"
"github.com/minishift/minishift/pkg/minikube/constants"
minishiftConfig "github.com/minishift/minishift/pkg/minishift/config"
minishiftConstants "github.com/minishift/minishift/pkg/minishift/constants"
profileActions "github.com/minishift/minishift/pkg/minishift/profile"
"github.com/minishift/minishift/pkg/util/filehelper"
"github.com/minishift/minishift/pkg/util/os/atexit"
"github.com/minishift/minishift/pkg/version"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/minishift/minishift/cmd/minishift/state"
)
const (
showLibmachineLogs = "show-libmachine-logs"
profileCmd = "profile"
profileFlag = "profile"
profileSetCmd = "set"
enableExperimentalEnv = "MINISHIFT_ENABLE_EXPERIMENTAL"
invalidProfileName = "Profile names must consist of alphanumeric characters only."
)
var viperWhiteList = []string{
"v",
"alsologtostderr",
"log_dir",
}
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "minishift",
Short: "Minishift is a tool for application development in local OpenShift clusters.",
Long: `Minishift is a command-line tool that provisions and manages single-node OpenShift clusters optimized for development workflows.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
var (
err error
isAddonInstallRequired bool
)
// If profile name is 'minishift' then ignore the vaild profile check.
if constants.ProfileName != constants.DefaultProfileName {
checkForValidProfileOrExit(cmd)
}
constants.MachineName = constants.ProfileName
constants.Minipath = constants.GetProfileHomeDir(constants.ProfileName)
// Initialize the instance directory structure
state.InstanceDirs = state.NewMinishiftDirs(constants.Minipath)
constants.KubeConfigPath = filepath.Join(state.InstanceDirs.Machines, constants.MachineName+"_kubeconfig")
if !filehelper.Exists(state.InstanceDirs.Addons) {
isAddonInstallRequired = true
}
// creating all directories for minishift run
createMinishiftDirs(state.InstanceDirs)
// Ensure the global viper config file exists.
ensureConfigFileExists(constants.GlobalConfigFile)
// Ensure the viper config file exists.
ensureConfigFileExists(constants.ConfigFile)
// Read the config file and get the details about existing image cache.
// This should be removed after 2-3 release of minishift.
cfg, err := minishiftConfig.ReadViperConfig(constants.ConfigFile)
if err != nil {
atexit.ExitWithMessage(1, err.Error())
}
cacheImages := cmdImage.GetConfiguredCachedImages(cfg)
addonConfig := cmdAddon.GetAddOnConfiguration()
// If AllInstanceConfig is not defined we should define it now.
if minishiftConfig.AllInstancesConfig == nil {
ensureAllInstanceConfigPath(constants.AllInstanceConfigPath)
minishiftConfig.AllInstancesConfig, err = minishiftConfig.NewAllInstancesConfig(constants.AllInstanceConfigPath)
if err != nil {
atexit.ExitWithMessage(1, fmt.Sprintf("Error creating all instance config: %s", err.Error()))
}
}
// If older instance state config file exists, rename the file else create a new file
_, err = os.Stat(minishiftConstants.GetInstanceStateConfig
8000
OldPath())
if err == nil {
if err := os.Rename(minishiftConstants.GetInstanceStateConfigOldPath(), minishiftConstants.GetInstanceStateConfigPath()); err != nil {
atexit.ExitWithMessage(1, fmt.Sprintf("Error moving old state config to new one: %s", err.Error()))
}
}
minishiftConfig.InstanceStateConfig, err = minishiftConfig.NewInstanceStateConfig(minishiftConstants.GetInstanceStateConfigPath())
if err != nil {
atexit.ExitWithMessage(1, fmt.Sprintf("Error creating config for VM: %s", err.Error()))
}
// Create MACHINE_NAME.json (machine config file)
minishiftConfig.InstanceConfig, err = minishiftConfig.NewInstanceConfig(minishiftConstants.GetInstanceConfigPath())
if err != nil {
atexit.ExitWithMessage(1, fmt.Sprintf("Error creating config for VM: %s", err.Error()))
}
// If hostfolder config exists for an instance then copy it to the new instance state config.
// This change should be removed after few releases.
hostfolder := minishiftConfig.InstanceStateConfig.HostFolders
if len(hostfolder) != 0 {
minishiftConfig.InstanceConfig.HostFolders = hostfolder
if err != minishiftConfig.InstanceConfig.Write() {
atexit.ExitWithMessage(1, fmt.Sprintf("Error coping existing hostfolder to new instance config: %s", err.Error()))
}
}
// If cache-config exists then copy it to the new instance config.
// This should be removed after 2-3 release of Minishift.
if len(cacheImages) != 0 {
minishiftConfig.InstanceConfig.CacheImages = cacheImages
if err != minishiftConfig.InstanceConfig.Write() {
atexit.ExitWithMessage(1, fmt.Sprintf("Error coping existing cache images to new instance config: %s", err.Error()))
}
delete(cfg, "cache-images")
if err != minishiftConfig.WriteViperConfig(constants.ConfigFile, cfg) {
atexit.ExitWithMessage(1, fmt.Sprintf("Error removing the cache-images entry from older config %s: %s", constants.ConfigFile, err.Error()))
}
}
// If addon config exists then copy it to the new instance config.
// This should be removed after 2-3 release of Minishift.
if len(addonConfig) != 0 {
minishiftConfig.InstanceConfig.AddonConfig = addonConfig
if err != minishiftConfig.InstanceConfig.Write() {
atexit.ExitWithMessage(1, fmt.Sprintf("Error coping existing addon config to new instance config: %s", err.Error()))
}
delete(cfg, "addons")
if err != minishiftConfig.WriteViperConfig(constants.ConfigFile, cfg) {
atexit.ExitWithMessage(1, fmt.Sprintf("Error removing the addon config entry from older config %s: %s", constants.ConfigFile, err.Error()))
}
}
if isAddonInstallRequired {
if err := cmdUtil.UnpackAddons(state.InstanceDirs.Addons); err != nil {
atexit.ExitWithMessage(1, fmt.Sprintf("Error installing default add-ons : %s", err))
}
}
// Check marker file created by update command and perform post update execution steps
if filehelper.Exists(filepath.Join(constants.Minipath, constants.UpdateMarkerFileName)) {
if err := performPostUpdateExecution(filepath.Join(constants.Minipath, constants.UpdateMarkerFileName)); err != nil {
atexit.ExitWithMessage(1, fmt.Sprintf("Error in performing post update exeuction: %s", err))
}
}
if minishiftConfig.EnableExperimental {
glog.Info("Experimental features are enabled")
}
shouldShowLibmachineLogs := viper.GetBool(showLibmachineLogs)
if glog.V(3) {
log.SetDebug(true)
}
if !shouldShowLibmachineLogs {
log.SetOutWriter(ioutil.Discard)
log.SetErrWriter(ioutil.Discard)
}
setDefaultActiveProfile()
// Adding minishift version information to debug logs
if glog.V(2) {
fmt.Println(fmt.Sprintf("-- minishift version: v%s+%s", version.GetMinishiftVersion(), version.GetCommitSha()))
}
},
}
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
atexit.ExitWithMessage(1, err.Error())
}
}
// Handle config values for flags used in external packages (e.g. glog)
// by setting them directly, using values from viper when not passed in as args
func setFlagsUsingViper() {
for _, config := range viperWhiteList {
var a = pflag.Lookup(config)
viper.SetDefault(a.Name, a.DefValue)
// If the flag is set, override viper value
if a.Changed {
viper.Set(a.Name, a.Value.String())
}
// Viper will give precedence first to calls to the Set command,
// then to values from the config.yml
a.Value.Set(viper.GetString(a.Name))
a.Changed = true
}
}
func processEnvVariables() {
enableExperimental, err := cmdUtil.GetBoolEnv(enableExperimentalEnv)
if err == cmdUtil.BooleanFormatError {
atexit.ExitWithMessage(1, fmt.Sprintf("Error enabling experimental features: %s", err))
}
minishiftConfig.EnableExperimental = enableExperimental
}
func init() {
processEnvVariables()
RootCmd.PersistentFlags().Bool(showLibmachineLogs, false, "Show logs from libmachine.")
RootCmd.PersistentFlags().String(profileFlag, constants.DefaultProfileName, "Profile name")
RootCmd.AddCommand(configCmd.ConfigCmd)
RootCmd.AddCommand(cmdOpenshift.OpenShiftCmd)
RootCmd.AddCommand(hostfolderCmd.HostFolderCmd)
RootCmd.AddCommand(addon.AddonsCmd)
RootCmd.AddCommand(image.ImageCmd)
RootCmd.AddCommand(cmdProfile.ProfileCmd)
if minishiftConfig.EnableExperimental {
RootCmd.AddCommand(dns.DnsCmd)
}
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
logDir := pflag.Lookup("log_dir")
if !logDir.Changed {
logDir.Value.Set(constants.MakeMiniPath("logs"))
}
viper.BindPFlags(RootCmd.PersistentFlags())
cobra.OnInitialize(initConfig)
verbosity := pflag.Lookup("v")
verbosity.Usage += ". Level varies from 1 to 5 (default 1)."
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
profile := initializeProfile()
if (profile != "") && (profile != constants.DefaultProfileName) {
constants.ProfileName = profile
constants.ConfigFile = constants.MakeMiniPath("profiles", profile, "config", "config.json")
}
// Initializing the global config file with Viper
viper.SetConfigFile(constants.GlobalConfigFile)
viper.SetConfigType("json")
err := viper.ReadInConfig()
if err != nil {
glog.Warningf("Error reading config file at '%s': %s", constants.GlobalConfigFile, err)
}
configPath := constants.ConfigFile
viper.SetConfigFile(configPath)
viper.SetConfigType("json")
err = viper.MergeInConfig()
if err != nil {
glog.Warningf("Error reading config file at '%s': %s", configPath, err)
}
setupViper()
}
// initializeProfile always return profile name based on below checks.
// 1. If profile set <PROFILE_NAME> is used then return PROFILE_NAME
// 2. If --profile <PROFILE_NAME> then return PROFILE_NAME
// 3. If no profile command or flag then return active profile name.
func initializeProfile() string {
var (
profileName string
err error
activeProfile string
profileCmdAlias = []string{
"profiles",
"instance",
}
)
// Check if profileCmd is part of os.Args so that it takes preference instead `--profile` argument
var isProfileCmdUsed bool
for _, arg := range os.Args {
if arg == profileCmd || arg == profileCmdAlias[0] || arg == profileCmdAlias[1] {
isProfileCmdUsed = true
}
}
for i, arg := range os.Args {
if !isProfileCmdUsed {
// This will match if `--profile` flag is used
if arg == "--"+profileFlag {
profileName = os.Args[i+1]
break
}
}
// This will match if we used profile or it's alias commands
if arg == profileCmd || arg == profileCmdAlias[0] || arg == profileCmdAlias[1] {
// This make sure if user specify profile command without any subcommand then
// it should not panic with out of index error
if len(os.Args) <= i+2 {
break
}
// For use cases when minishift profile set PROFILE_NAME is used
if os.Args[i+1] == profileSetCmd {
profileName = os.Args[i+2]
}
break
}
}
// Check if the allinstance config is present. If present we need to check active profile information.
_, err = os.Stat(constants.AllInstanceConfigPath)
if !os.IsNotExist(err) {
minishiftConfig.AllInstancesConfig, err = minishiftConfig.NewAllInstancesConfig(constants.AllInstanceConfigPath)
if err != nil {
atexit.ExitWithMessage(1, fmt.Sprintf("Error initializing all instance config: %s", err.Error()))
}
activeProfile = profileActions.GetActiveProfile()
}
if profileName != "" {
return profileName
}
if activeProfile != "" {
return activeProfile
}
return ""
}
func setupViper() {
viper.SetEnvPrefix(constants.MiniShiftEnvPrefix)
// Replaces '-' in flags with '_' in env variables
// e.g. show-libmachine-logs => $ENVPREFIX_SHOW_LIBMACHINE_LOGS
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
setFlagsUsingViper()
}
func ensureConfigFileExists(configPath string) {
if _, err := os.Stat(configPath); os.IsNotExist(err) {
jsonRoot := []byte("{}")
f, err := os.Create(configPath)
if err != nil {
glog.Exitf("Cannot create file '%s'
8C36
: %s", configPath, err)
}
defer f.Close()
_, err = f.Write(jsonRoot)
if err != nil {
glog.Exitf("Cannot encode config '%s': %s", configPath, err)
}
}
}
// performPostUpdateExecution executes the post update actions like unpacking the default addons
// if user chose to update addons during `minishift update` command.
// It also remove the marker file created by update command to avoid repeating the post update execution process
func performPostUpdateExecution(markerPath string) error {
var markerData UpdateMarker
file, err := ioutil.ReadFile(markerPath)
if err != nil {
return err
}
json.Unmarshal(file, &markerData)
if markerData.InstallAddon {
fmt.Println(fmt.Sprintf("Minishift was upgraded from v%s to v%s. Running post update actions.", markerData.PreviousVersion, version.GetMinishiftVersion()))
fmt.Print("--- Updating default add-ons ... ")
cmdUtil.UnpackAddons(state.InstanceDirs.Addons)
fmt.Println("OK")
fmt.Println(fmt.Sprintf("Default add-ons '%s' installed", strings.Join(cmdUtil.DefaultAssets, ", ")))
}
// Delete the marker file once post update execution is done
if err := os.Remove(markerPath); err != nil {
return err
}
return nil
}
func ensureAllInstanceConfigPath(configPath string) {
configDir := filepath.Dir(configPath)
if err := os.MkdirAll(configDir, 0777); err != nil {
atexit.ExitWithMessage(1, fmt.Sprintf("Error creating directory: %s", configDir))
}
}
func createMinishiftDirs(dirs *state.MinishiftDirs) {
dirPaths := reflect.ValueOf(*dirs)
for i := 0; i < dirPaths.NumField(); i++ {
path := dirPaths.Field(i).Interface().(string)
if err := os.MkdirAll(path, 0777); err != nil {
atexit.ExitWithMessage(1, fmt.Sprintf("Error creating directory: %s", path))
}
}
}
// If there is no active profile we need to set minishift as the default profile.
// Because this will make the profile behaviour backward compatible and consistent with user expectation.
func setDefaultActiveProfile() {
if minishiftConfig.AllInstancesConfig == nil {
atexit.ExitWithMessage(1, "Error: All instance config is not initialized")
}
activeProfile := profileActions.GetActiveProfile()
if activeProfile == "" {
err := profileActions.SetDefaultProfileActive()
if err != nil {
atexit.ExitWithMessage(1, err.Error())
}
// Only set oc context to default profile when user is looking for default profile
// i.e. "minishift start" with minishift as active profile or "minishift start --profile minishift"
// Otherwise minishift will be the active profile irrespective of what user chooses
if constants.ProfileName == constants.DefaultProfileName {
cmdUtil.SetOcContext(constants.DefaultProfileName)
}
}
}
// checkForValidProfileOrExit checks if a profile exist or not when --profile flag used.
// If profile not exist then it will error out with message.
func checkForValidProfileOrExit(cmd *cobra.Command) {
if !cmdUtil.IsValidProfileName(constants.ProfileName) {
atexit.ExitWithMessage(1, invalidProfileName)
}
if cmd.Parent() != nil {
// This condition true for each command execpt `minishift profile <subcommand>` and `minishift start ...``
if cmd.Parent().Name() != profileCmd && cmd.Name() != startCmd.Name() {
if !cmdUtil.IsValidProfile(constants.ProfileName) {
atexit.ExitWithMessage(1, fmt.Sprintf("Profile '%s' doesn't exist, Use 'minishift profile set %s' or 'minishift start --profile %s' to create", constants.ProfileName, constants.ProfileName, constants.ProfileName))
}
}
}
}