19 #include <portlib/NdbDir.hpp>
20 #include <portlib/NdbSleep.h>
22 static bool create_directory(
const char * path);
32 for (
size_t i = 0;
i < config.m_clusters.size();
i++)
35 for (
size_t j = 0; j<cluster.m_processes.size(); j++)
38 const char * dir = proc.m_proc.m_cwd.
c_str();
41 if (lstat(dir, &sbuf) == 0)
43 if (S_ISDIR(sbuf.st_mode))
53 g_logger.
error(
"Could not find directory: %s", dir);
56 g_logger.
error(
"%s is not a directory!", dir);
63 g_logger.
error(
"%s is not a directory!", dir);
72 g_logger.
error(
"Failed to remove %s!", dir);
78 else if (exists == -1)
82 g_logger.
error(
"Failed to remove %s!", dir);
90 if (!create_directory(dir))
102 printfile(FILE* out,
Properties& props,
const char * section, ...)
105 const char *
name = it.first();
109 va_start(ap, section);
110 vfprintf(out, section, ap);
114 for (;
name; name = it.next())
117 props.get(name, &val);
118 fprintf(out,
"%s %s\n", name + 2, val);
126 setup_files(
atrt_config& config,
int setup,
int sshx)
134 mycnf.
assfmt(
"%s/my.cnf", g_basedir);
136 if (!create_directory(g_basedir))
141 if (mycnf != g_my_cnf)
144 int ret = lstat(to_native(mycnf).c_str(), &sbuf);
148 if (unlink(to_native(mycnf).c_str()) != 0)
150 g_logger.
error(
"Failed to remove %s", mycnf.
c_str());
158 if (sh(cp.
c_str()) != 0)
165 if (setup == 2 || config.m_generated)
170 for (
size_t i = 0;
i < config.m_clusters.size();
i++)
173 for (
size_t j = 0; j<cluster.m_processes.size(); j++)
176 if (proc.m_type == atrt_process::AP_MYSQLD)
180 require(proc.m_options.m_loaded.get(
"--datadir=", &val));
182 tmp.assfmt(
"%s/bin/mysql_install_db --defaults-file=%s/my.cnf --datadir=%s > %s/mysql_install_db.log 2>&1",
183 g_prefix, g_basedir, val, proc.m_proc.m_cwd.
c_str());
186 if (sh(tmp.c_str()) != 0)
188 g_logger.
error(
"Failed to mysql_install_db for %s, cmd: '%s'",
189 proc.m_proc.m_cwd.
c_str(),
194 g_logger.
info(
"mysql_install_db for %s",
195 proc.m_proc.m_cwd.
c_str());
200 g_logger.
info(
"not running mysql_install_db for %s",
201 proc.m_proc.m_cwd.
c_str());
210 if (config.m_generated ==
false)
212 g_logger.
info(
"Nothing configured...");
216 out = fopen(mycnf.
c_str(),
"a+");
219 g_logger.
error(
"Failed to open %s for append", mycnf.
c_str());
222 time_t now = time(0);
223 fprintf(out,
"#\n# Generated by atrt\n");
224 fprintf(out,
"# %s\n", ctime(&now));
227 for (
size_t i = 0;
i < config.m_clusters.size();
i++)
233 printfile(out, cluster.m_options.m_generated,
234 "[mysql_cluster%s]", cluster.m_name.
c_str());
237 for (
size_t j = 0; j<cluster.m_processes.size(); j++)
244 case atrt_process::AP_NDB_MGMD:
245 printfile(out, proc.m_options.m_generated,
246 "[cluster_config.ndb_mgmd.%d%s]",
247 proc.m_index, proc.m_cluster->m_name.
c_str());
249 case atrt_process::AP_NDBD:
250 printfile(out, proc.m_options.m_generated,
251 "[cluster_config.ndbd.%d%s]",
252 proc.m_index, proc.m_cluster->m_name.
c_str());
254 case atrt_process::AP_MYSQLD:
255 printfile(out, proc.m_options.m_generated,
257 proc.m_index, proc.m_cluster->m_name.
c_str());
259 case atrt_process::AP_NDB_API:
261 case atrt_process::AP_CLIENT:
262 printfile(out, proc.m_options.m_generated,
264 proc.m_index, proc.m_cluster->m_name.
c_str());
266 case atrt_process::AP_ALL:
267 case atrt_process::AP_CLUSTER:
276 tmp.
assfmt(
"%s/env.sh", proc.m_proc.m_cwd.
c_str());
279 if (env[0] || proc.m_proc.m_path.
length())
282 FILE *fenv = fopen(tmp.
c_str(),
"w+");
285 g_logger.
error(
"Failed to open %s for writing", tmp.
c_str());
289 for (
size_t k = 0; env[k]; k++)
292 ssize_t pos = tmp.
indexOf(
'=');
295 fprintf(fenv,
"%s=\"%s\"\n", env[k], env[k]+pos+1);
296 keys.push_back(env[k]);
299 if (proc.m_proc.m_path.
length())
301 fprintf(fenv,
"CMD=\"%s", proc.m_proc.m_path.
c_str());
302 if (proc.m_proc.m_args.
length())
304 fprintf(fenv,
" %s", proc.m_proc.m_args.
c_str());
306 fprintf(fenv,
"\"\nexport CMD\n");
309 fprintf(fenv,
"PATH=%s/bin:%s/libexec:$PATH\n", g_prefix, g_prefix);
310 keys.push_back(
"PATH");
311 for (
size_t k = 0; k<keys.size(); k++)
312 fprintf(fenv,
"export %s\n", keys[k].c_str());
319 tmp.
assfmt(
"%s/ssh-login.sh", proc.m_proc.m_cwd.
c_str());
320 FILE* fenv = fopen(tmp.
c_str(),
"w+");
323 g_logger.
error(
"Failed to open %s for writing", tmp.
c_str());
327 fprintf(fenv,
"#!/bin/sh\n");
328 fprintf(fenv,
"cd %s\n", proc.m_proc.m_cwd.
c_str());
329 fprintf(fenv,
"[ -f /etc/profile ] && . /etc/profile\n");
330 fprintf(fenv,
". ./env.sh\n");
331 fprintf(fenv,
"ulimit -Sc unlimited\n");
332 fprintf(fenv,
"bash -i");
351 create_directory(
const char * path)
358 if (tmp.
split(list,
"/") == 0)
360 g_logger.
error(
"Failed to create directory: %s", tmp.
c_str());
365 for (
size_t i = 0;
i < list.size();
i++)
369 NdbDir::create(cwd.
c_str(),
370 NdbDir::u_rwx() | NdbDir::g_r() | NdbDir::g_x(),
375 if (lstat(native.c_str(), &sbuf) != 0 ||
376 !S_ISDIR(sbuf.st_mode))
378 g_logger.
error(
"Failed to create directory: %s (%s)",
388 remove_dir(
const char * path,
bool inclusive)
393 const int max_retries = 20;
398 if (NdbDir::remove_recursive(path, !inclusive))
402 if (attempt > max_retries)
404 g_logger.
error(
"Failed to remove directory '%s'!", path);
408 g_logger.
warning(
" - attempt %d to remove directory '%s' failed "
409 ", retrying...", attempt, path);
411 NdbSleep_MilliSleep(100);