-
-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
shared/cliconfig: Add keepalive proxy support
Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
- Loading branch information
Showing
3 changed files
with
88 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//go:build !windows | ||
|
||
package cliconfig | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/lxc/incus/client" | ||
"github.com/lxc/incus/shared/subprocess" | ||
"github.com/lxc/incus/shared/util" | ||
) | ||
|
||
func (c *Config) handleKeepAlive(remote Remote, name string, args *incus.ConnectionArgs) (incus.InstanceServer, error) { | ||
// Create the socker directory if missing. | ||
socketDir := filepath.Join(c.ConfigDir, "keepalive") | ||
err := os.Mkdir(socketDir, 0700) | ||
if err != nil && !os.IsExist(err) { | ||
return nil, err | ||
} | ||
|
||
// Attempt to use the existing socket. | ||
socketPath := filepath.Join(socketDir, fmt.Sprintf("%s.socket", name)) | ||
d, err := incus.ConnectIncusUnix(socketPath, args) | ||
if err != nil { | ||
// Delete any existing sockets. | ||
_ = os.Remove(socketPath) | ||
|
||
// Spawn the proxy. | ||
proc, err := subprocess.NewProcess("incus", []string{"remote", "proxy", name, socketPath, fmt.Sprintf("--timeout=%d", remote.KeepAlive)}, "", "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = proc.Start(context.Background()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Try up to 10 times over 5s. | ||
for i := 0; i < 10; i++ { | ||
if util.PathExists(socketPath) { | ||
break | ||
} | ||
|
||
time.Sleep(500 * time.Millisecond) | ||
} | ||
|
||
// Connect to the proxy. | ||
d, err = incus.ConnectIncusUnix(socketPath, args) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return d, nil | ||
} |
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,13 @@ | ||
//go:build windows | ||
|
||
package cliconfig | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/lxc/incus/client" | ||
) | ||
|
||
func (c *Config) handleKeepAlive(remote Remote, name string, args *incus.ConnectionArgs) (incus.InstanceServer, error) { | ||
return nil, fmt.Errorf("Keepalive isn't supported on Windows") | ||
} |
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