Skip to content

Commit 94e7877

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

File tree

8 files changed

+584
-0
lines changed

8 files changed

+584
-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: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# GitLab Webhook Configuration Script
5+
# This script waits for GitLab to be ready and creates a webhook for CodeAgent
6+
7+
GITLAB_URL="${gitlab_url}"
8+
CODEAGENT_IP="${codeagent_ip}"
9+
TOKEN="${gitlab_token}"
10+
WEBHOOK_SECRET="${webhook_secret}"
11+
PROJECT_ID="${project_id}"
12+
13+
echo "============================================"
14+
echo "Configuring GitLab Webhook"
15+
echo "GitLab URL: $GITLAB_URL"
16+
echo "CodeAgent IP: $CODEAGENT_IP"
17+
echo "Project ID: $PROJECT_ID"
18+
echo "============================================"
19+
20+
# Create webhook with retry (GitLab will be checked as part of the API call)
21+
echo "Creating webhook for project ID $PROJECT_ID..."
22+
23+
max_retry=30
24+
retry=0
25+
26+
while [ $retry -lt $max_retry ]; do
27+
echo "Attempt $((retry + 1))/$max_retry..."
28+
29+
response=$(curl -s -w "\n%%{http_code}" --request POST \
30+
--header "PRIVATE-TOKEN: $TOKEN" \
31+
--data "url=http://$CODEAGENT_IP:8889/hook" \
32+
--data "push_events=true" \
33+
--data "merge_requests_events=true" \
34+
--data "issues_events=true" \
35+
--data "confidential_issues_events=true" \
36+
--data "note_events=true" \
37+
--data "confidential_note_events=true" \
38+
--data "enable_ssl_verification=false" \
39+
--data "token=$WEBHOOK_SECRET" \
40+
"$GITLAB_URL/api/v4/projects/$PROJECT_ID/hooks" 2>/dev/null)
41+
42+
http_code=$(echo "$response" | tail -1)
43+
body=$(echo "$response" | sed '$d')
44+
45+
if [ "$http_code" = "201" ]; then
46+
echo "✓ Webhook created successfully!"
47+
echo "Response: $body"
48+
exit 0
49+
elif echo "$body" | grep -q "already exists"; then
50+
echo "✓ Webhook already exists"
51+
exit 0
52+
else
53+
echo "Got response code: $http_code"
54+
retry=$((retry + 1))
55+
if [ $retry -lt $max_retry ]; then
56+
echo "Retrying in 10 seconds..."
57+
sleep 10
58+
fi
59+
fi
60+
done
61+
62+
echo ""
63+
echo "WARNING: Failed to create webhook after $max_retry attempts"
64+
echo "You can manually create it later with:"
65+
echo " curl --request POST --header \"PRIVATE-TOKEN: $TOKEN\" \\"
66+
echo " --data \"url=http://$CODEAGENT_IP:8889/hook\" --data \"token=$WEBHOOK_SECRET\" \\"
67+
echo " \"$GITLAB_URL/api/v4/projects/$PROJECT_ID/hooks\""
68+
exit 0
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: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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 = templatefile("${path.module}/configure_webhook.sh", {
99+
gitlab_url = format("http://%s", qiniu_compute_instance.gitlab_instance.public_ip_addresses[0].ipv4)
100+
codeagent_ip = qiniu_compute_instance.codeagent_instance.public_ip_addresses[0].ipv4
101+
gitlab_token = local.gitlab_token
102+
webhook_secret = local.gitlab_webhook_secret
103+
project_id = "1"
104+
})
105+
interpreter = ["/bin/bash", "-c"]
106+
}
107+
}
108+
109+
resource "random_string" "resource_suffix" {
110+
length = 6
111+
upper = false
112+
lower = true
113+
special = false
114+
}
115+
116+
locals {
117+
instance_suffix = random_string.resource_suffix.result
118+
119+
# Hardcoded GitLab configuration for CodeAgent
120+
gitlab_webhook_secret = "7Xk9pL2qNvR" #gitlab 内置测试项目配置的webhook_secret密钥,仅做测试用,请勿用于真实环境
121+
gitlab_token = "glpat-vkEFt2B0j-bFbEJaUmfWcm86MQp1OjIH.01.0w1yp1q9m" #gitlab 内置配置的codeagent的token ,仅做测试用,请勿用于真实环境
122+
}

0 commit comments

Comments
 (0)