Skip to content

Commit b9c26ca

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

File tree

7 files changed

+526
-0
lines changed

7 files changed

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