This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
/
stop_start.go
166 lines (145 loc) · 4.78 KB
/
stop_start.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
/*
Copyright 2020 Gravitational, 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 cli
import (
"github.com/gravitational/gravity/lib/loc"
"github.com/gravitational/gravity/lib/localenv"
"github.com/gravitational/gravity/lib/pack"
"github.com/gravitational/gravity/lib/systeminfo"
"github.com/gravitational/gravity/lib/systemservice"
"github.com/fatih/color"
"github.com/gravitational/trace"
)
const systemStopMessage = `This action will stop all Gravity and Kubernetes services on the node.
Would you like to proceed? You can launch the command with --confirm flag to suppress this prompt in future.`
func stopGravity(env *localenv.LocalEnvironment, confirmed bool) error {
if !confirmed {
env.Println(color.YellowString(systemStopMessage))
confirmed, err := confirm()
if err != nil {
return trace.Wrap(err)
}
if !confirmed {
env.Println("Action cancelled by user.")
return nil
}
}
packages, err := findInstalledPackages(env.Packages)
if err != nil {
return trace.Wrap(err)
}
svm, err := systemservice.New()
if err != nil {
return trace.Wrap(err)
}
for _, pack := range packages {
env.PrintStep("Stopping system service %s", pack)
if err := svm.StopPackageService(pack); err != nil {
return trace.Wrap(err)
}
env.PrintStep("Disabling system service %s", pack)
if err := svm.DisablePackageService(pack); err != nil {
return trace.Wrap(err)
}
}
env.PrintStep("Gravity services have been stopped and disabled")
return nil
}
const systemStartMessage = `This action will start all Gravity and Kubernetes services on the node.
Would you like to proceed? You can launch the command with --confirm flag to suppress this prompt in future.`
func startGravity(env *localenv.LocalEnvironment, confirmed bool) error {
if !confirmed {
env.Println(color.YellowString(systemStartMessage))
confirmed, err := confirm()
if err != nil {
return trace.Wrap(err)
}
if !confirmed {
env.Println("Action cancelled by user.")
return nil
}
}
err := checkAdvertiseAddress(env.Packages)
if err != nil {
return trace.Wrap(err)
}
packages, err := findInstalledPackages(env.Packages)
if err != nil {
return trace.Wrap(err)
}
svm, err := systemservice.New()
if err != nil {
return trace.Wrap(err)
}
for _, pack := range packages {
env.PrintStep("Enabling system service %s", pack)
if err := svm.EnablePackageService(pack); err != nil {
return trace.Wrap(err)
}
env.PrintStep("Starting system service %s", pack)
if err := svm.StartPackageService(pack, false); err != nil {
return trace.Wrap(err)
}
}
env.PrintStep("Gravity services have been enabled and started")
return nil
}
func findInstalledPackages(packages pack.PackageService) ([]loc.Locator, error) {
planet, err := pack.FindInstalledPackage(packages, loc.Planet)
if err != nil {
return nil, trace.Wrap(err)
}
teleport, err := pack.FindInstalledPackage(packages, loc.Teleport)
if err != nil {
return nil, trace.Wrap(err)
}
return []loc.Locator{*teleport, *planet}, nil
}
// checkAdvertiseAddress makes sure that the node has a network interface for
// its configured advertise address.
//
// This helps prevent scenarios when, for example, a node is migrated to
// another machine and user does not provide a new advertise address to the
// start command.
func checkAdvertiseAddress(packages pack.PackageService) error {
// Use the runtime package label to determine the current advertise address.
locator, err := pack.FindInstalledConfigPackage(packages, loc.Planet)
if err != nil {
return trace.Wrap(err)
}
envelope, err := packages.ReadPackageEnvelope(*locator)
if err != nil {
return trace.Wrap(err)
}
advertiseIP := envelope.RuntimeLabels[pack.AdvertiseIPLabel]
if advertiseIP == "" {
log.Warnf("No %v label on %v.", pack.AdvertiseIPLabel, envelope)
return nil
}
ifaces, err := systeminfo.NetworkInterfaces()
if err != nil {
return trace.Wrap(err)
}
var ips []string
for _, iface := range ifaces {
ips = append(ips, iface.IPv4)
}
for _, ip := range ips {
if ip == advertiseIP {
return nil
}
}
return trace.NotFound(`The cluster node is configured with advertise address %v but it's not present on the machine.
Available addresses are: %v.
If you wish to reconfigure the node to use a different advertise address, use "gravity start --advertise-addr=<new-ip>" command.`, advertiseIP, ips)
}