Skip to content

Commit

Permalink
chore: refactor conn.py
Browse files Browse the repository at this point in the history
  • Loading branch information
yangbodong22011 committed Dec 6, 2024
1 parent eef8b19 commit ac006dc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 62 deletions.
1 change: 0 additions & 1 deletion Terraform/Aliyun/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then
fi
cd resp-compatibility
git checkout dailyTest
python3 conn.py
EOF
}
Expand Down
123 changes: 62 additions & 61 deletions conn.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
import os
import threading
import time
import yaml
import subprocess

# 更新配置文件
try:
def update_config():
yml_file_path = '/root/db_config.yml'
config_file_path = 'config.yaml'

with open(yml_file_path, 'r') as yml_file, open(config_file_path, 'r') as config_file:
yml_data = yaml.safe_load(yml_file)
config_data = yaml.safe_load(config_file)
try:
with open(yml_file_path, 'r') as yml_file, open(config_file_path, 'r') as config_file:
yml_data = yaml.safe_load(yml_file)
config_data = yaml.safe_load(config_file)

for db_name, db_config in yml_data.items():
if db_name in config_data['Database']:
for key, value in db_config.items():
config_data['Database'][db_name][key] = value
else:
config_data['Database'][db_name] = db_config
for db_name, db_config in yml_data.items():
if db_name in config_data['Database']:
for key, value in db_config.items():
config_data['Database'][db_name][key] = value
else:
config_data['Database'][db_name] = db_config

with open(config_file_path, 'w') as config_file:
yaml.dump(config_data, config_file, default_flow_style=False)
with open(config_file_path, 'w') as config_file:
yaml.dump(config_data, config_file, default_flow_style=False)

print("Config file updated successfully.")
print("Config file updated successfully.")

except FileNotFoundError as e:
print(f"Error: {e}. Please check the file paths.")
except yaml.YAMLError as e:
print(f"Error in YAML processing: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
except FileNotFoundError as e:
print(f"Error: {e}. Please check the file paths.")
except yaml.YAMLError as e:
print(f"Error in YAML processing: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")


# 执行命令并判断是否成功
def execute_command(commands):
for command in commands:
result = subprocess.run(command, shell=True, capture_output=True, text=True)
Expand All @@ -44,50 +41,32 @@ def execute_command(commands):
print(f"Successfully executed command '{command}': {result.stdout}")
return True

def run_tests():
run_test_command = [
"pip3 install -r requirements.txt",
"python3 resp_compatibility.py --testfile cts.json --genhtml --show-failed",
]


commands = [
"apt-get update",
"apt-get install -y python3-pip",
]
if not execute_command(commands):
print("Failed to update or install packages. Exiting...")
exit(1)

# 运行测试命令
run_test_command = [
"pip3 install -r requirements.txt",
"python3 resp_compatibility.py --testfile cts.json --genhtml --show-failed",
]

def run_test():
if not execute_command(run_test_command):
print("Test failed. Exiting...")
exit(1)
else:
print("Test completed successfully.")

# 启动测试脚本的线程
test_thread = threading.Thread(target=run_test)
test_thread.start()

time.sleep(300)

# 提交和推送测试结果
commit_and_push_commands = [
"mv html /root",
"git stash -u",
"git checkout gh-pages || git checkout -b gh-pages",
"git pull origin gh-pages",
"cp -r /root/html/* .",
"git add .",
"git commit -m 'Update test results'",
]
if not execute_command(commit_and_push_commands):
print("Failed to commit and push changes. Exiting...")
exit(1)

# 推送到 GitHub 并重试
def commit_and_push_results():
commit_and_push_commands = [
"mv html /root",
"git stash -u",
"git checkout gh-pages || git checkout -b gh-pages",
"git pull origin gh-pages",
"cp -r /root/html/* .",
"git add .",
"git commit -m 'Update test results'",
]
if not execute_command(commit_and_push_commands):
print("Failed to commit and push changes. Exiting...")
exit(1)

def git_push_with_retry():
while True:
result = subprocess.run("git push -u origin gh-pages", shell=True, capture_output=True, text=True)
Expand All @@ -98,5 +77,27 @@ def git_push_with_retry():
print(f"Git push failed: {result.stderr}. Retrying in 5 seconds...")
time.sleep(5)

git_push_with_retry()
def main():
update_config()

package_update_commands = [
"apt-get update",
"apt-get install -y python3-pip",
]
if not execute_command(package_update_commands):
print("Failed to update or install packages. Exiting...")
exit(1)

test_thread = threading.Thread(target=run_tests)
test_thread.start()
test_thread.join(timeout=300)

if test_thread.is_alive():
print("Test timed out. Exiting...")
exit(1)

commit_and_push_results()
git_push_with_retry()

if __name__ == "__main__":
main()

0 comments on commit ac006dc

Please sign in to comment.