Sha256: fa91205b3076bb7661a183a1cd535b93d6d391ca45e188c99c30597858fdb30b

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

require 'astrovan/deploy'
require 'astrovan/exec'
require 'astrovan/util'
require 'astrovan/rake'
require 'astrovan/update'

module Astrovan
  # Created by the using method, this is the context within which all other deployment methods run.
  class Session
    include Astrovan::Deploy
    include Astrovan::Exec
    include Astrovan::Util
    include Astrovan::Rake
    include Astrovan::Update

    #:stopdoc:
    DEFAULTS = {
      :application => nil,
      :deploy_to => nil,
      :env => nil,
      :password => nil,
      :rakefile => nil,
      :username => nil
    }
    #:startdoc:

    # array of names of the hosts for the deployment session
    attr_reader :hosts

    def initialize(hosts, environment = {}) #:nodoc:
      surrogates  :disable => 'web:disable',
                  :enable => 'web:enable',
                  :migrate => 'db:migrate'

      @hosts = hosts
      @environment = DEFAULTS.merge environment
      
      options = {}
      options[:username] = environment[:username] if environment[:username]
      options[:password] = environment[:password] if environment[:password]

      @session = Net::SSH::Multi.start
      @servers = hosts.inject({}) { |servers,host| servers[host] = @session.use(host, options); servers }
    end

    def close #:nodoc:
      @session, session = nil, @session
      session.close if session
    end

  protected
    def environment # :nodoc:
      @environment
    end

    def session # :nodoc:
      @session
    end

    def servers # :nodoc:
      @servers
    end

    def session_for(hosts) # :nodoc:
      if hosts
        @session.on(servers_for(hosts))
      else
        @session
      end
    end

    def servers_for(hosts) # :nodoc:
      @servers.values_at(*hosts)
    end

  private
    def method_missing(symbol, *args)
      if @environment.include?(symbol)
        @environment[symbol]
      elsif symbol.to_s =~ /^(.+)=$/ && !methods.include?($1)
        @environment[$1.to_sym] = args.first
      else
        super
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sbfaulkner-astrovan-0.5.0 lib/astrovan/session.rb
sbfaulkner-astrovan-0.5.2 lib/astrovan/session.rb
sbfaulkner-astrovan-0.5.3 lib/astrovan/session.rb