summaryrefslogtreecommitdiffstats
path: root/openlmi-mof-register
blob: 6107503b7fe882497df1b172186087cbcd66295f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/bin/sh
#
# Copyright (C) 2012-2013 Red Hat, Inc.  All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# Authors: Radek Novacek <rnovacek@redhat.com>
#          Jan Safranek <jsafrane@redhat.com>
#

pegasus_repository="/var/lib/Pegasus/"
default_namespace="root/cimv2"

function log()
{
    printf "%(%Y-%m-%d %H:%M:%S)T " -1 >&2
    printf "$*\n" >&2
}

function log_command()
{
    log "COMMAND:" "$*" >&2
    "$@"
    code=$?
    log "EXIT CODE:" "$code" >&2
    return $code
}

trap 'log "DONE WITH EXIT CODE:" "$?"' EXIT

function start_pegasus()
{
    # Start Pegasus CIMOM in restricted mode, listening only on its unix socket
    log_command /usr/sbin/cimserver daemon=true \
            enableHttpConnection=false \
            enableHttpsConnection=false \
            enableRemotePrivilegedUserAccess=false \
            slp=false

    if [ "$?" != "0" ]; then
        echo "Cannot start Pegasus" >&2
        exit 1
    fi
}

function stop_pegasus()
{
    log_command /usr/sbin/cimserver -s
}

function usage()
{
    printf "Usage: $0 [ --just-mofs ] [ -n namespace ] [ -c cimom ] [-v version]
                    CMD <mof> [mof] [...] [reg]
    CMD is one of [ register, unregister ]

    Default namespace is $default_namespace, which can be changed with '-n' option.

    If a registration file is provided, '-v' parameter is mandatory and specifies
    version of the provider API.

    Supported cimoms are sfcbd and tog-pegasus. Without \"-c\" argument, the
    operation is processed for any cimom present on system (all of them).

    --just-mofs option causes that all arguments after CMD will be
      treated as mof files - no registration file is expected.

      usage with --just-mofs:
        $0 --just-mofs CMD <mof> [mof] [...]
      usage without:
        $0 -v <version> CMD <mof> [mof] [...] <reg>\n"
 
}

function register()
{
    if [ $JUST_MOFS -eq 0 ]; then
        reg=$1
        shift
    fi
    mofs="$@"
    if [ $HAS_SFCBD -eq 1 ] && echo $cimom | grep -q 'all\|sfcbd';
    then
        log_command /usr/bin/sfcbstage -n $namespace ${reg:+-r} $reg $mofs
        log_command /usr/bin/sfcbrepos -f
        log_command /usr/bin/systemctl reload-or-try-restart sblim-sfcb.service
    fi

    if [ $HAS_PEGASUS -eq  1 ] && echo $cimom | grep -q 'all\|tog-pegasus';
    then
        /usr/sbin/cimserver --status > /dev/null 2>&1
        if [ $? -eq 0 ];
        then
            CIMMOF="/usr/bin/cimmof -aEV -n $namespace"
        else
            CIMMOF="/usr/bin/cimmofl -aEV -R $pegasus_repository -n $namespace"
        fi

        log_command $CIMMOF -uc $mofs
        if [ $JUST_MOFS -eq 0 ]; then
            if [ -x $(dirname $0)/openlmi-register-pegasus ];
            then
                log "COMMAND: cat \"$reg\" | $(dirname $0)/openlmi-register-pegasus -v \"$version\" | $CIMMOF -uc -n root/interop"
                cat "$reg" | $(dirname $0)/openlmi-register-pegasus -v "$version" | $CIMMOF -uc -n root/interop
                log "EXIT CODE:" "$?"
            else
                log "COMMAND: cat \"$reg\" | /usr/libexec/openlmi-register-pegasus -v \"$version\" | $CIMMOF -uc -n root/interop"
                cat "$reg" | /usr/libexec/openlmi-register-pegasus -v "$version" | $CIMMOF -uc -n root/interop
                log "EXIT CODE:" "$?"
            fi
        fi
    fi
}

function unregister()
{
    if [ $JUST_MOFS -eq 0 ]; then
        reg=$1
        shift
    fi

    declare -a mofs=("$@")
    if [ $HAS_SFCBD -eq 1 ] && echo $cimom | grep -q 'all\|sfcbd';
    then
        # convert mofs to `basename mof`
        declare -a bmofs
        for ((i=0; i<${#mofs[@]}; i++)); do
            bmofs[$i]=$(basename "${mofs[$i]}")
        done
        log_command /usr/bin/sfcbunstage -n $namespace ${reg:+-r} $(basename "$reg") ${bmofs[@]}
        log_command /usr/bin/sfcbrepos -f
        log_command /usr/bin/systemctl reload-or-try-restart sblim-sfcb.service
    fi

    if [ $HAS_PEGASUS -eq 1 ] && echo $cimom | grep -q 'all\|tog-pegasus';
    then
        # Pegasus must be running when removing MOF files and providers
        if ! /usr/sbin/cimserver --status &>/dev/null; then
            CUSTOM_PEGASUS=1
            start_pegasus
        fi
        if [ -n "$reg" ];
        then
            providers=$(sed -n 's/ *group: *//p' "$reg" | sort | uniq)
            if [ -z "$providers" ]; then
                # fall back to location if group: is not present
                providers=$(sed -n 's/ *location: *//p' "$reg" | sort | uniq)
            fi
            for provider in $providers;
            do
                log_command /usr/bin/cimprovider -d -m ${provider}
                log_command /usr/bin/cimprovider -r -m ${provider}
            done
        fi
        mofcomp -n $namespace -r ${mofs[@]}
        if [ -n "$CUSTOM_PEGASUS" ]; then
            stop_pegasus
        fi
    fi
}

log "STARTING" "$0" "$@"

JUST_MOFS=0
optspec=":hn:c:v:-:"

while getopts "$optspec" optchar; do
    case "$optchar" in
        -)  # long options parsing
            case "$OPTARG" in
                just-mofs)
                    JUST_MOFS=1
                    ;;
                help)
                    usage;
                    exit 0;
                    ;;
                *)
                    if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then
                        echo "Unknown option --${OPTARG}" >&2
                        exit 1
                    fi
                    ;;
            esac
            ;;
        n)
            namespace="$OPTARG"
            ;;
        c)
            cimom="$OPTARG"
            if [ "$cimom" != "sfcbd" -a "$cimom" != "tog-pegasus" ]; then
                echo "Not supported cimom: $cimom" >&2
                exit 1
            fi
            ;;
        h)
            usage;
            exit 0;
            ;;
        v)
            version="$OPTARG"
            ;;
        *)
            if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then
                echo "Non-option argument: '-${OPTARG}'" >&2
                exit 1
            fi
            ;;
    esac
done


shift $(($OPTIND - 1))
namespace=${namespace:-$default_namespace}
cimom=${cimom:-all}

if [ "$JUST_MOFS" -eq 0 -a -z "$version" ]; then
    echo "Missing -v option"
    usage
    exit 1
fi

if [ $# -lt 2 ];
then
    usage
    exit 1
fi

if [ -e /usr/sbin/sfcbd ];
then
    HAS_SFCBD=1
else
    HAS_SFCBD=0
    if [ $cimom = "sfcbd" ]; then
        echo "Sfcbd not detected on system!" >&2
        exit 1
    fi
fi

if [ -e /usr/sbin/cimserver ];
then
    HAS_PEGASUS=1
else
    HAS_PEGASUS=0
    if [ $cimom = "tog-pegasus" ]; then
        echo "Pegasus not detected on system!" >&2
        exit 1
    fi
fi

# TODO: check if at least one server is installed
CMD=$1
shift

# parse the reg and mofs - use $@ and remove the last item
declare -a ARGS=("$@")
if [ $JUST_MOFS -eq 0 ];
then
    LEN=$(( ${#ARGS[@]} -1 ))
    REG=${ARGS[$LEN]}
    MOFS=(${ARGS[@]:0:$(($LEN))})
else
    MOFS=("$@")
fi

case $CMD in
    register)
        register $REG ${MOFS[@]}
        ;;
    unregister)
        unregister $REG ${MOFS[@]}
        ;;
    **)
        usage
        exit 1
esac

exit 0