Skip to content
This repository has been archived by the owner on Dec 10, 2017. It is now read-only.

Commit

Permalink
Add --named-screen option
Browse files Browse the repository at this point in the history
  • Loading branch information
spajus committed Nov 10, 2014
1 parent 02b0ccf commit bb1a2ca
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions herd/herd.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def run(local_file, remote_file, hosts):

def transfer(host, local_file, remote_target, retry=0):
rp = opts['remote_path']
named_screen = opts['named_screen']
file_name = os.path.basename(local_file)
remote_file = '%s/%s' % (rp, file_name)
if ssh(host, 'test -d %s/BitTornado' % rp) != 0:
Expand All @@ -151,13 +152,17 @@ def transfer(host, local_file, remote_target, retry=0):
rp,
remote_file,
remote_target)
if named_screen:
command = 'screen -S %s -- %s' % (named_screen, command)
log.info("running \"%s\" on %s", command, host)
result = ssh(host, command)
if result == 0:
cmd = 'python %s/herd.py %s %s --seed True' % (
rp,
remote_file,
remote_target)
if named_screen:
cmd = 'screen -S %s -- %s' % (named_screen, cmd)
s = threading.Thread(target=ssh, args=(host, cmd,))
s.daemon = True
s.start()
Expand All @@ -166,7 +171,7 @@ def transfer(host, local_file, remote_target, retry=0):
while retry != 0:
retry = retry - 1
log.info("retrying on %s" % host)
transfer(host, local_file, remote_target, 0)
transfer(host, local_file, remote_target, 0, named_screen)
return host


Expand Down Expand Up @@ -201,6 +206,11 @@ def mktorrent(file_name, tracker):
return torrent_file[1]


def screen_exists():
return_code = subprocess.call(['which', 'screen'])
return_code == 0


def track():
bttrack.track(["--dfile", opts['data_file'], "--port", opts['port']])

Expand Down Expand Up @@ -239,13 +249,17 @@ def herdmain():

def run_with_opts(local_file, remote_file, hosts='', retry=0, port=8998,
remote_path='/tmp/herd', data_file='./data',
log_dir='/tmp/herd', hostlist=False):
log_dir='/tmp/herd', hostlist=False, named_screen=''):
"""Can include herd into existing python easier."""
global opts
opts['local-file'] = local_file
opts['remote-file'] = remote_file
opts['hosts'] = hosts
opts['retry'] = retry
if screen_exists:
opts['named_screen'] = named_screen
else:
opts['named_screen'] = False
opts['port'] = get_random_open_port(port)
opts['remote_path'] = remote_path
opts['data_file'] = data_file
Expand Down Expand Up @@ -274,6 +288,10 @@ def entry_point():
help="Number of times to retry in case of failure. " +
"Use -1 to make it retry forever (not recommended)")

parser.add_argument('--named-screen',
default='',
help="If value is given, runs murder inside named screen for easy cleanup")

parser.add_argument('--port',
default=8998,
help="Port number to run the tracker on. Port range " +
Expand Down Expand Up @@ -301,6 +319,9 @@ def entry_point():

opts = vars(parser.parse_args())

if not screen_exists():
opts['named_screen'] = False

# potentially select a random port
opts['port'] = get_random_open_port(opts['port'])

Expand Down

0 comments on commit bb1a2ca

Please sign in to comment.