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
-
#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.
-
#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 |
# 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 # 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 << " #{options[:dist]}" cmd << " #{@config.ve_private}" cmd << " #{options[: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 #{options[:src]} #{@config.ve_private}/#{options[: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 & [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
#command(command) ⇒ Object
Run a shell command within the container.
206 207 208 209 210 |
# File 'lib/openvz/container.rb', line 206 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.
194 195 196 197 |
# File 'lib/openvz/container.rb', line 194 def cp_into(={}) cmd = "/bin/cp #{options[:src]} #{@config.ve_private}/#{options[:dst]}" execute(cmd) end |
#create(options = {}) ⇒ Object
Create a new empty instance from a template.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/openvz/container.rb', line 125 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.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/openvz/container.rb', line 161 def debootstrap(={}) cmd = "/usr/sbin/debootstrap" .each do |opt,val| unless opt.to_s =~ /dist|dest|mirror/ cmd << " --#{opt} #{val}" end end cmd << " #{options[:dist]}" cmd << " #{@config.ve_private}" cmd << " #{options[: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
343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/openvz/container.rb', line 343 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.
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/openvz/container.rb', line 279 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 |
#set(options = {}) ⇒ Object
Update one or multiple Container properties and keep the configration object up to date
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/openvz/container.rb', line 99 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
258 259 260 261 262 263 264 265 266 267 |
# File 'lib/openvz/container.rb', line 258 def state(mode, action) current_state = self.status() if current_state & [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
245 246 247 248 249 250 251 252 253 |
# File 'lib/openvz/container.rb', line 245 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.
218 219 220 221 222 223 |
# File 'lib/openvz/container.rb', line 218 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.
272 273 274 |
# File 'lib/openvz/container.rb', line 272 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.
232 233 234 235 236 237 |
# File 'lib/openvz/container.rb', line 232 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 |