🌐 AI搜索 & 代理 主页
Skip to content

Commit c5ab446

Browse files
committed
Enhances GCP deployment for PostgresAI
Adds metadata files for GCP Marketplace integration. Refactors configuration management for instances.yml, supporting both automatic updates and manual configuration for Marketplace deployments. Updates default values and variable descriptions for improved usability. Streamlines the user data script and configuration process.
1 parent 9f1151c commit c5ab446

File tree

11 files changed

+594
-163
lines changed

11 files changed

+594
-163
lines changed

terraform/gcp/.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ Thumbs.db
2626

2727
# Backup files
2828
*.bak
29-
*.backup
29+
*.backupgsutil cp
3030

3131
# SSH keys
3232
*.pem
3333
*.key
34+
35+
36+
/dist/*

terraform/gcp/README.md

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Deploy postgres_ai monitoring stack on Google Cloud Platform using Compute Engine.
44

5+
> **Note for GCP Marketplace users**: After deployment from Marketplace, monitoring instances must be configured manually (see "Configure monitoring instances" section below).
6+
57
## Architecture
68

79
Single Compute Engine instance running Docker Compose with all monitoring components:
@@ -134,38 +136,72 @@ terraform output -json grafana_credentials
134136
gcloud compute ssh ubuntu@production-postgres-ai-monitoring --zone=us-central1-a
135137
```
136138

137-
## Monitoring setup
138-
139-
### Add Postgres instance
139+
## Configure monitoring instances
140140

141-
Create monitoring user:
142-
143-
```sql
144-
create user monitoring with password 'secure_password';
145-
grant pg_monitor to monitoring;
146-
grant connect on database postgres to monitoring;
147-
```
141+
### Method 1: Terraform (before deployment)
148142

149143
Add to `terraform.tfvars`:
150144

151145
```hcl
152146
monitoring_instances = [
153147
{
154148
name = "my-db"
155-
conn_str = "host=10.0.0.5 port=5432 user=monitoring dbname=postgres password=secure_password sslmode=require"
149+
conn_str = "postgresql://monitoring:password@host:5432/postgres"
156150
environment = "production"
157151
cluster = "main"
158152
node_name = "primary"
159153
}
160154
]
161155
```
162156

163-
Apply changes:
164-
157+
Deploy:
165158
```bash
166159
terraform apply
167160
```
168161

162+
### Method 2: Manual (after deployment, for Marketplace)
163+
164+
1. Create monitoring user on your Postgres instance:
165+
166+
```sql
167+
create user monitoring with password 'secure_password';
168+
grant pg_monitor to monitoring;
169+
grant connect on database postgres to monitoring;
170+
```
171+
172+
2. SSH to monitoring instance:
173+
174+
```bash
175+
gcloud compute ssh ubuntu@<instance-name> --zone=<zone>
176+
```
177+
178+
3. Edit instances.yml:
179+
180+
```bash
181+
sudo -u postgres_ai vim /home/postgres_ai/postgres_ai/instances.yml
182+
```
183+
184+
Add your instances:
185+
186+
```yaml
187+
- name: my-db
188+
conn_str: postgresql://monitoring:password@host:5432/postgres
189+
preset_metrics: full
190+
custom_metrics:
191+
ts_enabled: true
192+
group: default
193+
custom_tags:
194+
env: production
195+
cluster: main
196+
node_name: primary
197+
```
198+
199+
4. Apply configuration:
200+
201+
```bash
202+
sudo -u postgres_ai /home/postgres_ai/postgres_ai/postgres_ai update-config
203+
```
204+
169205
### PostgresAI reports
170206

171207
Configure API key:

terraform/gcp/config_management.tf

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# This file provides automatic instances.yml configuration management
2+
#
3+
# NOTE: This file is NOT compatible with GCP Marketplace due to provider restrictions
4+
# For Marketplace deployments, this file is automatically excluded from the package
5+
# For regular Terraform deployments, this file enables automatic config updates
6+
#
7+
# To use:
8+
# 1. Keep this file for regular Terraform deployments
9+
# 2. Update monitoring_instances in terraform.tfvars
10+
# 3. Run terraform apply - instances.yml will be automatically updated on the server
11+
12+
terraform {
13+
required_providers {
14+
local = {
15+
source = "hashicorp/local"
16+
version = "~> 2.0"
17+
}
18+
}
19+
}
20+
21+
# Generate instances.yml from template
22+
resource "local_sensitive_file" "instances_config" {
23+
content = templatefile("${path.module}/instances.yml.tpl", {
24+
monitoring_instances = var.monitoring_instances
25+
enable_demo_db = var.enable_demo_db
26+
})
27+
filename = "${path.module}/.terraform/instances.yml"
28+
}
29+
30+
# Deploy instances.yml to GCP instance when config changes
31+
resource "terraform_data" "deploy_config" {
32+
triggers_replace = {
33+
config_hash = local_sensitive_file.instances_config.content_md5
34+
}
35+
36+
depends_on = [google_compute_instance.main, google_compute_disk.data]
37+
38+
provisioner "remote-exec" {
39+
inline = [
40+
"if ! sudo test -f /home/postgres_ai/postgres_ai/postgres_ai; then echo 'Skipping - installation not complete'; exit 0; fi",
41+
"cat > /tmp/instances.yml << 'EOF'",
42+
local_sensitive_file.instances_config.content,
43+
"EOF",
44+
"sudo mv /tmp/instances.yml /home/postgres_ai/postgres_ai/instances.yml",
45+
"sudo chown postgres_ai:postgres_ai /home/postgres_ai/postgres_ai/instances.yml",
46+
"sudo -u postgres_ai /home/postgres_ai/postgres_ai/postgres_ai update-config",
47+
"echo 'Config updated successfully'"
48+
]
49+
50+
connection {
51+
type = "ssh"
52+
user = "ubuntu"
53+
host = var.use_static_ip ? google_compute_address.main[0].address : google_compute_instance.main.network_interface[0].access_config[0].nat_ip
54+
}
55+
}
56+
}
57+

terraform/gcp/main.tf

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ terraform {
55
source = "hashicorp/google"
66
version = "~> 5.0"
77
}
8-
local = {
9-
source = "hashicorp/local"
10-
version = "~> 2.0"
11-
}
128
}
139
}
1410

@@ -17,14 +13,8 @@ provider "google" {
1713
region = var.region
1814
}
1915

20-
# Get available zones
21-
data "google_compute_zones" "available" {
22-
region = var.region
23-
status = "UP"
24-
}
25-
2616
locals {
27-
zone = var.zone != "" ? var.zone : data.google_compute_zones.available.names[0]
17+
zone = var.zone != "" ? var.zone : "${var.region}-a"
2818
common_tags = {
2919
environment = var.environment
3020
managed_by = "terraform"
@@ -34,21 +24,21 @@ locals {
3424

3525
# VPC Network
3626
resource "google_compute_network" "main" {
37-
name = "${var.environment}-postgres-ai-network"
27+
name = "${var.goog_cm_deployment_name}-network"
3828
auto_create_subnetworks = false
3929
}
4030

4131
# Subnet
4232
resource "google_compute_subnetwork" "main" {
43-
name = "${var.environment}-postgres-ai-subnet"
33+
name = "${var.goog_cm_deployment_name}-subnet"
4434
ip_cidr_range = var.subnet_cidr
4535
region = var.region
4636
network = google_compute_network.main.id
4737
}
4838

4939
# Firewall rules
5040
resource "google_compute_firewall" "ssh" {
51-
name = "${var.environment}-postgres-ai-allow-ssh"
41+
name = "${var.goog_cm_deployment_name}-allow-ssh"
5242
network = google_compute_network.main.name
5343

5444
allow {
@@ -61,7 +51,7 @@ resource "google_compute_firewall" "ssh" {
6151
}
6252

6353
resource "google_compute_firewall" "grafana" {
64-
name = "${var.environment}-postgres-ai-allow-grafana"
54+
name = "${var.goog_cm_deployment_name}-allow-grafana"
6555
network = google_compute_network.main.name
6656

6757
allow {
@@ -75,7 +65,7 @@ resource "google_compute_firewall" "grafana" {
7565

7666
# Data disk
7767
resource "google_compute_disk" "data" {
78-
name = "${var.environment}-postgres-ai-data"
68+
name = "${var.goog_cm_deployment_name}-data"
7969
type = var.data_disk_type
8070
zone = local.zone
8171
size = var.data_volume_size
@@ -87,22 +77,22 @@ resource "google_compute_disk" "data" {
8777
resource "google_compute_address" "main" {
8878
count = var.use_static_ip ? 1 : 0
8979

90-
name = "${var.environment}-postgres-ai-ip"
80+
name = "${var.goog_cm_deployment_name}-ip"
9181
region = var.region
9282
}
9383

9484
# Compute Engine instance
9585
resource "google_compute_instance" "main" {
96-
name = "${var.environment}-postgres-ai-monitoring"
86+
name = "${var.goog_cm_deployment_name}-vm"
9787
machine_type = var.machine_type
9888
zone = local.zone
9989

10090
tags = ["postgres-ai-monitoring"]
10191

10292
boot_disk {
10393
initialize_params {
104-
image = "ubuntu-os-cloud/ubuntu-2204-lts"
105-
size = 30
94+
image = var.source_image != "" ? var.source_image : "ubuntu-os-cloud/ubuntu-2204-lts"
95+
size = var.boot_disk_size
10696
type = var.boot_disk_type
10797
}
10898
}
@@ -131,8 +121,12 @@ resource "google_compute_instance" "main" {
131121
metadata_startup_script = templatefile("${path.module}/user_data.sh", {
132122
grafana_password = var.grafana_password
133123
postgres_ai_api_key = var.postgres_ai_api_key
134-
monitoring_instances = var.monitoring_instances
135124
enable_demo_db = var.enable_demo_db
125+
postgres_ai_version = var.postgres_ai_version
126+
instances_yml = templatefile("${path.module}/instances.yml.tpl", {
127+
monitoring_instances = var.monitoring_instances
128+
enable_demo_db = var.enable_demo_db
129+
})
136130
})
137131

138132
labels = local.common_tags
@@ -144,40 +138,4 @@ resource "google_compute_instance" "main" {
144138
allow_stopping_for_update = true
145139
}
146140

147-
# Generate instances.yml from template
148-
resource "local_file" "instances_config" {
149-
content = templatefile("${path.module}/instances.yml.tpl", {
150-
monitoring_instances = var.monitoring_instances
151-
enable_demo_db = var.enable_demo_db
152-
})
153-
filename = "${path.module}/.terraform/instances.yml"
154-
}
155-
156-
# Deploy instances.yml to GCP instance when config changes
157-
resource "terraform_data" "deploy_config" {
158-
triggers_replace = {
159-
config_hash = local_file.instances_config.content_md5
160-
}
161-
162-
depends_on = [google_compute_instance.main, google_compute_disk.data]
163-
164-
provisioner "remote-exec" {
165-
inline = [
166-
"if ! sudo test -f /home/postgres_ai/postgres_ai/postgres_ai; then echo 'Skipping - installation not complete'; exit 0; fi",
167-
"cat > /tmp/instances.yml << 'EOF'",
168-
local_file.instances_config.content,
169-
"EOF",
170-
"sudo mv /tmp/instances.yml /home/postgres_ai/postgres_ai/instances.yml",
171-
"sudo chown postgres_ai:postgres_ai /home/postgres_ai/postgres_ai/instances.yml",
172-
"sudo -u postgres_ai /home/postgres_ai/postgres_ai/postgres_ai update-config",
173-
"echo 'Config updated successfully'"
174-
]
175-
176-
connection {
177-
type = "ssh"
178-
user = "ubuntu"
179-
host = var.use_static_ip ? google_compute_address.main[0].address : google_compute_instance.main.network_interface[0].access_config[0].nat_ip
180-
}
181-
}
182-
}
183141

0 commit comments

Comments
 (0)