-
Notifications
You must be signed in to change notification settings - Fork 90
/
install.go
116 lines (99 loc) · 2.94 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
110
111
112
113
114
115
116
package metrics
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"regexp"
"time"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/buildversion"
"github.com/replicatedhq/kots/pkg/util"
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
"github.com/segmentio/ksuid"
)
const (
DevEndpoint = "http://localhost:30016"
ProdEndpoint = "https://replicated.app"
)
type InstallMetrics struct {
endpoint string
disableOutboundConnections bool
InstallID string `json:"install_id"`
StartedAt time.Time `json:"started"`
FinishedAt time.Time `json:"finished"`
FailedAt time.Time `json:"failed"`
KotsVersion string `json:"kots_version"`
Cause string `json:"cause"`
}
func InitInstallMetrics(license *kotsv1beta1.License, disableOutboundConnections bool) InstallMetrics {
m := InstallMetrics{
endpoint: getEndpoint(license),
disableOutboundConnections: disableOutboundConnections,
InstallID: ksuid.New().String(),
StartedAt: time.Now(),
KotsVersion: buildversion.Version(),
}
return m
}
func (m InstallMetrics) ReportInstallStart() error {
if m.endpoint == "" || m.InstallID == "" {
return nil
}
url := fmt.Sprintf("%s/kots_metrics/start_install/%s", m.endpoint, m.InstallID)
return m.Post(url)
}
func (m InstallMetrics) ReportInstallFail(cause string) error {
if m.endpoint == "" || m.InstallID == "" {
return nil
}
m.FailedAt = time.Now()
m.Cause = cause
url := fmt.Sprintf("%s/kots_metrics/fail_install/%s", m.endpoint, m.InstallID)
return m.Post(url)
}
func (m InstallMetrics) ReportInstallFinish() error {
if m.endpoint == "" || m.InstallID == "" {
return nil
}
m.FinishedAt = time.Now()
url := fmt.Sprintf("%s/kots_metrics/finish_install/%s", m.endpoint, m.InstallID)
return m.Post(url)
}
func (m InstallMetrics) Post(url string) error {
if m.disableOutboundConnections {
return nil
}
b, err := json.Marshal(m)
if err != nil {
return errors.Wrap(err, "failed to marshal json")
}
req, err := util.NewRequest("POST", url, bytes.NewBuffer(b))
if err != nil {
return errors.Wrap(err, "failed to create new request")
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "failed to execute post request")
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
return errors.Errorf("unexpected status code: %d", resp.StatusCode)
}
return nil
}
func getEndpoint(license *kotsv1beta1.License) string {
if license != nil {
if isDevEndpoint(license.Spec.Endpoint) {
// cluster ip services are not resolvable from the cli ...
return DevEndpoint
}
return license.Spec.Endpoint
}
return ProdEndpoint
}
func isDevEndpoint(endpoint string) bool {
result, _ := regexp.MatchString(`replicated-app`, endpoint)
return result
}