3 SCRIPT_DIR
= File
.dirname(File
.expand_path(__FILE__
))
4 SCRIPT_NAME
= $0.split('/')[-1]
7 require "#{SCRIPT_DIR}/../lib/ciconia_daemonizer"
12 if ARGV.size
!= 2 and ARGV[2] != '--kill-daemon'
14 STDERR.puts
"SYNOPSIS:"
15 STDERR.puts
" #{SCRIPT_NAME} \e[4mSIZE\e[24m \e[4mTEMPLATE\e[24m"
16 STDERR.puts
" #{SCRIPT_NAME} \e[4mSIZE\e[24m \e[4mTEMPLATE\e[24m " +
20 STDERR.puts
"DESCRIPTION:"
21 STDERR.puts
" --kill_daemon "
22 STDERR.puts
" Kills the daemon and removes related PID files from /tmp"
28 raise 'Unsafe SIZE' if !(ARGV[0] =~
/^[0-9]*$/)
33 raise "unsafe $template #{$template.inspect}" unless
34 $template =~
/^[a-zA-Z0-9\._-]*$/
37 if ARGV[2] != '--kill-daemon'
39 # Check if template exists
40 require "#{SCRIPT_DIR}/../lib/database_for_scripts"
41 host
= OpenvzServer
.find(:all).last
.ip
43 cmd
= "echo \"stat /vz/template/cache/#{$template}.tar.gz\" | #{SSH} -T #{host}"
47 pid
, cmd_in
, cmd_out
, cmd_err
= Open4
::popen4(cmd
)
50 STDERR.puts cmd_err
.entries
.join
51 #"The #{$template} template is not on the system"
57 cmd
= "vzlist -a --no-header -o veid"
59 pid
, cmd_in
, cmd_out
, cmd_err
= Open4
::popen4(cmd
)
62 STDERR.print cmd_err
.entries
.join('')
67 current_list
= cmd_out
.collect
{|ve
| ve
.strip
.to_i
}
68 desired_list
= (10..(9 + $size)).to_a
# Adding 10 to make it double digits
69 $gaps = desired_list
- current_list
72 puts
"#{$size} VE(s) were already created"
79 class CiconiaDaemonizer
< Daemon
::Base
81 raise 'no $gaps' if $gaps == nil
82 raise 'no $template' if $template == nil
85 run_and_record("vzctl create #{gap} --ostemplate #{$template}")
89 `mv #{@@pid_file} #{@@pid_file}.with_errors_output`
91 `mv #{@@pid_file} #{@@pid_file}.with_output`
102 CiconiaDaemonizer
.daemonize("#{SCRIPT_NAME} #{$size} #{$template}", 0)