Skip to content

Instantly share code, notes, and snippets.

@digambar15
Created December 12, 2014 12:25
Show Gist options
  • Save digambar15/8ed6557ccfbf2b96a69b to your computer and use it in GitHub Desktop.
Save digambar15/8ed6557ccfbf2b96a69b to your computer and use it in GitHub Desktop.
# 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.
import docker
from oslo.config import cfg
from docker import tls
CONF = cfg.CONF
docker_opts = [
cfg.StrOpt('host_url',
default='unix:///var/run/docker.sock',
help='tcp://host:port to bind/connect to or '
'unix://path/to/socket to use'),
cfg.BoolOpt('api_insecure',
default=False,
help='If set, ignore any SSL validation issues'),
cfg.StrOpt('ca_file',
help='Location of CA certificates file for '
'securing docker api requests (tlscacert).'),
cfg.StrOpt('cert_file',
help='Location of TLS certificate file for '
'securing docker api requests (tlscert).'),
cfg.StrOpt('key_file',
help='Location of TLS private key file for '
'securing docker api requests (tlskey).'),
cfg.StrOpt('vif_driver',
default='novadocker.virt.docker.vifs.DockerGenericVIFDriver'),
cfg.StrOpt('snapshots_directory',
default='$instances_path/snapshots',
help='Location where docker driver will temporarily store '
'snapshots.'),
cfg.BoolOpt('inject_key',
default=False,
help='Inject the ssh public key at boot time'),
]
CONF.register_opts(docker_opts, 'docker')
class DockerClient(object):
def __init__(self, url='unix://var/run/docker.sock'):
tls_config=None
client_cert = None
if (CONF.docker.cert_file or
CONF.docker.key_file):
client_cert = (CONF.docker.cert_file, CONF.docker.key_file)
else:
client_cert = None
if (CONF.docker.ca_file or
CONF.docker.api_insecure or
client_cert):
tls_config = tls.TLSConfig(
client_cert=client_cert,
ca_cert=CONF.docker.ca_file,
verify=CONF.docker.api_insecure)
else:
ssl_config = False
self.version='1.13'
self.timeout=10
self.client = client.Client(base_url=url, tls=tls_config)\
or Client(base_url=url, tls=tls_config)
def _encode_utf8(self, value):
return unicode(value).encode('utf-8')
def _create_container(self, image_name, command):
inspect_image= self.client.inspect_image(iself,_encode_utf8(image_name))
conn = self.client.create_container(image_name, command)
self._start_container(conn)
def _start_container(self, container_id):
self.client.start_container(container_obj)
if __name__ == "__main__":
dc = DockerClient(url='tcp://127.0.0.1:2375')
import pdb;pdb.set_trace()
dc._create_container(image_name='busybox:latest', command="/bin/sleep 30")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment