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.

{
  :rails => Frameworks::Rails
}

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

Instance Method Details

#config(shell) ⇒ Object

Place-holder method.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.5.0



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

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



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

def config!
  invoke [:config]
end

#deploy!true

Deploys a new project.

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



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

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



114
115
116
117
118
119
120
121
# 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) ⇒ Object

Installs any additional dependencies.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.3.0



199
200
201
202
203
204
205
206
207
# 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!true

Installs the project on the destination server.

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



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

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



353
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
# 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) ⇒ 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



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# 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

#load_framework!Object (protected)

Loads the framework configuration.

Since:

  • 0.3.0



510
511
512
513
514
515
516
517
518
519
520
# 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!Object (protected)

Loads the server configuration.

Raises:

Since:

  • 0.3.0



529
530
531
532
533
534
535
536
537
538
539
# 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

#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



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

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

#migrate(shell) ⇒ Object

Place-holder method.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.3.0



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

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



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

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



131
132
133
134
135
136
137
138
# 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!true

Redeploys a project.

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



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

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



94
95
96
97
98
99
100
101
102
103
104
# 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) ⇒ Object

Place-holder method.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.5.0



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

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



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

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



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

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



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

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



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

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



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

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



166
167
168
169
170
171
172
# 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!true

Sets up the deployment repository for the project.

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



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

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



150
151
152
153
154
155
156
# 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) ⇒ Object

Place-holder method.

Parameters:

  • shell (Shell)

    The remote shell to execute commands through.

Since:

  • 0.5.0



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

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



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

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



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

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



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

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



182
183
184
185
186
187
188
189
# 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!true

Updates the deployed repository of the project.

Returns:

  • (true)

    Indicates that the tasks were successfully completed.

Since:

  • 0.4.0



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

def update!
  invoke [:update]
end