+class Ready:
+ """
+ A utility that does exit(0) iff network as perceived
+ from the sliver is ready. Designed to be run before Server,
+ so one can wait for the right conditions.
+ """
+ def main(self):
+ parser = OptionParser()
+ # by default use another port so we don't run into
+ # the SO_LINGER kind of trouble
+ parser.add_option("-p", "--port", action="store", dest="port", type="int",
+ default=9999, help="port number")
+ parser.add_option("-a", "--address", action="store", dest="address",
+ default=socket.gethostname(), help="address")
+ (options, args) = parser.parse_args()
+
+ myprint("==================== tcptest.py ready", id='ready')
+ def can_bind ():
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ try:
+ s.bind((options.address, options.port))
+ return True
+ except Exception as e:
+ print(e)
+ return False
+
+ def eth0_has_ipv4():
+ command = "ip address show eth0 | grep -q ' inet '"
+ return subprocess.check_call(command, shell=True) == 0
+
+ sys.exit(0 if can_bind() and eth0_has_ipv4() else 1)
+