-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
120 lines (98 loc) · 2.72 KB
/
container.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
// Copyright 2014 Bowery, Inc.
package main
import (
"encoding/json"
"io"
"os"
"path/filepath"
"strings"
"github.com/Bowery/gopackages/schemas"
)
var (
storedContainerPath = filepath.Join(boweryDir, "agent_container.json")
containersDir = filepath.Join(boweryDir, "containers")
sshDir = filepath.Join(boweryDir, "ssh")
currentContainer *Container
)
// Container wraps a schemas container to provide methods on it.
type Container struct {
*schemas.Container
}
// NewContainer creates the paths for the given container.
func NewContainer(container *schemas.Container) (*Container, error) {
root := filepath.Join(containersDir, container.ID)
if err := os.MkdirAll(root, os.ModePerm|os.ModeDir); err != nil {
return nil, err
}
sshPath := filepath.Join(sshDir, container.ID)
if err := os.MkdirAll(root, os.ModePerm|os.ModeDir); err != nil {
return nil, err
}
container.RemotePath = root
container.SSHPath = sshPath
return &Container{Container: container}, nil
}
// LoadContainer reads the stored container info and creates it in memory.
func LoadContainer() (*Container, error) {
file, err := os.Open(storedContainerPath)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
defer file.Close()
var containers []*Container
decoder := json.NewDecoder(file)
err = decoder.Decode(&containers)
if err != nil {
return nil, err
}
container := containers[0] // Will always be available.
if container != nil {
container, err = NewContainer(container.Container)
}
return container, err
}
// Save saves the container info to the FS.
func (container *Container) Save() error {
// Use slice so loading can capture null, rather than the zero value.
containers := []*Container{container}
dat, err := json.MarshalIndent(containers, "", " ")
if err != nil {
return err
}
err = os.MkdirAll(boweryDir, os.ModePerm|os.ModeDir)
if err != nil {
return err
}
file, err := os.OpenFile(storedContainerPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, strings.NewReader(string(dat)))
return err
}
// DeleteContainer removes the Docker container for the container and it's
// image.
func (container *Container) DeleteDocker() error {
// Inspect to get the containers image.
dcontainer, err := DockerClient.Inspect(container.DockerID)
if err != nil {
return err
}
err = DockerClient.Remove(container.DockerID)
if err != nil {
return err
}
return DockerClient.RemoveImage(dcontainer.Image)
}
// Delete deletes the containers paths.
func (container *Container) DeletePaths() error {
err := os.RemoveAll(container.RemotePath)
if err != nil {
return err
}
return os.RemoveAll(container.SSHPath)
}