Skip to content

Instantly share code, notes, and snippets.

View raymyers's full-sized avatar
🌳
Automating!

Ray Myers raymyers

🌳
Automating!
View GitHub Profile
@raymyers
raymyers / google-drive-example.rb
Created December 18, 2015 19:55
Example Google Drive ruby API usage with google-api-client version 0.9.pre4 authenticating with a service account
ENV['GOOGLE_APPLICATION_CREDENTIALS'] = "#{ENV['HOME']}/google-service-account.json"
scopes = ['https://www.googleapis.com/auth/drive']
drive = Google::Apis::DriveV2::DriveService.new
auth_client = Google::Auth.get_application_default(scopes).dup
auth_client.sub = '[email protected]'
drive.authorization = auth_client
report_folders = drive.list_files(q: "title = 'Reports'")
raise "Expected 1 folder called 'Reports', found #{report_folders.items.count}" if report_folders.items.count != 1
parent_id = report_folders.items[0]
@raymyers
raymyers / blacklist.js
Created February 19, 2015 16:15
angular directive to validate with a blacklist
angular.module('myapp.directive.blacklist', []).
directive('maBlacklist', [
function() {
return {
restrict: 'A',
require: 'ngModel',
scope: {'blacklist':'=gsBlacklist'},
link: function($scope, $elem, $attrs, modelCtrl) {
var check = function(value) {
@raymyers
raymyers / drain_port.sh
Last active July 21, 2022 19:29
Bash script for connection drain. Uses netstat to repeatedly count established connections
port=8080
max_tries=10
seconds_to_wait=3
echo "Waiting for traffic on port $port to stop..."
for (( tries=1; tries<=$max_tries; tries++ )); do
# Netstat invocation tested on Ubuntu. Check your distro for differences.
connections=$(netstat -ant | grep ":$port.*:.*ESTABLISHED" | wc -l)
echo " $connections connections remaining. Try $tries/$max_tries."
if [ $connections -eq 0 ]; then
exit 0
@raymyers
raymyers / ShellSplitter.java
Last active December 2, 2024 10:50
Simple Java program to tokenize string as a shell would - similar to shlex in Python. Not checked for POSIX compliance yet - feel free to comment with edge cases that could be fixed. This is my original work, free to reuse. Created for Stack Overflow question: https://stackoverflow.com/questions/1082953/shlex-alternative-for-java/20725050:
package com.cadrlife;
import java.util.ArrayList;
import java.util.List;
public class ShellSplitter {
public List<String> shellSplit(CharSequence string) {
List<String> tokens = new ArrayList<String>();
boolean escaping = false;
char quoteChar = ' ';
@raymyers
raymyers / iptables_port_switching.sh
Created September 13, 2013 19:07
Iptables commands supporting hot deploy by transferring traffic between app servers
sudo iptables -t nat -N hot-deploy
sudo iptables -t nat -A PREROUTING -j hot-deploy
# Enable first server
sudo iptables -t nat -A hot-deploy -p tcp --dport 80 -j REDIRECT --to-ports 8080
# Enable second
sudo iptables -t nat -A hot-deploy -p tcp --dport 80 -j REDIRECT --to-ports 8081 && sudo iptables -t nat -D hot-deploy 1
# Back to first
@raymyers
raymyers / checkJava5.gradle
Last active December 21, 2015 11:19
Gradle task that will fail if there are compile dependencies that require Java > 5.
task checkCompileDependenciesJava5Compatibility << {
configurations.compile.each { dep ->
def failureReason
zipTree(dep).matching { include("**/*.class") }.visit { fileDetails ->
if (!fileDetails.isDirectory()) {
def stream = fileDetails.file.newDataInputStream()
def magic1 = stream.readUnsignedShort()
def magic2 = stream.readUnsignedShort()
stream.skipBytes(2); // minor version
def majorVersion = stream.readUnsignedShort()
apply plugin: 'java'
apply plugin : 'war'
group = 'com.cadrlife'
version = '1.0.0'
buildscript {
repositories {
mavenCentral()
}
@raymyers
raymyers / EmbeddedJetty9Server.java
Created April 5, 2013 22:47
Embedded server with Jetty 9, including filelock workaround.
package com.cadrlife;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class EmbeddedJetty9Server {
// compile 'org.eclipse.jetty:jetty-server:9.0.0.v20130308'
// compile 'org.eclipse.jetty:jetty-webapp:9.0.0.v20130308'
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
@raymyers
raymyers / build.gradle
Created March 27, 2013 11:59
Build Gradle for using elasticsearch in DevSearch
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin : 'war'
apply plugin : 'jetty'
sourceCompatibility = 1.7
version = '0.1'
group = 'org.computermentor'
version = '1.0.0'
@raymyers
raymyers / LazySequencer.java
Created February 20, 2013 00:54
Find all subsequences in Java. Comparing plain JDK w/Lists to a lazy version using Iterables with Google Guava.
package com.cadrlife.blog;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
public class LazySequencer {
public static <T> Iterable<? extends Iterable<T>> subn(int n, Iterable<T> li) {
if (n == 0) {