aibangjuxin / groovy

study groovy
1 stars 0 forks source link

DNS #77

Open aibangjuxin opened 10 months ago

aibangjuxin commented 10 months ago

你可以使用以下命令向你的Cloud DNS中添加一条CNAME记录。请替换其中的YOUR_PROJECT_IDYOUR_ZONE_NAMEYOUR_RECORD_NAMEYOUR_TARGET_ADDRESS

gcloud dns record-sets transaction start --zone=YOUR_ZONE_NAME --project=YOUR_PROJECT_ID
gcloud dns record-sets transaction add --name="YOUR_RECORD_NAME" --type=CNAME --ttl=300 "YOUR_TARGET_ADDRESS" --zone=YOUR_ZONE_NAME --project=YOUR_PROJECT_ID
gcloud dns record-sets transaction execute --zone=YOUR_ZONE_NAME --project=YOUR_PROJECT_ID

这会在指定的Cloud DNS区域中添加一条CNAME记录。确保使用正确的项目ID、区域名称、记录名称和目标地址。

aibangjuxin commented 10 months ago

add cname

# Prepare to add CNAME record
add_record="*.run.app"
echo "$add_record"

# Check if the corresponding CNAME record already exists
cnamerecord=$(gcloud dns record-sets list --zone="${clouddns_zone}" --project="${project}" --filter="name:run.app" --format="csv[no-heading](NAME","DATA)")

if [ -z "$cnamerecord" ]; then
  echo "The record does not exist. Creating..."

  # Start DNS transaction
  gcloud dns record-sets transaction start --zone="${clouddns_zone}" --project="${project}"

  # Create CNAME record
  gcloud dns record-sets create "$add_record" --zone="${clouddns_zone}" --type="CNAME" --ttl="300" --rrdatas="restricted.googleapis.com." --project="${project}"

  # Execute DNS transaction
  gcloud dns record-sets transaction execute --zone="${clouddns_zone}" --project="${project}"

  echo "Print Record:"

  # List the created CNAME record
  gcloud dns record-sets list --zone="${clouddns_zone}" --project="${project}" --filter="name:run.app"
else
  echo "The record already exists. Listing it..."

  # List the existing CNAME record
  gcloud dns record-sets list --zone="${clouddns_zone}" --project="${project}" --filter="name:run.app" --format="csv[no-heading](NAME","DATA)"

  # Exit the script
  exit
fi
aibangjuxin commented 10 months ago

gcloud dns record-sets describe "$add_record" --zone="${clouddns_zone}" --project="${project}" --type="CNAME" 在这个命令中,"$add_record"是要查询的CNAME记录的名称,--zone="${clouddns_zone}"指定了区域,--project="${project}"指定了项目,--type="CNAME"指定了记录的类型为CNAME。

使用describe命令可以直接获取指定名称的CNAME记录的详细信息,而不需要通过list命令进行过滤。

Share Retry

aibangjuxin commented 10 months ago

Sure, here is a more professional version of the shell code:

#!/bin/bash

# Set variables
clouddns_zone="YOUR_CLOUDDNS_ZONE"
project="YOUR_PROJECT_ID"

# Prepare to add CNAME record
add_record="*.run.app"
echo "Preparing to add CNAME record: $add_record"

# Check if the corresponding CNAME record already exists
cnamerecord=$(gcloud dns record-sets list --zone="${clouddns_zone}" --project="${project}" --filter="name:run.app" --format="csv[no-heading](NAME","DATA)")

if [ -z "$cnamerecord" ]; then
  echo "The CNAME record does not exist. Creating it now..."

  # Start DNS transaction for grouping changes
  gcloud dns record-sets transaction start --zone="${clouddns_zone}" --project="${project}"

  # Create CNAME record with a TTL of 300 seconds
  gcloud dns record-sets create "$add_record" --zone="${clouddns_zone}" --type="CNAME" --ttl="300" --rrdatas="restricted.googleapis.com." --project="${project}"

  # Execute DNS transaction to apply changes
  gcloud dns record-sets transaction execute --zone="${clouddns_zone}" --project="${project}"

  echo "CNAME record created successfully!"

  # Display the newly created record
  gcloud dns record-sets list --zone="${clouddns_zone}" --project="${project}" --filter="name:run.app"

else
  echo "The CNAME record already exists. Details:"

  # Display the existing record
  gcloud dns record-sets list --zone="${clouddns_zone}" --project="${project}" --filter="name:run.app" --format="csv[no-heading](NAME","DATA)"

  # Exit the script since the record already exists
  exit
fi

echo "Process completed!"
echo "The CNAME record was created or updated successfully."

exit

This version uses more formal language and avoids slang or colloquialisms. It also includes more detailed comments to explain the purpose of each step, making the code more self-explanatory and easier to understand for other developers or technical users.

aibangjuxin commented 10 months ago

summary It for add a CNAME record for *.run.app. It checks if the corresponding CNAME record already exists in the specified Google Cloud DNS zone. If the record does not exist, it starts a DNS transaction, creates the CNAME record, and executes the transaction. Then it prints the created CNAME record. If the record already exists, it simply lists the existing CNAME record and exits the script. Finally, it prints a success message and exits.