Kevin Warrington

Loading clever subtitle...

Modernizr Plugin API

Modernizr provides a great plugin API, where custom tests can be added, for features that are currently not supported.

To add a test, simply call the addTest method.

Here is a example of a test used to determine if a browser cuts the mustard:

(function (window, document, Modernizr) {
  'use strict';
  Modernizr.addTest('mustard', function () {
    if('querySelector' in document &&
        'localStorage' in window &&
        'addEventListener' in window) {
      return true;
    }
  });
}(this, document, this.Modernizr));

Vagrant

Vagrant is used to create and configure lightweight, reproducible, and portable (Linux, Mac OS X or Windows) development environments

Setup

A Vagrantfile is used to describe your project’s environment, specifically:

  1. root directory
  2. box or machine type, allocated resources and how it is accessed
  3. software installed

Software installation can be deferred to provisioning tools such as shell scripts, Chef, or Puppet.

To create a new Vagrantfile, run:

vagrant init

Boxes

Vagrant uses as a box (base image) to clone a virtual machine.

So, the first step is to add a box from the Vagrant Cloud:

vagrant box add chef/ubuntu-13.10

Inside your Vagrantfile, reference this box:

Vagrant.configure("2") do |config|
    config.vm.box = "chef/ubuntu-13.10"
end

Verify

vagrant up
vagrant ssh

Provisioning

Vagrant has built in provisioning via the shell provisioner.

First create a public directory to house our web files:

mkdir public
echo "HI" > public/index.html

Simply create a bootstrap.sh file at the root of our project:

#!/usr/bin/env bash

apt-get update
apt-get install -y apache2
rm -rf /var/www
ln -fs /vagrant/public /var/www

Add this script reference to our Vagrantfile:

Vagrant.configure("2") do |config|
    config.vm.provision :shell, :path => "bootstrap.sh"
end

Then reload and run the provisoner

vagrant reload --provision

Verify

vagrant ssh
wget -qO- 127.0.0.1

Networking

Vagrant supports port forwarding, public and private networks

To setup port forwarding for our apache server simply:

Vagrant.configure("2") do |config|
    config.vm.network :forwarded_port, host: 4567, guest: 80
end

Then reload

vagrant reload

Verify

http://localhost:4567

Teardown

Vagrant supports a number of teardown options:

  • Suspend saves VM RAM and contents to disk, fast boot
  • Halt saves VM contents to disk, cold boot
  • Destroy removes VM from disk

Cloud Computing Service Models

IaaS (Infrastructure as a Service)

  • Provider manages underlying infrastructure, eg. storage, network and computing resources
  • You can deploy, run and control software
  • Pricing model: usage and component based

Providers:

  • AWS EC2
  • Joyent
  • Rackspace

PaaS (Platform as a Service)

  • Provider manages underlying infrastructure
  • Provider manages operating system
  • Provider manages programming languages, frameworks, libraries, services and tools for you to create and deploy applications.
  • You control deployed applications and configuration
  • Pricing model: usage and component based

Providers:

  • Heroku
  • AWS Elastic Beanstalk
  • Engine Yard
  • Google App Engine
  • Windows Azure
  • OpenStack
  • Nodejitsu

SaaS (Software as a Service)

  • Provider manages underlying infrastructure
  • Provider manages operating system
  • Provider manages application platform and even individual application capabilities
  • You control user-specific configuration
  • You use application on client devices through either a web browser or API
  • Pricing model: monthly or yearly flat fee

Providers:

  • Salesforce CRM
  • Dropbox
  • Google Apps
  • LinkedIn

Using the Loopback Interface

The hosts file is used to map hostnames to ip addresses.

localhost is the hostname for the loopback network interface, in /etc/hosts:

# IPv4 loopback address
127.0.0.1 localhost

# IPv6 loopback address
::1 localhost

# IPv6 link-local address
fe80::1%lo0 localhost

An loopback address is used to send a packet to itself, whereas link-local allows packet transfer between devices on the local link (not routable).

Loopback addresses are often used in web development:

127.0.0.1 localhost project1.local project2.local

They are also used to increase privacy and security. Here is a very helpful resource on the subject.

After updating your hosts file, be sure to clear the directory service cache:

dscacheutil -flushcache

Error: Failed to Create Host-Only Adapter

When trying to run vagrant up, received error:

There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["hostonlyif", "create"]

Stderr: 0%...
Progress state: NS_ERROR_FAILURE
VBoxManage: error: Failed to create the host-only adapter
VBoxManage: error: VBoxNetAdpCtl: Error while adding new interface: failed to open /dev/vboxnetctl: No such file or directory

VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component HostNetworkInterface, interface IHostNetworkInterface
VBoxManage: error: Context: "int handleCreate(HandlerArg*, int, int*)" at line 68 of file VBoxManageHostonly.cpp

This happens because another VM is running in VirtualBox, run:

sudo /Library/StartupItems/VirtualBox/VirtualBox restart

If VirtualBox is loading on startup, remove it:

cd /Library/StartupItems
rm -r VirtualBox

Verifying Data Using Checksums

Checksums are used verify data integrity and in some cases, authenticity.

It is especially important to run checksums on large files, such as operating systems, where 100% completeness is desired. As file size increases, so will the possibility of errors during transmission.

To check a single file on Mac OS X, simply run:

shasum -a 256 file.iso

To check multiple files using a SHA256SUMS file:

cd ~/Downloads
shasum -a 256 -c 1.4.3_SHA256SUMS 2>&1 | grep OK

If the file was verified you should see:

Vagrant-1.4.3.dmg: OK

HowToSHA256SUM

Error: MySQL Server Has Gone Away

When trying to create a new mysql user, I came across the error:

ERROR 2006 (HY000) at line 1: MySQL server has gone away

The fix turned out to be simple, as the documentation states, “mysql_upgrade should be executed each time you upgrade MySQL”. The following command fixed the problem.

mysql_upgrade --password

Joining Files on the Command Line

Here’s an extremely easy way to join files in unix. It uses the () subshell operator, which waits for the enclosed command to return before passing the result to standard out.

(cat file1 file2)> file3

Converting Between Formats

There are many tools you can use to convert between file formats.

Convert bin to iso, with bchunk:

brew install bchunk
bchunk input.bin input.cue output.iso

Convert png to svg, with convert and potrace, for simple images:

brew install imagemagick potrace
convert file.png file.pnm
potrace file.pnm -s -o file.svg -C#ff0000 -k0.6 # red foreground, more detail
rm file.pnm

More to come!

Unix Pipelines vs Redirection

Redirection is used to send data from standard streams to specific locations.

To send the standard output stream to a file, instead of the terminal:

command1 > outfile
command1 1> outfile

Same as above, but instead send the standard error stream:

command1 2> outfile

To send standard output/error streams to a file, instead of the terminal:

command1 > outfile 2>&1

Output can also be disposed of using the null device:

command1 > /dev/null 2>&1

To use the contents of a file as the standard input stream to a command, instead of using keyboard input:

command1 < infile

Input can be read from one file and output to another:

command1 < infile > outfile

The standard output of one command can also be used as the standard input to another using a temporary file:

command1 > file
command2 < file
rm file

However, this is inefficient as the second command has to wait for the first to complete before proceeding. Also, there is a chance that the temporary file will overwrite an already existing one.

Instead, it is more efficient to directly stream the output of one command into another via pipes:

command1 | command2

Along with the standard out, you can also send standard error, notice that it appears before the pipe.

command1 2>&1 | command2

It is also possible to direct the output of a command to standard out and an outfile using tee.

command1 | tee outfile

Lastly, if you want to avoid overwriting files when redirecting, set noclobber:

set -o noclobber
command1 > existingfile
# -bash: existingfile: cannot overwrite existing file