Created
January 18, 2019 16:38
-
-
Save z4yx/5a05c11b1957b9b1bf84db35be0b0d46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf8 -*- | |
import os | |
import json | |
from aliyunsdkcore.client import AcsClient | |
from aliyunsdkcore.acs_exception.exceptions import ClientException | |
from aliyunsdkcore.acs_exception.exceptions import ServerException | |
from aliyunsdkalidns.request.v20150109 import DescribeDomainRecordsRequest,AddDomainRecordRequest,DescribeSubDomainRecordsRequest,UpdateDomainRecordRequest,DeleteDomainRecordRequest | |
# 创建 AcsClient 实例 | |
client = AcsClient( | |
"L***q", | |
"L***3", | |
"cn-hangzhou" | |
); | |
# 创建 request,并设置参数 | |
# request = DescribeDomainRecordsRequest.DescribeDomainRecordsRequest() | |
# request.set_DomainName('thu-skyworks.org') | |
# 发起 API 请求并打印返回 | |
# response = json.loads(client.do_action_with_exception(request)) | |
# print (response) | |
CERTBOT_DOMAIN=os.environ['CERTBOT_DOMAIN'] | |
CERTBOT_VALIDATION=os.environ['CERTBOT_VALIDATION'] | |
#parts = CERTBOT_DOMAIN.split('.', 1) | |
#assert len(parts)==2 | |
#assert len(parts[0])>0 and len(parts[1])>0 | |
#print("RR="+parts[0]) | |
#print("domain="+parts[1]) | |
domain_name='thu-skyworks.org' | |
rr="_acme-challenge."+CERTBOT_DOMAIN[0:-len(domain_name)] | |
rr=rr[0:-1] #remove tailing '.' | |
print("RR="+rr) | |
request = DescribeSubDomainRecordsRequest.DescribeSubDomainRecordsRequest() | |
request.set_SubDomain("_acme-challenge."+CERTBOT_DOMAIN) | |
request.set_Type('TXT') | |
response = json.loads(client.do_action_with_exception(request)) | |
print(response) | |
if response['TotalCount'] > 0: # update if exists | |
record_id = response['DomainRecords']['Record'][0]['RecordId'] | |
# print("Updating existing record({})...".format(record_id)) | |
# request = UpdateDomainRecordRequest.UpdateDomainRecordRequest() | |
# request.set_RecordId(record_id) | |
# request.set_RR("_acme-challenge."+parts[0]) | |
# request.set_Type('TXT') | |
# request.set_Value(CERTBOT_VALIDATION) | |
# response = json.loads(client.do_action_with_exception(request)) | |
print("Deleting existing record({})...".format(record_id)) | |
request = DeleteDomainRecordRequest.DeleteDomainRecordRequest() | |
request.set_RecordId(record_id) | |
response = json.loads(client.do_action_with_exception(request)) | |
print("Adding TXT record...") | |
request = AddDomainRecordRequest.AddDomainRecordRequest() | |
request.set_DomainName(domain_name) | |
request.set_RR(rr) | |
request.set_Type('TXT') | |
request.set_Value(CERTBOT_VALIDATION) | |
response = json.loads(client.do_action_with_exception(request)) | |
print(response) | |
# try: | |
# response = json.loads(client.do_action_with_exception(request)) | |
# print (response) | |
# except ServerException as e: | |
# if e.get_error_code() == 'DomainRecordDuplicate': | |
# # update if exists | |
# else: | |
# print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment