-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change docker compose using docker api sdk
- Loading branch information
Showing
28 changed files
with
1,603 additions
and
45 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
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
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,138 @@ | ||
package docker | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/docker/docker/client" | ||
"github.com/rotisserie/eris" | ||
|
||
"pkg.world.dev/world-cli/common/config" | ||
"pkg.world.dev/world-cli/common/docker/service" | ||
"pkg.world.dev/world-cli/common/logger" | ||
) | ||
|
||
type Client struct { | ||
cli *client.Client | ||
cfg *config.Config | ||
} | ||
|
||
func NewClient(cfg *config.Config) (*Client, error) { | ||
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) | ||
if err != nil { | ||
return nil, eris.Wrap(err, "Failed to create docker client") | ||
} | ||
|
||
cfg.BuildkitSupport = checkBuildKitSupport(cli) | ||
|
||
return &Client{ | ||
cli: cli, | ||
cfg: cfg, | ||
}, nil | ||
} | ||
|
||
func (c *Client) Close() error { | ||
return c.cli.Close() | ||
} | ||
|
||
func (c *Client) Start(ctx context.Context, cfg *config.Config, //nolint:gocognit | ||
services ...service.GetDockerService) error { | ||
defer func() { | ||
if !cfg.Detach { | ||
err := c.Stop(cfg, services...) | ||
if err != nil { | ||
logger.Error("Failed to stop containers", err) | ||
} | ||
} | ||
}() | ||
|
||
namespace := cfg.DockerEnv["CARDINAL_NAMESPACE"] | ||
err := c.createNetworkIfNotExists(namespace) | ||
if err != nil { | ||
return eris.Wrap(err, "Failed to create network") | ||
} | ||
|
||
err = c.createVolumeIfNotExists(namespace) | ||
if err != nil { | ||
return eris.Wrap(err, "Failed to create volume") | ||
} | ||
|
||
// var for storing container names | ||
containers := make([]string, 0) | ||
|
||
// iterate over configurations and create containers | ||
for _, getService := range services { | ||
service := getService(cfg) | ||
if cfg.Build && service.Dockerfile.Script != "" { | ||
for _, preImage := range service.Dockerfile.PrerequisiteImages { | ||
if err := c.pullImageIfNotExists(ctx, preImage); err != nil { | ||
return eris.Wrap(err, "Failed to pull prerequisite image") | ||
} | ||
} | ||
|
||
if err := c.buildImage(ctx, service.Dockerfile, service.Image); err != nil { | ||
return eris.Wrap(err, "Failed to build image") | ||
} | ||
} else { | ||
if err := c.pullImageIfNotExists(ctx, service); err != nil { | ||
return eris.Wrap(err, "Failed to pull image") | ||
} | ||
} | ||
|
||
if err := c.createContainer(ctx, service); err != nil { | ||
return eris.Wrap(err, "Failed to create container") | ||
} | ||
|
||
containers = append(containers, service.Name) | ||
} | ||
|
||
// log containers if not detached | ||
if !cfg.Detach { | ||
c.logContainers(ctx, containers) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Client) Stop(cfg *config.Config, services ...service.GetDockerService) error { | ||
ctx := context.Background() | ||
for _, getService := range services { | ||
configuration := getService(cfg) | ||
if err := c.stopAndRemoveContainer(ctx, configuration.Name); err != nil { | ||
return eris.Wrap(err, "Failed to stop container") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Client) Purge(cfg *config.Config) error { | ||
ctx := context.Background() | ||
err := c.Stop(cfg, service.Nakama, service.NakamaDB, | ||
service.Cardinal, service.Redis, service.CelestiaDevNet, service.EVM) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = c.removeVolume(ctx, cfg.DockerEnv["CARDINAL_NAMESPACE"]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = c.removeNetwork(ctx, cfg.DockerEnv["CARDINAL_NAMESPACE"]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Client) Restart(ctx context.Context, cfg *config.Config, | ||
services ...service.GetDockerService) error { | ||
// stop containers | ||
err := c.Stop(cfg, services...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.Start(ctx, cfg, services...) | ||
} |
Oops, something went wrong.