From e644ad8bf70b54e9926b627ed0b7928d589116cb Mon Sep 17 00:00:00 2001 From: winlin Date: Wed, 9 Aug 2023 12:41:25 +0800 Subject: [PATCH] Set nginx max body to 100GB. v1.0.303 --- DEVELOPER.md | 41 +++++++++---------- scripts/setup-aapanel/do_install.sh | 12 ++++++ scripts/setup-bt/do_install.sh | 12 ++++++ scripts/setup-droplet/scripts/01-srs.sh | 12 ++++++ scripts/setup-lighthouse/build.sh | 13 +++--- scripts/setup-lighthouse/setup_lighthouse.sh | 26 +++++++++--- scripts/tools/tencent-cloud/create-cvm.py | 13 ++++-- scripts/tools/tencent-cloud/create-image.py | 24 ++++++++--- ...{create-verify-cvm.py => create-verify.py} | 8 ++-- scripts/tools/tencent-cloud/query-cvm-ip.py | 8 ++-- scripts/tools/tencent-cloud/share-image.py | 13 +----- scripts/tools/tencent-cloud/tools.py | 6 ++- 12 files changed, 126 insertions(+), 62 deletions(-) rename scripts/tools/tencent-cloud/{create-verify-cvm.py => create-verify.py} (92%) diff --git a/DEVELOPER.md b/DEVELOPER.md index 800e9031..dfed9117 100644 --- a/DEVELOPER.md +++ b/DEVELOPER.md @@ -302,40 +302,37 @@ Create a CVM instance: ```bash rm -f /tmp/lh-*.txt && -VM_TOKEN=$(openssl rand -base64 16 | tr -dc 'a-zA-Z0-9' | head -c 16) && echo "$VM_TOKEN" >/tmp/lh-token.txt && -VM_TOKEN=$VM_TOKEN bash scripts/tools/tencent-cloud/helper.sh create-cvm.py 2>/tmp/lh-instance.txt && VM_INSTANCE=$(cat /tmp/lh-instance.txt) && -bash scripts/tools/tencent-cloud/helper.sh query-cvm-ip.py --instance $VM_INSTANCE 2>/tmp/lh-ip.txt && VM_IP=$(cat /tmp/lh-ip.txt) && -echo "Instance: $VM_INSTANCE, IP: ubuntu@$VM_IP" +echo $(openssl rand -base64 16 | tr -dc 'a-zA-Z0-9' | head -c 16) >/tmp/lh-token.txt && +VM_TOKEN=$(cat /tmp/lh-token.txt) bash scripts/tools/tencent-cloud/helper.sh create-cvm.py --id /tmp/lh-instance.txt +bash scripts/tools/tencent-cloud/helper.sh query-cvm-ip.py --instance $(cat /tmp/lh-instance.txt) --id /tmp/lh-ip.txt && +echo "Instance: $(cat /tmp/lh-instance.txt), IP: ubuntu@$(cat /tmp/lh-ip.txt), Password: $(cat /tmp/lh-token.txt)" && sleep 5 && +bash scripts/setup-lighthouse/build.sh --ip $(cat /tmp/lh-ip.txt) --os ubuntu --user ubuntu --password $(cat /tmp/lh-token.txt) && +bash scripts/tools/tencent-cloud/helper.sh create-image.py --instance $(cat /tmp/lh-instance.txt) --id /tmp/lh-image.txt && +bash scripts/tools/tencent-cloud/helper.sh share-image.py --image $(cat /tmp/lh-image.txt) && +echo "Image: $(cat /tmp/lh-image.txt) created and shared." && +bash scripts/tools/tencent-cloud/helper.sh remove-cvm.py --instance $(cat /tmp/lh-instance.txt) ``` -Run blueprint script: +Next, create a test CVM instance with the image: ```bash -VM_IP=$(cat /tmp/lh-ip.txt) && VM_TOKEN=$(cat /tmp/lh-token.txt) && VM_INSTANCE=$(cat /tmp/lh-instance.txt) && -bash scripts/setup-lighthouse/build.sh --ip $VM_IP --os ubuntu --user ubuntu --password $VM_TOKEN && -bash scripts/tools/tencent-cloud/helper.sh create-image.py --instance $VM_INSTANCE 2>/tmp/lh-image.txt && VM_IMAGE=$(cat /tmp/lh-image.txt) && -bash scripts/tools/tencent-cloud/helper.sh share-image.py --image $VM_IMAGE && -echo "Image: $VM_IMAGE created and shared." +echo $(openssl rand -base64 16 | tr -dc 'a-zA-Z0-9' | head -c 16) >/tmp/lh-token2.txt && +VM_TOKEN=$(cat /tmp/lh-token2.txt) bash scripts/tools/tencent-cloud/helper.sh create-verify.py --image $(cat /tmp/lh-image.txt) --id /tmp/lh-test.txt && +bash scripts/tools/tencent-cloud/helper.sh query-cvm-ip.py --instance $(cat /tmp/lh-test.txt) --id /tmp/lh-ip2.txt && +echo "IP: ubuntu@$(cat /tmp/lh-ip2.txt), Password: $(cat /tmp/lh-token2.txt)" && +echo "http://$(cat /tmp/lh-ip2.txt)" ``` -Next, create a test CVM instance with the image: +Verify then cleanup the test CVM instance: ```bash -TEST_TOKEN=$(openssl rand -base64 16 | tr -dc 'a-zA-Z0-9' | head -c 16) && VM_IMAGE=$(cat /tmp/lh-image.txt) && -VM_TOKEN=$TEST_TOKEN bash scripts/tools/tencent-cloud/helper.sh create-verify-cvm.py --image $VM_IMAGE 2>/tmp/lh-test.txt && TEST_INSTANCE=$(cat /tmp/lh-test.txt) && -bash scripts/tools/tencent-cloud/helper.sh query-cvm-ip.py --instance $TEST_INSTANCE 2>/tmp/lh-ip2.txt && TEST_IP=$(cat /tmp/lh-ip2.txt) && -echo "Instance: $TEST_INSTANCE, IP: ubuntu@$TEST_IP, Password: $TEST_TOKEN" && -echo "http://$TEST_IP" +bash scripts/tools/tencent-cloud/helper.sh remove-cvm.py --instance $(cat /tmp/lh-test.txt) ``` -Then run the script to remove all the CVM, disk images, and snapshots: +After publish to lighthouse, cleanup the CVM, disk images, and snapshots: ```bash -VM_INSTANCE=$(cat /tmp/lh-instance.txt) && VM_IMAGE=$(cat /tmp/lh-image.txt) && TEST_INSTANCE=$(cat /tmp/lh-test.txt) && -(bash scripts/tools/tencent-cloud/helper.sh remove-cvm.py --instance $TEST_INSTANCE || echo OK) && -(bash scripts/tools/tencent-cloud/helper.sh remove-cvm.py --instance $VM_INSTANCE || echo OK) && -(bash scripts/tools/tencent-cloud/helper.sh remove-image.py --image $VM_IMAGE || echo OK) && -echo "Cleanup Instance: $VM_INSTANCE, Image: $VM_IMAGE OK." +bash scripts/tools/tencent-cloud/helper.sh remove-image.py --image $(cat /tmp/lh-image.txt) ``` # Tips diff --git a/scripts/setup-aapanel/do_install.sh b/scripts/setup-aapanel/do_install.sh index aefe5f88..00cf9203 100644 --- a/scripts/setup-aapanel/do_install.sh +++ b/scripts/setup-aapanel/do_install.sh @@ -50,6 +50,18 @@ Install() { touch ${DATA_HOME}/config/nginx.server.conf if [[ $? -ne 0 ]]; then echo "Create /data/config failed"; exit 1; fi +# TODO: FIXME: Move to code. +echo "Start to setup nginx.http.conf" +if [[ -f ${DATA_HOME}/config/nginx.http.conf && -s ${DATA_HOME}/config/nginx.http.conf ]]; then + echo "The nginx.http.conf already exists, skip" +else + cat << END > ${DATA_HOME}/config/nginx.http.conf +# Limit for upload file size +client_max_body_size 100g; +END + if [[ $? -ne 0 ]]; then echo "Setup nginx.http.conf failed"; exit 1; fi +fi + # Allow network forwarding, required by docker. # See https://stackoverflow.com/a/41453306/17679565 echo "Controls IP packet forwarding" diff --git a/scripts/setup-bt/do_install.sh b/scripts/setup-bt/do_install.sh index aefe5f88..00cf9203 100644 --- a/scripts/setup-bt/do_install.sh +++ b/scripts/setup-bt/do_install.sh @@ -50,6 +50,18 @@ Install() { touch ${DATA_HOME}/config/nginx.server.conf if [[ $? -ne 0 ]]; then echo "Create /data/config failed"; exit 1; fi +# TODO: FIXME: Move to code. +echo "Start to setup nginx.http.conf" +if [[ -f ${DATA_HOME}/config/nginx.http.conf && -s ${DATA_HOME}/config/nginx.http.conf ]]; then + echo "The nginx.http.conf already exists, skip" +else + cat << END > ${DATA_HOME}/config/nginx.http.conf +# Limit for upload file size +client_max_body_size 100g; +END + if [[ $? -ne 0 ]]; then echo "Setup nginx.http.conf failed"; exit 1; fi +fi + # Allow network forwarding, required by docker. # See https://stackoverflow.com/a/41453306/17679565 echo "Controls IP packet forwarding" diff --git a/scripts/setup-droplet/scripts/01-srs.sh b/scripts/setup-droplet/scripts/01-srs.sh index d1840ded..15320d8f 100644 --- a/scripts/setup-droplet/scripts/01-srs.sh +++ b/scripts/setup-droplet/scripts/01-srs.sh @@ -37,6 +37,18 @@ touch ${DATA_HOME}/config/nginx.server.conf if [[ $? -ne 0 ]]; then echo "Create /data/config failed"; exit 1; fi echo "Create data and config files ok" +# TODO: FIXME: Move to code. +echo "Start to setup nginx.http.conf" +if [[ -f ${DATA_HOME}/config/nginx.http.conf && -s ${DATA_HOME}/config/nginx.http.conf ]]; then + echo "The nginx.http.conf already exists, skip" +else + cat << END > ${DATA_HOME}/config/nginx.http.conf +# Limit for upload file size +client_max_body_size 100g; +END + if [[ $? -ne 0 ]]; then echo "Setup nginx.http.conf failed"; exit 1; fi +fi + # Setup the nginx configuration. rm -f /etc/nginx/nginx.conf && cp ${SOURCE}/platform/containers/conf/nginx.conf /etc/nginx/nginx.conf && diff --git a/scripts/setup-lighthouse/build.sh b/scripts/setup-lighthouse/build.sh index 1a3e9284..99a7a85d 100644 --- a/scripts/setup-lighthouse/build.sh +++ b/scripts/setup-lighthouse/build.sh @@ -47,9 +47,8 @@ echo "SOURCE=$SOURCE, ip=$ip, os=$os, user=$user, password=${#password}B, cleanu sshCmd="sshpass -p $password ssh -o StrictHostKeyChecking=no" scpCmd="sshpass -p $password scp -o StrictHostKeyChecking=no" -$sshCmd -t $user@$ip "hostname" >/dev/null 2>/dev/null && -echo "Check sshpass ok" -if [[ $? -ne 0 ]]; then echo "Check sshpass failed"; echo "For mac to install sshpass, see https://stackoverflow.com/a/32258393/17679565"; exit 1; fi +$sshCmd -t $user@$ip "hostname" && echo "Check sshpass ok" +if [[ $ret -ne 0 ]]; then echo "Check sshpass failed"; echo "See https://stackoverflow.com/a/32258393/17679565"; exit 1; fi SRS_HOME=/tmp/lighthouse/srs-cloud && rm -rf $(dirname $SRS_HOME) && mkdir -p $SRS_HOME && @@ -66,8 +65,8 @@ cp ${SOURCE}/mgmt/bootstrap ${SRS_HOME}/mgmt/bootstrap && cp ${SOURCE}/platform/containers/conf/nginx.conf ${SRS_HOME}/platform/containers/conf/nginx.conf if [[ $? -ne 0 ]]; then echo "Copy srs-cloud failed"; exit 1; fi -tgzName=/tmp/lighthouse/srs-cloud.tar.bz2 && -(cd $(dirname $tgzName) && rm -f $tgzName && tar jcf $tgzName $(basename $SRS_HOME)) && +tgzName=/tmp/lighthouse/srs-cloud.zip && +(cd $(dirname $tgzName) && rm -f $tgzName && zip -q -r $tgzName $(basename $SRS_HOME)) && echo "Package $tgzName ok" && ls -lh $tgzName if [[ $? -ne 0 ]]; then echo "Package $tgzName failed"; exit 1; fi @@ -78,11 +77,11 @@ echo "Copy $tgzName to $ip ok" tgzFile=$(basename $tgzName) && SRS_NAME=$(basename $SRS_HOME) && echo "Run command on server: $ip" && -echo " tar xf $tgzFile" +echo " unzip -q $tgzFile" echo " bash ~/$SRS_NAME/scripts/setup-lighthouse/setup_lighthouse.sh" $sshCmd -t $user@$ip " - rm -rf $SRS_NAME && tar xf $tgzFile && \ + rm -rf $SRS_NAME && unzip -q $tgzFile && \ sudo bash $SRS_NAME/scripts/setup-lighthouse/setup_lighthouse.sh && if [[ $cleanup == yes ]]; then sudo bash $SRS_NAME/scripts/setup-lighthouse/post_build.sh && diff --git a/scripts/setup-lighthouse/setup_lighthouse.sh b/scripts/setup-lighthouse/setup_lighthouse.sh index fec22406..e393c2da 100755 --- a/scripts/setup-lighthouse/setup_lighthouse.sh +++ b/scripts/setup-lighthouse/setup_lighthouse.sh @@ -62,11 +62,15 @@ if [[ ! -d ~lighthouse ]]; then fi ######################################################################################################################## -# Install depends services. -apt-get update -y && -apt-get install -y git gcc g++ gdb make tree dstat docker docker.io nginx curl net-tools && -apt-get -qqy clean -if [[ $? -ne 0 ]]; then echo "Install dependencies failed"; exit 1; fi +# Install depends services. Retry because apt-get might be busy. +for ((i=0; i<3; i++)); do + apt-get update -y && + apt-get install -y git gcc g++ gdb make tree dstat docker docker.io nginx curl net-tools && + apt-get -qqy clean + ret=$?; if [[ $ret -eq 0 ]]; then break; fi + sleep 5; +done +if [[ $ret -ne 0 ]]; then echo "Install dependencies failed"; exit 1; fi echo "Enable service" && systemctl enable docker nginx && @@ -99,6 +103,18 @@ touch ${DATA_HOME}/config/nginx.server.conf if [[ $? -ne 0 ]]; then echo "Create /data/config failed"; exit 1; fi echo "Create data and config files ok" +# TODO: FIXME: Move to code. +echo "Start to setup nginx.http.conf" +if [[ -f ${DATA_HOME}/config/nginx.http.conf && -s ${DATA_HOME}/config/nginx.http.conf ]]; then + echo "The nginx.http.conf already exists, skip" +else + cat << END > ${DATA_HOME}/config/nginx.http.conf +# Limit for upload file size +client_max_body_size 100g; +END + if [[ $? -ne 0 ]]; then echo "Setup nginx.http.conf failed"; exit 1; fi +fi + # Setup the nginx configuration. rm -f /etc/nginx/nginx.conf && cp ${SOURCE}/platform/containers/conf/nginx.conf /etc/nginx/nginx.conf && diff --git a/scripts/tools/tencent-cloud/create-cvm.py b/scripts/tools/tencent-cloud/create-cvm.py index 9961272a..b05bc6c9 100644 --- a/scripts/tools/tencent-cloud/create-cvm.py +++ b/scripts/tools/tencent-cloud/create-cvm.py @@ -1,5 +1,10 @@ #coding: utf-8 -import dotenv, os, time, sys, tools +import dotenv, os, time, sys, tools, argparse + +parser = argparse.ArgumentParser(description="TencentCloud") +parser.add_argument("--id", type=str, required=False, help="Write ID result to this file") + +args = parser.parse_args() if os.path.exists(f'{os.getenv("HOME")}/.lighthouse/.env'): dotenv.load_dotenv(dotenv.find_dotenv(filename=f'{os.getenv("HOME")}/.lighthouse/.env')) @@ -16,6 +21,7 @@ exit(1) region = "ap-beijing" +print(f"Run with region={region}, id={args.id}") images = tools.get_images(region, "Ubuntu")['ImageSet'] image = None @@ -71,5 +77,6 @@ instance_detail = instance_details[0] print(f"Instance {instance_id}, public ip={instance_detail['PublicIpAddresses'][0]}, private ip={instance_detail['PrivateIpAddresses'][0]}") -# print the instance id to stderr. -print(instance_id, file=sys.stderr) +if args.id != None: + with open(args.id, 'w') as f: + print(instance_id, file=f) diff --git a/scripts/tools/tencent-cloud/create-image.py b/scripts/tools/tencent-cloud/create-image.py index 4d7a2308..a076bbef 100644 --- a/scripts/tools/tencent-cloud/create-image.py +++ b/scripts/tools/tencent-cloud/create-image.py @@ -1,8 +1,9 @@ #coding: utf-8 -import dotenv, os, tools, argparse, sys +import dotenv, os, tools, argparse, sys, time, datetime parser = argparse.ArgumentParser(description="TencentCloud") parser.add_argument("--instance", type=str, required=False, help="The CVM instance id") +parser.add_argument("--id", type=str, required=False, help="Write ID result to this file") args = parser.parse_args() @@ -26,11 +27,24 @@ region = "ap-beijing" image_name = "srs" instance_id = args.instance -print(f"Create CVM instance={instance_id}, region={region}, image={image_name}") +image_desc = f"{image_name} from {instance_id} at {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}" +print(f"Create CVM instance={instance_id}, region={region}, image={image_name}, desc={image_desc}, id={args.id}") -r0 = tools.create_image(region, instance_id, image_name) +r0 = tools.create_image(region, instance_id, image_name, image_desc) image_id = r0['ImageId'] print(f"Image {image_name} created id={image_id}") -# print the instance public ip to stderr. -print(image_id, file=sys.stderr) +while True: + info = tools.query_image(region, image_id)['ImageSet'] + if len(info) != 1: + raise Exception(f"Image {image_id} not found") + + if info[0]['ImageState'] == 'NORMAL': + break + + print(f"Image {image_id} state is {info[0]['ImageState']}, wait 5 seconds") + time.sleep(5) + +if args.id != None: + with open(args.id, 'w') as f: + print(image_id, file=f) diff --git a/scripts/tools/tencent-cloud/create-verify-cvm.py b/scripts/tools/tencent-cloud/create-verify.py similarity index 92% rename from scripts/tools/tencent-cloud/create-verify-cvm.py rename to scripts/tools/tencent-cloud/create-verify.py index ae34cea7..7396f3e7 100644 --- a/scripts/tools/tencent-cloud/create-verify-cvm.py +++ b/scripts/tools/tencent-cloud/create-verify.py @@ -3,6 +3,7 @@ parser = argparse.ArgumentParser(description="TencentCloud") parser.add_argument("--image", type=str, required=False, help="The CVM image id") +parser.add_argument("--id", type=str, required=False, help="Write ID result to this file") args = parser.parse_args() @@ -28,7 +29,7 @@ region = "ap-beijing" image_id = args.image -print(f"Select image: {image_id}, {region}") +print(f"Select image: {image_id}, {region}, id={args.id}") instance_quotas = tools.get_zone_instance(region)['InstanceTypeQuotaSet'] if len(instance_quotas) == 0: @@ -72,5 +73,6 @@ instance_detail = instance_details[0] print(f"Instance {instance_id}, public ip={instance_detail['PublicIpAddresses'][0]}, private ip={instance_detail['PrivateIpAddresses'][0]}") -# print the instance id to stderr. -print(instance_id, file=sys.stderr) +if args.id != None: + with open(args.id, 'w') as f: + print(instance_id, file=f) diff --git a/scripts/tools/tencent-cloud/query-cvm-ip.py b/scripts/tools/tencent-cloud/query-cvm-ip.py index 11083f9a..7f4a0aaa 100644 --- a/scripts/tools/tencent-cloud/query-cvm-ip.py +++ b/scripts/tools/tencent-cloud/query-cvm-ip.py @@ -3,6 +3,7 @@ parser = argparse.ArgumentParser(description="TencentCloud") parser.add_argument("--instance", type=str, required=False, help="The CVM instance id") +parser.add_argument("--id", type=str, required=False, help="Write ID result to this file") args = parser.parse_args() @@ -25,7 +26,7 @@ region = "ap-beijing" instance_id = args.instance -print(f"Query CVM instance={instance_id}, region={region}") +print(f"Query CVM instance={instance_id}, region={region}, id={args.id}") instance_details = tools.query_instance_detail(region, instance_id)['InstanceSet'] if len(instance_details) != 1: @@ -35,5 +36,6 @@ private_ip = instance_detail['PrivateIpAddresses'][0] print(f"Instance {instance_id}, public ip={public_ip}, private ip={private_ip}") -# print the instance public ip to stderr. -print(public_ip, file=sys.stderr) +if args.id != None: + with open(args.id, 'w') as f: + print(public_ip, file=f) diff --git a/scripts/tools/tencent-cloud/share-image.py b/scripts/tools/tencent-cloud/share-image.py index 951c2f49..9b6b390d 100644 --- a/scripts/tools/tencent-cloud/share-image.py +++ b/scripts/tools/tencent-cloud/share-image.py @@ -1,5 +1,5 @@ #coding: utf-8 -import dotenv, os, tools, argparse, time +import dotenv, os, tools, argparse parser = argparse.ArgumentParser(description="TencentCloud") parser.add_argument("--image", type=str, required=False, help="The CVM image id") @@ -31,16 +31,5 @@ account_id = os.getenv("LH_ACCOUNT") print(f"Share image id={image_id}, region={region} to account={account_id}") -while True: - info = tools.query_image(region, image_id)['ImageSet'] - if len(info) != 1: - raise Exception(f"Image {image_id} not found") - - if info[0]['ImageState'] == 'NORMAL': - break - - print(f"Image {image_id} state is {info[0]['ImageState']}, wait 5 seconds") - time.sleep(5) - tools.share_image(region, image_id, account_id) print(f"Image {image_id} shared to account {account_id}") diff --git a/scripts/tools/tencent-cloud/tools.py b/scripts/tools/tencent-cloud/tools.py index c3359bfc..db17f28a 100644 --- a/scripts/tools/tencent-cloud/tools.py +++ b/scripts/tools/tencent-cloud/tools.py @@ -335,7 +335,7 @@ def delete_snapshot_and_image(region, snapshot_id): resp = client.DeleteSnapshots(req) return json.loads(resp.to_json_string()) -def create_image(region, instance_id, image_name): +def create_image(region, instance_id, image_name, image_desc): cred = credential.Credential(os.getenv("SECRET_ID"), os.getenv("SECRET_KEY")) httpProfile = HttpProfile() httpProfile.endpoint = "cvm.tencentcloudapi.com" @@ -347,7 +347,9 @@ def create_image(region, instance_id, image_name): req = cvm_models.CreateImageRequest() params = { "InstanceId": instance_id, - "ImageName": image_name + "ImageName": image_name, + "ImageDescription": image_desc, + "ForcePoweroff": "TRUE" } req.from_json_string(json.dumps(params))