Skip to content

Commit

Permalink
updated template files
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuajerin committed Jun 2, 2024
1 parent cafd187 commit cfd04d4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
21 changes: 21 additions & 0 deletions tembo-cli/src/cmd/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const DOCKERFILE_NAME: &str = "Dockerfile";
const DOCKERCOMPOSE_NAME: &str = "docker-compose.yml";
const POSTGRESCONF_NAME: &str = "postgres.conf";
const MAX_INT32: i32 = 2147483647;
const PGDATA_NAME: &str = "init_pgdata.sh";

/// Deploys a tembo.toml file
#[derive(Args)]
Expand Down Expand Up @@ -299,6 +300,15 @@ fn docker_apply_instance(
true,
)?;

let rendered_pgdata_script: String = get_rendered_init_pgdata()?;

FileUtils::create_file(
PGDATA_NAME.to_string(),
instance_setting.instance_name.clone() + "/" + PGDATA_NAME,
rendered_pgdata_script,
true,
)?;

instance_setting.final_extensions = extensions;

FileUtils::create_file(
Expand Down Expand Up @@ -1039,6 +1049,17 @@ pub fn get_instance_settings(
Ok(base_settings)
}

pub fn get_rendered_init_pgdata() -> Result<String, anyhow::Error> {
let contents = include_str!("../../tembo/init_pgdata.sh.template");

let mut tera = Tera::new("templates/**/*").unwrap();
let _ = tera.add_raw_template("init_pgdata", contents);
let context = Context::new();

let rendered_init_pgdata = tera.render("init_pgdata", &context)?;
Ok(rendered_init_pgdata)
}

pub fn get_rendered_dockerfile(
trunk_installs: &Option<Vec<ControllerTrunkInstall>>,
stack: &Stack,
Expand Down
10 changes: 8 additions & 2 deletions tembo-cli/tembo/Dockerfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ RUN openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 \
chown postgres:postgres /var/lib/postgresql/server.* && \
chmod 600 /var/lib/postgresql/server.key

# Copy the init_pgdata.sh script to the correct location
COPY init_pgdata.sh /docker-entrypoint-initdb.d/init_pgdata.sh

# Set permissions for the init_pgdata.sh script
RUN chmod +x /docker-entrypoint-initdb.d/init_pgdata.sh

USER postgres

# Initialize the database
RUN pg_ctl -c init
# Initialize the database if not already initialized
RUN if [ ! -s "$PGDATA/PG_VERSION" ]; then pg_ctl -c init; fi

# Set permissive authentication (for local testing)
RUN echo "hostssl all all 0.0.0.0/0 trust" >> ${PGDATA}/pg_hba.conf
Expand Down
8 changes: 8 additions & 0 deletions tembo-cli/tembo/docker-compose.yml.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ version: '3.8'
services:
{% for key, instance in instance_settings %}
{{instance.instance_name}}:
platform: linux/amd64
build:
context: ./{{instance.instance_name}}
container_name: {{instance.instance_name}}
volumes:
- {{instance.instance_name}}-data:/var/lib/postgresql/data2
networks:
- tembo
labels:
Expand Down Expand Up @@ -107,3 +110,8 @@ services:

networks:
tembo: {}

volumes:
{% for key, instance in instance_settings %}
{{instance.instance_name}}-data:
{% endfor %}
13 changes: 13 additions & 0 deletions tembo-cli/tembo/init_pgdata.sh.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -e

# Initialize the database if PGDATA is empty
if [ ! -s "$PGDATA/PG_VERSION" ]; then
echo "Initializing database in $PGDATA"
initdb -D "$PGDATA" > /var/log/initdb.log 2>&1
else
echo "Database already initialized in $PGDATA"
fi

# Start PostgreSQL
exec postgres

0 comments on commit cfd04d4

Please sign in to comment.