-
Notifications
You must be signed in to change notification settings - Fork 41
/
install.go
109 lines (98 loc) · 3.76 KB
/
install.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
// Copyright 2019 Tetrate
//
// 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 envoy
import (
"context"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"time"
"github.com/tetratelabs/func-e/internal/globals"
"github.com/tetratelabs/func-e/internal/moreos"
"github.com/tetratelabs/func-e/internal/tar"
"github.com/tetratelabs/func-e/internal/version"
)
var binEnvoy = filepath.Join("bin", "envoy"+moreos.Exe)
// InstallIfNeeded downloads an Envoy binary corresponding to globals.GlobalOpts and returns a path to it or an error.
func InstallIfNeeded(ctx context.Context, o *globals.GlobalOpts) (string, error) {
v := o.EnvoyVersion
installPath := filepath.Join(o.HomeDir, "versions", v.String())
envoyPath := filepath.Join(installPath, binEnvoy)
_, err := os.Stat(envoyPath)
switch {
case os.IsNotExist(err):
var evs *version.ReleaseVersions // Get version metadata for what we will install
evs, err = o.GetEnvoyVersions(ctx)
if err != nil {
return "", err
}
tarballURL := evs.Versions[v].Tarballs[o.Platform] // Ensure there is a version for this platform
if tarballURL == "" {
return "", fmt.Errorf("couldn't find version %q for platform %q", v, o.Platform)
}
tarball := version.Tarball(path.Base(string(tarballURL)))
sha256Sum := evs.SHA256Sums[tarball]
if len(sha256Sum) != 64 {
return "", fmt.Errorf("couldn't find sha256Sum of version %q for platform %q: %w", v, o.Platform, err)
}
var mtime time.Time // Create a directory for the version, preserving the release date as its mtime
if mtime, err = time.Parse("2006-01-02", string(evs.Versions[v].ReleaseDate)); err != nil {
return "", fmt.Errorf("couldn't find releaseDate of version %q for platform %q: %w", v, o.Platform, err)
}
if err = os.MkdirAll(installPath, 0o750); err != nil {
return "", fmt.Errorf("unable to create directory %q: %w", installPath, err)
}
o.Logf("downloading %s\n", tarballURL)
if err = untarEnvoy(ctx, installPath, tarballURL, sha256Sum, o.Platform, o.Version); err != nil { //nolint
return "", err
}
if err = os.Chtimes(installPath, mtime, mtime); err != nil { // overwrite the mtime to preserve it in the list
return "", fmt.Errorf("unable to set date of directory %q: %w", installPath, err)
}
case err == nil:
o.Logf("%s is already downloaded\n", v)
default:
// TODO: figure out how to get a stat error that isn't file not exist so we can test this
return "", err
}
return verifyEnvoy(installPath)
}
func verifyEnvoy(installPath string) (string, error) {
envoyPath := filepath.Join(installPath, binEnvoy)
stat, err := os.Stat(envoyPath)
if err != nil {
return "", err
}
if !moreos.IsExecutable(stat) {
return "", fmt.Errorf("envoy binary not executable at %q", envoyPath)
}
return envoyPath, nil
}
func untarEnvoy(ctx context.Context, dst string, src version.TarballURL, // dst, src order like io.Copy
sha256Sum version.SHA256Sum, p version.Platform, v string) error {
res, err := httpGet(ctx, string(src), p, v)
if err != nil {
return err
}
defer res.Body.Close() //nolint
if res.StatusCode != http.StatusOK {
return fmt.Errorf("received %v status code from %s", res.StatusCode, src)
}
if err = tar.UntarAndVerify(dst, res.Body, sha256Sum); err != nil {
return fmt.Errorf("error untarring %s: %w", src, err)
}
return nil
}