Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request moby#6 from squaremo/net_ext_plus_plugins
Add in (rebased) plugin branch
- Loading branch information
Showing
11 changed files
with
322 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
# temporary Gunfile for plugins development on OS X. | ||
# curious to use it yourself? | ||
# https://github.com/gliderlabs/glidergun | ||
|
||
init() { | ||
cmd-export build | ||
cmd-export push | ||
cmd-export restore | ||
cmd-export backup | ||
cmd-export install | ||
} | ||
|
||
build() { | ||
make BINDDIR=. | ||
} | ||
|
||
install() { | ||
cp bundles/1.5.0-plugins/cross/darwin/amd64/docker-1.5.0-plugins \ | ||
~/Projects/.bin/docker | ||
} | ||
|
||
push() { | ||
cat bundles/1.5.0-plugins/binary/docker-1.5.0-plugins \ | ||
| boot2docker ssh "cat > docker" | ||
boot2docker ssh " | ||
chmod +x ./docker | ||
sudo mv ./docker /usr/local/bin/docker | ||
sudo /etc/init.d/docker restart | ||
" | ||
} | ||
|
||
backup() { | ||
boot2docker ssh "cp /usr/local/bin/docker /usr/local/bin/docker.backup" | ||
} | ||
|
||
restore() { | ||
boot2docker ssh "cp /usr/local/bin/docker.backup /usr/local/bin/docker" | ||
boot2docker ssh "sudo /etc/init.d/docker restart" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.5.0-dev | ||
1.5.0-plugins |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package plugins | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"net/http/httputil" | ||
"time" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/docker/docker/pkg/ioutils" | ||
) | ||
|
||
const pluginApiVersion = "v1" | ||
|
||
func connect(addr string) (*httputil.ClientConn, error) { | ||
c, err := net.DialTimeout("unix", addr, 30*time.Second) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return httputil.NewClientConn(c, nil), nil | ||
} | ||
|
||
func call(addr, method, path string, data interface{}) (io.ReadCloser, error) { | ||
client, err := connect(addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
reqBody, err := json.Marshal(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
log.Debugf("sending request for extension:\n%s", string(reqBody)) | ||
path = "/" + pluginApiVersion + "/" + path | ||
req, err := http.NewRequest(method, path, bytes.NewBuffer(reqBody)) | ||
if err != nil { | ||
client.Close() | ||
return nil, err | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
client.Close() | ||
return nil, err | ||
} | ||
|
||
// FIXME: this should be better defined | ||
if resp.StatusCode >= 400 { | ||
return nil, fmt.Errorf("got bad status: %s", resp.Status) | ||
} | ||
|
||
return ioutils.NewReadCloserWrapper(resp.Body, func() error { | ||
if err := resp.Body.Close(); err != nil { | ||
return err | ||
} | ||
return client.Close() | ||
}), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package plugins | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
) | ||
|
||
type Plugin struct { | ||
addr string | ||
kind string | ||
} | ||
|
||
type handshakeResp struct { | ||
InterestedIn []string | ||
Name string | ||
Author string | ||
Org string | ||
Website string | ||
} | ||
|
||
func (p *Plugin) Call(method, path string, data interface{}) (io.ReadCloser, error) { | ||
path = p.kind + "/" + path | ||
return call(p.addr, method, path, data) | ||
} | ||
|
||
func (p *Plugin) handshake() (*handshakeResp, error) { | ||
// Don't use the local `call` because this shouldn't be namespaced | ||
respBody, err := call(p.addr, "POST", "handshake", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer respBody.Close() | ||
|
||
var data handshakeResp | ||
return &data, json.NewDecoder(respBody).Decode(&data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package plugins | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// Temporary singleton | ||
var Repo = NewRepository() | ||
|
||
var ErrNotRegistered = errors.New("plugin type is not registered") | ||
|
||
type Repository struct { | ||
plugins map[string]Plugins | ||
} | ||
|
||
type Plugins []*Plugin | ||
|
||
func (repository *Repository) GetPlugins(kind string) (Plugins, error) { | ||
plugins, exists := repository.plugins[kind] | ||
// TODO: check whether 'kind' is a supportedPluginType | ||
if !exists { | ||
// If no plugins have been registered for this kind yet, that's | ||
// OK. Just set and return an empty list. | ||
repository.plugins[kind] = make([]*Plugin, 0) | ||
return repository.plugins[kind], nil | ||
} | ||
return plugins, nil | ||
} | ||
|
||
var supportedPluginTypes = map[string]struct{}{ | ||
"volume": {}, | ||
} | ||
|
||
func NewRepository() *Repository { | ||
return &Repository{ | ||
plugins: make(map[string]Plugins), | ||
} | ||
} | ||
|
||
func (repository *Repository) RegisterPlugin(addr string) error { | ||
plugin := &Plugin{addr: addr} | ||
resp, err := plugin.handshake() | ||
if err != nil { | ||
return fmt.Errorf("error in plugin handshake: %v", err) | ||
} | ||
|
||
for _, interest := range resp.InterestedIn { | ||
if _, exists := supportedPluginTypes[interest]; !exists { | ||
return fmt.Errorf("plugin type %s is not supported", interest) | ||
} | ||
|
||
if _, exists := repository.plugins[interest]; !exists { | ||
repository.plugins[interest] = []*Plugin{} | ||
} | ||
plugin.kind = interest | ||
repository.plugins[interest] = append(repository.plugins[interest], plugin) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.