     console.log "Cluster.startApp #{app}, #{version}, #{instance}, #{EJSON.stringify options}, #{EJSON.stringify parameters} in project #{Settings.get('project')}."
 
     user = getUser @userId
-    agentUrl = if options?.targetHost then "http://#{options.targetHost}" else pickAgent()
+    agentUrl = if options?.targetHost then "http://#{options.targetHost}" else getAgent()
     appDef = findAppDef app, version
     dockerCompose = YAML.load substituteParameters appDef.dockerCompose, parameters
     bigboatCompose = YAML.load appDef.bigboatCompose

  console.log stdout, stderr
  cb && cb()

pickAgent = ->
  #round robin
  settings = Settings.all()
  agents = settings.agentUrl
  agent = agents.shift()
  agents.push agent
  Settings.set 'agentUrl', agents
  agent

findAppDef = (name, version) ->
  ApplicationDefs.findOne

@Cluster = @Agent =
  getStorageBucketSize: (id) ->
    name = StorageBuckets.findOne(id)?.name
    callAgent 'get', "/storage/#{name}/size", {}, (res) ->
      StorageBuckets.upsert {name: name}, $set: JSON.parse res.content
  listStorageBuckets: ->
    callAgent 'get', '/storage/list'
  deleteStorageBucket: (name) ->
    callAgent 'del', "/storage/#{name}"
  createStorageBucket: (name) ->
    callAgent 'put', '/storage', data: name: name
  copyStorageBucket: (source, destination) ->
    callAgent 'put', '/storage', data: name: destination, source: source

  stopAll: ->
    Instances.find().forEach (inst) -> stopInstance inst.name
