lxc 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile CHANGED
@@ -1,5 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gem "json" if (RUBY_VERSION == "1.8.7")
4
+ gem "activesupport", "< 4.0.0" if (RUBY_VERSION < "1.9.3")
4
5
 
5
6
  gemspec
data/lib/lxc/container.rb CHANGED
@@ -259,17 +259,16 @@ class LXC
259
259
 
260
260
  ZTK::RescueRetry.try(:tries => 5, :on => ContainerError) do
261
261
  tempfile = Tempfile.new("bootstrap")
262
- bootstrap_tempfile = File.join("/", "tmp", File.basename(tempfile.path))
262
+ lxc_tempfile = File.join("/", "tmp", File.basename(tempfile.path))
263
+ host_tempfile = File.join(self.fs_root, lxc_tempfile)
263
264
 
264
- self.exec(<<-SCRIPT)
265
- cat <<-EOF | tee #{bootstrap_tempfile}
266
- #{content}
267
- EOF
268
- SCRIPT
265
+ self.lxc.runner.file(:target => host_tempfile, :chmod => '0755', :chown => 'root:root') do |file|
266
+ file.puts(content)
267
+ end
269
268
 
270
- output = self.lxc.attach(%(-- /bin/bash #{bootstrap_tempfile}))
269
+ output = self.attach(%(-- /bin/bash #{lxc_tempfile}))
271
270
 
272
- if !(output =~ /#{bootstrap_tempfile}: No such file or directory/).nil?
271
+ if !(output =~ /#{lxc_tempfile}: No such file or directory/).nil?
273
272
  raise ContainerError, "We could not find the bootstrap file!"
274
273
  end
275
274
  end
@@ -65,6 +65,38 @@ class LXC
65
65
  output.join.strip
66
66
  end
67
67
 
68
+ # File I/O Wrapper
69
+ #
70
+ # This method renders the supplied *content* to the file named *name* on
71
+ # the LXC host.
72
+ #
73
+ # @param [Hash] options The options hash.
74
+ # @option options [String] :target The target file on the remote host.
75
+ # @option options [String] :chown A user:group representation of who
76
+ # to change ownership of the target file to (i.e. 'root:root').
77
+ # @option options [String] :chmod An octal file mode which to set the
78
+ # target file to (i.e. '0755').
79
+ # @return [Boolean] True if successful.
80
+ def file(name, options={}, &block)
81
+ flags = (options[:flags] || 'w')
82
+ mode = (options[:mode] || nil)
83
+
84
+ target = options[:target]
85
+ chown = options[:chown]
86
+ chmod = options[:chmod]
87
+
88
+ target.nil? and raise SSHError, "You must supply a target file!"
89
+ !block_given? and raise SSHError, "You must supply a block!"
90
+
91
+ File.open(target, flags, mode) do |file|
92
+ yield(file)
93
+ file.respond_to?(:flush) and file.flush
94
+ end
95
+
96
+ chown.nil? or self.exec(%(chown -v #{chown} #{target}))
97
+ chmod.nil? or self.exec(%(chmod -v #{chmod} #{target}))
98
+ end
99
+
68
100
  # Provides a concise string representation of the class
69
101
  # @return [String]
70
102
  def inspect
@@ -22,12 +22,24 @@ class LXC
22
22
  # @param [Hash] options Options hash.
23
23
  # @option options [Boolean] :use_sudo (false) Whether or not to prefix all
24
24
  # commands with 'sudo'.
25
+ # @option options [ZTK::SSH,Net::SSH] :ssh (nil) The SSH object to use for
26
+ # remote connections.
27
+ # @option options [ZTK::SSH,Net::SFTP] :sftp (nil) The SFTP object to use
28
+ # for remote file options.
25
29
  def initialize(options={})
26
30
  @ui = (options[:ui] || ZTK::UI.new)
27
31
  @use_sudo = (options[:use_sudo] || true)
28
32
  @ssh = (options[:ssh])
33
+ @sftp = (options[:sftp])
34
+
35
+ # If the @ssh object is an instance of ZTK::SSH then use it for our
36
+ # SFTP object as well.
37
+ if @ssh.is_a?(ZTK::SSH)
38
+ @sftp = @ssh
39
+ end
29
40
 
30
41
  @ssh.nil? and raise SSHError, "You must supply a ZTK::SSH or Net::SSH instance!"
42
+ @sftp.nil? and raise SSHError, "You must supply a ZTK::SSH or Net::SFTP instance!"
31
43
  end
32
44
 
33
45
  # Linux container command execution wrapper
@@ -66,6 +78,40 @@ class LXC
66
78
  output.join.strip
67
79
  end
68
80
 
81
+ # File I/O Wrapper
82
+ #
83
+ # This method renders the supplied *content* to the file named *name* on
84
+ # the LXC host.
85
+ #
86
+ # @param [Hash] options The options hash.
87
+ # @option options [String] :target The target file on the remote host.
88
+ # @option options [String] :chown A user:group representation of who
89
+ # to change ownership of the target file to (i.e. 'root:root').
90
+ # @option options [String] :chmod An octal file mode which to set the
91
+ # target file to (i.e. '0755').
92
+ # @return [Boolean] True if successful.
93
+ def file(options={}, &block)
94
+ if !@sftp.is_a?(ZTK::SSH)
95
+ # Massage for Net::SSH
96
+ flags = (options[:flags] || 'w')
97
+ mode = (options[:mode] || nil)
98
+
99
+ target = options[:target]
100
+ chown = options[:chown]
101
+ chmod = options[:chmod]
102
+
103
+ @sftp.file.open(target, flags, mode) do |file|
104
+ yield(file)
105
+ end
106
+
107
+ chown.nil? or self.exec(%(chown -v #{chown} #{target}))
108
+ chmod.nil? or self.exec(%(chmod -v #{chmod} #{target}))
109
+ else
110
+ # Pass-through for ZTK:SSH
111
+ @sftp.file(options, &block)
112
+ end
113
+ end
114
+
69
115
  # Provides a concise string representation of the class
70
116
  # @return [String]
71
117
  def inspect
data/lib/lxc/version.rb CHANGED
@@ -2,7 +2,7 @@ class LXC
2
2
 
3
3
  unless const_defined?(:VERSION)
4
4
  # LXC Gem Version
5
- VERSION = "0.3.0"
5
+ VERSION = "0.3.1"
6
6
  end
7
7
 
8
8
  end
data/spec/lxc_spec.rb CHANGED
@@ -191,7 +191,7 @@ describe LXC do
191
191
  :keys => File.join(ENV['HOME'], '.ssh', 'id_rsa'),
192
192
  :keys_only => true
193
193
  )
194
- runner = ::LXC::Runner::SSH.new(:ssh => connection.ssh)
194
+ runner = ::LXC::Runner::SSH.new(:ssh => connection)
195
195
 
196
196
  LXC.new(:runner => runner)
197
197
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lxc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: