-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
setup.go
154 lines (135 loc) · 5 KB
/
setup.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 setup
import (
"fmt"
"os"
"path/filepath"
devtools "github.com/elastic/beats/dev-tools/mage"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
)
// RunSetup runs any remaining setup commands after the vendor directory has been setup
func RunSetup() error {
vendorPath := "./vendor/github.com/"
//Copy mage stuff
toMkdir := filepath.Join(vendorPath, "magefile")
err := os.MkdirAll(toMkdir, 0755)
if err != nil {
return errors.Wrapf(err, "error making mage directory at %s", toMkdir)
}
err = sh.Run("cp", "-R", filepath.Join(vendorPath, "elastic/beats/vendor/github.com/magefile/mage"), filepath.Join(vendorPath, "magefile"))
if err != nil {
return errors.Wrapf(err, "error copying vendored magefile to %s", filepath.Join(vendorPath, "magefile"))
}
//Copy the pkg helper
err = sh.Run("cp", "-R", filepath.Join(vendorPath, "elastic/beats/vendor/github.com/pkg"), vendorPath)
if err != nil {
return errors.Wrapf(err, "error copying pkg to %s", vendorPath)
}
return LinkImportsHelper()
}
// LinkImportsHelper links generate_imports_helper.py
func LinkImportsHelper() error {
vendorPath := "./vendor/github.com/"
pwd, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "error gettting current directory")
}
return sh.Run("ln", "-sf", filepath.Join(pwd, vendorPath, "elastic/beats/metricbeat/scripts/generate_imports_helper.py"), filepath.Join(pwd, vendorPath, "elastic/beats/script/generate_imports_helper.py"))
}
// CopyVendor copies a new version of beats into the vendor directory of PWD
// By default this uses git archive, meaning any uncommitted changes will not be copied.
// Set the NEWBEAT_DEV env variable to use a slow `cp` copy that will catch uncommited changes
func CopyVendor() error {
vendorPath := "./vendor/github.com/elastic/"
beatPath, err := devtools.ElasticBeatsDir()
if err != nil {
return errors.Wrap(err, "Could not find ElasticBeatsDir")
}
err = os.MkdirAll(vendorPath, 0755)
if err != nil {
return errors.Wrap(err, "error creating vendor dir")
}
isClean, err := checkBeatsDirClean()
if err != nil {
return errors.Wrap(err, "error in checkIfBeatsDirClean")
}
if !isClean {
//Dev mode. Use CP.
fmt.Printf("You have uncommited changes in elastic/beats. Running CopyVendor running in dev mode, elastic/beats will be copied into the vendor directory with cp\n")
vendorPath = filepath.Join(vendorPath, "beats")
err = sh.Run("cp", "-R", beatPath, vendorPath)
if err != nil {
return errors.Wrap(err, "error copying vendor dir")
}
err = sh.Rm(filepath.Join(vendorPath, ".git"))
if err != nil {
return errors.Wrap(err, "error removing vendor git directory")
}
err = sh.Rm(filepath.Join(vendorPath, "x-pack"))
if err != nil {
return errors.Wrap(err, "error removing x-pack directory")
}
} else {
//not dev mode. Use git archive
vendorPath = filepath.Join(vendorPath, "beats")
err = os.MkdirAll(vendorPath, 0755)
if err != nil {
return errors.Wrap(err, "error creating vendor dir")
}
err = sh.Run("sh",
"-c",
"git archive --remote "+beatPath+" HEAD | tar -x --exclude=x-pack -C "+vendorPath)
if err != nil {
return errors.Wrap(err, "error running git archive")
}
}
return nil
}
// checkIfBeatsDirClean checks to see if the working elastic/beats dir is modified.
// If it is, we'll use a different method to copy beats to vendor/
func checkBeatsDirClean() (bool, error) {
beatPath, err := devtools.ElasticBeatsDir()
if err != nil {
return false, errors.Wrap(err, "Could not find ElasticBeatsDir")
}
out, err := sh.Output("git", "-C", beatPath, "status", "--porcelain")
if err != nil {
return false, errors.Wrap(err, "Error checking status of elastic/beats repo")
}
if len(out) == 0 {
return true, nil
}
return false, nil
}
// GitInit initializes a new git repo in the current directory
func GitInit() error {
return sh.Run("git", "init")
}
// GitAdd adds the current working directory to git and does an initial commit
func GitAdd() error {
err := sh.Run("git", "add", "-A")
if err != nil {
return errors.Wrap(err, "error running git add")
}
return sh.Run("git", "commit", "-q", "-m", "Initial commit, Add generated files")
}
// Update updates the generated files (aka make update).
func Update() error {
return sh.Run("make", "update")
}