Browse code

Add info.sh

Change-Id: I4394482df2db4d4b251d97678d2692a2849715a1

Dean Troyer authored on 2011/12/31 08:43:20
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,213 @@
0
+#!/usr/bin/env bash
1
+# info.sh - Produce a report on the state of devstack installs
2
+#
3
+# Output fields are separated with '|' chars
4
+# Output types are git,localrc,os,pip,pkg:
5
+#   git|<project>|<branch>[<shaq>]
6
+#   localtc|<var>=<value>
7
+#   os|<var>=<value>
8
+#   pip|<package>|<version>
9
+#   pkg|<package>|<version>
10
+
11
+function usage {
12
+    echo "$0 - Report on the devstack configuration"
13
+    echo ""
14
+    echo "Usage: $0"
15
+    exit 1
16
+}
17
+
18
+if [ "$1" = "-h" ]; then
19
+    usage
20
+fi
21
+
22
+# Keep track of the current directory
23
+TOOLS_DIR=$(cd $(dirname "$0") && pwd)
24
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
25
+cd $TOP_DIR
26
+
27
+# Source params
28
+source $TOP_DIR/stackrc
29
+
30
+DEST=${DEST:-/opt/stack}
31
+FILES=$TOP_DIR/files
32
+if [[ ! -d $FILES ]]; then
33
+    echo "ERROR: missing devstack/files - did you grab more than just stack.sh?"
34
+    exit 1
35
+fi
36
+
37
+# Repos
38
+# -----
39
+
40
+# git_report <dir>
41
+function git_report() {
42
+    local dir=$1
43
+    local proj ref branch head
44
+    if [[ -d $dir/.git ]]; then
45
+        pushd $dir >/dev/null
46
+        proj=$(basename $dir)
47
+        ref=$(git symbolic-ref HEAD)
48
+        branch=${ref##refs/heads/}
49
+        head=$(git show-branch --sha1-name $branch | cut -d' ' -f1)
50
+        echo "git|${proj}|${branch}${head}"
51
+        popd >/dev/null
52
+    fi
53
+}
54
+
55
+for i in $DEST/*; do
56
+    if [[ -d $i ]]; then
57
+        git_report $i
58
+    fi
59
+done
60
+
61
+# OS
62
+# --
63
+
64
+GetOSInfo() {
65
+    # Figure out which vedor we are
66
+    if [ -r /etc/lsb-release ]; then
67
+        . /etc/lsb-release
68
+        VENDORNAME=$DISTRIB_ID
69
+        RELEASE=$DISTRIB_RELEASE
70
+    else
71
+        for r in RedHat CentOS Fedora; do
72
+            VENDORPKG="`echo $r | tr [:upper:] [:lower:]`-release"
73
+            VENDORNAME=$r
74
+            RELEASE=`rpm -q --queryformat '%{VERSION}' $VENDORPKG`
75
+            if [ $? = 0 ]; then
76
+                break
77
+            fi
78
+            VENDORNAME=""
79
+        done
80
+        # Get update level
81
+        if [ -n "`grep Update /etc/redhat-release`" ]; then
82
+            # Get update
83
+            UPDATE=`cat /etc/redhat-release | sed s/.*Update\ // | sed s/\)$//`
84
+        else
85
+            # Assume update 0
86
+            UPDATE=0
87
+        fi
88
+    fi
89
+
90
+    echo "os|vendor=$VENDORNAME"
91
+    echo "os|release=$RELEASE"
92
+    if [ -n "$UPDATE" ]; then
93
+        echo "os|version=$UPDATE"
94
+    fi
95
+}
96
+
97
+GetOSInfo
98
+
99
+# Packages
100
+# --------
101
+
102
+# - We are going to check packages only for the services needed.
103
+# - We are parsing the packages files and detecting metadatas.
104
+#  - If we have the meta-keyword dist:DISTRO or
105
+#    dist:DISTRO1,DISTRO2 it will be installed only for those
106
+#    distros (case insensitive).
107
+function get_packages() {
108
+    local file_to_parse="general"
109
+    local service
110
+
111
+    for service in ${ENABLED_SERVICES//,/ }; do
112
+        # Allow individual services to specify dependencies
113
+        if [[ -e $FILES/apts/${service} ]]; then
114
+            file_to_parse="${file_to_parse} $service"
115
+        fi
116
+        if [[ $service == n-* ]]; then
117
+            if [[ ! $file_to_parse =~ nova ]]; then
118
+                file_to_parse="${file_to_parse} nova"
119
+            fi
120
+        elif [[ $service == g-* ]]; then
121
+            if [[ ! $file_to_parse =~ glance ]]; then
122
+                file_to_parse="${file_to_parse} glance"
123
+            fi
124
+        elif [[ $service == key* ]]; then
125
+            if [[ ! $file_to_parse =~ keystone ]]; then
126
+                file_to_parse="${file_to_parse} keystone"
127
+            fi
128
+        fi
129
+    done
130
+
131
+    for file in ${file_to_parse}; do
132
+        local fname=${FILES}/apts/${file}
133
+        local OIFS line package distros distro
134
+        [[ -e $fname ]] || { echo "missing: $fname"; exit 1; }
135
+
136
+        OIFS=$IFS
137
+        IFS=$'\n'
138
+        for line in $(<${fname}); do
139
+            if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then # We are using BASH regexp matching feature.
140
+                        package=${BASH_REMATCH[1]}
141
+                        distros=${BASH_REMATCH[2]}
142
+                        for distro in ${distros//,/ }; do  #In bash ${VAR,,} will lowecase VAR
143
+                            [[ ${distro,,} == ${DISTRO,,} ]] && echo $package
144
+                        done
145
+                        continue
146
+            fi
147
+
148
+            echo ${line%#*}
149
+        done
150
+        IFS=$OIFS
151
+    done
152
+}
153
+
154
+for p in $(get_packages); do
155
+    ver=$(dpkg -s $p 2>/dev/null | grep '^Version: ' | cut -d' ' -f2)
156
+    echo "pkg|${p}|${ver}"
157
+done
158
+
159
+# Pips
160
+# ----
161
+
162
+function get_pips() {
163
+    cat $FILES/pips/* | uniq
164
+}
165
+
166
+# Pip tells us what is currently installed
167
+FREEZE_FILE=$(mktemp --tmpdir freeze.XXXXXX)
168
+pip freeze >$FREEZE_FILE 2>/dev/null
169
+
170
+# Loop through our requirements and look for matches
171
+for p in $(get_pips); do
172
+    [[ "$p" = "-e" ]] && continue
173
+    if [[ "$p" =~ \+?([^#]*)#? ]]; then
174
+         # Get the URL from a remote reference
175
+         p=${BASH_REMATCH[1]}
176
+    fi
177
+    line="`grep -i $p $FREEZE_FILE`"
178
+    if [[ -n "$line" ]]; then
179
+        if [[ "$line" =~ \+(.*)@(.*)#egg=(.*) ]]; then
180
+            # Handle URLs
181
+            p=${BASH_REMATCH[1]}
182
+            ver=${BASH_REMATCH[2]}
183
+        elif [[ "$line" =~ (.*)[=\<\>]=(.*) ]]; then
184
+            # Normal pip packages
185
+            p=${BASH_REMATCH[1]}
186
+            ver=${BASH_REMATCH[2]}
187
+        else
188
+            # Unhandled format in freeze file
189
+            #echo "unknown: $p"
190
+            continue
191
+        fi
192
+        echo "pip|${p}|${ver}"
193
+    else
194
+        # No match in freeze file
195
+        #echo "unknown: $p"
196
+        continue
197
+    fi
198
+done
199
+
200
+rm $FREEZE_FILE
201
+
202
+# localrc
203
+# -------
204
+
205
+# Dump localrc with 'localrc|' prepended and comments and passwords left out
206
+if [[ -r $TOP_DIR/localrc ]]; then
207
+    sed -e '
208
+        /PASSWORD/d;
209
+        /^#/d;
210
+        s/^/localrc\|/;
211
+    ' $TOP_DIR/localrc | sort
212
+fi