fix line length; cleanup
This commit is contained in:
parent
cbbfa7bc9e
commit
567f1ec2ff
|
@ -20,8 +20,7 @@ def setup_logger():
|
||||||
ch = logging.StreamHandler()
|
ch = logging.StreamHandler()
|
||||||
ch.setLevel(logging.INFO)
|
ch.setLevel(logging.INFO)
|
||||||
|
|
||||||
formatter = logging.Formatter(
|
formatter = logging.Formatter('%(asctime)s - %(name)s- %(levelname)s - %(message)s')
|
||||||
'%(asctime)s - %(name)s- %(levelname)s - %(message)s')
|
|
||||||
ch.setFormatter(formatter)
|
ch.setFormatter(formatter)
|
||||||
# add handler to logger
|
# add handler to logger
|
||||||
logger.addHandler(ch)
|
logger.addHandler(ch)
|
||||||
|
@ -32,10 +31,7 @@ def setup_logger():
|
||||||
def load_config(logger):
|
def load_config(logger):
|
||||||
try:
|
try:
|
||||||
config_path = os.path.join(
|
config_path = os.path.join(
|
||||||
os.path.expanduser("~"),
|
os.path.expanduser("~"), ".certbot_dns_corenetworks", "config.ini")
|
||||||
".certbot_dns_corenetworks",
|
|
||||||
"config.ini"
|
|
||||||
)
|
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read(config_path)
|
config.read(config_path)
|
||||||
|
|
||||||
|
@ -47,7 +43,7 @@ def load_config(logger):
|
||||||
data["ZONE"] = config['DNS']['ZONE']
|
data["ZONE"] = config['DNS']['ZONE']
|
||||||
except KeyError, e:
|
except KeyError, e:
|
||||||
logger.error("Key %s not found in config" % (e))
|
logger.error("Key %s not found in config" % (e))
|
||||||
sys.exit(0)
|
sys.exit(1)
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
@ -65,7 +61,7 @@ def get_auth_token(user, passwd, host, logger):
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
except requests.exceptions.HTTPError, e:
|
except requests.exceptions.HTTPError, e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
sys.exit(0)
|
sys.exit(1)
|
||||||
|
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
@ -79,7 +75,35 @@ def get_zone(token, host, zone, logger):
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
except requests.exceptions.HTTPError, e:
|
except requests.exceptions.HTTPError, e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
sys.exit(0)
|
sys.exit(1)
|
||||||
|
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
def set_record(token, host, zone, logger):
|
||||||
|
url = urlparse.urljoin(host, os.path.join("dnszones", zone, "records/"))
|
||||||
|
headers = {"Authorization": "Bearer %s" % (token)}
|
||||||
|
|
||||||
|
try:
|
||||||
|
certbot_domain = "_acme-challenge.%s" % (os.environ['CERTBOT_DOMAIN'])
|
||||||
|
certbot_validation = os.environ['CERTBOT_VALIDATION']
|
||||||
|
except KeyError, e:
|
||||||
|
logger.error("Environment Variable %s not set" % (e))
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
data = {}
|
||||||
|
data["name"] = certbot_domain
|
||||||
|
data["ttl"] = 120
|
||||||
|
data["type"] = "TXT"
|
||||||
|
data["data"] = certbot_validation
|
||||||
|
json_data = json.dumps(data)
|
||||||
|
|
||||||
|
try:
|
||||||
|
r = requests.post(url, headers=headers, data=json_data)
|
||||||
|
r.raise_for_status()
|
||||||
|
except requests.exceptions.HTTPError, e:
|
||||||
|
logger.error(e)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
@ -92,8 +116,7 @@ def main():
|
||||||
|
|
||||||
# get auth token
|
# get auth token
|
||||||
logger.info("Try to get auth token")
|
logger.info("Try to get auth token")
|
||||||
r_token = get_auth_token(
|
r_token = get_auth_token(config["API_USER"], config["PASSWORD"], config["API_HOST"], logger)
|
||||||
config["API_USER"], config["PASSWORD"], config["API_HOST"], logger)
|
|
||||||
auth_token = r_token.json()["token"]
|
auth_token = r_token.json()["token"]
|
||||||
logger.info("Authentication successful")
|
logger.info("Authentication successful")
|
||||||
|
|
||||||
|
@ -105,9 +128,12 @@ def main():
|
||||||
if zone_active:
|
if zone_active:
|
||||||
logger.info("Zone '%s' verified (active)" % (zone_name))
|
logger.info("Zone '%s' verified (active)" % (zone_name))
|
||||||
else:
|
else:
|
||||||
logger.error(
|
logger.error("Zone '%s' verified (not active)" % (zone_name))
|
||||||
"Zone '%s' verified (not active)" % (zone_name))
|
sys.exit(1)
|
||||||
sys.exit(0)
|
|
||||||
|
# add txt record
|
||||||
|
z_record = set_record(auth_token, config["API_HOST"], zone_name, logger)
|
||||||
|
print z_record
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Reference in New Issue
Block a user