<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Management of dockers: overview of this module
==============================================

.. note::

    The DockerIO integration is still in beta, the api is subject to change

General notes
-------------

- As we use states, we don't want to pop continuously dockers, we will map each
  container id  (or image) with a grain whenever it is relevant.
- As a corollary, we will resolve for a container id either directly this
  container id or try to find a container id matching something stocked in
  grain

Installation prerequisites
--------------------------

- You will need the 'docker-py' python package in your python installation
  running salt.
- For now, you need docker-py from sources:

    https://github.com/dotcloud/docker-py

Prerequisite pillar configuration for authentication
----------------------------------------------------

- To push or pull you will need to be authenticated as the docker-py bindings
  require it
- For this to happen, you will need to configure a mapping in the pillar
  representing your per URL authentication bits::

    docker-registries:
        registry_url:
            email: <EMAIL>
            password: <PASSWORD>
            username: foo

- You need at least an entry to the default docker index::

    docker-registries:
        https://index.docker.io/v1:
            email: <EMAIL>
            password: <PASSWORD>
            username: foo

you can define multiple registries blocks for them to be aggregated, their id
just must finish with -docker-registries::

   ac-docker-registries:
        https://index.bar.io/v1:
            email: <EMAIL>
            password: <PASSWORD>
            username: foo

   ab-docker-registries:
        https://index.foo.io/v1:
            email: <EMAIL>
            password: <PASSWORD>
            username: foo

Would be the equivalent to::

   docker-registries:
        https://index.bar.io/v1:
            email: <EMAIL>
            password: <PASSWORD>
            username: foo
        https://index.foo.io/v1:
            email: <EMAIL>
            password: <PASSWORD>
            username: foo

Registry dialog methods
-----------------------

- login
- push
- pull

Docker management
-----------------

- version
- info

Image management
----------------

You have those methods:

- search
- inspect_image
- get_images
- remove_image
- import_image
- build
- tag

Container management
--------------------

You have those methods:

- start
- stop
- kill
- wait
- get_containers
- inspect_container
- remove_container
- is_running
- top
- ports
- logs
- diff
- commit
- create_container
- export
- get_container_root

Runtime execution within a specific already existing and running container
--------------------------------------------------------------------------

- Idea is to use lxc-attach to execute inside the container context.
- We do not use a "docker run command" but want to execute something inside a
  running container.


You have those methods:

- retcode
- run
- run_all
- run_stderr
- run_stdout
- script
- script_retcode

'''
__docformat__ = 'restructuredtext en'

import datetime
import json
import os
import re
import traceback
import shutil

from salt.modules import cmdmod
from salt.exceptions import CommandExecutionError
from salt._compat import string_types
import salt.utils
from salt.utils.odict import OrderedDict

try:
    import docker
    HAS_DOCKER = True
except ImportError:
    HAS_DOCKER = False

import logging

log = logging.getLogger(__name__)

INVALID_RESPONSE = 'We did not get any expectable answer from docker'
VALID_RESPONSE = ''
NOTSET = object()
base_status = {
    'status': None,
    'id': None,
    'comment': '',
    'out': None
}


def __virtual__():
    '''
    Only load if docker libs are present
    '''
    if HAS_DOCKER:
        return 'docker'
    return False


def _sizeof_fmt(num):
    '''
    Return disk format size data<commit_msg>`salt.modules.dockerio` is now using `__virtualname__`<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Management of dockers: overview of this module
==============================================

.. note::

    The DockerIO integration is still in beta, the api is subject to change

General notes
-------------

- As we use states, we don't want to pop continuously dockers, we will map each
  container id  (or image) with a grain whenever it is relevant.
- As a corollary, we will resolve for a container id either directly this
  container id or try to find a container id matching something stocked in
  grain

Installation prerequisites
--------------------------

- You will need the 'docker-py' python package in your python installation
  running salt.
- For now, you need docker-py from sources:

    https://github.com/dotcloud/docker-py

Prerequisite pillar configuration for authentication
----------------------------------------------------

- To push or pull you will need to be authenticated as the docker-py bindings
  require it
- For this to happen, you will need to configure a mapping in the pillar
  representing your per URL authentication bits::

    docker-registries:
        registry_url:
            email: <EMAIL>
            password: <PASSWORD>
            username: foo

- You need at least an entry to the default docker index::

    docker-registries:
        https://index.docker.io/v1:
            email: <EMAIL>
            password: <PASSWORD>
            username: foo

you can define multiple registries blocks for them to be aggregated, their id
just must finish with -docker-registries::

   ac-docker-registries:
        https://index.bar.io/v1:
            email: <EMAIL>
            password: <PASSWORD>
            username: foo

   ab-docker-registries:
        https://index.foo.io/v1:
            email: <EMAIL>
            password: <PASSWORD>
            username: foo

Would be the equivalent to::

   docker-registries:
        https://index.bar.io/v1:
            email: <EMAIL>
            password: <PASSWORD>
            username: foo
        https://index.foo.io/v1:
            email: <EMAIL>
            password: <PASSWORD>
            username: foo

Registry dialog methods
-----------------------

- login
- push
- pull

Docker management
-----------------

- version
- info

Image management
----------------

You have those methods:

- search
- inspect_image
- get_images
- remove_image
- import_image
- build
- tag

Container management
--------------------

You have those methods:

- start
- stop
- kill
- wait
- get_containers
- inspect_container
- remove_container
- is_running
- top
- ports
- logs
- diff
- commit
- create_container
- export
- get_container_root

Runtime execution within a specific already existing and running container
--------------------------------------------------------------------------

- Idea is to use lxc-attach to execute inside the container context.
- We do not use a "docker run command" but want to execute something inside a
  running container.


You have those methods:

- retcode
- run
- run_all
- run_stderr
- run_stdout
- script
- script_retcode

'''
__docformat__ = 'restructuredtext en'

import datetime
import json
import os
import re
import traceback
import shutil

from salt.modules import cmdmod
from salt.exceptions import CommandExecutionError
from salt._compat import string_types
import salt.utils
from salt.utils.odict import OrderedDict

try:
    import docker
    HAS_DOCKER = True
except ImportError:
    HAS_DOCKER = False

import logging

log = logging.getLogger(__name__)

INVALID_RESPONSE = 'We did not get any expectable answer from docker'
VALID_RESPONSE = ''
NOTSET = object()
base_status = {
    'status': None,
    'id': None,
    'comment': '',
    'out': None
}

# Define the module's virtual name
__virtualname__ = 'docker'


def __virtual__():
    '''
    Only load if docker libs are present
    '''
    if HAS_DOCKER:
        return __virtualname__
    return False


def _sizeof_fmt(num):
    '''
    Return disk format size data
{'fraction_non_alphanumeric': 0.13892675672326188, 'fraction_numerical': 0.0014871731317387532, 'mean_word_length': 2.2267093162734906, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '85352', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2162, 'n_words': 826}