Skip to content

Commit d771b08

Browse files
committed
feat: 新增codeagent 和 gitlab 双实例配置
1 parent bfad356 commit d771b08

File tree

7 files changed

+523
-0
lines changed

7 files changed

+523
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# CodeAgent Setup Script
5+
# This script configures CodeAgent with GitLab integration
6+
7+
LOG_FILE="/var/log/codeagent_setup.log"
8+
exec > >(tee -a "$LOG_FILE") 2>&1
9+
10+
# Variables passed from Terraform
11+
MODEL_API_KEY='${model_api_key}'
12+
GITLAB_BASE_URL='${gitlab_base_url}'
13+
GITLAB_WEBHOOK_SECRET='${gitlab_webhook_secret}'
14+
GITLAB_TOKEN='${gitlab_token}'
15+
16+
echo "=========================================="
17+
echo "Starting CodeAgent configuration..."
18+
echo "Log file: $LOG_FILE"
19+
echo "=========================================="
20+
21+
22+
# Step 1: Update API_KEY in supervisor config
23+
echo "----------------------------------------"
24+
echo "Step 1: Updating API_KEY in supervisor config..."
25+
echo "----------------------------------------"
26+
27+
SUPERVISOR_CONF="/etc/supervisor/conf.d/codeagent.conf"
28+
if [ ! -f "$SUPERVISOR_CONF" ]; then
29+
echo "ERROR: $SUPERVISOR_CONF not found!"
30+
exit 1
31+
fi
32+
33+
echo "Found supervisor config at: $SUPERVISOR_CONF"
34+
sed -i.bak "s/\"fake_token\"/\"$MODEL_API_KEY\"/g" "$SUPERVISOR_CONF"
35+
echo "✓ API_KEY updated successfully"
36+
37+
# Step 2: Update GitLab configuration in codeagent.yaml
38+
echo "----------------------------------------"
39+
echo "Step 2: Updating GitLab configuration..."
40+
echo "----------------------------------------"
41+
42+
CODEAGENT_CONF="/home/codeagent/codeagent/_package/conf/codeagent.yaml"
43+
if [ ! -f "$CODEAGENT_CONF" ]; then
44+
echo "ERROR: $CODEAGENT_CONF not found!"
45+
exit 1
46+
fi
47+
48+
echo "Found CodeAgent config at: $CODEAGENT_CONF"
49+
cp "$CODEAGENT_CONF" "$CODEAGENT_CONF.bak"
50+
51+
# Replace base_url
52+
sed -i "s|base_url: https://gitlab.com|base_url: $GITLAB_BASE_URL|g" "$CODEAGENT_CONF"
53+
echo "✓ Updated base_url to: $GITLAB_BASE_URL"
54+
55+
# Replace webhook_secret
56+
sed -i "s|webhook_secret: \".*\"|webhook_secret: \"$GITLAB_WEBHOOK_SECRET\"|g" "$CODEAGENT_CONF"
57+
echo "✓ Updated webhook_secret"
58+
59+
# Replace token
60+
sed -i "s|token: \"glpat-.*\"|token: \"$GITLAB_TOKEN\"|g" "$CODEAGENT_CONF"
61+
echo "✓ Updated GitLab token"
62+
63+
echo "✓ GitLab configuration updated successfully"
64+
65+
# Step 3: Restart CodeAgent service
66+
echo "----------------------------------------"
67+
echo "Step 3: Restarting CodeAgent service..."
68+
echo "----------------------------------------"
69+
70+
if ! command -v supervisorctl &> /dev/null; then
71+
echo "ERROR: supervisorctl command not found!"
72+
exit 1
73+
fi
74+
75+
supervisorctl reread
76+
supervisorctl update
77+
echo "✓ CodeAgent service restarted successfully"
78+
79+
# Step 4: Verify services
80+
echo "----------------------------------------"
81+
echo "Step 4: Verifying services..."
82+
echo "----------------------------------------"
83+
84+
echo "Supervisor status:"
85+
supervisorctl status codeagent
86+
87+
echo ""
88+
echo "=========================================="
89+
echo "CodeAgent configuration completed!"
90+
echo "=========================================="
91+
echo ""
92+
echo "Configuration:"
93+
echo " - GitLab URL: $GITLAB_BASE_URL"
94+
echo " - CodeAgent service is running"
95+
echo ""
96+
echo "Next Steps:"
97+
echo " 1. Check service status: supervisorctl status codeagent"
98+
echo " 2. View service logs: supervisorctl tail -f codeagent"
99+
echo " 3. View setup log: cat $LOG_FILE"
100+
echo "=========================================="
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# GitLab Setup Script
5+
# This script configures GitLab with the public IP
6+
7+
LOG_FILE="/var/log/gitlab_setup.log"
8+
exec > >(tee -a "$LOG_FILE") 2>&1
9+
10+
# Variables passed from Terraform
11+
PUBLIC_IP='${public_ip}'
12+
13+
echo "=========================================="
14+
echo "Starting GitLab configuration..."
15+
echo "Log file: $LOG_FILE"
16+
echo "Public IP: $PUBLIC_IP"
17+
echo "=========================================="
18+
19+
# Wait for cloud-init to complete
20+
echo "Waiting for cloud-init to complete..."
21+
cloud-init status --wait || true
22+
echo "Cloud-init completed."
23+
24+
# Update GitLab external_url
25+
echo "----------------------------------------"
26+
echo "Step 1: Updating GitLab external_url..."
27+
echo "----------------------------------------"
28+
29+
GITLAB_CONF="/etc/gitlab/gitlab.rb"
30+
if [ ! -f "$GITLAB_CONF" ]; then
31+
echo "ERROR: $GITLAB_CONF not found!"
32+
exit 1
33+
fi
34+
35+
echo "Found GitLab config at: $GITLAB_CONF"
36+
cp "$GITLAB_CONF" "$GITLAB_CONF.bak"
37+
38+
# Replace external_url
39+
sed -i "s|external_url '.*'|external_url 'http://$PUBLIC_IP'|g" "$GITLAB_CONF"
40+
echo "✓ Updated external_url to http://$PUBLIC_IP"
41+
42+
# Reconfigure GitLab
43+
echo "----------------------------------------"
44+
echo "Step 2: Reconfiguring GitLab..."
45+
echo "----------------------------------------"
46+
47+
if ! command -v gitlab-ctl &> /dev/null; then
48+
echo "ERROR: gitlab-ctl command not found!"
49+
exit 1
50+
fi
51+
52+
gitlab-ctl reconfigure
53+
echo "✓ GitLab reconfigured successfully"
54+
55+
# Verify GitLab status
56+
echo "----------------------------------------"
57+
echo "Step 3: Verifying GitLab status..."
58+
echo "----------------------------------------"
59+
60+
gitlab-ctl status | head -n 5
61+
62+
echo ""
63+
echo "=========================================="
64+
echo "GitLab configuration completed!"
65+
echo "=========================================="
66+
echo ""
67+
echo "Access Information:"
68+
echo " - GitLab URL: http://$PUBLIC_IP"
69+
echo ""
70+
echo "Next Steps:"
71+
echo " 1. Wait 5-10 minutes for GitLab to fully start"
72+
echo " 2. Access GitLab at http://$PUBLIC_IP"
73+
echo " 3. Get root password: cat /etc/gitlab/initial_root_password"
74+
echo "=========================================="
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Generate random passwords
2+
resource "random_password" "gitlab_instance_password" {
3+
length = 16
4+
special = true
5+
lower = true
6+
upper = true
7+
numeric = true
8+
}
9+
10+
resource "random_password" "codeagent_instance_password" {
11+
length = 16
12+
special = true
13+
lower = true
14+
upper = true
15+
numeric = true
16+
}
17+
18+
# Step 1: Create GitLab instance first
19+
resource "qiniu_compute_instance" "gitlab_instance" {
20+
instance_type = var.gitlab_instance_type
21+
name = format("gitlab-%s", local.instance_suffix)
22+
description = format("GitLab instance %s", local.instance_suffix)
23+
image_id = var.gitlab_image_id
24+
system_disk_size = var.gitlab_system_disk_size
25+
internet_max_bandwidth = var.gitlab_internet_max_bandwidth
26+
password = random_password.gitlab_instance_password.result
27+
28+
timeouts {
29+
create = "30m"
30+
update = "20m"
31+
delete = "10m"
32+
}
33+
}
34+
35+
# Use null_resource to configure GitLab with actual public IP via SSH
36+
resource "null_resource" "configure_gitlab" {
37+
depends_on = [qiniu_compute_instance.gitlab_instance]
38+
39+
connection {
40+
type = "ssh"
41+
user = "root"
42+
password = random_password.gitlab_instance_password.result
43+
host = qiniu_compute_instance.gitlab_instance.public_ip_addresses[0].ipv4
44+
timeout = "10m"
45+
agent = false
46+
host_key = null
47+
}
48+
49+
# Upload the setup script
50+
provisioner "file" {
51+
content = templatefile("${path.module}/gitlab_setup.sh", {
52+
public_ip = qiniu_compute_instance.gitlab_instance.public_ip_addresses[0].ipv4
53+
})
54+
destination = "/tmp/gitlab_setup.sh"
55+
}
56+
57+
# Execute the script
58+
provisioner "remote-exec" {
59+
inline = [
60+
"chmod +x /tmp/gitlab_setup.sh",
61+
"/tmp/gitlab_setup.sh"
62+
]
63+
}
64+
}
65+
66+
# Step 2: Create CodeAgent instance after GitLab is configured
67+
resource "qiniu_compute_instance" "codeagent_instance" {
68+
depends_on = [null_resource.configure_gitlab]
69+
70+
instance_type = var.codeagent_instance_type
71+
name = format("codeagent-%s", local.instance_suffix)
72+
description = format("CodeAgent instance %s", local.instance_suffix)
73+
image_id = var.codeagent_image_id
74+
system_disk_size = var.codeagent_system_disk_size
75+
internet_max_bandwidth = var.codeagent_internet_max_bandwidth
76+
password = random_password.codeagent_instance_password.result
77+
78+
# Configure CodeAgent with GitLab URL
79+
user_data = base64encode(templatefile("${path.module}/codeagent_setup.sh", {
80+
model_api_key = var.model_api_key
81+
gitlab_base_url = format("http://%s", qiniu_compute_instance.gitlab_instance.public_ip_addresses[0].ipv4)
82+
gitlab_webhook_secret = local.gitlab_webhook_secret
83+
gitlab_token = local.gitlab_token
84+
}))
85+
86+
timeouts {
87+
create = "30m"
88+
update = "20m"
89+
delete = "10m"
90+
}
91+
}
92+
93+
# Step 3: Configure GitLab webhook after CodeAgent is ready
94+
resource "null_resource" "configure_gitlab_webhook" {
95+
depends_on = [qiniu_compute_instance.codeagent_instance]
96+
97+
provisioner "local-exec" {
98+
command = <<-EOT
99+
# Create webhook for project ID 1
100+
curl --request POST \
101+
--header "PRIVATE-TOKEN: ${local.gitlab_token}" \
102+
--data "url=http://${qiniu_compute_instance.codeagent_instance.public_ip_addresses[0].ipv4}:8889/hook" \
103+
--data-urlencode "push_events=true" \
104+
--data-urlencode "merge_requests_events=true" \
105+
--data-urlencode "issues_events=true" \
106+
--data-urlencode "confidential_issues_events=true" \
107+
--data-urlencode "note_events=true" \
108+
--data-urlencode "confidential_note_events=true" \
109+
--data "enable_ssl_verification=false" \
110+
--data "token=${local.gitlab_webhook_secret}" \
111+
"http://${qiniu_compute_instance.gitlab_instance.public_ip_addresses[0].ipv4}/api/v4/projects/1/hooks"
112+
EOT
113+
}
114+
}
115+
116+
resource "random_string" "resource_suffix" {
117+
length = 6
118+
upper = false
119+
lower = true
120+
special = false
121+
}
122+
123+
locals {
124+
instance_suffix = random_string.resource_suffix.result
125+
126+
# Hardcoded GitLab configuration for CodeAgent
127+
gitlab_webhook_secret = "7X#k9$pL!2qN@vR" #gitlab 内置测试项目配置的webhook_secret密钥,仅做测试用,请勿用于真实环境
128+
gitlab_token = "glpat-vkEFt2B0j-bFbEJaUmfWcm86MQp1OjIH.01.0w1yp1q9m" #gitlab 内置配置的codeagent的token ,仅做测试用,请勿用于真实环境
129+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# GitLab instance outputs
2+
output "gitlab_instance_id" {
3+
value = qiniu_compute_instance.gitlab_instance.id
4+
description = "GitLab instance ID"
5+
}
6+
7+
output "gitlab_public_ip" {
8+
value = qiniu_compute_instance.gitlab_instance.public_ip_addresses[0].ipv4
9+
description = "GitLab instance public IP address"
10+
}
11+
12+
output "gitlab_private_ip" {
13+
value = qiniu_compute_instance.gitlab_instance.private_ip_addresses[0].ipv4
14+
description = "GitLab instance private IP address"
15+
}
16+
17+
output "gitlab_instance_password" {
18+
value = random_password.gitlab_instance_password.result
19+
description = "Root password for SSH access to GitLab instance"
20+
sensitive = true
21+
}
22+
23+
output "gitlab_url" {
24+
value = format("http://%s", qiniu_compute_instance.gitlab_instance.public_ip_addresses[0].ipv4)
25+
description = "GitLab access URL"
26+
}
27+
28+
# CodeAgent instance outputs
29+
output "codeagent_instance_id" {
30+
value = qiniu_compute_instance.codeagent_instance.id
31+
description = "CodeAgent instance ID"
32+
}
33+
34+
output "codeagent_public_ip" {
35+
value = qiniu_compute_instance.codeagent_instance.public_ip_addresses[0].ipv4
36+
description = "CodeAgent instance public IP address"
37+
}
38+
39+
output "codeagent_private_ip" {
40+
value = qiniu_compute_instance.codeagent_instance.private_ip_addresses[0].ipv4
41+
description = "CodeAgent instance private IP address"
42+
}
43+
44+
output "codeagent_instance_password" {
45+
value = random_password.codeagent_instance_password.result
46+
description = "Root password for SSH access to CodeAgent instance"
47+
sensitive = true
48+
}
49+
50+
# Combined information
51+
output "deployment_summary" {
52+
value = {
53+
gitlab = {
54+
instance_id = qiniu_compute_instance.gitlab_instance.id
55+
public_ip = qiniu_compute_instance.gitlab_instance.public_ip_addresses[0].ipv4
56+
url = format("http://%s", qiniu_compute_instance.gitlab_instance.public_ip_addresses[0].ipv4)
57+
}
58+
codeagent = {
59+
instance_id = qiniu_compute_instance.codeagent_instance.id
60+
public_ip = qiniu_compute_instance.codeagent_instance.public_ip_addresses[0].ipv4
61+
}
62+
}
63+
description = "Complete deployment summary"
64+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# CodeAgent & GitLab Deployment Configuration Example
2+
# Copy this file to terraform.tfvars and update with your values
3+
4+
# ===== Required Parameters =====
5+
6+
# AI Model API Key (Required)
7+
model_api_key = "your-ai-model-api-key-here"
8+
9+
# ===== Optional: Instance Configuration =====
10+
11+
# GitLab Instance Configuration
12+
# gitlab_instance_type = "ecs.c1.c16m32" # 16 cores, 32GB RAM
13+
# gitlab_system_disk_size = 100 # 100GB system disk
14+
# gitlab_internet_max_bandwidth = 100 # 100Mbps bandwidth
15+
# gitlab_image_id = "image-693bdc845d7ae428f33348af"
16+
17+
# CodeAgent Instance Configuration
18+
# codeagent_instance_type = "ecs.c1.c16m32" # 16 cores, 32GB RAM
19+
# codeagent_system_disk_size = 100 # 100GB system disk
20+
# codeagent_internet_max_bandwidth = 100 # 100Mbps bandwidth
21+
# codeagent_image_id = "image-693b7d014fc9d0719531c21f"
22+
23+
# ===== Note =====
24+
# GitLab configuration (webhook_secret and token) is hardcoded in the module

0 commit comments

Comments
 (0)