Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael Mahemoff mahemoff

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

Vim has a Terminal Mode!

Since v8.1 (May 2018), Vim has shipped with a built-in terminal. See https://vimhelp.org/terminal.txt.html 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
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
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 example.com -m admin@example.com --non-interactive --agree-tos && sudo service apache2 start
@mahemoff
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 https://sive.rs/com - 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
mahemoff / tuning.md
Last active May 23, 2022
Tuning MySQL and Apache
View tuning.md

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 https://github.com/mahemoff/dotfiles

Slow log

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

slow_query_log = 1
@mahemoff
mahemoff / names.txt
Created Apr 5, 2012
Reserved username list
View names.txt
###
A meta-compilation of:
https://raw.github.com/gist/1453705/d253733a56632a8d2c29321a75c18b627fa4dda8/reserved_usernames.rb
http://blog.postbit.com/reserved-username-list.html
http://www.quora.com/How-do-sites-prevent-vanity-URLs-from-colliding-with-future-features
(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 https://www.google.com/search?q=profanity+list, but don't try to apply regexp's here because Scunthorpe - http://en.wikipedia.org/wiki/Scunthorpe_problem)
@mahemoff
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
mahemoff / README.md
Last active Sep 25, 2021
MySQL thread-safe multi-database switching in Rails 5
View README.md

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

BACKGROUND: HOW NOT TO CHANGE DATABASES

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
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 = Set.new
Sidekiq::Queue.new(queue_name).each { |job|
key = { job.klass => job.args }
key.in?(seen_already) ? job.delete : seen_already << key
}
end