Class: Instance
- Inherits:
-
Object
- Object
- Instance
- Defined in:
- lib/capistrano/container/instance.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#dsl ⇒ Object
writeonly
Sets the attribute dsl.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #container_id ⇒ Object
- #container_role ⇒ Object
- #cp(src, target) ⇒ Object
- #download!(src, target) ⇒ Object
- #execute(command) ⇒ Object
- #has_role?(role) ⇒ Boolean
-
#initialize(name, config) ⇒ Instance
constructor
A new instance of Instance.
- #invoke(task_name) ⇒ Object
- #upload!(src, target) ⇒ Object
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
#config ⇒ Object (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
6 7 8 |
# File 'lib/capistrano/container/instance.rb', line 6 def dsl=(value) @dsl = value end |
#name ⇒ Object (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_id ⇒ Object
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_role ⇒ Object
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
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 |