Skip to content

Commit

Permalink
Change upload_manifest.py to randomly select host upload port
Browse files Browse the repository at this point in the history
This patch enhances upload_manifest.py to select a random
upload port on host side instead of a fixed 10000.

Fixes #1017

Signed-off-by: Waldemar Kozaczuk <jwkozaczuk@gmail.com>
  • Loading branch information
wkozaczuk committed Dec 14, 2018
1 parent fbc22bd commit 6e58395
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions scripts/upload_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import optparse, os, subprocess, socket, threading, stat, sys
from manifest_common import add_var, expand, unsymlink, read_manifest, defines, strip_file
from contextlib import closing

try:
import StringIO
Expand All @@ -12,7 +13,13 @@
# This works on Python 3
StringIO = io.StringIO

def upload(osv, manifest, depends):
def find_free_port():
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(('localhost', 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return s.getsockname()[1]

def upload(osv, manifest, depends, port):
manifest = [(x, y % defines) for (x, y) in manifest]
files = list(expand(manifest))
files = [(x, unsymlink(y)) for (x, y) in files]
Expand All @@ -25,7 +32,7 @@ def upload(osv, manifest, depends):
os.write(sys.stdout.fileno(), line)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 10000))
s.connect(("127.0.0.1", port))

# We'll want to read the rest of the guest's output, so that it doesn't
# hang, and so the user can see what's happening. Easiest to do this with
Expand Down Expand Up @@ -146,9 +153,10 @@ def main():
depends.write('%s: \\\n' % (options.output,))

image_path = os.path.abspath(options.output)
osv = subprocess.Popen('cd ../..; scripts/run.py --vnc none -m 512 -c1 -i %s -u -s -e "--norandom --nomount --noinit /tools/mkfs.so; /tools/cpiod.so --prefix /zfs/zfs/; /zfs.so set compression=off osv" --forward tcp::10000-:10000' % image_path, shell=True, stdout=subprocess.PIPE)
upload_port = find_free_port()
osv = subprocess.Popen('cd ../..; scripts/run.py --vnc none -m 512 -c1 -i %s -u -s -e "--norandom --nomount --noinit /tools/mkfs.so; /tools/cpiod.so --prefix /zfs/zfs/; /zfs.so set compression=off osv" --forward tcp:127.0.0.1:%s-:10000' % (image_path,upload_port), shell=True, stdout=subprocess.PIPE)

upload(osv, manifest, depends)
upload(osv, manifest, depends, upload_port)

ret = osv.wait()
if ret != 0:
Expand Down

0 comments on commit 6e58395

Please sign in to comment.