summaryrefslogtreecommitdiffstats
path: root/provider-register.sh
blob: b1564278dd7afe3f01c9a9bed773f9da2dc4291e (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
#!/bin/sh
# $Id: provider-register.sh,v 1.9 2009/05/23 02:52:50 tyreld Exp $
# ==================================================================
# (C) Copyright IBM Corp. 2002, 2009, 2009
#
# THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
# ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
# CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
#
# You can obtain a current copy of the Eclipse Public License from
# http://www.eclipse.org/legal/epl-v10.html
#
# Author:       Viktor Mihajlovski <mihajlov@de.ibm.com>
# Contributors:
# Description:  Script to install class definitions (MOFs) and 
#               registration data for a variety of supported CIMOMs
# ==================================================================

pegasus_repository()
{
    for p in $PEGASUS_HOME /var/lib/Pegasus /var/lib/pegasus \
	/usr/local/var/lib/pegasus /var/local/lib/pegasus /var/opt/tog-pegasus 
    do
      if test -d $p/repository
      then
	  echo $p/$1
	  return 0
      fi
    done
    return 1
}

pegasus_path()
{
    for p in $PEGASUS_HOME/bin /usr/bin /usr/sbin /usr/local/bin \
	/usr/local/sbin /opt/tog-pegasus/bin /opt/tog-pegasus/sbin 
    do
      if test -x $p/$1
      then
	  echo $p/$1
	  return 0
      fi
    done
    return 1
}

pegasus_transform()
{
    OUTFILE=$1
    shift
    regfiles=$*
    PROVIDERMODULES=`cat $regfiles 2> /dev/null | grep -v '^[[:space:]]*#.*' | cut -d ' ' -f 4 | sort | uniq`
    if test x"$PROVIDERMODULES" = x
    then
	echo Failed to read registration files >&2
	return 1
    fi
    PROVIDERS=`cat $regfiles 2> /dev/null | grep -v '^[[:space:]]*#.*' | cut -d ' ' -f 3-4 | sort | uniq`
    
# produce ProviderModules
    echo > $OUTFILE
    chatter "Processing provider modules:" $PROVIDERMODULES
    for pm in $PROVIDERMODULES
    do
      cat >> $OUTFILE <<EOFPM
instance of PG_ProviderModule
{
   Name = "$pm";
   Location = "$pm";
   Vendor = "SBLIM";
   Version = "2.0.0";
   InterfaceType = "CMPI";
   InterfaceVersion = "2.0.0";
};

EOFPM
    done
    
# produce Providers
    set -- $PROVIDERS
    while test x$1 != x
    do
      cat >> $OUTFILE <<EOFP
instance of PG_Provider
{
   Name = "$1";
   ProviderModuleName = "$2";
};

EOFP
      shift 2
    done

#produce Capabilities
    let serial=0
    for rf in $regfiles
    do
      cat $rf | grep -v '^[[:space:]]*#.*' | while read CLASSNAME NAMESPACE PROVIDERNAME PROVIDERMODULE CAPS
      do
	let serial=serial+1
	numcap=
	for cap in $CAPS
	do
	  case $cap in
	      instance) 
		  if test x$numcap = x 
		  then numcap=2
		  else numcap="$numcap, 2"
		  fi;;
	      association) 
		  if test x$numcap = x 
		  then numcap=3
		  else numcap="$numcap, 3"
		  fi;;
	      indication) 
		  if test x$numcap = x 
		  then numcap=4
		  else numcap="$numcap, 4"
		  fi;;
	      method) 
		  if test x$numcap = x 
		  then numcap=5
		  else numcap="$numcap, 5"
		  fi;;
	      **) echo unknown provider type $cap >&2
		  return 1;;
	  esac	  
	done
	cat >> $OUTFILE <<EOFC
instance of PG_ProviderCapabilities
{
   ProviderModuleName = "$PROVIDERMODULE";
   ProviderName = "$PROVIDERNAME";
   ClassName = "$CLASSNAME";
   ProviderType = { $numcap };
   Namespaces = {"$NAMESPACE"};
   SupportedProperties = NULL;
   SupportedMethods = NULL;
   CapabilityID = "$CLASSNAME-$serial";
};

EOFC
      done
    done
}

pegasus_install()
{
    if ps -C cimserver > /dev/null 2>&1 
    then
	CIMMOF=`pegasus_path cimmof`
	if test $? != 0
	then
	    echo "Error: cimmof not found" >&2
	    return 1
	fi
	state=active
    else
	CIMMOF=`pegasus_path cimmofl`
	if test $? != 0
	then
	    echo "Error: cimmofl not found" >&2
	    return 1
	fi
	PEGASUSREPOSITORY=`pegasus_repository`
	if test $? != 0
	then
	    echo "Error: pegasus repository not found" >&2
	    return 1
	fi
	CIMMOF="$CIMMOF -R $PEGASUSREPOSITORY"
	state=inactive
    fi

    mofpath=
    mymofs=
    myregs=
    mofmode=1
    namespace=$1
    shift

    while test x$1 != x
    do 
      if test $1 = ":"
      then 
	  mofmode=0
	  shift
	  continue
      fi
      if test $mofmode = 1
      then
	  if test x$mofpath = x
	  then
	      mofpath=`dirname $1`
	  fi
	  mymofs="$mymofs $1"
      else
	  myregs="$myregs $1"
      fi
      shift
    done
  
    for _TEMPDIR in /var/tmp /tmp
    do
      if test -w $_TEMPDIR
      then
	  _REGFILENAME=$_TEMPDIR/$$.mof
	  break
      fi
    done

    
    trap "rm -f $_REGFILENAME" EXIT

    if pegasus_transform $_REGFILENAME $myregs
    then
	chatter Registering providers with $state cimserver
	$CIMMOF -uc -I $mofpath -n $namespace $mymofs &&
	$CIMMOF -uc -n root/PG_Interop $_REGFILENAME
    else
	echo "Failed to build pegasus registration MOF." >&2
	return 1
    fi
}

pegasus_uninstall()
{
    mymofs=
    myregs=
    mofmode=1
    namespace=$1
    shift

    while test x$1 != x
    do 
      if test $1 = ":"
      then 
	  mofmode=0
	  shift
	  continue
      fi
      if test $mofmode = 1
      then
	  mymofs="$mymofs $1"
      else
	  myregs="$myregs $1"
      fi
      shift
    done
  
    if ps -C cimserver > /dev/null 2>&1 
    then
	PROVIDERMODULES=`cat $myregs 2> /dev/null | grep -v '^[[:space:]]*#.*' | cut -d ' ' -f 4 | sort | uniq`
	if test x"$PROVIDERMODULES" = x
	    then
	    echo Failed to read registration files >&2
	    return 1
	fi
	CIMPROVIDER=`pegasus_path cimprovider`	
	if test $? != 0
        then
	    echo "Error: cimprovider not found" >&2
	    return 1
	fi
	for pm in $PROVIDERMODULES
	do
	  chatter "Remove provider module" $pm
	  $CIMPROVIDER -d -m $pm > /dev/null &&
	  $CIMPROVIDER -r -m $pm > /dev/null
	done
	WBEMEXEC=`pegasus_path wbemexec`	
	if test $? != 0
        then
	    echo "Error: wbemexec not found" >&2
	    return 1
	fi
	CLASSES=`cat $myregs 2> /dev/null | grep -v '^[[:space:]]*#.*' | cut -d ' ' -f 1 | grep -v '^CIM_'`

	for _TEMPDIR in /var/tmp /tmp
	  do
	  if test -w $_TEMPDIR
	      then
	      _DELETE_NAME=$TEMPDIR/delete-class.mof
	      break
	  fi
	done

	trap "rm -f $_DELETE_NAME" EXIT

	for cls in $CLASSES
	do
	  chatter Delete CIM Class $cls
	  cat > $_DELETE_NAME <<EOFA
<?xml version="1.0" encoding="utf-8" ?>
<CIM CIMVERSION="2.0" DTDVERSION="2.0">
 <MESSAGE ID="4711" PROTOCOLVERSION="1.0">
  <SIMPLEREQ>
   <IMETHODCALL NAME="DeleteClass">
    <LOCALNAMESPACEPATH>
EOFA
	  for ns in `echo $namespace | sed 's?/? ?'g`
	  do
	    cat >> $_DELETE_NAME <<EOFX
     <NAMESPACE NAME="$ns"></NAMESPACE>
EOFX
	  done
	  cat >> $_DELETE_NAME <<EOFE
    </LOCALNAMESPACEPATH>
    <IPARAMVALUE NAME="ClassName">
     <CLASSNAME NAME="$cls"/>
    </IPARAMVALUE>
   </IMETHODCALL>
  </SIMPLEREQ>
 </MESSAGE>
</CIM>
EOFE
	  $WBEMEXEC > /dev/null $_DELETE_NAME
	done
    else
	echo "Sorry, cimserver must be running to deregister the providers." >&2
	return 1
    fi
}

sfcb_transform()
{
    OUTFILE=$1
    shift
    regfiles=$*

#produce sfcb registraion
    for rf in $regfiles
    do
      cat $rf | grep -v '^[[:space:]]*#.*' | while read CLASSNAME NAMESPACE PROVIDERNAME PROVIDERMODULE CAPS
      do
	chatter "Registering class" $CLASSNAME
	cat >> $OUTFILE <<EOFC
[$CLASSNAME]   
   provider: $PROVIDERNAME
   location: $PROVIDERMODULE
   type: $CAPS
   namespace: $NAMESPACE
#
EOFC
      done
    done
}

sfcb_rebuild()
{
    if ps -C sfcbd > /dev/null 2>&1
    then
        # sfcb is running -- need to restart
	for INITSCRIPT in /etc/init.d/sfcb /usr/local/etc/init.d/sfcb none
	do
	  if test -x $INITSCRIPT
	  then
	      break;
	  fi
	done
	chatter "Shutting down sfcb."
	if test $INITSCRIPT = none
	then
	    killall sfcbd
	else
	    $INITSCRIPT stop
	fi
	t=0
	while ps -C sfcbd > /dev/null 2>&1
	do
	  sleep 1
	  t=`expr $t + 1`
	  if test $t -gt 10
	  then
	      echo "Timed out waiting for sfcb shutdown..." >&2
	      echo "Please stop sfcb manually and rebuild the repository using sfcbrepos." >&2
	      return 1
	  fi
	done
	chatter "Rebuilding repository."
	sfcbrepos -f $endian
	if test $? != 0
	then
	    echo "Repository rebuild failed." >&2
	    return 1
	fi
	
	if test $INITSCRIPT = none
	then
	    echo "No init script found - you need to start sfcbd manually." >&2
	    return 1
	else
	    chatter "Restarting sfcb."
	    $INITSCRIPT start
	fi
    else
	# Not running - rebuild repository
	chatter "Rebuilding repository."
	sfcbrepos -f $endian
    fi
}

sfcb_install()
{    
    mymofs=
    myregs=
    mofmode=1
    namespace=$1
    shift
    
    while test x$1 != x
    do 
      if test $1 = ":"
      then 
	  mofmode=0
	  shift
	  baseregname=`basename $1 .registration`
	  continue
      fi
      if test $mofmode = 1
      then
	  mymofs="$mymofs $1"
      else
	  myregs="$myregs $1"
      fi
      shift
    done

    for _TEMPDIR in /var/tmp /tmp
    do
      if test -w $_TEMPDIR
      then
	  _REGFILENAME=$_TEMPDIR/$baseregname.reg
	  break
      fi
    done

    trap "rm -f $_REGFILENAME" EXIT

    if sfcb_transform $_REGFILENAME $myregs
    then
	chatter "Staging provider registration."
	sfcbstage -n $namespace -r $_REGFILENAME $mymofs
	if test $? != 0 
	then
	    echo "Failed to stage provider registration." >&2
	    return 1
	fi
	sfcb_rebuild
    else
	echo "Failed to build sfcb registration file." >&2
	return 1
    fi
}

sfcb_uninstall()
{    
    mymofs=
    namespace=$1
    shift

    while test x$1 != x
    do 
      if test $1 = ":"
      then 
	  shift
	  baseregname=`basename $1 .registration`
	  break
      fi
      mymofs="$mymofs `basename $1`"
      shift
    done
    
    # "Unstage" MOFs and the registration file
    chatter "Unstaging provider registrations."
    sfcbunstage -n $namespace -r $baseregname.reg $mymofs

    # Rebuild repository
    sfcb_rebuild
}

openwbem_transform()
{
    OUTFILE=$1
    shift
    moffiles=$*
    
    if rm -f $OUTFILE
    then
	for _f in $moffiles
	do
	  sed "s/Provider *( *\"cmpi:/Provider(\"cmpi::/g" < $_f >> $OUTFILE
	done
    fi
}

openwbem_repository()
{
    for p in /var/lib/openwbem /usr/local/var/openwbem
    do
      if test -f $p/schema.dat
      then
	  echo $p
	  return 0
      fi
    done
    return 1
}

openwbem_install()
{
    CIMMOF=`which owmofc 2> /dev/null`
    if test $? != 0
    then
	echo "Error: cimmof not found" >&2
	return 1
    fi

    if ps -C owcimomd > /dev/null 2>&1 
    then
	state=active
    else
	CIMMOF="$CIMMOF -d `openwbem_repository`"
	if test $? != 0
	then
	    echo "Error: OpenWBEM repository not found" >&2
	    return 1
	fi
	state=inactive
    fi

    for _TEMPDIR in /var/tmp /tmp
    do
      if test -w $_TEMPDIR
      then
	  _REGFILENAME=$_TEMPDIR/$$.mof
	  break
      fi
    done

    trap "rm -f $_REGFILENAME" EXIT

    if openwbem_transform $_REGFILENAME $*
    then
	chatter Registering providers with $state owcimomd
	$CIMMOF $_REGFILENAME > /dev/null
    else
	echo "Failed to build OpenWBEM registration MOF." >&2
	return 1
    fi       
}

openwbem_uninstall()
{
    CIMMOF=`which owmofc 2> /dev/null`
    if test $? != 0
    then
	echo "Error: cimmof not found" >&2
	return 1
    fi

    if ps -C owcimomd > /dev/null 2>&1 
    then
	state=active
    else
	CIMMOF="$CIMMOF -d `openwbem_repository`"
	if test $? != 0
	then
	    echo "Error: OpenWBEM repository not found" >&2
	    return 1
	fi
	state=inactive
    fi

    for _TEMPDIR in /var/tmp /tmp
    do
      if test -w $_TEMPDIR
      then
	  _REGFILENAME=$_TEMPDIR/$$.mof
	  break
      fi
    done

    trap "rm -f $_REGFILENAME" EXIT

    if openwbem_transform $_REGFILENAME $*
    then
	chatter Deregistering providers with $state owcimomd
	$CIMMOF -r $_REGFILENAME > /dev/null
    else
	echo "Failed to build OpenWBEM registration MOF." >&2
	return 1
    fi       
}

cim_server()
{
    for exname in sfcbd cimserver owcimomd
    do
      if pegasus_path $exname > /dev/null
      then
	  case $exname in
	      sfcbd) echo sfcb; return 0;;
	      cimserver) echo pegasus; return 0;;
	      owcimomd) echo openwbem; return 0;;
	  esac
	  break;
      fi
    done
    echo unknown
    return 1
}

usage() 
{
    echo "usage: $0 [-h] [-v] [-d] [-t <cimserver>] [-n <namespace>] -r regfile ... -m mof ..."
}

chatter()
{
    if test x$verbose != x
    then
	echo $*
    fi
}

gb_getopt()
{
    rmode=0
    mmode=0
    options=
    moffiles=
    registrations=
    while [ -n "$1" ]
    do
      case $1 in
	  -r) mmode=0;
	      rmode=1;
	      shift;;
	  -m) mmode=1;
	      rmode=0;
	      shift;;
	  -*) mmode=0;
	      rmode=0;
	      options="$options $1";
	      shift;;
	  **) if [ $mmode = 1 ] 
	      then moffiles="$moffiles $1"       
	      elif [ $rmode = 1 ]
	      then registrations="$registrations -r $1" 
	      else options="$options $1";
	      fi; 
	      shift;;
      esac
    done
    echo $options $registrations $moffiles 
}

prepargs=`gb_getopt $*`
args=`getopt dvhX:t:r:n: $prepargs`
rc=$?

if [ $rc = 127 ]
then
    echo "warning: getopt not found ...continue without syntax check"
    args=$prepargs
elif [ $rc != 0 ]
then
    usage $0
    exit 1
fi

namespace="root/cimv2"

set -- $args

while [ -n "$1" ]
do
  case $1 in
      -h) help=1; 
	  shift;
	  break;;
      -v) verbose=1; 
	  shift;;
      -X) endian="-X $2"; 
	  shift 2;;
      -d) deregister=1; 
	  shift;;
      -t) cimserver=$2;
	  shift 2;;
      -n) namespace=$2;
	  shift 2;;
      -r) regs="$regs $2";
	  shift 2;;
      --) shift;
	  break;;
      **) break;;
  esac
done

mofs=$*

if [ "$help" = "1" ]
then
    usage
    echo -e "\t-h display help message"
    echo -e "\t-v verbose mode"
    echo -e "\t-d deregister provider and uninstall schema"
    echo -e "\t-t specify cimserver type (pegasus|sfcb|openwbem|sniacimom)"
    echo -e "\t-r specify registration files"
    echo -e "\t-m specify schema mof files"
    echo -e "\t-X create repository for alternate platform (sfcb only at the moment)."
    echo -e "\t-n target namespace definition (default: root/cimv2)."
    echo
    echo Use this command to install schema mofs and register providers.
    echo CIM Server Type is required as well as at least one registration file and one mof.
    exit 0
fi

if test x"$mofs" = x || test x"$regs" = x
then
    usage $0
    exit 1
fi

if test x$cimserver = x
then
    cimserver=`cim_server`
    if test $? = 0
    then
	chatter "Autoselected CIM server type:" $cimserver
    else
	echo "CIM server type could not be determined, specify with -t." >&2
	exit 1
    fi
fi

if test x$deregister = x
then
    case $cimserver in
	pegasus) pegasus_install $namespace $mofs ":" $regs;;
	sfcb)    sfcb_install $namespace $mofs ":" $regs;;
	openwbem) openwbem_install $mofs ;;
	sniacimom) echo sniacimom not yet supported && exit 1 ;;
	**)	echo "Invalid CIM Server Type " $cimserver && exit 1;;
    esac
else
    case $cimserver in
	pegasus) pegasus_uninstall $namespace $mofs ":" $regs;;
	sfcb)    sfcb_uninstall $namespace $mofs ":" $regs;;
	openwbem) openwbem_uninstall $mofs ;;
	sniacimom) echo sniacimom not yet supported && exit 1 ;;
	**)	echo "Invalid CIM Server Type " $cimserver && exit 1;;
    esac
fi