~nobuto/+junk/juju-sosreport

« back to all changes in this revision

Viewing changes to juju-sosreport

  • Committer: Nobuto MURATA
  • Date: 2014-01-09 17:22:02 UTC
  • Revision ID: nobuto@ubuntu.com-20140109172202-paooqfvdq50r6o0j
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
 
 
3
set -e
 
4
set -u
 
5
 
 
6
## TODO: getopt
 
7
 
 
8
# check jq command
 
9
if ! which jq >/dev/null; then
 
10
    echo 'jq command is required to run juju-sosreport.'
 
11
    [ "`lsb_release -sc`" = 'precise' ] && echo 'You can install "jq" package from backports repository.'
 
12
    exit 1
 
13
fi
 
14
 
 
15
# extract service units
 
16
units=`juju status --format json | jq '.services | .[] | .units | keys[]' 2>/dev/null | tr -d '\"'`
 
17
 
 
18
if [ -z "$units" ]; then
 
19
    echo 'failed to get units.'
 
20
    exit 1
 
21
fi
 
22
 
 
23
# prepare script for ssh
 
24
SCRIPT=$(cat <<"EOF"
 
25
    set -e
 
26
    # if sosreport package is not available, enable backports
 
27
    if ! apt-cache show sosreport >/dev/null 2>&1 && [ "`lsb_release -sc`" = 'precise' ]; then
 
28
        sudo apt-add-repository -y 'deb http://archive.ubuntu.com/ubuntu precise-backports main universe'
 
29
        sudo apt-get update
 
30
    fi
 
31
 
 
32
    # install sosreport package
 
33
    which sosreport >/dev/null || sudo apt-get install -y sosreport
 
34
 
 
35
    # run sosreport
 
36
    sudo sosreport --batch
 
37
EOF
 
38
)
 
39
 
 
40
# create directory to save results
 
41
SOSREPORT_DIR=`mktemp -d /tmp/juju-sosreport_XXXXXXXX`
 
42
juju status > "$SOSREPORT_DIR"/juju-status.txt
 
43
 
 
44
# ssh into bootstrap node and all other units
 
45
for unit in 0 $units; do
 
46
    if [ "$unit" = '0' ]; then
 
47
        unit_name=bootstrap
 
48
    else
 
49
        unit_name="$unit"
 
50
    fi
 
51
 
 
52
    echo "$unit_name: executing sosreport..."
 
53
    report=`juju ssh "$unit" "$SCRIPT" "$@" 2>/dev/null | grep -A1 '^Your sosreport has been generated and saved in:' | tail -n1 | tr -d '\r\ '`
 
54
 
 
55
    echo "$unit_name: retrieving the result..."
 
56
    juju ssh "$unit" "sudo chown ubuntu: $report" 2>/dev/null
 
57
    juju scp "$unit":"$report" "$SOSREPORT_DIR"/"${unit_name/\//_}_`basename $report`" >/dev/null
 
58
    juju scp "$unit":"$report".md5 "$SOSREPORT_DIR"/"${unit_name/\//_}_`basename $report`.md5" >/dev/null
 
59
 
 
60
    echo ''
 
61
done
 
62
 
 
63
echo "Your sosreports have been saved in:"
 
64
echo "    $SOSREPORT_DIR/"