Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
116 changes: 57 additions & 59 deletions bin/split-silo-database
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python
import os

import subprocess

import click
from django.apps import apps

from sentry.runner import configure
from sentry.runner.commands.devservices import get_docker_client
from sentry.silo.base import SiloMode

configure()
Expand All @@ -15,13 +15,17 @@ from django.conf import settings
from sentry.models.organizationmapping import OrganizationMapping


def exec_run(container, command):
wrapped_command = f'sh -c "{" ".join(command)}"'
exit_code, output = container.exec_run(cmd=wrapped_command, stdout=True, stderr=True)
if exit_code:
def exec_run(container_name: str, command: list[str]) -> bytes:
"""Execute a command in a docker container using docker exec."""
shell_command = " ".join(command)
docker_command = ["docker", "exec", container_name, "sh", "-c", shell_command]

try:
result = subprocess.run(docker_command, capture_output=True, check=True, shell=False)
return result.stdout
except subprocess.CalledProcessError as e:
click.echo("Container operation Failed!")
click.echo(f"Container operation failed with {output}")
return output
click.echo(f"Command failed with {e.stderr}")


def split_database(tables: list[str], source: str, destination: str, reset: bool, verbose: bool):
Expand All @@ -31,71 +35,65 @@ def split_database(tables: list[str], source: str, destination: str, reset: bool
command.extend(["-t", table])
command.extend([">", f"/tmp/{destination}-tables.sql"])

with get_docker_client() as client:
postgres_container = (
"sentry-postgres-1"
if os.environ.get("USE_OLD_DEVSERVICES") != "1"
else "sentry_postgres"
)
postgres = client.containers.get(postgres_container)
postgres_container = "sentry-postgres-1"

if verbose:
click.echo(f">> Running {' '.join(command)}")
exec_run(postgres_container, command)

if reset:
click.echo(f">> Dropping existing {destination} database")
exec_run(postgres_container, ["dropdb", "-U", "postgres", "--if-exists", destination])
exec_run(postgres_container, ["createdb", "-U", "postgres", destination])

if verbose:
click.echo(f">> Running {' '.join(command)}")
exec_run(postgres, command)
citext_command = [
"psql",
"-U",
"postgres",
destination,
"-c",
"'CREATE EXTENSION IF NOT EXISTS citext'",
]

if reset:
click.echo(f">> Dropping existing {destination} database")
exec_run(postgres, ["dropdb", "-U", "postgres", "--if-exists", destination])
exec_run(postgres, ["createdb", "-U", "postgres", destination])
if verbose:
click.echo(f">> RUNNING: {' '.join(citext_command)}")
exec_run(postgres_container, citext_command)

# Use the dump file to build control silo tables.
click.echo(f">> Building {destination} database from dump file")
import_command = [
"psql",
"-U",
"postgres",
destination,
"<",
f"/tmp/{destination}-tables.sql",
]
if verbose:
click.echo(f">> Running {' '.join(import_command)}")
exec_run(postgres_container, import_command)

citext_command = [
if destination == "region" and reset:
click.echo(">> Cloning stored procedures")
function_dump = [
"psql",
"-U",
"postgres",
destination,
source,
"-c",
"'CREATE EXTENSION IF NOT EXISTS citext'",
"'\\sf sentry_increment_project_counter'",
]
function_sql = exec_run(postgres_container, function_dump)

if verbose:
click.echo(f">> RUNNING: {' '.join(citext_command)}")
exec_run(postgres, citext_command)

# Use the dump file to build control silo tables.
click.echo(f">> Building {destination} database from dump file")
import_command = [
import_function = [
"psql",
"-U",
"postgres",
destination,
"<",
f"/tmp/{destination}-tables.sql",
"-c",
"'" + function_sql.decode("utf8") + "'",
]
if verbose:
click.echo(f">> Running {' '.join(import_command)}")
exec_run(postgres, import_command)

if destination == "region" and reset:
click.echo(">> Cloning stored procedures")
function_dump = [
"psql",
"-U",
"postgres",
source,
"-c",
"'\\sf sentry_increment_project_counter'",
]
function_sql = exec_run(postgres, function_dump)

import_function = [
"psql",
"-U",
"postgres",
destination,
"-c",
"'" + function_sql.decode("utf8") + "'",
]
exec_run(postgres, import_function)
exec_run(postgres_container, import_function)


def revise_organization_mappings(legacy_region_name: str):
Expand Down
55 changes: 15 additions & 40 deletions devenv/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ def main(context: dict[str, str]) -> int:
FRONTEND_ONLY = os.environ.get("SENTRY_DEVENV_FRONTEND_ONLY") is not None
SKIP_FRONTEND = os.environ.get("SENTRY_DEVENV_SKIP_FRONTEND") is not None

USE_OLD_DEVSERVICES = os.environ.get("USE_OLD_DEVSERVICES") == "1"

if constants.DARWIN and os.path.exists(f"{constants.root}/bin/colima"):
binroot = f"{reporoot}/.devenv/bin"
colima.uninstall(binroot)
Expand Down Expand Up @@ -239,41 +237,20 @@ def main(context: dict[str, str]) -> int:
print("Skipping python migrations since SENTRY_DEVENV_FRONTEND_ONLY is set.")
return 0

if USE_OLD_DEVSERVICES:
# Ensure new devservices is not being used, otherwise ports will conflict
proc.run(
(f"{venv_dir}/bin/devservices", "down"),
pathprepend=f"{reporoot}/.devenv/bin",
exit=True,
)
# TODO: check healthchecks for redis and postgres to short circuit this
proc.run(
(
f"{venv_dir}/bin/{repo}",
"devservices",
"up",
"redis",
"postgres",
),
pathprepend=f"{reporoot}/.devenv/bin",
exit=True,
)
else:
# Ensure old sentry devservices is not being used, otherwise ports will conflict
proc.run(
(
f"{venv_dir}/bin/{repo}",
"devservices",
"down",
),
pathprepend=f"{reporoot}/.devenv/bin",
exit=True,
)
proc.run(
(f"{venv_dir}/bin/devservices", "up", "--mode", "migrations"),
pathprepend=f"{reporoot}/.devenv/bin",
exit=True,
)
proc.run(
(
f"{venv_dir}/bin/{repo}",
"devservices",
"down",
),
pathprepend=f"{reporoot}/.devenv/bin",
exit=True,
)
proc.run(
(f"{venv_dir}/bin/devservices", "up", "--mode", "migrations"),
pathprepend=f"{reporoot}/.devenv/bin",
exit=True,
)

if not run_procs(
repo,
Expand All @@ -290,9 +267,7 @@ def main(context: dict[str, str]) -> int:
):
return 1

postgres_container = (
"sentry_postgres" if os.environ.get("USE_OLD_DEVSERVICES") == "1" else "sentry-postgres-1"
)
postgres_container = "sentry-postgres-1"

# faster prerequisite check than starting up sentry and running createuser idempotently
stdout = proc.run(
Expand Down
4 changes: 0 additions & 4 deletions scripts/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
# shellcheck disable=SC2001 # https://github.com/koalaman/shellcheck/wiki/SC2001

POSTGRES_CONTAINER="sentry-postgres-1"
USE_OLD_DEVSERVICES=${USE_OLD_DEVSERVICES:-"0"}
if [ "$USE_OLD_DEVSERVICES" == "1" ]; then
POSTGRES_CONTAINER="sentry_postgres"
fi

venv_name=".venv"

Expand Down
4 changes: 0 additions & 4 deletions scripts/upgrade-postgres.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#!/bin/bash

POSTGRES_CONTAINER="sentry-postgres-1"
USE_OLD_DEVSERVICES=${USE_OLD_DEVSERVICES:-"0"}
if [ "$USE_OLD_DEVSERVICES" == "1" ]; then
POSTGRES_CONTAINER="sentry_postgres"
fi

OLD_VERSION="9.6"
NEW_VERSION="14"
Expand Down
13 changes: 3 additions & 10 deletions src/sentry/runner/commands/devserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,18 +377,11 @@ def devserver(

# Create all topics if the Kafka eventstream is selected
if kafka_consumers:
use_old_devservices = os.environ.get("USE_OLD_DEVSERVICES") == "1"
valid_kafka_container_names = ["kafka-kafka-1", "sentry_kafka"]
kafka_container_name = "sentry_kafka" if use_old_devservices else "kafka-kafka-1"
kafka_container_warning_message = (
f"""
valid_kafka_container_names = ["kafka-kafka-1"]
kafka_container_name = "kafka-kafka-1"
kafka_container_warning_message = f"""
Devserver is configured to work with `sentry devservices`. Looks like the `{kafka_container_name}` container is not running.
Please run `sentry devservices up kafka` to start it."""
if use_old_devservices
else f"""
Devserver is configured to work with the revamped devservices. Looks like the `{kafka_container_name}` container is not running.
Please run `devservices up` to start it."""
)
if not any(name in containers for name in valid_kafka_container_names):
raise click.ClickException(
f"""
Expand Down
Loading
Loading