# Copyright (c) 2013 dotCloud, Inc.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
A Docker Hypervisor which allows running Linux Containers instead of VMs.
"""

import os
import random
import socket
import time

from oslo.config import cfg

from nova.compute import flavors
from nova.compute import power_state
from nova.compute import task_states
from nova import exception
from nova.image import glance
from nova.openstack.common.gettextutils import _
from nova.openstack.common import jsonutils
from nova.openstack.common import log
from nova.openstack.common import units
from nova import utils
import novadocker.virt.docker.client as docker_client
from novadocker.virt.docker import hostinfo
from novadocker.virt.docker import network
from novadocker.virt import driver


docker_opts = [
    cfg.IntOpt('registry_default_port',
               default=5042,
               help=_('Default TCP port to find the '
                      'docker-registry container'),
               deprecated_group='DEFAULT',
               deprecated_name='docker_registry_default_port'),
]

CONF = cfg.CONF
CONF.register_opts(docker_opts, 'docker')
CONF.import_opt('my_ip', 'nova.netconf')

LOG = log.getLogger(__name__)


class DockerDriver(driver.ComputeDriver):
    """Docker hypervisor driver."""

    def __init__(self, virtapi):
        super(DockerDriver, self).__init__(virtapi)
        self._docker = None

    @property
    def docker(self):
        if self._docker is None:
            self._docker = docker_client.DockerHTTPClient()
        return self._docker

    def init_host(self, host):
        LOG.warning(_('The docker driver does not meet the Nova project\'s '
                      'requirements for quality verification and is planned '
                      'for removal. This may change, but users should plan '
                      'accordingly. Additional details here: '
                      'https://wiki.openstack.org/wiki/HypervisorSupportMatrix'
                      '/DeprecationPlan'))
        if self._is_daemon_running() is False: