/
import.go
241 lines (203 loc) · 5.47 KB
/
import.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
Copyright (c) 2019 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package library
import (
"context"
"errors"
"flag"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/govc/importx"
"github.com/vmware/govmomi/vapi/library"
"github.com/vmware/govmomi/vim25/soap"
)
type item struct {
*flags.ClientFlag
*flags.OutputFlag
library.Item
manifest bool
pull bool
}
func init() {
cli.Register("library.import", &item{})
}
func (cmd *item) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)
cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
cmd.OutputFlag.Register(ctx, f)
f.StringVar(&cmd.Name, "n", "", "Library item name")
f.StringVar(&cmd.Type, "t", "", "Library item type")
f.BoolVar(&cmd.manifest, "m", false, "Require ova manifest")
f.BoolVar(&cmd.pull, "pull", false, "Pull library item from http endpoint")
}
func (cmd *item) Usage() string {
return "LIBRARY ITEM"
}
func (cmd *item) Description() string {
return `Import library items.
Examples:
govc library.import library_name file.ova
govc library.import library_name file.ovf
govc library.import library_name file.iso
govc library.import library_id file.iso # Use library id if multiple libraries have the same name
govc library.import library_name/item_name file.ova # update existing item
govc library.import library_name http://example.com/file.ovf # download and push to vCenter
govc library.import -pull library_name http://example.com/file.ova # direct pull from vCenter`
}
func (cmd *item) Process(ctx context.Context) error {
if err := cmd.ClientFlag.Process(ctx); err != nil {
return err
}
return cmd.OutputFlag.Process(ctx)
}
func (cmd *item) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() != 2 {
return flag.ErrHelp
}
file := f.Arg(1)
base := filepath.Base(file)
ext := filepath.Ext(base)
mf := strings.Replace(base, ext, ".mf", 1)
kind := ""
client, err := cmd.Client()
if err != nil {
return err
}
opener := importx.Opener{Client: client}
archive := &importx.ArchiveFlag{Archive: &importx.FileArchive{Path: file, Opener: opener}}
manifest := make(map[string]*library.Checksum)
if cmd.Name == "" {
cmd.Name = strings.TrimSuffix(base, ext)
}
switch ext {
case ".ova":
archive.Archive = &importx.TapeArchive{Path: file, Opener: opener}
base = "*.ovf"
mf = "*.mf"
kind = library.ItemTypeOVF
case ".ovf":
kind = library.ItemTypeOVF
case ".iso":
kind = library.ItemTypeISO
}
if cmd.Type == "" {
cmd.Type = kind
}
if !cmd.pull && cmd.Type == library.ItemTypeOVF {
f, _, err := archive.Open(mf)
if err == nil {
sums, err := library.ReadManifest(f)
_ = f.Close()
if err != nil {
return err
}
manifest = sums
} else {
msg := fmt.Sprintf("manifest %q: %s", mf, err)
if cmd.manifest {
return errors.New(msg)
}
fmt.Fprintln(os.Stderr, msg)
}
}
c, err := cmd.RestClient()
if err != nil {
return err
}
cmd.KeepAlive(c)
m := library.NewManager(c)
res, err := flags.ContentLibraryResult(ctx, c, "", f.Arg(0))
if err != nil {
return err
}
switch t := res.GetResult().(type) {
case library.Library:
cmd.LibraryID = t.ID
cmd.ID, err = m.CreateLibraryItem(ctx, cmd.Item)
if err != nil {
return err
}
case library.Item:
cmd.Item = t
default:
return fmt.Errorf("%q is a %T", f.Arg(0), t)
}
session, err := m.CreateLibraryItemUpdateSession(ctx, library.Session{
LibraryItemID: cmd.ID,
})
if cmd.pull {
_, err = m.AddLibraryItemFileFromURI(ctx, session, filepath.Base(file), file)
if err != nil {
return err
}
return m.WaitOnLibraryItemUpdateSession(ctx, session, 3*time.Second, nil)
}
upload := func(name string) error {
f, size, err := archive.Open(name)
if err != nil {
return err
}
defer f.Close()
if e, ok := f.(*importx.TapeArchiveEntry); ok {
name = e.Name // expand path.Match's (e.g. "*.ovf" -> "name.ovf")
}
info := library.UpdateFile{
Name: name,
SourceType: "PUSH",
Checksum: manifest[name],
Size: size,
}
update, err := m.AddLibraryItemFile(ctx, session, info)
if err != nil {
return err
}
p := soap.DefaultUpload
p.ContentLength = size
u, err := url.Parse(update.UploadEndpoint.URI)
if err != nil {
return err
}
if cmd.OutputFlag.TTY {
logger := cmd.ProgressLogger(fmt.Sprintf("Uploading %s... ", name))
p.Progress = logger
defer logger.Wait()
}
return c.Upload(ctx, f, u, &p)
}
if err = upload(base); err != nil {
return err
}
if cmd.Type == library.ItemTypeOVF {
o, err := archive.ReadOvf(base)
if err != nil {
return err
}
e, err := archive.ReadEnvelope(o)
if err != nil {
return fmt.Errorf("failed to parse ovf: %s", err)
}
for i := range e.References {
if err = upload(e.References[i].Href); err != nil {
return err
}
}
}
return m.CompleteLibraryItemUpdateSession(ctx, session)
}