Script ganeti_listrunner
[hide private]
[frames] | no frames]

Script ganeti_listrunner

Run an executable on a list of hosts.

Script to serially run an executable on a list of hosts via ssh with password auth as root. If the provided log dir does not yet exist, it will try to create it.

Implementation:

Security considerations:

Functions [hide private]
 
LogDirUseable(logdir)
Ensure log file directory is available and usable.
 
GetTimeStamp(timestamp=None)
Return ISO8601 timestamp.
 
PingByTcp(target, port, timeout=10, live_port_needed=False, source=None)
Simple ping implementation using TCP connect(2).
 
GetHosts(hostsfile)
Return list of hosts from hostfile.
 
WriteLog(message, logfile)
Writes message, terminated by newline, to logfile.
 
GetAgentKeys()
Tries to get a list of ssh keys from an agent.
 
SetupSshConnection(host, username, password, use_agent, logfile)
Setup the ssh connection used for all later steps.
 
UploadFiles(connection, executable, filelist, logfile)
Uploads the specified files via sftp.
 
CleanupRemoteDir(connection, upload_dir, filelist, logfile)
Cleanes out and removes the remote work directory.
 
RunRemoteCommand(connection, command, logfile)
Execute the command via ssh on the remote host.
 
HostWorker(logdir, username, password, use_agent, hostname, executable, exec_args, command, filelist)
Per-host worker.
 
LaunchWorker(child_pids, logdir, username, password, use_agent, hostname, executable, exec_args, command, filelist)
Launch the per-host worker.
 
ParseOptions()
Parses the command line options.
 
main()
main.
Variables [hide private]
  REMOTE_PATH_BASE = "/tmp/listrunner"
  USAGE = "%prog -l logdir {-c command | -x /path/to/file} [-b b...

Imports: errno, optparse, getpass, logging, os, random, select, socket, sys, time, traceback, paramiko


Function Details [hide private]

GetTimeStamp(timestamp=None)

 

Return ISO8601 timestamp.

Returns ISO8601 timestamp, optionally expects a time.localtime() tuple in timestamp, but will use the current time if this argument is not supplied.

PingByTcp(target, port, timeout=10, live_port_needed=False, source=None)

 

Simple ping implementation using TCP connect(2).

Try to do a TCP connect(2) from an optional source IP to the specified target IP and the specified target port. If the optional parameter live_port_needed is set to true, requires the remote end to accept the connection. The timeout is specified in seconds and defaults to 10 seconds. If the source optional argument is not passed, the source address selection is left to the kernel, otherwise we try to connect using the passed address (failures to bind other than EADDRNOTAVAIL will be ignored).

GetHosts(hostsfile)

 

Return list of hosts from hostfile.

Reads the hostslist file and returns a list of hosts. Expects the hostslist file to contain one hostname per line.

SetupSshConnection(host, username, password, use_agent, logfile)

 

Setup the ssh connection used for all later steps.

This function sets up the ssh connection that will be used both for upload and remote command execution.

On success, it will return paramiko.Transport object with an already logged in session. On failure, False will be returned.

UploadFiles(connection, executable, filelist, logfile)

 

Uploads the specified files via sftp.

Uploads the specified files to a random, freshly created directory with a temporary name under /tmp. All uploaded files are chmod 0400 after upload with the exception of executable, with is chmod 500.

Upon success, returns the absolute path to the remote upload directory, but will return False upon failure.

HostWorker(logdir, username, password, use_agent, hostname, executable, exec_args, command, filelist)

 

Per-host worker.

This function does not return - it's the main code of the childs, which exit at the end of this function. The exit code 0 or 1 will be interpreted by the parent.

Parameters:
  • logdir - the directory where the logfiles must be created
  • username - SSH username
  • password - SSH password
  • use_agent - whether we should instead use an agent
  • hostname - the hostname to connect to
  • executable - the executable to upload, if not None
  • exec_args - Additional arguments for executable
  • command - the command to run
  • filelist - auxiliary files to upload

LaunchWorker(child_pids, logdir, username, password, use_agent, hostname, executable, exec_args, command, filelist)

 

Launch the per-host worker.

Arguments are the same as for HostWorker, except for child_pids, which is a dictionary holding the pid-to-hostname mapping.

ParseOptions()

 

Parses the command line options.

In case of command line errors, it will show the usage and exit the program.

Returns:
the options in a tuple

Variables Details [hide private]

USAGE

Value:
"%prog -l logdir {-c command | -x /path/to/file} [-b batch_size]" " {-\
f hostfile|-h hosts} [-u username]" " [-p password_file | -A]"