Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Add postgres example
Browse files Browse the repository at this point in the history
  • Loading branch information
tchaudhry91 committed Sep 4, 2019
1 parent 7263536 commit b7eb929
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 96 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ jobs:
with:
go-version: "1.12.9"
- run: curl -sL https://taskfile.dev/install.sh | sh
- run: go test -v ./...
- run: curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.17.1
- run: go fmt ./...
- run: ./bin/golangci-lint run
- run: go test -v -race ./...
- run: ./bin/task release
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ run:

linters-settings:
errcheck:
ignore: fmt:.*,github.com/go-kit/kit/log:.*,github.com/boltdb/bolt:.*
ignore: fmt:.*,github.com/go-kit/kit/log:.*,github.com/boltdb/bolt:.*,github.com/tchaudhry91/spinme/spin:^Slash.*
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ Admin users are as follows:
- Mysql = `root`
- Mongo = `mongoadmin`

The password wherever needed is set to `password`. Once the container is up, you may modify it as required. Future versions will support supplying root passwords at create time (PRs welcome!)
The password wherever needed is set to `password`. Once the container is up, you may modify it as required. The environment variables for the images to override this can also be set via the command line:
e.g `--env PG_PASSWORD=1231`


## Library
Expand Down
4 changes: 2 additions & 2 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ var statusCmd = &cobra.Command{
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("ID\tIP\tService\tPorts\n")
fmt.Printf("ID\tIP\tService\tEndpoints\n")
for _, o := range oo {
fmt.Printf("%s\t%s\t%s\t%v\n", o.ID, o.IP, o.Service, o.Ports)
fmt.Printf("%s\t%s\t%s\t%v\n", o.ID, o.IP, o.Service, o.Endpoints)
}
},
}
Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/tchaudhry91/spinme
go 1.12

require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.4.13 // indirect
github.com/boltdb/bolt v1.3.1
github.com/containerd/containerd v1.2.7 // indirect
Expand All @@ -11,18 +12,19 @@ require (
github.com/docker/docker v0.7.3-0.20190731001754-589f1dad8dad
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.4.0 // indirect
github.com/go-check/check v0.0.0-20180628173108-788fd7840127 // indirect
github.com/gogo/protobuf v1.2.1 // indirect
github.com/golang/protobuf v1.3.1 // indirect
github.com/gorilla/mux v1.7.3 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/lib/pq v1.2.0
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c // indirect
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
google.golang.org/grpc v1.22.1 // indirect
gotest.tools v2.2.0+incompatible // indirect
)
86 changes: 4 additions & 82 deletions go.sum

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions spin/databases_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package spin_test

import (
"context"
"database/sql"
"fmt"
"github.com/tchaudhry91/spinme/spin"
"strings"
"time"

_ "github.com/lib/pq"
)

func ExamplePostgres() {
out, err := spin.Postgres(context.Background(), nil)
if err != nil {
fmt.Println(err)
return
}
defer spin.SlashID(context.Background(), out.ID)
// Give postgres a couple of seconds to boot-up, sadly there is no "ready" check yet
time.Sleep(2 * time.Second)
var hostEp string
var ok bool
// Grab the host endpoint mapping for the container
if hostEp, ok = out.Endpoints["5432/tcp"]; !ok {
fmt.Println("Didn't find expected port mapping")
}
// pq nees an independent port, not the entire endpoint
ep := strings.Split(hostEp, ":")
connStr := fmt.Sprintf("user=postgres password=password dbname=testdb port=%s sslmode=disable", ep[len(ep)-1])
db, err := sql.Open("postgres", connStr)
if err != nil {
fmt.Println(err)
return
}
defer db.Close()
err = db.Ping()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Connected!")
//Output: Connected!
}
17 changes: 11 additions & 6 deletions spin/spin.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ type SpinConfig struct {

// SpinOut is an output structure containing values from the recently spun service
type SpinOut struct {
ID string
IP string
Service string
Ports nat.PortMap
Env []string
ID string
IP string
Service string
Endpoints map[string]string
Env []string
}

// Spinner is an interface to be implemented by service that need to be spun up
Expand Down Expand Up @@ -143,7 +143,12 @@ func Generic(ctx context.Context, c *SpinConfig) (SpinOut, error) {
}
out.ID = ccb.ID
out.IP = cInsp.NetworkSettings.IPAddress
out.Ports = cInsp.NetworkSettings.Ports
out.Endpoints = make(map[string]string)
for k, pm := range cInsp.NetworkSettings.Ports {
for _, pb := range pm {
out.Endpoints[fmt.Sprintf("%s/%s", k.Port(), k.Proto())] = fmt.Sprintf("%s:%s", pb.HostIP, pb.HostPort)
}
}
out.Env = cInsp.Config.Env
return out, nil
}
Expand Down

0 comments on commit b7eb929

Please sign in to comment.