-
Notifications
You must be signed in to change notification settings - Fork 88
/
upload_license.go
110 lines (92 loc) · 2.85 KB
/
upload_license.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
package upload
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/k8sutil"
"github.com/replicatedhq/kots/pkg/logger"
)
type UploadLicenseOptions struct {
Namespace string
Kubeconfig string
NewAppName string
}
func UploadLicense(path string, uploadLicenseOptions UploadLicenseOptions) error {
b, err := ioutil.ReadFile(path)
if err != nil {
return errors.Wrap(err, "failed to read license file")
}
license := string(b)
// Make sure we have a name or slug
if uploadLicenseOptions.NewAppName == "" {
appName, err := relentlesslyPromptForAppName("")
if err != nil {
return errors.Wrap(err, "failed to prompt for app name")
}
uploadLicenseOptions.NewAppName = appName
}
// Find the kotadm-api pod
log := logger.NewLogger()
log.ActionWithSpinner("Uploading license to Admin Console")
podName, err := findKotsadm(uploadLicenseOptions.Namespace)
if err != nil {
log.FinishSpinnerWithError()
return errors.Wrap(err, "failed to find kotsadm pod")
}
// set up port forwarding to get to it
stopCh, err := k8sutil.PortForward(uploadLicenseOptions.Kubeconfig, 3000, 3000, uploadLicenseOptions.Namespace, podName)
if err != nil {
log.FinishSpinnerWithError()
return errors.Wrap(err, "failed to start port forwarding")
}
defer close(stopCh)
// upload using http to the pod directly
req, err := createUploadLicenseRequest(license, uploadLicenseOptions, "http://localhost:3000/api/v1/kots/license")
if err != nil {
log.FinishSpinnerWithError()
return errors.Wrap(err, "failed to create upload request")
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.FinishSpinnerWithError()
return errors.Wrap(err, "failed to execute request")
}
if resp.StatusCode != 201 {
log.FinishSpinnerWithError()
return errors.Errorf("unexpected status code: %d", resp.StatusCode)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.FinishSpinnerWithError()
return errors.Wrap(err, "failed to read response body")
}
type UploadResponse struct {
URI string `json:"uri"`
}
var uploadResponse UploadResponse
if err := json.Unmarshal(respBody, &uploadResponse); err != nil {
log.FinishSpinnerWithError()
return errors.Wrap(err, "failed to unmarshal response")
}
log.FinishSpinner()
return nil
}
func createUploadLicenseRequest(license string, uploadLicenseOptions UploadLicenseOptions, uri string) (*http.Request, error) {
body := map[string]string{
"name": uploadLicenseOptions.NewAppName,
"license": license,
}
b, err := json.Marshal(body)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal json")
}
req, err := http.NewRequest("POST", uri, bytes.NewBuffer(b))
if err != nil {
return nil, errors.Wrap(err, "failed to create new request")
}
req.Header.Set("Content-Type", "application/json")
return req, nil
}