The Frontier Group - Blog

Managing multiple Ubuntu servers painlessly

November 16th, 2008, by mlambie

Like most programmers, I don’t like to repeat manual tasks, well, manually. We use Ubuntu Server almost exclusively on our servers and whilst package management is very simple (login, aptitude update, aptitude dist-upgrade, all done) when you have to look after many (fourteen and expanding) servers it can get repetitive.

I use Terminal (or is it called Terminal.app?) on Mac OS X as my terminal emulator, and I’ve messed around with using Applescript to open multiple tabs before, so I figured this was a problem worthy of a Sunday-morning.

What I envisaged was iterating over a list of servers and having Applescript manage a Terminal instance, opening new tabs where appropriate, and executing the upgrade command in each tab for each server. Sounds simple enough.

And it was. I used Martin Ström’s excellent article as a base for my script, and ended up with the following:

set tab_count to 0
set servers to {"astrotrain", "bumblebee", "grimlock", "ironhide", ¬
	"jazz", "laserbeak", "mirage", "prime", ¬
	"prowl", "ratchet", "ravage", "rumble", ¬
	"soundwave", "wheeljack"}
-- Update the package list, dist-upgrade and remove the downloaded
-- packages so they're not included in the backups
set dist_upgrade to ¬
	" 'sudo aptitude update &&
	  sudo aptitude dist-upgrade &&
	  sudo aptitude clean'"

-- Make our settings globally available
global tab_count, servers, dist_upgrade

-- Mainline
on main()
	tell application "Terminal"
		activate
		repeat with server in servers
			set cmd to "ssh -t " & server & dist_upgrade & " && exit"
			my open_tab(cmd)
		end repeat
	end tell
end main

on open_tab(cmd)
	tell application "Terminal" to activate
	my create_new_window_or_tab()
	tell application "Terminal" to ¬
		do script with command (cmd) in last tab of window 1
end open_tab

on create_new_window_or_tab()
	if tab_count ≤ 0 then
		tell application "Terminal" to do script ""
		set tab_count to tab_count + 1
	else
		tell application "System Events" to ¬
			tell process "Terminal" to ¬
				keystroke "t" using command down
	end if
end create_new_window_or_tab

-- Run the mainline
main()

Apologies for the highlighting – the plugin doesn’t understand Applescript.

We are a web development company and this is our blog. We specialize in building web applications with the Ruby on Rails framework. You can read more about our Ruby on Rails development or contact us.

4 Responses to “Managing multiple Ubuntu servers painlessly”

  1. Have you tried Capistrano for sending commands to multiple servers at once? It was mostly built for Rails deployment but works great for any kind of setup. There are some articles out there on how to use it outside Rails.

    Glad you find my script usable as a start for your.

    Cheers

  2. mlambie says:

    I’ve checked out Capistrano in the past (but need to investigate it further). For my situation, I want to be able to interact with each console, so I didn’t think it would work. For example, I need to be able to tell aptitude that I want to keep my existing configuration files for some applications on some servers.

    Regardless, I’ll look into it further and thanks for the useful starting point. I had a hard time finding how to open a new tab for Terminal a year ago when I looked into it, and you solved it nicely :)

  3. JohnMc says:

    Take a look at Fabric. It might provide the automation you are looking without use of terminal.

  4. [...] to my previous foray into the world of Applescript, I’ve modified my server management script to now prompt me for a sudo password. Previously I [...]

Leave a Reply


Follow Us

Stay in the Loop

  • Enter your email address to subscribe to our mailing list. You'll get updates about our products, specials and bonus offers, and general behind the scenes news from our team.

Twitter

Newsletters

Alexa Rank

Testimonial

The boys at The Frontier Group are amazing! For such a relaxed and personable organisation, they have phenomenal technical ability and a rampant professionalism. They have customisable solutions for all of my IT needs and they always deliver, on time and beyond expectation.

They fix problems other service providers can't and they helped me get a critical section of my web site up and running 10 minutes after I emailed the request!

Alex Hyndman, Nexus Car Share.

Featured Project

Case Study - Caudo Group - www.caudo.com.au

Website

www.caudo.com.au

Caudo Machinery

Caudo Group engaged our services to redesign their outdated website. We sent our photographer on-site to capture the essence of their business and turned it into a stunning web design.