Class: DeploYML::Environment

Inherits:
Configuration show all
Defined in:
lib/deployml/environment.rb

Overview

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

Constant Summary collapse

SERVERS =

Mapping of possible 'server' names to their mixins.

{
  :apache => Servers::Apache,
  :mongrel => Servers::Mongrel,
  :thin => Servers::Thin
}
FRAMEWORKS =

Mapping of possible 'framework' names to their mixins.

{
  :rails2 => Frameworks::Rails2,
  :rails3 => Frameworks::Rails3
}

Constants inherited from Configuration

Configuration::DEFAULT_SCM, Configuration::TASKS

Instance Attribute Summary

Attributes inherited from Configuration

#after, #before, #bundler, #debug, #dest, #environment, #framework, #orm, #server_name, #server_options, #source

Instance Method Summary collapse

Methods inherited from Configuration

#each_dest, #normalize, #normalize_array, #normalize_hash, #parse_address, #parse_commands, #parse_dest, #parse_server

Constructor Details

#initialize(name, config = {}) ⇒ Environment

Creates a new deployment environment.

Parameters:

  • name (Symbol, String)

    The name of the deployment environment.

  • config (Hash{String => Object}) (defaults to: {})

    Environment specific configuration.

Raises:

  • (MissingOption)

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

Since:

  • 0.3.0



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/deployml/environment.rb', line 45

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

Instance Method Details

#config(shell) ⇒ Object

Place-holder method.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.5.0



273
274
275
# File 'lib/deployml/environment.rb', line 273

def config(shell)
  server_config(shell)
end

#config!true

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

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



440
441
442
# File 'lib/deployml/environment.rb', line 440

def config!
  invoke [:config]
end

#deploy!true

Deploys a new project.

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



488
489
490
# File 'lib/deployml/environment.rb', line 488

def deploy!
  invoke [:setup, :install, :migrate, :config, :start]
end

#exec(command) ⇒ true

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

Returns:

  • (true)

Since:

  • 0.3.0



115
116
117
118
119
120
121
122
# File 'lib/deployml/environment.rb', line 115

def exec(command)
  remote_shell do |shell|
    shell.cd(shell.uri.path)
    shell.exec(command)
  end

  return true
end

#install(shell) ⇒ Object

Installs any additional dependencies.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.3.0



200
201
202
203
204
205
206
207
208
# File 'lib/deployml/environment.rb', line 200

def install(shell)
  if @bundler
    shell.status "Bundling dependencies ..."

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

    shell.status "Dependencies bundled."
  end
end

#install!true

Installs the project on the destination server.

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



416
417
418
# File 'lib/deployml/environment.rb', line 416

def install!
  invoke [:install]
end

#invoke(tasks) ⇒ true

Deploys the project.

Parameters:

  • tasks (Array<Symbol>)

    The tasks to run during the deployment.

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/deployml/environment.rb', line 354

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) ⇒ Object

Invokes a task.

Parameters:

  • task (Symbol)

    The name of the task to run.

  • shell (Shell)

    The shell to run the task in.

Raises:

  • (RuntimeError)

    The task name was not known.

Since:

  • 0.5.0



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/deployml/environment.rb', line 327

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

#load_framework!Object (protected)

Loads the framework configuration.

Since:

  • 0.3.0



511
512
513
514
515
516
517
518
519
520
521
# File 'lib/deployml/environment.rb', line 511

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!Object (protected)

Loads the server configuration.

Raises:

Since:

  • 0.3.0



530
531
532
533
534
535
536
537
538
539
540
# File 'lib/deployml/environment.rb', line 530

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

#local_shell {|shell| ... } ⇒ Array<LocalShell>

Creates a local shell.

Yields:

  • (shell)

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

Yield Parameters:

  • shell (LocalShell)

    The remote shell session.

Returns:

Since:

  • 0.3.0



76
77
78
# File 'lib/deployml/environment.rb', line 76

def local_shell(&block)
  each_dest.map { |dest| LocalShell.new(dest,&block) }
end

#migrate(shell) ⇒ Object

Place-holder method.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.3.0



218
219
# File 'lib/deployml/environment.rb', line 218

def migrate(shell)
end

#migrate!true

Migrates the database used by the project.

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



428
429
430
# File 'lib/deployml/environment.rb', line 428

def migrate!
  invoke [:migrate]
end

#rake(task, *arguments) ⇒ true

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

Returns:

  • (true)

Since:

  • 0.3.0



132
133
134
135
136
137
138
139
# File 'lib/deployml/environment.rb', line 132

def rake(task,*arguments)
  remote_shell do |shell|
    shell.cd(shell.uri.path)
    shell.rake(task,*arguments)
  end

  return true
end

#redeploy!true

Redeploys a project.

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



500
501
502
# File 'lib/deployml/environment.rb', line 500

def redeploy!
  invoke [:update, :install, :migrate, :restart]
end

#remote_shell {|shell| ... } ⇒ Array<RemoteShell, LocalShell>

Creates a remote shell with the destination server.

Yields:

  • (shell)

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

Yield Parameters:

Returns:

  • (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



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/deployml/environment.rb', line 95

def remote_shell(&block)
  each_dest.map do |dest|
    shell = if dest.scheme == 'file'
              LocalShell
            else
              RemoteShell
            end

    shell.new(dest,&block)
  end
end

#restart(shell) ⇒ Object

Place-holder method.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.5.0



309
310
311
# File 'lib/deployml/environment.rb', line 309

def restart(shell)
  server_restart(shell)
end

#restart!true

Restarts the Web server for the project.

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



476
477
478
# File 'lib/deployml/environment.rb', line 476

def restart!
  invoke [:restart]
end

#server_config(shell) ⇒ Object

Place-holder method.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.3.0



229
230
# File 'lib/deployml/environment.rb', line 229

def server_config(shell)
end

#server_restart(shell) ⇒ Object

Place-holder method.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.3.0



262
263
# File 'lib/deployml/environment.rb', line 262

def server_restart(shell)
end

#server_start(shell) ⇒ Object

Place-holder method.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.3.0



240
241
# File 'lib/deployml/environment.rb', line 240

def server_start(shell)
end

#server_stop(shell) ⇒ Object

Place-holder method.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.3.0



251
252
# File 'lib/deployml/environment.rb', line 251

def server_stop(shell)
end

#setup(shell) ⇒ Object

Sets up the deployment repository for the project.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.3.0



167
168
169
170
171
172
173
# File 'lib/deployml/environment.rb', line 167

def setup(shell)
  shell.status "Cloning #{@source} ..."

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

  shell.status "Cloned #{@source}."
end

#setup!true

Sets up the deployment repository for the project.

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



392
393
394
# File 'lib/deployml/environment.rb', line 392

def setup!
  invoke [:setup]
end

#ssh(*arguments) ⇒ true

Starts an SSH session with the destination server.

Parameters:

  • arguments (Array)

    Additional arguments to pass to SSH.

Returns:

  • (true)

Since:

  • 0.3.0



151
152
153
154
155
156
157
# File 'lib/deployml/environment.rb', line 151

def ssh(*arguments)
  each_dest do |dest|
    RemoteShell.new(dest).ssh(*arguments)
  end

  return true
end

#start(shell) ⇒ Object

Place-holder method.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.5.0



285
286
287
# File 'lib/deployml/environment.rb', line 285

def start(shell)
  server_start(shell)
end

#start!true

Starts the Web server for the project.

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



452
453
454
# File 'lib/deployml/environment.rb', line 452

def start!
  invoke [:start]
end

#stop(shell) ⇒ Object

Place-holder method.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.5.0



297
298
299
# File 'lib/deployml/environment.rb', line 297

def stop(shell)
  server_stop(shell)
end

#stop!true

Stops the Web server for the project.

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



464
465
466
# File 'lib/deployml/environment.rb', line 464

def stop!
  invoke [:stop]
end

#update(shell) ⇒ Object

Updates the deployed repository for the project.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.3.0



183
184
185
186
187
188
189
190
# File 'lib/deployml/environment.rb', line 183

def update(shell)
  shell.status "Updating ..."

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

  shell.status "Updated."
end

#update!true

Updates the deployed repository of the project.

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



404
405
406
# File 'lib/deployml/environment.rb', line 404

def update!
  invoke [:update]
end