/
restart.go
141 lines (123 loc) · 3.79 KB
/
restart.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
// Copyright 2023 Khulnasoft, Inc.
//
// 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 system
import (
"fmt"
"github.com/khulnasoft/meshplay/meshplayctl/internal/cli/root/config"
"github.com/khulnasoft/meshplay/meshplayctl/pkg/utils"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
silentFlagSet bool
)
// restartCmd represents the restart command
var restartCmd = &cobra.Command{
Use: "restart",
Short: "Stop, then start Meshplay",
Long: `Restart all Meshplay containers / pods.`,
Example: `
// Restart all Meshplay containers, their instances and their connected volumes
meshplayctl system restart
// (optional) skip checking for new updates available in Meshplay.
meshplayctl system restart --skip-update
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
//Check prerequisite
hcOptions := &HealthCheckOptions{
IsPreRunE: true,
PrintLogs: false,
Subcommand: cmd.Use,
}
hc, err := NewHealthChecker(hcOptions)
if err != nil {
return ErrHealthCheckFailed(err)
}
// execute healthchecks
err = hc.RunPreflightHealthChecks()
if err != nil {
cmd.SilenceUsage = true
}
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New(utils.SystemLifeCycleError(fmt.Sprintf("restart takes only one flag. See '%s --help' for more information.\n", cmd.CommandPath()), "restart"))
}
return restart()
},
}
func restart() error {
// Get viper instance used for context
mctlCfg, err := config.GetMeshplayCtl(viper.GetViper())
if err != nil {
utils.Log.Error(err)
return nil
}
// get the platform, channel and the version of the current context
// if a temp context is set using the -c flag, use it as the current context
if tempContext != "" {
err = mctlCfg.SetCurrentContext(tempContext)
if err != nil {
return ErrSettingTemporaryContext(err)
}
}
currCtx, err := mctlCfg.GetCurrentContext()
if err != nil {
return ErrRetrievingCurrentContext(err)
}
currPlatform := currCtx.GetPlatform()
running, err := utils.AreMeshplayComponentsRunning(currPlatform)
if err != nil {
return err
}
if !running { // Meshplay is not running
if err := start(); err != nil {
return ErrRestartMeshplay(err)
}
} else {
if currPlatform == "kubernetes" {
userResponse := false
if utils.SilentFlag {
userResponse = true
} else {
// ask user for confirmation
userResponse = utils.AskForConfirmation("Meshplay deployments will be deleted from your cluster. Are you sure you want to continue")
}
if !userResponse {
log.Info("Restart aborted.")
return nil
}
// take a backup of silentFlag value to pass it to start() function later
silentFlagSet = utils.SilentFlag
// skips asking for confirmation in the stop() function
utils.SilentFlag = true
}
log.Info("Restarting Meshplay...")
if err := stop(); err != nil {
return ErrRestartMeshplay(err)
}
// reset the silent flag to avoid overriding the flag for start command
utils.SilentFlag = silentFlagSet
if err := start(); err != nil {
return ErrRestartMeshplay(err)
}
}
return nil
}
func init() {
restartCmd.Flags().BoolVarP(&skipUpdateFlag, "skip-update", "", false, "(optional) skip checking for new Meshplay's container images.")
}