Skip to content
Merged
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
3 changes: 1 addition & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ func findConfig() string {
// Provide a default.
if runtime.GOOS == "linux" {
return strings.Join([]string{"etc", "pygmy", "config.yml"}, string(os.PathSeparator))
} else {
return strings.Join([]string{home, ".pygmy.yml"}, string(os.PathSeparator))
}
return strings.Join([]string{home, ".pygmy.yml"}, string(os.PathSeparator))
}

// initConfig reads in config file and ENV variables if set.
Expand Down
1 change: 1 addition & 0 deletions service/color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

var colorableOutput = colorable.NewColorableStdout()

// Print will print text to an interface using a colour via go-colourable.
func Print(input interface{}) {
fmt.Fprint(colorableOutput, input)
}
6 changes: 4 additions & 2 deletions service/dnsmasq/dnsmasq.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package dnsmasq

import (
"fmt"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"
model "github.com/fubarhouse/pygmy-go/service/interface"
)

// New will provide the standard object for the dnsmasq container.
func New() model.Service {
func New(c *model.Params) model.Service {
return model.Service{
Config: container.Config{
Image: "andyshinn/dnsmasq:2.83",
Cmd: []string{
"-A",
"/docker.amazee.io/127.0.0.1",
fmt.Sprintf("/%s/127.0.0.1", c.Domain),
},
Labels: map[string]string{
"pygmy.defaults": "true",
Expand Down
5 changes: 3 additions & 2 deletions service/dnsmasq/dnsmasq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import (

"github.com/docker/go-connections/nat"
"github.com/fubarhouse/pygmy-go/service/dnsmasq"
model "github.com/fubarhouse/pygmy-go/service/interface"
. "github.com/smartystreets/goconvey/convey"
)

func Example() {
dnsmasq.New()
dnsmasq.New(&model.Params{})
}

func Test(t *testing.T) {
Convey("DNSMasq: Field equality tests...", t, func() {
obj := dnsmasq.New()
obj := dnsmasq.New(&model.Params{Domain: "docker.amazee.io"})

So(obj.Config.Image, ShouldEqual, "andyshinn/dnsmasq:2.83")
So(fmt.Sprint(obj.Config.Cmd), ShouldEqual, fmt.Sprint([]string{"-A", "/docker.amazee.io/127.0.0.1"}))
Expand Down
9 changes: 7 additions & 2 deletions service/haproxy/haproxy.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package haproxy

import (
"fmt"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"
model "github.com/fubarhouse/pygmy-go/service/interface"
)

// New will provide the standard object for the haproxy container.
func New() model.Service {
func New(c *model.Params) model.Service {
return model.Service{
Config: container.Config{
Image: "amazeeio/haproxy",
Expand All @@ -17,9 +19,12 @@ func New() model.Service {
"pygmy.enable": "true",
"pygmy.name": "amazeeio-haproxy",
"pygmy.network": "amazeeio-network",
"pygmy.url": "http://docker.amazee.io/stats",
"pygmy.url": fmt.Sprintf("http://%s/stats", c.Domain),
"pygmy.weight": "14",
},
Env: []string{
fmt.Sprintf("AMAZEEIO_URL=%s", c.Domain),
},
},
HostConfig: container.HostConfig{
Binds: []string{"/var/run/docker.sock:/tmp/docker.sock"},
Expand Down
5 changes: 3 additions & 2 deletions service/haproxy/haproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import (

"github.com/docker/go-connections/nat"
"github.com/fubarhouse/pygmy-go/service/haproxy"
model "github.com/fubarhouse/pygmy-go/service/interface"
. "github.com/smartystreets/goconvey/convey"
)

func Example() {
haproxy.New()
haproxy.New(&model.Params{})
haproxy.NewDefaultPorts()
}

func Test(t *testing.T) {
Convey("HAProxy: Field equality tests...", t, func() {
obj := haproxy.New()
obj := haproxy.New(&model.Params{Domain: "docker.amazee.io"})
objPorts := haproxy.NewDefaultPorts()
So(obj.Config.Image, ShouldEqual, "amazeeio/haproxy")
So(obj.Config.Labels["pygmy.defaults"], ShouldEqual, "true")
Expand Down
32 changes: 21 additions & 11 deletions service/interface/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func DockerNetworkConnect(network string, containerName string) error {
return nil
}

// DockerNetworkConnect will check if a container is connected to a network.
// DockerNetworkConnected will check if a container is connected to a network.
func DockerNetworkConnected(network string, containerName string) (bool, error) {
// Reset network state:
c, _ := DockerContainerList()
Expand Down Expand Up @@ -417,22 +417,28 @@ func DockerExec(container string, command string) ([]byte, error) {
return []byte{}, err
}

if rst, err := cli.ContainerExecCreate(ctx, container, types.ExecConfig{
rst, err := cli.ContainerExecCreate(ctx, container, types.ExecConfig{
AttachStdout: true,
AttachStderr: true,
Cmd: strings.Split(command, " ")}); err != nil {
Cmd: strings.Split(command, " ")})

if err != nil {
return []byte{}, err
} else {
if response, err := cli.ContainerExecAttach(context.Background(), rst.ID, types.ExecStartCheck{}); err != nil {
return []byte{}, err
} else {
data, _ := ioutil.ReadAll(response.Reader)
defer response.Close()
return data, nil
}
}

response, err := cli.ContainerExecAttach(context.Background(), rst.ID, types.ExecStartCheck{})

if err != nil {
return []byte{}, err
}

data, _ := ioutil.ReadAll(response.Reader)
defer response.Close()
return data, nil

}

// DockerContainerCreate will create a container, but will not run it.
func DockerContainerCreate(ID string, config container.Config, hostconfig container.HostConfig, networkconfig network.NetworkingConfig) (container.ContainerCreateCreatedBody, error) {
ctx := context.Background()
cli, err := client.NewEnvClient()
Expand All @@ -447,6 +453,7 @@ func DockerContainerCreate(ID string, config container.Config, hostconfig contai
return resp, err
}

// DockerContainerStart will run an existing container.
func DockerContainerStart(ID string, options types.ContainerStartOptions) error {
ctx := context.Background()
cli, err := client.NewEnvClient()
Expand All @@ -460,6 +467,9 @@ func DockerContainerStart(ID string, options types.ContainerStartOptions) error
return err
}

// DockerContainerLogs will synchronously (blocking, non-concurrently) print
// logs to stdout and stderr, useful for quick containers with a small amount
// of output which are expected to exit quickly.
func DockerContainerLogs(ID string) ([]byte, error) {
ctx := context.Background()
cli, err := client.NewEnvClient()
Expand Down
2 changes: 1 addition & 1 deletion service/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (Service *Service) Stop() error {
return nil
}

// Stop will stop the container.
// Remove will stop the container.
func (Service *Service) Remove() error {

discrete, _ := Service.GetFieldBool("discrete")
Expand Down
8 changes: 8 additions & 0 deletions service/interface/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import (
"github.com/docker/docker/api/types/network"
)

// Params is an arbitrary struct to pass around configuration from the top
// level to the lowest level - such as variable input to one of the
// containers.
type Params struct {
// Domain is the target domain for Pygmy to use.
Domain string
}

// DockerService is the requirements for a Docker container to be compatible.
// The Service struct is used to implement this interface, and individual
// variables of type Service can/have overwritten them when logic deems
Expand Down
3 changes: 3 additions & 0 deletions service/library/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type Config struct {
// Keys are the paths to the Keys which should be added.
Keys []string `yaml:"Keys"`

// Domain is the default domain suffix to use.
Domain string `yaml:"domain"`

// Services is a []model.Service for an index of all Services.
Services map[string]model.Service `yaml:"services"`

Expand Down
15 changes: 9 additions & 6 deletions service/library/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func Setup(c *Config) {
// Set default value for default inheritance:
viper.SetDefault("defaults", true)

// Set the default domain.
viper.SetDefault("domain", "docker.amazee.io")

// Resolvers don't have hard defaults defined which
// are mergable. So we set them in viper before
// unmarshalling the config so that config specified
Expand All @@ -84,15 +87,15 @@ func Setup(c *Config) {
var ResolvMacOS = resolv.Resolv{
Data: "# Generated by amazeeio pygmy\nnameserver 127.0.0.1\nport 6053\n",
Enabled: true,
File: "docker.amazee.io",
File: c.Domain,
Folder: "/etc/resolver",
Name: "MacOS Resolver",
}

var ResolvLinux = resolv.Resolv{
Data: "# Generated by amazeeio pygmy\n[Resolve]\nDNS=127.0.0.1:6053\nDomains=docker.amazee.io\n",
Data: fmt.Sprintf("# Generated by amazeeio pygmy\n[Resolve]\nDNS=127.0.0.1:6053\nDomains=%s\n", c.Domain),
Enabled: true,
File: "docker.amazee.io.conf",
File: fmt.Sprintf("%s.conf", c.Domain),
Folder: "/usr/lib/systemd/resolved.conf.d",
Name: "Linux Resolver",
}
Expand Down Expand Up @@ -127,9 +130,9 @@ func Setup(c *Config) {

ImportDefaults(c, "amazeeio-ssh-agent", agent.New())
ImportDefaults(c, "amazeeio-ssh-agent-add-key", key.NewAdder())
ImportDefaults(c, "amazeeio-dnsmasq", dnsmasq.New())
ImportDefaults(c, "amazeeio-haproxy", haproxy.New())
ImportDefaults(c, "amazeeio-mailhog", mailhog.New())
ImportDefaults(c, "amazeeio-dnsmasq", dnsmasq.New(&model.Params{Domain: c.Domain}))
ImportDefaults(c, "amazeeio-haproxy", haproxy.New(&model.Params{Domain: c.Domain}))
ImportDefaults(c, "amazeeio-mailhog", mailhog.New(&model.Params{Domain: c.Domain}))

// We need Port 80 to be configured by default.
// If a port on amazeeio-haproxy isn't explicitly declared,
Expand Down
5 changes: 3 additions & 2 deletions service/library/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/fubarhouse/pygmy-go/service/color"
"github.com/fubarhouse/pygmy-go/service/endpoint"
model "github.com/fubarhouse/pygmy-go/service/interface"
"github.com/fubarhouse/pygmy-go/service/interface/docker"
"github.com/fubarhouse/pygmy-go/service/resolv"
. "github.com/logrusorgru/aurora"
Expand Down Expand Up @@ -76,10 +77,10 @@ func Status(c Config) {

for _, resolver := range c.Resolvers {
r := resolv.Resolv{Name: resolver.Name, Data: resolver.Data, Folder: resolver.Folder, File: resolver.File}
if s := r.Status(); s {
if s := r.Status(&model.Params{Domain: c.Domain}); s {
color.Print(Green(fmt.Sprintf("[*] Resolv %v is properly connected\n", resolver.Name)))
} else {
color.Print(Red(fmt.Sprintf("[ ] Resolv %v is not properly conected\n", resolver.Name)))
color.Print(Red(fmt.Sprintf("[ ] Resolv %v is not properly connected\n", resolver.Name)))
}
}

Expand Down
5 changes: 3 additions & 2 deletions service/library/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/fubarhouse/pygmy-go/service/color"
"github.com/fubarhouse/pygmy-go/service/endpoint"
model "github.com/fubarhouse/pygmy-go/service/interface"
"github.com/fubarhouse/pygmy-go/service/interface/docker"
. "github.com/logrusorgru/aurora"
)
Expand Down Expand Up @@ -121,8 +122,8 @@ func Up(c Config) {
}

for _, resolver := range c.Resolvers {
if !resolver.Status() {
resolver.Configure()
if !resolver.Status(&model.Params{Domain: c.Domain}) {
resolver.Configure(&model.Params{Domain: c.Domain})
}
}

Expand Down
2 changes: 2 additions & 0 deletions service/library/version_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package library

import "fmt"

// PYGMY_VERSION is the equivalent to the version pygmy is being associated to.
// This variable is exclusively used when packaging a formal release.
var PYGMY_VERSION = ""

func printversion() bool {
Expand Down
8 changes: 5 additions & 3 deletions service/mailhog/mailhog.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package mailhog

import (
"fmt"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"
model "github.com/fubarhouse/pygmy-go/service/interface"
)

// New will provide the standard object for the mailhog container.
func New() model.Service {
func New(c *model.Params) model.Service {
return model.Service{
Config: container.Config{
User: "0",
Expand All @@ -21,15 +23,15 @@ func New() model.Service {
"MH_UI_BIND_ADDR=0.0.0.0:80",
"MH_API_BIND_ADDR=0.0.0.0:80",
"AMAZEEIO=AMAZEEIO",
"AMAZEEIO_URL=mailhog.docker.amazee.io",
fmt.Sprintf("AMAZEEIO_URL=mailhog.%s", c.Domain),
},
Image: "mailhog/mailhog",
Labels: map[string]string{
"pygmy.defaults": "true",
"pygmy.enable": "true",
"pygmy.name": "amazeeio-mailhog",
"pygmy.network": "amazeeio-network",
"pygmy.url": "http://mailhog.docker.amazee.io",
"pygmy.url": fmt.Sprintf("http://mailhog.%s", c.Domain),
"pygmy.weight": "15",
},
},
Expand Down
5 changes: 3 additions & 2 deletions service/mailhog/mailhog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import (
"testing"

"github.com/docker/go-connections/nat"
model "github.com/fubarhouse/pygmy-go/service/interface"
"github.com/fubarhouse/pygmy-go/service/mailhog"
. "github.com/smartystreets/goconvey/convey"
)

func Example() {
mailhog.New()
mailhog.New(&model.Params{})
mailhog.NewDefaultPorts()
}

func Test(t *testing.T) {
Convey("MailHog: Field equality tests...", t, func() {
obj := mailhog.New()
obj := mailhog.New(&model.Params{Domain: "docker.amazee.io"})
objPorts := mailhog.NewDefaultPorts()
So(obj.Config.User, ShouldEqual, "0")
So(obj.Config.Image, ShouldEqual, "mailhog/mailhog")
Expand Down
Loading