/* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved. 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "atrt.hpp" #include #include static bool create_directory(const char * path); bool setup_directories(atrt_config& config, int setup) { /** * 0 = validate * 1 = setup * 2 = setup+clean */ for (unsigned i = 0; i < config.m_clusters.size(); i++) { atrt_cluster& cluster = *config.m_clusters[i]; for (unsigned j = 0; j %s/mysql_install_db.log 2>&1", g_mysql_install_db_bin_path, g_basedir, g_prefix, val, proc.m_proc.m_cwd.c_str()); to_fwd_slashes(tmp); if (sh(tmp.c_str()) != 0) { g_logger.error("Failed to mysql_install_db for %s, cmd: '%s'", proc.m_proc.m_cwd.c_str(), tmp.c_str()); } else { g_logger.info("mysql_install_db for %s", proc.m_proc.m_cwd.c_str()); } } #else { g_logger.info("not running mysql_install_db for %s", proc.m_proc.m_cwd.c_str()); } #endif } } } FILE * out = NULL; bool retval = true; if (config.m_generated == false) { g_logger.info("Nothing configured..."); } else { out = fopen(mycnf.c_str(), "a+"); if (out == 0) { g_logger.error("Failed to open %s for append", mycnf.c_str()); return false; } time_t now = time(0); fprintf(out, "#\n# Generated by atrt\n"); fprintf(out, "# %s\n", ctime(&now)); } for (unsigned i = 0; i < config.m_clusters.size(); i++) { atrt_cluster& cluster = *config.m_clusters[i]; if (out) { Properties::Iterator it(&cluster.m_options.m_generated); printfile(out, cluster.m_options.m_generated, "[mysql_cluster%s]", cluster.m_name.c_str()); } for (unsigned j = 0; jm_name.c_str()); break; case atrt_process::AP_NDBD: printfile(out, proc.m_options.m_generated, "[cluster_config.ndbd.%d%s]", proc.m_index, proc.m_cluster->m_name.c_str()); break; case atrt_process::AP_MYSQLD: printfile(out, proc.m_options.m_generated, "[mysqld.%d%s]", proc.m_index, proc.m_cluster->m_name.c_str()); break; case atrt_process::AP_NDB_API: break; case atrt_process::AP_CLIENT: printfile(out, proc.m_options.m_generated, "[client.%d%s]", proc.m_index, proc.m_cluster->m_name.c_str()); break; case atrt_process::AP_ALL: case atrt_process::AP_CLUSTER: abort(); } } /** * Create env.sh */ BaseString tmp; tmp.assfmt("%s/env.sh", proc.m_proc.m_cwd.c_str()); to_native(tmp); char **env = BaseString::argify(0, proc.m_proc.m_env.c_str()); if (env[0] || proc.m_proc.m_path.length()) { Vector keys; FILE *fenv = fopen(tmp.c_str(), "w+"); if (fenv == 0) { g_logger.error("Failed to open %s for writing", tmp.c_str()); retval = false; goto end; } for (size_t k = 0; env[k]; k++) { tmp = env[k]; ssize_t pos = tmp.indexOf('='); require(pos > 0); env[k][pos] = 0; fprintf(fenv, "%s=\"%s\"\n", env[k], env[k]+pos+1); keys.push_back(env[k]); free(env[k]); } if (proc.m_proc.m_path.length()) { fprintf(fenv, "CMD=\"%s", proc.m_proc.m_path.c_str()); if (proc.m_proc.m_args.length()) { fprintf(fenv, " %s", proc.m_proc.m_args.c_str()); } fprintf(fenv, "\"\nexport CMD\n"); } fprintf(fenv, "PATH="); for (int i = 0; g_search_path[i] != 0; i++) { fprintf(fenv, "%s/%s:", g_prefix, g_search_path[i]); } fprintf(fenv, "$PATH\n"); keys.push_back("PATH"); { /** * In 5.5...binaries aren't compiled with rpath * So we need an explicit LD_LIBRARY_PATH * * Use path from libmysqlclient.so */ char * dir = dirname(g_libmysqlclient_so_path); #if defined(__MACH__) fprintf(fenv, "DYLD_LIBRARY_PATH=%s:$DYLD_LIBRARY_PATH\n", dir); keys.push_back("DYLD_LIBRARY_PATH"); #else fprintf(fenv, "LD_LIBRARY_PATH=%s:$LD_LIBRARY_PATH\n", dir); keys.push_back("LD_LIBRARY_PATH"); #endif free(dir); } for (unsigned k = 0; k list; if (tmp.split(list, "/") == 0) { g_logger.error("Failed to create directory: %s", tmp.c_str()); return false; } BaseString cwd = IF_WIN("","/"); for (unsigned i = 0; i < list.size(); i++) { cwd.append(list[i].c_str()); cwd.append("/"); NdbDir::create(cwd.c_str(), NdbDir::u_rwx() | NdbDir::g_r() | NdbDir::g_x(), true); } struct stat sbuf; if (lstat(native.c_str(), &sbuf) != 0 || !S_ISDIR(sbuf.st_mode)) { g_logger.error("Failed to create directory: %s (%s)", native.c_str(), cwd.c_str()); return false; } return true; } bool remove_dir(const char * path, bool inclusive) { if (access(path, 0)) return true; const int max_retries = 20; int attempt = 0; while(true) { if (NdbDir::remove_recursive(path, !inclusive)) return true; attempt++; if (attempt > max_retries) { g_logger.error("Failed to remove directory '%s'!", path); return false; } g_logger.warning(" - attempt %d to remove directory '%s' failed " ", retrying...", attempt, path); NdbSleep_MilliSleep(100); } abort(); // Never reached return false; }