Skip to content

Instantly share code, notes, and snippets.

View hartsock's full-sized avatar

Shawn Hartsock hartsock

View GitHub Profile
@hartsock
hartsock / debug_http.py
Created July 7, 2014 19:32
A snippet of code that shows how to turn on debugging for the Python HTTP client library
import requests
import logging
# These two lines enable debugging at httplib level (requests->urllib3->http.client)
# You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# The only thing missing will be the response.body which is not logged.
try:
import http.client as http_client
except ImportError:
# Python 2
@hartsock
hartsock / Jenkinsfile
Created May 15, 2019 17:48
A Jenkins Declarative Pipeline with parallel execution and coordination based on actions in each parallel node.
/**
* Problem:
* I have some action occurring on several parallel nodes. The action may have a diffrent result on each node non-deterministically.
* I need to act from the master depending on the result at each node.
*
* For a contrived example, we record the name of each node used to act on the parallel actions specified in this pipeline. The
* names are collected in a global string and a global `Map<String,String>` object for use by the master.
*
*/
value = ':'
@hartsock
hartsock / repeat.sh
Created October 4, 2022 13:50
Repeat the rest of the CLI 30 times. Example: `repeat.sh echo "foo"`
#!/usr/bin/env bash
STOP=30
i=0;
while [ $i -le $STOP ]
do
i=$((i+1))
echo -n "${i} :"
exec "$@"&
echo
@hartsock
hartsock / upgrade-photon.yml
Created October 22, 2019 17:38
An Ansible playbook for upgrading VMware Photon OS 2.x to 3.x
---
- hosts: photon
remote_user: root
tasks:
- name: pip prerequisite
command: bash -c "tdnf -y install python-pip"
- name: python prerequisites
command: bash -c "pip install -I pexpect"
@hartsock
hartsock / .profile
Created October 17, 2018 13:14
Shawn's Bash Profile
if [ -d $HOME/bin ]; then export PATH=$HOME/bin:$PATH ; fi
# interactive only below this point
if [ -t 1 ]; then
export SOURCE_IP=`echo $SSH_CONNECTION | awk '{print $1}'`
export CURRENT_IP=`echo $SSH_CONNECTION | awk '{print $3}'`
if [[ ! -z "${CURRENT_IP}" ]]; then
export PS1="${CURRENT_IP}:\w $ "
fi
echo ""
fi
@hartsock
hartsock / mirrorWSDL.groovy
Created September 20, 2012 15:39
mirror a WSDL from a server using Groovy
File mirrorWsdl(URL firstUrl, File dest) {
assert firstUrl.getText(), "Can't read from ${firstUrl} "
assert dest.mkdir() || dest.exists() && dest.isDirectory(), "Can't read/write to directory ${dest}"
def file = firstUrl.getFile()
def parentPath = file.substring(0,file.lastIndexOf('/') + 1)
def files = [:]
def urls = [firstUrl]
while(urls) {
URL url = urls.pop()
def path = url.getFile()
@hartsock
hartsock / vmware_cache_wsdl.py
Last active March 16, 2022 23:04
A tool to help you create a local cache of vSphere WSDLs. This tool is only necessary if you are running vSphere 4.1 or 5.0 and need to fix the malformed XML in the WSDL for your local python parser. This script will fill in the missing XML files and create a cache that the OpenStack nova-compute driver for vSphere can use. The last line of the …
#!/usr/bin/python
import collections
import optparse
import os
import sys
import urllib
ROOT_WSDL_FILE = 'vimService.wsdl'
WSDL_FILES = frozenset([
@hartsock
hartsock / datacenters_listing.py
Created December 3, 2014 23:31
Get all the datacenters in a vCenter using pyVmomi
from __future__ import print_function
from pyVim import connect
from pyVmomi import vim
si = connect.SmartConnect(host='vcsa', user='my_user', pwd='my_password')
content = si.RetrieveContent()
# A list comprehension of all the root folder's first tier children...
datacenters = [entity for entity in content.rootFolder.childEntity
@hartsock
hartsock / find_virtual_machine_by_datastore_path.py
Created December 4, 2014 00:23
Find a virtual machine by it's datastore path
si = connect.SmartConnect(host='vcsa', user='my_user', pwd='my_password')
content = si.RetrieveContent()
datacenters = [entity for entity in content.rootFolder.childEntity
if hasattr(entity, 'vmFolder')]
dc = None
for datacenter in datacenters:
if datacenter.name == 'my_datacenter':
dc = datacenter
@hartsock
hartsock / test_vm_to_mac.py
Created April 13, 2015 15:09
This is a quick and dirty sample that shows how to go from a VM's name to a mac address by using the inventory path (more robust than a simple name)
def test_vm_to_mac(self):
si = connect.SmartConnect(host='vcsa',
user='my_user',
pwd='my_password')
content = si.RetrieveContent()
# where the name of the VM is 'box'
vm = content.searchIndex.FindByInventoryPath("Datacenter0/vm/box")
self.assertEqual(vm.name, 'box')
nics = [dev for dev in vm.config.hardware.device
if isinstance(dev, vim.vm.device.VirtualEthernetCard)]