-
Notifications
You must be signed in to change notification settings - Fork 48
/
runtime_check.go
72 lines (59 loc) · 1.49 KB
/
runtime_check.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
package checks
import (
"errors"
"io/ioutil"
"net/http"
"path/filepath"
"regexp"
"time"
"github.com/newrelic/newrelic-lambda-extension/util"
)
type httpClient interface {
Get(string) (*http.Response, error)
}
var (
client httpClient
re = regexp.MustCompile(`\/releases\/tag\/(v[0-9.]+)`)
)
func init() {
client = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Timeout: time.Second * 10,
}
}
func checkAndReturnRuntime() (runtimeConfig, error) {
for k, v := range runtimeConfigs {
p := filepath.Join(runtimeLookupPath, string(k))
if util.PathExists(p) {
err := latestAgentTag(&v)
return v, err
}
}
// If we make it here that means the runtime is not one we
// currently validate so we don't want to warn against anything
return runtimeConfig{}, nil
}
func latestAgentTag(r *runtimeConfig) error {
resp, err := client.Get(r.agentVersionUrl)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 && resp.StatusCode != 302 {
// The version check HTTP request failed; this doesn't tell us anything
util.Debugf("Can't query latest agent version. Request to %v returned status %v", r.agentVersionUrl, resp.StatusCode)
return nil
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
rs := re.FindStringSubmatch(string(body))
if len(rs) != 2 {
return errors.New("Can't determine latest agent version.")
}
r.AgentVersion = rs[1]
return nil
}