chef-metal-docker 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,156 @@
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
@@ -0,0 +1,3 @@
1
+ module ChefMetalDocker
2
+ VERSION = '0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chef-metal-docker
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Tom Duffield
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chef
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: chef-metal
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: docker-api
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Provisioner for creating Docker containers in Chef Metal.
84
+ email: tom@getchef.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files:
88
+ - README.md
89
+ - LICENSE
90
+ files:
91
+ - Rakefile
92
+ - LICENSE
93
+ - README.md
94
+ - lib/chef/provider/docker_container.rb
95
+ - lib/chef/resource/docker_container.rb
96
+ - lib/chef_metal/provisioner_init/docker_init.rb
97
+ - lib/chef_metal_docker/docker_convergence_strategy.rb
98
+ - lib/chef_metal_docker/docker_provisioner.rb
99
+ - lib/chef_metal_docker/docker_transport.rb
100
+ - lib/chef_metal_docker/docker_unix_machine.rb
101
+ - lib/chef_metal_docker/helpers/container/actions.rb
102
+ - lib/chef_metal_docker/helpers/container/helpers.rb
103
+ - lib/chef_metal_docker/helpers/container.rb
104
+ - lib/chef_metal_docker/helpers.rb
105
+ - lib/chef_metal_docker/version.rb
106
+ - lib/chef_metal_docker.rb
107
+ homepage: https://github.com/opscode/chef-metal-docker
108
+ licenses: []
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.0.3
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Provisioner for creating Docker containers in Chef Metal.
130
+ test_files: []
131
+ has_rdoc: