Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add docker resource #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions development/docker_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"golang.org/x/net/context"
)

func main() {
ctx := context.Background()
//cli, err := client.NewClientWithOpts(client.WithHost("unix:///var/run/docker.sock"))
cli, err := client.NewEnvClient()
if err != nil {
panic(err.Error())
}

fmt.Println(cli.DaemonHost())

createdContainer, err := cli.ContainerCreate(ctx, &container.Config{
Image: "alpine",
}, nil, nil, "testing_golang1")

if err != nil {
panic(err.Error())
}
fmt.Println(createdContainer.ID)

containers, _ := cli.ContainerList(ctx,
types.ContainerListOptions{
//All: true,
//Filters: filters.NewArgs(
// filters.KeyValuePair{
// Key: "names", Value: "frosty_heyrovsky",
// },
//),
})

fmt.Println("Looking up image")
for _, c := range containers {
fmt.Println("hey")
fmt.Println(c.Names)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/aelsabbahy/go-ps v0.0.0-20170721000941-443386855ca1
github.com/alecthomas/chroma v0.6.3
github.com/cheekybits/genny v0.0.0-20160824153601-e8e29e67948b
github.com/docker/docker v0.0.0-20161109014415-383a2f046b16
github.com/docker/docker v0.0.0-20170504205632-89658bed64c2
github.com/fatih/color v0.0.0-20161025120501-bf82308e8c85
github.com/golang/mock v1.2.0
github.com/miekg/dns v0.0.0-20161018060808-58f52c57ce9d
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ github.com/dlclark/regexp2 v1.1.6 h1:CqB4MjHw0MFCDj+PHHjiESmHX+N7t0tJzKvC6M97BRg
github.com/dlclark/regexp2 v1.1.6/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/docker/docker v0.0.0-20161109014415-383a2f046b16 h1:8J7CV9qtX4ygmYnM9uL5Ola9iI9QWzDt0rI3rwKSfSo=
github.com/docker/docker v0.0.0-20161109014415-383a2f046b16/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v0.0.0-20170504205632-89658bed64c2 h1:kHRF20b5JAKgm0QPXsIlq7y0YfkcwORvqo6489vlVfo=
github.com/docker/docker v0.0.0-20170504205632-89658bed64c2/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v1.13.1 h1:IkZjBSIc8hBjLpqeAbeE5mca5mNgeatLHBy3GO78BWo=
github.com/fatih/color v0.0.0-20161025120501-bf82308e8c85 h1:g7ijd5QIEMWwZNVp/T/6kQ8RSh8rN+YNhghMcrET3qY=
github.com/fatih/color v0.0.0-20161025120501-bf82308e8c85/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down
60 changes: 60 additions & 0 deletions internal/docker-client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package docker_client

import (
"context"
"fmt"
"github.com/docker/docker/api/types"
orig_client "github.com/docker/docker/client"
"github.com/fsouza/go-dockerclient"
)

// DockerClient holds the client which is connected to the docker daemon
type DockerClient struct {
client *orig_client.Client
}

// Container represents the container structure
type Container struct {
ID string
Running bool
Names []string
Image string
}

// NewDockerClientFromEnv creates a new client from your current environment
func NewDockerClientFromEnv() *DockerClient {
client, err := orig_client.NewEnvClient()
if err != nil {
panic(err.Error())
}

return &DockerClient{
client: client,
}
}

// List returns containers by given name
func (d *DockerClient) List(filters map[string][]string) ([]Container, error) {
opts := docker.ListContainersOptions{Filters: filters}

containers, err := d.client.ContainerList(context.Background(), types.ContainerListOptions{
Filters: parse.Flag(),
})
if err != nil {
return []Container{}, fmt.Errorf("Error occured: %s", err.Error())
}

var result []Container
for _, container := range containers {
result = append(result, containerFromAPIContainer(container))
}
return result, nil
}

func containerFromAPIContainer(containers docker.APIContainers) Container {
return Container{
ID: containers.ID,
Image: containers.Image,
Names: containers.Names,
}
}
47 changes: 47 additions & 0 deletions resource/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package resource

import (
"github.com/SimonBaeumer/goss/system"
"github.com/SimonBaeumer/goss/util"
)

// Docker represents the docker resource
type Docker struct {
Image string `json:"-" yaml:"-"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Count int `json:"count,omitempty" yaml:"count,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
}

// ID returns the id of the resource
func (d *Docker) ID() string {
return d.Image
}

// SetID sets the id of the resource
func (d *Docker) SetID(id string) {
d.Image = id
}

func (d *Docker) GetTitle() string {
return d.Title
}

func (d *Docker) GetMeta() meta {
return d.GetMeta()
}

// Validate validates the docker resource
func (d *Docker) Validate(sys *system.System) []TestResult {
skip := false
sysDocker := sys.NewDocker(d.Image, sys, util.Config{})

var results []TestResult
results = append(results, ValidateValue(d, "image", d.Image, sysDocker.Image, skip))
return results
}

// TODO: implement for add feature
func NewDocker(sysDocker system.Docker) (*Docker, error) {
return &Docker{}, nil
}
1 change: 1 addition & 0 deletions resource/docker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package resource
4 changes: 4 additions & 0 deletions resource/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/SimonBaeumer/goss/util"
)

// Group represents the group resource
type Group struct {
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Expand All @@ -21,6 +22,7 @@ func (g *Group) SetID(id string) { g.Groupname = id }
func (g *Group) GetTitle() string { return g.Title }
func (g *Group) GetMeta() meta { return g.Meta }

// Validate validates the group resource
func (g *Group) Validate(sys *system.System) []TestResult {
skip := false
sysgroup := sys.NewGroup(g.Groupname, sys, util.Config{})
Expand All @@ -37,6 +39,8 @@ func (g *Group) Validate(sys *system.System) []TestResult {
return results
}

// NewGroup will be used to get the group by the current setting
// Will be used for the add and auto-add features
func NewGroup(sysGroup system.Group, config util.Config) (*Group, error) {
groupname := sysGroup.Groupname()
exists, _ := sysGroup.Exists()
Expand Down
46 changes: 46 additions & 0 deletions system/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package system

import (
"fmt"
"github.com/SimonBaeumer/goss/internal/docker-client"
"github.com/SimonBaeumer/goss/util"
)

type Docker interface {
Image() string
Running() bool
Count() int
}

type DefDocker struct {
ImageName string
}

func createDockerClient() {
client := docker_client.NewDockerClientFromEnv()
containers, _ := client.List(map[string][]string{"name": {"confident_wilson"}})

fmt.Println("Looking up image")
for _, c := range containers {
fmt.Println("hey")
fmt.Println(c.Names)
}
}

func (d DefDocker) Image() string {
return d.ImageName
}

func (d DefDocker) Running() bool {
return true
}

func (d DefDocker) Count() int {
return 0
}

func NewDefDocker(name string, system *System, config util.Config) Docker {
return DefDocker{
ImageName: name,
}
}
7 changes: 7 additions & 0 deletions system/docker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package system

import "testing"

func Test_createDockerClient(t *testing.T) {
createDockerClient()
}
2 changes: 2 additions & 0 deletions system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type System struct {
NewMount func(string, *System, util2.Config) Mount
NewInterface func(string, *System, util2.Config) Interface
NewHTTP func(string, *System, util2.Config) HTTP
NewDocker func(string, *System, util2.Config) Docker
ports map[string][]GOnetstat.Process
portsOnce sync.Once
procMap map[string][]ps.Process
Expand Down Expand Up @@ -71,6 +72,7 @@ func New() *System {
NewMount: NewDefMount,
NewInterface: NewDefInterface,
NewHTTP: NewDefHTTP,
NewDocker: NewDefDocker,
}
sys.detectService()
return sys
Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/Microsoft/go-winio/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/Microsoft/go-winio/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/Microsoft/go-winio/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading