-
Notifications
You must be signed in to change notification settings - Fork 173
/
execution_vsphere.go
128 lines (112 loc) · 3.99 KB
/
execution_vsphere.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
// Copyright 2016 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 exec2
import (
"fmt"
"net/url"
)
// PortLayerVsphere is a WIP implementation of the execution.go interfaces
type PortLayerVsphere struct {
vmomiGateway VmomiGateway
handles HandleFactory
containers map[ID]*container
}
func (p *PortLayerVsphere) getContainer(handle Handle) *container {
return p.containers[handle.(*PendingCommit).ContainerID]
}
func (p *PortLayerVsphere) newHandle(cid ID) *PendingCommit {
return p.handles.createHandle(cid).(*PendingCommit)
}
func (p *PortLayerVsphere) Init(gateway VmomiGateway, factory HandleFactory) error {
p.handles = factory
p.vmomiGateway = gateway
p.containers = make(map[ID]*container)
return nil
}
func (p *PortLayerVsphere) CreateContainer(name string) (Handle, error) {
cid := GenerateID()
handle := p.newHandle(cid)
handle.config.Name = name
handle.runState = Created
return handle, nil
}
func (p *PortLayerVsphere) GetHandle(cid ID) (Handle, error) {
c := p.containers[cid]
if c == nil {
return nil, fmt.Errorf("Invalid container ID")
}
return p.handles.createHandle(c.ContainerID), nil
}
func (p *PortLayerVsphere) SetEntryPoint(handle Handle, workDir string, execPath string, execArgs string) (Handle, error) {
resolvedHandle := handle.(*PendingCommit)
resolvedHandle.mainProcess = NewProcessConfig(workDir, execPath, execArgs)
return p.handles.refreshHandle(handle), nil
}
func (p *PortLayerVsphere) Commit(handle Handle) (ID, error) {
var err error
c := p.getContainer(handle)
if c == nil {
c, err = p.createContainer(handle)
} else {
// if c.vm == nil {
// return "", fmt.Errorf("Cannot modify container with no VM")
// }
err = p.modifyContainer(c.runState, handle)
}
// Handle will be garbage collected
return c.ContainerID, err
}
func (p *PortLayerVsphere) CopyTo(handle Handle, targetDir string, fname string, perms int16, data []byte) (Handle, error) {
var result Handle
resolvedHandle := handle.(*PendingCommit)
u, err := url.Parse("file://" + targetDir + "/" + fname)
if err == nil {
fileToCopy := FileToCopy{target: *u, perms: perms, data: data}
resolvedHandle.filesToCopy = append(resolvedHandle.filesToCopy, fileToCopy)
result = p.handles.refreshHandle(handle)
}
return result, err
}
func (p *PortLayerVsphere) SetLimits(handle Handle, memoryMb int, cpuMhz int) (Handle, error) {
resolvedHandle := handle.(*PendingCommit)
resolvedHandle.config.Limits = ResourceLimits{MemoryMb: memoryMb, CPUMhz: cpuMhz}
return p.handles.refreshHandle(handle), nil
}
func (p *PortLayerVsphere) SetRunState(handle Handle, runState RunState) (Handle, error) {
resolvedHandle := handle.(*PendingCommit)
resolvedHandle.runState = runState
return p.handles.refreshHandle(handle), nil
}
func (p *PortLayerVsphere) DestroyContainer(cid ID) error {
c := p.containers[cid]
if c == nil {
return fmt.Errorf("Invalid container ID")
}
delete(p.containers, cid)
return nil
}
func (p *PortLayerVsphere) createContainer(handle Handle) (*container, error) {
resolvedHandle := handle.(*PendingCommit)
c := container{}
p.containers[resolvedHandle.ContainerID] = &c
c.ContainerID = resolvedHandle.ContainerID
c.runState = resolvedHandle.runState
// followed by other transfer of state from pending to container
// fmt.Printf("Creating container for %v\n", pending)
return &c, nil
}
func (p *PortLayerVsphere) modifyContainer(runState RunState, handle Handle) error {
// fmt.Printf("Modifying container for %v\n", pending)
return nil
}