-
Notifications
You must be signed in to change notification settings - Fork 3
/
workspace.go
176 lines (159 loc) · 3.85 KB
/
workspace.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"time"
"github.com/fsouza/go-dockerclient"
)
type EvalResult struct {
Command string
Code int
Deleted bool
Duration time.Duration
Log *Buffer
Changes []docker.Change
Id string //container ID
BaseImage string //assumed base image, used during :from switches
Image string //image run against
NewImage string //image with committed changes
}
type Workspace struct {
Mode string
Image string //configured base image
CurrentImage string
history []EvalResult
docker DockerService
}
func NewWorkspace(docker DockerService, mode string, image string) *Workspace {
ws := &Workspace{
Mode: mode,
Image: image,
CurrentImage: image,
history: []EvalResult{},
docker: docker,
}
return ws
}
func (w *Workspace) SetImage(image string) error {
if err := verifyImage(w.docker, image); err != nil {
return err
}
if w.CurrentImage == w.Image {
w.CurrentImage = image
}
w.Image = image
return nil
}
func (w *Workspace) CommitLast() (string, error) {
history := w.history
if len(history) == 0 {
return "", errors.New("No container found to commit")
}
lastResult := len(w.history)-1
if history[lastResult].NewImage != "" {
return "", errors.New("Container already committed")
}
image, err := w.commit(history[lastResult].Id)
if err != nil {
return "", err
}
history[lastResult].NewImage = image
history[lastResult].Deleted = false
w.history = history
return image, nil
}
// Run runs Eval but also auto-commits on return code 0
func (w *Workspace) Run(command string) (EvalResult, error) {
res, err := w.evalCommand(command)
if res.Code == 0 {
if imageId, err := w.commit(res.Id); err == nil {
res.NewImage = imageId
} else {
fmt.Println(err)
}
}
w.history = append(w.history, res)
return res, err
}
// Eval runs the command and updates lastContainer
func (w *Workspace) Eval(command string) (EvalResult, error) {
res, err := w.evalCommand(command)
res.Deleted = true
w.history = append(w.history, res)
return res, err
}
func (w *Workspace) evalCommand(command string) (EvalResult, error) {
res, err := Eval(w.docker, command, w.CurrentImage)
res.BaseImage = w.Image
return res, err
}
type ResetResult struct {
Err error
Id string
}
// Reset cleans up all containers in the history
func (w *Workspace) Reset() (results []ResetResult) {
history := w.history
for i, entry := range history {
if !history[i].Deleted {
err := RemoveContainer(w.docker, entry.Id)
results = append(results, ResetResult{Err: err, Id: entry.Id})
history[i].Deleted = true
}
}
w.history = history
w.CurrentImage = w.Image
return
}
func (w *Workspace) Sprint() ([]string, error) {
res := []string{"FROM " + w.Image}
for _, entry := range w.history {
if !entry.Deleted {
res = append(res, "RUN "+entry.Command)
}
}
return res, nil
}
// Write writes the output from Sprint to the provided file
// The file will be created, if necessary and overwrite the contents
// if it already exists
func (w *Workspace) Write(path string) error {
lines, err := w.Sprint()
if err != nil {
return err
}
var out []byte
for _, line := range lines {
out = append(out, []byte(line+"\n")...)
}
return ioutil.WriteFile(path, out, 0644)
}
func (w *Workspace) commit(id string) (string, error) {
imageId, err := CommitContainer(w.docker, id)
if err == nil {
w.CurrentImage = imageId
}
return imageId, err
}
func (w *Workspace) back(n int) error {
history := w.history
deleted := 0
if n > len(history) {
return errors.New("no history that far back")
}
for i := len(history) - 1; i > -1; i -= 1 {
if history[i].Deleted {
continue
}
RemoveContainer(w.docker, history[i].Id)
history[i].Deleted = true
deleted += 1
if deleted == n {
w.CurrentImage = history[i].Image
break
}
}
w.history = history
return nil
}