Class: Marionetta::Manipulators::Deployer

Inherits:
Object
  • Object
show all
Includes:
Commandable
Defined in:
lib/marionetta/manipulators/deployer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ Deployer

The keys ‘[:deployer]` and `[:deployer]` must be set in your `server` hash in order for `Deployer` to work.



38
39
40
# File 'lib/marionetta/manipulators/deployer.rb', line 38

def initialize(server)
  @server = server
end

Class Method Details

.tasksObject

‘Deployer` provides two rake tasks when used with `RakeHelper` namely `:deploy` and `:rollback`. When applied through `RakeHelper` they will appear namespaced under `:deployer` and your group name.

With a group name of ‘:staging` would appear as:

deployer:staging:deploy
deployer:staging:rollback


28
29
30
# File 'lib/marionetta/manipulators/deployer.rb', line 28

def self.tasks()
  [:deploy, :rollback]
end

Instance Method Details

#can?Boolean

Call ‘.can?()` to check if the correct keys have be passed in as the server.

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
# File 'lib/marionetta/manipulators/deployer.rb', line 45

def can?()
  d = server[:deployer]
  
  if d.has_key?(:from) and d.has_key?(:to)
    return true
  else
    return false
  end
end

#cleanObject

Delete release directories on remote machine.



139
140
141
142
143
144
145
146
147
148
# File 'lib/marionetta/manipulators/deployer.rb', line 139

def clean()
  rels = releases()
  rels.pop()

  unless rels.empty?
    rm = ['rm', '-rf'].concat(rels.map {|r| release_dir(r)})
    rm << release_dir('skip-*')
    cmd.ssh(rm)
  end
end

#deployObject

Run a deploy to your remote server. The process involves:

- `:from` directory rsync'd to remote cache directory
  with `:exclude` files being ignored
- cache directory copied on remote machine to
  releases directory
- `:before_script` and `:before_scripts` run
- release directory symlinked to a current directory
- `:after_script` and `:before_scripts` run

The directory structure under ‘server[:to]` looks something like this:

cache/
current/ -> ./releases/2012-09-20_14:04:39
releases/
  2012-09-20_13:59:15
  2012-09-20_14:04:39


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/marionetta/manipulators/deployer.rb', line 83

def deploy()
  release = create_release_name()

  write_deploy_version_file()

  DirectorySync.sync(server, from_dir, cache_dir, server[:deployer])
  copy_cache_dir_to_release(release)

  symlink_shared_directories(release)

  send_scripts()
  run_script(:before, release)
  symlink_release_dir(release)
  run_script(:after, release)
end

#releasesObject

Get an array of all releases call ‘.releases()`. Any release that is subsequently rolled back will not be listed.



116
117
118
# File 'lib/marionetta/manipulators/deployer.rb', line 116

def releases()
  releases_including_skipped.delete_if {|r| r =~ /^skip-/}
end

#releases_including_skippedObject

Get an array of all releases including those which have been rolled back (skipped).



102
103
104
105
106
107
108
109
110
# File 'lib/marionetta/manipulators/deployer.rb', line 102

def releases_including_skipped()
  files = []

  cmd.ssh("ls -m #{releases_dir}") do |stdout|
    files.concat(stdout.read.split(/[,\s]+/))
  end

  return files
end

#rollbackObject

If you push out and need to rollback to the previous version you can use ‘.rollback()` to do just that. Currently you can only rollback once at a time.



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/marionetta/manipulators/deployer.rb', line 124

def rollback()
  rollback_to_release = releases[-2]

  if rollback_to_release.nil?
    server[:logger].warn('No release to rollback to')
  else
    current_release_dir = release_dir(releases.last)
    skip_current_release_dir = release_dir("skip-#{releases.last}")
    cmd.ssh("mv #{current_release_dir} #{skip_current_release_dir}")
    symlink_release_dir(rollback_to_release)
  end
end

#setupObject

Setup deploy environment on remote server.



59
60
61
# File 'lib/marionetta/manipulators/deployer.rb', line 59

def setup()
  create_directories
end