-
Notifications
You must be signed in to change notification settings - Fork 2
/
docker.go
204 lines (187 loc) · 5.47 KB
/
docker.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
/*
Copyright © 2020 Kosta Harlan <kosta@kostaharlan.net>
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 cmd
import (
"fmt"
"github.com/briandowns/spinner"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"log"
"os"
"os/exec"
"strings"
"time"
)
var dockerCmd = &cobra.Command{
Use: "docker",
Short: "Provides subcommands for interacting with MediaWiki's docker development environment",
RunE: nil,
}
var startCmd = &cobra.Command{
Use: "start",
Short: "Start the development environment",
Run: func(cmd *cobra.Command, args []string) {
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Prefix = "Starting the development environment "
s.Start()
command := exec.Command("docker-compose", "up", "-d")
if isLinuxHost() {
command.Env = os.Environ()
command.Env = append(
command.Env,
fmt.Sprintf("MW_DOCKER_UID=%s", string(os.Getuid())),
fmt.Sprintf("MW_DOCKER_GID=%s", string(os.Getgid())))
}
stdoutStderr, _ := command.CombinedOutput()
fmt.Print(string(stdoutStderr))
s.Stop()
handlePortError(stdoutStderr)
if composerDependenciesNeedInstallation() {
promptToInstallComposerDependencies()
}
printSuccess()
},
PreRun: func(cmd *cobra.Command, args []string) {
if !isInCoreDirectory() {
os.Exit(1)
}
if isLinuxHost() {
// TODO: We should also check the contents for correctness, maybe
// using docker-compose config and asserting that UID/GID mapping is present
// and with correct values.
_, err := os.Stat("docker-compose.override.yml")
if err != nil {
fmt.Println("Creating docker-compose.override.yml for correct user ID and group ID mapping from host to container")
var data = `
version: '3.7'
services:
mediawiki:
user: "${MW_DOCKER_UID}:${MW_DOCKER_GID}"
`
file, err := os.Create("docker-compose.override.yml")
if err != nil {
log.Fatal(err)
}
defer file.Close()
_, err = file.WriteString(data)
if err != nil {
log.Fatal(err)
}
file.Sync()
}
}
},
}
var stopCmd = &cobra.Command{
Use: "stop",
Short: "Stop development environment",
PreRun: func(cmd *cobra.Command, args []string) {
if !isInCoreDirectory() {
os.Exit(1)
}
},
Run: func(cmd *cobra.Command, args []string) {
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Prefix = "Stopping development environment "
s.Start()
command := exec.Command("docker-compose", "stop")
stdoutStderr, err := command.CombinedOutput()
s.Stop()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", stdoutStderr)
},
}
func printSuccess() {
portCommand := exec.Command("docker-compose", "port", "mediawiki", "8080")
portCommandOutput, _ := portCommand.CombinedOutput()
// Replace 0.0.0.0 with localhost
fmt.Printf("Success! View MediaWiki-Docker at http://%s",
strings.Replace(string(portCommandOutput), "0.0.0.0", "localhost", 1))
}
func handlePortError(stdoutStderr []byte) {
portError := strings.Index(string(stdoutStderr), " failed: port is already allocated")
if portError > 0 {
// TODO: This breaks if someone set port 80 for example.
fmt.Println(string(stdoutStderr))
fmt.Printf("Port %s is already allocated! \n\nPlease override the port via a .env file, see https://www.mediawiki.org/wiki/MediaWiki-Docker for instructions\n",
string(stdoutStderr[portError-4:])[0:4])
os.Exit(1)
}
}
func promptToInstallComposerDependencies() {
fmt.Println("MediaWiki has some external dependencies that need to be installed")
prompt := promptui.Prompt{
IsConfirm: true,
Label: "Install dependencies now",
}
_, err := prompt.Run()
if err == nil {
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Prefix = "Installing Composer dependencies (this may take a few minutes) "
s.Start()
os.Mkdir("cache", 0700)
depsCommand := exec.Command(
"docker-compose",
"exec",
"-T",
"mediawiki",
"composer",
"update",
)
out, err := depsCommand.CombinedOutput()
if err != nil {
fmt.Print(string(out))
log.Fatal(err)
os.Exit(1)
}
s.Stop()
}
}
func composerDependenciesNeedInstallation() bool {
// Detect if composer dependencies are not installed and prompt user to install
dependenciesCheck := exec.Command(
"docker-compose",
"exec",
"-T",
"mediawiki",
"php",
"maintenance/install.php",
"--help",
)
stdoutStderr, _ := dependenciesCheck.CombinedOutput()
return strings.Index(string(stdoutStderr), " dependencies that need to be installed") > 0
}
func isInCoreDirectory() bool {
if _, err := os.Stat("README.mediawiki"); err == nil {
return true
}
fmt.Println("❌ Please run this command within the root of the MediaWiki core repository.")
return false
}
func isLinuxHost() bool {
unameCommand := exec.Command("uname")
stdout, err := unameCommand.CombinedOutput()
if err != nil {
log.Fatal(err)
os.Exit(1)
}
return string(stdout) == "Linux\n"
}
func init() {
rootCmd.AddCommand(dockerCmd)
dockerCmd.AddCommand(startCmd)
dockerCmd.AddCommand(stopCmd)
}