class DeploYML::Environment

Contains environment specific configuration loaded by {Project} from YAML files within `config/deploy/`.

Constants

FRAMEWORKS

Mapping of possible 'framework' names to their mixins.

SERVERS

Mapping of possible 'server' names to their mixins.

Public Class Methods

new(name,config={}) click to toggle source

Creates a new deployment environment.

@param [Symbol, String] name

The name of the deployment environment.

@param [Hash{String => Object}] config

Environment specific configuration.

@raise [MissingOption]

Either the `source` or `dest` options were not specified in the
confirmation.

@since 0.3.0

Calls superclass method DeploYML::Configuration::new
# File lib/deployml/environment.rb, line 44
def initialize(name,config={})
  super(config)

  unless @source
    raise(MissingOption,":source option is missing for the #{@name} environment",caller)
  end

  unless @dest
    raise(MissingOption,":dest option is missing for the #{@name} environment",caller)
  end

  @environment ||= name.to_sym

  load_framework!
  load_server!
end

Public Instance Methods

config(shell) click to toggle source

Place-holder method.

@param [Shell] shell

The remote shell to execute commands through.

@since 0.5.0

# File lib/deployml/environment.rb, line 272
def config(shell)
  server_config(shell)
end
config!() click to toggle source

Configures the Web server to be ran on the destination server.

@return [true]

Indicates that the tasks were successfully completed.

@since 0.4.0

# File lib/deployml/environment.rb, line 439
def config!
  invoke [:config]
end
deploy!() click to toggle source

Deploys a new project.

@return [true]

Indicates that the tasks were successfully completed.

@since 0.4.0

# File lib/deployml/environment.rb, line 487
def deploy!
  invoke [:setup, :install, :migrate, :config, :start]
end
exec(command) click to toggle source

Runs a command on the destination server, in the destination directory.

@return [true]

@since 0.3.0

# File lib/deployml/environment.rb, line 114
def exec(command)
  remote_shell do |shell|
    shell.cd(shell.uri.path)
    shell.exec(command)
  end

  return true
end
install(shell) click to toggle source

Installs any additional dependencies.

@param [Shell] shell

The remote shell to execute commands through.

@since 0.3.0

# File lib/deployml/environment.rb, line 199
def install(shell)
  if @bundler
    shell.status "Bundling dependencies ..."

    shell.run 'bundle', 'install', '--deployment'

    shell.status "Dependencies bundled."
  end
end
install!() click to toggle source

Installs the project on the destination server.

@return [true]

Indicates that the tasks were successfully completed.

@since 0.4.0

# File lib/deployml/environment.rb, line 415
def install!
  invoke [:install]
end
invoke(tasks) click to toggle source

Deploys the project.

@param [Array<Symbol>] tasks

The tasks to run during the deployment.

@return [true]

Indicates that the tasks were successfully completed.

@since 0.4.0

# File lib/deployml/environment.rb, line 353
def invoke(tasks)
  remote_shell do |shell|
    # setup the deployment repository
    invoke_task(:setup,shell) if tasks.include?(:setup)

    # cd into the deployment repository
    shell.cd(shell.uri.path)

    # update the deployment repository
    invoke_task(:update,shell) if tasks.include?(:update)

    # framework tasks
    invoke_task(:install,shell) if tasks.include?(:install)
    invoke_task(:migrate,shell) if tasks.include?(:migrate)

    # server tasks
    if tasks.include?(:config)
      invoke_task(:config,shell)
    elsif tasks.include?(:start)
      invoke_task(:start,shell)
    elsif tasks.include?(:stop)
      invoke_task(:stop,shell)
    elsif tasks.include?(:restart)
      invoke_task(:restart,shell)
    end
  end

  return true
end
invoke_task(task,shell) click to toggle source

Invokes a task.

@param [Symbol] task

The name of the task to run.

@param [Shell] shell

The shell to run the task in.

@raise [RuntimeError]

The task name was not known.

@since 0.5.0

# File lib/deployml/environment.rb, line 326
def invoke_task(task,shell)
  unless TASKS.include?(task)
    raise("invalid task: #{task}")
  end

  if @before.has_key?(task)
    @before[task].each { |command| shell.exec(command) }
  end

  send(task,shell) if respond_to?(task)

  if @after.has_key?(task)
    @after[task].each { |command| shell.exec(command) }
  end
end
local_shell(&block) click to toggle source

Creates a local shell.

@yield [shell]

If a block is given, it will be passed the new local shell.

@yieldparam [LocalShell] shell

The remote shell session.

@return [Array<LocalShell>]

The local shell.

@since 0.3.0

# File lib/deployml/environment.rb, line 75
def local_shell(&block)
  each_dest.map { |dest| LocalShell.new(dest,self,&block) }
end
migrate(shell) click to toggle source

Place-holder method.

@param [Shell] shell

The remote shell to execute commands through.

@since 0.3.0

# File lib/deployml/environment.rb, line 217
def migrate(shell)
end
migrate!() click to toggle source

Migrates the database used by the project.

@return [true]

Indicates that the tasks were successfully completed.

@since 0.4.0

# File lib/deployml/environment.rb, line 427
def migrate!
  invoke [:migrate]
end
rake(task,*arguments) click to toggle source

Executes a Rake task on the destination server, in the destination directory.

@return [true]

@since 0.3.0

# File lib/deployml/environment.rb, line 131
def rake(task,*arguments)
  remote_shell do |shell|
    shell.cd(shell.uri.path)
    shell.rake(task,*arguments)
  end

  return true
end
redeploy!() click to toggle source

Redeploys a project.

@return [true]

Indicates that the tasks were successfully completed.

@since 0.4.0

# File lib/deployml/environment.rb, line 499
def redeploy!
  invoke [:update, :install, :migrate, :restart]
end
remote_shell(&block) click to toggle source

Creates a remote shell with the destination server.

@yield [shell]

If a block is given, it will be passed the new remote shell.

@yieldparam [LocalShell, RemoteShell] shell

The remote shell.

@return [Array<RemoteShell, LocalShell>]

The remote shell. If the destination is a local `file://` URI,
a local shell will be returned instead.

@since 0.3.0

# File lib/deployml/environment.rb, line 94
def remote_shell(&block)
  each_dest.map do |dest|
    shell = if dest.scheme == 'file'
              LocalShell
            else
              RemoteShell
            end

    shell.new(dest,self,&block)
  end
end
restart(shell) click to toggle source

Place-holder method.

@param [Shell] shell

The remote shell to execute commands through.

@since 0.5.0

# File lib/deployml/environment.rb, line 308
def restart(shell)
  server_restart(shell)
end
restart!() click to toggle source

Restarts the Web server for the project.

@return [true]

Indicates that the tasks were successfully completed.

@since 0.4.0

# File lib/deployml/environment.rb, line 475
def restart!
  invoke [:restart]
end
server_config(shell) click to toggle source

Place-holder method.

@param [Shell] shell

The remote shell to execute commands through.

@since 0.3.0

# File lib/deployml/environment.rb, line 228
def server_config(shell)
end
server_restart(shell) click to toggle source

Place-holder method.

@param [Shell] shell

The remote shell to execute commands through.

@since 0.3.0

# File lib/deployml/environment.rb, line 261
def server_restart(shell)
end
server_start(shell) click to toggle source

Place-holder method.

@param [Shell] shell

The remote shell to execute commands through.

@since 0.3.0

# File lib/deployml/environment.rb, line 239
def server_start(shell)
end
server_stop(shell) click to toggle source

Place-holder method.

@param [Shell] shell

The remote shell to execute commands through.

@since 0.3.0

# File lib/deployml/environment.rb, line 250
def server_stop(shell)
end
setup(shell) click to toggle source

Sets up the deployment repository for the project.

@param [Shell] shell

The remote shell to execute commands through.

@since 0.3.0

# File lib/deployml/environment.rb, line 166
def setup(shell)
  shell.status "Cloning #{@source} ..."

  shell.run 'git', 'clone', '--depth', 1, @source, shell.uri.path

  shell.status "Cloned #{@source}."
end
setup!() click to toggle source

Sets up the deployment repository for the project.

@return [true]

Indicates that the tasks were successfully completed.

@since 0.4.0

# File lib/deployml/environment.rb, line 391
def setup!
  invoke [:setup]
end
ssh(*arguments) click to toggle source

Starts an SSH session with the destination server.

@param [Array] arguments

Additional arguments to pass to SSH.

@return [true]

@since 0.3.0

# File lib/deployml/environment.rb, line 150
def ssh(*arguments)
  each_dest do |dest|
    RemoteShell.new(dest).ssh(*arguments)
  end

  return true
end
start(shell) click to toggle source

Place-holder method.

@param [Shell] shell

The remote shell to execute commands through.

@since 0.5.0

# File lib/deployml/environment.rb, line 284
def start(shell)
  server_start(shell)
end
start!() click to toggle source

Starts the Web server for the project.

@return [true]

Indicates that the tasks were successfully completed.

@since 0.4.0

# File lib/deployml/environment.rb, line 451
def start!
  invoke [:start]
end
stop(shell) click to toggle source

Place-holder method.

@param [Shell] shell

The remote shell to execute commands through.

@since 0.5.0

# File lib/deployml/environment.rb, line 296
def stop(shell)
  server_stop(shell)
end
stop!() click to toggle source

Stops the Web server for the project.

@return [true]

Indicates that the tasks were successfully completed.

@since 0.4.0

# File lib/deployml/environment.rb, line 463
def stop!
  invoke [:stop]
end
update(shell) click to toggle source

Updates the deployed repository for the project.

@param [Shell] shell

The remote shell to execute commands through.

@since 0.3.0

# File lib/deployml/environment.rb, line 182
def update(shell)
  shell.status "Updating ..."

  shell.run 'git', 'reset', '--hard', 'HEAD'
  shell.run 'git', 'pull', '-f'

  shell.status "Updated."
end
update!() click to toggle source

Updates the deployed repository of the project.

@return [true]

Indicates that the tasks were successfully completed.

@since 0.4.0

# File lib/deployml/environment.rb, line 403
def update!
  invoke [:update]
end

Protected Instance Methods

load_framework!() click to toggle source

Loads the framework configuration.

@since 0.3.0

# File lib/deployml/environment.rb, line 510
def load_framework!
  if @orm
    unless FRAMEWORKS.has_key?(@framework)
      raise(UnknownFramework,"Unknown framework #{@framework}",caller)
    end

    extend FRAMEWORKS[@framework]

    initialize_framework if respond_to?(:initialize_framework)
  end
end
load_server!() click to toggle source

Loads the server configuration.

@raise [UnknownServer]

@since 0.3.0

# File lib/deployml/environment.rb, line 529
def load_server!
  if @server_name
    unless SERVERS.has_key?(@server_name)
      raise(UnknownServer,"Unknown server name #{@server_name}",caller)
    end

    extend SERVERS[@server_name]

    initialize_server if respond_to?(:initialize_server)
  end
end