Merge lp:~percona-toolkit-dev/percona-toolkit/pt-agent-uninstall into lp:~percona-toolkit-dev/percona-toolkit/release-2.2.5

Proposed by Daniel Nichter
Status: Merged
Approved by: Daniel Nichter
Approved revision: 593
Merged at revision: 593
Proposed branch: lp:~percona-toolkit-dev/percona-toolkit/pt-agent-uninstall
Merge into: lp:~percona-toolkit-dev/percona-toolkit/release-2.2.5
Diff against target: 202 lines (+162/-2)
1 file modified
bin/pt-agent (+162/-2)
To merge this branch: bzr merge lp:~percona-toolkit-dev/percona-toolkit/pt-agent-uninstall
Reviewer Review Type Date Requested Status
Daniel Nichter Approve
Review via email: mp+186142@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Daniel Nichter (daniel-nichter) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/pt-agent'
2--- bin/pt-agent 2013-09-17 17:41:57 +0000
3+++ bin/pt-agent 2013-09-17 19:29:11 +0000
4@@ -5368,7 +5368,9 @@
5
6 $o->usage_or_errors();
7
8- if ( $o->get('interactive') || $o->get('install') ) {
9+ if ( $o->get('interactive')
10+ || $o->get('install')
11+ || $o->get('uninstall') ) {
12 $OUTPUT_AUTOFLUSH = 1
13 }
14
15@@ -5408,7 +5410,7 @@
16 );
17
18 # ########################################################################
19- # --install and exit.
20+ # --(un)install and exit.
21 # ########################################################################
22 if ( $o->get('install') ) {
23 $exit_on_signals = 1;
24@@ -5420,6 +5422,14 @@
25 );
26 return $exit_status;
27 }
28+ elsif ( $o->get('uninstall') ) {
29+ $exit_on_signals = 1;
30+ uninstall(
31+ OptionParser => $o,
32+ Cxn => $cxn,
33+ );
34+ return $exit_status;
35+ }
36
37 # ########################################################################
38 # Nothing works without required Perl modules.
39@@ -8627,6 +8637,151 @@
40 return scalar @missing_deps;
41 }
42
43+# ################ #
44+# --uninstall subs #
45+# ################ #
46+
47+sub uninstall {
48+ my (%args) = @_;
49+ have_required_args(\%args, qw(
50+ OptionParser
51+ Cxn
52+ )) or die;
53+ my $o = $args{OptionParser};
54+ my $cxn = $args{Cxn};
55+ my $flags = $args{flags};
56+
57+ if ( $EUID != 0 ) {
58+ die "You must run pt-agent --uninstall as root.\n";
59+ }
60+
61+ my $config_file = get_config_file();
62+ my $lib_dir = $o->get('lib');
63+ my $spool_dir = $o->get('spool');
64+
65+ print "Uninstalling pt-agent...\n";
66+
67+ # Stop the agent. This must succeed else it's not safe to remove its
68+ # files and dirs while it's running.
69+ my $stopped = stop_agent(
70+ pid_file => $o->get('pid'),
71+ lib_dir => $o->get('lib'),
72+ );
73+ if ( !$stopped ) {
74+ $logger->fatal("Failed to stop pt-agent.");
75+ }
76+
77+ # Agent is stopped so now it's safe to remove all our files and dirs.
78+ my @shell_cmds;
79+ if ( -d $lib_dir ) {
80+ push @shell_cmds, "rm -rf $lib_dir";
81+ }
82+ if ( -d $spool_dir ) {
83+ push @shell_cmds, "rm -rf $spool_dir"
84+ }
85+ if ( -d "/etc/percona/agent" ) {
86+ push @shell_cmds, "rm -rf /etc/percona/agent/";
87+ }
88+ if ( -f $config_file ) {
89+ push @shell_cmds, "rm -f $config_file"
90+ }
91+
92+ my $rm_files_ok;
93+ if ( scalar @shell_cmds ) {
94+ print "Are you sure you want to run these command "
95+ . "to uninstall pt-agent?\n"
96+ . join("\n", map { " $_" } @shell_cmds) . "\n";
97+ while ( !$rm_files_ok ) {
98+ print "Enter 'yes' to run these commands, or CTRL-C to abort: ";
99+ $rm_files_ok = <STDIN>;
100+ chomp($rm_files_ok) if $rm_files_ok;
101+ if ( $rm_files_ok && $rm_files_ok eq 'yes' ) {
102+ last;
103+ }
104+ else {
105+ $rm_files_ok = 0;
106+ }
107+ }
108+ # CTRL-C should prevent us from getting here, but just in case:
109+ return if @shell_cmds && !$rm_files_ok;
110+ foreach my $cmd ( @shell_cmds ) {
111+ print "$cmd\n";
112+ system($cmd);
113+ if ( $CHILD_ERROR ) {
114+ warn "Command failed: $cmd\n";
115+ $rm_files_ok = 0;
116+ }
117+ }
118+ }
119+ else {
120+ warn "ERROR: No pt-agent files or directories found. You can ignore this "
121+ . "error if the agent is not installed, or if it has already been "
122+ . "removed. Else, verify that the values in $config_file are "
123+ . "correct and try again.\n";
124+ }
125+
126+ eval {
127+ $cxn->connect();
128+ };
129+ if ( $EVAL_ERROR ) {
130+ chomp $EVAL_ERROR;
131+ die "ERROR: Cannot connect to MySQL: $EVAL_ERROR\n"
132+ . "Please re-run pt-agent --uninstall and specify MySQL connection "
133+ . "options like --user and --host to connect to MySQL as a user "
134+ . "with sufficient privileges to drop MySQL users.\n";
135+ }
136+
137+ my $drop_mysql_user_ok;
138+ eval {
139+ $cxn->dbh->selectall_arrayref("SHOW GRANTS FOR 'pt_agent'\@'localhost'");
140+ };
141+ if ( !$EVAL_ERROR ) {
142+ my $sql = "DROP USER 'pt_agent'\@'localhost'";
143+ print "Are you sure you want to execute this statement "
144+ . "to remove the pt-agent MySQL user?\n$sql\n";
145+ while ( !$drop_mysql_user_ok ) {
146+ print "Enter 'yes' to execute this statment, or CTRL-C to abort: ";
147+ $drop_mysql_user_ok = <STDIN>;
148+ chomp($drop_mysql_user_ok) if $drop_mysql_user_ok;
149+ if ( $drop_mysql_user_ok && $drop_mysql_user_ok eq 'yes' ) {
150+ last;
151+ }
152+ else {
153+ $drop_mysql_user_ok = 0;
154+ }
155+ }
156+ # CTRL-C should prevent us from getting here, but just in case:
157+ return unless $drop_mysql_user_ok;
158+ eval {
159+ $cxn->dbh->do($sql);
160+ };
161+ if ( $EVAL_ERROR ) {
162+ warn "Error dropping the pt-agent MySQL user: $EVAL_ERROR";
163+ $drop_mysql_user_ok = 0;
164+ }
165+ }
166+ else {
167+ warn "ERROR: No pt-agent MySQL user found. You can ignore this "
168+ . "error if the agent is not installed, or if it has already been "
169+ . "removed. Else, verify that the values in $config_file are "
170+ . "correct and try again.\n";
171+ }
172+
173+ print "\n";
174+ if ( $rm_files_ok && $drop_mysql_user_ok ) {
175+ print "pt-agent and all its data has been removed from this server, "
176+ . "but the agent and any data it sent has not been deleted from "
177+ . "Percona Cloud Tools. Go to https://cloud.percona.com/agents "
178+ . "to delete the agent.\n";
179+ }
180+ else {
181+ warn "Uninstalling pt-agent failed. See previous output for errors "
182+ . "and try again. Contact Percona if you need help.\n";
183+ }
184+
185+ return
186+}
187+
188 # ################## #
189 # Misc and util subs #
190 # ################## #
191@@ -9216,6 +9371,11 @@
192
193 Stop pt-agent and all services.
194
195+=item --uninstall
196+
197+Completely remove pt-agent and all its data from the server. This does not
198+delete the agent from https://cloud.percona.com.
199+
200 =item --user
201
202 short form: -u; type: string

Subscribers

People subscribed via source and target branches

to all changes: