Skip to content

Commit

Permalink
Merge pull request pathwar#423 from pathwar/dev/moul/improve-varnames
Browse files Browse the repository at this point in the history
fix: improve varnames
  • Loading branch information
moul authored Feb 23, 2020
2 parents 7b86610 + 624d3c7 commit ad90645
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
14 changes: 7 additions & 7 deletions go/pkg/pwagent/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func applyNginxConfig(ctx context.Context, apiInstances *pwapi.AgentListInstance
return errcode.ErrComposeGetContainersInfo.Wrap(err)
}
if opts.DomainSuffix == "local" {
proxyNetworkIP := containersInfo.NginxProxyInstance.NetworkSettings.Networks[pwcompose.ProxyNetworkName].IPAddress
proxyNetworkIP := containersInfo.NginxContainer.NetworkSettings.Networks[pwcompose.ProxyNetworkName].IPAddress
opts.DomainSuffix = proxyNetworkIP + ".xip.io"
}
config, err := genNginxConfig(apiInstances, containersInfo, opts)
Expand All @@ -58,7 +58,7 @@ func applyNginxConfig(ctx context.Context, apiInstances *pwapi.AgentListInstance
if err != nil {
return errcode.ErrBuildNginxConfig.Wrap(err)
}
nginxContainer := containersInfo.NginxProxyInstance
nginxContainer := containersInfo.NginxContainer
logger.Debug("copy nginx config into the container", zap.String("container-id", nginxContainer.ID))
err = dockerClient.CopyToContainer(ctx, nginxContainer.ID, "/etc/nginx/", buf, types.CopyToContainerOptions{})
if err != nil {
Expand Down Expand Up @@ -193,14 +193,14 @@ func genNginxConfig(apiInstances *pwapi.AgentListInstances_Output, containersInf

// compute upstreams
for _, flavor := range containersInfo.RunningFlavors {
for _, instance := range flavor.Instances {
for idx, port := range instance.Ports {
for _, container := range flavor.Containers {
for idx, port := range container.Ports {
if port.PublicPort != 0 {
upstream := nginxUpstream{
Name: fmt.Sprintf("%s.%d", instance.Names[0][1:], idx),
Name: fmt.Sprintf("%s.%d", container.Names[0][1:], idx),
InstanceID: flavor.InstanceKey,
AllowedUsers: allowedUsers[flavor.InstanceKey],
Host: instance.NetworkSettings.Networks[pwcompose.ProxyNetworkName].IPAddress,
Host: container.NetworkSettings.Networks[pwcompose.ProxyNetworkName].IPAddress,
Port: strconv.Itoa(int(port.PrivatePort)),
}
config.Upstreams[upstream.Name] = upstream
Expand Down Expand Up @@ -405,7 +405,7 @@ type nginxUpstream struct {
Host string
Port string
Hashes []string
AllowedUsers []int64 // map[INSTANCE_ID][]USER_ID, map[42][]string{4242, 4343}
AllowedUsers []int64
}

const nginxConfigTemplate = `
Expand Down
69 changes: 35 additions & 34 deletions go/pkg/pwcompose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func Up(ctx context.Context, preparedCompose string, instanceKey string, forceRe
}
}

// down instances if force recreate
// down containers if force recreate
if forceRecreate {
err = Clean(ctx, []string{challengeID}, false, false, false, cli, logger)
if err != nil {
Expand Down Expand Up @@ -252,7 +252,7 @@ func Up(ctx context.Context, preparedCompose string, instanceKey string, forceRe
return nil, errcode.ErrComposeUpdateTempFile.Wrap(err)
}

// create instances
// create containers
args := append(composeCliCommonArgs(tmpPreparedComposePath), "up", "--no-start", "--quiet-pull")
logger.Debug("docker-compose", zap.Strings("args", args))
cmd := exec.Command("docker-compose", args...)
Expand All @@ -271,18 +271,18 @@ func Up(ctx context.Context, preparedCompose string, instanceKey string, forceRe
if err != nil {
return nil, errcode.ErrComposeGetContainersInfo.Wrap(err)
}
for _, instance := range containersInfo.RunningInstances {
if challengeID == instance.ChallengeID() {
for _, container := range containersInfo.RunningContainers {
if challengeID == container.ChallengeID() {
// update entrypoints to run pwinit first
imageInspect, _, err := cli.ImageInspectWithRaw(ctx, instance.ImageID)
imageInspect, _, err := cli.ImageInspectWithRaw(ctx, container.ImageID)
if err != nil {
return nil, errcode.ErrDockerAPIImageInspect.Wrap(err)
}
for name, service := range preparedComposeStruct.Services {
if name != instance.Labels[serviceNameLabel] {
if name != container.Labels[serviceNameLabel] {
continue
}
// find service from compose file of current instance
// find service from compose file of current container
entrypoint := []string{}
if len(imageInspect.Config.Entrypoint) > 0 {
entrypoint = imageInspect.Config.Entrypoint
Expand Down Expand Up @@ -310,7 +310,7 @@ func Up(ctx context.Context, preparedCompose string, instanceKey string, forceRe
return nil, errcode.ErrComposeUpdateTempFile.Wrap(err)
}

// build definitive instances
// build definitive containers
args = append(composeCliCommonArgs(tmpPreparedComposePath), "up", "--no-start")
logger.Debug("docker-compose", zap.Strings("args", args))
cmd = exec.Command("docker-compose", args...)
Expand All @@ -330,8 +330,8 @@ func Up(ctx context.Context, preparedCompose string, instanceKey string, forceRe
return nil, errcode.ErrComposeGetContainersInfo.Wrap(err)
}

for _, instance := range containersInfo.RunningInstances {
if challengeID != instance.ChallengeID() {
for _, container := range containersInfo.RunningContainers {
if challengeID != container.ChallengeID() {
continue
}

Expand All @@ -355,15 +355,15 @@ func Up(ctx context.Context, preparedCompose string, instanceKey string, forceRe
if err != nil {
return nil, errcode.ErrCopyPWInitToContainer.Wrap(err)
}
logger.Debug("copy pwinit into the container", zap.String("container-id", instance.ID))
err = cli.CopyToContainer(ctx, instance.ID, "/", buf, types.CopyToContainerOptions{})
logger.Debug("copy pwinit into the container", zap.String("container-id", container.ID))
err = cli.CopyToContainer(ctx, container.ID, "/", buf, types.CopyToContainerOptions{})
if err != nil {
return nil, errcode.ErrCopyPWInitToContainer.Wrap(err)
}

}

// start instances
// start containers
args = append(composeCliCommonArgs(tmpPreparedComposePath), "up", "-d")
logger.Debug("docker-compose", zap.Strings("args", args))
cmd = exec.Command("docker-compose", args...)
Expand All @@ -381,12 +381,12 @@ func Up(ctx context.Context, preparedCompose string, instanceKey string, forceRe
if err != nil {
return nil, errcode.ErrComposeGetContainersInfo.Wrap(err)
}
for _, instance := range containersInfo.RunningInstances {
if challengeID != instance.ChallengeID() {
for _, container := range containersInfo.RunningContainers {
if challengeID != container.ChallengeID() {
continue
}
if proxyNetworkID != "" && instance.NeedsNginxProxy() {
err = cli.NetworkConnect(ctx, proxyNetworkID, instance.ID, nil)
if proxyNetworkID != "" && container.NeedsNginxProxy() {
err = cli.NetworkConnect(ctx, proxyNetworkID, container.ID, nil)
if err != nil {
return nil, errcode.ErrContainerConnectNetwork.Wrap(err)
}
Expand Down Expand Up @@ -421,24 +421,25 @@ func Clean(ctx context.Context, containerIDs []string, removeImages bool, remove

toRemove := map[string]container{}

if withNginx && containersInfo.NginxProxyInstance.ID != "" {
toRemove[containersInfo.NginxProxyInstance.ID] = containersInfo.NginxProxyInstance
if withNginx && containersInfo.NginxContainer.ID != "" {
toRemove[containersInfo.NginxContainer.ID] = containersInfo.NginxContainer
}

if len(containerIDs) == 0 { // all containers
for _, container := range containersInfo.RunningInstances {
for _, container := range containersInfo.RunningContainers {
toRemove[container.ID] = container
}
} else { // only specific ones
for _, id := range containerIDs {
for _, flavor := range containersInfo.RunningFlavors {
if id == flavor.Name || id == flavor.ChallengeID() {
for _, instance := range flavor.Instances {
toRemove[instance.ID] = instance

for _, container := range flavor.Containers {
toRemove[container.ID] = container
}
}
}
for _, container := range containersInfo.RunningInstances {
for _, container := range containersInfo.RunningContainers {
if id == container.ID || id == container.ID[0:7] {
toRemove[container.ID] = container
}
Expand Down Expand Up @@ -467,12 +468,12 @@ func Clean(ctx context.Context, containerIDs []string, removeImages bool, remove
}
}

if withNginx && containersInfo.NginxProxyNetwork.ID != "" {
err = cli.NetworkRemove(ctx, containersInfo.NginxProxyNetwork.ID)
if withNginx && containersInfo.NginxNetwork.ID != "" {
err = cli.NetworkRemove(ctx, containersInfo.NginxNetwork.ID)
if err != nil {
return errcode.ErrDockerAPINetworkRemove.Wrap(err)
}
logger.Debug("network removed", zap.String("ID", containersInfo.NginxProxyNetwork.ID))
logger.Debug("network removed", zap.String("ID", containersInfo.NginxNetwork.ID))
}

return nil
Expand All @@ -489,7 +490,7 @@ func PS(ctx context.Context, depth int, cli *client.Client, logger *zap.Logger)
table.SetHeader([]string{"ID", "CHALLENGE", "SVC", "PORTS", "STATUS", "CREATED"})

for _, flavor := range containersInfo.RunningFlavors {
for uid, container := range flavor.Instances {
for uid, container := range flavor.Containers {

ports := []string{}
for _, port := range container.Ports {
Expand Down Expand Up @@ -571,8 +572,8 @@ func GetContainersInfo(ctx context.Context, cli *client.Client) (*ContainersInfo
}

containersInfo := ContainersInfo{
RunningFlavors: map[string]challengeFlavors{},
RunningInstances: map[string]container{},
RunningFlavors: map[string]challengeFlavors{},
RunningContainers: map[string]container{},
}

for _, dockerContainer := range containers {
Expand All @@ -581,7 +582,7 @@ func GetContainersInfo(ctx context.Context, cli *client.Client) (*ContainersInfo
// pathwar nginx proxy
for _, name := range c.Names {
if name[1:] == NginxContainerName {
containersInfo.NginxProxyInstance = c
containersInfo.NginxContainer = c
}
}

Expand All @@ -592,15 +593,15 @@ func GetContainersInfo(ctx context.Context, cli *client.Client) (*ContainersInfo
flavor := c.ChallengeID()
if _, found := containersInfo.RunningFlavors[flavor]; !found {
challengeFlavor := challengeFlavors{
Instances: map[string]container{},
Containers: map[string]container{},
}
challengeFlavor.Name = c.Labels[challengeNameLabel]
challengeFlavor.Version = c.Labels[challengeVersionLabel]
challengeFlavor.InstanceKey = c.Labels[instanceKeyLabel]
containersInfo.RunningFlavors[flavor] = challengeFlavor
}
containersInfo.RunningFlavors[flavor].Instances[c.ID] = c
containersInfo.RunningInstances[c.ID] = c
containersInfo.RunningFlavors[flavor].Containers[c.ID] = c
containersInfo.RunningContainers[c.ID] = c
}

// find proxy network
Expand All @@ -610,7 +611,7 @@ func GetContainersInfo(ctx context.Context, cli *client.Client) (*ContainersInfo
}
for _, networkResource := range networks {
if networkResource.Name == ProxyNetworkName {
containersInfo.NginxProxyNetwork = networkResource
containersInfo.NginxNetwork = networkResource
break
}
}
Expand Down
13 changes: 6 additions & 7 deletions go/pkg/pwcompose/types.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package pwcompose

import (
fmt "fmt"
"fmt"

"github.com/docker/docker/api/types"
)

// https://github.com/digibib/docker-compose-dot/blob/master/docker-compose-dot.go
type config struct {
Version string
Networks map[string]network
Expand Down Expand Up @@ -51,10 +50,10 @@ type dabservice struct {
}

type ContainersInfo struct {
RunningFlavors map[string]challengeFlavors
RunningInstances map[string]container
NginxProxyInstance container
NginxProxyNetwork types.NetworkResource
RunningFlavors map[string]challengeFlavors
RunningContainers map[string]container
NginxContainer container
NginxNetwork types.NetworkResource
}

type container types.Container
Expand All @@ -76,7 +75,7 @@ type challengeFlavors struct {
Name string
Version string
InstanceKey string
Instances map[string]container
Containers map[string]container
}

func (cf challengeFlavors) ChallengeID() string {
Expand Down

0 comments on commit ad90645

Please sign in to comment.