chef-provisioning-lxc 0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +201 -0
- data/README.md +47 -0
- data/Rakefile +6 -0
- data/lib/chef/provisioning/driver_init/lxc.rb +3 -0
- data/lib/chef/provisioning/lxc_driver.rb +2 -0
- data/lib/chef/provisioning/lxc_driver/driver.rb +170 -0
- data/lib/chef/provisioning/lxc_driver/lxc_transport.rb +157 -0
- data/lib/chef/provisioning/lxc_driver/version.rb +7 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 98feb202324d501eea2e0218a47a88e1057b3c48
|
4
|
+
data.tar.gz: 6e436317dc0fcdb00bdc537b4bb1d46782d9b351
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aedb4d363a95e317ef8cabcf3dda6bd86c51ae7fd905447a82614cc49ae98619fdf1d0258f6d1f3f0e9e316c5f5cbed508de7315bec672d036219a7adb59fe7e
|
7
|
+
data.tar.gz: 34675cc97896cd6d6325225feaf07ba71e8bdeaef19883ef3ddaa4680fe23cfddbf11613b38a8a81f94e63fe4eb6a23e337505f59f23d0c344b94e0c3ef6bc5a
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# chef-provisioning-lxc
|
2
|
+
|
3
|
+
This is the LXC provisioner for chef-provisioning.
|
4
|
+
|
5
|
+
## Quick Start
|
6
|
+
|
7
|
+
To run the tests, which creates an Ubuntu machine and installs this into it:
|
8
|
+
|
9
|
+
```
|
10
|
+
gem install chef-provisioning
|
11
|
+
rake build
|
12
|
+
cd test/
|
13
|
+
chef-client -z vagrant.rb ubuntu12.rb test_machine.rb
|
14
|
+
```
|
15
|
+
|
16
|
+
## Basic Usage
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
require 'chef/provisioning/lxc_driver/lxc_provisioner'
|
20
|
+
with_provisioner Chef::Provisioning::LXCDriver::LXCProvisioner.new
|
21
|
+
```
|
22
|
+
|
23
|
+
## Provisioner Creation
|
24
|
+
|
25
|
+
The provisioner represents lxc containers in a directory.
|
26
|
+
|
27
|
+
`Chef::Provisioning::LXCDriver::LXCProvisioner.new(...)`
|
28
|
+
|
29
|
+
### Arguments
|
30
|
+
|
31
|
+
**lxc_path** - path where the lxc containers will be located. This ends up being a sort of namespace, since the machines' names must be unique under each path.
|
32
|
+
|
33
|
+
## Provisioning Options
|
34
|
+
|
35
|
+
To specify provisioning options that will be used with all subsequent `machine` definitions:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
with_provisioner_options :template => <template name>,
|
39
|
+
:template_options => <arguments>
|
40
|
+
:backing_store => <backing store>
|
41
|
+
```
|
42
|
+
|
43
|
+
These options correspond to the options in [lxc create](https://qa.linuxcontainers.org/master/current/doc/man/lxc-create.1.html):
|
44
|
+
|
45
|
+
* **template**: the LXC template name to use.
|
46
|
+
* **template_options**: An array of string arguments to pass to the template.
|
47
|
+
* **backing_store**: The backing store to use for the file system.
|
data/Rakefile
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
require 'chef/mixin/shell_out'
|
2
|
+
require 'chef/provisioning/driver'
|
3
|
+
require 'chef/provisioning/machine/unix_machine'
|
4
|
+
require 'chef/provisioning/convergence_strategy/install_cached'
|
5
|
+
require 'chef/provisioning/lxc_driver/lxc_transport'
|
6
|
+
require 'chef/provisioning/lxc_driver/version'
|
7
|
+
require 'lxc'
|
8
|
+
require 'shellwords'
|
9
|
+
|
10
|
+
class Chef
|
11
|
+
module Provisioning
|
12
|
+
module LXCDriver
|
13
|
+
# Provisions machines in lxc.
|
14
|
+
class Driver < Chef::Provisioning::Driver
|
15
|
+
|
16
|
+
include Chef::Mixin::ShellOut
|
17
|
+
|
18
|
+
# URL scheme:
|
19
|
+
# lxc:<path>
|
20
|
+
# <path> defaults to LXC config 'lxc.lxcpath'
|
21
|
+
# canonical URL calls realpath on <path>
|
22
|
+
def self.from_url(driver_url, config)
|
23
|
+
Driver.new(driver_url, config)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.canonicalize_url(driver_url, config)
|
27
|
+
scheme, lxc_path = driver_url.split(':', 2)
|
28
|
+
if lxc_path.nil? || lxc_path == ':'
|
29
|
+
lxc_path = LXC.global_config_item('lxc.lxcpath')
|
30
|
+
end
|
31
|
+
lxc_path = File.realpath(lxc_path)
|
32
|
+
[ "lxc:#{lxc_path}", config ]
|
33
|
+
end
|
34
|
+
|
35
|
+
def initialize(driver_url, config)
|
36
|
+
super
|
37
|
+
end
|
38
|
+
|
39
|
+
def lxc_path
|
40
|
+
scheme, lxc_path = driver_url.split(':', 2)
|
41
|
+
lxc_path
|
42
|
+
end
|
43
|
+
|
44
|
+
# Valid machine options:
|
45
|
+
# :template - template name
|
46
|
+
# :template_options - additional arguments for templates
|
47
|
+
# :backingstore - backing storage (lvm, thinpools, btrfs etc)
|
48
|
+
# :config_file - <path> path to LXC file a la https://wiki.archlinux.org/index.php/Linux_Containers#Configuration_file
|
49
|
+
# :extra_config - { 'key' => 'value', ... } a set of LXC config key/value pairs to individually set. Merges with, and overrides any values in config_file.
|
50
|
+
def allocate_machine(action_handler, machine_spec, machine_options)
|
51
|
+
# Create the container if it does not exist
|
52
|
+
if machine_spec.location
|
53
|
+
ct = LXC::Container.new(machine_spec.location['name'], lxc_path)
|
54
|
+
else
|
55
|
+
ct = LXC::Container.new(machine_spec.name, lxc_path)
|
56
|
+
if ct.defined?
|
57
|
+
# Should this be a warning? Configurable, at least.
|
58
|
+
raise "container #{machine_spec.name} already exists and is not managed by this LXC driver."
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
unless ct.defined?
|
63
|
+
action_handler.perform_action "create lxc container #{ct.name}" do
|
64
|
+
#
|
65
|
+
# Set config
|
66
|
+
#
|
67
|
+
# TODO if config file changes, reload container?
|
68
|
+
if machine_options[:config_file]
|
69
|
+
ct.load_config(machine_options[:config_file])
|
70
|
+
end
|
71
|
+
if machine_options[:extra_config]
|
72
|
+
machine_options[:extra_config].each_pair do |key, value|
|
73
|
+
ct.set_config_item(key, value)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
#
|
78
|
+
# Create the machine
|
79
|
+
#
|
80
|
+
ct.create(machine_options[:template], machine_options[:backingstore], machine_options[:devspecs], 0, machine_options[:template_options])
|
81
|
+
|
82
|
+
machine_spec.location = {
|
83
|
+
'driver_url' => driver_url,
|
84
|
+
'driver_version' => Chef::Provisioning::LXCDriver::VERSION,
|
85
|
+
'name' => machine_spec.name,
|
86
|
+
'host_node' => action_handler.host_node,
|
87
|
+
'allocated_at' => Time.now.utc.to_s
|
88
|
+
}
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def ready_machine(action_handler, machine_spec, machine_options)
|
94
|
+
ct = LXC::Container.new(machine_spec.location['name'], lxc_path)
|
95
|
+
|
96
|
+
# Unfreeze the frozen
|
97
|
+
if ct.state == :frozen
|
98
|
+
action_handler.perform_action "unfreeze lxc container #{machine_spec.location['name']} (state is #{ct.state})" do
|
99
|
+
ct.unfreeze
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# Get stopped containers running
|
104
|
+
unless ct.running?
|
105
|
+
action_handler.perform_action "start lxc container #{machine_spec.location['name']} (state is #{ct.state})" do
|
106
|
+
# Have to shell out to lxc-start for now, ct.start holds server sockets open!
|
107
|
+
lxc_start = "lxc-start -d -n #{Shellwords.escape(machine_spec.location['name'])}"
|
108
|
+
# TODO add ability to change options on start
|
109
|
+
# if machine_options[:config_file]
|
110
|
+
# lxc_start << " -f #{Shellwords.escape(machine_options[:config_file])}"
|
111
|
+
# end
|
112
|
+
# if machine_options[:extra_config]
|
113
|
+
# machine_options[:extra_config].each_pair do |key,value|
|
114
|
+
# lxc_start << " -s #{Shellwords.escape("#{key}=#{value}")}"
|
115
|
+
# end
|
116
|
+
# end
|
117
|
+
shell_out!(lxc_start)
|
118
|
+
# ct.start
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Create machine object for callers to use
|
123
|
+
machine_for(machine_spec, machine_options)
|
124
|
+
end
|
125
|
+
|
126
|
+
# Connect to machine without acquiring it
|
127
|
+
def connect_to_machine(machine_spec, machine_options)
|
128
|
+
machine_for(machine_spec, machine_options)
|
129
|
+
end
|
130
|
+
|
131
|
+
def destroy_machine(action_handler, machine_spec, machine_options)
|
132
|
+
if machine_spec.location
|
133
|
+
ct = LXC::Container.new(machine_spec.location['name'], lxc_path)
|
134
|
+
if ct.defined?
|
135
|
+
action_handler.perform_action "delete lxc container #{machine_spec.location['name']}" do
|
136
|
+
ct.destroy
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
convergence_strategy_for(machine_spec, machine_options).cleanup_convergence(action_handler, machine_spec)
|
141
|
+
end
|
142
|
+
|
143
|
+
def stop_machine(action_handler, node)
|
144
|
+
if machine_spec.location
|
145
|
+
ct = LXC::Container.new(machine_spec.location['name'], lxc_path)
|
146
|
+
if ct.running?
|
147
|
+
action_handler.perform_action "delete lxc container #{machine_spec.location['name']}" do
|
148
|
+
ct.stop
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
protected
|
155
|
+
|
156
|
+
def machine_for(machine_spec, machine_options)
|
157
|
+
Chef::Provisioning::Machine::UnixMachine.new(machine_spec, transport_for(machine_spec), convergence_strategy_for(machine_spec, machine_options))
|
158
|
+
end
|
159
|
+
|
160
|
+
def convergence_strategy_for(machine_spec, machine_options)
|
161
|
+
Chef::Provisioning::ConvergenceStrategy::InstallCached.new(machine_options[:convergence_options], config)
|
162
|
+
end
|
163
|
+
|
164
|
+
def transport_for(machine_spec)
|
165
|
+
Chef::Provisioning::LXCDriver::LXCTransport.new(machine_spec.location['name'], lxc_path)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
require 'chef/provisioning/transport'
|
2
|
+
require 'lxc/extra'
|
3
|
+
require 'chef/mixin/shell_out'
|
4
|
+
require 'lxc/extra/proxy_client_side'
|
5
|
+
require 'lxc/extra/proxy_server_side'
|
6
|
+
require 'lxc/extra/channel'
|
7
|
+
require 'uri'
|
8
|
+
require 'socket'
|
9
|
+
|
10
|
+
class Chef
|
11
|
+
module Provisioning
|
12
|
+
module LXCDriver
|
13
|
+
class LXCTransport < Chef::Provisioning::Transport
|
14
|
+
@@active_transports = []
|
15
|
+
|
16
|
+
class LXCExecuteResult < Struct.new(:command, :options, :stdout, :stderr, :exitstatus)
|
17
|
+
def error!
|
18
|
+
raise "Error: '#{command}' failed with exit code #{exitstatus}.\nSTDOUT:#{stdout}\nSTDERR:#{stderr}" if exitstatus != 0
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
attr_reader :name, :options, :lxc_path
|
23
|
+
|
24
|
+
include Chef::Mixin::ShellOut
|
25
|
+
|
26
|
+
def initialize(name, lxc_path, options={})
|
27
|
+
@options = options
|
28
|
+
@name = name
|
29
|
+
@lxc_path = lxc_path
|
30
|
+
@port_forwards = {}
|
31
|
+
@@active_transports << self
|
32
|
+
end
|
33
|
+
|
34
|
+
def container
|
35
|
+
@container ||= LXC::Container.new(name, lxc_path)
|
36
|
+
end
|
37
|
+
|
38
|
+
def rootfs
|
39
|
+
container.config_item('lxc.rootfs')
|
40
|
+
end
|
41
|
+
|
42
|
+
def container_path(path)
|
43
|
+
File.join(rootfs, path)
|
44
|
+
end
|
45
|
+
|
46
|
+
def execute(command, options = {})
|
47
|
+
Chef::Log.info("Executing #{command} on #{name}")
|
48
|
+
container.execute(:timeout => (execute_timeout(options) || 0)) do
|
49
|
+
begin
|
50
|
+
# TODO support streaming (shell out needs work)
|
51
|
+
out = shell_out(command)
|
52
|
+
LXCExecuteResult.new(command, options, out.stdout, out.stderr, out.exitstatus)
|
53
|
+
rescue Exception => e
|
54
|
+
LXCExecuteResult.new('', e.message, -1)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def make_url_available_to_remote(local_url)
|
60
|
+
uri = URI(local_url)
|
61
|
+
host = Socket.getaddrinfo(uri.host, uri.scheme, nil, :STREAM)[0][3]
|
62
|
+
if host == '127.0.0.1' || host == '::1'
|
63
|
+
unless @port_forwards[uri.port]
|
64
|
+
|
65
|
+
Chef::Log.debug("Forwarding container port #{uri.port} to local port #{uri.port}")
|
66
|
+
# Create the channel that will let the container and the host talk to each other
|
67
|
+
channel = LXC::Extra::Channel.new
|
68
|
+
|
69
|
+
# Start the container side of the proxy, listening for client connections
|
70
|
+
pid = container.attach do
|
71
|
+
begin
|
72
|
+
server = TCPServer.new(host, uri.port)
|
73
|
+
proxy = LXC::Extra::ProxyClientSide.new(channel, server)
|
74
|
+
proxy.start
|
75
|
+
rescue
|
76
|
+
Chef::Log.error("ERROR in proxy (container side): #{$!}\n#{$!.backtrace.join("\n")}")
|
77
|
+
raise
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Start the host side of the proxy, which contacts the real server
|
82
|
+
thread = Thread.new do
|
83
|
+
proxy = LXC::Extra::ProxyServerSide.new(channel) do
|
84
|
+
TCPSocket.new(host, uri.port)
|
85
|
+
end
|
86
|
+
proxy.start
|
87
|
+
end
|
88
|
+
|
89
|
+
Chef::Log.debug("Forwarded #{uri.port} on container #{name} to local port #{uri.port}. Container listener id PID #{pid}")
|
90
|
+
|
91
|
+
@port_forwards[uri.port] = [ pid, thread, channel ]
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
local_url
|
96
|
+
end
|
97
|
+
|
98
|
+
def read_file(path)
|
99
|
+
if File.exists?(container_path(path))
|
100
|
+
File.read(container_path(path))
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def download_file(path, local_path)
|
105
|
+
Chef::Log.debug("Copying file #{path} from #{name} to local #{local_path}")
|
106
|
+
FileUtils.cp_r(container_path(path), local_path)
|
107
|
+
end
|
108
|
+
|
109
|
+
def write_file(path, content)
|
110
|
+
File.open(container_path(path), 'w') do |f|
|
111
|
+
f.write(content)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def upload_file(local_path, path)
|
116
|
+
FileUtils.cp_r(local_path, container_path(path))
|
117
|
+
end
|
118
|
+
|
119
|
+
def disconnect
|
120
|
+
@port_forwards.each_pair do |port, (pid, thread, channel)|
|
121
|
+
Chef::Log.debug("stopping port forward #{port} for container #{name}")
|
122
|
+
begin
|
123
|
+
Chef::Log.debug("Killing PID #{pid}")
|
124
|
+
Process.kill('KILL', pid)
|
125
|
+
rescue
|
126
|
+
end
|
127
|
+
begin
|
128
|
+
thread.kill
|
129
|
+
rescue
|
130
|
+
end
|
131
|
+
end
|
132
|
+
@port_forwards = {}
|
133
|
+
@@active_transports.delete(self)
|
134
|
+
end
|
135
|
+
|
136
|
+
def available?
|
137
|
+
begin
|
138
|
+
execute('pwd')
|
139
|
+
true
|
140
|
+
rescue Exception =>e
|
141
|
+
false
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def self.disconnect_active_transports
|
146
|
+
@@active_transports.to_a.dup.each do |transport|
|
147
|
+
transport.disconnect
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
at_exit do
|
156
|
+
Chef::Provisioning::LXCDriver::LXCTransport.disconnect_active_transports
|
157
|
+
end
|
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chef-provisioning-lxc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.5'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ranjib Dey
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-05 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-provisioning
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.11'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: ruby-lxc
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.1'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: lxc-extra
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.0'
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 0.0.3
|
65
|
+
type: :runtime
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - "~>"
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0.0'
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 0.0.3
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rspec
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rake
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
description: Provisioner for creating LXC containers in Chef Metal.
|
104
|
+
email: dey.ranjib@gmail.com
|
105
|
+
executables: []
|
106
|
+
extensions: []
|
107
|
+
extra_rdoc_files:
|
108
|
+
- README.md
|
109
|
+
- LICENSE
|
110
|
+
files:
|
111
|
+
- LICENSE
|
112
|
+
- README.md
|
113
|
+
- Rakefile
|
114
|
+
- lib/chef/provisioning/driver_init/lxc.rb
|
115
|
+
- lib/chef/provisioning/lxc_driver.rb
|
116
|
+
- lib/chef/provisioning/lxc_driver/driver.rb
|
117
|
+
- lib/chef/provisioning/lxc_driver/lxc_transport.rb
|
118
|
+
- lib/chef/provisioning/lxc_driver/version.rb
|
119
|
+
homepage: https://github.com/opscode/chef-provisioning-lxc
|
120
|
+
licenses: []
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.2.2
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: Provisioner for creating LXC containers in Chef Metal.
|
142
|
+
test_files: []
|
143
|
+
has_rdoc:
|