Skip to content

Commit 28588c3

Browse files
authored
Support building/running copies of Sync Gateway (#385)
1 parent 9426a60 commit 28588c3

8 files changed

Lines changed: 199 additions & 2 deletions

File tree

environment/aws/topology_setup/test_server_platforms/exe_bridge.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,25 @@ class ExeBridge(PlatformBridge):
5858
A class to manage executable applications on local or remote machines.
5959
"""
6060

61-
def __init__(self, exe_path: str, extra_args: list[str] | None = None):
61+
def __init__(
62+
self,
63+
exe_path: str,
64+
extra_args: list[str] | None = None,
65+
*,
66+
log_filename: str = "server.log",
67+
):
6268
"""
6369
Initialize the ExeBridge with the executable path and optional extra arguments.
6470
6571
Args:
6672
exe_path (str): The path to the executable file.
6773
extra_args (Optional[List[str]]): A list of extra arguments to pass to the executable.
74+
log_filename (str): The filename for the log output (default "server.log").
6875
"""
6976
self.__exe_path = exe_path
7077
self.__extra_args = extra_args or []
7178
self.__exe_name = Path(self.__exe_path).name
79+
self.__log_filename = log_filename
7280

7381
def validate(self, location: str) -> None:
7482
"""
@@ -104,7 +112,7 @@ def run(self, location: str) -> None:
104112

105113
args = [self.__exe_path]
106114
args.extend(self.__extra_args)
107-
log_file = Path(self.__exe_path).parent / "server.log"
115+
log_file = Path(self.__exe_path).parent / self.__log_filename
108116
log_fd = open(log_file, "w")
109117
process = subprocess.Popen(
110118
args, start_new_session=True, stdout=log_fd, stderr=log_fd

environment/local/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Build artifacts for windows
2+
*.exe
3+
4+
# Sync Gateway local builds
5+
sync_gateway
6+
sync_gateway_clone/
7+
*.log

environment/local/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Local Environment Setup
2+
3+
This directory contains scripts to help set up and run a local testing environment.
4+
5+
## Building Sync Gateway
6+
7+
Use `build_sync_gateway.py` to compile Sync Gateway from source. This requires `go` to be installed on your system.
8+
9+
```bash
10+
# Build from an existing local repository
11+
uv run environment/local/build_sync_gateway.py --repo-path /path/to/sync-gateway
12+
13+
# Build from a specific git tag/branch (clones to environment/local/sync_gateway_clone)
14+
uv run environment/local/build_sync_gateway.py --git-tag release/3.3.0
15+
```
16+
17+
## Running Sync Gateway
18+
19+
Once built, you can manage the Sync Gateway process using `run_sync_gateway.py`. You must provide either `--start` (with a `--server` type) or `--stop`.
20+
21+
```bash
22+
# Start Sync Gateway in the background using Rosmar
23+
uv run environment/local/run_sync_gateway.py --start --server rosmar
24+
25+
# Start Sync Gateway in the background using CBS
26+
uv run environment/local/run_sync_gateway.py --start --server cbs
27+
28+
# Stop the background Sync Gateway process
29+
uv run environment/local/run_sync_gateway.py --stop
30+
```
31+
32+
- **Logs:** Written to `environment/local/sync_gateway.log`.
33+
- **Configuration:**
34+
- `--server rosmar`: uses `environment/local/sync_gateway_config/basic_sync_gateway_rosmar.json`
35+
- `--server cbs`: uses `environment/local/sync_gateway_config/basic_sync_gateway_cbs.json`
36+
37+
## Running the Test Server
38+
39+
To start the local test server (CBL-C):
40+
41+
```bash
42+
uv run environment/local/start_local.py
43+
```
44+
45+
## Running Tests
46+
47+
After starting the environment, you can run tests against it:
48+
49+
```bash
50+
cd tests/dev_e2e
51+
52+
# If using Couchbase Server (cbs)
53+
uv run pytest --config ../../environment/local/cbs_config.json
54+
55+
# If using Rosmar
56+
uv run pytest --config ../../environment/local/rosmar_config.json
57+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import subprocess
4+
import sys
5+
6+
import click
7+
8+
9+
@click.command()
10+
@click.option("--repo-path", help="Path to an existing sync_gateway repo")
11+
@click.option("--git-tag", help="Build tag (will clone/reset a local repo to this tag)")
12+
def main(repo_path, git_tag):
13+
"""Build Sync Gateway"""
14+
if bool(repo_path) == bool(git_tag):
15+
raise click.UsageError(
16+
"Exactly one of --repo-path or --git-tag must be provided."
17+
)
18+
19+
env_local_dir = os.path.dirname(os.path.abspath(__file__))
20+
output_bin = os.path.join(env_local_dir, "sync_gateway")
21+
22+
if repo_path:
23+
repo_dir = os.path.abspath(repo_path)
24+
if not os.path.isdir(repo_dir):
25+
click.secho(f"Error: Repository path {repo_dir} does not exist.", fg="red")
26+
sys.exit(1)
27+
else:
28+
# We use a local clone
29+
repo_dir = os.path.join(env_local_dir, "sync_gateway_clone")
30+
repo_url = "https://github.com/couchbase/sync_gateway.git"
31+
32+
if not os.path.exists(repo_dir):
33+
click.echo(f"Cloning {repo_url} into {repo_dir}...")
34+
subprocess.check_call(["git", "clone", repo_url, repo_dir])
35+
36+
click.echo(f"Fetching updates and checking out {git_tag}...")
37+
subprocess.check_call(["git", "fetch", "--all", "--tags"], cwd=repo_dir)
38+
subprocess.check_call(["git", "reset", "--hard"], cwd=repo_dir)
39+
subprocess.check_call(["git", "checkout", git_tag], cwd=repo_dir)
40+
41+
click.echo(f"Building sync_gateway in {repo_dir}...")
42+
build_cmd = ["go", "build", "-tags", "cb_sg_enterprise", "-o", output_bin, "."]
43+
44+
try:
45+
subprocess.check_call(build_cmd, cwd=repo_dir)
46+
click.secho(f"Successfully built sync_gateway to {output_bin}", fg="green")
47+
except subprocess.CalledProcessError as e:
48+
click.secho(f"Error building sync_gateway: {e}", fg="red")
49+
sys.exit(1)
50+
51+
52+
if __name__ == "__main__":
53+
main()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
import pathlib
3+
import sys
4+
5+
import click
6+
7+
SCRIPT_DIR = pathlib.Path(__file__).parent.resolve()
8+
if __name__ == "__main__":
9+
sys.path.append(str(SCRIPT_DIR.parent.parent))
10+
11+
from environment.aws.topology_setup.test_server_platforms.exe_bridge import ExeBridge
12+
13+
14+
@click.command()
15+
@click.option("--start", is_flag=True, help="Start the sync_gateway instance.")
16+
@click.option("--stop", is_flag=True, help="Stop the running sync_gateway instance.")
17+
@click.option(
18+
"--server",
19+
type=click.Choice(["rosmar", "cbs"]),
20+
help="The server type to use (required for --start).",
21+
)
22+
def main(start, stop, server):
23+
"""Manage local Sync Gateway process."""
24+
if start == stop:
25+
raise click.UsageError("Exactly one of --start or --stop must be provided.")
26+
27+
if start and not server:
28+
raise click.UsageError("--server is required when using --start.")
29+
30+
exe_path = str(SCRIPT_DIR / "sync_gateway")
31+
32+
# We only need config if starting
33+
config_path = None
34+
if start:
35+
config_dir = SCRIPT_DIR / "sync_gateway_config"
36+
config_file = (
37+
"basic_sync_gateway_rosmar.json"
38+
if server == "rosmar"
39+
else "basic_sync_gateway_cbs.json"
40+
)
41+
config_path = str(config_dir / config_file)
42+
43+
bridge = ExeBridge(
44+
exe_path=exe_path,
45+
extra_args=[config_path] if config_path else [],
46+
log_filename="sync_gateway.log",
47+
)
48+
49+
if stop:
50+
bridge.stop("localhost")
51+
else:
52+
bridge.run("localhost")
53+
54+
55+
if __name__ == "__main__":
56+
main()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"bootstrap": {
3+
"server": "couchbase://localhost",
4+
"server_tls_skip_verify": true,
5+
"use_tls_server": false,
6+
"username": "Administrator",
7+
"password": "password"
8+
},
9+
"logging": {
10+
"console": {
11+
"enabled": true,
12+
"log_level": "info",
13+
"log_keys": ["*"]
14+
}
15+
}
16+
}

environment/local/basic_sync_gateway_rosmar.json renamed to environment/local/sync_gateway_config/basic_sync_gateway_rosmar.json

File renamed without changes.

0 commit comments

Comments
 (0)