Skip to content

Instantly share code, notes, and snippets.


Michael Mahemoff mahemoff

View GitHub Profile
mahemoff /
Last active Sep 21, 2022
Vim Terminal Mode - A short introduction

Vim has a Terminal Mode!

Since v8.1 (May 2018), Vim has shipped with a built-in terminal. See or type :help terminal for more info.

Why use this? Mainly because it saves you jumping to a separate terminal window. You can also use Vim commands to manipulate a shell session and easily transfer clipboard content between the terminal and files you're working on.

Key Bindings

mahemoff / Ansible Disk Check
Last active Aug 22, 2022
Show disk space and warn about disk full in Ansible
View Ansible Disk Check
* Shows a message while asserting like:
ok: [host] => {
"msg": "disk usage 4.2B of total 20.0GB (21.0%) (should be within limit 90.0%)"
* Note this only looks at first mount point on current node
* Fails if disk is near-full
* Last step pushes to a push-based monitoring service, which will alert us if it doesn't get there after some time
* Need to setup a variable `disk_limit`, which is the max acceptable usage ratio, e.g. set it to 0.8 if you want to keep disks within 80% of max size
mahemoff / certbot
Last active Aug 21, 2022
Certbot for Apache one-liner
View certbot
# Certbot can be a bit tedious, but it's easy to automate a one-liner non-interactive. Substitute your own domain and email below.
sudo service apache2 stop && sudo certbot --apache -d -m --non-interactive --agree-tos && sudo service apache2 start
mahemoff / unique_domains.rb
Created Jul 27, 2022
Unique domains list from ICANN zones files
View unique_domains.rb
#!/usr/bin/env ruby
# Based on - extended it to read from gzipped file, support any domain extension, and output to separate file
require 'hashie'
require 'zlib'
class Parser < Hashie::Dash
property :extension, required: 'true' # e.g. "com" for dotcom database
mahemoff /
Last active May 23, 2022
Tuning MySQL and Apache

A few general tips to deal with slow performance and hanging. Target system is Ubunty 20.04 and MySQL 8.0, but applicable to other configurations.

Relies on some functions in

Slow log

MySQL can log slow queries. Enable it in /etc/mysql/mysql.conf.d/mysqld.cnf.

slow_query_log = 1
mahemoff / names.txt
Created Apr 5, 2012
Reserved username list
View names.txt
A meta-compilation of:
(Took out some company-specific terms at the top of the first one (e.g. imulus, stacks), added one term so far - 'edits'.)
NOTE: Does not include profanities. You may or may not want to add a list of those.
(See, but don't try to apply regexp's here because Scunthorpe -
mahemoff / elasticsearch.rake
Created Sep 19, 2012 — forked from jarosan/elasticsearch.rake
Elasticsearch reindex task
View elasticsearch.rake
# Run with: rake environment elasticsearch:reindex
# Begins by creating the index using tire:import command. This will create the "official" index name, e.g. "person" each time.
# Then we rename it to, e.g. "person20121001" and alias "person" to it.
namespace :elasticsearch do
desc "re-index elasticsearch"
task :reindex => :environment do
klasses = [Place, Person, Caption]
mahemoff /
Last active Sep 25, 2021
MySQL thread-safe multi-database switching in Rails 5

The code here is a tech demo showing how to switch between databases in Rails 5, in a thread-safe manner.


The simple way to switch databases is:

ActiveRecord::Base.establish_connection :user_shard1
puts User.find(1) # this is executed on the database keyed on "user_shard1" in database.yml
ActiveRecord::Base.establish_connection :development
mahemoff / uniq.rb
Created Sep 12, 2014
Ensure Sidekiq jobs are unique (if the queue contains several jobs with same class and args, this will delete all but one of them)
View uniq.rb
def self.uniq! queue_name
seen_already = { |job|
key = { job.klass => job.args } ? job.delete : seen_already << key