Ensure "init all" (and other init commands too) does not remove existing data
authorPavan Deolasee <[email protected]>
Tue, 14 Jun 2016 06:22:13 +0000 (11:52 +0530)
committerPavan Deolasee <[email protected]>
Tue, 14 Jun 2016 06:22:13 +0000 (11:52 +0530)
directories unless "force" option is used

We'd tried to fix this earlier, but looks like double quote is not getting
passed to the shell correctly. Instead use a single quote.

Report by Pallavi Sontakke during QA testing.

contrib/pgxc_ctl/coord_cmd.c
contrib/pgxc_ctl/datanode_cmd.c
contrib/pgxc_ctl/gtm_cmd.c

index aa68a9aed569319b75268ba5fc823bef6bbb2fc1..ec69a35760386abc96fd964c1c19239e8c463689 100644 (file)
@@ -82,9 +82,9 @@ cmd_t *prepare_initCoordinatorMaster(char *nodeName)
        remoteDirCheck[0] = '\0';
        if (!forceInit)
        {
-               sprintf(remoteDirCheck, "if [ \"$(ls -A %s 2> /dev/null)\" ]; then echo 'ERROR: "
+               sprintf(remoteDirCheck, "if [ '$(ls -A %s 2> /dev/null)' ]; then echo 'ERROR: "
                                "target directory (%s) exists and not empty. "
-                               "Skip Coordinator initilialization'; exit; fi",
+                               "Skip Coordinator initilialization'; exit; fi;",
                                aval(VAR_coordMasterDirs)[jj],
                                aval(VAR_coordMasterDirs)[jj]
                           );
@@ -92,7 +92,7 @@ cmd_t *prepare_initCoordinatorMaster(char *nodeName)
 
        cmd = cmdInitdb = initCmd(aval(VAR_coordMasterServers)[jj]);
        snprintf(newCommand(cmdInitdb), MAXLINE, 
-                        "%s;"
+                        "%s"
                         "rm -rf %s;"
                         "mkdir -p %s;"
                         "PGXC_CTL_SILENT=1 initdb --nodename %s -D %s",
@@ -269,7 +269,7 @@ cmd_t *prepare_initCoordinatorSlave(char *nodeName)
        remoteDirCheck[0] = '\0';
        if (!forceInit)
        {
-               sprintf(remoteDirCheck, "if [ \"$(ls -A %s 2> /dev/null)\" ]; then echo 'ERROR: "
+               sprintf(remoteDirCheck, "if [ '$(ls -A %s 2> /dev/null)' ]; then echo 'ERROR: "
                                "target directory (%s) exists and not empty. "
                                "Skip Coordinator slave initilialization'; exit; fi;",
                                aval(VAR_coordSlaveDirs)[idx],
index 109c0f5a064399e66211ef51ca1bc7897c01a395..dff0001e768a327d98649bcc72fb645bc0908ffd 100644 (file)
@@ -65,6 +65,7 @@ cmd_t *prepare_initDatanodeMaster(char *nodeName)
        FILE *f;
        char timeStamp[MAXTOKEN+1];
        char remoteDirCheck[MAXPATH * 2 + 128];
+       char remoteWalDirCheck[MAXPATH * 2 + 128];
        bool wal;
 
        if ((idx = datanodeIdx(nodeName)) < 0)
@@ -78,19 +79,20 @@ cmd_t *prepare_initDatanodeMaster(char *nodeName)
                wal = false;
 
        remoteDirCheck[0] = '\0';
+       remoteWalDirCheck[0] = '\0';
        if (!forceInit)
        {
-               sprintf(remoteDirCheck, "if [ \"$(ls -A %s 2> /dev/null)\" ]; then echo 'ERROR: "
+               sprintf(remoteDirCheck, "if [ '$(ls -A %s 2> /dev/null)' ]; then echo 'ERROR: "
                                "target directory (%s) exists and not empty. "
-                               "Skip Datanode initilialization'; exit; fi",
+                               "Skip Datanode initilialization'; exit; fi;",
                                aval(VAR_datanodeMasterDirs)[idx],
                                aval(VAR_datanodeMasterDirs)[idx]
                           );
                if (wal)
                {
-                       sprintf(remoteDirCheck, "if [ \"$(ls -A %s 2> /dev/null)\" ]; then echo 'ERROR: "
+                       sprintf(remoteWalDirCheck, "if [ '$(ls -A %s 2> /dev/null)' ]; then echo 'ERROR: "
                                        "target directory (%s) exists and not empty. "
-                                       "Skip Datanode initilialization'; exit; fi",
+                                       "Skip Datanode initilialization'; exit; fi;",
                                        aval(VAR_datanodeMasterWALDirs)[idx],
                                        aval(VAR_datanodeMasterWALDirs)[idx]
                                   );
@@ -101,10 +103,11 @@ cmd_t *prepare_initDatanodeMaster(char *nodeName)
        /* Build each datanode's initialize command */
        cmd = cmdInitdb = initCmd(aval(VAR_datanodeMasterServers)[idx]);
        snprintf(newCommand(cmdInitdb), MAXLINE,
-                        "%s;"
+                        "%s %s"
                         "rm -rf %s;"
                         "mkdir -p %s; PGXC_CTL_SILENT=1 initdb --nodename %s %s %s -D %s",
                         remoteDirCheck,
+                        remoteWalDirCheck,
                         aval(VAR_datanodeMasterDirs)[idx], aval(VAR_datanodeMasterDirs)[idx],
                         aval(VAR_datanodeNames)[idx],
                         wal ? "-X" : "",
@@ -296,9 +299,9 @@ cmd_t *prepare_initDatanodeSlave(char *nodeName)
        remoteDirCheck[0] = '\0';
        if (!forceInit)
        {
-               sprintf(remoteDirCheck, "if [ \"$(ls -A %s 2> /dev/null)\" ]; then echo 'ERROR: "
+               sprintf(remoteDirCheck, "if [ '$(ls -A %s 2> /dev/null)' ]; then echo 'ERROR: "
                                "target directory (%s) exists and not empty. "
-                               "Skip Datanode initilialization'; exit; fi",
+                               "Skip Datanode initilialization'; exit; fi;",
                                aval(VAR_datanodeSlaveDirs)[idx],
                                aval(VAR_datanodeSlaveDirs)[idx]
                           );
index 3994bda73aa466d0238df35875dacceaac760d7c..8371e101c0522ca0b8d89f273b6a3170807dc2e6 100644 (file)
@@ -58,9 +58,9 @@ cmd_t *prepare_initGtmMaster(bool stop)
        remoteDirCheck[0] = '\0';
        if (!forceInit)
        {
-               sprintf(remoteDirCheck, "if [ \"$(ls -A %s 2> /dev/null)\" ]; then echo 'ERROR: "
+               sprintf(remoteDirCheck, "set -x; if [ '$(ls -A %s 2> /dev/null)' ]; then echo 'ERROR: "
                                "target directory (%s) exists and not empty. "
-                               "Skip GTM initilialization'; exit; fi",
+                               "Skip GTM initilialization'; exit; fi;",
                                sval(VAR_gtmMasterDir),
                                sval(VAR_gtmMasterDir)
                           );
@@ -69,7 +69,7 @@ cmd_t *prepare_initGtmMaster(bool stop)
        /* Kill current gtm, bild work directory and run initgtm */
        cmdInitGtmMaster = initCmd(sval(VAR_gtmMasterServer));
        snprintf(newCommand(cmdInitGtmMaster), MAXLINE,
-                        "%s;"
+                        "%s"
                         "[ -f %s/gtm.pid ] && gtm_ctl -D %s -m immediate -Z gtm stop;"
                         "rm -rf %s;"
                         "mkdir -p %s;"
@@ -567,7 +567,6 @@ cmd_t *prepare_stopGtmMaster(void)
        cmdGtmCtl = initCmd(sval(VAR_gtmMasterServer));
        snprintf(newCommand(cmdGtmCtl), MAXLINE,
                         "gtm_ctl stop -Z gtm -D %s",
-                        sval(VAR_gtmMasterDir),
                         sval(VAR_gtmMasterDir));
        return(cmdGtmCtl);
 }
@@ -1043,6 +1042,7 @@ cmd_t *prepare_initGtmProxy(char *nodeName)
        FILE *f;
        char timestamp[MAXTOKEN+1];
        char **fileList = NULL;
+       char remoteDirCheck[MAXPATH * 2 + 128];
 
        if ((idx = gtmProxyIdx(nodeName)) < 0)
        {
@@ -1050,13 +1050,25 @@ cmd_t *prepare_initGtmProxy(char *nodeName)
                return NULL;
        }
 
+       remoteDirCheck[0] = '\0';
+       if (!forceInit)
+       {
+               sprintf(remoteDirCheck, "if [ '$(ls -A %s 2> /dev/null)' ]; then echo 'ERROR: "
+                               "target directory (%s) exists and not empty. "
+                               "Skip GTM proxy initilialization'; exit; fi;",
+                               aval(VAR_gtmProxyDirs)[idx],
+                               aval(VAR_gtmProxyDirs)[idx]);
+       }
+
        /* Build directory and run initgtm */
        cmdInitGtm = initCmd(aval(VAR_gtmProxyServers)[idx]);
        snprintf(newCommand(cmdInitGtm), MAXLINE,
+                        "%s"
                         "[ -f %s/gtm_proxy.pid ] && gtm_ctl -D %s -m immediate -Z gtm_proxy stop;"
                         "rm -rf %s;"
                         "mkdir -p %s;"
                         "PGXC_CTL_SILENT=1 initgtm -Z gtm_proxy -D %s",
+                        remoteDirCheck,
                         aval(VAR_gtmProxyDirs)[idx],
                         aval(VAR_gtmProxyDirs)[idx],
                         aval(VAR_gtmProxyDirs)[idx],