-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete-zone
executable file
·167 lines (132 loc) · 4.04 KB
/
delete-zone
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to delete zones from the database
#
use strict;
use HOSTDB;
use Getopt::Long;
use vars qw ($opt_id $opt_zonename $opt_debug $opt_force $opt_quiet $opt_sue_me);
my %o = ();
my $res = GetOptions (
"id=i",
"zonename=s",
"debug",
"force",
"sue_me",
"quiet"
);
#die ("$0: Parsing options failed\n") if ($res);
my ($search_for, $datatype);
my $debug = defined ($opt_debug);
if (defined ($opt_id)) {
$datatype = "ID";
$search_for = $opt_id;
}
if (defined ($opt_zonename)) {
die ("$0: Can't specify more than one search criteria at once (zonename)\n") if ($search_for);
$datatype = "ZONENAME";
$search_for = $opt_zonename;
}
usage("") if (! $search_for);
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
my $zone = get_zone ($search_for, $datatype);
die ("$0: Could not find zone object\n") unless ($zone);
if (! $opt_quiet) {
printf ("%-25s %-10s %-15s %s\n", "zonename", "serial", "owner", "delegated");
printf "%-25s %-10s %-15s %s\n", $zone->zonename (),
$zone->serial (), $zone->owner (), $zone->delegated ();
}
if (! $opt_force) {
die ("$0: Dying, you have to delete with --force\n");
}
my $zonename = $zone->zonename ();
my @hosts_in_zone = $hostdb->findhostbyzone ($zonename);
my @aliases_in_zone = $hostdb->findhostaliasbyzone ($zonename);
if (@hosts_in_zone or @aliases_in_zone) {
if (@hosts_in_zone) {
print ("\nThe following hosts are currently in the zone you are trying to remove :\n\n");
printf " %-7s %-7s %-16s %s\n", 'id', 'partof', 'ip', 'hostname';
foreach my $host (@hosts_in_zone) {
printf " %-7s %-7s %-16s %s\n", $host->id (),
defined ($host->partof ())?$host->partof ():'-',
$host->ip (), $host->hostname ();
}
}
if (@aliases_in_zone) {
print ("\nThe following aliases are currently in the zone you are trying to remove :\n\n");
printf " %-7s %-25s %s\n", 'id', 'aliasname', 'hostname';
foreach my $alias (@aliases_in_zone) {
my $h = $hostdb->findhostbyid ($alias->hostid ());
my $hostname = 'NOT FOUND';
if ($h) {
$hostname = $h->hostname () . ' (id ' . $h->id () . ')';
}
printf " %-7s %-25s %s\n", $alias->id (), $alias->aliasname (), $hostname;
}
}
if (! $opt_sue_me) {
print "\n\nMove the hosts to another zone before removing the zone, or if\n" .
"you REALLY want to do it anyways run this command again with --sue_me\n\n";
die ("Not deleting zone since it contains host objects.\n");
} else {
print "\n\nSince you said I could sue you, I will remove the zone anyways\n" .
"but PLEASE run 'housekeep-dnszone' now or DNS zone config building\n" .
"might get really messed up!\n\n";
}
}
$zone->delete ($opt_force?"YES":"WELL, UHH") or die ("$0: Could not delete zone object - $zone->{error}\n");
print ("Zone object deleted\n") if (! $opt_quiet);
exit (0);
sub usage
{
my $msg = shift;
# interpolation
die(<<EOT);
${msg}Syntax: $0 datatype search_for
options:
--debug debug
--force well, force
--quiet quiet
datatypes:
--id host id
--zonename name of the zone
EOT
}
sub get_zone
{
my $search_for = shift;
my $datatype = shift;
my @zone_refs;
if ($datatype eq "ID") {
if ($search_for =~ /^\d+$/) {
@zone_refs = $hostdb->findzonebyid ($search_for);
} else {
warn ("Search failed: '$search_for' is not a valid ID");
return undef;
}
} elsif ($datatype eq "ZONENAME") {
if ($hostdb->is_valid_domainname ($search_for)) {
@zone_refs = $hostdb->findzonebyname ($search_for);
} else {
warn ("Search failed: '$search_for' is not a valid domain name");
return undef;
}
} else {
warn ("Search failed: don't recognize datatype '$datatype'");
return undef;
}
if ($#zone_refs == -1) {
warn ("$0: Search for '$search_for' (type '$datatype') failed - no match\n");
return undef;
}
if ($#zone_refs == -1) {
my $count = $#zone_refs + 1;
warn ("$0: Search for '$search_for' (type '$datatype') failed - more than one ($count) match\n");
return undef;
}
return $zone_refs[0];
}