     args.push "root@#{@ip_address}:/root"
 
     ssh = spawn 'scp', args
-    ssh.stdout.on 'data', callbacks.stdout
-    ssh.stderr.on 'data', callbacks.stderr
-    ssh.on 'exit', callbacks.exit
+    ssh.stdout.on 'data', (data) -> console.log data.toString('ascii')
+    ssh.stderr.on 'data', (data) -> console.log data.toString('ascii')
+    ssh.on 'exit', callback
 
   # Wait until we can connect to port 22 of instance
   _wait_for_sshd: ->

  state: (callback) ->

  # Run a shell command as root on the server.
  sh: (command, callbacks) ->
    @_ssh LithiumConfig.sshkey_private, command, callbacks

  # Copy files to the instance
  cp: (files, callbacks) ->
    @_scp LithiumConfig.sshkey_private, files, callbacks

  # Copy & run hooks on instance
  # TODO: coupled
  run_hooks: ->
    callbacks =
      stdout: (data) -> console.log data
      stderr: (data) -> console.log data
      exit: (code) -> console.log code

    files_to_cp = _.map @config.hooks, (hook) =>
      "classes/#{@config.name}/hooks/#{hook}" if /\d+_.+\.r\..+/.test hook

    @cp files_to_cp, callbacks
    _.each @config.hooks, (hook) =>
      if /\d+_.+\.r\..+/.test hook
        @sh "sh /root/#{hook}", callbacks
      if /\d+_.+\.l\..+/.test hook
        @_local_sh "sh classes/#{@config.name}/#{hook}", callbacks

  #### Private methods ####

  # Connect via SSH and execute command
  _ssh: (key, command, callbacks) ->
    @_wait_for_sshd()
    args = [
      '-o', 'LogLevel=ERROR', '-o', 'UserKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=no', '-i', key, "root@#{@ip_address}"]
    args = args.concat(command.split ' ')

    ssh = spawn 'ssh', args
    ssh.stdout.on 'data', callbacks.stdout
    ssh.stderr.on 'data', callbacks.stderr
    ssh.on 'exit', callbacks.exit

  # Copy files via SCP
  # TODO: factor out into SSH & SCP class
  _scp: (key, files, callbacks) ->
    @_wait_for_sshd()
    args = [
      '-o', 'LogLevel=ERROR', '-o', 'UserKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=no', '-i', key ]

net           = require 'net'

_             = require 'underscore'
async         = require 'async'

cf            = require 'config'
LithiumConfig = require('lithium_config').LithiumConfig
