Skip to content

Instantly share code, notes, and snippets.

View ejoubaud's full-sized avatar

Emmanuel Joubaud ejoubaud

View GitHub Profile
@ejoubaud
ejoubaud / list_instance_private_ips.sh
Last active June 20, 2016 14:54
One-liner to list instance private IPs with the AWS CLI
aws ec2 describe-instances --region us-east-1 --query 'Reservations[].Instances[].[ Tags[?Key==`Name`].Value | [0], PrivateIpAddress, PublicIpAddress ]' --output text | sort
@ejoubaud
ejoubaud / keydown.js
Last active April 9, 2024 19:00
Trigger keyboard event in Javascript, works in OSX Chrome 58
// Based on http://stackoverflow.com/a/10520017/1307721
// which wouldn't work for my use-case on my Mac because metaKey would always be true, for some reason
// Adapted to override metaKey to false
Podium = {};
Podium.keydown = function(k) {
var oEvent = document.createEvent('KeyboardEvent');
// Chromium Hack
Object.defineProperty(oEvent, 'keyCode', {
get : function() {
@ejoubaud
ejoubaud / cf_asg_instances_status_check.rb
Created December 14, 2016 17:46
Checks the status and AMI ID of instances in the ASG of a CloudFormation stack (used to test an AMI rotation script)
#!/usr/bin/env ruby
require 'aws-sdk'
require 'forwardable'
DEFAULT_REGION = "us-east-1".freeze
STACK_NAMES = ['market-dj'].freeze
class Instance
extend Forwardable
@ejoubaud
ejoubaud / trace_files_git_history.rb
Created December 22, 2016 15:58
Say you have a file that's been duplicated a few and you want to get an overwiew of how each copy diverged, this gives you a TSV you can import in Excel
#!/usr/bin/env ruby
`for i in market*.sh.erb; do echo $i; git log --format="%ad%x09%h%x09%s" --date=iso -- $i; done > history1.log`
lines = File.readlines('history1.log')
commits_by_file = lines.reduce(['', Hash.new([])]) { |(current_file, commits_by_file), line| line.include?("\t") ? [current_file, commits_by_file.merge(current_file => (commits_by_file[current_file] + [line]))] : [line, commits_by_file] }.last
commits_by_file_parsed = Hash[commits_by_file.map{|file, commits| [file, commits.map{|c|c.split("\t")}.map{|s|{:date => s[0], :sha => s[1], :msg => s[2]}}]}]
flat_commits = commits_by_file_parsed.flat_map{|file, commits| commits.map {|commit| commit.merge(:file => file)}}
commits = flat_commits.reduce({}) { |mem, commit| new_commit = mem[commit[:sha]] ? mem[commit[:sha]].merge(:files => (mem[commit[:sha]][:files] + [commit[:file]])) : commit.select{|k| k != :file}.merge(:files => [commit[:file]]); mem.merge(commit[:sha] => new_commit) }.values.sort_by{|c| c[:date]}
files = commits_by_file.keys
@ejoubaud
ejoubaud / get-rds-slow-logs.sh
Last active March 30, 2017 08:31
Fetch all available RDS slow logs into single file
aws-vault exec customer -- aws rds describe-db-log-files --db-instance-identifier market-database-clustercluster-16apkpdp1bgkx-us-east-1a --region us-east-1 --query 'DescribeDBLogFiles[].LogFileName' --output text | tr '\t' '\n' | grep slowquery | while read file; do aws-vault exec customer -- aws rds download-db-log-file-portion --db-instance-identifier market-database-clustercluster-16apkpdp1bgkx-us-east-1a --region us-east-1 --log-file-name $file --output text >> slowlogs.log; done
@ejoubaud
ejoubaud / waiting_transactions.sql
Created April 11, 2017 13:35
Mysql Waiting transactions diagnostic
SELECT
r.trx_id as waiting_trx_id,
r.trx_mysql_thread_id as waiting_thread,
r.trx_query as waiting_query,
b.trx_id as blocking_trx_id,
b.trx_mysql_thread_id as blocking_thread,
b.trx_query as blocking_query,
bp.host as blocking_host,
rp.host as waiting_host,
bp.command as blocking_command,
@ejoubaud
ejoubaud / mock_as_noun_testing_in_elixir_controllers_is_a_pain.ex
Last active April 28, 2017 14:28
Tried Elixir "Mocks-as-noun" tests of seminal http://blog.plataformatec.com.br/2015/10/mocks-and-explicit-contracts/ fame. Hated them. They require a lot of boilerplates and force duplications. Besides controllers aren't well designed for unit testing. Looks like isolated unit testing is not very compatible with Phoenix controllers :(
defmodule MyApp.AuthController do
use MyApp.Web, :controller
# 1. I need that \\ default arg here. I guess I can live with this. Explicit deps, pure functions, why not.
def callback(%{assigns: %{ueberauth_auth: auth}} = conn, _params, auth_service \\ MyApp.Auth) do
case auth_service.sign_up_or_sign_in(auth) do
{:ok, user} ->
conn
# 0. More of a problem with controller unit tests than with mocks, but still related as it's about isolation unit tests:
# `#put_session` won't work here in my unit test
@ejoubaud
ejoubaud / keybase.md
Created July 17, 2017 07:35
Keybase verification

Keybase proof

I hereby claim:

  • I am ejoubaud on github.
  • I am ejoubaud (https://keybase.io/ejoubaud) on keybase.
  • I have a public key ASC4V70jP4GYLh15POtHMHdB6bbITboG_AUDC-wjPf4IZAo

To claim this, I am signing this object:

@ejoubaud
ejoubaud / check_ntp.sh
Created January 18, 2019 12:59
Check that ntp is running correctly
ntpq -pn
@ejoubaud
ejoubaud / dj_check.sql
Last active April 16, 2019 08:50
DJ (delayed_jobs) queue check
select
if(locate('PerformableMethod', s1) > 0,
if(locate('method_name', s3) > 0,
s3,
s2
),
s1
) s,
sum(run_at <= now()) as total_waiting, -- date_add(now(), interval 8 hour))
min(priority),