Thursday, April 2, 2015

Workstation Automation, Part 1: Virtualization

Curious about how to efficiently build a workstation to work on a variety of projects using reliable repeatable processes?  Then this post is for you. It is part 1 in a series that I will write about this topic.
There are a variety of hardware workstations that you may be using: a desktop, laptop, maybe two laptops. The OS on these computers could also be of different types: Windows (7,8), Mac OS X, or linux (RedHat,Centos,etc.). We can normalize (or make same) the process of working on projects across these profiles (hardware & OS) with a group of tools designed to work together efficiently. These are virtualization and bootstrap automation tools (or  CLI). Two specific tools in these categories are VirtualBox and Vagrant. There are others, however these serve as working examples for this post. Both tools are open source and free. You can easily download and install them on all the before mentioned profiles (hardware & OS).
Now that you have the tools to create new virtual machines easily you still need to have base image. The base image is the OS of your choosing that you would like available for working on projects. You are not restricted to just one though, you may have multiple projects on different operating systems and you can have base images for each. I use a centos 7 minimum install base image often and it will work well for the purpose of this blog.  The centos 7 minimum image is available to everyone from the online service Atlas. I've uploaded my base image there and made it available to the public.  Atlas is built by Hashicorp, the same makers of Vagrant, additionally having built my own base image I can be confident in the reliability of it being accessible and working.
Now that we have identified tools and a base image. The only final necessary component to get my virtual machine running is to create a "Vagrantfile". The file can be as simple as the following:
----------------- Start File: Vagrantfile ------------------
Vagrant.configure(2) do |config|
config.vm.box = "abrooke/centos-7min"
end
----------------- End File: Vagrantfile -------------------
In the same directory as the before mentioned file. Simply run:
#>   vagrant up
Once the output indicates successful completion the machine will be online and ready to be accessed using SSH. With a properly configured SSH client you will be able to access the virtual machine with the simple command:
#>   vagrant ssh
In the next part in the series we will explore more features of the VagrantFile and configuring the Virtual Machine with Chef.

Saturday, December 20, 2014

Task: Create a Virtual Box for Atlas

[Task time 2 hours]

Use Atlas, a recently released product of Hashicorp, to host virtualbox images.  I already created an account, which was easy and free. I've also created my first box "abrooke/centos-7" from the web GUI at atlas. However, I need to now create a "box" for vagrant to upload it after configuring a "virtualbox" provider in my new box's configuration in Atlas and that is where the blog post is picking up the story...
  • Download a minimal ISO from centos.
  • Create a new VirtualBox VM and use the ISO to install centos.
    • Use "Nat" for the first network adapter.
    • Note the MAC address for later.
    • The minimal install doesn't come with dhclient running, so no IP. So addition setup instructions:
      • List the interfaces with "nmcli d"
      • Set the disconnected interface to automatically connect with "nmtui"
      • Then finally restart the network "systemctl restart network"
  • Then follow the instructions for createing a base box and Virtual Box specifics:
    • Set root password to "vagrant"
    • Add "vagrant" user using command: "adduser vagrant"
    • Set "vagrant" password to "vagrant" using command: "passwd vagrant"
    • Give "vagrant" user sudo permissions:
      • Run "visudo"
      • Add to the file "vagrant ALL=(ALL) NOPASSWD: ALL"
      • Remove "requiretty" so that sudo can run without a tty.
    • Add insecure key to authorized_keys file of the vagrant user.
      • Install wget as root: "yum install wget"
      • Wget the public key from vagrant on github and put it in the "authorized_keys" file of the "vagrant" user in the "~/.ssh/" directory.
      • Set modes for ssh files:
        • "chmod 600 ~/.ssh/authorized_keys"
        • "chmod 700 ~/.ssh"
    • Install Virtual Box guest additions:
      • REQUIRES bzip2, kernel-devel, kernel-headers, gcc:
        • "yum install bzip2"
        • "yum install kernel-devel-$(uname -r)"
        • "yum install kernel-headers-$(uname -r)"
        • "yum install gcc"
        • Troubleshooting: you might need to reboot.
      • Mount the Virtualbox guest additions:
        • Make sure the virtual cd with the additions is loaded.
        • Make mount point "/media/cdrom"
        • Now mount the "mount /dev/cdrom /media/cdrom"
    • Set "UseDNS  no" in the "/etc/ssh/sshd_config" to speed up the box when not connected to the internet.
  • HACK Vagrant version 1.7.1 to actually package the Vagrant Box:
  • Package box in current working directory with "vagrant package --base [name of VM]".

After the Vagrant box has been created, the testing of the box can be done by loading it into vagrant's box list using:
  • "vagrant add box [box name] [box path from package process]

After testing the box, the box can be uploaded to Atlas. Then subsequently a "Vagrantfile" can be configured to use it. Then "vagrant up" will use the "Vagrantfile" and download the box from Atlas. If the box is private in Atlas, then "vagrant login" will be necessary.

Tuesday, December 16, 2014

Start SSH Agent on Bash Shell Login (Works in Cygwin)

Automatically starting ssh-agent on login to bash shell can be done by adding the following script to ".bash_profile" file in a users home directly. This should work on linux and cygwin.  This script was adapted from the stack overflow post start-ssh-agent-on-login.

--- START SCRIPT ---

SSH_ENV="$HOME/.ssh/environment"

function start_agent {

    echo "Initialising new SSH agent..."
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
    echo succeeded
    chmod 600 "${SSH_ENV}"
    . "${SSH_ENV}" > /dev/null
    echo "Manage keys as follows..."
    echo "ssh-add <identity file>    Add identity file."
    echo "ssh-add -L                 List the keys currently."   
    echo "ssh-add -D                 Delete the keys."
}

# Source SSH settings, if applicable


if [ -f "${SSH_ENV}" ]; then

    . "${SSH_ENV}" > /dev/null
    #ps ${SSH_AGENT_PID} doesn't work under cywgin
    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi

--- END SCRIPT ---