Skip to content

Commit

Permalink
feat: #39 support dird (re-) configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Aug 25, 2024
1 parent a502389 commit 90c1772
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 4 deletions.
44 changes: 43 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,36 @@ chmod +x \$SEED_VERSION/ptah-agent
ln -nsf \$SEED_VERSION/ptah-agent \$HOME/ptah-agent/current
echo "Installing docker-ingress-routing-daemon (DIRD)..."
echo " See https://github.com/moby/moby/issues/25526 why the DIRD is needed."
echo " See https://github.com/newsnowlabs/docker-ingress-routing-daemon for technical details."
mkdir -p \$HOME/dird
curl -L https://raw.githubusercontent.com/newsnowlabs/docker-ingress-routing-daemon/b7f58dbac0038f0a925938e639c95a75392c9208/docker-ingress-routing-daemon -o \$HOME/dird/docker-ingress-routing-daemon
chmod +x \$HOME/dird/docker-ingress-routing-daemon
echo '--tcp-ports 80,443 --ingress-gateway-ips 10.0.0.2' > \$HOME/dird/params.conf
echo '#!/usr/bin/env bash' > \$HOME/dird/start.sh
echo '\$HOME/dird/docker-ingress-routing-daemon --install --preexisting --iptables-wait $(cat \$HOME/dird/params.conf)' >> \$HOME/dird/start.sh
chmod +x \$HOME/dird/start.sh
EOF

if [ -z "$(which systemctl)" ]; then
echo "systemctl was not found."
echo ""
echo "Are you running in Docker?"
echo ""
echo "Please add the following command to your init system manually:"
echo "Please add the following commands to your init system manually:"
echo ""
echo " /home/$USER/ptah-agent/current".
echo ""
echo " /home/$USER/dird/start.sh".
echo ""
echo "Installation completed."

exit 0
Expand Down Expand Up @@ -201,10 +221,32 @@ RestartMaxDelaySec=30
WantedBy=multi-user.target
EOF

cat <<EOF > /etc/systemd/system/dird.service
[Unit]
Description=Dird Service
After=sysinit.target dockerd.service
StartLimitIntervalSec=0
[Service]
ExecStart=\$HOME/dird/start.sh
Restart=always
[Path]
PathModified=\$HOME/dird/params.conf
[Install]
WantedBy=multi-user.target
EOF


echo "Reloading systemd..."

systemctl daemon-reload

systemctl enable ptah-agent
systemctl start ptah-agent

systemctl enable dird
systemctl start dird

echo "Installation completed. Please check status on https://ctl.ptah.sh."
57 changes: 57 additions & 0 deletions internal/app/ptah-agent/dird.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ptah_agent

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

t "github.com/ptah-sh/ptah-agent/internal/pkg/ptah-client"
)

func (te *taskExecutor) updateDird(_ context.Context, req *t.UpdateDirdReq) (*t.UpdateDirdRes, error) {
var tcpPorts, udpPorts []string
for _, portSpec := range req.NodePorts {
parts := strings.Split(portSpec, "/")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid port specification: %s", portSpec)
}
protocol, port := strings.ToLower(parts[0]), parts[1]
switch protocol {
case "tcp":
tcpPorts = append(tcpPorts, port)
case "udp":
udpPorts = append(udpPorts, port)
default:
return nil, fmt.Errorf("unknown protocol: %s", protocol)
}
}

ingressGatewayIPs := strings.Join(req.NodeAddresses, ",")
if ingressGatewayIPs == "" {
ingressGatewayIPs = "10.0.0.2"
}
services := strings.Join(req.DockerServices, ",")

var args []string
if len(tcpPorts) > 0 {
args = append(args, fmt.Sprintf("--tcp-ports %s", strings.Join(tcpPorts, ",")))
}
if len(udpPorts) > 0 {
args = append(args, fmt.Sprintf("--udp-ports %s", strings.Join(udpPorts, ",")))
}
args = append(args, fmt.Sprintf("--ingress-gateway-ips %s", ingressGatewayIPs))
if services != "" {
args = append(args, fmt.Sprintf("--services %s", services))
}

formattedLine := strings.Join(args, " ")

paramsFilePath := filepath.Join(te.rootDir, "dird", "params.conf")
if err := os.WriteFile(paramsFilePath, []byte(formattedLine), 0644); err != nil {
return nil, fmt.Errorf("failed to write to params.conf: %w", err)
}

return &t.UpdateDirdRes{}, nil
}
3 changes: 3 additions & 0 deletions internal/app/ptah-agent/parse_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ptah_agent
import (
"encoding/json"
"fmt"

ptahClient "github.com/ptah-sh/ptah-agent/internal/pkg/ptah-client"
)

Expand Down Expand Up @@ -49,6 +50,8 @@ func parseTask(taskType int, payload string) (interface{}, error) {
return unmarshalTask(payload, &ptahClient.S3UploadReq{})
case 19:
return unmarshalTask(payload, &ptahClient.JoinSwarmReq{})
case 20:
return unmarshalTask(payload, &ptahClient.UpdateDirdReq{})
default:
return nil, fmt.Errorf("parse task: unknown task type %d", taskType)
}
Expand Down
6 changes: 4 additions & 2 deletions internal/app/ptah-agent/task_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package ptah_agent
import (
"context"
"fmt"

dockerClient "github.com/docker/docker/client"
caddyClient "github.com/ptah-sh/ptah-agent/internal/pkg/caddy-client"
)
import (

t "github.com/ptah-sh/ptah-agent/internal/pkg/ptah-client"
)

Expand Down Expand Up @@ -60,6 +60,8 @@ func (e *taskExecutor) executeTask(ctx context.Context, task interface{}) (inter
return e.s3upload(ctx, task.(*t.S3UploadReq))
case *t.JoinSwarmReq:
return e.joinSwarm(ctx, task.(*t.JoinSwarmReq))
case *t.UpdateDirdReq:
return e.updateDird(ctx, task.(*t.UpdateDirdReq))
default:
return nil, fmt.Errorf("execute task: unknown task type %T", task)
}
Expand Down
11 changes: 10 additions & 1 deletion internal/pkg/ptah-client/task_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/api/types/swarm"
)
import "github.com/docker/docker/api/types/network"

type TaskError struct {
Message string `json:"message"`
Expand Down Expand Up @@ -218,3 +218,12 @@ type JoinSwarmReq struct {

type JoinSwarmRes struct {
}

type UpdateDirdReq struct {
NodeAddresses []string
DockerServices []string
NodePorts []string
}

type UpdateDirdRes struct {
}

0 comments on commit 90c1772

Please sign in to comment.