Easily escape HTML for a WordPress post

Here’s a one-liner for converting angle brackets and other characters that need escaping. This will work on any system that has Python installed. Run this command:

python -c 'import cgi; import sys; print cgi.escape(sys.stdin.read())' | pbcopy

Once you hit enter on the command above, paste the content to convert and after pasting hit enter followed by ctrl+d.

If you’re not on OS X, remove the pipe to pbcopy. If you are on OS X, your converted text is now in your clipboard!

Auto-starting VirtualBox VMs on OS X

sudo cp \
/Applications/VirtualBox.app/Contents/MacOS/org.virtualbox.vboxautostart.plist \

Then edit /Library/LaunchDaemons/org.virtualbox.vboxautostart.plist set Disabled to false, set KeepAlive to true, and confirm the last string entry in the command array is set to /etc/vbox/autostart.cfg. The file should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

Make the directory /etc/vbox and create the file /etc/vbox/autostart.cfg with the following content:

# Default policy is to "deny" starting a VM, the other option is "allow".
default_policy = deny

berto = {
allow = true

Make sure to change berto to the username on your system that the VMs are under.

Next properly set permissions:

sudo chmod +x /Applications/VirtualBox.app/Contents/MacOS/VBoxAutostartDarwin.sh
sudo chown root:wheel /etc/vbox
sudo chown root:wheel /etc/vbox/autostart.cfg
sudo chown root:wheel /Library/LaunchDaemons/org.virtualbox.vboxautostart.plist

Now, configure the VMs that should automatically start and set how they should be stopped:

VBoxManage modifyvm jenkins --autostop-type acpishutdown
VBoxManage modifyvm chefpa-jenkins --autostart-enabled on

Finally, test the configuration by running:

sudo launchctl load /Library/LaunchDaemons/org.virtualbox.vboxautostart.plist

After a reboot, the VMs that have been set with autostart enabled should be running!


Updating the first commit in a git repo

I started a git repo in a VM without configuring an author and wanted to update all the commits, including the first one, which is a bit elusive.

The StackOverflow post Change first commit of project with git? almost nails it. For my use-case, or git version, I found Step 4 to be slightly incorrect. It states to use <commit after changed>, but I got it to work with the same hash used in Step 2. For example, say you are on the master branch and you want to update the author information for the first commit:

$ git checkout -b rename
$ first_commit=$(git rev-list HEAD | tail -n 1)
$ git reset --hard ${first_commit}
$ git commit --amend --author "Roberto Aguilar " -C HEAD
$ git rebase --onto rename ${first_commit} master

Now that the first commit is correct, I use the trick outlined in my Bulk Updating Author in Git to finish up the job:

$ new_first_commit=$(git rev-list HEAD | tail -n 1)
$ GIT_EDITOR="sed -i -e 's/^pick/edit/'" git rebase -i ${new_first_commit}
$ while [ 1 ]; do git commit --amend -C HEAD --author='Roberto Aguilar <roberto@baremetal.io' && git rebase --continue || break; done;

Efficiently getting number of rows in a PostgreSQL table

Running SELECT COUNT(*) [...] on very large tables is an expensive operation because it does a full table scan. Generally an exact number isn’t needed and one’s simply looking for a rough estimate.

A very efficient way of getting a rough estimate is using the following query:

SELECT reltuples FROM pg_class WHERE relname='{{ table_name }}'

If you happen to need an exact count, you’ll have to pay the expense.

VirtualBox VM poweroff with Ubuntu Server install

I installed a command-line version of Ubuntu in VirtualBox. After doing so, shutting down the VM would leave the VM container running instead of closing it out as I assumed. I found that it’s because ACPI was not installed and, potentially, the VM config wasn’t set to ACPI.

First, ensure the VM is setup for ACPI:

VBoxManage modifyvm [nameofmachine] --acpi on

And then in the VM, install acpid and when shutting down use the poweroff command:

sudo apt-get install acpid
user@virtualmachine: sudo poweroff