1 /*
2    Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 
26 #include <ndb_global.h>
27 
28 #include <OutputStream.hpp>
29 #include <NdbOut.hpp>
30 #include <NdbSleep.h>
31 #include <getarg.h>
32 
33 #include <NdbRestarter.hpp>
34 #include <NDBT.hpp>
35 
main(int argc,const char ** argv)36 int main(int argc, const char** argv){
37   ndb_init();
38 
39   const char* _hostName = NULL;
40   int _initial = 0;
41   int _help = 0;
42   int _wait = 1;
43 
44 
45   struct getargs args[] = {
46     { "initial", 'i', arg_flag, &_initial, "Do initial restart", ""},
47     { "wait", '\0', arg_negative_flag, &_wait, "Wait until restarted(default=true)", ""},
48     { "usage", '?', arg_flag, &_help, "Print help", "" }
49 
50   };
51   int num_args = sizeof(args) / sizeof(args[0]);
52   int optind = 0;
53   char desc[] =
54     "hostname:port\n"\
55     "This program will connect to the mgmsrv of a NDB cluster\n"\
56     " and restart the cluster. \n";
57 
58   if(getarg(args, num_args, argc, argv, &optind) || _help) {
59     arg_printusage(args, num_args, argv[0], desc);
60     return NDBT_ProgramExit(NDBT_WRONGARGS);
61   }
62   _hostName = argv[optind];
63 
64   NdbRestarter restarter(_hostName);
65   setOutputLevel(1); // Show only g_err
66   int result = NDBT_OK;
67   if (_initial){
68     ndbout << "Restarting cluster with initial restart" << endl;
69     if (restarter.restartAll(true, false, false) != 0)
70       result = NDBT_FAILED;
71   } else {
72     ndbout << "Restarting cluster " << endl;
73     if (restarter.restartAll() != 0)
74       result = NDBT_FAILED;
75   }
76   if (result == NDBT_FAILED){
77     g_err << "Failed to restart cluster" << endl;
78     return NDBT_ProgramExit(NDBT_FAILED);
79   }
80 
81   if (_wait == 1){
82     ndbout << "Waiting for cluster to start" << endl;
83     if ( restarter.waitClusterStarted(120) != 0){
84       ndbout << "Failed waiting for restart of cluster" << endl;
85       result = NDBT_FAILED;
86     }
87   }
88   ndbout << "Cluster restarted" << endl;
89 
90   return NDBT_ProgramExit(result);
91 }
92