system administration as a ssh wrapper • Executes to local or remote ✓ local: not a ssh connection. uses python subprocess module. ✓ run or sudo: when executing against remote, it can execute as the current user or as current user with sudo rights. Saturday, June 15, 13
• Install it ✓ pip install fabric ✓ Once installed, fab command is available in your system path. ✓ Start writing your python function in fabfile.py Saturday, June 15, 13
and install vagrant http://www.vagrantup.com cd ~/VirtualBox\ VMs mkdir debsqueeze64 cd debsqueeze64 vagrant init debsqueeze64 http://www.emken.biz/vagrant- boxes/debsqueeze64.box vagrant up Saturday, June 15, 13
debian 64-bit on your computer. • We will use fabric on our local machine to interact with this debian virtual machine • See http://www.vagrantbox.es to download other prepared boxes. Saturday, June 15, 13
OS X: package management with port or brew sudo port -v install virtualenv_select py27-virtualenv sudo port -v install py27-virtualenvwrapper sudo port select --set virtualenv virtualenv27 (optional) Saturday, June 15, 13
source `which virtualenvwrapper.sh` • $HOME -- this refers to your user home or “~” (i.e., on my computer, “~” means “/Users/calvin” directory) (optional) Saturday, June 15, 13
and optionally our python virtualenv ready, we begin git clone git://github.com/calvinchengx/learnfabric.git cd learnfabric pip install -r requirements.txt cp -rf ~/VirtualBox\ VMs/debsqueeze64/.vagrant . • This installs fabric and installs python-vagrant (a wrapper around vagrant commands so we can programmatically control our vagrant instance) Saturday, June 15, 13
and optionally our python virtualenv ready, we begin git clone git://github.com/calvinchengx/learnfabric.git cd learnfabric pip install -r requirements.txt cp -rf ~/VirtualBox\ VMs/debsqueeze64/.vagrant . • This installs fabric and installs python-vagrant (a wrapper around vagrant commands so we can programmatically control our vagrant instance) Saturday, June 15, 13
subprocess • run() runs a command remotely, via ssh • sudo() runs a command remotely as sudo, via ssh • put() copies a file from local to remote, via ssh • get() copies a file from remote to local, via ssh Saturday, June 15, 13
help us avoid typing in passwords - place fabric ssh user (env.user)’s public key in remote node’s authorized_keys • Avoid directly using root user • Give your fabric ssh user sudo rights instead (env.user) Saturday, June 15, 13
is simply a dictionary containing host information, roles, user (env.user); and • any other custom information you would like to include. • $HOME/.fabricrc allows custom configuration, e.g. user = ssh_user_name where “ssh_user_name” is any value you so desire to pass in to env.user when a fab function runs. Saturday, June 15, 13
invoke(command): “”” Invoke an arbitrary command “”” sudo(command) # command line fab invoke:“aptitude update” fab invoke:”aptitude upgrade” Saturday, June 15, 13
invoke(command): “”” Invoke an arbitrary command “”” sudo(command) # command line fab -P invoke:“aptitude update” fab -P invoke:”aptitude upgrade” Saturday, June 15, 13
deploy` or fab -H mysite.com deploy`. Execute a deployment to the given groups of hosts or host """ if not chk_req(): return if git_branch_check() or test_host_check(): manage_release('Deployment start') git_archive_and_upload_tar() pip_requirements() collectstatic(deploy=True) symlink_current() webserver() migrate_db() # post-deployment tasks manage_release('Deployment end') _releases_cleanup() email_on_success(trigger=email) # command line Saturday, June 15, 13
host1.com task:arg1,arg2 • fab -P -H localhost, host1.com, host2.com task • fab -R web task # roles defined by env.roledefs • fab -R web task:arg1,arg2 • fab -P -R web task • fab -l • more options explained via fab --help Saturday, June 15, 13
and deployment to staging and/or live servers by sysadmin • Automation leads to continuous integration (e.g. Jenkins CI Server) and continuous delivery • Support heterogeneous development environment in combination with vagrant (developers test against vagrant, which has same distro as staging and production hosts) Saturday, June 15, 13
that accompanies this set of slides https://github.com/calvinchengx/learnfabric • Fabric code and documentation https://github.com/fabric/fabric http://fabric.readthedocs.org • Multiple vagrant boxes for testing your server config http://awaseroot.wordpress.com/2012/05/06/script-for- adding-multiple-vagrant-boxes/ Saturday, June 15, 13