Sha256: c0cb569fe0d1fe92a635d262eb726bbcfe49a6ccda8010bd92b8c509bb3fb08f

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 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)
    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)
    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} bash -c '#{command.gsub("'", "\'")}'"

    @dsl.execute(command)
  end

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

    @dsl.execute(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.2 lib/capistrano/container/instance.rb