-
Notifications
You must be signed in to change notification settings - Fork 26
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 #75 from ivanilves/debloat-local
Debloat local
- Loading branch information
Showing
10 changed files
with
285 additions
and
221 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
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,46 @@ | ||
package version | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/tv42/httpunix" | ||
) | ||
|
||
// Detect detects Docker API version through the passed Docker socket | ||
func Detect(dockerSocket string) (string, error) { | ||
hc := http.Client{Transport: getTransport(dockerSocket)} | ||
|
||
resp, err := hc.Get("http+unix://docker/version") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return parseJSON(resp.Body) | ||
} | ||
|
||
func getTransport(dockerSocket string) *httpunix.Transport { | ||
t := &httpunix.Transport{ | ||
DialTimeout: 200 * time.Millisecond, | ||
RequestTimeout: 2 * time.Second, | ||
ResponseHeaderTimeout: 2 * time.Second, | ||
} | ||
t.RegisterLocation("docker", dockerSocket) | ||
|
||
return t | ||
} | ||
|
||
func parseJSON(data io.ReadCloser) (string, error) { | ||
v := struct { | ||
APIVersion string `json:"ApiVersion"` | ||
}{} | ||
|
||
err := json.NewDecoder(data).Decode(&v) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return v.APIVersion, 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,28 @@ | ||
package version | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
const dockerSocket = "/var/run/docker.sock" | ||
const invalidSocket = "/var/run/somethinginvalid.sock" | ||
|
||
func TestDetect(t *testing.T) { | ||
_, err := Detect(dockerSocket) | ||
if err != nil { | ||
t.Fatalf( | ||
"Unable to detect Docker API version: %s", | ||
err.Error(), | ||
) | ||
} | ||
} | ||
|
||
func TestWithInvalidSocket(t *testing.T) { | ||
_, err := Detect(invalidSocket) | ||
if err == nil { | ||
t.Fatalf( | ||
"Unable to detect Docker API version: %s", | ||
err.Error(), | ||
) | ||
} | ||
} |
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,108 @@ | ||
package client | ||
|
||
import ( | ||
"io/ioutil" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/filters" | ||
"github.com/moby/moby/client" | ||
|
||
"golang.org/x/net/context" | ||
|
||
"github.com/ivanilves/lstags/docker" | ||
"github.com/ivanilves/lstags/docker/client/api/version" | ||
"github.com/ivanilves/lstags/docker/config" | ||
) | ||
|
||
// DockerSocket is a socket we use to connect to the Docker daemon | ||
var DockerSocket = "/var/run/docker.sock" | ||
|
||
// DockerClient is a raw Docker client convenience wrapper | ||
type DockerClient struct { | ||
cli *client.Client | ||
cnf *config.Config | ||
} | ||
|
||
// New creates new instance of DockerClient (our Docker client wrapper) | ||
func New(cnf *config.Config) (*DockerClient, error) { | ||
apiVersion, err := version.Detect(DockerSocket) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cli, err := client.NewClient("unix://"+DockerSocket, apiVersion, nil, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &DockerClient{cli: cli, cnf: cnf}, nil | ||
} | ||
|
||
// ListImagesForRepo lists images present locally for the repo specified | ||
func (dc *DockerClient) ListImagesForRepo(repo string) ([]types.ImageSummary, error) { | ||
listOptions, err := buildImageListOptions(repo) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return dc.cli.ImageList(context.Background(), listOptions) | ||
} | ||
|
||
func buildImageListOptions(repo string) (types.ImageListOptions, error) { | ||
repoFilter := "reference=" + repo | ||
filterArgs := filters.NewArgs() | ||
|
||
filterArgs, err := filters.ParseFlag(repoFilter, filterArgs) | ||
if err != nil { | ||
return types.ImageListOptions{}, err | ||
} | ||
|
||
return types.ImageListOptions{Filters: filterArgs}, nil | ||
} | ||
|
||
// Pull pulls Docker image specified | ||
func (dc *DockerClient) Pull(ref string) error { | ||
registryAuth, _ := dc.cnf.GetRegistryAuth( | ||
docker.GetRegistry(ref), | ||
) | ||
|
||
pullOptions := types.ImagePullOptions{RegistryAuth: registryAuth} | ||
if registryAuth == "" { | ||
pullOptions = types.ImagePullOptions{} | ||
} | ||
|
||
resp, err := dc.cli.ImagePull(context.Background(), ref, pullOptions) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = ioutil.ReadAll(resp) | ||
|
||
return err | ||
} | ||
|
||
// Push pushes Docker image specified | ||
func (dc *DockerClient) Push(ref string) error { | ||
registryAuth, _ := dc.cnf.GetRegistryAuth( | ||
docker.GetRegistry(ref), | ||
) | ||
|
||
pushOptions := types.ImagePushOptions{RegistryAuth: registryAuth} | ||
if registryAuth == "" { | ||
pushOptions = types.ImagePushOptions{} | ||
} | ||
|
||
resp, err := dc.cli.ImagePush(context.Background(), ref, pushOptions) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = ioutil.ReadAll(resp) | ||
|
||
return err | ||
} | ||
|
||
// Tag puts a "dst" tag on "src" Docker image | ||
func (dc *DockerClient) Tag(src, dst string) error { | ||
return dc.cli.ImageTag(context.Background(), src, dst) | ||
} |
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 docker | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// DefaultRegistry is a registry we use if none could be resolved from image ref | ||
var DefaultRegistry = "registry.hub.docker.com" | ||
|
||
// GetRegistry tries to get Docker registry name from a repository or reference | ||
// .. if it is not possible it returns default registry name (usually Docker Hub) | ||
func GetRegistry(repoOrRef string) string { | ||
r := strings.Split(repoOrRef, "/")[0] | ||
|
||
if isHostname(r) { | ||
return r | ||
} | ||
|
||
return DefaultRegistry | ||
} | ||
|
||
func isHostname(s string) bool { | ||
if strings.Contains(s, ".") { | ||
return true | ||
} | ||
|
||
if strings.Contains(s, ":") { | ||
return true | ||
} | ||
|
||
if s == "localhost" { | ||
return true | ||
} | ||
|
||
return false | ||
} |
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,32 @@ | ||
package docker | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGetRegistry(t *testing.T) { | ||
examples := map[string]string{ | ||
"mesosphere/marathon": DefaultRegistry, | ||
"bogohost/my/inner/troll": DefaultRegistry, | ||
"bogohost/my/inner/troll:1.0.1": DefaultRegistry, | ||
"registry.hipsta.io/hype/hotshit": "registry.hipsta.io", | ||
"localhost/my/image": "localhost", | ||
"localhost/my/image:latest": "localhost", | ||
"bogohost:5000/mymymy/img": "bogohost:5000", | ||
"bogohost:5000/mymymy/img:0.0.1": "bogohost:5000", | ||
"bogohost:5000/mymymy/img:edge": "bogohost:5000", | ||
} | ||
|
||
for repoOrRef, expectedRegistry := range examples { | ||
registry := GetRegistry(repoOrRef) | ||
|
||
if registry != expectedRegistry { | ||
t.Fatalf( | ||
"Got unexpected Docker registry name '%s' from repo/ref '%s' (expected: '%s')", | ||
registry, | ||
repoOrRef, | ||
expectedRegistry, | ||
) | ||
} | ||
} | ||
} |
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.