Skip to content

Commit

Permalink
feat: pwcompose down --with-nginx (pathwar#308)
Browse files Browse the repository at this point in the history
feat: pwcompose down --with-nginx
  • Loading branch information
moul authored Dec 12, 2019
2 parents d968a48 + a0a5221 commit 1df6006
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 190 deletions.
7 changes: 4 additions & 3 deletions api/errcode.proto
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ enum ErrCode {
ErrWritePWInitConfigFile = 3022;
ErrWritePWInitCloseTarWriter = 3023;
ErrCopyPWInitToContainer = 3024;
ErrComposeGetPathwarInfo = 3025;
ErrComposeGetContainersInfo = 3025;


//// Pathwar API (starting at 4001)
Expand Down Expand Up @@ -154,7 +154,7 @@ enum ErrCode {

//// Pathwar Agent (starting at 7001)

ErrAgentGetPathwarInfo = 7001;
ErrAgentGetContainersInfo = 7001;
ErrCheckNginxContainer = 7002;
ErrRemoveNginxContainer = 7003;
ErrBuildNginxContainer = 7004;
Expand Down Expand Up @@ -195,7 +195,8 @@ enum ErrCode {
ErrDockerAPIImagePull = 8009;
ErrDockerAPINetworkList = 8010;
ErrDockerAPINetworkCreate = 8011;
ErrDockerAPIExitCode = 8012;
ErrDockerAPINetworkRemove = 8012;
ErrDockerAPIExitCode = 8013;

//// Pathwar Init (starting at 9001)

Expand Down
2 changes: 1 addition & 1 deletion docs/gen.sum

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

3 changes: 3 additions & 0 deletions go/cmd/pathwar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var (
apiDBURN string
composeDownKeepVolumes bool
composeDownRemoveImages bool
composeDownWithNginx bool
composePSDepth int
composePrepareNoPush bool
composePreparePrefix string
Expand Down Expand Up @@ -120,6 +121,7 @@ func main() {
apiFlags.StringVar(&ssoRealm, "sso-realm", defaultSSORealm, "SSO Realm")
composeDownFlags.BoolVar(&composeDownKeepVolumes, "keep-volumes", false, "keep volumes")
composeDownFlags.BoolVar(&composeDownRemoveImages, "rmi", false, "remove images as well")
composeDownFlags.BoolVar(&composeDownWithNginx, "with-nginx", false, "down nginx container and proxy network as well")
composePSFlags.IntVar(&composePSDepth, "depth", 0, "depth to display")
composePrepareFlags.BoolVar(&composePrepareNoPush, "no-push", false, "don't push images")
composePrepareFlags.StringVar(&composePreparePrefix, "prefix", defaultDockerPrefix, "docker image prefix")
Expand Down Expand Up @@ -467,6 +469,7 @@ func main() {
args,
composeDownRemoveImages,
!composeDownKeepVolumes,
composeDownWithNginx,
cli,
logger,
)
Expand Down
2 changes: 1 addition & 1 deletion go/gen.sum

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

272 changes: 138 additions & 134 deletions go/pkg/errcode/errcode.pb.go

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions go/pkg/pwagent/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func Daemon(ctx context.Context, clean bool, runOnce bool, loopDelay time.Durati
}

if clean {
err := pwcompose.Down(ctx, []string{}, true, true, cli, logger)
err := pwcompose.Down(ctx, []string{}, true, true, true, cli, logger)
if err != nil {
return errcode.ErrCleanPathwarInstances.Wrap(err)
}
Expand All @@ -83,9 +83,9 @@ func Daemon(ctx context.Context, clean bool, runOnce bool, loopDelay time.Durati

func run(ctx context.Context, apiInstances *pwapi.AgentListInstances_Output, cli *client.Client, logger *zap.Logger) error {
// fetch local info from docker daemon
pathwarInfo, err := pwcompose.GetPathwarInfo(ctx, cli)
containersInfo, err := pwcompose.GetContainersInfo(ctx, cli)
if err != nil {
return errcode.ErrComposeGetPathwarInfo.Wrap(err)
return errcode.ErrComposeGetContainersInfo.Wrap(err)
}

agentOpts := AgentOpts{
Expand All @@ -103,7 +103,7 @@ func run(ctx context.Context, apiInstances *pwapi.AgentListInstances_Output, cli
for _, apiInstance := range apiInstances.GetInstances() {
found := false
needRedump := false
for _, flavor := range pathwarInfo.RunningFlavors {
for _, flavor := range containersInfo.RunningFlavors {
if apiInstanceFlavor := apiInstance.GetFlavor(); apiInstanceFlavor != nil {
if apiInstanceFlavorChallenge := apiInstanceFlavor.GetChallenge(); apiInstanceFlavorChallenge != nil {
if flavor.InstanceKey == strconv.FormatInt(apiInstance.GetID(), 10) {
Expand All @@ -123,17 +123,17 @@ func run(ctx context.Context, apiInstances *pwapi.AgentListInstances_Output, cli
return errcode.ErrParseInitConfig.Wrap(err)
}

err = pwcompose.Up(ctx, apiInstance.GetFlavor().GetComposeBundle(), strconv.FormatInt(apiInstance.GetID(), 10), needRedump, &configData, cli, logger)
err = pwcompose.Up(ctx, apiInstance.GetFlavor().GetComposeBundle(), strconv.FormatInt(apiInstance.GetID(), 10), true, &configData, cli, logger)
if err != nil {
return errcode.ErrUpPathwarInstance.Wrap(err)
}
}
}

// update pathwar infos
pathwarInfo, err = pwcompose.GetPathwarInfo(ctx, cli)
containersInfo, err = pwcompose.GetContainersInfo(ctx, cli)
if err != nil {
return errcode.ErrComposeGetPathwarInfo.Wrap(err)
return errcode.ErrComposeGetContainersInfo.Wrap(err)
}

// update nginx configuration
Expand All @@ -146,7 +146,7 @@ func run(ctx context.Context, apiInstances *pwapi.AgentListInstances_Output, cli
if team := subscription.GetTeam(); team != nil {
if members := team.GetMembers(); members != nil {
for _, member := range members {
for _, flavor := range pathwarInfo.RunningFlavors {
for _, flavor := range containersInfo.RunningFlavors {
if flavor.InstanceKey == strconv.FormatInt(apiInstance.GetID(), 10) {
for _, instance := range flavor.Instances {
for _, port := range instance.Ports {
Expand Down
35 changes: 15 additions & 20 deletions go/pkg/pwagent/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ import (
"pathwar.land/go/pkg/pwcompose"
)

const (
nginxContainerName = "pathwar-agent-nginx"
proxyNetworkName = "pathwar-proxy-network"
)

const nginxConfigTemplate = `
{{$root := .}}
#user www www;
Expand Down Expand Up @@ -125,13 +120,13 @@ func Nginx(ctx context.Context, opts AgentOpts, cli *client.Client, logger *zap.
return errcode.ErrDockerAPINetworkList.Wrap(err)
}
for _, networkResource := range networkResources {
if networkResource.Name == proxyNetworkName {
if networkResource.Name == pwcompose.ProxyNetworkName {
proxyNetworkID = networkResource.ID
}
}
if proxyNetworkID == "" {
logger.Debug("proxy network create", zap.String("name", proxyNetworkName))
response, err := cli.NetworkCreate(ctx, proxyNetworkName, types.NetworkCreate{
logger.Debug("proxy network create", zap.String("name", pwcompose.ProxyNetworkName))
response, err := cli.NetworkCreate(ctx, pwcompose.ProxyNetworkName, types.NetworkCreate{
CheckDuplicate: true,
})
proxyNetworkID = response.ID
Expand Down Expand Up @@ -187,7 +182,7 @@ func Nginx(ctx context.Context, opts AgentOpts, cli *client.Client, logger *zap.
}

// connect nginx container to proxy network
if _, onProxyNetwork := nginxContainer.NetworkSettings.Networks[proxyNetworkName]; !onProxyNetwork {
if _, onProxyNetwork := nginxContainer.NetworkSettings.Networks[pwcompose.ProxyNetworkName]; !onProxyNetwork {
logger.Debug("connect nginx to proxy network", zap.String("nginx-id", nginxContainer.ID), zap.String("network-id", proxyNetworkID))
err = cli.NetworkConnect(ctx, proxyNetworkID, nginxContainer.ID, nil)
if err != nil {
Expand All @@ -201,18 +196,18 @@ func Nginx(ctx context.Context, opts AgentOpts, cli *client.Client, logger *zap.
}

// update proxy network nginx container IP
proxyNetworkIP := nginxContainer.NetworkSettings.Networks[proxyNetworkName].IPAddress
proxyNetworkIP := nginxContainer.NetworkSettings.Networks[pwcompose.ProxyNetworkName].IPAddress

// make sure that exposed containers are connected to proxy network
pwInfo, err := pwcompose.GetPathwarInfo(ctx, cli)
containersInfo, err := pwcompose.GetContainersInfo(ctx, cli)
if err != nil {
return errcode.ErrAgentGetPathwarInfo.Wrap(err)
return errcode.ErrAgentGetContainersInfo.Wrap(err)
}
for _, flavor := range pwInfo.RunningFlavors {
for _, flavor := range containersInfo.RunningFlavors {
for _, instance := range flavor.Instances {
for _, port := range instance.Ports {
if port.PrivatePort != 0 {
if _, onProxyNetwork := instance.NetworkSettings.Networks[proxyNetworkName]; !onProxyNetwork {
if _, onProxyNetwork := instance.NetworkSettings.Networks[pwcompose.ProxyNetworkName]; !onProxyNetwork {
logger.Debug("connect container to proxy network", zap.String("container-id", instance.ID), zap.String("network-id", proxyNetworkID))
err = cli.NetworkConnect(ctx, proxyNetworkID, instance.ID, nil)
if err != nil {
Expand All @@ -236,11 +231,11 @@ func Nginx(ctx context.Context, opts AgentOpts, cli *client.Client, logger *zap.
}

// update config data with containers infos
pwInfo, err = pwcompose.GetPathwarInfo(ctx, cli)
containersInfo, err = pwcompose.GetContainersInfo(ctx, cli)
if err != nil {
return errcode.ErrAgentGetPathwarInfo.Wrap(err)
return errcode.ErrAgentGetContainersInfo.Wrap(err)
}
for _, flavor := range pwInfo.RunningFlavors {
for _, flavor := range containersInfo.RunningFlavors {
for _, instance := range flavor.Instances {
for _, port := range instance.Ports {
// FIXME: support non-standard ports using labels (later)
Expand All @@ -249,7 +244,7 @@ func Nginx(ctx context.Context, opts AgentOpts, cli *client.Client, logger *zap.
}
if port.PrivatePort != 0 {
upstream.Name = instance.Names[0][1:]
upstream.Host = instance.NetworkSettings.Networks[proxyNetworkName].IPAddress
upstream.Host = instance.NetworkSettings.Networks[pwcompose.ProxyNetworkName].IPAddress
upstream.Port = strconv.Itoa(int(port.PrivatePort))

// add hash per users to proxy configuration
Expand Down Expand Up @@ -369,7 +364,7 @@ func buildNginxContainer(ctx context.Context, cli *client.Client, opts AgentOpts
},
&container.HostConfig{
PortBindings: portBinding,
}, nil, nginxContainerName)
}, nil, pwcompose.NginxContainerName)
if err != nil {
return "", errcode.ErrDockerAPIContainerCreate.Wrap(err)
}
Expand All @@ -386,7 +381,7 @@ func checkNginxContainer(ctx context.Context, cli *client.Client) (*types.Contai
}
for _, container := range containers {
for _, name := range container.Names {
if name[1:] == nginxContainerName {
if name[1:] == pwcompose.NginxContainerName {
return &container, nil
}
}
Expand Down
Loading

0 comments on commit 1df6006

Please sign in to comment.