chef-metal-docker 0.2 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,156 +0,0 @@
1
- module ChefMetalDocker
2
- module Helpers
3
- module Container
4
- # These are helper functions that the Chef::Provider::DockerContainer class
5
- # will use to to help execute commands and analyze the current system
6
- module Helpers
7
-
8
- def cidfile
9
- if service?
10
- new_resource.cidfile || "/var/run/#{service_name}.cid"
11
- else
12
- new_resource.cidfile
13
- end
14
- end
15
-
16
- def container_command_matches_if_exists?(command)
17
- return true if new_resource.command.nil?
18
- # try the exact command but also the command with the ' and " stripped out, since docker will
19
- # sometimes strip out quotes.
20
- subcommand = new_resource.command.gsub(/['"]/, '')
21
- command.include?(new_resource.command) || command.include?(subcommand)
22
- end
23
-
24
- def container_id_matches?(id)
25
- if new_resource.id == nil
26
- false
27
- else
28
- id.start_with?(new_resource.id)
29
- end
30
- end
31
-
32
- def container_image_matches?(image)
33
- if new_resource.image == nil
34
- false
35
- else
36
- image.include?(new_resource.image)
37
- end
38
- end
39
-
40
- def container_name_matches_if_exists?(names)
41
- return false if new_resource.container_name && new_resource.container_name != names
42
- true
43
- end
44
-
45
- def container_name
46
- if service?
47
- new_resource.container_name || new_resource.image.gsub(/^.*\//, '')
48
- else
49
- new_resource.container_name
50
- end
51
- end
52
-
53
- # Helper method for `docker_containers` that looks at the position of the headers in the output of
54
- # `docker ps` to figure out the span of the data for each column within a row. This information is
55
- # stored in the `ranges` hash, which is returned at the end.
56
- def get_ranges(header)
57
- container_id_index = 0
58
- image_index = header.index('IMAGE')
59
- command_index = header.index('COMMAND')
60
- created_index = header.index('CREATED')
61
- status_index = header.index('STATUS')
62
- ports_index = header.index('PORTS')
63
- names_index = header.index('NAMES')
64
-
65
- ranges = {
66
- :id => [container_id_index, image_index],
67
- :image => [image_index, command_index],
68
- :command => [command_index, created_index],
69
- :created => [created_index, status_index]
70
- }
71
- if ports_index > 0
72
- ranges[:status] = [status_index, ports_index]
73
- ranges[:ports] = [ports_index, names_index]
74
- else
75
- ranges[:status] = [status_index, names_index]
76
- end
77
- ranges[:names] = [names_index]
78
- ranges
79
- end
80
-
81
- #
82
- # Get a list of all docker containers by parsing the output of `docker ps -a -notrunc`.
83
- #
84
- # Uses `get_ranges` to determine where column data is within each row. Then, for each line after
85
- # the header, a hash is build up with the values for each of the columns. A special 'line' entry
86
- # is added to the hash for the raw line of the row.
87
- #
88
- # The array of hashes is returned.
89
- def docker_containers
90
- dps = docker_cmd!('ps -a -notrunc')
91
-
92
- lines = dps.stdout.lines.to_a
93
- ranges = get_ranges(lines[0])
94
-
95
- lines[1, lines.length].map do |line|
96
- ps = { 'line' => line }
97
- [:id, :image, :command, :created, :status, :ports, :names].each do |name|
98
- if ranges.key?(name)
99
- start = ranges[name][0]
100
- if ranges[name].length == 2
101
- finish = ranges[name][1]
102
- else
103
- finish = line.length
104
- end
105
- ps[name.to_s] = line[start..finish - 1].strip
106
- end
107
- end
108
- ps
109
- end
110
- end
111
-
112
- def command_timeout_error_message(cmd)
113
- <<-EOM
114
-
115
- Command timed out:
116
- #{cmd}
117
-
118
- Please adjust node container_cmd_timeout attribute or this docker_container cmd_timeout attribute if necessary.
119
- EOM
120
- end
121
-
122
- def exists?
123
- @current_resource.id
124
- end
125
-
126
- def port
127
- # DEPRECATED support for public_port attribute and Fixnum port
128
- if new_resource.public_port && new_resource.port.is_a?(Fixnum)
129
- "#{new_resource.public_port}:#{new_resource.port}"
130
- elsif new_resource.port && new_resource.port.is_a?(Fixnum)
131
- ":#{new_resource.port}"
132
- else
133
- new_resource.port
134
- end
135
- end
136
-
137
- def running?
138
- @current_resource.status.include?('Up') if @current_resource.status
139
- end
140
-
141
- def service?
142
- new_resource.init_type
143
- end
144
-
145
- def service_name
146
- container_name
147
- end
148
-
149
- def sockets
150
- return [] if port.empty?
151
- [*port].map { |p| p.gsub!(/.*:/, '') }
152
- end
153
- end
154
- end
155
- end
156
- end