Tuesday, September 6, 2016

Improving the user experience of hub

I can't remember things well,  So when possible rely tools (like bash completion) to help me complete or explore commands with linux so that I don't have to remember every commands 100+ options.

With hub, the same applies and you may not have bash-completion for this new tool. If you install it directly from the hub site. 

To help you solve that, you can do:
if [[ -f /usr/share/bash-completion/completions/hub ]]; then echo "hub bash-completion already installed"; else  sudo curl -o /usr/share/bash-completion/completions/hub https://raw.githubusercontent.com/github/hub/master/etc/hub.bash_completion.sh; fi
This relies on the bash-completion construct but its the simplest way to add completion commands to this new tool.
  • This also updates your "completion" commands for git so be aware that if you have not aliased "git > hub" your completion commands may not correctly display your available options.
It should also be noted that if your not installing the hub tooling from the project site directly (but instead using your OS's package manger), bash completion may already come with the software. Be sure to explore this (and reload your shell after installing). 

Simplifying your github workflow with "hub"

As a support engineering / software maintenance engineer, I spend a lot of time looking at code, reading code, searching code, etc. So needless to say I have a few git repos's sitting arround. 

In most cases, I use these repositories to help me understand errors, or issues that customers see when using a product or library, that I am supporting. However in some situations, the problems once identified are simple to fix, it becomes time for me to change hats and become an contributor.

In most cases contributing its a pain because it involves understanding parts of the SCM (git) tooking that can be difficult to understand (when your first getting started). Depending on the project, contributing, or the workflow in how you provide contributions to the project can be "challenging". Luckily services like github have arisen to make the sharing / hosting of OpenSource projects simple. They have also worked to make "contributing" to projects simple (however these efforts of go with out praise).

One such example is github's invention of the "pull request". While this is a source control concept for "git", however its workflow definition has fundamentally altered, how contributions to projects work because it defined tooling to unify and simplify the process of contributions.


One of the complications with the "pull request" is that it, with out "sevice tooling" (Github) you are more or less providing "patches" that have to be manualy managed. The biggest complication caues by this "sevice tooling" (Github) is that you have to use the "web ui" to create / submitt a PR.

Not any more. With "hub" you can remove this complication, and move back to the terminal.

To get started you will need to install the "hub tooling", in my case on fedora I can just run.
sudo yum install hub
I can then use the use hub like 'git'.
hub clone https://github.com/openshift/openshift-docs
cd openshift-docs/
Except now, I have new options that integrate directly with the github service.
hub fork
git remote -v
From here on out, most of the "contribution process" is likely the same as what you do in for any git project. Branch -> Modify - > Commit - > Push.
git checkout -b typos
< make changes > 
git add install_config/install/prerequisites.adoc install_config/install/disconnected_install.adoc
 

<confirm changes> 
git diff master
git status
git commit -m "fixing typos"

git push <GITHUB_USER> typos
With you changes now in your "fork" and "feature branch" you can switch back to hub to complete your PR and contribute to the project.
git pull-request
  •  Note: you need to ensure that you setup your github ssh key, or you set on your git configuration:

    git config --global hub.protocol https 

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. 








Monday, May 16, 2016

Learning how to type / spell (in weechat)

So I am REALLY not a good speller, and I spend a lot of time in front of a keyboard, and in applications that don't always have a good spell check mechanism. 

Lucky for me most do, and they are enabled by default. However some are not, and this makes me sad.

With that said, I took some time today to help remedy this issue, at least for my IRC client. As most of you know I moved to weechat some time ago, see http://sferich888.blogspot.com/2015/04/weechat-for-xchat-refugees.html for the basics of my configuration, and as such spell checking is not something weechat does by default.

This is because spelling is HARD, even for computers and applications. So weechat offloads the work to something that is good at it, aspell. As such you need to make sure your OS has the needed aspell dependencies installed, in order for this to work.

On my system this means:

# yum install aspell aspell-en
  OR
# dnf install aspell aspell-en
  • Note: This also installs the dictionaries that I use as an English speaker. 
From here all you need to do is start weechat and make a few configuration changes.  First of which is starting the built in integration component, and defining what language you want to use.
/aspell enable
### Example: /set aspell.check.default_dict <language>
/set aspell.check.default_dict en
With the Language and feature enabled, you should see spelling error get highlighted (in red), in your "input" bar.  You can change the color by setting aspell.color.misspelled to be a different value.

Its great an all, that the editor now shows me I have made a mistake, however it would be nice/better if it suggested, how to correct the mistake. For this you need to do 2 things. First enable the spelling suggestions in your "status" bar.
### Example: /set weechat.bar.status.items "<whatever was there>,[aspell_suggest]".
/set weechat.bar.status.items [time],buffer_number+: +buffer_name,[lag],completion,scroll,[aspell_suggest]
Next is to define how many suggestions you would like to see
/ set aspell.check.suggestions 3
Once this is complete, you should see, not only spelling errors, but suggestions (3 of them) in your status bar, show up so that you can correct them.

Be sure to save your configuration, or you might be forced to re-implement this every time you start weechat.  
/save



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 ...