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



68
69
70
71
72
# File 'lib/capistrano/container/instance.rb', line 68

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

  @dsl.execute_local_or_remote(command)
end

#download!(src, target) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/capistrano/container/instance.rb', line 47

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

#execute(command) ⇒ Object



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

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

  @dsl.execute_local_or_remote(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



74
75
76
# File 'lib/capistrano/container/instance.rb', line 74

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

#upload!(src, target) ⇒ Object



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

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