Skip to content

Instantly share code, notes, and snippets.

@hroncok
Last active January 24, 2021 13:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hroncok/166305c39fd8e80609d1 to your computer and use it in GitHub Desktop.
Save hroncok/166305c39fd8e80609d1 to your computer and use it in GitHub Desktop.
OpenShift WEDOS domain updater
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
OpenShift WEDOS domain updater
==============================
This script makes sure that your main domain (example.org) resolves to the
same IP address as your WWW domain (www.example.org). When you run your
webapp on OpenShift, you should set CNAME DNS records, however, this is not
possible for main domains, such as example.org without a subdomain.
Therefor, you have to set an A record for your main domain, but the IP
addresses of OpenShift apps may change (and they do). Assuming your
www.example.org subdomain has a correct CNAME record, it resolves to the
correct IP address.
When example.org and www.example.org do not resolve to the same address,
this script uses WEDOS API to chnage the A record to IP address of your app.
Set up your WEDOS API (Czech): http://kb.wedos.com/wapi/aktivace-nastaveni.html
And set your credentials few lines bellow. Then run this daily.
This was tested on Python 3 only and requires requests.
Copyright (c) 2015 Miro Hrončok <miro@hroncok.cz>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
Provided license texts might have their own copyrights and restrictions
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import datetime
import hashlib
import json
import socket
import requests
# Change these!
LOGIN = 'you@foobar.tld'
PASSWORD = 'passw0rd'
DOMAIN = 'foobar.tld'
# Keep this
API = 'https://api.wedos.com/wapi/json'
def get_auth(login=LOGIN, password=PASSWORD):
passhash = hashlib.sha1(password.encode('utf8')).hexdigest()
phrase = login + passhash + datetime.datetime.now().strftime('%H')
return hashlib.sha1(phrase.encode('utf8')).hexdigest()
def request(command, data, login=LOGIN, auth=get_auth(), url=API):
d = {'request': {'user': login, 'auth': auth, 'command': command, 'data': data}}
r = requests.post(url, data={'request': json.dumps(d)})
data = r.json()
if data['response']['result'] != 'OK':
raise Exception('Response from WEDOS was {}'.format(data['response']['result']))
return data
def dns_rows_list(domain=DOMAIN):
return request('dns-rows-list', locals())
def dns_row_update(row_id, ttl, rdata, domain=DOMAIN):
return request('dns-row-update', locals())
def dns_domain_commit(name=DOMAIN):
return request('dns-domain-commit', locals())
def find_A():
data = dns_rows_list()
for row in data['response']['data']['row']:
if row['rdtype'] == 'A' and row['name'] == '':
return row['ID'], row['ttl']
raise Exception('Cannot find A record for the domain')
def update_A(ip, domain=DOMAIN):
dns_row_update(*find_A(), rdata=ip)
dns_domain_commit()
print('Updated {} to {}'.format(domain, ip))
def update_A_as_www(domain=DOMAIN):
ip = socket.gethostbyname(domain)
wwwip = socket.gethostbyname('www.' + domain)
if ip != wwwip:
update_A(wwwip, domain)
else:
print('IP addresses match')
update_A_as_www()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment