Class: DkComposer::Stack
- Inherits:
-
Object
show all
- Includes:
- Helper
- Defined in:
- lib/dkcomposer/stack.rb
Instance Attribute Summary collapse
Attributes included from Helper
#build_path, #longdesc, #shortdesc
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Helper
#config, #deep_clone
Constructor Details
#initialize(name) ⇒ Stack
Returns a new instance of Stack.
5
6
7
8
9
10
11
|
# File 'lib/dkcomposer/stack.rb', line 5
def initialize(name)
@name = name
@services = []
@volumes = {}
@networks = {}
@version = '2'
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
4
5
6
|
# File 'lib/dkcomposer/stack.rb', line 4
def name
@name
end
|
#networks ⇒ Object
Returns the value of attribute networks.
4
5
6
|
# File 'lib/dkcomposer/stack.rb', line 4
def networks
@networks
end
|
#services ⇒ Object
Returns the value of attribute services.
4
5
6
|
# File 'lib/dkcomposer/stack.rb', line 4
def services
@services
end
|
#version(value = nil) ⇒ Object
Returns the value of attribute version.
4
5
6
|
# File 'lib/dkcomposer/stack.rb', line 4
def version
@version
end
|
#volumes ⇒ Object
Returns the value of attribute volumes.
4
5
6
|
# File 'lib/dkcomposer/stack.rb', line 4
def volumes
@volumes
end
|
Class Method Details
.create(name, *params, **opts, &block) ⇒ Object
83
84
85
86
87
88
|
# File 'lib/dkcomposer/stack.rb', line 83
def self.create(name, *params, **opts, &block)
ret = Stack.new(name)
ret.config(opts)
ret.instance_exec(*params, &block) if block
ret
end
|
.load_compose_file(file, name = :auto_generated) ⇒ Object
117
118
119
120
121
122
|
# File 'lib/dkcomposer/stack.rb', line 117
def self.load_compose_file(file, name = :auto_generated)
opts = YAML.load_file(file)
ret = create(name)
ret.from_h(opts.symbolize_key)
ret
end
|
Instance Method Details
#from_h(h) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/dkcomposer/stack.rb', line 102
def from_h(h)
version h[:version]
h[:volumes].each do |k, v|
volume(k, v || {})
end
h[:networks].each do |k, v|
network(k, v || {})
end
h[:services].each do |k, v|
service(k, v)
end
end
|
#network(name, **opts, &block) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/dkcomposer/stack.rb', line 37
def network(name, **opts, &block)
ret = if NETWORK[name]
NETWORK[name].deep_clone
else
Network.create(name)
end
ret.config(opts)
ret.instance_exec(&block) if block
@networks[name] = ret
ret
end
|
#run(*opts, path: build_path) ⇒ Object
53
54
55
56
57
58
59
60
61
|
# File 'lib/dkcomposer/stack.rb', line 53
def run(*opts, path: build_path)
composefile = tempfile(to_yaml, 'docker-compose.yml', path)
up_cmd = [
"pushd #{path}",
"docker-compose -f #{composefile} up #{opts.join(' ')}",
'popd'
].join('&&')
run_command(up_cmd)
end
|
#service(name, **opts, &block) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/dkcomposer/stack.rb', line 13
def service(name, **opts, &block)
ret = if SERVICE[name]
SERVICE[name].deep_clone
else
Service.create(name)
end
ret.config(opts)
ret.instance_exec(&block) if block
@services << ret
ret
end
|
#to_h ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/dkcomposer/stack.rb', line 63
def to_h
ret = {
version: @version,
volumes: @volumes.each_with_object({}) do |(_, v), r|
r.merge! v.to_h
end.stringfy_key,
networks: @networks.each_with_object({}) do |(_, v), r|
r.merge! v.to_h
end.stringfy_key,
services: @services.each_with_object({}) do |s, r|
r.merge! s.to_h
end
}
ret
end
|
#to_s ⇒ Object
Also known as:
dump
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/dkcomposer/stack.rb', line 90
def to_s
ret = "stack(:\#{name}, version: '\#{version}',build_path:\"\#{build_path}\") do\n\#{@volumes.map { |(_, v)| v.to_s }.join(\"\\n\")}\n\#{@networks.map { |(_, v)| v.to_s }.join(\"\\n\")}\n\#{@services.map(&:to_s).join(\"\\n\")}\nend\n"
ret
end
|
#to_yaml ⇒ Object
79
80
81
|
# File 'lib/dkcomposer/stack.rb', line 79
def to_yaml
Hash[to_h.collect { |k, v| [k.to_s, v] }].to_yaml
end
|
#volume(name, **opts, &block) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/dkcomposer/stack.rb', line 25
def volume(name, **opts, &block)
ret = if VOLUME[name]
VOLUME[name].deep_clone
else
Volume.create(name)
end
ret.config(opts)
ret.instance_exec(&block) if block
@volumes[name] = ret
ret
end
|