brash-kilobyte-32523
07/25/2024, 3:48 PMbig-architect-71258
07/25/2024, 6:15 PMlocalhost:5432
?brash-kilobyte-32523
07/26/2024, 5:34 AMpostgresql:index:Role (agentApplicationUser):
error: sdk-v2/provider2.go:385: sdk.helper_schema: Error connecting to PostgreSQL server localhost (scheme: postgres): read tcp [::1]:58361->[::1]:5432: read: connection reset by peer: provider=postgresql@3.11.3
error: 1 error occurred:
* Error connecting to PostgreSQL server localhost (scheme: postgres): read tcp [::1]:58361->[::1]:5432: read: connection reset by peer
big-architect-71258
07/26/2024, 7:47 AMconnection reset by peer
means: the client could connect to the socket, but the server closed it. So I wonder what's really wrong here. So I'd try to get more logging data:
TF_LOG=TRACE pulumi up --logtostderr --logflow -v=10 2> out.txt
https://www.pulumi.com/docs/support/troubleshooting/brash-kilobyte-32523
07/26/2024, 10:01 AMbrash-kilobyte-32523
07/26/2024, 10:35 AMbig-architect-71258
07/26/2024, 10:50 AMbig-architect-71258
07/26/2024, 10:56 AMpulumi preview
Try to use pulumi up --skip-preview
.big-architect-71258
07/26/2024, 11:04 AMbig-architect-71258
07/26/2024, 11:04 AMbig-architect-71258
07/26/2024, 11:05 AMbig-architect-71258
07/26/2024, 11:24 AMpsql
it works without any issuesbrash-kilobyte-32523
07/26/2024, 11:32 AMbig-architect-71258
07/26/2024, 11:33 AMbrash-kilobyte-32523
07/26/2024, 11:36 AMbig-architect-71258
07/26/2024, 11:38 AMbrash-kilobyte-32523
07/26/2024, 11:42 AMbrash-kilobyte-32523
07/26/2024, 12:11 PMbig-architect-71258
07/26/2024, 12:15 PMsslmode
must be set to disabled
. When you do that, a correct error message about the missing schema
property is shown. Then is added schema: "public"
and everything went smoothly.big-architect-71258
07/26/2024, 12:16 PMbig-architect-71258
07/26/2024, 12:18 PMbig-architect-71258
07/26/2024, 12:27 PMbig-architect-71258
07/26/2024, 12:27 PMbig-architect-71258
07/26/2024, 12:27 PMbig-architect-71258
07/26/2024, 12:28 PMimport * as pulumi from "@pulumi/pulumi";
import * as docker from "@pulumi/docker";
import * as postgresql from "@pulumi/postgresql";
import * as time from "@pulumiverse/time"
const network = new docker.Network("identus-stack", {
ipamConfigs: [{
subnet: "172.18.0.0/16",
}],
driver: "bridge", // You can choose other drivers like "overlay", "host", etc.
attachable: true,
checkDuplicate: true
});
// Create a Docker container running PostgreSQL
const postgresContainer = new docker.Container("postgresContainer", {
image: "postgres:16",
ports: [{
internal: 5432,
external: 5432,
}],
envs: [
"POSTGRES_DB=agent",
"POSTGRES_USER=postgres",
"POSTGRES_PASSWORD=postgres",
],
hostname: "postgres-agent",
publishAllPorts: true,
rm: true, // Remove the container when stopped
healthcheck: {
tests: ["CMD", "pg_isready", "-U", "postgres"],
interval: "30s",
timeout: "10s",
retries: 5,
},
command: [
"postgres", "-c", "log_statement=all", "-c", "log_destination=stderr", "-c", "log_connections=true", "-c", "log_error_verbosity=VERBOSE",
],
networksAdvanced: [
{
name: network.name,
ipv4Address: "172.18.0.2",
}
],
}, { dependsOn: network });
const containerIp = pulumi.output(postgresContainer.networksAdvanced).apply(networks => {
const networkInfo = networks && networks[0];
return networkInfo ? networkInfo.ipv4Address : undefined;
});
containerIp.apply(ip => {
console.log(`Postgres Container IP: ${ip}`);
});
export const containerId = postgresContainer.id;
export const containerName = postgresContainer.name;
const wait = new time.Sleep("wait-container", {
createDuration: "10s"
}, {
dependsOn: [
postgresContainer
]
})
const pgProvider = new postgresql.Provider("pgProvider", {
host: "127.0.0.1",
port: 5432,
username: "postgres",
password: "postgres",
sslmode: "disable"
},
{ dependsOn: [wait], parent: postgresContainer }
);
const agentDbApplicationUser = new postgresql.Role("agentApplicationUser", {
name: "agent-application-user",
password: "postgres",
login: true,
},
{ provider: pgProvider, parent: pgProvider }
);
const agentDbApplicationUserPrivileges = new postgresql.Grant("agentApplicationUserPrivileges", {
role: agentDbApplicationUser.name,
database: "agent",
objectType: "table",
schema: "public",
privileges: ["SELECT", "INSERT", "UPDATE", "DELETE"],
},
{
provider: pgProvider, parent: pgProvider
}
);
big-architect-71258
07/26/2024, 12:28 PMpulumi destroy
works flawlessly as wellbig-architect-71258
07/26/2024, 12:32 PMbig-architect-71258
07/26/2024, 12:35 PMbig-architect-71258
07/26/2024, 12:36 PMbig-architect-71258
07/26/2024, 12:44 PMdocker.RemoteImage
instance to get the image ID and added networkMode: "bridge",
.
With that the deployment seem to be stable.big-architect-71258
07/26/2024, 12:45 PMimport * as pulumi from "@pulumi/pulumi";
import * as docker from "@pulumi/docker";
import * as postgresql from "@pulumi/postgresql";
import * as time from "@pulumiverse/time"
const network = new docker.Network("identus-stack", {
ipamConfigs: [{
subnet: "172.18.0.0/16",
}],
driver: "bridge", // You can choose other drivers like "overlay", "host", etc.
attachable: true,
checkDuplicate: true
});
const postgresImage = new docker.RemoteImage("image", {
name: "postgres:16"
})
// Create a Docker container running PostgreSQL
const postgresContainer = new docker.Container("postgresContainer", {
image: postgresImage.imageId,
ports: [{
internal: 5432,
external: 5432,
}],
envs: [
"POSTGRES_DB=agent",
"POSTGRES_USER=postgres",
"POSTGRES_PASSWORD=postgres",
],
hostname: "postgres-agent",
publishAllPorts: true,
rm: true, // Remove the container when stopped
healthcheck: {
tests: ["CMD", "pg_isready", "-U", "postgres"],
interval: "30s",
timeout: "10s",
retries: 5,
},
command: [
"postgres", "-c", "log_statement=all", "-c", "log_destination=stderr", "-c", "log_connections=true", "-c", "log_error_verbosity=VERBOSE",
],
networksAdvanced: [
{
name: network.name,
ipv4Address: "172.18.0.2",
}
],
networkMode: "bridge",
}, { dependsOn: network });
const containerIp = pulumi.output(postgresContainer.networksAdvanced).apply(networks => {
const networkInfo = networks && networks[0];
return networkInfo ? networkInfo.ipv4Address : undefined;
});
containerIp.apply(ip => {
console.log(`Postgres Container IP: ${ip}`);
});
export const containerId = postgresContainer.id;
export const containerName = postgresContainer.name;
const wait = new time.Sleep("wait-container", {
createDuration: "10s"
}, {
parent: postgresContainer,
dependsOn: [
postgresContainer
]
})
const pgProvider = new postgresql.Provider("pgProvider", {
host: "127.0.0.1",
port: 5432,
username: "postgres",
password: "postgres",
sslmode: "disable"
},
{ dependsOn: [wait], parent: postgresContainer }
);
const agentDbApplicationUser = new postgresql.Role("agentApplicationUser", {
name: "agent-application-user",
password: "postgres",
login: true,
},
{ provider: pgProvider, parent: pgProvider }
);
const agentDbApplicationUserPrivileges = new postgresql.Grant("agentApplicationUserPrivileges", {
role: agentDbApplicationUser.name,
database: "agent",
objectType: "table",
schema: "public",
privileges: ["SELECT", "INSERT", "UPDATE", "DELETE"],
},
{
provider: pgProvider, parent: pgProvider
}
);
big-architect-71258
07/26/2024, 12:49 PMkeepLocally: true,
to the RemoteImage
when you don't want to have image deleted locally on destruction.brash-kilobyte-32523
07/26/2024, 1:15 PMbig-architect-71258
07/26/2024, 1:16 PMtime.Sleep
resource gets recreated and thus will spend the amout of waiting time on creation, us the triggers
parameter and add the container.id
to it.
https://www.pulumi.com/registry/packages/time/api-docs/sleep/#inputsbrash-kilobyte-32523
07/26/2024, 1:16 PMbig-architect-71258
07/26/2024, 1:17 PMbrash-kilobyte-32523
07/26/2024, 1:17 PMbrash-kilobyte-32523
07/26/2024, 1:18 PMbrash-kilobyte-32523
07/26/2024, 1:19 PMbrash-kilobyte-32523
07/26/2024, 1:19 PMbig-architect-71258
07/26/2024, 1:21 PMbig-architect-71258
07/26/2024, 1:29 PMI have in mind the idea to reuse the bootstrap logic for different stacks (docker/k8s/aws)Create a
ComponentResource
https://www.pulumi.com/docs/concepts/resources/components/ and distribute the code as a nodejs (Typescript) package. So that it's easily reusable.big-architect-71258
07/26/2024, 1:30 PMbig-architect-71258
07/26/2024, 1:31 PMbig-architect-71258
07/26/2024, 1:32 PMbig-architect-71258
07/26/2024, 1:40 PMpg_ready
CLI could be used to test if the PostgreSQL server is ready. https://www.postgresql.org/docs/current/app-pg-isready.html
You could use the Pulumi Command provider to run pg_ready
. https://www.pulumi.com/registry/packages/command/api-docs/local/command/#command-local-commandbig-architect-71258
07/26/2024, 1:41 PMbig-architect-71258
07/26/2024, 1:52 PMpg_isready
is available in the container. So that it's not required to install this tool locally you could use the Pulumi Command provider and run a command inside the DB container
docker exec $DOCKER_CONTAINER_NAME pg_isready
The docker
cli must be installed anyway because of the Docker Provider.brash-kilobyte-32523
07/26/2024, 2:22 PM