Class: Jarl::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/jarl/application.rb

Defined Under Namespace

Classes: Instance

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, name, params, base_path) ⇒ Application

Create Application object from application definition:

app_name:

image: blabla
volumes:
  - /host/path:/container/path
ports:
  - 1234:5678
command: blabla


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jarl/application.rb', line 17

def initialize(group, name, params, base_path)
  @group = group
  @name = name
  @base_path = base_path
  @image = params['image'] || fail("Application '#{self}' has no 'image' defined")
  unless @image.is_a?(String)
    fail "Application '#{self}' has invalid 'image' definition, String is expected"
  end
  @volumes = params['volumes'] || []
  unless @volumes.is_a?(Array)
    fail "Application '#{self}' has invalid 'volumes' definition, Array is expected"
  end
  @ports = params['ports'] || []
  unless @ports.is_a?(Array)
    fail "Application '#{self}' has invalid 'ports' definition, Array is expected"
  end
  @environment = params['environment'] || {}
  unless @environment.is_a?(Hash)
    fail "Application '#{self}' has invalid 'environment' definition, Hash is expected"
  end
  @entrypoint = params['entrypoint']
  if @entrypoint && !@entrypoint.is_a?(String)
    fail "Application '#{self}' has invalid 'entrypoint' definition, String is expected"
  end
  @command = params['command']
  if @command && !@command.is_a?(String)
    fail "Application '#{self}' has invalid 'command' definition, String is expected"
  end
  @serial = self.class.next_serial
  @hostname = @name
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



3
4
5
# File 'lib/jarl/application.rb', line 3

def base_path
  @base_path
end

#commandObject (readonly)

Returns the value of attribute command.



4
5
6
# File 'lib/jarl/application.rb', line 4

def command
  @command
end

#entrypointObject (readonly)

Returns the value of attribute entrypoint.



4
5
6
# File 'lib/jarl/application.rb', line 4

def entrypoint
  @entrypoint
end

#environmentObject (readonly)

Returns the value of attribute environment.



4
5
6
# File 'lib/jarl/application.rb', line 4

def environment
  @environment
end

#groupObject (readonly)

Returns the value of attribute group.



3
4
5
# File 'lib/jarl/application.rb', line 3

def group
  @group
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



3
4
5
# File 'lib/jarl/application.rb', line 3

def hostname
  @hostname
end

#imageObject (readonly)

Returns the value of attribute image.



4
5
6
# File 'lib/jarl/application.rb', line 4

def image
  @image
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/jarl/application.rb', line 3

def name
  @name
end

#portsObject (readonly)

Returns the value of attribute ports.



4
5
6
# File 'lib/jarl/application.rb', line 4

def ports
  @ports
end

#serialObject (readonly)

Returns the value of attribute serial.



5
6
7
# File 'lib/jarl/application.rb', line 5

def serial
  @serial
end

#volumesObject (readonly)

Returns the value of attribute volumes.



4
5
6
# File 'lib/jarl/application.rb', line 4

def volumes
  @volumes
end

Class Method Details

.next_serialObject



123
124
125
126
127
# File 'lib/jarl/application.rb', line 123

def self.next_serial
  @current_serial ||= 0
  @current_serial += 1
  @current_serial - 1
end

Instance Method Details

#buildObject



99
100
101
# File 'lib/jarl/application.rb', line 99

def build
  Docker::Image.new(image_name, image_full_path).build!
end

#execute(execute_command, args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/jarl/application.rb', line 82

def execute(execute_command, args)
  Docker.execute(
    name: full_name,
    hostname: hostname,
    image: image_name,
    volumes: volumes,
    ports: ports,
    environment: environment,
    command: (execute_command || command || '') + ' ' + args.join(' ')
  )
end

#full_nameObject



49
50
51
# File 'lib/jarl/application.rb', line 49

def full_name
  "#{group}.#{name}"
end

#image_full_pathObject



115
116
117
# File 'lib/jarl/application.rb', line 115

def image_full_path
  image_is_a_path? ? Pathname.new(base_path).join(image).expand_path : nil
end

#image_is_a_path?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/jarl/application.rb', line 53

def image_is_a_path?
  image =~ /^[~\.\/]/
end

#image_is_a_registry_path?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/jarl/application.rb', line 57

def image_is_a_registry_path?
  image =~ /^.+:\d+\//
end

#image_nameObject



107
108
109
# File 'lib/jarl/application.rb', line 107

def image_name
  image_is_a_path? ? File.basename(image) : image
end

#image_pathObject



111
112
113
# File 'lib/jarl/application.rb', line 111

def image_path
  image_is_a_path? ? image : nil
end

#instancesObject



65
66
67
68
69
# File 'lib/jarl/application.rb', line 65

def instances
  Docker.containers_running.select { |c| c.name =~ /^#{full_name}(\.\d+)?$/ }.map do |c|
    Instance.new(self, c)
  end
end

#pullObject



103
104
105
# File 'lib/jarl/application.rb', line 103

def pull
  Docker::Image.new(image_name, image_full_path).pull!
end

#running?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/jarl/application.rb', line 61

def running?
  instances.size > 0
end

#ssh(command = nil) ⇒ Object



94
95
96
97
# File 'lib/jarl/application.rb', line 94

def ssh(command = nil)
  fail 'Not a running application' unless running?
  instances.first.ssh(command)
end

#start(scale = 1) ⇒ Object

Start scale instances of the application



73
74
75
76
77
78
79
80
# File 'lib/jarl/application.rb', line 73

def start(scale = 1)
  instances.map(&:stop!) if running?
  if scale > 1
    scale.times { |n| Instance.start(self, n + 1) }
  else
    Instance.start(self)
  end
end

#to_sObject



119
120
121
# File 'lib/jarl/application.rb', line 119

def to_s
  full_name
end