-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add import ova for vSphere #2970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| /* | ||
| Copyright (c) 2014-2015 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 vsphere | ||
|
|
||
| import ( | ||
| "archive/tar" | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "flag" | ||
| "fmt" | ||
| "io" | ||
| "io/ioutil" | ||
| "net/url" | ||
| "os" | ||
| "path" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/vmware/govmomi/ovf" | ||
| "github.com/vmware/govmomi/vim25" | ||
| "github.com/vmware/govmomi/vim25/soap" | ||
| ) | ||
|
|
||
| // ArchiveFlag doesn't register any flags; | ||
| // only encapsulates some common archive related functionality. | ||
| type ArchiveFlag struct { | ||
| Archive | ||
| } | ||
|
|
||
| func newArchiveFlag(ctx context.Context) (*ArchiveFlag, context.Context) { | ||
| return &ArchiveFlag{}, ctx | ||
| } | ||
|
|
||
| // Register ArchiveFlag | ||
| func (f *ArchiveFlag) Register(ctx context.Context, fs *flag.FlagSet) { | ||
| } | ||
|
|
||
| // Process ArchiveFlag | ||
| func (f *ArchiveFlag) Process(ctx context.Context) error { | ||
| return nil | ||
| } | ||
|
|
||
| // ReadOvf - open and read OVF file | ||
| func (f *ArchiveFlag) ReadOvf(fpath string) ([]byte, error) { | ||
| r, _, err := f.Open(fpath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer r.Close() | ||
|
|
||
| return ioutil.ReadAll(r) | ||
| } | ||
|
|
||
| // ReadEnvelope - open and read OVF envelope | ||
| func (f *ArchiveFlag) ReadEnvelope(data []byte) (*ovf.Envelope, error) { | ||
| e, err := ovf.Unmarshal(bytes.NewReader(data)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse ovf: %s", err) | ||
| } | ||
|
|
||
| return e, nil | ||
| } | ||
|
|
||
| // Archive interface | ||
| type Archive interface { | ||
| Open(string) (io.ReadCloser, int64, error) | ||
| } | ||
|
|
||
| // TapeArchive struct | ||
| type TapeArchive struct { | ||
| Path string | ||
| Opener | ||
| } | ||
|
|
||
| //TapeArchiveEntry struct | ||
| type TapeArchiveEntry struct { | ||
| io.Reader | ||
| f io.Closer | ||
|
|
||
| Name string | ||
| } | ||
|
|
||
| // Close the TapeArchiveEntry | ||
| func (t *TapeArchiveEntry) Close() error { | ||
| return t.f.Close() | ||
| } | ||
|
|
||
| // Open the TapeArchive locally | ||
| func (t *TapeArchive) Open(name string) (io.ReadCloser, int64, error) { | ||
| f, _, err := t.OpenFile(t.Path) | ||
| if err != nil { | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| r := tar.NewReader(f) | ||
|
|
||
| for { | ||
| h, err := r.Next() | ||
| if err == io.EOF { | ||
| break | ||
| } | ||
| if err != nil { | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| matched, err := path.Match(name, path.Base(h.Name)) | ||
| if err != nil { | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| if matched { | ||
| return &TapeArchiveEntry{r, f, h.Name}, h.Size, nil | ||
| } | ||
| } | ||
|
|
||
| _ = f.Close() | ||
|
|
||
| return nil, 0, os.ErrNotExist | ||
| } | ||
|
|
||
| // FileArchive contains the path and the opener, which can be either remotely vCenter | ||
| // or locally | ||
| type FileArchive struct { | ||
| Path string | ||
| Opener | ||
| } | ||
|
|
||
| // Open the FileArchive | ||
| func (t *FileArchive) Open(name string) (io.ReadCloser, int64, error) { | ||
| fpath := name | ||
| if name != t.Path { | ||
| index := strings.LastIndex(t.Path, "/") | ||
| if index != -1 { | ||
| fpath = t.Path[:index] + "/" + name | ||
| } | ||
| } | ||
|
|
||
| return t.OpenFile(fpath) | ||
| } | ||
|
|
||
| // Opener - contains the soap client to vCenter | ||
| type Opener struct { | ||
| *vim25.Client | ||
| } | ||
|
|
||
| func isRemotePath(path string) bool { | ||
| if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") { | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // OpenLocal - open the OVF/OVA locally | ||
| func (o Opener) OpenLocal(path string) (io.ReadCloser, int64, error) { | ||
| f, err := os.Open(filepath.Clean(path)) | ||
| if err != nil { | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| s, err := f.Stat() | ||
| if err != nil { | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| return f, s.Size(), nil | ||
| } | ||
|
|
||
| // OpenFile - determine which method to open the OVA/OVF | ||
| func (o Opener) OpenFile(path string) (io.ReadCloser, int64, error) { | ||
| if isRemotePath(path) { | ||
| return o.OpenRemote(path) | ||
| } | ||
| return o.OpenLocal(path) | ||
| } | ||
|
|
||
| // OpenRemote - open in vSphere | ||
| func (o Opener) OpenRemote(link string) (io.ReadCloser, int64, error) { | ||
| if o.Client == nil { | ||
| return nil, 0, errors.New("remote path not supported") | ||
| } | ||
|
|
||
| u, err := url.Parse(link) | ||
| if err != nil { | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| return o.Download(context.Background(), u, &soap.DefaultDownload) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like the only thing being changed from the govmomi repo is adding some comments. These are exported types so I would think we should be able to use these just from the vendored code and not copy here...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a technical reason... the things we need from the vendored code is not exportable. Therefore, we copied file(s) here with a modified
package vspherethereby making them accessible.