Sha256: fd538e1e91d265e57d3ba8c3851b0476b4abcd236f18968ca912d8f7fbb12d87

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

require 'securerandom'

class Instance
  attr_reader :name
  attr_reader :config
  attr_writer :dsl

  def initialize(name, config)
    @dsl    = nil
    @name   = name
    @config = {shared_on_host: '/tmp'}.merge(config)
  end

  def has_role?(role)
    return false unless @config.key? :roles

    role = role.to_s if role.is_a? Symbol

    @config[:roles].include? role
  end

  def container_id
    return @name unless @config.key? :container_id

    @config[:container_id]
  end

  def container_role
    "container_#{@name}".to_sym
  end

  def upload!(src, target)
    if @dsl.local_stage?
      self.cp(src, "#{container_id}:#{target}")
      return
    end

    tmp = "#{@config[:shared_on_host]}/capistrano-docker.#{SecureRandom.uuid}.tmp"

    @dsl.upload!(src, tmp)

    self.cp(tmp, "#{container_id}:#{target}")

    @dsl.execute("rm -rf #{tmp}")
  end

  def download!(src, target)
    if @dsl.local_stage?
      self.cp("#{container_id}:#{src}", target)
      return
    end

    tmp = "#{@config[:shared_on_host]}/capistrano-docker.#{SecureRandom.uuid}.tmp"

    self.cp("#{container_id}:#{src}", tmp)

    @dsl.download!(tmp, target)

    @dsl.execute("rm -rf #{tmp}")
  end

  def execute(command)
    command = "docker exec -i #{container_id} sh -c '#{command.gsub("'", "\'")}'"

    @dsl.execute_local_or_remote(command)
  end

  def cp(src, target)
    command = "docker cp #{src} #{target}"

    @dsl.execute_local_or_remote(command)
  end

  def invoke(task_name)
    @dsl.invoke "container:#{@name}:#{task_name}"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
capistrano-container-0.0.5 lib/capistrano/container/instance.rb