Created
July 29, 2019 20:39
-
-
Save robbmanes/4e9c1955564a565b98200eba4020ed53 to your computer and use it in GitHub Desktop.
A python script that attempts to connect to a UNIX socket. This is useful when verifying if a container can reach a host socket, or has permissions issues with your current settings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import os | |
| import sys | |
| import time | |
| import logging | |
| import socket | |
| def check_unix_socket(): | |
| SOCKET_LOCATION = "/var/run/docker.sock" | |
| RETRY_TIMER = 10 | |
| logging.info("Beginning check_unix_socket") | |
| logger = logging.getLogger("check_unix_socket") | |
| logger.setLevel(logging.DEBUG) | |
| if os.getenv('UNIX_SOCKET_LOCATION') is not None: | |
| SOCKET_LOCATION = os.environ['UNIX_SOCKET_LOCATION'] | |
| if os.getenv('RETRY_TIMER') is not None: | |
| RETRY_TIMER = int(os.environ['RETRY_TIMER']) | |
| logger.info("Using UNIX socket %s. Now attempting to connect to socket..." % (SOCKET_LOCATION)) | |
| while True: | |
| try: | |
| sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
| sock.connect(SOCKET_LOCATION) | |
| logger.info("Successfully connected to UNIX socket %s. Closing socket and retrying in %d seconds." % (SOCKET_LOCATION, RETRY_TIMER)) | |
| sock.close() | |
| except Exception as e: | |
| logger.error("Failed to connect to socket %s with exception {%s}" % (SOCKET_LOCATION, e)) | |
| logger.error("Retrying in %d seconds..." % (RETRY_TIMER)) | |
| time.sleep(RETRY_TIMER) | |
| if __name__ == '__main__': | |
| sys.exit(check_unix_socket()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment