Class: Panoramix::Plugin::DockerUp

Inherits:
Base
  • Object
show all
Defined in:
lib/panoramix/plugin/docker_up.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#shell

Constructor Details

#initialize(dst, src, env, host) ⇒ DockerUp



15
16
17
18
19
20
21
22
# File 'lib/panoramix/plugin/docker_up.rb', line 15

def initialize(dst, src, env, host)
  @dst = dst
  @src = src
  @env = env

  @env = parse_env @env.keys
  @env["DOCKER_HOST"] = "tcp://#{host}" unless host.nil?
end

Instance Attribute Details

#dstObject (readonly)

Returns the value of attribute dst.



11
12
13
# File 'lib/panoramix/plugin/docker_up.rb', line 11

def dst
  @dst
end

#envObject (readonly)

Returns the value of attribute env.



13
14
15
# File 'lib/panoramix/plugin/docker_up.rb', line 13

def env
  @env
end

#srcObject (readonly)

Returns the value of attribute src.



12
13
14
# File 'lib/panoramix/plugin/docker_up.rb', line 12

def src
  @src
end

Instance Method Details

#clobberObject

Action clobber for this task



48
49
50
# File 'lib/panoramix/plugin/docker_up.rb', line 48

def clobber
  shell("docker-compose -p #{@dst} -f #{@src} kill; docker-compose -p #{@dst} -f #{@src} rm -v --force", false, @env) if created?
end

#created?Boolean

Has this image already been created



37
38
39
40
# File 'lib/panoramix/plugin/docker_up.rb', line 37

def created?
  info = shell("docker-compose -p #{@dst} -f #{@src} ps -q", true, @env)[:out]
  @created = info = info.empty? ? nil: info
end

#idObject



81
82
83
84
85
86
87
# File 'lib/panoramix/plugin/docker_up.rb', line 81

def id
  info = created?
  if info
    containers = info.split("\n")
    puts containers
  end
end

#logsObject

Print docker-compose project logs



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/panoramix/plugin/docker_up.rb', line 90

def logs
        info = created?
        if info
containers = info.split("\n")
colors = ['red', 'green','yellow','blue','magenta','cyan']
containers.each do |c|
  out = shell("docker logs #{c} | tail -n 200", true, @env)
  name = shell("docker inspect -f {{.Name}} #{c}", true, @env)[:out]
  name.gsub!("\n", "")
  name.gsub!("/", "")
  name = eval %& "#{name} >".#{colors.first}&
  colors.push colors.shift
  out[:out].split("\n").each{ |out| puts name + out }
  out[:err].split("\n").each{ |out| puts name + out }
end
        end
end

#needed?(timestamps) ⇒ Boolean

When this instance needs to be executed



53
54
55
56
# File 'lib/panoramix/plugin/docker_up.rb', line 53

def needed? timestamps
  this_time = timestamp
  timestamps.any? { |t| t > this_time }
end

#psObject

Print docker-compose ps



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/panoramix/plugin/docker_up.rb', line 109

def ps
  puts "Service: #{@dst}"
        if created?
     puts "Timestamp: #{timestamp}"
     info = shell("docker-compose -p #{@dst} -f #{@src} ps", true, @env)[:out]
  puts info
        else
puts "Timestamp: Not created"
        end
        puts
end

#rmObject

Action rm for this task



43
44
45
# File 'lib/panoramix/plugin/docker_up.rb', line 43

def rm
  clobber
end

#run_defaultObject

Default action for this task



59
60
61
62
63
64
65
66
67
# File 'lib/panoramix/plugin/docker_up.rb', line 59

def run_default
# Raise an exception if already has been created
if created?
  message = I18n.t('errors.docker_up.cluster_deployed', {:id => @dst})
  raise(DockerUpExceptionError, message)
end
   # Run docker-compose up otherwise
  shell("docker-compose -p #{dst} -f #{@src}  up -d", false, @env)
end

#startObject



75
76
77
78
79
# File 'lib/panoramix/plugin/docker_up.rb', line 75

def start
  if created?
    shell("docker-compose -p #{dst} -f #{@src}  up -d", false, @env)
  end
end

#stopObject



69
70
71
72
73
# File 'lib/panoramix/plugin/docker_up.rb', line 69

def stop
  if created?
    shell("docker-compose -p #{dst} -f #{@src} stop", false, @env)
  end
end

#timestampObject

Return current timestamp for the container



25
26
27
28
29
30
31
32
33
34
# File 'lib/panoramix/plugin/docker_up.rb', line 25

def timestamp
  info = created?
  if info
    id = info.split("\n").first.strip
    time = shell("docker inspect -f {{.Created}} #{id}", true, @env)[:out]
    return Time.parse(time)
  else
    Time.at 0
  end
end