Skip to content

Instantly share code, notes, and snippets.

View garymacindoe's full-sized avatar

Gary Macindoe garymacindoe

View GitHub Profile
@garymacindoe
garymacindoe / assume-role.sh
Created September 3, 2019 10:16
Wraps `aws sts assume-role` to launch commands in a modified environment so that it can be used with `wormhole`.
#!/bin/sh
die() {
printf '%s\n' "${1}" >&2
exit 1
}
ROLE_ARN=""
ROLE_SESSION_NAME="dev-${USER}"
EXTERNAL_ID=""
@garymacindoe
garymacindoe / redshift.conf.d
Last active June 30, 2021 10:22
OpenRC Initscripts for Redshift
# /etc/conf.d/redshift
DISPLAY="${DISPLAY:-:0}"
# Make it a function in case we have to repeat it in init script later
set_xauth() {
# common case (works in almost all tested environments (except of lightdm)):
#XAUTHORITY="$(ps wwax -C X,Xorg -o args= --sort=-stime | grep -m 1 -o '\B[-]auth\s*/var\S*' | cut -d ' ' -f 2)"
@garymacindoe
garymacindoe / script.sh
Created November 25, 2018 19:24
How to redirect stdout and stderr to log files in a Bash script
#!/bin/bash
script=$(basename ${0})
exec > >(tee -ia /var/log/${script}_out.log) # Redirect stdout to logfile and continue to print on stdout
exec 2> >(tee -ia /root/${script}_err.log >&2) # Redirect stderr to logfile and continue to print of stderr
echo stdout # Appears on stdout and in logfile
echo stderr >&2 # Appears on stderr and in logfile
@garymacindoe
garymacindoe / change_vertex_id.groovy
Created November 5, 2018 10:08
Query to change the ID of a vertex using Gremlin
g.V()
// Find (single) vertex to change
.as('old')
.addV(select('old').label())
.as('new')
.property(id, new_id) // Insert new ID for vertex here
.sideEffect(
select('old')
@garymacindoe
garymacindoe / checkout.sh
Created April 5, 2018 15:19
Checkout a team's projects in SVN
#!/bin/bash
REPO_BASE="https://svn.repo/department/team"
LOCAL_BASE="${HOME}/svn"
backoff() {
local count iteration timeout output ret
timeout=1
count=5
iteration=0
@garymacindoe
garymacindoe / with-backoff
Created February 1, 2018 17:02
Run a command with exponential backoff, stopping when the command exits with a certain status and (optionally) outputs a specified string
#!/bin/bash
args=$(getopt n:t:r:o: ${*})
if [ $? -ne 0 ] || [ $# -lt 1 ]
then
echo "Usage: ${0} [-n=<max-attempts>] [-t=<initial-timeout>] [-r=0] [-o=<output>] <command> <args...>"
echo "Runs <command> with <args> a maximum of <max-attempts> times until it succeeds. \"Succeeds\" is"
echo "defined as returning <r> to the shell (default 0) and printing <output> to the console."
echo "Waits <initial-timeout> between the first and second attempts, and doubles the timeout each time"
@garymacindoe
garymacindoe / postmkvirtualenv
Created April 6, 2017 10:22
Pre/post scripts for virtualenvwrapper to enable jupyter notebook integration for virtualenvs
#!/bin/bash
# This hook is sourced after a new virtualenv is activated.
pip install ipykernel
python -m ipykernel install --user --name $(basename ${VIRTUAL_ENV}) --display-name "$(basename ${VIRTUAL_ENV})"
@garymacindoe
garymacindoe / C.java
Last active November 3, 2016 17:13
Code that is both valid C and Java
//\
public class C { //\
public static void main(String[] args) { //\
System.out.println("hello"/*
#include <stdio.h>
int main() {
{
return puts("world"/**/);
}
}
@garymacindoe
garymacindoe / kadane.cpp
Created September 14, 2015 20:38
O(n) solution to the maximum subarray sum problem using Kadane's algorithm
#include <tuple>
#include <iterator>
#include <algorithm>
template <class InputIterator>
std::tuple<typename std::iterator_traits<InputIterator>::value_type,
InputIterator, InputIterator>
maximum_subarray(InputIterator first, InputIterator last) {
typedef typename std::iterator_traits<InputIterator>::value_type value_type;
using std::max;
@garymacindoe
garymacindoe / fibonacci.c
Created September 12, 2015 18:32
Calculation of the nth term of the Fibonacci sequence in constant time
#include <stdio.h>
#include <inttypes.h>
#include <math.h>
uint64_t fibonacci(uint64_t n) {
static const double golden = 1.61803398874989484820;
return (uint64_t)floor((pow(golden, (double)n) / sqrt(5.0)) + 0.5);
}