-
Notifications
You must be signed in to change notification settings - Fork 28
/
manual-hook.py
178 lines (140 loc) · 4.68 KB
/
manual-hook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
import sys
import os
import getopt
import time
if sys.version_info < (3,0):
import ConfigParser
else:
import configparser as ConfigParser
import aliyun
import logging
import logging.handlers
# Set the global configuration
CONFIG_FILENAME = 'config.ini'
configFilepath = os.path.split(os.path.realpath(__file__))[0] + os.path.sep + CONFIG_FILENAME
config = ConfigParser.ConfigParser()
config.read(configFilepath)
logger = logging.getLogger("logger")
formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s %(message)s")
consoleHandler = logging.StreamHandler(stream=sys.stdout)
logger.setLevel(logging.DEBUG)
consoleHandler.setLevel(logging.DEBUG)
consoleHandler.setFormatter(formatter)
logger.addHandler(consoleHandler)
fileLogFlag = True if config.get('log','enable').lower() == 'true' else False
if fileLogFlag:
logfile = config.get('log','logfile')
fileHandler = logging.FileHandler(filename=logfile)
fileHandler.setLevel(logging.DEBUG)
fileHandler.setFormatter(formatter)
logger.addHandler(fileHandler)
def getAliyunDnsInstance():
appid = config.get('aliyun', 'appid')
appsecret = config.get('aliyun', 'appsecret')
return aliyun.AliyunDns(appid, appsecret)
def auth():
try:
if 'CERTBOT_DOMAIN' not in os.environ:
raise Exception('Environment variable CERTBOT_DOMAIN is empty.')
if 'CERTBOT_VALIDATION' not in os.environ:
raise Exception('Environment variable CERTBOT_VALIDATION is empty.')
domain = os.environ['CERTBOT_DOMAIN']
value = os.environ['CERTBOT_VALIDATION']
logger.info('Start setting DNS')
logger.info('Domain:' + domain)
logger.info('Value:' + value)
aliyunDns = getAliyunDnsInstance()
# aliyunDns.toString()
# add letsencrypt domain record
aliyunDns.addLetsencryptDomainRecord(domain, value)
# wait for completion
logger.info('sleep 10 secs')
time.sleep(10)
logger.info('Success.')
logger.info('DNS setting end!')
except Exception as e:
logger.error(str(e))
sys.exit()
def cleanup():
try:
if 'CERTBOT_DOMAIN' not in os.environ:
raise Exception('Environment variable CERTBOT_DOMAIN is empty.')
domain = os.environ['CERTBOT_DOMAIN']
logger.info('Start to clean up')
logger.info('Domain:' + domain)
aliyunDns = getAliyunDnsInstance()
# aliyunDns.toString()
# delete letsencrypt domain record
aliyunDns.deleteLetsencryptDomainRecord(domain)
# wait for completion
time.sleep(10)
logger.info('Success.')
logger.info('Clean up end!')
except Exception as e:
logger.error(str(e))
sys.exit()
def usage():
def printOpt(opt, desc):
firstPartMaxLen = 30
firstPart = ' ' + ', '.join(opt)
secondPart = desc.replace('\n', '\n' + ' ' * firstPartMaxLen)
delim = ''
firstPartLen = len(firstPart)
if firstPartLen >= firstPartMaxLen:
spaceLen = firstPartMaxLen
delim = '\n'
else:
spaceLen = firstPartMaxLen - firstPartLen
delim = delim + ' ' * spaceLen
print(firstPart + delim + secondPart)
print('Usage: python %s [option] [arg] ...' % os.path.basename(__file__))
print('Options:')
printOpt(['-h', '--help'],
'Display help information.')
printOpt(['-v', '--version'],
'Display version information.')
printOpt(['--auth'],
'auth hook.')
printOpt(['--cleanup'],
'auth hook.')
def version():
print('dmlkdevtool.py ' + __version__)
print(__copyright__)
print('License ' + __license__ + '.')
print('Written by ' + __author__ + '.')
def main(argc, argv):
try:
if(argc == 1):
usage()
raise Exception('')
opts, args = getopt.getopt(
argv[1:],
'hv',
[
'help',
'version',
'auth',
'cleanup',
]
)
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
elif opt in ('-v', '--version'):
version()
elif opt in ('--auth'):
auth()
elif opt in ('--cleanup'):
cleanup()
else:
logger.error('Invalid option: ' + opt)
except getopt.GetoptError as e:
logger.error(str(e))
except AttributeError as e:
logger.error(e.args)
except Exception as e:
logger.error(str(e))
sys.exit()
if __name__ == '__main__':
main(len(sys.argv), sys.argv)