以類似ChatGPT的網站前端界面的HTML頁面、CSS樣式表和JavaScript腳本為例,用Python代碼將整個前端代碼文件的目錄,其中包括所有創建的前端代碼文件用Docker打包成鏡像文件,提前檢查Docker軟件是否已經安裝,并如果容器服務不存在,則在Amazon ECR中注冊容器服務,在Amazon ECS中上傳對應的鏡像文件并啟動運行和返回運行狀態,程序需要異常處理和日志文件記錄每一次運行的參數和結果,所有運行所需的參數都存儲在ini配置文件中。
這個解決方案提供了完整的CI/CD流水線基礎框架,可以根據實際需求擴展自動化測試、藍綠部署等高級功能。分步解決方案包含前端代碼和部署腳本。以下是完整的實現:
- 前端代碼文件結構:
chatgpt-frontend/
├── static/
│ ├── css/
│ │ └── style.css
│ └── js/
│ └── script.js
├── templates/
│ └── index.html
├── Dockerfile
├── requirements.txt
├── deploy.py
└── config.ini
- 前端代碼文件內容:
static/css/style.css:
.chat-container {max-width: 800px;margin: 0 auto;padding: 20px;background-color: #f5f5f5;border-radius: 10px;height: 80vh;display: flex;flex-direction: column;
}.chat-messages {flex: 1;overflow-y: auto;padding: 10px;background-color: white;border-radius: 5px;margin-bottom: 10px;
}.message {margin: 10px 0;padding: 10px;border-radius: 5px;max-width: 70%;
}.user-message {background-color: #e3f2fd;margin-left: auto;
}.bot-message {background-color: #f5f5f5;
}.input-container {display: flex;gap: 10px;
}input[type="text"] {flex: 1;padding: 10px;border: 1px solid #ddd;border-radius: 5px;
}button {padding: 10px 20px;background-color: #007bff;color: white;border: none;border-radius: 5px;cursor: pointer;
}button:hover {background-color: #0056b3;
}
templates/index.html:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>ChatGPT Clone</title><link rel="stylesheet" href="/static/css/style.css">
</head>
<body><div class="chat-container"><div class="chat-messages" id="chat-messages"><div class="message bot-message">Hello! How can I help you today?</div></div><div class="input-container"><input type="text" id="user-input" placeholder="Type your message..."><button onclick="sendMessage()">Send</button></div></div><script src="/static/js/script.js"></script>
</body>
</html>
static/js/script.js:
function appendMessage(message, isUser) {const chatMessages = document.getElementById('chat-messages');const messageDiv = document.createElement('div');messageDiv.className = `message ${isUser ? 'user-message' : 'bot-message'}`;messageDiv.textContent = message;chatMessages.appendChild(messageDiv);chatMessages.scrollTop = chatMessages.scrollHeight;
}async function sendMessage() {const userInput = document.getElementById('user-input');const message = userInput.value.trim();if (message) {appendMessage(message, true);userInput.value = '';try {const response = await fetch('/api/chat', {method: 'POST',headers: {'Content-Type': 'application/json'},body: JSON.stringify({ message })});const data = await response.json();appendMessage(data.reply, false);} catch (error) {appendMessage('Error connecting to the server', false);}}
}
Dockerfile:
FROM nginx:alpine
COPY ./static /usr/share/nginx/html/static
COPY ./templates /usr/share/nginx/html/templates
COPY ./templates/index.html /usr/share/nginx/html/index.html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
requirements.txt:
boto3==1.34.28
configparser==5.3.0
docker==6.1.3
config.ini:
[aws]
region = us-west-2
ecr_repository = chatgpt-frontend
ecs_cluster = ecs-cluster
ecs_service = chatgpt-frontend-service
task_definition = chatgpt-frontend-task[deployment]
image_name = chatgpt-frontend
image_tag = latest
port = 80
deploy.py:
import os
import subprocess
import configparser
import logging
import boto3
from docker import from_env
from docker.errors import DockerException
from botocore.exceptions import ClientError, NoCredentialsError# Configure logging
logging.basicConfig(filename='deployment.log',level=logging.INFO,format='%(asctime)s - %(levelname)s - %(message)s'
)class DeploymentError(Exception):passclass AWSClients:def __init__(self, region):self.ecr = boto3.client('ecr', region_name=region)self.ecs = boto3.client('ecs', region_name=region)def load_config():config = configparser.ConfigParser()if not config.read('config.ini'):raise DeploymentError("Configuration file config.ini not found")return configdef check_docker():try:docker_client = from_env()docker_client.ping()logging.info("Docker is running")except DockerException:logging.error("Docker not installed or not running")raise DeploymentError("Docker not available")def build_docker_image(image_name, image_tag):try:client = from_env()image, build_logs = client.images.build(path='.',tag=f"{image_name}:{image_tag}",rm=True)logging.info(f"Successfully built image {image.tags}")return imageexcept DockerException as e:logging.error(f"Docker build error: {str(e)}")raise DeploymentError("Docker build failed")def push_to_ecr(aws_clients, image_name, image_tag):try:auth = aws_clients.ecr.get_authorization_token()username, password = auth['authorizationData'][0]['authorizationToken'].decode('utf-8').split(':')registry = auth['authorizationData'][0]['proxyEndpoint']client = from_env()client.login(username=username, password=password, registry=registry)ecr_image = f"{registry.replace('https://', '')}/{image_name}:{image_tag}"image = client.images.get(f"{image_name}:{image_tag}")image.tag(ecr_image)push_log = client.images.push(ecr_image)logging.info(f"Image pushed to ECR: {ecr_image}")return ecr_imageexcept ClientError as e:logging.error(f"AWS API error: {str(e)}")raise DeploymentError("ECR operation failed")def deploy_to_ecs(aws_clients, ecr_image, config):try:# Update task definitiontask_definition = {'family': config['deployment']['image_name'],'executionRoleArn': 'ecsTaskExecutionRole','containerDefinitions': [{'name': 'web','image': ecr_image,'portMappings': [{'containerPort': int(config['deployment']['port']),'hostPort': int(config['deployment']['port'])}],'essential': True}],'requiresCompatibilities': ['FARGATE'],'networkMode': 'awsvpc','cpu': '256','memory': '512'}response = aws_clients.ecs.register_task_definition(**task_definition)task_arn = response['taskDefinition']['taskDefinitionArn']logging.info(f"Registered task definition: {task_arn}")# Update serviceaws_clients.ecs.update_service(cluster=config['aws']['ecs_cluster'],service=config['aws']['ecs_service'],taskDefinition=task_arn,forceNewDeployment=True)logging.info("Service updated successfully")except ClientError as e:logging.error(f"ECS deployment error: {str(e)}")raise DeploymentError("ECS deployment failed")def main():try:config = load_config()aws_clients = AWSClients(config['aws']['region'])logging.info("Starting deployment process")# Docker operationscheck_docker()build_docker_image(config['deployment']['image_name'],config['deployment']['image_tag'])ecr_image = push_to_ecr(aws_clients,config['deployment']['image_name'],config['deployment']['image_tag'])# ECS deploymentdeploy_to_ecs(aws_clients, ecr_image, config)logging.info("Deployment completed successfully")print("Deployment successful! Service is updating...")except DeploymentError as e:logging.error(f"Deployment failed: {str(e)}")print(f"Error: {str(e)}")except Exception as e:logging.error(f"Unexpected error: {str(e)}")print(f"Unexpected error: {str(e)}")if __name__ == "__main__":main()
使用說明:
- 確保已安裝以下依賴:
- Docker
- Python 3.8+
- AWS CLI 配置好憑證
- 部署步驟:
# 安裝Python依賴
pip install -r requirements.txt# 配置AWS憑證(如果尚未配置)
aws configure# 運行部署腳本
python deploy.py
這個實現包含以下功能:
- 前端界面:
- 響應式聊天界面
- 消息氣泡顯示
- 用戶輸入和發送功能
- 模擬API調用(需后端實現)
- 部署功能:
- Docker鏡像構建和推送
- ECS服務更新
- 完整的錯誤處理
- 日志記錄
- 配置管理
- 安全特性:
- AWS憑證安全管理
- Docker安全構建
- 安全組配置(需在AWS控制臺預先配置)
注意:實際部署前需要完成以下準備工作:
- 在AWS創建ECR倉庫
- 創建ECS集群和任務執行角色
- 配置VPC和安全組
- 在config.ini中填寫正確的AWS資源配置