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

feat: add proxy deployment #18

Merged
merged 12 commits into from
Nov 11, 2024
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/target/
/.github
/.vscode
/.git
/cursor.toml
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
POSTGRES_DB=atoma_proxy_db
POSTGRES_USER=atoma_proxy_user
POSTGRES_PASSWORD=

TRACE_LEVEL=info
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ target/
config.toml
logs/
cursor.toml

.env
53 changes: 53 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Builder stage
FROM rust:1.76-slim-bullseye as builder

ARG TRACE_LEVEL

# Install build dependencies
RUN apt-get update && apt-get install -y \
build-essential \
pkg-config \
libssl-dev \
curl \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/atoma-proxy

COPY . .

# Build the application
RUN RUST_LOG=${TRACE_LEVEL} cargo build --release --bin atoma-proxy

# Final stage
FROM debian:bullseye-slim

# Install runtime dependencies
RUN apt-get update && apt-get install -y \
ca-certificates \
libssl1.1 \
libpq5 \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Create necessary directories
RUN mkdir -p /app/logs

# Copy the built binary from builder stage
COPY --from=builder /usr/src/atoma-proxy/target/release/atoma-proxy /usr/local/bin/atoma-proxy

# Copy configuration file
COPY config.toml ./config.toml

# Set executable permissions explicitly
RUN chmod +x /usr/local/bin/atoma-proxy

# Copy and set up entrypoint script
COPY entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh

# Copy host client.yaml and modify keystore path
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

# Use full path in CMD
CMD ["/usr/local/bin/atoma-proxy", "--config-path", "/app/config.toml"]
28 changes: 0 additions & 28 deletions atoma-state/src/state_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,6 @@ impl AtomaStateManager {
}
}

async fn create_database_if_not_exists(db_url: &str, db_name: &str) -> Result<()> {
// Connect to the PostgreSQL server (default database)
let pool: PgPool = PgPool::connect(db_url).await?;

// Check if the database exists
let exists: (bool,) =
sqlx::query_as("SELECT EXISTS(SELECT 1 FROM pg_database WHERE datname = $1)")
.bind(db_name)
.fetch_one(&pool)
.await?;

// Create the database if it does not exist
if !exists.0 {
sqlx::query(&format!("CREATE DATABASE {}", db_name))
.execute(&pool)
.await?;
println!("Database '{}' created.", db_name);
} else {
println!("Database '{}' already exists.", db_name);
}

Ok(())
}

/// Creates a new `AtomaStateManager` instance from a database URL.
///
/// This method establishes a connection to the Postgres database using the provided URL,
Expand All @@ -75,10 +51,6 @@ impl AtomaStateManager {
event_subscriber_receiver: FlumeReceiver<AtomaEvent>,
state_manager_receiver: FlumeReceiver<AtomaAtomaStateManagerEvent>,
) -> Result<Self> {
let (db_url, db_name) = database_url
.rsplit_once('/')
.ok_or(AtomaStateManagerError::DatabaseUrlError)?;
Self::create_database_if_not_exists(db_url, db_name).await?;
let db = PgPool::connect(&database_url).await?;
queries::create_all_tables(&db).await?;
Ok(Self {
Expand Down
3 changes: 2 additions & 1 deletion config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ sui_keystore_path = "~/.sui/sui_config/sui.keystore" # Path to the Sui keystore
cursor_path = "."

[atoma-state]
database_url = "" # URL of the PostgreSQL database
# URL of the PostgreSQL database, it SHOULD be the same as the `ATOMA_STATE_DATABASE_URL` variable value in the .env file
database_url = "postgresql://POSTGRES_USER:POSTGRES_PASSWORD@db:5432/POSTGRES_DB"

[atoma-service]
service_bind_address = "0.0.0.0:8080" # Address to bind the service to
40 changes: 40 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
services:
db:
image: postgres:13
restart: always
environment:
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
ports:
- "5432:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
networks:
- atoma-network

atoma-proxy:
build:
context: .
dockerfile: Dockerfile
args:
TRACE_LEVEL: ${TRACE_LEVEL:-info}
volumes:
- ./config.toml:/app/config.toml
- ./logs:/app/logs
- ${SUI_CONFIG_PATH:-~/.sui/sui_config}:/root/.sui/sui_config
env_file:
- .env
ports:
- "${ATOMA_SERVICE_PORT:-8080}:8080" # Expose Atoma service port
depends_on:
- db
networks:
- atoma-network

volumes:
postgres-data:

networks:
atoma-network:
driver: bridge
12 changes: 12 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# Ensure the directory exists
mkdir -p /root/.sui/sui_config

# If client.yaml exists, modify it
if [ -f /root/.sui/sui_config/client.yaml ]; then
sed -i 's|File: .*|File: /root/.sui/sui_config/sui.keystore|' /root/.sui/sui_config/client.yaml
fi

# Execute the main command passed to the container
exec "$@"