2 # Mwamko Copyright (C) 2007 Aleksandr O. Levchuk
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as
6 # published by the Free Software Foundation, either version 3 of the
7 # License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SCRIPT_DIR
= File
.dirname(File
.expand_path(__FILE__
))
19 SCRIPT_NAME
= __FILE__
.split('/')[-1]
22 require File
.expand_path("#{VzStart::SCRIPT_DIR}/../config/mwamko_constants")
23 require File
.expand_path("#{VzStart::SCRIPT_DIR}/../lib/ciconia_daemonizer")
24 require File
.expand_path("#{VzStart::SCRIPT_DIR}/../lib/database_for_scripts")
27 if ARGV.size
!= 1 and ARGV[1] != '--kill-daemon'
30 puts
" #{VzStart::SCRIPT_NAME} \e[4mVEID\e[24m"
31 puts
" #{VzStart::SCRIPT_NAME} \e[4mVEID\e[24m --kill-daemon"
34 puts
" --kill_daemon "
35 puts
" Kills the daemon and removes related PID files from /tmp"
37 puts NO_DEFAULT_RAILS_ENV
43 raise 'Unsafe VEID' if !(ARGV[0] =~
/^[0-9]*$/)
47 if ARGV[1] != '--kill-daemon'
48 # Check against vzlist
50 current_list
= OpenVZ
.list(:veid, :status).collect
do |row
|
52 veid
, status
= row
.veid
.to_i
, row
.status
56 if status
!= 'stopped'
57 raise "VE #{$VEID} is already #{status}"
63 raise ["VE #{$VEID} does not exist on the system.",
64 "Make sure you are using the correct VEID.",
66 "If you are seeing this VEID on Mwamko then:",
67 RUN_UPDATE_CACHE_ERROR
,
71 if VirtualEnvironment
.find_by_veid($VEID).nil?
72 raise ["VE #{$VEID} is not in Mwamko's database.",
73 RUN_UPDATE_CACHE_ERROR
,
80 # All checks passed. Time to define and start the daemon
84 class Daemon
< CiconiaDaemonizer
::Base
87 ReEstablishDatabaseConnection
.call
89 # Contract: $VEID is integer
90 # Contract: $VEID was recently a VE on the selected OpenVZ server
91 # Contract: $VEID was recently stopped
92 # Contract: $VEID was recently in Mwamko's database
94 raise 'no $VEID' if $VEID == nil
96 ve
= VirtualEnvironment
.find_by_veid($VEID)
98 raise ["#{$VEID} is no longer in the database.",
99 RUN_UPDATE_CACHE_ERROR
,
104 OpenVZ
.set_ip_in_ve(ve
)
110 "#{REMOTE_VZTOOLS_PATH}/vzctl",
114 start_the_ve_cmd
= TO_SSH
.call(remote_cmd
, OpenvzServer
.selected_server
)
115 results
= RUN4(start_the_ve_cmd
)
117 # Wait for SSHD to come up
119 remote_cmd
= ["#{REMOTE_SUDO}",
120 "#{REMOTE_VZTOOLS_PATH}/vzctl exec",
121 "#{$VEID} 'ps | grep sshd'",
124 start_the_ve_cmd
= TO_SSH
.call(remote_cmd
, OpenvzServer
.selected_server
)
125 results
= RUN4(start_the_ve_cmd
)
127 if results
.out
.empty
?
134 ve
= VirtualEnvironment
.find_by_veid($VEID.to_i
)
135 ve
.status
= 'started'
136 ve
.updated_on
= Time
.now
139 require "#{VzStart::SCRIPT_DIR}/../lib/password_gen"
141 upass
= PasswordGen
.generate_pronounceable
142 uname
= User
.find(ve
.uid
).name
144 OpenVZ
.set_ve_userpassw(ve
.veid
, uname
, upass
)
145 OpenVZ
.add_user_to_ve_sudoers(uname
, ve
.veid
)
146 OpenVZ
.ve_DNS_config(ve
.veid
)
147 OpenVZ
.ssh_keygen(ve
.veid
)
149 ve
.setup_needed
= false
160 # The daemon is ready.
161 # Now it is time to run it, respecting the Queueing policy
163 task_identifier
= "#{VzStart::SCRIPT_NAME} #{$VEID}"
165 if ARGV[1] != '--kill-daemon'
166 Daemon
.daemonize(task_identifier
)