/
relayered_image.go
223 lines (177 loc) · 5.12 KB
/
relayered_image.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//go:build relayered || !onlyprovider
package relayered
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"github.com/dustin/go-humanize"
"github.com/nanovms/ops/lepton"
"github.com/nanovms/ops/log"
"github.com/nanovms/ops/types"
"github.com/olekukonko/tablewriter"
)
// BuildImage to be upload on relayered
func (v *Relayered) BuildImage(ctx *lepton.Context) (string, error) {
c := ctx.Config()
err := lepton.BuildImage(*c)
if err != nil {
return "", err
}
return v.CustomizeImage(ctx)
}
// BuildImageWithPackage to upload on relayered.
func (v *Relayered) BuildImageWithPackage(ctx *lepton.Context, pkgpath string) (string, error) {
c := ctx.Config()
err := lepton.BuildImageFromPackage(pkgpath, *c)
if err != nil {
return "", err
}
return v.CustomizeImage(ctx)
}
func (v *Relayered) destroyImage(snapshotid string) {
}
// CreateImage - Creates image on relayered using nanos images
func (v *Relayered) CreateImage(ctx *lepton.Context, imagePath string) error {
imgName := ctx.Config().CloudConfig.ImageName
v.createImage(ctx, "", imgName)
return nil
}
func (v *Relayered) createImage(ctx *lepton.Context, icow string, imgName string) {
filename := lepton.GetOpsHome() + "/" + "images/" + imgName
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
fileWriter, err := bodyWriter.CreateFormFile("uploadfile", imgName)
if err != nil {
fmt.Println("error writing to buffer")
}
fh, err := os.Open(filename)
if err != nil {
fmt.Println("error opening file")
}
defer fh.Close()
_, err = io.Copy(fileWriter, fh)
if err != nil {
fmt.Println(err)
}
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
uri := baseURI + "/images/create"
client := &http.Client{}
req, err := http.NewRequest("POST", uri, bodyBuf)
if err != nil {
fmt.Println(err)
}
req.Header.Add("Content-Type", contentType)
req.Header.Set("RELAYERED_TOKEN", v.token)
req.Header.Add("Accept", "application/json")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(body))
}
// ImageListResponse is the set of instances available from relayered in an
// images list call.
type ImageListResponse []Image
// Image represents a given relayered image configuration.
type Image struct {
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
CreatedAt string `json:"created_at"`
}
// GetImages return all images on relayered
// needs tags added
func (v *Relayered) GetImages(ctx *lepton.Context) ([]lepton.CloudImage, error) {
client := &http.Client{}
uri := baseURI + "/images/list"
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
fmt.Println(err)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Set("RELAYERED_TOKEN", v.token)
req.Header.Add("Accept", "application/json")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
}
var ilr ImageListResponse
err = json.Unmarshal(body, &ilr)
if err != nil {
fmt.Println(err)
}
err = json.Unmarshal(body, &ilr)
if err != nil {
fmt.Println(err)
}
var images []lepton.CloudImage
for _, img := range ilr {
images = append(images, lepton.CloudImage{
ID: img.ID,
Name: img.Name,
Status: img.Status,
Path: "",
})
}
return images, nil
}
// ListImages lists images on relayered
func (v *Relayered) ListImages(ctx *lepton.Context) error {
images, err := v.GetImages(ctx)
if err != nil {
fmt.Println(err)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Name", "Date created", "Size", "Status"})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor})
table.SetRowLine(true)
for _, image := range images {
var row []string
row = append(row, image.ID)
row = append(row, image.Name)
row = append(row, "")
row = append(row, humanize.Bytes(uint64(image.Size)))
row = append(row, image.Status)
table.Append(row)
}
table.Render()
return nil
}
// DeleteImage deletes image from v
func (v *Relayered) DeleteImage(ctx *lepton.Context, snapshotID string) error {
return nil
}
// SyncImage syncs image from provider to another provider
func (v *Relayered) SyncImage(config *types.Config, target lepton.Provider, image string) error {
log.Warn("not yet implemented")
return nil
}
// ResizeImage is not supported on relayered.
func (v *Relayered) ResizeImage(ctx *lepton.Context, imagename string, hbytes string) error {
return fmt.Errorf("operation not supported")
}
// CustomizeImage returns image path with adaptations needed by cloud provider
func (v *Relayered) CustomizeImage(ctx *lepton.Context) (string, error) {
imagePath := ctx.Config().RunConfig.ImageName
return imagePath, nil
}