forked from docker-archive/deploykit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
187 lines (164 loc) · 4.65 KB
/
plugin.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
package file
import (
"encoding/json"
"fmt"
"path/filepath"
"time"
log "github.com/Sirupsen/logrus"
"github.com/docker/infrakit/pkg/spi"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/docker/infrakit/pkg/types"
"github.com/spf13/afero"
"math/rand"
)
// This example uses local files as a representation of an instance. When we
// create an instance, we write a file in a directory. The content of the file is simply
// the message in the provision spec, so we can verify correctness of the content easily.
// When we destroy an instance, we remove the file.
// DescribeInstances simply would list the files with the matching
// tags.
// Spec is just whatever that can be unmarshalled into a generic JSON map
type Spec map[string]interface{}
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
// fileInstance represents a single file instance on disk.
type fileInstance struct {
instance.Description
Spec instance.Spec
}
type plugin struct {
Dir string
fs afero.Fs
}
// NewPlugin returns an instance plugin backed by disk files.
func NewPlugin(dir string) instance.Plugin {
log.Debugln("file instance plugin. dir=", dir)
return &plugin{
Dir: dir,
fs: afero.NewOsFs(),
}
}
// Info returns a vendor specific name and version
func (p *plugin) VendorInfo() *spi.VendorInfo {
return &spi.VendorInfo{
InterfaceSpec: spi.InterfaceSpec{
Name: "infrakit-instance-file",
Version: "0.3.0",
},
URL: "https://github.com/docker/infrakit",
}
}
// ExampleProperties returns the properties / config of this plugin
func (p *plugin) ExampleProperties() *types.Any {
any, err := types.AnyValue(Spec{
"exampleString": "a_string",
"exampleBool": true,
"exampleInt": 1,
})
if err != nil {
return nil
}
return any
}
// Validate performs local validation on a provision request.
func (p *plugin) Validate(req *types.Any) error {
log.Debugln("validate", req.String())
spec := Spec{}
if err := req.Decode(&spec); err != nil {
return err
}
log.Debugln("Validated:", spec)
return nil
}
// Provision creates a new instance based on the spec.
func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
// simply writes a file
// use timestamp as instance id
id := instance.ID(fmt.Sprintf("instance-%d", rand.Int63()))
buff, err := json.MarshalIndent(fileInstance{
Description: instance.Description{
Tags: spec.Tags,
ID: id,
LogicalID: spec.LogicalID,
},
Spec: spec,
}, "", "")
log.Debugln("provision", id, "data=", string(buff), "err=", err)
if err != nil {
return nil, err
}
return &id, afero.WriteFile(p.fs, filepath.Join(p.Dir, string(id)), buff, 0644)
}
// Label labels the instance
func (p *plugin) Label(instance instance.ID, labels map[string]string) error {
fp := filepath.Join(p.Dir, string(instance))
buff, err := afero.ReadFile(p.fs, fp)
if err != nil {
return err
}
instanceData := fileInstance{}
err = json.Unmarshal(buff, &instanceData)
if err != nil {
return err
}
if instanceData.Description.Tags == nil {
instanceData.Description.Tags = map[string]string{}
}
for k, v := range labels {
instanceData.Description.Tags[k] = v
}
buff, err = json.MarshalIndent(instanceData, "", "")
log.Debugln("label:", instance, "data=", string(buff), "err=", err)
if err != nil {
return err
}
return afero.WriteFile(p.fs, fp, buff, 0644)
}
// Destroy terminates an existing instance.
func (p *plugin) Destroy(instance instance.ID, context instance.Context) error {
fp := filepath.Join(p.Dir, string(instance))
log.Debugln("destroy", fp)
return p.fs.Remove(fp)
}
// DescribeInstances returns descriptions of all instances matching all of the provided tags.
// TODO - need to define the fitlering of tags => AND or OR of matches?
func (p *plugin) DescribeInstances(tags map[string]string, properties bool) ([]instance.Description, error) {
log.Debugln("describe-instances", tags)
entries, err := afero.ReadDir(p.fs, p.Dir)
if err != nil {
return nil, err
}
result := []instance.Description{}
scan:
for _, entry := range entries {
fp := filepath.Join(p.Dir, entry.Name())
file, err := p.fs.Open(fp)
if err != nil {
log.Warningln("error opening", fp)
continue scan
}
inst := fileInstance{}
err = json.NewDecoder(file).Decode(&inst)
if err != nil {
log.Warning("cannot decode", entry.Name())
continue scan
}
if properties {
if blob, err := types.AnyValue(inst.Spec); err == nil {
inst.Properties = blob
}
}
if len(tags) == 0 {
result = append(result, inst.Description)
} else {
for k, v := range tags {
if inst.Tags[k] != v {
continue scan // we implement AND
}
}
result = append(result, inst.Description)
}
}
return result, nil
}