Skip to content

Instantly share code, notes, and snippets.

View stevenharman's full-sized avatar

Steven Harman stevenharman

View GitHub Profile
@stevenharman
stevenharman / expanding_raid_5_array.sh
Created March 31, 2015 00:03
Improving RAID Expansion speed (and wall clock time) on my Synology DS1515+.
# log the default values:
echo "speed_limit_max: `cat /proc/sys/dev/raid/speed_limit_max`" #=> 200000
echo "speed_limit_min: `cat /proc/sys/dev/raid/speed_limit_min`" #=> 10000
echo "stripe_cache_size: `/sys/block/md2/md/stripe_cache_size`" #=> 256
# update to use more RAM (Stripe Cache Size) and higher lower bound (speed_limit_min)
echo 100000 > /proc/sys/dev/raid/speed_limit_min
# This will result in more memory usage. bumping to 32768 resulted in ~512MB RAM increase.
echo 32768 > /sys/block/md2/md/stripe_cache_size
@stevenharman
stevenharman / enable_sudo_touchid
Last active August 19, 2022 14:55
Enable TouchID for sudo permissions on macOS
#!/usr/bin/env bash
if grep 'pam_tid.so' /etc/pam.d/sudo --silent; then
echo "TouchID is already enabled ✅"
else
echo "Enabling TouchID for sudo 🪪";
sudo sed -i -e '1s;^;auth sufficient pam_tid.so\n;' /etc/pam.d/sudo
fi
@stevenharman
stevenharman / config.rb
Last active April 12, 2022 02:19
An example of an app-specific configuration wrapper around ENV and Rails.env. This consolidates access to config vars, while providing a seam for adding conditional logic, data munging, type coercion, etc...
module Example
class Config
def initialize(host_env: ENV, rails_env: Rails.env)
@host_env = host_env.to_hash
@rails_env = rails_env
end
def admin_qa_tooling_enabled?
coerce_boolean(host_env.fetch("ADMIN_QA_TOOLING", "false"))
end
@stevenharman
stevenharman / generate_ssl_cert
Created August 16, 2013 14:41
Generate a self-signed cert, update your hosts file, and add it to your OS X Keychain for local SSL success!
#!/usr/bin/env sh
echo "Creating a self-signed certificate..."
openssl req -new -newkey rsa:2048 -sha1 -days 3650 -nodes -x509 -subj "/C=US/ST=Georgia/L=Atlanta/O=BNR/CN=localhost.ssl" -keyout config/server.key -out config/server.crt
if ! grep -q "\blocalhost\.ssl\b" /private/etc/hosts; then
echo "Adding localhost.ssl to your hosts file..."
echo "127.0.0.1 localhost.ssl" | sudo tee -a /private/etc/hosts
fi
@stevenharman
stevenharman / date_range.rb
Created March 4, 2022 15:03
A DateRange value object, for those times you need to work with a range (or span) of dates (or really... datetimes)!
# frozen_string_literal: true
require 'active_support'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/time'
class DateRange
attr_reader :start, :stop
def initialize(start: nil, stop: nil)
@stevenharman
stevenharman / add_rails_helper_to_files_with_grep_sed_xargs.sh
Last active January 21, 2022 17:31
Add a line of text to a file ONLY if the file doesn't already contain the line. Oh, but first append a line to the start of every file without that line. grep, sed, and xargs, folks!
# So… uuuh… what had happend was - I'd changed our `.rspec` config so that we
# could have isolated specs. The only real change was to not auto-require
# `rails_helper` in the `.rspec` config. Instead we auto-require `spec_helper`,
# and then files that need `rails_helper` (to boot rails) will have to add
# `require 'rails_helper'`.
#
# I didn’t think much of it b/c I’d noted that the specs I’d been looking at
# were already doing `require 'rails_helper'`, so it should be a no-op for
# them. It turns out, 700 or so other spec files ARE NOT already requiring
# `rails_helper`. So… what does one do when confronted with needing to in-place
@stevenharman
stevenharman / scoping_in_ruby_19.rb
Last active January 13, 2022 20:08
ProTip™ on Constant Scoping and Resolution in Ruby: One of these is not like the other.
module Root
FOO = 'Ayyyy!'
end
module Root::NotNested
def self.speak
puts FOO
end
end
@stevenharman
stevenharman / telemetry_initializer.rb
Last active November 23, 2021 21:05
A very thin wrapper for consistently configuring Ruby components for exporting OpenTelemetry tracing to Honeycomb
Telemetry.configure do |c|
c.component = ENV.fetch("COMPONENT_NAME")
c.environment = ENV.fetch("APP_ENV")
c.deployment_environment = ENV.fetch("DEPLOYMENT_ENVIRONMENT")
c.honeycomb_api_key = ENV.fetch("HONEYCOMB_API_KEY", nil)
c.honeycomb_dataset = ENV.fetch("HONEYCOMB_DATASET", nil)
# This is delegating through to `OpenTelemetry::SDK::Configurator#use_all`
c.use_all("OpenTelemetry::Instrumentation::Sidekiq" => {span_naming: :job_class})
@stevenharman
stevenharman / Gemfile
Created November 20, 2021 03:02
A minimally functional OpenTelemetry setup to send traces to Honeycomb. This leaves a lot to be desired, but is a starting point.
# Observability
gem "opentelemetry-sdk"
gem "opentelemetry-exporter-otlp"
gem "opentelemetry-instrumentation-all"
@stevenharman
stevenharman / select_from_subquery.sql
Created January 21, 2013 20:44
Using Arel/ActiveRecord to execute a subquery, including a SUM.
select fund_sums.total, fund_products.name
from (
select sum(allocation_amount) total, fund_product_id
from transactions
where transactions.investor_id = 490
group by fund_product_id
) fund_sums
left join fund_products on fund_sums.fund_product_id = fund_products.id