Skip to content

Instantly share code, notes, and snippets.

View rrichards's full-sized avatar
💭
Rocking the CLI

Ryan Richards rrichards

💭
Rocking the CLI
  • R3 Technologies, Inc.
  • Oklahoma City, OK
  • X @rrichards
View GitHub Profile
module System
extend self
def cpu_count
return Java::Java.lang.Runtime.getRuntime.availableProcessors if defined? Java::Java
return File.read('/proc/cpuinfo').scan(/^processor\s*:/).size if File.exist? '/proc/cpuinfo'
require 'win32ole'
WIN32OLE.connect("winmgmts://").ExecQuery("select * from Win32_ComputerSystem").NumberOfProcessors
rescue LoadError
Integer `sysctl -n hw.ncpu 2>/dev/null` rescue 1
end
@rrichards
rrichards / deploy.rb
Created February 4, 2014 18:39 — forked from ihassin/deploy.rb
set :application, '<APP_NAME>'
set :repo_url, "<git@PATH/REPO.git>"
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }
set :deploy_to, "/home/deploy/rails/<APP_NAME>"
# set :scm, :git
set :format, :pretty
set :log_level, :debug

Most instructions for using Capistrano tell you how to make it restart Phusion Passenger by 'touch'ing the restart.txt file, but this doesn't immediately restart the app - instead the first person to try to use the application will cause it to be restarted, so they will see a delay of at least a few seconds.

This shows how to add a post-deploy task to 'ping' the server, to cause it to restart immediately.

Then add a deploy:ping task to config/deploy.rb and set it to run automatically after deploy:restart. Alternatively you could put it into the deploy:restart task directly.

TODO: Test the response HTTP header of the ping, that it is a 200, and not some other error response (404/500).

namespace :postgres do
desc 'Backup postgres database'
task :backup => :environment do
db_config = Rails.application.config.database_configuration[Rails.env]
backup_dir = '/home/deploy/backups'
outfile = "#{ backup_dir }/#{ Rails.env }_#{ DateTime.now.strftime("%Y%m%d_%H%M%S.sql.gz") }"
cmd = "pg_dump -h localhost -U #{ db_config['username'] } #{ db_config['database'] } | gzip -c > #{outfile}"
puts "Making database backup..."
application:open-your-keymap
application:open-your-stylesheet
autocomplete:attach
autoflow:reflow-paragraph
bookmarks:clear-bookmarks
bookmarks:jump-to-next-bookmark
bookmarks:jump-to-previous-bookmark
bookmarks:toggle-bookmark
bookmarks:view-all
check:correct-misspelling
#!/usr/bin/env ruby
require "nokogiri"
# opens every file in the given dir tree and converts any html img tags to rails image_tag calls
#
# example usage:
# ruby convert.rb ~/my_rails_app/app/views
#
# ***be careful and backup before using this***
#

How to patch Ubuntu for Heartbleed

  1. sudo apt-get update
  2. sudo apt-get install -y libssl1.0.0 openssl
  3. openssl version -a and confirm the "built on" date is >= 2014-04-07
  4. sudo lsof -n | grep ssl | grep DEL and restart all listed services.

Repeat #4 until no results are returned.

Add this to /etc/apt/sources.list
<snip>
deb http://security.ubuntu.com/ubuntu precise-security main restricted
deb-src http://security.ubuntu.com/ubuntu precise-security main restricted
deb http://security.ubuntu.com/ubuntu precise-security universe
deb-src http://security.ubuntu.com/ubuntu precise-security universe
deb http://security.ubuntu.com/ubuntu precise-security multiverse
deb-src http://security.ubuntu.com/ubuntu precise-security multiverse
</snip>

Docker Cheat Sheet

Why

Why Should I Care (For Developers)

"Docker interests me because it allows simple environment isolation and repeatability. I can create a run-time environment once, package it up, then run it again on any other machine. Furthermore, everything that runs in that environment is isolated from the underlying host (much like a virtual machine). And best of all, everything is fast and simple."

TL;DR, I just want a dev environment

require "open3"
def readfile(file)
f = File.open(file)
lines = f.readlines("\n\n\n\n")
lines.each do |line|
fields = line.scan(/\s+Date:\s([^\n]+)$\s+Topic:\s([^\n]+)\n(.*)/m)
date = fields[0][0].strip