summaryrefslogtreecommitdiffstats
path: root/contrib/ruby/examples/remove_system.rb
blob: d592165ae4f736acbe14fe65bf6aff1488d393ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/ruby
#
# remove_system.rb - example of using rubygem-cobbler to remove a system.
# 
# Copyright (C) 2008 Red Hat, Inc.
# Written by Darryl L. Pierce <dpierceredhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA  02110-1301, USA.  A copy of the GNU General Public License is
# also available at http://www.gnu.org/copyleft/gpl.html.
 
base = File.expand_path(File.join(File.dirname(__FILE__), ".."))
$LOAD_PATH << File.join(base, "lib")
$LOAD_PATH << File.join(base, "examples")

require 'getoptlong'

require 'cobbler'

include Cobbler

opts = GetoptLong.new(
  ["--server",   "-s", GetoptLong::REQUIRED_ARGUMENT ],
  ["--system",   "-t", GetoptLong::REQUIRED_ARGUMENT ],
  ["--username", "-u", GetoptLong::REQUIRED_ARGUMENT ],
  ["--password", "-p", GetoptLong::REQUIRED_ARGUMENT ],
  ["--help",    "-h", GetoptLong::NO_ARGUMENT]
)

hostname = nil
system   = nil
username = nil
password = nil

opts.each do |opt, arg|
  case opt
  when '--server'   then hostname = arg
  when '--system'   then system   = arg
  when '--username' then username = arg
  when '--password' then password = arg
  when '--help'     then 
    puts "Usage: #{$0} --server hostname --system system-name\n"
  end
end

SystemExit.new('No hostname specified.') unless hostname

if hostname && system && username && password
  System.hostname = hostname
  System.username = username
  System.password = password
  
  puts "Removing \"#{system}\"..."
    
  begin
    puts "Deleted \"#{system}" if System.remove(system)
  rescue Exception => e
    puts "Error: #{e.message}"
  end
end