Improved database_for_scripts.rb
[mwamko.git] / script / vz-slow-create
blob8e392e419a5e7a7394e1e7986b9d48519bb65514
1 #!/usr/bin/env ruby
3 SCRIPT_DIR = File.dirname(File.expand_path(__FILE__))
4 SCRIPT_NAME = $0.split('/')[-1]
5 SSH = '/usr/bin/ssh'
7 require "#{SCRIPT_DIR}/../lib/ciconia_daemonizer"
10 # Check ARGV
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 " +
17 "--kill-daemon"
19 STDERR.puts ""
20 STDERR.puts "DESCRIPTION:"
21 STDERR.puts " --kill_daemon "
22 STDERR.puts " Kills the daemon and removes related PID files from /tmp"
24 exit
25 end
27 # Check ARGV
28 raise 'Unsafe SIZE' if !(ARGV[0] =~ /^[0-9]*$/)
30 $size = ARGV[0].to_i
31 $template = ARGV[1]
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}"
45 puts cmd
47 pid, cmd_in, cmd_out, cmd_err = Open4::popen4(cmd)
48 cmd_in.close
49 if !cmd_err.eof?
50 STDERR.puts cmd_err.entries.join
51 #"The #{$template} template is not on the system"
52 exit
53 end
56 # Find Gaps
57 cmd = "vzlist -a --no-header -o veid"
59 pid, cmd_in, cmd_out, cmd_err = Open4::popen4(cmd)
60 cmd_in.close
61 if !cmd_err.eof?
62 STDERR.print cmd_err.entries.join('')
63 exit
64 end
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
71 if $gaps == []
72 puts "#{$size} VE(s) were already created"
73 exit
74 end
76 end
79 class CiconiaDaemonizer < Daemon::Base
80 def self.start
81 raise 'no $gaps' if $gaps == nil
82 raise 'no $template' if $template == nil
84 $gaps.each do |gap|
85 run_and_record("vzctl create #{gap} --ostemplate #{$template}")
86 end
88 if @@has_stderr
89 `mv #{@@pid_file} #{@@pid_file}.with_errors_output`
90 elsif @@has_stdout
91 `mv #{@@pid_file} #{@@pid_file}.with_output`
92 else
93 `rm #{@@pid_file}`
94 end
95 end
97 def self.stop
98 `kill #{@child_pid}`
99 end
102 CiconiaDaemonizer.daemonize("#{SCRIPT_NAME} #{$size} #{$template}", 0)