Skip to content

Commit

Permalink
Start neo-local-faucet with CLI (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
revett committed Oct 28, 2018
1 parent 18f14f4 commit 981d611
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 59 deletions.
61 changes: 33 additions & 28 deletions cli/commands/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/CityOfZion/neo-local/cli/services"
"github.com/CityOfZion/neo-local/cli/stack"
"github.com/fatih/color"
"github.com/urfave/cli"

"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -38,21 +37,21 @@ func (s Start) ToCommand() cli.Command {

func (s Start) action() func(c *cli.Context) error {
return func(c *cli.Context) error {
verbose := c.Bool("v")
if verbose {
log.Println("Verbose logging is enabled")
}

saveState := c.Bool("ss")
if saveState {
log.Println("Save state is enabled, existing environment will not be destroyed")
} else {
log.Printf(
"Save state is %s, existing environment will be %s",
color.RedString("disabled"),
color.RedString("destroyed"),
)
}
// verbose := c.Bool("v")
// if verbose {
// log.Println("Verbose logging is enabled")
// }

// saveState := c.Bool("ss")
// if saveState {
// log.Println("Save state is enabled, existing environment will not be destroyed")
// } else {
// log.Printf(
// "Save state is %s, existing environment will be %s",
// color.RedString("disabled"),
// color.RedString("destroyed"),
// )
// }

ctx := context.Background()
cli, err := client.NewEnvClient()
Expand All @@ -70,11 +69,13 @@ func (s Start) action() func(c *cli.Context) error {
return err
}

services := stack.Services()

for _, service := range services {
for _, service := range stack.Services() {
resp, err := cli.ContainerCreate(
ctx, service.Config(), nil, nil, service.ContainerName(),
ctx,
service.Config(),
service.HostConfig,
nil,
service.ContainerName(),
)
if err != nil {
return err
Expand All @@ -94,13 +95,17 @@ func (s Start) action() func(c *cli.Context) error {

func (s Start) flags() []cli.Flag {
return []cli.Flag{
cli.BoolFlag{
Name: "save-state, ss",
Usage: "Any state in the existing environment will be saved (default: false)",
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "Enable verbose logging (default: false)",
},
// cli.BoolFlag{
// Name: "pull-images, pi",
// Usage: "Pull the latest Docker images (default: true)",
// },
// cli.BoolFlag{
// Name: "save-state, ss",
// Usage: "Any state in the existing environment will be saved (default: false)",
// },
// cli.BoolFlag{
// Name: "verbose, v",
// Usage: "Enable verbose logging (default: false)",
// },
}
}
28 changes: 12 additions & 16 deletions cli/services/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/CityOfZion/neo-local/cli/logger"
"github.com/CityOfZion/neo-local/cli/stack"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"golang.org/x/net/context"
Expand All @@ -25,30 +26,25 @@ func CheckDockerRunning(ctx context.Context, cli *client.Client) bool {
func PullDockerImages(ctx context.Context, cli *client.Client) error {
log.Println("Pulling Docker images")

for _, imageName := range dockerImageNames() {
prefix := fmt.Sprintf("↪ %s", imageName)
for _, service := range stack.Services() {
prefix := fmt.Sprintf("↪ %s", service.ImageName())
s := logger.NewSpinner(prefix)
s.Start()

reader, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{})
reader, err := cli.ImagePull(
ctx, service.ImageName(), types.ImagePullOptions{},
)
if err != nil {
return fmt.Errorf("Error when pulling Docker image '%s': %s", imageName, err)
return fmt.Errorf(
"Error when pulling Docker image '%s': %s",
service.ImageName(),
err,
)
}

defer reader.Close()
s.Stop()
}

return nil
}

func dockerImageNames() []string {
return []string{
"cityofzion/neo-local-faucet:latest",
"cityofzion/neo-privatenet:2.7.6",
"cityofzion/neo-python:v0.8.1",
"library/postgres:10.5",
"registry.gitlab.com/cityofzion/neo-scan/api:latest",
"registry.gitlab.com/cityofzion/neo-scan/sync:latest",
}
}
}
36 changes: 36 additions & 0 deletions cli/stack/faucet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package stack

import (
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
)

// NewFaucet creates a new service for the cityofzion/neo-local-faucet image.
func NewFaucet() Service {
return Service{
Author: "cityofzion",
ContainerConfig: &container.Config{
Env: []string{
"NEOSCAN=neo-scan-api:4000",
},
ExposedPorts: map[nat.Port]struct{}{
"4002/tcp": {},
},
},
HostConfig: &container.HostConfig{
// Links: []string{"neo-scan-api:4000"}, TODO: Add when container exists for linking.
PortBindings: map[nat.Port][]nat.PortBinding{
"4002/tcp": {
{
HostIP: "0.0.0.0",
HostPort: "4002",
},
},
},
Privileged: false,
PublishAllPorts: true,
},
Image: "neo-local-faucet",
Tag: "latest",
}
}
10 changes: 6 additions & 4 deletions cli/stack/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import (
"github.com/docker/go-connections/nat"
)

// NewPostgres creates a new service for the Postgres container.
// NewPostgres creates a new service for the library/postgres container.
func NewPostgres() Service {
return Service{
ContainerConfig: container.Config{
Author: "library",
ContainerConfig: &container.Config{
Env: []string{
"POSTGRES_DB=neoscan_prodv",
"POSTGRES_PASSWORD=postgres",
"POSTGRES_USER=postgres",
},
ExposedPorts: map[nat.Port]struct{}{"5432/tcp": {}},
},
Image: "library/postgres",
Tag: "10.5",
HostConfig: nil,
Image: "postgres",
Tag: "10.5",
}
}
15 changes: 5 additions & 10 deletions cli/stack/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
type (
// Service defines a Docker container to run within the stack.
Service struct {
ContainerConfig container.Config
Author string
ContainerConfig *container.Config
HostConfig *container.HostConfig
Image string
Tag string
}
Expand All @@ -19,7 +21,7 @@ type (
func (s Service) Config() *container.Config {
config := s.ContainerConfig
config.Image = s.ImageName()
return &config
return config
}

// ContainerName is the Docker container name.
Expand All @@ -29,12 +31,5 @@ func (s Service) ContainerName() string {

// ImageName is the full Docker image name for the service, including the tag.
func (s Service) ImageName() string {
return fmt.Sprintf("%s:%s", s.Image, s.Tag)
}

// Services returns all the services within the Docker stack.
func Services() []Service {
return []Service{
NewPostgres(),
}
return fmt.Sprintf("%s/%s:%s", s.Author, s.Image, s.Tag)
}
9 changes: 9 additions & 0 deletions cli/stack/services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package stack

// Services returns all the services within the Docker stack.
func Services() []Service {
return []Service{
NewFaucet(),
NewPostgres(),
}
}
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ services:
POSTGRES_USER: postgres
expose:
- 5432
image: postgres:10.5
image: library/postgres:10.5

0 comments on commit 981d611

Please sign in to comment.