-
Notifications
You must be signed in to change notification settings - Fork 88
/
app_online.go
66 lines (53 loc) · 1.59 KB
/
app_online.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
package reporting
import (
"encoding/base64"
"fmt"
"net/http"
"sync"
"time"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/util"
)
var onlineAppInfoMtx sync.Mutex
func (r *OnlineReporter) SubmitAppInfo(appID string) error {
// make sure events are reported in order
onlineAppInfoMtx.Lock()
defer func() {
time.Sleep(1 * time.Second)
onlineAppInfoMtx.Unlock()
}()
a, err := store.GetStore().GetApp(appID)
if err != nil {
if store.GetStore().IsNotFound(err) {
return nil
}
return errors.Wrap(err, "failed to get license for app")
}
license, err := store.GetStore().GetLatestLicenseForApp(a.ID)
if err != nil {
return errors.Wrap(err, "failed to get license for app")
}
endpoint := license.Spec.Endpoint
if !canReport(endpoint) {
return nil
}
url := fmt.Sprintf("%s/kots_metrics/license_instance/info", endpoint)
postReq, err := util.NewRequest("POST", url, nil)
if err != nil {
return errors.Wrap(err, "failed to create http request")
}
postReq.Header.Set("Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", license.Spec.LicenseID, license.Spec.LicenseID)))))
postReq.Header.Set("Content-Type", "application/json")
reportingInfo := GetReportingInfo(a.ID)
InjectReportingInfoHeaders(postReq, reportingInfo)
resp, err := http.DefaultClient.Do(postReq)
if err != nil {
return errors.Wrap(err, "failed to post request")
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return errors.Errorf("Unexpected status code %d", resp.StatusCode)
}
return nil
}