Created
April 21, 2015 03:07
-
-
Save feczo/eb4b279259751dddeca8 to your computer and use it in GitHub Desktop.
Automatically update your compute engine instance IP with its hostname in your public DNS zone
This file contains hidden or 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/python | |
import httplib2 | |
import urllib | |
import json | |
import sys | |
from apiclient.discovery import build | |
from oauth2client import client as oauth2_client | |
# PREREQUISITE | |
# apt-get update && apt-get -y install python-pip | |
# pip install --upgrade google-api-python-client | |
METADATA_SERVER = 'http://metadata/computeMetadata/v1/instance/service-accounts' | |
IP_META = 'http://metadata/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip' | |
HOST_META = 'http://metadata/computeMetadata/v1/instance/hostname' | |
SCOPE = 'https://www.googleapis.com/auth/ndev.clouddns.readwrite' | |
PROJECT_META = 'http://metadata/computeMetadata/v1/project/project-id' | |
SERVICE_ACCOUNT = 'default' | |
# === YOU NEED TO CHANGE THESE TWO === | |
DNS_ZONE_NAME = 'gecho' | |
DNS_DOMAIN_NAME = 'sub.eegecho.com' | |
def service_auth(): | |
http = httplib2.Http() | |
token_uri = '%s/%s/token' % (METADATA_SERVER, SERVICE_ACCOUNT) | |
resp, content = http.request(token_uri, method='GET', | |
body=None, | |
headers={'Metadata-Flavor': 'Google'}) | |
if resp.status == 200: | |
d = json.loads(content) | |
access_token = d['access_token'] # Save the access token | |
credentials = oauth2_client.AccessTokenCredentials(d['access_token'], | |
'my-user-agent/1.0') | |
return credentials | |
else: | |
print resp.status | |
def updateARecord(dns_service, zone_name, arecord, newvalue): | |
args = { | |
'project' : project, | |
'managedZone' : zone_name | |
} | |
newset = {} | |
response = dns_service.resourceRecordSets().list(**args).execute() | |
# we assume that this is an additon only | |
addition = { | |
"kind": "dns#resourceRecordSet", | |
"name": arecord, | |
"rrdatas": [newvalue], | |
"type": "A" | |
} | |
deletion = None | |
# however if we find the record to exist we populate the deletion and match the addition | |
for rrset in response['rrsets']: | |
if (rrset['type'] == 'A') and (rrset['name'] == arecord): | |
addition = dict(rrset) | |
deletion = dict(rrset) | |
addition['rrdatas'] = [newvalue] | |
# override the TTL | |
addition['ttl'] = 180 | |
body = { | |
'additions': [addition], | |
'deletions': [deletion] | |
} | |
args['body'] = body | |
response = dns_service.changes().create(**args).execute() | |
# print args | |
# print 'Record set created, result object: ' | |
# print json.dumps(response, | |
# sort_keys=True, | |
# indent=4, | |
# separators=(',', ': ')) | |
if __name__ == '__main__': | |
http = httplib2.Http() | |
resp, project = http.request(PROJECT_META, method='GET', body=None, headers={'Metadata-Flavor': 'Google'}) | |
credentials = service_auth() | |
dns_service = build('dns', 'v1', http=credentials.authorize(http)) | |
resp, myip = http.request(IP_META, method='GET', body=None, headers={'Metadata-Flavor': 'Google'}) | |
if resp.status == 200: | |
if resp.status == 200: | |
resp, myhostname = http.request(HOST_META, method='GET', body=None, headers={'Metadata-Flavor': 'Google'}) | |
myhostname = myhostname.split('.c.')[0] | |
updateARecord(dns_service, DNS_ZONE_NAME, '%s.%s.' % (myhostname, DNS_DOMAIN_NAME), myip) | |
else: | |
print resp.status | |
else: | |
print resp.status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment