Homepage: first publish of a full set of parts for the server
[mwamko.git] / script / vz-start
blob8244a18138b90b49b271d6f6e331a354bd98fe07
1 #!/usr/bin/env ruby
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/>.
17 module VzStart
18 SCRIPT_DIR = File.dirname(File.expand_path(__FILE__))
19 SCRIPT_NAME = __FILE__.split('/')[-1]
20 end
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'
29 puts "SYNOPSIS:"
30 puts " #{VzStart::SCRIPT_NAME} \e[4mVEID\e[24m"
31 puts " #{VzStart::SCRIPT_NAME} \e[4mVEID\e[24m --kill-daemon"
32 puts ""
33 puts "DESCRIPTION:"
34 puts " --kill_daemon "
35 puts " Kills the daemon and removes related PID files from /tmp"
36 puts ""
37 puts NO_DEFAULT_RAILS_ENV
39 exit
40 end
42 # Check ARGV
43 raise 'Unsafe VEID' if !(ARGV[0] =~ /^[0-9]*$/)
45 $VEID = ARGV[0].to_i
47 if ARGV[1] != '--kill-daemon'
48 # Check against vzlist
49 ve_found = false
50 current_list = OpenVZ.list(:veid, :status).collect do |row|
52 veid, status = row.veid.to_i, row.status
54 if $VEID == veid
55 ve_found = true
56 if status != 'stopped'
57 raise "VE #{$VEID} is already #{status}"
58 end
59 end
60 end
62 if !ve_found
63 raise ["VE #{$VEID} does not exist on the system.",
64 "Make sure you are using the correct VEID.",
65 "",
66 "If you are seeing this VEID on Mwamko then:",
67 RUN_UPDATE_CACHE_ERROR,
68 ""].join("\n")
69 end
71 if VirtualEnvironment.find_by_veid($VEID).nil?
72 raise ["VE #{$VEID} is not in Mwamko's database.",
73 RUN_UPDATE_CACHE_ERROR,
74 ""].join("\n")
75 end
77 end
80 # All checks passed. Time to define and start the daemon
84 class Daemon < CiconiaDaemonizer::Base
85 def self.start
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)
97 if ve.nil?
98 raise ["#{$VEID} is no longer in the database.",
99 RUN_UPDATE_CACHE_ERROR,
100 ""].join("\n")
103 ve.grab_available_ip
104 OpenVZ.set_ip_in_ve(ve)
106 ve.save!
108 remote_cmd = [
109 "#{REMOTE_SUDO}",
110 "#{REMOTE_VZTOOLS_PATH}/vzctl",
111 "start #{$VEID}"
112 ].join("\\\n ")
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
118 loop do
119 remote_cmd = ["#{REMOTE_SUDO}",
120 "#{REMOTE_VZTOOLS_PATH}/vzctl exec",
121 "#{$VEID} 'ps | grep sshd'",
122 ""].join("\\\n ")
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?
128 sleep 4
129 else
130 break
134 ve = VirtualEnvironment.find_by_veid($VEID.to_i)
135 ve.status = 'started'
136 ve.updated_on = Time.now
138 if ve.setup_needed
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
152 ve.save!
155 def self.stop
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)
167 exit