Class: OpenVZ::Container
Overview
OpenVZ::Container
Defined Under Namespace
Classes: Config, StatemachineError
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#ctid ⇒ Object
readonly
Returns the value of attribute ctid.
-
#rw config(config) ⇒ Object
OpenVZ::Container.
Instance Method Summary collapse
-
#checkpoint(snapshot_path) ⇒ Object
Checkpoint the container.
-
#command(command) ⇒ Object
Run a shell command within the container.
-
#cp_into(options = {}) ⇒ Object
Copy a file from the hardware node into the container.
-
#create(options = {}) ⇒ Object
Create a new empty instance from a template.
-
#debootstrap(options = {}) ⇒ Object
Bootstrap a Debian container, this requires /usr/sbin/debootstrap to be installed.
-
#destroy ⇒ Object
Destroy a container.
-
#execute(cmd) ⇒ Object
Execute a System Command.
-
#initialize(ctid = false) ⇒ Container
constructor
A new instance of Container.
-
#load_config_file ⇒ Object
Load the container configration file.
-
#mount ⇒ Object
Mount a container.
-
#restart ⇒ Object
Restart a container.
-
#restore(snapshot_path) ⇒ Object
Restore a checkpoint.
-
#set(options = {}) ⇒ Object
Update one or multiple Container properties and keep the configration object up to date.
-
#start ⇒ Object
Start a container.
-
#state(mode, action) ⇒ Object
Check whether current_state == :require||:validate.
- #state_require(*opts) ⇒ Object
-
#status ⇒ Object
Return the current machine status string.
-
#stop ⇒ Object
Stop a container.
-
#umount ⇒ Object
Umount a container.
-
#update(key, value) ⇒ Object
Notify Method! :-) Config objects will notify, as soon as they are changed.
-
#uptime ⇒ Object
Return the current uptime in seconds.
Constructor Details
#initialize(ctid = false) ⇒ Container
Returns a new instance of Container.
37 38 39 40 41 42 43 44 |
# File 'lib/openvz/container.rb', line 37 def initialize(ctid=false) @ctid = ctid @vzctl = "/usr/sbin/vzctl" @configfile = "/etc/vz/conf/#{ctid}.conf" @config = Config.new(load_config_file) @config.add_observer(self) end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
30 31 32 |
# File 'lib/openvz/container.rb', line 30 def config @config end |
#ctid ⇒ Object (readonly)
Returns the value of attribute ctid.
31 32 33 |
# File 'lib/openvz/container.rb', line 31 def ctid @ctid end |
#rw config(config) ⇒ Object
OpenVZ::Container
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/openvz/container.rb', line 28 class Container attr_accessor :config attr_reader :ctid class StatemachineError < StandardError;end class Config < ::OpenVZ::ConfigHash ; end def initialize(ctid=false) @ctid = ctid @vzctl = "/usr/sbin/vzctl" @configfile = "/etc/vz/conf/#{ctid}.conf" @config = Config.new(load_config_file) @config.add_observer(self) end # Start a container # def start cmd = "#{@vzctl} start #{@ctid}" execute(cmd) end # Stop a container # def stop cmd = "#{@vzctl} stop #{@ctid}" execute(cmd) end # Restart a container # def restart cmd = "#{@vzctl} restart #{@ctid}" execute(cmd) end # Mount a container # def mount cmd = "#{@vzctl} mount #{@ctid}" execute(cmd) end # Umount a container # def umount cmd = "#{@vzctl} umount #{@ctid}" execute(cmd) end # Destroy a container # def destroy cmd = "#{@vzctl} destroy #{@ctid}" execute(cmd) end # Checkpoint the container # def checkpoint(snapshot_path) cmd = "#{@vzctl} chkpnt #{@ctid} --dumpfile #{snapshot_path}" execute(cmd) end # Restore a checkpoint # def restore(snapshot_path) cmd = "#{@vzctl} restore #{@ctid} --dumpfile #{snapshot_path}" execute(cmd) end # Update one or multiple Container properties and keep the configration # object up to date # # @param [Hash] The options to update, eg. `{ :privvmpages => '123:123' }` def set( = {}) # Construct a set command cmd = "#{@vzctl} set #{@ctid} --save" .each do |opt,val| cmd << " --#{opt}" cmd << " #{val}" end execute(cmd) # Each time we update a setting, reload the configuration. @config.load(@ctid) end # Create a new empty instance from a template. # # @example Create a new empty container from a template. # The following example will create a container, from an empty # template, which can be used to bootstrap the whole installation # later on. It will as well apply the vps.unlimited template config. # # container.create(:ostemplate => "debian-6.0-bootstrap", :config => "vps.unlimited") # # @param [Hash] options you want to pass to the create statement. def create(={}) unless [:ostemplate] # We need at least a valid ostemplate raise ArgumentError, "Create requires argument :ostemplate." end cmd = "#{@vzctl} create #{@ctid}" .each do |opt,val| cmd << " --#{opt}" cmd << " #{val}" end execute(cmd) Log.debug("Reading new container configuration file: #{@configfile}") @config = Config.new(load_config_file) @config.add_observer(self) end # Bootstrap a Debian container, this requires /usr/sbin/debootstrap to # be installed. Executing this function usually takes a while. # # @example Debootstrap a container object. # container.debootstrap(:dist => "squeeze", :mirror => "http://cdn.debian.net/debian") # # @example Debootstrap a container, include and exclude certain packages. # container.debootstrap( # :dist => "squeeze", # :mirror => "http://cdn.debian.net/debian", # :include => "libreadline6,screen,file,less,dnsutils,tcpdump,vim-nox,puppet,facter", # :exclude => "dhcp-client,dhcp3-client,dhcp3-common,dmidecode,gcc-4.2-base,module-init-tools,tasksel,tasksel-data,libdb4.4,libsasl2-2,libgnutls26,libconsole,libgnutls13,libtasn1-3,liblzo2-2,libopencdk10,libgcrypt11", # ) # # @param [Hash] options you want to pass to the bootstrapping tool. def debootstrap(={}) cmd = "/usr/sbin/debootstrap" .each do |opt,val| unless opt.to_s =~ /dist|dest|mirror/ cmd << " --#{opt} #{val}" end end cmd << " #{[:dist]}" cmd << " #{@config.ve_private}" cmd << " #{[:mirror]}" execute(cmd) # FIXME - Remove gettys from inititab automatically. We # Need a searchandreplace function first... :-) # # Util.searchandreplace( "#{@config.ve_private}/etc/inittab", # "/^(?!#)(.*\/sbin\/getty)/", # '#\1') end # Copy a file from the hardware node into the container. # # @example Copy resolv.conf # container.cp_into( # :src => '/etc/resolv.conf', # :dst => '/etc/resolv.conf' # ) # # @param [Hash] define def cp_into(={}) cmd = "/bin/cp #{[:src]} #{@config.ve_private}/#{[:dst]}" execute(cmd) end # Run a shell command within the container. # # @example Update the package sources. # container.command("aptitude update") # # @param [String] Command to be executed. def command(command) cmd = "#{@vzctl} exec2 #{@ctid} " cmd << command execute(cmd) end # Return the current machine status string. # # @example # puts container.status() # exist mounted running def status cmd = "#{@vzctl} status #{@ctid}" status = execute(cmd).split(/\s/) Log.debug("Container (#{@ctid}) status requested: #{status}") status.drop(2) end # Return the current uptime in seconds. # Return zero if the container is not running. # # @example # puts container.uptime() # 1188829 def uptime return 0 unless status.include? "running" raw = command "cat /proc/uptime" Log.debug("Container (#{@ctid}) uptime requested: #{raw}") raw.split(/\W/).first.to_i end #### #### Helper methods #### def state_require(*opts) current_state = status if current_state & opts puts "req: #{opts.join("-")}, cur: #{current_state.join("-")}" return true else raise StateMachineError, "Required container status not met for this action. req: #{opts.join("-")}, cur: #{current_state.join("-")}" end end # Check whether current_state == :require||:validate # def state(mode, action) current_state = self.status() if current_state & @statemachine_options[action.to_sym][mode.to_sym] return true end raise StateMachineError, "Required container status not met. req: #{@state_control[:action]}, cur: #{state}" end # Notify Method! :-) Config objects will notify, as soon as they are changed. # def update(key, value) set(key.to_sym => value) end # Load the container configration file. # def load_config_file data = {} if File.exists?(@configfile) File.open(@configfile, "r").each_line do |line| # strip blank spaces, tabs etc. off the lines line.gsub!(/\s*$/, "") if (line =~ /^([^=]+)="([^=]*)"$/) key = $1.downcase val = $2 case key when /^ve_(private|root)$/ data[key] = val.gsub!(/\$VEID/, @ctid) else data[key] = val end end end end data end # TODO: implement :-) # # NETIF="ifname=eth0, # bridge=vzbr303, # mac=02:01:79:15:03:02, # host_ifname=veth17915.303, # host_mac=12:01:79:15:03:02" #def load_net_config # if @config.netif ### DOES THAT WORK? # str = @config.netif # if str =~ /ifname=([^,]+),bridge=([^,]+),mac=([^,]+),host_ifname=([^,]+),host_mac=([^,]+)/ # @net.ifname = $1 # @net.bridge = $2 # @net.mac = $3 # @net.host_ifname = $4 # @net.host_mac = $5 # @net.type = "bridged" # ### MISSING: IP, SUBNET, GATEWAY within VM # end # end #end # This will give us a nicer object feeling and dynamically # exposes variables to the outside. # # @examples Setting and getting a variable # container.config.ipaddress = 1.2.3.4 # # #def method_missing(sym, *args) # if sym.to_s =~ /(.+)=$/ # self[$1] = args.first # else # self[sym] # end #end # Execute a System Command def execute(cmd) Log.debug("#{cmd}") s = Shell.new("#{cmd}", :cwd => "/tmp") s.runcommand if s.status.exitstatus != 0 raise "Cannot execute: '#{cmd}'. Return code: #{s.status.exitstatus}. Stderr: #{s.stderr}" end s.stdout end end |
Instance Method Details
#checkpoint(snapshot_path) ⇒ Object
Checkpoint the container
97 98 99 100 |
# File 'lib/openvz/container.rb', line 97 def checkpoint(snapshot_path) cmd = "#{@vzctl} chkpnt #{@ctid} --dumpfile #{snapshot_path}" execute(cmd) end |
#command(command) ⇒ Object
Run a shell command within the container.
222 223 224 225 226 |
# File 'lib/openvz/container.rb', line 222 def command(command) cmd = "#{@vzctl} exec2 #{@ctid} " cmd << command execute(cmd) end |
#cp_into(options = {}) ⇒ Object
Copy a file from the hardware node into the container.
210 211 212 213 |
# File 'lib/openvz/container.rb', line 210 def cp_into(={}) cmd = "/bin/cp #{[:src]} #{@config.ve_private}/#{[:dst]}" execute(cmd) end |
#create(options = {}) ⇒ Object
Create a new empty instance from a template.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/openvz/container.rb', line 141 def create(={}) unless [:ostemplate] # We need at least a valid ostemplate raise ArgumentError, "Create requires argument :ostemplate." end cmd = "#{@vzctl} create #{@ctid}" .each do |opt,val| cmd << " --#{opt}" cmd << " #{val}" end execute(cmd) Log.debug("Reading new container configuration file: #{@configfile}") @config = Config.new(load_config_file) @config.add_observer(self) end |
#debootstrap(options = {}) ⇒ Object
Bootstrap a Debian container, this requires /usr/sbin/debootstrap to be installed. Executing this function usually takes a while.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/openvz/container.rb', line 177 def debootstrap(={}) cmd = "/usr/sbin/debootstrap" .each do |opt,val| unless opt.to_s =~ /dist|dest|mirror/ cmd << " --#{opt} #{val}" end end cmd << " #{[:dist]}" cmd << " #{@config.ve_private}" cmd << " #{[:mirror]}" execute(cmd) # FIXME - Remove gettys from inititab automatically. We # Need a searchandreplace function first... :-) # # Util.searchandreplace( "#{@config.ve_private}/etc/inittab", # "/^(?!#)(.*\/sbin\/getty)/", # '#\1') end |
#destroy ⇒ Object
Destroy a container
89 90 91 92 |
# File 'lib/openvz/container.rb', line 89 def destroy cmd = "#{@vzctl} destroy #{@ctid}" execute(cmd) end |
#execute(cmd) ⇒ Object
Execute a System Command
359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/openvz/container.rb', line 359 def execute(cmd) Log.debug("#{cmd}") s = Shell.new("#{cmd}", :cwd => "/tmp") s.runcommand if s.status.exitstatus != 0 raise "Cannot execute: '#{cmd}'. Return code: #{s.status.exitstatus}. Stderr: #{s.stderr}" end s.stdout end |
#load_config_file ⇒ Object
Load the container configration file.
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/openvz/container.rb', line 295 def load_config_file data = {} if File.exists?(@configfile) File.open(@configfile, "r").each_line do |line| # strip blank spaces, tabs etc. off the lines line.gsub!(/\s*$/, "") if (line =~ /^([^=]+)="([^=]*)"$/) key = $1.downcase val = $2 case key when /^ve_(private|root)$/ data[key] = val.gsub!(/\$VEID/, @ctid) else data[key] = val end end end end data end |
#mount ⇒ Object
Mount a container
73 74 75 76 |
# File 'lib/openvz/container.rb', line 73 def mount cmd = "#{@vzctl} mount #{@ctid}" execute(cmd) end |
#restart ⇒ Object
Restart a container
65 66 67 68 |
# File 'lib/openvz/container.rb', line 65 def restart cmd = "#{@vzctl} restart #{@ctid}" execute(cmd) end |
#restore(snapshot_path) ⇒ Object
Restore a checkpoint
105 106 107 108 |
# File 'lib/openvz/container.rb', line 105 def restore(snapshot_path) cmd = "#{@vzctl} restore #{@ctid} --dumpfile #{snapshot_path}" execute(cmd) end |
#set(options = {}) ⇒ Object
Update one or multiple Container properties and keep the configration object up to date
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/openvz/container.rb', line 115 def set( = {}) # Construct a set command cmd = "#{@vzctl} set #{@ctid} --save" .each do |opt,val| cmd << " --#{opt}" cmd << " #{val}" end execute(cmd) # Each time we update a setting, reload the configuration. @config.load(@ctid) end |
#start ⇒ Object
Start a container
49 50 51 52 |
# File 'lib/openvz/container.rb', line 49 def start cmd = "#{@vzctl} start #{@ctid}" execute(cmd) end |
#state(mode, action) ⇒ Object
Check whether current_state == :require||:validate
274 275 276 277 278 279 280 281 282 283 |
# File 'lib/openvz/container.rb', line 274 def state(mode, action) current_state = self.status() if current_state & @statemachine_options[action.to_sym][mode.to_sym] return true end raise StateMachineError, "Required container status not met. req: #{@state_control[:action]}, cur: #{state}" end |
#state_require(*opts) ⇒ Object
261 262 263 264 265 266 267 268 269 |
# File 'lib/openvz/container.rb', line 261 def state_require(*opts) current_state = status if current_state & opts puts "req: #{opts.join("-")}, cur: #{current_state.join("-")}" return true else raise StateMachineError, "Required container status not met for this action. req: #{opts.join("-")}, cur: #{current_state.join("-")}" end end |
#status ⇒ Object
Return the current machine status string.
234 235 236 237 238 239 |
# File 'lib/openvz/container.rb', line 234 def status cmd = "#{@vzctl} status #{@ctid}" status = execute(cmd).split(/\s/) Log.debug("Container (#{@ctid}) status requested: #{status}") status.drop(2) end |
#stop ⇒ Object
Stop a container
57 58 59 60 |
# File 'lib/openvz/container.rb', line 57 def stop cmd = "#{@vzctl} stop #{@ctid}" execute(cmd) end |
#umount ⇒ Object
Umount a container
81 82 83 84 |
# File 'lib/openvz/container.rb', line 81 def umount cmd = "#{@vzctl} umount #{@ctid}" execute(cmd) end |
#update(key, value) ⇒ Object
Notify Method! :-) Config objects will notify, as soon as they are changed.
288 289 290 |
# File 'lib/openvz/container.rb', line 288 def update(key, value) set(key.to_sym => value) end |
#uptime ⇒ Object
Return the current uptime in seconds. Return zero if the container is not running.
248 249 250 251 252 253 |
# File 'lib/openvz/container.rb', line 248 def uptime return 0 unless status.include? "running" raw = command "cat /proc/uptime" Log.debug("Container (#{@ctid}) uptime requested: #{raw}") raw.split(/\W/).first.to_i end |