class Dockerploy::Deploy

Constants

HTTP_PORT
SSH_PORT

Public Class Methods

new(config, options = {}) click to toggle source
# File lib/dockerploy/deploy.rb, line 6
def initialize(config, options = {})
  @config = config
  @options = options
end

Public Instance Methods

deploy() click to toggle source
# File lib/dockerploy/deploy.rb, line 29
def deploy
  create_tag if @config.tagging?
  @config.servers.each do |server|
    run_before_hooks(server)
    ssh_client = SSHClient.new(server[:host], server[:username], server[:password], server[:port])
    destroy(ssh_client, server)
    run(ssh_client, server)
    run_after_hooks(server)
  end
end
destroy(ssh_client, server) click to toggle source
# File lib/dockerploy/deploy.rb, line 11
def destroy(ssh_client, server)
  container_name = sprintf('%s_%s', @config.application_name, server[:container][:http_port])
  ssh_client.command(sprintf('docker rm -f %s', container_name))
end
run(ssh_client, server) click to toggle source
# File lib/dockerploy/deploy.rb, line 16
def run(ssh_client, server)
  option_delimiter = ' '
  command = sprintf('docker run -d --name %s_%s', @config.application_name, server[:container][:http_port])
  command << option_delimiter + hostname_option
  command << option_delimiter + port_option(server)
  command << option_delimiter + volume_option
  command << option_delimiter + environment_variables_option
  custom_variables = custom_environment_variables_option(server)
  command << option_delimiter + custom_variables if custom_variables.length > 0
  command << option_delimiter + image_name
  ssh_client.command(command)
end
run_after_hooks(server) click to toggle source
# File lib/dockerploy/deploy.rb, line 45
def run_after_hooks(server)
  @config.after_all_hooks.each { |hook| run_hook(server, hook) }
  @config.after_hooks.each { |hook| run_hook(server, hook) }
end
run_before_hooks(server) click to toggle source
# File lib/dockerploy/deploy.rb, line 40
def run_before_hooks(server)
  @config.before_all_hooks.each { |hook| run_hook(server, hook) }
  @config.before_hooks.each { |hook| run_hook(server, hook) }
end
run_hook(server, hook) click to toggle source
# File lib/dockerploy/deploy.rb, line 50
def run_hook(server, hook)
  if hook[:local]
    hook[:local].each do |command|
      ShellClient.new(abort_on_failure: true).command(command)
    end
  elsif hook[:remote]
    hook[:remote].each do |command|
      SSHClient.new(server[:host], server[:username], server[:password], server[:port]).command(command)
    end
  end
end

Private Instance Methods

create_tag() click to toggle source
# File lib/dockerploy/deploy.rb, line 63
def create_tag
  tag_name = sprintf('%s-%s', @config.env, Time.now.strftime('%Y%m%dT%H%M'))
  ShellClient.new.command(sprintf('git tag %s %s', tag_name, @config.branch))
  ShellClient.new.command(sprintf('git push origin %s', tag_name))
end
custom_environment_variables_option(server) click to toggle source
# File lib/dockerploy/deploy.rb, line 92
def custom_environment_variables_option(server)
  result = ''
  variables = server[:custom_variables]
  if variables
    variables.each_pair do |key, value|
      result << sprintf("-e %s='%s' ", key.upcase, value)
    end
  end
  result.strip
end
environment_variables_option() click to toggle source
# File lib/dockerploy/deploy.rb, line 78
def environment_variables_option
  env_file = @config.env_file
  result = ''
  if env_file
    if File.exist?(env_file)
      environment_variables = YAML.load_file(env_file).symbolize_keys
      environment_variables.each do |k, v|
        result << sprintf("-e %s='%s' ", k.upcase, v)
      end
    end
  end
  result.strip
end
hostname_option() click to toggle source
# File lib/dockerploy/deploy.rb, line 110
def hostname_option
  sprintf('--hostname %s', @config.application_name)
end
image_name() click to toggle source
# File lib/dockerploy/deploy.rb, line 114
def image_name
  tag = @options[:tag] || @config.image_tag
  tag ? sprintf('%s:%s', @config.image_name, tag) : @config.image_name
end
port_option(server) click to toggle source
# File lib/dockerploy/deploy.rb, line 103
def port_option(server)
  container_config = server[:container]
  result = ''
  result << sprintf('-p %s:%s:%s ', container_config[:host], container_config[:ssh_port], SSH_PORT)
  result << sprintf('-p %s:%s:%s', container_config[:host], container_config[:http_port], HTTP_PORT)
end
volume_option() click to toggle source
# File lib/dockerploy/deploy.rb, line 69
def volume_option
  volumes = @config.volumes
  result = ''
  if volumes
    volumes.each { |volume| result << sprintf('-v %s:%s ', volume[:volume][:host], volume[:volume][:guest]) }
  end
  result
end