Wednesday, November 15, 2023

SSH wrapping for autocompletion hostnames and domain

 intelligent ssh username and host completion


ssh u1106089
ssh u1106089.west.test.company.com
ssh p1106089
ssh p1106089.west.prod.company.com


Add this to your .zshrc or .bashrc and create the $HOME/sshwrap.sh and make that executable chmod 755 sshwrap.sh

ssh() {
if [[ "$0" == "ssh" ]]; then
_host=$1
shift
. ~/sshwrap.sh $_host $@
fi
}



The contents for sshwrap.sh

#!/bin/bash
_host="$1"
shift
if [[ $(echo "$_host" | grep "^p" | wc -l ) -eq 1 ]]
then
#xprod
if [[ $(echo "${_host}" | grep "prod.cloud.com" | wc -l ) -eq 1 ]]
then
_host="${_host}.prod.cloud.com"
fi
if [[ $(echo "$_host" | grep "^f751818" | wc -l) -eq 0 ]]
then
_host="ff751818@${_host}"
fi
echo "connecting production syste, $_host"
elif [[ $(echo "$_host" | grep "^u" | wc -l ) -eq 1 ]]
then
if [[ $(echo "$_host" | grep test.cloud.com | wc -l ) -eq 0 ]]
then
_host="${_host}.test.cloud.com"
fi
if [[ $(echo "$_host" | grep "^ff751818" | wc -l ) -eq 0 ]]
then
_host="ff751818@${_host}"
fi
echo "connecting test system, $_host"
elif [[ $(echo "$_host" | grep "^c" | wc -l) -eq 1 ]]
then
if [[ $(echo "$_host" | grep test.cloud.com | wc -l ) -eq 0 ]]
then
_host="${_host}.test.cloud.com"
fi
if [[ $(echo "$_host" | grep "^ak751818" | wc -l ) -eq 0 ]]
then
_host="ak751818@${_host}"
fi
echo "connecting test system on premises, $_host"
else
echo "other host definition $_host"
fi
echo "contacting $_host $@"
/usr/bin/ssh $_host $@



Tuesday, January 5, 2021

Friday, June 19, 2020

My minimalistic Atom StyleSheet.


/*
 * Your Stylesheet
 *
 * This stylesheet is loaded when Atom starts up and is reloaded automatically
 * when it is changed and saved.
 *
 * Add your own CSS or Less to fully customize Atom.
 * If you are unfamiliar with Less, you can read more about it here:
 * http://lesscss.org
 */


/*
 * Examples
 * (To see them, uncomment and save)
 */

// style the background color of the tree view
.tree-view {
  color: white;
  //background-color: black;
}
atom-pane-container atom-pane .item-views {
  border-width: 1px;
  border-color: gray;
  border-style: dashed;
}

.tab-bar .tab .close-icon {
  border-width: 1pt;
  border-color: white;
  border-style: solid;
}
.tab-bar .tab {
  border-width: 1pt;
  border-color: black;
  border-style: dashed;

}
.tab-bar .tab.active {
  border-width: 1px;
  border-color: gray;
  border-style: dashed;
}
// style the background and foreground colors on the atom-text-editor-element itself
atom-text-editor {
   //color: black;
   color: white;
   //background-color: white;
   //font-weight: bold;
   .gutter .line-number {
     //color: black
     color: white;
   }
//Background-color: lightgray;
}

// style UI elements inside atom-text-editor
atom-text-editor .cursor {
  // border-color: red;
}

Sunday, April 26, 2020

How to Patch Methods with mock for python


def test_command_line_gitlab_blocker( LOAD, monkeypatch, get_client, cicd_context, session_context):
    def return_value(self):
        return []
    def update_purge(self,mock):
        return
    with patch.object(GitlabBlocker, 'find_all_to_be_blocked_users',return_value):
        with patch.object(GitlabBlocker, 'update_purge_action_dt', update_purge):
            sys.argv =['gitlab_blocker.py', '--test','cycle','-f', 'my_test.cfg']
            runpy.run_module('gitlab_blocker', run_name='__main__')





class GitlabBlocker(object):
    
    def __init__(self, config, gitlab_client, test = 'cycle'):
    
    def _open(self):

    def find_all_to_be_blocked_users(self):

    def update_purge_action_dt(self, uuid):
        
    def blocking(self, no_Block_call=False):

Monday, March 9, 2020

saving of requests_cache objects as responses of requests in python, handy for mocking services.

def dump_sqlite_response_cache():
    import sqlite3
    con = sqlite3.connect('demo_cache.sqlite')
    con.row_factory = sqlite3.Row
    from requests_cache.backends.sqlite import DbCache
    import requests_cache
    requests_cache.install_cache('demo_cache')
    #c = BaseCache.remove_old_entries.get_cache()
    s = CachedSession()
    #print(s.__dict__)
    c = requests_cache.core.get_cache()
    #print(c)
    cur = con.cursor()
    cur.execute('select * from responses')
    for i in cur.fetchall():
        cc = i['key']
        ii=0
        with open('response%02d.json' % (ii), 'w+') as f:
            res = c.get_response_and_time(i['key'])
            r = res[0].json()
            f.write(json.dumps(r))
            f.write('\n')
            ii+=1

Friday, January 3, 2020

python2 EOL now upgrade to python3 as default

While other installation options are available, the easiest way to get started is with Homebrew:
$ brew install pyenv
🍺  /usr/local/Cellar/pyenv/1.2.10: 634 files, 2.4MB
Now let's install the latest Python version (3.7.3 as of this writing):
$ pyenv install 3.7.3
python-build: use openssl 1.0 from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.3.tar.xz...
-> https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz
Installing Python-3.7.3...
## further output not included ##
Now that Python 3 is installed through pyenv, we want to set it as our global default version for pyenv environments:
$ pyenv global 3.7.3
# and verify it worked
$ pyenv version 3.7.3
(set by /Users/Guest/.pyenv/version)
The power of pyenv comes from its control over our shell's path. In order for it to work correctly, we need to add the following to our configuration file (.zshrc for me, possibly .bash_profile for you):
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.zshrc
We also need to remove the aliases we used in the sections above since they would prevent using pyenv correctly. After removing them, we can confirm pyenv is managing our Python 3 version:
# I start by resetting the current shell
exec $0
which python
/Users/Guest/.pyenv/shims/python
$ python -V
Python 3.7.3
$ pip -V
pip 19.0.3 from /Users/Guest/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pip (python 3.7)
Now we know for certain that we're using Python 3.7.3 and pip will update alongside it without any manual aliasing between versions. Using Moshe's recommendation to use a version manager (pyenv) enables us to easily accept future upgrades without getting confused about which Python we are running at a given time.