Browse code

Use cat instead of read<file

When reading a file, it is expected that
the read process will exit 1 as this is
expected whenever the input reaches an EOF.

Because it is not clear if the 'exit 1' is
from a successful read or a more serious error,
and as this edge-case of 'read' is not well-known,
we instead change this code to read the file using
'cat'.

The new code is moved into a function, is_docker_running.

Furthermore, we now quote the variables and check
for the existance of the pid file for better and safer
error handling.

Change-Id: Idb56b87349a5a84d5d255715cfb7191341363118
Closes-Bug: 1286441

Eric Windisch authored on 2014/03/01 14:17:32
Showing 1 changed files
... ...
@@ -57,6 +57,18 @@ function configure_nova_hypervisor {
57 57
     iniset $GLANCE_API_CONF DEFAULT container_formats ami,ari,aki,bare,ovf,docker
58 58
 }
59 59
 
60
+# is_docker_running - Return 0 (true) if Docker is running, otherwise 1
61
+function is_docker_running {
62
+    local docker_pid
63
+    if [ -f "$DOCKER_PID_FILE" ]; then
64
+        docker_pid=$(cat "$DOCKER_PID_FILE")
65
+    fi
66
+    if [[ -z "$docker_pid" ]] || ! ps -p "$docker_pid" | grep [d]ocker; then
67
+        return 1
68
+    fi
69
+    return 0
70
+}
71
+
60 72
 # install_nova_hypervisor() - Install external components
61 73
 function install_nova_hypervisor {
62 74
     # So far this is Ubuntu only
... ...
@@ -69,19 +81,15 @@ function install_nova_hypervisor {
69 69
         die $LINENO "Docker is not installed.  Please run tools/docker/install_docker.sh"
70 70
     fi
71 71
 
72
-    local docker_pid
73
-    read docker_pid <$DOCKER_PID_FILE
74
-    if [[ -z $docker_pid ]] || ! ps -p $docker_pid | grep [d]ocker; then
72
+    if ! (is_docker_running); then
75 73
         die $LINENO "Docker not running"
76 74
     fi
77 75
 }
78 76
 
79 77
 # start_nova_hypervisor - Start any required external services
80 78
 function start_nova_hypervisor {
81
-    local docker_pid
82
-    read docker_pid <$DOCKER_PID_FILE
83
-    if [[ -z $docker_pid ]] || ! ps -p $docker_pid | grep [d]ocker; then
84
-        die $LINENO "Docker not running, start the daemon"
79
+    if ! (is_docker_running); then
80
+        die $LINENO "Docker not running"
85 81
     fi
86 82
 
87 83
     # Start the Docker registry container