Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom installation path for Docker mode #163

Merged
merged 5 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,19 @@
"mounts": [
// Bind mount docker socket under an alias to support docker-from-docker
"source=/var/run/docker.sock,target=/var/run/docker-host.sock,type=bind",
"source=/opt/tyger,target=/opt/tyger,type=bind",
"source=/tmp/tyger,target=/tmp/tyger,type=bind",
"source=tyger-local-buffers,target=/docker-volumes/buffers,type=volume",
"source=tyger-local-run-logs,target=/docker-volumes/run-logs,type=volume"
],

"remoteUser": "vscode",
"overrideCommand": false,
"initializeCommand": ".devcontainer/prepare-host.sh",
"onCreateCommand": ".devcontainer/devcontainer-on-create.sh",

"containerEnv": {
"DOCKER_BUIKDKIT": "1",
"DEVCONTAINER_HOST_HOME": "${localEnv:HOME}",
"TYGER_ACCESSING_FROM_DOCKER": "1"
"TYGER_ACCESSING_FROM_DOCKER": "1",
"TYGER_DOCKER_HOST_PATH_TRANSLATIONS": "${containerWorkspaceFolder}=${localWorkspaceFolder}"
},

"forwardPorts": [
Expand Down
31 changes: 0 additions & 31 deletions .devcontainer/prepare-host.sh

This file was deleted.

2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"mode": "auto",
"program": "cli/cmd/tyger",
"cwd": "${workspaceFolder}",
"args": ["login", "status", "--format", "json"],
"args": ["api", "install", "-f", "/home/vscode/tyger.config"],
"env": {
"TYGER_CACHE_FILE": "/home/vscode/.cache/tyger/.tyger-docker"
},
Expand Down
27 changes: 19 additions & 8 deletions Makefile.docker
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ login-wip-acr:
# do nothing. We don't push any images local docker mode.

set-localsettings: ensure-data-plane-cert
run_secrets_path="/opt/tyger/control-plane/run-secrets/"
ephemeral_buffers_path="/opt/tyger/control-plane/ephemeral-buffers"
install_path=$$(realpath "install/local")
run_secrets_path="$${install_path}/control-plane/run-secrets"
ephemeral_buffers_path="$${install_path}//ephemeral"
logs_path="/docker-volumes/run-logs"
host_path_translations=$$(echo "$$TYGER_DOCKER_HOST_PATH_TRANSLATIONS" | tr ':' '\n' \
| awk -F= '{ \
s=$$1; d=$$2; \
if (substr(s, length(s), 1) != "/") s = s"/"; \
if (substr(d, length(d), 1) != "/") d = d"/"; \
printf "\"%s\":\"%s\",", s, d \
}' \
| sed 's/,$$//'); \

jq <<- EOF > ${CONTROL_PLANE_SERVER_PATH}/appsettings.local.json
{
"urls": "http://unix:/opt/tyger/control-plane/tyger.sock",
"urls": "http://unix:$${install_path}/api.sock",
"logging": { "Console": {"FormatterName": "simple" } },
"auth": {
"enabled": "false"
Expand All @@ -24,7 +33,8 @@ set-localsettings: ensure-data-plane-cert
"docker": {
"runSecretsPath": "$${run_secrets_path}",
"ephemeralBuffersPath": "$${ephemeral_buffers_path}",
"networkName": "tyger-local-network"
"networkName": "tyger-local-network",
"hostPathTranslations": {$$host_path_translations}
}
},
"logArchive": {
Expand All @@ -36,12 +46,12 @@ set-localsettings: ensure-data-plane-cert
"primarySigningPrivateKeyPath": "$$(echo '${ENVIRONMENT_CONFIG_JSON}' | jq -r '.signingKeys.primary.private')",
"bufferSidecarImage": "$$(echo '${ENVIRONMENT_CONFIG_JSON}' | jq -r '.bufferSidecarImage')",
"localStorage": {
"dataPlaneEndpoint": "http+unix:///opt/tyger/data-plane/tyger.data.sock",
"dataPlaneEndpoint": "http+unix://$${install_path}/data-plane/tyger.data.sock",
"tcpDataPlaneEndpoint": "http://localhost:$$(echo '${ENVIRONMENT_CONFIG_JSON}' | jq -r '.dataPlanePort')"
}
},
"database": {
"host": "/opt/tyger/database",
"host": "$${install_path}/database",
"username": "tyger-server",
"databaseName": "tyger-server",
"autoMigrate": "true",
Expand All @@ -51,9 +61,10 @@ set-localsettings: ensure-data-plane-cert
EOF

set-data-plane-localsettings:
install_path=$$(realpath "install/local")
jq <<- EOF > ${DATA_PLANE_SERVER_PATH}/appsettings.local.json
{
"urls": "http://unix:/opt/tyger/data-plane/tyger.data.sock",
"urls": "http://unix:$${install_path}/data-plane/tyger.data.sock",
"logging": { "Console": {"FormatterName": "simple" } },
"dataDirectory": "/docker-volumes/buffers",
"PrimarySigningPublicKeyPath": "$$(echo '${ENVIRONMENT_CONFIG_JSON}' | jq -r '.signingKeys.primary.public')"
Expand Down Expand Up @@ -81,7 +92,7 @@ run-data-plane: set-data-plane-localsettings
dotnet run -v m --no-restore

login:
tyger login --local
tyger login unix://install/local/api.sock

connect-db:
docker exec -it tyger-local-db psql -U tyger-server tyger-server
Expand Down
32 changes: 20 additions & 12 deletions cli/integrationtest/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,33 @@ func TestCloudMigrations(t *testing.T) {
assert.Contains(t, logs, "Migration 2 complete")
}

func getTempDockerInstallationPath(t *testing.T) string {
lowercaseTestName := strings.ToLower(t.Name())

installationPath := fmt.Sprintf("../../install/%s", lowercaseTestName)
require.NoError(t, os.MkdirAll(installationPath, 0755))
installationPath, err := filepath.Abs(installationPath)
require.NoError(t, err)
installationPath, err = filepath.EvalSymlinks(installationPath)
require.NoError(t, err)

return installationPath
}

func TestDockerOnlineMigrations(t *testing.T) {
t.Parallel()
skipUnlessUsingUnixSocket(t)
skipIfNotUsingUnixSocketDirectly(t)

installationPath := getTempDockerInstallationPath(t)
defer os.RemoveAll(installationPath)

environmentConfig := runCommandSucceeds(t, "../../scripts/get-config.sh", "--docker")

lowercaseTestName := strings.ToLower(t.Name())
if len(lowercaseTestName) > 23 {
lowercaseTestName = lowercaseTestName[:23]
}

environmentConfig := runCommandSucceeds(t, "../../scripts/get-config.sh", "--docker")

installationPath := fmt.Sprintf("/tmp/tyger/%s", lowercaseTestName)
defer func() {
os.RemoveAll(installationPath)
}()

configMap := make(map[string]any)
require.NoError(t, yaml.Unmarshal([]byte(environmentConfig), &configMap))
configMap["environmentName"] = lowercaseTestName
Expand Down Expand Up @@ -193,10 +203,8 @@ func TestDockerOfflineMigrations(t *testing.T) {
environmentConfig := runCommandSucceeds(t, "../../scripts/get-config.sh", "--docker")
devConfig := getDevConfig(t)

installationPath := fmt.Sprintf("/tmp/tyger/%s", lowercaseTestName)
defer func() {
os.RemoveAll(installationPath)
}()
installationPath := getTempDockerInstallationPath(t)
defer os.RemoveAll(installationPath)

configMap := make(map[string]any)
require.NoError(t, yaml.Unmarshal([]byte(environmentConfig), &configMap))
Expand Down
14 changes: 12 additions & 2 deletions cli/internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net"
"net/http"
"net/url"
"os"
"strings"
"time"

Expand All @@ -21,8 +22,8 @@ import (
)

const (
DefaultControlPlaneUnixSocketPath = "/opt/tyger/api.sock"
DefaultControlPlaneUnixSocketUrl = "http+unix://" + DefaultControlPlaneUnixSocketPath + ":"
DefaultControlPlaneSocketPathEnvVar = "TYGER_SOCKET_PATH"
defaultControlPlaneUnixSocketPath = "/opt/tyger/api.sock"
)

var (
Expand All @@ -31,6 +32,15 @@ var (
DefaultRetryableClient *retryablehttp.Client
)

func GetDefaultSocketUrl() string {
path := os.Getenv(DefaultControlPlaneSocketPathEnvVar)
if path == "" {
path = defaultControlPlaneUnixSocketPath
}

return "http+unix://" + path + ":"
}

type MakeRoundTripper func(next http.RoundTripper) http.RoundTripper

type MakeDialer func(next dialContextFunc) dialContextFunc
Expand Down
21 changes: 1 addition & 20 deletions cli/internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package client

import (
"context"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -109,36 +108,18 @@ func TestGetProxyFuncExplicitProxyWithoutScheme(t *testing.T) {
}

func TestGetProxyFuncWithDataPlaneProxy(t *testing.T) {
cpClient, err := NewClient(&ClientOptions{ProxyString: "none"})
require.NoError(t, err)

dpProxy := "http://111.222.333.444:5555"
dpClient, err := NewClient(&ClientOptions{ProxyString: dpProxy})
require.NoError(t, err)

controlPlaneUrl, err := url.Parse("https://example.com")
require.NoError(t, err)

tygerClient := &TygerClient{
ControlPlaneUrl: controlPlaneUrl,
ControlPlaneClient: cpClient,
GetAccessToken: func(ctx context.Context) (string, error) {
return "", nil
},
DataPlaneClient: dpClient,
Principal: "me",
RawControlPlaneUrl: controlPlaneUrl,
RawProxy: nil,
}

dataPlaneUrl, err := url.Parse("https://dataplane.example.com")
require.NoError(t, err)

req := &http.Request{
URL: dataPlaneUrl,
}

proxyURL, err := tygerClient.DataPlaneClient.Proxy(req)
proxyURL, err := dpClient.Proxy(req)
require.NoError(t, err)
require.NotNil(t, proxyURL)
require.Equal(t, dpProxy, proxyURL.String())
Expand Down
2 changes: 1 addition & 1 deletion cli/internal/client/sshurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (sp *SshParams) FormatLoginArgs(add ...string) []string {
args := []string{"login"}

if sp.SocketPath != "" {
args = append(args, "--socket-path", sp.SocketPath)
args = append(args, "--server-url", fmt.Sprintf("http+unix://%s", sp.SocketPath))
}

args = append(args, add...)
Expand Down
2 changes: 1 addition & 1 deletion cli/internal/cmd/stdioproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func writeResponseForError(err error) {
}

func newStdioProxyLoginCommand() *cobra.Command {
serverUrl := client.DefaultControlPlaneUnixSocketUrl
serverUrl := client.GetDefaultSocketUrl()
preflight := false
cmd := &cobra.Command{
Use: "login",
Expand Down
24 changes: 22 additions & 2 deletions cli/internal/controlplane/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -80,7 +81,7 @@ type serviceInfo struct {
func Login(ctx context.Context, options LoginConfig) (*client.TygerClient, error) {
if options.ServerUri == LocalUriSentinel {
optionsClone := options
optionsClone.ServerUri = client.DefaultControlPlaneUnixSocketUrl
optionsClone.ServerUri = client.GetDefaultSocketUrl()
c, errUnix := Login(ctx, optionsClone)
if errUnix == nil {
return c, nil
Expand Down Expand Up @@ -344,7 +345,26 @@ func NormalizeServerUri(uri string) (*url.URL, error) {
}

if parsedUrl.Scheme == "http+unix" || parsedUrl.Scheme == "https+unix" {
if !strings.HasSuffix(uri, ":") {
// Turn a relative path into an absolute path
// If the path is already absolute, Host will be empty
// Otherwise, host will be the first path segment.
if parsedUrl.Host != "" {
if parsedUrl.Path == "" {
parsedUrl.Path = parsedUrl.Host
} else {
parsedUrl.Path = parsedUrl.Host + parsedUrl.Path
}
parsedUrl.Host = ""
}

if !path.IsAbs(parsedUrl.Path) {
parsedUrl.Path, err = filepath.Abs(parsedUrl.Path)
if err != nil {
return nil, fmt.Errorf("failed to make path absolute: %w", err)
}
}

if !strings.HasSuffix(parsedUrl.Path, ":") {
parsedUrl.Path += ":"
}
}
Expand Down
Loading
Loading