forked from cncf/tag-contributor-strategy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagefile.go
282 lines (234 loc) · 7.42 KB
/
magefile.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//go:build mage
// +build mage
// This is a magefile, and is a "makefile for go".
// See https://magefile.org/
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/carolynvs/magex/pkg"
"github.com/carolynvs/magex/shx"
"github.com/carolynvs/magex/xplat"
"github.com/magefile/mage/mg"
"github.com/pkg/errors"
)
var (
// Default target to run when none is specified
Default = Preview
contributeRepo = "../contribute"
must = shx.CommandBuilder{StopOnError: true}
)
const (
containerName = "sig-contributor-strategy"
img = containerName
)
// Ensure Mage is installed and on the PATH.
func EnsureMage() error {
return pkg.EnsureMage("")
}
// Compile the website to website/public.
func Build() error {
mg.Deps(clean, buildImage)
// Build the volume mount for a local contribute repo, if present
contribMount, goModMount, err := useLocalContributeModule()
if err != nil {
return err
}
pwd, _ := os.Getwd()
return shx.Command("docker", "run", "--rm", "-v", pwd+":/src",
contribMount, goModMount, containerName, "--debug", "--verbose").CollapseArgs().RunV()
}
// Run a local server to preview the website and watch for changes.
func Preview() error {
mg.Deps(clean, buildImage)
// Build the volume mount for a local contribute repo, if present
contribMount, goModMount, err := useLocalContributeModule()
if err != nil {
return err
}
port := getPort()
pwd, _ := os.Getwd()
err = shx.Command("docker", "run", "-d", "-v", pwd+":/src",
contribMount, goModMount, "-p", port+":1313",
"--name", containerName, img, "server", "--debug", "--verbose",
"--buildDrafts", "--buildFuture", "--noHTTPCache", "--watch", "--bind=0.0.0.0").CollapseArgs().RunV()
if err != nil {
return errors.Wrap(err, "could not run website container")
}
err = awaitContainer(containerName, "Web Server is available")
if err != nil {
return errors.Wrap(err, "error waiting for the website to become ready")
}
url := "http://localhost:" + getPort()
return errors.Wrap(openURL(url), "could not open the website in a browser")
}
func Logs() error {
return shx.RunV("docker", "logs", "-f", img)
}
// Use hugo in a docker container.
func Hugo() error {
pwd, _ := os.Getwd()
cmd := shx.Command("docker", "run", "--rm", "-it", "-v", pwd+":/src", "-w", "/src/website", img, "shell").
Stdout(os.Stdout)
cmd.Cmd.Stdin = os.Stdin
_, _, err := cmd.Exec()
return errors.Wrap(err, "could not start hugo in a container")
}
// Build the live website.
func Deploy() error {
mg.Deps(docsy, syncGoMod, netlifySetup)
return shx.RunV("hugo", "-s", "website", "--debug", "--verbose", "-b", "https://contribute.cncf.io/")
}
// Deploy branch builds the website, including future dated and draft posts
func DeployBranch() error {
mg.Deps(docsy, syncGoMod, netlifySetup)
return shx.RunV("hugo", "-s", "website", "--debug", "--buildDrafts", "--buildFuture", "--verbose", "-b", getBaseUrl())
}
// Deploy preview builds the website for a pull request, using the same build settings as the live site.
func DeployPreview() error {
mg.Deps(docsy, syncGoMod, netlifySetup)
return shx.RunV("hugo", "-s", "website", "--debug", "--buildDrafts", "--buildFuture", "--verbose", "-b", getBaseUrl())
}
func syncGoMod() error {
return shx.RunV("go", "mod", "download")
}
func getBaseUrl() string {
host := os.Getenv("DEPLOY_PRIME_URL")
if host != "" {
return host + "/"
}
return "https://contribute.cncf.io/"
}
// Create go.local.mod with any appropriate replace statements, and
// returns the local contribute mount flag if present.
func useLocalContributeModule() (contribMount string, goModMount string, err error) {
// Edit a copy of website/go.mod so that it doesn't always show up as modified in git
pwd, _ := os.Getwd()
localGoMod := filepath.Join(pwd, "website/go.local.mod")
err = copyFile("website/go.mod", localGoMod)
if err != nil {
return "", "", err
}
goModMount = fmt.Sprintf("-v=%s:/src/website/go.mod", localGoMod)
err = shx.RunV("docker", "run", "--rm", "--entrypoint", "go",
"-v", pwd+":/src", goModMount, "-w", "/src/website", img,
"mod", "download")
if err != nil {
return "", "", errors.Wrap(err, "could not modify resolve go.mod")
}
// Only mount the local repo if it exists, otherwise use the one on github
if mg.Verbose() {
fmt.Println("Checking for a local copy of github.com/cncf/contribute at", contributeRepo)
}
if repo, ok := os.LookupEnv("CONTRIBUTE_REPO"); ok {
contributeRepo = repo
}
contributeRepo, _ := filepath.Abs(contributeRepo)
_, err = os.Stat(contributeRepo)
if err != nil {
return "", goModMount, nil
}
log.Println("Using your local copy of github.com/cncf/contribute ->", contributeRepo)
err = shx.RunV("docker", "run", "--rm", "--entrypoint", "go",
"-v", pwd+":/src", goModMount, "-w", "/src/website", img,
"mod", "edit", "-replace", "github.com/cncf/contribute=/src/contribute")
if err != nil {
return "", "", errors.Wrap(err, "could not modify go.mod to use your local copy of github.com/cncf/contribute")
}
contribMount = fmt.Sprintf("-v=%s:/src/contribute", contributeRepo)
return contribMount, goModMount, nil
}
func copyFile(src string, dest string) error {
s, err := os.Open(src)
if err != nil {
return errors.Wrapf(err, "could not open %s", src)
}
d, err := os.Create(dest)
if err != nil {
return errors.Wrapf(err, "could not create %s", dest)
}
_, err = io.Copy(d, s)
return errors.Wrapf(err, "could not copy %s to %s", src, dest)
}
func openURL(url string) error {
shell := xplat.DetectShell()
if shell == "msystem" {
return shx.RunE("cmd", "/C", "open "+url)
}
if runtime.GOOS == "windows" {
return shx.RunE("powershell", "Start-Process", url)
}
return shx.RunE(shell, "-c", "open "+url)
}
func getPort() string {
port := os.Getenv("PORT")
if port == "" {
port = "1313"
}
return port
}
func buildImage() error {
mg.Deps(docsy)
err := shx.RunE("docker", "build", "-t", img,
"-f", "website/Dockerfile", ".")
return errors.Wrap(err, "could not build website image")
}
func docsy() error {
_, err := os.Stat("website/themes/docsy/assets/vendor/bootstrap/scss/bootstrap.scss")
if err != nil {
if os.IsNotExist(err) {
return shx.RunV("git", "submodule", "update", "--init", "--recursive", "--force")
}
return errors.Wrap(err, "could not clone the docsy theme")
}
return nil
}
func containerExists(name string) bool {
output, err := shx.Output("docker", "ps", "--all", "--filter", "name="+name)
return err == nil && strings.Contains(output, name)
}
func removeContainer(name string) error {
return shx.RunV("docker", "rm", "-f", name)
}
func awaitContainer(name string, logSearch string) error {
cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
for {
select {
case <-cxt.Done():
return errors.Errorf("timeout waiting for container %s to become ready", name)
default:
logs, err := shx.Output("docker", "logs", name)
if err != nil {
return errors.Wrapf(err, "could not get logs for container %s", name)
}
if strings.Contains(logs, logSearch) {
return nil
}
if mg.Verbose() {
fmt.Println(logs)
}
time.Sleep(time.Second)
}
}
}
func netlifySetup() error {
return shx.Command("npm", "install").In("website").RunV()
}
func clean() error {
err := os.RemoveAll("website/public")
if err != nil {
return errors.Wrap(err, "could not remove website/public")
}
if containerExists(containerName) {
return removeContainer(containerName)
}
return nil
}