-
Notifications
You must be signed in to change notification settings - Fork 83
/
command.go
90 lines (81 loc) · 2.98 KB
/
command.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
// Copyright Istio Authors. All Rights Reserved.
//
// 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 util
import (
"os"
"strings"
"github.com/Masterminds/semver/v3"
"sigs.k8s.io/yaml"
"istio.io/istio/pkg/log"
"istio.io/release-builder/pkg/model"
)
func StandardEnv(manifest model.Manifest) []string {
env := os.Environ()
env = append(env,
"GOPATH="+manifest.WorkDir(),
"TAG="+manifest.Version,
"VERSION="+manifest.Version,
"BUILD_WITH_CONTAINER=0", // Build should already run in container, having multiple layers of docker causes issues
"IGNORE_DIRTY_TREE=1",
"INCLUDE_UNTAGGED_DEFAULT=true",
"DOCKER_ARCHITECTURES="+strings.Join(manifest.Architectures, ","),
)
if manifest.Docker != "" {
env = append(env, "HUB="+manifest.Docker)
}
return env
}
func removeEnvKey(s []string, key string) []string {
for i, v := range s {
if strings.HasPrefix(v, key+"=") {
return append(s[:i], s[i+1:]...)
}
}
return s
}
// RunMake runs a make command for the repo, with standard environment variables set
func RunMake(manifest model.Manifest, repo string, env []string, c ...string) error {
cmd := VerboseCommand("make", c...)
cmd.Env = StandardEnv(manifest)
// Unset the environment variables that are set in a container which cause `make` artifacts
// to build in the container directories. release-builder expects all `make` artifacts to be
// created in the manifest specified directory.
cmd.Env = removeEnvKey(cmd.Env, "TARGET_OUT")
cmd.Env = removeEnvKey(cmd.Env, "TARGET_OUT_LINUX")
cmd.Env = removeEnvKey(cmd.Env, "CONTAINER_TARGET_OUT")
cmd.Env = removeEnvKey(cmd.Env, "CONTAINER_TARGET_OUT_LINUX")
cmd.Env = removeEnvKey(cmd.Env, "TARGET_OS")
cmd.Env = removeEnvKey(cmd.Env, "TARGET_ARCH")
cmd.Env = removeEnvKey(cmd.Env, "FOR_BUILD_CONTAINER")
cmd.Env = append(cmd.Env, env...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Dir = manifest.RepoDir(repo)
log.Infof("Running make %v with env=%v wd=%v", strings.Join(c, " "), strings.Join(env, " "), cmd.Dir)
return cmd.Run()
}
// YamlLog logs a object as yaml
func YamlLog(prefix string, i interface{}) {
manifestYaml, _ := yaml.Marshal(i)
log.Infof("%s: %v", prefix, string(manifestYaml))
}
// IsValidSemver checks if the string is a valid semver
// Mirror https://github.com/helm/helm/blob/9fafb4ad6811afb017cc464b630be2ff8390ac63/pkg/chart/metadata.go#L144
func IsValidSemver(v string) bool {
_, err := semver.NewVersion(v)
if err != nil {
log.Infof("Error returned from semver check %v", err)
}
return err == nil
}