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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
#!@perlexec@
# BEGIN COPYRIGHT BLOCK
# 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., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA.
#
# Copyright (C) 2007 Red Hat, Inc.
# All rights reserved.
# END COPYRIGHT BLOCK
#
use lib qw(@perlpath@);
use strict;
use File::Basename;
use File::Path;
use Util;
use FileConn;
sub usage {
print(STDERR "Usage: $0 [-f] -i instance\n\n");
print(STDERR " Opts: -f - force removal\n");
print(STDERR " -i instance - instance name to remove (e.g. - slapd-example)\n");
}
# remove_tree($centry, $key, $instname, [$isparent, [$dontremove]])
# $centry: entry to look for the path to be removed
# $key: key to look for the path in the entry
# $instname: instance name "slapd-<ID>" to check the path
# $isparent: specify 1 to remove from the parent dir
# $dontremove: pattern not to be removed (e.g., ".db$")
sub remove_tree
{
my $centry = shift;
my $key = shift;
my $instname = shift;
my $isparent = shift;
my $dontremove = shift;
foreach my $path ( @{$centry->{$key}} )
{
my $rmdir = "";
my $rc = 0;
if ( 1 == $isparent )
{
$rmdir = dirname($path);
}
else
{
$rmdir = $path;
}
if ( -d $rmdir && $rmdir =~ /$instname/ )
{
if ( "" eq "$dontremove" )
{
$rc = rmtree($rmdir);
if ( 0 == $rc )
{
print STDERR "Warning: $rmdir was not removed.\n";
}
}
else
{
# Skip the dontremove files
$rc = opendir(DIR, $rmdir);
if ($rc)
{
while (defined(my $file = readdir(DIR)))
{
next if ( "$file" =~ /$dontremove/ );
next if ( "$file" eq "." );
next if ( "$file" eq ".." );
my $rmfile = $rmdir . "/" . $file;
my $rc0 = rmtree($rmfile);
if ( 0 == $rc0 )
{
print STDERR "Warning: $rmfile was not removed.\n";
}
}
closedir(DIR);
}
my $newrmdir = $rmdir . ".removed";
my $rc1 = 1;
if ( -d $newrmdir )
{
$rc1 = rmtree($newrmdir);
if ( 0 == $rc1 )
{
print STDERR "Warning: $newrmdir was not removed.\n";
}
}
if ( 0 < $rc1 )
{
rename($rmdir, $newrmdir);
}
}
}
}
}
sub remove_pidfile
{
my ($type, $instdir, $instname) = @_;
my $pattern = "^" . $type . ".*=";
my $pidline = `grep $pattern $instdir/start-slapd`;
chomp($pidline);
my ($key, $pidfile) = split(/=/, $pidline);
if ( -e $pidfile && $pidfile =~ /$instname/ )
{
unlink($pidfile);
}
}
my $i = 0;
my $force = "";
my $instname = "";
if ($#ARGV > 2) {
&usage; exit(1);
}
# load args from the command line
while ($i <= $#ARGV) {
if ( "$ARGV[$i]" eq "-f" ) {
$force = 1;
} elsif ("$ARGV[$i]" eq "-i") {
$i++;
$instname = $ARGV[$i];
}
$i++;
}
# Make sure the instance name option was provided.
unless ($instname) {
&usage; exit(1);
}
# Make sure a full instance name was provided.
my ($slapd, $inst) = split(/-/, $instname, 2);
unless ($inst) {
print STDERR "Full instance name must be specified (e.g. - slapd-example)\n";
exit 1;
}
my $configdir = "@instconfigdir@/slapd-$inst";
if ( ! -d $configdir )
{
print STDERR "Error: $configdir does not exist\n";
exit 1;
}
# read the config file to find out the paths
my $dseldif = "@instconfigdir@/$instname/dse.ldif";
my $conn = new FileConn($dseldif);
my $dn = "cn=config";
my $entry = $conn->search($dn, "base", "(cn=*)", 0);
if (!$entry)
{
print STDERR "Error: Search $dn in $dseldif failed: $entry\n";
exit 1;
}
$dn = "cn=config,cn=ldbm database,cn=plugins,cn=config";
my $dbentry = $conn->search($dn, "base", "(cn=*)", 0);
if (!$dbentry)
{
print "Error: Search $dn in $dseldif failed: $dbentry\n";
exit 1;
}
$conn->close();
# stop the server
my $instdir = "";
foreach my $path ( @{$entry->{"nsslapd-instancedir"}} )
{
if ( -d $path )
{
my $prog = $path . "/stop-slapd";
if (-x $prog) {
$? = 0;
# run the stop command
my $output = `$prog 2>&1`;
my $status = $?;
if ($status) {
# Ignore the stop failure
print STDERR "Warning: Could not stop directory server: $output\n";
}
$instdir = $path; # need to use it later...
} elsif (!$force) {
print STDERR "Error: The program $prog does not exist\n";
exit 1;
}
}
}
# remove physical dirs/files
remove_tree($dbentry, "nsslapd-directory", $instname, 1);
remove_tree($dbentry, "nsslapd-db-logdirectory", $instname, 1);
remove_tree($entry, "nsslapd-lockdir", $instname);
remove_tree($entry, "nsslapd-tmpdir", $instname);
remove_tree($entry, "nsslapd-bakdir", $instname, 1);
remove_tree($entry, "nsslapd-errorlog", $instname, 1);
# instance dir
if ( -d $instdir && $instdir =~ /$instname/ )
{
# clean up pid files (if any)
remove_pidfile("STARTPIDFILE", $instdir, $instname);
remove_pidfile("PIDFILE", $instdir, $instname);
my $rc = rmtree($instdir);
if ( 0 == $rc )
{
print STDERR "Warning: $instdir was not removed.\n";
}
}
# Finally, config dir
remove_tree($entry, "nsslapd-schemadir", $instname, 1, "\.db\$");
# if we got here, report success
print "Instance $instname removed.\n";
exit 0;
|