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
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
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 |