root.go 6.68 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*Package mwdd is used to interact a mwdd v2 setup

Copyright © 2020 Addshore

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package mwdd

import (
21
	"bytes"
22
	"os"
Addshore's avatar
Addshore committed
23
24
25
26
	"os/user"

	"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/exec"
	"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/mwdd/files"
27
	"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/util/dotenv"
28
29
30
31
32
33
)

/*MWDD representation of a mwdd v2 setup*/
type MWDD string

/*DefaultForUser returns the default mwdd working directory for the user*/
Addshore's avatar
Addshore committed
34
func DefaultForUser() MWDD {
35
36
37
38
	return MWDD(mwddUserDirectory() + string(os.PathSeparator) + "default")
}

func mwddUserDirectory() string {
Addshore's avatar
Addshore committed
39
40
41
42
43
44
45
46
47
	// user home dir can not be used in Gitlab CI, must use the project dir instead!
	// https://medium.com/@patrick.winters/mounting-volumes-in-sibling-containers-with-gitlab-ci-534e5edc4035
	// TODO maybe this should be pushed further up and the whole mwcli dir should be moved?!
	_, inGitlabCi := os.LookupEnv("GITLAB_CI")
	if inGitlabCi {
		ciDir, _ := os.LookupEnv("CI_PROJECT_DIR")
		return ciDir + ".mwcli/mwdd"
	}

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	currentUser, err := user.Current()
	if err != nil {
		panic(err)
	}

	// If we are root, check to see if we can detect sudo being used
	if currentUser.Uid == "0" {
		sudoUID := os.Getenv("SUDO_UID")
		if sudoUID == "" {
			panic("detected sudo but no SUDO_UID")
		}
		currentUser, err = user.LookupId(sudoUID)
		if err != nil {
			panic(err)
		}
	}

65
66
	projectDirectory := currentUser.HomeDir + string(os.PathSeparator) + ".mwcli/mwdd"
	return projectDirectory
67

68
69
70
71
72
}

/*Directory the directory containing the development environment*/
func (m MWDD) Directory() string {
	return string(m)
Addshore's avatar
Addshore committed
73
74
}

75
76
77
78
79
/*DockerComposeProjectName the name of the docker-compose project*/
func (m MWDD) DockerComposeProjectName() string {
	return "mwcli-mwdd-default"
}

Addshore's avatar
Addshore committed
80
/*Env ...*/
81
82
func (m MWDD) Env() dotenv.File {
	return dotenv.FileForDirectory(m.Directory())
Addshore's avatar
Addshore committed
83
84
85
86
87
}

/*EnsureReady ...*/
func (m MWDD) EnsureReady() {
	files.EnsureReady(m.Directory())
88
	m.Env().EnsureExists()
Addshore's avatar
Addshore committed
89
90
}

91
92
// DockerComposeCommand results in something like: `docker-compose <automatic project stuff> <command> <commandArguments>`
type DockerComposeCommand struct {
Addshore's avatar
Addshore committed
93
94
95
	Command          string
	CommandArguments []string
	HandlerOptions   exec.HandlerOptions
96
97
98
}

/*DockerCompose runs any docker-compose command for the mwdd project with the correct project settings and all files loaded*/
Addshore's avatar
Addshore committed
99
func (m MWDD) DockerCompose(command DockerComposeCommand) error {
Addshore's avatar
Addshore committed
100
101
	context := exec.ComposeCommandContext{
		ProjectDirectory: m.Directory(),
Addshore's avatar
Addshore committed
102
103
		ProjectName:      m.DockerComposeProjectName(),
		Files:            files.ListRawDcYamlFilesInContextOfProjectDirectory(m.Directory()),
Addshore's avatar
Addshore committed
104
105
	}

106
	return exec.RunCommand(
107
		command.HandlerOptions,
Addshore's avatar
Addshore committed
108
109
		exec.ComposeCommand(
			context,
110
			command.Command,
Addshore's avatar
Addshore committed
111
			command.CommandArguments...,
112
113
		),
	)
Addshore's avatar
Addshore committed
114
115
}

116
/*DockerComposeTTY runs any docker-compose command for the mwdd project with the correct project settings and all files loaded in a TTY*/
Addshore's avatar
Addshore committed
117
func (m MWDD) DockerComposeTTY(command DockerComposeCommand) {
118
119
	context := exec.ComposeCommandContext{
		ProjectDirectory: m.Directory(),
Addshore's avatar
Addshore committed
120
121
		ProjectName:      m.DockerComposeProjectName(),
		Files:            files.ListRawDcYamlFilesInContextOfProjectDirectory(m.Directory()),
122
123
124
125
126
127
128
	}

	exec.RunTTYCommand(
		command.HandlerOptions,
		exec.ComposeCommand(
			context,
			command.Command,
Addshore's avatar
Addshore committed
129
			command.CommandArguments...,
130
131
132
133
		),
	)
}

134
/*Exec runs `docker-compose exec -T <service> <commandAndArgs>`*/
Addshore's avatar
Addshore committed
135
func (m MWDD) Exec(service string, commandAndArgs []string, options exec.HandlerOptions, user string) {
136
	// TODO refactor this code path to make handeling options nicer
137
138
	m.DockerComposeTTY(
		DockerComposeCommand{
Addshore's avatar
Addshore committed
139
140
141
			Command:          "exec",
			CommandArguments: append([]string{"-T", "--user", user, service}, commandAndArgs...),
			HandlerOptions:   options,
142
143
144
145
		},
	)
}

Addshore's avatar
Addshore committed
146
147
/*ExecNoOutput runs `docker-compose exec -T <service> <commandAndArgs>` with no output*/
func (m MWDD) ExecNoOutput(service string, commandAndArgs []string, options exec.HandlerOptions, user string) error {
148
149
	options.HandleStdout = func(stdout bytes.Buffer) {}
	options.HandleError = func(stderr bytes.Buffer, err error) {}
150
	return m.DockerCompose(
151
		DockerComposeCommand{
Addshore's avatar
Addshore committed
152
153
154
			Command:          "exec",
			CommandArguments: append([]string{"-T", "--user", user, service}, commandAndArgs...),
			HandlerOptions:   options,
155
		},
Addshore's avatar
Addshore committed
156
157
158
	)
}

159
/*UpDetached runs `docker-compose up -d <services>`*/
Addshore's avatar
Addshore committed
160
func (m MWDD) UpDetached(services []string, options exec.HandlerOptions) {
161
	m.DockerComposeTTY(
162
		DockerComposeCommand{
Addshore's avatar
Addshore committed
163
164
165
			Command:          "up",
			CommandArguments: append([]string{"-d"}, services...),
			HandlerOptions:   options,
166
		},
Addshore's avatar
Addshore committed
167
168
169
	)
}

170
/*DownWithVolumesAndOrphans runs `docker-compose down --volumes --remove-orphans`*/
Addshore's avatar
Addshore committed
171
func (m MWDD) DownWithVolumesAndOrphans(options exec.HandlerOptions) {
172
	m.DockerComposeTTY(
173
		DockerComposeCommand{
Addshore's avatar
Addshore committed
174
175
176
			Command:          "down",
			CommandArguments: []string{"--volumes", "--remove-orphans"},
			HandlerOptions:   options,
177
		},
Addshore's avatar
Addshore committed
178
179
180
	)
}

181
/*Stop runs `docker-compose stop <services>`*/
Addshore's avatar
Addshore committed
182
func (m MWDD) Stop(services []string, options exec.HandlerOptions) {
183
	m.DockerComposeTTY(
184
		DockerComposeCommand{
Addshore's avatar
Addshore committed
185
			Command:          "stop",
186
			CommandArguments: services,
Addshore's avatar
Addshore committed
187
			HandlerOptions:   options,
188
		},
Addshore's avatar
Addshore committed
189
190
191
	)
}

192
/*Start runs `docker-compose start <services>`*/
Addshore's avatar
Addshore committed
193
func (m MWDD) Start(services []string, options exec.HandlerOptions) {
194
	m.DockerComposeTTY(
195
		DockerComposeCommand{
Addshore's avatar
Addshore committed
196
			Command:          "start",
197
			CommandArguments: services,
Addshore's avatar
Addshore committed
198
			HandlerOptions:   options,
199
		},
Addshore's avatar
Addshore committed
200
201
202
	)
}

203
/*Rm runs `docker-compose rm --stop --force -v <services>`*/
Addshore's avatar
Addshore committed
204
func (m MWDD) Rm(services []string, options exec.HandlerOptions) {
205
	m.DockerComposeTTY(
206
		DockerComposeCommand{
Addshore's avatar
Addshore committed
207
208
209
			Command:          "rm",
			CommandArguments: append([]string{"--stop", "--force", "-v"}, services...),
			HandlerOptions:   options,
210
211
212
213
214
		},
	)
}

/*RmVolumes runs `docker volume rm <volume names with docker-compose project prefixed>`*/
Addshore's avatar
Addshore committed
215
func (m MWDD) RmVolumes(dcVolumes []string, options exec.HandlerOptions) {
216
217
	dockerVolumes := []string{}
	for _, dcVolume := range dcVolumes {
Addshore's avatar
Addshore committed
218
		dockerVolumes = append(dockerVolumes, m.DockerComposeProjectName()+"_"+dcVolume)
219
	}
220
	exec.RunTTYCommand(
221
		options,
Addshore's avatar
Addshore committed
222
		exec.Command("docker", append([]string{"volume", "rm"}, dockerVolumes...)...),
223
224
	)
}