This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
forked from crosbymichael/skydock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.go
125 lines (107 loc) · 2.64 KB
/
plugins.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
package main
import (
"fmt"
"github.com/crosbymichael/log"
"github.com/crosbymichael/skydock/docker"
"github.com/crosbymichael/skydock/utils"
"github.com/robertkrimen/otto"
"github.com/skynetservices/skydns/msg"
"io/ioutil"
)
type pluginRuntime struct {
o *otto.Otto
}
func (r *pluginRuntime) createService(container *docker.Container) (*msg.Service, error) {
value, err := r.o.ToValue(*container)
if err != nil {
return nil, err
}
result, err := r.o.Call("createService", nil, value)
if err != nil {
return nil, err
}
if !result.IsObject() {
return nil, fmt.Errorf("createService plugin did not return a valid object")
}
var (
obj = result.Object()
service = &msg.Service{}
)
rawTTL, err := getInt(obj, "TTL")
if err != nil {
return nil, err
}
rawPort, err := getInt(obj, "Port")
if err != nil {
return nil, err
}
if service.Name, err = getString(obj, "Service"); err != nil {
return nil, err
}
if service.Version, err = getString(obj, "Instance"); err != nil {
return nil, err
}
if service.Host, err = getString(obj, "Host"); err != nil {
return nil, err
}
if service.Environment, err = getString(obj, "Environment"); err != nil {
return nil, err
}
service.TTL = uint32(rawTTL)
service.Port = uint16(rawPort)
// I'm glad that is over
return service, nil
}
func newRuntime(file string) (*pluginRuntime, error) {
runtime := otto.New()
log.Logf(log.INFO, "loading plugins from %s", file)
content, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
if _, err := runtime.Run(string(content)); err != nil {
return nil, err
}
if err := loadDefaults(runtime); err != nil {
return nil, err
}
return &pluginRuntime{runtime}, nil
}
func loadDefaults(runtime *otto.Otto) error {
if err := runtime.Set("defaultTTL", ttl); err != nil {
return err
}
if err := runtime.Set("defaultEnvironment", environment); err != nil {
return err
}
if err := runtime.Set("cleanImageName", func(call otto.FunctionCall) otto.Value {
name := call.Argument(0).String()
result, _ := otto.ToValue(utils.CleanImageName(name))
return result
}); err != nil {
return err
}
if err := runtime.Set("removeSlash", func(call otto.FunctionCall) otto.Value {
name := call.Argument(0).String()
result, _ := otto.ToValue(utils.RemoveSlash(name))
return result
}); err != nil {
return err
}
return nil
}
// util functions
func getString(obj *otto.Object, name string) (string, error) {
v, err := obj.Get(name)
if err != nil {
return "", err
}
return v.ToString()
}
func getInt(obj *otto.Object, name string) (int64, error) {
v, err := obj.Get(name)
if err != nil {
return -1, err
}
return v.ToInteger()
}