devstack/lib/nova_plugins/hypervisor-docker
2014-03-06 16:24:12 +00:00

133 lines
3.8 KiB
Plaintext

# lib/nova_plugins/docker
# Configure the Docker hypervisor
# Enable with:
#
# VIRT_DRIVER=docker
# Dependencies:
#
# - ``functions`` file
# - ``nova`` and ``glance`` configurations
# install_nova_hypervisor - install any external requirements
# configure_nova_hypervisor - make configuration changes, including those to other services
# start_nova_hypervisor - start any external services
# stop_nova_hypervisor - stop any external services
# cleanup_nova_hypervisor - remove transient data and cache
# Save trace setting
MY_XTRACE=$(set +o | grep xtrace)
set +o xtrace
# Defaults
# --------
# Set up default directories
DOCKER_DIR=$DEST/docker
DOCKER_UNIX_SOCKET=/var/run/docker.sock
DOCKER_PID_FILE=/var/run/docker.pid
DOCKER_REGISTRY_PORT=${DOCKER_REGISTRY_PORT:-5042}
DOCKER_IMAGE=${DOCKER_IMAGE:-cirros:latest}
DOCKER_IMAGE_NAME=$DEFAULT_IMAGE_NAME
DOCKER_REGISTRY_IMAGE=${DOCKER_REGISTRY_IMAGE:-registry:latest}
DOCKER_REGISTRY_IMAGE_NAME=registry
DOCKER_REPOSITORY_NAME=${SERVICE_HOST}:${DOCKER_REGISTRY_PORT}/${DOCKER_IMAGE_NAME}
DOCKER_APT_REPO=${DOCKER_APT_REPO:-https://get.docker.io/ubuntu}
# Entry Points
# ------------
# clean_nova_hypervisor - Clean up an installation
function cleanup_nova_hypervisor {
stop_service docker
# Clean out work area
sudo rm -rf /var/lib/docker
}
# configure_nova_hypervisor - Set config files, create data dirs, etc
function configure_nova_hypervisor {
iniset $NOVA_CONF DEFAULT compute_driver docker.DockerDriver
iniset $GLANCE_API_CONF DEFAULT container_formats ami,ari,aki,bare,ovf,docker
}
# is_docker_running - Return 0 (true) if Docker is running, otherwise 1
function is_docker_running {
local docker_pid
if [ -f "$DOCKER_PID_FILE" ]; then
docker_pid=$(cat "$DOCKER_PID_FILE")
fi
if [[ -z "$docker_pid" ]] || ! ps -p "$docker_pid" | grep [d]ocker; then
return 1
fi
return 0
}
# install_nova_hypervisor() - Install external components
function install_nova_hypervisor {
# So far this is Ubuntu only
if ! is_ubuntu; then
die $LINENO "Docker is only supported on Ubuntu at this time"
fi
# Make sure Docker is installed
if ! is_package_installed lxc-docker; then
die $LINENO "Docker is not installed. Please run tools/docker/install_docker.sh"
fi
if ! (is_docker_running); then
die $LINENO "Docker not running"
fi
}
# start_nova_hypervisor - Start any required external services
function start_nova_hypervisor {
if ! (is_docker_running); then
die $LINENO "Docker not running"
fi
# Start the Docker registry container
docker run -d -p ${DOCKER_REGISTRY_PORT}:5000 \
-e SETTINGS_FLAVOR=openstack -e OS_USERNAME=${OS_USERNAME} \
-e OS_PASSWORD=${OS_PASSWORD} -e OS_TENANT_NAME=${OS_TENANT_NAME} \
-e OS_GLANCE_URL="${SERVICE_PROTOCOL}://${GLANCE_HOSTPORT}" \
-e OS_AUTH_URL=${OS_AUTH_URL} \
$DOCKER_REGISTRY_IMAGE_NAME ./docker-registry/run.sh
echo "Waiting for docker registry to start..."
DOCKER_REGISTRY=${SERVICE_HOST}:${DOCKER_REGISTRY_PORT}
if ! timeout $SERVICE_TIMEOUT sh -c "while ! curl -s $DOCKER_REGISTRY; do sleep 1; done"; then
die $LINENO "docker-registry did not start"
fi
# Tag image if not already tagged
if ! docker images | grep $DOCKER_REPOSITORY_NAME; then
docker tag $DOCKER_IMAGE_NAME $DOCKER_REPOSITORY_NAME
fi
# Make sure we copied the image in Glance
if ! (glance image-show "$DOCKER_IMAGE"); then
docker push $DOCKER_REPOSITORY_NAME
fi
}
# stop_nova_hypervisor - Stop any external services
function stop_nova_hypervisor {
# Stop the docker registry container
docker kill $(docker ps | grep docker-registry | cut -d' ' -f1)
}
# Restore xtrace
$MY_XTRACE
# Local variables:
# mode: shell-script
# End: