3 SCRIPT_DIR
= File
.dirname(File
.expand_path(__FILE__
))
4 SCRIPT_NAME
= $0.split('/')[-1]
6 SSH
= '/usr/bin/ssh' # TODO: pipe to ssh istead of using
8 VZLIST
= '/usr/sbin/vzlist'
9 VZCTL
= '/usr/sbin/vzctl'
12 require "/etc/mwamko/available_ip_addresses.range"
14 if ARGV.size
!= 1 and ARGV[1] != '--kill-daemon'
16 STDERR.puts
"SYNOPSIS:"
17 STDERR.puts
" #{SCRIPT_NAME} \e[4mVEID\e[24m"
18 STDERR.puts
" #{SCRIPT_NAME} \e[4mVEID\e[24m --kill-daemon"
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 VEID' if !(ARGV[0] =~
/^[0-9]*$/)
32 if ARGV[1] != '--kill-daemon'
33 # Check against vzlist
34 out
= `#{VZLIST} -a --no-header -o veid,status`
37 current_list
= out
.collect
do |line
|
39 ve
, status
= line
.strip
.split(' ')
45 if status
!= 'stopped'
46 STDERR.puts
"VE #{$VEID} is already #{status}"
53 STDERR.puts
"VE #{$VEID} does not exist on the system"
59 # All checks passed. Time to define and start the daemon
61 require "#{SCRIPT_DIR}/../lib/ciconia_daemonizer"
63 class CiconiaDaemonizer
< Daemon
::Base
65 raise 'no $VEID' if $VEID == nil
67 run_and_record("echo > /dev/null")
70 require "#{SCRIPT_DIR}/../lib/database_for_scripts"
73 detail
.message
+ "\n" +
74 detail
.backtrace
.join("\n")
75 run_and_record("echo > /dev/null")
80 ve
= VirtualEnvironment
.find_by_veid($VEID.to_i
)
82 OpenVZ
.set_ip_in_ve(ve
)
88 detail
.message
+ "\n" +
89 detail
.backtrace
.join("\n")
90 run_and_record("echo > /dev/null")
93 run_and_record("#{VZCTL} start #{$VEID}")
95 # if !@@has_stderr and !@@has_rubyerr
98 # LocalIP.find_all_by_veid($VEID.to_i).collect {|lip| lip.ip}
101 # local_ips.collect { |lip| "--ipadd #{lip}" }.join(' ')
103 # unless ip_list.empty?
104 # run_and_record("#{VZCTL} set #{$VEID} #{ip_list} --save")
108 # detail.message + "\n" +
109 # detail.backtrace.join("\n")
110 # run_and_record("echo > /dev/null")
117 # The part in () is a hack - it ignores vzqouta errors
118 if (@
@std_errors_txt =~
/vzquota/ or (!@
@has_stderr)) and !@
@has_rubyerr
120 # Wait for SSHD to come up
122 if `#{VZCTL} exec #{$VEID} "ps | grep sshd"`.empty
?
130 ve
= VirtualEnvironment
.find_by_veid($VEID.to_i
)
131 ve
.status
= 'started'
132 ve
.updated_on
= Time
.now
135 require "#{SCRIPT_DIR}/../lib/password_gen"
137 upass
= PasswordGen
.generate_pronounceable
138 uname
= User
.find(ve
.uid
).name
140 OpenVZ
.set_ve_userpassw(ve
.veid
, uname
, upass
)
141 OpenVZ
.add_user_to_ve_sudoers(uname
, ve
.veid
)
142 OpenVZ
.ve_DNS_config(ve
.veid
)
143 OpenVZ
.ssh_keygen(ve
.veid
)
145 ve
.setup_needed
= false
152 detail
.message
+ "\n" +
153 detail
.backtrace
.join("\n")
154 run_and_record("echo > /dev/null")
160 `mv #{@@pid_file} #{@@pid_file[0..-5]}.rubyerr`
162 `mv #{@@pid_file} #{@@pid_file[0..-5]}.stderr`
164 `mv #{@@pid_file} #{@@pid_file[0..-5]}.out`
178 # The daemon is ready.
179 # Now it is time to run it, respecting the Queueing policy
181 task_identifier
= "#{SCRIPT_NAME} #{$VEID}"
183 if ARGV[1] != '--kill-daemon'
184 CiconiaDaemonizer
.daemonize(task_identifier
, 0)
190 # * If it is your turn then it is time to daemonize and run
191 # * Else check if the queue is running
192 # * If it is, then put yourself in the queue and exit this script
193 # * If not, then safe start the Queue
194 ##require "#{SCRIPT_DIR}/../lib/queue"
195 ##q = Queue.new(SCRIPT_NAME, task_identifier)
198 ##CiconiaDaemonizer.daemonize(task_identifier, 0) rescue nil
200 # Figure out the next script in the queue
201 # * If there is one then run it