Skip to content

Commit

Permalink
feat: Add support for socket Activation
Browse files Browse the repository at this point in the history
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
  • Loading branch information
Shubhranshu153 committed Oct 31, 2024
1 parent 65fbf5a commit 8bfea07
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 8 deletions.
39 changes: 32 additions & 7 deletions cmd/finch-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
// register HTTP handler for /debug/pprof on the DefaultServeMux.
_ "net/http/pprof"

"github.com/coreos/go-systemd/v22/activation"
"github.com/coreos/go-systemd/v22/daemon"
"github.com/runfinch/finch-daemon/api/router"
"github.com/runfinch/finch-daemon/pkg/flog"
Expand Down Expand Up @@ -69,6 +70,35 @@ func runAdapter(cmd *cobra.Command, _ []string) error {
return run(options)
}

func getListener(options *DaemonOptions) (net.Listener, error) {
var listener net.Listener
var err error

if options.socketAddr == "fd://" {
if options.socketOwner != -1 {
return nil, fmt.Errorf("socket-owner is not supported while using socket activation using fd://")
}

listeners, err := activation.Listeners()
if err != nil {
return nil, fmt.Errorf("cannot retrieve listeners: %w", err)
}
if len(listeners) != 1 {
return nil, fmt.Errorf("unexpected number of socket activations (%d != 1)", len(listeners))
}
listener = listeners[0]
} else {
listener, err = net.Listen("unix", options.socketAddr)
if err != nil {
return nil, fmt.Errorf("failed to listen on %s: %w", options.socketAddr, err)
}
if err := os.Chown(options.socketAddr, options.socketOwner, options.socketOwner); err != nil {
return nil, fmt.Errorf("failed to chown the socket: %w", err)
}
}
return listener, nil
}

func run(options *DaemonOptions) error {
// This sets the log level of the dependencies that use logrus (e.g., containerd library).
if options.debug {
Expand All @@ -84,14 +114,9 @@ func run(options *DaemonOptions) error {
serverWg := &sync.WaitGroup{}
serverWg.Add(1)

listener, err := net.Listen("unix", options.socketAddr)
listener, err := getListener(options)
if err != nil {
return fmt.Errorf("failed to listen on %s: %w", options.socketAddr, err)
}
// TODO: Revisit this after we use systemd to manage finch-daemon.
// Related: https://github.com/lima-vm/lima/blob/5a9bca3d09481ed7109b14f8d3f0074816731f43/examples/podman-rootful.yaml#L44
if err := os.Chown(options.socketAddr, options.socketOwner, options.socketOwner); err != nil {
return fmt.Errorf("failed to chown the finch-daemon socket: %w", err)
return fmt.Errorf("failed to create a listener: %w", err)
}

if options.debugAddress != "" {
Expand Down
2 changes: 1 addition & 1 deletion docs/debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ sudo journalctl -u finch
```

> **Note**
> The command above assumes that you have used the unit file definition [finch.service](../finch.service) we have provided. If you have created your own unit file for `finch-daemon` and replace `finch-daemon` with the one you have made. Amazon Linux distributions of Finch also use the name `finch` for the finch-daemon service.
> The command above assumes that you have used the unit file definition [finch.service](./sample-service-files/finch.service) we have provided. If you have created your own unit file for `finch-daemon` and replace `finch-daemon` with the one you have made. Amazon Linux distributions of Finch also use the name `finch` for the finch-daemon service.
If you have started `finch-daemon` manually, logs will either be emitted to stderr/stdout.

Expand Down
24 changes: 24 additions & 0 deletions docs/finch-daemon-with-systemd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Using finch-daemon with systemd


# Configuring finch-daemon to support socket activation

This guide provides instructions for setting up and using socket activation for the Finch Daemon with systemd.

### Configure Socket and Service Files

Add the following configuration files to systemd:

### Socket Configuration

Create the socket unit file at `/etc/systemd/system/finch.socket`. An example can be found in [finch-socket-activation.socket](./sample-service-files/finch-socket-activation.socket)

### Service file Configuration

Create the service unit file at /etc/systemd/system/finch.service. An example can be found in [finch-socket-activation.service](./sample-service-files/finch-socket-activation.service)


### Enable the service

sudo systemctl enable finch.socket finch.service
sudo systemctl start finch.socket
Empty file.
Empty file.
File renamed without changes.

0 comments on commit 8bfea07

Please sign in to comment.