Browse Source

Add docker installation script

There are several projects that tried to install docker in different
ways. This potentially lead to failure of more than one such services
were enabled. This commit consolidate docker installation into
a devstack plugin so that other services could depend on it.

The initial script was mainly from Kuryr-libnetwork. Kuryr, Fuxi,
and Zun might leverage this script to install Docker in the future.

Change-Id: I97dbff2f361acc98b12ec6f40ab115c8548477a3
Hongbin Lu 2 years ago
parent
commit
69716eb42f
4 changed files with 169 additions and 0 deletions
  1. 20
    0
      README.rst
  2. 106
    0
      devstack/lib/docker
  3. 37
    0
      devstack/plugin.sh
  4. 6
    0
      devstack/settings

+ 20
- 0
README.rst View File

@@ -0,0 +1,20 @@
1
+=================
2
+ Container Plugin
3
+=================
4
+
5
+This plugin enables installation of container engine on Devstack. The default
6
+container engine is Docker.
7
+
8
+======================
9
+ Enabling in Devstack
10
+======================
11
+
12
+1. Download DevStack
13
+
14
+2. Add this repo as an external repository::
15
+
16
+     > cat local.conf
17
+     [[local|localrc]]
18
+     enable_plugin devstack-plugin-container https://github.com/openstack/devstack-plugin-container
19
+
20
+3. run ``stack.sh``

+ 106
- 0
devstack/lib/docker View File

@@ -0,0 +1,106 @@
1
+#!/bin/bash
2
+
3
+# Dependencies:
4
+#
5
+# - functions
6
+# - ``STACK_USER`` must be defined
7
+# - ``ETCD_PORT`` must be defined if on swarm mode
8
+
9
+# stack.sh
10
+# ---------
11
+# - install_docker
12
+
13
+# Save trace setting
14
+_XTRACE_DOCKER=$(set +o | grep xtrace)
15
+set +o xtrace
16
+
17
+
18
+# Defaults
19
+# --------
20
+
21
+DOCKER_ENGINE_SOCKET_FILE=${DOCKER_ENGINE_SOCKET_FILE:-/var/run/docker.sock}
22
+DOCKER_ENGINE_PORT=${DOCKER_ENGINE_PORT:-2375}
23
+SWARM_MODE=$(trueorfalse False SWARM_MODE)
24
+
25
+
26
+# Functions
27
+# ---------
28
+
29
+function check_docker {
30
+    if is_ubuntu; then
31
+       dpkg -s docker-engine > /dev/null 2>&1 || dpkg -s docker-ce > /dev/null 2>&1
32
+    else
33
+       rpm -q docker-engine > /dev/null 2>&1 || rpm -q docker > /dev/null 2>&1 || rpm -q docker-ce > /dev/null 2>&1
34
+    fi
35
+}
36
+
37
+function install_docker {
38
+    if [[ -z "$os_PACKAGE" ]]; then
39
+        GetOSVersion
40
+    fi
41
+
42
+    local lsb_dist=${os_VENDOR,,}
43
+    local dist_version=${os_CODENAME}
44
+    local arch=$(dpkg --print-architecture)
45
+    if is_ubuntu; then
46
+        apt_get install linux-image-extra-$(uname -r) linux-image-extra-virtual
47
+        curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
48
+        sudo add-apt-repository -y \
49
+            "deb [arch=${arch}] https://download.docker.com/linux/${lsb_dist} \
50
+            ${dist_version} \
51
+            stable"
52
+        REPOS_UPDATED=False apt_get_update
53
+        apt_get install docker-ce
54
+    elif is_fedora; then
55
+        if [[ "$lsb_dist" = "centos" ]]; then
56
+            sudo yum-config-manager \
57
+                --add-repo \
58
+                https://download.docker.com/linux/centos/docker-ce.repo
59
+        elif [[ "$lsb_dist" = "fedora" ]]; then
60
+            sudo dnf config-manager \
61
+                --add-repo \
62
+                https://download.docker.com/linux/fedora/docker-ce.repo
63
+        fi
64
+        yum_install docker-ce
65
+    fi
66
+}
67
+
68
+function configure_docker {
69
+    # After an ./unstack it will be stopped. So it is ok if it returns exit-code == 1
70
+    sudo systemctl stop docker.service || true
71
+
72
+    local cluster_store_opts=""
73
+    if [[ "$SWARM_MODE" = "True" ]]; then
74
+        cluster_store_opts+="\"cluster-store\": \"etcd://localhost:$ETCD_PORT\","
75
+    fi
76
+    local docker_config_file=/etc/docker/daemon.json
77
+    sudo mkdir -p $(dirname ${docker_config_file})
78
+    cat <<EOF | sudo tee $docker_config_file >/dev/null
79
+{
80
+    $cluster_store_opts
81
+    "group": "$STACK_USER",
82
+    "hosts": [
83
+        "unix://$DOCKER_ENGINE_SOCKET_FILE",
84
+        "tcp://0.0.0.0:$DOCKER_ENGINE_PORT"
85
+    ]
86
+}
87
+EOF
88
+    # NOTE(hongbin): We override ExecStart to workaround issue 22339.
89
+    # https://github.com/docker/docker/issues/22339
90
+    local docker_drop_in_file=/etc/systemd/system/docker.service.d/docker.conf
91
+    sudo mkdir -p $(dirname ${docker_drop_in_file})
92
+    cat <<EOF | sudo tee $docker_drop_in_file >/dev/null
93
+[Service]
94
+ExecStart=
95
+ExecStart=/usr/bin/dockerd --config-file=$docker_config_file
96
+EOF
97
+    sudo systemctl daemon-reload
98
+    sudo systemctl --no-block restart docker.service
99
+}
100
+
101
+function stop_docker {
102
+    sudo systemctl stop docker.service || true
103
+}
104
+
105
+# Restore xtrace
106
+$_XTRACE_DOCKER

+ 37
- 0
devstack/plugin.sh View File

@@ -0,0 +1,37 @@
1
+# container - Devstack extras script to install container engine
2
+
3
+# Save trace setting
4
+XTRACE=$(set +o | grep xtrace)
5
+set -o xtrace
6
+
7
+echo_summary "container's plugin.sh was called..."
8
+source $DEST/devstack-plugin-container/devstack/lib/docker
9
+(set -o posix; set)
10
+
11
+if is_service_enabled container; then
12
+    if [[ "$1" == "stack" && "$2" == "install" ]]; then
13
+        echo_summary "Installing container engine"
14
+        if [[ ${CONTAINER_ENGINE} == "docker" ]]; then
15
+            check_docker || install_docker
16
+        fi
17
+    elif [[ "$1" == "stack" && "$2" == "post-config" ]]; then
18
+        echo_summary "Configuring container engine"
19
+        if [[ ${CONTAINER_ENGINE} == "docker" ]]; then
20
+            configure_docker
21
+        fi
22
+    fi
23
+
24
+    if [[ "$1" == "unstack" ]]; then
25
+        if [[ ${CONTAINER_ENGINE} == "docker" ]]; then
26
+            stop_docker
27
+        fi
28
+    fi
29
+
30
+    if [[ "$1" == "clean" ]]; then
31
+        # nothing needed here
32
+        :
33
+    fi
34
+fi
35
+
36
+# Restore xtrace
37
+$XTRACE

+ 6
- 0
devstack/settings View File

@@ -0,0 +1,6 @@
1
+# Devstack settings
2
+
3
+CONTAINER_ENGINE=${CONTAINER_ENGINE:-docker}
4
+
5
+# Enable container services
6
+enable_service container

Loading…
Cancel
Save