Initial commit
This commit is contained in:
+191
@@ -0,0 +1,191 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
https://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
Copyright 2015 Docker, Inc.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
https://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
[](https://godoc.org/github.com/docker/go-connections)
|
||||
|
||||
# Introduction
|
||||
|
||||
go-connections provides common package to work with network connections.
|
||||
|
||||
## Usage
|
||||
|
||||
See the [docs in godoc](https://godoc.org/github.com/docker/go-connections) for examples and documentation.
|
||||
|
||||
## License
|
||||
|
||||
go-connections is licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for the full license text.
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package sockets
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var errClosed = errors.New("use of closed network connection")
|
||||
|
||||
// InmemSocket implements net.Listener using in-memory only connections.
|
||||
type InmemSocket struct {
|
||||
chConn chan net.Conn
|
||||
chClose chan struct{}
|
||||
addr string
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// dummyAddr is used to satisfy net.Addr for the in-mem socket
|
||||
// it is just stored as a string and returns the string for all calls
|
||||
type dummyAddr string
|
||||
|
||||
// NewInmemSocket creates an in-memory only net.Listener
|
||||
// The addr argument can be any string, but is used to satisfy the `Addr()` part
|
||||
// of the net.Listener interface
|
||||
func NewInmemSocket(addr string, bufSize int) *InmemSocket {
|
||||
return &InmemSocket{
|
||||
chConn: make(chan net.Conn, bufSize),
|
||||
chClose: make(chan struct{}),
|
||||
addr: addr,
|
||||
}
|
||||
}
|
||||
|
||||
// Addr returns the socket's addr string to satisfy net.Listener
|
||||
func (s *InmemSocket) Addr() net.Addr {
|
||||
return dummyAddr(s.addr)
|
||||
}
|
||||
|
||||
// Accept implements the Accept method in the Listener interface; it waits for the next call and returns a generic Conn.
|
||||
func (s *InmemSocket) Accept() (net.Conn, error) {
|
||||
select {
|
||||
case conn := <-s.chConn:
|
||||
return conn, nil
|
||||
case <-s.chClose:
|
||||
return nil, errClosed
|
||||
}
|
||||
}
|
||||
|
||||
// Close closes the listener. It will be unavailable for use once closed.
|
||||
func (s *InmemSocket) Close() error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
select {
|
||||
case <-s.chClose:
|
||||
default:
|
||||
close(s.chClose)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dial is used to establish a connection with the in-mem server
|
||||
func (s *InmemSocket) Dial(network, addr string) (net.Conn, error) {
|
||||
srvConn, clientConn := net.Pipe()
|
||||
select {
|
||||
case s.chConn <- srvConn:
|
||||
case <-s.chClose:
|
||||
return nil, errClosed
|
||||
}
|
||||
|
||||
return clientConn, nil
|
||||
}
|
||||
|
||||
// Network returns the addr string, satisfies net.Addr
|
||||
func (a dummyAddr) Network() string {
|
||||
return string(a)
|
||||
}
|
||||
|
||||
// String returns the string form
|
||||
func (a dummyAddr) String() string {
|
||||
return string(a)
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package sockets
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
|
||||
// GetProxyEnv allows access to the uppercase and the lowercase forms of
|
||||
// proxy-related variables. See the Go specification for details on these
|
||||
// variables. https://golang.org/pkg/net/http/
|
||||
func GetProxyEnv(key string) string {
|
||||
proxyValue := os.Getenv(strings.ToUpper(key))
|
||||
if proxyValue == "" {
|
||||
return os.Getenv(strings.ToLower(key))
|
||||
}
|
||||
return proxyValue
|
||||
}
|
||||
|
||||
// DialerFromEnvironment takes in a "direct" *net.Dialer and returns a
|
||||
// proxy.Dialer which will route the connections through the proxy using the
|
||||
// given dialer.
|
||||
func DialerFromEnvironment(direct *net.Dialer) (proxy.Dialer, error) {
|
||||
allProxy := GetProxyEnv("all_proxy")
|
||||
if len(allProxy) == 0 {
|
||||
return direct, nil
|
||||
}
|
||||
|
||||
proxyURL, err := url.Parse(allProxy)
|
||||
if err != nil {
|
||||
return direct, err
|
||||
}
|
||||
|
||||
proxyFromURL, err := proxy.FromURL(proxyURL, direct)
|
||||
if err != nil {
|
||||
return direct, err
|
||||
}
|
||||
|
||||
noProxy := GetProxyEnv("no_proxy")
|
||||
if len(noProxy) == 0 {
|
||||
return proxyFromURL, nil
|
||||
}
|
||||
|
||||
perHost := proxy.NewPerHost(proxyFromURL, direct)
|
||||
perHost.AddFromString(noProxy)
|
||||
|
||||
return perHost, nil
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
// Package sockets provides helper functions to create and configure Unix or TCP sockets.
|
||||
package sockets
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Why 32? See https://github.com/docker/docker/pull/8035.
|
||||
const defaultTimeout = 32 * time.Second
|
||||
|
||||
// ErrProtocolNotAvailable is returned when a given transport protocol is not provided by the operating system.
|
||||
var ErrProtocolNotAvailable = errors.New("protocol not available")
|
||||
|
||||
// ConfigureTransport configures the specified Transport according to the
|
||||
// specified proto and addr.
|
||||
// If the proto is unix (using a unix socket to communicate) or npipe the
|
||||
// compression is disabled.
|
||||
func ConfigureTransport(tr *http.Transport, proto, addr string) error {
|
||||
switch proto {
|
||||
case "unix":
|
||||
return configureUnixTransport(tr, proto, addr)
|
||||
case "npipe":
|
||||
return configureNpipeTransport(tr, proto, addr)
|
||||
default:
|
||||
tr.Proxy = http.ProxyFromEnvironment
|
||||
dialer, err := DialerFromEnvironment(&net.Dialer{
|
||||
Timeout: defaultTimeout,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tr.Dial = dialer.Dial
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
// +build !windows
|
||||
|
||||
package sockets
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
const maxUnixSocketPathSize = len(syscall.RawSockaddrUnix{}.Path)
|
||||
|
||||
func configureUnixTransport(tr *http.Transport, proto, addr string) error {
|
||||
if len(addr) > maxUnixSocketPathSize {
|
||||
return fmt.Errorf("Unix socket path %q is too long", addr)
|
||||
}
|
||||
// No need for compression in local communications.
|
||||
tr.DisableCompression = true
|
||||
tr.Dial = func(_, _ string) (net.Conn, error) {
|
||||
return net.DialTimeout(proto, addr, defaultTimeout)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func configureNpipeTransport(tr *http.Transport, proto, addr string) error {
|
||||
return ErrProtocolNotAvailable
|
||||
}
|
||||
|
||||
// DialPipe connects to a Windows named pipe.
|
||||
// This is not supported on other OSes.
|
||||
func DialPipe(_ string, _ time.Duration) (net.Conn, error) {
|
||||
return nil, syscall.EAFNOSUPPORT
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package sockets
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/Microsoft/go-winio"
|
||||
)
|
||||
|
||||
func configureUnixTransport(tr *http.Transport, proto, addr string) error {
|
||||
return ErrProtocolNotAvailable
|
||||
}
|
||||
|
||||
func configureNpipeTransport(tr *http.Transport, proto, addr string) error {
|
||||
// No need for compression in local communications.
|
||||
tr.DisableCompression = true
|
||||
tr.Dial = func(_, _ string) (net.Conn, error) {
|
||||
return DialPipe(addr, defaultTimeout)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DialPipe connects to a Windows named pipe.
|
||||
func DialPipe(addr string, timeout time.Duration) (net.Conn, error) {
|
||||
return winio.DialPipe(addr, &timeout)
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
// Package sockets provides helper functions to create and configure Unix or TCP sockets.
|
||||
package sockets
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net"
|
||||
)
|
||||
|
||||
// NewTCPSocket creates a TCP socket listener with the specified address and
|
||||
// the specified tls configuration. If TLSConfig is set, will encapsulate the
|
||||
// TCP listener inside a TLS one.
|
||||
func NewTCPSocket(addr string, tlsConfig *tls.Config) (net.Listener, error) {
|
||||
l, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if tlsConfig != nil {
|
||||
tlsConfig.NextProtos = []string{"http/1.1"}
|
||||
l = tls.NewListener(l, tlsConfig)
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
// +build !windows
|
||||
|
||||
package sockets
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// NewUnixSocket creates a unix socket with the specified path and group.
|
||||
func NewUnixSocket(path string, gid int) (net.Listener, error) {
|
||||
if err := syscall.Unlink(path); err != nil && !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
mask := syscall.Umask(0777)
|
||||
defer syscall.Umask(mask)
|
||||
|
||||
l, err := net.Listen("unix", path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := os.Chown(path, 0, gid); err != nil {
|
||||
l.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := os.Chmod(path, 0660); err != nil {
|
||||
l.Close()
|
||||
return nil, err
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
Docker
|
||||
Copyright 2012-2015 Docker, Inc.
|
||||
|
||||
This product includes software developed at Docker, Inc. (https://www.docker.com).
|
||||
|
||||
This product contains software (https://github.com/kr/pty) developed
|
||||
by Keith Rarick, licensed under the MIT License.
|
||||
|
||||
The following is courtesy of our legal counsel:
|
||||
|
||||
|
||||
Use and transfer of Docker may be subject to certain restrictions by the
|
||||
United States and other governments.
|
||||
It is your responsibility to ensure that your use and/or transfer does not
|
||||
violate applicable laws.
|
||||
|
||||
For more information, please see https://www.bis.doc.gov
|
||||
|
||||
See also https://www.apache.org/dev/crypto.html and/or seek legal counsel.
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
# go-plugins-helpers
|
||||
|
||||
A collection of helper packages to extend Docker Engine in Go
|
||||
|
||||
Plugin type | Documentation | Description
|
||||
--------------|---------------|--------------------------------------------------
|
||||
Authorization | [Link](https://docs.docker.com/engine/extend/authorization/) | Extend API authorization mechanism
|
||||
Network | [Link](https://docs.docker.com/engine/extend/plugins_network/) | Extend network management
|
||||
Volume | [Link](https://docs.docker.com/engine/extend/plugins_volume/) | Extend persistent storage
|
||||
IPAM | [Link](https://github.com/docker/libnetwork/blob/master/docs/ipam.md) | Extend IP address management
|
||||
Graph (experimental) | [Link](https://github.com/docker/docker/blob/master/experimental/plugins_graphdriver.md) | Extend image and container fs storage
|
||||
|
||||
See the [understand Docker plugins documentation section](https://docs.docker.com/engine/extend/plugins/).
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// DefaultContentTypeV1_1 is the default content type accepted and sent by the plugins.
|
||||
const DefaultContentTypeV1_1 = "application/vnd.docker.plugins.v1.1+json"
|
||||
|
||||
// DecodeRequest decodes an http request into a given structure.
|
||||
func DecodeRequest(w http.ResponseWriter, r *http.Request, req interface{}) (err error) {
|
||||
if err = json.NewDecoder(r.Body).Decode(req); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// EncodeResponse encodes the given structure into an http response.
|
||||
func EncodeResponse(w http.ResponseWriter, res interface{}, err bool) {
|
||||
w.Header().Set("Content-Type", DefaultContentTypeV1_1)
|
||||
if err {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
json.NewEncoder(w).Encode(res)
|
||||
}
|
||||
|
||||
// StreamResponse streams a response object to the client
|
||||
func StreamResponse(w http.ResponseWriter, data io.ReadCloser) {
|
||||
w.Header().Set("Content-Type", DefaultContentTypeV1_1)
|
||||
if _, err := copyBuf(w, data); err != nil {
|
||||
fmt.Printf("ERROR in stream: %v\n", err)
|
||||
}
|
||||
data.Close()
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
const activatePath = "/Plugin.Activate"
|
||||
|
||||
// Handler is the base to create plugin handlers.
|
||||
// It initializes connections and sockets to listen to.
|
||||
type Handler struct {
|
||||
mux *http.ServeMux
|
||||
}
|
||||
|
||||
// NewHandler creates a new Handler with an http mux.
|
||||
func NewHandler(manifest string) Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc(activatePath, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", DefaultContentTypeV1_1)
|
||||
fmt.Fprintln(w, manifest)
|
||||
})
|
||||
|
||||
return Handler{mux: mux}
|
||||
}
|
||||
|
||||
// Serve sets up the handler to serve requests on the passed in listener
|
||||
func (h Handler) Serve(l net.Listener) error {
|
||||
server := http.Server{
|
||||
Addr: l.Addr().String(),
|
||||
Handler: h.mux,
|
||||
}
|
||||
return server.Serve(l)
|
||||
}
|
||||
|
||||
// ServeTCP makes the handler to listen for request in a given TCP address.
|
||||
// It also writes the spec file in the right directory for docker to read.
|
||||
// Due to constrains for running Docker in Docker on Windows, data-root directory
|
||||
// of docker daemon must be provided. To get default directory, use
|
||||
// WindowsDefaultDaemonRootDir() function. On Unix, this parameter is ignored.
|
||||
func (h Handler) ServeTCP(pluginName, addr, daemonDir string, tlsConfig *tls.Config) error {
|
||||
l, spec, err := newTCPListener(addr, pluginName, daemonDir, tlsConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if spec != "" {
|
||||
defer os.Remove(spec)
|
||||
}
|
||||
return h.Serve(l)
|
||||
}
|
||||
|
||||
// ServeUnix makes the handler to listen for requests in a unix socket.
|
||||
// It also creates the socket file in the right directory for docker to read.
|
||||
func (h Handler) ServeUnix(addr string, gid int) error {
|
||||
l, spec, err := newUnixListener(addr, gid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if spec != "" {
|
||||
defer os.Remove(spec)
|
||||
}
|
||||
return h.Serve(l)
|
||||
}
|
||||
|
||||
// ServeWindows makes the handler to listen for request in a Windows named pipe.
|
||||
// It also creates the spec file in the right directory for docker to read.
|
||||
// Due to constrains for running Docker in Docker on Windows, data-root directory
|
||||
// of docker daemon must be provided. To get default directory, use
|
||||
// WindowsDefaultDaemonRootDir() function. On Unix, this parameter is ignored.
|
||||
func (h Handler) ServeWindows(addr, pluginName, daemonDir string, pipeConfig *WindowsPipeConfig) error {
|
||||
l, spec, err := newWindowsListener(addr, pluginName, daemonDir, pipeConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if spec != "" {
|
||||
defer os.Remove(spec)
|
||||
}
|
||||
return h.Serve(l)
|
||||
}
|
||||
|
||||
// HandleFunc registers a function to handle a request path with.
|
||||
func (h Handler) HandleFunc(path string, fn func(w http.ResponseWriter, r *http.Request)) {
|
||||
h.mux.HandleFunc(path, fn)
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const buffer32K = 32 * 1024
|
||||
|
||||
var buffer32KPool = &sync.Pool{New: func() interface{} { return make([]byte, buffer32K) }}
|
||||
|
||||
// copyBuf uses a shared buffer pool with io.CopyBuffer
|
||||
func copyBuf(w io.Writer, r io.Reader) (int64, error) {
|
||||
buf := buffer32KPool.Get().([]byte)
|
||||
written, err := io.CopyBuffer(w, r, buf)
|
||||
buffer32KPool.Put(buf)
|
||||
return written, err
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type protocol string
|
||||
|
||||
const (
|
||||
protoTCP protocol = "tcp"
|
||||
protoNamedPipe protocol = "npipe"
|
||||
)
|
||||
|
||||
// PluginSpecDir returns plugin spec dir in relation to daemon root directory.
|
||||
func PluginSpecDir(daemonRoot string) string {
|
||||
return ([]string{filepath.Join(daemonRoot, "plugins")})[0]
|
||||
}
|
||||
|
||||
// WindowsDefaultDaemonRootDir returns default data directory of docker daemon on Windows.
|
||||
func WindowsDefaultDaemonRootDir() string {
|
||||
return filepath.Join(os.Getenv("programdata"), "docker")
|
||||
}
|
||||
|
||||
func createPluginSpecDirWindows(name, address, daemonRoot string) (string, error) {
|
||||
_, err := os.Stat(daemonRoot)
|
||||
if os.IsNotExist(err) {
|
||||
return "", fmt.Errorf("Deamon root directory must already exist: %s", err)
|
||||
}
|
||||
|
||||
pluginSpecDir := PluginSpecDir(daemonRoot)
|
||||
|
||||
if err := windowsCreateDirectoryWithACL(pluginSpecDir); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return pluginSpecDir, nil
|
||||
}
|
||||
|
||||
func createPluginSpecDirUnix(name, address string) (string, error) {
|
||||
pluginSpecDir := PluginSpecDir("/etc/docker")
|
||||
if err := os.MkdirAll(pluginSpecDir, 0755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return pluginSpecDir, nil
|
||||
}
|
||||
|
||||
func writeSpecFile(name, address, pluginSpecDir string, proto protocol) (string, error) {
|
||||
specFileDir := filepath.Join(pluginSpecDir, name+".spec")
|
||||
|
||||
url := string(proto) + "://" + address
|
||||
if err := ioutil.WriteFile(specFileDir, []byte(url), 0644); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return specFileDir, nil
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"runtime"
|
||||
|
||||
"github.com/docker/go-connections/sockets"
|
||||
)
|
||||
|
||||
func newTCPListener(address, pluginName, daemonDir string, tlsConfig *tls.Config) (net.Listener, string, error) {
|
||||
listener, err := sockets.NewTCPSocket(address, tlsConfig)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
addr := listener.Addr().String()
|
||||
|
||||
var specDir string
|
||||
if runtime.GOOS == "windows" {
|
||||
specDir, err = createPluginSpecDirWindows(pluginName, addr, daemonDir)
|
||||
} else {
|
||||
specDir, err = createPluginSpecDirUnix(pluginName, addr)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
specFile, err := writeSpecFile(pluginName, addr, specDir, protoTCP)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return listener, specFile, nil
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
// +build linux freebsd
|
||||
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/docker/go-connections/sockets"
|
||||
)
|
||||
|
||||
const pluginSockDir = "/run/docker/plugins"
|
||||
|
||||
func newUnixListener(pluginName string, gid int) (net.Listener, string, error) {
|
||||
path, err := fullSocketAddress(pluginName)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
listener, err := sockets.NewUnixSocket(path, gid)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return listener, path, nil
|
||||
}
|
||||
|
||||
func fullSocketAddress(address string) (string, error) {
|
||||
if err := os.MkdirAll(pluginSockDir, 0755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if filepath.IsAbs(address) {
|
||||
return address, nil
|
||||
}
|
||||
return filepath.Join(pluginSockDir, address+".sock"), nil
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// +build linux freebsd
|
||||
// +build nosystemd
|
||||
|
||||
package sdk
|
||||
|
||||
import "net"
|
||||
|
||||
func setupSocketActivation() (net.Listener, error) {
|
||||
return nil, nil
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
// +build linux freebsd
|
||||
// +build !nosystemd
|
||||
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/coreos/go-systemd/activation"
|
||||
)
|
||||
|
||||
// isRunningSystemd checks whether the host was booted with systemd as its init
|
||||
// system. This functions similarly to systemd's `sd_booted(3)`: internally, it
|
||||
// checks whether /run/systemd/system/ exists and is a directory.
|
||||
// http://www.freedesktop.org/software/systemd/man/sd_booted.html
|
||||
//
|
||||
// Copied from github.com/coreos/go-systemd/util.IsRunningSystemd
|
||||
func isRunningSystemd() bool {
|
||||
fi, err := os.Lstat("/run/systemd/system")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return fi.IsDir()
|
||||
}
|
||||
|
||||
func setupSocketActivation() (net.Listener, error) {
|
||||
if !isRunningSystemd() {
|
||||
return nil, nil
|
||||
}
|
||||
listenFds := activation.Files(false)
|
||||
if len(listenFds) > 1 {
|
||||
return nil, fmt.Errorf("expected only one socket from systemd, got %d", len(listenFds))
|
||||
}
|
||||
var listener net.Listener
|
||||
if len(listenFds) == 1 {
|
||||
l, err := net.FileListener(listenFds[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
listener = l
|
||||
}
|
||||
return listener, nil
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// +build !linux,!freebsd
|
||||
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
)
|
||||
|
||||
var (
|
||||
errOnlySupportedOnLinuxAndFreeBSD = errors.New("unix socket creation is only supported on Linux and FreeBSD")
|
||||
)
|
||||
|
||||
func newUnixListener(pluginName string, gid int) (net.Listener, string, error) {
|
||||
return nil, "", errOnlySupportedOnLinuxAndFreeBSD
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
// +build windows
|
||||
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"github.com/Microsoft/go-winio"
|
||||
)
|
||||
|
||||
// Named pipes use Windows Security Descriptor Definition Language to define ACL. Following are
|
||||
// some useful definitions.
|
||||
const (
|
||||
// This will set permissions for everyone to have full access
|
||||
AllowEveryone = "S:(ML;;NW;;;LW)D:(A;;0x12019f;;;WD)"
|
||||
|
||||
// This will set permissions for Service, System, Adminstrator group and account to have full access
|
||||
AllowServiceSystemAdmin = "D:(A;ID;FA;;;SY)(A;ID;FA;;;BA)(A;ID;FA;;;LA)(A;ID;FA;;;LS)"
|
||||
)
|
||||
|
||||
func newWindowsListener(address, pluginName, daemonRoot string, pipeConfig *WindowsPipeConfig) (net.Listener, string, error) {
|
||||
winioPipeConfig := winio.PipeConfig{
|
||||
SecurityDescriptor: pipeConfig.SecurityDescriptor,
|
||||
InputBufferSize: pipeConfig.InBufferSize,
|
||||
OutputBufferSize: pipeConfig.OutBufferSize,
|
||||
}
|
||||
listener, err := winio.ListenPipe(address, &winioPipeConfig)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
addr := listener.Addr().String()
|
||||
|
||||
specDir, err := createPluginSpecDirWindows(pluginName, addr, daemonRoot)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
spec, err := writeSpecFile(pluginName, addr, specDir, protoNamedPipe)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return listener, spec, nil
|
||||
}
|
||||
|
||||
func windowsCreateDirectoryWithACL(name string) error {
|
||||
sa := syscall.SecurityAttributes{Length: 0}
|
||||
sddl := "D:P(A;OICI;GA;;;BA)(A;OICI;GA;;;SY)"
|
||||
sd, err := winio.SddlToSecurityDescriptor(sddl)
|
||||
if err != nil {
|
||||
return &os.PathError{Op: "mkdir", Path: name, Err: err}
|
||||
}
|
||||
sa.Length = uint32(unsafe.Sizeof(sa))
|
||||
sa.InheritHandle = 1
|
||||
sa.SecurityDescriptor = uintptr(unsafe.Pointer(&sd[0]))
|
||||
|
||||
namep, err := syscall.UTF16PtrFromString(name)
|
||||
if err != nil {
|
||||
return &os.PathError{Op: "mkdir", Path: name, Err: err}
|
||||
}
|
||||
|
||||
e := syscall.CreateDirectory(namep, &sa)
|
||||
if e != nil {
|
||||
return &os.PathError{Op: "mkdir", Path: name, Err: e}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// +build !windows
|
||||
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
)
|
||||
|
||||
var (
|
||||
errOnlySupportedOnWindows = errors.New("named pipe creation is only supported on Windows")
|
||||
)
|
||||
|
||||
func newWindowsListener(address, pluginName, daemonRoot string, pipeConfig *WindowsPipeConfig) (net.Listener, string, error) {
|
||||
return nil, "", errOnlySupportedOnWindows
|
||||
}
|
||||
|
||||
func windowsCreateDirectoryWithACL(name string) error {
|
||||
return nil
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package sdk
|
||||
|
||||
// WindowsPipeConfig is a helper structure for configuring named pipe parameters on Windows.
|
||||
type WindowsPipeConfig struct {
|
||||
// SecurityDescriptor contains a Windows security descriptor in SDDL format.
|
||||
SecurityDescriptor string
|
||||
|
||||
// InBufferSize in bytes.
|
||||
InBufferSize int32
|
||||
|
||||
// OutBufferSize in bytes.
|
||||
OutBufferSize int32
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
# Docker volume extension api.
|
||||
|
||||
Go handler to create external volume extensions for Docker.
|
||||
|
||||
## Usage
|
||||
|
||||
This library is designed to be integrated in your program.
|
||||
|
||||
1. Implement the `volume.Driver` interface.
|
||||
2. Initialize a `volume.Handler` with your implementation.
|
||||
3. Call either `ServeTCP` or `ServeUnix` from the `volume.Handler`.
|
||||
|
||||
### Example using TCP sockets:
|
||||
|
||||
```go
|
||||
d := MyVolumeDriver{}
|
||||
h := volume.NewHandler(d)
|
||||
h.ServeTCP("test_volume", ":8080")
|
||||
```
|
||||
|
||||
### Example using Unix sockets:
|
||||
|
||||
```go
|
||||
d := MyVolumeDriver{}
|
||||
h := volume.NewHandler(d)
|
||||
u, _ := user.Lookup("root")
|
||||
gid, _ := strconv.Atoi(u.Gid)
|
||||
h.ServeUnix("test_volume", gid)
|
||||
```
|
||||
|
||||
## Full example plugins
|
||||
|
||||
- https://github.com/calavera/docker-volume-glusterfs
|
||||
- https://github.com/calavera/docker-volume-keywhiz
|
||||
- https://github.com/quobyte/docker-volume
|
||||
+229
@@ -0,0 +1,229 @@
|
||||
package volume
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/docker/go-plugins-helpers/sdk"
|
||||
)
|
||||
|
||||
const (
|
||||
// DefaultDockerRootDirectory is the default directory where volumes will be created.
|
||||
DefaultDockerRootDirectory = "/var/lib/docker-volumes"
|
||||
|
||||
manifest = `{"Implements": ["VolumeDriver"]}`
|
||||
createPath = "/VolumeDriver.Create"
|
||||
getPath = "/VolumeDriver.Get"
|
||||
listPath = "/VolumeDriver.List"
|
||||
removePath = "/VolumeDriver.Remove"
|
||||
hostVirtualPath = "/VolumeDriver.Path"
|
||||
mountPath = "/VolumeDriver.Mount"
|
||||
unmountPath = "/VolumeDriver.Unmount"
|
||||
capabilitiesPath = "/VolumeDriver.Capabilities"
|
||||
)
|
||||
|
||||
// CreateRequest is the structure that docker's requests are deserialized to.
|
||||
type CreateRequest struct {
|
||||
Name string
|
||||
Options map[string]string `json:"Opts,omitempty"`
|
||||
}
|
||||
|
||||
// RemoveRequest structure for a volume remove request
|
||||
type RemoveRequest struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
// MountRequest structure for a volume mount request
|
||||
type MountRequest struct {
|
||||
Name string
|
||||
ID string
|
||||
}
|
||||
|
||||
// MountResponse structure for a volume mount response
|
||||
type MountResponse struct {
|
||||
Mountpoint string
|
||||
}
|
||||
|
||||
// UnmountRequest structure for a volume unmount request
|
||||
type UnmountRequest struct {
|
||||
Name string
|
||||
ID string
|
||||
}
|
||||
|
||||
// PathRequest structure for a volume path request
|
||||
type PathRequest struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
// PathResponse structure for a volume path response
|
||||
type PathResponse struct {
|
||||
Mountpoint string
|
||||
}
|
||||
|
||||
// GetRequest structure for a volume get request
|
||||
type GetRequest struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
// GetResponse structure for a volume get response
|
||||
type GetResponse struct {
|
||||
Volume *Volume
|
||||
}
|
||||
|
||||
// ListResponse structure for a volume list response
|
||||
type ListResponse struct {
|
||||
Volumes []*Volume
|
||||
}
|
||||
|
||||
// CapabilitiesResponse structure for a volume capability response
|
||||
type CapabilitiesResponse struct {
|
||||
Capabilities Capability
|
||||
}
|
||||
|
||||
// Volume represents a volume object for use with `Get` and `List` requests
|
||||
type Volume struct {
|
||||
Name string
|
||||
Mountpoint string
|
||||
Status map[string]interface{}
|
||||
}
|
||||
|
||||
// Capability represents the list of capabilities a volume driver can return
|
||||
type Capability struct {
|
||||
Scope string
|
||||
}
|
||||
|
||||
// ErrorResponse is a formatted error message that docker can understand
|
||||
type ErrorResponse struct {
|
||||
Err string
|
||||
}
|
||||
|
||||
// NewErrorResponse creates an ErrorResponse with the provided message
|
||||
func NewErrorResponse(msg string) *ErrorResponse {
|
||||
return &ErrorResponse{Err: msg}
|
||||
}
|
||||
|
||||
// Driver represent the interface a driver must fulfill.
|
||||
type Driver interface {
|
||||
Create(*CreateRequest) error
|
||||
List() (*ListResponse, error)
|
||||
Get(*GetRequest) (*GetResponse, error)
|
||||
Remove(*RemoveRequest) error
|
||||
Path(*PathRequest) (*PathResponse, error)
|
||||
Mount(*MountRequest) (*MountResponse, error)
|
||||
Unmount(*UnmountRequest) error
|
||||
Capabilities() *CapabilitiesResponse
|
||||
}
|
||||
|
||||
// Handler forwards requests and responses between the docker daemon and the plugin.
|
||||
type Handler struct {
|
||||
driver Driver
|
||||
sdk.Handler
|
||||
}
|
||||
|
||||
// NewHandler initializes the request handler with a driver implementation.
|
||||
func NewHandler(driver Driver) *Handler {
|
||||
h := &Handler{driver, sdk.NewHandler(manifest)}
|
||||
h.initMux()
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *Handler) initMux() {
|
||||
h.HandleFunc(createPath, func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("Entering go-plugins-helpers createPath")
|
||||
req := &CreateRequest{}
|
||||
err := sdk.DecodeRequest(w, r, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = h.driver.Create(req)
|
||||
if err != nil {
|
||||
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
|
||||
return
|
||||
}
|
||||
sdk.EncodeResponse(w, struct{}{}, false)
|
||||
})
|
||||
h.HandleFunc(removePath, func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("Entering go-plugins-helpers removePath")
|
||||
req := &RemoveRequest{}
|
||||
err := sdk.DecodeRequest(w, r, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = h.driver.Remove(req)
|
||||
if err != nil {
|
||||
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
|
||||
return
|
||||
}
|
||||
sdk.EncodeResponse(w, struct{}{}, false)
|
||||
})
|
||||
h.HandleFunc(mountPath, func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("Entering go-plugins-helpers mountPath")
|
||||
req := &MountRequest{}
|
||||
err := sdk.DecodeRequest(w, r, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res, err := h.driver.Mount(req)
|
||||
if err != nil {
|
||||
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
|
||||
return
|
||||
}
|
||||
sdk.EncodeResponse(w, res, false)
|
||||
})
|
||||
h.HandleFunc(hostVirtualPath, func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("Entering go-plugins-helpers hostVirtualPath")
|
||||
req := &PathRequest{}
|
||||
err := sdk.DecodeRequest(w, r, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res, err := h.driver.Path(req)
|
||||
if err != nil {
|
||||
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
|
||||
return
|
||||
}
|
||||
sdk.EncodeResponse(w, res, false)
|
||||
})
|
||||
h.HandleFunc(getPath, func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("Entering go-plugins-helpers getPath")
|
||||
req := &GetRequest{}
|
||||
err := sdk.DecodeRequest(w, r, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res, err := h.driver.Get(req)
|
||||
if err != nil {
|
||||
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
|
||||
return
|
||||
}
|
||||
sdk.EncodeResponse(w, res, false)
|
||||
})
|
||||
h.HandleFunc(unmountPath, func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("Entering go-plugins-helpers unmountPath")
|
||||
req := &UnmountRequest{}
|
||||
err := sdk.DecodeRequest(w, r, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = h.driver.Unmount(req)
|
||||
if err != nil {
|
||||
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
|
||||
return
|
||||
}
|
||||
sdk.EncodeResponse(w, struct{}{}, false)
|
||||
})
|
||||
h.HandleFunc(listPath, func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("Entering go-plugins-helpers listPath")
|
||||
res, err := h.driver.List()
|
||||
if err != nil {
|
||||
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
|
||||
return
|
||||
}
|
||||
sdk.EncodeResponse(w, res, false)
|
||||
})
|
||||
|
||||
h.HandleFunc(capabilitiesPath, func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("Entering go-plugins-helpers capabilitiesPath")
|
||||
sdk.EncodeResponse(w, h.driver.Capabilities(), false)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user