This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
/
deploy.go
211 lines (190 loc) · 6.72 KB
/
deploy.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
Copyright 2018 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 opsservice
import (
"fmt"
"os"
"strconv"
"github.com/gravitational/gravity/lib/constants"
"github.com/gravitational/gravity/lib/defaults"
"github.com/gravitational/gravity/lib/schema"
"github.com/gravitational/gravity/lib/utils"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
)
// planetEnterCommand returns a new command that executes the command specified
// with args inside planet container
func (s *site) planetEnterCommand(args ...string) []string {
exe := utils.Executable{Path: defaults.GravityBin}
return exe.PlanetCommandSlice(args, s.contextArgs()...)
}
// gravityCommand generates a command line for a gravity sub-command specified with args.
// It adds additional flags depending on the context.
func (s *site) gravityCommand(args ...string) []string {
command := []string{constants.GravityBin}
command = append(command, s.contextArgs()...)
command = append(command, args...)
return command
}
// planetGravityCommand returns gravity command that is executed inside planet,
// the difference with "gravityCommand" is that it uses gravity binary at the default
// location in /usr/bin
func (s *site) planetGravityCommand(args ...string) []string {
command := []string{defaults.GravityBin}
command = append(command, s.contextArgs()...)
command = append(command, args...)
return utils.PlanetCommandSlice(command, s.contextArgs()...)
}
// etcdctlCommand returns etcdctl command that will run inside planet
func (s *site) etcdctlCommand(args ...string) []string {
cmd := s.planetEnterCommand(
"/usr/bin/etcdctl",
"--endpoint=https://127.0.0.1:2379",
"--cert-file=/var/state/etcd.cert",
"--key-file=/var/state/etcd.key",
"--ca-file=/var/state/root.cert")
return append(cmd, args...)
}
// contextArgs returns a list of additional command line arguments for a gravity binary
// depending on the context
func (s *site) contextArgs() (args []string) {
if s.shouldUseInsecure() {
args = append(args, "--insecure")
}
if s.service.cfg.Devmode {
args = append(args, "--debug")
}
return args
}
func remoteDirectories(server *ProvisionedServer, manifest schema.Manifest, uid, gid string) (commands []Command, err error) {
// list of directories to create
directories := []string{
server.InGravity("local", "packages", "blobs"),
server.InGravity("local", "packages", "unpacked"),
server.InGravity("local", "packages", "tmp"),
server.InGravity("teleport", "auth"),
server.InGravity("teleport", "node"),
server.InGravity("planet", "state"),
server.InGravity("planet", "etcd"),
server.InGravity("planet", "registry"),
server.InGravity("planet", "docker"),
server.InGravity("planet", "kubelet"),
server.InGravity("planet", "share", "hooks"),
server.InGravity("planet", "log", "journal"),
server.InGravity("site", "teleport"),
server.InGravity("site", "packages", "blobs"),
server.InGravity("site", "packages", "unpacked"),
server.InGravity("site", "packages", "tmp"),
server.InGravity("secrets"),
server.InGravity("backup"),
server.InGravity("logrange"),
// names prometheus-db/alertmanager-db are hardcoded subPath values
// in prometheus-operator
server.InGravity("monitoring", "prometheus-db"),
server.InGravity("monitoring", "alertmanager-db"),
}
chownList := []string{
server.InGravity("local"),
server.InGravity("teleport"),
server.InGravity("planet", "etcd"),
server.InGravity("planet", "log"),
server.InGravity("planet", "registry"),
server.InGravity("planet", "share"),
server.InGravity("planet", "state"),
server.InGravity("site"),
server.InGravity("secrets"),
server.InGravity("backup"),
server.InGravity("monitoring"),
server.InGravity("logrange"),
}
chmodList := []string{
server.StateDir(),
server.InGravity("local"),
}
mounts, err := GetMounts(manifest, server.Server)
if err != nil {
return nil, trace.Wrap(err)
}
for _, mount := range mounts {
// TODO: this code still runs on expand.
// We will be upgrading this to use the same install fsm
// so this will be removed.
// For now, simply skip the volumes marked with SkipIfMissing
// since these are meant for already existing directories outside
// of telekube system root.
if mount.SkipIfMissing {
log.Debugf("Skipping volume %v.", mount.Source)
continue
}
if mount.CreateIfMissing {
commands = append(commands,
Cmd([]string{"mkdir", "-p", mount.Source}, "creating directory %v", mount.Source),
)
}
if mount.UID != nil || mount.GID != nil {
expr := chownExpr(mount.UID, mount.GID)
commands = append(commands, Cmd(
[]string{"chown", expr, mount.Source},
"setting ownership of %v to %v", mount.Source, expr))
} else {
// set standard ownership
commands = append(commands, Cmd(
[]string{"chown", fmt.Sprintf("%v:%v", uid, gid), mount.Source},
"setting ownership of %v to %v:%v", mount.Source, uid, gid))
}
if mount.Mode != "" {
commands = append(commands,
Cmd([]string{"chmod", mount.Mode, mount.Source},
"setting file mode of %v to %v", mount.Source, formatFileMode(mount.Mode)),
)
}
}
for _, dir := range directories {
commands = append(commands,
Cmd([]string{"mkdir", "-p", dir}, "creating directory %v", dir),
)
}
commands = append(commands,
Cmd(
// Change ownership of the top-level gravity directory non-recursively
[]string{"chown", fmt.Sprintf("%v:%v", uid, gid), server.StateDir()},
"setting ownership of %v to %v:%v", server.StateDir(), uid, gid,
),
)
for _, dir := range chownList {
commands = append(commands,
Cmd(
[]string{"chown", "-R", fmt.Sprintf("%v:%v", uid, gid), dir},
"setting ownership of %v to %v:%v", dir, uid, gid,
),
)
}
for _, dir := range chmodList {
commands = append(commands,
Cmd(
[]string{"chmod", fmt.Sprintf("%o", defaults.SharedDirMask), dir},
"setting file mode of %v to %v", dir, os.FileMode(defaults.SharedDirMask)),
)
}
return commands, nil
}
// formatFileMode formats the specified mode as os.FileMode value.
// It does not fail in case it cannot parse the mode as octal numeral
// and returns the specified input mode unaltered.
func formatFileMode(mode string) string {
fileMode, err := strconv.ParseUint(mode, 8, 32)
if err == nil {
mode = os.FileMode(fileMode).String()
}
return mode
}