def up(self): """Run service locally""" for service in self.services: # docker build -t gcr.io/query-router/client-config-store . # docker push gcr.io/query-router/client-config-store # gcloud run deploy client-config-store \ # --image gcr.io/query-router/client-config-store \ # --platform managed \ # --region europe-west1 logger.info(f"Building '{service}'...") client = AutoVersionClient() output = client.build(path=str( self._absolute_paths.service_dir(service)), tag=f'gcr.io/query-router/{service}', dockerfile=str(self._absolute_paths.root / 'services/Dockerfile')) for lines in output: for line in str(lines, 'utf-8').split('\n'): line = line.strip(" \r") if not line: continue log = json.loads(line) if stream := log.get('stream', None): logger.debug(stream.strip('\n ')) if error := log.get('errorDetail', None): logger.error(error['message'].strip('\n '))
def init_docker_client(timeout=2): """ Init docker-py client. """ if sys.platform.startswith('darwin') \ or sys.platform.startswith('win32'): # mac or win kwargs = kwargs_from_env() kwargs['tls'].assert_hostname = False kwargs['timeout'] = timeout client = AutoVersionClient(**kwargs) else: # unix-based client = AutoVersionClient(timeout=timeout, base_url='unix://var/run/docker.sock') return client
def __init__(self, config_file=None): if not config_file: self.config_file = '/etc/openshift_tools/container_metrics.yml' else: self.config_file = config_file self.config = None self.parse_config() self.cli = AutoVersionClient(base_url='unix://var/run/docker.sock', timeout=120) self.docker_util = DockerUtil(self.cli) self.zagg_sender = ZaggSender(verbose=True)
def get_client(timeout=None): """ Create and return docker-py client instance. :param timeout: int seconds :return: `AutoVersionClient` """ if sys.platform.startswith('darwin') \ or sys.platform.startswith('win32'): # mac or win kwargs = kwargs_from_env() # hack from here: # http://docker-py.readthedocs.org/en/latest/boot2docker/ # See also: https://github.com/docker/docker-py/issues/406 if 'tls' in kwargs: kwargs['tls'].assert_hostname = False kwargs['timeout'] = timeout return AutoVersionClient(**kwargs) else: # unix-based kwargs = kwargs_from_env(ssl_version=ssl.PROTOCOL_TLSv1, assert_hostname=False) kwargs['timeout'] = timeout return AutoVersionClient(**kwargs)
def main(): '''get docker and openshift versions and send to metric sender ''' args = parse_args() mts = MetricSender(verbose=args.verbose, debug=args.debug) # Get docker version cli = AutoVersionClient(base_url='unix://var/run/docker.sock', timeout=120) docker_version = cli.version()["Version"] mts.add_metric({"docker.version": docker_version}, key_tags={'miq_metric': 'true'}) # Get openshift version try: return_value = subprocess.check_output("oc version", stderr=subprocess.STDOUT, shell=True) oc_version = return_value.split('\n')[0].split(' ')[1] mts.add_metric({"oc.version": oc_version}, key_tags={'miq_metric': 'true'}) except subprocess.CalledProcessError as error: print ("Failed to get openshift version: ", error.output)
def main(): import _dimgx _dimgx._logexception = exitonraise(_dimgx._logexception) # WARNING: monkey patch; pylint: disable=protected-access args = buildparser().parse_args(sys_argv[1:]) logging_basicConfig(format=args.log_format) getLogger().setLevel(logging_getLevelName(args.log_level)) patch_broken_tarfile_29760() dc_kw = kwargs_from_env() # TODO: hack to work around github:docker/docker-py#706 if DOCKER_TLS_VERIFY == '0': dc_kw['tls'].assert_hostname = False dc = AutoVersionClient(**dc_kw) layers_dict = inspectlayers(dc, args.image) top_most_layer_id, selected_layers = selectlayers(args, layers_dict) if not selected_layers: _LOGGER.warning('no known layers selected') if args.target is None: printlayerinfo(args, selected_layers) else: extractlayers(dc, args, selected_layers, top_most_layer_id)
# Adding the ignore because it does not like the naming of the script # to be different than the class name # pylint: disable=invalid-name import time import os from docker import AutoVersionClient from docker.errors import APIError # Jenkins doesn't have our tools which results in import errors # pylint: disable=import-error from openshift_tools.monitoring.metric_sender import MetricSender ZBX_KEY = "docker.container.dns.resolution" if __name__ == "__main__": cli = AutoVersionClient(base_url='unix://var/run/docker.sock') container_id = os.environ['container_uuid'] container = cli.create_container( image=cli.inspect_container(container_id)['Image'], command='getent hosts redhat.com') cli.start(container=container.get('Id')) exit_code = cli.wait(container) for i in range(0, 3): try: cli.remove_container(container.get('Id')) break except APIError:
def __init__(self, timeout=None, clear_handler=None, refresh_handler=None, logger=None): """ Initialize the Docker wrapper. :param timeout: int :param clear_handler: callable :param refresh_handler: callable :param logger: logger """ assert callable(clear_handler) assert callable(refresh_handler) self.logger = logger self.exception = None self.handlers = { 'attach': (self.attach, 'Attach to a running container.'), 'build': (self.build, ("Build a new image from the source" " code")), 'clear': (clear_handler, "Clear the window."), 'create': (self.create, 'Create a new container.'), 'exec': (self.execute, ("Run a command in a running" " container.")), 'help': (self.help, "Help on available commands."), 'pause': (self.pause, "Pause all processes within a container."), 'ps': (self.containers, "List containers."), 'port': (self.port, ("List port mappings for the container, or " "lookup the public-facing port that is " "NAT-ed to the private_port.")), 'pull': (self.pull, ("Pull an image or a repository from the " "registry.")), 'push': (self.push, ("Push an image or a repository to the " "registry.")), 'images': (self.images, "List images."), 'info': (self.info, "Display system-wide information."), 'inspect': (self.inspect, "Return low-level information on a " + "container or image."), 'kill': (self.kill, "Kill one or more running containers"), 'login': (self.login, ("Register or log in to a Docker registry " "server (defaut " "\"https://index.docker.io/v1/\").")), 'logs': (self.logs, "Fetch the logs of a container."), 'refresh': (refresh_handler, "Refresh autocompletions."), 'rename': (self.rename, "Rename a container."), 'restart': (self.restart, "Restart a running container."), 'run': (self.run, "Run a command in a new container."), 'rm': (self.rm, "Remove one or more containers."), 'rmi': (self.rmi, "Remove one or more images."), 'search': (self.search, "Search the Docker Hub for images."), 'shell': (self.shell, "Get shell into a running container."), 'start': (self.start, "Restart a stopped container."), 'stop': (self.stop, "Stop a running container."), 'tag': (self.tag, "Tag an image into a repository."), 'top': (self.top, "Display the running processes of a container."), 'unpause': (self.unpause, ("Unpause all processes within a " "container.")), 'version': (self.version, "Show the Docker version information."), 'volume create': (self.volume_create, "Create a new volume."), 'volume inspect': (self.volume_inspect, "Inspect one or more " "volumes."), 'volume ls': (self.volume_ls, "List volumes."), 'volume rm': (self.volume_rm, "Remove a volume."), } self.output = None self.after = None self.command = None self.log = None self.is_refresh_containers = False self.is_refresh_running = False self.is_refresh_images = False self.is_refresh_volumes = False disable_warnings() if sys.platform.startswith('darwin') \ or sys.platform.startswith('win32'): try: # mac or win kwargs = kwargs_from_env() # hack from here: # http://docker-py.readthedocs.org/en/latest/boot2docker/ # See also: https://github.com/docker/docker-py/issues/406 if 'tls' in kwargs: kwargs['tls'].assert_hostname = False kwargs['timeout'] = timeout self.instance = AutoVersionClient(**kwargs) except DockerException as x: if 'CERTIFICATE_VERIFY_FAILED' in str(x): raise DockerSslException(x) elif 'ConnectTimeoutError' in str(x): raise DockerTimeoutException(x) else: raise x else: # unix-based kwargs = kwargs_from_env(ssl_version=ssl.PROTOCOL_TLSv1, assert_hostname=False) kwargs['timeout'] = timeout self.instance = AutoVersionClient(**kwargs)
try: start_timeout = int(os.environ.get('WEBDRIVER_WHARF_START_TIMEOUT', 60)) except (TypeError, ValueError): print 'WEBDRIVER_WHARF_START_TIMEOUT must be an integer, defaulting to 60' start_timeout = 60 # TODO: Making these configurable would be good # lowest port for webdriver binding _wd_port_start = 4900 # Offsets for finding the other ports _vnc_port_offset = 5900 - _wd_port_start _http_port_offset = 6900 - _wd_port_start # docker client is localhost only for now client = AutoVersionClient(timeout=120) logger = logging.getLogger(__name__) container_pool_size = 4 last_pulled_image_id = None @contextmanager def apierror_squasher(): try: yield except errors.APIError as ex: err_tpl = 'Docker APIError Caught: %s' if ex.explanation: logger.error(err_tpl, ex.explanation) else: logger.error(err_tpl, ex.args[0])
# import http.client # python 2.x import httplib PORT = os.getenv('PORT') JOB_NAME = os.getenv('JOB_NAME') DOCKER_ADDRESS = os.getenv('DOCKER_ADDRESS') REGISTRY_ADDRESS = os.getenv('REGISTRY_ADDRESS') WORKSPACE = os.getenv('WORKSPACE') DOCKERFILE = WORKSPACE + '/Dockerfile' IMAGE_NAME = JOB_NAME FULL_IMAGE_NAME = REGISTRY_ADDRESS + '/' + IMAGE_NAME # http://docker-py.readthedocs.org/en/latest/api/#containers jenkinsCli = AutoVersionClient(base_url='localhost:2375', timeout=10) swarmCli = AutoVersionClient(base_url=DOCKER_ADDRESS, timeout=10) # registryConn = http.client.HTTPConnection(REGISTRY_ADDRESS) registryConn = httplib.HTTPConnection(REGISTRY_ADDRESS) def remove_image_in_registry(): try: registryConn.request('GET', 'v1/repositories/' + IMAGE_NAME + '/tags/latest') response = registryConn.getresponse() print("query image %s in registry,http status %d" % (FULL_IMAGE_NAME, response.status)) # if image is in registry if response.status / 100 == 2: print("registry image %s id : %s" %