Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 824d22bfb8
Fetching contributors…

Cannot retrieve contributors at this time

file 350 lines (300 sloc) 11.553 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
require 'erb'
require 'switchtower/command'
require 'switchtower/gateway'
require 'switchtower/ssh'

module SwitchTower

  # An Actor is the entity that actually does the work of determining which
  # servers should be the target of a particular task, and of executing the
  # task on each of them in parallel. An Actor is never instantiated
  # directly--rather, you create a new Configuration instance, and access the
  # new actor via Configuration#actor.
  class Actor

    # An adaptor for making the SSH interface look and act like that of the
    # Gateway class.
    class DefaultConnectionFactory #:nodoc:
      def initialize(config)
        @config= config
      end

      def connect_to(server)
        SSH.connect(server, @config)
      end
    end

    class <<self
      attr_accessor :connection_factory
      attr_accessor :command_factory
    end

    self.connection_factory = DefaultConnectionFactory
    self.command_factory = Command

    # The configuration instance associated with this actor.
    attr_reader :configuration

    # A hash of the tasks known to this actor, keyed by name. The values are
    # instances of Actor::Task.
    attr_reader :tasks

    # A hash of the SSH sessions that are currently open and available.
    # Because sessions are constructed lazily, this will only contain
    # connections to those servers that have been the targets of one or more
    # executed tasks.
    attr_reader :sessions

    # The call stack of the tasks. The currently executing task may inspect
    # this to see who its caller was. The current task is always the last
    # element of this stack.
    attr_reader :task_call_frames

    # The history of executed tasks. This will be an array of all tasks that
    # have been executed, in the order in which they were called.
    attr_reader :task_call_history

    # A struct for representing a single instance of an invoked task.
    TaskCallFrame = Struct.new(:name, :rollback)

    # Represents the definition of a single task.
    class Task #:nodoc:
      attr_reader :name, :options

      def initialize(name, options)
        @name, @options = name, options
      end

      # Returns the list of servers (_not_ connections to servers) that are
      # the target of this task.
      def servers(configuration)
        unless @servers
          roles = [*(@options[:roles] || configuration.roles.keys)].map { |name| configuration.roles[name] or raise ArgumentError, "task #{self.name.inspect} references non-existant role #{name.inspect}" }.flatten
          only = @options[:only] || {}

          unless only.empty?
            roles = roles.delete_if do |role|
              catch(:done) do
                only.keys.each do |key|
                  throw(:done, true) if role.options[key] != only[key]
                end
                false
              end
            end
          end

          @servers = roles.map { |role| role.host }.uniq
        end

        @servers
      end
    end

    def initialize(config) #:nodoc:
      @configuration = config
      @tasks = {}
      @task_call_frames = []
      @sessions = {}
      @factory = self.class.connection_factory.new(configuration)
    end

    # Define a new task for this actor. The block will be invoked when this
    # task is called.
    def define_task(name, options={}, &block)
      @tasks[name] = Task.new(name, options)
      define_method(name) do
        send "before_#{name}" if respond_to? "before_#{name}"
        logger.trace "executing task #{name}"
        begin
          push_task_call_frame name
          result = instance_eval &block
        ensure
          pop_task_call_frame
        end
        send "after_#{name}" if respond_to? "after_#{name}"
        result
      end
    end

    # Execute the given command on all servers that are the target of the
    # current task. If a block is given, it is invoked for all output
    # generated by the command, and should accept three parameters: the SSH
    # channel (which may be used to send data back to the remote process),
    # the stream identifier (<tt>:err</tt> for stderr, and <tt>:out</tt> for
    # stdout), and the data that was received.
    #
    # If +pretend+ mode is active, this does nothing.
    def run(cmd, options={}, &block)
      block ||= Proc.new do |ch, stream, out|
        logger.debug(out, "#{stream} :: #{ch[:host]}")
      end

      logger.debug "executing #{cmd.strip.inspect}"

      # get the currently executing task and determine which servers it uses
      servers = tasks[task_call_frames.last.name].servers(configuration)
      servers = servers.first if options[:once]
      logger.trace "servers: #{servers.inspect}"

      if !pretend
        # establish connections to those servers, as necessary
        establish_connections(servers)

        # execute the command on each server in parallel
        command = self.class.command_factory.new(servers, cmd, block, options, self)
        command.process! # raises an exception if command fails on any server
      end
    end

    # Deletes the given file from all servers targetted by the current task.
    # If <tt>:recursive => true</tt> is specified, it may be used to remove
    # directories.
    def delete(path, options={})
      cmd = "rm -%sf #{path}" % (options[:recursive] ? "r" : "")
      run(cmd, options)
    end

    # Store the given data at the given location on all servers targetted by
    # the current task. If <tt>:mode</tt> is specified it is used to set the
    # mode on the file.
    def put(data, path, options={})
      # Poor-man's SFTP... just run a cat on the remote end, and send data
      # to it.

      cmd = "cat > #{path}"
      cmd << " && chmod #{options[:mode].to_s(8)} #{path}" if options[:mode]
      run(cmd, options.merge(:data => data + "\n\4")) do |ch, stream, out|
        logger.important out, "#{stream} :: #{ch[:host]}" if out == :err
      end
    end

    # Like #run, but executes the command via <tt>sudo</tt>. This assumes that
    # the sudo password (if required) is the same as the password for logging
    # in to the server.
    def sudo(command, options={}, &block)
      block ||= Proc.new do |ch, stream, out|
        logger.debug(out, "#{stream} :: #{ch[:host]}")
      end

      run "sudo #{command}", options do |ch, stream, out|
        if out =~ /^Password:/
          ch.send_data "#{password}\n"
        else
          block.call(ch, stream, out)
        end
      end
    end

    # Renders an ERb template and returns the result. This is useful for
    # dynamically building documents to store on the remote servers.
    #
    # Usage:
    #
    # render("something", :foo => "hello")
    # look for "something.rhtml" in the current directory, or in the
    # switchtower/recipes/templates directory, and render it with
    # foo defined as a local variable with the value "hello".
    #
    # render(:file => "something", :foo => "hello")
    # same as above
    #
    # render(:template => "<%= foo %> world", :foo => "hello")
    # treat the given string as an ERb template and render it with
    # the given hash of local variables active.
    def render(*args)
      options = args.last.is_a?(Hash) ? args.pop : {}
      options[:file] = args.shift if args.first.is_a?(String)
      raise ArgumentError, "too many parameters" unless args.empty?

      case
        when options[:file]
          file = options.delete :file
          unless file[0] == ?/
            dirs = [".",
              File.join(File.dirname(__FILE__), "recipes", "templates")]
            dirs.each do |dir|
              if File.file?(File.join(dir, file))
                file = File.join(dir, file)
                break
              elsif File.file?(File.join(dir, file + ".rhtml"))
                file = File.join(dir, file + ".rhtml")
                break
              end
            end
          end

          render options.merge(:template => File.read(file))

        when options[:template]
          erb = ERB.new(options[:template])
          b = Proc.new { binding }.call
          options.each do |key, value|
            next if key == :template
            eval "#{key} = options[:#{key}]", b
          end
          erb.result(b)

        else
          raise ArgumentError, "no file or template given for rendering"
      end
    end

    # Inspects the remote servers to determine the list of all released versions
    # of the software. Releases are sorted with the most recent release last.
    def releases
      unless @releases
        buffer = ""
        run "ls -x1 #{releases_path}", :once => true do |ch, str, out|
          buffer << out if str == :out
          raise "could not determine releases #{out.inspect}" if str == :err
        end
        @releases = buffer.split.sort
      end

      @releases
    end

    # Returns the most recent deployed release
    def current_release
      release_path(releases.last)
    end

    # Returns the release immediately before the currently deployed one
    def previous_release
      release_path(releases[-2])
    end

    # Invoke a set of tasks in a transaction. If any task fails (raises an
    # exception), all tasks executed within the transaction are inspected to
    # see if they have an associated on_rollback hook, and if so, that hook
    # is called.
    def transaction
      if task_call_history
        yield
      else
        logger.info "transaction: start"
        begin
          @task_call_history = []
          yield
          logger.info "transaction: commit"
        rescue Object => e
          current = task_call_history.last
          logger.important "transaction: rollback", current ? current.name : "transaction start"
          task_call_history.reverse.each do |task|
            begin
              logger.debug "rolling back", task.name
              task.rollback.call if task.rollback
            rescue Object => e
              logger.info "exception while rolling back: #{e.class}, #{e.message}", task.name
            end
          end
          raise
        ensure
          @task_call_history = nil
        end
      end
    end

    # Specifies an on_rollback hook for the currently executing task. If this
    # or any subsequent task then fails, and a transaction is active, this
    # hook will be executed.
    def on_rollback(&block)
      task_call_frames.last.rollback = block
    end

    private

      def metaclass
        class << self; self; end
      end

      def define_method(name, &block)
        metaclass.send(:define_method, name, &block)
      end

      def push_task_call_frame(name)
        frame = TaskCallFrame.new(name)
        task_call_frames.push frame
        task_call_history.push frame if task_call_history
      end

      def pop_task_call_frame
        task_call_frames.pop
      end

      def establish_connections(servers)
        @factory = establish_gateway if needs_gateway?
        servers.each do |server|
          @sessions[server] ||= @factory.connect_to(server)
        end
      end

      def establish_gateway
        logger.debug "establishing connection to gateway #{gateway}"
        @established_gateway = true
        Gateway.new(gateway, configuration)
      end

      def needs_gateway?
        gateway && !@established_gateway
      end

      def method_missing(sym, *args, &block)
        if @configuration.respond_to?(sym)
          @configuration.send(sym, *args, &block)
        else
          super
        end
      end
  end
end
Something went wrong with that request. Please try again.