Friday, June 17, 2016

Implementing Dynamic DNS in OpenShift v3 (with Python and Free IPA)

For a few days, now I have been trying to think of a way that I could, remove the wild card DNS requirement from OpenShift v3.

In the v2 we had the capability / functionality dynamically update DNS servers, when applications were created. So naturally I went looking for a way to also do this.

It turns out it is possible to monitor the routes API in OpenShift v3 and with the information provided, by the events stream, then update a Dynamic DNS service like IPA.




Using Python and "requests" to access the Free IPA API

For a few days, not I have needed a dynamic way to create DNS entries, or Host on my DNS/IDM provider.

So I set out on a quest to see if I could use python and the Free IPA API, to add dynamically created hosts (in say a cloud environment or IaaS platform), and update DNS or Host Records.

In my search I found a good article by Alexander Bokovoy that gave me the information I needed, to get started, and complete my desired goal.

Below is a sample of what I needed. 
#!/bin/python

#import http-parser 
import requests
import json

ipaurl="https://idm.example.com/ipa/"

session = requests.Session() 

resp = session.post('{0}session/login_password'.format(ipaurl),
   params="", data = {'user':'certadmin','password':'redhat'}, verify=False, 
   headers={'Content-Type':'application/x-www-form-urlencoded', 'Accept':'applicaton/json'})

header={'referer': ipaurl, 'Content-Type':'application/json', 'Accept':'application/json'}

create_host = session.post('{0}session/json'.format(ipaurl), headers=header,
    data=json.dumps({'id': 0, 'method': 'host_add', 
        'params': [[event['object']['spec']['host']], 
            {'force': True, 'ip_address': 192.168.1.101}]}), verify=False) 

print "    Host Create Return Code: {0}".format(create_host.status_code) 
 
This should create a host, entry in your IPA server, and set its IP address. As such allowing you to query the IPA server for DNS and resolve the proper IP of the host name that is created.

Using Python and "requests" to monitor and read the OpenShift API.

As part of an effort to automate what I do with OpenShift, I had to learn how to take advantage of OpenShift's "watch" QueryParameter on some of its API's.

As I typically work with python I quickly learned, that watching streams is not so easy. That said, it can be done.

Below is an example of how I use this method to watch the routes API in openshift, and print out, the events, that happen in the "joe" namespace.
#!/bin/python

import requests
import json

s = requests.Session()

token = "lTBiDnvYlHhuOl3C9Tj_Mb-FvL0hcMMONIua0E0D5CE"
openshift_api="https://master.openshift.example.com:8443"
namespace="joe"

def streaming():
    req = requests.Request("GET",'{0}/oapi/v1/namespaces/{1}/routes?watch=true'.format(openshift_api, namespace),
                           headers={'Authorization': 'Bearer {0}'.format(token)},
                           params="").prepare()

    resp = s.send(req, stream=True, verify=False)
    print resp.status_code

    for line in resp.iter_lines():
        if line:
            yield line


def read_stream():

    for line in streaming():
        event = {}
        try: 
            event = json.loads(line)

            if event['type']:
                print event 
 
        except Exception as e: 
            print e
            continue

read_stream() 

Thursday, June 9, 2016

The copy, paste road block


So I spend a lot of time in my text editor. Like a lot of time. I use it for writing, code, notes, or well everything. So that often means that I need to open a file for review, copy something and paste it into a new location.

For most folks this would not be an issue (and to be honest for most of my life it was not an issue for me either). I would just use my mouse, select the text I need/want and a few key strokes later, badabing badaboom.

However as I have begun using a terminal more and more. I have been finding more and more instances, when I have to capture multiple lines from a file (more lines than, my terminal has). For a vim user, where the clipboard is not compiled into binary I uses because I  run a distribution of Linux (Fedora) that just so happens to not build vim with clipboard support.
$ --> vim --version | grep -Eo [-+]clipboard
-clipboard

I have an extra challenge for copy, and pasting content from one source to another.

Luckily for me I found a tool (xclip) some time ago, for taking output from commands and allowing me to move it to my clipboard.

Using this tool and keybinding in my .vimrc, I can quickly solve my copy, past woes.

To do this you simply need to add the following keybinding.
vmap y ":w !xclip -sel clip
What this maps to is:
:'<,'>w !xclip -sel clip
Which if we break this down, states; take the selected area, and write the buffer and send it to the command (xclip, that selects the clipboard as its storage location).

This will allow you to select blocks of text from with in your text editor and move then to your clipboard (using xclip), with having clipboard support built into the editor. 








Its the little things (like opening a web browser)

Sometimes as your developing a solution/script for a problem your faced with interesting challenges where the dumbest workaround (opening a ...