Class: Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/container/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config) ⇒ Instance

Returns a new instance of Instance.



8
9
10
11
12
# File 'lib/capistrano/container/instance.rb', line 8

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/capistrano/container/instance.rb', line 5

def config
  @config
end

#dsl=(value) ⇒ Object (writeonly)

Sets the attribute dsl

Parameters:

  • value

    the value to set the attribute dsl to.



6
7
8
# File 'lib/capistrano/container/instance.rb', line 6

def dsl=(value)
  @dsl = value
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/capistrano/container/instance.rb', line 4

def name
  @name
end

Instance Method Details

#container_idObject



22
23
24
25
26
# File 'lib/capistrano/container/instance.rb', line 22

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

  @config[:container_id]
end

#container_roleObject



28
29
30
# File 'lib/capistrano/container/instance.rb', line 28

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

#cp(src, target) ⇒ Object



58
59
60
61
62
# File 'lib/capistrano/container/instance.rb', line 58

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

  @dsl.execute(command)
end

#download!(src, target) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/capistrano/container/instance.rb', line 42

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

#execute(command) ⇒ Object



52
53
54
55
56
# File 'lib/capistrano/container/instance.rb', line 52

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

  @dsl.execute(command)
end

#has_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
# File 'lib/capistrano/container/instance.rb', line 14

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

  role = role.to_s if role.is_a? Symbol

  @config[:roles].include? role
end

#invoke(task_name) ⇒ Object



64
65
66
# File 'lib/capistrano/container/instance.rb', line 64

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

#upload!(src, target) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/capistrano/container/instance.rb', line 32

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