# PaCkAgE DaTaStReAm
opensvc 1 8608
# end of header
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            0707010000b7fd000081a4000000000000000000000001550eba89000000d8000000660000000300000000000000000000001000000000opensvc/pkginfo   CLASSES=none
PSTAMP=OPENSVC-1.6,REV=222
EMAIL=support@opensvc.com
VENDOR=http://www.opensvc.com
CATEGORY=application
VERSION=1.6,REV=222
ARCH=all
NAME=opensvc Service Manager for logical servers v1.6-222
PKG=opensvc
0707010000b7fc000081a4000000000000000000000001550eba8900009605000000660000000300000000000000000000000f00000000opensvc/pkgmap    : 1 8608
1 d none /opt/opensvc 0755 root root
1 d none /opt/opensvc/bin 0755 root root
1 s none /opt/opensvc/bin/alldownservices=svcmgr
1 s none /opt/opensvc/bin/allprimaryservices=svcmgr
1 s none /opt/opensvc/bin/allsecondaryservices=svcmgr
1 s none /opt/opensvc/bin/allservices=svcmgr
1 s none /opt/opensvc/bin/allupservices=svcmgr
1 d none /opt/opensvc/bin/cron 0755 root root
1 f none /opt/opensvc/bin/cron/opensvc 0755 root root 1537 63522 1425832955
1 f none /opt/opensvc/bin/darwin.com.opensvc.svcmgr.plist 0644 root root 586 50623 1425832955
1 f none /opt/opensvc/bin/nodemgr 0755 root root 7360 22084 1425832955
1 f none /opt/opensvc/bin/opensvc.init.AIX 0755 root root 248 22133 1425832955
1 f none /opt/opensvc/bin/opensvc.init.Darwin 0755 root root 739 61196 1425832955
1 f none /opt/opensvc/bin/opensvc.init.FreeBSD 0755 root root 462 38943 1425832955
1 f none /opt/opensvc/bin/opensvc.init.OSF1 0755 root root 229 20460 1425832955
1 f none /opt/opensvc/bin/opensvc.init.SunOS 0755 root root 557 47488 1425832955
1 f none /opt/opensvc/bin/opensvc.init.debian 0755 root root 615 48997 1425832955
1 f none /opt/opensvc/bin/opensvc.init.hpux 0755 root root 807 65499 1425832955
1 f none /opt/opensvc/bin/opensvc.init.redhat 0755 root root 725 62016 1425832955
1 f none /opt/opensvc/bin/opensvc.init.suse 0755 root root 617 49237 1425832955
1 f none /opt/opensvc/bin/perfagt.Darwin 0755 root root 2523 46695 1425832955
1 f none /opt/opensvc/bin/perfagt.HP-UX 0755 root root 3175 37459 1425832955
1 f none /opt/opensvc/bin/perfagt.Linux 0755 root root 4310 48437 1425832955
1 f none /opt/opensvc/bin/perfagt.SunOS 0755 root root 5513 61686 1425832955
1 f none /opt/opensvc/bin/postinstall 0755 root root 29066 20633 1425832955
1 f none /opt/opensvc/bin/postinstall.cmd 0644 root root 433 34010 1425832955
1 f none /opt/opensvc/bin/svcmgr 0755 root root 15875 60627 1425832955
1 f none /opt/opensvc/bin/svcmon 0755 root root 9388 62940 1425832955
1 f none /opt/opensvc/bin/systemd.opensvc-agent.service 0644 root root 388 36130 1425832955
1 d none /opt/opensvc/lib 0755 root root
1 f none /opt/opensvc/lib/_multiprocessing.py 0644 root root 63 5473 1425832955
1 f none /opt/opensvc/lib/checkAdvfsUsageOSF1.py 0644 root root 1683 62347 1425832955
1 f none /opt/opensvc/lib/checkBtrfsDevStatsLinux.py 0644 root root 3254 43752 1425832955
1 f none /opt/opensvc/lib/checkEthHP-UX.py 0644 root root 3560 42853 1425832955
1 f none /opt/opensvc/lib/checkEthLinux.py 0644 root root 3611 53190 1425832955
1 f none /opt/opensvc/lib/checkEthSunOS.py 0644 root root 10948 5668 1425832955
1 f none /opt/opensvc/lib/checkFmFmadm.py 0644 root root 1791 8251 1425832955
1 s none /opt/opensvc/lib/checkFmFmadmSunOS.py=checkFmFmadm.py
1 f none /opt/opensvc/lib/checkFmOpenManage.py 0644 root root 2403 50012 1425832955
1 s none /opt/opensvc/lib/checkFmOpenManageLinux.py=checkFmOpenManage.py
1 f none /opt/opensvc/lib/checkFsInodeAIX.py 0644 root root 1790 507 1425832955
1 f none /opt/opensvc/lib/checkFsInodeDarwin.py 0644 root root 1821 5364 1425832955
1 f none /opt/opensvc/lib/checkFsInodeHP-UX.py 0644 root root 2232 28203 1425832955
1 f none /opt/opensvc/lib/checkFsInodeLinux.py 0644 root root 2259 36126 1425832955
1 f none /opt/opensvc/lib/checkFsInodeSunOS.py 0644 root root 1953 12689 1425832955
1 f none /opt/opensvc/lib/checkFsUsageAIX.py 0644 root root 1790 490 1425832955
1 s none /opt/opensvc/lib/checkFsUsageDarwin.py=checkFsUsageLinux.py
1 s none /opt/opensvc/lib/checkFsUsageFreeBSD.py=checkFsUsageLinux.py
1 s none /opt/opensvc/lib/checkFsUsageHP-UX.py=checkFsUsageLinux.py
1 f none /opt/opensvc/lib/checkFsUsageLinux.py 0644 root root 2139 28714 1425832955
1 f none /opt/opensvc/lib/checkFsUsageSunOS.py 0644 root root 1948 12438 1425832955
1 f none /opt/opensvc/lib/checkFsUsageWindows.py 0644 root root 1806 6739 1425832955
1 f none /opt/opensvc/lib/checkLagHP-UX.py 0644 root root 2658 59775 1425832955
1 f none /opt/opensvc/lib/checkLagLinux.py 0644 root root 4820 19118 1425832955
1 f none /opt/opensvc/lib/checkLagSunOS.py 0644 root root 7603 11196 1425832955
1 f none /opt/opensvc/lib/checkLagWindows.py 0644 root root 1526 53143 1425832955
1 f none /opt/opensvc/lib/checkMceLinux.py 0644 root root 2510 53388 1425832955
1 f none /opt/opensvc/lib/checkMpathAIX.py 0644 root root 3035 16912 1425832955
1 f none /opt/opensvc/lib/checkMpathHP-UX.py 0644 root root 3098 23445 1425832955
1 f none /opt/opensvc/lib/checkMpathLinux.py 0644 root root 5041 11729 1425832955
1 f none /opt/opensvc/lib/checkMpathOSF1.py 0644 root root 1630 60030 1425832955
1 f none /opt/opensvc/lib/checkMpathPowerpath.py 0644 root root 3591 44723 1425832955
1 s none /opt/opensvc/lib/checkMpathPowerpathAIX.py=checkMpathPowerpath.py
1 s none /opt/opensvc/lib/checkMpathPowerpathHP-UX.py=checkMpathPowerpath.py
1 s none /opt/opensvc/lib/checkMpathPowerpathLinux.py=checkMpathPowerpath.py
1 s none /opt/opensvc/lib/checkMpathPowerpathSunOS.py=checkMpathPowerpath.py
1 f none /opt/opensvc/lib/checkMpathSunOS.py 0644 root root 3386 33781 1425832955
1 f none /opt/opensvc/lib/checkMpathWindows.py 0644 root root 2127 27429 1425832955
1 f none /opt/opensvc/lib/checkNumaLinux.py 0644 root root 2182 27267 1425832955
1 f none /opt/opensvc/lib/checkRaidMegaRaid.py 0644 root root 5499 41579 1425832955
1 s none /opt/opensvc/lib/checkRaidMegaRaidLinux.py=checkRaidMegaRaid.py
1 s none /opt/opensvc/lib/checkRaidMegaRaidSunOS.py=checkRaidMegaRaid.py
1 f none /opt/opensvc/lib/checkRaidSas2.py 0644 root root 4608 57609 1425832955
1 s none /opt/opensvc/lib/checkRaidSas2Linux.py=checkRaidSas2.py
1 s none /opt/opensvc/lib/checkRaidSas2SunOS.py=checkRaidSas2.py
1 f none /opt/opensvc/lib/checkRaidSmartArray.py 0644 root root 3915 22656 1425832955
1 s none /opt/opensvc/lib/checkRaidSmartArrayLinux.py=checkRaidSmartArray.py
1 s none /opt/opensvc/lib/checkRaidSmartArraySunOS.py=checkRaidSmartArray.py
1 f none /opt/opensvc/lib/checkRaidSmartArrayWindows.py 0644 root root 1340 43683 1425832955
1 f none /opt/opensvc/lib/checkVgUsageAIX.py 0644 root root 2118 24558 1425832955
1 f none /opt/opensvc/lib/checkVgUsageHP-UX.py 0644 root root 2635 64776 1425832955
1 f none /opt/opensvc/lib/checkVgUsageLinux.py 0644 root root 1954 14175 1425832955
1 f none /opt/opensvc/lib/checkZfsUsageSunOS.py 0644 root root 3371 42746 1425832955
1 s none /opt/opensvc/lib/checkZpoolFreeBSD.py=checkZpoolSunOS.py
1 f none /opt/opensvc/lib/checkZpoolSunOS.py 0644 root root 4346 29319 1425832955
1 f none /opt/opensvc/lib/checks.py 0644 root root 4744 14311 1425832955
1 f none /opt/opensvc/lib/collector.py 0644 root root 14697 13863 1425832955
1 f none /opt/opensvc/lib/compliance.py 0644 root root 25469 22407 1425832955
1 f none /opt/opensvc/lib/freezer.py 0644 root root 1780 9411 1425832955
1 f none /opt/opensvc/lib/hostidAIX.py 0644 root root 1107 26120 1425832955
1 f none /opt/opensvc/lib/hostidDarwin.py 0644 root root 888 9879 1425832955
1 s none /opt/opensvc/lib/hostidFreeBSD.py=hostidLinux.py
1 f none /opt/opensvc/lib/hostidHP-UX.py 0644 root root 1203 33767 1425832955
1 f none /opt/opensvc/lib/hostidLinux.py 0644 root root 888 9879 1425832955
1 f none /opt/opensvc/lib/hostidOSF1.py 0644 root root 829 5075 1425832955
1 f none /opt/opensvc/lib/hostidSunOS.py 0644 root root 888 9879 1425832955
1 f none /opt/opensvc/lib/hostidWindows.py 0644 root root 888 9879 1425832955
1 f none /opt/opensvc/lib/lock.py 0644 root root 3240 45479 1425832955
1 f none /opt/opensvc/lib/loopSunOS.py 0644 root root 1439 52901 1425832955
1 f none /opt/opensvc/lib/node.py 0644 root root 54232 13714 1425832955
1 f none /opt/opensvc/lib/nodeDarwin.py 0644 root root 1224 36311 1425832955
1 f none /opt/opensvc/lib/nodeHP-UX.py 0644 root root 1106 25464 1425832955
1 f none /opt/opensvc/lib/nodeLinux.py 0644 root root 1076 23916 1425832955
1 f none /opt/opensvc/lib/provDiskDcs.py 0644 root root 669 53980 1425832955
1 f none /opt/opensvc/lib/provFs.py 0644 root root 5847 28501 1425832955
1 f none /opt/opensvc/lib/provFsExt2.py 0644 root root 143 11721 1425832955
1 f none /opt/opensvc/lib/provFsExt3.py 0644 root root 143 11723 1425832955
1 f none /opt/opensvc/lib/provFsExt4.py 0644 root root 143 11725 1425832955
1 f none /opt/opensvc/lib/provFsVxfs.py 0644 root root 167 13496 1425832955
1 f none /opt/opensvc/lib/provFsZfs.py 0644 root root 1558 61639 1425832955
1 f none /opt/opensvc/lib/provIp.py 0644 root root 125 11090 1425832955
1 f none /opt/opensvc/lib/provKvm.py 0644 root root 4565 594 1425832955
1 f none /opt/opensvc/lib/provLoopLinux.py 0644 root root 877 65497 1425832955
1 f none /opt/opensvc/lib/provLxc.py 0644 root root 8263 52250 1425832955
1 f none /opt/opensvc/lib/provSrp.py 0644 root root 2972 12172 1425832955
1 f none /opt/opensvc/lib/provVgHP-UX.py 0644 root root 1766 60530 1425832955
1 f none /opt/opensvc/lib/provVgLinux.py 0644 root root 1779 60156 1425832955
1 f none /opt/opensvc/lib/provVgRadosLinux.py 0644 root root 1054 14821 1425832955
1 f none /opt/opensvc/lib/provZone.py 0644 root root 15568 43302 1425832955
1 f none /opt/opensvc/lib/provisioning.py 0644 root root 168 12704 1425832955
1 f none /opt/opensvc/lib/rcAddService.py 0644 root root 917 11807 1425832955
1 f none /opt/opensvc/lib/rcAdvfs.py 0644 root root 6402 38153 1425832955
1 f none /opt/opensvc/lib/rcAsset.py 0644 root root 20989 28498 1425832955
1 f none /opt/opensvc/lib/rcAssetAIX.py 0644 root root 3893 21584 1425832955
1 f none /opt/opensvc/lib/rcAssetDarwin.py 0644 root root 4004 37366 1425832955
1 f none /opt/opensvc/lib/rcAssetFreeBSD.py 0644 root root 2856 14962 1425832955
1 f none /opt/opensvc/lib/rcAssetHP-UX.py 0644 root root 9148 48357 1425832955
1 f none /opt/opensvc/lib/rcAssetLinux.py 0644 root root 14986 43697 1425832955
1 f none /opt/opensvc/lib/rcAssetOSF1.py 0644 root root 11890 37406 1425832955
1 f none /opt/opensvc/lib/rcAssetSunOS.py 0644 root root 7808 22346 1425832955
1 f none /opt/opensvc/lib/rcAssetWindows.py 0644 root root 5116 58263 1425832955
1 f none /opt/opensvc/lib/rcBrocade.py 0644 root root 4116 44291 1425832955
1 f none /opt/opensvc/lib/rcBtrfs.py 0644 root root 12568 4569 1425832955
1 f none /opt/opensvc/lib/rcCloud.py 0644 root root 444 33680 1425832955
1 f none /opt/opensvc/lib/rcCloudAmazon.py 0644 root root 1283 40589 1425832955
1 f none /opt/opensvc/lib/rcCloudGandi.py 0644 root root 1352 38858 1425832955
1 f none /opt/opensvc/lib/rcCloudOpenstack.py 0644 root root 2152 37985 1425832955
1 f none /opt/opensvc/lib/rcCloudVcloud.py 0644 root root 1900 16684 1425832955
1 f none /opt/opensvc/lib/rcCommandWorker.py 0644 root root 2377 49596 1425832955
1 f none /opt/opensvc/lib/rcContainerLinux.py 0644 root root 5093 12449 1425832955
1 f none /opt/opensvc/lib/rcDcs.py 0644 root root 9711 40810 1425832955
1 f none /opt/opensvc/lib/rcDevTree.py 0644 root root 9431 33142 1425832955
1 f none /opt/opensvc/lib/rcDevTreeAIX.py 0644 root root 2040 16817 1425832955
1 f none /opt/opensvc/lib/rcDevTreeHP-UX.py 0644 root root 6133 32296 1425832955
1 f none /opt/opensvc/lib/rcDevTreeLinux.py 0644 root root 17094 2003 1425832955
1 f none /opt/opensvc/lib/rcDevTreeOSF1.py 0644 root root 3094 11878 1425832955
1 f none /opt/opensvc/lib/rcDevTreeSunOS.py 0644 root root 8890 25666 1425832955
1 f none /opt/opensvc/lib/rcDevTreeWindows.py 0644 root root 619 47948 1425832955
1 f none /opt/opensvc/lib/rcDiskInfo.py 0644 root root 1361 41417 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoAIX.py 0644 root root 2937 21136 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoDarwin.py 0644 root root 888 9990 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoFreeBSD.py 0644 root root 1361 45357 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoHP-UX.py 0644 root root 7513 9267 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoLinux.py 0644 root root 9056 43611 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoOSF1.py 0644 root root 3638 64018 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoSunOS.py 0644 root root 5255 36039 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoWindows.py 0644 root root 4363 39417 1425832955
1 f none /opt/opensvc/lib/rcDocker.py 0644 root root 7641 50848 1425832955
1 f none /opt/opensvc/lib/rcEthtool.py 0644 root root 2499 56193 1425832955
1 f none /opt/opensvc/lib/rcEva.py 0644 root root 4069 38949 1425832955
1 f none /opt/opensvc/lib/rcExceptions.py 0644 root root 3861 53261 1425832955
1 f none /opt/opensvc/lib/rcGlobalEnv.py 0644 root root 6407 39766 1425832955
1 f none /opt/opensvc/lib/rcHds.py 0644 root root 3545 59098 1425832955
1 f none /opt/opensvc/lib/rcHp3par.py 0644 root root 9545 35829 1425832955
1 f none /opt/opensvc/lib/rcIbmDs.py 0644 root root 3768 17214 1425832955
1 f none /opt/opensvc/lib/rcIbmSvc.py 0644 root root 3273 46333 1425832955
1 f none /opt/opensvc/lib/rcIfconfig.py 0644 root root 5322 64765 1425832955
1 f none /opt/opensvc/lib/rcIfconfigAIX.py 0644 root root 3734 64764 1425832955
1 f none /opt/opensvc/lib/rcIfconfigDarwin.py 0644 root root 3778 1607 1425832955
1 f none /opt/opensvc/lib/rcIfconfigFreeBSD.py 0644 root root 2736 60429 1425832955
1 f none /opt/opensvc/lib/rcIfconfigHP-UX.py 0644 root root 5763 57176 1425832955
1 f none /opt/opensvc/lib/rcIfconfigLinux.py 0644 root root 9748 35841 1425832955
1 f none /opt/opensvc/lib/rcIfconfigOSF1.py 0644 root root 3969 11786 1425832955
1 f none /opt/opensvc/lib/rcIfconfigSunOS.py 0644 root root 5509 49908 1425832955
1 f none /opt/opensvc/lib/rcIfconfigWindows.py 0644 root root 2013 26232 1425832955
1 f none /opt/opensvc/lib/rcListener.py 0755 root root 1545 57923 1425832955
1 f none /opt/opensvc/lib/rcLogger.py 0644 root root 2878 33966 1425832955
1 f none /opt/opensvc/lib/rcLoopDarwin.py 0644 root root 1846 16218 1425832955
1 f none /opt/opensvc/lib/rcLoopLinux.py 0644 root root 1694 4675 1425832955
1 f none /opt/opensvc/lib/rcLvmAIX.py 0644 root root 8007 45184 1425832955
1 f none /opt/opensvc/lib/rcMd5.py 0644 root root 9399 15831 1425832955
1 f none /opt/opensvc/lib/rcMounts.py 0644 root root 2394 53717 1425832955
1 f none /opt/opensvc/lib/rcMountsAIX.py 0644 root root 4586 52397 1425832955
1 f none /opt/opensvc/lib/rcMountsDarwin.py 0644 root root 2437 54853 1425832955
1 f none /opt/opensvc/lib/rcMountsFreeBSD.py 0644 root root 2437 54853 1425832955
1 f none /opt/opensvc/lib/rcMountsHP-UX.py 0644 root root 2253 45587 1425832955
1 f none /opt/opensvc/lib/rcMountsLinux.py 0644 root root 2302 49134 1425832955
1 f none /opt/opensvc/lib/rcMountsOSF1.py 0644 root root 1670 19 1425832955
1 f none /opt/opensvc/lib/rcMountsSunOS.py 0644 root root 1939 19167 1425832955
1 f none /opt/opensvc/lib/rcNecIsm.py 0644 root root 10602 14317 1425832955
1 f none /opt/opensvc/lib/rcNexenta.py 0755 root root 11735 4274 1425832955
1 f none /opt/opensvc/lib/rcNode.py 0644 root root 1577 1569 1425832955
1 f none /opt/opensvc/lib/rcNsr.py 0644 root root 1179 17717 1425832955
1 f none /opt/opensvc/lib/rcOptParser.py 0644 root root 12977 61132 1425832955
1 f none /opt/opensvc/lib/rcOs.py 0644 root root 128 9832 1425832955
1 f none /opt/opensvc/lib/rcOsHP-UX.py 0644 root root 122 10053 1425832955
1 f none /opt/opensvc/lib/rcOsLinux.py 0644 root root 216 16341 1425832955
1 f none /opt/opensvc/lib/rcOsSunOS.py 0644 root root 177 13842 1425832955
1 f none /opt/opensvc/lib/rcOvm.py 0755 root root 4499 12375 1425832955
1 f none /opt/opensvc/lib/rcPasswdAIX.py 0644 root root 347 27171 1425832955
1 f none /opt/opensvc/lib/rcPasswdLinux.py 0644 root root 353 27529 1425832955
1 f none /opt/opensvc/lib/rcPasswdSunOS.py 0644 root root 1970 12852 1425832955
1 f none /opt/opensvc/lib/rcPkgAIX.py 0644 root root 1271 37076 1425832955
1 f none /opt/opensvc/lib/rcPkgDarwin.py 0644 root root 1754 9786 1425832955
1 f none /opt/opensvc/lib/rcPkgFreeBSD.py 0644 root root 1308 39656 1425832955
1 f none /opt/opensvc/lib/rcPkgHP-UX.py 0644 root root 1584 58969 1425832955
1 f none /opt/opensvc/lib/rcPkgLinux.py 0644 root root 2446 57881 1425832955
1 f none /opt/opensvc/lib/rcPkgOSF1.py 0644 root root 2073 28953 1425832955
1 f none /opt/opensvc/lib/rcPkgSunOS.py 0644 root root 3578 2614 1425832955
1 f none /opt/opensvc/lib/rcPkgWindows.py 0644 root root 5313 13604 1425832955
1 f none /opt/opensvc/lib/rcPrintTable.py 0644 root root 2703 51464 1425832955
1 f none /opt/opensvc/lib/rcScheduler.py 0644 root root 26062 16686 1425832955
1 f none /opt/opensvc/lib/rcStats.py 0644 root root 3977 40974 1425832955
1 f none /opt/opensvc/lib/rcStatsAIX.py 0644 root root 4851 34443 1425832955
1 f none /opt/opensvc/lib/rcStatsDarwin.py 0644 root root 5463 37133 1425832955
1 f none /opt/opensvc/lib/rcStatsFreeBSD.py 0644 root root 5268 21932 1425832955
1 f none /opt/opensvc/lib/rcStatsHP-UX.py 0644 root root 7637 2991 1425832955
1 f none /opt/opensvc/lib/rcStatsLinux.py 0644 root root 14149 14948 1425832955
1 f none /opt/opensvc/lib/rcStatsSunOS.py 0644 root root 8211 11797 1425832955
1 f none /opt/opensvc/lib/rcStatus.py 0644 root root 6674 51623 1425832955
1 f none /opt/opensvc/lib/rcSymmetrix.py 0644 root root 5995 39204 1425832955
1 f none /opt/opensvc/lib/rcSysReport.py 0644 root root 13070 17543 1425832955
1 f none /opt/opensvc/lib/rcSysReportAIX.py 0644 root root 152 13334 1425832955
1 f none /opt/opensvc/lib/rcSysReportDarwin.py 0644 root root 152 13334 1425832955
1 f none /opt/opensvc/lib/rcSysReportFreeBSD.py 0644 root root 152 13334 1425832955
1 f none /opt/opensvc/lib/rcSysReportHP-UX.py 0644 root root 152 13334 1425832955
1 f none /opt/opensvc/lib/rcSysReportLinux.py 0644 root root 152 13334 1425832955
1 f none /opt/opensvc/lib/rcSysReportOSF1.py 0644 root root 152 13334 1425832955
1 f none /opt/opensvc/lib/rcSysReportSunOS.py 0644 root root 152 13334 1425832955
1 f none /opt/opensvc/lib/rcUpdatePkgAIX.py 0644 root root 223 17362 1425832955
1 f none /opt/opensvc/lib/rcUpdatePkgDarwin.py 0644 root root 448 35295 1425832955
1 f none /opt/opensvc/lib/rcUpdatePkgHP-UX.py 0644 root root 264 21363 1425832955
1 f none /opt/opensvc/lib/rcUpdatePkgLinux.py 0644 root root 578 45763 1425832955
1 f none /opt/opensvc/lib/rcUpdatePkgOSF1.py 0644 root root 562 43217 1425832955
1 f none /opt/opensvc/lib/rcUpdatePkgSunOS.py 0644 root root 1563 56611 1425832955
1 f none /opt/opensvc/lib/rcUpdatePkgWindows.py 0644 root root 178 13967 1425832955
1 f none /opt/opensvc/lib/rcUtilities.py 0644 root root 11734 19304 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesAIX.py 0644 root root 326 23568 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesDarwin.py 0644 root root 376 25693 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesFreeBSD.py 0644 root root 292 21393 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesHP-UX.py 0644 root root 262 18527 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesLinux.py 0644 root root 3116 34867 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesOSF1.py 0644 root root 262 18523 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesSunOS.py 0644 root root 1240 24309 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesWindows.py 0644 root root 772 61112 1425832955
1 f none /opt/opensvc/lib/rcVioServer.py 0644 root root 3569 5301 1425832955
1 f none /opt/opensvc/lib/rcWakeOnLan.py 0644 root root 2246 44463 1425832955
1 f none /opt/opensvc/lib/rcWinScheduler.py 0755 root root 2979 45059 1425832955
1 f none /opt/opensvc/lib/rcZfs.py 0644 root root 7698 47344 1425832955
1 f none /opt/opensvc/lib/rcZone.py 0644 root root 4044 52804 1425832955
1 f none /opt/opensvc/lib/resApp.py 0644 root root 13158 34458 1425832955
1 f none /opt/opensvc/lib/resAppVm.py 0644 root root 4068 47480 1425832955
1 f none /opt/opensvc/lib/resContainer.py 0644 root root 9126 12143 1425832955
1 f none /opt/opensvc/lib/resContainerAmazon.py 0644 root root 12072 18588 1425832955
1 f none /opt/opensvc/lib/resContainerDocker.py 0644 root root 6971 56942 1425832955
1 f none /opt/opensvc/lib/resContainerEsx.py 0644 root root 4761 7558 1425832955
1 f none /opt/opensvc/lib/resContainerHpVm.py 0644 root root 5393 61622 1425832955
1 f none /opt/opensvc/lib/resContainerJail.py 0644 root root 4597 63333 1425832955
1 f none /opt/opensvc/lib/resContainerKvm.py 0644 root root 7502 18837 1425832955
1 f none /opt/opensvc/lib/resContainerLdom.py 0644 root root 5615 873 1425832955
1 f none /opt/opensvc/lib/resContainerLxc.py 0644 root root 10073 41520 1425832955
1 f none /opt/opensvc/lib/resContainerOpenstack.py 0644 root root 12426 44722 1425832955
1 f none /opt/opensvc/lib/resContainerOvm.py 0644 root root 6787 22064 1425832955
1 f none /opt/opensvc/lib/resContainerSrp.py 0644 root root 9237 52425 1425832955
1 f none /opt/opensvc/lib/resContainerVbox.py 0644 root root 5252 45861 1425832955
1 f none /opt/opensvc/lib/resContainerVcloud.py 0644 root root 10334 38647 1425832955
1 f none /opt/opensvc/lib/resContainerVz.py 0644 root root 6027 27588 1425832955
1 f none /opt/opensvc/lib/resContainerXen.py 0644 root root 4687 8451 1425832955
1 f none /opt/opensvc/lib/resContainerZone.py 0644 root root 15769 25904 1425832955
1 f none /opt/opensvc/lib/resDg.py 0644 root root 3057 33184 1425832955
1 f none /opt/opensvc/lib/resDrbd.py 0644 root root 6897 45071 1425832955
1 f none /opt/opensvc/lib/resHb.py 0644 root root 2677 3936 1425832955
1 f none /opt/opensvc/lib/resHbLinuxHA.py 0644 root root 2321 43474 1425832955
1 f none /opt/opensvc/lib/resHbOpenHA.py 0644 root root 18073 42701 1425832955
1 f none /opt/opensvc/lib/resHbRhcs.py 0644 root root 2180 28706 1425832955
1 f none /opt/opensvc/lib/resHbSg.py 0644 root root 1972 16627 1425832955
1 f none /opt/opensvc/lib/resHbVcs.py 0644 root root 2057 19361 1425832955
1 f none /opt/opensvc/lib/resHbovm.py 0644 root root 3455 59819 1425832955
1 f none /opt/opensvc/lib/resIp.py 0644 root root 11384 29136 1425832955
1 f none /opt/opensvc/lib/resIpAIX.py 0644 root root 2028 32647 1425832955
1 f none /opt/opensvc/lib/resIpCrossbow.py 0644 root root 6282 56438 1425832955
1 f none /opt/opensvc/lib/resIpDarwin.py 0644 root root 1970 26639 1425832955
1 f none /opt/opensvc/lib/resIpFreeBSD.py 0644 root root 2036 32860 1425832955
1 f none /opt/opensvc/lib/resIpHP-UX.py 0644 root root 2052 31470 1425832955
1 f none /opt/opensvc/lib/resIpLinux.py 0644 root root 2505 2599 1425832955
1 f none /opt/opensvc/lib/resIpOSF1.py 0644 root root 1976 27883 1425832955
1 f none /opt/opensvc/lib/resIpRhcsLinux.py 0644 root root 1422 48717 1425832955
1 f none /opt/opensvc/lib/resIpSgHP-UX.py 0644 root root 1048 20992 1425832955
1 f none /opt/opensvc/lib/resIpSgLinux.py 0644 root root 1048 21150 1425832955
1 f none /opt/opensvc/lib/resIpSunOS.py 0644 root root 1783 16172 1425832955
1 f none /opt/opensvc/lib/resIpVcsLinux.py 0644 root root 1048 21150 1425832955
1 f none /opt/opensvc/lib/resIpZone.py 0644 root root 3588 1103 1425832955
1 f none /opt/opensvc/lib/resLoop.py 0644 root root 2177 27999 1425832955
1 f none /opt/opensvc/lib/resLoopDarwin.py 0644 root root 2993 25547 1425832955
1 f none /opt/opensvc/lib/resLoopFreeBSD.py 0644 root root 3608 1261 1425832955
1 f none /opt/opensvc/lib/resLoopLinux.py 0644 root root 2953 21301 1425832955
1 f none /opt/opensvc/lib/resLvVcsLinux.py 0644 root root 2174 24783 1425832955
1 f none /opt/opensvc/lib/resMount.py 0644 root root 6830 42628 1425832955
1 f none /opt/opensvc/lib/resMountAIX.py 0644 root root 5837 27466 1425832955
1 f none /opt/opensvc/lib/resMountDarwin.py 0644 root root 8137 52994 1425832955
1 f none /opt/opensvc/lib/resMountFreeBSD.py 0644 root root 6204 55654 1425832955
1 f none /opt/opensvc/lib/resMountHP-UX.py 0644 root root 4444 60922 1425832955
1 f none /opt/opensvc/lib/resMountLinux.py 0644 root root 12549 29714 1425832955
1 f none /opt/opensvc/lib/resMountOSF1.py 0644 root root 5737 17478 1425832955
1 f none /opt/opensvc/lib/resMountRhcsLinux.py 0644 root root 2053 13211 1425832955
1 f none /opt/opensvc/lib/resMountSgHP-UX.py 0644 root root 2455 42162 1425832955
1 f none /opt/opensvc/lib/resMountSgLinux.py 0644 root root 2465 43353 1425832955
1 f none /opt/opensvc/lib/resMountSunOS.py 0644 root root 6492 60327 1425832955
1 f none /opt/opensvc/lib/resMountVcsLinux.py 0644 root root 2439 41446 1425832955
1 f none /opt/opensvc/lib/resScsiReserv.py 0644 root root 7186 1660 1425832955
1 s none /opt/opensvc/lib/resScsiReservFreeBSD.py=resScsiReservLinux.py
1 f none /opt/opensvc/lib/resScsiReservHP-UX.py 0644 root root 6160 60991 1425832955
1 f none /opt/opensvc/lib/resScsiReservLinux.py 0644 root root 5489 27649 1425832955
1 f none /opt/opensvc/lib/resScsiReservOSF1.py 0644 root root 7304 14679 1425832955
1 s none /opt/opensvc/lib/resScsiReservSunOS.py=resScsiReservLinux.py
1 f none /opt/opensvc/lib/resShareNfsHP-UX.py 0644 root root 5171 41165 1425832955
1 f none /opt/opensvc/lib/resShareNfsLinux.py 0644 root root 5925 35062 1425832955
1 f none /opt/opensvc/lib/resStonith.py 0644 root root 2822 4550 1425832955
1 f none /opt/opensvc/lib/resStonithCallout.py 0644 root root 857 53189 1425832955
1 f none /opt/opensvc/lib/resStonithIlo.py 0644 root root 976 63084 1425832955
1 f none /opt/opensvc/lib/resSync.py 0644 root root 5220 56470 1425832955
1 f none /opt/opensvc/lib/resSyncBtrfs.py 0644 root root 16897 18849 1425832955
1 f none /opt/opensvc/lib/resSyncDcs.py 0644 root root 5659 27223 1425832955
1 f none /opt/opensvc/lib/resSyncDcsCkpt.py 0644 root root 8045 3536 1425832955
1 f none /opt/opensvc/lib/resSyncDcsSnap.py 0644 root root 7416 59619 1425832955
1 f none /opt/opensvc/lib/resSyncDds.py 0644 root root 15473 19354 1425832955
1 f none /opt/opensvc/lib/resSyncDocker.py 0644 root root 6904 51279 1425832955
1 f none /opt/opensvc/lib/resSyncEvasnap.py 0644 root root 11551 63113 1425832955
1 f none /opt/opensvc/lib/resSyncHp3par.py 0644 root root 12755 21918 1425832955
1 f none /opt/opensvc/lib/resSyncIbmdsSnap.py 0644 root root 9644 35588 1425832955
1 f none /opt/opensvc/lib/resSyncNecIsmSnap.py 0644 root root 6708 26822 1425832955
1 f none /opt/opensvc/lib/resSyncNetapp.py 0644 root root 11342 40671 1425832955
1 f none /opt/opensvc/lib/resSyncNexenta.py 0644 root root 9512 39422 1425832955
1 f none /opt/opensvc/lib/resSyncRados.py 0644 root root 11710 40127 1425832955
1 f none /opt/opensvc/lib/resSyncRsync.py 0644 root root 16703 46432 1425832955
1 f none /opt/opensvc/lib/resSyncSymSrdfS.py 0644 root root 17531 54039 1425832955
1 f none /opt/opensvc/lib/resSyncSymclone.py 0644 root root 9990 4431 1425832955
1 f none /opt/opensvc/lib/resSyncSymcloneLinux.py 0644 root root 3301 40090 1425832955
1 f none /opt/opensvc/lib/resSyncZfs.py 0644 root root 14090 60203 1425832955
1 f none /opt/opensvc/lib/resVdisk.py 0644 root root 3235 42108 1425832955
1 f none /opt/opensvc/lib/resVgAIX.py 0644 root root 9752 30190 1425832955
1 f none /opt/opensvc/lib/resVgAdvfs.py 0644 root root 4402 60949 1425832955
1 f none /opt/opensvc/lib/resVgGandi.py 0644 root root 7562 13780 1425832955
1 f none /opt/opensvc/lib/resVgHP-UX.py 0644 root root 13216 22548 1425832955
1 f none /opt/opensvc/lib/resVgHpVm.py 0644 root root 5648 4552 1425832955
1 f none /opt/opensvc/lib/resVgLdom.py 0644 root root 4568 51096 1425832955
1 f none /opt/opensvc/lib/resVgLinux.py 0644 root root 6837 19340 1425832955
1 f none /opt/opensvc/lib/resVgRadosLinux.py 0644 root root 9932 52020 1425832955
1 f none /opt/opensvc/lib/resVgRaw.py 0644 root root 4782 9736 1425832955
1 f none /opt/opensvc/lib/resVgRawHP-UX.py 0644 root root 2109 19249 1425832955
1 f none /opt/opensvc/lib/resVgRawLinux.py 0644 root root 12174 8974 1425832955
1 f none /opt/opensvc/lib/resVgRawSunOS.py 0644 root root 2662 49868 1425832955
1 f none /opt/opensvc/lib/resVgSgHP-UX.py 0644 root root 950 13388 1425832955
1 f none /opt/opensvc/lib/resVgSgLinux.py 0644 root root 950 13546 1425832955
1 f none /opt/opensvc/lib/resVgVcsLinux.py 0644 root root 1338 40853 1425832955
1 f none /opt/opensvc/lib/resVgVeritas.py 0644 root root 4632 62944 1425832955
1 f none /opt/opensvc/lib/resVgZfs.py 0644 root root 6490 7747 1425832955
1 f none /opt/opensvc/lib/resources.py 0644 root root 22316 61599 1425832955
1 f none /opt/opensvc/lib/snap.py 0644 root root 5147 59907 1425832955
1 f none /opt/opensvc/lib/snapAdvfsOSF1.py 0644 root root 3895 40126 1425832955
1 f none /opt/opensvc/lib/snapJfs2AIX.py 0644 root root 4927 36941 1425832955
1 f none /opt/opensvc/lib/snapLvmLinux.py 0644 root root 5247 61023 1425832955
1 f none /opt/opensvc/lib/snapVxfsHP-UX.py 0644 root root 3570 10516 1425832955
1 f none /opt/opensvc/lib/snapZfsSunOS.py 0644 root root 2189 43488 1425832955
1 f none /opt/opensvc/lib/svc.py 0644 root root 91849 61196 1425832955
1 f none /opt/opensvc/lib/svcBuilder.py 0644 root root 96408 34844 1425832955
1 f none /opt/opensvc/lib/svcDict.py 0644 root root 103127 54302 1425832955
1 f none /opt/opensvc/lib/svcHosted.py 0644 root root 1757 7650 1425832955
1 f none /opt/opensvc/lib/svcRhcs.py 0644 root root 6463 19758 1425832955
1 f none /opt/opensvc/lib/svcSg.py 0644 root root 10281 60493 1425832955
1 f none /opt/opensvc/lib/svcVcs.py 0644 root root 7670 56775 1425832955
1 f none /opt/opensvc/lib/tabulate.py 0644 root root 26346 12381 1425832955
1 f none /opt/opensvc/lib/version.py 0644 root root 20 1321 1427028617
1 f none /opt/opensvc/lib/wmi.py 0644 root root 46934 3064 1425832955
1 f none /opt/opensvc/lib/xmlrpcClient.py 0644 root root 50957 49815 1425832955
1 d none /opt/opensvc/usr 0755 root root
1 d none /opt/opensvc/usr/share 0755 root root
1 f none /opt/opensvc/usr/share/AUTHORS 0644 root root 48 4634 1425832955
1 f none /opt/opensvc/usr/share/BANNER 0644 root root 828 5079 1425832955
1 f none /opt/opensvc/usr/share/COPYING 0644 root root 26436 9725 1425832955
1 d none /opt/opensvc/usr/share/bash_completion.d 0755 root root
1 f none /opt/opensvc/usr/share/bash_completion.d/opensvc 0644 root root 10602 52978 1425832955
1 d none /opt/opensvc/usr/share/doc 0755 root root
1 f none /opt/opensvc/usr/share/doc/node.conf 0644 root root 8698 15261 1425832955
1 f none /opt/opensvc/usr/share/doc/opensvc.haresource 0644 root root 506 43100 1425832955
1 d none /opt/opensvc/usr/share/doc/provisioning 0755 root root
1 f none /opt/opensvc/usr/share/doc/provisioning/provisioning.agent.debian 0755 root root 2602 50922 1425832955
1 f none /opt/opensvc/usr/share/doc/provisioning/provisioning.example 0644 root root 362 30449 1425832955
1 f none /opt/opensvc/usr/share/doc/template.DEFAULT.env.gz 0644 root root 3565 59644 1425832955
1 f none /opt/opensvc/usr/share/doc/template.app.env.gz 0644 root root 1280 30407 1425832955
1 f none /opt/opensvc/usr/share/doc/template.comp_module.py 0755 root root 1235 30145 1425832955
1 f none /opt/opensvc/usr/share/doc/template.comp_module.sh 0755 root root 488 35015 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.amazon.env.gz 0644 root root 1286 26857 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.docker.env.gz 0644 root root 1315 35413 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.esx.env.gz 0644 root root 1201 18006 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.hpvm.env.gz 0644 root root 1110 10555 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.jail.env.gz 0644 root root 1196 19782 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.kvm.env.gz 0644 root root 1240 30763 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.ldom.env.gz 0644 root root 1110 8724 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.lxc.env.gz 0644 root root 1247 25013 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.openstack.env.gz 0644 root root 1290 34040 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.ovm.env.gz 0644 root root 1282 28418 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.srp.env.gz 0644 root root 1218 21114 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.vbox.env.gz 0644 root root 1110 8594 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.vcloud.env.gz 0644 root root 1284 28245 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.vz.env.gz 0644 root root 1186 19803 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.xen.env.gz 0644 root root 1239 26615 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.zone.env.gz 0644 root root 1289 33668 1425832955
1 f none /opt/opensvc/usr/share/doc/template.disk.env.gz 0644 root root 987 60441 1425832955
1 f none /opt/opensvc/usr/share/doc/template.drbd.env.gz 0644 root root 1368 40148 1425832955
1 f none /opt/opensvc/usr/share/doc/template.fs.env.gz 0644 root root 1481 58294 1425832955
1 f none /opt/opensvc/usr/share/doc/template.hb.LinuxHA.env.gz 0644 root root 1010 64549 1425832955
1 f none /opt/opensvc/usr/share/doc/template.hb.OpenHA.env.gz 0644 root root 1047 63294 1425832955
1 f none /opt/opensvc/usr/share/doc/template.ip.crossbow.env.gz 0644 root root 1482 59360 1425832955
1 f none /opt/opensvc/usr/share/doc/template.ip.env.gz 0644 root root 1523 1951 1425832955
1 f none /opt/opensvc/usr/share/doc/template.loop.env.gz 0644 root root 1074 4071 1425832955
1 f none /opt/opensvc/usr/share/doc/template.pool.env.gz 0644 root root 1098 6323 1425832955
1 f none /opt/opensvc/usr/share/doc/template.share.nfs.env.gz 0644 root root 1106 6140 1425832955
1 f none /opt/opensvc/usr/share/doc/template.stonith.callout.env.gz 0644 root root 318 41384 1425832955
1 f none /opt/opensvc/usr/share/doc/template.stonith.ilo.env.gz 0644 root root 282 35610 1425832955
1 f none /opt/opensvc/usr/share/doc/template.subset.env.gz 0644 root root 247 29619 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.btrfs.env.gz 0644 root root 1390 48127 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.dcsckpt.env.gz 0644 root root 1497 57299 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.dcssnap.env.gz 0644 root root 1432 48348 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.dds.env.gz 0644 root root 1824 34156 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.docker.env.gz 0644 root root 1321 39660 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.evasnap.env.gz 0644 root root 1502 60038 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.hp3par.env.gz 0644 root root 1499 57844 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.ibmdssnap.env.gz 0644 root root 1526 64285 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.necismsnap.env.gz 0644 root root 1294 29464 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.netapp.env.gz 0644 root root 1477 52069 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.rsync.env.gz 0644 root root 2066 4989 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.symclone.env.gz 0644 root root 1671 13035 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.symsrdfs.env.gz 0644 root root 1373 41877 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.zfs.env.gz 0644 root root 1619 3566 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sysreport.conf 0644 root root 765 66 1425832955
1 f none /opt/opensvc/usr/share/doc/template.vdisk.env.gz 0644 root root 1081 2818 1425832955
1 f none /opt/opensvc/usr/share/doc/template.vg.rados.env.gz 0644 root root 1679 20447 1425832955
1 f none /opt/opensvc/usr/share/doc/template.vg.raw.env.gz 0644 root root 1509 56599 1425832955
1 f none /opt/opensvc/usr/share/doc/template.vg.veritas.env.gz 0644 root root 1417 48821 1425832955
1 f none /opt/opensvc/usr/share/doc/template.vmdg.ldom.env.gz 0644 root root 1330 38418 1425832955
1 d none /opt/opensvc/var 0755 root root
1 d none /opt/opensvc/var/compliance 0755 root root
1 d none /opt/opensvc/var/compliance/com.opensvc 0755 root root
1 f none /opt/opensvc/var/compliance/com.opensvc/authkeys.py 0755 root root 10838 55159 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/bios.py 0755 root root 2407 41835 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/chkconfig.py 0755 root root 3564 59594 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/comp.py 0644 root root 1204 31330 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/cron.py 0755 root root 7506 56105 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/etcsystem.py 0755 root root 7230 40930 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/fileinc.py 0755 root root 9977 16664 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/files.py 0755 root root 11740 44318 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/firmware.py 0755 root root 6763 26070 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/fs.py 0755 root root 22394 44414 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/groups.py 0755 root root 8321 6737 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/groups_membership.py 0755 root root 7911 61412 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/keyval.py 0755 root root 9869 30515 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/keyval_parser.py 0755 root root 4748 6236 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/linux.mpath.py 0755 root root 15901 52996 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/nodeconf.py 0755 root root 5854 19067 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/packages.py 0755 root root 25838 24307 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/process.py 0755 root root 13637 1086 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/rc.py 0755 root root 4632 4871 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/smfcfgs.py 0755 root root 13308 26086 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/sudoers.py 0755 root root 2443 51581 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/svcconf.py 0755 root root 10896 44880 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/sysctl.py 0755 root root 7934 34101 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/sysvinit.py 0755 root root 7805 31271 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/users.py 0755 root root 15491 34495 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/utilities.py 0644 root root 1683 8796 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/vuln.py 0755 root root 23404 13276 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/xinetd.py 0755 root root 5782 9680 1425832955
1 i pkginfo 216 17767 1427028617
1 i postinstall 473 42424 1427028617
   07070100000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000b00000000TRAILER!!!                                                                                                                                                                                                                                                                                                                                                                                                                                        0707010000b7fd000081a4000000000000000000000001550eba89000000d8000000660000000300000000000000000000000800000000pkginfo   CLASSES=none
PSTAMP=OPENSVC-1.6,REV=222
EMAIL=support@opensvc.com
VENDOR=http://www.opensvc.com
CATEGORY=application
VERSION=1.6,REV=222
ARCH=all
NAME=opensvc Service Manager for logical servers v1.6-222
PKG=opensvc
0707010000b7fc000081a4000000000000000000000001550eba8900009605000000660000000300000000000000000000000700000000pkgmap    : 1 8608
1 d none /opt/opensvc 0755 root root
1 d none /opt/opensvc/bin 0755 root root
1 s none /opt/opensvc/bin/alldownservices=svcmgr
1 s none /opt/opensvc/bin/allprimaryservices=svcmgr
1 s none /opt/opensvc/bin/allsecondaryservices=svcmgr
1 s none /opt/opensvc/bin/allservices=svcmgr
1 s none /opt/opensvc/bin/allupservices=svcmgr
1 d none /opt/opensvc/bin/cron 0755 root root
1 f none /opt/opensvc/bin/cron/opensvc 0755 root root 1537 63522 1425832955
1 f none /opt/opensvc/bin/darwin.com.opensvc.svcmgr.plist 0644 root root 586 50623 1425832955
1 f none /opt/opensvc/bin/nodemgr 0755 root root 7360 22084 1425832955
1 f none /opt/opensvc/bin/opensvc.init.AIX 0755 root root 248 22133 1425832955
1 f none /opt/opensvc/bin/opensvc.init.Darwin 0755 root root 739 61196 1425832955
1 f none /opt/opensvc/bin/opensvc.init.FreeBSD 0755 root root 462 38943 1425832955
1 f none /opt/opensvc/bin/opensvc.init.OSF1 0755 root root 229 20460 1425832955
1 f none /opt/opensvc/bin/opensvc.init.SunOS 0755 root root 557 47488 1425832955
1 f none /opt/opensvc/bin/opensvc.init.debian 0755 root root 615 48997 1425832955
1 f none /opt/opensvc/bin/opensvc.init.hpux 0755 root root 807 65499 1425832955
1 f none /opt/opensvc/bin/opensvc.init.redhat 0755 root root 725 62016 1425832955
1 f none /opt/opensvc/bin/opensvc.init.suse 0755 root root 617 49237 1425832955
1 f none /opt/opensvc/bin/perfagt.Darwin 0755 root root 2523 46695 1425832955
1 f none /opt/opensvc/bin/perfagt.HP-UX 0755 root root 3175 37459 1425832955
1 f none /opt/opensvc/bin/perfagt.Linux 0755 root root 4310 48437 1425832955
1 f none /opt/opensvc/bin/perfagt.SunOS 0755 root root 5513 61686 1425832955
1 f none /opt/opensvc/bin/postinstall 0755 root root 29066 20633 1425832955
1 f none /opt/opensvc/bin/postinstall.cmd 0644 root root 433 34010 1425832955
1 f none /opt/opensvc/bin/svcmgr 0755 root root 15875 60627 1425832955
1 f none /opt/opensvc/bin/svcmon 0755 root root 9388 62940 1425832955
1 f none /opt/opensvc/bin/systemd.opensvc-agent.service 0644 root root 388 36130 1425832955
1 d none /opt/opensvc/lib 0755 root root
1 f none /opt/opensvc/lib/_multiprocessing.py 0644 root root 63 5473 1425832955
1 f none /opt/opensvc/lib/checkAdvfsUsageOSF1.py 0644 root root 1683 62347 1425832955
1 f none /opt/opensvc/lib/checkBtrfsDevStatsLinux.py 0644 root root 3254 43752 1425832955
1 f none /opt/opensvc/lib/checkEthHP-UX.py 0644 root root 3560 42853 1425832955
1 f none /opt/opensvc/lib/checkEthLinux.py 0644 root root 3611 53190 1425832955
1 f none /opt/opensvc/lib/checkEthSunOS.py 0644 root root 10948 5668 1425832955
1 f none /opt/opensvc/lib/checkFmFmadm.py 0644 root root 1791 8251 1425832955
1 s none /opt/opensvc/lib/checkFmFmadmSunOS.py=checkFmFmadm.py
1 f none /opt/opensvc/lib/checkFmOpenManage.py 0644 root root 2403 50012 1425832955
1 s none /opt/opensvc/lib/checkFmOpenManageLinux.py=checkFmOpenManage.py
1 f none /opt/opensvc/lib/checkFsInodeAIX.py 0644 root root 1790 507 1425832955
1 f none /opt/opensvc/lib/checkFsInodeDarwin.py 0644 root root 1821 5364 1425832955
1 f none /opt/opensvc/lib/checkFsInodeHP-UX.py 0644 root root 2232 28203 1425832955
1 f none /opt/opensvc/lib/checkFsInodeLinux.py 0644 root root 2259 36126 1425832955
1 f none /opt/opensvc/lib/checkFsInodeSunOS.py 0644 root root 1953 12689 1425832955
1 f none /opt/opensvc/lib/checkFsUsageAIX.py 0644 root root 1790 490 1425832955
1 s none /opt/opensvc/lib/checkFsUsageDarwin.py=checkFsUsageLinux.py
1 s none /opt/opensvc/lib/checkFsUsageFreeBSD.py=checkFsUsageLinux.py
1 s none /opt/opensvc/lib/checkFsUsageHP-UX.py=checkFsUsageLinux.py
1 f none /opt/opensvc/lib/checkFsUsageLinux.py 0644 root root 2139 28714 1425832955
1 f none /opt/opensvc/lib/checkFsUsageSunOS.py 0644 root root 1948 12438 1425832955
1 f none /opt/opensvc/lib/checkFsUsageWindows.py 0644 root root 1806 6739 1425832955
1 f none /opt/opensvc/lib/checkLagHP-UX.py 0644 root root 2658 59775 1425832955
1 f none /opt/opensvc/lib/checkLagLinux.py 0644 root root 4820 19118 1425832955
1 f none /opt/opensvc/lib/checkLagSunOS.py 0644 root root 7603 11196 1425832955
1 f none /opt/opensvc/lib/checkLagWindows.py 0644 root root 1526 53143 1425832955
1 f none /opt/opensvc/lib/checkMceLinux.py 0644 root root 2510 53388 1425832955
1 f none /opt/opensvc/lib/checkMpathAIX.py 0644 root root 3035 16912 1425832955
1 f none /opt/opensvc/lib/checkMpathHP-UX.py 0644 root root 3098 23445 1425832955
1 f none /opt/opensvc/lib/checkMpathLinux.py 0644 root root 5041 11729 1425832955
1 f none /opt/opensvc/lib/checkMpathOSF1.py 0644 root root 1630 60030 1425832955
1 f none /opt/opensvc/lib/checkMpathPowerpath.py 0644 root root 3591 44723 1425832955
1 s none /opt/opensvc/lib/checkMpathPowerpathAIX.py=checkMpathPowerpath.py
1 s none /opt/opensvc/lib/checkMpathPowerpathHP-UX.py=checkMpathPowerpath.py
1 s none /opt/opensvc/lib/checkMpathPowerpathLinux.py=checkMpathPowerpath.py
1 s none /opt/opensvc/lib/checkMpathPowerpathSunOS.py=checkMpathPowerpath.py
1 f none /opt/opensvc/lib/checkMpathSunOS.py 0644 root root 3386 33781 1425832955
1 f none /opt/opensvc/lib/checkMpathWindows.py 0644 root root 2127 27429 1425832955
1 f none /opt/opensvc/lib/checkNumaLinux.py 0644 root root 2182 27267 1425832955
1 f none /opt/opensvc/lib/checkRaidMegaRaid.py 0644 root root 5499 41579 1425832955
1 s none /opt/opensvc/lib/checkRaidMegaRaidLinux.py=checkRaidMegaRaid.py
1 s none /opt/opensvc/lib/checkRaidMegaRaidSunOS.py=checkRaidMegaRaid.py
1 f none /opt/opensvc/lib/checkRaidSas2.py 0644 root root 4608 57609 1425832955
1 s none /opt/opensvc/lib/checkRaidSas2Linux.py=checkRaidSas2.py
1 s none /opt/opensvc/lib/checkRaidSas2SunOS.py=checkRaidSas2.py
1 f none /opt/opensvc/lib/checkRaidSmartArray.py 0644 root root 3915 22656 1425832955
1 s none /opt/opensvc/lib/checkRaidSmartArrayLinux.py=checkRaidSmartArray.py
1 s none /opt/opensvc/lib/checkRaidSmartArraySunOS.py=checkRaidSmartArray.py
1 f none /opt/opensvc/lib/checkRaidSmartArrayWindows.py 0644 root root 1340 43683 1425832955
1 f none /opt/opensvc/lib/checkVgUsageAIX.py 0644 root root 2118 24558 1425832955
1 f none /opt/opensvc/lib/checkVgUsageHP-UX.py 0644 root root 2635 64776 1425832955
1 f none /opt/opensvc/lib/checkVgUsageLinux.py 0644 root root 1954 14175 1425832955
1 f none /opt/opensvc/lib/checkZfsUsageSunOS.py 0644 root root 3371 42746 1425832955
1 s none /opt/opensvc/lib/checkZpoolFreeBSD.py=checkZpoolSunOS.py
1 f none /opt/opensvc/lib/checkZpoolSunOS.py 0644 root root 4346 29319 1425832955
1 f none /opt/opensvc/lib/checks.py 0644 root root 4744 14311 1425832955
1 f none /opt/opensvc/lib/collector.py 0644 root root 14697 13863 1425832955
1 f none /opt/opensvc/lib/compliance.py 0644 root root 25469 22407 1425832955
1 f none /opt/opensvc/lib/freezer.py 0644 root root 1780 9411 1425832955
1 f none /opt/opensvc/lib/hostidAIX.py 0644 root root 1107 26120 1425832955
1 f none /opt/opensvc/lib/hostidDarwin.py 0644 root root 888 9879 1425832955
1 s none /opt/opensvc/lib/hostidFreeBSD.py=hostidLinux.py
1 f none /opt/opensvc/lib/hostidHP-UX.py 0644 root root 1203 33767 1425832955
1 f none /opt/opensvc/lib/hostidLinux.py 0644 root root 888 9879 1425832955
1 f none /opt/opensvc/lib/hostidOSF1.py 0644 root root 829 5075 1425832955
1 f none /opt/opensvc/lib/hostidSunOS.py 0644 root root 888 9879 1425832955
1 f none /opt/opensvc/lib/hostidWindows.py 0644 root root 888 9879 1425832955
1 f none /opt/opensvc/lib/lock.py 0644 root root 3240 45479 1425832955
1 f none /opt/opensvc/lib/loopSunOS.py 0644 root root 1439 52901 1425832955
1 f none /opt/opensvc/lib/node.py 0644 root root 54232 13714 1425832955
1 f none /opt/opensvc/lib/nodeDarwin.py 0644 root root 1224 36311 1425832955
1 f none /opt/opensvc/lib/nodeHP-UX.py 0644 root root 1106 25464 1425832955
1 f none /opt/opensvc/lib/nodeLinux.py 0644 root root 1076 23916 1425832955
1 f none /opt/opensvc/lib/provDiskDcs.py 0644 root root 669 53980 1425832955
1 f none /opt/opensvc/lib/provFs.py 0644 root root 5847 28501 1425832955
1 f none /opt/opensvc/lib/provFsExt2.py 0644 root root 143 11721 1425832955
1 f none /opt/opensvc/lib/provFsExt3.py 0644 root root 143 11723 1425832955
1 f none /opt/opensvc/lib/provFsExt4.py 0644 root root 143 11725 1425832955
1 f none /opt/opensvc/lib/provFsVxfs.py 0644 root root 167 13496 1425832955
1 f none /opt/opensvc/lib/provFsZfs.py 0644 root root 1558 61639 1425832955
1 f none /opt/opensvc/lib/provIp.py 0644 root root 125 11090 1425832955
1 f none /opt/opensvc/lib/provKvm.py 0644 root root 4565 594 1425832955
1 f none /opt/opensvc/lib/provLoopLinux.py 0644 root root 877 65497 1425832955
1 f none /opt/opensvc/lib/provLxc.py 0644 root root 8263 52250 1425832955
1 f none /opt/opensvc/lib/provSrp.py 0644 root root 2972 12172 1425832955
1 f none /opt/opensvc/lib/provVgHP-UX.py 0644 root root 1766 60530 1425832955
1 f none /opt/opensvc/lib/provVgLinux.py 0644 root root 1779 60156 1425832955
1 f none /opt/opensvc/lib/provVgRadosLinux.py 0644 root root 1054 14821 1425832955
1 f none /opt/opensvc/lib/provZone.py 0644 root root 15568 43302 1425832955
1 f none /opt/opensvc/lib/provisioning.py 0644 root root 168 12704 1425832955
1 f none /opt/opensvc/lib/rcAddService.py 0644 root root 917 11807 1425832955
1 f none /opt/opensvc/lib/rcAdvfs.py 0644 root root 6402 38153 1425832955
1 f none /opt/opensvc/lib/rcAsset.py 0644 root root 20989 28498 1425832955
1 f none /opt/opensvc/lib/rcAssetAIX.py 0644 root root 3893 21584 1425832955
1 f none /opt/opensvc/lib/rcAssetDarwin.py 0644 root root 4004 37366 1425832955
1 f none /opt/opensvc/lib/rcAssetFreeBSD.py 0644 root root 2856 14962 1425832955
1 f none /opt/opensvc/lib/rcAssetHP-UX.py 0644 root root 9148 48357 1425832955
1 f none /opt/opensvc/lib/rcAssetLinux.py 0644 root root 14986 43697 1425832955
1 f none /opt/opensvc/lib/rcAssetOSF1.py 0644 root root 11890 37406 1425832955
1 f none /opt/opensvc/lib/rcAssetSunOS.py 0644 root root 7808 22346 1425832955
1 f none /opt/opensvc/lib/rcAssetWindows.py 0644 root root 5116 58263 1425832955
1 f none /opt/opensvc/lib/rcBrocade.py 0644 root root 4116 44291 1425832955
1 f none /opt/opensvc/lib/rcBtrfs.py 0644 root root 12568 4569 1425832955
1 f none /opt/opensvc/lib/rcCloud.py 0644 root root 444 33680 1425832955
1 f none /opt/opensvc/lib/rcCloudAmazon.py 0644 root root 1283 40589 1425832955
1 f none /opt/opensvc/lib/rcCloudGandi.py 0644 root root 1352 38858 1425832955
1 f none /opt/opensvc/lib/rcCloudOpenstack.py 0644 root root 2152 37985 1425832955
1 f none /opt/opensvc/lib/rcCloudVcloud.py 0644 root root 1900 16684 1425832955
1 f none /opt/opensvc/lib/rcCommandWorker.py 0644 root root 2377 49596 1425832955
1 f none /opt/opensvc/lib/rcContainerLinux.py 0644 root root 5093 12449 1425832955
1 f none /opt/opensvc/lib/rcDcs.py 0644 root root 9711 40810 1425832955
1 f none /opt/opensvc/lib/rcDevTree.py 0644 root root 9431 33142 1425832955
1 f none /opt/opensvc/lib/rcDevTreeAIX.py 0644 root root 2040 16817 1425832955
1 f none /opt/opensvc/lib/rcDevTreeHP-UX.py 0644 root root 6133 32296 1425832955
1 f none /opt/opensvc/lib/rcDevTreeLinux.py 0644 root root 17094 2003 1425832955
1 f none /opt/opensvc/lib/rcDevTreeOSF1.py 0644 root root 3094 11878 1425832955
1 f none /opt/opensvc/lib/rcDevTreeSunOS.py 0644 root root 8890 25666 1425832955
1 f none /opt/opensvc/lib/rcDevTreeWindows.py 0644 root root 619 47948 1425832955
1 f none /opt/opensvc/lib/rcDiskInfo.py 0644 root root 1361 41417 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoAIX.py 0644 root root 2937 21136 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoDarwin.py 0644 root root 888 9990 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoFreeBSD.py 0644 root root 1361 45357 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoHP-UX.py 0644 root root 7513 9267 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoLinux.py 0644 root root 9056 43611 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoOSF1.py 0644 root root 3638 64018 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoSunOS.py 0644 root root 5255 36039 1425832955
1 f none /opt/opensvc/lib/rcDiskInfoWindows.py 0644 root root 4363 39417 1425832955
1 f none /opt/opensvc/lib/rcDocker.py 0644 root root 7641 50848 1425832955
1 f none /opt/opensvc/lib/rcEthtool.py 0644 root root 2499 56193 1425832955
1 f none /opt/opensvc/lib/rcEva.py 0644 root root 4069 38949 1425832955
1 f none /opt/opensvc/lib/rcExceptions.py 0644 root root 3861 53261 1425832955
1 f none /opt/opensvc/lib/rcGlobalEnv.py 0644 root root 6407 39766 1425832955
1 f none /opt/opensvc/lib/rcHds.py 0644 root root 3545 59098 1425832955
1 f none /opt/opensvc/lib/rcHp3par.py 0644 root root 9545 35829 1425832955
1 f none /opt/opensvc/lib/rcIbmDs.py 0644 root root 3768 17214 1425832955
1 f none /opt/opensvc/lib/rcIbmSvc.py 0644 root root 3273 46333 1425832955
1 f none /opt/opensvc/lib/rcIfconfig.py 0644 root root 5322 64765 1425832955
1 f none /opt/opensvc/lib/rcIfconfigAIX.py 0644 root root 3734 64764 1425832955
1 f none /opt/opensvc/lib/rcIfconfigDarwin.py 0644 root root 3778 1607 1425832955
1 f none /opt/opensvc/lib/rcIfconfigFreeBSD.py 0644 root root 2736 60429 1425832955
1 f none /opt/opensvc/lib/rcIfconfigHP-UX.py 0644 root root 5763 57176 1425832955
1 f none /opt/opensvc/lib/rcIfconfigLinux.py 0644 root root 9748 35841 1425832955
1 f none /opt/opensvc/lib/rcIfconfigOSF1.py 0644 root root 3969 11786 1425832955
1 f none /opt/opensvc/lib/rcIfconfigSunOS.py 0644 root root 5509 49908 1425832955
1 f none /opt/opensvc/lib/rcIfconfigWindows.py 0644 root root 2013 26232 1425832955
1 f none /opt/opensvc/lib/rcListener.py 0755 root root 1545 57923 1425832955
1 f none /opt/opensvc/lib/rcLogger.py 0644 root root 2878 33966 1425832955
1 f none /opt/opensvc/lib/rcLoopDarwin.py 0644 root root 1846 16218 1425832955
1 f none /opt/opensvc/lib/rcLoopLinux.py 0644 root root 1694 4675 1425832955
1 f none /opt/opensvc/lib/rcLvmAIX.py 0644 root root 8007 45184 1425832955
1 f none /opt/opensvc/lib/rcMd5.py 0644 root root 9399 15831 1425832955
1 f none /opt/opensvc/lib/rcMounts.py 0644 root root 2394 53717 1425832955
1 f none /opt/opensvc/lib/rcMountsAIX.py 0644 root root 4586 52397 1425832955
1 f none /opt/opensvc/lib/rcMountsDarwin.py 0644 root root 2437 54853 1425832955
1 f none /opt/opensvc/lib/rcMountsFreeBSD.py 0644 root root 2437 54853 1425832955
1 f none /opt/opensvc/lib/rcMountsHP-UX.py 0644 root root 2253 45587 1425832955
1 f none /opt/opensvc/lib/rcMountsLinux.py 0644 root root 2302 49134 1425832955
1 f none /opt/opensvc/lib/rcMountsOSF1.py 0644 root root 1670 19 1425832955
1 f none /opt/opensvc/lib/rcMountsSunOS.py 0644 root root 1939 19167 1425832955
1 f none /opt/opensvc/lib/rcNecIsm.py 0644 root root 10602 14317 1425832955
1 f none /opt/opensvc/lib/rcNexenta.py 0755 root root 11735 4274 1425832955
1 f none /opt/opensvc/lib/rcNode.py 0644 root root 1577 1569 1425832955
1 f none /opt/opensvc/lib/rcNsr.py 0644 root root 1179 17717 1425832955
1 f none /opt/opensvc/lib/rcOptParser.py 0644 root root 12977 61132 1425832955
1 f none /opt/opensvc/lib/rcOs.py 0644 root root 128 9832 1425832955
1 f none /opt/opensvc/lib/rcOsHP-UX.py 0644 root root 122 10053 1425832955
1 f none /opt/opensvc/lib/rcOsLinux.py 0644 root root 216 16341 1425832955
1 f none /opt/opensvc/lib/rcOsSunOS.py 0644 root root 177 13842 1425832955
1 f none /opt/opensvc/lib/rcOvm.py 0755 root root 4499 12375 1425832955
1 f none /opt/opensvc/lib/rcPasswdAIX.py 0644 root root 347 27171 1425832955
1 f none /opt/opensvc/lib/rcPasswdLinux.py 0644 root root 353 27529 1425832955
1 f none /opt/opensvc/lib/rcPasswdSunOS.py 0644 root root 1970 12852 1425832955
1 f none /opt/opensvc/lib/rcPkgAIX.py 0644 root root 1271 37076 1425832955
1 f none /opt/opensvc/lib/rcPkgDarwin.py 0644 root root 1754 9786 1425832955
1 f none /opt/opensvc/lib/rcPkgFreeBSD.py 0644 root root 1308 39656 1425832955
1 f none /opt/opensvc/lib/rcPkgHP-UX.py 0644 root root 1584 58969 1425832955
1 f none /opt/opensvc/lib/rcPkgLinux.py 0644 root root 2446 57881 1425832955
1 f none /opt/opensvc/lib/rcPkgOSF1.py 0644 root root 2073 28953 1425832955
1 f none /opt/opensvc/lib/rcPkgSunOS.py 0644 root root 3578 2614 1425832955
1 f none /opt/opensvc/lib/rcPkgWindows.py 0644 root root 5313 13604 1425832955
1 f none /opt/opensvc/lib/rcPrintTable.py 0644 root root 2703 51464 1425832955
1 f none /opt/opensvc/lib/rcScheduler.py 0644 root root 26062 16686 1425832955
1 f none /opt/opensvc/lib/rcStats.py 0644 root root 3977 40974 1425832955
1 f none /opt/opensvc/lib/rcStatsAIX.py 0644 root root 4851 34443 1425832955
1 f none /opt/opensvc/lib/rcStatsDarwin.py 0644 root root 5463 37133 1425832955
1 f none /opt/opensvc/lib/rcStatsFreeBSD.py 0644 root root 5268 21932 1425832955
1 f none /opt/opensvc/lib/rcStatsHP-UX.py 0644 root root 7637 2991 1425832955
1 f none /opt/opensvc/lib/rcStatsLinux.py 0644 root root 14149 14948 1425832955
1 f none /opt/opensvc/lib/rcStatsSunOS.py 0644 root root 8211 11797 1425832955
1 f none /opt/opensvc/lib/rcStatus.py 0644 root root 6674 51623 1425832955
1 f none /opt/opensvc/lib/rcSymmetrix.py 0644 root root 5995 39204 1425832955
1 f none /opt/opensvc/lib/rcSysReport.py 0644 root root 13070 17543 1425832955
1 f none /opt/opensvc/lib/rcSysReportAIX.py 0644 root root 152 13334 1425832955
1 f none /opt/opensvc/lib/rcSysReportDarwin.py 0644 root root 152 13334 1425832955
1 f none /opt/opensvc/lib/rcSysReportFreeBSD.py 0644 root root 152 13334 1425832955
1 f none /opt/opensvc/lib/rcSysReportHP-UX.py 0644 root root 152 13334 1425832955
1 f none /opt/opensvc/lib/rcSysReportLinux.py 0644 root root 152 13334 1425832955
1 f none /opt/opensvc/lib/rcSysReportOSF1.py 0644 root root 152 13334 1425832955
1 f none /opt/opensvc/lib/rcSysReportSunOS.py 0644 root root 152 13334 1425832955
1 f none /opt/opensvc/lib/rcUpdatePkgAIX.py 0644 root root 223 17362 1425832955
1 f none /opt/opensvc/lib/rcUpdatePkgDarwin.py 0644 root root 448 35295 1425832955
1 f none /opt/opensvc/lib/rcUpdatePkgHP-UX.py 0644 root root 264 21363 1425832955
1 f none /opt/opensvc/lib/rcUpdatePkgLinux.py 0644 root root 578 45763 1425832955
1 f none /opt/opensvc/lib/rcUpdatePkgOSF1.py 0644 root root 562 43217 1425832955
1 f none /opt/opensvc/lib/rcUpdatePkgSunOS.py 0644 root root 1563 56611 1425832955
1 f none /opt/opensvc/lib/rcUpdatePkgWindows.py 0644 root root 178 13967 1425832955
1 f none /opt/opensvc/lib/rcUtilities.py 0644 root root 11734 19304 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesAIX.py 0644 root root 326 23568 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesDarwin.py 0644 root root 376 25693 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesFreeBSD.py 0644 root root 292 21393 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesHP-UX.py 0644 root root 262 18527 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesLinux.py 0644 root root 3116 34867 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesOSF1.py 0644 root root 262 18523 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesSunOS.py 0644 root root 1240 24309 1425832955
1 f none /opt/opensvc/lib/rcUtilitiesWindows.py 0644 root root 772 61112 1425832955
1 f none /opt/opensvc/lib/rcVioServer.py 0644 root root 3569 5301 1425832955
1 f none /opt/opensvc/lib/rcWakeOnLan.py 0644 root root 2246 44463 1425832955
1 f none /opt/opensvc/lib/rcWinScheduler.py 0755 root root 2979 45059 1425832955
1 f none /opt/opensvc/lib/rcZfs.py 0644 root root 7698 47344 1425832955
1 f none /opt/opensvc/lib/rcZone.py 0644 root root 4044 52804 1425832955
1 f none /opt/opensvc/lib/resApp.py 0644 root root 13158 34458 1425832955
1 f none /opt/opensvc/lib/resAppVm.py 0644 root root 4068 47480 1425832955
1 f none /opt/opensvc/lib/resContainer.py 0644 root root 9126 12143 1425832955
1 f none /opt/opensvc/lib/resContainerAmazon.py 0644 root root 12072 18588 1425832955
1 f none /opt/opensvc/lib/resContainerDocker.py 0644 root root 6971 56942 1425832955
1 f none /opt/opensvc/lib/resContainerEsx.py 0644 root root 4761 7558 1425832955
1 f none /opt/opensvc/lib/resContainerHpVm.py 0644 root root 5393 61622 1425832955
1 f none /opt/opensvc/lib/resContainerJail.py 0644 root root 4597 63333 1425832955
1 f none /opt/opensvc/lib/resContainerKvm.py 0644 root root 7502 18837 1425832955
1 f none /opt/opensvc/lib/resContainerLdom.py 0644 root root 5615 873 1425832955
1 f none /opt/opensvc/lib/resContainerLxc.py 0644 root root 10073 41520 1425832955
1 f none /opt/opensvc/lib/resContainerOpenstack.py 0644 root root 12426 44722 1425832955
1 f none /opt/opensvc/lib/resContainerOvm.py 0644 root root 6787 22064 1425832955
1 f none /opt/opensvc/lib/resContainerSrp.py 0644 root root 9237 52425 1425832955
1 f none /opt/opensvc/lib/resContainerVbox.py 0644 root root 5252 45861 1425832955
1 f none /opt/opensvc/lib/resContainerVcloud.py 0644 root root 10334 38647 1425832955
1 f none /opt/opensvc/lib/resContainerVz.py 0644 root root 6027 27588 1425832955
1 f none /opt/opensvc/lib/resContainerXen.py 0644 root root 4687 8451 1425832955
1 f none /opt/opensvc/lib/resContainerZone.py 0644 root root 15769 25904 1425832955
1 f none /opt/opensvc/lib/resDg.py 0644 root root 3057 33184 1425832955
1 f none /opt/opensvc/lib/resDrbd.py 0644 root root 6897 45071 1425832955
1 f none /opt/opensvc/lib/resHb.py 0644 root root 2677 3936 1425832955
1 f none /opt/opensvc/lib/resHbLinuxHA.py 0644 root root 2321 43474 1425832955
1 f none /opt/opensvc/lib/resHbOpenHA.py 0644 root root 18073 42701 1425832955
1 f none /opt/opensvc/lib/resHbRhcs.py 0644 root root 2180 28706 1425832955
1 f none /opt/opensvc/lib/resHbSg.py 0644 root root 1972 16627 1425832955
1 f none /opt/opensvc/lib/resHbVcs.py 0644 root root 2057 19361 1425832955
1 f none /opt/opensvc/lib/resHbovm.py 0644 root root 3455 59819 1425832955
1 f none /opt/opensvc/lib/resIp.py 0644 root root 11384 29136 1425832955
1 f none /opt/opensvc/lib/resIpAIX.py 0644 root root 2028 32647 1425832955
1 f none /opt/opensvc/lib/resIpCrossbow.py 0644 root root 6282 56438 1425832955
1 f none /opt/opensvc/lib/resIpDarwin.py 0644 root root 1970 26639 1425832955
1 f none /opt/opensvc/lib/resIpFreeBSD.py 0644 root root 2036 32860 1425832955
1 f none /opt/opensvc/lib/resIpHP-UX.py 0644 root root 2052 31470 1425832955
1 f none /opt/opensvc/lib/resIpLinux.py 0644 root root 2505 2599 1425832955
1 f none /opt/opensvc/lib/resIpOSF1.py 0644 root root 1976 27883 1425832955
1 f none /opt/opensvc/lib/resIpRhcsLinux.py 0644 root root 1422 48717 1425832955
1 f none /opt/opensvc/lib/resIpSgHP-UX.py 0644 root root 1048 20992 1425832955
1 f none /opt/opensvc/lib/resIpSgLinux.py 0644 root root 1048 21150 1425832955
1 f none /opt/opensvc/lib/resIpSunOS.py 0644 root root 1783 16172 1425832955
1 f none /opt/opensvc/lib/resIpVcsLinux.py 0644 root root 1048 21150 1425832955
1 f none /opt/opensvc/lib/resIpZone.py 0644 root root 3588 1103 1425832955
1 f none /opt/opensvc/lib/resLoop.py 0644 root root 2177 27999 1425832955
1 f none /opt/opensvc/lib/resLoopDarwin.py 0644 root root 2993 25547 1425832955
1 f none /opt/opensvc/lib/resLoopFreeBSD.py 0644 root root 3608 1261 1425832955
1 f none /opt/opensvc/lib/resLoopLinux.py 0644 root root 2953 21301 1425832955
1 f none /opt/opensvc/lib/resLvVcsLinux.py 0644 root root 2174 24783 1425832955
1 f none /opt/opensvc/lib/resMount.py 0644 root root 6830 42628 1425832955
1 f none /opt/opensvc/lib/resMountAIX.py 0644 root root 5837 27466 1425832955
1 f none /opt/opensvc/lib/resMountDarwin.py 0644 root root 8137 52994 1425832955
1 f none /opt/opensvc/lib/resMountFreeBSD.py 0644 root root 6204 55654 1425832955
1 f none /opt/opensvc/lib/resMountHP-UX.py 0644 root root 4444 60922 1425832955
1 f none /opt/opensvc/lib/resMountLinux.py 0644 root root 12549 29714 1425832955
1 f none /opt/opensvc/lib/resMountOSF1.py 0644 root root 5737 17478 1425832955
1 f none /opt/opensvc/lib/resMountRhcsLinux.py 0644 root root 2053 13211 1425832955
1 f none /opt/opensvc/lib/resMountSgHP-UX.py 0644 root root 2455 42162 1425832955
1 f none /opt/opensvc/lib/resMountSgLinux.py 0644 root root 2465 43353 1425832955
1 f none /opt/opensvc/lib/resMountSunOS.py 0644 root root 6492 60327 1425832955
1 f none /opt/opensvc/lib/resMountVcsLinux.py 0644 root root 2439 41446 1425832955
1 f none /opt/opensvc/lib/resScsiReserv.py 0644 root root 7186 1660 1425832955
1 s none /opt/opensvc/lib/resScsiReservFreeBSD.py=resScsiReservLinux.py
1 f none /opt/opensvc/lib/resScsiReservHP-UX.py 0644 root root 6160 60991 1425832955
1 f none /opt/opensvc/lib/resScsiReservLinux.py 0644 root root 5489 27649 1425832955
1 f none /opt/opensvc/lib/resScsiReservOSF1.py 0644 root root 7304 14679 1425832955
1 s none /opt/opensvc/lib/resScsiReservSunOS.py=resScsiReservLinux.py
1 f none /opt/opensvc/lib/resShareNfsHP-UX.py 0644 root root 5171 41165 1425832955
1 f none /opt/opensvc/lib/resShareNfsLinux.py 0644 root root 5925 35062 1425832955
1 f none /opt/opensvc/lib/resStonith.py 0644 root root 2822 4550 1425832955
1 f none /opt/opensvc/lib/resStonithCallout.py 0644 root root 857 53189 1425832955
1 f none /opt/opensvc/lib/resStonithIlo.py 0644 root root 976 63084 1425832955
1 f none /opt/opensvc/lib/resSync.py 0644 root root 5220 56470 1425832955
1 f none /opt/opensvc/lib/resSyncBtrfs.py 0644 root root 16897 18849 1425832955
1 f none /opt/opensvc/lib/resSyncDcs.py 0644 root root 5659 27223 1425832955
1 f none /opt/opensvc/lib/resSyncDcsCkpt.py 0644 root root 8045 3536 1425832955
1 f none /opt/opensvc/lib/resSyncDcsSnap.py 0644 root root 7416 59619 1425832955
1 f none /opt/opensvc/lib/resSyncDds.py 0644 root root 15473 19354 1425832955
1 f none /opt/opensvc/lib/resSyncDocker.py 0644 root root 6904 51279 1425832955
1 f none /opt/opensvc/lib/resSyncEvasnap.py 0644 root root 11551 63113 1425832955
1 f none /opt/opensvc/lib/resSyncHp3par.py 0644 root root 12755 21918 1425832955
1 f none /opt/opensvc/lib/resSyncIbmdsSnap.py 0644 root root 9644 35588 1425832955
1 f none /opt/opensvc/lib/resSyncNecIsmSnap.py 0644 root root 6708 26822 1425832955
1 f none /opt/opensvc/lib/resSyncNetapp.py 0644 root root 11342 40671 1425832955
1 f none /opt/opensvc/lib/resSyncNexenta.py 0644 root root 9512 39422 1425832955
1 f none /opt/opensvc/lib/resSyncRados.py 0644 root root 11710 40127 1425832955
1 f none /opt/opensvc/lib/resSyncRsync.py 0644 root root 16703 46432 1425832955
1 f none /opt/opensvc/lib/resSyncSymSrdfS.py 0644 root root 17531 54039 1425832955
1 f none /opt/opensvc/lib/resSyncSymclone.py 0644 root root 9990 4431 1425832955
1 f none /opt/opensvc/lib/resSyncSymcloneLinux.py 0644 root root 3301 40090 1425832955
1 f none /opt/opensvc/lib/resSyncZfs.py 0644 root root 14090 60203 1425832955
1 f none /opt/opensvc/lib/resVdisk.py 0644 root root 3235 42108 1425832955
1 f none /opt/opensvc/lib/resVgAIX.py 0644 root root 9752 30190 1425832955
1 f none /opt/opensvc/lib/resVgAdvfs.py 0644 root root 4402 60949 1425832955
1 f none /opt/opensvc/lib/resVgGandi.py 0644 root root 7562 13780 1425832955
1 f none /opt/opensvc/lib/resVgHP-UX.py 0644 root root 13216 22548 1425832955
1 f none /opt/opensvc/lib/resVgHpVm.py 0644 root root 5648 4552 1425832955
1 f none /opt/opensvc/lib/resVgLdom.py 0644 root root 4568 51096 1425832955
1 f none /opt/opensvc/lib/resVgLinux.py 0644 root root 6837 19340 1425832955
1 f none /opt/opensvc/lib/resVgRadosLinux.py 0644 root root 9932 52020 1425832955
1 f none /opt/opensvc/lib/resVgRaw.py 0644 root root 4782 9736 1425832955
1 f none /opt/opensvc/lib/resVgRawHP-UX.py 0644 root root 2109 19249 1425832955
1 f none /opt/opensvc/lib/resVgRawLinux.py 0644 root root 12174 8974 1425832955
1 f none /opt/opensvc/lib/resVgRawSunOS.py 0644 root root 2662 49868 1425832955
1 f none /opt/opensvc/lib/resVgSgHP-UX.py 0644 root root 950 13388 1425832955
1 f none /opt/opensvc/lib/resVgSgLinux.py 0644 root root 950 13546 1425832955
1 f none /opt/opensvc/lib/resVgVcsLinux.py 0644 root root 1338 40853 1425832955
1 f none /opt/opensvc/lib/resVgVeritas.py 0644 root root 4632 62944 1425832955
1 f none /opt/opensvc/lib/resVgZfs.py 0644 root root 6490 7747 1425832955
1 f none /opt/opensvc/lib/resources.py 0644 root root 22316 61599 1425832955
1 f none /opt/opensvc/lib/snap.py 0644 root root 5147 59907 1425832955
1 f none /opt/opensvc/lib/snapAdvfsOSF1.py 0644 root root 3895 40126 1425832955
1 f none /opt/opensvc/lib/snapJfs2AIX.py 0644 root root 4927 36941 1425832955
1 f none /opt/opensvc/lib/snapLvmLinux.py 0644 root root 5247 61023 1425832955
1 f none /opt/opensvc/lib/snapVxfsHP-UX.py 0644 root root 3570 10516 1425832955
1 f none /opt/opensvc/lib/snapZfsSunOS.py 0644 root root 2189 43488 1425832955
1 f none /opt/opensvc/lib/svc.py 0644 root root 91849 61196 1425832955
1 f none /opt/opensvc/lib/svcBuilder.py 0644 root root 96408 34844 1425832955
1 f none /opt/opensvc/lib/svcDict.py 0644 root root 103127 54302 1425832955
1 f none /opt/opensvc/lib/svcHosted.py 0644 root root 1757 7650 1425832955
1 f none /opt/opensvc/lib/svcRhcs.py 0644 root root 6463 19758 1425832955
1 f none /opt/opensvc/lib/svcSg.py 0644 root root 10281 60493 1425832955
1 f none /opt/opensvc/lib/svcVcs.py 0644 root root 7670 56775 1425832955
1 f none /opt/opensvc/lib/tabulate.py 0644 root root 26346 12381 1425832955
1 f none /opt/opensvc/lib/version.py 0644 root root 20 1321 1427028617
1 f none /opt/opensvc/lib/wmi.py 0644 root root 46934 3064 1425832955
1 f none /opt/opensvc/lib/xmlrpcClient.py 0644 root root 50957 49815 1425832955
1 d none /opt/opensvc/usr 0755 root root
1 d none /opt/opensvc/usr/share 0755 root root
1 f none /opt/opensvc/usr/share/AUTHORS 0644 root root 48 4634 1425832955
1 f none /opt/opensvc/usr/share/BANNER 0644 root root 828 5079 1425832955
1 f none /opt/opensvc/usr/share/COPYING 0644 root root 26436 9725 1425832955
1 d none /opt/opensvc/usr/share/bash_completion.d 0755 root root
1 f none /opt/opensvc/usr/share/bash_completion.d/opensvc 0644 root root 10602 52978 1425832955
1 d none /opt/opensvc/usr/share/doc 0755 root root
1 f none /opt/opensvc/usr/share/doc/node.conf 0644 root root 8698 15261 1425832955
1 f none /opt/opensvc/usr/share/doc/opensvc.haresource 0644 root root 506 43100 1425832955
1 d none /opt/opensvc/usr/share/doc/provisioning 0755 root root
1 f none /opt/opensvc/usr/share/doc/provisioning/provisioning.agent.debian 0755 root root 2602 50922 1425832955
1 f none /opt/opensvc/usr/share/doc/provisioning/provisioning.example 0644 root root 362 30449 1425832955
1 f none /opt/opensvc/usr/share/doc/template.DEFAULT.env.gz 0644 root root 3565 59644 1425832955
1 f none /opt/opensvc/usr/share/doc/template.app.env.gz 0644 root root 1280 30407 1425832955
1 f none /opt/opensvc/usr/share/doc/template.comp_module.py 0755 root root 1235 30145 1425832955
1 f none /opt/opensvc/usr/share/doc/template.comp_module.sh 0755 root root 488 35015 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.amazon.env.gz 0644 root root 1286 26857 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.docker.env.gz 0644 root root 1315 35413 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.esx.env.gz 0644 root root 1201 18006 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.hpvm.env.gz 0644 root root 1110 10555 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.jail.env.gz 0644 root root 1196 19782 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.kvm.env.gz 0644 root root 1240 30763 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.ldom.env.gz 0644 root root 1110 8724 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.lxc.env.gz 0644 root root 1247 25013 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.openstack.env.gz 0644 root root 1290 34040 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.ovm.env.gz 0644 root root 1282 28418 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.srp.env.gz 0644 root root 1218 21114 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.vbox.env.gz 0644 root root 1110 8594 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.vcloud.env.gz 0644 root root 1284 28245 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.vz.env.gz 0644 root root 1186 19803 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.xen.env.gz 0644 root root 1239 26615 1425832955
1 f none /opt/opensvc/usr/share/doc/template.container.zone.env.gz 0644 root root 1289 33668 1425832955
1 f none /opt/opensvc/usr/share/doc/template.disk.env.gz 0644 root root 987 60441 1425832955
1 f none /opt/opensvc/usr/share/doc/template.drbd.env.gz 0644 root root 1368 40148 1425832955
1 f none /opt/opensvc/usr/share/doc/template.fs.env.gz 0644 root root 1481 58294 1425832955
1 f none /opt/opensvc/usr/share/doc/template.hb.LinuxHA.env.gz 0644 root root 1010 64549 1425832955
1 f none /opt/opensvc/usr/share/doc/template.hb.OpenHA.env.gz 0644 root root 1047 63294 1425832955
1 f none /opt/opensvc/usr/share/doc/template.ip.crossbow.env.gz 0644 root root 1482 59360 1425832955
1 f none /opt/opensvc/usr/share/doc/template.ip.env.gz 0644 root root 1523 1951 1425832955
1 f none /opt/opensvc/usr/share/doc/template.loop.env.gz 0644 root root 1074 4071 1425832955
1 f none /opt/opensvc/usr/share/doc/template.pool.env.gz 0644 root root 1098 6323 1425832955
1 f none /opt/opensvc/usr/share/doc/template.share.nfs.env.gz 0644 root root 1106 6140 1425832955
1 f none /opt/opensvc/usr/share/doc/template.stonith.callout.env.gz 0644 root root 318 41384 1425832955
1 f none /opt/opensvc/usr/share/doc/template.stonith.ilo.env.gz 0644 root root 282 35610 1425832955
1 f none /opt/opensvc/usr/share/doc/template.subset.env.gz 0644 root root 247 29619 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.btrfs.env.gz 0644 root root 1390 48127 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.dcsckpt.env.gz 0644 root root 1497 57299 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.dcssnap.env.gz 0644 root root 1432 48348 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.dds.env.gz 0644 root root 1824 34156 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.docker.env.gz 0644 root root 1321 39660 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.evasnap.env.gz 0644 root root 1502 60038 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.hp3par.env.gz 0644 root root 1499 57844 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.ibmdssnap.env.gz 0644 root root 1526 64285 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.necismsnap.env.gz 0644 root root 1294 29464 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.netapp.env.gz 0644 root root 1477 52069 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.rsync.env.gz 0644 root root 2066 4989 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.symclone.env.gz 0644 root root 1671 13035 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.symsrdfs.env.gz 0644 root root 1373 41877 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sync.zfs.env.gz 0644 root root 1619 3566 1425832955
1 f none /opt/opensvc/usr/share/doc/template.sysreport.conf 0644 root root 765 66 1425832955
1 f none /opt/opensvc/usr/share/doc/template.vdisk.env.gz 0644 root root 1081 2818 1425832955
1 f none /opt/opensvc/usr/share/doc/template.vg.rados.env.gz 0644 root root 1679 20447 1425832955
1 f none /opt/opensvc/usr/share/doc/template.vg.raw.env.gz 0644 root root 1509 56599 1425832955
1 f none /opt/opensvc/usr/share/doc/template.vg.veritas.env.gz 0644 root root 1417 48821 1425832955
1 f none /opt/opensvc/usr/share/doc/template.vmdg.ldom.env.gz 0644 root root 1330 38418 1425832955
1 d none /opt/opensvc/var 0755 root root
1 d none /opt/opensvc/var/compliance 0755 root root
1 d none /opt/opensvc/var/compliance/com.opensvc 0755 root root
1 f none /opt/opensvc/var/compliance/com.opensvc/authkeys.py 0755 root root 10838 55159 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/bios.py 0755 root root 2407 41835 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/chkconfig.py 0755 root root 3564 59594 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/comp.py 0644 root root 1204 31330 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/cron.py 0755 root root 7506 56105 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/etcsystem.py 0755 root root 7230 40930 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/fileinc.py 0755 root root 9977 16664 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/files.py 0755 root root 11740 44318 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/firmware.py 0755 root root 6763 26070 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/fs.py 0755 root root 22394 44414 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/groups.py 0755 root root 8321 6737 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/groups_membership.py 0755 root root 7911 61412 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/keyval.py 0755 root root 9869 30515 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/keyval_parser.py 0755 root root 4748 6236 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/linux.mpath.py 0755 root root 15901 52996 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/nodeconf.py 0755 root root 5854 19067 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/packages.py 0755 root root 25838 24307 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/process.py 0755 root root 13637 1086 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/rc.py 0755 root root 4632 4871 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/smfcfgs.py 0755 root root 13308 26086 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/sudoers.py 0755 root root 2443 51581 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/svcconf.py 0755 root root 10896 44880 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/sysctl.py 0755 root root 7934 34101 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/sysvinit.py 0755 root root 7805 31271 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/users.py 0755 root root 15491 34495 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/utilities.py 0644 root root 1683 8796 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/vuln.py 0755 root root 23404 13276 1425832955
1 f none /opt/opensvc/var/compliance/com.opensvc/xinetd.py 0755 root root 5782 9680 1425832955
1 i pkginfo 216 17767 1427028617
1 i postinstall 473 42424 1427028617
   0707010000bcf3000041ed000000000000000000000002550eba8b00000000000000660000000300000000000000000000000800000000install   0707010000bcf4000081a4000000000000000000000001550eba89000001d9000000660000000300000000000000000000001400000000install/postinstall   #!/sbin/sh

su root -c "LD_LIBRARY_PATH=/opt/opensvc/usr/local/lib:$LD_LIBRARY_PATH PATH=${PKG_INSTALL_ROOT}/opt/opensvc/usr/local/bin:/usr/local/bin:/export/home/opensvc/bin/pkg:/usr/sbin:/usr/bin:/usr/local/bin:/bin:/usr/ccs/bin:/usr/sfw/bin:/opt/sfw/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/etc:/usr/dt/bin:/usr/ucb:/usr/symcli/bin:/opt/opensvc/etc:/opt/opensvc/bin:/opt/sfw/bin:/usr/sfw/bin:/usr/local/bin ${PKG_INSTALL_ROOT}/opt/opensvc/bin/postinstall"
   0707010000b7fe000041ed000000000000000000000003550eba8900000000000000660000000300000000000000000000000500000000root  0707010000b816000041ed000000000000000000000003550eba8900000000000000660000000300000000000000000000000900000000root/opt  0707010000b817000041ed000000000000000000000006550eba8b00000000000000660000000300000000000000000000001100000000root/opt/opensvc  0707010000b818000041ed000000000000000000000003550eba8900000000000000660000000300000000000000000000001500000000root/opt/opensvc/bin  0707010000b819000041ed000000000000000000000002550eba8900000000000000660000000300000000000000000000001a00000000root/opt/opensvc/bin/cron 0707010000b81a000081ed00000000000000000000000154fc7bfb00000601000000660000000300000000000000000000002200000000root/opt/opensvc/bin/cron/opensvc #!/opt/opensvc/bin/python
#
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import sys
import os

"""
When installed in /etc/cron.daily/ this must be a symlink to the
file in the opensvc distrib, wherever installed, so that we can compute
relative paths to locate libs
"""

pathsvc = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..'))
pathlib = os.path.join(pathsvc, 'lib')
sys.path.append(pathlib)

import node
n = node.Node()
n.options.cron = True

for action in n.scheduler_actions:
    try:
        n.action(action)
    except:
        import traceback
        traceback.print_exc()

for s in n.svcs:
    s.cron = True
    try:
        s.action('compliance_auto')
    except:
        import traceback
        traceback.print_exc()

n.close()
   0707010000b81b000081a400000000000000000000000154fc7bfb0000024a000000660000000300000000000000000000003500000000root/opt/opensvc/bin/darwin.com.opensvc.svcmgr.plist  <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>com.opensvc.svcmgr</string>
	<key>ProgramArguments</key>
	<array>
		<string>/opt/opensvc/bin/opensvc.init.Darwin</string>
		<string>start</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
	<key>StandardErrorPath</key>
	<string>/opt/opensvc/log/svcmgr_boot_stderr.log</string>
	<key>StandardOutPath</key>
	<string>/opt/opensvc/log/svcmgr_boot_stdout.log</string>
</dict>
</plist>
  0707010000b81c000081ed00000000000000000000000154fc7bfb00001cc0000000660000000300000000000000000000001d00000000root/opt/opensvc/bin/nodemgr  #!/usr/bin/env /opt/opensvc/bin/python
#
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import sys
import os
import optparse

#
# add project lib to path
#
pathsvc = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
pathetc = os.path.join(pathsvc, 'etc')
sys.path.append(os.path.join(pathsvc, 'lib'))
prog = os.path.basename(__file__)

import rcExceptions as ex
from rcGlobalEnv import *
from rcUtilities import check_privs, ximport

check_privs()

node_mod = ximport('node')

try:
    from version import version
except:
    version = "dev"

n = node_mod.Node()

__ver = prog + " version " + version
__usage = "%prog [options] command\n\n"
parser = optparse.OptionParser(version=__ver, usage=__usage + n.format_desc())
parser.add_option("--debug", default=False,
		  action="store_true", dest="debug",
                  help="debug mode")
parser.add_option("--stats-dir", default=None,
		  action="store", dest="stats_dir",
                  help="points the directory where the metrics files are stored for pushstats")
parser.add_option("--module", default="",
		  action="store", dest="module",
                  help="compliance, set module list")
parser.add_option("--moduleset", default="",
		  action="store", dest="moduleset",
                  help="compliance, set moduleset list. The 'all' value can be used in conjonction with detach.")
parser.add_option("--ruleset", default="",
		  action="store", dest="ruleset",
                  help="compliance, set ruleset list. The 'all' value can be used in conjonction with detach.")
parser.add_option("--filterset", default="",
		  action="store", dest="filterset",
                  help="set a filterset to limit collector extractions")
parser.add_option("--ruleset-date", default="",
		  action="store", dest="ruleset_date",
                  help="compliance, use rulesets valid on specified date")
parser.add_option("--attach", default=False,
		  action="store_true", dest="attach",
                  help="attach the modulesets specified during a compliance check/fix/fixable command")
parser.add_option("--cron", default=False,
		  action="store_true", dest="cron",
                  help="cron mode")
parser.add_option("--force", default=False,
		  action="store_true", dest="force",
                  help="force action")
parser.add_option("--symcli-db-file", default=None,
		  action="store", dest="symcli_db_file",
                  help="[pushsym option] use symcli offline mode with the specified file. aclx files are expected to be found in the same directory and named either <symid>.aclx or <same_prefix_as_bin_file>.aclx")
parser.add_option("--param", default=None,
		  action="store", dest="param",
                  help="point a node configuration parameter for the 'get' and 'set' actions")
parser.add_option("--value", default=None,
		  action="store", dest="value",
                  help="set a node configuration parameter value for the 'set --param' action")
parser.add_option("--duration", default=None, action="store", dest="duration", type="int",
                  help="a duration expressed in minutes. used with the 'collector ack action' action")
parser.add_option("--begin", default=None, action="store", dest="begin",
                  help="a begin date expressed as 'YYYY-MM-DD hh:mm'. used with the 'collector ack action' and pushstats action")
parser.add_option("--end", default=None, action="store", dest="end",
                  help="a end date expressed as 'YYYY-MM-DD hh:mm'. used with the 'collector ack action' and pushstats action")
parser.add_option("--comment", default=None, action="store", dest="comment",
                  help="a comment to log when used with the 'collector ack action' action")
parser.add_option("--author", default=None, action="store", dest="author",
                  help="the acker name to log when used with the 'collector ack action' action")
parser.add_option("--id", default=0, action="store", dest="id", type="int",
                  help="specify an id to act on")
parser.add_option("--resource", default=[], action="append",
                  help="a resource definition in json dictionary format fed to the provision action")
parser.add_option("--object", default=[], action="append", dest="objects",
                  help="an object to limit a push* action to. multiple --object <object id> parameters can be set on a single command line")
parser.add_option("--mac", default=None,
		  action="store", dest="mac",
                  help="list of mac addresses, comma separated, used by the 'wol' action")
parser.add_option("--tag", default=None,
		  action="store", dest="tag",
                  help="a tag specifier used by 'collector create tag', 'collector add tag', 'collector del tag'")
parser.add_option("--like", default="%",
		  action="store", dest="like",
                  help="a sql like filtering expression. leading and trailing wildcards are automatically set.")
parser.add_option("--broadcast", default=None,
		  action="store", dest="broadcast",
                  help="list of broadcast addresses, comma separated, used by the 'wol' action")
parser.add_option("--sync", default=False, action="store_true", dest="syncrpc",
                  help="use synchronous collector rpc if available. to use with pushasset when chaining a compliance run, to make sure the node ruleset is up-to-date.")
parser.add_option("--table", default=False, action="store_true", dest="table",
                  help="used table representation of collector data instead of the default itemized list of objects and properties")


(options, args) = parser.parse_args()

n.options = options

def do_symcli_db_file(symcli_db_file):
    if symcli_db_file is None:
        return
    if not os.path.exists(symcli_db_file):
        print("File does not exist: %s"%symcli_db_file)
        return
    os.environ['SYMCLI_DB_FILE'] = symcli_db_file
    os.environ['SYMCLI_OFFLINE'] = '1'
    
do_symcli_db_file(options.symcli_db_file)

if len(args) is 0:
    n.close()
    parser.error("Missing action")
action = '_'.join(args)
if not action in n.supported_actions():
    n.close()
    parser.set_usage(__usage + n.format_desc(action))
    parser.error("unsupported action: %s"%action)

def _exit(r):
    n.close()
    sys.exit(r)

err = 0
try:
    err = n.action(action)
except KeyboardInterrupt:
    sys.stderr.write("Keybord Interrupt\n")
    err = 1
except ex.excError:
    import traceback
    exc_type, exc_value, exc_traceback = sys.exc_info()
    sys.stderr.write(str(exc_value)+'\n')
    err = 1
except:
    raise
    err = 1

_exit(err)
0707010000b81d000081ed00000000000000000000000154fc7bfb000000f8000000660000000300000000000000000000002600000000root/opt/opensvc/bin/opensvc.init.AIX #!/bin/ksh

osvc_path=/opt/opensvc/bin
osvc_opts="--parallel"
allservices=${osvc_path}/allservices

case $1 in
start)
        ${osvc_path}/nodemgr pushasset
	${allservices} ${osvc_opts} boot
	;;
stop)
	${allservices} ${osvc_opts} shutdown
	;;
esac
0707010000b81e000081ed00000000000000000000000154fc7bfb000002e3000000660000000300000000000000000000002900000000root/opt/opensvc/bin/opensvc.init.Darwin  #!/bin/bash
#
# Starts the services driven by OpenSVC
#
# description: Starts the services driven by OpenSVC whose
#              autostart node is this node.
# processname:

PATH=/usr/bin:/usr/sbin:$PATH

DEFAULTS="/etc/defaults/opensvc"

# Include opensvc defaults if available
[ -r "$DEFAULTS" ] && . "$DEFAULTS"

osvc_path=/opt/opensvc/bin
osvc_opts=${osvc_opts:---parallel}
allservices=${osvc_path}/allservices

case $1 in
start)
        ipconfig waitall
        echo "OpenSVC : Pushing node information"
        ${osvc_path}/nodemgr pushasset
        echo
        echo "OpenSVC : Starting Services"
	${allservices} ${osvc_opts} boot
	;;
stop)
        echo "OpenSVC : Stopping Services"
	${allservices} ${osvc_opts} shutdown
	;;
esac
 0707010000b81f000081ed00000000000000000000000154fc7bfb000001ce000000660000000300000000000000000000002a00000000root/opt/opensvc/bin/opensvc.init.FreeBSD #!/bin/sh
#

# PROVIDE: opensvc
# REQUIRE: LOGIN sshd cleanvar
# BEFORE: 
# KEYWORD: shutdown

DEFAULTS="/etc/defaults/opensvc"

# Include opensvc defaults if available
[ -r "$DEFAULTS" ] && . "$DEFAULTS"

osvc_path=/opt/opensvc/bin
osvc_opts=${osvc_opts:---parallel}
allservices=${osvc_path}/allservices

case $1 in
faststart)
        ${osvc_path}/nodemgr pushasset
	${allservices} ${osvc_opts} boot
	;;
faststop)
	${allservices} ${osvc_opts} shutdown
	;;
esac
  0707010000b820000081ed00000000000000000000000154fc7bfb000000e5000000660000000300000000000000000000002700000000root/opt/opensvc/bin/opensvc.init.OSF1    #!/usr/bin/ksh

osvc_path=/opt/opensvc/bin
allservices=${osvc_path}/allservices

case $1 in
start)
        ${osvc_path}/nodemgr pushasset
	${allservices} ${osvc_opts} boot
	;;
stop)
	${allservices} ${osvc_opts} shutdown
	;;
esac
   0707010000b821000081ed00000000000000000000000154fc7bfb0000022d000000660000000300000000000000000000002800000000root/opt/opensvc/bin/opensvc.init.SunOS   #!/bin/bash
#
#	/etc/init.d/opensvc
#
# Starts the services driven by OpenSVC
#
# description: Starts the services driven by OpenSVC whose
#              autostart node is this node.
# processname:

DEFAULTS="/etc/default/opensvc"

# Include opensvc defaults if available
[ -r "$DEFAULTS" ] && . "$DEFAULTS"

osvc_path=/opt/opensvc/bin
osvc_opts=${osvc_opts:---parallel}
allservices=${osvc_path}/allservices

case $1 in
start)
        ${osvc_path}/nodemgr pushasset
	${allservices} ${osvc_opts} boot
	;;
stop)
	${allservices} ${osvc_opts} shutdown
	;;
esac
   0707010000b822000081ed00000000000000000000000154fc7bfb00000267000000660000000300000000000000000000002900000000root/opt/opensvc/bin/opensvc.init.debian  #!/bin/sh -e

### BEGIN INIT INFO
# Provides:             opensvc
# Required-Start:       $all
# Required-Stop:        
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    OpenSVC services startup script
### END INIT INFO

DEFAULTS="/etc/default/opensvc"

# Include opensvc defaults if available
[ -r "$DEFAULTS" ] && . "$DEFAULTS"

osvc_path=/opt/opensvc/bin
osvc_opts=${osvc_opts:---parallel}
allservices=${osvc_path}/allservices

case $1 in
start)
        ${osvc_path}/nodemgr pushasset
	${allservices} ${osvc_opts} boot
	;;
stop)
	${allservices} ${osvc_opts} shutdown
	;;
esac
 0707010000b828000081ed00000000000000000000000154fc7bfb00000327000000660000000300000000000000000000002700000000root/opt/opensvc/bin/opensvc.init.hpux    #!/bin/sh

PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

if [ -r /etc/rc.config.d/opensvc ] ; then 
    . /etc/rc.config.d/opensvc
fi

osvc_path=/opt/opensvc/bin
osvc_opts="--parallel"
allservices=${osvc_path}/allservices
agt=${osvc_path}/perfagt.`uname -s`

case $1 in
start_msg)
	if [ "$RUN_OPENSVC" -ne 0 ] ; then
		echo "Starting opensvc services"
	fi
	;;
start)
	if [ "$RUN_OPENSVC" -ne 0 ] ; then
		echo "Starting opensvc services"
	else
		exit 0
	fi
	if [ -x $agt ] ; then
		$agt
	fi
        ${osvc_path}/nodemgr pushasset
	${allservices} ${osvc_opts} boot
	;;
stop_msg)
	if [ "$RUN_OPENSVC" -ne 0 ] ; then
		echo "Shutting down opensvc services"
	fi
	;;
stop)
	if [ "$RUN_OPENSVC" -ne 0 ] ; then
		echo "Starting opensvc services"
	else
		exit 0
	fi
	${allservices} ${osvc_opts} shutdown
	;;
esac
 0707010000b829000081ed00000000000000000000000154fc7bfb000002d5000000660000000300000000000000000000002900000000root/opt/opensvc/bin/opensvc.init.redhat  #!/bin/bash
#
#	/etc/rc.d/init.d/opensvc
#
# Starts the services driven by OpenSVC
#
# chkconfig: 2345 99 01
# description: Starts the services driven by OpenSVC whose
#              autostart node is this node.
# processname:

# Source function library.
. /etc/init.d/functions

DEFAULTS="/etc/sysconfig/opensvc"

# Include opensvc defaults if available
[ -r "$DEFAULTS" ] && . "$DEFAULTS"

osvc_path=/opt/opensvc/bin
osvc_opts=${osvc_opts:---parallel}
allservices=${osvc_path}/allservices

case $1 in
start)
	${osvc_path}/nodemgr pushasset
	${allservices} ${osvc_opts} boot
	[ -d /var/lock/subsys ] && touch /var/lock/subsys/opensvc
	;;
stop)
	${allservices} ${osvc_opts} shutdown
	rm -f /var/lock/subsys/opensvc
	;;
esac

   0707010000b975000081ed00000000000000000000000154fc7bfb00000269000000660000000300000000000000000000002700000000root/opt/opensvc/bin/opensvc.init.suse    #!/bin/sh -e

### BEGIN INIT INFO
# Provides:             opensvc
# Required-Start:       $all
# Required-Stop:        
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    OpenSVC services startup script
### END INIT INFO

DEFAULTS="/etc/sysconfig/opensvc"

# Include opensvc defaults if available
[ -r "$DEFAULTS" ] && . "$DEFAULTS"

osvc_path=/opt/opensvc/bin
osvc_opts=${osvc_opts:---parallel}
allservices=${osvc_path}/allservices

case $1 in
start)
        ${osvc_path}/nodemgr pushasset
	${allservices} ${osvc_opts} boot
	;;
stop)
	${allservices} ${osvc_opts} shutdown
	;;
esac
   0707010000b976000081ed00000000000000000000000154fc7bfb000009db000000660000000300000000000000000000002400000000root/opt/opensvc/bin/perfagt.Darwin   #!/opt/opensvc/bin/python

import os
import sys
import datetime

pathsvc = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(os.path.join(pathsvc, 'lib'))

import node
from rcUtilities import justcall

node = node.Node()
now = datetime.datetime.now()

def fs_u():
    vars = ['date',
            'nodename',
            'mntpt',
            'size',
            'used']

    cmd = ['df', '-lkP']
    (out, err, ret) = justcall(cmd)
    if ret != 0:
        return self.undef
    lines = out.split('\n')
    if len(lines) < 2:
        return self.undef
    vals = []
    for line in lines[1:]:
        l = line.split()
        if len(l) != 6:
            continue
        if l[5].startswith('/Volumes'):
            # Darwin automount package files under /Volumes
            continue
        vals.append([str(now), node.nodename, l[5], l[1], l[4].replace('%','')])
    return (vars, vals)

def mem_u():
    basedir = os.path.join(pathsvc, 'var', 'stats')
    if not os.path.exists(basedir):
        os.makedirs(basedir)
    fname = os.path.join(basedir, 'mem_u%0.2d'%now.day)
    if not os.path.exists(fname):
        try:
            f = open(fname, 'w')
        except:
            return
    else:
        mtime = os.stat(fname).st_mtime
        if datetime.datetime.fromtimestamp(mtime) < now - datetime.timedelta(days=1):
            os.unlink(fname)
            try:
                f = open(fname, 'w')
            except:
                return
        else:
            try:
                f = open(fname, 'a')
            except:
                return
 

    cmd = ['/usr/sbin/sysctl', '-n', 'hw.pagesize']
    (out, err, ret) = justcall(cmd)
    if ret != 0:
        return
    pagesize = int(out.split()[0])

    cmd = ['vm_stat']
    (out, err, ret) = justcall(cmd)
    if ret != 0:
        return
    h = {}
    for line in out.split('\n'):
        l = line.split(':')
        if len(l) != 2:
            continue
        key = l[0]
        try:
            val = int(l[1].strip(' .'))
        except:
            continue
        h[key] = val
    f.write(' '.join((now.strftime('%H:%M:%S'),
                      str(h['Pages free']*pagesize/1024),
                      str(h['Pages active']*pagesize/1024),
                      str(h['Pages inactive']*pagesize/1024),
                      str(h['Pages speculative']*pagesize/1024),
                      str(h['Pages wired down']*pagesize/1024)
            ))+'\n')


node.collector.call('push_stats_fs_u', fs_u())
mem_u()
 0707010000b977000081ed00000000000000000000000154fc7bfb00000c67000000660000000300000000000000000000002300000000root/opt/opensvc/bin/perfagt.HP-UX    #!/opt/opensvc/bin/python

import os
import sys
import datetime
from subprocess import *

pathsvc = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(os.path.join(pathsvc, 'lib'))

import node
from rcUtilities import justcall, is_exe

node = node.Node()
now = str(datetime.datetime.now())

def fs_u():
    vars = ['date',
            'nodename',
            'mntpt',
            'size',
            'used']

    cmd = ['df', '-lP']
    (out, err, ret) = justcall(cmd)
    if ret != 0:
        return
    lines = out.split('\n')
    if len(lines) < 2:
        return
    vals = []
    for line in lines[1:]:
        l = line.split()
        if len(l) != 6:
            continue
        vals.append([now, node.nodename, l[5], l[1], l[4].replace('%','')])
    return (vars, vals)

def glance_running(cmd_str):
    (out, err, ret) = justcall(['ps', '-ef'])
    if ret != 0:
        print('ps error')
        return

    for line in out.split('\n'):
        l = line.split()
        if len(l) < 6:
            continue
        if cmd_str in ' '.join(l[6:]):
            return True

    return False

def run_glance():
    glance = '/opt/perf/bin/glance'
    syn = '/opt/opensvc/tmp/glance.syntax'
    now = datetime.datetime.now()
    iterations = (23-now.hour)*6+(60-now.minute)//10
    cmd = ['/opt/perf/bin/glance', '-aos', syn, '-j', '600', '-iterations']
    cmd_str = ' '.join(cmd)

    if not is_exe(glance):
        print('glance executable not found')
        return

    if glance_running(cmd_str):
        print('glance is already running')
        return

    buff = """print GBL_STATTIME," ",
// usr
0.00+GBL_CPU_NORMAL_UTIL+GBL_CPU_REALTIME_UTIL," ",
// nice
0.00+GBL_CPU_NICE_UTIL+GBL_CPU_NNICE_UTIL," ",
// sys
0.00+GBL_CPU_SYSCALL_UTIL+GBL_CPU_CSWITCH_UTIL+GBL_CPU_TRAP_UTIL+GBL_CPU_VFAULT_UTIL," ",
// irq
0.00+GBL_CPU_INTERRUPT_UTIL," ",
// wait
0.00+GBL_CPU_WAIT_UTIL," ",
// idle
0.00+GBL_CPU_IDLE_UTIL-GBL_CPU_WAIT_UTIL," ",

// mem
0+GBL_MEM_PHYS," ",
0+GBL_MEM_FREE," ",
0+GBL_MEM_CACHE," ",
0+GBL_MEM_FILE_PAGE_CACHE," ",
0+GBL_MEM_SYS," ",
0+GBL_MEM_USER," ",

// swap
0+GBL_MEM_SWAP," ",
0+GBL_SWAP_SPACE_AVAIL-GBL_MEM_PHYS," ",

// load
GBL_LOADAVG," ",
GBL_LOADAVG5," ",
GBL_LOADAVG15," ",
GBL_CPU_QUEUE," ",

// process list
TBL_PROC_TABLE_USED," ",

// disk io
GBL_DISK_PHYS_READ_RATE," ",
GBL_DISK_PHYS_WRITE_RATE," ",

// disk kB/s
GBL_DISK_PHYS_READ_BYTE_RATE," ",
GBL_DISK_PHYS_WRITE_BYTE_RATE
"""
    try:
        with open(syn, 'w') as f:
            f.write(buff)
    except:
        print('error writing %s'%syn)
        return

    collect_d = '/opt/opensvc/var/'
    collect_f = 'glance%0.2d'%now.day
    collect_p = os.path.join(collect_d, collect_f)

    if os.path.exists(collect_p):
        mtime = os.stat(collect_p).st_mtime
        if datetime.datetime.fromtimestamp(mtime) < now - datetime.timedelta(days=1):
            os.unlink(collect_p)

    _cmd = 'nohup %s %d >>%s &'%(cmd_str, iterations, collect_p)
    process = Popen(_cmd, shell=True, stdout=PIPE, stderr=PIPE)


run_glance()
fs_u_data = fs_u()
if fs_u_data is not None:
    node.collector.call('push_stats_fs_u', fs_u_data)
node.close()

 0707010000b978000081ed00000000000000000000000154fc7bfb000010d6000000660000000300000000000000000000002300000000root/opt/opensvc/bin/perfagt.Linux    #!/opt/opensvc/bin/python

from __future__ import print_function
import os
import sys
import datetime
import time
import json

pathsvc = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(os.path.join(pathsvc, 'lib'))

import node
from rcUtilities import justcall, which

node = node.Node()

now = str(datetime.datetime.now())

def fs_u():
    cmd = ['df', '-lP']
    (out, err, ret) = justcall(cmd)
    if ret != 0:
        return
    lines = out.split('\n')
    if len(lines) < 2:
        return
    vals = []
    for line in lines[1:]:
        l = line.split()
        if len(l) != 6:
            continue
        vals.append([now, node.nodename, l[5], l[1], l[4].replace('%','')])

    pathvar = os.path.join(pathsvc, 'var')
    stats_fs_u_p = os.path.join(pathvar, 'stats_fs_u.%d' % datetime.datetime.now().day)
    if not os.path.exists(stats_fs_u_p):
        # create the stats file
        mode = 'w+'
    elif os.stat(stats_fs_u_p).st_mtime < time.time() - 86400:
        # reset the stats file from last month
        mode = 'w+'
    else:
        # append to the daily stats file
        mode = 'a'

    with open(stats_fs_u_p, mode) as f:
        f.write(json.dumps(vals)+'\n')

"""
xentop
NAME STATE CPU(sec) CPU(%) MEM(k) MEM(%) MAXMEM(k) MAXMEM(%) VCPUS NETS NETTX(k) NETRX(k) VBDS VBD_OO VBD_RD VBD_WR VBD_RSECT VBD_WSECT SSID
"""

def xentop(node):
    import os, sys, platform
    import select
    import logging
    import time
    import datetime
    import subprocess

    if not which('xentop'):
        return

    node.build_services()
    containernames = {}
    for svc in node.svcs:
        for r in svc.get_resources("container"):
            if r.type in ("container.ovm", "container.xen"):
                if hasattr(r, "uuid"):
                    containernames[r.uuid] = r.name

    zs_d = os.path.join(pathsvc, 'log', 'xentop')
    zs_prefix = 'xentop'
    zs_f = os.path.join(zs_d, zs_prefix + datetime.datetime.now().strftime("%d"))
    datenow = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    n = datetime.datetime.now()
    tn = time.mktime(n.timetuple())
    
    if not os.path.exists(zs_d):
        os.makedirs(zs_d)
    
    try:
        t = os.path.getmtime(zs_f)
        d = tn - t
    except:
        d = 0
    
    if d > 27*24*3600:
        os.remove(zs_f)
    
    f = open(zs_f, "a")
    
    stor = {}
    
    p = subprocess.Popen('xentop -b -d.1 -i2 -f',
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT,
                         shell=True,
                         bufsize=0)
    
    out = p.stdout.readline()
    pr = 0
    
    while out:
        line = out
        line = line.rstrip("\n")
    
        if "NAME" in line:
            pr += 1
            out = p.stdout.readline()
            continue
    
        line = line.replace("no limit", "0")
        fields = line.split()
        if len(fields) == 19 and pr > 1:
            uuid = fields[0]
            if uuid in containernames:
                uuid = containernames[uuid]
            stor[uuid] = {
             'STATE': fields[1],
             'CPU_SEC': fields[2],
             'CPU_PCT': fields[3],
             'MEM': str(int(fields[4])//1024),
             'MEM_PCT': fields[5],
             'MEM_MAX': str(int(fields[6])//1024),
             'MEM_MAX_PCT': fields[7],
             'VCPUS': fields[8],
             'NETS': fields[9],
             'NET_TX': fields[10],
             'NET_RX': fields[11],
             'VBDS': fields[12],
             'VBD_OO': fields[13],
             'VBD_RD': fields[14],
             'VBD_WR': fields[15],
             'VBD_RSECT': fields[16],
             'VBD_WSECT': fields[17],
             'SSID': fields[18] 
            }
            print(datenow, uuid, stor[uuid]['STATE'], stor[uuid]['CPU_SEC'], stor[uuid]['CPU_PCT'], stor[uuid]['MEM'], stor[uuid]['MEM_PCT'], stor[uuid]['MEM_MAX'], stor[uuid]['MEM_MAX_PCT'], stor[uuid]['VCPUS'], stor[uuid]['NETS'], stor[uuid]['NET_TX'], stor[uuid]['NET_RX'], stor[uuid]['VBDS'], stor[uuid]['VBD_OO'], stor[uuid]['VBD_RD'], stor[uuid]['VBD_WR'], stor[uuid]['VBD_RSECT'], stor[uuid]['VBD_WSECT'], stor[uuid]['SSID'], file=f)
        out = p.stdout.readline()
    
    p.wait()

fs_u()

xentop(node)

node.close()
  0707010000b9a7000081ed00000000000000000000000154fc7bfb00001589000000660000000300000000000000000000002300000000root/opt/opensvc/bin/perfagt.SunOS    #!/opt/opensvc/bin/python
"""
YYYY-MM-DD hh:mm:ss ZONE SWAP RSS CAP at avgat pg avgpg NPROC mem% cpu% TIME LastReboot
"""

from __future__ import print_function
import os, sys, platform
import select
import logging
import time
import datetime
import subprocess

pathsvc = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(os.path.join(pathsvc, 'lib'))

import node
from rcUtilities import justcall, which

node = node.Node()
now = str(datetime.datetime.now())

zs_d = os.path.join(os.sep, 'var', 'adm', 'zonestat')
zs_prefix = 'zs'
zs_f = os.path.join(zs_d, zs_prefix + datetime.datetime.now().strftime("%d"))
datenow = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
n = datetime.datetime.now()
tn = time.mktime(n.timetuple())

if not os.path.exists(zs_d):
    os.makedirs(zs_d)

try:
    t = os.path.getmtime(zs_f)
    d = tn - t
except:
    d = 0

if d > 27*24*3600:
    os.remove(zs_f)

f = open(zs_f, "a")

stor = {}

p = subprocess.Popen('/usr/bin/prstat -Z -n1,60 1 1',
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT,
                     shell=True,
                     bufsize=0)

out = p.stdout.readline()
pr = 0

while out:
    line = out
    line = line.rstrip("\n")

    if "ZONEID" in line:
        pr = 1
        out = p.stdout.readline()
        continue

    if "Total:" in line:
        pr = 0
        out = p.stdout.readline()
        continue

    if "%" in line and pr == 1:
        fields = line.split()
        stor[fields[7]] = {
         'SWAP': fields[2],
         'RSS': fields[3],
         'CAP': '0',
         'at': '0',
         'avgat': '0',
         'pg': '0',
         'avgpg': '0',
         'NPROC': fields[1],
         'mem': fields[4],
         'cpu': fields[6],
         'TIME': fields[5] 
        }
    out = p.stdout.readline()

p.wait()
fi=1
pr=0
p = subprocess.Popen('/usr/bin/rcapstat -z 1 2',
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT,
                     bufsize=0,
                     shell=True)
out = p.stdout.readline()

while out:
    line = out
    line = line.rstrip("\n")

    if "id zone" in line and fi == 1:
        fi = 0
        out = p.stdout.readline()
        continue

    if "id zone" in line and fi == 0:
        pr = 1
        out = p.stdout.readline()
        continue

    if not "id zone" in line and pr == 1:
        fields = line.split()
        stor[fields[1]]['CAP'] = fields[5]
        stor[fields[1]]['at'] = fields[6]
        stor[fields[1]]['avgat'] = fields[7]
        stor[fields[1]]['pg'] = fields[8]
        stor[fields[1]]['avgpg'] = fields[9]

    out = p.stdout.readline()

p.wait()

for z in stor:
    zn = z
    if z == 'global' :
        zn = platform.node()
        p = subprocess.Popen('/usr/bin/who -b',
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT,
                         bufsize=0,
                         shell=True)
    else :
        p = subprocess.Popen('/usr/sbin/zlogin ' + z + ' /usr/bin/who -b',
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT,
                         bufsize=0,
                         shell=True)
    out = p.stdout.readline()
    txt = out.split()
    print(datenow, zn, stor[z]['SWAP'], stor[z]['RSS'], stor[z]['CAP'], stor[z]['at'], stor[z]['avgat'], stor[z]['pg'], stor[z]['avgpg'], stor[z]['NPROC'], stor[z]['mem'], stor[z]['cpu'], stor[z]['TIME'], txt[-3], txt[-2], txt[-1], file=f)
    p.wait()


"""
 fs_u
"""
def fs_u():
    vars = ['date',
            'nodename',
            'mntpt',
            'size',
            'used']
    vals = []
    vals += fs_u_t("vxfs")
    vals += fs_u_t("ufs")
    vals += fs_u_zfs()
    return vars, vals

def fs_u_t(t):
    if not which('df'):
        return []
    cmd = ['df', '-F', t, '-k']
    (out,err,ret) = justcall(cmd)
    if ret != 0:
        return []
    lines = out.split('\n')
    if len(lines) < 2:
        return []
    vals = []
    for line in lines[1:]:
        l = line.split()
        if len(l) == 5:
            l = [''] + l
        elif len(l) != 6:
            continue
        vals.append([now, node.nodename, l[5], l[1], l[4].replace('%','')])
    return vals

def fs_u_zfs():
    if not which('zfs'):
        return []
    cmd = ['zfs', 'list', '-o', 'name,used,avail,mountpoint', '-H']
    (out,err,ret) = justcall(cmd)
    if ret != 0:
        return []
    lines = out.split('\n')
    if len(lines) == 0:
        return []
    vals = []
    for line in lines:
        l = line.split()
        if len(l) != 4:
            continue
        if "@" in l[0]:
            # do not report clone usage
            continue
        if "osvc_sync_" in l[0]:
            # do not report osvc sync snapshots fs usage
            continue
        used = convert(l[1])
        if l[2] == '0':
            l[2] = '0K'
        avail = convert(l[2])
        total = used + avail
        pct = used / total * 100
        vals.append([now, node.nodename, l[0], str(total), str(pct)])
    return vals

def convert(s):
    s = s.replace(',', '.')
    if len(s) < 2:
        raise
    if s.endswith('T'):
        s = float(s[:-1])*1024*1024*1024
    elif s.endswith('G'):
        s = float(s[:-1])*1024*1024
    elif s.endswith('M'):
        s = float(s[:-1])*1024
    elif s.endswith('K'):
        s = float(s[:-1])
    else:
        raise
    return s

node.collector.call('push_stats_fs_u', fs_u())

   0707010000b9a8000081ed00000000000000000000000154fc7bfb0000718a000000660000000300000000000000000000002100000000root/opt/opensvc/bin/postinstall  #!/usr/bin/env python

import os
import errno
import shutil
import glob
import sys
import tempfile
import inspect

python_names = ['python2.7', 'python2.6']
python_paths = []
python_paths += glob.glob("/opt/python-2.7.*/bin/python2.7")
python_paths += glob.glob("/opt/python-2.6.*/bin/python2.6")
python_paths += [
  os.path.join(os.sep, "opt", "python-2.6.4", "bin", "python"),
  os.path.join(os.sep, "usr", "local", "bin", "python2.6")
]

try:
    sysname, nodename, x, x, machine = os.uname()
except:
    import platform
    sysname, nodename, x, x, machine, x = platform.uname()

try:
    pathbin = os.path.dirname(__file__)
except NameError:
    # old python version do not support __file__
    pathbin = os.path.join(os.sep, 'opt', 'opensvc', 'bin')

def make_sure_path_exists(path):
    try:
        os.makedirs(path, 0755)
    except OSError, exception:
        if exception.errno != errno.EEXIST:
            raise

pathbin = os.path.realpath(pathbin)
if not pathbin.rstrip(os.sep).endswith('bin'):
    # hpux executes the postinstall from dir
    # /var/tmp/XXXXXXXXX/catalog/opensvc/commands/
    pathbin = os.path.join(os.sep, 'opt', 'opensvc', 'bin')
pathsvc = os.path.realpath(os.path.join(pathbin, '..'))
pathbin = os.path.join(pathsvc, 'bin')
pathetc = os.path.join(pathsvc, 'etc')
pathvar = os.path.join(pathsvc, 'var')
pathtmp = os.path.join(pathsvc, 'tmp')
pathlib = os.path.join(pathsvc, 'lib')
pathlog = os.path.join(pathsvc, 'log')
pathusr = os.path.join(pathsvc, 'usr')

make_sure_path_exists(pathlog)

def logit(msg):
    curframe = inspect.currentframe()
    calframe = inspect.getouterframes(curframe, 2)
    try:
        import datetime
        timestamp = str(datetime.datetime.now())
    except:
        timestamp = '?'

    osvlog = os.path.join(pathlog, 'postinstall.log')
    content = '['+calframe[1][3]+']['+timestamp+'] '+msg
    f = open(osvlog, 'a')
    f.write(content+'\n')
    f.close()

logit("Starting OpenSVC PostInstall")

SolarisRootRelocate = False
if sysname == 'SunOS' and "PKG_INSTALL_ROOT" in os.environ and os.environ['PKG_INSTALL_ROOT'] != '/':
     SolarisRootRelocate = True

variables = {"pathsvc":pathsvc, "pathbin":pathbin, "pathetc":pathetc, "pathvar":pathvar, "pathtmp":pathtmp, "pathlib":pathlib, "pathlog":pathlog, "pathusr":pathusr, "SolarisRootRelocate":SolarisRootRelocate}
for key in variables:
    logit("var %s <%s>"%(key,variables[key]))

def python_path():
    logit("Begin")
    for n in python_names:
        p = _python_path(n)
        if p is not None:
            return p
    for p in python_paths:
        if os.path.exists(p):
            return p

def _python_path(python_exe):
    logit("Begin")
    for path in os.environ["PATH"].split(os.pathsep):
        exe_file = os.path.join(path, python_exe)
        if os.path.exists(exe_file) and os.access(exe_file, os.X_OK):
            return exe_file
    return None

def install_cron():
    logit("Begin")
    if sysname == 'Windows':
        return 
    else:
        return install_cron_unix()

def install_cron_windows():
    logit("Begin")
    print "install OsvcSched service"
    schedstop()
    schedremove()
    schedinstall()
    schedstart()

def schedremove():
    logit("Begin")
    cmd = ' remove'
    schedcmd(cmd)

def schedstart():
    logit("Begin")
    cmd = ' start'
    schedcmd(cmd)

def schedstop():
    logit("Begin")
    cmd = ' stop'
    schedcmd(cmd)

def schedinstall():
    logit("Begin")
    cmd = ''
    cmd += ' --username LocalSystem'
    cmd += ' --startup auto'
    cmd += ' install\n'
    schedcmd(cmd)

def schedcmd(_cmd):
    logit("Begin")
    print _cmd
    rc = '"'+sys.executable+'" "'+os.path.join(pathlib, 'rcWinScheduler.py')+'"'
    cmd = "@echo off\n"
    cmd += rc
    cmd += _cmd
    fd, fname = tempfile.mkstemp(dir=pathtmp, suffix='.cmd')
    f = os.fdopen(fd, 'w')
    f.write(cmd)
    f.close()
    import subprocess
    subprocess.call([fname])
    os.unlink(fname)
 
def install_cron_unix():
    logit("Begin")
    """install opensvc cron jobs
    """

    agt = os.path.join(pathbin, 'perfagt.'+sysname)
    ce = [{
           'sched': "0,10,20,30,40,50 * * * *",
           'reset_sched': False,
           'user': "",
           'cmd': "[ -x /opt/opensvc/bin/svcmon ] && /opt/opensvc/bin/svcmon --updatedb --maxdelaydb 120 >/dev/null 2>&1",
           'marker': 'svcmon --updatedb',
           'ok': False
    },{
           'sched': "0,10,20,30,40,50 * * * *",
           'reset_sched': True,
           'user': "",
           'cmd': "[ -x /opt/opensvc/bin/cron/opensvc ] && /opt/opensvc/bin/cron/opensvc >/dev/null 2>&1",
           'marker': '/opt/opensvc/bin/cron/opensvc',
           'ok': False
    },{
           'sched': "0,10,20,30,40,50 * * * *",
           'reset_sched': False,
           'user': "",
           'cmd': "[ -x %s ] && %s >/dev/null 2>&1"%(agt,agt),
           'marker': agt,
           'ok': False
    }]
    remove_entries = ['bin/nodemgr compliance check']

    purge = []
    root_crontab = False

    """ order of preference
    """
    if sysname == 'SunOS' :
	if SolarisRootRelocate is True:
	    suncron = os.environ["PKG_INSTALL_ROOT"] + '/var/spool/cron/crontabs/root'
            root_crontab_locs = [
               suncron
            ]
	else:
	    root_crontab_locs = [
		'/var/spool/cron/crontabs/root'
            ]
    else:
        root_crontab_locs = [
            '/etc/cron.d/opensvc',
            '/var/spool/cron/crontabs/root',
            '/var/spool/cron/root',
            '/var/cron/tabs/root',
            '/usr/lib/cron/tabs/root',
        ]
    for loc in root_crontab_locs:
        if os.path.exists(os.path.dirname(loc)):
            if not root_crontab:
                root_crontab = loc
                if root_crontab == '/etc/cron.d/opensvc':
                    ce[0]['user'] = "root"
                    ce[1]['user'] = "root"
                    ce[2]['user'] = "root"
            elif os.path.exists(loc):
                purge.append(loc)

    if not root_crontab:
        print "no root crontab found in usual locations %s"%str(root_crontab_locs)
        return False

    ce[0]['full'] = ' '.join([ce[0]['sched'], ce[0]['user'], ce[0]['cmd']])
    ce[1]['full'] = ' '.join([ce[1]['sched'], ce[1]['user'], ce[1]['cmd']])

    if os.path.exists(agt):
        ce[2]['full'] = ' '.join([ce[2]['sched'], ce[2]['user'], ce[2]['cmd']])
    else:
        ce[2]['full'] = None

    new = False
    if os.path.exists(root_crontab):
        try:
            f = open(root_crontab, 'r')
            new = f.readlines()
            f.close()
        except:
            f.close()
            import traceback
            traceback.print_exc()

        i = -1
        for line in new:
            i += 1
            for c in ce:
                if c['full'] is None:
                    continue
                if line.find(c['marker']) != -1:
                    if line.find(c['cmd']) != -1:
                        sched = ' '.join(line.split()[:5])
                    if c['reset_sched'] and sched != c['sched']:
                        new[i] = ' '.join([c['sched'], c['user'], c['cmd']])+'\n'
                        c['ok'] = True
                    else:
                        # preserve scheduling
                        if c['reset_sched']:
                            sched = c['sched']
                        else:
                            sched = ' '.join(line.split()[:5])
                        new[i] = ' '.join([sched, c['user'], c['cmd']])+'\n'
                        c['ok'] = True
        for c in ce:
            if c['full'] is not None and not c['ok']:
                new.append(c['full']+'\n')
    else:
        new = []
        for c in ce:
            if c['full'] is not None and not c['ok']:
                new.append(c['full']+'\n')

    if not new:
        print "problem preparing the new crontab"
        return False
 
    i = -1
    for line in new:
        i += 1
        for re in remove_entries:
            if line.find(re) != -1:
                print 'delete line "%s" from %s'%(re,root_crontab)
                del new[i]

    try:
        f = open(root_crontab, 'w')
        f.write(''.join(new))
        f.close()
    except:
        f.close()
        import traceback
        traceback.print_exc()

    """ Activate changes (actually only needed on HP-UX)
    """
    if root_crontab.find('/var/spool/') != -1:
        cmd = ['crontab', root_crontab]
        ret = os.system(' '.join(cmd))

    for loc in purge:
        try:
            f = open(loc, 'r')
            new = [ line for line in f.readlines() if line.find('opensvc.daily') == -1 and line.find('svcmon --updatedb') == -1 ]
            f.close()
            f = open(loc, 'w')
            f.write(''.join(new))
            f.close()
        except:
            f.close()
            import traceback
            traceback.print_exc()

    """ Clean up old standard file locations
    """
    for f in ['/etc/cron.daily/opensvc', '/etc/cron.daily/opensvc.daily']:
        if os.path.exists(f):
            os.unlink(f)

def activate_chkconfig(svc):
    logit("Begin")
    cmd = ['chkconfig', '--add', svc]
    ret = os.system(' '.join(cmd))
    if ret > 0:
        return False
    return True

def activate_systemd(launcher):
    logit("Begin")
    systemdsvc = 'opensvc-agent.service'
    
    # populate systemd tree with opensvc unit file
    src = os.path.join(pathbin, 'systemd.opensvc-agent.service')
    dst = os.path.join('/etc/systemd/system/', systemdsvc)
    shutil.copyfile(src, dst)
    os.chmod(dst, 0644)
    
    # add symlink to resolve systemd service call
    systemd_call = '/opt/opensvc/bin/opensvc.init'
    if not os.path.islink(systemd_call):
        if os.path.exists(systemd_call):
            os.unlink(systemd_call)
        msg = "create link %s -> %s"%(systemd_call, launcher)
        logit(msg)
        try:
            os.symlink(launcher, systemd_call)
        except:
            logit("Error while trying to create opensvc.init symlink")

    # reload systemd configuration
    cmd = ['systemctl', '-q', 'daemon-reload']
    ret = os.system(' '.join(cmd))
    if ret > 0:
        logit("WARN : Problem during systemctl reload")
    
    # enable opensvc agent startup through systemd
    cmd = ['systemctl', '-q', 'enable', systemdsvc]
    ret = os.system(' '.join(cmd))
    if ret > 0:
        logit("WARN : Problem during systemctl enable")


def systemd_mgmt():
    logit("Begin")
    cmd = ['systemctl', '--version', '>>/dev/null', '2>&1']
    ret = os.system(' '.join(cmd))
    if ret > 0:
        return False
    return True

def activate_ovm(launcher):
    logit("Begin")
    activate_chkconfig('zopensvc')

def activate_redhat(launcher):
    logit("Begin")
    activate_chkconfig('opensvc')

def activate_debian(launcher):
    logit("Begin")
    cmd = ['update-rc.d', '-f', 'opensvc', 'remove']
    ret = os.system(' '.join(cmd))
    if ret > 0:
        return False
    cmd = ['update-rc.d', 'opensvc', 'defaults']
    ret = os.system(' '.join(cmd))
    if ret > 0:
        return False
    return True

def activate_hpux(launcher):
    logit("Begin")
    rc = "/sbin/init.d/opensvc"
    links = ["/sbin/rc1.d/K010opensvc", "/sbin/rc2.d/K010opensvc", "/sbin/rc3.d/S990opensvc"]
    if os.path.exists("/sbin/rc2.d/S990opensvc"):
        os.unlink("/sbin/rc2.d/S990opensvc")
    for l in links:
        if not os.path.islink(l):
            if os.path.exists(l):
                os.unlink(l)
            os.symlink(rc, l)
    try:
        f = open("/etc/rc.config.d/opensvc", "w")
        f.write("RUN_OPENSVC=1\n")
        f.close()
    except:
        f.close()
        import traceback
        traceback.print_exc()
    return True

def activate_AIX(launcher):
    logit("Begin")
    rc = "/etc/rc.d/init.d/opensvc"
    links = ["/etc/rc.d/rc2.d/S990opensvc"]
    for l in links:
        if not os.path.islink(l):
            if os.path.exists(l):
                os.unlink(l)
            print "create link %s -> %s"%(l, rc)
            os.symlink(rc, l)
    return True

def activate_OSF1(launcher):
    logit("Begin")
    rc = "/sbin/init.d/opensvc"
    links = ["/sbin/rc0.d/K010opensvc", "/sbin/rc2.d/K010opensvc", "/sbin/rc3.d/S990opensvc"]
    for l in links:
	if not os.path.islink(l):
	    if os.path.exists(l):
	        os.unlink(l)
	    os.symlink(rc, l)
    return True

def activate_SunOS(launcher):
    logit("Begin")
    if SolarisRootRelocate is True:
        rc = "/etc/init.d/opensvc"
        links = [os.environ["PKG_INSTALL_ROOT"] + "/etc/rc0.d/K00opensvc", os.environ["PKG_INSTALL_ROOT"] + "/etc/rc3.d/S99opensvc"]
    else:
	rc = "/etc/init.d/opensvc"
	links = ["/etc/rc0.d/K00opensvc", "/etc/rc3.d/S99opensvc"]
    logit("rc <%s>"%rc)
    for l in links:
        logit("link <%s>"%l)
        if not os.path.islink(l):
            if os.path.exists(l):
                os.unlink(l)
            os.symlink(rc, l)
    return True

def activate_FreeBSD(launcher):
    logit("Begin")
    return True

def activate_Darwin(launcher):
    logit("Begin")
    return True

def update_file(filename, srctext, replacetext):
    logit("Begin")
    """ replace into filename srctext by replacetext
    """
    import fileinput
    for line in fileinput.input(filename, inplace=1):
        if line.rstrip('\n') == srctext.rstrip('\n') :
            line = replacetext
        print line.rstrip('\n')
    fileinput.close()

def install_params(path2file):
    logit("Begin")
    """ install template file with tunable variables
    """
    if os.path.exists(path2file):
        return

    try:
        f = open(path2file, "w")
    except:
        import traceback
        traceback.print_exc()
    else:
        f.write("# This is the OpenSVC startup configuration file\n")
        f.write("# You may need to adapt parameters to fit your environment\n")
        f.write("# This file is not modified during software upgrades\n")
        f.write("# If empty, default settings are used in the init script\n\n\n")
        f.write("# arguments passed to the allservices boot command at system boot\n")
        f.write("#osvc_opts=\n")
        f.close()
 
def install_rc():
    logit("Begin")
    """install startup script
    """
    params = None
    copyrc = True

    if os.path.exists('/etc/debian_version'):
        rc = '/etc/init.d/opensvc'
        params = '/etc/default/opensvc'
        src = '/opt/opensvc/bin/opensvc.init.debian'
        if systemd_mgmt():
            logit("Debian with systemd")
            copyrc = False
            activate = activate_systemd
        else:
            logit("Debian with update-rc.d (rely on insserv)")
            activate = activate_debian
    elif os.path.exists('/etc/SuSE-release'):
        rc = '/etc/init.d/opensvc'
        params = '/etc/sysconfig/opensvc'
        src = '/opt/opensvc/bin/opensvc.init.suse'
        if systemd_mgmt():
            logit("SuSE with systemd")
            copyrc = False
            activate = activate_systemd
        else:
            logit("SuSE with chkconfig (rely on insserv)")
            activate = activate_redhat       
    elif os.path.exists('/etc/redhat-release'):
        params = '/etc/sysconfig/opensvc'
        src = '/opt/opensvc/bin/opensvc.init.redhat'
        try:
            f = open('/etc/redhat-release', 'r')
            buff = f.read()
            f.close()
        except:
            buff = ""
        if buff.find('Oracle VM server') != -1:
            rc = '/etc/init.d/zopensvc'
            activate = activate_ovm
        else:
            rc = '/etc/init.d/opensvc'
            if systemd_mgmt():
                logit("Red Hat with systemd")
                copyrc = False
                activate = activate_systemd
            else:
                logit("Red Hat with chkconfig (rely on insserv)")
                activate = activate_redhat    

    elif sysname == "HP-UX":
        rc = '/sbin/init.d/opensvc'
        src = '/opt/opensvc/bin/opensvc.init.hpux'
        activate = activate_hpux
    elif sysname == "SunOS":
	if SolarisRootRelocate is True:
            rc = os.environ["PKG_INSTALL_ROOT"] + '/etc/init.d/opensvc'
            params = os.environ["PKG_INSTALL_ROOT"] + '/etc/default/opensvc'
            src = os.environ["PKG_INSTALL_ROOT"] + '/opt/opensvc/bin/opensvc.init.SunOS'
        else:
            rc = '/etc/init.d/opensvc'
            params = '/etc/default/opensvc'
            src = '/opt/opensvc/bin/opensvc.init.SunOS'
        activate = activate_SunOS
    elif sysname == "OSF1":
        rc = '/sbin/init.d/opensvc'
        src = '/opt/opensvc/bin/opensvc.init.OSF1'
        activate = activate_OSF1
    elif sysname == "FreeBSD":
        rc = '/etc/rc.d/opensvc'
        params = '/etc/defaults/opensvc'
        src = '/opt/opensvc/bin/opensvc.init.FreeBSD'
        activate = activate_FreeBSD
    elif sysname == "AIX":
        rc = '/etc/rc.d/init.d/opensvc'
        src = '/opt/opensvc/bin/opensvc.init.AIX'
        activate = activate_AIX
    elif sysname == "Darwin":
        rc = '/Library/LaunchDaemons/com.opensvc.svcmgr.plist'
        params = '/etc/defaults/opensvc'
        src = '/opt/opensvc/bin/darwin.com.opensvc.svcmgr.plist'
        activate = activate_Darwin
    elif sysname == 'Windows':
        return False
    else:
        print "could not select an init script: unsupported operating system"
        return False

    if os.path.islink(rc):
        os.unlink(rc)

    if copyrc:
        logit("Copying src launcher script to rc")
        shutil.copyfile(src, rc)
        os.chmod(rc, 0755)

    if params is not None and not os.path.exists(params):
        logit("Installing default parameters file")
        install_params(params)
        
    activate(src)

def gen_keys():
    logit("Begin")
    if sysname == 'Windows':
        return
    home = os.path.expanduser("~root")
    logit("home <%s>"%home)
    if SolarisRootRelocate is True:
	home = os.environ['PKG_INSTALL_ROOT'] + os.path.expanduser("~root")
        logit("SunOS and relocatable install home is now <%s>"%home)
    sshhome = os.path.join(home, ".ssh")
    logit("gen_keys(): sshhome <%s>"%sshhome)
    if not os.path.exists(sshhome):
	print "create dir", sshhome
	os.makedirs(sshhome, 0700)
    priv = os.path.join(sshhome, "id_dsa")
    pub = os.path.join(sshhome, "id_dsa.pub")
    if os.path.exists(pub) or os.path.exists(priv):
        return
    cmd = ['ssh-keygen', '-t', 'dsa', '-b', '1024', '-P', '""', '-f', priv]
    ret = os.system(' '.join(cmd))

def missing_dir(sub):
    logit("Begin")
    pathd = os.path.join(pathsvc, sub)
    if not os.path.exists(pathd):
	print "create dir", pathd
	os.makedirs(pathd, 0755)

def missing_dirs():
    logit("Begin")
    missing_dir('log')
    missing_dir('tmp')
    missing_dir('var')

def convert_svclinks():
    logit("Begin")
    missing_dir('etc')
    svcmgr = os.path.join('..', 'bin', 'svcmgr')
    if not os.path.exists(svcmgr):
	return 1
    rcService = os.path.realpath(os.path.join(os.sep, 'opt', 'opensvc', 'bin', 'rcService'))
    if not os.path.exists(rcService):
	return 1
    for fname in os.listdir(pathetc):
        fpath = os.path.join(pathetc, fname)
        if not os.path.islink(fpath):
            continue
        rpath = os.path.realpath(fpath)
        if rpath != rcService:
            continue
        os.unlink(fpath)
        os.symlink(svcmgr, fpath)

def move_usr_to_opt():
    logit("Begin")
    linksvc = os.path.join(os.sep, 'service')
    old_pathsvc = os.path.join(os.sep, 'usr', 'local', 'opensvc')
    old_pathvar = os.path.join(old_pathsvc, 'var')
    old_pathetc = os.path.join(old_pathsvc, 'etc')

    if os.path.exists(old_pathvar):
        for f in glob.glob(old_pathvar+'/*'):
            dst = os.path.join(pathvar, os.path.basename(f))
            if os.path.exists(dst) and dst.find('host_mode') == -1:
                continue
            if os.path.isdir(f):
                shutil.copytree(f, dst, symlinks=True)
            elif os.path.islink(f):
                linkto = os.readlink(f)
                os.symlink(linkto, dst)
            else:
                shutil.copy2(f, dst)

    if os.path.exists(old_pathetc):
        for f in glob.glob(old_pathetc+'/*'):
            dst = os.path.join(pathetc, os.path.basename(f))
            if os.path.exists(dst):
                continue
            if os.path.islink(f):
                linkto = os.readlink(f)
                os.symlink(linkto, dst)
            elif os.path.isdir(f):
                shutil.copytree(f, dst, symlinks=True)
            else:
                shutil.copy2(f, dst)

    if os.path.exists(old_pathsvc):
        shutil.rmtree(old_pathsvc)

    if os.path.islink(linksvc) and os.path.realpath(linksvc) == old_pathsvc:
        os.unlink(linksvc)

def install_etc_path():
    logit("Begin")
    p = os.path.join(os.sep, 'etc', 'PATH')
    if not os.path.exists(p):
        return
    try:
        f = open(p, "r")
        buff = f.read()
        f.close()
    except:
        return
    l = buff.strip().split(":")
    n = len(l)
    for op in (pathbin, pathetc):
        if op in l:
            continue
        l.append(op)
    if len(l) == n:
        return
    try:
        f = open(p, "w")
        f.write(":".join(l)+'\n')
        f.close()
    except:
        return

def install_profile():
    logit("Begin")
    prof_d = os.path.join(os.sep, 'etc', 'profile.d')
    prof = os.path.join(prof_d, 'opensvc.sh')
    buff = "export PATH=$PATH:"+pathbin+":"+pathetc+"\n"
    if not os.path.exists(prof_d):
        return
    try:
        f = open(prof, 'w')
        f.write(buff)
        f.close()
    except:
        f.close()
        import traceback
        traceback.print_exc()

def install_bash_completion():
    logit("Begin")
    src = os.path.join(pathsvc, 'usr', 'share', 'bash_completion.d', 'opensvc')
    ds = [os.path.join(os.sep, 'etc', 'bash_completion.d'),
          os.path.join(os.sep, 'etc', 'bash', 'bash_completion.d')]
    for d in ds:
        dst = os.path.join(d, 'opensvc')
        if not os.path.exists(d):
            d = None
            continue
        else:
            break
    if d is None:
        return
    shutil.copyfile(src, dst)
    os.chmod(dst, 0644)

def install_link(source, target):
    logit("Begin")
    if source == '' or target == '':
        return False
    if os.path.realpath(source) == os.path.realpath(target):
        return True
    if os.path.islink(target) or os.path.exists(target):
        os.unlink(target)
    os.symlink(source,target)

def install_pythonlink():
    logit("Begin")
    if sysname == 'Windows':
        return install_pythonlink_windows()
    else:
        return install_pythonlink_unix()


def install_pythonlink_windows():
    logit("Begin")
    logit("Before appending pathlib to syspath")
    logit(os.environ["PATH"])
    sys.path.append(pathlib)
    logit("After appending pathlib to syspath")
    logit(os.environ["PATH"])
    from rcUtilitiesWindows import get_registry_value
    logit("Before reading installfolder in registry")
    try:
        installfolder = get_registry_value('HKEY_CURRENT_USER', 'Software\\OpenSVC', 'path')
    except:
        logit("ERROR while trying to read path into registry HKCU/Software/OpenSVC/path")
        sys.exit(1)

    installfolder = installfolder.rstrip('\\')   
    logit("installfolder = <"+installfolder+">")
    osvcenv = os.path.join(installfolder, 'osvcenv.cmd')
    content = '@echo off\nset OSVCROOT='+installfolder+'\nset OSVCPYTHONROOT=%OSVCROOT%\python\nset PYTHONPATH=%OSVCROOT%\lib\nset OSVCPYTHONEXEC=%OSVCPYTHONROOT%\python.exe\ncall inpath.cmd OSVCPYTHONROOT'
    logit(content)
    f = open(osvcenv, 'w')
    f.write(content)
    f.close()
		
def install_pythonlink_unix():
    logit("Begin")
    p = python_path()
    if p is None:
        print >>sys.stderr, "could not find a valid python installation"
        sys.exit(1)
    target = os.path.join(pathbin, 'python')
    return install_link(source=p, target=target)

def move_host_mode():
    logit("Begin")
    hm = os.path.join(pathvar, 'host_mode')
    cf = os.path.join(pathetc, 'node.conf')
    nodemgr = os.path.join(pathbin, 'nodemgr')
    if not os.path.exists(hm):
        return
    try:
        fp = open(hm, 'r')
        mode = fp.read().split()[0]
        fp.close()
    except:
        print 'failed to read old host_mode. renamed to', hm+'.old'
        shutil.move(hm, hm+'.old')
        return
    cmd = [nodemgr, 'set', '--param', 'node.host_mode', '--value', mode]
    ret = os.system(' '.join(cmd))
    if ret != 0:
        print 'failed to set host_mode in node.conf'
        return
    shutil.move(hm, hm+'.old')
 
def nodeconf_params():
    logit("Begin")
    nodeconf = os.path.join(pathetc, 'node.conf')
    dotnodeconf = os.path.join(pathetc, '.node.conf')

    # reset etc/.node.conf (autogenerated)
    if os.path.exists(dotnodeconf):
        os.unlink(dotnodeconf)

    if not os.path.exists(nodeconf):
        return

    import ConfigParser
    import copy
    try:
        config = ConfigParser.RawConfigParser()
    except AttributeError:
        return
    config.read(nodeconf)
    changed = False

    # no DEFAULT in etc/node.conf
    for o in copy.copy(config.defaults()):
        config.remove_option('DEFAULT', o)
        changed = True

    # sync section goes to etc/.node.conf
    if config.has_section('sync'):
        config.remove_section('sync')
        changed = True

    for s in config.sections():
        for o in config.options(s):
            if o in ['sync_interval', 'push_interval', 'comp_check_interval']:
                v = config.getint(s, o)
                config.remove_option(s, o)
                config.set(s, 'interval', v)
                changed = True
            if o in ['sync_days', 'push_days', 'comp_check_days']:
                v = config.get(s, o)
                config.remove_option(s, o)
                config.set(s, 'days', v)
                changed = True
            if o in ['sync_period', 'push_period', 'comp_check_period']:
                v = config.get(s, o)
                config.remove_option(s, o)
                config.set(s, 'period', v)
                changed = True

    if changed:
        try:
            fp = open(nodeconf, 'w')
            config.write(fp)
            fp.close()
        except:
            print >>sys.stderr, "failed to write new %s"%nodeconf

def save_exc():
    logit("Begin")
    import traceback
    try:
        import tempfile

        try:
            import datetime
            now = str(datetime.datetime.now()).replace(' ', '-')
        except:
            now =""

        try:
            f = tempfile.NamedTemporaryFile(dir=pathtmp, prefix='exc-'+now+'-')
        except:
            return
        f.close()
        f = open(f.name, 'w')
        traceback.print_exc(file=f)
        print >>sys.stderr, "unexpected error. stack saved in %s"%f.name
        f.close()
    except:
        print >>sys.stderr, "unexpected error"
        traceback.print_exc()

def purge_collector_api_cache():
    logit("Begin")
    fname = os.path.join(pathvar, "collector")
    if os.path.exists(fname) and os.path.isfile(fname):
        os.unlink(fname)

def chmod_directories():
    if not hasattr(os, "walk"):
        return
    logit("Begin")
    if sysname == 'Windows':
	logit("Skip : unsupported on Windows")
        return
    for d in (pathbin, pathlib, pathusr):
        for dirname, dirnames, filenames in os.walk(d):
            for subdirname in dirnames:
                dirpath = os.path.join(dirname, subdirname)
                try:
                    os.chmod(dirpath, 0755)
                    msg = "Setting %s permissions to 0755" % dirpath
                except:
                    msg = "Error while trying to set %s permissions to 0755" % dirpath
                logit(msg)

def log_file_info(path):
    try:
        info = os.lstat(path)
    except:
	msg = "Error while trying to get [%s] os.lstat information" % path
        logit(msg)
	return
    string = "uid[%d] gid[%d] perms[%s] file[%s]" % (info.st_uid, info.st_gid, oct(info.st_mode & 0777), path)
    logit(string)

def dump_install_content():
    logit("Begin")
    if sysname == 'Windows':
        return
    if not hasattr(os, "walk"):
        return
    for d in (pathbin, pathlib, pathusr):
        for dirname, dirnames, filenames in os.walk(d):
            for subdirname in dirnames:
                dirpath = os.path.join(dirname, subdirname)
                log_file_info(dirpath)
            for filename in filenames:
                filepath = os.path.join(dirname, filename)
                log_file_info(filepath)

try:
    install_pythonlink()
    move_usr_to_opt()
    missing_dirs()
    convert_svclinks()
    install_cron()
    install_rc()
    gen_keys()
    install_profile()
    install_etc_path()
    install_bash_completion()
    move_host_mode()
    nodeconf_params()
    purge_collector_api_cache()
    chmod_directories()
    dump_install_content()
except:
    save_exc()
    sys.exit(1)
  0707010000b9a9000081a400000000000000000000000154fc7bfb000001b1000000660000000300000000000000000000002500000000root/opt/opensvc/bin/postinstall.cmd  @echo off

set OSVCROOT=%~1
if %OSVCROOT:~-1%==\ set OSVCROOT=%OSVCROOT:~0,-1%
set OSVCPYTHONROOT=%OSVCROOT%\python
set OSVCPYTHONEXEC=%OSVCPYTHONROOT%\python.exe
set PYTHONPATH=%OSVCROOT%\lib
call "%OSVCROOT%\inpath.cmd" OSVCROOT
call "%OSVCROOT%\inpath.cmd" OSVCPYTHONROOT
"%OSVCPYTHONEXEC%" "%OSVCROOT%\bin\postinstall" 
if errorlevel 1 (
   echo Failure Reason Given is %errorlevel%
   pause
    exit /b %errorlevel%
)
exit /b 0
   0707010000b9ab000081ed00000000000000000000000154fc7bfb00003e03000000660000000300000000000000000000001c00000000root/opt/opensvc/bin/svcmgr   #!/usr/bin/env /opt/opensvc/bin/python
#
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from __future__ import print_function
import sys
import os
import optparse

#
# add project lib to path
#
pathsvc = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
pathetc = os.path.join(pathsvc, 'etc')
sys.path.append(os.path.join(pathsvc, 'lib'))
prog = os.path.basename(__file__)
action = None

import svcBuilder
import rcStatus
import rcOptParser
import rcExceptions as ex
from rcUtilities import ximport
node_mod = ximport('node')

build_err = False

try:
    from version import version
except:
    version = "dev"

def _exit(r):
    node.close()
    if build_err:
        sys.exit(1)
    sys.exit(r)

node = node_mod.Node()

def install_service(svcnames, src_env):
    if len(svcnames) != 1:
        print("only one service must be specified", file=sys.stderr)
        return 1

    if not os.path.exists(src_env):
        print("%s does not exists"%src_env, file=sys.stderr)
        return 1

    try:
        _install_service(svcnames[0], src_env)
    except Exception as e:
        print(e)
        return 1

    return 0

def _install_service(svcname, src_env):
    import shutil

    pathsvc = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
    pathetc = os.path.join(pathsvc, 'etc')
    pathbin = os.path.join(pathsvc, 'bin')

    # install env file in etc/
    src_env = os.path.realpath(src_env)
    dst_env = os.path.join(pathetc, svcname+'.env')
    if dst_env != src_env:
        shutil.copy2(src_env, dst_env)

    # install .dir
    d = os.path.join(pathetc, svcname+'.dir')
    if not os.path.exists(d):
        os.makedirs(d)

    import platform
    sysname, nodename, x, x, machine, x = platform.uname()
    if sysname == 'Windows':
        return

    # install .d
    ld = os.path.join(pathetc, svcname+'.d')
    if not os.path.exists(ld):
        os.symlink(d, ld)
    elif not os.path.exists(ld+os.sep):
        # repair broken symlink
        os.unlink(ld)
        os.symlink(d, ld)

    # install svcmgr link
    ls = os.path.join(pathetc, svcname)
    s = os.path.join(pathbin, 'svcmgr')
    if not os.path.exists(ls):
        os.symlink(s, ls)
    elif os.path.realpath(s) != os.path.realpath(ls):
        os.unlink(ls)
        os.symlink(s, ls)


__ver = prog + " version " + version
__usage = "%prog [options] command\n\n"
parser = optparse.OptionParser(version=__ver, usage=__usage + rcOptParser.format_desc())
parser.add_option("--debug", default=False,
		  action="store_true", dest="debug",
                  help="debug mode")
parser.add_option("--dry-run", default=False, action="store_true", dest="dry_run",
                  help="Show the action execution plan")
parser.add_option("--disable-rollback", default=False, action="store_true", dest="disable_rollback",
                  help="Exit without resource activation rollback on start action error")
parser.add_option("-p", "--parallel", default=False, action="store_true", dest="parallel",
                  help="start actions on specified services in parallel")
parser.add_option("--ignore-affinity", default=False, action="store_true", dest="ignore_affinity",
                  help="ignore service anti-affinity with other services check")
parser.add_option("--remote", default=False, action="store_true", dest="remote",
                  help="flag action as triggered by a remote node. used to avoid recursively triggering actions amongst nodes")
parser.add_option("-f", "--force", default=False, action="store_true", dest="force",
                  help="force action, ignore sanity check warnings")
parser.add_option("--cron", default=False, action="store_true", dest="cron",
                  help="used by cron'ed action to tell the collector to treat the log entries as such")
parser.add_option("--slaves", default=False, action="store_true", dest="slaves",
                  help="option to set to limit the action scope to all slave service resources")
parser.add_option("--slave", default=None, action="store", dest="slave",
                  help="option to set to limit the action scope to the service resources in the specified, comma-sperated, slaves")
parser.add_option("--master", default=False, action="store_true", dest="master",
                  help="option to set to limit the action scope to the master service resources")
parser.add_option("-c", "--cluster", default=False, action="store_true", dest="cluster",
                  help="option to set when excuting from a clusterware to disable safety net")
parser.add_option("-i", "--interactive", default=False, action="store_true", dest="interactive",
                  help="prompt user for a choice instead of going for defaults or failing")
parser.add_option("--rid", default=None, action="store", dest="parm_rid",
                  help="comma-separated list of resource to limit action to")
parser.add_option("--tags", default=None, action="store", dest="parm_tags",
                  help="comma-separated list of resource tags to limit action to")
parser.add_option("--resource", default=[], action="append",
                  help="a resource definition in json dictionary format fed to create or update")
parser.add_option("--provision", default=False, action="store_true", dest="provision",
                  help="provision the service in addition to env file creation. defaults to False.")
parser.add_option("--waitlock", default=60, action="store", dest="parm_waitlock", type="int",
                  help="comma-separated list of resource tags to limit action to")
parser.add_option("--to", default=None, action="store", dest="parm_destination_node",
                  help="remote node to start or migrate the service to")
parser.add_option("--attach", default=False,
                  action="store_true", dest="attach",
                  help="attach the modulesets specified during a compliance check/fix/fixable command")
parser.add_option("--module", default="",
                  action="store", dest="module",
                  help="compliance, set module list")
parser.add_option("--moduleset", default="",
                  action="store", dest="moduleset",
                  help="compliance, set moduleset list. The 'all' value can be used in conjonction with detach.")
parser.add_option("--ruleset", default="",
                  action="store", dest="ruleset",
                  help="compliance, set ruleset list. The 'all' value can be used in conjonction with detach.")
parser.add_option("--ruleset-date", default="",
                  action="store", dest="ruleset_date",
                  help="compliance, use rulesets valid on specified date")
parser.add_option("--param", default=None,
                  action="store", dest="param",
                  help="point a service configuration parameter for the 'get' and 'set' actions")
parser.add_option("--value", default=None,
                  action="store", dest="value",
                  help="set a service configuration parameter value for the 'set --param' action")
parser.add_option("--duration", default=None, action="store", dest="duration", type="int",
                  help="a duration expressed in minutes. used with the 'collector ack unavailability' action")
parser.add_option("--account", default=False, action="store_true", dest="account",
                  help="decides that the unavailabity period should be deduced from the service availability anyway. used with the 'collector ack unavailability' action")
parser.add_option("--begin", default=None, action="store", dest="begin",
                  help="a begin date expressed as 'YYYY-MM-DD hh:mm'. used with the 'collector ack unavailability' action")
parser.add_option("--end", default=None, action="store", dest="end",
                  help="a end date expressed as 'YYYY-MM-DD hh:mm'. used with the 'collector ack unavailability' action")
parser.add_option("--comment", default=None, action="store", dest="comment",
                  help="a comment to log when used with the 'collector ack unavailability' action")
parser.add_option("--author", default=None, action="store", dest="author",
                  help="the acker name to log when used with the 'collector ack unavailability' action")
parser.add_option("--id", default=0, action="store", dest="id", type="int",
                  help="specify an id to act on")
parser.add_option("--table", default=False, action="store_true", dest="table",
                  help="use table representation of collector data instead of the default itemized list of objects and properties")
parser.add_option("--refresh", default=False, action="store_true", dest="refresh",
                  help="drop last resource status cache and re-evaluate before printing with the 'print [json] status' commands")
parser.add_option("--tag", default=None,
                  action="store", dest="tag",
                  help="a tag specifier used by 'collector create tag', 'collector add tag', 'collector del tag'")
parser.add_option("--like", default="%",
                  action="store", dest="like",
                  help="a sql like filtering expression. leading and trailing wildcards are automatically set.")


cluster = False
svcnames = []
cmd = os.path.basename(__file__)

try:
    if cmd == 'allservices':
        node.build_services()
    elif cmd == 'allupservices':
        node.build_services(status=rcStatus.UP)
        node.build_services(status=rcStatus.WARN)
    elif cmd == 'alldownservices':
        node.build_services(status=rcStatus.DOWN)
    elif cmd == 'allprimaryservices':
        node.build_services(onlyprimary=True)
    elif cmd == 'allsecondaryservices':
        node.build_services(onlysecondary=True)
    elif cmd == 'svcmgr':
        parser.add_option("-s", "--service", default=None, action="store", dest="parm_svcs",
                  help="comma-separated list of service to operate on")
        parser.add_option("--status", default=None, action="store", dest="parm_status",
                  help="operate only on service in the specified status (up/down/warn)")
        parser.add_option("--onlyprimary", default=None, action="store_true", dest="parm_primary",
                  help="operate only on service flagged for autostart on this node")
        parser.add_option("--onlysecondary", default=None, action="store_true", dest="parm_secondary",
                  help="operate only on service not flagged for autostart on this node")
        parser.add_option("--envfile", default=None, action="store", dest="param_envfile",
                  help="the envfile to use when installing a service")
    else:
        svcname = cmd.split('.')
        if len(svcname) > 1 and svcname[-1] == 'cluster':
                cluster = True
                svcname = ".".join(svcname[:-1])
        elif len(svcname) > 1 and svcname[-1] == 'stonith':
                cluster = True
                svcname = ".".join(svcname[:-1])
                action = "stonith"
        else:
            svcname = cmd
        node.build_services(svcnames=svcname)
except ex.excError:
    build_err = True

docker_argv = None
if len(sys.argv) > 1 and 'docker' in sys.argv:
    pos = sys.argv.index('docker')
    if len(sys.argv) > pos + 1:
        docker_argv = sys.argv[pos+1:]
    else:
        docker_argv = []
    sys.argv = sys.argv[:pos+1]

(options, args) = parser.parse_args()
if action is not None:
    args = ['stonith']

if node.svcs is None:
    parm_primary = False
    parm_secondary = False
    parm_status = None

    if hasattr(options, "parm_svcs") and options.parm_svcs is not None:
        svcnames = options.parm_svcs.split(',')

    if hasattr(options, "parm_status") and options.parm_status is not None:
        status = rcStatus.status_value(options.parm_status)
    else:
        status = None

    if hasattr(options, "parm_primary") and options.parm_primary is not None and \
       hasattr(options, "parm_secondary") and options.parm_secondary is not None:
        node.close()
        parser.error("--onlyprimary and --onlysecondary are exclusive")

    if hasattr(options, "parm_primary") and options.parm_primary is not None:
        onlyprimary = options.parm_primary
    else:
        onlyprimary = False

    if hasattr(options, "parm_secondary") and options.parm_secondary is not None:
        onlysecondary = options.parm_secondary
    else:
        onlysecondary = False

    try:
        node.build_services(svcnames=svcnames,
                            status=status,
                            onlyprimary=onlyprimary,
                            onlysecondary=onlysecondary)
    except ex.excError:
        build_err = True

if len(args) is 0:
    node.close()
    parser.error("Missing action")

if action is None:
    action = '_'.join(args)

if not action in rcOptParser.supported_actions():
    node.close()
    parser.set_usage(__usage + rcOptParser.format_desc(action=action))
    parser.error("unsupported action")

if node.svcs is not None:
    svcnames = map(lambda x: x.svcname, node.svcs)

if action in ("create", "install") and hasattr(options, "parm_svcs") and options.parm_svcs is not None:
    svcnames = options.parm_svcs.split(',')

if cmd == 'svcmgr' and len(svcnames) == 0:
    sys.stderr.write("""No service specified. Try:
 allservices
 allupservices
 alldownservices
 allprimaryservices
 allsecondaryservices
 svcmgr -s svcname,...
 svcname
""")
    _exit(1)

if action == 'install':
    r = install_service(svcnames, options.param_envfile)
    _exit(r)

if hasattr(options, "parm_rid") and options.parm_rid is not None:
   rid = options.parm_rid.split(',')
else:
   rid = []

if options.slave is not None:
   slave = options.slave.split(',')
else:
   slave = None

if options.parm_tags is not None:
   tags = set(options.parm_tags.split(','))
else:
   tags = set([])

if action in ['create', 'update']:
    data = getattr(svcBuilder, action)(svcnames, options.resource, interactive=options.interactive, provision=options.provision)
    if options.provision:
        rid = data.get("rid", [])
        # force a refresh of node.svcs
        # don't push to the collector yet
        del(node.svcs)
        node.svcs = None
        try:
            node.build_services(svcnames=svcnames, autopush=False)
        except ex.excError:
            build_err = True
        if len(node.svcs) == 1:
            node.svcs[0].action("provision", rid=rid)
    _exit(data["ret"])

node.options.parallel = options.parallel
node.options.waitlock = options.parm_waitlock

for s in node.svcs:
    s.options = options
    s.force = options.force
    s.remote = options.remote
    s.cron = options.cron
    s.options.slaves = options.slaves
    s.options.slave = slave
    s.options.master = options.master
    if cluster:
        s.cluster = cluster
    else:
        s.cluster = options.cluster
    s.destination_node = options.parm_destination_node
    if docker_argv is not None:
        s.docker_argv = docker_argv

err = node.do_svcs_action(action, rid, tags)

try:
    import logging
    logging.shutdown()
except:
    pass

_exit(err)
 0707010000b9ac000081ed00000000000000000000000154fc7bfb000024ac000000660000000300000000000000000000001c00000000root/opt/opensvc/bin/svcmon   #!/usr/bin/env /opt/opensvc/bin/python
#
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import sys
import os
import optparse
import string
import platform

#
# add project lib to path
#
pathsvc = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
pathetc = os.path.join(pathsvc, 'etc')
sys.path.append(os.path.join(pathsvc, 'lib'))
prog = os.path.basename(__file__)

import svcBuilder
import rcExceptions as ex
from rcUtilities import *
from lock import *
import node
from rcStatus import colorize

sysname, nodename, x, x, machine, x = platform.uname()

if sysname == "Windows":                                                                                                                              
    mp = False                                                                                                                                        
else:                                                                                                                                                 
    try:                                                                                                                                              
        from multiprocessing import Process, Queue, Lock                                                                                              
        mp = True                                                                                                                                     
    except:                                                                                                                                           
        mp = False                                                                                                                                    

try:
    from version import version
except:
    version = "dev"

def svcmon_verbose(svcs):
    for svc in svcs:
        try:
            svc.print_status()
        except:
            pass

def max_len(svcs):
    _len = 7
    for svc in svcs:
        l = len(getattr(svc, "svcname"))
        if _len < l:
            _len = l
        for container in svc.get_resources('container'):
            l = len(getattr(container, "name")) + 2
            if _len < l:
                _len = l
    return _len

def svcmon_normal1(svc,upddb=False, fmt=None, queue=None, lock=None):
    # don't schedule svcmon updates for encap services.
    # those are triggered by the master node
    status = svc.group_status()
    l = []
    buff = fmt % (
              svc.svcname,
              svc.svctype,
              svc.svcmode,
              colorize(status["container"]),
              colorize(status["ip"]),
              colorize(status["disk"]),
              colorize(status["fs"]),
              colorize(status.get("share", "n/a")),
              colorize(status["app"]),
              colorize(status["hb"]),
              colorize(status["sync"]),
              colorize(status["avail"]),
              colorize(status["overall"]),
              svc.frozen())
    l.append(buff)
    containers = svc.get_resources("container")
    if len(containers) > 0 and svc.has_encap_resources:
        for container in containers:
            try:
                s = svc.encap_json_status(container)
            except ex.excNotAvailable as e:
                s = {'resources': [],
                     'ip': 'n/a',
                     'disk': 'n/a',
                     'sync': 'n/a',
                     'hb': 'n/a',
                     'container': 'n/a',
                     'fs': 'n/a',
                     'share': 'n/a',
                     'app': 'n/a',
                     'avail': 'n/a',
                     'overall': 'n/a'}

            buff = fmt % (
                      ' @'+container.name,
                      '',
                      container.type.replace('container.', ''),
                      colorize(s["container"]),
                      colorize(s["ip"]),
                      colorize(s["disk"]),
                      colorize(s["fs"]),
                      colorize(s.get("share", "n/a")),
                      colorize(s["app"]),
                      colorize(s["hb"]),
                      colorize(s["sync"]),
                      colorize(s["avail"]),
                      colorize(s["overall"]),
                      '')
            l.append(buff)

    if lock is not None:
        lock.acquire()
    print('\n'.join(l))
    if lock is not None:
        lock.release()

    if upddb:
        o = svc.svcmon_push_lists(status)
        _size = len(str(o))
        if queue is None or _size > 30000:
            # multiprocess Queue not supported, can't combine results
            g_vars, g_vals, r_vars, r_vals = svc.svcmon_push_lists(status)
            svc.node.collector.call('svcmon_update_combo', g_vars, g_vals, r_vars, r_vals)
        else:
            queue.put(svc.svcmon_push_lists(status))

def svcmon_normal(svcs, upddb=False):
    fmt = '%-' + str(max_len(svcs)) + 's'
    fmt += ' %-7s %-9s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-6s'

    print(fmt % ("service", "service", "container", "container", "ip    ", "disk  ", "fs    ", "share ", "app   ", "hb    ", "sync  ", "avail ", "overall", "      "))
    print(fmt % ("name   ", "type   ", "type     ", "status   ", "status", "status", "status", "status", "status", "status", "status", "status", "status ", "frozen"))
    print(fmt % ("-------", "-------", "---------", "---------", "------", "------", "------", "------", "------", "------", "------", "------", "-------", "------"))
    ps = []
    queues = {}

    for svc in svcs:
        if svc.encap and upddb:
            continue
        if not mp:
            svcmon_normal1(svc, upddb, fmt, None)
            continue
        try:
            queues[svc.svcname] = Queue(maxsize=32000)
            lock = Lock()
        except:
            # some platform don't support Queue's synchronize (bug 3770)
            queues[svc.svcname] = None
        p = Process(target=svcmon_normal1, args=(svc, upddb, fmt, queues[svc.svcname], lock))
        p.start()
        ps.append(p)
    for p in ps:
        p.join()

    if upddb and mp:
        g_vals = []
        r_vals = []
        if options.delay > 0:
            import random
            import time
            delay = int(random.random()*options.delay)
            time.sleep(delay)

        for svc in svcs:
            if svc.svcname not in queues or queues[svc.svcname] is None:
                continue
            if queues[svc.svcname].empty():
                continue
            g_vars, _g_vals, r_vars, _r_vals = queues[svc.svcname].get()
            g_vals.append(_g_vals)
            r_vals.append(_r_vals)
        if len(g_vals) > 0:
            svc.node.collector.call('svcmon_update_combo', g_vars, g_vals, r_vars, r_vals)

__ver = prog + " version " + version
__usage = "%prog [options]\n\nDisplay services status"
parser = optparse.OptionParser(version=__ver, usage=__usage)
parser.add_option("--service", default="", action="store", dest="parm_svcs",
                  help="comma-separated list of service to display status of")
parser.add_option("--updatedb", default=False, action="store_true", dest="upddb",
                  help="update resource status in central database")
parser.add_option("--verbose", default=False, action="store_true", dest="verbose",
                  help="display per-resource status for each selected service")
parser.add_option("--maxdelaydb", default=0, action="store", type="int", dest="delay",
                  help="introduce a random delay before pushing to database to level the load on the collector")
parser.add_option("--debug", default=False, action="store_true", dest="debug",
                  help="debug mode")

(options, args) = parser.parse_args()

if options.upddb:
    lockf = 'svcmon.lock'
    try:
        lockfd = monlock(fname=lockf)
    except ex.excError:
        sys.exit(1)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(1)
 
node = node.Node()

if len(options.parm_svcs) > 0:
    node.build_services(svcnames=options.parm_svcs.split(','))
else:
    node.build_services()

for s in node.svcs:
    s.options.debug = options.debug
    s.options.refresh = options.upddb

if options.verbose:
    svcmon_verbose(node.svcs)
else:
    svcmon_normal(node.svcs, options.upddb)

node.close()

if options.upddb:
    try:
        monunlock(lockfd)
    except ex.excError:
        sys.exit(1)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(1)

sys.exit(0)
0707010000b9ad000081a400000000000000000000000154fc7bfb00000184000000660000000300000000000000000000003300000000root/opt/opensvc/bin/systemd.opensvc-agent.service    [Unit]
Description=OpenSVC Agent
Documentation=http://docs.opensvc.com/ file:/opt/opensvc/usr/share/doc/

[Service]
Type=idle
Environment="PATH=/opt/opensvc/bin:/opt/opensvc/etc:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
ExecStart=/opt/opensvc/bin/opensvc.init start
ExecStop=/opt/opensvc/bin/opensvc.init stop
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
0707010000b9ae000041ed000000000000000000000002550eba8a00000000000000660000000300000000000000000000001500000000root/opt/opensvc/lib  0707010000b9af000081a400000000000000000000000154fc7bfb0000003f000000660000000300000000000000000000002900000000root/opt/opensvc/lib/_multiprocessing.py  # workaround sol 8 & 9 python 2.6.2 bug
import multiprocessing
 0707010000bb4d000081a400000000000000000000000154fc7bfb00000693000000660000000300000000000000000000002c00000000root/opt/opensvc/lib/checkAdvfsUsageOSF1.py   #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
import glob
import re
import os
from rcUtilities import justcall
import rcAdvfs

class check(checks.check):
    def __init__(self, svcs=[]):
        checks.check.__init__(self, svcs)

    chk_type = "fs_u"

    def find_svc(self, name):
        for svc in self.svcs:
            for rs in svc.get_res_sets('pool'):
                for r in rs.resources:
                    if r.poolname == name:
                        return svc.svcname
        return ''

    def do_check(self):
        o = rcAdvfs.Fdmns()
        r = []
        for dom in o.list_fdmns():
            try:
                d = o.get_fdmn(dom)
                r.append({
                          'chk_instance': dom,
                          'chk_value': str(d.used_pct),
                          'chk_svcname': self.find_svc(dom),
                         })
            except rcAdvfs.ExInit:
                pass
        return r
 0707010000bb4e000081a400000000000000000000000154fc7bfb00000cb6000000660000000300000000000000000000003000000000root/opt/opensvc/lib/checkBtrfsDevStatsLinux.py   # Copyright (c) 2012 Lucien Hercaud <hercaud@hercaud.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall
import os
import re
from rcGlobalEnv import rcEnv
 
class check(checks.check):
    """
# btrfs dev stats /mnt
[/dev/loop0].write_io_errs   0
[/dev/loop0].read_io_errs    0
[/dev/loop0].flush_io_errs   0
[/dev/loop0].corruption_errs 0
[/dev/loop0].generation_errs 0
[/dev/loop1].write_io_errs   0
[/dev/loop1].read_io_errs    0
[/dev/loop1].flush_io_errs   0
[/dev/loop1].corruption_errs 0
[/dev/loop1].generation_errs 0
    """
    chk_type = "btrfs"
 
    def _get_dev_stats(self, mntpt, data):
        cmd = ['btrfs', 'dev', 'stats', mntpt]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return data
        for line in out.split('\n'):
            l = line.split()
            if len(l) != 2:
                continue
            key, val = l
            l = key.split('.')
            if len(l) != 2:
                continue
            dev, err_type = l
            dev = dev.lstrip('[').rstrip(']')
            if dev not in data:
                data[dev] = {}
            data[dev][err_type] = val
        return data

    def get_dev_stats(self):
        mntpts = self.get_btrfs_mounts()
        data = {}
        if mntpts is None:
            return data
        for mntpt in mntpts:
            data = self._get_dev_stats(mntpt, data)
        return data

    def get_btrfs_mounts(self):
        mntpts = []
        p = '/proc/mounts'
        if not os.path.exists(p):
            return
        with open(p, 'r') as f:
            buff = f.read()
        for line in buff.split('\n'):
            if 'btrfs' not in line:
                continue
            l = line.split()
            if len(l) < 2:
                continue
            mntpt = l[1]
            if not os.path.exists(mntpt):
                continue
            mntpts.append(mntpt)
        return mntpts

    def find_svc(self, dev):
        for svc in self.svcs:
            if dev in svc.disklist():
                return svc.svcname
        return ''
 
    def do_check(self):
        r = []
        data = self.get_dev_stats()
        if data is None:
            return r
        for dev, _data in data.items():
            for err_type, val in _data.items():
                r.append({'chk_instance': dev+'.'+err_type,
                          'chk_value': val,
                          'chk_svcname': self.find_svc(dev),
                        })
        return r

if __name__ == "__main__":
    o = check()
    r = o.do_check()
    print(r)
  0707010000bb4f000081a400000000000000000000000154fc7bfb00000de8000000660000000300000000000000000000002600000000root/opt/opensvc/lib/checkEthHP-UX.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
import os
from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv
import glob
import rcEthtool

class check(checks.check):
    chk_type = "eth"

    def do_check(self):
        cmd = ["lanscan", "-q"]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        r = []
        intf = set([])
        for line in out.split("\n"):
            if len(line) == 0:
                continue
            l = line.split()
            n = len(l)
            if n == 1:
                # add interfaces with an inet config
                if self.has_inet(l[0]):
                    intf.add(l[0])
            elif n > 1:
                # add slaves for apa with an inet config
                if self.has_inet(l[0]):
                    for w in l[1:]:
                        intf.add(w)
            else:
                continue

        for i in intf:
            r += self.do_check_intf(i)

        return r

    def has_inet(self, intf):
        cmd = ["ifconfig", "lan"+intf]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return False
        if 'inet' in out:
            return True
        return False

    def do_check_intf(self, intf):
        r = []
        cmd = ["lanadmin", "-x", intf]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return []

        intf = "lan"+intf
        inst = intf + ".link"
        if "link is down" in out:
            val = "0"
        else:
            val = "1"
        r.append({
                  'chk_instance': inst,
                  'chk_value': val,
                  'chk_svcname': '',
                 })

        inst = intf + ".speed"
        val = "0"
        for line in out.split('\n'):
            if "Speed" not in line:
                continue
            try:
                val = line.split()[2]
            except:
                pass
        r.append({
                  'chk_instance': inst,
                  'chk_value': val,
                  'chk_svcname': '',
                 })

        inst = intf + ".autoneg"
        val = "0"
        for line in out.split('\n'):
            if "Autoneg" not in line:
                continue
            if " On":
                val = "1"
        r.append({
                  'chk_instance': inst,
                  'chk_value': val,
                  'chk_svcname': '',
                 })

        inst = intf + ".duplex"
        val = '0'
        for line in out.split('\n'):
            if "Speed" not in line:
                continue
            if 'Full-Duplex' in line:
                val = "1"
        r.append({
                  'chk_instance': inst,
                  'chk_value': val,
                  'chk_svcname': '',
                 })

        return r
0707010000bb50000081a400000000000000000000000154fc7bfb00000e1b000000660000000300000000000000000000002600000000root/opt/opensvc/lib/checkEthLinux.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
import os
from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv
import glob
import rcEthtool

class check(checks.check):
    chk_type = "eth"
    bonding_p = '/proc/net/bonding'

    def get_intf(self):
        intf = []
        l = glob.glob(self.bonding_p+'/*')
        for bond in l:
            intf += self.add_slaves(bond)
        rcIfconfig = __import__("rcIfconfig"+rcEnv.sysname)
        ifconfig = rcIfconfig.ifconfig()
        for i in ifconfig.intf:
            if not i.name.startswith('eth'):
                continue
            if len(i.ipaddr) == 0 and len(i.ip6addr) == 0:
                continue
            if i.name in intf:
                continue
            intf.append(i.name)
        return intf

    def add_slaves(self, bond):
        intf = []
        try:
            f = open(bond, 'r')
            buff = f.read()
            f.close()
        except:
            return intf
        for line in buff.split('\n'):
            if line.startswith('Slave Interface:'):
                intf.append(line.split()[-1])
        return intf

    def do_check(self):
        l = self.get_intf()
        if len(l) == 0:
            return self.undef
        r = []
        for intf in l:
            r += self.do_check_intf(intf)
        return r

    def do_check_intf(self, intf):
        r = []
        try:
            ethtool = rcEthtool.Ethtool(intf)
            ethtool.load()
        except rcEthtool.LoadError:
            return []

        inst = intf + ".speed"
        val = ethtool.speed
        if val is not None:
            val.replace('Mb/s', '')
            r.append({
                  'chk_instance': inst,
                  'chk_value': val,
                  'chk_svcname': '',
                 })

        inst = intf + ".autoneg"
        if val is not None:
            val = ethtool.auto_negotiation
            if val == 'on':
                val = '1'
            else:
                val = '0'
            r.append({
                  'chk_instance': inst,
                  'chk_value': val,
                  'chk_svcname': '',
                 })

        inst = intf + ".duplex"
        val = ethtool.duplex
        if val is not None:
            if val == 'Full':
                val = '1'
            else:
                val = '0'
            r.append({
                  'chk_instance': inst,
                  'chk_value': val,
                  'chk_svcname': '',
                 })

        inst = intf + ".link"
        val = ethtool.link_detected
        if val is not None:
            if val == 'yes':
                val = '1'
            else:
                val = '0'
            r.append({
                  'chk_instance': inst,
                  'chk_value': val,
                  'chk_svcname': '',
                 })

        return r
 0707010000bb51000081a400000000000000000000000154fc7bfb00002ac4000000660000000300000000000000000000002600000000root/opt/opensvc/lib/checkEthSunOS.py #
# Copyright (c) 2012 Lucien Hercaud <hercaud@hercaud.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
import os
import re
from rcUtilities import justcall
from rcUtilitiesSunOS import get_solaris_version
from rcGlobalEnv import rcEnv

"""
# ifconfig -a
lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        inet 127.0.0.1 netmask ff000000
lo0:1: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        zone frcp00vpd0385
        inet 127.0.0.1 netmask ff000000
lo0:2: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        zone frcp00vpd0388
        inet 127.0.0.1 netmask ff000000
lo0:3: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        zone frcp00vpd0192
        inet 127.0.0.1 netmask ff000000
lo0:4: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        zone frcp00vpd0192
        inet 128.1.1.192 netmask ffff0000
lo0:5: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        zone frcp00vpd0179
        inet 127.0.0.1 netmask ff000000
aggr1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        inet 172.31.4.195 netmask ffffff00 broadcast 172.31.4.255
        ether 0:15:17:bb:85:58
aggr1:1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        zone frcp00vpd0385
        inet 172.31.4.180 netmask ffffff00 broadcast 172.31.4.255
aggr1:2: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        zone frcp00vpd0388
        inet 172.31.4.183 netmask ffffff00 broadcast 172.31.4.255
aggr1:3: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        zone frcp00vpd0179
        inet 172.31.4.67 netmask ffffff00 broadcast 172.31.4.255
aggr2: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 5
        inet 172.31.195.195 netmask ffffff00 broadcast 172.31.195.255
        ether 0:15:17:bb:85:59
bnx3: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 4
        inet 55.16.201.195 netmask fffffc00 broadcast 55.16.203.255
        ether 0:24:e8:35:9d:dd
bnx3:1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 4
        zone frcp00vpd0385
        inet 55.16.201.142 netmask fffffc00 broadcast 55.16.203.255
bnx3:2: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 4
        zone frcp00vpd0388
        inet 55.16.201.145 netmask fffffc00 broadcast 55.16.203.255
bnx3:3: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 4
        zone frcp00vpd0179
        inet 55.16.202.98 netmask fffffc00 broadcast 55.16.203.255

Solaris 10
==========
# ndd -get /dev/bnx3 link_speed
1000
# ndd -get /dev/bnx3 link_duplex
1
# ndd -get /dev/bnx3 link_status
1
kstat -p | grep link_ | grep  ce:0:ce0:link
ce:0:ce0:link_asmpause  0
ce:0:ce0:link_duplex    2
ce:0:ce0:link_pause     0
ce:0:ce0:link_speed     1000
ce:0:ce0:link_up        1

Solaris 11
==========
# dladm show-link -p -o link,class,over l226g0
l226g0:vnic:aggr0
# dladm show-link -p -o link,class,over aggr0
aggr0:aggr:net0 net2
# dladm show-link -p -o link,class,over net0
net0:phys:
# dladm show-phys -p -o state,speed,duplex net0
up:1000:full
# dladm show-link -p -o link,class,over net2
# dladm show-phys -p -o state,speed,duplex net2
up:1000:full
"""

class check(checks.check):
    chk_type = "eth"
    kstat = None

    def _findphys(self, netif):
        res = ""
        cmd = ['/usr/sbin/dladm', 'show-link', '-p', '-o', 'link,class,over', netif]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return ""
        lines = out.split('\n')
        for line in lines:
            if len(line) == 0:
                break
            v = line.split(':')
            if v[1] == 'phys':
                self.l[self.topif].add(v[0])
            else:
                ifs = v[2].split(' ')
                for i in ifs:
                    res = self._findphys(i)
            return "OK"

    def do_check(self):
        self.osver = get_solaris_version()
        self.ifs = []
        cmd = ['/usr/sbin/ifconfig', '-a']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) == 0:
            return self.undef
        for line in lines:
            if line.startswith(' '):
                continue
            if line.startswith('lo'):
                continue
            if line.startswith('sppp'):
                continue
            if self.osver < 11:
                if line.startswith('aggr'):
                    continue
            if 'index' not in line:
                continue
            l = line.split(':')
            if 'index' not in l[1]:
                continue
            if len(l[0]) < 3:
                continue
            if l[0] in self.ifs:
                continue
            else:
                self.ifs.append(l[0])
        if self.osver >= 11:
            self.l = {}
            for ifn in self.ifs:
                if ifn not in self.l:
                    self.l[ifn] = set([])
                    self.topif = ifn
                    ret = self._findphys(ifn)
            cmd = ['/usr/sbin/dladm', 'show-phys', '-p', '-o', 'link,state,speed,duplex,device']
            out, err, ret = justcall(cmd)
            if ret != 0:
                return self.undef
            lines = out.split('\n')
            if len(lines) == 0:
                return self.undef
            self.phys = lines

        r = []
        r += self.do_check_speed()
        r += self.do_check_duplex()
        r += self.do_check_link()
        return r

    def do_check_speed(self):
        r = []
        if self.osver >= 11:
            for ifn in self.ifs:
                for phy in self.l[ifn]:
                    for line in self.phys:
                        if line.startswith(phy+':'):
                            l = line.split(':')
                            val = l[2]
                            r.append({
                                      'chk_instance': '%s.%s.speed'%(ifn,l[4]),
                                      'chk_value': str(val),
                                      'chk_svcname': '',
                                     })
            return r
        for ifn in self.ifs:
            val = self.get_param(ifn, 'link_speed')
            r.append({
                      'chk_instance': '%s.speed'%ifn,
                      'chk_value': str(val),
                      'chk_svcname': '',
                     })
        return r

    def do_check_duplex(self):
        r = []
        if self.osver >= 11:
            for ifn in self.ifs:
                for phy in self.l[ifn]:
                    for line in self.phys:
                        if line.startswith(phy+':'):
                            l = line.split(':')
                            if l[3] != 'full':
                                val = 1
                            else:
                                val = 0
                            r.append({
                                      'chk_instance': '%s.%s.duplex'%(ifn,l[4]),
                                      'chk_value': str(val),
                                      'chk_svcname': '',
                                     })
            return r
        for ifn in self.ifs:
            val = self.get_param(ifn, 'link_duplex')
            r.append({
                      'chk_instance': '%s.duplex'%ifn,
                      'chk_value': str(val),
                      'chk_svcname': '',
                     })
        return r

    def do_check_link(self):
        r = []
        if self.osver >= 11:
            for ifn in self.ifs:
                for phy in self.l[ifn]:
                    for line in self.phys:
                        if line.startswith(phy+':'):
                            l = line.split(':')
                            if l[1] != 'up':
                                val = 1
                            else:
                                val = 0
                            r.append({
                                      'chk_instance': '%s.%s.link'%(ifn,l[4]),
                                      'chk_value': str(val),
                                      'chk_svcname': '',
                                     })
            return r
        for ifn in self.ifs:
            val = self.get_param(ifn, 'link_status')
            r.append({
                      'chk_instance': '%s.link'%ifn,
                      'chk_value': str(val),
                      'chk_svcname': '',
                     })
        return r

    def get_param(self, intf, param):
        val = self.get_from_ndd(intf, param)
        if val is None:
            val = self.get_from_kstat(intf, param)
        return val

    def get_from_ndd(self, intf, param):
        cmd = ['/usr/sbin/ndd', '-get', '/dev/'+intf, param]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return
        return out.strip()

    def get_from_kstat(self, intf, param):
        inst = re.sub(r"[a-zA-Z]+", "", intf)
        drv = re.sub(r"[0-9]+", "", intf)
        data = {
          "ce": {
            "link_status": ":"+intf+":link_up",
            "link_duplex": ":"+intf+":link_duplex",
            "link_speed": ":"+intf+":link_speed",
          },
          "nxge": {
            "link_status": ":mac:link_up",
            "link_duplex": ":mac:link_duplex",
            "link_speed": ":Port Stats:link_speed",
          },
        }

        if self.kstat is None:
            cmd = ['/usr/bin/kstat', '-p']
            out, err, ret = justcall(cmd)
            if ret == 0:
                self.kstat = out

        if self.kstat is None:
            return

        lines = self.kstat.split('\n')

        if len(lines) == 0:
            return

        prefix = ':'.join((drv, inst))
        if drv not in data:
            return
        _data = data[drv]
        if param not in _data:
            return
        _param = _data[param]
        patt = prefix + _param

        for line in lines:
            if not line.startswith(patt):
                continue
            l = line.split()
            return l[-1]
        return
0707010000bb52000081a400000000000000000000000154fc7bfb000006ff000000660000000300000000000000000000002500000000root/opt/opensvc/lib/checkFmFmadm.py  #
# Copyright (c) 2012 Lucien Hercaud <hercaud@hercaud.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
import os
from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv

class check(checks.check):
    prefixes = [os.path.join(os.sep, "usr", "sbin")]
    fmadm = "fmadm"
    chk_type = "fm"
    chk_name = "Solaris fmadm"

    def find_fmadm(self):
        if which(self.fmadm):
            return self.fmadm
        for prefix in self.prefixes:
            fmadm = os.path.join(prefix, self.fmadm)
            if os.path.exists(fmadm):
                return fmadm
        return

    def do_check(self):
        r = self.do_check_ldpdinfo()
        return r

    def do_check_ldpdinfo(self):
        fmadm = self.find_fmadm()
        if fmadm is None:
            return self.undef
        os.chdir(rcEnv.pathtmp)
        cmd = [fmadm, 'faulty']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        r = []
        r.append({
              'chk_instance': 'faults ',
              'chk_value': str(len(out)),
              'chk_svcname': '',
            })
        return r
 0707010000bb53000081a400000000000000000000000154fc7bfb00000963000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/checkFmOpenManage.py #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
import os
from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv

class check(checks.check):
    omreport = "/opt/dell/srvadmin/bin/omreport"
    chk_type = "om"
    chk_name = "OpenManage"

    def find_omreport(self):
        if which(self.omreport):
            return self.omreport
        return

    def do_check(self):
        r = self.do_check_system()
        r += self.do_check_chassis()
        return r

    def do_check_chassis(self):
        return self.do_check_gen("chassis")

    def do_check_system(self):
        return self.do_check_gen("system")

    def do_check_gen(self, command):
        omreport = self.find_omreport()
        if omreport is None:
            return self.undef
        cmd = [omreport, command]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) == 0:
            return self.undef
        r = []
        for line in lines:
            l = line.split(" : ")
            if len(l) != 2:
                continue
            inst = l[1].strip().lower()
            state = l[0].strip().lower()
            if state == "severity":
                continue
            elif state == "ok":
                state = 0
            else:
                state = 1
            r.append({
                      'chk_instance': inst,
                      'chk_value': str(state),
                      'chk_svcname': '',
                     })
        return r

if __name__ == "__main__":
    from rcUtilities import printplus
    o = check()
    tab = o.do_check()
    printplus(tab)

 0707010000bb54000081a400000000000000000000000154fc7bfb000006fe000000660000000300000000000000000000002800000000root/opt/opensvc/lib/checkFsInodeAIX.py   #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall

class check(checks.check):
    chk_type = "fs_i"

    def find_svc(self, mountpt):
        for svc in self.svcs:
            for rs in svc.get_res_sets('fs'):
                for r in rs.resources:
                    if r.mountPoint == mountpt:
                        return svc.svcname
        return ''

    def do_check(self):
        cmd = ['df', '-i']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 2:
            return self.undef
        r = []
        for line in lines[1:]:
            l = line.split()
            if len(l) != 7:
                continue
            if l[1] == '-':
                continue
            if ":/" in l[0]:
                continue
            r.append({
                      'chk_instance': l[6],
                      'chk_value': l[5],
                      'chk_svcname': self.find_svc(l[6]),
                     })
        return r
  0707010000bb55000081a400000000000000000000000154fc7bfb0000071d000000660000000300000000000000000000002b00000000root/opt/opensvc/lib/checkFsInodeDarwin.py    #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall

class check(checks.check):
    chk_type = "fs_i"

    def find_svc(self, mountpt):
        for svc in self.svcs:
            for rs in svc.get_res_sets('fs'):
                for r in rs.resources:
                    if r.mountPoint == mountpt:
                        return svc.svcname
        return ''

    def do_check(self):
        cmd = ['df', '-lPi']
        (out,err,ret) = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 2:
            return self.undef
        r = []
        for line in lines[1:]:
            l = line.split()
            if len(l) != 9:
                continue
            if l[5].startswith('/Volumes'):
                continue
            r.append({
                      'chk_instance': l[8],
                      'chk_value': l[7],
                      'chk_svcname': self.find_svc(l[8]),
                     })
        return r
   0707010000bb56000081a400000000000000000000000154fc7bfb000008b8000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/checkFsInodeHP-UX.py #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import call

class check(checks.check):
    chk_type = "fs_i"

    def find_svc(self, mountpt):
        for svc in self.svcs:
            for rs in svc.get_res_sets('fs'):
                for r in rs.resources:
                    if r.mountPoint == mountpt:
                        return svc.svcname
        return ''

    def do_check(self):
        """
        # bdf -li
        Filesystem          kbytes    used   avail %used  iused  ifree %iuse Mounted on
        /dev/vg00/lvol3    1048576  228160  814136   22%   2105  25607    8% /
        """
        cmd = ['bdf', '-li']
        (ret, out, err) = call(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 2:
            return self.undef
        r = []
        for line in lines[1:]:
            l = line.split()
            if len(l) == 9:
                inst = ' '.join(l[8:])
                r.append({'chk_instance': inst,
                      'chk_value': l[7],
                      'chk_svcname': self.find_svc(inst),
                     }
                    )
            elif len(l) == 8:
                inst = ' '.join(l[7:])
                r.append({'chk_instance': inst,
                      'chk_value': l[6],
                      'chk_svcname': self.find_svc(inst),
                     }
                    )
        return r
0707010000bb57000081a400000000000000000000000154fc7bfb000008d3000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/checkFsInodeLinux.py #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall

class check(checks.check):
    chk_type = "fs_i"

    def find_svc(self, mountpt):
        for svc in self.svcs:
            for rs in svc.get_res_sets('fs'):
                for r in rs.resources:
                    if r.mountPoint == mountpt:
                        return svc.svcname
        return ''

    def do_check(self):
        cmd = ['df', '-lPi']
        (out,err,ret) = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 2:
            return self.undef
        r = []
        for line in lines[1:]:
            l = line.split()
            if len(l) != 6:
                continue
            # discard bind mounts: we get metric from the source anyway
            if l[0].startswith('/') and not l[0].startswith('/dev') and not l[0].startswith('//'):
                continue
            if l[5].startswith('/Volumes'):
                continue
            if "osvc_sync_" in l[0]:
                # do not report osvc sync snapshots fs usage
                continue
            if l[4] == '-':
                # vfat, btrfs, ... have no inode counter in df -i
                continue
            r.append({
                      'chk_instance': l[5],
                      'chk_value': l[4],
                      'chk_svcname': self.find_svc(l[5]),
                     })
        return r
 0707010000bb58000081a400000000000000000000000154fc7bfb000007a1000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/checkFsInodeSunOS.py #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall

class check(checks.check):
    chk_type = "fs_i"

    def find_svc(self, mountpt):
        for svc in self.svcs:
            for rs in svc.get_res_sets('fs'):
                for r in rs.resources:
                    if r.mountPoint == mountpt:
                        return svc.svcname
        return ''

    def do_check(self):
        r = []
        for t in ['ufs', 'vxfs']:
            r += self._do_check(t)
        return r

    def _do_check(self, t):
        cmd = ['df', '-F', t, '-o', 'i']
        (out,err,ret) = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 2:
            return self.undef
        r = []
        for line in lines[1:]:
            l = line.split()
            if len(l) == 5:
                l = [''] + l
            elif len(l) != 6:
                continue
            r.append({
                      'chk_instance': l[5],
                      'chk_value': l[4],
                      'chk_svcname': self.find_svc(l[5]),
                     })
        return r
   0707010000bb59000081a400000000000000000000000154fc7bfb000006fe000000660000000300000000000000000000002800000000root/opt/opensvc/lib/checkFsUsageAIX.py   #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall

class check(checks.check):
    chk_type = "fs_u"

    def find_svc(self, mountpt):
        for svc in self.svcs:
            for rs in svc.get_res_sets('fs'):
                for r in rs.resources:
                    if r.mountPoint == mountpt:
                        return svc.svcname
        return ''

    def do_check(self):
        cmd = ['df', '-P']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 2:
            return self.undef
        r = []
        for line in lines[1:]:
            l = line.split()
            if len(l) != 6:
                continue
            if l[1] == '-':
                continue
            if ":/" in l[0]:
                continue
            r.append({
                      'chk_instance': l[5],
                      'chk_value': l[4],
                      'chk_svcname': self.find_svc(l[5]),
                     })
        return r
  0707010000bb5a000081a400000000000000000000000154fc7bfb0000085b000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/checkFsUsageLinux.py #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall

class check(checks.check):
    chk_type = "fs_u"

    def find_svc(self, mountpt):
        for svc in self.svcs:
            for rs in svc.get_res_sets('fs'):
                for r in rs.resources:
                    if r.mountPoint == mountpt:
                        return svc.svcname
        return ''

    def do_check(self):
        cmd = ['df', '-lP']
        (out,err,ret) = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 2:
            return self.undef
        r = []
        for line in lines[1:]:
            l = line.split()
            if len(l) != 6:
                continue
            # discard bind mounts: we get metric from the source anyway
            if l[0].startswith('/') and not l[0].startswith('/dev') and not l[0].startswith('//'):
                continue
            if l[5].startswith('/Volumes'):
                continue
            if "osvc_sync_" in l[0]:
                # do not report osvc sync snapshots fs usage
                continue
            r.append({
                      'chk_instance': l[5],
                      'chk_value': l[4],
                      'chk_svcname': self.find_svc(l[5]),
                     })
        return r
 0707010000bb5b000081a400000000000000000000000154fc7bfb0000079c000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/checkFsUsageSunOS.py #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall

class check(checks.check):
    chk_type = "fs_u"

    def find_svc(self, mountpt):
        for svc in self.svcs:
            for rs in svc.get_res_sets('fs'):
                for r in rs.resources:
                    if r.mountPoint == mountpt:
                        return svc.svcname
        return ''

    def do_check(self):
        r = []
        for t in ['ufs', 'vxfs']:
            r += self._do_check(t)
        return r

    def _do_check(self, t):
        cmd = ['df', '-F', t, '-k']
        (out,err,ret) = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 2:
            return self.undef
        r = []
        for line in lines[1:]:
            l = line.split()
            if len(l) == 5:
                l = [''] + l
            elif len(l) != 6:
                continue
            r.append({
                      'chk_instance': l[5],
                      'chk_value': l[4],
                      'chk_svcname': self.find_svc(l[5]),
                     })
        return r
0707010000bb5c000081a400000000000000000000000154fc7bfb0000070e000000660000000300000000000000000000002c00000000root/opt/opensvc/lib/checkFsUsageWindows.py   #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilitiesWindows import get_drives

class check(checks.check):
    chk_type = "fs_u"

    def find_svc(self, mountpt):
        for svc in self.svcs:
            for rs in svc.get_res_sets('fs'):
                for r in rs.resources:
                    if r.mountPoint == mountpt:
                        return svc.svcname
        return ''

    def do_check(self):
        import win32api
        cmd = ['df', '-lP']
        r = []
        for drive in get_drives():
            try:
                n_free_user, n_total, n_free = win32api.GetDiskFreeSpaceEx(drive+':\\')
            except:
                continue
            pct = 100 * (n_total - n_free) // n_total
            r.append({
                      'chk_instance': drive,
                      'chk_value': str(pct),
                      'chk_svcname': self.find_svc(drive),
                     })
        return r

if __name__ == "__main__":
    o = check()
    print(o.do_check())
  0707010000bb5d000081a400000000000000000000000154fc7bfb00000a62000000660000000300000000000000000000002600000000root/opt/opensvc/lib/checkLagHP-UX.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
import os
from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv
import glob
import rcEthtool

class check(checks.check):
    chk_type = "lag"

    def do_check(self):
        if not which("lanscan"):
            return []
        cmd = ["lanscan", "-q"]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        r = []
        self.lag = {}
        for line in out.split("\n"):
            if len(line) == 0:
                continue
            l = line.split()
            n = len(l)
            if n < 2:
                # not apa
                continue
            if self.has_inet(l[0]):
                self.lag[l[0]] = l[1:]

        cmd = ["lanscan", "-v"]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef

        self.intf_status = {}
        for line in out.split("\n"):
            if 'ETHER' not in line or line.startswith('0x'):
                continue
            l = line.split()
            n = len(l)
            if n < 5 or not l[3].startswith('lan'):
                continue
            intf = l[3].replace('lan', '')
            status = l[2]
            self.intf_status[intf] = status

        for intf, slaves in self.lag.items():
            i = 0
            for slave in slaves:
                if slave in self.intf_status and self.intf_status[slave] == 'UP':
                    i += 1
            inst = "lan" + intf + ".paths"
            val = str(i)
            r.append({
                  'chk_instance': inst,
                  'chk_value': val,
                  'chk_svcname': '',
                 })

        return r

    def has_inet(self, intf):
        cmd = ["ifconfig", "lan"+intf]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return False
        if 'inet' in out:
            return True
        return False
  0707010000bb5e000081a400000000000000000000000154fc7bfb000012d4000000660000000300000000000000000000002600000000root/opt/opensvc/lib/checkLagLinux.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
import os
import json
import glob
import datetime
from rcGlobalEnv import rcEnv
from rcIfconfigLinux import ifconfig

"""
Ethernet Channel Bonding Driver: v3.4.0 (October 7, 2008)

Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: eth0
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:23:7d:a0:20:fa

Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:23:7d:a0:20:f6

"""

class check(checks.check):
    chk_type = "lag"
    chk_name = "Linux network link aggregate"
    bonding_p = '/proc/net/bonding'

    def do_check(self):
        l = glob.glob(self.bonding_p+'/*')
        if len(l) == 0:
            return self.undef
        ifg = ifconfig()
        r = []
        for bond in l:
            ifname = os.path.basename(bond)
            intf = ifg.interface(ifname)
            if intf is None:
                continue
            if len(intf.ipaddr) + len(intf.ip6addr) == 0:
                continue
            r += self.do_check_bond(bond)
        return r

    def get_cache(self, bond, slave, uptime):
        cache_p = self.cache_path(bond, slave)
        try:
            with open(cache_p, 'r') as f:
                 buff = f.read()
            data = json.loads(buff)
            prev_uptime, prev_val = data
        except:
            prev_uptime, prev_val = 0, 0

        if prev_uptime >= uptime:
            # reboot
            prev_uptime, prev_val = 0, 0

        return prev_uptime, prev_val

    def uptime(self):
        with open('/proc/uptime', 'r') as f:
            uptime_seconds = float(f.readline().split()[0])
        return uptime_seconds

    def cache_path(self, bond, slave):
        cache_p = os.path.join(rcEnv.pathtmp, "checkLagLinux.cache."+os.path.basename(bond)+"."+slave)
        return cache_p

    def write_cache(self, bond, slave, val, uptime):
        cache_p = self.cache_path(bond, slave)
        with open(cache_p, 'w') as f: f.write(json.dumps([uptime, val]))
        try:
            with open(cache_p, 'w') as f:
                f.write(json.dumps([uptime, val]))
        except:
            pass

    def do_check_bond(self, bond):
        r = []
        try:
            f = open(bond, 'r')
            buff = f.read()
            f.close()
        except:
            return r
        n_slave = 0
        lag = os.path.basename(bond)
        inst = lag
        for line in buff.split('\n'):
            if line.startswith('Slave Interface:'):
                n_slave += 1
                slave = line.split()[-1]
                inst = '.'.join((lag, slave))
            elif line.startswith('MII Status:'):
                val = line.split()[-1]
                if val == "up":
                    val = "0"
                else:
                    val = "1"
                r.append({
                          'chk_instance': inst+'.mii_status',
                          'chk_value': val,
                          'chk_svcname': '',
                         })
            elif line.startswith('Link Failure Count:'):
                val = int(line.split()[-1])
                uptime = self.uptime()
                prev_uptime, prev_val = self.get_cache(bond, slave, uptime)
                if uptime - prev_uptime > 3600:
                    # don't mask alerts by refreshing the cache too soon
                    self.write_cache(bond, slave, val, uptime)
                # Link Failure Count per hour
                val = 3600. * (val - prev_val) / (uptime - prev_uptime)
                r.append({
                          'chk_instance': inst+'.link_failure_per_hour',
                          'chk_value': "%.2f"%val,
                          'chk_svcname': '',
                         })
        r.append({
                  'chk_instance': lag+'.paths',
                  'chk_value': str(n_slave),
                  'chk_svcname': '',
                 })
        return r
0707010000bb5f000081a400000000000000000000000154fc7bfb00001db3000000660000000300000000000000000000002600000000root/opt/opensvc/lib/checkLagSunOS.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import checks
import os
from rcUtilities import justcall, which
from rcUtilitiesSunOS import get_solaris_version
from rcGlobalEnv import rcEnv

"""
Solaris 10
key: 1 (0x0001) policy: L4      address: 0:15:17:bb:82:d2 (auto)
           device       address                 speed           duplex  link    state
           e1000g0      0:15:17:bb:82:d2          1000  Mbps    full    up      attached
           bnx0         0:24:e8:35:61:3b          1000  Mbps    full    up      attached

Solaris 11
# dladm show-aggr
LINK              POLICY   ADDRPOLICY           LACPACTIVITY  LACPTIMER   FLAGS
aggr0             L4       auto                 off           short       -----
aggrbck0          L4       auto                 off           short       -----
aggrpriv0         L4       auto                 off           short       -----

# dladm show-phys
LINK              MEDIA                STATE      SPEED  DUPLEX    DEVICE
net4              Ethernet             up         10     full      usbecm0
net1              Ethernet             up         1000   full      ixgbe1
net0              Ethernet             up         1000   full      ixgbe0
net2              Ethernet             up         1000   full      ixgbe2
net3              Ethernet             up         1000   full      ixgbe3

# dladm show-link
LINK                CLASS     MTU    STATE    OVER
net4                phys      1500   up       --
net1                phys      1500   up       --
net2                phys      1500   up       --
net0                phys      1500   up       --
aggrbck0            aggr      1500   up       net1
aggrpriv0           aggr      1500   up       net3
bckg0               vnic      1500   up       aggrbck0
zrac1_a0            vnic      1500   up       aggr0
zrac3_p_a0          vnic      1500   up       aggrpriv0
"""

class check(checks.check):
    chk_type = "lag"
    chk_name = "Solaris network interface lag"

    def do_check(self):
        if not which("dladm"):
            return self.undef
        self.osver = get_solaris_version()
        if self.osver >= 11:
            cmd = ['dladm', 'show-phys', '-p', '-o', 'link,state,speed,duplex']
            out, err, ret = justcall(cmd)
            if ret != 0:
                return self.undef
            self.phys = out.split('\n')
            cmd = ['dladm', 'show-aggr', '-p', '-o', 'link']
        else:
            cmd = ['dladm', 'show-aggr']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        if self.osver >= 11:
            self.aggs = out.split('\n')
            if len(self.aggs) == 0:
                return self.undef
            self.listaggs = {}
            cmd = ['dladm', 'show-link', '-p', '-o', 'link,over']
            out, err, ret = justcall(cmd)
            if ret != 0:
                return self.undef
            lines = out.split('\n')
            for line in lines:
                if len(line) == 0:
                    break
                l = line.split(':')
                if l[0] in self.aggs:
                    self.listaggs[l[0]] = l[1]
        else:
            self.lines = out.split('\n')
            if len(self.lines) == 0:
                return self.undef
        r = []
        r += self.do_check_speed()
        r += self.do_check_duplex()
        r += self.do_check_link()
        r += self.do_check_attach()
        return r

    def do_check_speed(self):
        r = []
        lag = ""
        i = 0
        if self.osver >= 11:
            for lag in self.aggs:
                if len(lag) == 0:
                    break
                nets = self.listaggs[lag].split(' ')
                for net in nets:
                    if len(net) == 0:
                        break
                    for phy in self.phys:
                        if phy.startswith(net+':'):
                            l = phy.split(':')
                            val = l[2]
                            r.append({
                                      'chk_instance': '%s.%s.speed'%(lag, net),
                                      'chk_value': str(val),
                                      'chk_svcname': '',
                                     })
            return r
        for line in self.lines:
            l = line.split()
            if len(l) < 4:
                continue
            elif line.startswith('key'):
                lag = l[1]
                i = 0
                continue
            elif l[0] == 'device':
                continue
            val = l[2]
            r.append({
                      'chk_instance': '%s.%d.speed'%(lag, i),
                      'chk_value': str(val),
                      'chk_svcname': '',
                     })
            i += 1
        return r

    def do_check_duplex(self):
        return self._do_check("duplex", "full", 4)

    def do_check_link(self):
        return self._do_check("link", "up", 5)

    def do_check_attach(self):
        return self._do_check("attach", "attached", 6)

    def _do_check(self, key, target, col):
        r = []
        lag = ""
        i = 0
        if self.osver >= 11:
            if key == "duplex":
                col = 3
            if key == "attach":
                return r
            if key == "link":
                col = 1
            for lag in self.aggs:
                if len(lag) == 0:
                    break
                nets = self.listaggs[lag].split(' ')
                for net in nets:
                    if len(net) == 0:
                        break
                    for phy in self.phys:
                        if phy.startswith(net+':'):
                            l = phy.split(':')
                            if l[col] != target:
                               val = 1
                            else:
                               val = 0
                            r.append({
                                      'chk_instance': '%s.%s.%s'%(lag, net, key),
                                      'chk_value': str(val),
                                      'chk_svcname': '',
                                     })
            return r
        for line in self.lines:
            l = line.split()
            if len(l) < col+1:
                continue
            elif line.startswith('key'):
                lag = l[1]
                i = 0
                continue
            elif l[0] == 'device':
                continue
            else:
                if l[col] != target:
                    val = 1
                else:
                    val = 0
                r.append({
                          'chk_instance': '%s.%d.%s'%(lag, i, key),
                          'chk_value': str(val),
                          'chk_svcname': '',
                         })
                i += 1
        return r
 0707010000bb60000081a400000000000000000000000154fc7bfb000005f6000000660000000300000000000000000000002800000000root/opt/opensvc/lib/checkLagWindows.py   #
# Copyright (c) 2013 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
import os
import wmi

class check(checks.check):
    chk_type = "lag"
    chk_name = "Windows network link aggregate"

    def do_check(self):
        try:
            self.w = wmi.WMI(namespace="root\hpq")
        except:
            # no HP lag
            return []
        r = []
        for team in self.w.HP_EthernetTeam():
            r += self.do_check_team(team)
        return r

    def do_check_team(self, team):
        r = []
        inst = team.Description
        val = team.RedundancyStatus
        r.append({
                'chk_instance': inst+'.redundancy',
                'chk_value': str(val),
                'chk_svcname': '',
               })
        return r

if __name__ == "__main__":
    o = check()
    o.do_check()
  0707010000bb61000081a400000000000000000000000154fc7bfb000009ce000000660000000300000000000000000000002600000000root/opt/opensvc/lib/checkMceLinux.py #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall, which
import os
from rcGlobalEnv import rcEnv
import datetime

class check(checks.check):
    chk_type = "mcelog"
    mcelog_p = "/var/log/mcelog"
    marker_p = os.path.join(rcEnv.pathtmp, "checkMceLinunx.marker")

    def get_last_marker(self):
        try:
            f = open(self.marker_p, 'r')
        except:
            return None
        buff = f.read()
        f.close()
        return buff

    def gen_marker(self):
        return "opensvc marker " + str(datetime.datetime.now())+'\n'

    def update_marker(self):
        m = self.gen_marker()
        try:
            f = open(self.mcelog_p, "a")
            f.write(m)
            f.close()
        except:
            return
        try:
            f = open(self.marker_p, "w")
            f.write(m)
            f.close()
        except:
            return

    def do_check(self):
        if not os.path.exists(self.mcelog_p):
            return self.undef

        if not which("mcelog"):
            return self.undef

        try:
            f = open(self.mcelog_p, "r")
        except:
            return self.undef

        marker = self.get_last_marker()
        marker_found = False
        l = 0
        total = 0

        for line in f.readlines():
            total += 1
            if line == marker:
                marker_found = True
                continue
            if not marker_found:
                continue
            l += 1

        if not marker_found:
            l = total

        r = []
        r.append({
                  'chk_instance': "new lines",
                  'chk_value': str(l),
                  'chk_svcname': "",
                 })

        self.update_marker()
        return r
  0707010000bb62000081a400000000000000000000000154fc7bfb00000bdb000000660000000300000000000000000000002600000000root/opt/opensvc/lib/checkMpathAIX.py #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall

class check(checks.check):
    chk_type = "mpath"
    svcdevs = {}

    def find_svc(self, dev):
        for svc in self.svcs:
            if svc not in self.svcdevs:
                try:
                    devs = svc.disklist()
                except Exception as e:
                    devs = []
                self.svcdevs[svc] = devs
            if dev in self.svcdevs[svc]:
                return svc.svcname
        return ''

    def odmget(self, lname, attr):
        cmd = ['odmget', '-q', 'name='+lname+' AND attribute='+attr, 'CuAt']
        out, err, ret = justcall(cmd)
        for f in out.split('\n'):
            if "value" not in f:
                continue
            return f.split(" = ")[-1].strip('"')
        return None

    def disk_wwid(self, dev):
        return self.odmget(dev, 'wwid')

    def disk_id(self, dev, typ):
        if typ.startswith("vscsi"):
            return 'vscsi.'+dev
        else:
            return self.disk_wwid(dev)

    def do_check(self):
        cmd = ['lspath']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 1:
            return self.undef
        r = []
        dev = None
        wwid = None
        for line in lines:
            l = line.split()
            if len(l) != 3:
                continue
            if l[0] != 'Enabled':
                continue
            if dev is None:
                dev = l[1]
                typ = l[2]
                wwid = self.disk_id(dev, typ)
                n = 1
            elif dev is not None and wwid is not None and dev != l[1]:
                r.append({'chk_instance': wwid,
                          'chk_value': str(n),
                          'chk_svcname': self.find_svc(dev),
                         })
                dev = l[1]
                typ = l[2]
                wwid = self.disk_id(dev, typ)
                n = 1
            else:
                n += 1
        if dev is not None and wwid is not None:
            r.append({'chk_instance': wwid,
                      'chk_value': str(n),
                      'chk_svcname': self.find_svc(dev),
                     })
        return r
 0707010000bb63000081a400000000000000000000000154fc7bfb00000c1a000000660000000300000000000000000000002800000000root/opt/opensvc/lib/checkMpathHP-UX.py   #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall

class check(checks.check):
    chk_type = "mpath"
    svcdevs = {}

    def find_svc(self, dev):
        for svc in self.svcs:
            if svc not in self.svcdevs:
                try:
                    devs = svc.disklist()
                except Exception as e:
                    devs = []
                self.svcdevs[svc] = devs
            if dev in self.svcdevs[svc]:
                return svc.svcname
        return ''

    def do_check(self):
        cmd = ['scsimgr', 'lun_map']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 1:
            return self.undef
        r = []
        dev = None
        wwid = None
        for line in lines:
            if "LUN PATH INFORMATION FOR LUN" in line:
                # new mpath
                # - store previous
                # - reset path counter
                if dev is not None and not dev.startswith('/dev/pt/pt') and wwid != '=' and "Virtual" not in proto:
                    r.append({'chk_instance': wwid,
                              'chk_value': str(n),
                              'chk_svcname': self.find_svc(dev),
                             })
                n = 0
                l = line.split()
                if len(l) < 2:
                    continue
                dev = l[-1]
            elif line.startswith("World Wide Identifier"):
                wwid = line.split()[-1].replace("0x","")
            elif line.startswith("SCSI transport protocol"):
                proto = line.split("=")[-1]
            elif line.startswith("State"):
                state = line.split("=")[-1].strip()
            elif line.startswith("Last Open or Close state"):
                last_known_state = line.split("=")[-1].strip()
                if state in ("ACTIVE", "STANDBY"):
                    n += 1
                elif state == "UNOPEN" and last_known_state in ("ACTIVE", "STANDBY"):
                    n += 1
        if dev is not None and not dev.startswith('/dev/pt/pt') and wwid != '=' and "Virtual" not in proto:
            r.append({'chk_instance': wwid,
                      'chk_value': str(n),
                      'chk_svcname': self.find_svc(dev),
            })
        return r
  0707010000bb64000081a400000000000000000000000154fc7bfb000013b1000000660000000300000000000000000000002800000000root/opt/opensvc/lib/checkMpathLinux.py   #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2014 Arnaud veron <arnaud.veron@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall, printplus

class check(checks.check):
    chk_type = "mpath"
    svcdevs = {}

    def find_svc(self, dev):
        for svc in self.svcs:
            if svc not in self.svcdevs:
                try:
                    devs = svc.disklist()
                except Exception as e:
                    devs = []
                self.svcdevs[svc] = devs
            if dev in self.svcdevs[svc]:
                return svc.svcname
        return ''

    def do_check_old(self):
        """
	mpath1 (3600508b4000971ca0000f00010650000)
	[size=404 GB][features="1 queue_if_no_path"][hwhandler="0"]
	\_ round-robin 0 [active]
	 \_ 0:0:0:2 sda 8:0   [active]
	 \_ 1:0:0:2 sde 8:64  [active]
	 \_ 1:0:1:2 sdf 8:80  [active]
	 \_ 0:0:1:2 sdi 8:128 [active]
	\_ round-robin 0 [enabled]
	 \_ 0:0:2:2 sdc 8:32  [active]
	 \_ 0:0:3:2 sdd 8:48  [active]
	 \_ 1:0:3:2 sdh 8:112 [active]
	 \_ 1:0:2:2 sdb 8:16  [active]
        """
        cmd = ['multipath', '-l']
        (out, err, ret) = justcall(cmd)
        lines = out.split('\n')
        if len(lines) < 1:
            return self.undef
        r = []
        wwid = None
        dev = None
        for line in lines:
            if len(line) > 0 and not '\_ ' in line and not line.startswith('['):
                # new mpath
                # - store previous
                # - reset path counter
                if wwid is not None:
                    r.append({'chk_instance': wwid,
                              'chk_value': str(n),
                              'chk_svcname': self.find_svc(dev),
                             })
                n = 0
                l = line.split()
                if len(l) == 2:
                    wwid = l[1][1:-1]
                elif len(l) == 1:
                    wwid = l[0]
                else:
                    wwid = None
                if wwid is not None and wwid.startswith('3'):
                    wwid = wwid[1:]
            if "[active]" in line and line.startswith(' '):
                n += 1
                dev = "/dev/"+line.split()[2]
        if wwid is not None:
            r.append({'chk_instance': wwid,
                      'chk_value': str(n),
                      'chk_svcname': self.find_svc(dev),
                     })
        return r

    def do_check(self):
        cmd = ['multipathd', '-kshow topo']
        (out, err, ret) = justcall(cmd)
        if 'list|show' in out:
            # multipathd does not support 'show topo'
            # try parsing 'multipath -l' output
            return self.do_check_old()
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 1:
            return self.undef
        r = []
        wwid = None
        dev = None
        for line in lines:
            if ' dm-' in line:
                # new mpath
                # - store previous
                # - reset path counter
                if wwid is not None:
                    r.append({'chk_instance': wwid,
                              'chk_value': str(n),
                              'chk_svcname': self.find_svc(dev),
                             })
                n = 0
                if line.startswith(": "):
                    line = line.replace(": ", "")
                l = line.split()
                if l[0].endswith(":"):
                    # skip prefix: create, swithpg, reload, ...
                    l = l[1:]
                if len(l) < 2:
                    continue
                if l[1].startswith('('):
                    wwid = l[1][1:-1]
                else:
                    wwid = l[0]
                if wwid is not None and wwid.startswith('3'):
                    wwid = wwid[1:]
            if "[active][ready]" in line or \
               "active ready" in line:
                n += 1
                dev = "/dev/"+line.split()[2]
        if wwid is not None:
            r.append({'chk_instance': wwid,
                      'chk_value': str(n),
                      'chk_svcname': self.find_svc(dev),
                     })
        return r

if __name__ == "__main__":
    paths = check()
    tab = paths.do_check()
    printplus(tab)
   0707010000bb65000081a400000000000000000000000154fc7bfb0000065e000000660000000300000000000000000000002700000000root/opt/opensvc/lib/checkMpathOSF1.py    #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall
from rcDiskInfoOSF1 import diskInfo

class check(checks.check):
    chk_type = "mpath"
    svcdevs = {}

    def find_svc(self, dev):
        devpath = '/dev/rdisk/'+dev
        for svc in self.svcs:
            if svc not in self.svcdevs:
                try:
                    devs = svc.disklist()
                except Exception as e:
                    devs = []
                self.svcdevs[svc] = devs
            if dev in self.svcdevs[svc]:
                return svc.svcname
        return ''

    def do_check(self):
        di = diskInfo()
        r = []
        for dev, data in di.h.items():
            r.append({'chk_instance': data['wwid'],
                      'chk_value': str(data['path_count']),
                      'chk_svcname': self.find_svc(dev),
                     })
        return r
  0707010000bb66000081a400000000000000000000000154fc7bfb00000e07000000660000000300000000000000000000002c00000000root/opt/opensvc/lib/checkMpathPowerpath.py   #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv
di = __import__("rcDiskInfo"+rcEnv.sysname)
_di = di.diskInfo()

class check(checks.check):
    chk_type = "mpath"
    chk_name = "PowerPath"
    svcdevs = {}

    def find_svc(self, dev):
        for svc in self.svcs:
            if svc not in self.svcdevs:
                try:
                    devs = svc.disklist()
                except Exception as e:
                    devs = []
                self.svcdevs[svc] = devs
            if dev in self.svcdevs[svc]:
                return svc.svcname
        return ''

    def do_check(self):
        """
	Pseudo name=emcpowerh
	Symmetrix ID=000290101523
	Logical device ID=17C6
	state=alive; policy=SymmOpt; priority=0; queued-IOs=0
	==============================================================================
	---------------- Host ---------------   - Stor -   -- I/O Path -  -- Stats ---
	###  HW Path                I/O Paths    Interf.   Mode    State  Q-IOs Errors
	==============================================================================
	   0 qla2xxx                   sdi       FA  9dB   active  alive      0      0
	   1 qla2xxx                   sds       FA  8dB   active  alive      0      0
        """

        if not which('powermt'):
            return self.undef

        cmd = ['powermt', 'display', 'dev=all']
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return self.undef

        lines = out.split('\n')
        if len(lines) < 1:
            return self.undef

        r = []
        dev = None
        name = None
        paths = []
        n = 0
        for line in lines:
            if len(line) == 0:
                # new mpath
                # - store previous
                # - reset path counter
                if dev is not None:
                    if len(paths) > 0:
                        did = _di.disk_id(paths[0])
                        if did is not None:
                            name = did
                    r.append({'chk_instance': name,
                              'chk_value': str(n),
                              'chk_svcname': self.find_svc(dev),
                             })
                    paths = []
                    dev = None
                    n = 0
            if 'Pseudo name' in line:
                l = line.split('=')
                if len(l) != 2:
                    continue
                name = l[1]
                dev = "/dev/"+name
            else:
                l = line.split()
                if len(l) < 3:
                    continue
                if l[2].startswith("sd"):
                    paths.append("/dev/"+l[2])
                if "active" in line and \
                   "alive" in line:
                    n += 1
        return r
 0707010000bb67000081a400000000000000000000000154fc7bfb00000d3a000000660000000300000000000000000000002800000000root/opt/opensvc/lib/checkMpathSunOS.py   #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall

class check(checks.check):
    """
    # mpathadm list LU
        /dev/rdsk/c6t600507680280809AB0000000000000E7d0s2
                Total Path Count: 4
                Operational Path Count: 4
        /scsi_vhci/disk@g60050768018085d7e0000000000004e5
                Total Path Count: 1
                Operational Path Count: 1
        /dev/rdsk/c6t60050768018085D7E0000000000004E4d0s2
                Total Path Count: 1
                Operational Path Count: 1
        /dev/rdsk/c6t60050768018085D7E00000000000056Bd0s2
                Total Path Count: 4
                Operational Path Count: 4
    """
    chk_type = "mpath"
    svcdevs = {}

    def find_svc(self, dev):
        for svc in self.svcs:
            if svc not in self.svcdevs:
                try:
                    devs = svc.disklist()
                except Exception as e:
                    devs = []
                self.svcdevs[svc] = devs
            if dev in self.svcdevs[svc]:
                return svc.svcname
        return ''

    def do_check(self):
        cmd = ['mpathadm', 'list', 'LU']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 4:
            return self.undef
        r = []
        dev = None
        wwid = ""
        for line in lines:
            if "/dev/" in line:
                # new mpath
                # - remember current dev
                # - remember current wwid
                # - reset path counter
                dev = line.strip()
                wwid = line[line.index('t')+1:line.rindex('d')]
                n = 0
            elif '/disk@g' in line:
                # unmapped dev
                # - remember current dev
                # - remember current wwid
                # - reset path counter
                dev = line.strip()
                wwid = '_'+line[line.index('@g')+2:]
                n = 0
            if "Total Path Count:" in line:
                continue
            if "Operational Path Count:" in line:
                # - store current dev if valid
                # - then:
                    # - reset path counter
                    # - reset dev
                n = int(line.split(':')[-1].strip())
                if dev is not None:
                    r.append({'chk_instance': wwid,
                              'chk_value': str(n),
                              'chk_svcname': self.find_svc(dev),
                             })
                    dev = None
                    n = 0
        return r

  0707010000bb68000081a400000000000000000000000154fc7bfb0000084f000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/checkMpathWindows.py #
# Copyright (c) 2013 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
import wmi
import tempfile
from subprocess import *
import os

class check(checks.check):
    chk_type = "mpath"
    svcdevs = {}

    def find_svc(self, dev):
        for svc in self.svcs:
            if svc not in self.svcdevs:
                try:
                    devs = svc.disklist()
                except Exception as e:
                    devs = []
                self.svcdevs[svc] = devs
            if dev in self.svcdevs[svc]:
                return svc.svcname
        return ''

    def diskpart_rescan(self):
        f = tempfile.NamedTemporaryFile()
        tmpf = f.name
        f.close()
        with open(tmpf, 'w') as f:
            f.write("rescan\n")
        p = Popen(["diskpart", "/s", tmpf], stdout=PIPE, stderr=PIPE, stdin=None)
        out, err = p.communicate()
        os.unlink(tmpf)

    def do_check(self):
        self.wmi = wmi.WMI(namespace="root/wmi")
        self.diskpart_rescan()
        r = []
        try:
            l = self.wmi.MPIO_DISK_INFO()
        except:
            l = []
        for disk in l:
            for drive in disk.driveinfo:
                name = drive.name
                n = drive.numberpaths
                r.append({'chk_instance': name,
                          'chk_value': str(n),
                          'chk_svcname': "",
                         })
        return r
 0707010000bb69000081a400000000000000000000000154fc7bfb00000886000000660000000300000000000000000000002700000000root/opt/opensvc/lib/checkNumaLinux.py    #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
import os
import glob
import math

class check(checks.check):
    chk_type = "numa"

    def do_check(self):
        nodeinfo = {}
        memtotal = 0
        n_nodes = 0
        n_cpu = 0
        for npath in glob.glob("/sys/devices/system/node/node*"):
            node_n_cpu = len(glob.glob(npath+"/cpu*"))
            node = os.path.basename(npath)
            with open(npath+"/meminfo", 'r') as f:
                lines = f.read().strip('\n').split('\n')
                for line in lines:
                    if 'MemTotal' in line:
                        try:
                            node_mem = int(line.split()[-2])
                        except:
                            continue
                        memtotal += node_mem
                        n_nodes += 1
                        n_cpu += node_n_cpu
                        nodeinfo[node] = {"mem": node_mem, "cpu": node_n_cpu}
                        break
        r = []
        if n_nodes < 2:
            return r
        target_per_cpu = memtotal / n_cpu
        for node, info in nodeinfo.items():
            target = target_per_cpu * info['cpu']
            deviation = math.fabs(100. * (info['mem'] - target) // target)
            r.append({
                  'chk_instance': node+'.mem.leveling',
                  'chk_value': str(deviation),
                  'chk_svcname': '',
                 })
        return r

  0707010000bb6a000081a400000000000000000000000154fc7bfb0000157b000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/checkRaidMegaRaid.py #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
import os
from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv

class check(checks.check):
    prefixes = [os.path.join(os.sep, "usr", "local", "admin"),
                os.path.join(os.sep, "opt", "MegaRAID", "MegaCli")]
    megacli = ["MegaCli64", "MegaCli", "megacli"]
    chk_type = "raid"
    chk_name = "MegaCli"

    def find_megacli(self):
        for prog in self.megacli:
            if which(prog):
                return prog
            for prefix in self.prefixes:
                megacli = os.path.join(prefix, prog)
                if os.path.exists(megacli):
                    return megacli
        return

    def do_check(self):
        r = self.do_check_ldpdinfo()
        r += self.do_check_bbustatus()
        return r

    def do_check_ldpdinfo(self):
        megacli = self.find_megacli()
        if megacli is None:
            return self.undef
        os.chdir(rcEnv.pathtmp)
        logs = [os.path.join(rcEnv.pathtmp, 'MegaSAS.log'),
                os.path.join(rcEnv.pathtmp, 'MegaCli.log'),
                os.path.join(rcEnv.pathtmp, 'MegaRaid.log')]
        for log in logs:
            try:
                os.unlink(log)
            except OSError as e:
                if e.errno == 2:
                    pass
                else:
                    raise
        cmd = [megacli, '-LdPdInfo', '-aALL']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) == 0:
            return self.undef
        r = []
        slot = ""
        errs = 0
        for line in lines:
            if line.startswith('Adapter'):
                l = line.split('#')
                if slot != "":
                    r.append({
                             'chk_instance': slot,
                              'chk_value': str(errs),
                              'chk_svcname': '',
                             })
                slot = 'slot'+l[-1]
                errs = 0
            if (line.startswith('State:') and 'Optimal' not in line) or \
               (line.startswith('Firmware state:') and 'Online' not in line):
                errs += 1
        if slot != "":
            r.append({
                 'chk_instance': slot,
                  'chk_value': str(errs),
                  'chk_svcname': '',
                 })
        return r

    def do_check_bbustatus(self):
        megacli = self.find_megacli()
        if megacli is None:
            return self.undef
        os.chdir(rcEnv.pathtmp)
        logs = [os.path.join(rcEnv.pathtmp, 'MegaSAS.log'),
                os.path.join(rcEnv.pathtmp, 'MegaCli.log'),
                os.path.join(rcEnv.pathtmp, 'MegaRaid.log')]
        for log in logs:
            try:
                os.unlink(log)
            except OSError as e:
                if e.errno == 2:
                    pass
                else:
                    raise
        cmd = [megacli, '-AdpBbuCmd', '-GetBbuStatus', '-aALL']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) == 0:
            return self.undef
        r = []
        slot = ""
        for line in lines:
            line = line.strip()
            if 'Adapter:' in line:
                l = line.split()
                slot = 'slot'+l[-1]
            if line.startswith('BatteryType:') and 'No Battery' in line:
                val = 1
                r.append({
                          'chk_instance': '%s battery NoBattery'%slot,
                          'chk_value': str(val),
                          'chk_svcname': '',
                         })
            if line.startswith('Relative State of Charge:'):
                val = line.strip('%').split()[-1]
                r.append({
                          'chk_instance': '%s battery charge'%slot,
                          'chk_value': str(val),
                          'chk_svcname': '',
                         })
            if line.startswith('Temperature:'):
                val = line.split()[-2]
                r.append({
                          'chk_instance': '%s battery temp'%slot,
                          'chk_value': str(val),
                          'chk_svcname': '',
                         })
            if line.startswith('isSOHGood:'):
                if 'Yes' in line:
                    val = 0
                else:
                    val = 1
                r.append({
                          'chk_instance': '%s battery isSOHGood'%slot,
                          'chk_value': str(val),
                          'chk_svcname': '',
                         })
        return r
 0707010000bb6b000081a400000000000000000000000154fc7bfb00001200000000660000000300000000000000000000002600000000root/opt/opensvc/lib/checkRaidSas2.py #
# Copyright (c) 2012 Lucien Hercaud <hercaud@hercaud.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
import os
from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv

class check(checks.check):
    prefixes = [os.path.join(os.sep, "usr", "local", "admin")]
    sas2ircu = "sas2ircu"
    chk_type = "raid"
    chk_name = "LSI SAS200"

    def find_sas2ircu(self):
        if which(self.sas2ircu):
            return self.sas2ircu
        for prefix in self.prefixes:
            sas2ircu = os.path.join(prefix, self.sas2ircu)
            if os.path.exists(sas2ircu):
                return sas2ircu
        return

    def do_check(self):
        r = self.do_check_ldpdinfo()
        return r

    def do_check_ldpdinfo(self):
        sas2ircu = self.find_sas2ircu()
        if sas2ircu is None:
            return self.undef
        os.chdir(rcEnv.pathtmp)
        logs = [os.path.join(rcEnv.pathtmp, 'sas2ircu.log')]
        for log in logs:
            try:
                os.unlink(log)
            except OSError as e:
                if e.errno == 2:
                    pass
                else:
                    raise
        cmd = [sas2ircu, 'LIST']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        idx = []
        lines = out.split('\n')
        for line in lines:
            if 'SAS20' in line:
                l = line.split()
                idx.append(l[0])

        r = []
        errs = 0
        for ix in idx:
            cmd = [sas2ircu, str(ix), 'DISPLAY']
            out, err, ret = justcall(cmd)
            lines = out.split('\n')
            ctrl = "ctrl:"+str(ix)
            slot=""
            chk_dsk = 0
            for line in lines:
                if line.startswith('IR volume'):
                    chk_dsk = 2
                if line.startswith('  Volume Name') and 'Virtual Disk' in line and (chk_dsk == 2):
                    l = line.split()
                    slot = 'LD'+str(l[-1])
                if line.startswith('  Status of volume') and (chk_dsk == 2):
                    if 'Okay (OKY)' not in line:
                        r.append({ 'chk_instance': ctrl+','+slot, 'chk_value': '1', 'chk_svcname': '', })
                        errs += 1
                    else :
                        r.append({ 'chk_instance': ctrl+','+slot, 'chk_value': '0', 'chk_svcname': '', })
                if line.startswith('Device is a Hard disk'):
                    chk_dsk = 1
                if line.startswith('  Enclosure #') and (chk_dsk == 1):
                    l = line.split()
                    enc = l[-1]
                if line.startswith('  Slot #') and (chk_dsk == 1):
                    l = line.split()
                    slot = 'PD'+str(enc)+':'+str(l[-1])
                if line.startswith('  State') and (chk_dsk == 1):
                    if 'Optimal (OPT)' not in line:
                        r.append({ 'chk_instance': ctrl+','+slot, 'chk_value': '1', 'chk_svcname': '', })
                        errs += 1
                    else :
                        r.append({ 'chk_instance': ctrl+','+slot, 'chk_value': '0', 'chk_svcname': '', })
                if line.startswith('Device is a Enclosure services device'):
                    chk_dsk = 3
                if line.startswith('  Enclosure #') and (chk_dsk == 3):
                    l = line.split()
                    slot = 'Enc'+str(l[-1])
                if line.startswith('  State') and (chk_dsk == 3):
                    if 'Standby (SBY)' not in line:
                        r.append({ 'chk_instance': ctrl+','+slot, 'chk_value': '1', 'chk_svcname': '', })
                        errs += 1
                    else :
                        r.append({ 'chk_instance': ctrl+','+slot, 'chk_value': '0', 'chk_svcname': '', })
            r.append({ 'chk_instance': 'all SAS20*', 'chk_value': str(errs), 'chk_svcname': '', })
        return r
0707010000bb6c000081a400000000000000000000000154fc7bfb00000f4b000000660000000300000000000000000000002c00000000root/opt/opensvc/lib/checkRaidSmartArray.py   #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2012 Lucien Hercaud <lucien@hercaud.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall, which
import os

sep = ':'
path_list = os.environ['PATH'].split(sep) + ['/opt/HPQacucli/sbin']

os.environ['PATH'] = sep.join(path_list)
os.environ['INFOMGR_BYPASS_NONSA'] = '1'

class check(checks.check):
    chk_type = "raid"
    chk_name = "HP SmartArray"

    def parse_errors(self, out):
        r = []
        lines = out.split('\n')
        if len(lines) == 0:
            return r
        for line in lines:
            l = line.split(': ')
            if len(l) < 2 or not line.startswith('  '):
                continue
            if l[-1].strip() != "OK":
                inst = line.strip().lower()
                status = 1
            else:
                inst = l[0].strip().lower()
                status = 0
            r += [(inst, status)]
        return r

    def check_logicaldrive(self, slot):
        cmd = ['controller', 'slot='+slot, 'logicaldrive', 'all', 'show', 'status']
        out, err, ret = self.hpacucli(cmd)
        if ret != 0:
            return []
        return self.parse_errors(out)

    def check_physicaldrive(self, slot):
        cmd = ['controller', 'slot='+slot, 'physicaldrive', 'all', 'show', 'status']
        out, err, ret = self.hpacucli(cmd)
        if ret != 0:
            return []
        return self.parse_errors(out)

    def check_array(self, slot):
        cmd = ['controller', 'slot='+slot, 'array', 'all', 'show', 'status']
        out, err, ret = self.hpacucli(cmd)
        if ret != 0:
            return []
        return self.parse_errors(out)

    def check_controller(self, slot):
        cmd = ['controller', 'slot='+slot, 'show', 'status']
        out, err, ret = self.hpacucli(cmd)
        if ret != 0:
            return []
        return self.parse_errors(out)

    def hpacucli(self, cmd):
        cmd = ['hpacucli'] + cmd
        try:
            out, err, ret = justcall(cmd)
        except OSError:
            cmd = [os.environ['SHELL']] + cmd
            out, err, ret = justcall(cmd)
        return out, err, ret

    def do_check(self):
        if not which('hpacucli'):
            return self.undef
        cmd = ['controller', 'all', 'show', 'status']
        out, err, ret = self.hpacucli(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) == 0:
            return self.undef
        r = []
        for line in lines:
            if ' Slot ' in line:
                l = line.split()
                idx = l.index('Slot')
                uslot = l[idx+1]
                slot = 'slot ' + uslot
                _r = []
                _r += self.check_controller(uslot)
                _r += self.check_array(uslot)
                _r += self.check_logicaldrive(uslot)
                _r += self.check_physicaldrive(uslot)
                for inst, value in _r:
                    r.append({
                          'chk_instance': ".".join((slot, inst)),
                          'chk_value': str(value),
                          'chk_svcname': '',
                         })
        return r
 0707010000bb7e000081a400000000000000000000000154fc7bfb00000ca8000000660000000300000000000000000000001d00000000root/opt/opensvc/lib/lock.py  import os
import time
import rcExceptions as ex
from rcGlobalEnv import rcEnv

class lockTimeout(Exception):
    """ acquire lock timed out
    """

class lockNoLockFile(Exception):
    """ no lockfile specified
    """

class lockCreateError(Exception):
    """ could not create lockfile
    """

class lockAcquire(Exception):
    """ could not acquire lock on lockfile
    """
    def __init__(self, pid):
        self.pid = pid

def monlock(timeout=30, delay=5, fname='svcmon.lock'):
    lockfile = os.path.join(rcEnv.pathlock, fname)
    try:
        lockfd = lock(timeout=timeout, delay=delay, lockfile=lockfile)
    except lockTimeout:
        print("timed out waiting for lock (%s)"%lockfile)
        raise ex.excError
    except lockNoLockFile:
        print("lock_nowait: set the 'lockfile' param")
        raise ex.excError
    except lockCreateError:
        print("can not create lock file (%s)"%lockfile)
        raise ex.excError
    except lockAcquire as e:
        print("another svcmon is currently running (pid=%s)"%e.pid)
        raise ex.excError
    except:
        print("unexpected locking error (%s)"%lockfile)
        raise ex.excError
    return lockfd

def monunlock(lockfd):
    unlock(lockfd)

def lock(timeout=30, delay=5, lockfile=None):
    if timeout == 0 or delay == 0:
        l = [1]
    else:
        l = range(timeout//delay)
    for i in l:
        if i > 0:
            time.sleep(delay)
        try:
            return lock_nowait(lockfile)
        except lockAcquire:
            pass
    raise lockTimeout

def lock_nowait(lockfile=None):
    if lockfile is None:
        raise lockNoLockFile

    pid = 0
    dir = os.path.dirname(lockfile)

    if not os.path.exists(dir):
        os.makedirs(dir)

    try:
        with open(lockfile, 'r') as fd:
            pid = int(fd.read())
            fd.close()
    except:
        pass

    try:
	flags = os.O_RDWR|os.O_CREAT|os.O_TRUNC
	if rcEnv.sysname != 'Windows':
	    flags |= os.O_SYNC
        lockfd = os.open(lockfile, flags, 0o644)
    except Exception as e:
        raise lockCreateError()

    try:
        """ test if we already own the lock
        """
        if pid == os.getpid():
            os.close(lockfd)
            return

        """ FD_CLOEXEC makes sure the lock is the held by processes
            we fork from this process
        """
        if os.name == 'posix':
            import fcntl
            fcntl.flock(lockfd, fcntl.LOCK_EX|fcntl.LOCK_NB)
            flags = fcntl.fcntl(lockfd, fcntl.F_GETFD)
            flags |= fcntl.FD_CLOEXEC

            """ acquire lock
            """
            fcntl.fcntl(lockfd, fcntl.F_SETFD, flags)
        elif os.name == 'nt':
            import msvcrt
            size = os.path.getsize(lockfile)
            msvcrt.locking(lockfd, msvcrt.LK_RLCK, size)

        """ drop our pid in the lockfile
        """
        os.write(lockfd, str(os.getpid()).encode('utf-8'))
        os.fsync(lockfd)
        return lockfd
    except IOError:
        raise lockAcquire(pid)
    except:
        raise

def unlock(lockfd):
    if lockfd is None:
        return
    try:
        os.close(lockfd)
    except:
        """ already released by a parent process ?
        """
        pass

0707010000bb6d000081a400000000000000000000000154fc7bfb0000053c000000660000000300000000000000000000003300000000root/opt/opensvc/lib/checkRaidSmartArrayWindows.py    #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2012 Lucien Hercaud <lucien@hercaud.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checkRaidSmartArray
import os

sep = ';'
path_list = os.environ['PATH'].split(sep)

if 'PROGRAMFILES' in os.environ:
    path_list.append(os.path.join(os.environ.get('PROGRAMFILES'),
                                  'compaq', 'hpacucli', 'bin'))
if 'PROGRAMFILES(X86)' in os.environ:
    path_list.append(os.path.join(os.environ.get('PROGRAMFILES(X86)'),
                                  'compaq', 'hpacucli', 'bin'))

os.environ['PATH'] = sep.join(path_list)

class check(checkRaidSmartArray.check):
    pass

0707010000bb6e000081a400000000000000000000000154fc7bfb00000846000000660000000300000000000000000000002800000000root/opt/opensvc/lib/checkVgUsageAIX.py   #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall

class check(checks.check):
    chk_type = "vg_u"

    def find_svc(self, vgname):
        for svc in self.svcs:
            for rs in svc.get_res_sets('disk.vg'):
                for r in rs.resources:
                    if r.name == vgname:
                        return svc.svcname
        return ''

    def do_check(self):
        r = []
        cmd = ['lsvg']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        vgs = out.split('\n')
        for vg in vgs:
            r += self._do_check(vg)
        return r

    def _do_check(self, vg):
        cmd = ['lsvg', '-p', vg]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 3:
            return self.undef
        r = []
        for line in lines[2:]:
            l = line.split()
            if len(l) != 5:
                continue
            size = int(l[2])
            free = int(l[3])
            val = int(100*(size-free)/size)
            r.append({'chk_instance': vg,
                      'chk_value': str(val),
                      'chk_svcname': self.find_svc(l[0]),
                     }
                    )
        return r
  0707010000bb6f000081a400000000000000000000000154fc7bfb00000a4b000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/checkVgUsageHP-UX.py #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import call

class check(checks.check):
    chk_type = "vg_u"

    def find_svc(self, vgname):
        for svc in self.svcs:
            for rs in svc.get_res_sets('disk.vg'):
                for r in rs.resources:
                    if r.name == vgname:
                        return svc.svcname
        return ''

    def do_check(self):
        """
        # vgdisplay -F
vg_name=/dev/vg00:vg_write_access=read,write:vg_status=available:max_lv=255:cur_lv=9:open_lv=9:max_pv=16:cur_pv=1:act_pv=1:max_pe_per_pv=4384:vgda=2:pe_size=32:total_pe=4347:alloc_pe=2712:free_pe=1635:total_pvg=0:total_spare_pvs=0:total_spare_pvs_in_use=0:vg_version=1.0:vg_max_size=2192g:vg_max_extents=70144
        """
        cmd = ['vgdisplay', '-F']
        (ret, out, err) = call(cmd, errlog=False)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 1:
            return self.undef
        r = []
        for line in lines:
            l = line.split(':')
            if len(l) < 10:
                continue
            instance = None
            free = None
            size = None
            for w in l:
                if 'vg_name' in w:
                    instance = w.split('=')[1].replace('/dev/','')
                elif 'total_pe' in w:
                    size = int(w.split('=')[1])
                elif 'free_pe' in w:
                    free = int(w.split('=')[1])
            if instance is None or free is None or size is None:
                continue
            val = int(100*(size-free)/size)
            r.append({'chk_instance': instance,
                      'chk_value': str(val),
                      'chk_svcname': self.find_svc(instance),
                     }
                    )
        return r
 0707010000bb70000081a400000000000000000000000154fc7bfb000007a2000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/checkVgUsageLinux.py #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall

class check(checks.check):
    chk_type = "vg_u"

    def find_svc(self, vgname):
        for svc in self.svcs:
            for rs in svc.get_res_sets('disk.vg'):
                for r in rs.resources:
                    if r.name == vgname:
                        return svc.svcname
        return ''

    def do_check(self):
        cmd = ['vgs', '--units', 'b', '--noheadings',
               '-o', 'vg_name,vg_size,vg_free']
        (out,err,ret) = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 1:
            return self.undef
        r = []
        for line in lines:
            l = line.split()
            if len(l) != 3:
                continue
            size = int(l[1].replace('B',''))
            free = int(l[2].replace('B',''))
            val = int(100*(size-free)/size)
            r.append({'chk_instance': l[0],
                      'chk_value': str(val),
                      'chk_svcname': self.find_svc(l[0]),
                     }
                    )
        return r
  0707010000bb71000081a400000000000000000000000154fc7bfb00000d2b000000660000000300000000000000000000002b00000000root/opt/opensvc/lib/checkZfsUsageSunOS.py    #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall

class check(checks.check):
    def __init__(self, svcs=[]):
        checks.check.__init__(self, svcs)
        self.zpcache = {}

    chk_type = "fs_u"

    def convert(self, s):
        s = s.replace(',', '.').upper()
        if s == "0":
            return 0
        if len(s) < 2:
            raise
        if s.endswith('T'):
            s = float(s[:-1])*1024*1024*1024
        elif s.endswith('G'):
            s = float(s[:-1])*1024*1024
        elif s.endswith('M'):
            s = float(s[:-1])*1024
        elif s.endswith('K'):
            s = float(s[:-1])
        else:
            raise
        return s

    def get_zonepath(self, name):
        if name in self.zpcache:
            return self.zpcache[name]
        cmd = ['zonecfg', '-z', name, 'info', 'zonepath']
        (out,err,ret) = justcall(cmd)
        if ret != 0:
            return None
        self.zpcache[name] = out.split()[-1].strip()
        return self.zpcache[name]

    def find_svc(self, name, mnt):
        for svc in self.svcs:
            for rs in svc.get_res_sets('container'):
                for r in rs.resources:
                    if  r.type == "container.zone": 
                        zp = self.get_zonepath(r.name)
                        if zp is not None and zp == mnt:
                            return svc.svcname
            for rs in svc.get_res_sets('fs'):
                for r in rs.resources:
                    if r.device == name:
                        return svc.svcname
        return ''

    def do_check(self):
        cmd = ['zfs', 'list', '-o', 'name,used,avail,mountpoint', '-H']
        (out,err,ret) = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) == 0:
            return self.undef
        r = []
        for line in lines:
            l = line.split()
            if len(l) != 4:
                continue
            if "@" in l[0]:
                # do not report clone usage
                continue
            if "osvc_sync_" in l[0]:
                # do not report osvc sync snapshots fs usage
                continue
            used = self.convert(l[1])
            avail = self.convert(l[2])
            total = used + avail
            pct = used / total * 100
            r.append({
                      'chk_instance': l[0],
                      'chk_value': str(pct),
                      'chk_svcname': self.find_svc(l[0], l[3]),
                     })
        return r
 0707010000bb72000081a400000000000000000000000154fc7bfb000010fa000000660000000300000000000000000000002800000000root/opt/opensvc/lib/checkZpoolSunOS.py   # Copyright (c) 2012 Lucien Hercaud <hercaud@hercaud.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import checks
from rcUtilities import justcall
import os
import re
from rcGlobalEnv import rcEnv
 
class check(checks.check):
    """
    # zpool status
      pool: rpool
     state: DEGRADED
    status: One or more devices has been taken offline by the administrator.
            Sufficient replicas exist for the pool to continue functioning in a
            degraded state.
    action: Online the device using 'zpool online' or replace the device with
            'zpool replace'.
     scrub: scrub completed after 0h40m with 0 errors on Sun Jun 24 05:41:27 2012
    config:
 
            NAME          STATE     READ WRITE CKSUM
            rpool         DEGRADED     0     0     0
              mirror      DEGRADED     0     0     0
                c0t0d0s0  ONLINE       0     0     0
                c0t1d0s0  OFFLINE      0     0     0
    """
    chk_type = "zpool"
 
    def find_svc(self, pool):
        devs = []
        cmd = ['zpool', 'status', pool]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return ''
        lines = out.split('\n')
        getdevs = 0
        for line in lines:
            if line.startswith('errors:'):
                getdevs = 0
                continue
            if len(line) < 2:
                continue
            if 'NAME' in line and 'STATE' in line and 'CKSUM' in line:
                getdevs = 1
                continue
            if pool in line and getdevs == 1:
                getdevs = 2
                continue
            if getdevs == 2 and ( 'mirror' in line or 'raidz' in line) :
                continue
            if getdevs == 2:
                l = line.split()
                x = l[0]
                if 'd0' in x and x.startswith(rcEnv.pathvar):
                    x = l[0].split('/')[-1]
                if 'd0' in x and x.startswith('c'):
                    d = x
                    if re.match("^.*[sp][0-9]*$", d) is not None:
                        # partition, substitute s2 to given part
                        regex = re.compile("[sp][0-9]*$", re.UNICODE)
                        d = regex.sub("s2", d)
                    else:
                        # base device, append s2
                        d += 's2'
                    if os.path.exists('/dev/rdsk/'+d):
                        devs.append(d)
        for d in devs:
            for svc in self.svcs:
                if '/dev/rdsk/'+d in svc.disklist():
                    return svc.svcname
        return ''
 
    def do_check(self):
        cmd = ['zpool', 'list', '-H', '-o', 'name,health']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.undef
        lines = out.split('\n')
        if len(lines) < 2:
            return self.undef
        r = []
        for line in lines:
            if len(line) < 2:
                continue
            l = line.split()
            pool = l[0]
            stat = l[1]
            if stat == 'ONLINE':
                stat_val = 0
            elif stat == 'DEGRADED':
                stat_val = 1
            elif stat == 'FAULTED':
                stat_val = 2
            elif stat == 'OFFLINE':
                stat_val = 3
            elif stat == 'REMOVED':
                stat_val = 4
            elif stat == 'UNAVAIL':
                stat_val = 5
            else:
                stat_val = 6
            if pool is not None:
                r.append({'chk_instance': pool,
                          'chk_value': str(stat_val),
                          'chk_svcname': self.find_svc(pool),
                        })
        return r
  0707010000bb73000081a400000000000000000000000154fc7bfb00001288000000660000000300000000000000000000001f00000000root/opt/opensvc/lib/checks.py    #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from __future__ import print_function
from rcGlobalEnv import rcEnv
import os

class check(object):
    undef = [{
              'check_svcname': '',
              'chk_instance': 'undef',
              'chk_value': '-1'
             }]
    def __init__(self, svcs=[]):
        self.svcs = svcs
        if self.svcs is None:
            self.svcs = []

    def do_check(self):
        return []

class checks(check):
    check_list = []

    def __init__(self, svcs=[]):
        self.svcs = svcs
        self.register('checkFsUsage')
        self.register('checkFsInode')
        self.register('checkVgUsage')
        self.register('checkEth')
        self.register('checkLag')
        self.register('checkMpath')
        self.register('checkMpathPowerpath')
        self.register('checkZfsUsage')
        self.register('checkRaidSmartArray')
        self.register('checkRaidMegaRaid')
        self.register('checkRaidSas2')
        self.register('checkFmFmadm')
        self.register('checkFmOpenManage')
        self.register('checkMce')
        self.register('checkZpool')
        self.register('checkBtrfsDevStats')
        self.register('checkAdvfsUsage')
        self.register('checkNuma')
        self.register_local_checkers()

    def __iadd__(self, c):
        if isinstance(c, check):
            self.check_list.append(c)
        elif isinstance(c, checks):
            self.check_list += c.check_list
        return self

    def register_local_checkers(self):
        import os
        import glob
        check_d = os.path.join(rcEnv.pathvar, 'check')
        if not os.path.exists(check_d):
            return
        import sys
        sys.path.append(check_d)
        for f in glob.glob(os.path.join(check_d, 'check*.py')):
            if rcEnv.sysname not in f:
                continue
            cname = os.path.basename(f).replace('.py', '')
            try:
                m = __import__(cname)
                self += m.check(svcs=self.svcs)
            except:
                print('Could not import check:', cname, file=sys.stderr)

    def register(self, chk_name):
        if not os.path.exists(os.path.join(rcEnv.pathlib, chk_name+rcEnv.sysname+'.py')):
            return
        m = __import__(chk_name+rcEnv.sysname)
        self += m.check(svcs=self.svcs)

    def do_checks(self):
        import datetime

        now = str(datetime.datetime.now())
        vars = [\
            "chk_nodename",
            "chk_svcname",
            "chk_type",
            "chk_instance",
            "chk_value",
            "chk_updated"]
        vals = []

        for chk in self.check_list:
            # print header
            s = chk.chk_type
            if hasattr(chk, "chk_name"):
                s += ' (' + chk.chk_name + ')'
            print(s)

            d = chk.do_check()
            if type(d) != list or len(d) == 0:
                continue
            for i in d:
                if not isinstance(i, dict):
                    continue
                if 'chk_instance' not in i:
                    continue
                if i['chk_instance'] == 'undef':
                    continue
                if 'chk_value' not in i:
                    continue
                if 'chk_svcname' in i:
                    chk_svcname = i['chk_svcname']
                else:
                    chk_svcname = ""

                # print instance
                s = "  " + i['chk_instance']
                if len(chk_svcname) > 0:
                    s += '@' + chk_svcname
                s += ': ' + i['chk_value']
                print(s)

                vals.append([\
                    rcEnv.nodename,
                    chk_svcname,
                    chk.chk_type,
                    i['chk_instance'],
                    i['chk_value'].replace("%",""),
                    now]
                )
        self.node.collector.call('push_checks', vars, vals)
0707010000bb74000081a400000000000000000000000154fc7bfb00003969000000660000000300000000000000000000002200000000root/opt/opensvc/lib/collector.py from stat import *
import os
import sys
import re
import datetime
import rcExceptions as ex
from rcGlobalEnv import rcEnv
from rcUtilities import is_exe, justcall, banner
from subprocess import *
from rcPrintTable import print_table

class Collector(object):
    def __init__(self, options=None, collector=None, svcname=None):
        self.options = options
        self.collector = collector
        self.svcname = svcname
        self.options = options

    def expand_list(self, d):
        if len(d) < 2:
            return []
        l = []
        titles = d[0]
        for _d in d[1:]:
            h = {}
            for a, b in zip(titles, _d):
                h[a] = b
            l.append(h)
        return l

    def rotate_root_pw(self, pw):
        opts = {}
        opts['pw'] = pw
        d = self.collector.call('collector_update_root_pw', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

    def collector_ack_unavailability(self):
        if self.svcname is None:
            return

        opts = {}
        opts['svcname'] = self.svcname
        if self.options.begin is not None:
            opts['begin'] = self.options.begin
        if self.options.end is not None:
            opts['end'] = self.options.end
        if self.options.author is not None:
            opts['author'] = self.options.author
        if self.options.comment is not None:
            opts['comment'] = self.options.comment
        if self.options.duration is not None:
            opts['duration'] = self.options.duration
        if self.options.account:
            opts['account'] = "1"
        else:
            opts['account'] = "0"

        d = self.collector.call('collector_ack_unavailability', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

    def collector_list_unavailability_ack(self):
        d = self._collector_list_unavailability_ack()
        print_table(d, table=self.options.table)

    def collector_json_list_unavailability_ack(self):
        d = self._collector_list_unavailability_ack()
        import json
        print(json.dumps(self.expand_list(d), indent=4, separators=(',', ': ')))

    def _collector_list_unavailability_ack(self):
        if self.svcname is None:
            return

        opts = {}
        opts['svcname'] = self.svcname
        if self.options.begin is not None:
            opts['begin'] = self.options.begin
        if self.options.end is not None:
            opts['end'] = self.options.end
        if self.options.author is not None:
            opts['author'] = self.options.author
        if self.options.comment is not None:
            opts['comment'] = self.options.comment
        if self.options.duration is not None:
            opts['duration'] = self.options.duration
        if self.options.account:
            opts['account'] = "1"
        else:
            opts['account'] = "0"

        d = self.collector.call('collector_list_unavailability_ack', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

        return d['data']

    def collector_list_actions(self):
        d = self._collector_list_actions()
        print_table(d, table=self.options.table)

    def collector_json_list_actions(self):
        d = self._collector_list_actions()
        import json
        print(json.dumps(self.expand_list(d), indent=4, separators=(',', ': ')))

    def _collector_list_actions(self):
        opts = {}
        if self.svcname is not None:
            opts['svcname'] = self.svcname
        if self.options.begin is not None:
            opts['begin'] = self.options.begin
        if self.options.end is not None:
            opts['end'] = self.options.end
        if self.options.duration is not None:
            opts['duration'] = self.options.duration

        d = self.collector.call('collector_list_actions', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

        return d['data']

    def collector_ack_action(self):
        opts = {}
        if self.svcname is not None:
            opts['svcname'] = self.svcname
        if self.options.author is not None:
            opts['author'] = self.options.author
        if self.options.comment is not None:
            opts['comment'] = self.options.comment
        if self.options.id == 0:
            raise ex.excError("--id is not set")
        else:
            opts['id'] = self.options.id

        d = self.collector.call('collector_ack_action', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

    def collector_status(self):
        d = self._collector_status()
        print_table(d, table=self.options.table)

    def collector_json_status(self):
        d = self._collector_status()
        import json
        print(json.dumps(self.expand_list(d), indent=4, separators=(',', ': ')))

    def _collector_status(self):
        opts = {}
        if self.svcname is not None:
            opts['svcname'] = self.svcname
        d = self.collector.call('collector_status', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

        return d['data']

    def collector_networks(self, table=True):
        d = self._collector_networks()
        print_table(d, table=self.options.table)

    def collector_json_networks(self):
        d = self._collector_networks()
        import json
        print(json.dumps(self.expand_list(d), indent=4, separators=(',', ': ')))

    def _collector_networks(self):
        opts = {}
        if self.svcname is not None:
            opts['svcname'] = self.svcname
        d = self.collector.call('collector_networks', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

        return d['data']

    def collector_asset(self, table=True):
        d = self._collector_asset()
        print_table(d, table=self.options.table)

    def collector_json_asset(self):
        d = self._collector_asset()
        import json
        print(json.dumps(self.expand_list(d), indent=4, separators=(',', ': ')))

    def _collector_asset(self):
        opts = {}
        if self.svcname is not None:
            opts['svcname'] = self.svcname
        d = self.collector.call('collector_asset', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

        return d['data']

    def collector_checks(self, table=True):
        d = self._collector_checks()
        print_table(d, table=self.options.table)

    def collector_json_checks(self):
        d = self._collector_checks()
        import json
        print(json.dumps(self.expand_list(d), indent=4, separators=(',', ': ')))

    def _collector_checks(self):
        opts = {}
        if self.svcname is not None:
            opts['svcname'] = self.svcname
        d = self.collector.call('collector_checks', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

        return d['data']

    def collector_disks(self):
        d = self._collector_disks()
        print_table(d, width=64, table=self.options.table)

    def collector_json_disks(self):
        d = self._collector_disks()
        import json
        print(json.dumps(self.expand_list(d), indent=4, separators=(',', ': ')))

    def _collector_disks(self):
        opts = {}
        if self.svcname is not None:
            opts['svcname'] = self.svcname
        d = self.collector.call('collector_disks', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

        return d['data']

    def collector_alerts(self):
        d = self._collector_alerts()
        print_table(d, width=30, table=self.options.table)

    def collector_json_alerts(self):
        d = self._collector_alerts()
        import json
        print(json.dumps(self.expand_list(d), indent=4, separators=(',', ': ')))

    def _collector_alerts(self):
        opts = {}
        if self.svcname is not None:
            opts['svcname'] = self.svcname
        d = self.collector.call('collector_alerts', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

        return d['data']

    def collector_events(self):
        d = self._collector_events()
        print_table(d, width=50, table=self.options.table)
        
    def collector_json_events(self):
        d = self._collector_events()
        import json
        print(json.dumps(self.expand_list(d), indent=4, separators=(',', ': ')))

    def _collector_events(self):
        opts = {}
        if self.svcname is not None:
            opts['svcname'] = self.svcname
        if self.options.begin is not None:
            opts['begin'] = self.options.begin
        if self.options.end is not None:
            opts['end'] = self.options.end
        d = self.collector.call('collector_events', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

        return d['data']

    def collector_show_actions(self):
        d = self._collector_show_actions()
        print_table(d, width=50, table=self.options.table)
        
    def collector_json_show_actions(self):
        d = self._collector_show_actions()
        import json
        print(json.dumps(self.expand_list(d), indent=4, separators=(',', ': ')))

    def _collector_show_actions(self):
        opts = {}
        if self.svcname is not None:
            opts['svcname'] = self.svcname
        if self.options.id != 0:
            opts['id'] = self.options.id
        if self.options.begin is not None:
            opts['begin'] = self.options.begin
        if self.options.end is not None:
            opts['end'] = self.options.end
        d = self.collector.call('collector_show_actions', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

        return d['data']

    def collector_untag(self):
        opts = {}
        opts['tag_name'] = self.options.tag
        if self.svcname:
            opts['svcname'] = self.svcname
        d = self.collector.call('collector_untag', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

    def collector_tag(self):
        opts = {}
        opts['tag_name'] = self.options.tag
        if self.svcname:
            opts['svcname'] = self.svcname
        d = self.collector.call('collector_tag', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

    def collector_create_tag(self):
        opts = {}
        opts['tag_name'] = self.options.tag
        if self.svcname:
            opts['svcname'] = self.svcname
        d = self.collector.call('collector_create_tag', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])

    def collector_list_tags(self):
        d = self._collector_list_tags()
        for tag in d:
            print(tag)

    def _collector_list_tags(self):
        opts = {'pattern': self.options.like}
        if self.svcname:
            opts['svcname'] = self.svcname
        d = self.collector.call('collector_list_tags', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])
        return d['data']

    def collector_show_tags(self):
        d = self._collector_show_tags()
        for tag in d:
            print(tag)

    def _collector_show_tags(self):
        opts = {}
        if self.svcname:
            opts['svcname'] = self.svcname
        d = self.collector.call('collector_show_tags', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])
        return d['data']

    def collector_list_nodes(self):
        d = self._collector_list_nodes()
        for node in d:
            print(node)
        
    def collector_json_list_nodes(self):
        d = self._collector_list_nodes()
        import json
        print(json.dumps(d, indent=4, separators=(',', ': ')))

    def _collector_list_nodes(self):
        opts = {'fset': self.options.filterset}
        d = self.collector.call('collector_list_nodes', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])
        return d['data']

    def collector_list_services(self):
        d = self._collector_list_services()
        for service in d:
            print(service)
        
    def collector_json_list_services(self):
        d = self._collector_list_services()
        import json
        print(json.dumps(d, indent=4, separators=(',', ': ')))

    def _collector_list_services(self):
        opts = {'fset': self.options.filterset}
        d = self.collector.call('collector_list_services', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])
        return d['data']

    def collector_list_filtersets(self):
        d = self._collector_list_filtersets()
        for fset in d:
            print(fset)
        
    def collector_json_list_filtersets(self):
        d = self._collector_list_filtersets()
        import json
        print(json.dumps(d, indent=4, separators=(',', ': ')))

    def _collector_list_filtersets(self):
        opts = {'fset': self.options.filterset}
        d = self.collector.call('collector_list_filtersets', opts)
        if d is None:
            raise ex.excError("xmlrpc unknown failure")
        if d['ret'] != 0:
            raise ex.excError(d['msg'])
        return d['data']

   0707010000bb75000081a400000000000000000000000154fc7bfb0000637d000000660000000300000000000000000000002300000000root/opt/opensvc/lib/compliance.py    from __future__ import print_function
from stat import *
import os
import sys
import re
import datetime
import rcExceptions as ex
from rcGlobalEnv import rcEnv
from rcUtilities import is_exe, justcall, banner, fork
from subprocess import *
from rcPrintTable import print_table
from rcStatus import color, _colorize

comp_dir = os.path.join(rcEnv.pathvar, 'compliance')

# ex: \x1b[37;44m\x1b[1mContact List\x1b[0m\n
regex = re.compile("\x1b\[([0-9]{1,3}(;[0-9]{1,3})*)?[m|K|G]", re.UNICODE)

class Module(object):
    pattern = '^S*[0-9]+-*%(name)s$'

    def __init__(self, name, autofix=False, moduleset=None):
        self.name = name
        self.moduleset = moduleset
        self.executable = None
        self.autofix = autofix

        dl = os.listdir(comp_dir)
        match = []
        for e in dl:
            if re.match(self.pattern%dict(name=name), e) is not None:
                match.append(e)
        if len(match) == 0:
            raise ex.excInitError('module %s not found in %s'%(name, comp_dir))
        if len(match) > 1:
            raise ex.excError('module %s matches too many entries in %s'%(name,
                              comp_dir))
        base = match[0]
        if base[0] == 'S':
            base == base[1:]
        for i, c in enumerate(base):
            if not c.isdigit():
               break
        self.ordering = int(base[0:i])
        regex2 = re.compile("^S*[0-9]+-*", re.UNICODE)
        self.name = regex2.sub("", match[0])

        locations = []
        locations.append(os.path.join(comp_dir, match[0]))
        locations.append(os.path.join(locations[0], 'main'))
        locations.append(os.path.join(locations[0], 'scripts', 'main'))

        for loc in locations:
            if not os.path.exists(loc):
                continue
            statinfo = os.stat(loc)
            mode = statinfo[ST_MODE]
            if statinfo.st_uid != 0 or statinfo.st_gid not in (0,2,3,4):
                raise ex.excError('%s is not owned by root. security hazard.'%(loc))
            if not S_ISREG(mode):
                continue
            if not is_exe(loc):
                mode |= S_IXUSR
                os.chmod(loc, mode)
            self.executable = loc
        if self.executable is None:
            raise ex.excError('executable not found for module %s'%(name))

    def __str__(self):
        a = []
        a.append("name: %s"%self.name)
        a.append("ordering: %d"%self.ordering)
        a.append("executable: %s"%self.executable)
        return '\n'.join(a)


    def strip_unprintable(self, s):
        return regex.sub('', s).decode('utf8', 'ignore')

    def log_action(self, out, ret, action):
        vals = [rcEnv.nodename,
                self.name,
                str(ret),
                self.strip_unprintable(out),
                action,
                self.rset_md5]
        if self.svcname is not None:
            vals.append(self.svcname)
        else:
            vals.append("")
        self.context.action_log_vals.append(vals)

    def setup_env(self):
        os.environ.clear()
        os.environ.update(self.context.env_bkp)
        for rule in self.ruleset.values():
            if rule["filter"] != "explicit attachment via moduleset" or ( \
               self.moduleset in self.context.data["modset_rset_relations"]  and \
               rule['name'] in self.context.data["modset_rset_relations"][self.moduleset]
               ):
                for var, val in rule['vars']:
                    os.environ[self.context.format_rule_var(var)] = self.context.format_rule_val(val)


    def action(self, action):
        self.print_bold(banner(self.name))

        if action not in ['check', 'fix', 'fixable', 'env']:
            print('action %s not supported')
            return 1

        if self.options.force:
            # short-circuit all pre and post action
            return self.do_action(action)

        if action == 'fix':
            if self.do_action('check') == 0:
                print('check passed, skip fix')
                return 0
            if self.do_action('fixable') not in (0, 2):
                print('not fixable, skip fix')
                return 1
            self.do_action('fix')
            r = self.do_action('check')
        elif action == 'check':
            r = self.do_action('check')
            if r == 1:
                self.do_action('fixable')
        elif action == 'fixable':
            r = self.do_action('fixable')
        elif action == 'env':
            r = self.do_env()
        return r

    def do_env(self):
        a = []
        self.setup_env()
        for var in sorted(os.environ):
            val = os.environ[var]
            a.append('%s=%s'%(var, val))
        print('\n'.join(a))
        return 0

    def do_action(self, action):
        start = datetime.datetime.now()
        cmd = [self.executable, action]
        log = ''
        self.print_bold("ACTION:   %s"%action)
        print("START:    %s"%str(start))
        print("COMMAND:  %s"%' '.join(cmd))
        print("LOG:")

        import tempfile
        import time
        fo = tempfile.NamedTemporaryFile()
        fe = tempfile.NamedTemporaryFile()
        _fo = None
        _fe = None

        def poll_out():
            fop = _fo.tell()
            line = _fo.readline()
            if not line:
                _fo.seek(fop)
                return None
            sys.stdout.write(line)
            sys.stdout.flush()
            return line

        def poll_err():
            fep = _fe.tell()
            line = _fe.readline()
            if not line:
                _fe.seek(fep)
                return None
            _line = color.RED + 'ERR: ' + color.END + line
            line = 'ERR: '+line
            sys.stdout.write(_line)
            sys.stdout.flush()
            return line

        def poll_pipes(log):
            i = 0
            while True:
                o = poll_out()
                e = poll_err()
                if o is not None:
                    log += o
                if e is not None:
                    log += e
                if o is None and e is None:
                    break
            return log

        try:
            self.setup_env()
            p = Popen(cmd, stdout=fo, stderr=fe, env=os.environ)
            _fo = open(fo.name, 'r')
            _fe = open(fe.name, 'r')
            while True:
                time.sleep(0.1)
                log = poll_pipes(log)
                if p.poll() != None:
                    log = poll_pipes(log)
                    break
        except OSError as e:
            if _fo is not None:
                _fo.close()
            if _fe is not None:
                _fe.close()
            fo.close()
            fe.close()
            if e.errno == 2:
                raise ex.excError("%s execution error (File not found or bad interpreter)"%self.executable)
            elif e.errno == 8:
                raise ex.excError("%s execution error (Exec format error)"%self.executable)
            else:
                raise
        fo.close()
        fe.close()
        _fo.close()
        _fe.close()
        end = datetime.datetime.now()
        self.print_rcode(p.returncode)
        print("DURATION: %s"%str(end-start))
        self.log_action(log, p.returncode, action)
        return p.returncode

    def print_bold(self, s):
        print(_colorize(s, color.BOLD))

    def print_rcode(self, r):
        if r == 1:
            print(_colorize("RCODE:    %d"%r, color.RED))
        elif r == 0:
            print(_colorize("RCODE:    %d"%r, color.GREEN))
        else:
            print("RCODE:    %d"%r)

    def env(self):
        return self.action('env')

    def check(self):
        return self.action('check')

    def fix(self):
        return self.action('fix')

    def fixable(self):
        return self.action('fixable')

class Compliance(object):
    def __init__(self, skip_action=None, options=None, collector=None, svcname=None):
        self.skip_action = skip_action
        self.options = options
        self.collector = collector
        self.svcname = svcname
        self.options = options
        self.module_o = {}
        self.module = []
        self.updatecomp = False
        self.moduleset = None
        self.data = None
        self.action_log_vals = []
        self.action_log_vars = [
          'run_nodename',
          'run_module',
          'run_status',
          'run_log',
          'run_action',
          'rset_md5',
          'run_svcname']
        self.env_bkp = os.environ.copy()

    def set_rset_md5(self):
        self.rset_md5 = ""
        rs = self.ruleset.get("osvc_collector")
        if rs is None:
            return
        for var, val in rs["vars"]:
            if var == "ruleset_md5":
                self.rset_md5 = val
                break

    def setup_env(self):
        for rule in self.ruleset.values():
            for var, val in rule['vars']:
                os.environ[self.format_rule_var(var)] = self.format_rule_val(val)

    def reset_env(self):
        os.environ.clear()
        os.environ.update(self.env_bkp)

    def scheduler_fork(fn):
        def _fn(*args, **kwargs):
            self = args[0]
            if self.options.cron:
                fork(fn, args, kwargs, serialize=True)
            else:
                fn(*args, **kwargs)
        return _fn

    def compliance_auto(self):
        flag = "last_comp_check"
        if self.svcname is not None:
            flag = '.'.join((flag, self.svcname))
        if self.skip_action is not None and \
           self.skip_action("compliance_auto", fname=flag):
            return
        self.task_compliance_auto()

    @scheduler_fork
    def task_compliance_auto(self):
        if self.updatecomp:
            self.node.updatecomp()
        self.do_auto()

    def compliance_env(self):
        self.do_run('env')

    def compliance_check(self):
        self.do_checks()

    def __iadd__(self, o):
        self.module_o[o.name] = o
        o.svcname = self.svcname
        o.ruleset = self.ruleset
        o.options = self.options
        o.collector = self.collector
        o.context = self
        o.rset_md5 = self.rset_md5
        return self

    def print_bold(self, s):
        print(_colorize(s, color.BOLD))

    def expand_modulesets(self, modulesets):
        l = []

        def recurse(ms):
            l.append(ms)
            if ms not in self.data["modset_relations"]:
                return
            for _ms in self.data["modset_relations"][ms]:
                recurse(_ms)

        for ms in modulesets:
            recurse(ms)

        return l

    def init(self):
        if self.options.moduleset != "" and self.options.module != "":
            raise ex.excError('--moduleset and --module are exclusive')

        if self.data is None:
            self.data = self.get_comp_data()
            modulesets = []
            if self.options.moduleset != "":
                # purge unspecified modulesets
                modulesets = self.options.moduleset.split(',')
                modulesets = self.expand_modulesets(modulesets)
                for ms in self.data["modulesets"].keys():
                    if ms not in modulesets:
                        del(self.data["modulesets"][ms])
            elif self.options.module != "":
                # purge unspecified modules
                modules = self.options.module.split(',')
                for ms, data in self.data["modulesets"].items():
                    n = len(data)
                    for i in sorted(range(n), reverse=True):
                        module, autofix = data[i]
                        if module not in modules:
                            del(self.data["modulesets"][ms][i])
                for module in modules:
                    in_modsets = []
                    for ms, data in self.data["modulesets"].items():
                        for _module, autofix in data:
                            if module == _module:
                               in_modsets.append(ms)
                    if len(in_modsets) == 0:
                        print("module %s not found in any attached moduleset" % module)
                    elif len(in_modsets) > 1:
                        raise ex.excError("module %s found in multiple attached moduleset (%s). Use --moduleset instead of --module to clear the ambiguity" % (module, ', '.join(in_modsets)))
                    
            if len(modulesets) > 0 and \
               hasattr(self.options, "attach") and self.options.attach:
                self._compliance_attach_moduleset(modulesets)

        self.module = self.merge_moduleset_modules()
        self.ruleset = self.data['rulesets']
        self.set_rset_md5()

        if not os.path.exists(comp_dir):
            os.makedirs(comp_dir, 0o755)
            raise ex.excError('modules [%s] are not present in %s'%(
                               ','.join(self.module), comp_dir))

        for module, autofix, moduleset in self.module:
            try:
                self += Module(module, autofix, moduleset)
            except ex.excInitError as e:
                print(e, file=sys.stderr)

        self.ordered_module = self.module_o.keys()
        self.ordered_module.sort(lambda x, y: cmp(self.module_o[x].ordering,
                                                  self.module_o[y].ordering))

    def __str__(self):
        print(banner('run context'))
        a = []
        a.append('modules:')
        for m in self.ordered_module:
            a.append(' %0.2d %s'%(self.module_o[m].ordering, m))
        a.append(self.str_ruleset())
        return '\n'.join(a)

    def format_rule_var(self, var):
        var = var.upper().replace('-', '_').replace(' ', '_').replace('.','_')
        var = '_'.join(('OSVC_COMP', var))
        return var

    def format_rule_val(self, val):
        if isinstance(val, unicode):
            val = repr(val).strip("'")
        else:
            val = str(val)
        return val

    def get_moduleset(self):
        if self.svcname is not None:
            moduleset = self.collector.call('comp_get_svc_data_moduleset', self.svcname)
        else:
            moduleset = self.collector.call('comp_get_data_moduleset')
        if moduleset is None:
            raise ex.excError('could not fetch moduleset')
        return moduleset

    def get_ruleset(self):
        if hasattr(self.options, 'ruleset') and \
           len(self.options.ruleset) > 0:
            return self.get_ruleset_md5(self.options.ruleset)
        return self.get_current_ruleset()

    def get_current_ruleset(self):
        if self.svcname is not None:
            ruleset = self.collector.call('comp_get_svc_ruleset', self.svcname)
        else:
            ruleset = self.collector.call('comp_get_ruleset')
        if ruleset is None:
            raise ex.excError('could not fetch ruleset')
        return ruleset

    def get_ruleset_md5(self, rset_md5):
        ruleset = self.collector.call('comp_get_ruleset_md5', rset_md5)
        if ruleset is None:
            raise ex.excError('could not fetch ruleset')
        return ruleset

    def str_ruleset(self):
        a = []
        a.append('rules:')
        for rule in self.ruleset.values():
            if len(rule['filter']) == 0:
                a.append(' %s'%rule['name'])
            else:
                a.append(' %s (%s)'%(rule['name'],rule['filter']))
            for var, val in rule['vars']:
                val = self.format_rule_val(val)
                if ' ' in val:
                    val = repr(val)
                a.append('  %s=%s'%(self.format_rule_var(var), val))
        return '\n'.join(a)

    def get_comp_data(self, modulesets=[]):
        if self.svcname is not None:
            return self.collector.call('comp_get_svc_data', self.svcname, modulesets)
        else:
            return self.collector.call('comp_get_data', modulesets)

    def merge_moduleset_modules(self):
        l = [] 
        for ms, data in self.data['modulesets'].items():
            for module, autofix in data:
                if (module, autofix) not in l:
                    l.append((module, autofix, ms))
                elif autofix and (module, False, ms) in l:
                    l.remove((module, False, ms))
                    l.append((module, True, ms))
        return l

    def digest_errors(self, err):
        passed = [m for m in err if err[m] == 0]
        errors = [m for m in err if err[m] == 1]
        na = [m for m in err if err[m] == 2]

        n_passed = len(passed)
        n_errors = len(errors)
        n_na = len(na)

        def _s(n):
            if n > 1:
                return 's'
            else:
                return ''

        def modules(l):
            if len(l) == 0:
                return ''
            return '\n%s'%'\n'.join(map(lambda x: ' '+x, l))

        self.print_bold(banner("digest"))
        print("%d n/a%s"%(n_na, modules(na)))
        print("%d passed%s"%(n_passed, modules(passed)))
        print("%d error%s%s"%(n_errors, _s(n_errors), modules(errors)))

        if len(errors) > 0:
            return 1
        return 0

    def compliance_show_moduleset(self):
        def recurse(ms, depth=0):
            prefix=" "*depth
            print(prefix+ms+':')
            for module, autofix in data["modulesets"][ms]:
                if autofix:
                    s = " (autofix)"
                else:
                    s = ""
                print(prefix+' %s%s' % (module, s))
            if ms in data["modset_relations"]:
                for _ms in data["modset_relations"][ms]:
                    recurse(_ms, depth+1)

        data = self.get_moduleset()
        for ms in data["root_modulesets"]:
            recurse(ms)

    def compliance_show_ruleset(self):
        self.ruleset = self.get_ruleset()
        print(self.str_ruleset())

    def do_run(self, action):
        err = {}
        self.init()
        start = datetime.datetime.now()
        for module in self.ordered_module:
            _action = action
            if action == "auto":
                if self.module_o[module].autofix:
                    _action = "fix"
                else:
                    _action = "check"
            err[module] = getattr(self.module_o[module], _action)()
        if action == "env":
            return 0
        r = self.digest_errors(err)
        end = datetime.datetime.now()
        print("total duration: %s"%str(end-start))
        self.collector.call('comp_log_actions', self.action_log_vars, self.action_log_vals)
        return r

    def do_auto(self):
        return self.do_run('auto')

    def do_checks(self):
        return self.do_run('check')

    def compliance_fix(self):
        return self.do_run('fix')

    def compliance_fixable(self):
        return self.do_run('fixable')

    def compliance_detach(self):
        did_something = False
        if hasattr(self.options, 'moduleset') and \
           len(self.options.moduleset) > 0:
            did_something = True
            self._compliance_detach_moduleset(self.options.moduleset.split(','))
        if hasattr(self.options, 'ruleset') and \
           len(self.options.ruleset) > 0:
            did_something = True
            self._compliance_detach_ruleset(self.options.ruleset.split(','))
        if not did_something:
            raise ex.excError('no moduleset nor ruleset specified. use --moduleset and/or --ruleset')

    def compliance_attach(self):
        did_something = False
        if hasattr(self.options, 'moduleset') and \
           len(self.options.moduleset) > 0:
            did_something = True
            self._compliance_attach_moduleset(self.options.moduleset.split(','))
        if hasattr(self.options, 'ruleset') and \
           len(self.options.ruleset) > 0:
            did_something = True
            self._compliance_attach_ruleset(self.options.ruleset.split(','))
        if not did_something:
            raise ex.excError('no moduleset nor ruleset specified. use --moduleset and/or --ruleset')

    def compliance_attach_moduleset(self):
        if not hasattr(self.options, 'moduleset') or \
           len(self.options.moduleset) == 0:
            raise ex.excError('no moduleset specified. use --moduleset')
        self._compliance_attach_moduleset(self.options.moduleset.split(','))

    def _compliance_attach_moduleset(self, modulesets):
        err = False
        for moduleset in modulesets:
            if self.svcname is not None:
                d = self.collector.call('comp_attach_svc_moduleset', self.svcname, moduleset)
            else:
                d = self.collector.call('comp_attach_moduleset', moduleset)
            if d is None:
                print("Failed to attach '%s' moduleset. The collector may not be reachable." % moduleset, file=sys.stderr)
                err = True
                continue
            if not d['status']:
                err = True
            print(d['msg'])
        if err:
            raise ex.excError()

    def compliance_detach_moduleset(self):
        if not hasattr(self.options, 'moduleset') or \
           len(self.options.moduleset) == 0:
            raise ex.excError('no moduleset specified. use --moduleset')
        self._compliance_detach_moduleset(self.options.moduleset.split(','))

    def _compliance_detach_moduleset(self, modulesets):
        err = False
        for moduleset in modulesets:
            if self.svcname is not None:
                d = self.collector.call('comp_detach_svc_moduleset', self.svcname, moduleset)
            else:
                d = self.collector.call('comp_detach_moduleset', moduleset)
            if d is None:
                print("Failed to detach '%s' moduleset. The collector may not be reachable." % moduleset, file=sys.stderr)
                err = True
                continue
            if not d['status']:
                err = True
            print(d['msg'])
        if err:
            raise ex.excError()

    def compliance_attach_ruleset(self):
        if not hasattr(self.options, 'ruleset') or \
           len(self.options.ruleset) == 0:
            raise ex.excError('no ruleset specified. use --ruleset')
        self._compliance_attach_ruleset(self.options.ruleset.split(','))

    def _compliance_attach_ruleset(self, rulesets):
        err = False
        for ruleset in rulesets:
            if self.svcname is not None:
                d = self.collector.call('comp_attach_svc_ruleset', self.svcname, ruleset)
            else:
                d = self.collector.call('comp_attach_ruleset', ruleset)
            if d is None:
                print("Failed to attach '%s' ruleset. The collector may not be reachable." % ruleset, file=sys.stderr)
                err = True
                continue
            if not d['status']:
                err = True
            print(d['msg'])
        if err:
            raise ex.excError()

    def compliance_detach_ruleset(self):
        if not hasattr(self.options, 'ruleset') or \
           len(self.options.ruleset) == 0:
            raise ex.excError('no ruleset specified. use --ruleset')
        self._compliance_detach_ruleset(self.options.ruleset.split(','))

    def _compliance_detach_ruleset(self, rulesets):
        err = False
        for ruleset in rulesets:
            if self.svcname is not None:
                d = self.collector.call('comp_detach_svc_ruleset', self.svcname, ruleset)
            else:
                d = self.collector.call('comp_detach_ruleset', ruleset)
            if d is None:
                print("Failed to detach '%s' ruleset. The collector may not be reachable." % ruleset, file=sys.stderr)
                err = True
                continue
            if not d['status']:
                err = True
            print(d['msg'])
        if err:
            raise ex.excError()

    def compliance_show_status(self):
        args = ['comp_show_status']
        if self.svcname is None:
           args.append('')
        else:
           args.append(self.svcname)
        if hasattr(self.options, 'module') and \
           len(self.options.module) > 0:
            args.append(self.options.module)
        l = self.collector.call(*args)
        if l is None:
            return
        print_table(l, width=50, table=self.options.table)

    def compliance_list_ruleset(self):
        if not hasattr(self.options, 'ruleset') or \
           len(self.options.ruleset) == 0:
            l = self.collector.call('comp_list_ruleset')
        else:
            l = self.collector.call('comp_list_ruleset', self.options.ruleset)
        if l is None:
            return
        print('\n'.join(l))

    def compliance_list_moduleset(self):
        if not hasattr(self.options, 'moduleset') or \
           len(self.options.moduleset) == 0:
            l = self.collector.call('comp_list_moduleset')
        else:
            l = self.collector.call('comp_list_moduleset', self.options.moduleset)
        if l is None:
            return
        print('\n'.join(l))

    def compliance_list_module(self):
        import glob
        regex2 = re.compile("^S*[0-9]+-*", re.UNICODE)
        for path in glob.glob(os.path.join(comp_dir, '*')):
            name = regex2.sub("", os.path.basename(path))
            try:
                m = Module(name)
                print(m.name)
            except:
                continue


   0707010000bb76000081a400000000000000000000000154fc7bfb000006f4000000660000000300000000000000000000002000000000root/opt/opensvc/lib/freezer.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import glob

def dummy():
    return

class Freezer:
    flag_dir = os.path.join(os.path.dirname(__file__), '..', 'var')
    base_flag = os.path.join(flag_dir, 'FROZEN')
    flag = base_flag

    def frozen(self):
        if os.path.exists(self.flag) or os.path.exists(self.base_flag):
            return True
        return False

    def freeze(self):
        open(self.flag, 'w').close() 

    def thaw(self):
        if self.flag != self.base_flag and os.path.exists(self.flag):
            os.unlink(self.flag)
            return
        for name in glob.glob(self.flag_dir + '/FROZEN*'):
            os.unlink(name)

    def __init__(self, name=''):
        if len(name) == 0:
            pass
        elif not os.path.exists(os.path.join(os.path.dirname(__file__), '..', 'etc', name)):
            self.freeze = dummy
            self.thaw = dummy
            self.frozen = dummy
        else:
            self.flag = self.flag + "." + name
0707010000bb77000081a400000000000000000000000154fc7bfb00000453000000660000000300000000000000000000002200000000root/opt/opensvc/lib/hostidAIX.py #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from subprocess import *

def hostid():
    cmd = ['uname', '-u']
    p = Popen(cmd, stderr=None, stdout=PIPE, close_fds=True)
    buff = p.communicate()
    if p.returncode != 0:
        return '1'
    sn = buff[0].split()[0]
    return str(hex(abs(sn.__hash__()))).replace('0x', '')
 0707010000bb78000081a400000000000000000000000154fc7bfb00000378000000660000000300000000000000000000002500000000root/opt/opensvc/lib/hostidDarwin.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from uuid import getnode

def hostid():
    return str(getnode())
0707010000bb79000081a400000000000000000000000154fc7bfb000004b3000000660000000300000000000000000000002400000000root/opt/opensvc/lib/hostidHP-UX.py   #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from rcUtilities import which
from subprocess import *

def hostid():
    if which('getconf') is None:
        return '1'
    cmd = ['getconf', 'MACHINE_SERIAL']
    p = Popen(cmd, stderr=None, stdout=PIPE, close_fds=True)
    buff = p.communicate()
    sn = buff[0].split()[0]
    if p.returncode != 0:
        return '1'
    return str(hex(abs(sn.__hash__()))).replace('0x', '')
 0707010000bb7a000081a400000000000000000000000154fc7bfb00000378000000660000000300000000000000000000002400000000root/opt/opensvc/lib/hostidLinux.py   #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from uuid import getnode

def hostid():
    return str(getnode())
0707010000bb7b000081a400000000000000000000000154fc7bfb0000033d000000660000000300000000000000000000002300000000root/opt/opensvc/lib/hostidOSF1.py    #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from uuid import getnode

def hostid():
    return str(getnode())
   0707010000bb7c000081a400000000000000000000000154fc7bfb00000378000000660000000300000000000000000000002400000000root/opt/opensvc/lib/hostidSunOS.py   #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from uuid import getnode

def hostid():
    return str(getnode())
0707010000bb7d000081a400000000000000000000000154fc7bfb00000378000000660000000300000000000000000000002600000000root/opt/opensvc/lib/hostidWindows.py #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from uuid import getnode

def hostid():
    return str(getnode())
0707010000bb7f000081a400000000000000000000000154fc7bfb0000059f000000660000000300000000000000000000002200000000root/opt/opensvc/lib/loopSunOS.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import resources
from rcGlobalEnv import *
import rcStatus

class Loop(resources.Resource):
    def is_up(self):
        """Returns True if the volume group is present and activated
        """
        return True

    def start(self):
        pass

    def stop(self):
        pass

    def status(self, verbose=False):
        if self.is_up():
            return rcStatus.UP
        else:
            return rcStatus.DOWN

    def __init__(self, file, disabled=False, tags=set([]), optional=False):
        self.file = file
        resources.Resource.__init__(self, disabled=disabled, tags=tags, optional=optional)
 0707010000bb80000081a400000000000000000000000154fc7bfb0000d3d8000000660000000300000000000000000000001d00000000root/opt/opensvc/lib/node.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

from __future__ import print_function
from svc import Svc
from freezer import Freezer
import svcBuilder
import xmlrpcClient
import os
import datetime
import time
import sys
import json
from rcGlobalEnv import rcEnv
import rcCommandWorker
import socket
import rcLogger
import rcUtilities
import rcExceptions as ex
from subprocess import *
from rcScheduler import *

try:
    import ConfigParser
except ImportError:
    import configparser as ConfigParser

class Options(object):
    def __init__(self):
        self.cron = False
        self.syncrpc = False
        self.force = False
        self.debug = False
        self.stats_dir = None
        self.begin = None
        self.end = None
        self.moduleset = ""
        self.module = ""
        self.ruleset_date = ""
        self.waitlock = 60
        self.parallel = False
        self.objects = []
        os.environ['LANG'] = 'C'

class Node(Svc, Freezer, Scheduler):
    """ Defines a cluster node.  It contain list of Svc.
        Implements node-level actions and checks.
    """
    def __str__(self):
        s = self.nodename
        return s

    def __init__(self):
        self.auth_config = None
        self.nodename = socket.gethostname().lower()
        self.authconf = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'etc', 'auth.conf'))
        self.nodeconf = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'etc', 'node.conf'))
        self.dotnodeconf = os.path.join(os.path.dirname(__file__), '..', 'etc', '.node.conf')
        self.setup_sync_flag = os.path.join(rcEnv.pathvar, 'last_setup_sync')
        self.reboot_flag = os.path.join(rcEnv.pathvar, "REBOOT_FLAG")
        self.config_defaults = {
          'clusters': '',
          'host_mode': 'TST',
          'push_schedule': '00:00-06:00@361 mon-sun',
          'sync_schedule': '04:00-06:00@121 mon-sun',
          'comp_schedule': '02:00-06:00@241 sun',
          'no_schedule': '',
        }
        self.svcs = None
        try:
            self.clusters = list(set(self.config.get('node', 'clusters').split()))
        except:
            self.clusters = []
        self.load_config()
        self.options = Options()
        Scheduler.__init__(self)
        Freezer.__init__(self, '')
        self.action_desc = {
          'Node actions': {
            'shutdown': 'shutdown the node to powered off state',
            'reboot': 'reboot the node',
            'schedule_reboot_status': 'tell if the node is scheduled for reboot',
            'schedule_reboot': 'mark the node for reboot at the next allowed period. the allowed period is defined by a "reboot" section in node.conf. the created flag file is %s' % self.reboot_flag,
            'unschedule_reboot': 'unmark the node for reboot at the next allowed period. the removed flag file is %s' % self.reboot_flag,
            'provision': 'provision the resources described in --resource arguments',
            'updatepkg': 'upgrade the opensvc agent version. the packages must be available behind the node.repo/packages url.',
            'updatecomp': 'upgrade the opensvc compliance modules. the modules must be available as a tarball behind the node.repo/compliance url.',
            'scanscsi': 'scan the scsi hosts in search of new disks',
            'dequeue_actions': "dequeue and execute actions from the collector's action queue for this node and its services.",
            'rotate_root_pw': "set a new root password and store it in the collector",
            'print_schedule': "print the node tasks schedule",
            'wol': "forge and send udp wake on lan packet to mac address specified by --mac and --broadcast arguments",
          },
          'Service actions': {
            'discover': 'discover vservices accessible from this host, cloud nodes for example',
            'syncservices':   'send var files, config files and configured replications to other nodes for each node service',
            'updateservices': 'refresh var files associated with services',
          },
          'Node configuration edition': {
            'register': 'obtain a registration number from the collector, used to authenticate the node',
            'get': 'get the value of the node configuration parameter pointed by --param',
            'set': 'set a node configuration parameter (pointed by --param) value (pointed by --value)',
            'unset': 'unset a node configuration parameter (pointed by --param)',
          },
          'Push data to the collector': {
            'pushasset':      'push asset information to collector',
            'pushservices':   'push services configuration to collector',
            'pushstats':      'push performance metrics to collector. By default pushed stats interval begins yesterday at the beginning of the allowed interval and ends now. This interval can be changed using --begin/--end parameters. The location where stats files are looked up can be changed using --stats-dir.',
            'pushdisks':      'push disks usage information to collector',
            'pushpkg':        'push package/version list to collector',
            'pushpatch':      'push patch/version list to collector',
            'pushsym':        'push symmetrix configuration to collector',
            'pusheva':        'push HP EVA configuration to collector',
            'pushnecism':     'push NEC ISM configuration to collector',
            'pushhds':        'push HDS configuration to collector',
            'pushdcs':        'push Datacore configuration to collector',
            'pushibmsvc':     'push IBM SVC configuration to collector',
            'pushhp3par':     'push HP 3par configuration to collector',
            'pushibmds':      'push IBM DS configuration to collector',
            'pushvioserver':  'push IBM VIO server configuration to collector',
            'pushbrocade':    'push Brocade switch configuration to collector',
            'pushnsr':        'push EMC Networker index to collector',
            'push_appinfo':   'push services application launchers appinfo key/value pairs to collector',
            'sysreport':      'push system report to the collector for archiving and diff analysis',
            'checks':         'run node sanity checks, push results to collector',
          },
          'Misc': {
            'prkey':          'show persistent reservation key of this node',
          },
          'Compliance': {
            'compliance_auto': 'run compliance checks or fix, according to the autofix property of each module. --ruleset <md5> instruct the collector to provide an historical ruleset.',
            'compliance_env': 'show the compliance modules environment variables.',
            'compliance_check': 'run compliance checks. --ruleset <md5> instruct the collector to provide an historical ruleset.',
            'compliance_fix':   'run compliance fixes. --ruleset <md5> instruct the collector to provide an historical ruleset.',
            'compliance_fixable': 'verify compliance fixes prerequisites. --ruleset <md5> instruct the collector to provide an historical ruleset.',
            'compliance_list_module': 'list compliance modules available on this node',
            'compliance_show_moduleset': 'show compliance rules applying to this node',
            'compliance_list_moduleset': 'list available compliance modulesets. --moduleset f% limit the scope to modulesets matching the f% pattern.',
            'compliance_attach_moduleset': 'attach moduleset specified by --moduleset for this node',
            'compliance_detach_moduleset': 'detach moduleset specified by --moduleset for this node',
            'compliance_list_ruleset': 'list available compliance rulesets. --ruleset f% limit the scope to rulesets matching the f% pattern.',
            'compliance_show_ruleset': 'show compliance rules applying to this node',
            'compliance_show_status': 'show compliance modules status',
            'compliance_attach': 'attach ruleset specified by --ruleset and/or moduleset specified by --moduleset for this node',
            'compliance_detach': 'detach ruleset specified by --ruleset and/or moduleset specified by --moduleset for this node',
            'compliance_attach_ruleset': 'attach ruleset specified by --ruleset for this node',
            'compliance_detach_ruleset': 'detach ruleset specified by --ruleset for this node',
          },
          'Collector management': {
            'collector_events': 'display node events during the period specified by --begin/--end. --end defaults to now. --begin defaults to 7 days ago',
            'collector_alerts': 'display node alerts',
            'collector_checks': 'display node checks',
            'collector_disks': 'display node disks',
            'collector_status': 'display node services status according to the collector',
            'collector_list_actions': 'list actions on the node, whatever the service, during the period specified by --begin/--end. --end defaults to now. --begin defaults to 7 days ago',
            'collector_ack_action': 'acknowledge an action error on the node. an acknowlegment can be completed by --author (defaults to root@nodename) and --comment',
            'collector_show_actions': 'show actions detailled log. a single action is specified by --id. a range is specified by --begin/--end dates. --end defaults to now. --begin defaults to 7 days ago',
            'collector_list_nodes': 'show the list of nodes matching the filterset pointed by --filterset',
            'collector_list_services': 'show the list of services matching the filterset pointed by --filterset',
            'collector_list_filtersets': 'show the list of filtersets available on the collector. if specified, --filterset <pattern> limits the resulset to filtersets matching <pattern>',
            'collector_asset': 'display asset information known to the collector',
            'collector_networks': 'display network information known to the collector for each node ip',
            'collector_json_asset': 'display asset information known to the collector, output in JSON',
            'collector_json_networks': 'display network information known to the collector for each node ip, output in JSON',
            'collector_json_list_unavailability_ack': 'same as "collector list unavailability ack", output in JSON',
            'collector_json_list_actions': 'same as "collector list actions", output in JSON',
            'collector_json_show_actions': 'same as "collector show actions", output in JSON',
            'collector_json_status': 'same as "collector status", output in JSON',
            'collector_json_checks': 'same as "collector checks", output in JSON',
            'collector_json_disks': 'same as "collector disks", output in JSON',
            'collector_json_alerts': 'same as "collector alerts", output in JSON',
            'collector_json_events': 'same as "collector events", output in JSON',
            'collector_json_list_nodes': 'same as "collector list nodes", output in JSON',
            'collector_json_list_services': 'same as "collector list services", output in JSON',
            'collector_json_list_filtersets': 'same as "collector list filtersets", output in JSON',
            'collector_tag': 'set a node tag (pointed by --tag)',
            'collector_untag': 'unset a node tag (pointed by --tag)',
            'collector_show_tags': 'list all node tags',
            'collector_list_tags': 'list all available tags. use --like to filter the output.',
            'collector_create_tag': 'create a new tag',
          },
        }
        self.collector = xmlrpcClient.Collector()
        self.cmdworker = rcCommandWorker.CommandWorker()
        try:
            rcos = __import__('rcOs'+rcEnv.sysname)
        except ImportError:
            rcos = __import__('rcOs')
        self.os = rcos.Os()
        rcEnv.logfile = os.path.join(rcEnv.pathlog, "node.log")
        self.log = rcLogger.initLogger(rcEnv.nodename)
	self.scheduler_actions = {
	 "syncservices": SchedOpts("syncservices"),
	 "checks": SchedOpts("checks"),
	 "pushstats": SchedOpts("stats"),
	 "pushpkg": SchedOpts("packages"),
	 "pushpatch": SchedOpts("patches"),
	 "pushasset": SchedOpts("asset"),
	 "pushnsr": SchedOpts("nsr", schedule_option="no_schedule"),
	 "pushhp3par": SchedOpts("hp3par", schedule_option="no_schedule"),
	 "pushibmds": SchedOpts("ibmds", schedule_option="no_schedule"),
	 "pushdcs": SchedOpts("dcs", schedule_option="no_schedule"),
	 "pushhds": SchedOpts("hds", schedule_option="no_schedule"),
	 "pushnecism": SchedOpts("necism", schedule_option="no_schedule"),
	 "pusheva": SchedOpts("eva", schedule_option="no_schedule"),
	 "pushibmsvc": SchedOpts("ibmsvc", schedule_option="no_schedule"),
	 "pushvioserver": SchedOpts("vioserver", schedule_option="no_schedule"),
	 "pushsym": SchedOpts("sym", schedule_option="no_schedule"),
	 "pushbrocade": SchedOpts("brocade", schedule_option="no_schedule"),
	 "pushdisks": SchedOpts("disks"),
	 "pushservices": SchedOpts("svcconf"),
	 "push_appinfo": SchedOpts("appinfo"),
	 "sysreport": SchedOpts("sysreport"),
	 "compliance_auto": SchedOpts("compliance", fname="last_comp_check", schedule_option="comp_schedule"),
	 "auto_rotate_root_pw": SchedOpts("rotate_root_pw", fname="last_rotate_root_pw", schedule_option="no_schedule"),
	 "auto_reboot": SchedOpts("reboot", fname="last_auto_reboot", schedule_option="no_schedule")
        }


    def scheduler_fork(fn):
        def _fn(*args, **kwargs):
            self = args[0]
            if self.options.cron:
                rcUtilities.fork(fn, args, kwargs, serialize=True)
            else:
                fn(*args, **kwargs)
        return _fn

    def call(self, cmd=['/bin/false'], cache=False, info=False,
             errlog=True, err_to_warn=False, err_to_info=False,
             outlog=False):
        """Use subprocess module functions to do a call
        """
        return rcUtilities.call(cmd, log=self.log,
                                cache=cache,
                                info=info, errlog=errlog,
                                err_to_warn=err_to_warn,
                                err_to_info=err_to_info,
                                outlog=outlog)

    def vcall(self, cmd, err_to_warn=False, err_to_info=False):
        """Use subprocess module functions to do a call and
        log the command line using the resource logger
        """
        return rcUtilities.vcall(cmd, log=self.log,
                                 err_to_warn=err_to_warn,
                                 err_to_info=err_to_info)

    def supported_actions(self):
        a = []
        for s in self.action_desc:
            a += self.action_desc[s].keys()
        return a

    def build_services(self, *args, **kwargs):
        if self.svcs is not None and \
           ('svcnames' not in kwargs or \
           (type(kwargs['svcnames']) == list and len(kwargs['svcnames'])==0)):
            return

        if 'svcnames' in kwargs and \
           type(kwargs['svcnames']) == list and \
           len(kwargs['svcnames'])>0 and \
           self.svcs is not None:
            svcnames_request = set(kwargs['svcnames'])
            svcnames_actual = set([s.svcname for s in self.svcs])
            svcnames_request = list(svcnames_request-svcnames_actual)
            if len(svcnames_request) == 0:
                return

        self.svcs = []
        autopush = True
        if 'autopush' in kwargs:
            if not kwargs['autopush']:
                autopush = False
            del kwargs['autopush']
        svcs = svcBuilder.build_services(*args, **kwargs)
        for svc in svcs:
            self += svc
        if autopush:
            for svc in self.svcs:
                if svc.collector_outdated():
                    svc.action('push')

        if 'svcnames' in kwargs:
            if type(kwargs['svcnames']) == list:
                n = len(kwargs['svcnames'])
            else:
                n = 1
            if len(self.svcs) != n:
                raise ex.excError("building error")


    def close(self):
        self.collector.stop_worker()
        self.cmdworker.stop_worker()

    def _setup_sync_conf(self):
        h = {}
        self.build_services()
        self.config = ConfigParser.RawConfigParser()
        for svc in self.svcs:
            for rs in [_rs for _rs in svc.resSets if _rs.type.startswith('sync')]:
                for r in rs.resources:
                    s = '#'.join((svc.svcname, r.rid))
                    if not self.config.has_section(s):
                        self.config.add_section(s)
                    self.config.set(s, 'svcname', svc.svcname)
                    self.config.set(s, 'rid', r.rid)
                    self.config.set(s, 'schedule', r.schedule)
        self.write_dotconfig()
        with open(self.setup_sync_flag, 'w') as f:
            f.write(str(time.time()))

    def write_dotconfig(self):
        for o in self.config_defaults:
            if self.config.has_option('DEFAULT', o):
                self.config.remove_option('DEFAULT', o)
        for s in self.config.sections():
            if '#sync#' not in s:
                self.config.remove_section(s)
        try:
            fp = open(self.dotnodeconf, 'w')
            self.config.write(fp)
            fp.close()
        except:
            print("failed to write new %s"%self.dotnodeconf, file=sys.stderr)
            raise Exception()
        self.load_config()

    def write_config(self):
        for o in self.config_defaults:
            if self.config.has_option('DEFAULT', o):
                self.config.remove_option('DEFAULT', o)
        for s in self.config.sections():
            if '#sync#' in s:
                self.config.remove_section(s)
        try:
            fp = open(self.nodeconf, 'w')
            self.config.write(fp)
            fp.close()
        except:
            print("failed to write new %s"%self.nodeconf, file=sys.stderr)
            raise Exception()
        self.load_config()

    def purge_status_last(self):
        for s in self.svcs:
            s.purge_status_last()

    def load_config(self):
        self.config = ConfigParser.RawConfigParser(self.config_defaults)
        self.config.read(self.nodeconf)
        try:
            self.config.read(self.dotnodeconf)
        except:
            self._setup_sync_conf()
            self.write_dotconfig()

    def load_auth_config(self):
        if self.auth_config is not None:
            return
        self.auth_config = ConfigParser.ConfigParser()
        self.auth_config.read(self.authconf)

    def setup_sync_outdated(self):
        """ return True if one env file has changed in the last 10'
            else return False
        """
        import datetime
        import glob
        envs = glob.glob(os.path.join(rcEnv.pathetc, '*.env'))
        if not os.path.exists(self.setup_sync_flag):
            return True
        for pathenv in envs:
            try:
                mtime = os.stat(pathenv).st_mtime
                f = open(self.setup_sync_flag)
                last = float(f.read())
                f.close()
            except:
                return True
            if mtime > last:
                return True
        return False

    def setup_sync_conf(self):
        if self.setup_sync_outdated():
            self._setup_sync_conf()
            return
        elif not self.config.has_section('sync'):
            self._setup_sync_conf()
            return
        elif not self.config.has_option('sync', 'schedule'):
            self._setup_sync_conf()

    def format_desc(self, action=None):
        from textwrap import TextWrapper
        from compliance import Compliance
        from collector import Collector
        wrapper = TextWrapper(subsequent_indent="%19s"%"", width=78)
        desc = ""
        for s in sorted(self.action_desc):
            valid_actions = []
            for a in sorted(self.action_desc[s]):
                if action is not None and not a.startswith(action):
                    continue
                valid_actions.append(a)
            if len(valid_actions) == 0:
                continue
            l = len(s)
            desc += s+'\n'
            for i in range(0, l):
                desc += '-'
            desc += "\n\n"
            for a in valid_actions:
                if a.startswith("compliance_"):
                    o = Compliance(self.skip_action, self.options, self.collector)
                    if not hasattr(o, a):
                        continue
                elif a.startswith("collector_"):
                    o = Collector(self.options, self.collector)
                    if not hasattr(o, a):
                        continue
                elif not hasattr(self, a):
                    continue
                fancya = a.replace('_', ' ')
                if len(a) < 16:
                    text = "  %-16s %s\n"%(fancya, self.action_desc[s][a])
                    desc += wrapper.fill(text)
                else:
                    text = "  %-16s"%(fancya)
                    desc += wrapper.fill(text)
                    desc += '\n'
                    text = "%19s%s"%(" ", self.action_desc[s][a])
                    desc += wrapper.fill(text)
                desc += '\n\n'
        return desc[0:-2]

    def __iadd__(self, s):
        if not isinstance(s, Svc):
            return self
        if self.svcs is None:
            self.svcs = []
        s.node = self
        if not hasattr(s, "clustername") and len(self.clusters) == 1:
            s.clustername = self.clusters[0]
        self.svcs.append(s)
        return self

    def action(self, a):
        if a.startswith("compliance_"):
            from compliance import Compliance
            o = Compliance(self.skip_action, self.options, self.collector)
            if self.options.cron and a == "compliance_auto" and \
               self.config.has_option('compliance', 'auto_update') and \
               self.config.getboolean('compliance', 'auto_update'):
                o.updatecomp = True
                o.node = self
            return getattr(o, a)()
        elif a.startswith("collector_"):
            from collector import Collector
            o = Collector(self.options, self.collector)
            return getattr(o, a)()
        else:
            return getattr(self, a)()

    def pushstats(self):
        # set stats range to push to "last pushstat => now"

        ts = self.get_timestamp_f(self.scheduler_actions["pushstats"].fname)
        try:
            with open(ts, "r") as f:
                buff = f.read()
            start = datetime.datetime.strptime(buff, "%Y-%m-%d %H:%M:%S.%f\n")
            now = datetime.datetime.now()
            delta = now - start
            interval = delta.days * 1440 + delta.seconds // 60
            #print("push stats for the last %d minutes since last push" % interval)
        except Exception as e:
            interval = 1440
            #print("can not determine last push date. push stats for the last %d minutes" % interval)
        if interval < 21:
            interval = 21

        if self.skip_action("pushstats"):
            return
        self.task_pushstats(interval)

    @scheduler_fork
    def task_pushstats(self, interval):
        if self.config.has_option("stats", "disable"):
            disable = self.config.get("stats", "disable")
        else:
            disable = []

        if isinstance(disable, str):
            try:
                disable = json.loads(disable)
            except:
                if ',' in disable:
                    disable = disable.replace(' ','').split(',')
                else:
                    disable = disable.split(' ')
        else:
            disable = []

        return self.collector.call('push_stats',
                                stats_dir=self.options.stats_dir,
                                stats_start=self.options.begin,
                                stats_end=self.options.end,
                                interval=interval,
                                disable=disable)

    def pushpkg(self):
        if self.skip_action("pushpkg"):
            return
        self.task_pushpkg()

    @scheduler_fork
    def task_pushpkg(self):
        self.collector.call('push_pkg')

    def pushpatch(self):
        if self.skip_action("pushpatch"):
            return
        self.task_pushpatch()

    @scheduler_fork
    def task_pushpatch(self):
        self.collector.call('push_patch')

    def pushasset(self):
        if self.skip_action("pushasset"):
            return
        self.task_pushasset()

    @scheduler_fork
    def task_pushasset(self):
        self.collector.call('push_asset', self)

    def pushnsr(self):
        if self.skip_action("pushnsr"):
            return
        self.task_pushnsr()

    @scheduler_fork
    def task_pushnsr(self):
        self.collector.call('push_nsr')

    def pushhp3par(self):
        if self.skip_action("pushhp3par"):
            return
        self.task_pushhp3par()

    @scheduler_fork
    def task_pushhp3par(self):
        self.collector.call('push_hp3par', self.options.objects)

    def pushibmds(self):
        if self.skip_action("pushibmds"):
            return
        self.task_pushibmds()

    @scheduler_fork
    def task_pushibmds(self):
        self.collector.call('push_ibmds', self.options.objects)

    def pushdcs(self):
        if self.skip_action("pushdcs"):
            return
        self.task_pushdcs()

    @scheduler_fork
    def task_pushdcs(self):
        self.collector.call('push_dcs', self.options.objects)

    def pushhds(self):
        if self.skip_action("pushhds"):
            return
        self.task_pushhds()

    @scheduler_fork
    def task_pushhds(self):
        self.collector.call('push_hds', self.options.objects)

    def pushnecism(self):
        if self.skip_action("pushnecism"):
            return
        self.task_pushnecism()

    @scheduler_fork
    def task_pushnecism(self):
        self.collector.call('push_necism', self.options.objects)

    def pusheva(self):
        if self.skip_action("pusheva"):
            return
        self.task_pusheva()

    @scheduler_fork
    def task_pusheva(self):
        self.collector.call('push_eva', self.options.objects)

    def pushibmsvc(self):
        if self.skip_action("pushibmsvc"):
            return
        self.task_pushibmsvc()

    @scheduler_fork
    def task_pushibmsvc(self):
        self.collector.call('push_ibmsvc', self.options.objects)

    def pushvioserver(self):
        if self.skip_action("pushvioserver"):
            return
        self.task_pushvioserver()

    @scheduler_fork
    def task_pushvioserver(self):
        self.collector.call('push_vioserver', self.options.objects)

    def pushsym(self):
        if self.skip_action("pushsym"):
            return
        self.task_pushsym()

    @scheduler_fork
    def task_pushsym(self):
        self.collector.call('push_sym', self.options.objects)

    def pushbrocade(self):
        if self.skip_action("pushbrocade"):
            return
        self.task_pushbrocade()

    @scheduler_fork
    def task_pushbrocade(self):
        self.collector.call('push_brocade', self.options.objects)

    def auto_rotate_root_pw(self):
        if self.skip_action("auto_rotate_root_pw"):
            return
        self.task_auto_rotate_root_pw()

    @scheduler_fork
    def task_auto_rotate_root_pw(self):
        self.rotate_root_pw()

    def unschedule_reboot(self):
        if not os.path.exists(self.reboot_flag):
            print("reboot already not scheduled")
            return
        os.unlink(self.reboot_flag)
        print("reboot unscheduled")

    def schedule_reboot(self):
        if not os.path.exists(self.reboot_flag):
            with open(self.reboot_flag, "w") as f: f.write("")
        import stat
        s = os.stat(self.reboot_flag)
        if s.st_uid != 0:
            os.chown(self.reboot_flag, 0, -1)
            print("set %s root ownership"%self.reboot_flag)
        if s.st_mode & stat.S_IWOTH:
            mode = s.st_mode ^ stat.S_IWOTH
            os.chmod(self.reboot_flag, mode)
            print("set %s not world-writable"%self.reboot_flag)
        print("reboot scheduled")

    def schedule_reboot_status(self):
        import stat
        if not os.path.exists(self.reboot_flag):
            print("reboot is not scheduled")
            return
        s = os.stat(self.reboot_flag)
        if s.st_uid != 0 or s.st_mode & stat.S_IWOTH:
            print("reboot is not scheduled")
            return
        sch = self.scheduler_actions["auto_reboot"]
        schedule = self.sched_get_schedule_raw(sch.section, sch.schedule_option)
        print("reboot is scheduled")
        print("reboot schedule: %s" % schedule)
        now = datetime.datetime.now()
        _max = 14400
        self.options.cron = True
        for i in range(_max):
            d = now + datetime.timedelta(minutes=i*10)
            if not self.skip_action("auto_reboot", now=d, verbose=False):
                print("next allowed reboot:", d.strftime("%a %Y-%m-%d %H:%M"))
                break
        if i == _max - 1:
            print("next allowed reboot: none in the next %d days" % (_max/144))

    def auto_reboot(self):
        if self.skip_action("auto_reboot"):
            return
        self.task_auto_reboot()

    @scheduler_fork
    def task_auto_reboot(self):
        if not os.path.exists(self.reboot_flag):
            print("%s is not present. no reboot scheduled" % self.reboot_flag)
            return
        import stat
        s = os.stat(self.reboot_flag)
        if s.st_uid != 0:
            print("%s does not belong to root. abort scheduled reboot" % self.reboot_flag)
            return
        if s.st_mode & stat.S_IWOTH:
            print("%s is world writable. abort scheduled reboot" % self.reboot_flag)
            return
        print("remove %s and reboot" % self.reboot_flag)
        os.unlink(self.reboot_flag)
        self.reboot()

    def pushdisks(self):
        if self.skip_action("pushdisks"):
            return
        self.task_pushdisks()

    @scheduler_fork
    def task_pushdisks(self):
        if self.svcs is None:
            self.build_services()
        self.collector.call('push_disks', self)

    def need_sync(self):
        l = []
        for s in self.config.sections():
            if '#sync#' not in s:
                continue
            ts = '_'.join(('last_sync',
                           self.config.get(s, 'svcname'),
                           self.config.get(s, 'rid')))
            if self.skip_action("need_sync",
                                section=s,
                                fname=ts,
                                schedule_option='sync_schedule'):
                    continue
            l.append(self.config.get(s, 'svcname'))
        return l

    def shutdown(self):
        print("TODO")

    def reboot(self):
        print("TODO")

    def syncservices(self):
        self.setup_sync_conf()
        svcnames = self.need_sync()
        if len(svcnames) == 0:
            return

        from multiprocessing import Process
        p = {}

        if self.svcs is None:
            self.build_services(svcnames=svcnames)

        for svc in self.svcs:
            if svc.svcname not in svcnames:
                continue
            p[svc.svcname] = rcCommandWorker.CommandWorker(name=svc.svcname)
            cmd = [os.path.join(rcEnv.pathetc, svc.svcname), 'syncall']
            if self.options.force:
                cmd.append('--force')
            if self.options.cron:
                cmd.append('--cron')
            p[svc.svcname].enqueue(cmd)

        for svcname in p:
            p[svcname].stop_worker()

    @scheduler_fork
    def updateservices(self):
        if self.svcs is None:
            self.build_services()
        for svc in self.svcs:
            svc.cron = self.options.cron
            svc.action('presync')

    def pushservices(self):
        if self.skip_action("pushservices"):
            return
        self.tassk_pushservices()

    @scheduler_fork
    def task_pushservices(self):
        if self.svcs is None:
            self.build_services()
        for svc in self.svcs:
            svc.cron = self.options.cron
            svc.action('push')

    def push_appinfo(self):
        if self.skip_action("push_appinfo"):
            return
        self.task_push_appinfo()

    @scheduler_fork
    def task_push_appinfo(self):
        if self.svcs is None:
            self.build_services()
        for svc in self.svcs:
            svc.cron = self.options.cron
            svc.action('push_appinfo')

    def sysreport(self):
        if self.skip_action("sysreport"):
            return
        self.task_sysreport()

    @scheduler_fork
    def task_sysreport(self):
        from rcGlobalEnv import rcEnv
        try:
            m = __import__('rcSysReport'+rcEnv.sysname)
        except ImportError:
            print("sysreport is not supported on this os")
            return
        m.SysReport(node=self).sysreport(force=self.options.force)

    def prkey(self):
        from rcGlobalEnv import rcEnv
        m = __import__('hostid'+rcEnv.sysname)
        print(m.hostid())

    def checks(self):
        if self.skip_action("checks"):
            return
        self.task_checks()

    @scheduler_fork
    def task_checks(self):
        import checks
        if self.svcs is None:
            self.build_services()
        c = checks.checks(self.svcs)
        c.node = self
        c.do_checks()

    def wol(self):
        import rcWakeOnLan
        if self.options.mac is None:
            print("missing parameter. set --mac argument. multiple mac addresses must be separated by comma", file=sys.stderr)
            print("example 1 : --mac 00:11:22:33:44:55", file=sys.stderr)
            print("example 2 : --mac 00:11:22:33:44:55,66:77:88:99:AA:BB", file=sys.stderr)
            return 1
        if self.options.broadcast is None:
            print("missing parameter. set --broadcast argument. needed to identify accurate network to use", file=sys.stderr)
            print("example 1 : --broadcast 10.25.107.255", file=sys.stderr)
            print("example 2 : --broadcast 192.168.1.5,10.25.107.255", file=sys.stderr)
            return 1
        macs = self.options.mac.split(',')
        broadcasts = self.options.broadcast.split(',')
        for brdcast in broadcasts:
            for mac in macs:
                req = rcWakeOnLan.wolrequest(macaddress=mac, broadcast=brdcast)
                if not req.check_broadcast():
                    print("Error : skipping broadcast address <%s>, not in the expected format 123.123.123.123"%req.broadcast, file=sys.stderr)
                    break
                if not req.check_mac():
                    print("Error : skipping mac address <%s>, not in the expected format 00:11:22:33:44:55"%req.mac, file=sys.stderr)
                    continue
                if req.send():
                    print("Sent Wake On Lan packet to mac address <%s>"%req.mac)
                else:
                    print("Error while trying to send Wake On Lan packet to mac address <%s>"%req.mac, file=sys.stderr)

    def unset(self):
        if self.options.param is None:
            print("no parameter. set --param", file=sys.stderr)
            return 1
        l = self.options.param.split('.')
        if len(l) != 2:
            print("malformed parameter. format as 'section.key'", file=sys.stderr)
            return 1
        section, option = l
        if not self.config.has_section(section):
            print("section '%s' not found"%section, file=sys.stderr)
            return 1
        if not self.config.has_option(section, option):
            print("option '%s' not found in section '%s'"%(option, section), file=sys.stderr)
            return 1
        try:
            self.config.remove_option(section, option)
            self.write_config()
        except:
            return 1
        return 0

    def get(self):
        if self.options.param is None:
            print("no parameter. set --param", file=sys.stderr)
            return 1
        l = self.options.param.split('.')
        if len(l) != 2:
            print("malformed parameter. format as 'section.key'", file=sys.stderr)
            return 1
        section, option = l
        if not self.config.has_section(section):
            print("section '%s' not found"%section, file=sys.stderr)
            return 1
        if not self.config.has_option(section, option):
            print("option '%s' not found in section '%s'"%(option, section), file=sys.stderr)
            return 1
        print(self.config.get(section, option))
        return 0

    def set(self):
        if self.options.param is None:
            print("no parameter. set --param", file=sys.stderr)
            return 1
        if self.options.value is None:
            print("no value. set --value", file=sys.stderr)
            return 1
        l = self.options.param.split('.')
        if len(l) != 2:
            print("malformed parameter. format as 'section.key'", file=sys.stderr)
            return 1
        section, option = l
        if not self.config.has_section(section):
            try:
                self.config.add_section(section)
            except ValueError as e:
                print(e)
                return 1
        if self.config.has_option(section, option) and \
           self.config.get(section, option) == self.options.value:
            return
        self.config.set(section, option, self.options.value)
        try:
            self.write_config()
        except:
            return 1
        return 0

    def register(self):
        u = self.collector.call('register_node')
        if u is None:
            print("failed to obtain a registration number", file=sys.stderr)
            return 1
        elif isinstance(u, list):
            print(u[0], file=sys.stderr)
            return 1
        try:
            if not self.config.has_section('node'):
                self.config.add_section('node')
            self.config.set('node', 'uuid', u)
            self.write_config()
        except:
            print("failed to write registration number: %s"%u, file=sys.stderr)
            return 1
        print("registered")
        return 0

    def service_action_worker(self, s, **kwargs):
        r = s.action(**kwargs)
        self.close()
        sys.exit(r)

    def devlist(self):
        try:
            m = __import__("rcDevTree"+rcEnv.sysname)
        except ImportError:
            return
        tree = m.DevTree()
        tree.load()
        l = []
        for dev in tree.get_top_devs():
            if len(dev.devpath) > 0:
                l.append(dev.devpath[0])
        return l

    def updatecomp(self):
        if self.config.has_option('node', 'repocomp'):
            pkg_name = self.config.get('node', 'repocomp').strip('/') + "/current"
        elif self.config.has_option('node', 'repo'):
            pkg_name = self.config.get('node', 'repo').strip('/') + "/compliance/current"
        else:
            if self.options.cron:
                return 0
            print("node.repo or node.repocomp must be set in node.conf", file=sys.stderr)
            return 1
        import tempfile
        f = tempfile.NamedTemporaryFile()
        tmpf = f.name
        f.close()
        print("get %s (%s)"%(pkg_name, tmpf))
        import urllib
        kwargs = {}
        try:
            import ssl
            context = ssl._create_unverified_context()
            kwargs['context'] = context
        except:
            pass
        try:
            fname, headers = urllib.urlretrieve(pkg_name, tmpf, **kwargs)
        except IOError as e:
            print("download failed", ":", e[1], file=sys.stderr)
            try:
                os.unlink(fname)
            except:
                pass
            if self.options.cron:
                return 0
            return 1
        if 'invalid file' in headers.values():
            try:
                os.unlink(fname)
            except:
                pass
            if self.options.cron:
                return 0
            print("invalid file", file=sys.stderr)
            return 1
        with open(fname, 'r') as f:
            content = f.read()
        if content.startswith('<') and '404 Not Found' in content:
            try:
                os.unlink(fname)
            except:
                pass
            if self.options.cron:
                return 0
            print("not found", file=sys.stderr)
            return 1
        tmpp = os.path.join(rcEnv.pathtmp, 'compliance')
        backp = os.path.join(rcEnv.pathtmp, 'compliance.bck')
        compp = os.path.join(rcEnv.pathvar, 'compliance')
        import shutil
        try:
            shutil.rmtree(backp)
        except:
            pass
        print("extract compliance in", rcEnv.pathtmp)
        import tarfile
        tar = tarfile.open(f.name)
        os.chdir(rcEnv.pathtmp)
        try:
            tar.extractall()
            tar.close()
        except:
            try:
                os.unlink(fname)
            except:
                pass
            print("failed to unpack", file=sys.stderr)
            return 1
        try:
            os.unlink(fname)
        except:
            pass
        print("install new compliance")
        shutil.move(compp, backp)
        shutil.move(tmpp, compp)

    def updatepkg(self):
        if not os.path.exists(os.path.join(rcEnv.pathlib, 'rcUpdatePkg'+rcEnv.sysname+'.py')):
            print("updatepkg not implemented on", rcEnv.sysname, file=sys.stderr)
            return 1
        m = __import__('rcUpdatePkg'+rcEnv.sysname)
        if self.config.has_option('node', 'repopkg'):
            pkg_name = self.config.get('node', 'repopkg').strip('/') + "/" + m.repo_subdir + '/current'
        elif self.config.has_option('node', 'repo'):
            pkg_name = self.config.get('node', 'repo').strip('/') + "/packages/" + m.repo_subdir + '/current'
        else:
            print("node.repo or node.repopkg must be set in node.conf", file=sys.stderr)
            return 1
        import tempfile
        f = tempfile.NamedTemporaryFile()
        tmpf = f.name
        f.close()
        print("get %s (%s)"%(pkg_name, tmpf))
        import urllib
        kwargs = {}
        try:
            import ssl
            context = ssl._create_unverified_context()
            kwargs['context'] = context
        except:
            pass
        try:
            fname, headers = urllib.urlretrieve(pkg_name, tmpf, **kwargs)
        except IOError as e:
            print("download failed", ":", e[1], file=sys.stderr)
            try:
                os.unlink(fname)
            except:
                pass
            return 1
        if 'invalid file' in headers.values():
            try:
                os.unlink(fname)
            except:
                pass
            print("invalid file", file=sys.stderr)
            return 1
        with open(fname, 'r') as f:
            content = f.read()
        if content.startswith('<') and '404 Not Found' in content:
            print("not found", file=sys.stderr)
            try:
                os.unlink(fname)
            except:
                pass
            return 1
        print("updating opensvc")
        m.update(tmpf)
        print("clean up")
        try:
            os.unlink(fname)
        except:
            pass
        return 0

    def provision(self):
        self.provision_resource = []
        for rs in self.options.resource:
            try:
                d = json.loads(rs)
            except:
                print("JSON read error: %s", rs, file=sys.stderr)
                return 1
            if 'rtype' not in d:
                print("'rtype' key must be set in resource dictionary: %s", rs, file=sys.stderr)
                return 1

            rtype = d['rtype']
            if len(rtype) < 2:
                print("invalid 'rtype' value: %s", rs, file=sys.stderr)
                return 1
            rtype = rtype[0].upper() + rtype[1:].lower()

            if 'type' in d:
                rtype +=  d['type'][0].upper() + d['type'][1:].lower()
            modname = 'prov' + rtype
            try:
                m = __import__(modname)
            except ImportError:
                print("provisioning is not available for resource type:", d['rtype'], "(%s)"%modname, file=sys.stderr)
                return 1
            if not hasattr(m, "d_provisioner"):
                print("provisioning with nodemgr is not available for this resource type:", d['rtype'], file=sys.stderr)
                return 1

            self.provision_resource.append((m, d))

        for o, d in self.provision_resource:
            getattr(o, "d_provisioner")(d)

    def get_ruser(self, node):
        default = "root"
        if not self.config.has_option('node', "ruser"):
            return default
        h = {}
        s = self.config.get('node', 'ruser').split()
        for e in s:
            l = e.split("@")
            if len(l) == 1:
                default = e
            elif len(l) == 2:
                _ruser, _node = l
                h[_node] = _ruser
            else:
                continue
        if node in h:
            return h[node]
        return default

    def dequeue_actions(self):
        actions = self.collector.call('collector_get_action_queue')
        if actions is None:
            return "unable to fetch actions scheduled by the collector"
        data = []
        for action in actions:
            ret, out, err = self.dequeue_action(action)
            data.append((action.get('id'), ret, out, err))
        if len(actions) > 0:
            self.collector.call('collector_update_action_queue', data)

    def dequeue_action(self, action):
        if rcEnv.sysname == "Windows":
            nodemgr = os.path.join(rcEnv.pathsvc, "nodemgr.cmd")
            svcmgr = os.path.join(rcEnv.pathsvc, "svcmgr.cmd")
        else:
            nodemgr = os.path.join(rcEnv.pathbin, "nodemgr")
            svcmgr = os.path.join(rcEnv.pathbin, "svcmgr")
        if action.get("svcname") is None:
            cmd = [nodemgr]
        else:
            cmd = [svcmgr, "-s", action.get("svcname")]
        cmd += action.get("command", "").split()
        print("dequeue action %s" % " ".join(cmd))
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        return p.returncode, out, err

    def rotate_root_pw(self):
        pw = self.genpw()

        from collector import Collector
        o = Collector(self.options, self.collector)
        try:
            data = getattr(o, 'rotate_root_pw')(pw)
        except Exception as e:
            print("unexpected error sending the new password to the collector (%s). Abording password change."%str(e), file=sys.stderr)
            return 1

        try:
            rc = __import__('rcPasswd'+rcEnv.sysname)
        except ImportError:
            print("not implemented")
            return 1
        except Exception as e:
            print(e)
            return 1
        r = rc.change_root_pw(pw)
        if r == 0:
            print("root password changed")
        else:
            print("failed to change root password")
        return r

    def genpw(self):
        import string
        chars = string.letters + string.digits + r'+/'
        assert 256 % len(chars) == 0  # non-biased later modulo
        PWD_LEN = 16
        return ''.join(chars[ord(c) % len(chars)] for c in os.urandom(PWD_LEN))

    def scanscsi(self):
        try:
            m = __import__("rcDiskInfo"+rcEnv.sysname)
        except ImportError:
            print("scanscsi is not supported on", rcEnv.sysname, file=sys.stderr)
            return 1
        o = m.diskInfo()
        if not hasattr(o, 'scanscsi'):
            print("scanscsi is not implemented on", rcEnv.sysname, file=sys.stderr)
            return 1
        return o.scanscsi()

    def discover(self):
        self.cloud_init()

    def cloud_init(self):
        r = 0
        for s in self.config.sections():
            try:
                self.cloud_init_section(s)
            except ex.excInitError as e:
                print(str(e), file=sys.stderr)
                r |= 1
        return r

    def cloud_get(self, s):
        if not s.startswith("cloud"):
            return

        if not s.startswith("cloud#"):
            raise ex.excInitError("cloud sections must have a unique name in the form '[cloud#n] in %s"%self.nodeconf)

        if hasattr(self, "clouds") and s in self.clouds:
            return self.clouds[s]

        try:
            cloud_type = self.config.get(s, 'type')
        except:
            raise ex.excInitError("type option is mandatory in cloud section in %s"%self.nodeconf)

        # noop if already loaded
        self.load_auth_config()
        try:
            auth_dict = {}
            for key, val in self.auth_config.items(s):
                auth_dict[key] = val
        except:
            raise ex.excInitError("%s must have a '%s' section"%(self.authconf, s))

        if len(cloud_type) == 0:
            raise ex.excInitError("invalid cloud type in %s"%self.nodeconf)

        mod_name = "rcCloud" + cloud_type[0].upper() + cloud_type[1:].lower()

        try:
            m = __import__(mod_name)
        except ImportError:
            raise ex.excInitError("cloud type '%s' is not supported"%cloud_type)

        if not hasattr(self, "clouds"):
            self.clouds = {}
        c = m.Cloud(s, auth_dict)
        self.clouds[s] = c
        return c

    def cloud_init_section(self, s):
        c = self.cloud_get(s)

        if c is None:
            return

        cloud_id = c.cloud_id()
        svcnames = c.list_svcnames()

        self.cloud_purge_services(cloud_id, map(lambda x: x[1], svcnames))

        for vmname, svcname in svcnames:
            self.cloud_init_service(c, vmname, svcname)

    def cloud_purge_services(self, suffix, svcnames):
        import glob
        envs = glob.glob(os.path.join(rcEnv.pathetc, '*.env'))
        for env in envs:
            svcname = os.path.basename(env).rstrip('.env')
            if svcname.endswith(suffix) and svcname not in svcnames:
                print("purge_service(svcname)", svcname)

    def cloud_init_service(self, c, vmname, svcname):
        import glob
        envs = glob.glob(os.path.join(rcEnv.pathetc, '*.env'))
        env = os.path.join(rcEnv.pathetc, svcname+'.env')
        if env in envs:
            print(svcname, "is already defined")
            return
        print("initialize", svcname)

        defaults = {
          'app': c.app_id(svcname),
          'mode': c.mode,
          'nodes': rcEnv.nodename,
          'service_type': 'TST',
          'vm_name': vmname,
          'cloud_id': c.cid,
        }
        config = ConfigParser.RawConfigParser(defaults)

        try:
            fp = open(env, 'w')
            config.write(fp)
            fp.close()
        except:
            print("failed to write %s"%env, file=sys.stderr)
            raise Exception()

        d = env.rstrip('.env')+'.dir'
        s = env.rstrip('.env')+'.d'
        x = os.path.join(rcEnv.pathbin, "svcmgr")
        b = env.rstrip('.env')
        try:
            os.makedirs(d)
        except:
            pass
        try:
            os.symlink(d, s)
        except:
            pass
        try:
            os.symlink(x, b)
        except:
            pass

    def do_svcs_action(self, action, rid, tags):
        err = 0
        if self.options.parallel:
            from multiprocessing import Process
	    if rcEnv.sysname == "Windows":
                from multiprocessing import set_executable
                set_executable(os.path.join(sys.exec_prefix, 'pythonw.exe'))
            p = {}
        for s in self.svcs:
            if self.options.parallel:
                d = {
                  'action': action,
                  'rid': rid,
                  'tags': tags,
                  'waitlock': self.options.waitlock
                }
                p[s.svcname] = Process(target=self.service_action_worker,
                                       name='worker_'+s.svcname,
                                       args=[s],
                                       kwargs=d)
                p[s.svcname].start()
            else:
                try:
                    err += s.action(action, rid=rid, tags=tags, waitlock=self.options.waitlock)
                except s.exMonitorAction:
                    s.action('toc')
                except ex.excSignal:
                    break

        if self.options.parallel:
            for svcname in p:
                p[svcname].join()
                r = p[svcname].exitcode
                if r > 0:
                   # r is negative when p[svcname] is killed by signal.
                   # in this case, we don't want to decrement the err counter.
                   err += r

        return err



if __name__ == "__main__" :
    for n in (Node,) :
        help(n)
0707010000bb85000081a400000000000000000000000154fc7bfb000016d7000000660000000300000000000000000000001f00000000root/opt/opensvc/lib/provFs.py    from provisioning import Provisioning
from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv
import os
import rcExceptions as ex

class ProvisioningFs(Provisioning):
    # required from children:
    #   mkfs = ['mkfs.ext4', '-F']
    #   info = ['tune2fs', '-l']
    def __init__(self, r):
        Provisioning.__init__(self, r)
        self.section = dict(r.svc.config.items(r.rid))
        for i in ('dev', 'mnt'):
            if i not in self.section:
                raise ex.excError("%s keyword is not set in section %s"%(i, r.rid))
            setattr(self, i, self.section[i])
        if 'size' not in self.section:
            if self.r.fsType != "zfs":
                raise ex.excError("'size' keyword is not set in section %s"%(r.rid))
        else:
            self.size = self.section['size']
            self.size_to_mb()

    def size_to_mb(self):
        if isinstance(self.size, int):
            # consider value expressed as megabytes
            return
        s = self.size.replace(' ', '')
        if len(s) < 2:
            raise ex.excError("size provisioning parameter too short")
        if s[-1].isdigit():
            # no unit, consider value expressed as megabytes
            return
        try:
            unit = s[-1].lower()
        except:
            # no unit, consider value expressed as megabytes
            return
        try:
            size = int(s[:-1])
        except:
            raise ex.excError("failed to convert provisioning size %s to integer"%size)
        if unit == 't':
            size *= 1024*1024
        elif unit == 'g':
            size *= 1024
        elif unit == 'm':
            pass
        elif unit == 'k':
            size = size // 1024
        else:
            raise ex.excError("failed to convert %s to megabytes"%self.size)
        self.size = str(size)

    def check_fs(self):
        cmd = self.info + [self.mkfs_dev]
        out, err, ret = justcall(cmd)
        if ret == 0:
            return True
        self.r.log.info("%s is not formatted"%self.mkfs_dev)
        return False

    def provision_dev_linux(self):
        if not which('vgdisplay'):
            self.r.log.error("vgdisplay command not found")
            raise ex.excError
        cmd = ['vgdisplay', self.section['vg']]
        out, err, ret = justcall(cmd)
        if ret != 0:
            self.r.log.error("volume group %s does not exist"%self.section['vg'])
            raise ex.excError
        if self.dev.startswith('/dev/mapper/'):
            dev = self.dev.replace('/dev/mapper/').replace(self.section['vg'].replace('-', '--')+'-', '')
            dev = dev.replace('--', '-')
        elif self.section['vg'] in self.dev:
            dev = os.path.basename(self.dev)
        else:
            self.r.log.error("malformat dev %s"%self.dev)
            raise ex.excError
        if which('lvcreate'):
            # create the logical volume
            cmd = ['lvcreate', '-n', dev, '-L', str(self.size)+'M', self.section['vg']]
            ret, out, err = self.r.vcall(cmd)
            if ret != 0:
                raise ex.excError
        else:
            self.r.log.error("lvcreate command not found")
            raise ex.excError

        # /dev/mapper/$vg-$lv and /dev/$vg/$lv creation is delayed ... refresh
        self.r.vcall(["dmsetup", "mknodes"])
        mapname = "%s-%s"%(self.section['vg'].replace('-','--'),
                           dev.replace('-','--'))
        self.dev = '/dev/mapper/'+mapname
        import time
        for i in range(3, 0, -1):
            if os.path.exists(self.dev):
                break
            if i != 0:
                time.sleep(1)
        if i == 0:
            self.r.log.error("timed out waiting for %s to appear"%self.dev)
            raise ex.excError
            
    def provision_dev_hpux(self):
        if not which('vgdisplay'):
            self.r.log.error("vgdisplay command not found")
            raise ex.excError
        cmd = ['vgdisplay', self.section['vg']]
        out, err, ret = justcall(cmd)
        if ret != 0:
            self.r.log.error("volume group %s does not exist"%self.section['vg'])
            raise ex.excError
        dev = os.path.basename(self.section['dev'])
        if which('lvcreate'):
            # create the logical volume
            cmd = ['lvcreate', '-n', dev, '-L', str(self.size)+'M', self.section['vg']]
            ret, out, err = self.r.vcall(cmd)
            if ret != 0:
                raise ex.excError
        else:
            self.r.log.error("lvcreate command not found")
            raise ex.excError

    def provision_dev(self):
        if 'vg' not in self.section:
            return
        if 'size' not in self.section:
            return
        vg = self.section['vg']
        if rcEnv.sysname == 'Linux':
            self.provision_dev_linux()
        elif rcEnv.sysname == 'HP-UX':
            self.provision_dev_hpux()
           
    def provisioner(self):
        if not os.path.exists(self.mnt):
            os.makedirs(self.mnt)
            self.r.log.info("%s mount point created"%self.mnt)

        if not os.path.exists(self.dev):
            self.provision_dev()

        self.mkfs_dev = self.dev
        if rcEnv.sysname == 'HP-UX':
            l = self.dev.split('/')
            l[-1] = 'r'+l[-1]
            self.mkfs_dev = '/'.join(l)
            if not os.path.exists(self.mkfs_dev):
               self.r.log.error("%s raw device does not exists"%self.mkfs_dev)
               return

        if not self.check_fs():
            cmd = self.mkfs + [self.mkfs_dev]
            (ret, out, err) = self.r.vcall(cmd)
            if ret != 0:
                self.r.log.error('Failed to format %s'%self.mkfs_dev)
                raise ex.excError

        self.r.log.info("provisioned")
        self.r.start()
        return True
 0707010000bb81000081a400000000000000000000000154fc7bfb000004c8000000660000000300000000000000000000002300000000root/opt/opensvc/lib/nodeDarwin.py    #
# Copyright (c) 2014 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2014 Cyril Galibern <cyril.galibern@opensvc.com>
# Copyright (c) 2014 Arnaud Veron <arnaud.veron@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import node

class Node(node.Node):
    def shutdown(self):
        cmd = ["shutdown", "-h", "now"]
        ret, out, err = self.vcall(cmd)

    def reboot(self):
        cmd = ["shutdown", "-r", "now"]
        ret, out, err = self.vcall(cmd)
0707010000bb82000081a400000000000000000000000154fc7bfb00000452000000660000000300000000000000000000002200000000root/opt/opensvc/lib/nodeHP-UX.py #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import node

class Node(node.Node):
    def shutdown(self):
        cmd = ["shutdown", "-h", "-y", "0"]
        ret, out, err = self.vcall(cmd)

    def reboot(self):
        cmd = ["shutdown", "-r", "-y", "0"]
        ret, out, err = self.vcall(cmd)
  0707010000bb83000081a400000000000000000000000154fc7bfb00000434000000660000000300000000000000000000002200000000root/opt/opensvc/lib/nodeLinux.py #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import node

class Node(node.Node):
    def shutdown(self):
        cmd = ["shutdown", "-h"]
        ret, out, err = self.vcall(cmd)

    def reboot(self):
        cmd = ["reboot"]
        ret, out, err = self.vcall(cmd)
0707010000bb84000081a400000000000000000000000154fc7bfb0000029d000000660000000300000000000000000000002400000000root/opt/opensvc/lib/provDiskDcs.py   import rcExceptions as ex
import rcDcs

"""
{
 u'paths': u'iqn.1994-05.com.domain:linux-iqn.2000-08.com.datacore:sds1-1,iqn.1994-05.com.domain:linux-iqn.2000-08.com.datacore:sds2-1',
 u'rtype': u'disk',
 u'array_model': u'SANsymphony-V',
 u'dg_name': u'sds1_pool1',
 u'caption': u'foo',
 u'type': u'dcs',
 u'array_name':
 u'local',
 u'size': 10
}
"""
def d_provisioner(data):
    o = rcDcs.Dcss()
    if 'array_name' not in data:
        raise ex.excError("'array_name' key is mandatory")
    dcs = o.get_dcs(data['array_name'])
    if dcs is None:
        raise ex.excError("no dcs found in domain %s to provision the disk"%data['array_name'])
    dcs.add_vdisk(data)
   0707010000bb86000081a400000000000000000000000154fc7bfb0000008f000000660000000300000000000000000000002300000000root/opt/opensvc/lib/provFsExt2.py    from provFs import ProvisioningFs

class ProvisioningFsExt2(ProvisioningFs):
    mkfs = ['mkfs.ext2', '-F', '-q']
    info = ['tune2fs', '-l']
 0707010000bb87000081a400000000000000000000000154fc7bfb0000008f000000660000000300000000000000000000002300000000root/opt/opensvc/lib/provFsExt3.py    from provFs import ProvisioningFs

class ProvisioningFsExt3(ProvisioningFs):
    mkfs = ['mkfs.ext3', '-F', '-q']
    info = ['tune2fs', '-l']
 0707010000bb88000081a400000000000000000000000154fc7bfb0000008f000000660000000300000000000000000000002300000000root/opt/opensvc/lib/provFsExt4.py    from provFs import ProvisioningFs

class ProvisioningFsExt4(ProvisioningFs):
    mkfs = ['mkfs.ext4', '-F', '-q']
    info = ['tune2fs', '-l']
 0707010000bb89000081a400000000000000000000000154fc7bfb000000a7000000660000000300000000000000000000002300000000root/opt/opensvc/lib/provFsVxfs.py    from provFs import ProvisioningFs

class ProvisioningFsVxfs(ProvisioningFs):
    mkfs = ['newfs', '-F', 'vxfs', '-o', 'largefiles', '-b', '8192']
    info = ['fstyp']
 0707010000bb8a000081a400000000000000000000000154fc7bfb00000616000000660000000300000000000000000000002200000000root/opt/opensvc/lib/provFsZfs.py #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2011 Cyril Galibern <cyril.galibern@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from provFs import ProvisioningFs
from rcUtilities import which
from rcZfs import Dataset

class ProvisioningFsZfs(ProvisioningFs):
    def provision_dev(self):
        if not which('zfs'):
            self.r.log.error("zfs command not found")
            raise ex.excError
        ds = Dataset(self.dev, log=self.r.log)
        if ds.exists() is False:
            ds.create(['-p'])

        nv_list = dict()
        #nv_list['mountpoint'] = self.mnt
        if 'size' in self.section:
            nv_list['refquota'] = self.section['size']
        ds.verify_prop(nv_list)
           
    def provisioner(self):

        self.provision_dev()

        self.r.log.info("provisioned")
        # self.r.start(), 
        return True
  0707010000bb8b000081a400000000000000000000000154fc7bfb0000007d000000660000000300000000000000000000001f00000000root/opt/opensvc/lib/provIp.py    from provisioning import Provisioning

class ProvisioningIp(Provisioning):
    def provisioner(self):
        self.r.start()
   0707010000bb8c000081a400000000000000000000000154fc7bfb000011d5000000660000000300000000000000000000002000000000root/opt/opensvc/lib/provKvm.py   from provisioning import Provisioning
from rcGlobalEnv import rcEnv
from rcUtilities import which
import os
import rcExceptions as ex

class ProvisioningKvm(Provisioning):
    def __init__(self, r):
        Provisioning.__init__(self, r)

        self.section = r.svc.config.defaults()

        if 'snapof' in self.section:
            self.snapof = self.section['snapof']
        else:
            self.snapof = None

        if 'snap' in self.section:
            self.snap = self.section['snap']
        else:
            self.snap = None

        if 'virtinst' in self.section:
            self.virtinst = self.section['virtinst']
        else:
            self.virtinst = None

    def check_kvm(self):
        if os.path.exists(self.r.cf):
            return True
        return False

    def setup_kvm(self):
        if self.virtinst is None:
            self.r.log.error("the 'virtinst' parameter must be set")
            raise ex.excError
        ret, out, err = self.r.vcall(self.virtinst.split())
        if ret != 0:
            raise ex.excError

    def setup_ips(self):
        self.purge_known_hosts()
        for rs in self.r.svc.get_res_sets("ip"):
            for r in rs.resources:
                self.purge_known_hosts(r.addr)

    def purge_known_hosts(self, ip=None):
        if ip is None:
            cmd = ['ssh-keygen', '-R', self.r.svc.svcname]
        else:
            cmd = ['ssh-keygen', '-R', ip]
        ret, out, err = self.r.vcall(cmd, err_to_info=True)

    def setup_snap(self):
        if self.snap is None:
            self.r.log.error("the 'snap' parameter must be set")
            raise ex.excError
        if self.snapof is None:
            self.r.log.error("the 'snapof' parameter must be set")
            raise ex.excError
        if not which('btrfs'):
            self.r.log.error("'btrfs' command not found")
            raise ex.excError
 
        cmd = ['btrfs', 'subvolume', 'snapshot', self.snapof, self.snap]
        ret, out, err = self.r.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def get_pubkey(self):
        p = os.path.join(os.sep, 'root', '.ssh', 'id_dsa.pub')
        try:
            with open(p) as f:
                pub = f.read(8000)
        except:
            self.r.log.error('failed to read root public key')
            raise ex.excError
        return pub

    def get_gw(self):
        cmd = ['route', '-n']
        ret, out, err = self.r.call(cmd)
        if ret != 0:
            self.r.log.error('failed to read routing table')
            raise ex.excError
        for line in out.split('\n'):
            if line.startswith('0.0.0.0'):
                l = line.split()
                if len(l) > 1:
                    return l[1]
        self.r.log.error('failed to find default gateway')
        raise ex.excError

    def get_ns(self):
        p = os.path.join(os.sep, 'etc', 'resolv.conf')
        with open(p) as f:
            for line in f.readlines():
                if 'nameserver' in line:
                    l = line.split()
                    if len(l) > 1:
                        return l[1]
        self.r.log.error('failed to find a nameserver')
        raise ex.excError

    def get_config(self):
        cf = ['todo']
        s = ';'.join(('vm', self.r.name))
        cf.append(s)
        s = 'ns;192.168.122.1'
        cf.append(s)
        s = ';'.join(('gw', self.get_gw()))
        cf.append(s)
        try:
            s = ';'.join(('hv_root_pubkey', self.get_pubkey()))
            cf.append(s)
        except ex.excError:
            pass
        for rs in self.r.svc.get_res_sets("ip"):
            for r in rs.resources:
                s = ';'.join((r.rid, r.ipDev, r.addr, r.mask))
                cf.append(s)
        cf.append('')
        return '\n'.join(cf)

    def setup_cfdisk(self):
        config = self.get_config()
        block = len(config)//512 + 1
        cfdisk = os.path.join(rcEnv.pathtmp, self.r.svc.svcname+'.cfdisk')
        try:
            with open(cfdisk, 'w') as f:
                f.write(config)
                f.seek(block*512)
                f.write('\0')
        except:
            self.r.log.error("failed to create config disk")
            raise ex.excError
        self.virtinst += " --disk path=%s,device=floppy"%cfdisk
        self.r.log.info("created config disk with content;\n%s", config)

    def provisioner(self):
        self.setup_snap()
        self.setup_cfdisk()
        self.setup_kvm()
        self.setup_ips()

        self.r.log.info("provisioned")
        return True
   0707010000bb8d000081a400000000000000000000000154fc7bfb0000036d000000660000000300000000000000000000002600000000root/opt/opensvc/lib/provLoopLinux.py from provisioning import Provisioning
import os
import rcExceptions as ex

class ProvisioningLoop(Provisioning):
    def __init__(self, r):
        Provisioning.__init__(self, r)
        self.path = r.svc.config.get(self.r.rid, 'file')
        self.size = r.svc.config.get(self.r.rid, 'size')

    def provisioner(self):
        d = os.path.dirname(self.path)
        try:
            if not os.path.exists(d):
                self.r.log.info("create directory %s"%d)
                os.makedirs(d)
            with open(self.path, 'w') as f:
                self.r.log.info("create file %s, size %sMB"%(self.path, self.size))
                f.seek(int(self.size)*1024*1024)
                f.write('\0')
        except:
            self.r.log.error("Failed to create %s"%self.path)
            raise ex.excError

        self.r.log.info("provisioned")
        self.r.start()
   0707010000bb8e000081a400000000000000000000000154fc7bfb00002047000000660000000300000000000000000000002000000000root/opt/opensvc/lib/provLxc.py   from provisioning import Provisioning
from rcGlobalEnv import rcEnv
import os
import rcExceptions as ex

class ProvisioningLxc(Provisioning):
    config_template = """\
lxc.utsname = %(vm_name)s
lxc.tty = 4
lxc.pts = 1024
lxc.console = /opt/opensvc/log/%(vm_name)s.console.log

lxc.rootfs = %(rootfs)s
lxc.cgroup.devices.deny = a
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# consoles
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
lxc.cgroup.devices.allow = c 4:0 rwm
lxc.cgroup.devices.allow = c 4:1 rwm
# /dev/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm

lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.name = eth0
lxc.network.mtu = 1500

# mounts point
lxc.mount.entry=proc %(rootfs)s/proc proc nodev,noexec,nosuid 0 0
lxc.mount.entry=devpts %(rootfs)s/dev/pts devpts defaults 0 0
lxc.mount.entry=sysfs %(rootfs)s/sys sysfs defaults 0 0
"""
    def __init__(self, r):
        Provisioning.__init__(self, r)

        self.section = r.svc.config.defaults()
        self.rootfs = r.svc.config.get(r.rid, 'rootfs')
        self.template = r.svc.config.get(r.rid, 'template')

        # hostname file in the container rootfs
        self.p_hostname = os.path.join(self.rootfs, 'etc', 'hostname')

        self.vm_name = r.name

        # lxc root conf dir
        self.d_lxc = os.path.join(os.sep, 'var', 'lib', 'lxc')
        if not os.path.exists(self.d_lxc):
            self.d_lxc = os.path.join(os.sep, 'usr', 'local', 'var', 'lib', 'lxc')
        if not os.path.exists(self.d_lxc):
            self.d_lxc = None

        # container config file
        if self.d_lxc is not None:
            self.config = os.path.join(self.d_lxc, self.vm_name, 'config')

        # network config candidates
        self.interfaces = os.path.join(self.rootfs, 'etc', 'network', 'interfaces')
        self.network = os.path.join(self.rootfs, 'etc', 'sysconfig', 'network-scripts')

    def validate(self):
        if self.d_lxc is None:
            self.r.log.error("this node is not lxc capable")
            return True

        if not self.check_vm_name():
            return False

        if not self.check_lxc():
            self.r.log.error("container is not created")
            return False

        return True

    def check_lxc(self):
        if os.path.exists(self.r.cf):
            return True
        return False

    def setup_lxc_config(self):
        import tempfile
        f = tempfile.NamedTemporaryFile(delete=False)
        f.write(self.config_template%dict(vm_name=self.vm_name, rootfs=self.rootfs))
        self.config = f.name
        f.close()

    def setup_lxc(self):
        if self.check_lxc():
            self.r.log.info("container is already created")
            return
        name = self.setup_lxc_config()
        with open("/opt/opensvc/log/%s.console.log"%self.vm_name, "a+") as f:
            f.write("")
        cmd = ['lxc-create', '-n', self.vm_name, '-f', self.config]
        (ret, out, err) = self.r.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def check_vm_name(self):
        if not os.path.exists(self.p_hostname):
            return False

        try:
            with open(self.p_hostname) as f:
                h = f.read().strip()
        except:
            self.r.log.error("can not get container hostname")
            raise ex.excError
    
        if h != self.vm_name:
            self.r.log.info("container hostname is not %s"%self.vm_name)
            return False

        return True

    def set_vm_name(self):
        if self.check_vm_name():
            self.r.log.info("container hostname already set")
            return
        with open(self.p_hostname, 'w') as f:
            f.write(self.vm_name+'\n')
        self.r.log.info("container hostname set to %s"%self.vm_name)

    def get_template(self):
        self.template_fname = os.path.basename(self.template)
        self.template_local = os.path.join(rcEnv.pathtmp, self.template_fname)
        if os.path.exists(self.template_local):
            self.r.log.info("template %s already downloaded"%self.template_fname)
            return
        import urllib
        kwargs = {}
        try:
            import ssl
            context = ssl._create_unverified_context()
            kwargs['context'] = context
        except:
            pass
        fname, headers = urllib.urlretrieve(template, self.template_local, **kwargs)
        if 'invalid file' in headers.values():
            self.r.log.error("%s not found"%self.template)
            raise ex.excError

    def unpack_template(self):
        import tarfile
        os.chdir(self.rootfs)
        tar = tarfile.open(name=self.template_local, errorlevel=0)
        if os.path.exists(os.path.join(self.rootfs,'etc')):
            self.r.log.info("template already unpacked")
            return
        tar.extractall()
        tar.close()

    def setup_template(self):
        self.set_vm_name()

    def disable_udev(self):
        updaterc = os.path.join(self.rootfs, 'usr', 'sbin', 'update-rc.d')
        chkconfig = os.path.join(self.rootfs, 'sbin', 'chkconfig')
        if os.path.exists(updaterc):
            self.r.vcall(['chroot', self.rootfs, 'update-rc.d', '-f', 'udev', 'remove'])
        elif os.path.exists(chkconfig):
            self.r.vcall(['chroot', self.rootfs, 'chkconfig', 'udev', 'off'])

    def setup_getty(self):
         getty = os.path.join(self.rootfs, 'sbin', 'getty')
         if os.path.exists(getty):
             self.r.log.info("setup getty")
             inittab = os.path.join(self.rootfs, 'etc', 'inittab')
             with open(inittab, 'a') as f:
                 f.write("""
1:2345:respawn:/sbin/getty 38400 console
c1:12345:respawn:/sbin/getty 38400 tty1 linux
""")

    def setup_authkeys(self):
        pub = os.path.join(os.sep, 'root', '.ssh', 'id_dsa.pub')
        authkeys = os.path.join(self.rootfs, 'root', '.ssh', 'authorized_keys')
        if not os.path.exists(pub):
            self.r.log.error("no dsa found on node for root")
            return
        import shutil
        if not os.path.exists(os.path.dirname(authkeys)):
            os.makedirs(os.path.dirname(authkeys))
        shutil.copyfile(pub, authkeys)
        os.chmod(authkeys, int('600', 8))
        self.r.log.info("setup hypervisor root trust")

    def setup_ip(self, r):
        self.purge_known_hosts(r.addr)
        if os.path.exists(self.interfaces):
            return self.setup_ip_debian(r)
        elif os.path.exists(self.network):
            return self.setup_ip_rh(r)

    def setup_ip_rh(self, r):
        r.getaddr()
        buff = """
DEVICE=%(ipdev)s
IPADDR=%(ipaddr)s
NETMASK=%(netmask)s
ONBOOT=yes
"""%dict(ipdev=r.ipDev, netmask=r.mask, ipaddr=r.addr)
        intf = os.path.join(self.network, 'ifcfg-'+r.ipDev)
        with open(intf, 'w') as f:
            f.write(buff)

    def setup_ip_debian(self, r):
        r.getaddr()
        buff = """
auto lo
iface lo inet loopback

auto %(ipdev)s
iface %(ipdev)s inet static
    address %(ipaddr)s
    netmask %(netmask)s

"""%dict(ipdev=r.ipDev, netmask=r.mask, ipaddr=r.addr)
        with open(self.interfaces, 'w') as f:
            f.write(buff)

    def setup_ips(self):
        self.purge_known_hosts()
        for rs in self.r.svc.get_res_sets("ip"):
            for r in rs.resources:
                self.setup_ip(r)

    def purge_known_hosts(self, ip=None):
        if ip is None:
            cmd = ['ssh-keygen', '-R', self.r.svc.svcname]
        else:
            cmd = ['ssh-keygen', '-R', ip]
        ret, out, err = self.r.vcall(cmd, err_to_info=True)

    def provisioner(self):
        path = self.rootfs

        if not os.path.exists(path):
            os.makedirs(path)

        self.get_template()
        self.unpack_template()
        self.setup_template()
        self.setup_lxc()
        self.disable_udev()
        self.setup_getty()
        self.setup_authkeys()
        self.setup_ips()

        self.r.start()
        self.r.log.info("provisioned")
        return True
 0707010000bb8f000081a400000000000000000000000154fc7bfb00000b9c000000660000000300000000000000000000002000000000root/opt/opensvc/lib/provSrp.py   from provisioning import Provisioning
from rcGlobalEnv import rcEnv
import os
import socket
import rcExceptions as ex

class ProvisioningSrp(Provisioning):
    def __init__(self, r):
        Provisioning.__init__(self, r)

        self.name = r.name
        self.rootpath = os.path.join(os.sep, 'var', 'hpsrp', self.name)
        try:
            self.prm_cores = r.svc.config.get(r.rid, 'prm_cores')
        except:
            self.prm_cores = "1"
        self.ip = r.svc.config.get(r.rid, 'ip')
        self.ip = self.lookup(self.ip)
        self.need_start = []

    def lookup(self, ip):
        try:
            int(ip[0])
            # already in cidr form
            return
        except:
            pass

        try:
            a = socket.getaddrinfo(ip, None)
            if len(a) == 0:
                raise Exception
            ip = a[0][-1][0]
            return ip
        except:
            raise ex.excError("could not resolve %s to an ip address"%self.ip)

    def validate(self):
        # False triggers provisioner, True skip provisioner
        if not which('srp'):
            self.r.log.error("this node is not srp capable")
            return True

        if self.check_srp():
            self.r.log.error("container is already created")
            return True

        return False

    def check_srp(self):
        try:
            self.r.get_status()
        except:
            return False
        return True

    def cleanup(self):
        rs = self.r.svc.get_resources('fs')
        rs.sort(lambda x, y: cmp(x.mountPoint, y.mountPoint), reverse=True)
        for r in rs:
            if r.mountPoint == self.rootpath:
                continue
            if not r.mountPoint.startswith(self.rootpath):
                continue
            r.stop()
            self.need_start.append(r)
            os.unlink(r.mountPoint)
            p = r.mountPoint
            while True:
                p = os.path.realpath(os.path.join(p, '..'))
                if p == self.rootpath:
                    break
                try:
                    self.r.log.info("unlink %s"%p)
                    os.unlink(p)
                except:
                    break

    def restart_fs(self):
        for r in self.need_start:
            r.start()

    def add_srp(self):
        self.cleanup()
        cmd = ['srp', '-batch',
               '-a', self.name,
               '-t', 'system',
               '-s', 'admin,cmpt,init,prm,network',
               'ip_address='+self.ip, 'assign_ip=no',
               'autostart=no',
               'delete_files_ok=no',
               'root_password=""',
               'prm_group_type=PSET',
               'prm_cores='+str(self.prm_cores)]
        ret, out, err = self.r.vcall(cmd)
        if ret != 0:
            raise ex.excError()
        self.restart_fs()

    def provisioner(self):
        self.add_srp()
        self.r.start()
        self.r.log.info("provisioned")
        return True
0707010000bb90000081a400000000000000000000000154fc7bfb000006e6000000660000000300000000000000000000002400000000root/opt/opensvc/lib/provVgHP-UX.py   from provisioning import Provisioning
import os
import json
import rcExceptions as ex
from stat import *
from rcUtilities import justcall
import glob

class ProvisioningVg(Provisioning):
    def __init__(self, r):
        Provisioning.__init__(self, r)
        self.pvs = r.svc.config.get(self.r.rid, 'pvs')
        self.pvs = self.pvs.split()
        try:
            self.options = r.svc.config.get(self.r.rid, 'options').split()
        except:
            self.options = []
        l = []
        for pv in self.pvs:
            l += glob.glob(pv)
        self.pvs = l

    def provisioner(self):
        if self.r.has_it():
            self.r.log.info("already provisioned")
            return

        err = False
        for i, pv in enumerate(self.pvs):
            if not os.path.exists(pv):
                self.r.log.error("pv %s does not exist"%pv)
                err |= True
            mode = os.stat(pv)[ST_MODE]
            if S_ISBLK(mode):
                continue
            else:
                self.r.log.error("pv %s is not a block device nor a loop file"%pv)
                err |= True
        if err:
            raise ex.excError

        for pv in self.pvs:
            pv = pv.replace('/disk/', '/rdisk/')
            cmd = ['pvcreate', '-f', pv]
            ret, out, err = self.r.vcall(cmd)
            if ret != 0:
                raise ex.excError

        pvs = []
        for pv in self.pvs:
            pvs.append(pv.replace('/rdisk/', '/disk/'))
        cmd = ['vgcreate']
        if len(self.options) > 0:
            cmd += self.options
        cmd += [self.r.name] + pvs
        ret, out, err = self.r.vcall(cmd)
        if ret != 0:
            raise ex.excError

        self.r.log.info("provisioned")
        return True
  0707010000bb91000081a400000000000000000000000154fc7bfb000006f3000000660000000300000000000000000000002400000000root/opt/opensvc/lib/provVgLinux.py   from provisioning import Provisioning
import os
import json
import rcExceptions as ex
from stat import *
from rcUtilities import justcall
import glob

class ProvisioningVg(Provisioning):
    def __init__(self, r):
        Provisioning.__init__(self, r)
        self.pvs = r.svc.config.get(self.r.rid, 'pvs')
        self.pvs = self.pvs.split()
        l = []
        for pv in self.pvs:
            l += glob.glob(pv)
        self.pvs = l

    def provisioner(self):
        if self.r.has_it():
            self.r.log.info("already provisioned")
            return

        err = False
        for i, pv in enumerate(self.pvs):
            if not os.path.exists(pv):
                self.r.log.error("pv %s does not exist"%pv)
                err |= True
            mode = os.stat(pv)[ST_MODE]
            if S_ISBLK(mode):
                continue
            elif S_ISREG(mode):
                cmd = ['losetup', '-j', pv]
                out, err, ret = justcall(cmd)
                if ret != 0 or not out.startswith('/dev/loop'):
                    self.r.log.error("pv %s a regular file but not a loop"%pv)
                    err |= True
                    continue
                self.pvs[i] = out.split(':')[0]
            else:
                self.r.log.error("pv %s is not a block device nor a loop file"%pv)
                err |= True
        if err:
            raise ex.excError

        for pv in self.pvs:
            cmd = ['pvcreate', '-f', pv]
            ret, out, err = self.r.vcall(cmd)
            if ret != 0:
                raise ex.excError

        cmd = ['vgcreate', self.r.name] + self.pvs
        ret, out, err = self.r.vcall(cmd)
        if ret != 0:
            raise ex.excError

        self.r.log.info("provisioned")
        return True
 0707010000bb92000081a400000000000000000000000154fc7bfb0000041e000000660000000300000000000000000000002900000000root/opt/opensvc/lib/provVgRadosLinux.py  from provisioning import Provisioning
import rcExceptions as ex

class ProvisioningVg(Provisioning):
    def __init__(self, r):
        Provisioning.__init__(self, r)

    def provisioner(self):
        for image in self.r.images:
            self.provisioner_one(image)
        self.r.log.info("provisioned")
        self.r.start()
        return True

    def provisioner_one(self, image):
        if self.r.exists(image):
            self.r.log.info("%s already provisioned"%image)
            return
        try:
            size = self.r.svc.config.get(self.r.rid, 'size')
        except:
            raise ex.excError("'size' provisioning parameter not set")
        try:
            image_format = self.r.svc.config.get(self.r.rid, 'image_format')
        except:
            image_format = None

        cmd = self.r.rbd_rcmd() + ['create', '--size', str(size), image]
        if image_format:
            cmd += ["--image-format", str(image_format)]
        ret, out, err = self.r.vcall(cmd)
        if ret != 0:
            raise ex.excError


  0707010000bb93000081a400000000000000000000000154fc7bfb00003cd0000000660000000300000000000000000000002100000000root/opt/opensvc/lib/provZone.py  #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2011 Cyril Galibern <cyril.galibern@opensvc.com>
# Copyright (c) 2014 Arnaud Veron <arnaud.veron@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os
import resContainerZone
import lock
from provisioning import Provisioning
from rcZfs import Dataset
import rcZone
from rcExceptions import excError
from rcGlobalEnv import rcEnv
from rcUtilitiesSunOS import get_solaris_version
from rcUtilities import justcall
import shutil


SYSIDCFG="/etc/sysidcfg"

class ProvisioningZone(Provisioning):
    def __init__(self, r):
        """
        """
        Provisioning.__init__(self, r)
        self.log = self.r.log
        self.section = r.svc.config.defaults()

        if 'container_origin' in self.section:
            self.container_origin = self.section['container_origin']
        else:
            self.container_origin = "skelzone"

        if 'snapof' in self.section:
            self.snapof = self.section['snapof']
        else:
            self.snapof = None

        if 'snap' in self.section:
            self.clone = self.section['snap']
        else:
            self.clone = "rpool/zones/" + r.name

        if 'zonepath' in self.section:
            self.zonepath = self.section['zonepath']
        else:
            self.zonepath = None

    def sysid_network(self):
        """
         network_interface=l226z1 {primary
          hostname=zone1-32
          ip_address=172.30.5.232
          netmask=255.255.255.0
          protocol_ipv6=no
          default_route=172.30.5.1}
        """
        envfile = os.path.join(rcEnv.pathetc, self.r.svc.svcname+'.env')
        s = ""

        for rs in self.r.svc.get_res_sets(["ip"]):
            for r in [_r for _r in rs.resources]:
                # Add mandatory tags for sol11 zones
                r.tags.add("noaction")
                r.tags.add("noalias")
                r.tags.add("exclusive")
                r.tags.remove("preboot")
                r.tags.remove("postboot")

                # Add nonrouted tag if no gateway provisioning keyword is passed
                if not r.svc.config.has_option(r.rid, "gateway"):
                    r.tags.add("nonrouted")

                if not r.svc.config.has_option(r.rid, "gateway"):
                    continue
                default_route = r.svc.config.get(r.rid, "gateway")

                if not r.svc.config.has_option(r.rid, "netmask"):
                    continue
                netmask = r.svc.config.get(r.rid, "netmask")

                if s == "":
                    s += "network_interface=%s {primary\n"%r.ipDev
                    s += " hostname=%s\n"%r.ipName
                    s += " ip_address=%s\n"%r.addr
                    s += " netmask=%s\n"%netmask
                    s += " protocol_ipv6=no\n"
                    s += " default_route=%s}\n"%default_route

        # save new service env file
        self.r.svc.config.set(r.rid, "tags", ' '.join(r.tags))
        with open(envfile, 'w') as f:
            self.r.svc.config.write(f)
            
        return s

    def get_tz(self):
        if "TZ" not in os.environ:
            return "MET"
        tz = os.environ["TZ"]
        tzp = os.path.join(os.sep, "etc", tz)
        if os.path.exists(tzp) and self.osver >= 11:
            p = os.path.realpath(tzp)
            l = p.split('zoneinfo/')
            if len(l) != 2:
                return "MET"
            return l[-1]
        else:
            return tz

    def get_ns(self):
        "return (domain, nameservers) detected from resolv.conf"
        p = os.path.join(os.sep, 'etc', 'resolv.conf')
        domain = None
        search = []
        nameservers = []
        with open(p) as f:
            for line in f.readlines():
                if line.strip().startswith('search'):
                    l = line.split()
                    if len(l) > 1:
                        search = l[1:]
                if line.strip().startswith('domain'):
                    l = line.split()
                    if len(l) > 1:
                        domain = l[1]
                if line.strip().startswith('nameserver'):
                    l = line.split()
                    if len(l) > 1 and l[1] not in nameservers:
                        nameservers.append(l[1])
        return (domain, nameservers, search)

    def create_sysidcfg(self, zone=None):
        self.r.log.info("creating zone sysidcfg file")
        if self.osver >= 11.0:
            self._create_sysidcfg_11(zone)
        else:
            self._create_sysidcfg_10(zone)

    def _create_sysidcfg_11(self, zone=None):
        try:
            domain, nameservers, search = self.get_ns()
            if domain is None and len(search) > 0:
                domain = search[0]
            if domain is None or len(nameservers) == 0:
                name_service="name_service=none"
            else:
                name_service = "name_service=DNS {domain_name=%s name_server=%s search=%s}\n" % (
                  domain,
                  ",".join(nameservers),
                  ",".join(search)
                )

            sysidcfg_dir = os.path.join(rcEnv.pathvar, self.r.svc.svcname)
            sysidcfg_filename = os.path.join(sysidcfg_dir, 'sysidcfg')
            contents = ""
            contents += "keyboard=US-English\n"
            contents += "system_locale=C\n"
            contents += "timezone=%s\n"%self.get_tz()
            contents += "terminal=vt100\n"
            contents += "timeserver=localhost\n"
            contents += self.sysid_network()
            contents += "root_password=NP\n"
            contents += "security_policy=NONE\n"
            contents += name_service

            try:
                os.makedirs(sysidcfg_dir)
            except:
                pass
            with open(sysidcfg_filename, "w") as sysidcfg_file:
                sysidcfg_file.write(contents)
            os.chdir(sysidcfg_dir)
            self.zonecfg_xml = os.path.join(sysidcfg_dir, "sc_profile.xml")
            try:
                os.unlink(self.zonecfg_xml)
            except:
                pass
            cmd = ['/usr/sbin/js2ai', '-s']
            out, err, ret = justcall(cmd)
            if not os.path.exists(self.zonecfg_xml):
                raise excError("js2ai conversion error")
        except Exception,e:
            raise excError("exception from %s: %s during create_sysidcfg file" % (e.__class__.__name__, e.__str__()))

    def _create_sysidcfg_10(self, zone=None):
        try:
            name_service = "name_service=NONE\n"

            sysidcfg_filename = zone.zonepath + "/root" + SYSIDCFG
            sysidcfg_file = open(sysidcfg_filename, "w" )
            contents = ""
            contents += "system_locale=C\n"
            contents += "timezone=MET\n"
            contents += "terminal=vt100\n"
            contents += "timeserver=localhost\n"
            contents += "security_policy=NONE\n"
            contents += "root_password=NP\n"
            contents += "nfs4_domain=dynamic\n"
            contents += "network_interface=NONE {hostname=%(zonename)s}\n" % {"zonename":zone.name}
            contents += name_service

            sysidcfg_file.write(contents)
            sysidcfg_file.close()
        except Exception,e:
            raise(excError("exception from %s: %s during create_sysidcfg file" % (e.__class__.__name__, e.__str__())))

    def test_net_interface(self, intf):
        cmd = ['dladm', 'show-link', intf]
        out, err, ret = justcall(cmd)
        if ret == 0:
            return True
        return False

    def zone_configure_net(self, zone=None):
        if zone is None:
            zone = self.r
        cmds = []
        for rs in self.r.svc.get_res_sets(["ip"]):
            for r in [_r for _r in rs.resources]:
                if not self.test_net_interface(r.ipDev):
                    raise excError("Missing interface: %s"%r.ipDev)
                cmds.append("add net ; set physical=%s ; end"%r.ipDev)
        for cmd in cmds:
            zone.zonecfg([cmd])

    def zone_configure(self, zone=None):
        """
            configure zone, if zone is None, configure self.r
        """
        if zone is None:
            zone = self.r

        if self.osver >= 11.0:
            cmd = "create -t " + self.container_origin + "; set zonepath=" + zone.zonepath
        else:
            cmd = "create; set zonepath=" + zone.zonepath

        if zone.state is None:
            zone.zonecfg([cmd])
            if zone.state != "configured":
                raise(excError("zone %s is not configured" % (zone.name)))

        if self.osver >= 11.0:
            try:
                self.zone_configure_net(zone)
            except:
                zone.zonecfg(["delete", "-F"])
                raise

    def create_zone2clone(self):
        if os.path.exists(self.r.zonepath):
            try:
                os.chmod(self.r.zonepath, 0700)
            except:
                pass
        if self.osver >= 11.0:
            self._create_zone2clone_11()
        else:
            self._create_zone2clone_10()

    def _create_zone2clone_11(self):
        zonename = self.container_origin
        zone2clone = resContainerZone.Zone(rid="container#skelzone", name=zonename)
        zone2clone.log = self.r.log
        if zone2clone.state == "installed":
            return
        self.zone_configure(zone=zone2clone)
        if zone2clone.state != "configured":
            raise(excError("zone %s is not configured" % (zonename)))
        self.create_sysidcfg(zone2clone)
        #zone2clone.zoneadm("clone", ['-c', self.zonecfg_xml, self.container_origin])
        zone2clone.zoneadm("install")
        if zone2clone.state != "installed":
            raise(excError("zone %s is not installed" % (zonename)))
        brand = zone2clone.brand
        if brand == "native":
            zone2clone.boot_and_wait_reboot()
        elif brand == "ipkg":
            zone2clone.boot()
        else:
            raise(excError("zone brand: %s not yet implemented" % (brand)))
        zone2clone.wait_multi_user()
        zone2clone.stop()
        zone2clone.zone_refresh()
        if zone2clone.state != "installed":
            raise(excError("zone %s is not installed" % (zonename)))

    def _create_zone2clone_10(self):
        """verify if self.container_origin zone is installed
        else configure container_origin if required
        then install container_origin if required
        """
        zonename = self.container_origin
        zone2clone = resContainerZone.Zone(rid="container#skelzone", name=zonename)
        zone2clone.log = self.r.log
        if zone2clone.state == "installed":
            return
        self.zone_configure(zone=zone2clone)
        if zone2clone.state != "configured":
            raise(excError("zone %s is not configured" % (zonename)))
        zone2clone.zoneadm("install")
        if zone2clone.state != "installed":
            raise(excError("zone %s is not installed" % (zonename)))
        self.create_sysidcfg(zone2clone)
        brand = zone2clone.brand
        if brand == "native":
            zone2clone.boot_and_wait_reboot()
        elif brand == "ipkg":
            zone2clone.boot()
        else:
            raise(excError("zone brand: %s not yet implemented" % (brand)))
        zone2clone.wait_multi_user()
        zone2clone.stop()
        zone2clone.zone_refresh()
        if zone2clone.state != "installed":
            raise(excError("zone %s is not installed" % (zonename)))

    def create_cloned_zone(self):
        zone = self.r
        if zone.state == "running":
            self.log.info("zone %s already running"%zone.name)
            return
        if zone.state == "configured":
            if self.osver >= 11.0:
                self._create_cloned_zone_11(zone)
            else:
                self._create_cloned_zone_10(zone)
        if zone.state != "installed":
            raise(excError("zone %s is not installed" % (zone.name)))

    def _create_cloned_zone_11(self, zone):
        zone.zoneadm("clone", ['-c', self.zonecfg_xml, self.container_origin])

    def _create_cloned_zone_10(self, zone):
        zone.zoneadm("clone", [self.container_origin])

    def create_zonepath(self):
        """create zonepath dataset from clone of snapshot of self.snapof
        snapshot for self.snapof will be created
        then cloned to self.clone
        """
        zonename = self.r.name
        source_ds = Dataset(self.snapof)
        if source_ds.exists(type="filesystem") is False:
            raise(excError("source dataset doesn't exist " + self.snapof))
        snapshot = source_ds.snapshot(zonename)
        snapshot.clone(self.clone, ['-o', 'mountpoint=' + self.r.zonepath])

    def provisioner(self, need_boot=True):
        """provision zone
        - configure zone
        - if snapof and zone brand is native
           then create zonepath from snapshot of snapof
           then attach zone
        - if snapof and zone brand is ipkg
           then try to detect zone associated with snapof
           then define container_origin
        - if container_origin
           then clone  container_origin
        - create sysidcfg
        - if need_boot boot and wait multiuser
        """
        self.osver = get_solaris_version()
        self.zone_configure()

        if self.osver >= 11:
            self.create_sysidcfg(self.r)
        else:
            if self.snapof is not None and self.r.brand == 'native':
                self.create_zonepath()
                self.r.zoneadm("attach", ["-F"])
            elif self.snapof is not None and self.r.brand == 'ipkg':
                zones = rcZone.Zones()
                src_dataset = Dataset(self.snapof)
                zonepath = src_dataset.getprop('mountpoint')
                self.container_origin = zones.zonename_from_zonepath(zonepath).zonename
                self.log.info("source zone is %s (detected from snapof %s)" % (self.container_origin, self.snapof))

        if self.container_origin is not None:
            lockname='create_zone2clone-' + self.container_origin
            lockfile = os.path.join(rcEnv.pathlock, lockname)
            self.log.info("wait get lock %s"%(lockname))
            try:
                lockfd = lock.lock(timeout=1200, delay=5, lockfile=lockfile)
            except:
                raise(excError("failure in get lock %s"%(lockname)))
            try:
                self.create_zone2clone()
            except:
                lock.unlock(lockfd)
                raise
            lock.unlock(lockfd)
            self.create_cloned_zone()

        if self.osver < 11:
            self.create_sysidcfg(self.r)

        if need_boot is True:
            self.r.boot()
            self.r.wait_multi_user()

        self.r.log.info("provisioned")
        return True
0707010000bb94000081a400000000000000000000000154fc7bfb000000a8000000660000000300000000000000000000002500000000root/opt/opensvc/lib/provisioning.py  class Provisioning(object):
    def __init__(self, r):
        self.r = r

    def validate(self):
        return True

    def provisioner(self):
        return True

0707010000bb95000081a400000000000000000000000154fc7bfb00000395000000660000000300000000000000000000002500000000root/opt/opensvc/lib/rcAddService.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import logging

def addservice():
    log = logging.getLogger('CREATE')
    log.info('enter')

   0707010000bb96000081a400000000000000000000000154fc7bfb00001902000000660000000300000000000000000000002000000000root/opt/opensvc/lib/rcAdvfs.py   import os
import glob
from rcUtilities import call

class ExInit(Exception):
    pass

class Fset(object):
    """
dor
	Id           : 46a70bfd.000964b0.6.8001
	Files        :      158,  SLim=        0,  HLim=        0
	Blocks  (1k) : 36035624,  SLim=        0,  HLim=        0
	Quota Status : user=on  group=on  
	Object Safety: off 
	Fragging     : on 
	DMAPI        : off 
stock_systemes
	Id           : 4ad8612f.000923f8.1.8001
	Clone is     : stock_systemes@osvc_sync
	Files        :   499709,  SLim=        0,  HLim=        0
	Blocks  (1k) : 35305996,  SLim=        0,  HLim=        0
	Quota Status : user=off group=off 
	Object Safety: off 
	Fragging     : on 
	DMAPI        : off 
stock_systemes@osvc_sync
	Id           : 4ad8612f.000923f8.2.8001
	Clone of     : stock_systemes
	Revision     : 1
	Object Safety: off 
	Fragging     : on 
	DMAPI        : off 
    """
    def __init__(self, lines):
        for line in lines:
            if not line.startswith('\t'):
                self.name = line.strip()
            elif "Id" in line:
                self.fsetid = line.split(':')[-1].strip()
            elif "Clone of" in line:
                self.cloneof = line.split(':')[-1].strip()
            elif "Clone is" in line:
                self.cloneis = line.split(':')[-1].strip()
            elif "Revision" in line:
                self.revision = line.split(':')[-1].strip()
            elif "Files" in line:
                line = line[line.index(':')+1:]
                l = line.split()
                self.files_count = int(l[0].replace(',',''))
                self.files_slim = int(l[2].replace(',',''))
                self.files_hlim = int(l[4].replace(',',''))
            elif "Blocks" in line:
                line = line[line.index(':')+1:]
                l = line.split()
                self.block_count = int(l[0].replace(',',''))
                self.block_slim = int(l[2].replace(',',''))
                self.block_hlim = int(l[4].replace(',',''))

    def fsname(self):
        return "#".join((self.domain.name, self.name))

    def __str__(self):
        s = "fileset:\n"
        s += " fsetid: %s\n" % self.fsetid
        s += " name: %s\n" % self.name
        s += " fsname: %s\n" % self.fsname()
        s += " files_count: %d\n" % self.files_count
        s += " files_slim: %d\n" % self.files_slim
        s += " files_hlim: %d\n" % self.files_hlim
        s += " block_count: %d\n" % self.block_count
        s += " block_slim: %d\n" % self.block_slim
        s += " block_hlim: %d\n" % self.block_hlim
        return s

class Volume(object):
    def __init__(self, s):
        l = s.split()
        if len(l) != 8:
            raise ExInit()
        self.volid = l[0]
        self.size = int(l[1])
        self.free = int(l[2])
        self.used_pct = int(l[3].replace('%',''))
        self.cmode = l[4]
        self.rblks = int(l[5])
        self.wblks = int(l[6])
        self.name = l[7]

    def __str__(self):
        s = "volume:\n"
        s += " volid: %s\n" % self.volid
        s += " name: %s\n" % self.name
        s += " size: %s\n" % self.size
        s += " free: %s\n" % self.free
        s += " used_pct: %s\n" % self.used_pct
        s += " cmode: %s\n" % self.cmode
        s += " rblks: %s\n" % self.rblks
        s += " wblks: %s\n" % self.wblks
        return s

class Fdmn(object):
    def __init__(self, name):
        self.used_pct = 0
        self.size = 0
        self.free = 0

        cmd = ['showfdmn', name]
        out, err, ret = call(cmd)
        if ret != 0:
            raise ExInit()
        d = {}
        """
               Id	       Date Created  LogPgs  Version  Domain Name
46a70bfd.000964b0  Wed Jul 25 10:38:21 2007     512        4  dom1

  Vol    1K-Blks        Free  % Used  Cmode  Rblks  Wblks  Vol Name 
   2L   62914560    21056568     67%     on    256    256  /dev/disk/dsk13c
        """
        lines = out.split('\n')
        if len(lines) < 5:
            raise ExInit()
        header = lines[2].split()
        self.domid = header[0]
        self.name = header[-1]
        self.version = header[-2]
        self.logpgs = header[-3]
        self.vols = {}
        self.fsets = {}
        for line in lines[5:]:
            try:
                v = Volume(line)
                self += v
            except ExInit:
                pass

        cmd = ['showfsets', '-k', name]
        out, err, ret = call(cmd)
        if ret != 0:
            raise ExInit()
        lines = out.split('\n')
        n_lines = len(lines)
        if n_lines == 0:
            return
        for i, line in enumerate(lines):
            if i != 0 and not line.startswith('\t') or i == n_lines - 1:
                f = Fset(lines[h:i])
                self += f
            if not line.startswith('\t'):
                h = i

    def __iadd__(self, o):
        if type(o) == Volume:
            self.size += o.size
            self.free += o.free
            self.used_pct = int(100. * (self.size - self.free) / self.size)
            o.domain = self
            self.vols[o.name] = o
        elif type(o) == Fset:
            o.domain = self
            self.fsets[o.name] = o
        return self

    def __str__(self):
        s = "domain:\n"
        s += " domid: %s\n" % self.domid
        s += " name: %s\n" % self.name
        s += " version: %s\n" % self.version
        s += " logpgs: %s\n" % self.logpgs
        for v in self.vols.values():
            s += str(v)
        for v in self.fsets.values():
            s += str(v)
        return s

    def list_volnames(self):
        l = []
        for v in self.vols.values():
            l.append(v.name)
        return l

class Fdmns(object):
    def __init__(self):
        self.load_fdmns()

    def list_fdmns(self):
        return self.fdmns.keys()

    def load_fdmns(self):
        self.fdmns = {}
        for n in glob.glob('/etc/fdmns/*'):
            n = os.path.basename(n)
            if n.startswith('.'):
                continue
            self.fdmns[n] = {}

    def load_fdmn(self, name):
        d = Fdmn(name)
        self.fdmns[name] = d

    def get_fdmn(self, name):
        if name not in self.fdmns:
            return
        if len(self.fdmns[name]) == 0:
            self.load_fdmn(name)
        return self.fdmns[name]

if __name__ == "__main__":
    o = Fdmns()
    print(o.list_fdmns())
    d = o.get_fdmn('dom1')
    print(d)
  0707010000bb97000081a400000000000000000000000154fc7bfb000051fd000000660000000300000000000000000000002000000000root/opt/opensvc/lib/rcAsset.py   #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from rcGlobalEnv import rcEnv
import os

class Asset(object):
    s_config = "node configuration file"
    s_probe = "probe"
    s_default = "default"

    def __init__(self, node):
        self.node = node

    def get_mem_bytes(self):
        s = '0'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'mem_bytes')
            source = self.s_config
        except:
            try:
                s = self._get_mem_bytes()
                source = self.s_probe
            except:
                pass
        self.print_mem_bytes(s, source)
        return s

    def print_mem_bytes(self, s, source):
        print("mem (%s)"%source)
        print("  %s MB"%s)

    def get_mem_banks(self):
        s = '0'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'mem_banks')
            source = self.s_config
        except:
            try:
                s = self._get_mem_banks()
                source = self.s_probe
            except:
                pass
        self.print_mem_banks(s, source)
        return s

    def print_mem_banks(self, s, source):
        print("mem banks (%s)"%source)
        print("  %s"%s)

    def get_mem_slots(self):
        s = '0'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'mem_slots')
            source = self.s_config
        except:
            try:
                s = self._get_mem_slots()
                source = self.s_probe
            except:
                pass
        self.print_mem_slots(s, source)
        return s

    def print_mem_slots(self, s, source):
        print("mem slots (%s)"%source)
        print("  %s"%s)

    def get_os_vendor(self):
        s = 'Unknown'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'os_vendor')
            source = self.s_config
        except:
            try:
                s = self._get_os_vendor()
                source = self.s_probe
            except:
                pass
        self.print_os_vendor(s, source)
        return s

    def print_os_vendor(self, s, source):
        print("os vendor (%s)"%source)
        print("  %s"%s)

    def get_os_release(self):
        s = 'Unknown'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'os_release')
            source = self.s_config
        except:
            try:
                s = self._get_os_release()
                source = self.s_probe
            except:
                pass
        self.print_os_release(s, source)
        return s

    def print_os_release(self, s, source):
        print("os release (%s)"%source)
        print("  %s"%s)

    def get_os_kernel(self):
        s = 'Unknown'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'os_kernel')
            source = self.s_config
        except:
            try:
                s = self._get_os_kernel()
                source = self.s_probe
            except:
                pass
        self.print_os_kernel(s, source)
        return s

    def print_os_kernel(self, s, source):
        print("os kernel (%s)"%source)
        print("  %s"%s)

    def get_os_arch(self):
        s = 'Unknown'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'os_arch')
            source = self.s_config
        except:
            try:
                s = self._get_os_arch()
                source = self.s_probe
            except:
                pass
        self.print_os_arch(s, source)
        return s

    def print_os_arch(self, s, source):
        print("os arch (%s)"%source)
        print("  %s"%s)

    def get_cpu_freq(self):
        s = '0'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'cpu_freq')
            source = self.s_config
        except:
            try:
                s = self._get_cpu_freq()
                source = self.s_probe
            except:
                pass
        self.print_cpu_freq(s, source)
        return s

    def print_cpu_freq(self, s, source):
        print("cpu freq (%s)"%source)
        print("  %s Mhz"%s)

    def get_cpu_threads(self):
        s = '0'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'cpu_threads')
            source = self.s_config
        except:
            try:
                s = self._get_cpu_threads()
                source = self.s_probe
            except:
                pass
        self.print_cpu_threads(s, source)
        return s

    def print_cpu_threads(self, s, source):
        print("cpu threads (%s)"%source)
        print("  %s"%s)

    def get_cpu_cores(self):
        s = '0'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'cpu_cores')
            source = self.s_config
        except:
            try:
                s = self._get_cpu_cores()
                source = self.s_probe
            except:
                pass
        self.print_cpu_cores(s, source)
        return s

    def print_cpu_cores(self, s, source):
        print("cpu cores (%s)"%source)
        print("  %s"%s)

    def get_cpu_dies(self):
        s = '0'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'cpu_dies')
            source = self.s_config
        except:
            try:
                s = self._get_cpu_dies()
                source = self.s_probe
            except:
                pass
        self.print_cpu_dies(s, source)
        return s

    def print_cpu_dies(self, s, source):
        print("cpu dies (%s)"%source)
        print("  %s"%s)

    def get_cpu_model(self):
        s = 'Unknown'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'cpu_model')
            source = self.s_config
        except:
            try:
                s = self._get_cpu_model()
                source = self.s_probe
            except:
                pass
        self.print_cpu_model(s, source)
        return s

    def print_cpu_model(self, s, source):
        print("cpu model (%s)"%source)
        print("  %s"%s)

    def get_serial(self):
        s = 'Unknown'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'serial')
            source = self.s_config
        except:
            try:
                s = self._get_serial()
                source = self.s_probe
            except:
                pass
        self.print_serial(s, source)
        return s

    def print_serial(self, s, source):
        print("serial (%s)"%source)
        print("  %s"%s)


    def get_enclosure(self):
        s = 'Unknown'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'enclosure')
            source = self.s_config
        except:
            try:
                s = self._get_enclosure()
                source = self.s_probe
            except:
                pass
        self.print_enclosure(s, source)
        return s

    def print_enclosure(self, s, source):
        print("enclosure (%s)"%source)
        print("  %s"%s)

    def get_model(self):
        s = 'Unknown'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'model')
            source = self.s_config
        except:
            try:
                s = self._get_model()
                source = self.s_probe
            except:
                pass
        self.print_model(s, source)
        return s

    def print_model(self, s, source):
        print("model (%s)"%source)
        print("  %s"%s)

    def get_host_mode(self):
        s = 'TST'
        source = self.s_default
        try:
            s = self.node.config.get('node', 'host_mode')
            source = self.s_config
        except:
            pass
        self.print_host_mode(s, source)
        return s

    def print_host_mode(self, s, source):
        print("host mode (%s)"%source)
        print("  %s"%s)

    def get_sec_zone(self):
        s = None
        source = self.s_default
        try:
            s = self.node.config.get('node', 'sec_zone')
            source = self.s_config
        except:
            pass
        self.print_sec_zone(s, source)
        return s

    def print_sec_zone(self, s, source):
        if s is None:
            return
        print("security zone (%s)"%source)
        print("  %s"%s)

    def get_environnement(self):
        s = None
        source = self.s_default
        try:
            s = self.node.config.get('node', 'environment')
            source = self.s_config
        except:
            pass
        self.print_environnement(s, source)
        return s

    def print_environnement(self, s, source):
        if s is None:
            return
        print("environment (%s)"%source)
        print("  %s"%s)

    def get_version(self):
        try:
            import version
            s = version.version
        except:
            s = "0"
        self.print_version(s)
        return s

    def print_version(self, s):
        print("agent version")
        print("  %s"%s)

    def get_listener_port(self):
        s = str(rcEnv.listener_port)
        source = self.s_default
        try:
            s = str(self.node.config.getint('listener', 'port'))
            source = self.s_config
        except:
            pass
        self.print_listener_port(s, source)
        return s

    def print_listener_port(self, s, source):
        if s is None:
            return
        print("listener port (%s)"%source)
        print("  %s"%s)

    def get_team_responsible(self):
        s = None
        source = self.s_default
        try:
            s = self.node.config.get('node', 'team_responsible')
            source = self.s_config
        except:
            pass
        self.print_team_responsible(s, source)
        return s

    def print_team_responsible(self, s, source):
        if s is None:
            return
        print("team responsible (%s)"%source)
        print("  %s"%s)

    def print_generic_cf(self, s, source, title):
        if s is None:
            return
        print("%s (%s)"%(title, source))
        print("  %s"%s)

    def get_loc_country(self):
        s = None
        source = self.s_default
        try:
            s = self.node.config.get('node', 'loc_country')
            source = self.s_config
        except:
            pass
        self.print_generic_cf(s, source, "location country")
        return s

    def get_loc_city(self):
        s = None
        source = self.s_default
        try:
            s = self.node.config.get('node', 'loc_city')
            source = self.s_config
        except:
            pass
        self.print_generic_cf(s, source, "location city")
        return s

    def get_loc_addr(self):
        s = None
        source = self.s_default
        try:
            s = self.node.config.get('node', 'loc_addr')
            source = self.s_config
        except:
            pass
        self.print_generic_cf(s, source, "location city")
        return s

    def get_loc_building(self):
        s = None
        source = self.s_default
        try:
            s = self.node.config.get('node', 'loc_building')
            source = self.s_config
        except:
            pass
        self.print_generic_cf(s, source, "location building")
        return s

    def get_loc_floor(self):
        s = None
        source = self.s_default
        try:
            s = self.node.config.get('node', 'loc_floor')
            source = self.s_config
        except:
            pass
        self.print_generic_cf(s, source, "location floor")
        return s

    def get_loc_room(self):
        s = None
        source = self.s_default
        try:
            s = self.node.config.get('node', 'loc_room')
            source = self.s_config
        except:
            pass
        self.print_generic_cf(s, source, "location room")
        return s

    def get_loc_rack(self):
        s = None
        source = self.s_default
        try:
            s = self.node.config.get('node', 'loc_rack')
            source = self.s_config
        except:
            pass
        self.print_generic_cf(s, source, "location rack")
        return s

    def get_loc_zip(self):
        s = None
        source = self.s_default
        try:
            s = self.node.config.get('node', 'loc_zip')
            source = self.s_config
        except:
            pass
        self.print_generic_cf(s, source, "location zip")
        return s

    def get_team_integ(self):
        s = None
        source = self.s_default
        try:
            s = self.node.config.get('node', 'team_integ')
            source = self.s_config
        except:
            pass
        self.print_generic_cf(s, source, "team integration")
        return s

    def get_team_support(self):
        s = None
        source = self.s_default
        try:
            s = self.node.config.get('node', 'team_support')
            source = self.s_config
        except:
            pass
        self.print_generic_cf(s, source, "team support")
        return s

    def get_project(self):
        s = None
        source = self.s_default
        try:
            s = self.node.config.get('node', 'project')
            source = self.s_config
        except:
            pass
        self.print_generic_cf(s, source, "project")
        return s

    def get_hba(self):
        try:
            hba = self._get_hba()
        except:
            hba = []
        self.print_hba(hba)
        return hba

    def print_hba(self, hba):
        print("hba (probe)")
        for h in hba:
            print("  %-5s %s"%(h[1], h[0]))

    def get_targets(self):
        try:
            s = self._get_targets()
        except:
            s = []
        self.print_targets(s)
        return s

    def print_targets(self, targets):
        print("paths to targets (probe)")
        for t in targets:
            print("  %s - %s"%t)

    def get_uids(self):
        return self.get_ids("/etc/passwd")

    def get_gids(self):
        return self.get_ids("/etc/group")

    def get_ids(self, p):
        if rcEnv.sysname == "Windows":
            return []
        if not os.path.exists(p):
            return []
        with open(p, 'r') as f:
            buff = f.read()
        d = []
        for line in buff.split('\n'):
            line = line.strip()
            if line.startswith("#"):
                continue
            l = line.split(':')
            if len(l) < 3:
                continue
            try:
                i = int(l[2])
            except:
                continue
            d.append((l[0], l[2]))
        return d

    def get_lan(self):
        kwargs = {'mcast': True}
        if rcEnv.sysname == 'HP-UX':
            kwargs['hwaddr'] = True
        rcIfconfig = __import__('rcIfconfig'+rcEnv.sysname)
        ifconfig = rcIfconfig.ifconfig(**kwargs)
        lan = {}
        for intf in ifconfig.intf:
            if len(intf.hwaddr) == 0:
                continue
            if intf.hwaddr not in lan:
                lan[intf.hwaddr] = []
            if type(intf.ipaddr) == str and intf.ipaddr != '':
                d = {'type': 'ipv4',
                     'intf': intf.name,
                     'addr': intf.ipaddr,
                     'mask': intf.mask,
                    }
                lan[intf.hwaddr] += [d]
            elif type(intf.ipaddr) == list:
                for i, ip in enumerate(intf.ipaddr):
                    if ip != '':
                        d = {'type': 'ipv4',
                             'intf': intf.name,
                             'addr': ip,
                             'mask': intf.mask[i],
                            }
                    lan[intf.hwaddr] += [d]
            for i, ip6 in enumerate(intf.ip6addr):
                d = {'type': 'ipv6',
                     'intf': intf.name,
                     'addr': intf.ip6addr[i],
                     'mask': intf.ip6mask[i],
                    }
                lan[intf.hwaddr] += [d]
            if intf.name in ifconfig.mcast_data:
                for addr in ifconfig.mcast_data[intf.name]:
                    if ':' in addr:
                        addr_type = 'ipv6'
                    else:
                        addr_type = 'ipv4'
                    d = {'type': addr_type,
                         'intf': intf.name,
                         'addr': addr,
                         'mask': "",
                        }
                    lan[intf.hwaddr] += [d]

                
        self.print_lan(lan)
        return lan

    def print_lan(self, lan):
        print("lan (probe)")
        for h, l in lan.items():
            for d in l:
                if d['mask'] != "":
                    addr_mask = "%s/%s" % (d['addr'], d['mask'])
                else:
                    addr_mask = d['addr']
                print("  %s %-8s %-5s %s"%(h, d['intf'], d['type'], addr_mask))

    def get_asset_dict(self):
        d = {}
        d['nodename'] = rcEnv.nodename
        d['fqdn'] = rcEnv.fqdn
        d['version'] = self.get_version()
        d['os_name'] = rcEnv.sysname
        d['os_vendor'] = self.get_os_vendor()
        d['os_release'] = self.get_os_release()
        d['os_kernel'] = self.get_os_kernel()
        d['os_arch'] = self.get_os_arch()
        d['mem_bytes'] = self.get_mem_bytes()
        d['mem_banks'] = self.get_mem_banks()
        d['mem_slots'] = self.get_mem_slots()
        d['cpu_freq'] = self.get_cpu_freq()
        d['cpu_threads'] = self.get_cpu_threads()
        d['cpu_cores'] = self.get_cpu_cores()
        d['cpu_dies'] = self.get_cpu_dies()
        d['cpu_model'] = self.get_cpu_model()
        d['serial'] = self.get_serial()
        d['model'] = self.get_model()
        d['host_mode'] = self.get_host_mode()
        d['enclosure'] = self.get_enclosure()
        d['listener_port'] = self.get_listener_port()
        sec_zone = self.get_sec_zone()
        if sec_zone is not None:
            d['sec_zone'] = sec_zone
        environnement = self.get_environnement()
        if environnement is not None:
            d['environnement'] = environnement
        loc_country = self.get_loc_country()
        if loc_country is not None:
            d['loc_country'] = loc_country
        loc_city = self.get_loc_city()
        if loc_city is not None:
            d['loc_city'] = loc_city
        loc_building = self.get_loc_building()
        if loc_building is not None:
            d['loc_building'] = loc_building
        loc_room = self.get_loc_room()
        if loc_room is not None:
            d['loc_room'] = loc_room
        loc_rack = self.get_loc_rack()
        if loc_rack is not None:
            d['loc_rack'] = loc_rack
        loc_addr = self.get_loc_addr()
        if loc_addr is not None:
            d['loc_addr'] = loc_addr
        loc_floor = self.get_loc_floor()
        if loc_floor is not None:
            d['loc_floor'] = loc_floor
        loc_zip = self.get_loc_zip()
        if loc_zip is not None:
            d['loc_zip'] = loc_zip
        team_responsible = self.get_team_responsible()
        if team_responsible is not None:
            d['team_responsible'] = team_responsible
        team_integ = self.get_team_integ()
        if team_integ is not None:
            d['team_integ'] = team_integ
        team_support = self.get_team_support()
        if team_support is not None:
            d['team_support'] = team_support
        project = self.get_project()
        if project is not None:
            d['project'] = project
        hba = self.get_hba()
        if hba is not None:
            d['hba'] = hba
        targets = self.get_targets()
        if targets is not None:
            d['targets'] = targets
        lan = self.get_lan()
        if lan is not None:
            d['lan'] = lan
        uids = self.get_uids()
        if uids is not None:
            d['uids'] = uids
        gids = self.get_gids()
        if gids is not None:
            d['gids'] = gids
        return d
   0707010000bb98000081a400000000000000000000000154fc7bfb00000f35000000660000000300000000000000000000002300000000root/opt/opensvc/lib/rcAssetAIX.py    #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import datetime
from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv
import rcAsset

class Asset(rcAsset.Asset):
    def __init__(self, node):
        rcAsset.Asset.__init__(self, node)
        (out, err, ret) = justcall(['prtconf'])
        if ret != 0:
            self.prtconf = []
        else:
            self.prtconf = out.split('\n')
        self.lpar = self.is_lpar()

    def is_lpar(self):
        cmd = ["prtconf", "-L"]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise
        if '-1' in out:
            return False
        return True

    def _get_mem_bytes(self):
        cmd = ["prtconf", "-m"]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return '0'
        l = out.split()
        if 'Memory Size:' not in out:
            return '0'
        if len(l) != 4:
            return '0'

        size = int(l[2])
        unit = l[3]

        if unit == 'GB':
            size = size * 1024
        elif unit == 'MB':
            pass
        else:
            return '0'

        return str(size)

    def _get_mem_banks(self):
        if self.lpar:
            return '0'
        return 'TODO'

    def _get_mem_slots(self):
        if self.lpar:
            return '0'
        return 'TODO'

    def _get_os_vendor(self):
        return 'IBM'

    def _get_os_name(self):
        return 'AIX'

    def _get_os_release(self):
        cmd = ["oslevel", "-s"]
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return 'Unknown'
        return out.strip()

    def _get_os_kernel(self):
        cmd = ["oslevel", "-r"]
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return 'Unknown'
        return out.strip()

    def _get_os_arch(self):
        for line in self.prtconf:
            if "Kernel Type:" in line:
                return line.split(":")[-1].strip()
        return 'Unknown'

    def _get_cpu_freq(self):
        for line in self.prtconf:
            if "Processor Clock Speed:" in line:
                return line.split(":")[-1].split()[0].strip()
        return '0'

    def _get_cpu_cores(self):
        cmd = ["bindprocessor", "-q"]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return '0'
        l = out.split(":")
        return str(len(l[-1].strip().split()))

    def _get_cpu_dies(self):
        cmd = ["lsdev", "-Cc", "processor"]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return '0'
        return str(len([line for line in out.split('\n') if 'proc' in line]))

    def _get_cpu_model(self):
        for line in self.prtconf:
            if "Processor Type:" in line:
                return line.split(":")[-1].strip()
        return 'Unknown'

    def _get_serial(self):
        for line in self.prtconf:
            if "Machine Serial Number:" in line:
                return line.split(":")[-1].strip()
        return 'Unknown'

    def _get_model(self):
        for line in self.prtconf:
            if "System Model:" in line:
                return line.split(":")[-1].strip()
        return 'Unknown'
   0707010000bb99000081a400000000000000000000000154fc7bfb00000fa4000000660000000300000000000000000000002600000000root/opt/opensvc/lib/rcAssetDarwin.py #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2014 Arnaud Veron <arnaud.veron@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import datetime
from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv
import rcAsset

class Asset(rcAsset.Asset):
    def __init__(self, node):
        rcAsset.Asset.__init__(self, node)
        self.info = {}
        (out, err, ret) = justcall(['system_profiler', 'SPHardwareDataType', 'SPSoftwareDataType'])
        if ret == 0:
            for line in out.split('\n'):
                l = line.split(':')
                if len(l) != 2: continue
                self.info[l[0].strip()] = l[1].strip()
        self.memslots = 0
        self.membanks = 0
        self._collect_memory_info()

    def _collect_memory_info(self):
        (out, err, ret) = justcall(['system_profiler', 'SPMemoryDataType'])
        if ret == 0:
            inBlock = False
            for line in out.split('\n'):
                line = line.strip()
                if not inBlock and line.startswith("BANK"):
                    inBlock = True
                    self.memslots += 1
                if inBlock and line.startswith("Status"):
                    l = line.split(':')
                    if 'OK' in l[1].strip():
                        self.membanks += 1
                    inBlock = False

    def _get_mem_bytes(self):
        if 'Memory' not in self.info:
            return '0'
        m = self.info['Memory'].split()
        size = int(m[0])
        unit = m[1]
        if unit == 'GB':
            size = size * 1024
        elif unit == 'MB':
            pass
        else:
            raise
        return str(size)

    def _get_mem_banks(self):
        return str(self.membanks)

    def _get_mem_slots(self):
        return str(self.memslots)

    def _get_os_vendor(self):
        return 'Apple'

    def _get_os_release(self):
        if 'System Version' in self.info:
            return self.info['System Version']
        (out, err, ret) = justcall(['uname', '-r'])
        if ret != 0:
            return 'Unknown'
        return out.split()[0]

    def _get_os_kernel(self):
        if 'Kernel Version' not in self.info:
            return '0'
        return self.info['Kernel Version']

    def _get_os_arch(self):
        cmd = ['uname', '-m']
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return 'Unknown'
        return out.split('\n')[0]

    def _get_cpu_freq(self):
        if 'Processor Speed' not in self.info:
            return '0'
        return self.info['Processor Speed']

    def _get_cpu_cores(self):
        if 'Total Number of Cores' not in self.info:
            return '0'
        return self.info['Total Number of Cores']

    def _get_cpu_dies(self):
        if 'Number of Processors' not in self.info:
            return '0'
        return self.info['Number of Processors']

    def _get_cpu_model(self):
        if 'Processor Name' not in self.info:
            return '0'
        return self.info['Processor Name']

    def _get_serial(self):
        if 'Hardware UUID' not in self.info:
            return '0'
        return self.info['Hardware UUID']

    def _get_model(self):
        if 'Model Name' not in self.info:
            return '0'
        return self.info['Model Name']

0707010000bb9a000081a400000000000000000000000154fc7bfb00000b28000000660000000300000000000000000000002700000000root/opt/opensvc/lib/rcAssetFreeBSD.py    #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import datetime
from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv
import rcAssetLinux

class Asset(rcAssetLinux.Asset):
    def _get_mem_bytes(self):
        cmd = ['sysctl', 'hw.realmem']
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return '0'
        lines = out.split('\n')
        if len(lines) < 1:
            return '0'
        line = lines[0].split()
        if len(line) < 2:
            return '0'
        mb = int(line[-1])
        return str(mb/1024/1024)

    def _get_os_vendor(self):
        return 'FreeBSD'

    def _get_os_release(self):
        return self._get_os_kernel()

    def _get_os_arch(self):
        cmd = ['sysctl', 'hw.machine_arch']
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return 'Unknown'
        lines = out.split('\n')
        if len(lines) < 1:
            return 'Unknown'
        line = lines[0].split()
        if len(line) < 2:
            return 'Unknown'
        return line[-1]

    def _get_cpu_model(self):
        cmd = ['sysctl', 'hw.model']
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return 'Unknown'
        lines = out.split('\n')
        if len(lines) < 1:
            return 'Unknown'
        line = lines[0].split()
        if len(line) < 2:
            return 'Unknown'
        return " ".join(line[1:])

    def _get_cpu_cores(self):
        cmd = ['sysctl', 'hw.ncpu']
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return 'Unknown'
        lines = out.split('\n')
        if len(lines) < 1:
            return 'Unknown'
        line = lines[0].split()
        if len(line) < 2:
            return 'Unknown'
        return line[-1]

    def _get_cpu_freq(self):
        cmd = ['sysctl', 'hw.clockrate']
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return 'Unknown'
        lines = out.split('\n')
        if len(lines) < 1:
            return 'Unknown'
        line = lines[0].split()
        if len(line) < 2:
            return 'Unknown'
        return line[-1]
0707010000bb9b000081a400000000000000000000000154fc7bfb000023bc000000660000000300000000000000000000002500000000root/opt/opensvc/lib/rcAssetHP-UX.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv
from subprocess import *
import rcAsset
import datetime

os.environ['PATH'] += ":/opt/ignite/bin:/opt/propplus/bin"

class Asset(rcAsset.Asset):
    def __init__(self, node):
        rcAsset.Asset.__init__(self, node)

        out, err, ret = justcall(['print_manifest'])
        if ret != 0:
            self.manifest = []
        else:
            self.manifest = out.split('\n')

        self.parse_memory()

    def _get_mem_bytes(self):
        cmd = ['swapinfo', '-Mq']
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return '0'
        return str(int(out)//1024)

    def parse_memory(self):
        self.banks =  0
        self.slots =  0

        cmd = ['cprop', '-summary', '-c', 'Memory']
        out, err, ret = justcall(cmd)

        if ret != 0:
            return '0'

        in_banks = True

        for line in out.split('\n'):
            if 'Empty Slots' in line:
                in_banks = False
            elif 'Instance' in line:
                self.slots += 1
                if in_banks:
                    self.banks += 1

    def _get_mem_banks(self):
        s = str(self.banks)
        if s == '0':
            s = self._get_mem_banks_ts99()
        return s

    def _get_mem_slots(self):
        return str(self.slots)

    def _get_os_vendor(self):
        return 'HP'

    def _get_os_release(self):
        (out, err, ret) = justcall(['uname', '-r'])
        if ret != 0:
            return 'Unknown'
        return out.split('\n')[0].strip()

    def _get_os_kernel(self):
        (out, err, ret) = justcall(['swlist', '-l', 'bundle', 'QPKBASE'])
        if ret != 0:
            return 'Unknown'
        for line in out.split('\n'):
            if 'QPKBASE' in line:
                return line.split()[1]
        return 'Unknown'

    def _get_os_arch(self):
        (out, err, ret) = justcall(['uname', '-m'])
        if ret != 0:
            return 'Unknown'
        return out.split('\n')[0].strip()

    def _get_cpu_freq(self):
        freq = self._get_cpu_freq_manifest()
        if freq == "Unknown":
            freq = self._get_cpu_freq_adb()
        return freq

    def _get_cpu_freq_manifest(self):
        m = self._get_cpu_model()
        if '(' not in m:
            return "Unknown"
        s = m.split('(')[-1]
        s = s.split(',')[0]
        freq, unit = s.split()
        if unit == 'GHz':
            try:
                freq = float(freq)
            except:
                return "Unknown"
            freq = str(int(freq * 1000))
        return freq

    def _get_cpu_freq_adb(self):
        process = Popen(['adb', '/stand/vmunix', '/dev/kmem'], stdin=PIPE, stdout=PIPE, stderr=None)
        (out, err) = process.communicate(input='itick_per_usec/2d')
        if process.returncode != 0:
            process = Popen(['adb', '-k', '/stand/vmunix', '/dev/mem'], stdin=PIPE, stdout=PIPE, stderr=None)
            (out, err) = process.communicate(input='itick_per_usec/D')
        if process.returncode != 0:
            return 'Unknown'
        lines = out.split('\n')
        if len(lines) < 2:
            return 'Unknown'
        return lines[1].split()[-1]

    def _get_cpu_cores(self):
        for line in self.manifest:
            if 'Processors:' in line:
                return line.split()[-1]
        return '0'

    def _get_cpu_dies(self):
        n = self._get_cpu_cores_per_die()
        if n == 0:
            return str(self._get_cpu_dies_ts99())
        cores = int(self._get_cpu_cores())
        return str(cores // n)

    def _get_cpu_cores_per_die(self):
        n = 0
        i = 0
        for line in self.manifest:
            line = line.replace('(', '').replace(')', '')
            if 'Processors:' in line:
                i = 1
                continue
            if i > 0 and i < 4:
                i += 1
                if "core" not in line and "socket" not in line:
                    continue
                words = line.split()
                for j, w in enumerate(words):
                    if w == "socket":
                        try:
                            n = int(words[j-2])
                        except:
                            break
                    if 'core' in w:
                        try:
                            n = int(words[j-1])
                        except:
                            break
            elif i >= 4:
                break
        return n

    def _get_mem_banks_ts99(self):
        p = '/var/tombstones/ts99'
        if not os.path.exists(p):
            return '0'
        with open(p, 'r') as f:
            buff = f.read()
        lines = buff.split('\n')
        c = 0
        for line in lines:
            if "DIMM Error Information" in line:
                c += 1
        return str(c)

    def _get_cpu_dies_ts99(self):
        # count different serial numbers
        p = '/var/tombstones/ts99'
        if not os.path.exists(p):
            return 1
        with open(p, 'r') as f:
            buff = f.read()
        lines = buff.split('\n')
        serials = set([])
        for line in lines:
            if "Cpu Serial Number" in line:
                serials.add(line.split()[-1])
        if len(serials) == 0:
            return 1
        return len(serials)

    def _get_cpu_model(self):
        s = self._get_cpu_model_manifest()
        if s == 'Unknown':
            s = self._get_cpu_model_ts99()
        return s

    def _get_cpu_model_ts99(self):
        p = '/var/tombstones/ts99'
        if not os.path.exists(p):
            return 'Unknown'
        with open(p, 'r') as f:
            buff = f.read()
        lines = buff.split('\n')
        for line in lines:
            if "CPU Module" in line:
                return line.strip().replace("CPU Module", "rev").replace('  ', ' ')
        return 'Unknown'

    def _get_cpu_model_manifest(self):
        marker = False
        for line in self.manifest:
            if 'Processors:' in line:
                marker = True
                continue
            if marker:
                if "processor" not in line:
                    return 'Unknown'
                e = line.split()
                return ' '.join(e[1:]).replace('processors','').replace('processor','')
        return 'Unknown'

    def _get_serial(self):
        (out, err, ret) = justcall(['getconf', 'MACHINE_SERIAL'])
        if ret != 0:
            return 'Unknown'
        return out.strip()

    def _get_model(self):
        (out, err, ret) = justcall(['getconf', 'MACHINE_MODEL'])
        if ret != 0:
            return 'Unknown'
        return out.strip()

    def __get_hba(self):
        if hasattr(self, "hba"):
            return self.hba
        self.hba = []
        cmd = ['/usr/sbin/ioscan', '-FunC', 'fc']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.hba
        lines = out.split('\n')
        if len(lines) < 2:
            return self.hba
        for line in lines:
            if '/dev/' not in line:
                continue
            dev = line.strip()
            hba_type = 'fc'

            cmd = ['/opt/fcms/bin/fcmsutil', dev]
            out, err, ret = justcall(cmd)
            if ret != 0:
                continue
            for _line in out.split('\n'):
                if not 'N_Port Port World Wide Name' in _line:
                    continue
                hba_id = _line.split('=')[-1].strip().lstrip("0x")

            cmd = ['/opt/fcms/bin/fcmsutil', dev, 'get', 'remote', 'all']
            out, err, ret = justcall(cmd)
            if ret != 0:
                continue
            targets = []
            for _line in out.split('\n'):
                if not 'Target Port World Wide Name' in _line:
                    continue
                targets.append(_line.split('=')[-1].strip().lstrip("0x"))

            self.hba.append((hba_id, hba_type, targets))
        return self.hba

    def _get_hba(self):
        hba = self.__get_hba()
        l = []
        for hba_id, hba_type, targets in hba:
            l.append((hba_id, hba_type))
        return l

    def _get_targets(self):
        hba = self.__get_hba()
        l = []
        for hba_id, hba_type, targets in hba:
            for target in targets:
                l.append((hba_id, target))
        return l

0707010000bb9c000081a400000000000000000000000154fc7bfb00003a8a000000660000000300000000000000000000002500000000root/opt/opensvc/lib/rcAssetLinux.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import datetime
from rcUtilities import justcall, which
import rcAsset

def is_container():
    p = '/proc/1/cgroup'
    if not os.path.exists(p):
        return False
    with open(p, 'r') as f:
        lines = f.readlines()
        if len(lines) != 1:
            return False
        if ':' not in lines[0]:
            return False
        l = lines[0].split(':')
        if l[-1].strip('\n') != '/':
            return True
    return False


class Asset(rcAsset.Asset):
    def __init__(self, node):
        rcAsset.Asset.__init__(self, node)
        self.container = is_container()
        self.detect_xen()
        if self.container:
            self.dmidecode = []
        else:
            out, err, ret = justcall(['dmidecode'])
            if ret != 0:
                self.dmidecode = []
            else:
                self.dmidecode = out.split('\n')

    def _get_mem_bytes_esx(self):
        cmd = ['vmware-cmd', '-s', 'getresource', 'system.mem.totalMem']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return '0'
        l = out.split(' = ')
        if len(l) < 2:
            return '0'
        try:
            size = str(int(l[-1])/1024)
        except:
            size = '0'
        return size

    def _get_mem_bytes_hv(self):
        if which('virsh'):
            return self._get_mem_bytes_virsh()
        if which('xm'):
            return self._get_mem_bytes_xm()
        else:
            return '0'

    def _get_mem_bytes_virsh(self):
        cmd = ['virsh', 'nodeinfo']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return '0'
        lines = out.split('\n')
        for line in lines:
            if 'Memory size' not in line:
                continue
            l = line.split()
            if len(l) < 2:
                continue
            return l[-2]
        return '0'

    def _get_mem_bytes_xm(self):
        cmd = ['xm', 'info']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return '0'
        lines = out.split('\n')
        for line in lines:
            if 'total_mem' not in line:
                continue
            l = line.split(':')
            if len(l) < 2:
                continue
            return l[-1]
        return '0'

    def _get_mem_bytes_phy(self):
        cmd = ['free', '-m']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return '0'
        lines = out.split('\n')
        if len(lines) < 2:
            return '0'
        line = lines[1].split()
        if len(line) < 2:
            return '0'
        return line[1]

    def detect_xen(self):
        c = os.path.join(os.sep, 'sys', 'hypervisor', 'uuid')
        self.xenguest = False
        self.xenhv = False
        if not os.path.exists(c):
            return
        with open(c, 'r') as f:
            if '00000000-0000-0000-0000-000000000000' in f.read():
                self.xenhv = True
            else:
                self.xenguest = True

    def is_esx_hv(self):
        return which('vmware-cmd')

    def _get_mem_bytes(self):
        if self.xenhv:
            return self._get_mem_bytes_hv()
        elif self.is_esx_hv():
            s = self._get_mem_bytes_esx()
            if s == '0':
                return self._get_mem_bytes_phy()
        else:
            return self._get_mem_bytes_phy()

    def _get_mem_banks(self):
        if self.container:
            return 'n/a'
        banks =  0
        inBlock = False
        for l in self.dmidecode:
            if not inBlock and l == "Memory Device":
                 inBlock = True
            if inBlock and "Size:" in l:
                 e = l.split()
                 if len(e) == 3:
                     try:
                         size = int(e[1])
                         banks += 1
                     except:
                         pass
        return str(banks)

    def _get_mem_slots(self):
        if self.container:
            return 'n/a'
        for l in self.dmidecode:
            if 'Number Of Devices:' in l:
                return l.split()[-1]
        return '0'

    def _get_os_vendor(self):
        if os.path.exists('/etc/lsb-release'):
            with open('/etc/lsb-release') as f:
                for line in f.readlines():
                    if 'DISTRIB_ID' in line:
                        return line.split('=')[-1].replace('\n','').strip('"')
        if os.path.exists('/etc/debian_version'):
            return 'Debian'
        if os.path.exists('/etc/SuSE-release'):
            return 'SuSE'
        if os.path.exists('/etc/vmware-release'):
            return 'VMware'
        if os.path.exists('/etc/oracle-release'):
            return 'Oracle'
        if os.path.exists('/etc/redhat-release'):
            with open('/etc/redhat-release', 'r') as f:
                buff = f.read()
                if 'CentOS' in buff:
                    return 'CentOS'
                elif 'Oracle' in buff:
                    return 'Oracle'
                else:
                    return 'Red Hat'
        return 'Unknown'

    def _get_os_release(self):
        files = ['/etc/debian_version',
                 '/etc/vmware-release',
                 '/etc/oracle-release',
                 '/etc/redhat-release']
        if os.path.exists('/etc/SuSE-release'):
            v = []
            with open('/etc/SuSE-release') as f:
                for line in f.readlines():
                    if 'VERSION' in line:
                        v += [line.split('=')[-1].replace('\n','').strip('" ')]
                    if 'PATCHLEVEL' in line:
                        v += [line.split('=')[-1].replace('\n','').strip('" ')]
            return '.'.join(v)
        if os.path.exists('/etc/lsb-release'):
            with open('/etc/lsb-release') as f:
                for line in f.readlines():
                    if 'DISTRIB_DESCRIPTION' in line:
                        r = line.split('=')[-1].replace('\n','').strip('"')
                        r = r.replace(self._get_os_vendor(), '').strip()
                        return r
        if os.path.exists('/etc/oracle-release') and \
           os.path.exists('/etc/redhat-release'):
            with open('/etc/oracle-release') as f1:
                if " VM " in f1.read():
                    with open('/etc/redhat-release') as f2:
                        return f2.read().split('\n')[0].replace(self._get_os_vendor(), '').strip()
        for f in files:
            if os.path.exists(f):
                (out, err, ret) = justcall(['cat', f])
                if ret != 0:
                    return 'Unknown'
                return out.split('\n')[0].replace(self._get_os_vendor(), '').strip()
        return 'Unknown'

    def _get_os_kernel(self):
        (out, err, ret) = justcall(['uname', '-r'])
        if ret != 0:
            return 'Unknown'
        return out.split('\n')[0]

    def _get_os_arch(self):
        if which('arch') is not None:
            cmd = ['arch']
        else:
            cmd = ['uname', '-m']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return 'Unknown'
        return out.split('\n')[0]

    def _get_cpu_freq(self):
        p = '/proc/cpuinfo'
        if not os.path.exists(p):
            return 'Unknown'
        with open(p, 'r') as f:
            for line in f.readlines():
                if 'cpu MHz' in line:
                    return line.split(':')[1].strip().split('.')[0]
        return 'Unknown'

    def _get_cpu_cores(self):
        if self.container:
            return 'n/a'
        try:
            with open('/proc/cpuinfo') as f:
                lines = f.readlines()
        except:
            return '0'
        phy = {}
        for line in lines:
            if 'physical id' in line:
                id = line.split(":")[-1].strip()
                if id not in phy:
                    phy[id] = []
            elif 'core id' in line:
                coreid = line.split(":")[-1].strip()
                if coreid not in phy[id]:
                    phy[id].append(coreid)
        n_cores = 0
        for id, coreids in phy.items():
            n_cores += len(coreids)
        if n_cores == 0:
            return self._get_cpu_dies()
        return str(n_cores)

    def _get_cpu_dies_dmi(self):
        if self.container:
            return 'n/a'
        n = 0
        for l in self.dmidecode:
            if 'Processor Information' in l:
                n += 1
        return str(n)

    def _get_cpu_dies_cpuinfo(self):
        if self.container:
            return 'n/a'
        try:
            with open('/proc/cpuinfo') as f:
                lines = f.readlines()
        except:
            return '0'
        _lines = set([l for l in lines if 'physical id' in l])
        n_dies = len(_lines)
        if n_dies > 0:
            return str(n_dies)
        # vmware do not show processor physical id
        _lines = [l for l in lines if 'processor' in l]
        n_dies = len(_lines)
        return str(n_dies)

    def _get_cpu_threads(self):
        if self.container:
            return 'n/a'
        try:
            with open('/proc/cpuinfo') as f:
                lines = f.readlines()
        except:
            return '0'
        lines = [l for l in lines if 'physical id' in l]
        n_threads = len(lines)
        if n_threads == 0:
            return self._get_cpu_dies()
        return str(len(lines))

    def _get_cpu_dies(self):
        n = self._get_cpu_dies_cpuinfo()
        if n == '0':
            n = self._get_cpu_dies_dmi()
        return n

    def _get_cpu_model(self):
        (out, err, ret) = justcall(['grep', 'model name', '/proc/cpuinfo'])
        if ret != 0:
            return 'Unknown'
        lines = out.split('\n')
        l = lines[0].split(':')
        return l[1].strip()

    def _get_serial_1(self):
        try:
            i = self.dmidecode.index('System Information')
        except ValueError:
            return 'Unknown'
        for l in self.dmidecode[i+1:]:
            if 'Serial Number:' in l:
                return l.split(':')[-1].strip()
        return 'Unknown'

    def _get_serial_2(self):
	""" Dell poweredge 2500 are known to be in this case
        """
        try:
            i = self.dmidecode.index('Chassis Information')
        except ValueError:
            return 'Unknown'
        for l in self.dmidecode[i+1:]:
            if 'Serial Number:' in l:
                return l.split(':')[-1].strip()
        return 'Unknown'

    def _get_serial(self):
        if self.container:
            return 'n/a'
        serial = self._get_serial_1()
        if serial in ('Unknown', 'Not Specified'):
            serial = self._get_serial_2()
        return serial

    def _get_enclosure(self):
        if self.container:
            return 'n/a'
        for l in self.dmidecode:
            if 'Enclosure Name:' in l:
                return l.split(':')[-1].strip()
        return 'Unknown'

    def _get_model(self):
        if self.container:
            return 'container'
        elif self.xenguest and len(self.dmidecode) < 5:
            return "Xen Virtual Machine (PVM)"
        for l in self.dmidecode:
            if 'Product Name:' in l:
                return l.split(':')[-1].strip()
        return 'Unknown'

    def get_iscsi_hba_id(self):
        path = os.path.join(os.sep, 'etc', 'iscsi', 'initiatorname.iscsi')
        hba_id = None
        if os.path.exists(path):
            with open(path, 'r') as f:
                hba_id = f.read().split('=')[-1].strip()
        return hba_id
 
    def __get_hba(self):
        # fc / fcoe
        l = []
        import glob
        paths = glob.glob('/sys/class/fc_host/host*/port_name')
        for path in paths:
            host_link = '/'.join(path.split('/')[0:5])
            if '/eth' in os.path.realpath(host_link):
                hba_type = 'fcoe'
            else:
                hba_type = 'fc'
            with open(path, 'r') as f:
                hba_id = f.read().strip('0x').strip('\n')
            host = path.replace('/sys/class/fc_host/host', '')
            host = host[0:host.index('/')]

            l.append((hba_id, hba_type, host))

        # redhat 4 qla driver does not export hba portname in sysfs
        paths = glob.glob("/proc/scsi/qla2xxx/*")
        for path in paths:
            with open(path, 'r') as f:
                buff = f.read()
                for line in buff.split("\n"):
                    if "adapter-port" not in line:
                        continue
                    _l = line.split("=")
                    if len(_l) != 2:
                        continue
                    host = os.path.basename(path)
                    e = (_l[1].rstrip(";"), "fc", host)
                    if e not in l:
                        l.append(e)

        # iscsi
        path = os.path.join(os.sep, 'etc', 'iscsi', 'initiatorname.iscsi')
        hba_type = 'iscsi'
        hba_id = self.get_iscsi_hba_id()
        if hba_id is not None:
            l.append((hba_id, hba_type, ''))

        return l

    def _get_hba(self):
        return map(lambda x: (x[0], x[1]), self.__get_hba())

    def _get_targets(self):
        import glob
        # fc / fcoe
        l = []
        hbas = self.__get_hba()
        for hba_id, hba_type, host in hbas:
            if not hba_type.startswith('fc'):
                continue
            for target in glob.glob('/sys/class/fc_transport/target%s:*/port_name'%host):
                with open(target, 'r') as f:
                    tgt_id = f.read().strip('0x').strip('\n')
                l.append((hba_id, tgt_id))

        # iscsi
        hba_id = self.get_iscsi_hba_id()
        if hba_id is not None:
           cmd = ['iscsiadm', '-m', 'session']
           out, err, ret = justcall(cmd)
           if ret == 0:
               """
               tcp: [1] 192.168.231.141:3260,1 iqn.2000-08.com.datacore:sds1-1
               tcp: [2] 192.168.231.142:3260,1 iqn.2000-08.com.datacore:sds2-1
               """
               for line in out.split('\n'):
                   if len(line) == 0:
                       continue
                   l.append((hba_id, line.split()[-1]))

        return l

  0707010000bb9d000081a400000000000000000000000154fc7bfb00002e72000000660000000300000000000000000000002400000000root/opt/opensvc/lib/rcAssetOSF1.py   #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os
from rcUtilities import justcall, which
import rcAsset
from distutils.version import LooseVersion as V
import re

sim = False
regex = re.compile("^\W*[0-9]*:")

class Asset(rcAsset.Asset):
    def __init__(self, node):
        rcAsset.Asset.__init__(self, node)

    def convert(self, s, unit):
        if unit == "M":
            return int(float(s))
        elif unit == "G":
            return 1024 * int(float(s))
        else:
            return 0
    
    def _get_mem_bytes(self):
        # MB
        cmd = ['vmstat', '-P']
        out, err, ret = _justcall(cmd)
        for line in out.split('\n'):
            if 'Total' not in line:
                continue
            l = line.split()
            s = l[-2]
            unit = l[-1]
            mem = self.convert(s, unit)
            return str(mem)
        return '0'

    def _get_mem_banks(self):
        return '0'

    def _get_mem_slots(self):
        return '0'

    def _get_os_vendor(self):
        return 'HP'

    def _get_os_release(self):
        cmd = ['uname', '-a']
        out, err, ret = _justcall(cmd)
        l = out.split()
        return ' '.join(l[2:4])

    def _get_os_kernel(self):
        cmd = ['dupatch', '-track', '-type', 'kit', '-nolog']
        out, err, ret = _justcall(cmd)
        l = []
        for line in out.split('\n'):
            line = line.strip()
            if not line.startswith('- T64') or not 'OSF' in line:
                continue
            l.append(line.split()[1])
        if len(l) == 0:
            return 'Unknown'
        l.sort(lambda x, y: V(x) < V(y))
        return l[-1].split('-')[0]

    def _get_os_arch(self):
        cmd = ['uname', '-a']
        out, err, ret = _justcall(cmd)
        l = out.split()
        return l[-1]

    def _get_cpu_freq(self):
        cmd = ['psrinfo', '-v']
        out, err, ret = _justcall(cmd)
        for line in out.split('\n'):
            if 'operates at' not in line:
                continue
            l = line.split()
            if len(l) < 2:
                continue
            return l[-2]
        return '0'

    def _get_cpu_cores(self):
        return self._get_cpu_dies()

    def _get_cpu_dies(self):
        cmd = ['psrinfo']
        out, err, ret = _justcall(cmd)
        return str(len(out.split('\n'))-1)

    def _get_cpu_model(self):
        cmd = ['psrinfo', '-v']
        out, err, ret = _justcall(cmd)
        for line in out.split('\n'):
            if 'operates at' not in line:
                continue
            l = line.split()
            if len(l) < 3:
                continue
            return l[2]
        return 'Unknown'

    def _get_serial(self):
        cmd = ['consvar', '-g', 'sys_serial_num']
        out, err, ret = _justcall(cmd)
        l = out.split('=')
        if len(l) == 2:
            return l[1].strip()
        return 'Unknown'

    def _get_model(self):
        cmd = ["hwmgr", "-v", "h"]
        out, err, ret = _justcall(cmd)
        for line in out.split('\n'):
            if "platform" not in line:
                continue
            l = line.split("platform")
            if len(l) != 2:
                continue
            return l[1].strip()
        return 'Unknown'

    def is_id(self, line):
        if regex.match(line) is None:
            return False
        return True

    def __get_hba(self):
        # fc / fcoe
        cmd = ['hwmgr', '-show', 'fibre', '-ada']
        out, err, ret = _justcall(cmd)
        hba = {}
        for line in out.split('\n'):
            if self.is_id(line):
                l = line.split()
                hba_name = l[1]
            elif 'WWPN' in line:
                l = line.split()
                hba_portname = l[1].replace('-', '').lower()
                hba[hba_name] = (hba_portname, 'fc')
        return hba

    def _get_hba(self):
        hba = self.__get_hba()
        return hba.values()

    def _get_targets(self):
        # fc / fcoe
        cmd = ['hwmgr', '-show', 'fibre', '-topo']
        out, err, ret = _justcall(cmd)
        tgt = []
        hba = self.__get_hba()
        for line in out.split('\n'):
            if self.is_id(line):
                l = line.split()
                hba_name = l[1]
            elif line.strip().startswith('0x'):
                l = line.split()
                if l[1].startswith('-'):
                    continue
                tgt_portname = l[2].replace('-', '').lower()
                hba_portname = hba[hba_name][0]
                tgt.append((hba_portname, tgt_portname))
        return tgt

def _justcall(cmd):
    if not sim:
        return justcall(cmd)

    data = {}

    data[('hwmgr', '-show', 'fibre', '-ada')] = """
    
            ADAPTER   LINK    LINK             FABRIC     SCSI     CARD
     HWID:  NAME      STATE   TYPE             STATE      BUS      MODEL
    --------------------------------------------------------------------------------
       53:  emx0      up      point-to-point   attached   scsi3    KGPSA-CA 
    
    		Revisions:  driver 2.17           firmware 3.93A0 
    		FC Address: 0x1ece00
    		TARGET:     -1
    		WWPN/WWNN:  1000-0000-c922-585c   2000-0000-c922-585c 
    
            ADAPTER   LINK    LINK             FABRIC     SCSI     CARD
     HWID:  NAME      STATE   TYPE             STATE      BUS      MODEL
    --------------------------------------------------------------------------------
       61:  emx1      up      point-to-point   attached   scsi4    KGPSA-CA 
    
    		Revisions:  driver 2.17           firmware 3.93A0 
    		FC Address: 0x1cce00
    		TARGET:     -1
    		WWPN/WWNN:  1000-0000-c924-a43d   2000-0000-c924-a43d 
    
    """
    
    data[('hwmgr', '-show', 'fibre', '-topo')] = """
    
            ADAPTER   LINK    LINK             FABRIC     SCSI     CARD
     HWID:  NAME      STATE   TYPE             STATE      BUS      MODEL
    --------------------------------------------------------------------------------
       53:  emx0      up      point-to-point   attached   scsi3    KGPSA-CA 
    
    	FC DID	  TARGET    WWPN     		 WWNN                 lfd  LSIT
    	------------------------------------------------------------------------
    	0x382200      2     5000-1fe1-5012-9d49  5000-1fe1-5012-9d40  l--  L--T
    	0x381200      3     5000-1fe1-5012-9d4f  5000-1fe1-5012-9d40  l--  L--T
    	0x380200      0     5000-1fe1-5012-9d4d  5000-1fe1-5012-9d40  l--  L--T
    	0x383200      1     5000-1fe1-5012-9d4b  5000-1fe1-5012-9d40  l--  L--T
    	0xfffffc     -1     21fc-0005-1e36-2110  1000-0005-1e36-2110  l-d  ----
    	0xfffffe     -1     20ce-0005-1e36-2110  1000-0005-1e36-2110  lf-  ----
    
            ADAPTER   LINK    LINK             FABRIC     SCSI     CARD
     HWID:  NAME      STATE   TYPE             STATE      BUS      MODEL
    --------------------------------------------------------------------------------
       61:  emx1      up      point-to-point   attached   scsi4    KGPSA-CA 
    
    	FC DID	  TARGET    WWPN     		 WWNN                 lfd  LSIT
    	------------------------------------------------------------------------
    	0xef1200      2     5000-1fe1-5012-9d4e  5000-1fe1-5012-9d40  l--  L--T
    	0xef3200      0     5000-1fe1-5012-9d4a  5000-1fe1-5012-9d40  l--  L--T
    	0xef0200      3     5000-1fe1-5012-9d4c  5000-1fe1-5012-9d40  l--  L--T
    	0xef2200      1     5000-1fe1-5012-9d48  5000-1fe1-5012-9d40  l--  L--T
    	0xfffffc     -1     21fc-0005-1e36-1eee  1000-0005-1e36-1eee  l-d  ----
    	0xfffffe     -1     20ce-0005-1e36-1eee  1000-0005-1e36-1eee  lf-  ----
    
    """
    
    data[('hwmgr', '-show', 'scsi', '-full', '-id', '83', '-nowrap')] = """
    
            SCSI                DEVICE    DEVICE  DRIVER NUM  DEVICE FIRST 
     HWID:  DEVICEID HOSTNAME   TYPE      SUBTYPE OWNER  PATH FILE   VALID PATH
    -------------------------------------------------------------------------
       83:  16       wrus01     disk      none    2      8    dsk13  [3/2/1]   
    
          WWID:01000010:6005-08b4-000b-440a-0000-f000-1325-0000
    
    
          BUS   TARGET  LUN   PATH STATE   
          ---------------------------------
          3     2       1     valid     
          3     3       1     valid     
          3     1       1     valid     
          3     0       1     valid     
          4     3       1     valid     
          4     2       1     valid     
          4     1       1     valid     
          4     0       1     valid     
    
    """
    
    data[('uname', '-a')] = """OSF1 wrus01 V5.1 2650 alpha
    """
    
    data[('dupatch', '-track', '-type', 'kit', '-nolog')] = """
    Gathering details of relevant patches, this may take a bit of time
    
    
    	Patches installed on the system came from following software kits:
    	------------------------------------------------------------------
    
    	- T64V51BB24AS0003-20030929 OSF540
    	- T64V51BB26AS0005-20050502 IOS540
    	- T64V51BB26AS0005-20050502 OSF540
              ================
                 kernelver
    
    				NOTE
    
    	When a patch kit is listed, it does not necessarily mean 
    	all patches on that kit are installed on your system.
    """
    
    data[('psrinfo',)] = """0	on-line   since 05/19/2012 16:00:50
    """
    
    data[('psrinfo', '-v')] = """Status of processor 0 as of: 06/15/12 18:35:40
      Processor has been on-line since 05/19/2012 16:00:50
      The alpha EV6.8CB (21264C) processor operates at 1000 MHz,
      has a cache size of 8388608 bytes,
      and has an alpha internal floating point processor.
    """
    
    data[('consvar', '-g', 'sys_serial_num')] = """ sys_serial_num = AY14610125
    """
    
    data[('hwmgr', '-v', 'h')] = """HWID:   hardware hierarchy
    -------------------------------------------------------------------------------
       1:   platform AlphaServer ES45 Model 2
       2:     cpu CPU0
       6:     bus iop0
       7:       bus hose0
    """
    
    data[('ifconfig', '-a')] = """ee0: flags=c63<UP,BROADCAST,NOTRAILERS,RUNNING,MULTICAST,SIMPLEX>
         inet 10.6.65.37 netmask fffffc00 broadcast 10.6.67.255 ipmtu 1500 
         inet 10.6.66.160 netmask fffffc00 broadcast 10.6.67.255 ipmtu 1500 
    
    ee1: flags=c63<UP,BROADCAST,NOTRAILERS,RUNNING,MULTICAST,SIMPLEX>
         inet 10.40.32.241 netmask fffffc00 broadcast 10.40.35.255 ipmtu 1500 
    
    lo0: flags=100c89<UP,LOOPBACK,NOARP,MULTICAST,SIMPLEX,NOCHECKSUM>
         inet 127.0.0.1 netmask ff000000 ipmtu 4096 
    
    sl0: flags=10<POINTOPOINT>
    
    tun0: flags=80<NOARP>
    
    tun1: flags=80<NOARP>
    """
    
    data[('vmstat', '-P')] = """Total Physical Memory =  1024.00 M
    """

    return data[tuple(cmd)], '', 0
    
    
if __name__ == "__main__":
    o = Asset("wrus01")
    print(o._get_mem_bytes())
    print(o._get_os_release())
    print(o._get_os_kernel())
    print(o._get_os_arch())
    print(o._get_cpu_freq())
    print(o._get_cpu_cores())
    print(o._get_cpu_dies())
    print(o._get_cpu_model())
    print(o._get_serial())
    print(o._get_model())
    print(o._get_hba())
    print(o._get_targets())
  0707010000bb9e000081a400000000000000000000000154fc7bfb00001e80000000660000000300000000000000000000002500000000root/opt/opensvc/lib/rcAssetSunOS.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import datetime
from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv
import rcAsset
from rcZone import is_zone

class Asset(rcAsset.Asset):
    def __init__(self, node=None):
        rcAsset.Asset.__init__(self, node)
        self.osver = 0.
        self.zone = is_zone()

        (out, err, ret) = justcall(['prtdiag'])
        if ret != 0 and len(out) < 4:
            self.prtdiag = []
        else:
            self.prtdiag = out.split('\n')
        (out, err, ret) = justcall(['prtconf'])
        if ret != 0 and len(out) < 4:
            self.prtconf = []
        else:
            self.prtconf = out.split('\n')

    def _get_mem_bytes(self):
        for l in self.prtconf:
            if 'Memory size:' in l:
                return l.split(':')[-1].split()[0]
        return '0'

    def _get_mem_banks(self):
        l = [e for e in self.prtdiag if 'DIMM' in e and 'in use' in e]
        return str(len(l))

    def _get_mem_slots(self):
        l = [e for e in self.prtdiag if 'DIMM' in e]
        return str(len(l))

    def _get_os_vendor(self):
        return 'Oracle'

    def _get_os_name(self):
        f = '/etc/release'
        if os.path.exists(f):
            (out, err, ret) = justcall(['cat', f])
            if ret != 0:
                return 'Unknown'
            if 'OpenSolaris' in out:
                return 'OpenSolaris'
        return 'SunOS'

    def _get_os_release(self):
        f = '/etc/release'
        if os.path.exists(f):
            (out, err, ret) = justcall(['cat', f])
            if ret != 0:
                return 'Unknown'
            return out.split('\n')[0].replace('OpenSolaris','').replace('Oracle', '').strip()
        return 'Unknown'

    def _get_os_kernel(self):
        (out, err, ret) = justcall(['uname', '-v'])
        if ret != 0:
            return 'Unknown'
        lines = out.split('\n')
        if len(lines) == 0:
            return 'Unknown'
        try:
            self.osver = float(lines[0])
        except:
            return lines[0]
        if self.osver < 11.:
            return lines[0]
        else:
            (out, err, ret) = justcall(['pkg', 'info', 'entire'])
            if ret != 0:
                return 'Unknown'
            nfo = out.split('\n')
            for l in nfo:
                if 'Version: ' in l:
                    if 'SRU' in l:
                        return ' '.join([lines[0], 'SRU', l.split()[6].strip(')')])
                    elif lines[0] in l:
                        return l.split()[4].strip(')')
                    else:
                        return ' '.join([lines[0], l.split()[4]])
        return 'Unknown'

    def _get_os_arch(self):
        (out, err, ret) = justcall(['uname', '-m'])
        if ret != 0:
            return 'Unknown'
        return out.split('\n')[0]

    def _get_cpu_freq(self):
        (out, err, ret) = justcall(['/usr/sbin/psrinfo', '-pv'])
        if ret != 0:
            return '0'
        for w in out.split():
            if 'MHz)' in w:
                return prev
            prev = w
        (out, err, ret) = justcall(['kstat', 'cpu_info'])
        if ret != 0:
            return '0'
        l = out.split()
        if 'clock_MHz' in l:
            freq = l[l.index('clock_MHz')+1]
            return freq
        return '0'

    def _get_cpu_cores(self):
        cmd = ['kstat', 'cpu_info']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return '0'
        core_ids = set([])
        if "core_id" in out:
            keyword = "core_id"
        else:
            keyword = "chip_id"
        for line in out.split('\n'):
            if not line.strip().startswith(keyword):
                continue
            core_ids.add(line.split()[-1])
        return str(len(core_ids))

    def _get_cpu_threads(self):
        out, err, ret = justcall(['/usr/sbin/psrinfo'])
        if ret != 0:
            return '0'
        return str(len(out.split('\n'))-1)

    def _get_cpu_dies(self):
        (out, err, ret) = justcall(['/usr/sbin/psrinfo', '-p'])
        if ret != 0:
            return '0'
        return out.split('\n')[0]

    def _get_cpu_model(self):
        (out, err, ret) = justcall(['/usr/sbin/psrinfo', '-pv'])
        if ret != 0:
            return 'Unknown'
        lines = out.split('\n')
        lines = [line for line in lines if len(line) > 0]
        if len(lines) == 0:
            return 'Unknown'
        model = lines[-1].strip()
        if model.startswith('The '):
            model = model.replace('The ', '')
        known_garbage = [' (chipid', ' (portid', ' physical proc']
        for s in known_garbage:
            try:
                i = model.index(s)
                model = model[:i]
            except ValueError:
                continue
        return model

    def _get_serial(self):
        if which("sneep"):
            cmd = ['sneep']
        else:
            cmd = ['hostid']
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return 'Unknown'
        return out.split('\n')[0]

    def _get_model(self):
        if self.zone:
            return "Solaris Zone"
        for l in self.prtdiag:
            if 'System Configuration:' in l:
                return l.split(':')[-1].strip()
        return 'Unknown'

    def __get_hba(self):
        # fc / fcoe
        """
        # cfgadm -s match="exact,select=type(fc-fabric)"
        Ap_Id      Type         Receptacle   Occupant     Condition
        c5         fc-fabric    connected    configured   unknown
        """
        l = []
        if not which('cfgadm'):
            return []
        if not which('luxadm'):
            return []
        cmd = ['cfgadm', '-lv', '-s', 'match=exact,select=type(fc-fabric)']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return []
        words = out.split()
        hba_names = [word for word in words if word.startswith("/devices/")]

        if len(hba_names) == 0:
            return []

        hba_type = 'fc'
        for hba_name in hba_names:
            targets = []
            cmd = ['luxadm', '-e', 'dump_map', hba_name]
            out, err, ret = justcall(cmd)
            if ret != 0:
                continue
            lines = out.split('\n')
            if len(lines) < 2:
                continue
            for line in lines[1:]:
                words = line.split()
                if len(words) < 5:
                    continue
                if 'Host Bus' in line:
                    hba_id = words[3]
                else:
                    targets.append(words[3])
            l.append((hba_id, hba_type, targets))

        return l

    def _get_hba(self):
        l = self.__get_hba()
        return map(lambda x: (x[0], x[1]), l)

    def _get_targets(self):
        l = self.__get_hba()
        m = []
        for hba_id, hba_type, targets in l:
             for target in targets:
                 m.append((hba_id, target))
        return m

if __name__ == "__main__":
    print(Asset()._get_cpu_model())
0707010000bb9f000081a400000000000000000000000154fc7bfb000013fc000000660000000300000000000000000000002700000000root/opt/opensvc/lib/rcAssetWindows.py    #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import sys
import platform
import datetime
from rcUtilities import justcall, which
from rcUtilitiesWindows import get_registry_value
import rcAsset
import ctypes
import wmi
from rcDiskInfoWindows import diskInfo

class MEMORYSTATUSEX(ctypes.Structure):
    _fields_ = [("dwLength", ctypes.c_uint),
                ("dwMemoryLoad", ctypes.c_uint),
                ("ullTotalPhys", ctypes.c_ulonglong),
                ("ullAvailPhys", ctypes.c_ulonglong),
                ("ullTotalPageFile", ctypes.c_ulonglong),
                ("ullAvailPageFile", ctypes.c_ulonglong),
                ("ullTotalVirtual", ctypes.c_ulonglong),
                ("ullAvailVirtual", ctypes.c_ulonglong),
                ("sullAvailExtendedVirtual", ctypes.c_ulonglong),]

    def __init__(self):
        # have to initialize this to the size of MEMORYSTATUSEX
        self.dwLength = 2*4 + 7*8     # size = 2 ints, 7 longs
        return super(MEMORYSTATUSEX, self).__init__()


class Asset(rcAsset.Asset):
    def __init__(self, node):
        self.w = wmi.WMI()
	self.cpuinfo = self.w.Win32_Processor()
        rcAsset.Asset.__init__(self, node)
        self.memstat = MEMORYSTATUSEX()
        ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(self.memstat))

    def _get_mem_bytes(self):
        return str(self.memstat.ullTotalPhys // 1024 // 1024)

    def _get_mem_banks(self):
        md = len(self.w.WIN32_PhysicalMemory())
        return str(md)

    def _get_mem_slots(self):
        n = 0
        for a in self.w.WIN32_PhysicalMemoryArray():
            n += a.MemoryDevices
        return str(n)

    def _get_os_vendor(self):
        return 'Microsoft'

    def _get_os_name(self):
        return 'Windows'

    def _get_os_release(self):
        v = sys.getwindowsversion()
        product = {
         1: 'Workstation',
         2: 'Domain Controller',
         3: 'Server',
        }
        s = platform.release()
        s = s.replace('Server', ' Server')
        s = s.replace('ServerR', ' Server R')
        s = s.replace('Workstation', ' Workstation')
        s += " %s" % v.service_pack
        return s

    def _get_os_kernel(self):
        v = sys.getwindowsversion()
        return ".".join(map(str, [v.major, v.minor, v.build]))

    def _get_os_arch(self):
        return platform.uname()[4]

    def _get_cpu_freq(self):
        for i in self.cpuinfo:
            cpuspeed = i.MaxClockSpeed
        return str(cpuspeed)

    def _get_cpu_cores(self):
        n = 0
        for p in self.cpuinfo:
	    try:
                cores = p.NumberOfCores
	    except:
                cores = 1
            n += cores
        return str(n)

    def _get_cpu_dies(self):
	s = set([])
        for p in self.cpuinfo:
	    s.add(p.SocketDesignation)
        n = len(s)
        return str(n)

    def _get_cpu_model(self):
        for i in self.cpuinfo:
            cputype = i.Name
        return cputype

    def _get_enclosure(self):
        for i in self.w.Win32_SystemEnclosure():
            name = i.Name
        return name

    def _get_serial(self):
        for i in self.w.Win32_ComputerSystemProduct():
            name = i.IdentifyingNumber
        return name

    def _get_model(self):
        for i in self.w.Win32_ComputerSystemProduct():
            name = i.Name
        return name

    def _get_hba(self):
        hbas = []
        self.di = diskInfo()
        for index, portwwn, host in self.di._get_fc_hbas():
            hbas.append((portwwn, 'fc'))
        return hbas

    def _get_targets(self):
        maps = []
        if not which('fcinfo'):
            print('  fcinfo is not installed')
            return []
        for index, portwwn, host in self.di._get_fc_hbas():
            cmd = ['fcinfo', '/mapping', '/ai:'+index]
            out, err, ret = justcall(cmd)
            if ret != 0:
                print('error executing', ' '.join(cmd), out, err, ret)
                continue
            for line in out.split('\n'):
                if not line.startswith('(x'):
                    continue
                l = line.split()
                if len(l) < 3:
                    continue
                tgtportwwn = l[2].strip(',').replace(':', '')
                if (portwwn, tgtportwwn) in maps:
                    continue
                maps.append((portwwn, tgtportwwn))
        return maps

0707010000bba0000081a400000000000000000000000154fc7bfb00001014000000660000000300000000000000000000002200000000root/opt/opensvc/lib/rcBrocade.py from rcUtilities import justcall, which
import rcExceptions as ex
import os
import ConfigParser
import telnetlib

pathlib = os.path.dirname(__file__)
pathbin = os.path.realpath(os.path.join(pathlib, '..', 'bin'))
pathetc = os.path.realpath(os.path.join(pathlib, '..', 'etc'))
pathtmp = os.path.realpath(os.path.join(pathlib, '..', 'tmp'))
if pathbin not in os.environ['PATH']:
    os.environ['PATH'] += ":"+pathbin

def brocadetelnetcmd(cmd, switch, username, password):
    tn = telnetlib.Telnet(switch)
    tn.read_until("login: ")
    tn.write(username + '\n')
    tn.read_until("Password: ")
    tn.write(password + '\n')
    tn.read_until("> ")
    tn.write(cmd + '\n')
    tn.write('exit\n')
    out = tn.read_all()
    return out, "", 0

def brocadecmd(cmd, switch, username, key):
    _cmd = ['ssh', '-l', username, '-i', key, switch, cmd]
    out, err, ret = justcall(_cmd)
    if "command not found" in err:
        # bogus firmware syntax
        _cmd = ['ssh', '-l', username, '-i', key, switch, 'bash --login -c '+cmd]
        out, err, ret = justcall(_cmd)
    if ret != 0:
        raise ex.excError("brocade command execution error")
    return out, err, ret

class Brocades(object):
    switchs = []

    def __init__(self, objects=[]):
        self.index = 0
        self.objects = objects
        if len(objects) > 0:
            self.filtering = True
        else:
            self.filtering = False
        cf = os.path.join(pathetc, "auth.conf")
        if not os.path.exists(cf):
            return
        conf = ConfigParser.RawConfigParser()
        conf.read(cf)
        m = []
        for s in conf.sections():
            if self.filtering and s not in self.objects:
                continue
            try:
                stype = conf.get(s, 'type')
            except:
                continue
            if stype != "brocade":
                continue
            name = s
            key = None
            password = None
            try:
                username = conf.get(s, 'username')
            except:
                print("no 'username' parameter in %s section %s"%(cf, s))
                continue
            try:
                key = conf.get(s, 'key')
            except:
                pass
            try:
                password = conf.get(s, 'password')
            except:
                pass
            if key is None and password is None:
                print("no 'key' nor 'password' parameter in %s section %s"%(cf, s))
                continue
            m.append([name, username, key, password])
        del(conf)
        for name, username, key, password in m:
            self.switchs.append(Brocade(name, username, key, password))

    def __iter__(self):
        return self

    def next(self):
        if self.index == len(self.switchs):
            raise StopIteration
        self.index += 1
        return self.switchs[self.index-1]

class Brocade(object):
    def __init__(self, name, username, key, password):
        self.name = name
        self.username = username
        self.password = password
        self.key = key
        self.keys = ['brocadeswitchshow', 'brocadensshow', 'brocadezoneshow']

    def brocadecmd(self, cmd):
        if self.key is not None:
            return brocadecmd(cmd, self.name, self.username, self.key)
        elif self.password is not None:
            return brocadetelnetcmd(cmd, self.name, self.username, self.password)
        else:
            raise Exception("ssh nor telnet method available")

    def get_brocadeswitchshow(self):
        cmd = 'switchshow'
        print("%s: %s"%(self.name, cmd))
        buff = self.brocadecmd(cmd)[0]
        return buff

    def get_brocadensshow(self):
        cmd = 'nsshow'
        print("%s: %s"%(self.name, cmd))
        buff = self.brocadecmd(cmd)[0]
        return buff

    def get_brocadezoneshow(self):
        cmd = 'zoneshow'
        print("%s: %s"%(self.name, cmd))
        buff = self.brocadecmd(cmd)[0]
        return buff

if __name__ == "__main__":
    o = Brocades()
    for brocade in o:
        print(brocade.get_brocadeswitchshow())
0707010000bba1000081a400000000000000000000000154fc7bfb00003118000000660000000300000000000000000000002000000000root/opt/opensvc/lib/rcBtrfs.py   #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from rcUtilities import justcall, vcall
import sys
import os
import logging
from rcGlobalEnv import rcEnv

class InitError(Exception):
     pass

class ExecError(Exception):
     pass

class ExistError(Exception):
     pass

class Btrfs(object):
    log = None
    #snapvol = ".osvcsnap"
    snapvol = ""

    def __init__(self, path=None, label=None, node=None, log=None):
        self.path = path
        self.label = label
        self.node = node

        if log is None:
            if Btrfs.log is None:
                Btrfs.log = logging.getLogger("BTRFS")
                Btrfs.log.addHandler(logging.StreamHandler(sys.stdout))
                Btrfs.log.setLevel(logging.INFO)
            self.log = Btrfs.log
        else:
            self.log = log

        if path is not None:
            if not self.dir_exists(path):
                raise InitError("path %s does not exist"%path)
            self.get_label_from_path(path)

        if self.label is None:
            raise InitError("failed to determine btrfs label")

        self.setup_rootvol()
        self.path = self.rootdir
        self.snapdir = os.path.join(self.rootdir, self.snapvol)
        self.snapdir = os.path.normpath(self.snapdir)

        self.get_subvols()

    def rmdir(self, path):
        cmd = ['rmdir', path]
        out, err, ret = self.justcall(cmd)
        if ret != 0:
            raise ExecError("error removing dir %s:\n%s"%(path,err))

    def dir_exists(self, path):
        cmd = ['test', '-d', path]
        out, err, ret = self.justcall(cmd)
        if ret > 1:
            raise ExecError("error joining remote node %s:\n%s"%(self.node, err))
        if ret == 1:
            return False
        return True

    def get_subvols(self):
        """
        ID 256 parent 5 top level 5 path btrfssvc
        ID 259 parent 256 top level 5 path btrfssvc/child
        ID 260 parent 5 top level 5 path btrfssvc@sent
        ID 261 parent 256 top level 5 path btrfssvc/child@sent
        ID 262 parent 5 top level 5 path btrfssvc@tosend
        ID 263 parent 256 top level 5 path btrfssvc/child@tosend
        ID 264 parent 5 top level 5 path subdir/vol
        ID 265 parent 256 top level 5 path btrfssvc/cross_mnt_snap
        """
        self.subvols = {}
        cmd = ['btrfs', 'subvol', 'list', '-p', self.path]
        out, err, ret = self.justcall(cmd)
        if ret != 0:
            raise InitError("error running btrfs subvol list %s:\n"%self.path+err)

        for line in out.split("\n"):
            if len(line) == 0:
                continue
            l = line.split()
            subvol = {}
            subvol['id'] = l[1]
            subvol['parent_id'] = l[3]
            subvol['top'] = l[6]
            subvol['path'] = line[line.index(" path ")+6:]
            self.subvols[subvol['id']] = subvol

    def subvol_delete(self, subvol=[], recursive=False):
        opts = []
        if recursive:
            opts.appendi('-R')

        if isinstance(subvol, list):
            subvols = subvol
        else:
            subvols = [subvol]

        # delete in descending depth order
        subvols.sort(reverse=True)

        cmd = []
        for subvol in subvols:
            if not self.has_subvol(subvol):
                continue
            cmd += ['btrfs', 'subvolume', 'delete'] + opts + [subvol, '&&']

        if len(cmd) == 0:
            return

        cmd = cmd[:-1]

        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ExecError()

    def get_subvols_in_path(self, path):
        self.get_subvols()
        head = self.path_to_subvol(path)
        subvols = [path]
        for subvol in self.subvols.values():
            if subvol['path'].startswith(head+'/'):
                subvols.append(self.rootdir+'/'+subvol['path'])
        return subvols

    def snapshot(self, origin, snap, readonly=False, recursive=False):
        if self.has_subvol(snap):
            raise ExistError("snapshot %s already exists"%snap)

        opts = []
        if recursive:
            opts.append('-R')
        if readonly:
            opts.append('-r')

        cmd = ['btrfs', 'subvolume', 'snapshot'] + opts + [origin, snap]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ExecError()

    def has_snapvol(self):
        return self.has_subvol(self.snapvol)

    def path_to_subvol(self, path):
        if path.startswith('/'):
            return path.replace(self.rootdir+'/', "")
        return path

    def has_subvol(self, subvol):
        # refresh subvol list
        self.get_subvols()

        subvol = self.path_to_subvol(subvol)
        for sub in self.subvols.values():
            if sub['path'] == subvol:
                return True
        return False

    def mount_snapvol(self):
        cmd = ['mount', '-t', 'btrfs', '-o', 'subvol='+self.snapvol, 'LABEL='+self.label, self.snapdir]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ExecError("error mounting %s subvol:\ncmd: %s\n%s"%(self.label,' '.join(cmd),err))

    def mount_rootvol(self):
        if self.is_mounted_subvol(self.rootdir):
            return
        cmd = ['mount', '-t', 'btrfs', '-o', 'subvolid=0', 'LABEL="%s"'%self.label, self.rootdir]
        out, err, ret = self.justcall(cmd)
        if ret != 0:
            raise ExecError("error mounting %s btrfs:\ncmd: %s\n%s"%(self.label,' '.join(cmd),err))

    def create_snapvol(self):
        error = False

        import tempfile
        tmpdir = tempfile.mktemp()
        cmd = ['mkdir', '-p', tmpdir]
        out, err, ret = self.justcall(cmd)
        if ret != 0:
            raise ExecError("error creating dir %s:\n"%tmpdir+err)

        cmd = ['mount', '-t', 'btrfs', '-o', 'subvolid=0', 'LABEL="%s"'%self.label, tmpdir]
        out, err, ret = self.justcall(cmd)
        if ret != 0:
            self.rmdir(tmpdir)
            raise ExecError("error mounting %s btrfs:\ncmd: %s\n%s"%(self.label,' '.join(cmd),err))

        try:
            self.create_subvol(os.path.join(tmpdir, self.snapvol))
        except:
            error = True

        cmd = ['umount', tmpdir]
        out, err, ret = self.justcall(cmd)
        if ret != 0:
            raise ExecError("error umounting %s btrfs:\n"%self.label+err)
        self.rmdir(tmpdir)
        if error:
            raise ExecError("failed to create %s"%self.snapvol)

    def vcall(self, cmd):
        if self.node is not None:
            cmd = [' '.join(cmd)]
            cmd = rcEnv.rsh.split() + [self.node] + cmd

        return vcall(cmd, log=self.log)

    def justcall(self, cmd):
        if self.node is not None:
            cmd = [' '.join(cmd)]
            cmd = rcEnv.rsh.split() + [self.node] + cmd
        return justcall(cmd)

    def create_subvol(self, path):
        cmd = ['btrfs', 'subvol', 'create', path]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ExecError("error creating %s subvol"%path)

    def setup_snap_subvol(self):
        # unused for now


        if not self.dir_exists(self.snapdir):
            cmd = ['mkdir', '-p', self.snapdir]
            ret, out, err = self.vcall(cmd)
            if ret != 0:
                raise ExecError("error creating dir %s:\n"%self.snapdir+err)

        if not self.has_snapvol():
            self.create_snapvol()
            self.mount_snapvol()

        try:
            o = Btrfs(self.snapdir)
        except InitError:
            self.mount_snapvol()
            o = Btrfs(self.snapdir)

        if o.label != self.label:
            raise ExecError("wrong fs mounted in %s: %s"%(self.snapdir, o.label))

        # verify this is the right subvol (missing: path->subvol name fn)

    def setup_rootvol(self):
        pathsvc = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
        pathvar = os.path.join(pathsvc, 'var')
        self.rootdir = os.path.join(pathvar, 'btrfs', self.label)

        if not self.dir_exists(self.rootdir):
            cmd = ['mkdir', '-p', self.rootdir]
            ret, out, err = self.vcall(cmd)
            if ret != 0:
                raise ExecError("error creating dir %s:\n"%self.rootdir+err)

        self.mount_rootvol()

    def setup_snap(self):
        if not self.has_snapvol():
            self.create_subvol(self.snapdir)
        try:
            o = Btrfs(self.snapdir)
        except InitError:
            self.mount_snapvol()
            o = Btrfs(self.snapdir)

        if o.label != self.label:
            raise ExecError("wrong fs mounted in %s: %s"%(self.snapdir, o.label))

        # verify this is the right subvol (missing: path->subvol name fn)

    def get_mounts(self):
        """
        /dev/vdb on /data type btrfs (rw) [data]
        """
        cmd = ['mount', '-t', 'btrfs', '-l']
        out, err, ret = self.justcall(cmd)
        if ret != 0:
            raise InitError("error running %s:\n"%' '.join(cmd)+err)
        mounts = {}
        for line in out.split("\n"):
            if len(line) == 0 or " on " not in line or " type btrfs " not in line:
                continue
            mntpt = line[line.index(" on ")+4:line.index(" type btrfs ")]
            if '[' in line:
                l = line.split('[')
                label = l[-1].strip(']')
            else:
                label = self.get_label(mntpt)
            mounts[mntpt] = label
        return mounts

    def get_label(self, mntpt):
        cmd = ['btrfs', 'fi', 'label', mntpt]
        out, err, ret = self.justcall(cmd)
        if ret != 0:
            raise excError("error running %s:\n"%' '.join(cmd)+err)
        return out.strip('\n')

    def is_mounted_subvol(self, path):
        path = path.rstrip('/')
        for mntpt, label in self.get_mounts().items():
            if mntpt == path and label == self.label:
                return True
        return False

    def get_label_from_path(self, path):
        path = path.rstrip('/')

        mounts = self.get_mounts()
        l = path.split('/')
        while len(l) > 0:
            m = '/'.join(l)
            if m in mounts:
                self.label = mounts[m]
                return
            l = l[:-1]

        raise InitError("could not get label from path %s"%path)
        
    def parse_fi_show(self):
        """
        Label: 'data'  uuid: 0d05d0b9-ffab-4ab8-b923-15a38ec806d5
                Total devices 2 FS bytes used 48.92MB
                devid    2 size 5.00GB used 1.51GB path /dev/vdc
                devid    1 size 5.00GB used 1.53GB path /dev/vdb
        """
        cmd = ['btrfs', 'fi', 'show', path]
        out, err, ret = self.justcall(cmd)
        if ret != 0:
            raise InitError("error running btrfs fi show:\n"+err)

        self.devs = {}
        for line in out.split("\n"):
            if "Label:" in line:
                l = line.split("'")
                if len(l) != 2:
                    raise InitError("unexpected line format: "+line)
                label = l[1]

                l = line.split()
                uuid = l[-1]
            elif line.strip().startswith("devid"):
                l = line.split()
                self.devs[l[-1]] = (label, uuid)

    def get_transid(self, path):
        cmd = ['btrfs', 'subvolume', 'find-new', path, '-1']
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ExecError("can't fetch %s transid:\n%s"%(path, err))
        return out.split()[-1]

    def __str__(self):
        s = "label: %s\n" % self.label
        s += "subvolumes:\n"
        for sub in self.subvols.values():
            s += "id: %s parent_id: %s top: %s path: %s\n"%(sub['id'], sub['parent_id'], sub['top'], sub['path'])
        return s

if __name__ == "__main__":
    o = Btrfs(label=sys.argv[1], node="deb2")
    print(o)
    #o.setup_snap()

0707010000bba2000081a400000000000000000000000154fc7bfb000001bc000000660000000300000000000000000000002000000000root/opt/opensvc/lib/rcCloud.py   import socket
import rcExceptions as ex

class Cloud(object):
    def __init__(self, s, auth):
        self.cid = s
        self.auth = auth

    def list_svcnames(self):
         print("todo")
         return []

    def list_nodes(self):
        try:
            nodes = self.driver.list_nodes()
        except socket.error as e:
            raise ex.excError("error connecting to %s cloud url (%s)"%(self.cid, str(e)))
        return nodes

0707010000bba3000081a400000000000000000000000154fc7bfb00000503000000660000000300000000000000000000002600000000root/opt/opensvc/lib/rcCloudAmazon.py import rcCloud
import rcExceptions as ex
import socket

try:
    from libcloud.compute.types import Provider
    from libcloud.compute.providers import get_driver
    import libcloud.security
except ImportError:
    raise ex.excInitError("apache-libcloud module must be installed")

class Cloud(rcCloud.Cloud):
    mode = 'amazon'

    def __init__(self, s, auth):
        rcCloud.Cloud.__init__(self, s, auth)
        if 'access_key_id' not in auth:
            raise ex.excInitError("option 'access_key_id' is mandatory in amazon section")
        if 'provider' not in auth:
            raise ex.excInitError("option 'provider' is mandatory in amazon section")
        if 'secret_key' not in auth:
            raise ex.excInitError("option 'secret_key' is mandatory in amazon section")
        o = get_driver(auth['provider'])
        self.driver = o(auth['access_key_id'], auth['secret_key'])
        if 'proxy' in auth:
            self.driver.connection.set_http_proxy(proxy_url=auth['proxy'])

    def app_id(self, svcname):
        return svcname.rstrip(self.auth['manager']).split('.')[-2]

    def cloud_id(self):
        return self.auth['provider']

    def app_cloud_id(self):
        return self.cloud_id()

    def list_svcnames(self):
        l = []
        return l

 0707010000bba4000081a400000000000000000000000154fc7bfb00000548000000660000000300000000000000000000002500000000root/opt/opensvc/lib/rcCloudGandi.py  import rcCloud
import rcExceptions as ex
import socket

try:
    from libcloud.compute.types import Provider
    from libcloud.compute.providers import get_driver
    import libcloud.security
except ImportError:
    raise ex.excInitError("apache-libcloud module must be installed")

class Cloud(rcCloud.Cloud):
    mode = 'gandi'

    def __init__(self, s, auth):
        rcCloud.Cloud.__init__(self, s, auth)
        if 'key' not in auth:
            raise ex.excInitError("option 'key' is mandatory in gandi section")
        gandi = get_driver(Provider.GANDI)
        try:
            self.driver = gandi(auth['key'])
        except Exception as e:
            raise ex.excInitError("error login to gandi cloud %s: %s"%(s, str(e)))

    def app_id(self):
        return ''

    def cloud_id(self):
        return mode

    def app_cloud_id(self):
        return mode

    def list_svcnames(self):
        l = []
        _id = self.app_cloud_id()
        try:
            vapps = self.driver.list_nodes()
        except socket.error as e:
            raise ex.excExecError("error connecting to %s cloud manager"%s)
        for vapp in vapps:
            __id = '.'.join((vapp.name, _id))
            for vm in vapp.extra['vms']:
                svcname = '.'.join((vm['name'], __id))
                l.append((vm['name'], svcname))
        return l

0707010000bba5000081a400000000000000000000000154fc7bfb00000868000000660000000300000000000000000000002900000000root/opt/opensvc/lib/rcCloudOpenstack.py  import rcCloud
import rcExceptions as ex
import socket

try:
    from libcloud.compute.types import Provider
    from libcloud.compute.providers import get_driver
    import libcloud.security
except ImportError:
    raise ex.excInitError("apache-libcloud module must be installed")

class Cloud(rcCloud.Cloud):
    mode = 'openstack'

    def __init__(self, s, auth):
        rcCloud.Cloud.__init__(self, s, auth)
        kwargs = {}

        if 'username' not in auth:
            raise ex.excInitError("option 'username' is mandatory in %s section"%self.mode)

        if 'password' not in auth:
            raise ex.excInitError("option 'password' is mandatory in %s section"%self.mode)

        if 'url' not in auth:
            raise ex.excInitError("option 'url' is mandatory in %s section"%self.mode)

        kwargs['ex_force_auth_url'] = auth['url']

        if 'tenant' in auth:
            self.tenant_name = auth['tenant']
            kwargs['ex_tenant_name'] = auth['tenant']
        else:
            self.tenant_name = None

        if 'version' in auth:
            kwargs['ex_force_auth_version'] = auth['version']
        else:
            kwargs['ex_force_auth_version'] = '2.0_password'

        if 'service_name' in auth:
            kwargs['ex_force_service_name'] = auth['service_name']

        if 'verify_ssl_cert' in auth and not auth['verify_ssl_cert']:
            libcloud.security.VERIFY_SSL_CERT = False

        openstack = get_driver(Provider.OPENSTACK)
        self.driver = openstack( auth['username'], auth['password'], **kwargs)

    def app_id(self, svcname=None):
        return self.tenant_name

    def cloud_id(self):
        return self.auth['url'].split("/")[2].split(':')[0]

    def app_cloud_id(self):
        _id = []
        app_id = self.app_id()
        if app_id is not None:
            _id.append(app_id)
        _id.append(self.cloud_id())
        return '.'.join(_id)

    def list_svcnames(self):
        l = []
        _id = self.app_cloud_id()
        for node in self.list_nodes():
            svcname = '.'.join((node.name, _id))
            l.append((node.name, svcname))
        return l

0707010000bba6000081a400000000000000000000000154fc7bfb0000076c000000660000000300000000000000000000002600000000root/opt/opensvc/lib/rcCloudVcloud.py import rcCloud
import rcExceptions as ex
import socket

try:
    from libcloud.compute.types import Provider
    from libcloud.compute.providers import get_driver
    import libcloud.security
except ImportError:
    raise ex.excInitError("apache-libcloud module must be installed")

class Cloud(rcCloud.Cloud):
    mode = 'vcloud'

    def __init__(self, s, auth):
        rcCloud.Cloud.__init__(self, s, auth)
        if 'username' not in auth:
            raise ex.excInitError("option 'username' is mandatory in vcloud section")
        if 'password' not in auth:
            raise ex.excInitError("option 'password' is mandatory in vcloud section")
        if 'manager' not in auth:
            raise ex.excInitError("option 'manager' is mandatory in vcloud section")
        if 'api_version' not in auth:
            auth['api_version'] = '1.5'
        vcloud = get_driver(Provider.VCLOUD)
        self.driver = vcloud(auth['username'], auth['password'],
                             host=auth['manager'], api_version=auth['api_version'])

    def app_id(self, svcname):
        return svcname.rstrip(self.auth['manager']).split('.')[-2]

    def cloud_id(self):
        return self.auth['manager']

    def app_cloud_id(self):
        _id = []
        l = self.auth['username'].split('@')
        if len(l) == 2:
            _id.append(l[1])
        _id.append(self.auth['manager'])
        return '.'.join(_id)

    def list_svcnames(self):
        l = []
        _id = self.app_cloud_id()
        try:
            vapps = self.driver.list_nodes()
        except socket.error as e:
            raise ex.excExecError("error connecting to %s cloud manager"%s)
        for vapp in vapps:
            __id = '.'.join((vapp.name, _id))
            for vm in vapp.extra['vms']:
                svcname = '.'.join((vm['name'], __id))
                l.append((vm['name'], svcname))
        return l

0707010000bba7000081a400000000000000000000000154fc7bfb00000949000000660000000300000000000000000000002800000000root/opt/opensvc/lib/rcCommandWorker.py   #!/opt/opensvc/bin/python

import os
from rcGlobalEnv import rcEnv

try:
    from multiprocessing import Queue, Process
    from Queue import Empty
    mp = True
    if rcEnv.sysname == "Windows":
        import sys
        from multiprocessing import set_executable
        set_executable(os.path.join(sys.exec_prefix, 'pythonw.exe'))
except:
    mp = False

from rcUtilities import justcall
import rcExceptions as ex

pathosvc = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))

import logging
import logging.handlers
logfile = os.path.join(pathosvc, 'log', 'cmdworker.log')
fileformatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
filehandler = logging.handlers.RotatingFileHandler(os.path.join(logfile),
                                                   maxBytes=5242880,
                                                   backupCount=5)
filehandler.setFormatter(fileformatter)
log = logging.getLogger("cmdworker")
log.addHandler(filehandler)
log.setLevel(logging.DEBUG)
log.debug("logger setup")

def worker(q):
    try:
        _worker(q)
    except ex.excSignal:
         log.info("interrupted by signal")

def _worker(q):
    log.debug("worker started")
    cmd = "foo"
    while cmd is not None:
        cmd = q.get()
        if cmd is None:
            log.debug("shutdown (poison pill)")
            break
        log.info("call: %s", ' '.join(cmd))
        out, err, ret = justcall(cmd)
        log.info("ret: %d", ret)
        continue
    log.debug("shutdown")

class CommandWorker(object):
    def __init__(self, name=""):
        self.q = None
        self.p = None
        self.name = "cmdworker"
        if name:
            self.name += "_"+name

    def start_worker(self):
        if not mp:
            return
        self.q = Queue()
        self.p = Process(target=worker, name=self.name, args=(self.q,))
        self.p.start()

    def enqueue(self, cmd):
        if not mp:
            out, err, ret = justcall(cmd)
            log.info("ret: %d", ret)
            return
        if self.p is None:
            self.start_worker()
        self.q.put(cmd, block=True)

    def stop_worker(self):
        if not mp:
            return
        if self.p is None or not self.p.is_alive():
            return
        log.debug("give poison pill to worker")
        self.enqueue(None)
        self.p.join()

   0707010000bba8000081a400000000000000000000000154fc7bfb000013e5000000660000000300000000000000000000002900000000root/opt/opensvc/lib/rcContainerLinux.py  import os
import rcExceptions as ex
from rcUtilities import justcall

default_cgroup_mntpt = '/cgroup'

def get_cgroup_mntpt():
    (out,err,ret) = justcall(['mount'])
    if ret != 0:
        return None
    for line in out.split('\n'):
        l = line.split()
        if len(l) < 6:
            continue
        if l[4] == 'cgroup':
            return l[2]
    return None

def cgroup_capable(res):
    kconf = os.path.join(os.sep, 'lib', 'modules',
                         os.uname()[2], 'build', '.config')
    if not os.path.exists(kconf):
        kconf = os.path.join(os.sep, 'boot', 'config-'+os.uname()[2])
    if not os.path.exists(kconf):
        res.log.info("can not detect if system supports containerization")
        return False
    with open(kconf, 'r') as f:
        for line in f.readlines():
            l = line.split('=')
            if len(l) != 2:
                continue
            if l[0] == 'CONFIG_CGROUPS' and l[1] == 'y\n':
                return True
    res.log.info("system does not support containerization")
    return False

def set_cgroup(res, name, value, force=False):
    if value is None:
        return
    if not force and get_cgroup(res, name).strip() == value:
        return
    path = os.path.join(res.cgroup, name)
    if not os.path.exists(path):
        res.log.error("can not find %s"%path)
        raise ex.excError
    res.log.info('/bin/echo %s > %s'%(value, path))
    with open(path, 'w') as f:
        f.write(str(value))

def get_cgroup(res, name):
    path = os.path.join(res.cgroup, name)
    if not os.path.exists(path):
        res.log.error("can not find %s"%path)
        raise ex.excError
    with open(path, 'r') as f:
        buff = f.read()
    return buff

def set_mem_cgroup(res):
    if hasattr(res.svc, 'container_mem_limit'):
        mem_limit = int(res.svc.container_mem_limit)
    else:
        mem_limit = None

    if hasattr(res.svc, 'container_vmem_limit'):
        vmem_limit = int(res.svc.container_vmem_limit)
    else:
        vmem_limit = None

    #
    # validate memory limits sanity and order adequately the resize
    # depending on increase/decrease of limits
    #
    if mem_limit is not None and vmem_limit is not None:
        if mem_limit > vmem_limit:
            res.log.error("container_vmem_limit must be greater than container_mem_limit")
            raise ex.excError
        cur_vmem_limit = int(get_cgroup(res, 'memory.memsw.limit_in_bytes'))
        if mem_limit > cur_vmem_limit:
            set_cgroup(res, 'memory.memsw.limit_in_bytes', vmem_limit)
            set_cgroup(res, 'memory.limit_in_bytes', mem_limit)
        else:
            set_cgroup(res, 'memory.limit_in_bytes', mem_limit)
            set_cgroup(res, 'memory.memsw.limit_in_bytes', vmem_limit)
    elif mem_limit is not None:
        cur_vmem_limit = int(get_cgroup(res, 'memory.memsw.limit_in_bytes'))
        if mem_limit > cur_vmem_limit:
            res.log.error("container_mem_limit must not be greater than current container_vmem_limit (%d)"%cur_vmem_limit)
            raise ex.excError
        set_cgroup(res, 'memory.limit_in_bytes', mem_limit)
    elif vmem_limit is not None:
        cur_mem_limit = int(get_cgroup(res, 'memory.limit_in_bytes'))
        if vmem_limit < cur_mem_limit:
            res.log.error("container_vmem_limit must not be lesser than current container_mem_limit (%d)"%cur_mem_limit)
            raise ex.excError
        set_cgroup(res, 'memory.memsw.limit_in_bytes', vmem_limit)

def containerize(res):
    if res.svc.svcmode == 'lxc':
        return
    if not cgroup_capable(res):
        return

    cgroup_mntpt = get_cgroup_mntpt()
    if cgroup_mntpt is None:
        cgroup_mntpt = default_cgroup_mntpt
        if not os.path.exists(cgroup_mntpt):
            res.log.info('mkdir %s'%cgroup_mntpt)
            os.makedirs(cgroup_mntpt)
        (ret, out, err) = res.vcall(['mount', '-t', 'cgroup', 'none', cgroup_mntpt])
        if ret != 0:
            raise ex.excError

    res.cgroup = os.path.join(cgroup_mntpt, res.svc.svcname)
    if not os.path.exists(res.cgroup):
        os.makedirs(res.cgroup)

    if hasattr(res.svc, 'container_cpus'):
        v_cpus = str(res.svc.container_cpus)
    else:
        # default to all cpus
        import multiprocessing
        v_cpus = '0-%d'%(multiprocessing.cpu_count()-1)

    if hasattr(res.svc, 'container_mems'):
        v_mems = str(res.svc.container_mems)
    else:
        # default to all mems
        v_mems = '0'

    if hasattr(res.svc, 'container_cpu_share'):
        cpu_share = res.svc.container_cpu_share
    else:
        cpu_share = None

    pid = os.getpid()

    try:
        set_cgroup(res, 'cpuset.cpus', v_cpus)
        set_cgroup(res, 'cpu.shares', cpu_share)
        set_cgroup(res, 'cpuset.mems', v_mems)
        set_mem_cgroup(res)
        set_cgroup(res, 'tasks', pid, force=True)
    except:
        res.log.error("containerization in '%s' cgroup failed"%res.svc.svcname)
        raise ex.excError
    res.log.info("container '%s' settings: cpu[%s], mem[%s]"%(res.svc.svcname, v_cpus, v_mems))
   0707010000bba9000081a400000000000000000000000154fc7bfb000025ef000000660000000300000000000000000000001e00000000root/opt/opensvc/lib/rcDcs.py from rcUtilities import justcall, which
from xml.etree.ElementTree import XML, fromstring
import rcExceptions as ex
import os
import ConfigParser
import uuid

pathlib = os.path.dirname(__file__)
pathbin = os.path.realpath(os.path.join(pathlib, '..', 'bin'))
pathetc = os.path.realpath(os.path.join(pathlib, '..', 'etc'))
pathtmp = os.path.realpath(os.path.join(pathlib, '..', 'tmp'))
if pathbin not in os.environ['PATH']:
    os.environ['PATH'] += ":"+pathbin

def dcscmd(cmd, manager, username, password, dcs=None, conn=None):
    if conn is None:
        conn = uuid.uuid1().hex

    if len(cmd) == 0:
        return

    _cmd = ['ssh', manager]
    if dcs is not None:
        _cmd += ["connect-dcsserver -server %s -username %s -password %s -connection %s ; "%(dcs, username, password, conn)+\
                 cmd+ " ; disconnect-dcsserver -connection %s"%conn]
    else:
        _cmd += [cmd]
    out, err, ret = justcall(_cmd)
    if "ErrorId" in err:
        print(_cmd)
        print(out)
        raise ex.excError("dcs command execution error")
    try:
        out = out.decode("latin1").encode("utf8")
    except:
        pass
    return out, err, ret

class Dcss(object):
    arrays = []

    def __init__(self, objects=[]):
        self.objects = objects
        if len(objects) > 0:
            self.filtering = True
        else:
            self.filtering = False
        self.index = 0
        cf = os.path.join(pathetc, "auth.conf")
        if not os.path.exists(cf):
            return
        conf = ConfigParser.RawConfigParser()
        conf.read(cf)
        m = []
        for s in conf.sections():
            try:
                stype = conf.get(s, 'type')
            except:
                continue
            if stype != "datacore":
                continue
            try:
                manager = s
                dcs = conf.get(s, 'dcs').split()
                username = conf.get(s, 'username')
                password = conf.get(s, 'password')
                m += [(manager, dcs, username, password)]
            except:
                print("error parsing section", s)
                pass
        del(conf)
        done = []
        for manager, dcs, username, password in m:
            for name in dcs:
                if self.filtering and name not in self.objects:
                    continue
                if name in done:
                    continue
                self.arrays.append(Dcs(name, manager, username, password))
                done.append(name)

    def __iter__(self):
        return self

    def next(self):
        if self.index == len(self.arrays):
            raise StopIteration
        self.index += 1
        return self.arrays[self.index-1]

    def get_dcs(self, domain):
        for dcs in self.arrays:
            _domain = dcs.get_domain()
            if _domain == domain:
                return dcs
        return None

class Dcs(object):
    def __init__(self, name, manager, username, password, conn=None):
        self.name = name
        self.manager = manager
        self.username = username
        self.password = password
        self.conn = conn
        if conn is None:
            self.conn = uuid.uuid1().hex
        self.keys = ['dcsservergroup',
                     'dcsserver',
                     'dcspool',
                     'dcspoolperf',
                     'dcslogicaldisk',
                     'dcslogicaldiskperf',
                     'dcsvirtualdisk',
                     'dcsphysicaldisk',
                     'dcsdiskpath',
                     'dcsport',
                     'dcspoolmember']

    def get_domain(self):
        if hasattr(self, 'domain'):
            return self.domain
        buff = self.get_dcsservergroup()
        for line in buff.split('\n'):
            if not line.startswith('Alias'):
                continue
            self.domain = line.split(': ')[-1].strip()
            break
        if hasattr(self, 'domain'):
            return self.domain
        return "unknown"

    def dcscmd(self, cmd):
        return dcscmd(cmd, self.manager, self.username, self.password, dcs=self.name, conn=self.conn)

    def get_dcsservergroup(self):
        cmd = 'get-dcsservergroup -connection %s'%self.conn
        print("%s: %s"%(self.name, cmd))
        buff = self.dcscmd(cmd)[0]
        return buff

    def get_dcsserver(self):
        cmd = 'get-dcsserver -connection %s'%self.conn
        print("%s: %s"%(self.name, cmd))
        buff = self.dcscmd(cmd)[0]
        return buff

    def get_dcspool(self):
        cmd = 'get-dcspool -connection %s'%self.conn
        print("%s: %s"%(self.name, cmd))
        buff = self.dcscmd(cmd)[0]
        return buff

    def get_dcslogicaldisk(self):
        cmd = 'get-dcslogicaldisk -connection %s'%self.conn
        print("%s: %s"%(self.name, cmd))
        buff = self.dcscmd(cmd)[0]
        return buff

    def get_dcsvirtualdisk(self):
        cmd = 'get-dcsvirtualdisk -connection %s'%self.conn
        print("%s: %s"%(self.name, cmd))
        buff = self.dcscmd(cmd)[0]
        return buff

    def get_dcsphysicaldisk(self):
        cmd = 'get-dcsphysicaldisk -connection %s'%self.conn
        print("%s: %s"%(self.name, cmd))
        buff = self.dcscmd(cmd)[0]
        return buff

    def get_dcsdiskpath(self):
        cmd = 'get-dcsdiskpath -connection %s'%self.conn
        print("%s: %s"%(self.name, cmd))
        buff = self.dcscmd(cmd)[0]
        return buff

    def get_dcspoolmember(self):
        cmd = 'get-dcspoolmember -connection %s'%self.conn
        print("%s: %s"%(self.name, cmd))
        buff = self.dcscmd(cmd)[0]
        return buff

    def get_dcspoolperf(self):
        cmd = 'get-dcspool -connection %s | get-dcsperformancecounter -connection %s'%(self.conn, self.conn)
        print("%s: %s"%(self.name, cmd))
        buff = self.dcscmd(cmd)[0]
        return buff

    def get_dcslogicaldiskperf(self):
        cmd = 'get-dcslogicaldisk -connection %s | get-dcsperformancecounter -connection %s'%(self.conn, self.conn)
        print("%s: %s"%(self.name, cmd))
        buff = self.dcscmd(cmd)[0]
        return buff

    def get_dcsport(self):
        cmd = 'get-dcsport -connection %s'%self.conn
        print("%s: %s"%(self.name, cmd))
        buff = self.dcscmd(cmd)[0]
        return buff

    def add_vdisk(self, data):
        if 'disk_name' not in data:
            raise ex.excError("'disk_name' key is mandatory")
        if 'size' not in data:
            raise ex.excError("'size' key is mandatory")
        if 'paths' not in data:
            raise ex.excError("'paths' key is mandatory")

        data['disk_name'] = data['disk_name'] + '.1'
        l = data['paths'].split(',')
        paths = []
        for path in l:
            if 'iqn' in path:
                c, s = path.split('-iqn')
                s = 'iqn' + s
                paths.append((c, s))
            elif '-' in path:
                c, s = path.split('-')
                paths.append((c, s))
        if len(paths) == 0:
            raise ex.excError("no initiator to present to")

        pools = data['dg_name'].split(',')
        if len(pools) == 2:
            _pool1 = pools[0].split(':')
            _pool2 = pools[1].split(':')
            if len(_pool1) != 2 or len(_pool2) != 2:
                raise ex.excError("'dg_name' value is misformatted")
            d = {
              'disk_name': data['disk_name'],
              'size': data['size'],
              'sds1': _pool1[0],
              'sds2': _pool2[0],
              'pool1': _pool1[1],
              'pool2': _pool2[1],
              'conn': self.conn,
            }
            cmd = """$v = Add-DcsVirtualDisk -connection %(conn)s -Name "%(disk_name)s" -Size %(size)dGB  -EnableRedundancy -FirstServer %(sds1)s -FirstPool "%(pool1)s" -SecondServer %(sds2)s -SecondPool "%(pool2)s" ;""" % d
        elif len(pools) == 1:
            _pool1 = pools[0].split(':')
            if len(_pool1) != 2:
                raise ex.excError("'dg_name' value is misformatted")
            d = {
              'disk_name': data['disk_name'],
              'size': data['size'],
              'sds1': _pool1[0],
              'pool1': _pool1[1],
              'conn': self.conn,
            }
            cmd = """$v = Add-DcsVirtualDisk -connection %(conn)s -Name "%(disk_name)s" -Size %(size)dGB -Server %(sds1)s -Pool "%(pool1)s" ;""" % d
        else:
            raise ex.excError("'dg_name' value is misformatted")
        for machine in self.get_machines(map(lambda x: x[0], paths)):
            cmd += " $v | Serve-DcsVirtualDisk -connection %s -Machine %s -EnableRedundancy ;"""%(self.conn, machine)
        print(cmd)
        out, err, ret = self.dcscmd(cmd)

    def get_machines(self, ids):
        for i, id in enumerate(ids):
            if 'iqn' in id or ('-' in id and len(id) == 16):
                # iscsi or already in correct format
                continue
            # convert to dcs portname format
            id = list(id.upper())
            for j in (14, 12, 10, 8, 6, 4, 2):
                id.insert(j, '-')
            id = ''.join(id)
            ids[i] = id
        if not hasattr(self, "buff_dcsport"):
            self.buff_dcsport = self.get_dcsport()
        machines = set([])
        for line in self.buff_dcsport.split('\n'):
            if line.startswith('HostId'):
                hostid = line.split(': ')[-1].strip()
            elif line.startswith('Id'):
                id = line.split(': ')[-1].strip()
                if id in ids:
                    machines.add(hostid)
        return machines

if __name__ == "__main__":
    o = Dcss()
    for dcs in o:
        print(dcs.get_dcsserver())
 0707010000bbaa000081a400000000000000000000000154fc7bfb000024d7000000660000000300000000000000000000002200000000root/opt/opensvc/lib/rcDevTree.py """
Top devices are bare disks or multipath paths.

Bottom devices are formatted devices or devices given to
applications like raw database devices.

A relation describes a parent-child link. A 'used' size can
be arbitrarily set on a relation : DevRelation.set_used()

A logical volume lv0 with segments on pv1 pv2 has two parent
relations : lv0-pv1 and lv0-pv2

"""
try:
    from hashlib import md5
    def hash(s):
        o = md5()
        o.update(s)
        return o.hexdigest()
except:
    from rcMd5 import md5
    def hash(s):
        return md5(s).digest().encode('hex')

class DevRelation(object):
    def __init__(self, parent, child, used=0):
        self.child = child
        self.parent = parent
        self.used = used
        self.used_set = False

    def set_used(self, used):
        self.used_set = True
        self.used = used
        
    def get_used(self, used):
        #
        # logical volumes and concatset need to set explicitly
        # the 'used' size the child consumes on the parent.
        #
        if self.used_set:
            return self.used
        child = self.tree.get_dev(self.child)
        if used == 0:
            used = child.size
        if child.devtype in ("multipath", "linear", "partition"):
            return used
        elif child.devtype in ("raid0"):
            n = len(child.parents)
            return used/n
        elif child.devtype in ("raid1", "raid10"):
            n = len(child.parents)
            return used*2/n
        elif child.devtype in ("raid5"):
            n = len(child.parents)
            return used/(n-1)
        elif child.devtype in ("raid6"):
            n = len(child.parents)
            return used/(n-2)
        raise Exception("unknown devtype %s for %s"%(child.devtype, child.devname))

    def get_size(self, chain):
        if self.used_set:
            return self.used
        if len(chain) < 2:
            self.used = self.tree.get_dev(chain[-1].child).size
        else:
            self.used = chain[-2].used
        return self.used

class Dev(object):
    def __init__(self, devname, size, devtype):
        self.devname = devname
        self.devpath = []
        self.alias = devname
        self.size = size
        self.devtype = devtype

        # list of relations
        self.parents = []
        self.children = []

    def __iadd__(self, o):
        pass

    def set_alias(self, alias):
        self.alias = alias

    def get_dev(self, devname):
        return self.tree.get_dev(devname)

    def get_size(self):
        return self.size

    def set_devtype(self, devtype):
        self.devtype = devtype

    def set_devpath(self, devpath):
        self.devpath.append(devpath)

    def print_dev(self, level=0, relation=None):
        if relation is None:
            parent_size = self.size
        else:
            parent_size = self.get_dev(relation.parent).get_size()
        if parent_size == 0:
            pct = 0
        else:
            pct = 100*self.size//parent_size
        s = "%s %s %d %d%%\n" % (self.alias, self.devtype, self.size, pct)
        for r in self.children:
            d = self.get_dev(r.child)
            for i in range(level+1):
                s += " "
            if d is None:
                s += "unknown (dev %s not added)\n"%r.child
            else:
                s += d.print_dev(level=level+1, relation=r)
        return s

    def get_child(self, devname):
        for r in self.children:
            if r.parent == devname:
                return r
        return None

    def get_parent(self, devname):
        for r in self.parents:
            if r.child == devname:
                return r
        return None

    def add_child(self, devname, size=0, devtype=None):
        r = self.get_child(devname)
        if r is None:
            r = self.tree.get_relation(self.devname, devname)
            if r is None:
                r = DevRelation(parent=self.devname, child=devname, used=size)
                r.tree = self.tree
            self.children.append(r)
        self.tree.add_dev(devname, size, devtype)
        return r

    def add_parent(self, devname, size=0, devtype=None):
        r = self.get_parent(devname)
        if r is None:
            r = self.tree.get_relation(devname, self.devname)
            if r is None:
                r = DevRelation(parent=devname, child=self.devname, used=size)
                r.tree = self.tree
            self.parents.append(r)
        self.tree.add_dev(devname, size, devtype)
        return r

    def is_parent(self, devname):
        for r in self.children:
            if r.child == devname:
                return True
            d = self.get_dev(r.child)
            if d.is_parent(devname):
                return True
        return False

    def get_top_devs(self):
        if len(self.parents) == 0:
            return set([self])
        d = set([])
        for parent in self.parents:
            dev = self.get_dev(parent.parent)
            d |= dev.get_top_devs()
        return d

    def get_top_devs_chain(self, chain=[]):
        if len(self.parents) == 0:
            return [[self, chain]]
        d = []
        for parent in self.parents:
            dev = self.get_dev(parent.parent)
            d += dev.get_top_devs_chain(chain+[parent])
        return d

    def print_dev_bottom_up(self, level=0, chain=[]):
        if len(chain) == 0:
            used = self.size
        else:
            used = chain[-1].get_size(chain)
        s = ""
        for i in range(level+1):
            s += " "
        s += "%s %s %d %d"%(self.alias, self.devtype, self.size, used)
        s += " %s"%self.devpath
        print(s)
        for parent in self.parents:
            dev = self.get_dev(parent.parent)
            #print(map(lambda x: (x.parent, x.child, x.used, x.get_size(chain+[parent]), x.used), chain+[parent]))
            dev.print_dev_bottom_up(level+1, chain+[parent])

class DevTree(object):
    def __init__(self):
        self.dev = {}

        # root node of the relation tree
        self.root = []

    def __iadd__(self, o):
        if isinstance(o, Dev):
            o.tree = self
            self.dev[o.devname] = o
            if not self.has_relations(o.devname):
                r = DevRelation(parent=None, child=o.devname, used=o.size)
                r.tree = self
                self.root.append(r)
        return self

    def __str__(self):
        s = ""
        for r in self.root:
            s += self.dev[r.child].print_dev()
        return s

    def print_tree_bottom_up(self):
        for dev in self.get_bottom_devs():
            dev.print_dev_bottom_up()
        
    def has_relations(self, devname):
        l = []
        for r in self.root:
            if r.child == devname:
                return True
            d = self.get_dev(r.child)
            if d.is_parent(devname):
                return True
        return False

    def get_dev(self, devname):
        if devname not in self.dev:
            return None
        return self.dev[devname]

    def get_dev_by_devpath(self, devpath):
        for dev in self.dev.values():
            if devpath in dev.devpath:
                return dev
        return None

    def blacklist(self, dev):
        """ overload this fn with os specific implementation
        """
        return False

    def add_dev(self, devname, size=0, devtype=None):
        if devname in self.dev:
            return self.dev[devname]
        if self.blacklist(devname):
            return
        d = Dev(devname, size, devtype)
        self += d
        return d

    def set_relation_used(self, parent, child, used):
        for d in self.dev.values():
            for r in d.children + d.parents:
                if parent == r.parent and child == r.child:
                    r.set_used(used)

    def get_relation(self, parent, child):
        for d in self.dev.values():
            for r in d.children + d.parents:
                if parent == r.parent and child == r.child:
                    return r
        return None

    def get_bottom_devs(self):
        return [self.dev[devname] for devname in self.dev if len(self.dev[devname].children) == 0]

    def get_top_devs(self):
        d = set([])
        for dev in self.get_bottom_devs():
            d |= dev.get_top_devs()
        return list(d)

    def get_used(self, chain):
        used = 0
        for rel in chain:
            used = rel.get_used(used)
        return used

    def get_top_devs_usage_for_devpath(self, devpath):
        dev = self.get_dev_by_devpath(devpath)
        if dev is None:
            return []
        l = []
        for d, chain in dev.get_top_devs_chain():
            if len(chain) == 0:
                used = d.size
                region = 0
            else:
                used = self.get_used(chain)
                ref = self.get_dev(chain[0].child).alias
                region = hash(ref)
            l.append((d.devpath[0], used, region))
        return l

if __name__ == "__main__":
    tree = DevTree()
    d = tree.add_dev('/dev/sdb', 10000)
    d.add_child('/dev/sdb1', 8000)
    d.add_child('/dev/sdb2', 2000)
    d = tree.add_dev('/dev/sdc', 20000)
    d.add_child('/dev/mapper/vg01-foo', 1000)
    d = tree.get_dev('/dev/sdb2')
    d.add_child('/dev/mapper/vg01-foo', 1000)
    d = tree.get_dev('/dev/mapper/vg01-foo')
    d.add_child('foo.vmdk', 500)
    print(tree)
 0707010000bbab000081a400000000000000000000000154fc7bfb000007f8000000660000000300000000000000000000002500000000root/opt/opensvc/lib/rcDevTreeAIX.py  import rcDevTree
import rcLvmAIX
import os
import re
from subprocess import *
from rcUtilities import which
from rcGlobalEnv import rcEnv

class DevTree(rcDevTree.DevTree):
    def load_lvm(self):
        lvm = rcLvmAIX.Lvm()
        for vg in lvm.vg.values():
            for lv in vg.lv.values():
                d = self.add_dev(lv.name, self.disk_size(lv.name), "linear")
                d.set_devpath('/dev/'+lv.name)
                for parentname in lv.pv_size:
                    size = lv.pv_size[parentname]
                    d.add_parent(parentname, size, "linear")
                    parent = self.get_dev(parentname)
                    parent.add_child(d.devname, size, "linear")

    def load_lsdev(self):
        if not which("lsdev"):
            return
        cmd = ["lsdev", "-Cc", "disk"]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode:
            return
        """
        hdisk0 Available  Virtual SCSI Disk Drive
        hdisk1 Available  Virtual SCSI Disk Drive
        """
        for line in out.split('\n'):
            if len(line) == 0:
                continue
            l = line.split()
            devname = l[0]
            d = self.add_dev(devname, self.disk_size(devname), "linear")
            d.set_devpath('/dev/'+devname)

    def disk_size(self, devname):
        cmd = ["bootinfo", "-s", devname]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode:
            return 0
        return int(out.strip())

    def load(self, di=None):
        self.load_lsdev()
        self.load_lvm()

    def blacklist(self, devname):
        bl = [r'^loop[0-9]*.*', r'^ram[0-9]*.*', r'^scd[0-9]*', r'^sr[0-9]*']
        for b in bl:
            if re.match(b, devname):
                return True
        return False

if __name__ == "__main__":
    tree = DevTree()
    tree.load()
    #print(tree)
    tree.print_tree_bottom_up()
    #print(map(lambda x: x.alias, tree.get_top_devs()))
0707010000bbac000081a400000000000000000000000154fc7bfb000017f5000000660000000300000000000000000000002700000000root/opt/opensvc/lib/rcDevTreeHP-UX.py    import rcDevTree
import glob
import os
import re
from subprocess import *
from rcUtilities import which
from rcGlobalEnv import rcEnv

dim = __import__("rcDiskInfoHP-UX")
di = dim.diskInfo()

class DevTree(rcDevTree.DevTree):
    pe_size = {}

    def add_part(self, parent_devpath, child_devpath):
        child_dev = self.add_disk(child_devpath)
        if child_dev is None:
            return
        parent_dev = self.get_dev_by_devpath(parent_devpath)
        if parent_dev is None:
            return
        child_dev.add_parent(parent_dev.devname)
        parent_dev.add_child(child_dev.devname)

    def add_disk(self, devpath):
        devname = devpath.split('/')[-1]
        if devpath in self.lunmap:
            devtype = "multipath"
        else:
            devtype = "linear"
        size = di.disk_size(devpath)

        # exclude 0-sized md, Symmetrix gatekeeper and vcmdb
        if size in [0, 2, 30, 45]:
            return

        d = self.add_dev(devname, size, devtype)
        d.set_devpath(devpath)
        d.set_devpath(devpath.replace('/disk/', '/rdisk/').replace('/dsk/', '/rdsk/'))
        if devpath in self.lunmap:
            d.set_alias(self.lunmap[devpath]['wwid'])
        else:
            wwid = di.disk_id(devpath)
            if wwid != "":
                d.set_alias(wwid)
        return d

    def load_ioscan(self):
        if not which("/usr/sbin/ioscan"):
            return
        cmd = ["/usr/sbin/ioscan", "-FunNC", "disk"]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode:
            if "illegal option -- N" not in err:
                return
            cmd = ["/usr/sbin/ioscan", "-FunC", "disk"]
            p = Popen(cmd, stdout=PIPE, stderr=PIPE)
            out, err = p.communicate()
            if p.returncode:
                return
        """
        scsi:wsio:T:T:F:31:188:0:disk:sdisk:0/2/1/0.0.0.0.0:0 0 5 18 0 0 0 0 195 124 63 185 173 253 214 203 :0:root.sba.lba.sasd.sasd_vbus.tgt.sdisk:sdisk:CLAIMED:DEVICE:HP      DG146BB976:0:
                      /dev/disk/disk11      /dev/disk/disk11_p2   /dev/rdisk/disk11     /dev/rdisk/disk11_p2
                      /dev/disk/disk11_p1   /dev/disk/disk11_p3   /dev/rdisk/disk11_p1  /dev/rdisk/disk11_p3
        """
        for w in out.split():
            if not w.startswith('/dev/'):
                new = True
                continue
            if new:
                disk = w
                new = False
                d = self.add_disk(disk)
                continue
            if d is None or '/rdisk/' in w:
                continue
            elif '/pt/' in w:
                d.set_devpath(w)
            elif '_p' in w:
                self.add_part(disk, w)
            else:
                # arbitrary dsf alias
                d.set_devpath(w)

    def get_lunmap(self):
        if hasattr(self, "lunmap"):
            return
        self.lunmap = {}
        if not which("scsimgr"):
            return
        cmd = ["scsimgr", "lun_map"]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode:
            return
        """

        LUN PATH INFORMATION FOR LUN : /dev/rdisk/disk10

Total number of LUN paths     = 1
World Wide Identifier(WWID)    = 0x5000c5000aba6793

LUN path : lunpath0
Class                         = lunpath
Instance                      = 0
Hardware path                 = 0/2/1/0.0x5000c5000aba6791.0x0
SCSI transport protocol       = sas
State                         = UNOPEN
Last Open or Close state      = ACTIVE
"""
        for line in out.split('\n'):
            if "INFORMATION" in line:
                disk = line.split()[-1]
                self.lunmap[disk] = {}
            if "WWID" in line:
                wwid = line.split()[-1].replace('0x', '')
                if wwid != "=":
                    self.lunmap[disk]['wwid'] = line.split()[-1].replace('0x', '')
                else:
                    del(self.lunmap[disk])

    def load_lv(self, lv):
        if not which("lvdisplay"):
            return
        cmd = ["lvdisplay", "-v", lv]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode:
            return

        vgname = lv.split('/')[2]

        # parser
        h = {}
        for line in out.split('\n'):
            line = line.strip()
            if 'LV Size' in line:
                size = int(line.split()[-1])
            if not line.startswith('/dev'):
                continue
            pv, le, pe = line.split()
            h[pv] = int(pe) * self.pe_size[vgname]
            
        # use the linux lvm naming convention
        devname = lv.replace('/dev/','').replace('-','--').replace('/','-')
        d = self.add_dev(devname, size, "linear")

        for pv in h:
            d.add_parent(pv.replace('/dev/disk/', ''), size=h[pv])
            d.set_devpath(lv)
            parent_dev = self.get_dev_by_devpath(pv)
            if parent_dev is None:
                continue
            parent_dev.add_child(d.devname)

    def load_lvm(self):
        if not which("vgdisplay"):
            return
        cmd = ["vgdisplay", "-v"]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        for line in out.split('\n'):
            if 'VG Name' in line:
                vgname = line.split()[-1].replace('/dev/','')
            if 'PE Size' in line:
                self.pe_size[vgname] = int(line.split()[-1])
            if 'LV Name' not in line:
                continue
            self.load_lv(line.split()[-1])

    def load(self, di=None):
        self.get_lunmap()
        self.load_ioscan()
        self.load_lvm()

    def blacklist(self, devname):
        bl = [r'^loop[0-9]*.*', r'^ram[0-9]*.*', r'^scd[0-9]*', r'^sr[0-9]*']
        for b in bl:
            if re.match(b, devname):
                return True
        return False

if __name__ == "__main__":
    tree = DevTree()
    tree.load()
    #print(tree)
    tree.print_tree_bottom_up()
    #print(map(lambda x: x.alias, tree.get_top_devs()))
   0707010000bbad000081a400000000000000000000000154fc7bfb000042c6000000660000000300000000000000000000002700000000root/opt/opensvc/lib/rcDevTreeLinux.py    import rcDevTree
import glob
import os
import re
import math
from subprocess import *
from rcUtilities import which
from rcGlobalEnv import rcEnv
di = __import__("rcDiskInfo"+rcEnv.sysname)
_di = di.diskInfo()

class DevTree(rcDevTree.DevTree):
    dev_h = {}

    def get_size(self, devpath):
        size = 0
        with open(devpath+'/size', 'r') as f:
            try:
                size = int(f.read().strip()) * 512 / 1024 / 1024
            except:
                pass
        return size

    def get_dm(self):
        if hasattr(self, 'dm_h'):
            return self.dm_h
        self.dm_h = {}
        if not os.path.exists("/dev/mapper"):
            return self.dm_h
        cmd = ['dmsetup', 'mknodes']
        p = Popen(cmd)
        p.communicate()
        devpaths = glob.glob("/dev/mapper/*")
        devpaths.remove('/dev/mapper/control')
        for devpath in devpaths:
            s = os.stat(devpath)
            minor = os.minor(s.st_rdev)
            self.dm_h[devpath.replace("/dev/mapper/", "")] = "dm-%d"%minor

        # reverse hash
        self._dm_h = {}
        for mapname, devname in self.dm_h.items():
            self._dm_h[devname] = mapname

        return self.dm_h

    def get_map_wwid(self, map):
        if not which("multipath"):
            return None
        if not hasattr(self, 'multipath_l'):
            self.multipath_l = []
            cmd = ['multipath', '-l']
            p = Popen(cmd, stdout=PIPE, stderr=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                return None
            self.multipath_l = out.split('\n')
        for line in self.multipath_l:
            if not line.startswith(map):
                continue
            try:
                wwid = line[line.index('(')+2:line.index(')')]
            except ValueError:
                wwid = line.split()[0]
            return wwid
        return None

    def get_wwid(self):
        if hasattr(self, 'wwid_h'):
            return self.wwid_h
        self.wwid_h = {}
        self.wwid_h.update(self.get_wwid_native())
        self.wwid_h.update(self.get_mp_powerpath())
        return self.wwid_h

    def get_wwid_native(self):
        if not which("multipath"):
            return self.wwid_h
        cmd = ['multipath', '-l']
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return self.wwid_h
        for line in out.split('\n'):
            if 'dm-' not in line:
                continue
            devname = line[line.index('dm-'):].split()[0]
            try:
                wwid = line[line.index('(')+2:line.index(')')]
            except ValueError:
                wwid = line.split()[0][1:]
            self.wwid_h[devname] = wwid
        return self.wwid_h

    def get_mp(self):
        if hasattr(self, 'mp_h'):
            return self.mp_h
        self.mp_h = {}
        self.mp_h.update(self.get_mp_native())
        self.mp_h.update(self.get_mp_powerpath())
        return self.mp_h

    def get_mp_powerpath(self):
        self.powerpath = {}
        if not which("powermt"):
            return {}
        cmd = ['powermt', 'display', 'dev=all']
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return {}
        lines = out.split('\n')
        if len(lines) < 1:
            return {}
        dev = None
        paths = []
        mp_h = {}
        for line in lines:
            if len(line) == 0:
                # new mpath
                # - store previous
                # - reset path counter
                if dev is not None:
                    if len(paths) > 0:
                        did = _di.disk_id(paths[0])
                    mp_h[name] = did
                    self.powerpath[name] = paths
                    dev = None
                    paths = []
            if 'Pseudo name' in line:
                l = line.split('=')
                if len(l) != 2:
                    continue
                name = l[1]
                dev = "/dev/"+name
            else:
                l = line.split()
                if len(l) < 3:
                    continue
                if l[2].startswith("sd"):
                    paths.append("/dev/"+l[2])
        return mp_h

    def get_mp_native(self):
        if not which('dmsetup'):
            return {}
        cmd = ['dmsetup', 'ls', '--target', 'multipath']
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return {}
        mp_h = {}
        for line in out.split('\n'):
            l = line.split()
            if len(l) == 0:
                continue
            mapname = l[0]
            major = l[1].strip('(,')
            minor = l[2].strip(' )')
            mp_h['dm-'+minor] = mapname
        return mp_h

    def get_md(self):
        if hasattr(self, 'md_h'):
            return self.md_h
        fpath = "/proc/mdstat"
        self.md_h = {}
        try:
            with open(fpath, 'r') as f:
                buff = f.read()
        except:
            return self.md_h
        for line in buff.split('\n'):
            if line.startswith("Personalities"):
                continue
            if len(line) == 0 or line[0] == " ":
                continue
            l = line.split()
            if len(l) < 4:
                continue
            self.md_h[l[0]] = l[3]
        return self.md_h

    def load_dm_dev_t(self):
        table = {}
        if not which('dmsetup'):
            return
        cmd = ['dmsetup', 'ls']
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return
        for line in out.split('\n'):
            l = line.split()
            if len(l) == 0:
                continue
            mapname = l[0]
            major = l[1].strip('(,')
            minor = l[2].strip(' )')
            dev_t = ':'.join((major, minor))
            self.dev_h[dev_t] = mapname

    def load_dm(self):
        table = {}
        self.load_dm_dev_t()
        if not which('dmsetup'):
            return
        cmd = ['dmsetup', 'table']
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return
        for line in out.split('\n'):
            l = line.split()
            if len(l) < 5:
                continue
            mapname = l[0].strip(':')
            size = int(math.ceil(1.*int(l[2])*512/1024/1024))
            maptype = l[3]

            if maptype == "multipath" and size in [0, 2, 3, 30, 45]:
                continue

            for w in l[4:]:
                if ':' not in w:
                    continue
                if mapname not in table:
                    table[mapname] = {"devs": [], "size": 0, "type": "linear"}
                table[mapname]["devs"].append(w)
                table[mapname]["size"] += size
                table[mapname]["type"] = maptype
        for mapname in table:
            d = self.add_dev(mapname, table[mapname]["size"])
            d.set_devtype(table[mapname]["type"])

            d.set_devpath('/dev/mapper/'+mapname)
            s = mapname.replace('--', ':').replace('-', '/').replace(':','-')
            if "/" in s:
                d.set_devpath('/dev/'+s)
            wwid = self.get_map_wwid(mapname)
            if wwid is not None:
                d.set_alias(wwid)
            for dev in table[mapname]["devs"]:
                if dev not in self.dev_h:
                    continue
                d.add_parent(self.dev_h[dev])
                parentdev = self.get_dev(self.dev_h[dev])
                parentdev.add_child(mapname)


    def get_lv_linear(self):
        if hasattr(self, 'lv_linear'):
            return self.lv_linear
        self.lv_linear = {}
        if not which('dmsetup'):
            return self.lv_linear
        cmd = ['dmsetup', 'table', '--target', 'linear']
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return self.lv_linear
        for line in out.split('\n'):
            l = line.split(':')
            if len(l) < 2:
                continue
            mapname = l[0]
            line = line[line.index(':')+1:]
            l = line.split()
            if len(l) < 3:
                continue
            length = int(l[1])*512/1024/1024
            devt = l[3]
            if mapname in self.lv_linear:
                self.lv_linear[mapname].append((devt, length))
            else:
                self.lv_linear[mapname] = [(devt, length)]
        return self.lv_linear

    def is_cdrom(self, devname):
        p = '/sys/block/%s/device/media'%devname
        if not os.path.exists(p):
            return False
        with open(p, 'r') as f:
            buff = f.read()
        if buff.strip() == "cdrom":
            return True
        return False

    def dev_type(self, devname):
        t = "linear"
        md_h = self.get_md()
        mp_h = self.get_mp()
        if devname in md_h:
            return md_h[devname]
        if devname in mp_h:
            return "multipath"
        return t

    def add_drbd_relations(self):
        if not which("drbdadm") or not os.path.exists('/proc/drbd'):
            return
        cmd = ["drbdadm", "dump-xml"]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return
        from xml.etree import ElementTree as etree
        tree = etree.fromstring(out)
        for res in tree.getiterator('resource'):
            for host in res.findall('host'):
                if host.attrib['name'] != rcEnv.nodename:
                    continue
                edisk = host.find('disk')
                edev = host.find('device')
                if edisk is None or edev is None:
                    continue
                devname = 'drbd'+edev.attrib['minor']
                parentpath = edisk.text
                d = self.get_dev_by_devpath(parentpath)
                if d is None:
                    continue
                d.add_child(devname)
                c = self.get_dev(devname)
                c.add_parent(d.devname)

    def load_dev(self, devname, devpath):
        if self.is_cdrom(devname):
            return

        mp_h = self.get_mp()
        wwid_h = self.get_wwid()
        size = self.get_size(devpath)

        # exclude 0-sized md, Symmetrix gatekeeper and vcmdb
        if devname in self.mp_h and size in [0, 2, 30, 45]:
            return

        devtype = self.dev_type(devname)
        d = self.add_dev(devname, size, devtype)

        if d is None:
            return

        self.get_dm()

        if 'cciss' in devname:
            d.set_devpath('/dev/'+devname.replace('!', '/'))
        elif devname in self.mp_h:
            d.set_devpath('/dev/mpath/'+self._dm_h[devname])
            d.set_devpath('/dev/'+devname)
        else:
            d.set_devpath('/dev/'+devname)

        # store devt
        with open("%s/dev"%devpath, 'r') as f:
            devt = f.read().strip()
            self.dev_h[devt] = devname

        # add holders
        holderpaths = glob.glob("%s/holders/*"%devpath)
        holdernames = map(lambda x: os.path.basename(x), holderpaths)
        for holdername, holderpath in zip(holdernames, holderpaths):
            if not os.path.exists(holderpath):
                # broken symlink
                continue
            size = self.get_size(holderpath)
            devtype = self.dev_type(holdername)
            d.add_child(holdername, size, devtype)

        # add lv aliases
        if devname in self._dm_h:
            d.set_alias(self._dm_h[devname])
            d.set_devpath('/dev/mapper/'+self._dm_h[devname])
            s = self._dm_h[devname].replace('--', ':').replace('-', '/').replace(':','-')
            d.set_devpath('/dev/'+s)

        # add slaves
        slavepaths = glob.glob("%s/slaves/*"%devpath)
        slavenames = map(lambda x: os.path.basename(x), slavepaths)
        for slavename, slavepath in zip(slavenames, slavepaths):
            if not os.path.exists(slavepath):
                # broken symlink
                continue
            size = self.get_size(slavepath)
            devtype = self.dev_type(slavename)
            d.add_parent(slavename, size, devtype)

        if devname in mp_h:
            d.set_alias(wwid_h[devname])
            d.set_devpath('/dev/mpath/3'+wwid_h[devname])

        return d

    def get_dev_t(self, dev):
        major, minor = self._get_dev_t(dev)
        return ":".join((str(major), str(minor)))

    def _get_dev_t(self, dev):
        try:
            s = os.stat(dev)
            minor = os.minor(s.st_rdev)
            major = os.major(s.st_rdev)
        except:
            return 0, 0
        return major, minor

    def load_fdisk(self):
        self.get_wwid()
        os.environ["LANG"] = "C"
        p = Popen(["fdisk", "-l"], stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return
        for line in out.split("\n"):
            if line.startswith('/dev/dm-'):
                continue
            elif line.startswith("Disk "):
                # disk
                devpath = line.split()[1].strip(':')
                if devpath.startswith('/dev/dm-'):
                    continue
                size = int(line.split()[-2]) / 1024 / 1024
                if size in [2, 3, 30, 45]:
                    continue
                devname = devpath.replace('/dev/','').replace("/","!")
                devtype = self.dev_type(devname)
                dev_t = self.get_dev_t(devpath)
                self.dev_h[dev_t] = devname
                d = self.add_dev(devname, size, devtype)
                if d is None:
                    continue
                d.set_devpath(devpath)
                if devname.startswith('emc') and devname in self.wwid_h:
                    d.set_alias(self.wwid_h[devname])
                    for path in self.powerpath[devname]:
                        p = self.add_dev(path.replace('/dev/',''), size, "linear")
                        p.set_devpath(path)
                        p.add_child(devname)
                        d.add_parent(path.replace('/dev/',''))
            elif line.startswith('Unit'):
                unit = int(line.split()[-2])
            elif line.startswith('/dev/'):
                # partition
                line = line.replace('*', '')
                _l = line.split()
                partpath = _l[0]
                partend = int(_l[2])
                partstart = int(_l[1])
                partsize = (partend - partstart) * unit / 1024/1024
                partname = partpath.replace('/dev/','').replace("/","!")
                dev_t = self.get_dev_t(partpath)
                self.dev_h[dev_t] = partname
                p = self.add_dev(partname, partsize, "linear")
                if p is None:
                    continue
                p.set_devpath(partpath)
                d.add_child(partname)
                p.add_parent(devname)

    def load_sysfs(self):
        devpaths = glob.glob("/sys/block/*")
        devnames = map(lambda x: os.path.basename(x), devpaths)
        for devname, devpath in zip(devnames, devpaths):
            d = self.load_dev(devname, devpath)

            if d is None:
                continue

            # add parts
            partpaths = glob.glob("%s/%s*"%(devpath, devname))
            partnames = map(lambda x: os.path.basename(x), partpaths)
            for partname, partpath in zip(partnames, partpaths):
                p = self.load_dev(partname, partpath)
                if p is None:
                    continue
                d.add_child(partname)
                p.add_parent(devname)
 
    def tune_lv_relations(self):
        dm_h = self.get_dm()
        for lv, segments in self.get_lv_linear().items():
            for devt, length in segments:
                if devt not in self.dev_h:
                    continue
                if lv not in dm_h:
                    continue
                child = dm_h[lv]
                parent = self.dev_h[devt]
                r = self.get_relation(parent, child)
                if r is not None:
                    r.set_used(length)

    def load(self, di=None):
        if len(glob.glob("/sys/block/*/slaves")) == 0:
            self.load_fdisk()
            self.load_dm()
        else:
            self.load_sysfs()
            self.tune_lv_relations()

        self.add_drbd_relations()

    def blacklist(self, devname):
        bl = [r'^loop[0-9]*.*', r'^ram[0-9]*.*', r'^scd[0-9]*', r'^sr[0-9]*']
        for b in bl:
            if re.match(b, devname):
                return True
        return False

if __name__ == "__main__":
    tree = DevTree()
    tree.load()
    #print(tree)
    tree.print_tree_bottom_up()
    #print(map(lambda x: x.alias, tree.get_top_devs()))
  0707010000bbae000081a400000000000000000000000154fc7bfb00000c16000000660000000300000000000000000000002600000000root/opt/opensvc/lib/rcDevTreeOSF1.py import rcDevTree
import os
import re
from rcUtilities import which, justcall
from rcGlobalEnv import rcEnv
from rcDiskInfoOSF1 import diskInfo

regex = re.compile('\W*[a-h]:')

class DevTree(rcDevTree.DevTree):
    def get_parts(self, devname, d):
        if d is None:
            return
        data = self.di.h[devname]
        cmd = ['disklabel', '/dev/rdisk/'+devname+'a']
        out, err, ret = justcall(cmd)
        """
# /dev/rdisk/dsk15a:
type: SCSI
disk: HSV210
label: 
flags:
bytes/sector: 512
sectors/track: 128
tracks/cylinder: 128
sectors/cylinder: 16384
cylinders: 15360
sectors/unit: 251658240
rpm: 3600
interleave: 1
trackskew: 7
cylinderskew: 26
headswitch: 0		# milliseconds
track-to-track seek: 0	# milliseconds
drivedata: 0 

8 partitions:
#            size       offset    fstype  fsize  bsize   cpg  # ~Cyl values
  a:       131072            0    unused      0      0        #      0 - 7 
  b:       262144       131072    unused      0      0        #      8 - 23 
  c:    251658240            0     AdvFS                      #      0 - 15359 
  d:            0            0    unused      0      0        #      0 - 0 
  e:            0            0    unused      0      0        #      0 - 0 
  f:            0            0    unused      0      0        #      0 - 0 
  g:    125632512       393216    unused      0      0        #     24 - 7691 
  h:    125632512    126025728    unused      0      0        #   7692 - 15359 
"""
        if ret != 0:
            return
        for line in out.split("\n"):
            if line.startswith("bytes/sector"):
                bs = int(line.split()[-1])
            if regex.match(line) is None:
                continue
            l = line.split()
            part = l[0].replace(':','')
            size = 1. * int(l[1]) * bs / 1024 / 1024
            size = int(size)
            
            partname = devname+part
            child_dev = self.add_dev(partname, size, "linear")
            child_dev.set_devpath('/dev/disk/'+partname)
            child_dev.set_devpath('/dev/rdisk/'+partname)
            if child_dev is None:
                continue
            child_dev.add_parent(devname)
            d.add_child(partname)

    def get_disks(self):
        for devname in self.di.h:
            if self.di.h[devname]['size'] in [0, 2, 30, 45]:
                continue
            d = self.add_dev(devname, self.di.h[devname]['size'], "linear")
            d.set_alias(self.di.h[devname]['wwid'])
            d.set_devpath('/dev/rdisk/'+devname)
            d.set_devpath('/dev/disk/'+devname)
            self.get_parts(devname, d)

    def load(self, di=None):
        self.di = diskInfo()
        self.get_disks()

    def blacklist(self, devname):
        bl = [r'^loop[0-9]*.*', r'^ram[0-9]*.*', r'^scd[0-9]*', r'^sr[0-9]*']
        for b in bl:
            if re.match(b, devname):
                return True
        return False

if __name__ == "__main__":
    tree = DevTree()
    tree.load()
    #print(tree)
    tree.print_tree_bottom_up()
    #print(map(lambda x: x.alias, tree.get_top_devs()))
  0707010000bbaf000081a400000000000000000000000154fc7bfb000022ba000000660000000300000000000000000000002700000000root/opt/opensvc/lib/rcDevTreeSunOS.py    import rcDevTree
import glob
import os
import re
from subprocess import *
from rcUtilities import which
from rcGlobalEnv import rcEnv

class DevTree(rcDevTree.DevTree):
    di = None
    zpool_members = {}
    zpool_used = {}
    zpool_used_zfs = {}
    zpool_size = {}
    zpool_datasets = {}
    zpool_datasets_used = {}

    def load_partitions(self, d):
        """
        *                          First     Sector    Last
        * Partition  Tag  Flags    Sector     Count    Sector  Mount Directory
               0      2    00   16779312  54281421  71060732
               1      3    01          0  16779312  16779311
               2      5    00          0  71127180  71127179
               7      0    00   71060733     66447  71127179
        """
        p = Popen(["prtvtoc", d.devpath[0]], stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return
        for line in out.split("\n"):
            line = line.strip()
            if line.startswith('*'):
                continue
            if line.startswith('2'):
                continue
            l = line.split()
            if len(l) < 6:
                continue
            partname = d.devname + 's' + l[0]
            partpath = d.devpath[0][:-1] + l[0]
            partsize = self.di.get_part_size(partpath)
            p = self.add_dev(partname, partsize, "linear")
            p.set_devpath(partpath)
            d.add_child(partname)
            p.add_parent(d.devname)

    def load_format(self):
        """
        0. c3t0d0 <SUN36G cyl 24620 alt 2 hd 27 sec 107>
          /pci@1f,700000/scsi@2/sd@0,0
        4. c5t600508B4000971CD00010000024A0000d0 <HP-HSV210-6200 cyl 61438 alt 2 hd 128 sec 128>  EVA_SAVE
          /scsi_vhci/ssd@g600508b4000971cd00010000024a0000
        """
        p = Popen(["format", "-e"], stdout=PIPE, stderr=PIPE, stdin=PIPE)
        out, err = p.communicate(input=None)
        for line in out.split("\n"):
            line = line.strip()
            if re.match(r"[0-9]+\. ", line) is None:
                continue
            l = line.split()
            devname = l[1]
            devpath = '/dev/rdsk/'+devname+'s2'
            size = self.di.get_size(devpath)
            d = self.add_dev(devname, size, "linear")
            d.set_devpath(devpath)
            self.load_partitions(d)

    def load_sds(self):
        if not os.path.exists("/usr/sbin/metastat"):
            return
        p = Popen(["metastat", "-p"], stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return
        lines = out.split('\n')
        lines.reverse()
        
        """
        # metastat -p
        d11 -m d2 d3 1
        d2 1 1 c3t0d0s1
        d3 1 1 c3t1d0s1
        """
        for line in lines:
            l = line.split()
            if len(l) < 3:
                continue
            childname = l[0]
            childpath = "/dev/md/dsk/"+childname
            childsize = self.di.get_size(childpath)
            if l[1] == "-m":
                childtype = "raid1"
            else:
                childtype = "linear"
            childdev = self.add_dev(childname, childsize, childtype)
            childdev.set_devpath(childpath)
            if l[1] == "-m":
                parentnames = l[2:-1]
            else:
                parentnames = [l[-1]]

            for parentname in parentnames:
                parentpath = "/dev/md/dsk/"+parentname
                parentsize = self.di.get_size(parentpath)
                parentdev = self.add_dev(parentname, parentsize, "linear")
                childdev.add_parent(parentname)
                parentdev.add_child(childname)

    def load_zpool(self):
        p = Popen(["zpool", "list", "-H"], stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return
        for line in out.split('\n'):
            l = line.split()
            if len(l) == 0:
                continue
            poolname = l[0]
            self.load_zpool1(poolname)

    def load_zpool1(self, poolname):
        p = Popen(["zpool", "status", poolname], stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return
        self.zpool_members[poolname] = []
        for line in out.split('\n'):
            l = line.split()
            if len(l) != 5:
                continue
            if l[0] == 'NAME':
                continue
            if l[0] == poolname:
                continue
            devname = l[0]

            # -d mode import ?
            if devname.startswith(rcEnv.pathvar):
                devname = devname.split('/')[-1]
            d = self.get_dev(devname)
            if d is None:
                continue
            self.zpool_members[poolname].append(d)

        p = Popen(["zpool", "iostat", poolname], stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return
        lines = out.split('\n')
        lines = [l for l in lines if len(l) > 0]
        self.zpool_used[poolname] = self.read_size(lines[-1].split()[1])
        zpool_free = self.read_size(lines[-1].split()[2])
        self.zpool_size[poolname] = self.zpool_used[poolname] + zpool_free

        p = Popen(["zfs", "list", "-H", "-r", "-t", "filesystem", poolname], stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return
        self.zpool_datasets[poolname] = []
        self.zpool_datasets_used[poolname] = 0
        for line in out.split('\n'):
            l = line.split()
            if len(l) == 0:
                continue
            zfsname = l[0]
            size = self.read_size(l[1])
            refer = self.read_size(l[3])
            size -= refer
            mnt = l[4]
            if zfsname == poolname:
                self.zpool_used_zfs[poolname] = size
                continue
            self.zpool_datasets[poolname].append((zfsname, size))
            self.zpool_datasets_used[poolname] += size

        p = Popen(["zfs", "list", "-H", "-t", "snapshot"], stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return
        for line in out.split('\n'):
            l = line.split()
            if len(l) == 0:
                continue
            zfsname = l[0]
            if not zfsname.startswith(poolname+'/') and \
               not zfsname.startswith(poolname+'@'):
                continue
            size = self.read_size(l[1])
            #refer = self.read_size(l[3])
            self.zpool_datasets[poolname].append((zfsname, size))
            self.zpool_datasets_used[poolname] += size

        rest = self.zpool_used_zfs[poolname] - self.zpool_datasets_used[poolname]
        if rest < 0:
            rest = 0
        self.zpool_datasets[poolname].append((poolname, rest))
        self.zpool_datasets_used[poolname] += rest

        if self.zpool_datasets_used[poolname] == 0:
            ratio = 0
        else:
            ratio = 1.0 * self.zpool_used[poolname] / self.zpool_datasets_used[poolname]

        for zfsname, size in self.zpool_datasets[poolname]:
            used = int(size*ratio)
            d = self.add_dev(zfsname, used, "zfs")
            d.set_devpath(zfsname)
            for m in self.zpool_members[poolname]:
                member_ratio = 1.0 * m.size / self.zpool_size[poolname]
                d.add_parent(m.devname)
                m.add_child(zfsname)
                self.set_relation_used(m.devname, zfsname, int(used*member_ratio))

    def read_size(self, s):
        if s == '0':
            return 0
        unit = s[-1]
        size = float(s[:-1].replace(',','.'))
        if unit == 'K':
            size = size / 1024
        elif unit == 'M':
            pass
        elif unit == 'G':
            size = size * 1024
        elif unit == 'T':
            size = size * 1024 * 1024
        elif unit == 'P':
            size = size * 1024 * 1024 * 1024
        elif unit == 'Z':
            size = size * 1024 * 1024 * 1024 * 1024
        else:
            raise Exception("unit not supported: %s"%unit)
        return int(size)
        
    def load(self, di=None):
        if di is not None:
            self.di = di
        if self.di is None:
            from rcDiskInfoSunOS import diskInfo
            self.di = diskInfo()
        self.load_format()
        self.load_zpool()
        self.load_sds()

    def blacklist(self, devname):
        bl = [r'^loop[0-9]*.*', r'^ram[0-9]*.*', r'^scd[0-9]*', r'^sr[0-9]*']
        for b in bl:
            if re.match(b, devname):
                return True
        return False

if __name__ == "__main__":
    tree = DevTree()
    tree.load()
    #print(tree)
    tree.print_tree_bottom_up()
    #print(map(lambda x: x.alias, tree.get_top_devs()))
  0707010000bbb0000081a400000000000000000000000154fc7bfb0000026b000000660000000300000000000000000000002900000000root/opt/opensvc/lib/rcDevTreeWindows.py  import rcDevTree
import wmi

class DevTree(rcDevTree.DevTree):

    def load_diskdrive(self):
        if not hasattr(self, 'wmi'):
            self.wmi = wmi.WMI()
        for drive in self.wmi.WIN32_DiskDrive():
            d = self.add_dev(drive.DeviceId, int(drive.size)//1024, "linear")
            d.set_devpath(drive.DeviceId)

    def load(self, di=None):
        self.load_diskdrive()

    def blacklist(self, devname):
        return False

if __name__ == "__main__":
    tree = DevTree()
    tree.load()
    #print(tree)
    tree.print_tree_bottom_up()
    #print(map(lambda x: x.alias, tree.get_top_devs()))
 0707010000bbb1000081a400000000000000000000000154fc7bfb00000551000000660000000300000000000000000000002300000000root/opt/opensvc/lib/rcDiskInfo.py    #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

class diskInfo(object):
    """Parent class for diskInfo OS"""

    print_diskinfo_fmt = "%-12s %-8s %12s MB %-8s %-8s %-16s"

    def disk_id(self, dev):
        return "tbd"

    def disk_vendor(self, dev):
        return "tbd"

    def disk_model(self, dev):
        return "tbd"

    def disk_size(self, dev):
        return "tbd"

    def print_diskinfo_header(self):
        print(self.print_diskinfo_fmt%(
          "hbtl",
          "devname",
          "size",
          "dev",
          "vendor",
          "model",
        ))

   0707010000bbb2000081a400000000000000000000000154fc7bfb00000b79000000660000000300000000000000000000002600000000root/opt/opensvc/lib/rcDiskInfoAIX.py #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from rcUtilities import call, justcall
import rcDiskInfo
import re

class diskInfo(rcDiskInfo.diskInfo):

    def __init__(self):
        self.h = {}

    def scan(self, lname):
        vid = 'unknown'
        pid = 'unknown'
        wwid = 'unknown'
        size = 'unknown'

        cmd = ['lscfg', '-vpl', lname]
        (ret, out, err) = call(cmd)

        for f in out.split('\n'):
            if "Manufacturer" in f:
                vid = f.split('.')[-1]
            if "Machine Type and Model" in f:
                pid = f.split('.')[-1]

        cmd = ['bootinfo', '-s', lname]
        out, err, ret = justcall(cmd)
        if ret == 0:
            size = int(out.strip())
        else:
            size = 0
        
        wwid = self.odmget(lname, 'ww_name').replace('0x', '')
        if wwid == 'unknown':
            wwid = self.get_vscsi_id(lname)

        self.h[lname] = dict(vid=vid, pid=pid, wwid=wwid, size=size)

    def get_vscsi_id(self, lname):
        cmd = ['lscfg', '-l', lname]
        (ret, out, err) = call(cmd)
        if ret != 0:
            return 'unknown'
        l = out.split()
        if len(l) < 2:
            return 'unknown'
        d = l[1]
        regex = re.compile(r'-C[0-9]+-T[0-9]+')
        d = regex.sub('', d)
        return d

    def odmget(self, lname, attr):
        cmd = ['odmget', '-q', 'name='+lname+' AND attribute='+attr, 'CuAt']
        (ret, out, err) = call(cmd)
        for f in out.split('\n'):
            if "value" not in f:
                continue
            return f.split(" = ")[-1].strip('"')
        return 'unknown'


    def devkey(self, dev):
        dev = dev.replace("/dev/", "")
        return dev

    def get(self, dev, type):
        dev = self.devkey(dev)
        if dev not in self.h:
            self.scan(dev)
        return self.h[dev][type]

    def disk_id(self, dev):
        return self.get(dev, 'wwid')

    def disk_vendor(self, dev):
        return self.get(dev, 'vid')

    def disk_model(self, dev):
        return self.get(dev, 'pid')

    def disk_size(self, dev):
        return self.get(dev, 'size')

   0707010000bbb3000081a400000000000000000000000154fc7bfb00000378000000660000000300000000000000000000002900000000root/opt/opensvc/lib/rcDiskInfoDarwin.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import rcDiskInfo

class diskInfo(rcDiskInfo.diskInfo):
    pass
0707010000bbb4000081a400000000000000000000000154fc7bfb00000551000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/rcDiskInfoFreeBSD.py #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from rcUtilities import call, which
import rcDiskInfo

class diskInfo(rcDiskInfo.diskInfo):
    disk_ids = {}

    def __init__(self, deferred=False):
        pass

    def disk_id(self, dev):
        print("%s:disk_id TODO"%__file__)
        return ""

    def disk_vendor(self, dev):
        print("%s:disk_vendor TODO"%__file__)
        return ""

    def disk_model(self, dev):
        print("%s:disk_model TODO"%__file__)
        return ""

    def disk_size(self, dev):
        print("%s:disk_size TODO"%__file__)
        return 0

   0707010000bbb5000081a400000000000000000000000154fc7bfb00001d59000000660000000300000000000000000000002800000000root/opt/opensvc/lib/rcDiskInfoHP-UX.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from rcUtilities import justcall, which
import rcDiskInfo
import os

class diskInfo(rcDiskInfo.diskInfo):
    legacy_size_cache = {}
    legacy_wwid_cache = {}

    def __init__(self):
        self.load_cache()

    def load_cache(self):
        self.load_aliases()

        self.h = {}
        cmd = ["scsimgr", "-p", "get_attr", "all_lun", "-a", "wwid", "-a", "device_file", "-a", "vid", "-a", "pid", "-a", "capacity"]
        out, err, ret = justcall(cmd)
        for e in out.split('\n'):
            if len(e) == 0:
                continue
            (wwid, dev, vid, pid, size) = e.split(':')
            wwid = wwid.replace('0x', '')
            if len(size) != 0:
                size = int(size)/2048
            else:
                size = 0
            vid = vid.strip('" ')
            pid = pid.strip('" ')
            if dev in self.aliases:
                aliases = self.aliases[dev]
            else:
                aliases = [dev]
            for alias in aliases:
                self.h[alias] = dict(wwid=wwid, vid=vid, pid=pid, size=size)

    def load_ioscan(self, refresh=False):
        if hasattr(self, "ioscan") and not refresh:
            return self.ioscan
        cmd = ['/usr/sbin/ioscan', '-FunNC', 'disk']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return
        self.ioscan = []
        """
        virtbus:wsio:T:T:F:1:13:10:disk:esdisk:64000/0xfa00/0xa:0 0 4 50 0 0 0 0 51 248 164 14 250 83 253 237 :18:root.ext_virtroot.esvroot.esdisk:esdisk:CLAIMED:DEVICE:EMC     SYMMETRIX:-1:online
                      /dev/disk/disk17            /dev/disk/disk17_p3         /dev/rdisk/disk17_p1      
                      /dev/disk/disk17_p1         /dev/pt/x64lmwbieb9_system  /dev/rdisk/disk17_p2      
                      /dev/disk/disk17_p2         /dev/rdisk/disk17           /dev/rdisk/disk17_p3      
        """
        for line in out.split('\n'):
            if not line.startswith(' ') and not line.startswith('\t') and len(line) > 0:
                l = line.split(":")
                blk_major = l[5]
                raw_major = l[6]
                index = l[7]
                vendor = l[17]
                # mark ready for insertion as soon as we get a devname
                devname = None
            elif devname is None:
                devname = line.split()[0]
                self.ioscan.append({
                  'devname': devname,
                  'dev': ':'.join((blk_major, index)),
                  'rdev': ':'.join((raw_major, index)),
                  'vendor': vendor,
                })
        return self.ioscan

    def load_aliases(self):
        self.aliases = {}
        cmd = ['/usr/sbin/ioscan', '-FunNC', 'disk']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return
        l = []
        for line in out.split('\n')+[':']:
            if ':' in line:
                if len(l) > 0:
                    for name in l:
                         self.aliases[name] = l
                l = []
                continue
            for w in line.split():
                l.append(w)

    def dev2char(self, dev):
        dev = dev.replace("/dev/disk/", "/dev/rdisk/")
        dev = dev.replace("/dev/dsk/", "/dev/rdsk/")
        return dev

    def scan(self, dev):
        cmd = ["scsimgr", "-p", "get_attr", "-D", self.dev2char(dev), "-a", "wwid", "-a", "device_file", "-a", "vid", "-a", "pid", "-a", "capacity"]
        out, err, ret = justcall(cmd)
        if ret != 0:
            self.h[dev] = dict(wwid="", vid="", pid="", size=0)
            return
        (wwid, foo, vid, pid, size) = out.split(':')
        wwid = wwid.replace('0x', '')
        if len(size) != 0:
            size = int(size)/2048
        else:
            size = 0
        vid = vid.strip('" ')
        pid = pid.strip('" ')
        self.h[dev] = dict(wwid=wwid, vid=vid, pid=pid, size=size)

    def get(self, dev, type):
        if dev not in self.h:
            self.scan(dev)
        return self.h[dev][type]

    def disk_id(self, dev):
        id = self.get(dev, 'wwid')
        if len(id) == 0:
            id = self.get_legacy_wwid(dev)
        return id

    def disk_vendor(self, dev):
        return self.get(dev, 'vid')

    def disk_model(self, dev):
        return self.get(dev, 'pid')

    def disk_size(self, dev):
        size = self.get(dev, 'size')
        if size == 0:
            size = self.get_legacy_size(dev)
        if size is None or size == "":
            # broken disk
            size = 0
        return size

    def print_diskinfo(self, info):
        info['size'] = self.disk_size(info['devname'])
        info['hbtl'] = "#:#:#:#"
        print(self.print_diskinfo_fmt%(
          info['hbtl'],
          os.path.basename(info['devname']),
          info['size'],
          info['dev'],
          info['vendor'],
          '',
        ))

    def scanscsi(self):
        ioscan_before = self.load_ioscan()
        disks_before = map(lambda x: x['devname'], ioscan_before)
        
        cmd = ['/usr/sbin/ioscan', '-fnC', 'disk']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return

        ioscan_after = self.load_ioscan(refresh=True)
        disks_after = map(lambda x: x['devname'], ioscan_after)
        new_disks = set(disks_after) - set(disks_before)

        self.print_diskinfo_header()
        for info in ioscan_after:
            if info['devname'] not in new_disks:
                continue
            self.print_diskinfo(info)

        return 0

    def get_legacy_wwid(self, devpath):
        if devpath in self.legacy_wwid_cache:
            self.legacy_wwid_cache[devpath]
        if which("autopath"):
            wwid = self.get_autopath_wwid(devpath)
            self.legacy_wwid_cache[devpath] = wwid
            return wwid
        return ""

    def get_autopath_wwid(self, devpath):
        cmd = ["autopath", "display", devpath]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return ""
        for line in out.split("\n"):
            if "Lun WWN" in line:
                return line.split(": ")[-1].replace("-","").lower()
        return ""

    def get_legacy_size(self, devpath):
        """ return devpath size in megabytes
        """
        if devpath in self.legacy_size_cache:
            return self.legacy_size_cache[devpath]
        if not which("diskinfo"):
            return 0
        cmd = ["diskinfo", "-b", devpath.replace("dsk", "rdsk").replace("disk", "rdisk")]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return 0
        size = int(out.strip())/1024
        self.legacy_size_cache[devpath] = size
        return size

   0707010000bbb6000081a400000000000000000000000154fc7bfb00002360000000660000000300000000000000000000002800000000root/opt/opensvc/lib/rcDiskInfoLinux.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
# Copyright (c) 2014 Arnaud Veron <arnaud.veron@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from __future__ import print_function
import sys
import os
import re
from rcUtilities import justcall, which
import rcDiskInfo
import math
from rcGlobalEnv import rcEnv

class diskInfo(rcDiskInfo.diskInfo):
    disk_ids = {}

    def __init__(self, deferred=False):
        pass

    def prefix_local(self, id):
        return '.'.join((rcEnv.nodename, id))

    def disk_id(self, dev):
        if 'cciss' in dev:
            id = self.cciss_id(dev)
        elif dev.startswith('/dev/mapper/3'):
            id = dev.replace('/dev/mapper/3', '')
        elif dev.startswith('/dev/mapper/2'):
            id = dev.replace('/dev/mapper/2', '')
        else:
            id = self.scsi_id(dev)
        if len(id) == 0:
            return self.prefix_local(dev.replace('/dev/','').replace('/','!'))
        return id

    def cciss_id(self, dev):
        if dev in self.disk_ids:
            return self.disk_ids[dev]
        if which('cciss_id'):
            cciss_id = 'cciss_id'
        else:
            return ""
        cmd = [cciss_id, dev]
        out, err, ret = justcall(cmd)
        if ret == 0:
            id = out.split('\n')[0]
            if id.startswith('3'):
                id = id[1:]
            else:
                id = self.prefix_local(id)
            self.disk_ids[dev] = id
            return id
        return ""

    def mpath_id(self, dev):
        self.load_mpath()
        if 'dev' not in self.mpath_h:
            return None
        return self.mpath_h(dev)

    def load_mpath_native(self):
        cmd = ['multipath', '-l']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return
        lines = out.split('\n')
        if len(lines) == 0:
            return
        self.mpath_h = {}
        regex = re.compile('[(]*[0-9a-f]*[)]*')
        for line in lines:
            if len(line) > 0 and \
               line[0] not in (' ', '\\', '[', '`', '|'):
                l = line.split()
                if l[0].startswith("size="):
                    continue
                wwid = None
                for w in l:
                    w = w.strip("()")
                    if len(w) not in [17, 33]:
                        continue
                    if regex.match(w) is None:
                        continue
                    if w.startswith("3"):
                        wwid = w[1:]
            elif " sd" in line:
                l = line.split()
                for i, w in enumerate(l):
                    if w.startswith('sd'):
                        dev = "/dev/"+w
                        self.mpath_h[dev] = wwid

    def load_mpath(self):
        if hasattr(self, "mpath_h"):
            return self.mpath_h
        self.mpath_h = {}
        if which('multipath'):
            self.load_mpath_native()
        return self.mpath_h

    def scsi_id(self, dev):
        s = self._scsi_id(dev, ["-p", "0x83"])
        if len(s) == 0:
            s = self._scsi_id(dev, ["-p", "pre-spc3-83"])
        return s

    def _scsi_id(self, dev, args=[]):
        wwid = self.mpath_id(dev)
        if wwid is not None:
            return wwid
        if dev in self.disk_ids:
            return self.disk_ids[dev]
        if which('scsi_id'):
            scsi_id = 'scsi_id'
        elif which('/lib/udev/scsi_id'):
            scsi_id = '/lib/udev/scsi_id'
        else:
            return ""
        cmd = [scsi_id, '-g', '-u'] + args + ['-d', dev]
        out, err, ret = justcall(cmd)
        if ret == 0:
            id = out.split('\n')[0]
            if id.startswith('3') or id.startswith('2'):
                id = id[1:]
            else:
                id = self.prefix_local(id)
            self.disk_ids[dev] = id
            return id
        sdev = dev.replace("/dev/", "/block/")
        cmd = [scsi_id, '-g', '-u'] + args + ['-s', sdev]
        out, err, ret = justcall(cmd)
        if ret == 0:
            id = out.split('\n')[0]
            if id.startswith('3') or id.startswith('2'):
                id = id[1:]
            else:
                id = self.prefix_local(id)
            self.disk_ids[dev] = id
            return id
        return ""

    def disk_vendor(self, dev):
        if 'cciss' in dev:
            return 'HP'
        s = ''
        dev = re.sub("[0-9]+$", "", dev)
        path = dev.replace('/dev/', '/sys/block/')+'/device/vendor'
        if not os.path.exists(path):
            return ""
        with open(path, 'r') as f:
            s = f.read()
            f.close()
        if '6900' in s:
            s = 'Red Hat'
        return s.strip()

    def disk_model(self, dev):
        if 'cciss' in dev:
            return 'VOLUME'
        s = ''
        vendor = self.disk_vendor(dev)
        dev = re.sub("[0-9]+$", "", dev)
        path = dev.replace('/dev/', '/sys/block/')+'/device/model'
        if not os.path.exists(path):
            if 'Red Hat' in vendor:
                return 'VirtIO'
            else:
                return ""
        with open(path, 'r') as f:
            s = f.read()
            f.close()
        return s.strip()

    def disk_size(self, dev):
        size = 0
        if '/dev/mapper/' in dev:
            try:
                statinfo = os.stat(dev)
            except:
                raise Exception("can not stat %s" % dev)
            dm = 'dm-' + str(os.minor(statinfo.st_rdev))
            path = '/sys/block/' + dm + '/size'
            if not os.path.exists(path):
                return 0
        else:
            path = dev.replace('/dev/', '/sys/block/')+'/size'
            if not os.path.exists(path):
                cmd = ['blockdev', '--getsize', dev]
                out, err, ret = justcall(cmd)
                if ret != 0:
                    return 0
                return int(math.ceil(1.*int(out)/2048))

        with open(path, 'r') as f:
            size = f.read()
            f.close()
        return int(math.ceil(1.*int(size)/2048))

    def print_diskinfo(self, disk):
        name = os.path.basename(disk)
        info = {
          'dev': '',
          'size': 0,
          'device/vendor': '',
          'device/model': '',
        }
        for i in info:
            i_f = os.path.join(disk, i)
            if not os.path.exists(i_f):
                continue
            with open(i_f, 'r') as f:
                info[i] = f.read().strip()
        if '6900' in info['device/vendor']:
            info['device/vendor'] = 'Red Hat'
            if info['device/model'] is '':
                info['device/model'] = 'VirtIO'
        info['hbtl'] = os.path.basename(os.path.realpath(os.path.join(disk, "device")))
        print(self.print_diskinfo_fmt%(
          info['hbtl'],
          name,
          int(float(info['size'])/2//1024),
          info['dev'],
          info['device/vendor'],
          info['device/model'],
        ))

    def scanscsi(self):
        if not os.path.exists('/sys') or not os.path.ismount('/sys'):
            print("scanscsi is not supported without /sys mounted", file=sys.stderr)
            return 1

        import glob

        disks_before = glob.glob('/sys/block/sd*')
        disks_before += glob.glob('/sys/block/vd*')
        hosts = glob.glob('/sys/class/scsi_host/host*')

        for host in hosts:
            scan_f = host+'/scan'
            if not os.path.exists(scan_f):
                continue
            print("scan", os.path.basename(host))
            os.system('echo - - - >'+scan_f)

        disks_after = glob.glob('/sys/block/sd*')
        disks_after += glob.glob('/sys/block/vd*')
        new_disks = set(disks_after) - set(disks_before)

        self.print_diskinfo_header()
        #for disk in disks_before:
        for disk in new_disks:
            self.print_diskinfo(disk)

        return 0

if __name__ == "__main__":
    diskinfo = diskInfo()
    import glob
    disks = glob.glob('/sys/block/sd*')
    disks += glob.glob('/sys/block/vd*')
    diskinfo.print_diskinfo_header()
    for disk in disks:
         diskinfo.print_diskinfo(disk)
    #dev = '/dev/vda'
    #vendor = diskinfo.disk_vendor(dev)
    #model = diskinfo.disk_model(dev)
    #print("%s has vendor [%s] model [%s]" % (dev, vendor, model))
0707010000bbb7000081a400000000000000000000000154fc7bfb00000e36000000660000000300000000000000000000002700000000root/opt/opensvc/lib/rcDiskInfoOSF1.py    #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from __future__ import print_function
from rcUtilities import justcall
import rcDiskInfo
import re

regex = re.compile("^\W*[0-9]*:")
regex_path = re.compile('\W*[0-9]*\W+')

class diskInfo(rcDiskInfo.diskInfo):

    def __init__(self):
        self.load_cache()

    def is_id(self, line):
        if regex.match(line) is None:
            return False
        return True

    def cache_add(self, id, dev, wwid, path_count):
        d = self.devattr(id)
        vid = d['manufacturer']
        pid = d['model']
        size = d['mb']
        self.h[dev] = dict(
          wwid=wwid,
          vid=vid,
          pid=pid,
          size=size,
          id=id,
          path_count=path_count
        )

    def load_cache(self):
        self.h = {}
        cmd = ["hwmgr", "show", "scsi", "-type", "disk", "-active", "-full"]
        out, err, ret = justcall(cmd)
        path_count = -1
        for e in out.split('\n'):
            if len(e) == 0:
                continue
            if self.is_id(e):
                if path_count >= 0:
                    self.cache_add(id, dev, wwid, path_count)
                l = e.split()
                if len(l) < 8:
                    continue
                id = l[0].strip(':')
                dev = l[7]
                path_count = 0
            elif 'WWID' in e:
                wwid = e.split(":")[-1].replace('-','').lower()
                wwid = wwid.strip('"').replace(" ", "_")
            elif regex_path.match(e) is not None and 'valid' in e:
                path_count += 1
        if path_count >= 0:
            self.cache_add(id, dev, wwid, path_count)

    def devattr(self, id):
        d = {'capacity': 0, 'block_size': 0, 'manufacturer': '', 'model': '', 'mb': 0}
        cmd = ["hwmgr", "get", "att", "-id", id,
               "-a", "model",
               "-a", "manufacturer",
               "-a", "capacity",
               "-a", "block_size"]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return d
        for line in out.split("\n"):
            if not line.startswith(' '):
                continue
            l = line.split('=')
            if len(l) !=2:
                continue
            d[l[0].strip()] = l[1].strip()
        d['mb'] = int(d['capacity']) * int(d['block_size']) // 1024 // 1024
        return d
        
    def get(self, dev, type):
        dev = dev.replace('/dev/rdisk/','')
        dev = dev.replace('/dev/disk/','')
        if dev not in self.h:
            return
        return self.h[dev][type]

    def disk_id(self, dev):
        return self.get(dev, 'wwid')

    def disk_vendor(self, dev):
        return self.get(dev, 'vid')

    def disk_model(self, dev):
        return self.get(dev, 'pid')

    def disk_size(self, dev):
        return self.get(dev, 'size')

if __name__ == "__main__":
    di = diskInfo()
    print(di.h.items())
  0707010000bbb8000081a400000000000000000000000154fc7bfb00001487000000660000000300000000000000000000002800000000root/opt/opensvc/lib/rcDiskInfoSunOS.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import rcDiskInfo
from rcUtilities import justcall
import math
from rcGlobalEnv import rcEnv
from rcZone import is_zone

class diskInfo(rcDiskInfo.diskInfo):
    h = {}
    done = []

    def get_val(self, line):
        l = line.split(":")
        if len(l) != 2:
            return
        return l[-1].strip()

    def get_part_size(self, dev):
        part = dev[-1]
        size = 0
        cmd = ['prtvtoc', dev]
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return size

        bytes_per_sect = 0
        for line in out.split('\n'):
            if not line.startswith('*'):
                continue
            if "bytes/sector" in line:
                bytes_per_sect = int(line.split()[1])

        if bytes_per_sect == 0:
            return 0

        for line in out.split('\n'):
            if line.startswith('*'):
                continue

            l = line.split()
            if len(l) != 6:
                continue

            if l[0] != part:
                continue

            return math.ceil(1.*int(l[4])*bytes_per_sect/1024/1024)

        return 0

    def get_size(self, dev):
        size = 0
        cmd = ['prtvtoc', dev]
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return size

        """
        *     512 bytes/sector
        *      63 sectors/track
        *     255 tracks/cylinder
        *   16065 sectors/cylinder
        *   19581 cylinders
        *   19579 accessible cylinders
        **  OR:
        *   188743612 accessible sectors
        """
        for line in out.split('\n'):
            if not line.startswith('*'):
                continue
            try:
                if "bytes/sector" in line:
                    n1 = int(line.split()[1])
                if "accessible sectors" in line:
                    s0 = int(line.split()[1])
                    size = math.ceil(1. * s0 * n1 / 1024 / 1024)
                    break
                if "sectors/cylinder" in line:
                    n2 = int(line.split()[1])
                if "cylinders" in line:
                    n3 = int(line.split()[1])
                size = math.ceil(1. * n1 * n2 * n3 / 1024 / 1024)
            except:
                pass

        return size

    def __init__(self, deferred=False):
        self.zone = is_zone()
        self.deferred = deferred
        if deferred:
            return
        self.scan()

    def scan(self):
        if 'scan' in self.done:
            return
        self.done.append('scan')
        cmd = ["/usr/bin/find", "/dev/rdsk", "-name", "c*s2"]
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return
        lines = out.split('\n')
        if len(lines) < 2:
            return
        for e in lines:
            if "/dev/" not in e:
                continue
            
            dev = e.strip()
            self.scan_dev(dev)

    def scan_dev(self, dev):
        cmd = ["mpathadm", "show", "lu", dev]
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return
        if "Error: Logical-unit " + dev + " is not found" in err:
            dsk = dev.replace("/dev/rdsk/", "")
            dsk = dsk.replace("s2", "")
            wwid = rcEnv.nodename + "." + dsk
            vid = "LOCAL"
            pid = ""
            size = 0
        else:
            wwid = ""
            vid = ""
            pid = ""
            size = 0

            for line in out.split('\n'):
                if line.startswith("\tVendor:"):
                    vid = self.get_val(line)
                elif line.startswith("\tProduct:"):
                    pid = self.get_val(line)
                elif line.startswith("\tName:"):
                    wwid = self.get_val(line)

        size = self.get_size(dev)
        self.h[dev] = dict(wwid=wwid, vid=vid, pid=pid, size=size)

    def get(self, dev, type):
        if self.deferred:
            self.scan_dev(dev)
        dummy = dict(wwid="unknown", vid="unknown", pid="unknown", size=0)
        if dev not in self.h:
            if self.zone:
                return None
            return dummy[type]
        return self.h[dev][type]

    def disk_id(self, dev):
        return self.get(dev, 'wwid')

    def disk_vendor(self, dev):
        return self.get(dev, 'vid')

    def disk_model(self, dev):
        return self.get(dev, 'pid')

    def disk_size(self, dev):
        return self.get(dev, 'size')

 0707010000bbb9000081a400000000000000000000000154fc7bfb0000110b000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/rcDiskInfoWindows.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import rcDiskInfo
import wmi
from rcUtilities import justcall, which

class diskInfo(rcDiskInfo.diskInfo):

    def __init__(self):
        self.h = {}
        self.fcluns = {}
        self.wmi = wmi.WMI()

    def scan_mapping(self):
        if len(self.fcluns) > 0:
            return

        if not which('fcinfo'):
            return

        for index, portwwn, host in self._get_fc_hbas():
            cmd = ['fcinfo', '/mapping', '/ai:'+index]
            out, err, ret = justcall(cmd)
            if ret != 0:
                continue
            lines = out.split('\n')
            for i, line in enumerate(lines):
                if line.startswith('(  '):
                    l = line.split()
                    if len(l) < 3:
                        continue
                    bus = int(l[-3].strip(','))
                    target = int(l[-2].strip(','))
                    lun = int(l[-1].strip(')'))
                    _index = (host, bus, target, lun)
                elif line.startswith('(cs:'):
                    l = line.split()
                    if len(l) < 2:
                        continue
                    wwid = l[-1].strip(')')
                    self.fcluns[_index] = dict(wwid=wwid)

    def scan(self):
        self.scan_mapping()

        vid = 'unknown'
        pid = 'unknown'
        wwid = 'unknown'
        size = 'unknown'

        for drive in self.wmi.WIN32_DiskDrive():
            id = drive.DeviceID
            vid = str(drive.Manufacturer)
            pid = str(drive.Caption)
            try:
                serial = str(drive.SerialNumber)
            except:
                serial = "unknown"
            size = int(drive.Size) // 1024 // 1024
            host = drive.SCSIPort
            bus = drive.SCSIBus
            target = drive.SCSITargetId
            lun = drive.SCSILogicalUnit


            d = dict(id=id,
                     vid=vid,
                     pid=pid,
                     wwid=wwid,
                     serial=serial,
                     host=host,
                     bus=bus,
                     target=target,
                     lun=lun,
                     size=size)

            d['wwid'] = self.get_wwid(d)
            if d['wwid'] is None:
                d['wwid'] = d['serial']

            self.h[id] = d

    def get_wwid(self, d):
        index = (d['host'], d['bus'], d['target'], d['lun'])
        if index not in self.fcluns:
            return None
        return self.fcluns[index]['wwid']

    def get(self, id, prop):
        if len(self.h) == 0:
            self.scan()
        if id not in self.h:
            return None
        return self.h[id][prop]

    def disk_id(self, dev):
        return self.get(dev, 'wwid')

    def disk_vendor(self, dev):
        return self.get(dev, 'vid')

    def disk_model(self, dev):
        return self.get(dev, 'pid')

    def disk_size(self, dev):
        return self.get(dev, 'size')

    def _get_fc_hbas(self):
        hbas = []
        if not which('fcinfo'):
            return []
        cmd = ['fcinfo']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return []
        for line in out.split('\n'):
            if 'PortWWN' not in line:
                continue
            l = line.split()
            i = l.index('PortWWN:')
            if len(l) < i+2:
                continue
            index = l[0].split('-')[-1].strip(':')
            portwwn = l[i+1].replace(':', '')
            host = int(l[-1].split('Scsi')[-1].strip(':'))
            hbas.append((index, portwwn, host))
        return hbas

 0707010000bbba000081a400000000000000000000000154fc7bfb00001dd9000000660000000300000000000000000000002100000000root/opt/opensvc/lib/rcDocker.py  #
# Copyright (c) 2014 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2014 Arnaud Veron <arnaud.veron@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
from datetime import datetime
from subprocess import *

import rcStatus
import json
import re
from rcUtilities import which, justcall
from rcGlobalEnv import rcEnv
import rcExceptions as ex

from svcBuilder import conf_get_string_scope

os.environ['LANG'] = 'C'

class DockerLib(object):

    def get_container_id_by_name(self):
        cmd = self.docker_cmd + ['ps', '-a']
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError(err)
        lines = out.split('\n')
        if len(lines) < 2:
            return
        try:
            start = lines[0].index('NAMES')
        except:
            return
        for line in lines[1:]:
            names = line[start:].strip().split(',')
            if self.container_name in names:
                return line.split()[0]

    def docker(self, action):
        cmd = self.docker_cmd + []
        if action == 'start':
            if self.container_id is None:
                self.container_id = self.get_container_id_by_name()
            if self.container_id is None:
                cmd += ['run', '-t', '-i', '-d', '--name='+self.container_name]
                cmd += self.add_run_args()
                cmd += [self.run_image]
                if self.run_command is not None and self.run_command != "":
                    cmd += [self.run_command]
            else:
                cmd += ['start', self.container_id]
        elif action == 'stop':
            cmd += ['stop', self.container_id]
        elif action == 'kill':
            cmd += ['kill', self.container_id]
        else:
            self.log.error("unsupported docker action: %s" % action)
            return 1

        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

        if action == 'start':
            self.container_id = self.get_container_id_by_name()

    def get_run_image_id(self, run_image=None):
        if run_image is None and hasattr(self, "run_image"):
            run_image = self.run_image
        if len(run_image) == 12 and re.match('^[a-f0-9]*$', run_image):
            return run_image
        try:
            image_name, image_tag = run_image.split(':')
        except:
            image_name, image_tag = [run_image, "latest"]

        cmd = self.docker_cmd + ['images', image_name]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return run_image
        for line in out.split('\n'):
            l = line.split()
            if len(l) < 3:
                continue
            if l[0] == image_name and l[1] == image_tag:
                return l[2]
        return run_image

    def image_userfriendly_name(self):
        if ':' in self.run_image:
            return self.run_image
        cmd = self.docker_cmd + ['images']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return self.run_image
        for line in out.split('\n'):
            l = line.split()
            if len(l) < 3:
                continue
            if l[2] == self.run_image:
                return l[0]+':'+l[1]
        return self.run_image
        
    def docker_inspect(self, id):
        cmd = self.docker_cmd + ['inspect', id]
        out, err, ret = justcall(cmd)
        data = json.loads(out)
        return data[0]

    def docker_stop(self):
        if not self.docker_running():
            return
        if self.docker_data_dir is None:
            return
        if not os.path.exists(self.docker_pid_file):
            return

        cmd = self.docker_cmd + ['ps', '-q']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return

        if len(out) > 0:
            # skip stop ... daemon still handles containers
            return

        try:
            with open(self.docker_pid_file, 'r') as f:
                pid = int(f.read())
        except:
            self.log.warning("can't read %s. skip docker daemon kill" % self.docker_pid_file)
            return

        self.log.info("no more container handled by docker daemon. shut it down")
        import signal
        os.kill(pid, signal.SIGTERM)

    def docker_start(self, verbose=True):
        # Sanity checks before deciding to start the daemon
        if self.docker_running():
            return

        if self.docker_data_dir is None:
            return

        resource = self.docker_data_dir_resource()
        if resource is not None and resource._status() not in (rcStatus.UP, rcStatus.STDBY_UP):
            state= rcStatus.status_str(resource._status())
            self.log.warning("the docker daemon data dir is handled by the %s resource in %s state. can't start the docker daemon" % (resource.rid, state))
            return

        # Now we can start the daemon, creating its data dir if necessary
        cmd = self.docker_cmd + ['-r=false', '-d',
               '-g', self.docker_data_dir,
               '-p', self.docker_pid_file]
        cmd += self.docker_daemon_args

        if verbose:
            self.log.info("starting docker daemon")
            self.log.info(" ".join(cmd))
        import subprocess
        subprocess.Popen(['nohup'] + cmd,
                 stdout=open('/dev/null', 'w'),
                 stderr=open('/dev/null', 'a'),
                 preexec_fn=os.setpgrp
                 )

        import time
        for i in range(self.max_wait_for_dockerd):
            if self.docker_running():
                self.container_id = self.get_container_id_by_name()
                return
            time.sleep(1)

    def docker_running(self):
        cmd = self.docker_cmd + ['info']
        out, err, ret = justcall(cmd)
        if ret != 0:
            return False
        return True

    def on_add(self):
        self.docker_var_d = os.path.join(rcEnv.pathvar, self.svc.svcname)
        if not os.path.exists(self.docker_var_d):
            os.makedirs(self.docker_var_d)
        self.docker_pid_file = os.path.join(self.docker_var_d, 'docker.pid')
        self.docker_socket = os.path.join(self.docker_var_d, 'docker.sock')
        self.docker_socket_uri = 'unix://' + self.docker_socket
        try:
            self.docker_data_dir = conf_get_string_scope(self.svc, self.svc.config, 'DEFAULT', 'docker_data_dir')
        except ex.OptNotFound:
            self.docker_data_dir = None
        try:
            self.docker_daemon_args = conf_get_string_scope(self.svc, self.svc.config, 'DEFAULT', 'docker_daemon_args').split()
        except ex.OptNotFound:
            self.docker_daemon_args = []
        self.docker_cmd = [self.docker_exe(), '-H', self.docker_socket_uri]

    def docker_exe(self):
        if which("docker.io"):
            return "docker.io"
        elif which("docker"):
            return "docker"
        else:
            raise ex.excInitError("docker executable not found")


   0707010000bbbb000081a400000000000000000000000154fc7bfb000009c3000000660000000300000000000000000000002200000000root/opt/opensvc/lib/rcEthtool.py # Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from rcUtilities import justcall, which

"""
Settings for eth0:
        Supported ports: [ TP ]
        Supported link modes:   1000baseT/Full
                                10000baseT/Full
        Supports auto-negotiation: Yes
        Advertised link modes:  1000baseT/Full
                                10000baseT/Full
        Advertised pause frame use: No
        Advertised auto-negotiation: No
        Speed: 5000Mb/s
        Duplex: Full
        Port: Twisted Pair
        PHYAD: 0
        Transceiver: internal
        Auto-negotiation: on
        MDI-X: Unknown
        Supports Wake-on: g
        Wake-on: d
        Link detected: yes
"""

class LoadError(Exception):
    pass

class Ethtool(object):
    def __init__(self, intf):
        self.intf = intf
        self.data = {}

    def __getattr__(self, attr):
        if len(self.data) == 0:
            self.load()
        if attr not in self.data:
            return None
        return self.data[attr]

    def load(self):
        if not which('ethtool'):
            raise LoadError("ethtool is not installed")
        cmd = ['ethtool', self.intf]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise LoadError("ret=%d\nout=%s\nerr=%s\n"%(ret, out, err))
        for line in out.split('\n'):
            if not line.startswith('\t'):
                continue
            l = line.split(': ')
            if len(l) != 2:
                continue
            param = l[0].strip().replace(" ", "_").replace("-", "_").lower()
            value = l[1].strip()
            self.data[param] = value

if __name__ == "__main__":
    o = Ethtool("eth0")
    o.load()
    for attr in o.data.keys():
        print("%-30s: %s"%(attr, str(o.data[attr])))

 0707010000bbbc000081a400000000000000000000000154fc7bfb00000fe5000000660000000300000000000000000000001e00000000root/opt/opensvc/lib/rcEva.py from rcUtilities import justcall, which
from xml.etree.ElementTree import XML, fromstring
import rcExceptions as ex
import os
import ConfigParser

pathlib = os.path.dirname(__file__)
pathbin = os.path.realpath(os.path.join(pathlib, '..', 'bin'))
pathetc = os.path.realpath(os.path.join(pathlib, '..', 'etc'))
pathtmp = os.path.realpath(os.path.join(pathlib, '..', 'tmp'))
if pathbin not in os.environ['PATH']:
    os.environ['PATH'] += ":"+pathbin

def sssu(cmd, manager, username, password, array=None):
    os.chdir(pathtmp)
    _cmd = ['sssu',
            "select manager %s username=%s password=%s"%(manager, username, password)]
    if array is not None:
        _cmd += ["select system %s"%array]
    _cmd += [cmd]
    out, err, ret = justcall(_cmd)
    if "Error" in out:
        print(_cmd)
        print(out)
        raise ex.excError("sssu command execution error")
    return out, err

class Evas(object):
    arrays = []

    def __init__(self, objects=[]):
        self.objects = objects
        if len(objects) > 0:
            self.filtering = True
        else:
            self.filtering = False
        self.index = 0
        cf = os.path.join(pathetc, "sssu.conf")
        if not os.path.exists(cf):
            return
        conf = ConfigParser.RawConfigParser()
        conf.read(cf)
        m = {}
        for s in conf.sections():
            try:
                manager = conf.get(s, 'manager')
                username = conf.get(s, 'username')
                password = conf.get(s, 'password')
                m[manager] = [username, password]
            except:
                print("error parsing section", s)
                pass
        del(conf)
        done = []
        for manager, creds in m.items():
            username, password = creds
            out, err = sssu('ls system', manager, username, password)
            _in = False
            for line in out.split('\n'):
                if 'Systems avail' in line:
                    _in = True
                    continue
                if not _in:
                    continue
                name = line.strip()
                if self.filtering and name not in self.objects:
                    continue
                self.arrays.append(Eva(name, manager, username, password))
                done.append(name)

    def __iter__(self):
        return self

    def next(self):
        if self.index == len(self.arrays):
            raise StopIteration
        self.index += 1
        return self.arrays[self.index-1]

class Eva(object):
    def __init__(self, name, manager, username, password):
        self.name = name
        self.manager = manager
        self.username = username
        self.password = password
        #self.keys = ['disk_group']
        self.keys = ['controller', 'disk_group', 'vdisk']

    def sssu(self, cmd):
        return sssu(cmd, self.manager, self.username, self.password, array=self.name)

    def stripxml(self, buff):
        try:
            buff = buff[buff.index("<object>"):]
        except:
            buff = ""
        lines = buff.split('\n')
        for i, line in enumerate(lines):
            if line.startswith("\\"):
                del lines[i]
        lines = ['<main>'] + lines + ['</main>']
        return '\n'.join(lines)

    def get_controller(self):
        cmd = 'ls controller full xml'
        print("%s: %s"%(self.name, cmd))
        buff = self.sssu(cmd)[0]
        return self.stripxml(buff)

    def get_disk_group(self):
        cmd = 'ls disk_group full xml'
        print("%s: %s"%(self.name, cmd))
        buff = self.sssu(cmd)[0]
        return self.stripxml(buff)

    def get_vdisk(self):
        cmd = 'ls vdisk full xml'
        print("%s: %s"%(self.name, cmd))
        buff = self.sssu(cmd)[0]
        return self.stripxml(buff)

    def get_lun(self):
        cmd = 'ls lun full xml'
        print("%s: %s"%(self.name, cmd))
        buff = self.sssu(cmd)[0]
        return self.stripxml(buff)

if __name__ == "__main__":
    o = Evas()
    for eva in o:
        print(eva.get_controller())
   0707010000bbbd000081a400000000000000000000000154fc7bfb00000f15000000660000000300000000000000000000002500000000root/opt/opensvc/lib/rcExceptions.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

class excEncapUnjoignable(Exception):
    pass

class excError(Exception):
    """ Failed action
    """
    def __init__(self, value=""):
        self.value = value
    def __str__(self):
        return str(self.value)

class OptNotFound(Exception):
    """ Service config file option not found
    """

class excSignal(Exception):
    """ Termination signal received
    """

class excUndefined(Exception):
    """ Mandatory Undefined action exception
    """
    def __init__(self,action=None,className=None,func=None):
        self.action=action
        self.className=className
        self.func=func
    def __str__(self):
        return "Undefined mandatory Action %s for className %s in function %s" % \
                (self.action,self.className,self.func)

class syncNoNodesToSync(Exception):
    """ No nodes to sync => abort stacked syncs resource actions
    """

class syncNoFilesToSync(Exception):
    """ No files to sync => move on to the next stacked syncs resource actions
    """

class syncConfigSyntaxError(Exception):
    """ Bogus configuration syntax => abort all
    """

class syncNotSnapable(Exception):
    """ A dir/file specified as source of a sync with snap is not included in a 
        snapable resource mount => abort all
    """

class syncSnapExists(Exception):
    """ The snapshot already exists
    """

class syncSnapCreateError(Exception):
    """ Error in snapshot creation => clean up
    """

class syncSnapDestroyError(Exception):
    """ Error in snapshot destroy => clean up
    """

class syncSnapMountError(Exception):
    """ Error mounting fs => clean up
    """

class excEndAction(Exception):
    """ End multi-resource action. Not an error.
    """

class excAbortAction(Exception):
    """ Abort multi-resource action
    """

class excInitError(Exception):
    """ Resource initialisation error
    """

class excScsiPrNotsupported(Exception):
    """ Scsi persistent reservation is not supported
    """

class excNotAvailable(Exception):
    """ Not available
    """

class excNotSupported(Exception):
    """ Not supported
    """

class excBug(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

class MissImpl(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

class IpDevDown(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

class IpConflict(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

class IpAlreadyUp(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

class IpNoActions(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)
   0707010000bbbe000081a400000000000000000000000154fc7bfb00001907000000660000000300000000000000000000002400000000root/opt/opensvc/lib/rcGlobalEnv.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
"""module rcGlobalEnv module define rcEnv class
   rcEnv class attribute may be updated with rcLocalEnv module if present
   rcLocalEnv module is not provided with opensvc and allow customers to
   redefine following vars:
       o dbopensvc_host
       o dbopensvc_port
       o rsh
       o rcp
   rcLocalEnv.py may be installed into path_opensvc/lib
"""
import sys
import os
import platform
import socket

class rcEnv:
    """Class to store globals
    """

    logging_initialized = []
    allowed_svctype = ['PRD', 'PPRD', 'REC', 'INT', 'DEV', 'TST', 'TMP', 'DRP', 'FOR', 'PRA', 'PRJ']
    _platform = sys.platform
    sysname, x, x, x, machine, x = platform.uname()
    nodename = socket.gethostname().lower()
    fqdn = socket.getfqdn().lower()
    listener_port = 1214

    """program used to execute remote command on other nodes or virtual hosts
    """
    if _platform == "sunos5" :
        if os.path.exists('/usr/local/bin/ssh'):
            rsh = "/usr/local/bin/ssh -q -o StrictHostKeyChecking=no -o ForwardX11=no -o BatchMode=yes -o ConnectTimeout=10"
            rcp = "/usr/local/bin/scp -q -o StrictHostKeyChecking=no -o ForwardX11=no -o BatchMode=yes -o ConnectTimeout=10"
        else:
            rsh = "/usr/bin/ssh -q -o StrictHostKeyChecking=no -o ForwardX11=no -o BatchMode=yes -n"
            rcp = "/usr/bin/scp -q -o StrictHostKeyChecking=no -o ForwardX11=no -o BatchMode=yes"
    elif os.path.exists('/etc/vmware-release'):
        rsh = "/usr/bin/ssh -q -o StrictHostKeyChecking=no -o ForwardX11=no -o BatchMode=yes"
        rcp = "/usr/bin/scp -q -o StrictHostKeyChecking=no -o ForwardX11=no -o BatchMode=yes"
    elif sysname == 'OSF1':
        rsh = "ssh -o StrictHostKeyChecking=no -o ForwardX11=no -o BatchMode=yes -o ConnectTimeout=10"
        rcp = "scp -o StrictHostKeyChecking=no -o ForwardX11=no -o BatchMode=yes -o ConnectTimeout=10"
    else:
        rsh = "/usr/bin/ssh -q -o StrictHostKeyChecking=no -o ForwardX11=no -o BatchMode=yes -o ConnectTimeout=10"
        rcp = "/usr/bin/scp -q -o StrictHostKeyChecking=no -o ForwardX11=no -o BatchMode=yes -o ConnectTimeout=10"

    """Database sink for node and service configurations and status collection.
    """
    dbopensvc_host = "dbopensvc"
    dbopensvc_port = "8000"
    dbopensvc_app = "feed"
    dbcompliance_host = dbopensvc_host
    dbcompliance_port = dbopensvc_port
    dbcompliance_app = "init"

    """EZ-HA defines. EZ-HA does heartbeat, stonith, automatic service failover

    ez_last_chance == True:
        check_up_script_gen.sh will try a ping + RSH other node before stonith
    ez_startapp_bg == True:
        startapp in background if EZ-HA take-over is succesful
    """
    ez_path = "/usr/local/cluster"
    ez_path_services = ez_path + "/conf/services"
    ez_last_chance = True
    ez_startapp_bg = True

    """Directory on DRP node where to store the PRD nodes files necessary
    for takeover.
    """
    drp_path = "/opt/opensvc/var/cache/"
    drp_sync_excludes = [
        '--exclude="/spice"',
        '--exclude="/dbadm"',
        '--exclude="*.dmp"',
        '--exclude="*.dbf"',
        '--exclude="*.rdo"',
        '--exclude="*.log"',
        '--exclude="*.Z"',
        '--exclude="*.gz"',
        '--exclude="*.tgz"',
        '--exclude="*.tar"',
        '--exclude="*.tmp"',
        '--exclude="/oracle/ficimp"',
        '--exclude="/oracle/tmp"',
        '--exclude="/oracle/LOG/*"',
        '--exclude="/oracle/product/*/network/log/listener*.log"',
    ]
    drp_sync_etc_solaris = [
        "/etc/inet",
        "/etc/inetd.conf",
        "/etc/defaultdomain",
        "/etc/lp",
        "/etc/printers.conf",
        "/etc/system",
        "/etc/auto_master",
        "/etc/auto_home",
        "/etc/hosts.equiv",
        "/etc/pam.conf",
        "/etc/cron.d",
    ]
    drp_sync_etc_linux = [
        # linux
        "/etc/xinetd.d",
        "/etc/xinetd.conf",
        "/etc/sysconfig",
        "/etc/cups",
        "/etc/auto.master",
        "/etc/auto.misc",
        "/etc/listener.ora",
        "/etc/oratab",
        "/etc/sqlnet.ora",
        "/etc/tnsnames.ora",
        "/etc/yp.conf",
        "/etc/pam.d",
        "/etc/cron.allow",
        "/etc/cron.deny",
    ]
    drp_sync_etc_common = [
        # common
        "/etc/shadow",
        "/etc/passwd",
        "/etc/group",
        "/etc/syslog.conf",
        "/etc/services",
        "/etc/hosts",
        "/etc/nsswitch.conf",
        "/etc/sudoers",
        "/etc/project",
        "/etc/user_attr",
        "/etc/ssh",
        "/etc/centrifydc",
        "/etc/krb5*",
        "/etc/sudoers",
    ]
    drp_sync_misc = [
        "/var/centrifydc",
        "/var/opt/oracle",
        "/var/spool/cron",
        "/var/spool/cron/crontabs",
        "/var/yp/binding",
        "/usr/local/oraenv", "/usr/local/coraenv", "/usr/local/dbhome",
        "/usr/local/etc/sudoers",
    ]
    drp_sync_files = [
        [drp_sync_etc_solaris + drp_sync_etc_linux + drp_sync_etc_common + drp_sync_misc, []],
        [["/home/oracle", "/home/sybase", "/opt/oracle", "/opt/sybase"], drp_sync_excludes],
    ]

    vt_cloud = ['vcloud', 'openstack', 'amazon']
    vt_libvirt = ['kvm']
    vt_vm = ['ldom', 'hpvm', 'kvm', 'xen', 'vbox', 'ovm', 'esx'] + vt_cloud
    vt_container = ['zone', 'lxc', 'jail', 'vz', 'srp', 'docker']
    vt_supported = vt_vm + vt_container

    dbopensvc = "http://%s:%s/%s/default/call/xmlrpc"%(dbopensvc_host, dbopensvc_port, dbopensvc_app)
    dbcompliance = "http://%s:%s/%s/compliance/call/xmlrpc"%(dbcompliance_host, dbcompliance_port, dbcompliance_app)

 0707010000bbbf000081a400000000000000000000000154fc7bfb00000dd9000000660000000300000000000000000000001e00000000root/opt/opensvc/lib/rcHds.py from rcUtilities import which, justcall
import rcExceptions as ex
import os
import ConfigParser

pathlib = os.path.dirname(__file__)
pathbin = os.path.realpath(os.path.join(pathlib, '..', 'bin'))
pathetc = os.path.realpath(os.path.join(pathlib, '..', 'etc'))
pathtmp = os.path.realpath(os.path.join(pathlib, '..', 'tmp'))

def _cmd(cmd, url, username, password, serial, bin):
    if which(bin) is None:
        print("Can not find %s"%bin)
        raise ex.excError
    l = [bin, url, cmd[0],
         "-u", username,
         "-p", password,
         "serialnum="+serial]
    if len(cmd) > 1:
        l += cmd[1:]
    out, err, ret = justcall(l)
    if ret != 0:
        raise ex.excError(err)
    return out, err, ret

class Hdss(object):
    arrays = []
    def __init__(self, objects=[]):
        self.objects = objects
        if len(objects) > 0:
            self.filtering = True
        else:
            self.filtering = False
        self.index = 0

        cf = os.path.join(pathetc, "auth.conf")
        if not os.path.exists(cf):
            return
        conf = ConfigParser.RawConfigParser()
        conf.read(cf)
        m = []
        for s in conf.sections():
            try:
                stype = conf.get(s, 'type')
            except:
                continue
            if stype != "hds":
                continue
            try:
                bin = conf.get(s, 'bin')
            except:
                bin = None
            try:
                url = conf.get(s, 'url')
                arrays = conf.get(s, 'array').split()
                username = conf.get(s, 'username')
                password = conf.get(s, 'password')
                m += [(url, arrays, username, password, bin)]
            except:
                print("error parsing section", s)
                pass

        del(conf)
        done = []
        for url, arrays, username, password, bin in m:
            for name in arrays:
                if self.filtering and name not in self.objects:
                    continue
                if name in done:
                    continue
                self.arrays.append(Hds(name, url, username, password, bin))
                done.append(name)

    def __iter__(self):
        return self

    def next(self):
        if self.index == len(self.arrays):
            raise StopIteration
        self.index += 1
        return self.arrays[self.index-1]

class Hds(object):
    def __init__(self, serial, url, username, password, bin=None):
        self.keys = ['lu', 'arraygroup', 'port']
        self.name = serial
        self.serial = serial
        self.url = url
        self.username = username
        self.password = password
        if bin is None:
            self.bin = "HiCommandCLI"
        else:
            self.bin = bin

    def _cmd(self, cmd):
        return _cmd(cmd, self.url, self.username, self.password, self.serial, self.bin)

    def get_lu(self):
        cmd = ['GetStorageArray', 'subtarget=Logicalunit', 'lusubinfo=Path,LDEV,VolumeConnection']
        print(' '.join(cmd))
        out, err, ret = self._cmd(cmd)
        return out

    def get_arraygroup(self):
        cmd = ['GetStorageArray', 'subtarget=ArrayGroup']
        print(' '.join(cmd))
        out, err, ret = self._cmd(cmd)
        return out

    def get_port(self):
        cmd = ['GetStorageArray', 'subtarget=Port']
        print(' '.join(cmd))
        out, err, ret = self._cmd(cmd)
        return out

if __name__ == "__main__":
    o = Hdss()
    for hds in o:
        print(hds.get_lu())

   0707010000bbc0000081a400000000000000000000000154fc7bfb00002549000000660000000300000000000000000000002100000000root/opt/opensvc/lib/rcHp3par.py  import os
import json
import rcExceptions as ex
import ConfigParser
from subprocess import *
import time
import urllib
import urllib2

pathlib = os.path.dirname(__file__)
pathbin = os.path.realpath(os.path.join(pathlib, '..', 'bin'))
pathetc = os.path.realpath(os.path.join(pathlib, '..', 'etc'))
pathtmp = os.path.realpath(os.path.join(pathlib, '..', 'tmp'))
if pathbin not in os.environ['PATH']:
    os.environ['PATH'] += ":"+pathbin

def reformat(s):
    lines = s.split('\n')
    for i, line in enumerate(lines):
        if '%' in line:
            # skip prompt
            x = line.index("%") + 2
            if x < len(line):
                line = line[x:]
            elif x == len(line):
                line = ""
        lines[i] = line
    s = '\n'.join(lines)
    s = s.replace("Pseudo-terminal will not be allocated because stdin is not a terminal.", "")
    return s.strip()

def proxy_cmd(cmd, array, manager, svcname, uuid=None, log=None):
    url = 'https://%s/api/cmd/' % manager
    user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
    header = { 'User-Agent' : user_agent }

    values = {
      'array' : array,
      'cmd' : cmd,
      'svcname' : svcname,
      'uuid' : uuid,
    }

    data = urllib.urlencode(values)
    req = urllib2.Request(url, data, header)

    try:
        f = urllib2.build_opener().open(req)
        response = f.read()
        #response = urllib2.urlopen(req)
    except Exception as e:
        return "", str(e)
    
    try:
        d = json.loads(response)
        ret = d['ret']
        out = d['out']
        err = d['err']
    except:
        ret = 1
        out = ""
        err = "unexpected proxy response format (not json)"

    if ret != 0:
        raise ex.excError("proxy error: %s" % err)

    return out, err

def cli_cmd(cmd, array, pwf, log=None):
    cmd = ['cli', '-sys', array, '-pwf', pwf, '-nohdtot', '-csvtable'] + cmd.split()
    p = Popen(cmd, stdout=PIPE, stderr=PIPE)
    out, err = p.communicate()
    out = reformat(out)
    err = reformat(err)

    if p.returncode != 0:
        if ("Connection closed by remote host" in err or "Too many local CLI connections." in err) and retry > 0:
            if log is not None:
                log.info("3par connection refused. try #%d" % retry)
            time.sleep(1)
            return _rcmd(_cmd, cmd, log=log, retry=retry-1)
        if log is not None:
            if len(out) > 0: log.info(out)
            if len(err) > 0: log.error(err)
        else:
            print(cmd)
            print(out)
        raise ex.excError("3par command execution error")

    return out, err

def ssh_cmd(cmd, manager, username, key, log=None):
    _cmd = ['ssh', '-i', key, '@'.join((username, manager))]
    cmd = 'setclienv csvtable 1 ; setclienv nohdtot 1 ; ' + cmd + ' ; exit'
    return _rcmd(_cmd, cmd, log=log)

def _rcmd(_cmd, cmd, log=None, retry=10):
    p = Popen(_cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE)
    p.stdin.write(cmd)
    out, err = p.communicate()
    out = reformat(out)
    err = reformat(err)

    if p.returncode != 0:
        if ("Connection closed by remote host" in err or "Too many local CLI connections." in err) and retry > 0:
            if log is not None:
                log.info("3par connection refused. try #%d" % retry)
            time.sleep(1)
            return _rcmd(_cmd, cmd, log=log, retry=retry-1)
        if log is not None:
            if len(out) > 0: log.info(out)
            if len(err) > 0: log.error(err)
        else:
            print(cmd)
            print(out)
        raise ex.excError("3par command execution error")

    return out, err

class Hp3pars(object):
    allowed_methods = ("ssh", "proxy")

    def __init__(self, objects=[]):
        self.objects = objects
        if len(objects) > 0:
            self.filtering = True
        else:
            self.filtering = False
        self.arrays = []
        self.index = 0
        cf = os.path.join(pathetc, "auth.conf")
        if not os.path.exists(cf):
            return
        conf = ConfigParser.RawConfigParser()
        conf.read(cf)
        m = {}

        for s in conf.sections():
            if not conf.has_option(s, "type") or \
               conf.get(s, "type") != "hp3par":
                continue

            if self.filtering and not s in self.objects:
                continue

            username = None
            manager = None
            key = None
            pwf = None

            try:
                method = conf.get(s, 'method')
            except:
                method = "ssh"

            if method not in self.allowed_methods:
                print("invalid method. allowed methods: %s" % ', '.join(self.allowed_methods))
                continue

            kwargs = {}
            try:
                manager = conf.get(s, 'manager')
                kwargs['manager'] = manager
            except:
                if method in ("proxy", "ssh"):
                    print("error parsing section", s)
                    continue

            try:
                username = conf.get(s, 'username')
                key = conf.get(s, 'key')
                kwargs['username'] = username
                kwargs['key'] = key
            except:
                if method in ("ssh"):
                    print("error parsing section", s)
                    continue

            try:
                key = conf.get(s, 'pwf')
                kwargs['pwf'] = pwf
            except:
                if method in ("cli"):
                    print("error parsing section", s)
                    continue

            self.arrays.append(Hp3par(s, method, **kwargs))

        del(conf)

    def __iter__(self):
        return self

    def next(self):
        if self.index == len(self.arrays):
            raise StopIteration
        self.index += 1
        return self.arrays[self.index-1]

class Hp3par(object):
    def __init__(self, name, method, manager=None, username=None, key=None, pwf=None, svcname=""):
        self.name = name
        self.manager = manager
        self.method = method
        self.username = username
        self.pwf = pwf
        self.svcname = svcname
        self.key = key
        self.keys = ['showvv', 'showsys', 'shownode', "showcpg", "showport", "showversion"]
        self.uuid = None

    def get_uuid(self):
        if self.uuid is not None:
            return self.uuid
        try:
            import ConfigParser
        except ImportError:
            import configparser as ConfigParser
        pathetc = os.path.join(os.path.dirname(__file__), '..', 'etc')
        nodeconf = os.path.join(pathetc, 'node.conf')
        config = ConfigParser.RawConfigParser()
        config.read(nodeconf)
        try:
            self.uuid = config.get("node", "uuid")
        except:
            pass
        return self.uuid

    def rcmd(self, cmd, log=None):
        if self.method == "ssh":
            return ssh_cmd(cmd, self.manager, self.username, self.key, log=log)
        elif self.method == "cli":
            return cli_cmd(cmd, self.name, self.pwf, log=log)
        elif self.method == "proxy":
            self.get_uuid()
            return proxy_cmd(cmd, self.name, self.manager, self.svcname, uuid=self.uuid, log=log)
        else:
            raise ex.excError("unsupported method %s set in auth.conf for array %s" % (self.method, self.name))

    def serialize(self, s, cols):
        l = []
        for line in s.split('\n'):
            v = line.split(',')
            h = {}
            for a, b in zip(cols, v):
                h[a] = b
            if len(h) > 1:
                l.append(h)
        return json.dumps(l)

    def get_showvv(self):
        cols = ["Name", "VV_WWN", "Prov", "CopyOf", "Tot_Rsvd_MB", "VSize_MB", "UsrCPG", "CreationTime", "RcopyGroup", "RcopyStatus"]
        cmd = 'showvv -showcols ' + ','.join(cols)
        print("%s: %s"%(self.name, cmd))
        s = self.rcmd(cmd)[0]
        return self.serialize(s, cols)

    def get_showsys(self):
        cols = ["ID", "Name", "Model", "Serial", "Nodes", "Master", "TotalCap", "AllocCap", "FreeCap", "FailedCap"]
        cmd = 'showsys'
        print("%s: %s"%(self.name, cmd))
        s = self.rcmd(cmd)[0]
        return self.serialize(s, cols)

    def get_shownode(self):
        cols = ["Available_Cache", "Control_Mem", "Data_Mem", "InCluster", "LED", "Master", "Name", "Node", "State"]
        cmd = 'shownode -showcols ' + ','.join(cols)
        print("%s: %s"%(self.name, cmd))
        s = self.rcmd(cmd)[0]
        return self.serialize(s, cols)

    def get_showcpg(self):
        cols = ["Id", "Name", "Warn%", "VVs", "TPVVs", "Usr", "Snp", "Total", "Used", "Total", "Used", "Total", "Used"]
        cmd = 'showcpg'
        print("%s: %s"%(self.name, cmd))
        s = self.rcmd(cmd)[0]
        return self.serialize(s, cols)

    def get_showport(self):
        cols = ["N:S:P", "Mode", "State", "Node_WWN", "Port_WWN", "Type", "Protocol", "Label", "Partner", "FailoverState"]
        cmd = 'showport'
        print("%s: %s"%(self.name, cmd))
        s = self.rcmd(cmd)[0]
        return self.serialize(s, cols)

    def get_showversion(self):
        cmd = 'showversion -s'
        print("%s: %s"%(self.name, cmd))
        s = self.rcmd(cmd)[0].strip("\n")
        return json.dumps({"Version": s})

if __name__ == "__main__":
    o = Hp3pars()
    for hp3par in o:
        print(hp3par.get_showvv())
        print(hp3par.get_showsys())
        print(hp3par.get_shownode())
        print(hp3par.get_showcpg())
   0707010000bbc1000081a400000000000000000000000154fc7bfb00000eb8000000660000000300000000000000000000002000000000root/opt/opensvc/lib/rcIbmDs.py   import rcExceptions as ex
import os
import ConfigParser
import tempfile
import sys
from subprocess import *

pathlib = os.path.dirname(__file__)
pathbin = os.path.realpath(os.path.join(pathlib, '..', 'bin'))
pathetc = os.path.realpath(os.path.join(pathlib, '..', 'etc'))
pathtmp = os.path.realpath(os.path.join(pathlib, '..', 'tmp'))
pathvar = os.path.realpath(os.path.join(pathlib, '..', 'var'))
if pathbin not in os.environ['PATH']:
    os.environ['PATH'] += ":"+pathbin

def dscli(cmd, hmc1, hmc2, username, pwfile, log=None):
    if len(hmc1) != 0:
       _hmc1 = ['-hmc1', hmc1]
    else:
       _hmc1 = []
    if len(hmc2) != 0:
       _hmc2 = ['-hmc2', hmc2]
    else:
       _hmc2 = []
    _cmd = ['/opt/ibm/dscli/dscli'] + _hmc1 + _hmc2 +['-user', username, '-pwfile', pwfile]
    if log is not None:
        log.info(cmd + ' | ' + ' '.join(_cmd))
    p = Popen(_cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    out, err = p.communicate(input=cmd)
    out = out.replace("dscli>", "")
    err = err.replace("dscli>", "")
    if log is not None:
        if len(out) > 0:
            log.info(out)
        if len(err) > 0:
            log.error(err)
    if p.returncode != 0:
        #print >>sys.stderr, out, err
        raise ex.excError("dscli command execution error")
    return out, err

class IbmDss(object):
    def __init__(self, objects=[]):
        self.objects = objects
        if len(objects) > 0:
            self.filtering = True
        else:
            self.filtering = False
        self.arrays = []
        self.index = 0
        cf = os.path.join(pathetc, "auth.conf")
        if not os.path.exists(cf):
            return
        conf = ConfigParser.RawConfigParser()
        conf.read(cf)
        m = {}
        for s in conf.sections():
            if not conf.has_option(s, "type") or \
               conf.get(s, "type") != "ibmds":
                continue
            if self.filtering and not s in self.objects:
                continue
            pwfile = os.path.join(pathvar, s+'.pwfile')
            if not os.path.exists(pwfile):
                raise ex.excError("%s does not exists. create it with 'dscli managepwfile ...'"%pwfile)

            try:
                username = conf.get(s, 'username')
                hmc1 = conf.get(s, 'hmc1')
                hmc2 = conf.get(s, 'hmc2')
                m[s] = [hmc1, hmc2, username, pwfile]
            except Exception as e:
                print("error parsing section", s, ":", e)
                pass
        del(conf)
        for name, creds in m.items():
            hmc1, hmc2, username, pwfile = creds
            self.arrays.append(IbmDs(name, hmc1, hmc2, username, pwfile))

    def __iter__(self):
        return self

    def get(self, array):
        for o in self.arrays:
            if o.name == array:
                return o
        raise ex.excError("%s not defined in auth.conf or not usable" % array)

    def next(self):
        if self.index == len(self.arrays):
            raise StopIteration
        self.index += 1
        return self.arrays[self.index-1]

class IbmDs(object):
    def __init__(self, name, hmc1, hmc2, username, pwfile):
        self.name = name
        self.username = username
        self.pwfile = pwfile
        self.hmc1 = hmc1
        self.hmc2 = hmc2
        self.keys = ['combo']

    def dscli(self, cmd, log=None):
        return dscli(cmd, self.hmc1, self.hmc2, self.username, self.pwfile, log=log)

    def get_combo(self):
        cmd = """setenv -banner off -header on -format delim
lsextpool
lsfbvol
lsioport
lssi
lsarray
lsarraysite
lsrank"""
        print("%s: %s"%(self.name, cmd))
        return self.dscli(cmd)[0]

if __name__ == "__main__":
    o = IbmDss()
    for ibmds in o:
        print(ibmds.get_combo())
0707010000bbc2000081a400000000000000000000000154fc7bfb00000cc9000000660000000300000000000000000000002100000000root/opt/opensvc/lib/rcIbmSvc.py  from rcUtilities import justcall, which
import rcExceptions as ex
import os
import ConfigParser

pathlib = os.path.dirname(__file__)
pathbin = os.path.realpath(os.path.join(pathlib, '..', 'bin'))
pathetc = os.path.realpath(os.path.join(pathlib, '..', 'etc'))
pathtmp = os.path.realpath(os.path.join(pathlib, '..', 'tmp'))
if pathbin not in os.environ['PATH']:
    os.environ['PATH'] += ":"+pathbin

def rcmd(cmd, manager, username, key):
    _cmd = ['ssh', '-i', key, '@'.join((username, manager))]
    _cmd += [cmd]
    out, err, ret = justcall(_cmd)
    if ret != 0:
        print(_cmd)
        print(out)
        raise ex.excError("ssh command execution error")
    return out, err

class IbmSvcs(object):
    def __init__(self, objects=[]):
        self.objects = objects
        if len(objects) > 0:
            self.filtering = True
        else:
            self.filtering = False
        self.arrays = []
        self.index = 0
        cf = os.path.join(pathetc, "auth.conf")
        if not os.path.exists(cf):
            return
        conf = ConfigParser.RawConfigParser()
        conf.read(cf)
        m = {}
        for s in conf.sections():
            if not conf.has_option(s, "type") or \
               conf.get(s, "type") != "ibmsvc":
                continue
            if self.filtering and not s in self.objects:
                continue
            try:
                username = conf.get(s, 'username')
                key = conf.get(s, 'key')
                m[s] = [username, key]
            except:
                print("error parsing section", s)
                pass
        del(conf)
        for name, creds in m.items():
            username, key = creds
            self.arrays.append(IbmSvc(name, username, key))

    def __iter__(self):
        return self

    def next(self):
        if self.index == len(self.arrays):
            raise StopIteration
        self.index += 1
        return self.arrays[self.index-1]

class IbmSvc(object):
    def __init__(self, name, username, key):
        self.name = name
        self.username = username
        self.key = key
        #self.keys = ['lsvdisk']
        self.keys = ['lsvdisk', 'lsmdiskgrp', 'lsnode', 'lscluster', 'svc_product_id', 'lsfabric']

    def rcmd(self, cmd):
        return rcmd(cmd, self.name, self.username, self.key)

    def get_lsvdisk(self):
        cmd = 'lsvdisk -delim :'
        print("%s: %s"%(self.name, cmd))
        return self.rcmd(cmd)[0]

    def get_lsmdiskgrp(self):
        cmd = 'lsmdiskgrp -delim :'
        print("%s: %s"%(self.name, cmd))
        return self.rcmd(cmd)[0]

    def get_lsnode(self):
        cmd = 'svcinfo lsnode -delim !'
        print("%s: %s"%(self.name, cmd))
        return self.rcmd(cmd)[0]

    def get_lscluster(self):
        cmd = 'svcinfo lscluster -delim :'
        print("%s: %s"%(self.name, cmd))
        return self.rcmd(cmd)[0]

    def get_lsfabric(self):
        cmd = 'lsfabric -delim :'
        print("%s: %s"%(self.name, cmd))
        return self.rcmd(cmd)[0]

    def get_svc_product_id(self):
        cmd = 'echo $SVC_PRODUCT_ID'
        print("%s: %s"%(self.name, cmd))
        return self.rcmd(cmd)[0]

if __name__ == "__main__":
    o = IbmSvcs()
    for ibmsvc in o:
        print(ibmsvc.lsmdiskgrp())
   0707010000bbc3000081a400000000000000000000000154fc7bfb000014ca000000660000000300000000000000000000002300000000root/opt/opensvc/lib/rcIfconfig.py    #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from subprocess import *
import logging
from rcGlobalEnv import *

class interface:
    def __str__(self):
        a = ['ifconfig %s:'%self.name]
        a += [' link_encap = ' + str(self.link_encap)]
        a += [' scope = ' + str(self.scope)]
        a += [' bcast = ' + str(self.bcast)]
        a += [' mtu = ' + str(self.mtu)]
        a += [' ipaddr = ' + str(self.ipaddr)]
        a += [' mask = ' + str(self.mask)]
        a += [' ip6addr = ' + str(self.ip6addr)]
        a += [' ip6mask = ' + str(self.ip6mask)]
        a += [' hwaddr = ' + self.hwaddr]
        a += [' flag_up = ' + str(self.flag_up)]
        a += [' flag_broadcast = ' + str(self.flag_broadcast)]
        a += [' flag_running = ' + str(self.flag_running)]
        a += [' flag_multicast = ' + str(self.flag_multicast)]
        a += [' flag_loopback = ' + str(self.flag_loopback)]
        if hasattr(self, 'groupname'):
            a += [' groupname = ' + str(self.groupname)]
        return '\n'.join(a)

    def __init__(self, name):
        self.name = name
        # defaults
        self.link_encap = ''
        self.scope = ''
        self.bcast = ''
        self.mask = ''
        self.mtu = ''
        self.ipaddr = ''
        self.ip6addr = []
        self.ip6mask = []
        self.hwaddr = ''
        self.flag_up = False
        self.flag_broadcast = False
        self.flag_running = False
        self.flag_multicast = False
        self.flag_loopback = False

class ifconfig(object):
    def add_interface(self, name):
        i = interface(name)
        self.intf.append(i)

    def interface(self, name):
        for i in self.intf:
            if i.name == name:
                return i
        return None

    def has_interface(self, name):
        for i in self.intf:
            if i.name == name:
                return 1
        return 0

    def has_param(self, param, value):
        for i in self.intf:
            if not hasattr(i, param):
                continue

            if isinstance(getattr(i, param), list):
                if value in getattr(i, param):
                    return i
            else:
                if getattr(i, param) == value:
                    return i
        return None

    def get_matching_interfaces(self, param, value):
        l = []
        for i in self.intf:
            if not hasattr(i, param):
                continue
            if isinstance(getattr(i, param), list):
                if value in getattr(i, param):
                    l.append(i)
            else:
                if getattr(i, param) == value:
                    l.append(i)
        return l

    def __str__(self):
	s = ""
        for intf in self.intf:
            s += str(intf)
	s += "\nmcast: " + str(self.mcast_data) 
	return s

    def __init__(self, mcast=False):
        self.intf = []
        self.mcast_data = {}

    def next_stacked_dev(self,dev):
        """Return the first available interfaceX:Y on  interfaceX
        """
        i = 1
        while True:
            stacked_dev = dev+':'+str(i)
            if not self.has_interface(stacked_dev):
                return stacked_dev
            i = i + 1

    def get_stacked_dev(self, dev, addr, log):
        """Upon start, a new interfaceX:Y will have to be assigned.
        Upon stop, the currently assigned interfaceX:Y will have to be
        found for ifconfig down
        """
        if ':' in addr:
            stacked_intf = self.has_param("ip6addr", addr)
        else:
            stacked_intf = self.has_param("ipaddr", addr)
        if stacked_intf is not None:
            if dev not in stacked_intf.name:
		base_intf = self.has_param("name", dev)
                if base_intf and hasattr(base_intf, "groupname"):
                    alt_intfs = [ i for i in self.get_matching_interfaces("groupname", base_intf.groupname) if i.name != base_intf.name and stacked_intf.name.startswith(i.name+":")]
                    if len(alt_intfs) == 1:
                        log.info("found %s plumbed on %s, in the same ipmp group than %s" % (addr, stacked_intf.name, dev))
                        return stacked_intf.name
                log.error("%s is plumbed but not on %s" % (addr, dev))
                return
            stacked_dev = stacked_intf.name
            log.debug("found matching stacked device %s" % stacked_dev)
        else:
            stacked_dev = self.next_stacked_dev(dev)
            log.debug("allocate new stacked device %s" % stacked_dev)
        return stacked_dev


  0707010000bbc4000081a400000000000000000000000154fc7bfb00000e96000000660000000300000000000000000000002600000000root/opt/opensvc/lib/rcIfconfigAIX.py #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from subprocess import *

import rcIfconfig

class ifconfig(rcIfconfig.ifconfig):
    def hexmask_to_str(self, mask):
        mask = mask.replace('0x', '')
        s = [str(int(mask[i:i+2], 16)) for i in range(0, len(mask), 2)]
        return '.'.join(s)

    def get_mac(self, intf):
        buff = self.get_netstat_in()
        for line in buff.split("\n"):
            l = line.split()
            if len(l) < 4:
                continue
            if l[0] != intf.name:
                continue
            if not l[2].startswith("link"):
                continue
            if '.' not in l[3]:
                return ""
            return l[3].replace('.', ':')
        return ""

    def get_netstat_in(self):
        if hasattr(self, "netstat_in_cache"):
            return self.netstat_in_cache
        cmd = ['netstat', '-in']
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return ""
        self.netstat_in_cache = out
        return out

    def parse(self, out):
        prev = ''
        prevprev = ''
        for w in out.split():
            if 'flags=' in w:
                i = rcIfconfig.interface(prev.replace(':',''))
                i.hwaddr = self.get_mac(i)
                self.intf.append(i)

                # defaults
                i.link_encap = ''
                i.scope = ''
                i.bcast = ''
                i.mtu = ''
                i.ipaddr = []
                i.mask = []
                i.ip6addr = []
                i.ip6mask = []
                i.flag_up = False
                i.flag_broadcast = False
                i.flag_running = False
                i.flag_multicast = False
                i.flag_loopback = False

                flags = w.split('<')[1].split('>')[0].split(',')
                if 'UP' in flags:
                    i.flag_up = True
                if 'BROADCAST' in flags:
                    i.flag_broadcast = True
                if 'RUNNING' in flags:
                    i.flag_running = True
                if 'MULTICAST' in flags:
                    i.flag_multicast = True
                if 'LOOPBACK' in flags:
                    i.flag_loopback = True
            elif 'inet' == prev:
                i.ipaddr += [w]
            elif 'netmask' == prev:
                i.mask += [self.hexmask_to_str(w)]
            elif 'inet6' == prev:
                i.ip6addr += [w.split('/')[0]]
                i.ip6mask += [w.split('/')[1]]
            elif 'ether' == prev:
                i.hwaddr = w

            prevprev = prev
            prev = w

    def __init__(self, mcast=False):
        rcIfconfig.ifconfig.__init__(self)
        self.intf = []
        out = Popen(['ifconfig', '-a'], stdout=PIPE).communicate()[0]
        self.parse(out)

if __name__ == "__main__":
    ifaces = ifconfig(mcast=True)
    print(ifaces)

  0707010000bbc5000081a400000000000000000000000154fc7bfb00000ec2000000660000000300000000000000000000002900000000root/opt/opensvc/lib/rcIfconfigDarwin.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@opensvc.com>'
# Copyright (c) 2014 Arnaud Veron <arnaud.veron@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from subprocess import *
from rcUtilities import which

import rcIfconfig

class ifconfig(rcIfconfig.ifconfig):
    def parse(self, out):
        prev = ''
        prevprev = ''
        for w in out.split():
            if 'flags=' in w:
                i = rcIfconfig.interface(prev.replace(':',''))
                self.intf.append(i)

                # defaults
                i.link_encap = ''
                i.scope = ''
                i.bcast = ''
                i.mtu = ''
                i.ipaddr = []
                i.mask = []
                i.ip6addr = []
                i.ip6mask = []
                i.hwaddr = ''
                i.flag_up = False
                i.flag_broadcast = False
                i.flag_running = False
                i.flag_multicast = False
                i.flag_loopback = False

                flags = w.split('<')[1].split('>')[0].split(',')
                if 'UP' in flags:
                    i.flag_up = True
                if 'BROADCAST' in flags:
                    i.flag_broadcast = True
                if 'RUNNING' in flags:
                    i.flag_running = True
                if 'MULTICAST' in flags:
                    i.flag_multicast = True
                if 'LOOPBACK' in flags:
                    i.flag_loopback = True
            elif 'inet' == prev:
                i.ipaddr += [w]
            elif 'inet6' == prev:
                i.ip6addr += [w.split('%')[0]]
            elif 'netmask' == prev:
                i.mask += [w]
            elif 'prefixlen' == prev:
                i.ip6mask += [w]
            elif 'ether' == prev:
                i.hwaddr = w

            prevprev = prev
            prev = w

    def get_mcast(self):
        if which('netstat'):
            cmd = ['netstat', '-gn']
            out = Popen(cmd, stdout=PIPE).communicate()[0]
            return self.parse_mcast_netstat(out)

    def parse_mcast_netstat(self, out):
        lines = out.split('\n')
        found = False
        data = {}
        for i, line in enumerate(lines):
            if line.startswith('IPv4 Multicast'):
                found = True
                break
        if not found:
            return data
        if len(lines) == i+1:
            return data
        lines = lines[i+2:]
        for line in lines:
            if line.startswith('IPv6 Multicast') or line.startswith('Group'):
                continue
            try:
                addr, lladdr, intf = line.split()
            except:
                continue
            if intf not in data:
                data[intf] = [addr]
            else:
                data[intf] += [addr]
        return data

    def __init__(self, mcast=False):
        self.intf = []
        if mcast:
            self.mcast_data = self.get_mcast()
        out = Popen(['ifconfig', '-a'], stdout=PIPE).communicate()[0]
        self.parse(out)
  0707010000bbc6000081a400000000000000000000000154fc7bfb00000ab0000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/rcIfconfigFreeBSD.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from subprocess import *

import rcIfconfig

class ifconfig(rcIfconfig.ifconfig):
    def parse(self, out):
        prev = ''
        prevprev = ''
        for w in out.split():
            if 'flags=' in w:
                i = rcIfconfig.interface(prev.replace(':',''))
                self.intf.append(i)

                # defaults
                i.link_encap = ''
                i.scope = ''
                i.bcast = ''
                i.mtu = ''
                i.ipaddr = []
                i.mask = []
                i.ip6addr = []
                i.ip6mask = []
                i.hwaddr = ''
                i.flag_up = False
                i.flag_broadcast = False
                i.flag_running = False
                i.flag_multicast = False
                i.flag_loopback = False

                flags = w.split('<')[1].split('>')[0].split(',')
                if 'UP' in flags:
                    i.flag_up = True
                if 'BROADCAST' in flags:
                    i.flag_broadcast = True
                if 'RUNNING' in flags:
                    i.flag_running = True
                if 'MULTICAST' in flags:
                    i.flag_multicast = True
                if 'LOOPBACK' in flags:
                    i.flag_loopback = True
            elif 'inet' == prev:
                i.ipaddr += [w]
            elif 'inet6' == prev:
                i.ip6addr += [w.split('%')[0]]
            elif 'netmask' == prev:
                i.mask += [w]
            elif 'prefixlen' == prev:
                i.ip6mask += [w]
            elif 'ether' == prev:
                i.hwaddr = w

            prevprev = prev
            prev = w

    def __init__(self, mcast=False):
        rcIfconfig.ifconfig.__init__(self, mcast=mcast)
        out = Popen(['ifconfig', '-a'], stdout=PIPE).communicate()[0]
        self.parse(out)

if __name__ == "__main__":
    o = ifconfig()
    print o
0707010000bbc7000081a400000000000000000000000154fc7bfb00001683000000660000000300000000000000000000002800000000root/opt/opensvc/lib/rcIfconfigHP-UX.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from subprocess import *

import rcIfconfig
import rcExceptions as ex

class ifconfig(rcIfconfig.ifconfig):
    def parse(self, out):
        if len(out) == 0:
            return
        intf = out.split()[0]
        if intf[len(intf)-1] == ':':
            intf = intf[0:len(intf)-1]

        i = rcIfconfig.interface(intf)
        self.intf.append(i)

        # defaults
        i.link_encap = ''
        i.scope = ''
        i.bcast = ''
        i.mask = ''
        i.mtu = ''
        i.ipaddr = ''
        i.ip6addr = []
        i.ip6mask = []
        i.hwaddr = ''
        if i.name in self.hwaddr:
            i.hwaddr = self.hwaddr[i.name]
        i.flag_up = False
        i.flag_broadcast = False
        i.flag_running = False
        i.flag_multicast = False
        i.flag_loopback = False

        prev = ''
        for w in out.split():
            if 'broadcast' in prev:
                i.bcast = w
            elif 'netmask' in prev:
                if w == '0':
                    i.mask = "0.0.0.0"
                elif len(w) == 8:
                    i.mask = "%d.%d.%d.%d"%(
                        int(w[0:2], 16),
                        int(w[2:4], 16),
                        int(w[4:6], 16),
                        int(w[6:8], 16)
                    )
                else:
                    raise ex.excError("malformed ifconfig %s netmask: %s"%(intf, w))
            elif 'inet' == prev:
                i.ipaddr = w
            elif 'inet6' == prev:
                i.ip6addr += [w]
            elif 'prefix' == prev:
                i.ip6mask += [w]

            if 'UP' in w:
                i.flag_up = True
            if 'BROADCAST' in w:
                i.flag_broadcast = True
            if 'RUNNING' in w:
                i.flag_running = True
            if 'MULTICAST' in w:
                i.flag_multicast = True
            if 'LOOPBACK' in w:
                i.flag_loopback = True

            prev = w

    def get_mcast(self):
        cmd = ['netstat', '-gn']
        out = Popen(cmd, stdout=PIPE).communicate()[0]
        return self.parse_mcast(out)

    def parse_mcast(self, out):
        lines = out.split('\n')
        found = False
        data = {}
        for i, line in enumerate(lines):
            if line.startswith('Name'):
                found = True
                break
        if not found:
            return data
        if len(lines) == i+1:
            return data
        lines = lines[i+1:]
        for i, line in enumerate(lines):
            if len(line) == 0:
                break
            try:
                intf, addr, refcnt = line.split()
            except:
                continue
            if addr == "*":
                continue
            if intf not in data:
                data[intf] = [addr]
            else:
                data[intf] += [addr]
        if len(lines) <= i + 1:
            return data
        lines = lines[i+1:]
        for i, line in enumerate(lines):
            if line.startswith('Name'):
                found = True
                break
        if not found:
            return data
        if len(lines) == i+1:
            return data
        lines = lines[i+1:]
        for i, line in enumerate(lines):
            if len(line) == 0:
                break
            try:
                intf, addr, refcnt = line.split()
            except:
                continue
            if addr == "*":
                continue
            if intf not in data:
                data[intf] = [addr]
            else:
                data[intf] += [addr]
        return data

    def __init__(self, hwaddr=False, mcast=False):
        self.intf = []
        intf_list = []
        self.hwaddr = {}
        if mcast:
            self.mcast_data = self.get_mcast()
        if hwaddr:
            lines = Popen(['lanscan', '-i', '-a'], stdout=PIPE).communicate()[0].split('\n')
            for line in lines:
                l = line.split()
                if len(l) < 2:
                    continue
                mac = l[0].replace('0x','').lower()
                if len(mac) < 11:
                    continue
                mac_l = list(mac)
                for c in (10, 8, 6, 4, 2):
                    mac_l.insert(c, ':')
                self.hwaddr[l[1]] = ''.join(mac_l)
        out = Popen(['netstat', '-win'], stdout=PIPE).communicate()[0]
        for line in out.split('\n'):
            if len(line) == 0:
                continue
            if 'IPv4:' in line or 'IPv6' in line:
                continue
            intf = line.split()[0]
            intf_list.append(intf.replace('*', ''))
        for intf in intf_list:
            p = Popen(['ifconfig', intf], stdout=PIPE, stderr=PIPE)
            out = p.communicate()
            if "no such interface" in out[1]:
                continue
            elif p.returncode != 0:
                raise ex.excError
            self.parse(out[0])
 0707010000bbc8000081a400000000000000000000000154fc7bfb00002614000000660000000300000000000000000000002800000000root/opt/opensvc/lib/rcIfconfigLinux.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from subprocess import *
from rcUtilities import which

import rcIfconfig
import copy

"""
ip addr:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
...
4: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 qlen 1000
    link/ether 00:23:7d:a1:6f:96 brd ff:ff:ff:ff:ff:ff
6: sit0: <NOARP> mtu 1480 qdisc noop
    link/sit 0.0.0.0 brd 0.0.0.0
7: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue
    link/ether 00:23:7d:a1:6f:96 brd ff:ff:ff:ff:ff:ff
    inet 10.151.32.29/22 brd 10.151.35.255 scope global bond0
    inet 10.151.32.50/22 brd 10.151.35.255 scope global secondary bond0:1
    inet6 fe80::223:7dff:fea1:6f96/64 scope link
       valid_lft forever preferred_lft forever

"""

def octal_to_cidr(s):
    i = int(s)
    _in = ""
    _out = ""
    for i in range(i):
        _in += "1"
    for i in range(32-i):
        _in += "0"
    _out += str(int(_in[0:8], 2))+'.'
    _out += str(int(_in[8:16], 2))+'.'
    _out += str(int(_in[16:24], 2))+'.'
    _out += str(int(_in[24:32], 2))
    return _out

class ifconfig(rcIfconfig.ifconfig):
    def parse_ip(self, out):
        out = str(out)
        for line in out.split("\n"):
            if len(line) == 0:
                continue
            if line[0] != " ":
                """
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
                """
                _line = line.split()
                ifname = _line[1].strip(":")

                i = rcIfconfig.interface(ifname)
                # defaults
                i.link_encap = ''
                i.scope = []
                i.bcast = []
                i.mask = []
                i.mtu = ''
                i.ipaddr = []
                i.ip6addr = []
                i.ip6mask = []
                i.hwaddr = ''
                i.flag_up = False
                i.flag_broadcast = False
                i.flag_running = False
                i.flag_multicast = False
                i.flag_loopback = False

                self.intf.append(i)

                prev = ''
                for w in _line:
                    if 'mtu' == prev:
                        i.mtu = w
                    elif w.startswith('<'):
                        w = w.strip('<').strip('>')
                        flags = w.split(',')
                        for w in flags:
                            if 'UP' == w:
                                i.flag_up = True
                            if 'BROADCAST' == w:
                                i.flag_broadcast = True
                            if 'RUNNING' == w:
                                i.flag_running = True
                            if 'MULTICAST' == w:
                                i.flag_multicast = True
                            if 'LOOPBACK' == w:
                                i.flag_loopback = True
    
    
                    prev = w
            elif line.strip().startswith("link"):
                """
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
                """
                _line = line.split()
                prev = ''
                for w in _line:
                    if 'link/' in w:
                        i.link_encap = w.split('/')[1]
                    elif 'link/ether' == prev:
                        i.hwaddr = w
                    prev = w
            elif line.strip().startswith("inet"):
                """
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
                """
                _line = line.split()
                if "global" in line and ":" in _line[-1]:
                    # clone parent intf and reset inet fields
                    ifname = line.split()[-1]
                    _i = copy.copy(i)
                    _i.name = ifname
                    _i.scope = []
                    _i.bcast = []
                    _i.mask = []
                    _i.ipaddr = []
                    _i.ip6addr = []
                    _i.ip6mask = []
                    self.intf.append(_i)
                else:
                    _i = i

                prev = ''
                for w in _line:
                    if 'inet' == prev :
                        ipaddr, mask = w.split('/')
                        _i.ipaddr += [ipaddr]
                        _i.mask += [octal_to_cidr(mask)]
                    elif 'inet6' == prev:
                        (ip6addr, ip6mask) = w.split('/')
                        _i.ip6addr += [ip6addr]
                        _i.ip6mask += [ip6mask]
                    elif 'brd' == prev and 'inet' in line:
                        _i.bcast += [w]
                    elif 'scope' == prev and 'inet' in line:
                        _i.scope += [w]
    
                    prev = w


    def parse_ifconfig(self, out):
        prev = ''
        prevprev = ''
        for w in out.split():
            if w == 'Link':
                i = rcIfconfig.interface(prev)
                self.intf.append(i)

                # defaults
                i.link_encap = ''
                i.scope = ''
                i.bcast = ''
                i.mask = ''
                i.mtu = ''
                i.ipaddr = ''
                i.ip6addr = []
                i.ip6mask = []
                i.hwaddr = ''
                i.flag_up = False
                i.flag_broadcast = False
                i.flag_running = False
                i.flag_multicast = False
                i.flag_loopback = False
            elif 'encap:' in w:
                (null, i.link_encap) = w.split(':')
            elif 'Scope:' in w:
                (null, i.scope) = w.split(':')
            elif 'Bcast:' in w:
                (null, i.bcast) = w.split(':')
            elif 'Mask:' in w:
                (null, i.mask) = w.split(':')
            elif 'MTU:' in w:
                (null, i.mtu) = w.split(':')

            if 'inet' == prev and 'addr:' in w:
                (null, i.ipaddr) = w.split(':')
            if 'inet6' == prevprev and 'addr:' == prev:
                (ip6addr, ip6mask) = w.split('/')
                i.ip6addr += [ip6addr]
                i.ip6mask += [ip6mask]
            if 'HWaddr' == prev:
                i.hwaddr = w
            if 'UP' == w:
                i.flag_up = True
            if 'BROADCAST' == w:
                i.flag_broadcast = True
            if 'RUNNING' == w:
                i.flag_running = True
            if 'MULTICAST' == w:
                i.flag_multicast = True
            if 'LOOPBACK' == w:
                i.flag_loopback = True

            prevprev = prev
            prev = w

    def get_mcast(self):
        if which('netstat'):
            cmd = ['netstat', '-gn']
            out = Popen(cmd, stdout=PIPE).communicate()[0]
            return self.parse_mcast_netstat(out)
        elif which('ip'):
            cmd = ['ip', 'maddr']
            out = Popen(cmd, stdout=PIPE).communicate()[0]
            return self.parse_mcast_ip(out)

    def parse_mcast_netstat(self, out):
        lines = out.split('\n')
        found = False
        data = {}
        for i, line in enumerate(lines):
            if line.startswith('--'):
                found = True
                break
        if not found:
            return data
        if len(lines) == i+1:
            return data
        lines = lines[i+1:]
        for line in lines:
            try:
                intf, refcnt, addr = line.split()
            except:
                continue
            if intf not in data:
                data[intf] = [addr]
            else:
                data[intf] += [addr]
        return data

    def parse_mcast_ip(self, out):
        lines = out.split('\n')
        found = False
        data = {}
        for line in lines:
            if not line.startswith("	"):
                # new interface
                try:
                    name = line.split(":")[-1].strip()
                except Exception as e:
                    print(e)
                    break
                if name == "":
                    continue
                data[name] = []
                continue
            if "inet" not in line:
                continue
            data[name].append(line.split()[-1])
        return data

    def __init__(self, mcast=False):
        self.intf = []
        if mcast:
            self.mcast_data = self.get_mcast()
        if which('ip'):
            out = Popen(['ip', 'addr'], stdout=PIPE).communicate()[0]
            self.parse_ip(out)
        else:
            out = Popen(['ifconfig', '-a'], stdout=PIPE).communicate()[0]
            self.parse_ifconfig(out)

if __name__ == "__main__":
    ifaces = ifconfig(mcast=True)
    print(ifaces)

0707010000bbc9000081a400000000000000000000000154fc7bfb00000f81000000660000000300000000000000000000002700000000root/opt/opensvc/lib/rcIfconfigOSF1.py    #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from subprocess import *

import rcIfconfig
from rcUtilities import justcall

def ipv4_bitmask(s):
    if len(s) != 8:
        return
    import re
    regex = re.compile('^[0-9a-f]*$')
    if regex.match(s) is None:
        return
    r = []
    for i in range(4):
        pk = s[2*i:2*i+2]
        r.append(int(pk, 16))
    return '.'.join(map(str, r))
        
class ifconfig(rcIfconfig.ifconfig):

    def __init__(self, mcast=False):
        rcIfconfig.ifconfig.__init__(self, mcast=mcast)
        out = Popen(['ifconfig', '-a'], stdin=None, stdout=PIPE,stderr=PIPE,close_fds=True).communicate()[0]
        self.parse(out)

    def set_hwaddr(self, i):
        if i is None or i.hwaddr != '':
            return i
        if ":" in i.name:
            name = i.name.split(":")[0]
        else:
            name = i.name
        cmd = ["hwmgr", "get", "attribute", "-category", "network",
               "-a", "name="+name, "-a", "MAC_address"]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return i
        for line in out.split('\n'):
            if not line.strip().startswith("MAC"):
                continue
            l = line.split("=")
            i.hwaddr = l[1].replace('-', ':').lower()
        return i

    def parse(self, out):
        i = None
        for l in out.split("\n"):
            if l == '' : continue
            if l[0]!=' ' :
                i = self.set_hwaddr(i)
                (ifname,ifstatus)=l.split(': ')

                i=rcIfconfig.interface(ifname)
                self.intf.append(i)

                # defaults
                i.link_encap = ''
                i.scope = ''
                i.bcast = []
                i.mtu = []
                i.mask = []
                i.ipaddr = []
                i.ip6addr = []
                i.ip6mask = []
                i.hwaddr = ''
                i.flag_up = False
                i.flag_broadcast = False
                i.flag_running = False
                i.flag_multicast = False
                i.flag_ipv4 = False
                i.flag_ipv6 = False
                i.flag_loopback = False

                if 'UP' in ifstatus : i.flag_up = True
                elif 'BROADCAST' in ifstatus : i.flag_broadcast = True
                elif 'RUNNING' in ifstatus   : i.flag_running = True
                elif 'MULTICAST' in ifstatus : i.flag_multicast = True
                elif 'IPv4' in ifstatus      : i.flag_ipv4 = True
                elif 'IPv6' in ifstatus      : i.flag_ipv6 = True
            else:
                n=0
                w=l.split()
                while n < len(w) :
                    [p,v]=w[n:n+2]
                    if p == 'inet' :
                        i.ipaddr.append(v)
                        i.mask.append(ipv4_bitmask(w[n+3]))
                    elif p == 'ipmtu' : i.mtu.append(v)
                    elif p == 'inet6' :
                        (a, m) = v.split('/')
                        i.ip6addr += [a]
                        i.ip6mask += [m]
                    n+=2
        i = self.set_hwaddr(i)


if __name__ == "__main__":
    for c in (ifconfig,) :
        help(c)
   0707010000bbca000081a400000000000000000000000154fc7bfb00001585000000660000000300000000000000000000002800000000root/opt/opensvc/lib/rcIfconfigSunOS.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from subprocess import *

import rcIfconfig

class ifconfig(rcIfconfig.ifconfig):

    def __init__(self, ifconfig=None, mcast=False):
        self.intf = []
        if mcast:
            self.mcast_data = self.get_mcast()
        if ifconfig is not None:
            out = ifconfig
        else:
            out = Popen(['/usr/sbin/ifconfig', '-a'], stdin=None, stdout=PIPE,stderr=PIPE,close_fds=True).communicate()[0]
        self.parse(out)

    def get_mcast(self):
        cmd = ['netstat', '-gn']
        out = Popen(cmd, stdout=PIPE).communicate()[0]
        return self.parse_mcast(out)

    def parse_mcast(self, out):
        lines = out.split('\n')
        found = False
        data = {}
        for i, line in enumerate(lines):
            if line.startswith('--'):
                found = True
                break
        if not found:
            return data
        if len(lines) == i+1:
            return data
        lines = lines[i+1:]
        for i, line in enumerate(lines):
	    if len(line) == 0:
	        break 
            try:
                intf, addr, refcnt = line.split()
            except:
                continue
            if intf not in data:
                data[intf] = [addr]
            else:
                data[intf] += [addr]
	if len(lines) <= i + 1:
	    return data
        lines = lines[i+1:]
        for i, line in enumerate(lines):
            if line.startswith('--'):
                found = True
                break
        if not found:
            return data
        if len(lines) == i+1:
            return data
        lines = lines[i+1:]
        for i, line in enumerate(lines):
	    if len(line) == 0:
	        break 
            try:
                intf, addr, refcnt = line.split()
            except:
                continue
            if intf not in data:
                data[intf] = [addr]
            else:
                data[intf] += [addr]
        return data


    def set_hwaddr(self, i):
        if i is None or i.hwaddr != '' or ':' not in i.name:
            return i
        base_ifname, index = i.name.split(':')
        base_intf = self.interface(base_ifname)
        if base_intf is not None and len(base_intf.hwaddr) > 0:
            i.hwaddr = base_intf.hwaddr
        else:
            i.hwaddr = self.mac_from_arp(i.ipaddr)
        return i

    def mac_from_arp(self, ipaddr):
        cmd = ['/usr/sbin/arp', ipaddr]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return ''
        for word in out.split():
            if ':' not in word:
                continue
            return word
        return ''

    def parse(self, out):
        i = None
        for l in out.split("\n"):
            if l == '' : break
            if l[0]!='\t' :
                i = self.set_hwaddr(i)
                (ifname,ifstatus)=l.split(': ')

                i=rcIfconfig.interface(ifname)
                self.intf.append(i)

                # defaults
                i.link_encap = ''
                i.scope = ''
                i.bcast = ''
                i.mask = ''
                i.mtu = ''
                i.ipaddr = ''
                i.ip6addr = []
                i.ip6mask = []
                i.hwaddr = ''
                i.groupname = ''
                i.flag_up = False
                i.flag_broadcast = False
                i.flag_running = False
                i.flag_multicast = False
                i.flag_ipv4 = False
                i.flag_ipv6 = False
                i.flag_loopback = False

                if 'UP' in ifstatus : i.flag_up = True
                elif 'BROADCAST' in ifstatus : i.flag_broadcast = True
                elif 'RUNNING' in ifstatus   : i.flag_running = True
                elif 'MULTICAST' in ifstatus : i.flag_multicast = True
                elif 'IPv4' in ifstatus      : i.flag_ipv4 = True
                elif 'IPv6' in ifstatus      : i.flag_ipv6 = True
            else:
                n=0
                w=l.split()
                while n < len(w) :
                    [p,v]=w[n:n+2]
                    if p == 'inet' : i.ipaddr=v
                    elif p == 'netmask' : i.mask=v
                    elif p == 'broadcast' : i.bcast=v
                    elif p == 'ether' : i.hwaddr=v
                    elif p == 'groupname' : i.groupname=v
                    elif p == 'inet6' :
                        (a, m) = v.split('/')
                        i.ip6addr += [a]
                        i.ip6mask += [m]
                    n+=2
        i = self.set_hwaddr(i)


if __name__ == "__main__":
    ifaces = ifconfig(mcast=True)
    print(ifaces)
   0707010000bbcb000081a400000000000000000000000154fc7bfb000007dd000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/rcIfconfigWindows.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from subprocess import *

import rcIfconfig
import wmi

class ifconfig(rcIfconfig.ifconfig):
    def parse(self, intf, intf_cf):
	if intf_cf.IPAddress is None:
	    return
        i = rcIfconfig.interface(intf.NetConnectionID)
        self.intf.append(i)

        # defaults
        i.link_encap = ''
        i.scope = ''
        i.bcast = ''
        i.mask = []
        i.mtu = intf_cf.MTU
        i.ipaddr = []
        i.ip6addr = []
        i.ip6mask = []
        i.hwaddr = intf_cf.MACAddress
        try:
            i.flag_up = intf.NetEnabled
        except:
            i.flag_up = False
        i.flag_broadcast = False
        i.flag_running = False
        i.flag_multicast = False
        i.flag_loopback = False

        for idx, ip in enumerate(intf_cf.IPAddress):
	    if ":" in ip:
	        i.ip6addr.append(ip)
	        i.ip6mask.append(intf_cf.IPsubnet[idx])
	    else:
	        i.ipaddr.append(ip)
	        i.mask.append(intf_cf.IPsubnet[idx])

    def __init__(self, mcast=False):
        self.wmi = wmi.WMI()
        self.intf = []
        for n, nc in zip(self.wmi.Win32_NetworkAdapter(), self.wmi.Win32_NetworkAdapterConfiguration()):
            self.parse(n, nc)

if __name__ == "__main__" :
    o = ifconfig()
    print(o)
   0707010000bbcc000081ed00000000000000000000000154fc7bfb00000609000000660000000300000000000000000000002300000000root/opt/opensvc/lib/rcListener.py    import os
import time
from subprocess import *
from rcGlobalEnv import rcEnv

pathsvc = os.path.join(os.path.dirname(__file__), '..')
nodeconf = os.path.join(pathsvc, "etc", "node.conf")

if rcEnv.sysname == "Windows":
    nodemgr = os.path.join(pathsvc, "nodemgr.cmd")
    svcmon = os.path.join(pathsvc, "svcmon.cmd")
    cron = os.path.join(pathsvc, "cron.cmd")
else:
    pathbin = os.path.join(pathsvc, "bin")
    nodemgr = os.path.join(pathbin, "nodemgr")
    svcmon = os.path.join(pathbin, "svcmon")
    cron = os.path.join(pathbin, "cron",  "cron")

import thread
import sys
from socket import *

try:
    import ConfigParser
except ImportError:
    import configparser as ConfigParser

try:
    config = ConfigParser.RawConfigParser()
    config.read(nodeconf)
    port = config.getint("listener", "port")
except:
    port = rcEnv.listener_port

def HandleClient(conn):
    data = conn.recv(1024)
    cmd = [nodemgr, 'dequeue_actions']
    p = Popen(cmd, stdout=None, stderr=None, stdin=None)
    p.communicate()
    conn.close()

class listener(object):
    def __init__(self):
        thread.start_new(self.do, tuple())
        while True:
            if getattr(sys, 'stop_listener', False):
                sys.exit(0)
            time.sleep(0.3)

    def do(self):
        sock = socket(AF_INET, SOCK_STREAM)
        sock.bind((gethostname(), port))
        sock.listen(5)

        while True:
            conn, addr = sock.accept()
            thread.start_new(HandleClient, (conn,))

if __name__ == '__main__':
    a = listener()

   0707010000bbcd000081a400000000000000000000000154fc7bfb00000b3e000000660000000300000000000000000000002100000000root/opt/opensvc/lib/rcLogger.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import sys
import logging
import logging.handlers
from rcGlobalEnv import *

min_resource_name_len = 8

def set_streamformatter(svc):
    maxlen = get_max_ressource_name_len(svc)
    streamformatter = logging.Formatter("%(asctime)s %(levelname)-7s %(name)-"+str(maxlen)+"s %(message)s", datefmt="%H:%M:%S")
    handler = svc.log.handlers[1]
    handler.setFormatter(streamformatter)

def get_max_ressource_name_len(svc):
    maxlen = min_resource_name_len
    for r in svc.resources_by_id.values():
        if r is None:
            continue
        l = len(r.log_label())
        if l > maxlen:
            maxlen = l
    return maxlen

def initLogger(name):
    log = logging.getLogger(name)
    if name in rcEnv.logging_initialized:
        return log
    name_width = len(name) + min_resource_name_len

    """Common log formatter
    """
    fileformatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    streamformatter = logging.Formatter("%(asctime)s %(levelname)-7s %(name)-"+str(name_width)+"s %(message)s", datefmt="%H:%M:%S")

    """Common logfile with rotation
    """
    filehandler = logging.handlers.RotatingFileHandler(rcEnv.logfile,
                                                       maxBytes=5242880,
                                                       backupCount=5)
    filehandler.setFormatter(fileformatter)
    log.addHandler(filehandler)

    """Stdout logger
    """
    streamhandler = logging.StreamHandler()
    streamhandler.setFormatter(streamformatter)
    log.addHandler(streamhandler)

    if '--debug' in sys.argv:
            rcEnv.loglevel = logging.DEBUG
            log.setLevel(logging.DEBUG)
    elif '--warn' in sys.argv:
            rcEnv.loglevel = logging.WARNING
            log.setLevel(logging.WARNING)
    elif '--error' in sys.argv:
            rcEnv.loglevel = logging.ERROR
            log.setLevel(logging.ERROR)
    else:
            rcEnv.loglevel = logging.INFO
            log.setLevel(logging.INFO)

    rcEnv.logging_initialized.append(name)
    return log
  0707010000bbce000081a400000000000000000000000154fc7bfb00000736000000660000000300000000000000000000002500000000root/opt/opensvc/lib/rcLoopDarwin.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@opensvc.com>'
# Copyright (c) 2014 Arnaud Veron <arnaud.veron@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import re
import plistlib

from rcGlobalEnv import *
from rcUtilities import call, which
import rcStatus
import resLoop as Res
import rcExceptions as ex

def file_to_loop(f):
    """Given a file path, returns the disk device associated. For example,
    /path/to/file => /dev/disk0s1
    """
    if which('hdiutil') is None:
        return []
    if not os.path.isfile(f):
        return []
    (ret, out, err) = call(['hdiutil', 'info', '-plist'])
    if ret != 0:
        return []

    devs= []
    pl = plistlib.readPlistFromString(out)
    for image in pl['images']:
        if image.get('image-path') == f:
            for se in image['system-entities']:
                if se.get('mount-point') is not None:
                    diskdevice = se.get('dev-entry')
                    if diskdevice is not None:
                        devs.append(diskdevice)
                    else:
                        return []
    
    return devs
  0707010000bbcf000081a400000000000000000000000154fc7bfb0000069e000000660000000300000000000000000000002400000000root/opt/opensvc/lib/rcLoopLinux.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import re

from rcGlobalEnv import *
from rcUtilities import call, which
import rcStatus
import resLoop as Res
import rcExceptions as ex

def file_to_loop(f):
    """Given a file path, returns the loop device associated. For example,
    /path/to/file => /dev/loop0
    """
    if which('losetup') is None:
        return []
    if not os.path.isfile(f):
        return []
    if rcEnv.sysname != 'Linux':
        return []
    (ret, out, err) = call(['losetup', '-j', f])
    if len(out) == 0:
        return []
    """ It's possible multiple loopdev are associated with the same file
    """
    devs= []
    for line in out.split('\n'):
        l = line.split(':')
        if len(l) == 0:
            continue
        if len(l[0]) == 0:
            continue
        if not os.path.exists(l[0]):
            continue
        devs.append(l[0])
    return devs

  0707010000bbd0000081a400000000000000000000000154fc7bfb00001f47000000660000000300000000000000000000002100000000root/opt/opensvc/lib/rcLvmAIX.py  from subprocess import *

"""
lsvg format
===========

VOLUME GROUP:       rootvg                   VG IDENTIFIER:  00082a6a0000d400000001321aa20bf2
VG STATE:           active                   PP SIZE:        64 megabyte(s)
VG PERMISSION:      read/write               TOTAL PPs:      959 (61376 megabytes)
MAX LVs:            256                      FREE PPs:       717 (45888 megabytes)
LVs:                11                       USED PPs:       242 (15488 megabytes)
OPEN LVs:           10                       QUORUM:         2 (Enabled)
TOTAL PVs:          1                        VG DESCRIPTORS: 2
STALE PVs:          0                        STALE PPs:      0
ACTIVE PVs:         1                        AUTO ON:        yes
MAX PPs per VG:     32512                                     
MAX PPs per PV:     1016                     MAX PVs:        32
LTG size (Dynamic): 256 kilobyte(s)          AUTO SYNC:      no
HOT SPARE:          no                       BB POLICY:      relocatable

lsvg -l vgname format
=====================

rootvg:
LV NAME             TYPE       LPs     PPs     PVs  LV STATE      MOUNT POINT
hd5                 boot       1       1       1    closed/syncd  N/A
hd6                 paging     32      32      1    open/syncd    N/A
hd8                 jfs2log    1       1       1    open/syncd    N/A
hd4                 jfs2       16      16      1    open/syncd    /
hd2                 jfs2       40      40      1    open/syncd    /usr
hd9var              jfs2       16      16      1    open/syncd    /var
hd3                 jfs2       16      16      1    open/syncd    /tmp
hd1                 jfs2       16      16      1    open/syncd    /home
hd10opt             jfs2       16      16      1    open/syncd    /opt
lv_logs             jfs2       32      32      1    open/syncd    /logs
lv_moteurs          jfs2       56      56      1    open/syncd    /moteurs

lspv format
===========

hdisk0          00082a6a1aa20b3c                    rootvg          active
hdisk1          00082a6ae73c7bb6                    datavg          active

lspv -l pvname format
=====================

hdisk0:
LV NAME               LPs     PPs     DISTRIBUTION          MOUNT POINT
hd10opt               16      16      00..00..16..00..00    /opt
hd2                   40      40      00..00..40..00..00    /usr
hd9var                16      16      00..00..16..00..00    /var
hd3                   16      16      00..00..16..00..00    /tmp
hd1                   16      16      00..00..16..00..00    /home
hd5                   1       1       01..00..00..00..00    N/A
hd6                   32      32      00..00..32..00..00    N/A
hd8                   1       1       00..00..01..00..00    N/A
hd4                   16      16      00..00..16..00..00    /
lv_logs               32      32      00..32..00..00..00    /logs
lv_moteurs            56      56      00..56..00..00..00    /moteurs

"""

class InitVgError(Exception):
    pass

class Container(dict):
    def __call__(self,key):
        return self.__getitem__(key)

    def __getitem__(self, key):
        dict.__getitem__(self, key)

    def __setitem__(self, key, value):
        dict.__setitem__(self, key, value)

    def __getattr__(self, key):
        return self[key]

    def __setattr__(self, key, value):
        self[key] = value

    def __iadd__(self, o):
        dict.__setitem__(self, o.name, o)
        return self

class Vg(object):
    props = [{"prop": "ppsize", "key": "PP SIZE:", "consume": 2}]

    def __init__(self, name):
        self.name = name
        self.lv = Container()
        self.pv = Container()
        self.load_vg(name)
        self.load_lv(name)

    def __str__(self):
        l = []
        l.append("type: vg")
        l.append("name: %s"%self.name)
        l.append("pp size: %d MB"%self.ppsize)
        s = '\n'.join(l)

        for lv in self.lv.values():
            s += str(lv) + '\n'

        return s

    def parse_ppsize(self, l):
        self.ppsize = int(l[0])
        if 'megabyte' in l[1]:
            pass
        elif 'kilobyte' in l[1]:
            self.ppsize /= 1024
        elif 'gigabyte' in l[1]:
            self.ppsize *= 1024

    def load_vg(self, name):
        cmd = ['lsvg', name]
        p = Popen(cmd, stdout=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            raise InitVgError()
        for line in out.split('\n'):
            for p in self.props:
                if p['key'] not in line:
                    continue
                _line = line[line.index(p['key'])+len(p['key']):]
                l = _line.split()
                getattr(self, 'parse_'+p['prop'])(l[0:p['consume']])

    def load_lv(self, name):
        cmd = ['lsvg', '-l', name]
        p = Popen(cmd, stdout=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            raise InitVgError()
        for line in out.split('\n'):
            if 'LV NAME' in line:
                continue
            l = line.split()
            if len(l) < 6:
                continue
            _name, _type, _lps, _pps, _pvs, _state = l[0:6]
            _mntpt = ' '.join(l[6:])
            lv = Lv(_name)
            lv.type = _type
            lv.lps = int(_lps)
            lv.pps = int(_pps)
            lv.pvs = int(_pvs)
            lv.state = _state
            lv.mntpt = _mntpt
            lv.size = lv.pps * self.ppsize
            self.lv += lv

class Pv(object):
    def __init__(self, name):
        self.name = name
        self.lv_pps = {}
        self.load_lv_pps(name)

    def load_lv_pps(self, name):
        cmd = ['lspv', '-l', name]
        p = Popen(cmd, stdout=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            raise InitVgError()
        for line in out.split('\n'):
            if 'LV NAME' in line:
                continue
            l = line.split()
            if len(l) < 4:
                continue
            _name, _lps, _pps, _distrib = l[0:4]
            _mntpt = ' '.join(l[4:])
            if name in self.lv_pps:
                self.lv_pps[_name] += int(_pps)
            else:
                self.lv_pps[_name] = int(_pps)

class Lv(object):
    def __init__(self, name):
        self.name = name
        self.pv_size = {}

    def __str__(self):
        l = []
        l.append("type: lv")
        l.append("name: %s"%self.name)
        l.append("lv size: %d MB"%self.size)
        l.append("pv usage: %s"%self.pv_size.items())
        return '\n'.join(l)

class Lvm(object):
    def __init__(self):
        self.vg = Container()
        self.pv = Container()
        self.load_vg()
        self.load_pv()

    def __str__(self):
        s = ""
        for vgname, vg in self.vg.items():
            s += str(vg) + '\n\n'
        return s

    def load_vg(self):
        cmd = ['lsvg']
        p = Popen(cmd, stdout=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            raise InitVgError()
        for vg in out.split():
            self.vg += Vg(vg)

    def load_pv(self):
        cmd = ['lspv']
        p = Popen(cmd, stdout=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            raise InitVgError()
        for line in out.split('\n'):
            l = line.split()
            if len(l) != 4:
                continue
            _name, _id, _vgname, _state = l
            pv = Pv(_name)
            pv.id = _id
            pv.vgname = _vgname
            pv.state = _state
            self.pv += pv
            for lvname, lvpps in pv.lv_pps.items():
                vg, lv = self.find_lv(lvname)
                if lv is None:
                    continue
                lv.pv_size[pv.name] = vg.ppsize * lvpps

    def find_lv(self, lvname):
        for vg in self.vg.values():
            for lv in vg.lv.values():
                if lv.name == lvname:
                    return vg, lv
        return None, None

if __name__ == "__main__" :
    lvm = Lvm()
    print(lvm)
 0707010000bbd1000081a400000000000000000000000154fc7bfb000024b7000000660000000300000000000000000000001e00000000root/opt/opensvc/lib/rcMd5.py """
/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
 */

/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
rights reserved.

License to copy and use this software is granted provided that it
is identified as the "RSA Data Security, Inc. MD5 Message-Digest
Algorithm" in all material mentioning or referencing this software
or this function.

License is also granted to make and use derivative works provided
that such works are identified as "derived from the RSA Data
Security, Inc. MD5 Message-Digest Algorithm" in all material
mentioning or referencing the derived work.

RSA Data Security, Inc. makes no representations concerning either
the merchantability of this software or the suitability of this
software for any particular purpose. It is provided "as is"
without express or implied warranty of any kind.

These notices must be retained in any copies of any part of this
documentation and/or software.
 */
"""

#/* Constants for MD5Transform routine.

S11 = 7
S12 = 12
S13 = 17
S14 = 22
S21 = 5
S22 = 9
S23 = 14
S24 = 20
S31 = 4
S32 = 11
S33 = 16
S34 = 23
S41 = 6
S42 = 10
S43 = 15
S44 = 21

PADDING = "\x80" + 63*"\0"   # do not overlook first byte again :-)

#/* F, G, H and I are basic MD5 functions.
def F(x, y, z): return (((x) & (y)) | ((~x) & (z)))

def G(x, y, z): return (((x) & (z)) | ((y) & (~z)))

def H(x, y, z): return ((x) ^ (y) ^ (z))

def I(x, y, z): return((y) ^ ((x) | (~z)))

#/* ROTATE_LEFT rotates x left n bits.

def ROTATE_LEFT(x, n):
    x = x & 0xffffffffL   # make shift unsigned
    return (((x) << (n)) | ((x) >> (32-(n)))) & 0xffffffffL

#/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
#Rotation is separate from addition to prevent recomputation.

def FF(a, b, c, d, x, s, ac):
    a = a + F ((b), (c), (d)) + (x) + (ac)
    a = ROTATE_LEFT ((a), (s))
    a = a + b
    return a # must assign this to a

def GG(a, b, c, d, x, s, ac):
    a = a + G ((b), (c), (d)) + (x) + (ac)
    a = ROTATE_LEFT ((a), (s))
    a = a + b
    return a # must assign this to a

def HH(a, b, c, d, x, s, ac):
    a = a + H ((b), (c), (d)) + (x) + (ac)
    a = ROTATE_LEFT ((a), (s))
    a = a + b
    return a # must assign this to a

def II(a, b, c, d, x, s, ac):
    a = a + I ((b), (c), (d)) + (x) + (ac)
    a = ROTATE_LEFT ((a), (s))
    a = a + b
    return a # must assign this to a


class md5:
    def __init__(self, initial=None):
        self.count = 0L
        self.state = (0x67452301L,
                      0xefcdab89L,
                      0x98badcfeL,
                      0x10325476L,)
        self.buffer = ""
        if initial:
            self.update(initial)

##/* MD5 block update operation. Continues an MD5 message-digest
##  operation, processing another message block, and updating the
##  context.
## */

##  /* Compute number of bytes mod 64 */
    def update(self, input):
        inputLen = len(input)
##  index = (unsigned int)((context->count[0] >> 3) & 0x3F);
        index = int(self.count >> 3) & 0x3F

##  /* Update number of bits */
        self.count = self.count + (inputLen << 3)

##  partLen = 64 - index;
        partLen = 64 - index

##  /* Transform as many times as possible.
        if inputLen >= partLen:
            self.buffer = self.buffer[:index] + input[:partLen]
            self.transform(self.buffer)
            i = partLen
            while i + 63 < inputLen:
                self.transform(input[i:i+64])
                i = i + 64
            index = 0
        else:
            i = 0

##  /* Buffer remaining input */
        self.buffer = self.buffer[:index] + input[i:inputLen]
        

##/* MD5 finalization. Ends an MD5 message-digest operation, writing the
##  the message digest and zeroizing the context.
## */
        
    def final(self):

##  /* Save number of bits */
        bits = Encode((self.count & 0xffffffffL, self.count>>32), 8)
            
##  /* Pad out to 56 mod 64.

        index = int((self.count >> 3) & 0x3f)
        if index < 56:
            padLen = (56 - index)
        else:
            padLen = (120 - index)
        self.update(PADDING[:padLen])

##  /* Append length (before padding) */
        self.update(bits)

##  /* Store state in digest */
        digest = Encode(self.state, 16)

##  /* Zeroize sensitive information.

        self.__dict__.clear()

        return digest
    
    digest = final  # alias

##/* MD5 basic transformation. Transforms state based on block.
## */

    def transform(self, block):
        a, b, c, d = state = self.state

        x = Decode(block, 64)

##  /* Round 1 */
        a = FF (a, b, c, d, x[ 0], S11, 0xd76aa478)#; /* 1 */
        d = FF (d, a, b, c, x[ 1], S12, 0xe8c7b756)#; /* 2 */
        c = FF (c, d, a, b, x[ 2], S13, 0x242070db)#; /* 3 */
        b = FF (b, c, d, a, x[ 3], S14, 0xc1bdceee)#; /* 4 */
        a = FF (a, b, c, d, x[ 4], S11, 0xf57c0faf)#; /* 5 */
        d = FF (d, a, b, c, x[ 5], S12, 0x4787c62a)#; /* 6 */
        c = FF (c, d, a, b, x[ 6], S13, 0xa8304613)#; /* 7 */
        b = FF (b, c, d, a, x[ 7], S14, 0xfd469501)#; /* 8 */
        a = FF (a, b, c, d, x[ 8], S11, 0x698098d8)#; /* 9 */
        d = FF (d, a, b, c, x[ 9], S12, 0x8b44f7af)#; /* 10 */
        c = FF (c, d, a, b, x[10], S13, 0xffff5bb1)#; /* 11 */
        b = FF (b, c, d, a, x[11], S14, 0x895cd7be)#; /* 12 */
        a = FF (a, b, c, d, x[12], S11, 0x6b901122)#; /* 13 */
        d = FF (d, a, b, c, x[13], S12, 0xfd987193)#; /* 14 */
        c = FF (c, d, a, b, x[14], S13, 0xa679438e)#; /* 15 */
        b = FF (b, c, d, a, x[15], S14, 0x49b40821)#; /* 16 */

## /* Round 2 */
        a = GG (a, b, c, d, x[ 1], S21, 0xf61e2562)#; /* 17 */
        d = GG (d, a, b, c, x[ 6], S22, 0xc040b340)#; /* 18 */
        c = GG (c, d, a, b, x[11], S23, 0x265e5a51)#; /* 19 */
        b = GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa)#; /* 20 */
        a = GG (a, b, c, d, x[ 5], S21, 0xd62f105d)#; /* 21 */
        d = GG (d, a, b, c, x[10], S22,  0x2441453)#; /* 22 */
        c = GG (c, d, a, b, x[15], S23, 0xd8a1e681)#; /* 23 */
        b = GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8)#; /* 24 */
        a = GG (a, b, c, d, x[ 9], S21, 0x21e1cde6)#; /* 25 */
        d = GG (d, a, b, c, x[14], S22, 0xc33707d6)#; /* 26 */
        c = GG (c, d, a, b, x[ 3], S23, 0xf4d50d87)#; /* 27 */
        b = GG (b, c, d, a, x[ 8], S24, 0x455a14ed)#; /* 28 */
        a = GG (a, b, c, d, x[13], S21, 0xa9e3e905)#; /* 29 */
        d = GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8)#; /* 30 */
        c = GG (c, d, a, b, x[ 7], S23, 0x676f02d9)#; /* 31 */
        b = GG (b, c, d, a, x[12], S24, 0x8d2a4c8a)#; /* 32 */

##  /* Round 3 */
        a = HH (a, b, c, d, x[ 5], S31, 0xfffa3942)#; /* 33 */
        d = HH (d, a, b, c, x[ 8], S32, 0x8771f681)#; /* 34 */
        c = HH (c, d, a, b, x[11], S33, 0x6d9d6122)#; /* 35 */
        b = HH (b, c, d, a, x[14], S34, 0xfde5380c)#; /* 36 */
        a = HH (a, b, c, d, x[ 1], S31, 0xa4beea44)#; /* 37 */
        d = HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9)#; /* 38 */
        c = HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60)#; /* 39 */
        b = HH (b, c, d, a, x[10], S34, 0xbebfbc70)#; /* 40 */
        a = HH (a, b, c, d, x[13], S31, 0x289b7ec6)#; /* 41 */
        d = HH (d, a, b, c, x[ 0], S32, 0xeaa127fa)#; /* 42 */
        c = HH (c, d, a, b, x[ 3], S33, 0xd4ef3085)#; /* 43 */
        b = HH (b, c, d, a, x[ 6], S34,  0x4881d05)#; /* 44 */
        a = HH (a, b, c, d, x[ 9], S31, 0xd9d4d039)#; /* 45 */
        d = HH (d, a, b, c, x[12], S32, 0xe6db99e5)#; /* 46 */
        c = HH (c, d, a, b, x[15], S33, 0x1fa27cf8)#; /* 47 */
        b = HH (b, c, d, a, x[ 2], S34, 0xc4ac5665)#; /* 48 */

##  /* Round 4 */
        a = II (a, b, c, d, x[ 0], S41, 0xf4292244)#; /* 49 */
        d = II (d, a, b, c, x[ 7], S42, 0x432aff97)#; /* 50 */
        c = II (c, d, a, b, x[14], S43, 0xab9423a7)#; /* 51 */
        b = II (b, c, d, a, x[ 5], S44, 0xfc93a039)#; /* 52 */
        a = II (a, b, c, d, x[12], S41, 0x655b59c3)#; /* 53 */
        d = II (d, a, b, c, x[ 3], S42, 0x8f0ccc92)#; /* 54 */
        c = II (c, d, a, b, x[10], S43, 0xffeff47d)#; /* 55 */
        b = II (b, c, d, a, x[ 1], S44, 0x85845dd1)#; /* 56 */
        a = II (a, b, c, d, x[ 8], S41, 0x6fa87e4f)#; /* 57 */
        d = II (d, a, b, c, x[15], S42, 0xfe2ce6e0)#; /* 58 */
        c = II (c, d, a, b, x[ 6], S43, 0xa3014314)#; /* 59 */
        b = II (b, c, d, a, x[13], S44, 0x4e0811a1)#; /* 60 */
        a = II (a, b, c, d, x[ 4], S41, 0xf7537e82)#; /* 61 */
        d = II (d, a, b, c, x[11], S42, 0xbd3af235)#; /* 62 */
        c = II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb)#; /* 63 */
        b = II (b, c, d, a, x[ 9], S44, 0xeb86d391)#; /* 64 */

        self.state = (0xffffffffL & (state[0] + a),
                      0xffffffffL & (state[1] + b),
                      0xffffffffL & (state[2] + c),
                      0xffffffffL & (state[3] + d),)

##  /* Zeroize sensitive information.

        del x

# end of the class. Now the helpers

import struct, string

def Encode(input, len):
    k = len >> 2
    res = apply(struct.pack, ("%iI" % k,) + tuple(input[:k]))
    return string.join(res, "")

def Decode(input, len):
    k = len >> 2
    res = struct.unpack("%iI" % k, input[:len])
    return list(res)

def test():
    print(`md5("/dev/sda").digest().encode('hex')`)
    #from md5 import new
    #print(`new("/dev/sda").digest()`)

if __name__=="__main__":
    test()
 0707010000bbd2000081a400000000000000000000000154fc7bfb0000095a000000660000000300000000000000000000002100000000root/opt/opensvc/lib/rcMounts.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@opensvc.com>'
# Copyright (c) 2014 Arnaud Veron <arnaud.veron@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

class Mount:
    def __init__(self, dev, mnt, type, mnt_opt):
        self.dev = dev.rstrip('/')
        self.mnt = mnt.rstrip('/')
        if mnt is '/':
            self.mnt = mnt
        self.type = type
        self.mnt_opt = mnt_opt

    def __str__(self):
        return "Mount: dev[%s] mnt[%s] type[%s] options[%s]" % \
            (self.dev,self.mnt,self.type,self.mnt_opt)

class Mounts:
    def __init__(self):
        """ OS dependent """
        self.mounts = []

    def __iter__(self):
        return iter(self.mounts)

    def match_mount(self):
        """ OS dependent """
        pass

    def mount(self, dev, mnt):
        for i in self.mounts:
            if self.match_mount(i, dev, mnt):
                return i
        return None

    def has_mount(self, dev, mnt):
        for i in self.mounts:
            if self.match_mount(i, dev, mnt):
                return True
        return False

    def has_param(self, param, value):
        for i in self.mounts:
            if getattr(i, param) == value:
                return i
        return None

    def sort(self, key='mnt', reverse=False):
        if len(self.mounts) == 0:
            return
        if key not in ('mnt', 'dev', 'type'):
            return
        self.mounts.sort(lambda x, y: cmp(getattr(x, key), getattr(y, key)), reverse=reverse)

    def __str__(self):
        output="%s" % (self.__class__.__name__)
        for m in self.mounts:
            output+="\n  %s" % m.__str__()
        return output
  0707010000bbd3000081a400000000000000000000000154fc7bfb000011ea000000660000000300000000000000000000002400000000root/opt/opensvc/lib/rcMountsAIX.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import rcMounts
#import resLoopAIX as Res
from rcUtilities import *
from subprocess import Popen

def get_src_dir_dev(dev):
    """Given a directory path, return its hosting device
    """
    process = Popen(['df', dev], stdout=PIPE, stderr=STDOUT, close_fds=True)
    buff = process.communicate()
    out = buff[0]
    i = out.index('/')
    return out[i:].split()[0]

class Mounts(rcMounts.Mounts):
    def match_mount(self, i, dev, mnt):
        """Given a line of 'mount' output, returns True if (dev, mnt) matches
        this line. Returns False otherwize. Also care about weirdos like loops
        and binds, ...
        """
        if os.path.isdir(dev):
            is_bind = True
            src_dir_dev = get_src_dir_dev(dev)
        else:
            is_bind = False

        if i.mnt != mnt:
            return False
        if i.dev == dev:
            return True
#        if i.dev in Res.file_to_loop(dev):
#            return True
        if is_bind and i.dev == src_dir_dev:
            return True
        return False

    def __init__(self):
        self.mounts = []
        (ret, out, err) = call(['mount'])
        lines = out.split('\n')
        if len(lines) < 3:
            return
        for l in lines[2:]:
            if len(l) == 0:
                continue
            x = l.split()
            if x[0] == '-hosts':
                continue
            elif x[0][0] == '/':
                dev, mnt, type, null, null, null, mnt_opt = l.split()
            else:
                v = l.split()
                if len(v) == 7:
                    node, dev, mnt, type, null, null, null = l.split()
                    mntopt = ""
                if len(v) == 8:
                    node, dev, mnt, type, null, null, null, mnt_opt = l.split()
                else:
                    continue
            m = rcMounts.Mount(dev, mnt, type, mnt_opt)
            self.mounts.append(m)

"""
  node       mounted        mounted over    vfs       date        options
-------- ---------------  ---------------  ------ ------------ ---------------
         /dev/hd4         /                jfs2   Jun 14 19:42 rw,log=/dev/hd8
         /dev/hd2         /usr             jfs2   Jun 14 19:42 rw,log=/dev/hd8
         /dev/hd9var      /var             jfs2   Jun 14 19:42 rw,log=/dev/hd8
         /dev/hd3         /tmp             jfs2   Jun 14 19:42 rw,log=/dev/hd8
         /dev/hd1         /home            jfs2   Jun 14 19:48 rw,log=/dev/hd8
         /proc            /proc            procfs Jun 14 19:48 rw
         /dev/hd10opt     /opt             jfs2   Jun 14 19:48 rw,log=/dev/hd8

  node       mounted        mounted over    vfs       date        options      
-------- ---------------  ---------------  ------ ------------ --------------- 
         /dev/hd4         /                jfs2   Nov 29 11:22 rw,log=/dev/hd8 
         /dev/hd2         /usr             jfs2   Nov 29 11:22 rw,log=/dev/hd8 
         /dev/hd9var      /var             jfs2   Nov 29 11:22 rw,log=/dev/hd8 
         /dev/hd3         /tmp             jfs2   Nov 29 11:23 rw,log=/dev/hd8 
         /dev/hd1         /home            jfs2   Nov 29 11:23 rw,log=/dev/hd8 
         /proc            /proc            procfs Nov 29 11:23 rw              
         /dev/hd10opt     /opt             jfs2   Nov 29 11:23 rw,log=/dev/hd8 
         /dev/lv_logs     /logs            jfs2   Nov 29 11:23 rw,log=/dev/hd8 
         /dev/lv_moteurs  /moteurs         jfs2   Nov 29 11:23 rw,log=/dev/hd8 
         -hosts           /net             autofs Nov 29 11:23 nosuid,vers=3,rw,nobrowse,ignore
x64lmwbief4 /install/outils  /mnt             nfs3   Dec 07 11:53                 

"""

if __name__ == "__main__" :
    help(Mounts)
    for m in Mounts():
        print(m)
  0707010000bbd4000081a400000000000000000000000154fc7bfb00000985000000660000000300000000000000000000002700000000root/opt/opensvc/lib/rcMountsDarwin.py    #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import rcMounts
from rcUtilities import *
from subprocess import Popen

def get_src_dir_dev(dev):
    """Given a directory path, return its hosting device
    """
    process = Popen(['df', '-l', dev], stdout=PIPE, stderr=STDOUT, close_fds=True)
    buff = process.communicate()
    out = buff[0]
    i = out.index('/')
    return out[i:].split()[0]

class Mounts(rcMounts.Mounts):

    def match_mount(self, i, dev, mnt):
        """Given a line of 'mount' output, returns True if (dev, mnt) matches
        this line. Returns False otherwize. Also care about weirdos like loops
        and binds, ...
        """
        if os.path.isdir(dev):
            is_bind = True
            src_dir_dev = get_src_dir_dev(dev)
        else:
            is_bind = False

        if i.mnt != mnt:
            return False
        if i.dev == dev:
            return True
        if is_bind and i.dev == src_dir_dev:
            return True
        return False

    def __init__(self):
        self.mounts = []
        (ret, out, err) = call(['mount'])
        for l in out.split('\n'):
            words = l.split()
            if len(words) < 4:
                return
            dev = words[0]
            mnt = words[2]
            opts = ' '.join(words[3:]).strip('(').strip(')').split(', ')
            type = opts[0]
            if len(opts) < 3:
                mnt_opt = ''
            else:
                mnt_opt = ','.join(opts[2:])
            m = rcMounts.Mount(dev, mnt, type, mnt_opt)
            self.mounts.append(m)

if __name__ == "__main__" :
    help(Mounts)
    for m in Mounts():
        print(m)
   0707010000bbd5000081a400000000000000000000000154fc7bfb00000985000000660000000300000000000000000000002800000000root/opt/opensvc/lib/rcMountsFreeBSD.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import rcMounts
from rcUtilities import *
from subprocess import Popen

def get_src_dir_dev(dev):
    """Given a directory path, return its hosting device
    """
    process = Popen(['df', '-l', dev], stdout=PIPE, stderr=STDOUT, close_fds=True)
    buff = process.communicate()
    out = buff[0]
    i = out.index('/')
    return out[i:].split()[0]

class Mounts(rcMounts.Mounts):

    def match_mount(self, i, dev, mnt):
        """Given a line of 'mount' output, returns True if (dev, mnt) matches
        this line. Returns False otherwize. Also care about weirdos like loops
        and binds, ...
        """
        if os.path.isdir(dev):
            is_bind = True
            src_dir_dev = get_src_dir_dev(dev)
        else:
            is_bind = False

        if i.mnt != mnt:
            return False
        if i.dev == dev:
            return True
        if is_bind and i.dev == src_dir_dev:
            return True
        return False

    def __init__(self):
        self.mounts = []
        (ret, out, err) = call(['mount'])
        for l in out.split('\n'):
            words = l.split()
            if len(words) < 4:
                return
            dev = words[0]
            mnt = words[2]
            opts = ' '.join(words[3:]).strip('(').strip(')').split(', ')
            type = opts[0]
            if len(opts) < 3:
                mnt_opt = ''
            else:
                mnt_opt = ','.join(opts[2:])
            m = rcMounts.Mount(dev, mnt, type, mnt_opt)
            self.mounts.append(m)

if __name__ == "__main__" :
    help(Mounts)
    for m in Mounts():
        print(m)
   0707010000bbd6000081a400000000000000000000000154fc7bfb000008cd000000660000000300000000000000000000002600000000root/opt/opensvc/lib/rcMountsHP-UX.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import rcMounts
from rcUtilities import *
from subprocess import Popen

def get_src_dir_dev(dev):
    """Given a directory path, return its hosting device
    """
    process = Popen(['df', '-l', dev], stdout=PIPE, stderr=STDOUT, close_fds=True)
    buff = process.communicate()
    out = buff[0]
    i = out.index('/')
    return out[i:].split()[0]

class Mounts(rcMounts.Mounts):

    def match_mount(self, i, dev, mnt):
        """Given a line of 'mount' output, returns True if (dev, mnt) matches
        this line. Returns False otherwize. Also care about weirdos like loops
        and binds, ...
        """
        if os.path.isdir(dev):
            is_bind = True
            src_dir_dev = get_src_dir_dev(dev)
        else:
            is_bind = False

        if i.mnt != mnt:
            return False
        if i.dev == dev:
            return True
        if is_bind and i.dev == src_dir_dev:
            return True
        return False

    def __init__(self):
        self.mounts = []
        out, err, ret = justcall(['mount', '-v'])
        for l in out.split('\n'):
            if len(l.split()) != 12:
                return
            dev, null, mnt, null, type, mnt_opt, null, null, null, null, null, null = l.split()
            m = rcMounts.Mount(dev, mnt, type, mnt_opt.strip('()'))
            self.mounts.append(m)

if __name__ == "__main__" :
    help(Mounts)
    for m in Mounts():
        print(m)
   0707010000bbd7000081a400000000000000000000000154fc7bfb000008fe000000660000000300000000000000000000002600000000root/opt/opensvc/lib/rcMountsLinux.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import rcMounts
import resLoopLinux as Res
from rcUtilities import *
from subprocess import Popen

def get_src_dir_dev(dev):
    """Given a directory path, return its hosting device
    """
    process = Popen(['df', '-l', dev], stdout=PIPE, stderr=STDOUT, close_fds=True)
    buff = process.communicate()
    out = buff[0]
    i = out.index('/')
    return out[i:].split()[0]

class Mounts(rcMounts.Mounts):

    def match_mount(self, i, dev, mnt):
        """Given a line of 'mount' output, returns True if (dev, mnt) matches
        this line. Returns False otherwize. Also care about weirdos like loops
        and binds, ...
        """
        if os.path.isdir(dev):
            is_bind = True
            src_dir_dev = get_src_dir_dev(dev)
        else:
            is_bind = False

        if i.mnt != mnt:
            return False
        if i.dev == dev:
            return True
        if i.dev in Res.file_to_loop(dev):
            return True
        if is_bind and i.dev == src_dir_dev:
            return True
        return False

    def __init__(self):
        self.mounts = []
        (ret, out, err) = call(['mount'])
        for l in out.split('\n'):
            if len(l.split()) != 6:
                return
            dev, null, mnt, null, type, mnt_opt = l.split()
            m = rcMounts.Mount(dev, mnt, type, mnt_opt.strip('()'))
            self.mounts.append(m)

if __name__ == "__main__" :
    help(Mounts)
    for m in Mounts():
        print(m)
  0707010000bbd8000081a400000000000000000000000154fc7bfb00000686000000660000000300000000000000000000002500000000root/opt/opensvc/lib/rcMountsOSF1.py  #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import rcMounts
from rcUtilities import justcall

class Mounts(rcMounts.Mounts):

    def match_mount(self, i, dev, mnt):
        """Given a line of 'mount' output, returns True if (dev, mnt) matches
        this line. Returns False otherwize. Also care about weirdos like loops
        and binds, ...
        """
        if i.mnt != mnt:
            return False
        if i.dev == dev:
            return True
        return False

    def __init__(self):
        self.mounts = []
        out, err, ret = justcall(['mount'])
        for l in out.split('\n'):
            l = l.replace(', ', ',')
            if len(l.split()) != 6:
                return
            dev, null, mnt, null, type, mnt_opt = l.split()
            m = rcMounts.Mount(dev, mnt, type, mnt_opt.strip('()'))
            self.mounts.append(m)

if __name__ == "__main__" :
    help(Mounts)
    for m in Mounts():
        print(m)
  0707010000bbd9000081a400000000000000000000000154fc7bfb00000793000000660000000300000000000000000000002600000000root/opt/opensvc/lib/rcMountsSunOS.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import rcMounts
from rcUtilities import *


class Mounts(rcMounts.Mounts):

    def match_mount(self, i, dev, mnt):
        """Given a line of 'mount' output, returns True if (dev, mnt) matches
        this line. Returns False otherwize. Also care about weirdos like loops
        and binds, ...
        """
        if i.mnt != mnt:
            return False
        if i.dev == dev:
            return True
        return False

    def __init__(self):
        self.mounts = []
        (ret, out, err) = call(['mount','-p'], outdebug=False)
        for line in out.split('\n'):
            words=line.split()
            if len(words) < 6 :
                continue
            elif words[1]+words[4] != '--' :
                # ignore mount line with space in mountpoint or dev
                continue
            elif len(words) == 6 :
                words.append('-')
            dev, null, mnt, type, null, null, mnt_opt = words
            m = rcMounts.Mount(dev, mnt, type, mnt_opt.strip('()'))
            self.mounts.append(m)

if __name__ == "__main__" :
    help(Mounts)
    M=Mounts()
    print(M)
    
 0707010000bbda000081a400000000000000000000000154fc7bfb0000296a000000660000000300000000000000000000002100000000root/opt/opensvc/lib/rcNecIsm.py  from rcUtilities import which, justcall
import rcExceptions as ex
import os

class Nec(object):
    arrays = []
    view_bin = None
    sc_query_bin = None
    sc_linkinfo_bin = None
    sc_unlink_bin = None
    sc_link_bin = None
    sc_create_bin = None

    def get_bin(self, bin_attr, candidates):
        if getattr(self, bin_attr) is not None:
            return
        for bin in candidates:
            if which(bin) is not None:
                setattr(self, bin_attr, bin)
                break
        if getattr(self, bin_attr) is None:
            raise ex.excError('Can not find %s program in PATH' % ' or '.join(candidates))

    def get_view_bin(self):
        self.get_bin('view_bin', ['iSMcc_view', 'iSMview'])

    def get_sc_query_bin(self):
        self.get_bin('sc_query_bin', ['iSMsc_query'])

    def get_sc_linkinfo_bin(self):
        self.get_bin('sc_linkinfo_bin', ['iSMsc_linkinfo'])

    def get_sc_link_bin(self):
        self.get_bin('sc_link_bin', ['iSMsc_link'])

    def get_sc_create_bin(self):
        self.get_bin('sc_create_bin', ['iSMsc_create'])

    def get_sc_unlink_bin(self):
        self.get_bin('sc_unlink_bin', ['iSMsc_unlink'])

    def view_cmd(self, cmd, on_array=True):
	self.get_view_bin()
        cmd = [self.view_bin] + cmd
        if on_array:
            cmd += [self.name]
        return justcall(cmd)

    def sc_query_cmd(self, cmd):
	self.get_sc_query_bin()
        cmd = [self.sc_query_bin] + cmd
        return justcall(cmd)

    def sc_linkinfo_cmd(self, cmd):
	self.get_sc_linkinfo_bin()
        cmd = [self.sc_linkinfo_bin] + cmd
        return justcall(cmd)

    def sc_unlink_cmd(self, cmd):
	self.get_sc_unlink_bin()
        cmd = [self.sc_unlink_bin] + cmd
        self.log.info(' '.join(cmd))
        return justcall(cmd)

    def sc_link_cmd(self, cmd):
	self.get_sc_link_bin()
        cmd = [self.sc_link_bin] + cmd
        self.log.info(' '.join(cmd))
        return justcall(cmd)

    def sc_create_cmd(self, cmd):
	self.get_sc_create_bin()
        cmd = [self.sc_create_bin] + cmd
        self.log.info(' '.join(cmd))
        return justcall(cmd)

    def sc_create_ld(self, bv, sv):
        cmd = ['-bv', bv, '-sv', sv, '-bvflg', 'ld', '-svflg', 'ld']
        out, err, ret = self.sc_create_cmd(cmd)
        self.log.info(out)
        if ret != 0:
            raise ex.excError(err)

    def sc_unlink_ld(self, ld):
        cmd = ['-lv', ld, '-lvflg', 'ld']
        out, err, ret = self.sc_unlink_cmd(cmd)
        self.log.info(out)
        if ret != 0:
            raise ex.excError(err)

    def sc_link_ld(self, sv, ld):
        cmd = ['-lv', ld, '-sv', sv, '-lvflg', 'ld', '-svflg', 'ld']
        out, err, ret = self.sc_link_cmd(cmd)
        self.log.info(out)
        if ret != 0:
            raise ex.excError(err)

    def get_arrays(self):
        cmd = ['-d']
        out, err, ret = self.view_cmd(cmd, on_array=False)
        if ret != 0:
            self.refresh_vollist()
        out, err, ret = self.view_cmd(cmd, on_array=False)
        if ret != 0:
            raise ex.excError(err)

        """

--- Disk Array List ---
Product ID        Disk Array Name                   Resource State  Monitoring
D1-10             D1_10                             ready           running


--- Disk Array List ---
Product ID        Disk Array Name                   Resource State
Optima3600        Optima7_LMW                       ready


        """
        lines = out.split('\n')
        for line in lines:
            if len(line) == 0:
                continue
            if '---' in line:
                continue
            if 'Product ID' in line:
                continue
            l = line.split()
            if len(l) < 3:
                continue
            if self.filtering and l[1] not in self.objects:
                continue
            self.arrays.append(NecIsm(l[1]))

    def sc_linkinfo_ld(self, vol):
        """

Specified Volume Information
SV:LD Name      : test_src_0000_SV0014
   Type         : LX
   Special File : -
   State        : link   (test_src_0000_LV0064)
   Mode         : nr

Destination Volume Information
    LV:test_src_0000_LV0050     LX link   (test_src_0000_SV0016)    rw
    LV:test_src_0000_LV005A     LX link   (test_src_0000_SV0015)    rw
    LV:test_src_0000_LV0064     LX link   (test_src_0000_SV0014)    rw

"""
        cmd = ['-vol', vol, '-volflg', 'ld', '-lcl']
        out, err, ret = self.sc_linkinfo_cmd(cmd)
        if ret != 0:
            raise ex.excError(err)
        data = {'dst': []}
        for line in out.split('\n'):
            if line.startswith('SV:LD Name'):
                data['SV:LD Name'] = line.split(': ')[1]
            elif line.strip().startswith('Type'):
                data['Type'] = line.split(': ')[1]
            elif line.strip().startswith('Special File'):
                data['Special File'] = line.split(': ')[1]
            elif line.strip().startswith('State'):
                data['State'] = line.split(': ')[1]
            elif line.strip().startswith('Mode'):
                data['Mode'] = line.split(': ')[1]
            elif line.strip().startswith('LV:'):
                data['dst'].append(line.split(':')[1])
        return data

    def sc_query_ld(self, sv):
        """
BV Information
    LD Name      : test_src_0000
    Type         : LX
    Special File : /dev/sdc
    State        : normal
    Reserve Area : -

SV Information
  LX:test_src_0000_SV0014    ( -1) snap/active   [2014/03/24 11:16:16] link
        """
        cmd = ['-sv', sv, '-svflg', 'ld']
        out, err, ret = self.sc_query_cmd(cmd)
        if ret != 0:
            raise ex.excError(err)
        data = {'sv': []}
        for line in out.split('\n'):
            if line.strip().startswith('LD Name'):
                data['LD Name'] = line.split(': ')[1]
            elif line.strip().startswith('Type'):
                data['Type'] = line.split(': ')[1]
            elif line.strip().startswith('Special File'):
                data['Special File'] = line.split(': ')[1]
            elif line.strip().startswith('State'):
                data['State'] = line.split(': ')[1]
            elif line.strip().startswith('Reserve Area'):
                data['Reserve Area'] = line.split(': ')[1]
            elif line.strip().startswith('LX:'):
                data['sv'].append(line[line.index(':')+1:])
        return data

    def sc_query_bv_detail(self, bv):
        """
        BV Information
            LD Name      : xxxxxxxxxxx_00CC
            Type         : LX
            Special File : /dev/sdaq
            State        : normal
            Reserve Area : -
        
        Pair Information
            SV:LD Name              : xxxxxxxxxxx_00cc_SV00ce
               Type                 : LX
               Generation(Attribute): -1(normal)
               Snap State           : snap/active        [2014/09/09 17:27:45]
               Create   Start Time  : 2014/09/09 17:27:45
               Processing Data Size : -
               Snapshot Data Size   : 47.4GB
               SV Guard             : off
               LV Link Status       : link
            LV:LD Name              : xxxxxxxxxxx_00cc_LV00cf
               Type                 : LX
               Special File         : /dev/sdar
               LV Access            : rw
            SV:LD Name              : xxxxxxxxxxx_00cc_SV00cd
               Type                 : LX
               Generation(Attribute): -2(normal)
               Snap State           : snap/active        [2014/09/09 17:19:12]
               Create   Start Time  : 2014/09/09 17:19:12
               Processing Data Size : -
               Snapshot Data Size   : 11.6GB
               SV Guard             : off
               LV Link Status       : link
            LV:LD Name              : xxxxxxxxxxx_00cc_LV00d0
               Type                 : LX
               LV Access            : rw
        """
        cmd = ['-bv', bv, '-bvflg', 'ld', '-detail']
        out, err, ret = self.sc_query_cmd(cmd)
        if ret != 0:
            raise ex.excError(err)
        data = {
          'sv': {},
          'lv': {}
        }
        section = ""
        for line in out.split('\n'):
            line = line.strip()
            if line.startswith("BV Information"):
                section = "bvinfo"
                continue
            elif line.startswith("Pair Information"):
                section = "pairinfo"
                continue

            if section == "bvinfo" and line.startswith("LD Name"):
                data['LD Name'] = line.split(': ')[1]
            elif section == "bvinfo" and line.startswith("State"):
                data['State'] = line.split(': ')[1]

            elif section.startswith("pairinfo") and line.startswith("SV:LD Name"):
                ld_name = line.split(': ')[1]
                if ld_name not in data['sv']:
                    data['sv'][ld_name] = {}
                section = "pairinfo_sv"
            elif section == "pairinfo_sv" and line.startswith("Snap State"):
                data['sv'][ld_name]["Snap State"] = line.split(': ')[1]


            elif section.startswith("pairinfo") and line.startswith("LV:LD Name"):
                ld_name = line.split(': ')[1]
                if ld_name not in data['lv']:
                    data['lv'][ld_name] = {}
                section = "pairinfo_lv"
            elif section == "pairinfo_lv" and line.startswith("Type"):
                data['lv'][ld_name]["Type"] = line.split(': ')[1]

        return data



class NecIsms(Nec):
    def __init__(self, objects=[]):
        self.objects = objects
        if len(objects) > 0:
            self.filtering = True
        else:
            self.filtering = False
        self.index = 0
        self.get_arrays()

    def __iter__(self):
        return self

    def refresh_vollist(self):
        if which('iSMvollist') is None:
            return
        cmd = ['iSMvollist', '-r']
        out, err, ret = justcall(cmd)
 
    def next(self):
        if self.index == len(self.arrays):
            raise StopIteration
        self.index += 1
        return self.arrays[self.index-1]

class NecIsm(Nec):
    def __init__(self, name):
        self.keys = ['all']
        self.name = name

    def get_all(self):
        cmd = ['-all']
        out, err, ret = self.view_cmd(cmd)
        return out

if __name__ == "__main__":
    o = NecIsms()
    for necism in o:
        print(necism.get_all())
        #print(o.sc_linkinfo_ld("test_src_0000_SV0014"))
        #print(o.sc_query_ld("test_src_0000_SV0014"))

  0707010000bbdb000081ed00000000000000000000000154fc7bfb00002dd7000000660000000300000000000000000000002200000000root/opt/opensvc/lib/rcNexenta.py #!/opt/opensvc/bin/python
#
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import json
import urllib2
import base64
import os
import rcExceptions as ex

class logger(object):
    def __init__(self):
        pass

    def info(self, msg):
        print(msg)
    
    def warning(self, msg):
        print(msg)
    
    def error(self, msg):
        print(msg)

class Nexenta(object):
    def __init__(self, head, log=None):
        self.object_type_cache = {}
        self.head = head
        self.auto_prefix = "svc:/system/filesystem/zfs/auto-sync:"
        self.username = None
        self.password = None
        self.port = 2000
        if log is not None:
            self.log = log
        else:
            self.log = logger()
   
    def init(self):
        if self.username is not None and self.password is not None:
            return
        import ConfigParser
        cf = os.path.join(os.path.dirname(__file__), '..', 'etc', "auth.conf")
        self.conf = ConfigParser.RawConfigParser()
        self.conf.read(cf)
        if not self.conf.has_section(self.head):
            raise ex.excError("no auth information for head %s"%self.head)
        if not self.conf.has_option(self.head, "username"):
            raise ex.excError("no username information for head %s"%self.head)
        if not self.conf.has_option(self.head, "password"):
            raise ex.excError("no password information for head %s"%self.head)
        self.username = self.conf.get(self.head, "username")
        self.password = self.conf.get(self.head, "password")
        if self.conf.has_option(self.head, "port"):
            self.port = self.conf.get(self.head, "port")
        self.url = 'https://%(head)s:%(port)d/rest/nms/ <https://%(head)s:%(port)d/rest/nms/>'%dict(head=self.head, port=self.port)

    def rest(self, obj, method, params):
        self.init()
        data = {"method": method, "params": params, "object": obj}
        data = json.dumps(data)
        request = urllib2.Request(self.url, data)
        base64string = base64.encodestring('%s:%s' % (self.username, self.password))[:-1]
        request.add_header('Authorization', 'Basic %s' % base64string)
        request.add_header('Content-Type' , 'application/json')
        try:
            response = urllib2.urlopen(request)
        except urllib2.URLError:
            raise ex.excError("unreachable head %s"%self.head)
        response = json.loads(response.read())
        return response

    def dbus_auth_keys_list(self):
        data = self.rest("appliance", "dbus_auth_keys_list", [])
        if data['error'] is not None:
            raise ex.excError(data['error'])
        return data['result']

    def ssh_list_bindings(self):
        data = self.rest("appliance", "ssh_list_bindings", [])
        if data['error'] is not None:
            raise ex.excError(data['error'])
        return data['result']

    def ssh_unbind(self, user, hostport, force="0"):
        data = self.rest("appliance", "ssh_unbind", [user, hostport, force])
        if data['error'] is not None:
            raise ex.excError(data['error'])
        return data['result']

    def ssh_bind(self, user, hostport, password):
        data = self.rest("appliance", "ssh_bind", [user, hostport, password])
        if data['error'] is not None:
            raise ex.excError(data['error'])
        return data['result']

    def autosync_get_names(self):
        data = self.rest("autosync", "get_names", [''])
        if data['error'] is not None:
            raise ex.excError(data['error'])
        return data['result']

    def autosync_disable(self, name):
        if not name.startswith(self.auto_prefix):
            name = self.auto_prefix+name
        data = self.rest("autosync", "disable", [name])
        if data['error'] is not None:
            raise ex.excError(data['error'])
        return data['result']

    def autosync_enable(self, name):
        if not name.startswith(self.auto_prefix):
            name = self.auto_prefix+name
        data = self.rest("autosync", "enable", [name])
        if data['error'] is not None:
            raise ex.excError(data['error'])
        return data['result']

    def autosync_execute(self, name):
        if not name.startswith(self.auto_prefix):
            name = self.auto_prefix+name
        data = self.rest("autosync", "execute", [name])
        if data['error'] is not None:
            raise ex.excError(data['error'])
        return data['result']

    def autosync_get_state(self, name):
        if not name.startswith(self.auto_prefix):
            name = self.auto_prefix+name
        data = self.rest("autosync", "get_state", [name])
        if data['error'] is not None:
            raise ex.excError(data['error'])
        return data['result']

    def autosync_set_prop(self, name, prop, value):
        if not name.startswith(self.auto_prefix):
            name = self.auto_prefix+name
        data = self.rest("autosync", "set_child_prop", [name, prop, value])
        if data['error'] is not None:
            raise ex.excError(data["error"])
        return data['result']

    def autosync_get_props(self, name):
        if not name.startswith(self.auto_prefix):
            name = self.auto_prefix+name
        data = self.rest("autosync", "get_child_props", [name, ''])
        if data['error'] is not None:
            raise ex.excError(data["error"])
        return data['result']

    def autosync_register(self, name):
        if not name.startswith(self.auto_prefix):
            name = self.auto_prefix+name
        data = self.rest("runner", "register", [name, {}, {}])
        if data['error'] is not None:
            raise ex.excError(data["error"])
        return data['result']
 
    def zvol_clone(self, src, dst):
        data = self.rest("zvol", "clone", [src, dst])
        if data['error'] is not None:
            raise ex.excError(data["error"])

    def folder_clone(self, src, dst):
        data = self.rest("folder", "clone", [src, dst])
        if data['error'] is not None:
            raise ex.excError(data["error"])

    def clone(self, src, dst):
        snap = "@".join([src, dst.replace('/','_')])
        object_type = self.object_type(src)
        if object_type == "folder":
            self.folder_clone(snap, dst)
        elif object_type == "zvol":
            self.zvol_clone(snap, dst)
        else:
            raise ex.excError("object type %s is not cloneable"%str(object_type))

    def snapshot_create(self, src, dst, recursive=0):
        dst = dst.replace('/','_')
        object_type = self.object_type(src)
        if object_type == "folder":
            self.folder_snapshot(src, dst, recursive)
        elif object_type == "zvol":
            self.zvol_snapshot(src, dst, recursive)
        else:
            raise ex.excError("object type %s is not snapable"%str(object_type))

    def zvol_snapshot(self, src, dst, recursive=0):
        data = self.rest("zvol", "create_snapshot", [src, dst, recursive])
        if data['error'] is not None:
            raise ex.excError(data["error"])

    def folder_snapshot(self, src, dst, recursive=0):
        snap = "@".join([src, dst])
        data = self.rest("snapshot", "create", [snap, recursive])
        if data['error'] is not None:
            raise ex.excError(data["error"])

    def snapshot_destroy(self, src, dst, recursive=''):
        snap = "@".join([src, dst])
        data = self.rest("snapshot", "destroy", [snap, recursive])
        if data['error'] is not None:
            raise ex.excError(data["error"])

    def snapshot_get_names(self):
        data = self.rest("snapshot", "get_names", [''])
        if data['error'] is not None:
            raise ex.excError(data["error"])
        return data['result']

    def folder_get_names(self):
        data = self.rest("folder", "get_names", [''])
        if data['error'] is not None:
            raise ex.excError(data["error"])
        for folder in data['result']:
            self.object_type_cache[folder] = "folder"
        return data['result']

    def zvol_get_names(self):
        data = self.rest("zvol", "get_names", [''])
        if data['error'] is not None:
            raise ex.excError(data["error"])
        for zvol in data['result']:
            self.object_type_cache[folder] = "zvol"
        return data['result']

    def object_type(self, o):
        if o in self.object_type_cache:
            return self.object_type_cache[o]
        if o in self.folder_get_names():
            self.object_type_cache[o] = "folder"
            return "folder"
        elif o in self.zvol_get_names():
            self.object_type_cache[o] = "zvol"
            return "zvol"
        else:
            raise ex.excError("can not determine type of object %s"%o)

    def set_prop(self, name, prop, val):
        otype = self.object_type(name)
        return self._set_prop(otype, name, prop, val)

    def _set_prop(self, otype, name, prop, val):
        data = self.rest(otype, "set_child_prop", [name, prop, val])
        if data['error'] is not None:
            raise ex.excError(data["error"])
        return data['result']

    def get_props(self, name):
        otype = self.object_type(name)
        return self._get_props(otype, name)

    def _get_props(self, otype, name):
        data = self.rest(otype, "get_child_props", [name, ''])
        if data['error'] is not None:
            raise ex.excError(data["error"])
        return data['result']

    def set_can_mount(self, name):
        p = self.get_props(name)
        if not 'canmount' in p:
            return
        self.set_prop(name, "canmount", "on")

    def autosync_set_can_mount(self, name):
        folders = self.folder_get_names()
        props = self.autosync_get_props(name)

        if props['zfs/from-host'] == 'localhost':
            synchead = props['zfs/from-fs']
        else:
            synchead = props['zfs/to-fs']

        synchead = synchead.lstrip('/')
        for folder in folders:
            if not folder.startswith(synchead):
                continue
            self.set_can_mount(folder)
            self.log.info("set 'canmount = on' on folder %s"%folder)

    def snapclone(self, src, dst):
        self.snapshot_create(src, dst)
        self.clone(src, dst)

if __name__ == "__main__":
    o = Nexenta("nexenta1")
    #names = o.autosync_register("test")
    #print(o.set_prop("vol1/folder1", "canmount", "on"))
    #print(o.get_props("vol1/folder1"))
    print(Nexenta("nexenta1").dbus_auth_keys_list())
    print(Nexenta("nexenta2").dbus_auth_keys_list())
    #print(o.autosync_set_can_mount("vol1-folder1-000"))
    #names = o.autosync_get_names()
    #print(o.autosync_set_prop(names[0], "zfs/reverse_capable", "1"))
    #print(o.autosync_get_state(names[0]))
    #print(o.autosync_get_props(names[0]))
    #print(o.snapshot_create("vol1/zvol1", "test"))
    #print(o.snapshot_get_names())
    #print(o.snapshot_destroy("vol1/zvol1", "test"))
    #print(o.snapclone("vol1/folder1", "vol1/folder2"))

 0707010000bbdc000081a400000000000000000000000154fc7bfb00000629000000660000000300000000000000000000001f00000000root/opt/opensvc/lib/rcNode.py    #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import sys
import re
from rcGlobalEnv import *
import logging
import rcExceptions as ex

try:
    import ConfigParser
except ImportError:
    import configparser as ConfigParser

def node_get_hostmode():
    nodeconf = os.path.join(os.path.dirname(__file__), '..', 'etc', 'node.conf')
    config = ConfigParser.RawConfigParser()
    config.read(nodeconf)
    if config.has_section('node'):
        if config.has_option('node', 'host_mode'):
            return config.get('node', 'host_mode')
    elif config.has_option('DEFAULT', 'host_mode'):
        return config.get('DEFAULT', 'host_mode')
    return 'TST'

def discover_node():
    """Fill rcEnv class with information from node discovery
    """
    rcEnv.host_mode = node_get_hostmode()

   0707010000bbdd000081a400000000000000000000000154fc7bfb0000049b000000660000000300000000000000000000001e00000000root/opt/opensvc/lib/rcNsr.py from rcUtilities import justcall, which
import rcExceptions as ex
import os
import socket

class Nsr(object):
    def __init__(self):
        if not which('mminfo'):
            raise ex.excError('mminfo not found')
        self.keys = ['mminfo']

    def get_mminfo(self):
        os.environ["LC_TIME"] = "en_DK"
        cmd = ['mminfo', '-x', 'c;', '-q', 'savetime>=last day', '-r', 'client,name,group,totalsize,savetime(30),ssretent(30),volume,level,ssid(53)']
        print(' '.join(cmd))
        lines = justcall(cmd)[0].split('\n')[1:]
        for li, line in enumerate(lines):
            if len(line) == 0:
                continue
            try:
                i = line.index(';')
            except ValueError:
                continue
            client = line[:i]
            try:
                a = socket.getaddrinfo(client, None)
            except socket.gaierror:
                a = []
            if len(a) > 0:
                ip = a[0][-1][0]
            else:
                ip = client
            lines[li] = ip + line[i:]
        return unicode('\n'.join(lines), errors='ignore')


if __name__ == "__main__":
    o = Nsr()
    print(o.get_mminfo())
 0707010000bbde000081a400000000000000000000000154fc7bfb000032b1000000660000000300000000000000000000002400000000root/opt/opensvc/lib/rcOptParser.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import optparse
from rcGlobalEnv import *

action_desc = {
    'Service actions': {
      'print_config': 'display service current configuration',
      'edit_config': 'edit service configuration',
      'boot': 'start a service if executed on the primary node (or one of the primary nodes in case of a flex service), startstandby if not',
      'shutdown': 'stop a service, disabling the background database logging',
      'start': 'start a service, chaining startip-diskstart-startapp',
      'startstandby': 'start service resources marked always_on',
      'startip': 'configure service ip addresses',
      'startshare': 'start network shares',
      'stopshare': 'stop network shares',
      'startfs': 'prepare devices, logical volumes, mount service filesystems, bootstrap containers',
      'startapp': 'execute service application startup script',
      'stop': 'stop a service, chaining stopapp-stopdisk-stopip',
      'stopip': 'unconfigure service ip addresses',
      'stopfs': 'shutdown container, umount service filesystems, deactivate logical volumes',
      'stopapp': 'execute service application stop script',
      'syncnodes': 'send to peer nodes the service config files and additional files described in the config file. --force bypass the schedule check.',
      'syncdrp': 'send to drp nodes the service config files and additional files described in the config file. --force bypass the schedule check.',
      'startcontainer': 'start the container resource',
      'stopcontainer': 'stop the container resource',
      'disable': 'disable resources passed through --rid in services passed through --service. Specifying no resource disables the whole service.',
      'enable': 'enable resources passed through --rid in services passed through --service. Specifying no resource enables the whole service.',
      'status': 'return service overall status code',
      'print_status': 'display service resource status',
      'print_resource_status': 'display a specific service resource status, pointed by --rid',
      'print_env_mtime': 'display service env file modification time',
      'freeze': 'set up a flag to block actions on this service',
      'thaw': 'remove the flag to unblock actions on this service',
      'frozen': 'report on the current blocking of actions on this service',
      'startloop': 'attach loop resources of this service',
      'stoploop': 'detach loop resources of this service',
      'startvg': 'activate/import volume group resources of this service',
      'stopvg': 'deactivate/deport volume group resources of this service',
      'startpool': 'activate zpool resources of this service',
      'stoppool': 'deactivate zpool resources of this service',
      'startdisk': 'combo action, activating standby disks, taking reservations, starting loopback devices and volume groups',
      'stopdisk': 'combo action, stopping volume groups and loopback devices, droping reservations, disabling standby disks',
      'presync': 'update var files associated to resources',
      'postsync': 'make use of files received from master nodes in var',
      'prstart': 'reserve scsi disks held by this service',
      'prstop': 'release scsi disks held by this service',
      'prstatus': 'report status of reservations on scsi disks held by this service',
      'restart': 'combo action, chaining stop-start',
      'resync': 'combo action, chaining stop-syncresync-start',
      'syncquiesce': 'trigger a storage hardware-assisted disk synchronization',
      'syncbreak': 'split a storage hardware-assisted disk synchronization',
      'syncsplit': 'split a EMC SRDF storage hardware-assisted disk synchronization',
      'syncestablish': 'establish a EMC SRDF storage hardware-assisted disk synchronization',
      'syncresync': 'like syncupdate, but not triggered by the scheduler (thus adapted for clone/snap operations)',
      'syncfullsync': 'trigger a full copy of the volume to its target',
      'syncupdate': 'trigger a one-time resync of the volume to its target',
      'syncresume': 're-establish a broken storage hardware-assisted synchronization',
      'syncrevert': 'revert to the pre-failover data (looses current data)',
      'syncverify': 'trigger a one-time checksum-based verify of the volume and its target',
      'syncall': 'combo action, chaining syncnodes-syncdrp-syncupdate.  --force bypass the schedule check.',
      'push': 'push service configuration to database',
      'push_appinfo': 'push service application launchers appinfo key/value pairs to database',
      'print_disklist': 'print service disk list',
      'print_devlist': 'print service device list',
      'switch': 'stop the service on the local node and start on the remote node. --to <node> specify the remote node to switch the service to.',
      'migrate': 'live migrate the service to the remote node. --to <node> specify the remote node to migrate the service to.',
      'json_status': 'provide the resource and aggregated status in json format, for use by tier tools',
      'json_env': 'provide the service configuration in json format, for use by tier tools',
      'json_disklist': 'provide the service disk list in json format, for use by tier tools',
      'json_devlist': 'provide the service device list in json format, for use by tier tools',
      'resource_monitor': 'detect monitored resource failures and trigger monitor_action',
      'stonith': 'command provided to the heartbeat daemon to fence peer node in case of split brain',
      'docker': 'wrap the docker client command, setting automatically the socket parameter to join the service-private docker daemon',
     },
    'Service configuration': {
      'install': 'install a service using the configuration file pointed by --envfile. used by master services when pushing the envfile to their slave.',
      'create': 'create a new service configuration file. --interactive triggers the interactive mode',
      'update': 'update definitions in an existing service configuration file',
      'delete': 'delete resources passed through --rid in services passed through --service',
      'set': 'set a service configuration parameter (pointed by --param) value (pointed by --value)',
      'get': 'get the value of the node configuration parameter pointed by --param',
      'unset': 'unset a node configuration parameter pointed by --param',
     },
    'Compliance': {
      'compliance_check': 'run compliance checks. --ruleset <md5> instruct the collector to provide an historical ruleset.',
      'compliance_env': 'show the compliance modules environment variables.',
      'compliance_fix': 'run compliance fixes. --ruleset <md5> instruct the collector to provide an historical ruleset.',
      'compliance_fixable': 'verify compliance fixes prerequisites. --ruleset <md5> instruct the collector to provide an historical ruleset.',
      'compliance_show_status': 'show compliance modules status',
      'compliance_show_moduleset': 'show compliance rules applying to this service',
      'compliance_list_moduleset': 'list available compliance modulesets. --moduleset f% limit the scope to modulesets matching the f% pattern.',
      'compliance_attach_moduleset': 'attach moduleset specified by --moduleset to this service',
      'compliance_detach_moduleset': 'detach moduleset specified by --moduleset from this service',
      'compliance_list_ruleset': 'list available compliance rulesets. --ruleset f% limit the scope to rulesets matching the f% pattern.',
      'compliance_show_ruleset': 'show compliance rules applying to this node',
      'compliance_attach_ruleset': 'attach ruleset specified by --ruleset to this service',
      'compliance_detach_ruleset': 'detach ruleset specified by --ruleset from this service',
      'compliance_attach': 'attach ruleset specified by --ruleset and/or moduleset specified by --moduleset to this service',
      'compliance_detach': 'detach ruleset specified by --ruleset and/or moduleset specified by --moduleset from this service',
     },
    'Collector management': {
      'collector_ack_unavailability': 'acknowledge an unavailability period. the period is specified by --begin/--end or --begin/--duration. omitting --begin defaults to now. an acknowlegment can be completed by --author (defaults to root@nodename), --account (default to 1) and --comment',
      'collector_list_unavailability_ack': 'list acknowledged periods for the service. the periods can be filtered by --begin/--end. omitting --end defaults to now. the wildcartd for --comment and --author is %',
      'collector_list_actions': 'list actions on the service, whatever the node, during the period specified by --begin/--end. --end defaults to now. --begin defaults to 7 days ago',
      'collector_ack_action': 'acknowledge an action error on the service. an acknowlegment can be completed by --author (defaults to root@nodename) and --comment',
      'collector_show_actions': 'show actions detailled log. a single action is specified by --id. a range is specified by --begin/--end dates. --end defaults to now. --begin defaults to 7 days ago',
      'collector_status': 'display service status on all nodes, according to the collector.',
      'collector_checks': 'display service checks',
      'collector_disks': 'display service disks',
      'collector_alerts': 'display service alerts',
      'collector_events': 'display service events during the period specified by --begin/--end. --end defaults to now. --begin defaults to 7 days ago',
      'collector_asset': 'display asset information known to the collector',
      'collector_networks': 'display network information known to the collector for each service ip',
      'collector_json_asset': 'display asset information known to the collector, output in JSON',
      'collector_json_networks': 'display network information known to the collector for each service ip, output in JSON',
      'collector_json_list_unavailability_ack': 'same as "collector list unavailability ack", output in JSON',
      'collector_json_list_actions': 'same as "collector list actions", output in JSON',
      'collector_json_show_actions': 'same as "collector show actions", output in JSON',
      'collector_json_status': 'same as "collector status", output in JSON',
      'collector_json_checks': 'same as "collector checks", output in JSON',
      'collector_json_disks': 'same as "collector disks", output in JSON',
      'collector_json_alerts': 'same as "collector alerts", output in JSON',
      'collector_json_events': 'same as "collector events", output in JSON',
      'collector_tag': 'set a service tag (pointed by --tag)',
      'collector_untag': 'unset a service tag (pointed by --tag)',
      'collector_show_tags': 'list all service tags',
      'collector_list_tags': 'list all available tags. use --like to filter the output.',
      'collector_create_tag': 'create a new tag',
    },
}

def format_desc(svc=False, action=None):
    from textwrap import TextWrapper
    wrapper = TextWrapper(subsequent_indent="%19s"%"", width=78)
    desc = ""
    for s in sorted(action_desc):
        valid_actions = []
        for a in sorted(action_desc[s]):
            if action is not None and not a.startswith(action):
                continue
            valid_actions.append(a)
        if len(valid_actions) == 0:
            continue

        l = len(s)
        desc += s+'\n'
        for i in range(0, l):
            desc += '-'
        desc += "\n\n"
        for a in valid_actions:
            if svc and not hasattr(svc, a):
                continue
            fancya = a.replace('_', ' ')
            if len(a) < 16:
                text = "  %-16s %s\n"%(fancya, action_desc[s][a])
                desc += wrapper.fill(text)
            else:
                text = "  %-16s"%(fancya)
                desc += wrapper.fill(text)
                desc += '\n'
                text = "%19s%s"%(" ", action_desc[s][a])
                desc += wrapper.fill(text)
            desc += '\n\n'
    return desc[0:-2]

def supported_actions():
    a = []
    for s in action_desc:
        a += action_desc[s].keys()
    return a
   0707010000bbdf000081a400000000000000000000000154fc7bfb00000080000000660000000300000000000000000000001d00000000root/opt/opensvc/lib/rcOs.py  class Os(object):
    def reboot(self):
        print("not implemented")

    def crash(self):
        print("not implemented")
0707010000bbe0000081a400000000000000000000000154fc7bfb0000007a000000660000000300000000000000000000002200000000root/opt/opensvc/lib/rcOsHP-UX.py import rcOs
from rcUtilities import justcall

class Os(rcOs.Os):
    def reboot(self):
        justcall(['reboot', '-q'])
  0707010000bbe1000081a400000000000000000000000154fc7bfb000000d8000000660000000300000000000000000000002200000000root/opt/opensvc/lib/rcOsLinux.py import shutil
import rcOs

class Os(rcOs.Os):
    def reboot(self):
        with open("/proc/sysrq-trigger", "w") as f:
            f.write("b")

    def crash(self):
        shutil.copyfile('/dev/zero', '/dev/mem')
0707010000bbe2000081a400000000000000000000000154fc7bfb000000b1000000660000000300000000000000000000002200000000root/opt/opensvc/lib/rcOsSunOS.py import rcOs
from rcUtilities import justcall

class Os(rcOs.Os):
    def reboot(self):
        justcall(['reboot', '-q'])

    def crash(self):
        justcall(['halt', '-q'])
   0707010000bbe3000081ed00000000000000000000000154fc7bfb00001193000000660000000300000000000000000000001e00000000root/opt/opensvc/lib/rcOvm.py #!/opt/opensvc/bin/python
#
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2011 Cyril Galibern <cyril.galibern@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import subprocess
from rcUtilities import which, call
import rcExceptions as ex
from rcGlobalEnv import rcEnv

class Ovm(object):
    def __init__(self, log=None):
        self.ovmcli = 'ovm'
        if log is not None:
            self.log = log
        else:
            import logging
            self.log = logging.getLogger('OVM')

        import ConfigParser
        cf = os.path.join(os.path.dirname(__file__), '..', 'etc', "auth.conf")
        self.conf = ConfigParser.RawConfigParser()
        self.conf.read(cf)
        if not self.conf.has_section("ovm"):
            raise ex.excError("no auth information for OVM manager")
        if not self.conf.has_option("ovm", "username"):
            raise ex.excError("no username information for OVM manager")
        if not self.conf.has_option("ovm", "password"):
            raise ex.excError("no password information for OVM manager")
        self.username = self.conf.get("ovm", "username")
        self.password = self.conf.get("ovm", "password")

    def test(self):
        if which(self.ovmcli):
            self.log.error("ovm CLI is not installed")
            return False
        if self.username is None:
            self.log.error("manager username is not set")
            return False
        if self.password is None:
            self.log.error("manager password is not set")
            return False
        return True

    def ovm(self, args, check=True, verbose=True):
        if not self.test:
            raise ex.excError
        cmd = [self.ovmcli, '-u', self.username, '-p', self.password, '-S";"'] + args
        if verbose:
            _cmd = [self.ovmcli, '-u', self.username, '-p', 'XXXXXX'] + args
            self.log.info(subprocess.list2cmdline(_cmd))
            ret, out, err = call(cmd, log=self.log)
            if 'Error:' in out > 0:
                self.log.error(out)
            else:
                self.log.info(out)
        else:
            ret, out, err = call(cmd, log=self.log)
        if check and ret != 0:
            raise ex.excError("ovm command execution error")
        return ret, out, err

    def get_pool(self):
        cmd = ['svr', 'ls']
        ret, out, err = self.ovm(cmd, verbose=False)
        for line in out.split('\n'):
            l = line.split(';')
            if len(l) != 4:
                continue
            if l[1].strip('"') == rcEnv.nodename:
                return l[3].strip('"')
        raise ex.excError("can't find node's pool name")

    def vm_enable_ha(self, vm):
        pool = self.get_pool()
        cmd = ['vm', 'conf', '-n', vm, '-s', pool, '-e']
        self.ovm(cmd)

    def vm_disable_ha(self, vm):
        pool = self.get_pool()
        cmd = ['vm', 'conf', '-n', vm, '-s', pool, '-d']
        self.ovm(cmd)

    def vm_info(self, vm):
        pool = self.get_pool()
        cmd = ['vm', 'info', '-n', vm, '-s', pool]
        ret, out, err = self.ovm(cmd, verbose=False)
        if ret != 0:
            raise ex.excError("failed to fetch VM information from manager")
        h = {}
        for line in out.split('\n'):
            l = line.split(':')
            if len(l) != 2:
                continue
            h[l[0].strip()] = l[1].strip()
        return h

    def vm_ha_enabled(self, vm):
        info = self.vm_info(vm)
        if 'Hign Availability' in info and info['Hign Availability'] == 'Enabled':
            return True
        if 'High Availability' in info and info['High Availability'] == 'Enabled':
            return True
        return False

if __name__ == "__main__":
    o = Ovm()
    #o.vm_disable_ha("ovmguest1")
    #o.vm_enable_ha("ovmguest1")
 0707010000bbe4000081a400000000000000000000000154fc7bfb0000015b000000660000000300000000000000000000002400000000root/opt/opensvc/lib/rcPasswdAIX.py   from subprocess import *
from rcUtilities import which

def change_root_pw(pw):
    if which('chpasswd') is not None:
        cmd = ['chpasswd']
        _input = "root:"+pw
    else:
        cmd = ['passwd', '-stdin', 'root']
        _input = pw
    p = Popen(cmd, stdin=PIPE)
    p.stdin.write(_input)
    p.communicate()
    return p.returncode
 0707010000bbe5000081a400000000000000000000000154fc7bfb00000161000000660000000300000000000000000000002600000000root/opt/opensvc/lib/rcPasswdLinux.py from subprocess import *
from rcUtilities import which

def change_root_pw(pw):
    if which('chpasswd') is not None:
        cmd = ['chpasswd']
        _input = "root:"+pw+"\n"
    else:
        cmd = ['passwd', '--stdin', 'root']
        _input = pw
    p = Popen(cmd, stdin=PIPE)
    p.stdin.write(_input)
    p.communicate()
    return p.returncode
   0707010000bbe6000081a400000000000000000000000154fc7bfb000007b2000000660000000300000000000000000000002600000000root/opt/opensvc/lib/rcPasswdSunOS.py from subprocess import *
from rcUtilities import which
import shutil
import crypt

shadow = '/etc/shadow'
tmpshadow = '/etc/shadow.tmp'
policy = '/etc/security/policy.conf'

def get_polices():
    """
       1	crypt_bsdmd5(5)
       2a	crypt_bsdbf(5)
       md5	crypt_sunmd5(5)
       5	crypt_sha256(5)
       6	crypt_sha512(5)
       __unix__	crypt_unix(5)	(default)
    """
    l = ["__unix__"]
    with open(policy, 'r') as fd:
        buff = fd.read()
        for line in buff.split('\n'):
            if line.strip().startswith('CRYPT_ALGORITHMS_ALLOW='):
                v = line.strip().split('=')
                if len(v) != 2:
                    continue
                l = v[1].split(',')
    return l

def change_root_pw(pw):
    try:
        _change_root_pw(pw)
    except Exception as e:
        print(e)
        return 1
    return 0

def _change_root_pw(pw):
    allowed = get_polices()
    if '1' not in allowed:
        raise Exception('can not generate a compatible password')
    shutil.copy(shadow, tmpshadow)
    with open(tmpshadow, 'r') as fd:
        buff = fd.read()
        lines = []
        for line in buff.split('\n'):
            if line.strip().startswith('#'):
                lines.append(line)
                continue
            v = line.split(':')
            if len(v) < 2 or v[0] != "root":
                lines.append(line)
                continue
            v[1] = pw_crypt(pw)
            lines.append(':'.join(v))
        buff = '\n'.join(lines)
    with open(tmpshadow, 'w') as fd:
        fd.write(buff+'\n')
    shutil.copy(tmpshadow, shadow)

def pw_crypt(pw):
    if not which('openssl'):
        raise Exception('openssl is mandatory')
    cmd = ['openssl', 'passwd', '-1', '-stdin']
    p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    out, err = p.communicate(input=pw)
    if p.returncode != 0:
        raise Exception()
    return out.strip('\n')

if __name__ == "__main__":
    change_root_pw('toto')
  0707010000bbe7000081a400000000000000000000000154fc7bfb000004f7000000660000000300000000000000000000002100000000root/opt/opensvc/lib/rcPkgAIX.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv

def listpkg():
    cmd = ['lslpp', '-Lc']
    out, err, ret = justcall(cmd)
    if ret != 0:
        return []
    lines = []
    for line in out.split('\n'):
        l = line.split(':')
        if len(l) < 5:
            continue
        pkgvers = l[2]
        pkgname = l[1].replace('-'+pkgvers, '')
        x = [rcEnv.nodename, pkgname, pkgvers, '']
        lines.append(x)
    return lines

def listpatch():
    return []

 0707010000bbe8000081a400000000000000000000000154fc7bfb000006da000000660000000300000000000000000000002400000000root/opt/opensvc/lib/rcPkgDarwin.py   #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
from rcUtilities import call, which
from rcGlobalEnv import rcEnv

"""
format:

package-id: com.apple.pkg.X11User
version: 10.6.0.1.1.1238328574
volume: /
location: /
install-time: 1285389505
groups: com.apple.snowleopard-repair-permissions.pkg-group com.apple.FindSystemFiles.pkg-group 
"""

def pkgversion(package):
    cmd = ['pkgutil', '--pkg-info', package]
    (ret, out, err) = call(cmd, errlog=False, cache=True)
    for line in out.split('\n'):
        l = line.split(': ')
        if len(l) != 2:
            continue
        if l[0] == 'version':
            return l[1]
    return ''

def listpkg():
    if which('pkgutil') is None:
        return []
    cmd = ['pkgutil', '--packages']
    (ret, out, err) = call(cmd, errlog=False, cache=True)
    lines = []
    for line in out.split('\n'):
        if len(line) == 0:
            continue
        x = [rcEnv.nodename, line, pkgversion(line), ""]
        lines.append(x)
    return lines

def listpatch():
    return []

  0707010000bbe9000081a400000000000000000000000154fc7bfb0000051c000000660000000300000000000000000000002500000000root/opt/opensvc/lib/rcPkgFreeBSD.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from rcUtilities import call, which
from rcGlobalEnv import rcEnv

def listpkg():
    if which('pkg_info') is None:
        return []
    cmd = ['pkg_info']
    (ret, out, err) = call(cmd, errlog=False, cache=True)
    lines = []
    for line in out.split('\n'):
        l = line.split()
        if len(l) < 2:
            continue
        nv = l[0].split('-')
        version = nv[-1]
        pkgname = '-'.join(nv[0:-1])
        x = [rcEnv.nodename, pkgname, version, '']
        lines.append(x)
    return lines

def listpatch():
    return [] 
0707010000bbea000081a400000000000000000000000154fc7bfb00000630000000660000000300000000000000000000002300000000root/opt/opensvc/lib/rcPkgHP-UX.py    #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from rcUtilities import call, which
from rcGlobalEnv import rcEnv
import datetime

def listpkg():
    if which('swlist') is None:
        return []
    lines = []
    for t in ('product', 'bundle'):
        lines += listpkg_t(t)
    return lines

def listpkg_t(t):
    cmd = ['swlist', '-l', t, '-a', 'revision', '-a', 'mod_time']
    (ret, out, err) = call(cmd, errlog=False, cache=True)
    lines = []
    for line in out.split('\n'):
        l = line.split()
        if len(l) < 3:
            continue
        if line[0] == '#':
            continue
        try:
            l[2] = datetime.datetime.fromtimestamp(int(l[2])).strftime("%Y-%m-%d %H:%M:%S")
        except:
            l[2] = ""
        x = [rcEnv.nodename, l[0], l[1], '', t, l[2]]
        lines.append(x)
    return lines

def listpatch():
    return []
0707010000bbeb000081a400000000000000000000000154fc7bfb0000098e000000660000000300000000000000000000002300000000root/opt/opensvc/lib/rcPkgLinux.py    #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
from rcUtilities import call, which
from rcGlobalEnv import rcEnv
import datetime
from stat import *

def listpkg_dummy():
    print("pushpkg supported on this system")
    return []

def listpkg_rpm():
    (ret, out, err) = call(cmd, errlog=False, cache=True)
    lines = []
    for line in out.split('\n'):
        if line.startswith('Signature'):
            sig = line.split()[-1].strip()
            continue
        elif not line.startswith('XX'):
            continue
        line = line[2:]
        l = line.split()
        if len(l) < 5:
            continue
        try:
            l[4] = datetime.datetime.fromtimestamp(int(l[4])).strftime("%Y-%m-%d %H:%M:%S")
        except:
            l[4] = ""
        x = [rcEnv.nodename] + l + [sig]
        lines.append(x)
    return lines

def listpkg_deb():
    (ret, out, err) = call(cmd, errlog=False, cache=True)
    lines = []
    arch = ""
    for line in out.split('\n'):
        l = line.split()
        if len(l) < 4:
            continue
        if l[0] != "ii":
            continue
        x = [rcEnv.nodename] + l[1:3] + [arch, "deb"]
        try:
            t = os.stat("/var/lib/dpkg/info/"+l[1]+".list")[ST_MTIME]
            t = datetime.datetime.fromtimestamp(t).strftime("%Y-%m-%d %H:%M:%S")
        except:
            t = ""
        x.append(t)
        lines.append(x)
    return lines

if which('dpkg') is not None:
    cmd = ['dpkg', '-l']
    listpkg = listpkg_deb
elif which('rpm') is not None:
    cmd = ['rpm', '-qai', '--queryformat=XX%{n} %{v}-%{r} %{arch} rpm %{installtime}\n']
    listpkg = listpkg_rpm
else:
    cmd = ['true']
    listpkg = listpkg_dummy

def listpatch():
    return [] 
  0707010000bbec000081a400000000000000000000000154fc7bfb00000819000000660000000300000000000000000000002200000000root/opt/opensvc/lib/rcPkgOSF1.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from rcUtilities import justcall
from rcGlobalEnv import rcEnv

"""
Subset               Status                 Description
------               ------                 -----------
IOSFRBASE540         installed              French Base System (French Support - Operating System)
IOSFRCDEHLP540       not installed          French CDE Online Help (French Support - Windowing Environment)
IOSFRCDEMIN540       installed              French CDE Minimum Runtime Environment(French Support - Windowing Environment)
IOSFRX11540          installed              French Basic X Environment (French Support - Windowing Environment)
"""

def _list():
    cmd = ['setld', '-i']
    out, err, ret = justcall(cmd)
    pkg = []
    patch = []
    pkgarch = ""
    pkgvers = ""
    if ret != 0:
        return []
    lines = out.split('\n')
    if len(lines) < 3:
        return []
    for line in lines[2:]:
        if "installed" not in line or "not installed" in line:
            continue
        name = line.split()[0]
        if "Patch:" in line:
            x = [rcEnv.nodename, name, pkgvers]
            patch.append(x)
        else:
            x = [rcEnv.nodename, name, pkgvers, pkgarch]
            pkg.append(x)
    return pkg, patch

def listpkg():
    return _list()[0]

def listpatch():
    return _list()[1]
   0707010000bbed000081a400000000000000000000000154fc7bfb00000dfa000000660000000300000000000000000000002300000000root/opt/opensvc/lib/rcPkgSunOS.py    #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import datetime
from rcUtilities import call, which
from rcGlobalEnv import rcEnv
from stat import *

"""
format:

   PKGINST:  SUNWzoneu
      NAME:  Solaris Zones (Usr)
  CATEGORY:  system
      ARCH:  i386
   VERSION:  11.11,REV=2009.04.08.17.26
    VENDOR:  Sun Microsystems, Inc.
      DESC:  Solaris Zones Configuration and Administration
   HOTLINE:  Please contact your local service provider
    STATUS:  completely installed

"""

def listpkg_ips():
    if which('uname') is None:
        return []
    cmd = ['uname', '-p']
    (ret, out, err) = call(cmd, errlog=False, cache=True)
    arc = out.split('\n')[0]
    if which('pkg') is None:
        return []
    cmd = ['pkg', 'list', '-H']
    (ret, out, err) = call(cmd, errlog=False, cache=True)
    lines = []
    for line in out.split('\n'):
        l = line.split()
        if len(l) != 3:
            continue
        x = [rcEnv.nodename, l[0], l[1], arc, "ips", ""]
        lines.append(x)
    return lines

def listpkg_legacy():
    if which('pkginfo') is None:
        return []
    cmd = ['pkginfo', '-l']
    (ret, out, err) = call(cmd, errlog=False, cache=True)
    lines = []
    for line in out.split('\n'):
        l = line.split(':')
        if len(l) != 2:
            continue
        l = map(lambda x: x.strip(), l)
        f = l[0]
        if f == "PKGINST":
            x = [rcEnv.nodename, l[1], "", "", "pkg", ""]
        elif f == "VERSION":
            x[2] = l[1]
        elif f == "ARCH":
            x[3] = l[1]
            lines.append(x)

    for i, x in enumerate(lines):
        # pkg install date
        try:
            t = os.stat("/var/sadm/pkg/"+x[1])[ST_MTIME]
            t = datetime.datetime.fromtimestamp(t).strftime("%Y-%m-%d %H:%M:%S")
        except Exception as e:
            t = ""
        lines[i][5] = t

    return lines

def listpkg():
    return listpkg_legacy() + listpkg_ips()

def listpatch():
    """
    Patch: patchnum-rev Obsoletes: num-rev[,patch-rev]... Requires: .... Incompatibles: ... Packages: ...
    """
    if which('showrev') is None:
        return []
    cmd = ['showrev', '-p']
    (ret, out, err) = call(cmd, errlog=False, cache=True)
    lines = []
    nodename = rcEnv.nodename
    for line in out.split('\n'):
        l = line.split(' ')
        if len(l) > 3:
            p = l[1].split('-')
            if len(p) != 2:
                continue
            else:
                lines.append( [ nodename , p[0], p[1] ] )

    for i, x in enumerate(lines):
        # pkg install date
        try:
            t = os.stat("/var/sadm/patch/"+x[1])[ST_MTIME]
            t = datetime.datetime.fromtimestamp(t).strftime("%Y-%m-%d %H:%M:%S")
        except:
            t = ""
        lines[i].append(t)

    return lines

if __name__ == "__main__" :
    print(listpatch())
  0707010000bbee000081a400000000000000000000000154fc7bfb000014c1000000660000000300000000000000000000002500000000root/opt/opensvc/lib/rcPkgWindows.py  #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from collections import namedtuple
from ctypes import byref, create_unicode_buffer, windll
from ctypes.wintypes import DWORD
from itertools import count
 
# defined at http://msdn.microsoft.com/en-us/library/aa370101(v=VS.85).aspx
UID_BUFFER_SIZE = 39
PROPERTY_BUFFER_SIZE = 256 
ERROR_MORE_DATA = 234
ERROR_INVALID_PARAMETER = 87
ERROR_SUCCESS = 0
ERROR_NO_MORE_ITEMS = 259 
ERROR_UNKNOWN_PRODUCT = 1605 
  
# diff propoerties of a product, not all products have all properties
PRODUCT_PROPERTIES = [u'Language',
                      u'ProductName',
                      u'PackageCode',
                      u'Transforms',
                      u'AssignmentType',
                      u'PackageName',
                      u'InstalledProductName',
                      u'VersionString',
                      u'RegCompany',
                      u'RegOwner',
                      u'ProductID',
                      u'ProductIcon',
                      u'InstallLocation',
                      u'InstallSource',
                      u'InstallDate',
                      u'Publisher',
                      u'LocalPackage',
                      u'HelpLink',
                      u'HelpTelephone',
                      u'URLInfoAbout',
                      u'URLUpdateInfo',] 

# class to be used for python users :)
Product = namedtuple('Product', PRODUCT_PROPERTIES)
 
def get_property_for_product(product, property, buf_size=PROPERTY_BUFFER_SIZE):
    """Retruns the value of a fiven property from a product."""
    property_buffer = create_unicode_buffer(buf_size)
    size = DWORD(buf_size)
    result = windll.msi.MsiGetProductInfoW(product, property, property_buffer,
                                           byref(size))
    if result == ERROR_MORE_DATA:
        return get_property_for_product(product, property,
                                        2 * buf_size)
    elif result == ERROR_SUCCESS:
        return property_buffer.value
    else:
        return None

def populate_product(uid):
    """Return a Product with the different present data."""
    properties = []
    for property in PRODUCT_PROPERTIES:
        properties.append(get_property_for_product(uid, property))
    return Product(*properties) 

def get_installed_products_uids():
    """Returns a list with all the different uid of the installed apps."""
    # enum will return an error code according to the result of the app
    products = []
    for i in count(0):
        uid_buffer = create_unicode_buffer(UID_BUFFER_SIZE)
        result = windll.msi.MsiEnumProductsW(i, uid_buffer)
        if result == ERROR_NO_MORE_ITEMS:
            # done interating over the collection
            break
        products.append(uid_buffer.value)
    return products
 
 
def get_installed_products():
    """Returns a collection of products that are installed in the system."""
    products = []
    for puid in  get_installed_products_uids():
        products.append(populate_product(puid))
    return products 
 
 
def is_product_installed_uid(uid):
    """Return if a product with the given id is installed.
 
    uid Most be a unicode object with the uid of the product using
    the following format {uid}
    """
    # we try to get the VersisonString for the uid, if we get an error it means
    # that the product is not installed in the system.
    buf_size = 256
    uid_buffer = create_unicode_buffer(uid)
    property = u'VersionString'
    property_buffer = create_unicode_buffer(buf_size)
    size = DWORD(buf_size)
    result = windll.msi.MsiGetProductInfoW(uid_buffer, property, property_buffer,
                                           byref(size))
    if result == ERROR_UNKNOWN_PRODUCT:
        return False
    else:
        return True

from rcGlobalEnv import rcEnv

def listpkg():
    plist = get_installed_products()
    lines = []
    for p in plist:
        x = [rcEnv.nodename,
             p.ProductName,
             p.VersionString,
             "",
	     "msi",
             p.InstallDate]
        lines.append(x)
    return lines


def listpatch():
    import wmi
    import datetime
    wmi = wmi.WMI()
    lines = []
    for hotfix in wmi.WIN32_QuickFixEngineering():
        try:
            t = hotfix.InstalledOn
            t = datetime.datetime.strptime(t, "%m/%d/%Y").strftime("%Y-%m-%d")
        except:
            t = ""
        lines.append([
          rcEnv.nodename,
          hotfix.HotFixID,
          "",
          t
        ])
    return lines

if __name__ == "__main__":
    print(get_installed_products())
   0707010000bbef000081a400000000000000000000000154fc7bfb00000a8f000000660000000300000000000000000000002500000000root/opt/opensvc/lib/rcPrintTable.py  from textwrap import wrap
import sys

if sys.version_info[0] >= 3:
    from functools import reduce

def parse_data(data):
    lines = data.splitlines()
    if len(lines) < 2:
        print("no data")
    labels = list(map(lambda x: x.split('.')[-1], lines[0].split(',')))
    lines = lines[1:]
    rows = []
    for line in lines:
        row = []
        incell = False
        cell_begin = 0
        l = len(line)
        for i, c in enumerate(line):
            if c != ',' and i < l-1:
                continue
            if incell and ((i>1 and line[i-1] == '"') or i == l-1):
                incell = False
            if not incell:
                if i > 0:
                    if i < l-1:
                        cell = line[cell_begin:i].replace('""', '"')
                    else:
                        cell = line[cell_begin:].replace('""', '"')
                else:
                    cell = ""
                if len(cell) > 1 and cell[0] == '"' and cell[-1] == '"':
                    if len(cell) > 2:
                        cell = cell[1:-1]
                    else:
                        cell = ""
                row.append(cell)
                cell_begin = i+1
                if i<l-1 and line[i+1] == '"':
                    incell = True
        rows.append(row)
    return [labels]+rows

def convert(s):
    try:
        return unicode(s)
    except:
        pass
    try:
        return unicode(s, errors="ignore")
    except:
        pass
    try:
        return str(s)
    except:
        pass
    return s

def print_table(data, width=20, table=False):
    if table:
        from tabulate import tabulate
        try:
            print(tabulate(data, headers="firstrow", tablefmt="simple"))
        except UnicodeEncodeError:
            print(tabulate(data, headers="firstrow", tablefmt="simple").encode("utf-8"))
        return
    if not isinstance(data, list):
        data = parse_data(data)
    if len(data) < 2:
        print("no data")
    labels = data[0]
    max_label_len = reduce(lambda x,y: max(x,len(y)), labels, 0)
    data = data[1:]
    subsequent_indent = ""
    for i in range(max_label_len+4):
        subsequent_indent += " "
    for j, d in enumerate(data):
        print("-")
        for i, label in enumerate(labels):
            val = '\n'.join(wrap(convert(d[i]),
                       initial_indent = "",
                       subsequent_indent = subsequent_indent,
                       width=78
                  ))
            try:
                print(" %s = %s" % (label.ljust(max_label_len), val))
            except UnicodeEncodeError:
                print(" %s = %s" % (label.ljust(max_label_len), val.encode("utf-8")))
 0707010000bbf0000081a400000000000000000000000154fc7bfb000065ce000000660000000300000000000000000000002400000000root/opt/opensvc/lib/rcScheduler.py   from __future__ import print_function
import sys
import os
import stat
import datetime
import json
import time

class SchedNotAllowed(Exception): 
    pass

class SchedNoDefault(Exception): 
    pass 
 
class SchedSyntaxError(Exception): 
    pass 
 
class SchedExcluded(Exception): 
    pass 
 
class SchedOpts(object): 
    def __init__(self, section, 
                 fname=None, 
                 schedule_option="push_schedule"): 
        self.section = section 
        self.fname = fname 
        if self.fname is None: 
            self.fname = "last_"+section+"_push" 
        self.schedule_option = schedule_option 

class Scheduler(object):
    def __init__(self):
        self.calendar_names = {
          "jan": 1,
          "feb": 2,
          "mar": 3,
          "apr": 4,
          "may": 5,
          "jun": 6,
          "jul": 7,
          "aug": 8,
          "sep": 9,
          "oct": 10,
          "nov": 11,
          "dec": 12,
          "january": 1,
          "february": 2,
          "march": 3,
          "april": 4,
          "june": 6,
          "july": 7,
          "august": 8,
          "september": 9,
          "october": 10,
          "november": 11,
          "december": 12,
          "mon": 0,
          "tue": 1,
          "wed": 2,
          "thu": 3,
          "fri": 4,
          "sat": 5,
          "sun": 6,
          "monday": 0,
          "tuesday": 1,
          "wednesday": 2,
          "thursday": 3,
          "friday": 4,
          "saturday": 5,
          "sunday": 6
        }

    def need_action_interval(self, timestamp_f, delay=10):
        """ Return False if timestamp is fresher than now-interval
            Return True otherwize.
            Zero is a infinite interval
        """
        if delay == 0:
            return False
        if not os.path.exists(timestamp_f):
            return True
        try:
            with open(timestamp_f, 'r') as f:
                d = f.read()
                last = datetime.datetime.strptime(d,"%Y-%m-%d %H:%M:%S.%f\n")
                limit = last + datetime.timedelta(minutes=delay)
                if datetime.datetime.now() < limit:
                    return False
                else:
                    return True
                f.close()
        except:
            return True

        # never reach here
        return True

    def timestamp(self, timestamp_f):
        timestamp_d = os.path.dirname(timestamp_f)
        if not os.path.isdir(timestamp_d):
            os.makedirs(timestamp_d, 0o755)
        with open(timestamp_f, 'w') as f:
            f.write(str(datetime.datetime.now())+'\n')
            f.close()
        return True

    def skip_action_interval(self, timestamp_f, interval):
        return not self.need_action_interval(timestamp_f, interval)

    def in_timerange_probabilistic(self, timerange, now=None):
        if not timerange.get("probabilistic", False):
            return

        try:
            begin = self.time_to_minutes(timerange["begin"])
            end = self.time_to_minutes(timerange["end"])
            now = self.time_to_minutes(now)
        except:
            raise SchedNotAllowed("time conversion error in probabilistic challenge")

        if begin > end:
            end += 1440
        if now < begin:
            now += 1440

        length = end - begin

        if length < 60:
            # no need to play this game on short allowed periods
            return

        if timerange["interval"] <= length:
            # don't skip if interval <= period length, because the user
            # expects the action to run multiple times in the period
            return

        length -= 11
        elapsed = now - begin
        elapsed_pct = min(100, int(100.0 * elapsed / length))

        """
            proba
              ^
        100%  |
         75%  |XXX
         50%  |XXXX
         25%  |XXXXXX
          0%  ----|----|-> elapsed
             0%  50%  100%

        This algo is meant to level collector's load which peaks
        when all daily cron trigger at the same minute.
        """
        if elapsed_pct < 50:
            # fixed skip proba for a perfect leveling on the first half-period
            p = 100.0 - max(1, 1000.0 / length)
        else:
            # decreasing skip proba on the second half-period
            p = 100.0 - min(elapsed_pct, 100)

        import random
        r = random.random()*100.0

        """
        print("begin:", begin)
        print("end:", end)
        print("now:", now)
        print("length:", length)
        print("elapsed:", elapsed)
        print("elapsed_pct:", elapsed_pct)
        print("p:", p)
        print("r:", r)
        """

        if r >= p:
            #print("win probabilistic challenge: %d, over %d"%(r, p))
            return

        raise SchedNotAllowed("lost probabilistic challenge: %d, over %d"%(r, p))

    def time_to_minutes(self, s):
        if type(s) == datetime.datetime:
            t = s
            s = t.hour * 60 + t.minute
        else:
            try:
                t = time.strptime(s, "%H:%M")
            except:
                print("malformed time string: %s"%str(s), file=sys.stderr)
                raise Exception("malformed time string: %s"%str(s))
            s = t.tm_hour * 60 + t.tm_min
        return s

    def in_timeranges(self, schedule, fname=None, now=None):
        if len(schedule["timeranges"]) == 0:
            raise SchedNotAllowed("no timeranges")
        l = []
        for tr in schedule["timeranges"]:
            try:
                self.in_timerange(tr, now=now)
                self.in_timerange_interval(tr, fname=fname, now=now)
                if fname is not None:
                    # fname as None indicates we run in test mode
                    self.in_timerange_probabilistic(tr, now=now)
                return
            except SchedNotAllowed as e:
                l.append(str(e))
        raise SchedNotAllowed(", ".join(l))

    def in_timerange(self, timerange, now=None):
        try:
            begin = self.time_to_minutes(timerange["begin"])
            end = self.time_to_minutes(timerange["end"])
            now = self.time_to_minutes(now)
        except:
            raise SchedNotAllowed("conversion error in timerange challenge")

        if begin <= end:
            if now >= begin and now <= end:
                return
        elif begin > end:
            """
                  XXXXXXXXXXXXXXXXX
                  23h     0h      1h
            """
            if (now >= begin and now <= 1440) or \
               (now >= 0 and now <= end):
                return
        raise SchedNotAllowed("not in timerange %s-%s"%(timerange["begin"],timerange["end"]))

    def in_timerange_interval(self, timerange, fname=None, now=None):
        if timerange["interval"] == 0:
            raise SchedNotAllowed("interval set to 0")
        if fname is None:
            # test mode
            return
        timestamp_f = self.get_timestamp_f(fname)
        if self.skip_action_interval(timestamp_f, timerange["interval"]):
            raise SchedNotAllowed("last run is too soon")
        return

    def _in_schedule(self, schedule, fname=None, now=None):
        self.in_days(schedule, now=now)
        self.in_timeranges(schedule, fname=fname, now=now)

    def in_schedule(self, schedules, fname=None, now=None):
        if len(schedules) == 0:
            raise SchedNotAllowed("no schedule")
        l = []
        for schedule in schedules:
            try:
                self._in_schedule(schedule, fname=fname, now=now)
                if schedule["exclude"]:
                    raise SchedExcluded('excluded by schedule member "%s"' % schedule["raw"])
                else:
                    return
            except SchedNotAllowed as e:
                l.append(str(e))
        raise SchedNotAllowed(", ".join(l))

    def sched_convert_to_schedule(self, config, section, prefix=""):
        days_s = config.get(section, prefix+'days')
        period_s = config.get(section, prefix+'period')
        interval_s = config.get(section, prefix+'interval')
        try:
            days = json.loads(days_s)
        except:
            return ""
        try:
            periods = json.loads(period_s)
            l = []
            if type(periods[0]) in (str, unicode):
                periods = [periods]
            for p in periods:
                l.append("%s-%s@%s" % (p[0], p[1], interval_s))
            period_s = ",".join(l)
        except:
            pass
        s = "%(period)s %(days)s" % dict(
              period=period_s,
              days=",".join(days),
            )
        return s.strip()

    def sched_get_schedule_raw(self, section, option):
        if option is None:
            raise SchedNoDefault

        if hasattr(self, "config"):
            config = self.config
        elif hasattr(self, "svc"):
            config =  self.svc.config

        if config.has_section(section) and \
           config.has_option(section, 'schedule'):
            schedule_s = config.get(section, 'schedule')
        elif config.has_section(section) and \
             config.has_option(section, 'days') and \
             config.has_option(section, 'interval') and \
             config.has_option(section, 'period'):
            schedule_s = self.sched_convert_to_schedule(config, section)
        elif config.has_option('DEFAULT', option):
            schedule_s = config.get('DEFAULT', option)
        else:
            raise SchedNoDefault

        return schedule_s

    def in_days(self, schedule, now=None):
        self._sched_validate_month(schedule["month"], now=now)
        self._sched_validate_week(schedule["week"], now=now)
        self._sched_validate_day(schedule["day"], now=now)

    def _sched_validate_day(self, day, now=None):
        for s in day.split(","):
            try:
                self.__sched_validate_day(s, now=now)
                return
            except SchedNotAllowed:
                pass
        raise SchedNotAllowed("not in allowed days")

    def __sched_validate_day(self, day, now=None):
        n_col = day.count(":")
        day_of_month = None
        from_tail = None
        from_head = None
        if n_col > 1:
            raise SchedSyntaxError
        elif n_col == 1:
            day, day_of_month = day.split(":")
            if len(day_of_month) == 0:
                raise SchedSyntaxError
            if day_of_month in ("first", "1st"):
                from_head = True
                day_of_month = 1
            elif day_of_month in ("second", "2nd"):
                from_head = True
                day_of_month = 2
            elif day_of_month in ("third", "3rd"):
                from_head = True
                day_of_month = 3
            elif day_of_month in ("fourth", "4th"):
                from_head = True
                day_of_month = 4
            elif day_of_month in ("fifth", "5th"):
                from_head = True
                day_of_month = 5
            elif day_of_month == "last":
                from_tail = True
                day_of_month = 1
            elif day_of_month[0] == "-":
                from_tail = True
                day_of_month = day_of_month[1:]
            elif day_of_month[0] == "+":
                from_head = True
                day_of_month = day_of_month[1:]
            try:
                day_of_month = int(day_of_month)
            except ValueError:
                raise SchedSyntaxError

        day = self.sched_expand_value(day)

        if day in ("*", ""):
            allowed_days = range(7)
        else:
            allowed_days = [ d for d in day if d >= 0 and d <= 6 ]
        if now is None:
            now = datetime.datetime.now()
        this_week_day = now.weekday()

        if this_week_day not in allowed_days:
            raise SchedNotAllowed

        if day_of_month is not None:
            _day = now
            _month = _day.month
            if from_head is True:
                if day == "":
                    d1 = _day - datetime.timedelta(days=day_of_month)
                    d2 = _day - datetime.timedelta(days=day_of_month-1)
                else:
                    d1 = _day - datetime.timedelta(days=7*day_of_month)
                    d2 = _day - datetime.timedelta(days=7*(day_of_month-1))
                if d1.month == _month or d2.month != _month:
                    raise SchedNotAllowed
            elif from_tail is True:
                if day == "":
                    d1 = _day + datetime.timedelta(days=day_of_month)
                    d2 = _day + datetime.timedelta(days=day_of_month-1)
                else:
                    d1 = _day + datetime.timedelta(days=7*day_of_month)
                    d2 = _day + datetime.timedelta(days=7*(day_of_month-1))
                if d1.month == _month or d2.month != _month:
                    raise SchedNotAllowed
            elif _day.day != day_of_month:
                raise SchedNotAllowed

        return

    def _sched_validate_week(self, week, now=None):
        week = self.sched_expand_value(week)

        if week == "*":
            return

        allowed_weeks = [ w for w in week if w >= 1 and w <= 53 ]
        if now is None:
            now = datetime.datetime.now()
        this_week_day = now.weekday()
        if now.isocalendar()[1] not in allowed_weeks:
            raise SchedNotAllowed("not in allowed weeks")
        return

    def _sched_validate_month(self, month, now=None):
        if month == "*":
            return

        allowed_months = set([])
        for s in month.split(","):
            n = s.count("%")
            if n == 1:
                month_s, modulo_s = s.split("%")
            elif n == 0:
                month_s = s
                modulo_s = None
            else:
                raise SchedSyntaxError("malformed month definition")

            
            if month_s in ("", "*"):
                _allowed_months = set(range(12))
            else:
                _allowed_months = self.sched_expand_value(month_s)
            if modulo_s is not None:
                _allowed_months &= self.__sched_validate_month(modulo_s)
            allowed_months |= _allowed_months

        if now is None:
            now = datetime.datetime.now()
        this_week_day = now.weekday()
        if now.month not in allowed_months:
            raise SchedNotAllowed("not in allowed months")
        return

    def __sched_validate_month(self, modulo):
        shift = 0
        n_plus = modulo.count("+")
        if n_plus > 1:
            raise SchedSyntaxError
        if n_plus == 1:
            modulo, shift = modulo.split("+")
        try:
            modulo = int(modulo)
            shift = int(shift)
        except ValueError:
            raise SchedSyntaxError
        return set([ m for m in range(1,13) if (m + shift) % modulo == 0])

    def sched_to_int(self, s):
        try:
            i = int(s)
            return i
        except ValueError:
            s = s.lower()
            if s not in self.calendar_names:
                 raise SchedSyntaxError("unknown calendar name")
            return self.calendar_names[s]
            
    def sched_expand_value(self, s):
        v = set([])
        if s in ("*", ""):
            return s
        l = s.split(",")
        for e in l:
            n_dash = e.count("-")
            if n_dash > 1:
                raise SchedSyntaxError
            elif n_dash == 0:
                v.add(self.sched_to_int(e))
                continue
            begin, end = e.split("-")
            begin = self.sched_to_int(begin)
            end = self.sched_to_int(end)
            _range = sorted([begin, end])
            v |= set(range(_range[0], _range[1]+1))
        return v

    def interval_from_timerange(self, tr):
        begin_m = self.time_to_minutes(tr['begin'])
        end_m = self.time_to_minutes(tr['end'])
        return end_m - begin_m + 1

    def sched_parse_timerange(self, s, section=None):
        def parse_timerange(s):
            if s == "*" or s == "":
                return {"begin": "00:00", "end": "23:59"}
            if "-" not in s:
                s = "-".join((s,s))
            try:
                begin, end = s.split("-")
            except:
                raise SchedSyntaxError
            if begin.count(":") != 1 or \
               end.count(":") != 1:
                raise SchedSyntaxError
            begin_m = self.time_to_minutes(begin)
            end_m = self.time_to_minutes(end)
            if begin_m > end_m:
                tmp = end
                end = begin
                begin = tmp
            elif begin_m == end_m:
                end_m += 10
                end = "%02d:%02d" % (end_m // 60, end_m % 60)
            return {"begin": begin, "end": end}

        if section and "#sync#" in section:
            probabilistic = False
        else:
            probabilistic = True

        tr = []
        for e in s.split(","):
            if len(e) == 0 or e == "*":
                d = {
                  "probabilistic": probabilistic,
                  "begin": "00:00",
                  "end": "23:59",
                  "interval": 1441
                }
                tr.append(d)
                continue
            n = e.count("@")
            if n == 0:
                d = parse_timerange(e)
                d["interval"] = self.interval_from_timerange(d)
                d["probabilistic"] = probabilistic
                tr.append(d)
                continue
            
            l = e.split("@")
            n = len(l)
            if n != 2:
                raise SchedSyntaxError
            d = parse_timerange(l[0])
            d["probabilistic"] = probabilistic
            try:
                d["interval"] = int(l[1])
            except:
                raise SchedSyntaxError
            tr.append(d)
        return tr

    def sched_get_schedule(self, section, option, now=None, schedules=None):
        if schedules is None:
            schedules = self.sched_get_schedule_raw(section, option)
        try:
            schedules = json.loads(schedules)
        except:
            pass
        if type(schedules) in (str, unicode):
            schedules = [schedules]

        data = []
        for schedule in schedules:
            schedule_orig = schedule
            schedule = schedule.strip()
            if len(schedule) == 0:
                continue
            if schedule.startswith("!"):
                exclude = True
                schedule = schedule[1:].strip()
            else:
                exclude = False
            if len(schedule) == 0:
                continue
            l = schedule.split()
            n = len(l)
            if n == 1:
                d = {
                 "timeranges": self.sched_parse_timerange(l[0], section=section),
                 "day": "*",
                 "week": "*",
                 "month": "*",
                }
            elif n == 2:
                _tr, _day = l
                d = {
                 "timeranges": self.sched_parse_timerange(_tr, section=section),
                 "day": _day,
                 "week": "*",
                 "month": "*",
                }
            elif n == 3:
                _tr, _day, _week = l
                d = {
                 "timeranges": self.sched_parse_timerange(_tr, section=section),
                 "day": _day,
                 "week": _week,
                 "month": "*",
                }
            elif n == 4:
                _tr, _day, _week, _month = l
                d = {
                 "timeranges": self.sched_parse_timerange(_tr, section=section),
                 "day": _day,
                 "week": _week,
                 "month": _month,
                }
            else:
                raise SchedSyntaxError
            d["exclude"] = exclude
            d["raw"] = schedule_orig
            data.append(d)
        return data

    def allow_action_schedule(self, section, option, fname=None, now=None):
        if option is None:
            return
        if now is None:
            now = datetime.datetime.now()
        try:
            schedule = self.sched_get_schedule(section, option)
            self.in_schedule(schedule, fname=fname, now=now)
        except SchedNoDefault:
            raise SchedNotAllowed("no schedule in section %s and no default schedule"%section)
        except SchedSyntaxError:
            raise SchedNotAllowed("malformed parameter value: %s.schedule"%section)

    def skip_action_schedule(self, section, option, fname=None, now=None):
        try:
            self.allow_action_schedule(section, option, fname=fname, now=now)
            return False
        except SchedExcluded:
            return True
        except:
            return True

    def get_timestamp_f(self, fname):
        timestamp_f = os.path.join(os.path.dirname(__file__), '..', 'var', fname)
        return timestamp_f

    def skip_action(self, action, section=None, fname=None, schedule_option=None, cmdline_parm=None, now=None, verbose=True):
        if section is None:
            section = self.scheduler_actions[action].section
        if fname is None:
            fname = self.scheduler_actions[action].fname
        if schedule_option is None:
            schedule_option = self.scheduler_actions[action].schedule_option

        def err(msg):
            if not verbose:
                return
            print('%-22s skip  '%section, msg)

        if not self.options.cron:
            # don't update the timestamp file
            return False

        # check if we are in allowed scheduling period
        try:
            self.allow_action_schedule(section, schedule_option, fname=fname, now=now)
        except Exception as e:
            err(str(e))
            return True

        # update the timestamp file
        timestamp_f = self.get_timestamp_f(fname)
        self.timestamp(timestamp_f)

        return False

    def print_schedule(self):
        print("%-20s  %-15s  %-21s  %s" % ("action", "section", "last", "schedule definition"))
        print("%-20s  %-15s  %-21s  %s" % ("------", "-------", "----", "-------------------"))
        for a in sorted(self.scheduler_actions):
            self._print_schedule(a)

    def _print_schedule(self, a):
        section = self.scheduler_actions[a].section
        schedule_option = self.scheduler_actions[a].schedule_option
        fname = self.scheduler_actions[a].fname

        try:
            schedule_s = self.sched_get_schedule_raw(section, schedule_option)
        except SchedNoDefault:
            schedule_s = "anytime"
        except SchedSyntaxError:
            schedule_s = "malformed"
        if len(schedule_s) == 0:
            schedule_s = "-"

        timestamp_f = self.get_timestamp_f(fname)
        try:
            with open(timestamp_f, 'r') as f:
                last_s = f.read()
                last_s = last_s.split('.')[0]
        except:
            last_s = "-"


        print("%-20s  %-15s  %-21s  %s" % (a, section, last_s, schedule_s))

    def str_to_datetime(self, s):
        d = datetime.datetime.strptime(s, "%Y-%m-%d %H:%M")
        return d

    def test_schedule(self, schedule_s, date_s, expected):
        d = self.str_to_datetime(date_s)
        
        try:
            schedule = self.sched_get_schedule("dummy", "dummy", schedules=schedule_s)
        except SchedSyntaxError:
            print("failed : schedule syntax error %s" % repr(schedule_s))
            return
        try:
            self.in_schedule(schedule, fname=None, now=d)
            result = True
            result_s = ""
        except SchedSyntaxError:
            print("failed : schedule syntax error %s" % repr(schedule_s))
            return
        except SchedNotAllowed as e:
            result = False
            result_s = "("+str(e)+")"
        if result == expected:
            check = "passed"
        else:
            check = "failed"
        print("%s : test '%s' in schedule %-50s expected %s => result %s %s" % (check, date_s, repr(schedule_s), str(expected), str(result), result_s))

if __name__ == "__main__":
    tests = [
     ("", "2015-02-27 10:00", False),
     ("@0", "2015-02-27 10:00", False),
     ("*@0", "2015-02-27 10:00", False),
     ("*", "2015-02-27 10:00", True),
     ("*@61", "2015-02-27 10:00", True),
     ("09:00-09:20", "2015-02-27 10:00", False),
     ("09:00-09:20@31", "2015-02-27 10:00", False),
     ("09:00-09:00", "2015-02-27 10:00", False),
     ("09:20-09:00", "2015-02-27 10:00", False),
     ("09:00", "2015-02-27 10:00", False),
     ("09:00-09:20", "2015-02-27 09:09", True),
     ("09:00-09:20@31", "2015-02-27 09:09", True),
     ("09:00-09:00", "2015-02-27 09:09", True),
     ("09:20-09:00", "2015-02-27 09:09", True),
     ("09:00", "2015-02-27 09:09", True),
     ("* *:last", "2015-01-30 10:00", True),
     ("* *:last", "2015-01-31 10:00", True),
     ("* *:-1", "2015-01-31 10:00", True),
     ("* *:-1", "2015-01-24 10:00", False),
     ("* *:-2", "2015-01-31 10:00", False),
     ("* :last", "2015-01-30 10:00", False),
     ("* :last", "2015-01-31 10:00", True),
     ("* :-1", "2015-01-31 10:00", True),
     ("* :-2", "2015-01-30 10:00", True),
     ("* :-2", "2015-01-31 10:00", False),
     ("* :-2", "2015-01-05 10:00", False),
     ("* :5", "2015-01-05 10:00", True),
     ("* :+5", "2015-01-05 10:00", True),
     ("* :fifth", "2015-01-05 10:00", True),
     ("* :5", "2015-01-06 10:00", False),
     ("* :+5", "2015-01-06 10:00", False),
     ("* :fifth", "2015-01-06 10:00", False),
     ("* * * jan", "2015-01-06 10:00", True),
     ("* * * jan-feb", "2015-01-06 10:00", True),
     ("* * * %2", "2015-01-06 10:00", False),
     ("* * * %2+1", "2015-01-06 10:00", True),
     ("* * * jan-feb%2", "2015-01-06 10:00", False),
     ("* * * jan-feb%2+1", "2015-01-06 10:00", True),
    ]
    sched = Scheduler()
    for test in tests:
        sched.test_schedule(*test)
        
  0707010000bbf1000081a400000000000000000000000154fc7bfb00000f89000000660000000300000000000000000000002000000000root/opt/opensvc/lib/rcStats.py   #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os
import datetime

class StatsProvider(object):
    def __init__(self, interval=2880, stats_dir=None, stats_start=None, stats_end=None):
        self.stats_dir = stats_dir
        self.interval = interval

        if stats_end is None:
            self.stats_end = datetime.datetime.now()
        else:
            self.stats_end = datetime.datetime.strptime(stats_end,"%Y-%m-%d %H:%M")

        if stats_start is None:
            self.stats_start = self.stats_end - datetime.timedelta(minutes=interval)
        else:
            self.stats_start = datetime.datetime.strptime(stats_start,"%Y-%m-%d %H:%M")
            delta = self.stats_end - self.stats_start
            interval = delta.days * 1440 + delta.seconds // 60

        x, self.nodename, x, x, x = os.uname()

        self.minutes_first_day = 60*self.stats_end.hour + self.stats_end.minute + 1

        one_minute = datetime.timedelta(minutes=1)
        one_day = datetime.timedelta(days=1)
        self.ranges = []
        i = 0
        end = self.stats_end

        while end > self.stats_start:
            start = end - one_day
            if start < self.stats_start:
                start = self.stats_start
            if start.day != end.day:
                start = end - datetime.timedelta(hours=end.hour, minutes=end.minute)
            if start != end:
                self.ranges.append((start, end))
            end = start - one_minute
        #print(self.stats_end, interval, map(lambda x: map(lambda y: y.strftime("%d-%m-%y %H:%M"), x), self.ranges))

    def get(self, fname):
        lines = []
        cols = []
        if not hasattr(self, fname):
            print(fname, 'is not implemented')
            return cols, lines
        for start, end in self.ranges:
            date = start.strftime("%Y-%m-%d")
            day = start.strftime("%d")
            start = start.strftime("%H:%M:%S")
            end = end.strftime("%H:%M:%S")
            _cols, _lines = getattr(self, fname)(date, day, start, end)
            if len(_cols) == 0 or len(_lines) == 0:
                continue
            cols = _cols
            lines += _lines
        return cols, lines

    def sarfile(self, day):
        if self.stats_dir is None:
            stats_dir = os.path.join(os.sep, 'var', 'log', 'sysstat')
            if not os.path.exists(stats_dir):
                stats_dir = os.path.join(os.sep, 'var', 'log', 'sa')
        else:
            stats_dir = self.stats_dir
        f = os.path.join(stats_dir, 'sa'+day)
        if os.path.exists(f):
            return f
        return None

    def cpu(self, d, day, start, end):
        return [], []

    def mem_u(self, d, day, start, end):
        return [], []

    def proc(self, d, day, start, end):
        return [], []

    def swap(self, d, day, start, end):
        return [], []

    def block(self, d, day, start, end):
        return [], []

    def blockdev(self, d, day, start, end):
        return [], []

    def netdev(self, d, day, start, end):
        return [], []

    def netdev_err(self, d, day, start, end):
        return [], []

if __name__ == "__main__":
    sp = StatsProvider(interval=20)
    print(sp.get('cpu'))
    print(sp.get('swap'))
   0707010000bbf2000081a400000000000000000000000154fc7bfb000012f3000000660000000300000000000000000000002300000000root/opt/opensvc/lib/rcStatsAIX.py    #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os
import datetime
from rcUtilities import call, which
from rcGlobalEnv import rcEnv

today = datetime.datetime.today()
yesterday = today - datetime.timedelta(days=1)

def sarfile(day):
    f = os.path.join(os.sep, 'var', 'adm', 'sa', 'sa'+day)
    if os.path.exists(f):
        return f
    return None

def twodays(fn):
    if which('sar') is None:
        return []
    lines = fn(yesterday)
    lines += fn(today)
    return lines

def stats_cpu():
    return twodays(stats_cpu_day)

def stats_cpu_day(t):
    d = t.strftime("%Y-%m-%d")
    day = t.strftime("%d")
    f = sarfile(day)
    if f is None:
        return []
    cmd = ['sar', '-u', '-P', 'ALL', '-f', f]
    (ret, buff, err) = call(cmd, errlog=False)
    lines = []
    for line in buff.split('\n'):
        l = line.split()
        if len(l) != 6:
            continue
        if l[1] == '%usr':
            continue
        if l[0] == 'Average':
            continue
        # SunOS:  date    %usr     %sys %wio                       %idle
        # xmlrpc: date cpu usr nice sys iowait steal irq soft guest idle nodename
        x = ['%s %s'%(d, l[0]), 'all', '0', '0', '0', '0', '0', '0', '0', '0', '0', rcEnv.nodename]
        x[1] = l[1].replace('-', 'all')
        x[2] = l[2]
        x[4] = l[3]
        x[5] = l[4]
        x[10] = l[5]
        lines.append(x)
    return lines

def stats_mem_u(file, collect_date=None):
    return twodays(stats_mem_u_day)

def stats_mem_u_day(t):
    return []

def stats_proc(file, collect_date=None):
    return twodays(stats_proc_day)

def stats_proc_day(t):
    d = t.strftime("%Y-%m-%d")
    day = t.strftime("%d")
    f = sarfile(day)
    if f is None:
        return []
    cmd = ['sar', '-q', '-f', f]
    (ret, buff, err) = call(cmd)
    lines = []
    for line in buff.split('\n'):
        l = line.split()
        if len(l) < 3:
            continue
        if ':' not in l[0]:
            continue
        """ xmlrpc: date runq_sz plist_sz ldavg_1 ldavg_5 ldavg_15 nodename
        """
        x = ['%s %s'%(d, l[0]), l[1], '0', '0', '0', '0', rcEnv.nodename]
        lines.append(x)
    return lines

def stats_swap(file, collect_date=None):
    return twodays(stats_swap_day)

def stats_swap_day(t):
    return []

def stats_block(file, collect_date=None):
    return twodays(stats_block_day)

def stats_block_day(t):
    d = t.strftime("%Y-%m-%d")
    day = t.strftime("%d")
    f = sarfile(day)
    if f is None:
        return []
    cmd = ['sar', '-b', '-f', f]
    (ret, buff, err) = call(cmd)
    lines = []
    for line in buff.split('\n'):
        l = line.split()
        if len(l) != 9:
            continue
        if ':' not in l[1]:
            continue

        """ xmlrpc: date tps rtps wtps rbps wbps nodename
        """
        x = ['%s %s'%(d, l[0]), '0', '0', '0', l[1], l[4], rcEnv.nodename]

        lines.append(x)
    return lines

def stats_blockdev(file, collect_date=None):
    return twodays(stats_blockdev_day)

def stats_blockdev_day(t):
    d = t.strftime("%Y-%m-%d")
    day = t.strftime("%d")
    f = sarfile(day)
    if f is None:
        return []
    cmd = ['sar', '-d', '-f', f]
    (ret, buff, err) = call(cmd, errlog=False)
    lines = []
    for line in buff.split('\n'):
        l = line.split()
        if len(l) == 8:
            date = l[0]
        if len(l) == 7:
            l = [date] + l
        if len(l) != 8:
            continue
        if l[1] == 'device':
            continue
        if l[0] == 'Average':
            continue
        """ xmlrpc: 22:05:01 DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
                    00:00:00 device %busy avque r+w/s blks/s avwait avserv
        """
        x = ['%s %s'%(d, l[0]), l[1], l[4], '0', '0', '0', l[3], l[6], l[7], l[2], rcEnv.nodename]
        lines.append(x)
    return lines

def stats_netdev(file, collect_date=None):
    return twodays(stats_netdev_day)

def stats_netdev_day(t):
    return []

def stats_netdev_err(file, collect_date=None):
    return twodays(stats_netdev_err_day)

def stats_netdev_err_day(t):
    return []

 0707010000bbf3000081a400000000000000000000000154fc7bfb00001557000000660000000300000000000000000000002600000000root/opt/opensvc/lib/rcStatsDarwin.py #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os
from rcUtilities import call, which
from rcGlobalEnv import rcEnv
import rcStats

class StatsProvider(rcStats.StatsProvider):
    def customfile(self, metric, day):
        f = os.path.join(rcEnv.pathvar, 'stats', metric+day)
        if os.path.exists(f):
            return f
        return None

    def cpu(self, d, day, start, end):
        cols = ['date',
                'cpu',
                'usr',
                'nice',
                'sys',
                'idle',
                'nodename']
        f = self.sarfile(day)
        lines = []
        if f is None:
            return cols, lines
        cmd = ['sar', '-u', '-f', f, '-s', start, '-e', end]
        (ret, buff, err) = call(cmd, errlog=False)
        for line in buff.split('\n'):
            l = line.split()
            if len(l) != 5:
                continue
            if l[1] == '%usr':
                continue
            if l[0] == 'Average:':
                continue
            (time, usr, nice, sys, idle) = l
            l = ['%s %s'%(d, time), 'all', usr, nice, sys, idle, self.nodename]
            lines.append(l)
        return cols, lines

    def mem_u(self, d, day, start, end):
        cols = ['date',
                'kbmemfree',
                'kbmemused',
                'kbbuffers',
                'kbcached',
                'kbmemsys',
                'nodename']
        fname = self.customfile('mem_u', day)
        lines = []
        if fname is None:
            return cols, lines
        try:
            f = open(fname, 'r')
            buff = f.read()
            f.close()
        except:
            return cols, lines
        for line in buff.split('\n'):
            l = line.split()
            if len(l) != 6:
                continue
            (time, free, inactive, active, speculative, wired) = l
            l = ['%s %s'%(d, time), free, active, speculative, inactive, wired, self.nodename]
            lines.append(l)
        return cols, lines

    def blockdev(self, d, day, start, end):
        cols = ['date',
                'dev',
                'tps',
                'rsecps',
                'nodename']
        f = self.sarfile(day)
        lines = []
        if f is None:
            return cols, lines
        cmd = ['sar', '-d', '-f', f, '-s', start, '-e', end]
        (ret, buff, err) = call(cmd, errlog=False)
        for line in buff.split('\n'):
           l = line.split()
           if len(l) != 4:
               continue
           if l[1] == 'device':
               continue
           if l[1] == 'Disk:':
               continue
           if l[0] == 'Average:':
               continue
           l.append(self.nodename)
           l[0] = '%s %s'%(d, l[0])
           lines.append(l)
        return cols, lines

    def netdev(self, d, day, start, end):
        cols = ['date',
                'dev',
                'rxpckps',
                'rxkBps',
                'txpckps',
                'txkBps',
                'nodename']

        f = self.sarfile(day)
        lines = []
        if f is None:
            return cols, lines
        cmd = ['sar', '-n', 'DEV', '-f', f, '-s', start, '-e', end]
        (ret, buff, err) = call(cmd, errlog=False)
        for line in buff.split('\n'):
           l = line.split()
           if len(l) != 6:
               continue
           if l[1] in ['IFACE', 'lo0'] :
               continue
           if 'dummy' in l[1] or 'vnet' in l[1] or 'veth' in l[1] or \
              'gif'   in l[1] or 'stf'  in l[1]:
               continue
           if l[0] == 'Average:':
               continue
           l.append(self.nodename)
           l[0] = '%s %s'%(d, l[0])
           lines.append(l)
        return cols, lines


    def netdev_err(self, d, day, start, end):
        cols = ['date',
                'dev',
                'rxerrps',
                'txerrps',
                'collps',
                'rxdropps',
                'nodename']
        f = self.sarfile(day)
        lines = []
        if f is None:
            return cols, lines
        cmd = ['sar', '-n', 'EDEV', '-f', f, '-s', start, '-e', end]
        (ret, buff, err) = call(cmd, errlog=False)
        for line in buff.split('\n'):
           l = line.split()
           if len(l) != 6:
               continue
           if l[1] in ['IFACE', 'lo0'] :
               continue
           if 'dummy' in l[1] or 'vnet' in l[1] or 'veth' in l[1] or \
              'gif'   in l[1] or 'stf'  in l[1]:
               continue
           if l[0] == 'Average:':
               continue
           l.append(self.nodename)
           l[0] = '%s %s'%(d, l[0])
           lines.append(l)
        return cols, lines


 0707010000bbf4000081a400000000000000000000000154fc7bfb00001494000000660000000300000000000000000000002700000000root/opt/opensvc/lib/rcStatsFreeBSD.py    #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os
from rcUtilities import call, which
import rcStats

class StatsProvider(rcStats.StatsProvider):
    def cpu(self, d, day, start, end):
        cols = ['date',
                'usr',
                'sys',
                'nice',
                'irq',
                'idle',
                'cpu',
                'nodename']
        cmd = ['bsdsar', '-u', '-n', day]
        (ret, buff, err) = call(cmd, errlog=False)
        lines = []
        if ret != 0:
            return cols, lines
        for line in buff.split('\n'):
            l = line.split()
            if len(l) != 6:
                continue
            if l[0] == 'Time':
                continue
            l += ['ALL', self.nodename]
            l[0] = '%s %s'%(d, l[0])
            lines.append(l)
        return cols, lines

    def kb(self, s):
        n = int(s[0:-1])
        unit = s[-1]
        if unit == 'k' or unit =='K':
            return n
        elif unit == 'M':
            return n*1024
        elif unit == 'G':
            return n*1024*1024
        elif unit == 'T':
            return n*1024*1024*1204
        elif unit == 'P':
            return n*1024*1024*1204*1024

    def mem_u(self, d, day, start, end):
        cols = ['date',
                'kbmemfree',
                'kbmemused',
                'pct_memused',
                'kbmemsys',
                'nodename']

        cmd = ['sysctl', 'hw.physmem']
        (ret, out, err) = call(cmd)
        physmem = int(out.split(': ')[1])/1024

        cmd = ['sysctl', 'hw.usermem']
        (ret, out, err) = call(cmd)
        usermem = int(out.split(': ')[1])/1024

        cmd = ['bsdsar', '-r', '-n', day]
        (ret, buff, err) = call(cmd)
        lines = []
        for line in buff.split('\n'):
            l = line.split()
            if len(l) != 7:
                continue
            if l[0] == 'Time':
                continue
            free = self.kb(l[1])
            used = self.kb(l[2])+self.kb(l[3])
            x = [l[0], str(free), str(used), str(used/(used+free)), str(physmem-usermem), self.nodename]
            x[0] = '%s %s'%(d, x[0])
            lines.append(x)
        return cols, lines

    def swap(self, d, day, start, end):
        cols = ['date',
                'kbswpfree',
                'kbswpused',
                'pct_swpused',
                'kbswpcad',
                'pct_swpcad',
                'nodename']
        cmd = ['bsdsar', '-r', '-n', day]
        (ret, buff, err) = call(cmd, errlog=False)
        lines = []
        if ret != 0:
            return cols, lines
        for line in buff.split('\n'):
           l = line.split()
           if len(l) != 7:
               continue
           if l[0] == 'Time':
               continue
           free = self.kb(l[6])
           used = self.kb(l[5])
           x = [l[0], str(free), str(used), str(used/(free+used)), '0', '0']
           x.append(self.nodename)
           x[0] = '%s %s'%(d, x[0])
           lines.append(x)
        return cols, lines

    def netdev(self, d, day, start, end):
        cols = ['date',
                'rxpckps',
                'rxkBps',
                'txpckps',
                'txkBps',
                'dev',
                'nodename']
        cmd = ['bsdsar', '-I', '-n', day]
        (ret, buff, err) = call(cmd, errlog=False)
        lines = []
        if ret != 0:
            return cols, lines
        for line in buff.split('\n'):
           l = line.split()
           if len(l) != 9:
               continue
           if l[0] == 'Time':
               continue
           x = [l[0], l[1], l[3], l[4], l[6], l[8], self.nodename]
           x[0] = '%s %s'%(d, x[0])
           lines.append(x)
        return cols, lines

    def netdev_err(self, d, day, start, end):
        cols = ['date',
                'rxerrps',
                'txerrps',
                'collps',
                'dev',
                'nodename']
        cmd = ['bsdsar', '-I', '-n', day]
        (ret, buff, err) = call(cmd, errlog=False)
        lines = []
        if ret != 0:
            return cols, lines
        for line in buff.split('\n'):
           l = line.split()
           if len(l) != 9:
               continue
           if l[0] == 'Time':
               continue
           x = [l[0], l[2], l[5], l[7], l[8], self.nodename]
           x[0] = '%s %s'%(d, l[0])
           lines.append(x)
        return cols, lines

0707010000bbf5000081a400000000000000000000000154fc7bfb00001dd5000000660000000300000000000000000000002500000000root/opt/opensvc/lib/rcStatsHP-UX.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os
from rcUtilities import call
from rcGlobalEnv import rcEnv
import rcStats

class StatsProvider(rcStats.StatsProvider):
    def glancefile(self, day):
        f = os.path.join(rcEnv.pathvar, 'glance'+day)
        if os.path.exists(f):
            return f
        return None

    def cpu(self, d, day, start, end):
        f = self.glancefile(day)
        if f is None:
            return [], []
        cols = ['date',
                'cpu',
                'usr',
                'nice',
                'sys',
                'iowait',
                'steal',
                'irq',
                'soft',
                'guest',
                'idle',
                'nodename']
        lines = []
        with open(f, 'r') as file:
            for line in file:
                l = line.split()
                if len(l) != 24:
                    continue
                """ hpux:            usr nice sys irq wait idle
                                     1   2    3   4   5    6
                    xmlrpc: date cpu usr nice sys iowait steal irq soft guest idle nodename
                """
                ts = '%s %s'%(d, l[0])
                ts = ts.replace('\0','')
                x = [ts,
                     'all',
                     l[1],
                     l[2],
                     l[3],
                     l[5],
                     '0',
                     l[4],
                     '0',
                     '0',
                     l[6],
                     self.nodename]
                lines.append(x)
            return cols, lines

    def mem_u(self, d, day, start, end):
        f = self.glancefile(day)
        if f is None:
            return [], []
        cols = ['date',
                'kbmemfree',
                'kbmemused',
                'pct_memused',
                'kbbuffers',
                'kbcached',
                'kbcommit',
                'pct_commit',
                'kbmemsys',
                'nodename']
        lines = []
        with open(f, 'r') as file:
            for line in file:
                l = line.split()
                if len(l) != 24:
                    continue
                """ hpux:            phys kbmemfree kbcached kbfilecached kbsys kbuser kbswapused kbswap
                                     7    8         9        10           11    12     13         14
                    xmlrpc: date kbmemfree kbmemused pct_memused kbbuffers kbcached kbcommit pct_commit kbmemsys nodename
                """
                phys = int(l[7])
                free = int(l[8])
                swapused = int(l[13])
                swap = int(l[14])
                used = phys - free
                commit = used + swapused
                vm = phys + swap
                if vm == 0 or phys == 0:
                    continue
                pct_commit = 100 * commit / vm
                pct_used = 100 * used / phys

                ts = '%s %s'%(d, l[0])
                ts = ts.replace('\0','')
                x = [ts,
                     l[8],
                     str(used),
                     str(pct_used),
                     l[9],
                     l[10],
                     str(commit),
                     str(pct_commit),
                     l[11],
                     self.nodename]
                lines.append(x)
        return cols, lines

    def proc(self, d, day, start, end):
        f = self.glancefile(day)
        if f is None:
            return [], []
        cols = ['date',
                'runq_sz',
                'plist_sz',
                'ldavg_1',
                'ldavg_5',
                'ldavg_15',
                'nodename']
        lines = []
        with open(f, 'r') as file:
            for line in file.readlines():
                l = line.split()
                if len(l) != 24:
                    continue
                """ hpux:            GBL_LOADAVG GBL_LOADAVG5 GBL_LOADAVG15 GBL_CPU_QUEUE TBL_PROC_TABLE_USED
                                     15          16           17            18            19
                    xmlrpc: date runq_sz plist_sz ldavg_1 ldavg_5 ldavg_15 nodename
                """
                ts = '%s %s'%(d, l[0])
                ts = ts.replace('\0','')
                x = [ts,
                     l[18],
                     l[19],
                     l[15],
                     l[16],
                     l[17],
                     self.nodename]
                lines.append(x)
        return cols, lines

    def swap(self, d, day, start, end):
        f = self.glancefile(day)
        if f is None:
            return [], []
        lines = []
        cols = ['date',
                'kbswpfree',
                'kbswpused',
                'pct_swpused',
                'kbswpcad',
                'pct_swpcad',
                'nodename']

        with open(f, 'r') as file:
            for line in file.readlines():
                l = line.split()
                if len(l) != 24:
                    continue
                """ hpux:        kbswapused kbswap
                                 13         14
                    xmlrpc: date kbswpfree kbswpused pct_swpused kbswpcad pct_swpcad nodename
                """
                swapused = int(l[13])
                swap = int(l[14])
                swapfree = swap - swapused

                ts = '%s %s'%(d, l[0])
                ts = ts.replace('\0','')
                x = [ts,
                     str(swapfree),
                     l[13],
                     str(100 * swapused / swap),
                     '0',
                     '0',
                     self.nodename]
                lines.append(x)
        return cols, lines

    def block(self, d, day, start, end):
        f = self.glancefile(day)
        if f is None:
            return [], []
        cols = ['date',
                'tps',
                'rtps',
                'wtps',
                'rbps',
                'wbps',
                'nodename']

        lines = []
        with open(f, 'r') as file:
            for line in file.readlines():
                l = line.split()
                if len(l) != 24:
                    continue
                """ hpux:        rio wio rkb wkb
                                 20  21  22  23
                    xmlrpc: date tps rtps wtps rbps wbps nodename
                """
                tps = float(l[20]) + float(l[21])
                ts = '%s %s'%(d, l[0])
                ts = ts.replace('\0','')
                x = [ts,
                     str(tps),
                     l[20],
                     l[21],
                     l[22],
                     l[23],
                     self.nodename]
                lines.append(x)
        return cols, lines

   0707010000bbf6000081a400000000000000000000000154fc7bfb00003745000000660000000300000000000000000000002500000000root/opt/opensvc/lib/rcStatsLinux.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os
import datetime
from rcUtilities import justcall, which
import rcStats
from rcGlobalEnv import rcEnv

class StatsProvider(rcStats.StatsProvider):
    def xentopfile(self, day):
        f = os.path.join(rcEnv.pathlog, 'xentop', 'xentop'+day)
        if os.path.exists(f):
            return f
        return None

    def svc(self, d, day, start, end):
        cols = ['date',
                'svcname',
                'cpu',
                'mem',
                'cap',
                'cap_cpu',
                'nodename']
        f = self.xentopfile(day)
        lines = []
        if f is None:
            return cols, lines
        try:
            with open(f, 'r') as f:
                buff = f.read()
        except:
            return cols, lines
        _start = datetime.datetime.strptime(start, "%H:%M:%S")
        _start = _start.hour * 3600 + _start.minute * 60 + _start.second
        _end = datetime.datetime.strptime(end, "%H:%M:%S")
        _end = _end.hour * 3600 + _end.minute * 60 + _end.second
        for line in buff.split('\n'):
            l = line.split()
            if len(l) != 21:
                continue
            _d = datetime.datetime.strptime(" ".join(l[0:2]), "%Y-%m-%d %H:%M:%S")
            _d = _d.hour * 3600 + _d.minute * 60 + _d.second
            if _d < _start or _d > _end:
                continue
            l = [" ".join((l[0], l[1]))] + [l[2], l[5], l[7], l[8], l[10], self.nodename]
            lines.append(l)
        return cols, lines

    def cpu(self, d, day, start, end):
        f = self.sarfile(day)
        if f is None:
            return [], []
        cmd = ['sar', '-t', '-u', 'ALL', '-P', 'ALL', '-f', f, '-s', start, '-e', end]
        (buff, err, ret) = justcall(cmd)
        if ret != 0:
            cmd = ['sar', '-t', '-u', '-P', 'ALL', '-f', f, '-s', start, '-e', end]
            (buff, err, ret) = justcall(cmd)
        cols = []
        lines = []
        for line in buff.split('\n'):
            l = line.split()
            if 'Linux' in l:
                continue
            if len(l) == 7:
                """ redhat 4
                    18:50:01 CPU %user %nice %system %iowait %idle
                """
                cols = ['date',
                        'cpu',
                        'usr',
                        'nice',
                        'sys',
                        'iowait',
                        'idle',
                        'nodename']
            elif len(l) == 8:
                """ redhat 5
                    05:20:01 CPU %user %nice %system %iowait %steal %idle
                """
                cols = ['date',
                        'cpu',
                        'usr',
                        'nice',
                        'sys',
                        'iowait',
                        'steal',
                        'idle',
                        'nodename']
            elif len(l) == 11:
                cols = ['date',
                        'cpu',
                        'usr',
                        'nice',
                        'sys',
                        'iowait',
                        'steal',
                        'irq',
                        'soft',
                        'guest',
                        'idle',
                        'nodename']
            elif len(l) == 12:
                cols = ['date',
                        'cpu',
                        'usr',
                        'nice',
                        'sys',
                        'iowait',
                        'steal',
                        'irq',
                        'soft',
                        'guest',
                        'gnice',
                        'idle',
                        'nodename']
            else:
                continue
            if l[1] == 'CPU':
                continue
            if l[0] == 'Average:':
                continue
            l.append(self.nodename)
            l[0] = '%s %s'%(d, l[0])
            lines.append(l)
        return cols, lines

    def mem_u(self, d, day, start, end):
        f = self.sarfile(day)
        if f is None:
            return [], []
        cmd = ['sar', '-t', '-r', '-f', f, '-s', start, '-e', end]
        (buff, err, ret) = justcall(cmd)

        if "kbactive" in buff:
            fmt = 3
            cols = ['date',
                    'kbmemfree',
                    'kbmemused',
                    'pct_memused',
                    'kbbuffers',
                    'kbcached',
                    'kbcommit',
                    'pct_commit',
                    'kbactive',
                    'kbinact',
                    'kbdirty',
                    'nodename']
        elif "pct_commit" in buff:
            fmt = 2
            cols = ['date',
                    'kbmemfree',
                    'kbmemused',
                    'pct_memused',
                    'kbbuffers',
                    'kbcached',
                    'kbcommit',
                    'pct_commit',
                    'nodename']
        else:
            fmt = 1
            cols = ['date',
                    'kbmemfree',
                    'kbmemused',
                    'pct_memused',
                    'kbbuffers',
                    'kbcached',
                    'nodename']

        n = len(cols) - 1
        lines = []
        for line in buff.split('\n'):
           l = line.split()
           if fmt > 1:
               if len(l) != n:
                   continue
           else:
               if len(l) < n:
                   continue
               l = l[:n]
           if l[1] == 'kbmemfree':
               continue
           if l[0] == 'Average:':
               continue

           l.append(self.nodename)
           l[0] = '%s %s'%(d, l[0])
           lines.append(l)
        return cols, lines

    def fs_u(self, d, day, start, end):
        pathvar = os.path.join(os.path.dirname(__file__), '..', 'var')
        now = datetime.datetime.now()
        _start = datetime.datetime.strptime(start, "%H:%M:%S")
        _start = _start.hour * 3600 + _start.minute * 60 + _start.second
        _end = datetime.datetime.strptime(end, "%H:%M:%S")
        _end = _end.hour * 3600 + _end.minute * 60 + _end.second
        f = os.path.join(pathvar, 'stats_fs_u.%s' % day)
        cols = ['date',
                'nodename',
                'mntpt',
                'size',
                'used']

        if not os.path.exists(f):
            return [], []

        with open(f, 'r') as fd:
            buff = fd.read()

        import json
        lines = []
        for line in buff.split('\n'):
            try:
                l = json.loads(line)
            except:
                continue
            for _l in l:
                if len(_l) != 5:
                    continue
                _now = datetime.datetime.strptime(_l[0], "%Y-%m-%d %H:%M:%S.%f")
                _now = _now.hour * 3600 + _now.minute * 60 + _now.second
                if _now < _start or _now > _end:
                    continue
                lines.append(_l)

        return cols, lines

    def proc(self, d, day, start, end):
        f = self.sarfile(day)
        cols = ['date',
                'runq_sz',
                'plist_sz',
                'ldavg_1',
                'ldavg_5',
                'ldavg_15',
                'nodename']

        if f is None:
            return [], []
        cmd = ['sar', '-t', '-q', '-f', f, '-s', start, '-e', end]
        (buff, err, ret) = justcall(cmd)
        lines = []
        if "blocked" in buff:
            n_fields = 7
            drop_blocked = True
        else:
            n_fields = 6
            drop_blocked = False
        for line in buff.split('\n'):
           l = line.split()
           if len(l) != n_fields:
               continue
           if l[1] == 'runq-sz':
               continue
           if l[0] == 'Average:':
               continue
           if drop_blocked:
              l = l[:-1]
           l.append(self.nodename)
           l[0] = '%s %s'%(d, l[0])
           lines.append(l)
        return cols, lines

    def swap(self, d, day, start, end):
        f = self.sarfile(day)
        cols = ['date',
                'kbswpfree',
                'kbswpused',
                'pct_swpused',
                'kbswpcad',
                'pct_swpcad',
                'nodename']

        if f is None:
            return [], []
        cmd = ['sar', '-t', '-S', '-f', f, '-s', start, '-e', end]
        (buff, err, ret) = justcall(cmd)
        if ret != 0:
            """ redhat 5
            """
            cmd = ['sar', '-t', '-r', '-f', f, '-s', start, '-e', end]
            (buff, err, ret) = justcall(cmd)
        lines = []
        for line in buff.split('\n'):
           l = line.split()
           if len(l) == 10:
               """ redhat 5
               """
               l = [l[0]] + l[6:] + ['0']
           if len(l) != 6:
               continue
           if 'kbswpfree'in l:
               continue
           if l[0] == 'Average:':
               continue
           l.append(self.nodename)
           l[0] = '%s %s'%(d, l[0])
           lines.append(l)
        return cols, lines

    def block(self, d, day, start, end):
        f = self.sarfile(day)
        cols = ['date',
                'tps',
                'rtps',
                'wtps',
                'rbps',
                'wbps',
                'nodename']

        if f is None:
            return [], []
        cmd = ['sar', '-t', '-b', '-f', f, '-s', start, '-e', end]
        (buff, err, ret) = justcall(cmd)
        lines = []
        for line in buff.split('\n'):
           l = line.split()
           if len(l) != 6:
               continue
           if l[1] == 'tps':
               continue
           if l[0] == 'Average:':
               continue
           l.append(self.nodename)
           l[0] = '%s %s'%(d, l[0])
           lines.append(l)
        return cols, lines

    def blockdev(self, d, day, start, end):
        f = self.sarfile(day)
        cols = ['date',
                'dev',
                'tps',
                'rsecps',
                'wsecps',
                'avgrq_sz',
                'avgqu_sz',
                'await',
                'svctm',
                'pct_util',
                'nodename']
        if f is None:
            return [], []
        cmd = ['sar', '-t', '-d', '-p', '-f', f, '-s', start, '-e', end]
        (buff, err, ret) = justcall(cmd)
        lines = []
        for line in buff.split('\n'):
           l = line.split()
           if len(l) != 10:
               continue
           if l[1] == 'DEV':
               continue
           if l[0] == 'Average:':
               continue
           l.append(self.nodename)
           l[0] = '%s %s'%(d, l[0])
           lines.append(l)
        return cols, lines

    def netdev(self, d, day, start, end):
        f = self.sarfile(day)
        cols = ['date',
                'dev',
                'rxpckps',
                'txpckps',
                'rxkBps',
                'txkBps',
                'nodename']

        if f is None:
            return [], []

        cmd = ['sar', '-t', '-n', 'DEV', '-f', f, '-s', start, '-e', end]
        (buff, err, ret) = justcall(cmd)

        if "%ifutil" in buff:
            n = 10
        else:
            n = 9

        lines = []
        div = 1
        for line in buff.split('\n'):
           l = line.split()
           if len(l) != n:
               continue
           if l[1] in ['IFACE', 'lo'] :
               if 'rxbyt/s' in l:
                   div = 1024
               continue
           if 'dummy' in l[1] or 'vnet' in l[1] or 'veth' in l[1] or \
              'pan'   in l[1] or 'sit'  in l[1]:
               continue
           if l[0] == 'Average:':
               continue
           m = []
           m.append('%s %s'%(d, l[0]))
           m.append(l[1])
           m.append(str(float(l[4])/div))
           m.append(str(float(l[5])/div))
           m.append(l[2])
           m.append(l[3])
           m.append(self.nodename)
           lines.append(m)
        return cols, lines


    def netdev_err(self, d, day, start, end):
        f = self.sarfile(day)
        cols = ['date',
                'dev',
                'rxerrps',
                'txerrps',
                'collps',
                'rxdropps',
                'txdropps',
                'nodename']

        if f is None:
            return [], []
        cmd = ['sar', '-t', '-n', 'EDEV', '-f', f, '-s', start, '-e', end]
        (buff, err, ret) = justcall(cmd)
        lines = []
        for line in buff.split('\n'):
           l = line.split()
           if len(l) != 11:
               continue
           if l[1] in ['IFACE', 'lo'] :
               continue
           if 'dummy' in l[1] or 'vnet' in l[1] or 'veth' in l[1] or \
              'pan'   in l[1] or 'sit'  in l[1]:
               continue
           if l[0] == 'Average:':
               continue
           l = l[0:7]
           l.append(self.nodename)
           l[0] = '%s %s'%(d, l[0])
           lines.append(l)
        return cols, lines

if __name__ == "__main__":
    sp = StatsProvider(interval=200)
    print(sp.get('mem_u'))
   0707010000bbf7000081a400000000000000000000000154fc7bfb00002013000000660000000300000000000000000000002500000000root/opt/opensvc/lib/rcStatsSunOS.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os
from rcUtilities import call, which
import rcStats
import datetime

class StatsProvider(rcStats.StatsProvider):
    def __init__(self, interval=2880, stats_dir=None,
                 stats_start=None, stats_end=None):
        rcStats.StatsProvider.__init__(self, interval=interval,
                                       stats_dir=stats_dir,
                                       stats_start=stats_start,
                                       stats_end=stats_end)
        cmd = ['pagesize']
        (ret, pagesize, err) = call(cmd)
        self.pagesize = int(pagesize)

    def zsfile(self, day):
        f = os.path.join(os.sep, 'var', 'adm', 'zonestat', 'zs'+day)
        if os.path.exists(f):
            return f
        return None

    def sarfile(self, day):
        f = os.path.join(os.sep, 'var', 'adm', 'sa', 'sa'+day)
        if os.path.exists(f):
            return f
        return None

    def svc(self, d, day, start, end):
        cols = ['date',
                'svcname',
                'swap',
                'rss',
                'cap',
                'at',
                'avgat',
                'pg',
                'avgpg',
                'nproc',
                'mem',
                'cpu',
                'nodename']
        f = self.zsfile(day)
        lines = []
        if f is None:
            return cols, lines
        try:
            with open(f, 'r') as f:
                buff = f.read()
        except:
            return cols, lines
        _start = datetime.datetime.strptime(start, "%H:%M:%S")
        _start = _start.hour * 3600 + _start.minute * 60 + _start.second
        _end = datetime.datetime.strptime(end, "%H:%M:%S")
        _end = _end.hour * 3600 + _end.minute * 60 + _end.second
        for line in buff.split('\n'):
            l = line.split()
            if len(l) != 17:
                continue
            _d = datetime.datetime.strptime(" ".join(l[0:2]), "%Y-%m-%d %H:%M:%S")
            _d = _d.hour * 3600 + _d.minute * 60 + _d.second
            if _d < _start or _d > _end:
                continue
            for i, e in enumerate(l):
                if e.endswith('T'):
                    l[i] = str(int(e[0:-1]) * 1024 * 1024)
                elif e.endswith('G'):
                    l[i] = str(int(e[0:-1]) * 1024)
                elif e.endswith('M'):
                    l[i] = e.rstrip('M')
                elif e.endswith('K'):
                    l[i] = str(1.0 * int(e[0:-1]) / 1024)
            l = [" ".join(l[0:2])] + l[2:-4] + [self.nodename]
            lines.append(l)
        return cols, lines

    def cpu(self, d, day, start, end):
        cols = ['date',
                'usr',
                'sys',
                'iowait',
                'idle',
                'cpu',
                'nodename']
        f = self.sarfile(day)
        lines = []
        if f is None:
            return cols, lines
        cmd = ['sar', '-u', '-f', f, '-s', start, '-e', end]
        (ret, buff, err) = call(cmd, errlog=False)
        for line in buff.split('\n'):
            l = line.split()
            if len(l) != 5:
                continue
            if l[1] == '%usr':
                continue
            if l[0] == 'Average':
                continue
            l += ['all', self.nodename]
            l[0] = '%s %s'%(d, l[0])
            lines.append(l)
        return cols, lines

    def mem_u(self, d, day, start, end):
        cols = ['date',
                'kbmemfree',
                'nodename']
        f = self.sarfile(day)
        lines = []
        if f is None:
            return cols, lines
        cmd = ['sar', '-r', '-f', f, '-s', start, '-e', end]
        (ret, buff, err) = call(cmd)
        for line in buff.split('\n'):
            l = line.split()
            if len(l) != 3:
                continue
            if l[1] == 'freemem':
                continue
            if l[0] == 'Average':
                continue

            try:
                freemem = int(l[1])*self.pagesize/1024
            except:
                continue
            x = ['%s %s'%(d, l[0]), str(freemem), self.nodename]
            lines.append(x)
        return cols, lines

    def proc(self, d, day, start, end):
        cols = ['date',
                'runq_sz',
                'nodename']
        f = self.sarfile(day)
        lines = []
        if f is None:
            return cols, lines
        cmd = ['sar', '-q', '-f', f, '-s', start, '-e', end]
        (ret, buff, err) = call(cmd)
        for line in buff.split('\n'):
            l = line.split()
            if len(l) != 5:
                continue
            if l[1] == 'runq-sz':
                continue
            if l[0] == 'Average':
                continue
            x = ['%s %s'%(d, l[0]), l[1], self.nodename]
            lines.append(x)
        return cols, lines

    def swap(self, d, day, start, end):
        cols = ['date',
                'kbswpfree',
                'nodename']
        f = self.sarfile(day)
        lines = []
        if f is None:
            return cols, lines
        cmd = ['sar', '-r', '-f', f, '-s', start, '-e', end]
        (ret, buff, err) = call(cmd)
        for line in buff.split('\n'):
            l = line.split()
            if len(l) != 3:
                continue
            if l[1] == 'freemem':
                continue
            if l[0] == 'Average':
                continue

            try:
                freeswap = int(l[2])/2
            except:
                continue
            x = ['%s %s'%(d, l[0]), str(freeswap), self.nodename]
            lines.append(x)
        return cols, lines

    def block(self, d, day, start, end):
        cols = ['date',
                'rbps',
                'wbps',
                'nodename']
        f = self.sarfile(day)
        lines = []
        if f is None:
            return [], []
        cmd = ['sar', '-b', '-f', f, '-s', start, '-e', end]
        (ret, buff, err) = call(cmd)
        for line in buff.split('\n'):
            l = line.split()
            if len(l) != 9:
                continue
            if l[1] == 'bread/s':
                continue
            if l[0] == 'Average':
                continue
            x = ['%s %s'%(d, l[0]), l[1], l[4], self.nodename]
            lines.append(x)
        return cols, lines

    def blockdev(self, d, day, start, end):
        cols = ['date',
                'dev',
                'pct_util',
                'avgqu_sz',
                'rsecps',
                'await',
                'svctm',
                'nodename']
        f = self.sarfile(day)
        lines = []
        if f is None:
            return cols, lines
        cmd = ['sar', '-d', '-f', f, '-s', start, '-e', end]
        (ret, buff, err) = call(cmd, errlog=False)
        for line in buff.split('\n'):
            l = line.split()
            if len(l) == 8:
                date = l[0]
            if len(l) == 7:
                l = [date] + l
            if len(l) != 8:
                continue
            if l[1] == 'device':
                continue
            if l[0] == 'Average':
                continue
            # 00:00:00 device %busy avque r+w/s [blks/s] avwait avserv
            x = ['%s %s'%(d, l[0]), l[1], l[2], l[3], l[4], l[6], l[7], self.nodename]
            lines.append(x)
        return cols, lines

 0707010000bbf8000081a400000000000000000000000154fc7bfb00001a12000000660000000300000000000000000000002100000000root/opt/opensvc/lib/rcStatus.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os

UP = 0
DOWN = 1
WARN = 2
NA = 3
TODO = 4
UNDEF = 5
STDBY_UP = 6
STDBY_DOWN = 7
STDBY_UP_WITH_UP = 8
STDBY_UP_WITH_DOWN = 9

GREEN = 32
RED = 31
YELLOW = 33

class color:
    PURPLE = '\033[95m'
    CYAN = '\033[96m'
    DARKCYAN = '\033[36m'
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    YELLOW = '\033[93m'
    RED = '\033[91m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
    END = '\033[0m'

def _colorize(s, c=None):
    if c is None:
        return s
    return c + s + color.END

def colorize(s, lpad=10):
    if type(s) == Status:
        s = str(s)
    fmt = "%-"+str(lpad)+"s"
    if not os.isatty(1):
        return fmt%s
    if s == "warn":
        return _colorize(fmt%s, color.YELLOW)
    elif s.endswith("down"):
        return _colorize(fmt%s, color.RED)
    elif s.endswith("up"):
        return _colorize(fmt%s, color.GREEN)
    elif s == "n/a":
        return _colorize(fmt%s, color.PURPLE)
    return fmt%s

_status_value = {
    'up': UP,
    'down': DOWN,
    'warn': WARN,
    'n/a': NA,
    'na': NA,
    'todo': TODO,
    'undef': UNDEF,
    'stdby up': STDBY_UP,
    'stdby down': STDBY_DOWN,
}

_status_str = {
    UP: 'up',
    DOWN: 'down',
    WARN: 'warn',
    NA: 'n/a',
    TODO: 'todo',
    UNDEF: 'undef',
    STDBY_UP: 'stdby up',
    STDBY_DOWN: 'stdby down',
    STDBY_UP_WITH_UP: 'up',
    STDBY_UP_WITH_DOWN: 'stdby up',
}

def status_value(str):
    if str not in _status_value.keys():
        return None
    return _status_value[str.lower()]

def status_str(val):
    if val not in _status_str.keys():
        return None
    return _status_str[val]

def _merge(s1, s2):
    """Merge too status: WARN and TODO taint UP and DOWN
    """
    if s1 not in _status_str:
        raise Exception("left member has unsupported value: %s"%str(s1))
    elif s2 not in _status_str:
        raise Exception("right member has unsupported value: %s"%str(s2))

    if s1 == UNDEF: return s2
    elif s2 == UNDEF: return s1
    setstate = set([s1, s2])
    if setstate == set([ UP, UP ]): return UP
    elif setstate == set([ UP, DOWN ]): return WARN
    elif setstate == set([ UP, WARN ]): return WARN
    elif setstate == set([ UP, NA ]): return UP
    elif setstate == set([ UP, TODO ]): return WARN
    elif setstate == set([ UP, STDBY_UP ]): return STDBY_UP_WITH_UP
    elif setstate == set([ UP, STDBY_DOWN ]): return WARN
    elif setstate == set([ UP, STDBY_UP_WITH_UP ]): return STDBY_UP_WITH_UP
    elif setstate == set([ UP, STDBY_UP_WITH_DOWN ]): return WARN
    elif setstate == set([ DOWN, DOWN ]): return DOWN
    elif setstate == set([ DOWN, WARN ]): return WARN
    elif setstate == set([ DOWN, NA ]): return DOWN
    elif setstate == set([ DOWN, TODO ]): return WARN
    elif setstate == set([ DOWN, STDBY_UP ]): return STDBY_UP_WITH_DOWN
    elif setstate == set([ DOWN, STDBY_DOWN ]): return STDBY_DOWN
    elif setstate == set([ DOWN, STDBY_UP_WITH_UP ]): return WARN
    elif setstate == set([ DOWN, STDBY_UP_WITH_DOWN ]): return STDBY_UP_WITH_DOWN
    elif setstate == set([ WARN, WARN ]): return WARN
    elif setstate == set([ WARN, NA ]): return WARN
    elif setstate == set([ WARN, TODO ]): return WARN
    elif setstate == set([ WARN, STDBY_UP ]): return WARN
    elif setstate == set([ WARN, STDBY_DOWN ]): return WARN
    elif setstate == set([ WARN, STDBY_UP_WITH_UP ]): return WARN
    elif setstate == set([ WARN, STDBY_UP_WITH_DOWN ]): return WARN
    elif setstate == set([ NA, NA ] ): return NA
    elif setstate == set([ NA, TODO ]): return WARN
    elif setstate == set([ NA, STDBY_UP ]): return STDBY_UP
    elif setstate == set([ NA, STDBY_DOWN ]): return STDBY_DOWN
    elif setstate == set([ NA, STDBY_UP_WITH_UP ]): return STDBY_UP_WITH_UP
    elif setstate == set([ NA, STDBY_UP_WITH_DOWN ]): return STDBY_UP_WITH_DOWN
    elif setstate == set([ TODO, TODO ]): return TODO
    elif setstate == set([ TODO, STDBY_UP ]): return TODO
    elif setstate == set([ TODO, STDBY_DOWN ]): return TODO
    elif setstate == set([ TODO, STDBY_UP_WITH_UP ]): return TODO
    elif setstate == set([ TODO, STDBY_UP_WITH_DOWN ]): return TODO
    elif setstate == set([ STDBY_UP, STDBY_UP ]): return STDBY_UP
    elif setstate == set([ STDBY_UP, STDBY_DOWN ]): return WARN
    elif setstate == set([ STDBY_UP, STDBY_UP_WITH_UP ]): return STDBY_UP_WITH_UP
    elif setstate == set([ STDBY_UP, STDBY_UP_WITH_DOWN ]): return STDBY_UP_WITH_DOWN
    elif setstate == set([ STDBY_DOWN, STDBY_DOWN ]): return STDBY_DOWN
    elif setstate == set([ STDBY_DOWN, STDBY_UP_WITH_UP ]): return WARN
    elif setstate == set([ STDBY_DOWN, STDBY_UP_WITH_DOWN ]): return WARN
    elif setstate == set([ STDBY_UP_WITH_UP, STDBY_UP_WITH_DOWN ]): return WARN
    elif setstate == set([ STDBY_UP_WITH_UP, STDBY_UP_WITH_UP ]): return STDBY_UP_WITH_UP
    elif setstate == set([ STDBY_UP_WITH_DOWN, STDBY_UP_WITH_DOWN ]): return STDBY_UP_WITH_DOWN
    raise Exception("some member has unsupported value: %s , %s "%(str(s1),str(s2)) )

class Status(object):
    """Class that wraps printing and calculation of resource status
    """
    def reset(self):
        self.status = UNDEF

    def __add__(self, s):
        self.status = _merge(self.status, s.status)
        return self

    def __iadd__(self, s):
        """Merge a status with current global status
        """
        if isinstance(s, Status):
            self.status = _merge(self.status, s.status)
        else:
            self.status = _merge(self.status, s)
        return self

    def __str__(self):
        return status_str(self.status)

    def __init__(self, initial_status=UNDEF):
        if type(initial_status) == int:
            self.status = initial_status
        elif type(initial_status) == Status:
            self.status = initial_status.status
        else:
            self.status = _status_value[str(initial_status)]
  0707010000bbf9000081a400000000000000000000000154fc7bfb0000176b000000660000000300000000000000000000002400000000root/opt/opensvc/lib/rcSymmetrix.py   from rcUtilities import justcall, which
from xml.etree.ElementTree import XML, fromstring
import rcExceptions as ex
import os

class Syms(object):
    syms = []
    def __init__(self, objects=[]):
        self.objects = objects
        if len(objects) > 0:
            self.filtering = True
        else:
            self.filtering = False
        self.index = 0
        if which('symcfg') is None:
            print('Can not find symcli programs in PATH')
            raise ex.excError
        out, err, ret = justcall(['symcfg', 'list', '-output', 'xml_element'])
        if ret != 0:
            print(err)
            raise ex.excError
        tree = fromstring(out)
        for symm in tree.getiterator('Symm_Info'):
            model = symm.find('model').text
            sid = symm.find('symid').text
            if self.filtering and sid not in self.objects:
                continue
            if model in ['VMAX-1']:
                self.syms.append(Vmax(sid))
            if 'DMX' in model or '3000-M' in model:
                self.syms.append(Dmx(sid))
            else:
                print("unsupported sym model: %s"%model)

    def __iter__(self):
        return self

    def next(self):
        if self.index == len(self.syms):
            raise StopIteration
        self.index += 1
        return self.syms[self.index-1]

class Sym(object):
    def __init__(self, sid):
        self.keys = ['sym_info',
                     'sym_dir_info',
                     'sym_dev_info',
                     'sym_dev_wwn_info',
                     'sym_devrdfa_info',
                     'sym_ficondev_info',
                     'sym_meta_info',
                     'sym_disk_info',
                     'sym_diskgroup_info']

        self.sid = sid

    def symcmd(self, cmd):
        cmd += ['-sid', self.sid, '-output', 'xml_element']
        return justcall(cmd)

    def symaccesscmd(self, cmd):
        if self.aclx is None:
            cmd += ['-output', 'xml_element']
        else:
            cmd += ['-file', self.aclx, '-output', 'xml_element']
        return justcall(cmd)

    def get_sym_info(self):
        cmd = ['symcfg', 'list']
        out, err, ret = self.symcmd(cmd)
        return out

    def get_sym_dir_info(self):
        cmd = ['symcfg', '-dir', 'all', '-v', 'list']
        out, err, ret = self.symcmd(cmd)
        return out

    def get_sym_dev_info(self):
        cmd = ['symdev', 'list']
        out, err, ret = self.symcmd(cmd)
        return out

    def get_sym_dev_wwn_info(self):
        cmd = ['symdev', 'list', '-wwn']
        out, err, ret = self.symcmd(cmd)
        return out

    def get_sym_devrdfa_info(self):
        cmd = ['symdev', 'list', '-v', '-rdfa']
        out, err, ret = self.symcmd(cmd)
        return out

    def get_sym_ficondev_info(self):
        cmd = ['symdev', 'list', '-ficon']
        out, err, ret = self.symcmd(cmd)
        return out

    def get_sym_meta_info(self):
        cmd = ['symdev', 'list', '-meta', '-v']
        out, err, ret = self.symcmd(cmd)
        return out

    def get_sym_disk_info(self):
        cmd = ['symdisk', 'list', '-v']
        out, err, ret = self.symcmd(cmd)
        return out

    def get_sym_diskgroup_info(self):
        cmd = ['symdisk', 'list', '-dskgrp_summary']
        out, err, ret = self.symcmd(cmd)
        return out

class Vmax(Sym):
    def __init__(self, sid):
        Sym.__init__(self, sid)
        self.keys += ['sym_ig_aclx',
                      'sym_pg_aclx',
                      'sym_sg_aclx',
                      'sym_view_aclx']

        if 'SYMCLI_DB_FILE' in os.environ:
            dir = os.path.dirname(os.environ['SYMCLI_DB_FILE'])
            # flat format
            self.aclx = os.path.join(dir, sid+'.aclx')
            if not os.path.exists(self.aclx):
                # emc grab format
                import glob
                files = glob.glob(os.path.join(dir, sid, sid+'*.aclx'))
                if len(files) == 1:
                    self.aclx = files[0]
            if not os.path.exists(self.aclx):
                print("missing file %s"%self.aclx)
        else:
            self.aclx = None

    def symaccesscmd(self, cmd):
        if self.aclx is None:
            cmd += ['-output', 'xml_element']
        else:
            cmd += ['-file', self.aclx, '-output', 'xml_element']
        return justcall(cmd)

    def get_sym_pg_aclx(self):
        cmd = ['symaccess', 'list', '-type', 'port']
        out, err, ret = self.symaccesscmd(cmd)
        return out

    def get_sym_sg_aclx(self):
        cmd = ['symaccess', 'list', '-type', 'storage']
        out, err, ret = self.symaccesscmd(cmd)
        return out

    def get_sym_ig_aclx(self):
        cmd = ['symaccess', 'list', '-type', 'initiator']
        out, err, ret = self.symaccesscmd(cmd)
        return out

    def get_sym_view_aclx(self):
        cmd = ['symaccess', 'list', 'view', '-details']
        out, err, ret = self.symaccesscmd(cmd)
        return out

class Dmx(Sym):
    def __init__(self, sid):
        Sym.__init__(self, sid)
        self.keys += ['sym_maskdb']

        if 'SYMCLI_DB_FILE' in os.environ:
            dir = os.path.dirname(os.environ['SYMCLI_DB_FILE'])
            # flat format
            self.maskdb = os.path.join(dir, sid+'.bin')
            if not os.path.exists(self.maskdb):
                # emc grab format
                self.maskdb = os.path.join(dir, sid, 'symmaskdb_backup.bin')
            if not os.path.exists(self.maskdb):
                print("missing file %s"%self.maskdb)
        else:
            self.maskdb = None

    def symaccesscmd(self, cmd):
        if self.maskdb is None:
            cmd += ['-sid', self.sid, '-output', 'xml_element']
        else:
            cmd += ['-f', self.maskdb, '-output', 'xml_element']
        return justcall(cmd)

    def get_sym_maskdb(self):
        cmd = ['symmaskdb', 'list', 'database']
        out, err, ret = self.symaccesscmd(cmd)
        return out


 0707010000bbfa000081a400000000000000000000000154fc7bfb0000330e000000660000000300000000000000000000002400000000root/opt/opensvc/lib/rcSysReport.py   from __future__ import print_function
import os
import shutil
import glob
from rcGlobalEnv import rcEnv
from rcUtilities import which, cmdline2list
from stat import *
from subprocess import *
import json

class SysReport(object):
    def __init__(self, node=None):
        self.todo = [
          ('INC', '/opt/opensvc/etc/node.conf'),
          ('INC', '/opt/opensvc/etc/*env'),
          ('INC', '/opt/opensvc/etc/sysreport.conf.d'),
        ]

        self.changed = []
        self.deleted = []
        self.sysreport_conf_d = os.path.join(rcEnv.pathetc, "sysreport.conf.d")
        self.sysreport_d = os.path.join(rcEnv.pathvar, "sysreport")
        self.collect_d = os.path.join(self.sysreport_d, rcEnv.nodename)
        self.collect_cmd_d = os.path.join(self.collect_d, "cmd")
        self.collect_file_d = os.path.join(self.collect_d, "file")
        self.collect_stat = os.path.join(self.collect_file_d, "stat")
        self.full = [self.collect_stat]
        self.stat_changed = False
        self.root_uid = 0
        self.root_gid = 0
        self.node = node
        self.archive_extension = '.tar'
        self.send_rpc = "send_sysreport"
        self.lstree_rpc = "sysreport_lstree"

    def init(self):
        self.init_dir(self.collect_d)
        self.init_dir(self.collect_cmd_d)
        self.init_dir(self.collect_file_d)
        self.init_dir(self.sysreport_conf_d)
        self.load_stat()
        self.merge_todo()

    def init_dir(self, fpath):
        self.init_collect_d(fpath)
        self.init_collect_d_ownership(fpath)
        self.init_collect_d_perms(fpath)

    def init_collect_d(self, fpath):
        if not os.path.exists(fpath):
            print("create dir", fpath)
            os.makedirs(fpath)

    def init_collect_d_perms(self, fpath):
        s = os.stat(fpath)
        mode = s[ST_MODE]
        if mode != 16768:
            print("set dir", fpath, "mode to 0600")
            os.chmod(fpath, 0600)

    def init_collect_d_ownership(self, fpath):
        s = os.stat(fpath)
        if s.st_uid != self.root_uid or s.st_gid != self.root_gid:
            print("set dir", self.collect_d, "ownership to", self.root_uid, self.root_gid)
            os.chown(self.collect_d, self.root_uid, self.root_gid)

    def load_stat(self):
        try:
            self.stat = self._load_stat()
        except:
            self.stat = {}

    def _load_stat(self):
        with open(self.collect_stat, "r") as f:
            buff = f.read()
        l = json.loads(buff)
        stat = {}
        for e in l:
            stat[e["fpath"]] = e
        return stat

    def write_stat(self):
        if not self.stat_changed:
            return
        self._write_stat()

    def _write_stat(self):
        l = []
        for fpath in sorted(self.stat.keys()):
            l.append(self.stat[fpath])
        with open(self.collect_stat, "w") as f:
            f.write(json.dumps(l, sort_keys=True, separators=[", ", ": "], indent=4))

    def merge_todo(self):
        for root, dnames, fnames in os.walk(self.sysreport_conf_d):
            for fname in fnames:
                fpath = os.path.join(self.sysreport_conf_d, fname)
                s = os.stat(fpath)
                mode = s[ST_MODE]
                if mode & S_IWOTH:
                    print("skip %s config file: file mode is insecure ('other' has write permission)" % fpath)
                    continue
                if s.st_uid != self.root_uid or s.st_gid != self.root_gid:
                    print("skip %s config file: file ownership is insecure (must be owned by root)" % fpath)
                    continue

                with open(fpath, 'r') as f:
                    buff = f.read()
                for line in buff.split("\n"):
                    line = line.strip()
                    if line.startswith("FILE"):
                        t = ("INC", line[4:].strip())
                    elif line.startswith("CMD"):
                        t = ("CMD", line[3:].strip())
                    elif line.startswith("DIR"):
                        t = ("INC", line[3:].strip())
                    elif line.startswith("GLOB"):
                        t = ("INC", line[4:].strip())
                    elif line.startswith("INC"):
                        t = ("INC", line[3:].strip())
                    elif line.startswith("EXC"):
                        t = ("EXC", line[3:].strip())
                    elif line == "":
                        continue
                    elif line.startswith("#"):
                        continue
                    elif line.startswith(";"):
                        continue
                    else:
                        print("unsupported item type:", line)
                        continue
                    if t not in self.todo:
                        self.todo.append(t)

        # expand
        inc = set([])
        exc = set([])
        self.cmds = set([])
        for mode, s in self.todo:
            if mode == "CMD":
                self.cmds.add(s)
                continue
            l = []
            for _s in glob.glob(s):
                if os.path.isdir(_s):
                    l += self.find_files(_s)
                else:
                    l.append(_s)
            if mode == "INC":
                inc |= set(l)
            elif mode == "EXC":
                exc |= set(l)
        self.files = inc - exc

        # find deleted
        dst_files = self.find_files(self.collect_file_d)
        n = len(self.collect_file_d)
        dst_files = map(lambda x: x[n:], dst_files)
        self.deleted = set(dst_files) - self.files - set(["/stat"])

        # order file lists
        self.files = sorted(list(self.files))
        self.deleted = sorted(list(self.deleted))

        # purge stat info of deleted files
        for fpath in self.deleted:
            if fpath in self.stat:
                del(self.stat[fpath])
                self.stat_changed = True
                if self.collect_stat not in self.changed:
                    self.changed.append(self.collect_stat)
                if self.collect_stat not in self.full:
                    self.full.append(self.collect_stat)
 

    def cmdlist2fname(self, l):
        fname = '(space)'.join(l)
        fname = fname.replace('|','(pipe)')
        fname = fname.replace('&','(amp)')
        fname = fname.replace('$','(dollar)')
        fname = fname.replace('^','(caret)')
        fname = fname.replace('/','(slash)')
        fname = fname.replace(':','(colon)')
        fname = fname.replace(';','(semicolon)')
        fname = fname.replace('<','(lt)')
        fname = fname.replace('>','(gt)')
        fname = fname.replace('=','(eq)')
        fname = fname.replace('?','(question)')
        fname = fname.replace('@','(at)')
        fname = fname.replace('!','(excl)')
        fname = fname.replace('#','(num)')
        fname = fname.replace('%','(pct)')
        fname = fname.replace('"','(dquote)')
        fname = fname.replace("'",'(squote)')
        return fname

    def write(self, fpath, buff):
        try:
            with open(fpath, 'r') as f:
                pbuff = f.read()
            if buff != pbuff:
                self.changed.append(fpath)
        except IOError:
            self.changed.append(fpath)
        with open(fpath, 'w') as f:
            f.write(buff)
        self.full.append(fpath)

    def collect_cmd(self, cmd):
        l = cmdline2list(cmd)
        if len(l) == 0:
            print(" err: syntax error")
            return
        if not os.path.exists(l[0]):
            return
        if which(l[0]) is None:
            print(" err: not executable")
            return
        fname = self.cmdlist2fname(l)
        cmd_d = os.path.join(self.collect_cmd_d, fname)
        p = Popen(l, stdout=PIPE, stderr=STDOUT, close_fds=True)
        out, err = p.communicate()
        self.write(os.path.join(cmd_d), out)

    def get_stat(self, fpath):
        st = os.stat(fpath)
        stat = {
          "fpath": fpath,
          "realpath": os.path.realpath(fpath),
          "mode": oct(st[ST_MODE]),
          "uid": st[ST_UID],
          "gid": st[ST_GID],
          "dev": st[ST_DEV],
          "nlink": st[ST_NLINK],
          "mtime": st[ST_MTIME],
          "ctime": st[ST_CTIME],
        }
        return stat

    def push_stat(self, fpath):
        stat = self.get_stat(fpath)
        cached_stat = self.stat.get(fpath)
        if cached_stat is None:
            self.stat[fpath] = stat
            self.stat_changed = True
            if self.collect_stat not in self.changed:
                self.changed.append(self.collect_stat)
            if self.collect_stat not in self.full:
                self.full.append(self.collect_stat)
            #print("  add %s stat info"%fpath)
            return
        for p in ("realpath", "mode", "uid", "gid", "dev", "nlink", "mtime", "ctime"):
            if stat[p] != cached_stat[p]:
                self.stat[fpath] = stat
                self.stat_changed = True
                if self.collect_stat not in self.changed:
                    self.changed.append(self.collect_stat)
                if self.collect_stat not in self.full:
                    self.full.append(self.collect_stat)
                #print("  change %s stat info"%fpath)
                return

    def collect_file(self, fpath):
        if not os.path.exists(fpath):
            return
        if os.path.islink(fpath):
            return
        dst_d = self.collect_file_d + os.path.dirname(fpath)
        fname = os.path.basename(fpath)
        dst_f = os.path.join(dst_d, fname)
        if not os.path.exists(dst_d):
            os.makedirs(dst_d)

        self.push_stat(fpath)

        try:
            with open(fpath, 'r') as f:
                buff = f.read()
            with open(dst_f, 'r') as f:
                pbuff = f.read()
            if buff != pbuff:
                self.changed.append(dst_f)
        except IOError:
            # in doubt, send ... git will know better on the collector
            self.changed.append(dst_f)
        shutil.copy2(fpath, dst_f)
        self.full.append(dst_f)

    def delete_collected(self, fpaths):
        for fpath in fpaths:
            self.delete_collected_one(fpath)

    def delete_collected_one(self, fpath):
        fp = self.collect_file_d + fpath
        os.unlink(fp)

    def find_files(self, fpath):
        l = []
        if not os.path.exists(fpath):
            return l
        for item in os.listdir(fpath):
            _fpath = os.path.join(fpath, item)
            if os.path.isdir(_fpath):
                l += self.find_files(_fpath)
            elif not os.path.islink(_fpath):
                l.append(_fpath)
        return l

    def sysreport(self, force=False):
        self.node.collector.init(self.send_rpc)
        if self.node.collector.proxy is None:
            print("no collector connexion. abort sysreport")
            return 1
        self.init()
        print("collect directory is", self.collect_d)
        for fpath in self.files:
            self.collect_file(fpath)
        for cmd in self.cmds:
            self.collect_cmd(cmd)
        self.delete_collected(self.deleted)
        self.write_stat()
        self.send(force)

    def deleted_report(self):
        print("files deleted:")
        for fpath in sorted(self.deleted):
            print("  "+fpath)

    def send(self, force=False):
        if force:
            to_send = self.full
            lstree_data = self.node.collector.call(self.lstree_rpc)
            n = len(self.collect_d)+1
            self.deleted = sorted(list(set(lstree_data) - set("file/stat") - set(map(lambda x: x[n:], self.full))))
        else:
            to_send = self.changed
            self.changed_report()

        if len(self.deleted) > 0:
            self.deleted_report()

        if len(to_send) == 0 and len(self.deleted) == 0:
            print("no change to report")
            return

        if len(to_send) > 0:
            tmpf = self.archive(to_send)
        else:
            tmpf = None

        print("sending sysreport")
        self.node.collector.call(self.send_rpc, tmpf, self.deleted)

        if tmpf is not None:
            os.unlink(tmpf)

    def archive(self, l):
        import tarfile
        import tempfile
        f = tempfile.NamedTemporaryFile(prefix="sysreport.", suffix=self.archive_extension, dir=self.collect_d)
        tmpf = f.name
        f.close()
        cwd = os.getcwd()
        os.chdir(self.sysreport_d)
        n = len(self.sysreport_d) + 1
        print("creating tarball", tmpf)
        tar = tarfile.open(tmpf, mode="w")
        for fpath in l:
            if len(fpath) < n:
                print(" err: can not archive", fpath, "(fpath too short)")
                continue
            tar.add(fpath[n:])
        tar.close()
        os.chdir(cwd)
        return tmpf

    def changed_report(self):
        if len(self.changed) > 0:
            print("changed files:")
            for fpath in self.changed:
                print(" ", fpath)

  0707010000bbfb000081a400000000000000000000000154fc7bfb00000098000000660000000300000000000000000000002700000000root/opt/opensvc/lib/rcSysReportAIX.py    import rcSysReport

class SysReport(rcSysReport.SysReport):
    def __init__(self, node=None):
        rcSysReport.SysReport.__init__(self, node=node)

0707010000bbfc000081a400000000000000000000000154fc7bfb00000098000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/rcSysReportDarwin.py import rcSysReport

class SysReport(rcSysReport.SysReport):
    def __init__(self, node=None):
        rcSysReport.SysReport.__init__(self, node=node)

0707010000bc15000081a400000000000000000000000154fc7bfb00001e12000000660000000300000000000000000000001e00000000root/opt/opensvc/lib/rcZfs.py #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from rcUtilities import justcall, call, vcall
import logging
import sys
"""
"""

def dataset_exists(device, type):
    """return Dataset(device).exists(type)"""
    return Dataset(device).exists(type)

def zfs_getprop(dataset='undef_ds', propname='undef_prop'):
    "return zfs dataset property propname value"
    cmd = [ 'zfs', 'get', '-Hp', '-o', 'value', propname, dataset ]
    (stdout, stderr, retcode) = justcall(cmd)
    if retcode == 0 :
        return stdout.split("\n")[0]
    else:
        return ""

def zfs_setprop(dataset='undef_ds', propname='undef_prop', propval='undef_val'):
    "set zfs dataset property propname to value propval"
    if zfs_getprop(dataset, propname) == propval :
        return True
    cmd = [ 'zfs', 'set', propname + '='+ propval, dataset ]
    print(' '.join(cmd))
    (retcode, stdout, stderr) = vcall(cmd)
    if retcode == 0 :
        return True
    else:
        return False

def a2pool_dataset(s):
    """return (pool,dataset) from mount point
       example: a2pool_dataset('/') => ('rpool','rpool/ROOT/opensolaris-b134')
                same with a2pool_dataset('rpool/ROOT/opensolaris-b134')
    """
    if len(s) == 0:
        return ("", "")
    ss = s
    if s[0] == '/':
        cmd = ['zfs', 'list', '-H',  '-o', 'name', s]
        (ret, out, err) = call(cmd)
        if ret != 0:
            return ("", "")
        ss = out.split('\n')[0]
    x = ss.split('/')
    if len(x) < 2:
        return (ss, ss)
    return (x[0], ss)

class Dataset(object):
    """Define Dataset Class"""
    log = None
    def __init__(self, name, log=None):
        self.name = name
        if log is None:
            if Dataset.log is None:
                Dataset.log = logging.getLogger("DATASET".upper())
                Dataset.log.addHandler(logging.StreamHandler(sys.stdout))
                Dataset.log.setLevel(logging.INFO)
            self.log = Dataset.log
        else:
            self.log = log
    def __str__(self, option=None):
        if option is None:
            cmd = ['zfs', 'list', self.name ]
        else:
            cmd = ['zfs', 'list'] + option + [ self.name ]
        (retcode, stdout, stderr) = call(cmd, log=self.log)
        if retcode == 0:
            return stdout
        else:
            return "Failed to list info for dataset: %s" % (self.name)

    def destroy(self):
        "destroy dataset"
        cmd = ['zfs', 'destroy', self.name ]
        (retcode, stdout, stderr) = vcall(cmd, log=self.log)
        if retcode == 0:
            return True
        else:
            return False

    def exists(self, type="all"):
        """return True if dataset exists else return False
        if type is provided, also verify dataset type"""
        (out, err, ret) = justcall('zfs get -H -o value type'.split()+[self.name])
        if ret == 0 and type == "all":
            return True
        elif ret == 0 and out.split('\n')[0] == type:
            return True
        else:
            return False

    def create(self, option = None):
        "create dataset with options"
        if option is None:
            cmd = ['zfs', 'create', self.name ]
        else:
            cmd = ['zfs', 'create'] + option + [ self.name ]
        (retcode, stdout, stderr) = vcall(cmd, log=self.log)
        if retcode == 0:
            return True
        else:
            return False

    def getprop(self, propname):
        """get a dataset propertie value of dataset
        If success return propperty value
        else return ''
        """
        cmd = [ 'zfs', 'get', '-Hp', '-o', 'value', propname, self.name ]
        (stdout, stderr, retcode) = justcall(cmd)
        if retcode == 0 :
            return stdout.rstrip('\n')
        else:
            return ""

    def setprop(self, propname, propval, err_to_warn=False, err_to_info=False):
        """set Dataset property value
        Return True is sucess else return False
        """
        cmd = [ 'zfs', 'set', propname + '='+ propval, self.name ]
        (retcode, stdout, stderr) = vcall(cmd, log=self.log,
                                        err_to_warn=err_to_warn,
                                        err_to_info=err_to_info)
        if retcode == 0 :
            return True
        else:
            return False

    def verify_prop(self, nv_pairs={}, err_to_warn=False, err_to_info=False):
        """for name, val from nv_pairs dict,
        if zfs name property value of dataset differ from val
        then zfs set name=value on dataset object"""
        for name in nv_pairs.keys():
            if self.getprop(name) != nv_pairs[name]:
                self.setprop(propname=name, propval=nv_pairs[name],
                            err_to_warn=err_to_warn,
                            err_to_info=err_to_info)

    def snapshot(self, snapname=None):
        """snapshot dataset
        return snapshot dataset object
        Return False if failure
        """
        if snapname is None:
            raise(rcExceptions.excBug("snapname should be defined"))
        snapdataset = self.name + "@" + snapname
        cmd = ['zfs', 'snapshot', snapdataset ]
        (retcode, stdout, stderr) = vcall(cmd, log=self.log)
        if retcode == 0:
            return Dataset(snapdataset)
        else:
            return False

    def clone(self, name, option=None):
        """clone dataset with options
        return clone object
        return False if failure
        """
        if option is None:
            cmd = ['zfs', 'clone', self.name, name]
        else:
            cmd = ['zfs', 'clone'] + option + [ self.name, name ]
        (retcode, stdout, stderr) = vcall(cmd, log=self.log)
        if retcode == 0:
            return Dataset(name)
        else:
            return False

if __name__ == "__main__":
    dsname="rpool/toto"
    ds = Dataset(dsname)
    if ds.create(option=[ "-o", "mountpoint=none"]) is False:
        print("========== Failed")
    else:
        print(ds)

    ds.verify_prop({'mountpoint':'/tmp/mnt', 'refquota':(10*1024*1024).__str__(),})

    print("show type,refquota,mountpoint")
    for p in ('type', 'refquota', 'mountpoint'):
        print('%s value: %s'%(p, ds.getprop(p)))
    print(ds)

    val = ds.setprop('opensvc:name', 'Example')
    print(ds.__str__(["-Ho", "opensvc:name"]))

    val = ds.getprop('opensvc:name')
    print("val Value=",val)

    for sname in ["foo" , "bar"]:
        s = ds.snapshot(sname)
        if s is False:
            print("========== Failed")
        else:
            print(s)
            c=s.clone(dsname + "/clone_"+ sname)
            if c is False:
                print("========== Failed")
            else:
                print(c)
                c.destroy()

            if s.destroy() is False:
                print("========== Failed")

    if ds.exists:
        print("Destroy dataset", ds.name)
        if ds.destroy() is False:
            print("Failed to create snapshot")

  0707010000bbfd000081a400000000000000000000000154fc7bfb00000098000000660000000300000000000000000000002b00000000root/opt/opensvc/lib/rcSysReportFreeBSD.py    import rcSysReport

class SysReport(rcSysReport.SysReport):
    def __init__(self, node=None):
        rcSysReport.SysReport.__init__(self, node=node)

0707010000bbfe000081a400000000000000000000000154fc7bfb00000098000000660000000300000000000000000000002900000000root/opt/opensvc/lib/rcSysReportHP-UX.py  import rcSysReport

class SysReport(rcSysReport.SysReport):
    def __init__(self, node=None):
        rcSysReport.SysReport.__init__(self, node=node)

0707010000bbff000081a400000000000000000000000154fc7bfb00000098000000660000000300000000000000000000002900000000root/opt/opensvc/lib/rcSysReportLinux.py  import rcSysReport

class SysReport(rcSysReport.SysReport):
    def __init__(self, node=None):
        rcSysReport.SysReport.__init__(self, node=node)

0707010000bc00000081a400000000000000000000000154fc7bfb00000098000000660000000300000000000000000000002800000000root/opt/opensvc/lib/rcSysReportOSF1.py   import rcSysReport

class SysReport(rcSysReport.SysReport):
    def __init__(self, node=None):
        rcSysReport.SysReport.__init__(self, node=node)

0707010000bc01000081a400000000000000000000000154fc7bfb00000098000000660000000300000000000000000000002900000000root/opt/opensvc/lib/rcSysReportSunOS.py  import rcSysReport

class SysReport(rcSysReport.SysReport):
    def __init__(self, node=None):
        rcSysReport.SysReport.__init__(self, node=node)

0707010000bc02000081a400000000000000000000000154fc7bfb000000df000000660000000300000000000000000000002700000000root/opt/opensvc/lib/rcUpdatePkgAIX.py    from subprocess import *

repo_subdir = "rpms"

def update(fpath):
    cmd = ['rpm', '-U', fpath, '--force', '--ignoreos', '--nodeps']
    print(' '.join(cmd))
    p = Popen(cmd)
    p.communicate()
    return p.returncode
 0707010000bc03000081a400000000000000000000000154fc7bfb000001c0000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/rcUpdatePkgDarwin.py from subprocess import *
import os

repo_subdir = "macos-pkg"

def update(fpath):
    # macos installer expect a .pkg file extension
    pkgfile = fpath+'.pkg'
    print("renaming %s to %s"%(fpath, pkgfile))
    os.rename(fpath, pkgfile)
    cmd = ['installer', '-package', pkgfile, '-target', '/']
    print(' '.join(cmd))
    p = Popen(cmd)
    p.communicate()
    try:
        os.unlink(pkgfile)
    except:
        pass
    return p.returncode
0707010000bc04000081a400000000000000000000000154fc7bfb00000108000000660000000300000000000000000000002900000000root/opt/opensvc/lib/rcUpdatePkgHP-UX.py  from subprocess import *

repo_subdir = "depot"

def update(fpath):
    cmd = ['swinstall', '-x', 'mount_all_filesystems=false', '-x', 'allow_downdate=true', '-s', fpath, '*']
    print(' '.join(cmd))
    p = Popen(cmd)
    p.communicate()
    return p.returncode
0707010000bc05000081a400000000000000000000000154fc7bfb00000242000000660000000300000000000000000000002900000000root/opt/opensvc/lib/rcUpdatePkgLinux.py  from subprocess import *
import os


def update_deb(fpath):
    cmd = ['dpkg', '-i', fpath]
    print(' '.join(cmd))
    p = Popen(cmd)
    p.communicate()
    return p.returncode

def update_rpm(fpath):
    cmd = ['rpm', '-U', fpath, '--force', '--nodeps']
    print(' '.join(cmd))
    p = Popen(cmd)
    p.communicate()
    return p.returncode

if os.path.exists('/etc/debian_version'):
    update = update_deb
    repo_subdir = "deb"
elif os.path.exists('/etc/SuSE-release') or \
     os.path.exists('/etc/redhat-release'):
    repo_subdir = "rpms"
    update = update_rpm


  0707010000bc06000081a400000000000000000000000154fc7bfb00000232000000660000000300000000000000000000002800000000root/opt/opensvc/lib/rcUpdatePkgOSF1.py   from __future__ import print_function
import os
import tarfile

repo_subdir = "tar"

def update(fpath):
    oldpath = os.getcwd()
    os.chdir(os.path.join(os.sep, "opt"))
    tar = tarfile.open(fpath)
    try:
        tar.extractall()
        tar.close()
    except:
        try:
            os.unlink(fpath)
        except:
            pass
        print("failed to unpack", file=sys.stderr)
        return 1
    try:
        os.unlink(fpath)
    except:
        pass

    cmd = '/opt/opensvc/bin/python /opt/opensvc/bin/postinstall'
    return os.system(cmd)
  0707010000bc07000081a400000000000000000000000154fc7bfb0000061b000000660000000300000000000000000000002900000000root/opt/opensvc/lib/rcUpdatePkgSunOS.py  from subprocess import *
from rcUtilitiesSunOS import get_solaris_version
import os

repo_subdir = "sunos-pkg"

def gen_adminfile():
    filename = "/var/tmp/opensvc.adminfile"
    f = open (filename, 'w')
    f.write("mail=\n")
    f.write("instance=overwrite\n")
    f.write("partial=nocheck\n")
    f.write("runlevel=nocheck\n")
    f.write("idepend=nocheck\n")
    f.write("rdepend=nocheck\n")
    f.write("space=nocheck\n")
    f.write("setuid=nocheck\n")
    f.write("conflict=nocheck\n")
    f.write("action=nocheck\n")
    f.write("basedir=default\n")
    f.close()
    return filename

def update(fpath):
    # check downloaded package integrity
    cmd = ['pkgchk', '-d', fpath, 'all']
    print(' '.join(cmd))
    p = Popen(cmd)
    p.communicate()
    if p.returncode != 0:
        return 1

    # check if initially installed with pkgadd -G
    file = '/var/sadm/install/gz-only-packages'
    GlobalOnly = False
    if os.path.isfile(file):
	f = open(file)
	for line in f:
	    if line.startswith("opensvc"):
		print("OpenSVC package was previously installed with pkgadd -G\n")
		GlobalOnly = True

    cmd = ['pkgrm', '-n', 'opensvc']
    print(' '.join(cmd))
    p = Popen(cmd)
    p.communicate()
    if p.returncode != 0:
        return 1
    osver = get_solaris_version()
    if osver < 10.0:
        opts = ''
    else:
	if GlobalOnly is True:
            opts = '-G'
        else:
	    opts = ''
    admin = gen_adminfile()
    opts += " -a %s " % admin
    cmd = 'pkgadd %s -d %s all' % (opts, fpath)
    print(cmd)
    return os.system(cmd)
 0707010000bc08000081a400000000000000000000000154fc7bfb000000b2000000660000000300000000000000000000002b00000000root/opt/opensvc/lib/rcUpdatePkgWindows.py    from subprocess import *

repo_subdir = "exe"

def update(fpath):
    cmd = [fpath, '/S']
    print(' '.join(cmd))
    p = Popen(cmd)
    p.communicate()
    return p.returncode
  0707010000bc09000081a400000000000000000000000154fc7bfb00002dd6000000660000000300000000000000000000002400000000root/opt/opensvc/lib/rcUtilities.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from __future__ import print_function
import os, sys
import logging
from subprocess import *
from rcGlobalEnv import rcEnv

if os.name == 'nt':
    close_fds = False
else:
    close_fds = True

def ximport(base):
    mod = base + rcEnv.sysname
    try:
        m = __import__(mod)
        return m
    except:
        pass

    return __import__(base)

def fork(fn, args=[], kwargs={}, serialize=False, delay=300):
    if os.fork() > 0:
        """ return to parent execution
        """
        return

    """ separate the son from the father
    """
    os.chdir('/')
    os.setsid()
    os.umask(0)

    try:
        pid = os.fork()
        if pid > 0:
            os._exit(0)
    except:
        os._exit(1)

    if serialize:
        from lock import lock, unlock
        lockfile=os.path.join(rcEnv.pathvar, "fork_"+fn.__name__+".lock")
        try:
            fd = lock(lockfile=lockfile, timeout=1, delay=1)
        except Exception as e:
            print("%s is already running" % fn.__name__)
            os._exit(0)

    # now wait for a random delay to not DoS the collector.
    if delay > 0:
        import random
        import time
        delay = int(random.random()*delay)
        print("delay '%s' for %d secs to level database load"%(fn.__name__, delay))
        try:
            time.sleep(delay)
        except KeyboardInterrupt as e:
            print(e)
            os._exit(1)

    try:
        fn(*args, **kwargs)
    except Exception as e:
        if serialize:
            unlock(fd)
        print(e, file=sys.stderr)
        os._exit(1)

    if serialize:
        unlock(fd)
    os._exit(0)

def check_privs():
    if os.name == 'nt':
        return
    if os.getuid() != 0:
        print('Insufficient privileges. Try:\n sudo ' + ' '.join(sys.argv))
        sys.exit(1)


def banner(text, ch='=', length=78):
    spaced_text = ' %s ' % text
    banner = spaced_text.center(length, ch)
    return banner

def is_exe(fpath):
    """Returns True if file path is executable, False otherwize
    does not follow symlink
    """
    return os.path.exists(fpath) and os.access(fpath, os.X_OK)

def which(program):
    def ext_candidates(fpath):
        yield fpath
        for ext in os.environ.get("PATHEXT", "").split(os.pathsep):
            yield fpath + ext

    fpath, fname = os.path.split(program)
    if fpath:
        if os.path.isfile(program) and is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            exe_file = os.path.join(path, program)
            for candidate in ext_candidates(exe_file):
                if is_exe(candidate):
                    return candidate

    return None

def justcall(argv=['/bin/false']):
    """subprosses call argv, return (stdout,stderr,returncode)
    """
    if which(argv[0]) is None:
        return ("", "", 1)
    process = Popen(argv, stdout=PIPE, stderr=PIPE, close_fds=close_fds)
    stdout, stderr = process.communicate(input=None)
    if sys.version_info[0] < 3:
        return stdout, stderr, process.returncode
    else:
        return str(stdout, "ascii"), str(stderr, "ascii"), process.returncode

def empty_string(buff):
    b = buff.strip(' ').strip('\n')
    if len(b) == 0:
        return True
    return False

def call(argv=['/bin/false'],
         cache=False,      # serve/don't serve cmd output from cache
         log=None,         # callers should provide there own logger
                           # or we'll have to allocate a generic one

         info=False,       # False: log cmd as debug
                           # True:  log cmd as info

         outlog=False,     # False: discard stdout

         errlog=True,      # False: discard stderr
                           # True:  log stderr as err, warn or info
                           #        depending on err_to_warn and
                           #        err_to_info value

         outdebug=True,    # True:  log.debug cmd stdout
                           # False: skip log.debug stdout

         errdebug=True,    # True:  log.debug cmd stderr
                           # False: skip log.debug stderr
                           #        depending on err_to_warn and
                           #        err_to_info value
         err_to_warn=False,
         err_to_info=False):
    "return(ret, stdout,stderr)"
    if log is None:
        log = logging.getLogger('CALL')
    if not argv or len(argv) == 0:
        return (0, '', '')
    if which(argv[0]) is None:
        log.error("%s does not exist or not in path or is not executable"%
                  argv[0])
        return (1, '', '')
    cmd = ' '.join(argv)
    if info:
        log.info(cmd)
    else:
        log.debug(cmd)
    if not hasattr(rcEnv, "call_cache"):
        rcEnv.call_cache = {}
    if not cache or cmd not in rcEnv.call_cache:
        if not cache:
            log.debug("caching for '%s' explicitely disabled"%cmd)
        elif cmd not in rcEnv.call_cache:
            log.debug("cache miss for '%s'"%cmd)
        process = Popen(argv, stdout=PIPE, stderr=PIPE, close_fds=close_fds)
        buff = process.communicate()
        if sys.version_info[0] >= 3:
            buff = tuple(map(lambda x: str(x, "ascii"), buff))
        ret = process.returncode
        if ret == 0:
            log.debug("store '%s' output in cache"%cmd)
            rcEnv.call_cache[cmd] = buff
        elif cmd in rcEnv.call_cache:
            log.debug("discard '%s' output from cache because ret!=0"%cmd)
            del rcEnv.call_cache[cmd]
        else:
            log.debug("skip store '%s' output in cache because ret!=0"%cmd)
    else:
        log.debug("serve '%s' output from cache"%cmd)
        buff = rcEnv.call_cache[cmd]
        ret = 0
    if not empty_string(buff[1]):
        if err_to_info:
            log.info('stderr:\n' + buff[1])
        elif err_to_warn:
            log.warning('stderr:\n' + buff[1])
        elif errlog:
            if ret != 0:
                log.error('stderr:\n' + buff[1])
            else:
                log.warning('command succesful but stderr:\n' + buff[1])
        elif errdebug:
            log.debug('stderr:\n' + buff[1])
    if not empty_string(buff[0]):
        if outlog:
            if ret == 0:
                log.info('output:\n' + buff[0])
            elif err_to_info:
                log.info('command failed with stdout:\n' + buff[0])
            elif err_to_warn:
                log.warning('command failed with stdout:\n' + buff[0])
            else:
                log.error('command failed with stdout:\n' + buff[0])
        elif outdebug:
            log.debug('output:\n' + buff[0])

    return (ret, buff[0], buff[1])

def qcall(argv=['/bin/false']) :
    """qcall Launch Popen it args disgarding output and stderr"""
    if not argv:
        return (0, '')
    process = Popen(argv, stdout=PIPE, stderr=PIPE, close_fds=close_fds)
    process.wait()
    return process.returncode

def vcall(argv=['/bin/false'],
          log=None,
          err_to_warn=False,
          err_to_info=False ):
    return call(argv,
                log=log,
                info=True,
                outlog=True,
                err_to_warn=err_to_warn,
                err_to_info=err_to_info)

def getmount(path):
    path = os.path.abspath(path)
    while path != os.path.sep:
        if not os.path.islink(path) and os.path.ismount(path):
            return path
        path = os.path.abspath(os.path.join(path, os.pardir))
    return path

def protected_mount(path):
    if getmount(path) in ['/', '/usr', '/var', '/sys', '/proc', '/tmp', '/opt', '/dev', '/dev/pts', '/home', '/boot', '/dev/shm']:
        return True
    return False

def printplus(obj):
    """
    Pretty-prints the object passed in.

    """
    # Dict
    if isinstance(obj, dict):
        for k, v in sorted(obj.items()):
            print(u'{0}: {1}'.format(k, v))

    # List or tuple            
    elif isinstance(obj, list) or isinstance(obj, tuple):
        for x in obj:
            print(x)

    # Other
    else:
        print(obj)

def cmdline2list(cmdline):
    """
    Translate a command line string into a list of arguments, using
    using the same rules as the MS C runtime:

    1) Arguments are delimited by white space, which is either a
       space or a tab.
    
    2) A string surrounded by double quotation marks is
       interpreted as a single argument, regardless of white space
       contained within.  A quoted string can be embedded in an
       argument.

    3) A double quotation mark preceded by a backslash is
       interpreted as a literal double quotation mark.

    4) Backslashes are interpreted literally, unless they
       immediately precede a double quotation mark.

    5) If backslashes immediately precede a double quotation mark,
       every pair of backslashes is interpreted as a literal
       backslash.  If the number of backslashes is odd, the last
       backslash escapes the next double quotation mark as
       described in rule 3.
    """

    # See
    # http://msdn.microsoft.com/library/en-us/vccelng/htm/progs_12.asp

    # Step 1: Translate all literal quotes into QUOTE.  Justify number
    # of backspaces before quotes.
    tokens = []
    bs_buf = ""
    QUOTE = 1 # \", literal quote
    for c in cmdline:
        if c == '\\':
            bs_buf += c
        elif c == '"' and bs_buf:
            # A quote preceded by some number of backslashes.
            num_bs = len(bs_buf)
            tokens.extend(["\\"] * (num_bs//2))
            bs_buf = ""
            if num_bs % 2:
                # Odd.  Quote should be placed literally in array
                tokens.append(QUOTE)
            else:
                # Even.  This quote serves as a string delimiter
                tokens.append('"')

        else:
            # Normal character (or quote without any preceding
            # backslashes)
            if bs_buf:
                # We have backspaces in buffer.  Output these.
                tokens.extend(list(bs_buf))
                bs_buf = ""

            tokens.append(c)

    # Step 2: split into arguments
    result = [] # Array of strings
    quoted = False
    arg = [] # Current argument
    tokens.append(" ")
    for c in tokens:
        if c == '"':
            # Toggle quote status
            quoted = not quoted
        elif c == QUOTE:
            arg.append('"')
        elif c in (' ', '\t'):
            if quoted:
                arg.append(c)
            else:
                # End of argument.  Output, if anything.
                if arg:
                    result.append(''.join(arg))
                    arg = []
        else:
            # Normal character
            arg.append(c)

    return result


if __name__ == "__main__":
    print("call(('id','-a'))")
    (r,output,err)=call(("/usr/bin/id","-a"))
    print("status: ", r, "output:", output)

  0707010000bc0a000081a400000000000000000000000154fc7bfb00000146000000660000000300000000000000000000002700000000root/opt/opensvc/lib/rcUtilitiesAIX.py    import os
import re
from rcUtilities import call

def check_ping(addr, timeout=5, count=1):
    cmd = ['ping', '-c', repr(count),
                   '-w', repr(timeout)]
    if ':' in addr:
        cmd += ['-a', 'inet6']
    cmd += [addr]
    (ret, out, err) = call(cmd)
    if ret == 0:
        return True
    return False

  0707010000bc0b000081a400000000000000000000000154fc7bfb00000178000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/rcUtilitiesDarwin.py from rcUtilities import call, qcall

def check_ping(addr, timeout=5, count=1):
    if ':' in addr:
        ping = 'ping6'
    else:
        ping = 'ping'
    cmd = [ping, '-c', repr(count),
                 '-W', repr(timeout),
                 '-t', repr(timeout),
                 addr]
    (ret, out, err) = call(cmd)
    if ret == 0:
        return True
    return False

0707010000bc0c000081a400000000000000000000000154fc7bfb00000124000000660000000300000000000000000000002b00000000root/opt/opensvc/lib/rcUtilitiesFreeBSD.py    from rcUtilities import call

def check_ping(addr, timeout=5, count=1):
    if ':' in addr:
        cmd = ['ping6']
    else:
        cmd = ['ping', '-W', str(timeout)]
    cmd += ['-c', repr(count), addr]
    (ret, out, err) = call(cmd)
    if ret == 0:
        return True
    return False
0707010000bc0d000081a400000000000000000000000154fc7bfb00000106000000660000000300000000000000000000002900000000root/opt/opensvc/lib/rcUtilitiesHP-UX.py  from rcUtilities import call

def check_ping(addr, timeout=5, count=1):
    cmd = ['ping', addr,
                   '-n', repr(count),
                   '-m', repr(timeout)]
    (ret, out, err) = call(cmd)
    if ret == 0:
        return True
    return False

  0707010000bc16000081a400000000000000000000000154fc7bfb00000fcc000000660000000300000000000000000000001f00000000root/opt/opensvc/lib/rcZone.py    #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2011 Cyril Galibern <cyril.galibern@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os
from rcZfs import Dataset
from rcUtilities import justcall, which

ZONEADM="/usr/sbin/zoneadm"

def is_zone():
    if not which('zonename'):
        return False
    out, err, ret = justcall(['zonename'])
    if ret != 0:
        return False
    if out.strip() == 'global':
        return False
    return True

class Zones(object):
    """Define zones (a.k.a. container) defined on a node"""
    def __init__(self):
        """scan node to get informations about its zones
        """
        self.zone_list = []
        self.zonename2zone = dict()
        self.zonepath2zone = dict()

        cmd = [ZONEADM, 'list', '-cip']
        (out, err, status) = justcall(cmd)
        if status == 0:
            #zoneid:zonename:state:zonepath:uuid:brand:ip-type
            for zoneadm_line in out.split('\n'):
                v = zoneadm_line.split(':')
                if len(v) != 7:
                    continue
                zone = Zone(zoneid=v[0], zonename=v[1], state=v[2],
                            zonepath=v[3], uuid=v[4], brand=v[5], ip_type=v[6])
                self.zonename2zone[zone.zonename] = zone
                self.zonepath2zone[zone.zonepath] = zone
                self.zone_list.append(zone)

    def refresh(self):
        """refresh zones information"""
        self.__init__()

    def zonename_from_zonepath(self, zonepath=None):
        """return zonename associated with zonepath, else return None"""
        if zonepath in self.zonepath2zone:
            return self.zonepath2zone[zonepath]
        else:
            return None

class Zone(object):
    def __init__(self, zoneid=None, zonename=None, state=None, zonepath=None,
                uuid=None, brand=None, ip_type=None ):
        """define Zone object attribute from zoneadm output line
                zoneid:zonename:state:zonepath:uuid:brand:ip-type
        """
        if state is None and zonename is not None:
            self.zonename = zonename
            self.refresh()
        else:
            self.zoneid = zoneid
            self.zonename = zonename
            self.state = state
            self.zonepath = zonepath
            self.uuid = uuid
            self.brand = brand
            self.ip_type = ip_type

    def refresh(self):
        """refresh zone information"""
        cmd = [ZONEADM, '-z', self.zonename, 'list', '-p']
        (out, err, status) = justcall(cmd)
        if status == 0:
            (self.zoneid, self.zonename, self.state, self.zonepath, self.uuid,
                self.brand, self.ip_type ) = out.split('\n')[0].split(':')
        else:
            print("fail to refresh zone informations for zonename", self.zonename)

if __name__ == "__main__":
    zones = Zones()
    print("Detected %s zones on system" % (len(zones.zone_list)))
    for zone in zones.zone_list:
        zonepath = zone.zonepath
        zonename = zone.zonename
        print("zonename=%s zonepath=%s zones.zonepath2zone[%s].zonename=%s" % (
            zonename, zonepath, zonepath, zones.zonepath2zone[zonepath].zonename
            ))
        z = Zone(zonename=zonename)
        print("zone %s : zoneid=%s, zonepath=%s brand=%s" % (zonename, z.zoneid,
                z.zonepath, z.brand))
0707010000bc0e000081a400000000000000000000000154fc7bfb00000c2c000000660000000300000000000000000000002900000000root/opt/opensvc/lib/rcUtilitiesLinux.py  import os
import re
import glob
from rcUtilities import call, qcall, justcall

def major(driver):
    path = os.path.join(os.path.sep, 'proc', 'devices')
    try:
        f = open(path)
    except:
        return -1
    for line in f.readlines():
        words = line.split()
        if len(words) == 2 and words[1] == driver:
            f.close()
            return int(words[0])
    f.close()
    return -1

def get_blockdev_sd_slaves(syspath):
    slaves = set()
    if not os.path.exists(syspath):
        return slaves
    for s in os.listdir(syspath):
        if re.match('^sd[a-z]*', s) is not None:
            slaves.add('/dev/' + s)
            continue
        deeper = os.path.join(syspath, s, 'slaves')
        if os.path.isdir(deeper):
            slaves |= get_blockdev_sd_slaves(deeper)
    return slaves

def check_ping(addr, timeout=5, count=1):
    if ':' in addr:
        ping = 'ping6'
    else:
        ping = 'ping'
    cmd = [ping, '-c', repr(count),
                 '-W', repr(timeout),
                 '-w', repr(timeout),
                 addr]
    out, err,ret = justcall(cmd)
    if ret == 0:
        return True
    return False

def lv_exists(self, device):
    if qcall(['lvs', device]) == 0:
        return True
    return False

def lv_info(self, device):
    (ret, buff, err) = self.call(['lvs', '-o', 'vg_name,lv_name,lv_size', '--noheadings', '--units', 'm', device])
    if ret != 0:
        return (None, None, None)
    info = buff.split()
    if 'M' in info[2]:
        lv_size = float(info[2].split('M')[0])
    elif 'm' in info[2]:
        lv_size = float(info[2].split('m')[0])
    else:
        self.log.error("%s output does not have the expected unit (m or M)"%' '.join(cmd))
        ex.excError
    return (info[0], info[1], lv_size)

def get_partition_parent(dev):
    syspath = '/sys/block/*/' + os.path.basename(dev)
    l = glob.glob(syspath)
    if len(l) == 1:
        return '/dev/'+l[0].split('/')[3]
    return None

def devs_to_disks(self, devs=set([])):
    """ If PV is a device map, replace by its sysfs name (dm-*)
        If device map has slaves, replace by its slaves
    """
    disks = set()
    dm_major = major('device-mapper')
    try: lo_major = major('loop')
    except: lo_major = 0
    for dev in devs:
        try:
            statinfo = os.stat(dev)
        except:
            self.log.error("can not stat %s" % dev)
            raise
        if os.major(statinfo.st_rdev) == dm_major:
            dm = 'dm-' + str(os.minor(statinfo.st_rdev))
            syspath = '/sys/block/' + dm + '/slaves'
            disks |= get_blockdev_sd_slaves(syspath)
        elif lo_major != 0 and os.major(statinfo.st_rdev) == lo_major:
            self.log.debug("skip loop device %s from disklist"%dev)
            pass
        else:
            parent = get_partition_parent(dev)
            if parent is not None:
                disks.add(parent)
            else:
                disks.add(dev)
    _disks = list(disks)
    for i, disk in enumerate(_disks):
        _disks[i] = re.sub("^(/dev/[vhs]d[a-z]*)[0-9]*$", r"\1", disk)
    return disks

0707010000bc0f000081a400000000000000000000000154fc7bfb00000106000000660000000300000000000000000000002800000000root/opt/opensvc/lib/rcUtilitiesOSF1.py   from rcUtilities import call

def check_ping(addr, timeout=5, count=1):
    cmd = ['ping', '-c', repr(count),
                   '-t', repr(timeout),
                   addr]
    (ret, out, err) = call(cmd)
    if ret == 0:
        return True
    return False

  0707010000bc10000081a400000000000000000000000154fc7bfb000004d8000000660000000300000000000000000000002900000000root/opt/opensvc/lib/rcUtilitiesSunOS.py  from rcUtilities import justcall

def check_ping(addr, timeout=5, count=1):
    cmd = ['ping', addr, "%s" % timeout]
    out, err, ret = justcall(cmd)
    if ret == 0:
        return True
    return False

def get_os_ver():
    cmd = ['uname', '-v']
    out, err, ret = justcall(cmd)
    if ret != 0:
        return self.undef
    lines = out.split('\n')
    if len(lines) == 0:
        return self.undef
    try:
        osver = float(lines[0])
    except:
        osver = 0
    return osver

# Solaris 2.6 is SunOS 5.6 : osver = 6.0
# Solaris 7 is SunOS 5.7   : osver = 7.0
# Solaris 8 is SunOS 5.8   : osver = 8.0
# ...
# Solaris 11 is SunOS 5.11 : osver = 11.0
# Solaris 11U1 is SunOS 5.11 : osver = 11.1
# Solaris 11U2 is SunOS 5.11 : osver = 11.2
def get_solaris_version():
    cmd = ['uname', '-r']
    out, err, ret = justcall(cmd)
    if ret != 0:
        return self.undef
    lines = out.split('\n')
    if len(lines) == 0:
        return self.undef
    try:
        base, osver = lines[0].split('.')
	osver = int(osver)
    except:
        osver = 0
    
    if osver >= '11':
	cmd = ['uname', '-v']
	out, err, ret = justcall(cmd)
	if ret == 0:
            lines = out.split('\n')
	    osver = lines[0]

    return float(osver)
0707010000bc11000081a400000000000000000000000154fc7bfb00000304000000660000000300000000000000000000002b00000000root/opt/opensvc/lib/rcUtilitiesWindows.py    import os
import re
from rcUtilities import justcall
import string
from ctypes import windll


def check_ping(addr, timeout=5, count=1):
    ping = 'ping.exe'
    cmd = [ping,
           '-n', repr(count),
           '-w', repr(timeout),
           addr]
    out, err, ret = justcall(cmd)
    if ret == 0:
        return True
    return False

def get_registry_value(key, subkey, value):
    import _winreg
    key = getattr(_winreg, key)
    handle = _winreg.OpenKey(key, subkey)
    (value, type) = _winreg.QueryValueEx(handle, value)
    return value

def get_drives():
    drives = []
    bitmask = windll.kernel32.GetLogicalDrives()
    for letter in string.uppercase:
        if bitmask & 1:
            drives.append(letter)
        bitmask >>= 1
    return drives
0707010000bc12000081a400000000000000000000000154fc7bfb00000df1000000660000000300000000000000000000002400000000root/opt/opensvc/lib/rcVioServer.py   from rcUtilities import justcall, which
import rcExceptions as ex
import os
import ConfigParser

pathlib = os.path.dirname(__file__)
pathbin = os.path.realpath(os.path.join(pathlib, '..', 'bin'))
pathetc = os.path.realpath(os.path.join(pathlib, '..', 'etc'))
pathtmp = os.path.realpath(os.path.join(pathlib, '..', 'tmp'))
if pathbin not in os.environ['PATH']:
    os.environ['PATH'] += ":"+pathbin

def rcmd(cmd, manager, username, key):
    _cmd = ['ssh', '-i', key, '@'.join((username, manager))]
    _cmd += [cmd]
    out, err, ret = justcall(_cmd)
    if ret != 0:
        print(' '.join(_cmd))
        print(out)
        raise ex.excError("ssh command execution error")
    return out, err

class VioServers(object):
    def __init__(self, objects=[]):
        self.objects = []
        if len(objects) > 0:
            self.filtering = True
        else:
            self.filtering = False
        self.arrays = []
        self.index = 0
        cf = os.path.join(pathetc, "auth.conf")
        if not os.path.exists(cf):
            return
        conf = ConfigParser.RawConfigParser()
        conf.read(cf)
        m = {}
        for s in conf.sections():
            if self.filtering and s not in self.objects:
                continue
            if not conf.has_option(s, "type") or \
               conf.get(s, "type") != "vioserver":
                continue
            try:
                username = conf.get(s, 'username')
                key = conf.get(s, 'key')
                m[s] = [username, key]
            except:
                print("error parsing section", s)
                pass
        del(conf)
        for name, creds in m.items():
            username, key = creds
            self.arrays.append(VioServer(name, username, key))

    def __iter__(self):
        return self

    def next(self):
        if self.index == len(self.arrays):
            raise StopIteration
        self.index += 1
        return self.arrays[self.index-1]

class VioServer(object):
    def __init__(self, name, username, key):
        self.name = name
        self.username = username
        self.key = key
        self.keys = ['lsmap', 'bootinfo', 'lsfware', 'lsdevattr', 'lsdevvpd', 'devsize']

    def rcmd(self, cmd):
        return rcmd(cmd, self.name, self.username, self.key)

    def get_lsmap(self):
        cmd = 'ioscli lsmap -all -fmt :'
        print("%s: %s"%(self.name, cmd))
        return self.rcmd(cmd)[0]

    def get_bootinfo(self):
        cmd = 'for i in $(ioscli lsmap -all -field backing|sed "s/Backing device//"); do echo $i $(bootinfo -s $i) ; done'
        print("%s: %s"%(self.name, cmd))
        return self.rcmd(cmd)[0]

    def get_lsfware(self):
        cmd = 'ioscli lsfware'
        print("%s: %s"%(self.name, cmd))
        return self.rcmd(cmd)[0]

    def get_lsdevattr(self):
        cmd = 'for i in $(ioscli lsdev -type disk -field name -fmt .) ; do echo $i $(ioscli lsdev -dev $i -attr|grep ww_name);done'
        print("%s: %s"%(self.name, cmd))
        return self.rcmd(cmd)[0]

    def get_lsdevvpd(self):
        cmd = 'for i in $(ioscli lsdev -type disk -field name -fmt .) ; do echo $i ; ioscli lsdev -dev $i -vpd;done'
        print("%s: %s"%(self.name, cmd))
        return self.rcmd(cmd)[0]

    def get_devsize(self):
        cmd = 'for i in $(ioscli lsdev -type disk -field name -fmt .) ; do echo $i $(bootinfo -s $i);done'
        print("%s: %s"%(self.name, cmd))
        return self.rcmd(cmd)[0]

if __name__ == "__main__":
    o = VioServers()
    for vioserver in o:
        print(vioserver.lsmap())
   0707010000bc13000081a400000000000000000000000154fc7bfb000008c6000000660000000300000000000000000000002400000000root/opt/opensvc/lib/rcWakeOnLan.py   #
# Copyright (c) 2014 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2014 Arnaud Veron <arnaud.veron@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import sys
import socket
import struct
import re

regex_mac = re.compile('[0-9a-f]{2}([-:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$')
regex_broadcast = re.compile('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')

class wolrequest(object):
    def __init__(self, macaddress, broadcast, udpport=7):
        self.mac = macaddress
        self.broadcast = broadcast
        self.udpport = udpport
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

    def check_mac(self):
        if not ':' and not '-' in self.mac: 
            return False
        if regex_mac.match(self.mac.lower()) is None:
            return False
        if ':' in self.mac:
            self.mac = self.mac.replace(':', '')
        if '-' in self.mac:
            self.mac = self.mac.replace('-', '')
        if len(self.mac) != 12:
            return False
        return True

    def check_broadcast(self):
        if regex_broadcast.match(self.broadcast) is None:
            return False
        return True

    def send(self):
        buf = ''.join(['FFFFFFFFFFFF', self.mac * 20])
        payload = ''
        for i in range(0, len(buf), 2):
            payload = ''.join([payload,struct.pack('B', int(buf[i: i + 2], 16))])
        try:
            self.sock.sendto(payload, (self.broadcast, self.udpport))
        except:
            return False
        return True

  0707010000bc14000081ed00000000000000000000000154fc7bfb00000ba3000000660000000300000000000000000000002700000000root/opt/opensvc/lib/rcWinScheduler.py    '''

 Author: Alex Baker
 Date: 7th July 2008
 Description : Simple python program to generate wrap as a service based on example on the web, see link below.

 http://essiene.blogspot.com/2005/04/python-windows-services.html

 Usage : python aservice.py install
 Usage : python aservice.py start
 Usage : python aservice.py stop
 Usage : python aservice.py remove

 C:\>python aservice.py  --username <username> --password <PASSWORD> --startup auto install

'''


import win32service
import win32serviceutil
import win32api
import win32con
import win32event
import win32evtlogutil
import os
import servicemanager

import datetime
from subprocess import *

pathsvc = os.path.join(os.path.dirname(__file__), '..')
nodemgr = os.path.join(pathsvc, "nodemgr.cmd")
svcmon = os.path.join(pathsvc, "svcmon.cmd")
cron = os.path.join(pathsvc, "cron.cmd")

import sys
from rcListener import listener

class OsvcSched(win32serviceutil.ServiceFramework):

    _svc_name_ = "OsvcSched"
    _svc_display_name_ = "OpenSVC job scheduler"
    _svc_description_ = "Schedule the OpenSVC jobs"

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        now = datetime.datetime.now()
        self.next_task10 = now + datetime.timedelta(minutes=1)


    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
	sys.stop_listener = True
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ''))
        a = listener()

        self.timeout = 60000

        while 1:
            # Wait for service stop signal, if I timeout, loop again
            rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
            # Check to see if self.hWaitStop happened
            if rc == win32event.WAIT_OBJECT_0:
                # Stop signal encountered
                servicemanager.LogInfoMsg("%s - STOPPED"%self._svc_name_)
                break
            else:
                #servicemanager.LogInfoMsg("%s - ALIVE"%self._svc_name_)
                self.SvcDoJob()

    def SvcDoJob(self):
        now = datetime.datetime.now()
        if now > self.next_task10:
            self.run_task10()
            self.next_task10 = now + datetime.timedelta(minutes=10)

    def run_task10(self):
        cmd = [svcmon, "--updatedb"]
        servicemanager.LogInfoMsg("run %s" % ' '.join(cmd))
        p = Popen(cmd, stdout=None, stderr=None, stdin=None)
        p.communicate()
        servicemanager.LogInfoMsg("run internal scheduler")
        cmd = [cron]
        p = Popen(cmd, stdout=None, stderr=None, stdin=None)
        p.communicate()

def ctrlHandler(ctrlType):
    return True

if __name__ == '__main__':
    win32api.SetConsoleCtrlHandler(ctrlHandler, True)
    win32serviceutil.HandleCommandLine(OsvcSched)

 0707010000bc17000081a400000000000000000000000154fc7bfb00003366000000660000000300000000000000000000001f00000000root/opt/opensvc/lib/resApp.py    #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from subprocess import *
from datetime import datetime
import os
import glob
import sys
import pwd
import time
import stat

from rcUtilities import justcall, which
from rcGlobalEnv import rcEnv
import resources as Res
import rcStatus
import rcExceptions as ex

try:
    if rcEnv.sysname == "Windows":
        raise
    from multiprocessing import Process, Queue
    mp = True
except:
    mp = False

def run_as_popen_kwargs(fpath):
    if rcEnv.sysname == "Windows":
        return {}
    try:
        st = os.stat(fpath)
    except Exception as e:
        raise ex.excError(str(e))
    cwd = rcEnv.pathtmp
    user_uid = st[stat.ST_UID]
    user_gid = st[stat.ST_GID]
    import pwd
    user_name = pwd.getpwuid(st[stat.ST_UID])[0]
    pw_record = pwd.getpwnam(user_name)
    user_name      = pw_record.pw_name
    user_home_dir  = pw_record.pw_dir
    env = os.environ.copy()
    env['HOME']  = user_home_dir
    env['LOGNAME']  = user_name
    env['PWD']  = cwd
    env['USER']  = user_name
    return {'preexec_fn': demote(user_uid, user_gid), 'cwd': cwd, 'env': env}

def demote(user_uid, user_gid):
    def result():
        os.setgid(user_gid)
        os.setuid(user_uid)
    return result

class StatusWARN(Exception):
    pass

class StatusNA(Exception):
    pass

class RsetApps(Res.ResourceSet):
    def __init__(self,
                 type=None,
                 resources=[],
                 parallel=False,
                 optional=False,
                 disabled=False,
                 tags=set([])):
        Res.ResourceSet.__init__(self,
                                 type=type,
                                 resources=resources,
                                 optional=optional,
                                 disabled=disabled,
                                 parallel=parallel,
                                 tags=tags)
        #
        # this bug should have be fixed, but it is not the case with python
        # 2.6.2 we ship for el5.
        # it manifests as apache failing to spawn workers because they can't
        # acquire stdin, closed upon thread startup.
        #
        sys.stdin = open(os.devnull)

    def action(self, action=None, tags=set([]), xtags=set([])):
        if action == 'start' and self.type == "app":
            import svcBuilder
            svcBuilder.add_apps_sysv(self.svc, self.svc.config)
            self.containerize()

        try:
            Res.ResourceSet.action(self, action=action, tags=tags, xtags=xtags)
        except Exception as e:
            if action == "stop":
                self.log.info("there were errors during app stop. please check the quality of the scripts. continuing anyway.")
                return
            raise

    def sort_resources(self, resources, action):
        if action == "rollback":
            action = "stop"
        attr = action + '_seq'
        l = [r for r in resources if hasattr(r, attr)]
        if len(l) != len(resources):
            attr = 'rid'
        resources.sort(lambda x, y: cmp(getattr(x, attr), getattr(y, attr)))
        return resources

    def containerize(self):
        if not self.svc.containerize:
            return
        try:
            container = __import__('rcContainer'+rcEnv.sysname)
        except ImportError:
            self.log.info("containerization not supported")
            return
        except Exception as e:
            print(e)
            raise
        container.containerize(self)


class App(Res.Resource):
    def __init__(self, rid=None,
                 script=None,
                 start=None,
                 stop=None,
                 check=None,
                 info=None,
                 timeout=None,
                 optional=False,
                 disabled=False,
                 subset=None,
                 tags=set([]),
                 always_on=set([]),
                 monitor=False,
                 restart=0):

        if script is None:
            raise ex.excInitError("script parameter must be defined in resource %s"%rid)

        Res.Resource.__init__(self, rid, "app", optional=optional,
                              subset=subset,
                              disabled=disabled, tags=tags,
                              monitor=monitor, restart=restart)
        self.rset_class = RsetApps
        self.script = script
        self.start_seq = start
        self.stop_seq = stop
        self.check_seq = check
        self.info_seq = info
        self.timeout = timeout
        self.label = os.path.basename(script)
        self.always_on = always_on

        self.script_exec = True

    def __cmp__(self, other):
        if other.start_seq is None:
            return 1
        if self.start_seq is None:
            return 0
        return cmp(self.start_seq, other.start_seq)

    def validate_on_action(self):
        self.validate_script_path()
        self.validate_script_exec()

    def validate_script_exec(self):
        if self.script is None:
            self.script_exec = False
            return
        if which(self.script) is None:
            self.status_log("script %s is not executable" % self.script)
            self.script_exec = False

    def validate_script_path(self):
        if self.script is None:
            return
        if not self.script.startswith('/'):
            self.script = os.path.join(self.svc.initd, self.script)
        if os.path.exists(self.script):
            self.script = os.path.realpath(self.script)
            return
        self.status_log("script %s does not exist" % self.script)
        self.script = None

    def is_up(self):
        if self.script is None:
            raise StatusNA()
        if not os.path.exists(self.script):
            self.status_log("script does not exist")
            raise StatusNA()
        if self.check_seq is None:
            self.status_log("check disabled")
            raise StatusNA()
        r = self.run('status', dedicated_log=False)
        return r

    def info(self):
        self.validate_on_action()
        if self.info_seq is None:
            return []
        l = []
        s = self.run('info', dedicated_log=False, return_out=True)
        if type(s) != str or len(s) == 0:
            l.append([self.svc.svcname, rcEnv.nodename, self.svc.clustertype, self.script, "Error", "info not implemented in launcher"])
            return l
        for line in s.split('\n'):
            if len(line) == 0:
                continue
            v = line.split(":")
            if len(v) < 2:
                l.append([self.svc.svcname, rcEnv.nodename, self.svc.clustertype, self.script, "Error", "parsing: %s"%line])
                continue
            l.append([self.svc.svcname, rcEnv.nodename, self.svc.clustertype, self.script, v[0].strip(), ":".join(v[1:]).strip()])
        return l

    def start(self):
        self.validate_on_action()

        if self.start_seq is None:
            return
        if self.script is None:
            raise ex.excError("script does not exist")

        try:
            status = self.is_up()
        except:
            status = 1

        if status == 0:
            self.log.info("%s is already started" % self.label)
            return

        r = self.run('start')
        if r != 0:
            raise ex.excError()
        self.can_rollback = True

    def stop(self):
        self.validate_on_action()

        if self.stop_seq is None:
            return
        if self.script is None:
            return
        if self.status() == rcStatus.DOWN:
            self.log.info("%s is already stopped" % self.label)
            return
        r = self.run('stop')
        if r != 0:
            raise ex.excError()

    def _status(self, verbose=False):
        self.validate_on_action()

        n_ref_res = len(self.svc.get_resources(['fs', 'ip', 'container', 'share', 'disk']))
        status = self.svc.group_status(excluded_groups=set(["sync", "app", "disk.scsireserv", "hb"]))
        if n_ref_res > 0 and str(status["overall"]) != "up" and len(self.svc.nodes) > 1:
            self.log.debug("abort resApp status because ip+fs status is %s"%status["overall"])
            if verbose: self.status_log("ip+fs status is %s, skip check"%status["overall"])
            return rcStatus.NA

        try:
            r = self.is_up()
        except StatusWARN:
            return rcStatus.WARN
        except StatusNA:
            return rcStatus.NA

        if r == 0:
            return rcStatus.UP
        elif r == 1:
            return rcStatus.DOWN

        self.status_log("check reports errors (%d)"%r)
        return rcStatus.WARN

    def set_executable(self):
        if self.script_exec: 
            return
        if not os.path.exists(self.script):
            return
        self.vcall(['chmod', '+x', self.script])

    def run(self, action, dedicated_log=True, return_out=False):
        if self.script is None:
            return 1

        if not os.path.exists(self.script):
            if action == "start":
                self.log.error("script %s does not exist. can't run %s action" % (self.script, action))
                return 1
            elif action == "stop":
                self.log.info("script %s does not exist. hosting fs might already be down" % self.script)
                return 0
            elif return_out: 
                return 0
            else:
                self.status_log("script %s does not exist" % self.script)
                raise StatusWARN()

        self.set_executable()

        cmd = [self.script, action]
        try:
            if dedicated_log:
                outf = os.path.join(rcEnv.pathtmp, 'svc_'+self.svc.svcname+'_'+os.path.basename(self.script)+'.log')
                f = open(outf, 'w')
                kwargs = {
                  'stdin': None,
                  'stdout': f.fileno(),
                  'stderr': f.fileno(),
                }
                kwargs.update(run_as_popen_kwargs(self.script))
                self.log.info('exec %s as user %s' % (' '.join(cmd), kwargs.get("env").get("LOGNAME")))
                t = datetime.now()
                p = Popen(cmd, **kwargs)
                try:
                    if self.timeout is None:
                        p.communicate()
                    else:
                        for i in range(self.timeout+1):
                            p.poll()
                            if p.returncode is not None:
                                break
                            time.sleep(1)
                        if p.returncode is None:
                            self.log.error("execution timeout (%d seconds)"%self.timeout)
                            p.terminate()
                            return 1
                        p.communicate()
                except (KeyboardInterrupt, ex.excSignal):
                    _len = datetime.now() - t
                    self.log.error('%s interrupted after %s - ret %d - logs in %s' % (action, _len, 1, outf))
                    f.close()
                    return 1
                _len = datetime.now() - t
                msg = '%s done in %s - ret %d - logs in %s' % (action, _len, p.returncode, outf)
                if p.returncode == 0:
                    self.log.info(msg)
                else:
                    self.log.error(msg)
                f.close()
                return p.returncode
            elif return_out:
                (out, err, ret) = justcall(cmd)
                if ret != 0:
                    return "Error: info not implemented in launcher"
                return out
            else:
                (out, err, ret) = justcall(cmd)
                self.log.debug("%s returned out=[%s], err=[%s], ret=[%d]"%(cmd, out, err, ret))
                return ret
        except OSError as e:
            if e.errno == 8:
                if not return_out and not dedicated_log:
                    self.status_log("exec format error")
                    raise StatusWARN()
                else:
                    self.log.error("%s execution error (Exec format error)"%self.script)
                return 1
            else:
                self.svc.save_exc()
                return 1
        except:
           self.svc.save_exc()
           return 1


if __name__ == "__main__":
    for c in (Apps,) :
        help(c)
  0707010000bc18000081a400000000000000000000000154fc7bfb00000fe4000000660000000300000000000000000000002100000000root/opt/opensvc/lib/resAppVm.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from subprocess import *
import os
import glob

from rcGlobalEnv import rcEnv
from rcUtilities import qcall, justcall
import resApp
import rcStatus
import rcExceptions as ex

class Apps(resApp.Apps):
    app_d = os.path.join(os.sep, 'svc', 'etc', 'init.d')

    def set_perms(self, rc):
        (ret, out, err) = self.call(self.prefix+['/usr/bin/find',
                                            self.app_d,
                                            '-name', os.path.basename(rc),
                                            '-a', '-user', 'root',
                                            '-a', '-group', 'root'])
        if len(out) == 0 or rc != out.split()[0]:
            self.vcall(self.prefix+['chown', 'root:root', rc])
        (ret, out, err) = self.call(self.prefix+['test', '-x', rc])
        if ret != 0:
            self.vcall(self.prefix+['chmod', '+x', rc])

    def check_reachable(self, container):
        cmd = self.prefix + ['/bin/pwd']
        ret = qcall(cmd)
        if ret != 0:
            return False
        return True

    def checks(self, verbose=False):
        container = self.svc.resources_by_id["container"]
        if container.guestos == 'Windows':
            raise ex.excNotAvailable
        if container.status(refresh=True) != rcStatus.UP:
            self.log.debug("abort resApp action because container status is %s"%rcStatus.status_str(container.status()))
            self.status_log("container is %s"%rcStatus.status_str(container.status()))
            raise ex.excNotAvailable
        if not self.check_reachable(container):
            self.log.debug("abort resApp action because container is unreachable")
            self.status_log("container is unreachable")
            return False
        cmd = self.prefix + ['test', '-d', self.app_d]
        ret = qcall(cmd)
        if ret == 0:
            return True
        cmd = self.prefix + ['/bin/mkdir', '-p', self.app_d]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            return False
        return True

    def stop_checks(self):
        return self.checks()

    def start_checks(self):
        return self.checks()

    def status_checks(self, verbose=False):
        return self.checks(verbose=verbose)

    def sorted_app_list(self, pattern):
        cmd = self.prefix + ['/usr/bin/find', self.app_d, '-name', pattern.replace('*', '\*')]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
        buff = p.communicate()
        if p.returncode != 0:
            self.log.debug("failed to fetch container startup scripts list")
            return []

        l = buff[0].split('\n')

        # most unix find commands don't support maxdepth.
        # discard manually the startup scripts found in subdirs of app_d
        n = self.app_d.count("/")
        if not self.app_d.endswith("/"):
            n += 1
        l = [e for e in l if e.count("/") == n]

        return sorted(l)

    def app_exist(self, name):
        """ verify app_exists inside Vm
        """
        (out, err, ret) = justcall (self.prefix + ['/bin/ls', '-Ld', name ])
        if ret == 0:
            return True
        else:
            return False

if __name__ == "__main__":
    for c in (Apps,) :
        help(c)
0707010000bc19000081a400000000000000000000000154fc7bfb000023a6000000660000000300000000000000000000002500000000root/opt/opensvc/lib/resContainer.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import rcStatus
import resources as Res
import time
import rcExceptions as ex
from rcUtilities import justcall
from rcGlobalEnv import rcEnv
import socket
from subprocess import *

class Container(Res.Resource):
    """ in seconds
    """
    startup_timeout = 600
    shutdown_timeout = 60

    def __init__(self,
                 rid,
                 name,
                 guestos=None,
                 type=None,
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        Res.Resource.__init__(self,
                              rid=rid,
                              type=type,
                              optional=optional,
                              disabled=disabled,
                              monitor=monitor,
                              restart=restart,
                              subset=subset,
                              tags=tags)
        self.sshbin = '/usr/bin/ssh'
        self.name = name
        self.label = name
        self.always_on = always_on
        self.guestos = guestos
        if guestos is not None:
            self.guestos = guestos.lower()
        if self.guestos != "windows":
            self.runmethod = rcEnv.rsh.split() + [name]
        self.booted = False

    def vm_hostname(self):
        if hasattr(self, 'vmhostname'):
            return self.vmhostname
        if self.guestos == "windows":
            self.vmhostname = self.name
            return self.vmhostname
        cmd = self.runmethod + ['hostname']
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            self.vmhostname = self.name
        else:
            self.vmhostname = out.strip()
        return self.vmhostname

    def getaddr(self):
        if hasattr(self, 'addr'):
            return
        if len(self.name) == 0:
            # explicitely disabled (ex: docker)
            return
        try:
            a = socket.getaddrinfo(self.name, None)
            if len(a) == 0:
                raise Exception
            self.addr = a[0][4][0]
        except:
            if not disabled:
                raise ex.excInitError

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def operational(self):
        if self.guestos == "windows":
            """ Windows has no sshd.
            """
            return True
        timeout = 1
        if 'ssh' in self.runmethod[0]:
            cmd = [ self.sshbin, '-o', 'StrictHostKeyChecking=no',
                                 '-o', 'ForwardX11=no',
                                 '-o', 'BatchMode=yes',
                                 '-n',
                                 '-o', 'ConnectTimeout='+repr(timeout),
                                  self.name, 'pwd']
        else:
            cmd = self.runmethod + ['pwd']
        out, err, ret = justcall(cmd)
        if ret == 0:
            return True
        return False

    def wait_for_fn(self, fn, tmo, delay):
        for tick in range(tmo//2):
            if fn():
                return
            time.sleep(delay)
        self.log.error("Waited too long for startup")
        raise ex.excError

    def wait_for_startup(self):
        self.log.info("wait for container up status")
        self.wait_for_fn(self.is_up, self.startup_timeout, 2)
        if hasattr(self, 'ping'):
            self.log.info("wait for container ping")
            self.wait_for_fn(self.ping, self.startup_timeout, 2)
        self.log.info("wait for container operational")
        self.wait_for_fn(self.operational, self.startup_timeout, 2)

    def wait_for_shutdown(self):
        self.log.info("wait for container down status")
        for tick in range(self.shutdown_timeout):
            if self.is_down():
                return
            time.sleep(1)
        self.log.error("Waited too long for shutdown")
        raise ex.excError

    def install_drp_flag(self):
        print("TODO: install_drp_flag()")

    def where_up(self):
        """ returns None if the vm is not found running anywhere
            or returns the nodename where the vm is found running
        """
        if self.is_up():
            return rcEnv.nodename
        if not hasattr(self, "is_up_on"):
            # to implement in Container child class
            return
        if rcEnv.nodename in self.svc.nodes:
            nodes = self.svc.nodes - set([rcEnv.nodename])
        elif rcEnv.nodename in self.svc.drpnodes:
            nodes = self.svc.drpnodes - set([rcEnv.nodename])
        else:
            nodes = []
        if len(nodes) == 0:
            return
        for node in nodes:
            if self.is_up_on(node):
                return node
        return

    def abort_start_ping(self):
        if len(self.name) == 0:
            # docker container for exemple
            return False
        try:
            self.getaddr()
            if not hasattr(self, 'addr'):
                Container.getaddr(self)
            if not hasattr(self, 'addr'):
                raise ex.excError()
            u = __import__("rcUtilities"+rcEnv.sysname)
            ping = u.check_ping
            self.log.info("test %s ip %s availability"%(self.name, self.addr))
            if ping(self.addr):
                return True
        except:
            self.log.info("could not resolve %s to an ip address. skip ip availability test."%self.name)

        return False

    def abort_start(self):
        if self.is_up():
            return False

        if self.abort_start_ping():
            return True

        nodename = self.where_up()
        if nodename is not None and nodename != rcEnv.nodename:
            return True

        return False

    def start(self):
        try:
            self.getaddr()
        except:
            self.log.error("could not resolve %s to an ip address"%self.name)
            raise ex.excError
        where = self.where_up()
        if where is not None:
            self.log.info("container %s already started on %s" % (self.label, where))
            return
        if rcEnv.nodename in self.svc.drpnodes:
            self.install_drp_flag()
        self.container_start()
        self.can_rollback = True
        self.wait_for_startup()
        self.booted = True

    def stop(self):
        try:
            self.getaddr()
        except:
            self.log.error("could not resolve %s to an ip address"%self.name)
            raise ex.excError
        if self.is_down():
            self.log.info("container %s already stopped" % self.label)
            return
        self.container_stop()
        try:
            self.wait_for_shutdown()
        except ex.excError:
            self.container_forcestop()
            self.wait_for_shutdown()

    def check_capabilities(self):
        #print("TODO: check_capabilities(self)")
        pass

    def container_start(self):
        print("TODO: container_start(self)")

    def container_stop(self):
        print("TODO: container_stop(self)")

    def container_forcestop(self):
        print("TODO: container_forcestop(self)")

    def check_manual_boot(self):
        print("TODO: check_manual_boot(self)")
        return False

    def is_up(self):
        return False

    def is_down(self):
        return not self.is_up()

    def _status(self, verbose=False):
        try:
            self.getaddr()
        except:
            self.status_log("could not resolve %s to an ip address"%self.name)
            return rcStatus.WARN
        if not self.check_capabilities():
            self.status_log("node capabilities do not permit this action")
            return rcStatus.WARN
        if not self.check_manual_boot():
            self.status_log("container auto boot is on")
            return rcStatus.WARN
        if self.is_up():
            return rcStatus.UP
        if self.is_down():
            return rcStatus.DOWN
        else:
            self.status_log("container status is neither up nor down")
            return rcStatus.WARN

    def get_container_info(self):
        print("TODO: get_container_info(self)")
        return {'vcpus': '0', 'vmem': '0'}
  0707010000bc1a000081a400000000000000000000000154fc7bfb00002f28000000660000000300000000000000000000002b00000000root/opt/opensvc/lib/resContainerAmazon.py    #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import rcStatus
import resources as Res
import time
import os
import rcExceptions as ex
from rcGlobalEnv import rcEnv
from rcUtilities import justcall
from rcUtilitiesLinux import check_ping
import resContainer

class CloudVm(resContainer.Container):
    startup_timeout = 240
    shutdown_timeout = 120
    save_timeout = 240

    def __init__(self,
                 rid,
                 name,
                 guestos=None,
                 cloud_id=None,
                 image_id=None,
                 size="t2.micro",
                 key_name=None,
                 subnet=None,
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name=name,
                                        type="container.amazon",
                                        guestos=guestos,
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=restart,
                                        subset=subset,
                                        tags=tags,
                                        always_on=always_on)
        self.cloud_id = cloud_id
        self.save_name = name + '.save'
        self.size_id = size
        self.image_id = image_id
        self.key_name = key_name
        self.subnet_name = subnet
        self.addr = None

    def keyfile(self):
        kf = [os.path.join(rcEnv.pathetc, self.key_name+'.pem'),
              os.path.join(rcEnv.pathetc, self.key_name+'.pub'),
              os.path.join(rcEnv.pathvar, self.key_name+'.pem'),
              os.path.join(rcEnv.pathvar, self.key_name+'.pub')]
        for k in kf:
            if os.path.exists(k):
                return k
        raise ex.excError("key file for key name '%s' not found"%self.key_name)

    def rcp_from(self, src, dst):
        if self.guestos == "Windows":
            """ Windows has no sshd.
            """
            raise ex.excNotSupported("remote copy not supported on Windows")

        self.getaddr()
        if self.addr is None:
            raise ex.excError('no usable ip to send files to')

        timeout = 5
        cmd = [ 'scp', '-o', 'StrictHostKeyChecking=no',
                       '-o', 'ConnectTimeout='+str(timeout),
                       '-i', self.keyfile(),
                        self.addr+':'+src, dst]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def rcp(self, src, dst):
        if self.guestos == "Windows":
            """ Windows has no sshd.
            """
            raise ex.excNotSupported("remote copy not supported on Windows")

        self.getaddr()
        if self.addr is None:
            raise ex.excError('no usable ip to send files to')

        timeout = 5
        cmd = [ 'scp', '-o', 'StrictHostKeyChecking=no',
                       '-o', 'ConnectTimeout='+str(timeout),
                       '-i', self.keyfile(),
                        src, self.addr+':'+dst]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def rcmd(self, cmd):
        if self.guestos == "Windows":
            """ Windows has no sshd.
            """
            raise ex.excNotSupported("remote commands not supported on Windows")

        self.getaddr()
        if self.addr is None:
            raise ex.excError('no usable ip to send command to')

        if type(cmd) == str:
            cmd = cmd.split(" ")

        timeout = 5
        cmd = [ 'ssh', '-o', 'StrictHostKeyChecking=no',
                       '-o', 'ForwardX11=no',
                       '-o', 'BatchMode=yes',
                       '-n',
                       '-o', 'ConnectTimeout='+str(timeout),
                       '-i', self.keyfile(),
                        self.addr] + cmd
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def get_subnet(self):
        c = self.get_cloud()
        for subnet in c.driver.ex_list_subnets():
            if subnet.name == self.subnet_name:
                return subnet
        raise ex.excError("%s subnet not found"%self.subnet_name)

    def get_size(self):
        c = self.get_cloud()
        for size in c.driver.list_sizes():
            if size.id == self.size_id:
                return size
        raise ex.excError("%s size not found"%self.size_name)

    def get_cloud(self):
        if hasattr(self, 'cloud'):
            return self.cloud
        c = self.svc.node.cloud_get(self.cloud_id)
        self.cloud = c
        return self.cloud

    def get_node(self):
        c = self.get_cloud()
        l = c.list_nodes()
        for n in l:
            if n.name == self.name:
                return n
        return

    def get_image(self, image_id):
        c = self.get_cloud()
        l = c.driver.list_images(ex_image_ids=[image_id])
        d = {}
        for image in l:
            if image.id == image_id:
                # exact match
                return image
        raise ex.excError("image %s not found" % image_id)

    def has_image(self, image_id):
        c = self.get_cloud()
        l = c.driver.list_images([image_id])
        for image in l:
            if image.id == image_id:
                return True
        return False

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def getaddr(self):
        if self.addr is not None:
            return
        n = self.get_node()
        if n is None:
            raise ex.excError("could not get node details")
        ips = set(n.public_ips+n.private_ips)
        if len(ips) == 0:
            return 0

        # find first pinging ip
        for ip in ips:
            if check_ping(ip, timeout=1, count=1):
                self.addr = ip
                break

        return 0

    def files_to_sync(self):
        return []

    def check_capabilities(self):
        return True

    def ping(self):
        if self.addr is None:
            return 0
        return check_ping(self.addr, timeout=1, count=1)

    def start(self):
        if self.is_up():
            self.log.info("container %s already started" % self.name)
            return
        if rcEnv.nodename in self.svc.drpnodes:
            self.install_drp_flag()
        self.container_start()
        self.can_rollback = True
        self.wait_for_startup()

    def container_start(self):
        """
	    RUNNING = 0
	    REBOOTING = 1
	    TERMINATED = 2
	    PENDING = 3
	    UNKNOWN = 4
	    STOPPED = 5
	    SUSPENDED = 6
	    ERROR = 7
	    PAUSED = 8
        """
        from libcloud.compute.types import NodeState
        n = self.get_node()
        if n is None:
            self.provision()
            return
        elif n.state == NodeState().RUNNING:
            self.log.info("already running")
            return
        elif n.state == NodeState().PENDING:
            self.log.info("already pending. wait for running state.")
            self.wait_for_fn(self.is_up, self.startup_timeout, 5)
            return
        elif n.state == NodeState().REBOOTING:
            self.log.info("currently rebooting. wait for running state.")
            self.wait_for_fn(self.is_up, self.startup_timeout, 5)
            return
        elif n.state == NodeState().STOPPED:
            c = self.get_cloud()
            self.log.info("starting ebs ec2 instance through aws")
            c.driver.ex_start_node(n)
            self.log.info("wait for container up status")
            self.wait_for_fn(self.is_up, self.startup_timeout, 5)
            return
        raise ex.excError("don't know what to do with node in state: %s"%NodeState().tostring(n.state))

    def container_reboot(self):
        c = self.get_cloud()
        n = self.get_node()
        try:
            c.driver.reboot_node(n)
        except Exception as e:
            raise ex.excError(str(e))

    def wait_for_startup(self):
        pass

    def stop(self):
        if self.is_down():
            self.log.info("container %s already stopped" % self.name)
            return
        try:
            self.container_stop()
            self.wait_for_shutdown()
        except ex.excError:
            self.container_forcestop()
            self.wait_for_shutdown()

    def container_stop(self):
        cmd = "shutdown -h now"
        self.log.info("remote command: %s"%cmd)
        self.rcmd(cmd)

    def container_forcestop(self):
        c = self.get_cloud()
        n = self.get_node()
        self.log.info("stopping ebs ec2 instance through aws")
        c.driver.ex_stop_node(n)

    def print_obj(self, n):
        for k in dir(n):
            if '__' in k:
                continue
            print(k, "=", getattr(n, k))

    def is_up(self):
        from libcloud.compute.types import NodeState
        n = self.get_node()
        if n is not None and n.state == NodeState().RUNNING:
            return True
        if n is None:
            self.status_log("state:unknown")
        else:
            self.status_log("state:"+NodeState().tostring(n.state))
        return False

    def get_container_info(self):
        self.info = {'vcpus': '0', 'vmem': '0'}
        c = self.get_cloud()
        n = self.get_node()
        try:
            size = c.driver.ex_get_size(n.extra['flavorId'])
            self.info['vmem'] = str(size.ram)
        except:
            pass
        return self.info

    def check_manual_boot(self):
        return True

    def install_drp_flag(self):
        pass

    def provision(self):
        prereq = True
        if self.image_id is None:
            self.log.error("the image keyword is mandatory for the provision action")
            prereq &= False
        if self.size_id is None:
            self.log.error("the size keyword is mandatory for the provision action")
            prereq &= False
        if self.subnet_name is None:
            self.log.error("the subnet keyword is mandatory for the provision action")
            prereq &= False
        if self.key_name is None:
            self.log.error("the key_name keyword is mandatory for the provision action")
            prereq &= False
        if not prereq:
            raise ex.excError()

        c = self.get_cloud()
        image = self.get_image(self.image_id)
        size = self.get_size()
        subnet = self.get_subnet()
        self.log.info("create instance %s, size %s, image %s, key %s, subnet %s"%(self.name, size.name, image.name, self.key_name, subnet.name))
        c.driver.create_node(name=self.name, size=size, image=image, ex_keyname=self.key_name, ex_subnet=subnet)
        self.log.info("wait for container up status")
        self.wait_for_fn(self.is_up, self.startup_timeout, 5)

0707010000bc1b000081a400000000000000000000000154fc7bfb00001b3b000000660000000300000000000000000000002b00000000root/opt/opensvc/lib/resContainerDocker.py    #
# Copyright (c) 2014 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2014 Arnaud Veron <arnaud.veron@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
from datetime import datetime
from subprocess import *

import sys
import rcStatus
import rcDocker
import json
import re
import resources as Res
from rcUtilitiesLinux import check_ping
from rcUtilities import which, justcall
from rcGlobalEnv import rcEnv
import resContainer
import rcExceptions as ex

from svcBuilder import conf_get_string_scope

os.environ['LANG'] = 'C'

class Docker(resContainer.Container, rcDocker.DockerLib):

    def files_to_sync(self):
        return []

    def operational(self):
        return True

    def rcp_from(self, src, dst):
        rootfs = self.get_rootfs()
        if len(rootfs) == 0:
            raise ex.excError()
        src = rootfs + src
        cmd = ['cp', src, dst]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def rcp(self, src, dst):
        rootfs = self.get_rootfs()
        if len(rootfs) == 0:
            raise ex.excError()
        dst = rootfs + dst
        cmd = ['cp', src, dst]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def docker_data_dir_resource(self):
        mntpts = []
        mntpt_res = {}
        for resource in self.svc.get_resources('fs'):
            mntpts.append(resource.mountPoint)
            mntpt_res[resource.mountPoint] = resource
        for mntpt in sorted(mntpts, reverse=True):
            if mntpt.startswith(self.docker_data_dir):
                return mntpt_res[mntpt]

    def add_run_args(self):
        if self.run_args is None:
            return []
        l = self.run_args.split()
        for e, i in enumerate(l):
            if e != '-p':
                continue
            if len(l) < i + 2:
                # bad
                break
            volarg = l[i+1]
            if ':' in volarg:
                # mapping ... check source dir presence
                v = volarg.split(':')
                if len(v) != 3:
                    raise ex.excError("mapping %s should be formatted as <src>:<dst>:<ro|rw>" % (volarg))
                if not os.path.exists(v[0]):
                    raise ex.excError("source dir of mapping %s does not exist" % (volarg))
        return l

    def container_start(self):
        self.docker_start()
        self.docker('start')

    def container_stop(self):
        self.docker('stop')

    def stop(self):
        resContainer.Container.stop(self)
        self.docker_stop()
 
    def _status(self, verbose=False):
        s = resContainer.Container._status(self, verbose)
        try:
            inspect = self.docker_inspect(self.container_id)
        except Exception as e:
            return s
        running_image_id = str(inspect['Image'][:12])
        run_image_id = self.get_run_image_id()

        if run_image_id != running_image_id:
            self.status_log("the running container is based on image '%s' instead of '%s'"%(running_image_id, run_image_id))
            s = rcStatus._merge(s, rcStatus.WARN)

        return s

    def container_forcestop(self):
        self.docker('kill')

    def _ping(self):
        return check_ping(self.addr, timeout=1)

    def is_up(self, nodename=None):
        if self.docker_data_dir is None:
            self.status_log("DEFAULT.docker_data_dir must be defined")

        if not self.docker_running():
            self.status_log("docker daemon is not running")
            return False

        if self.container_id is None:
            self.status_log("can not find container id")
            return False

        cmd = self.docker_cmd + ['ps', '-q']
        out, err, ret = justcall(cmd)

        if self.container_id in out.replace('\n', ' ').split():
            return True
        return False

    def get_container_info(self):
        cpu_set = self.get_cf_value("lxc.cgroup.cpuset.cpus")
        #d = json.loads(response)
        if cpu_set is None:
            vcpus = 0
        else:
            vcpus = len(cpu_set.split(','))
        return {'vcpus': str(vcpus), 'vmem': '0'}

    def check_manual_boot(self):
        return True

    def check_capabilities(self):
        return True

    def __init__(self,
                 rid,
                 run_image,
                 run_command=None,
                 run_args=None,
                 guestos="Linux",
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name="",
                                        type="container.docker",
                                        guestos=guestos,
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=restart,
                                        subset=subset,
                                        tags=tags,
                                        always_on=always_on)

        self.run_image = run_image
        self.run_command = run_command
        self.run_args = run_args
        self.max_wait_for_dockerd = 5

    def on_add(self):
        self.container_name = self.svc.svcname+'.'+self.rid
        self.container_name = self.container_name.replace('#', '.')
        rcDocker.DockerLib.on_add(self)
        self.label = ""
        try:
            self.container_id = self.get_container_id_by_name()
            self.label += self.container_id + "@"
        except Exception as e:
            self.container_id = None
        self.label += self.image_userfriendly_name()

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def provision(self):
        # docker resources are naturally provisioned
        self.start()

 0707010000bc1c000081a400000000000000000000000154fc7bfb00001299000000660000000300000000000000000000002800000000root/opt/opensvc/lib/resContainerEsx.py   #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import resources as Res
import os
import rcExceptions as ex
import resContainer
from rcUtilities import which

rcU = __import__("rcUtilities" + os.uname()[0])

class Esx(resContainer.Container):
    startup_timeout = 180
    shutdown_timeout = 120

    def __init__(self,
                 rid,
                 name,
                 guestos=None,
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name=name,
                                        guestos=guestos,
                                        type="container.esx",
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=restart,
                                        subset=subset,
                                        tags=tags,
                                        always_on=always_on)
        self.vmx = None

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def vmcmd(self, l, verbose=False):
        self.find_vmx()
        if verbose:
            ret, out, err = self.vcall(['vmware-cmd', self.vmx] + l)
        else:
            ret, out, err = self.call(['vmware-cmd', self.vmx] + l)
        return ret, out, err

    def list_conffiles(self):
        return []

    def files_to_sync(self):
        return self.list_conffiles()

    def check_capabilities(self):
        return which('vmware-cmd')

    def ping(self):
        return rcU.check_ping(self.addr, timeout=1, count=1)

    def find_vmx(self):
        if self.vmx is not None:
            return self.vmx
        cmd = ['vmware-cmd', '-l']
        ret, out, err = self.call(cmd)
        if ret != 0:
            raise ex.excError
        l = out.split()
        pattern = '/'+self.name+'.vmx'
        for vmx in l:
            if pattern in vmx:
                self.vmx = vmx
        if self.vmx is None:
            self.log.error("vm %s not found on this node"%self.name)
            raise ex.excError
        return self.vmx

    def _migrate(self):
        print("TODO")
        pass

    def container_start(self):
        cmd = ['start']
        ret, buff, err = self.vmcmd(cmd, verbose=True)
        if ret != 0:
            raise ex.excError

    def container_stop(self):
        cmd = ['stop', 'soft']
        ret, buff, err = self.vmcmd(cmd, verbose=True)
        if ret != 0:
            raise ex.excError

    def container_forcestop(self):
        cmd = ['stop', 'hard']
        ret, buff, err = self.vmcmd(cmd, verbose=True)
        if ret != 0:
            raise ex.excError

    def is_up(self):
        try:
            (ret, out, err) = self.vmcmd(['getstate'])
        except:
            return False
        if ret != 0:
            return False
        l = out.split()
        if len(l) != 3:
            return False
        if l[-1] == 'on':
            return True
        return False

    def getconfig(self, key):
        cmd = ['getconfig', key]
        ret, out, err = self.vmcmd(cmd)
        if ret != 0:
            return None
        l = out.split()
        if len(l) != 3:
            return None
        return l[-1]

    def get_container_info(self):
        self.info = {'vcpus': '0', 'vmem': '0'}
        try:
            self.info['vcpus'] = self.getconfig('numvcpus')
        except:
            self.info['vcpus'] = '0'
        try:
            self.info['vmem'] = self.getconfig('memsize')
        except:
            self.info['vmem'] = '0'
        return self.info           

    def check_manual_boot(self):
        """ ESX will handle the vm startup itself """
        return True
   0707010000bc1d000081a400000000000000000000000154fc7bfb00001511000000660000000300000000000000000000002900000000root/opt/opensvc/lib/resContainerHpVm.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import rcStatus
import resources as Res
import time
import os
import rcExceptions as ex
from rcUtilities import qcall
from rcGlobalEnv import rcEnv
import resContainer
u = __import__('rcUtilitiesHP-UX')
import resVgHpVm

class HpVm(resContainer.Container):
    def __init__(self,
                 rid,
                 name,
                 guestos="HP-UX",
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name=name,
                                        type="container.hpvm",
                                        guestos=guestos,
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=restart,
                                        subset=subset,
                                        tags=tags,
                                        always_on=always_on)
        self.vg = resVgHpVm.Vg(
          rid = 'vmdg#'+self.rid,
          name = 'vmdg_'+self.name,
          container_name = self.name
        )

    def on_add(self):
        self.vg.svc = self.svc

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def files_to_sync(self):
        import glob
        a = self.vg.files_to_sync()
        guest = os.path.join(os.sep, 'var', 'opt', 'hpvm', 'guests', self.name)
        uuid = os.path.realpath(guest)
        share = os.path.join(rcEnv.pathvar, 'vg_'+self.name+'_*.share')
        if os.path.exists(guest):
            a.append(guest)
        if os.path.exists(uuid):
            a.append(uuid)
        files = glob.glob(share)
        if len(files) > 0:
            a += files
        return a

    def ping(self):
        return u.check_ping(self.addr, timeout=1, count=1)

    def container_start(self):
        cmd = ['/opt/hpvm/bin/hpvmstart', '-P', self.name]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def container_stop(self):
        cmd = ['/opt/hpvm/bin/hpvmstop', '-g', '-F', '-P', self.name]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def container_forcestop(self):
        cmd = ['/opt/hpvm/bin/hpvmstop', '-F', '-P', self.name]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def check_manual_boot(self):
        cmd = ['/opt/hpvm/bin/hpvmstatus', '-M', '-P', self.name]
        (ret, out, err) = self.call(cmd, cache=True)
        if ret != 0:
            return False
        if out.split(":")[11] == "Manual":
            return True
        self.log.info("Auto boot should be turned off")
        return False

    def get_container_info(self):
        cmd = ['/opt/hpvm/bin/hpvmstatus', '-M', '-P', self.name]
        (ret, out, err) = self.call(cmd, cache=True)
        self.info = {'vcpus': '0', 'vmem': '0'}
        if ret != 0:
            return self.info
        self.info['vcpus'] = out.split(':')[19].split(';')[0]
        self.info['vmem'] = out.split(':')[20].split(';')[0]
        if 'GB' in self.info['vmem']:
            self.info['vmem'] = str(1024*1024*int(self.info['vmem'].replace('GB','')))
        return self.info

    def is_up_on(self, nodename):
        return self.is_up(nodename)

    def is_up(self, nodename=None):
        cmd = ['/opt/hpvm/bin/hpvmstatus', '-M', '-P', self.name]
        if nodename is not None:
            cmd = rcEnv.rsh.split() + [nodename] + cmd
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            return False
        if out.split(":")[10] == "On":
            return True
        return False

    def check_capabilities(self):
        if os.path.exists('/opt/hpvm/bin/hpvmstatus'):
            return True
        return False

    def _migrate(self):
        cmd = ['hpvmmigrate', '-o', '-P', self.name, '-h', self.svc.destination_node]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def disklist(self):
        return self.vg.disklist()

    def devlist(self):
        return self.vg.devlist()

    def presync(self):
        return self.vg.presync()

    def postsync(self):
        return self.vg.postsync()
   0707010000bc1e000081a400000000000000000000000154fc7bfb000011f5000000660000000300000000000000000000002900000000root/opt/opensvc/lib/resContainerJail.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
from datetime import datetime

import rcStatus
import resources as Res
from rcUtilitiesFreeBSD import check_ping
from rcUtilities import qcall
import resContainer
import rcExceptions as ex

class Jail(resContainer.Container):
    """ jail -c name=jail1
                path=/usr/local/opt/jail1.opensvc.com
                host.hostname=jail1.opensvc.com
                ip4.addr=192.168.0.208
                command=/bin/sh /etc/rc
    """
    def files_to_sync(self):
        return []

    def operational(self):
        return True

    def install_drp_flag(self):
        rootfs = self.jailroot
        flag = os.path.join(rootfs, ".drp_flag")
        self.log.info("install drp flag in container : %s"%flag)
        with open(flag, 'w') as f:
            f.write(' ')
            f.close()

    def container_start(self):
        cmd = ['jail', '-c', 'name='+self.basename, 'path='+self.jailroot,
               'host.hostname='+self.name]
        if len(self.ips) > 0:
            cmd += ['ip4.addr='+','.join(self.ips)]
        if len(self.ip6s) > 0:
            cmd += ['ip6.addr='+','.join(self.ip6s)]
        cmd += ['command=/bin/sh', '/etc/rc']
        self.log.info(' '.join(cmd))
        ret = qcall(cmd)
        if ret != 0:
            raise ex.excError

    def container_stop(self):
        cmd = ['jail', '-r', self.basename]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def container_forcestop(self):
        """ no harder way to stop a lxc container, raise to signal our
            helplessness
        """
        self.log.error("no forced stop method")
        raise ex.excError

    def ping(self):
        return check_ping(self.addr, timeout=1)

    def is_up_on(self, nodename):
        return self.is_up(nodename)

    def is_up(self, nodename=None):
        cmd = ['jls']
        if nodename is not None:
            cmd = rcEnv.rsh.split() + [nodename] + cmd
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError
        for line in out.split('\n'):
            l = line.split()
            if len(l) < 4:
                continue
            if l[2] == self.name:
                return True
        return False

    def get_container_info(self):
        print("TODO: get_container_info()")
        return {'vcpus': '0', 'vmem': '0'}

    def _status(self, verbose=False):
        if self.is_up():
            return rcStatus.UP
        else:
            return rcStatus.DOWN

    def __init__(self,
                 rid,
                 name,
                 guestos="FreeBSD",
                 jailroot="/tmp",
                 ips=[],
                 ip6s=[],
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name=name,
                                        guestos=guestos,
                                        type="container.jail",
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=restart,
                                        subset=subset,
                                        tags=tags,
                                        always_on=always_on)
        self.jailroot = jailroot
        self.ips = ips
        self.ip6s = ip6s

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

   0707010000bc1f000081a400000000000000000000000154fc7bfb00001d4e000000660000000300000000000000000000002800000000root/opt/opensvc/lib/resContainerKvm.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import rcStatus
import resources as Res
import time
import os
import rcExceptions as ex
from rcGlobalEnv import rcEnv
from rcUtilities import qcall
from rcUtilitiesLinux import check_ping
import resContainer

class Kvm(resContainer.Container):
    startup_timeout = 180
    shutdown_timeout = 120

    def __init__(self,
                 rid,
                 name,
                 guestos=None,
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name=name,
                                        type="container.kvm",
                                        guestos=guestos,
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=restart,
                                        subset=subset,
                                        tags=tags,
                                        always_on=always_on)
        self.cf = os.path.join(os.sep, 'etc', 'libvirt', 'qemu', name+'.xml')

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def list_kvmconffiles(self):
        if os.path.exists(self.cf):
            return [self.cf]
        return []

    def files_to_sync(self):
        return self.list_kvmconffiles()

    def check_capabilities(self):
        cmd = ['virsh', 'capabilities']
        (ret, out, err) = self.call(cmd, errlog=False)
        if ret != 0:
            self.status_log("can not fetch capabilities")
            return False
        if 'hvm' not in out:
            self.status_log("hvm not supported by host")
            return False
        return True

    def ping(self):
        return check_ping(self.addr, timeout=1, count=1)

    def container_start(self):
        if not os.path.exists(self.cf):
            self.log.error("%s not found"%self.cf)
            raise ex.excError
        cmd = ['virsh', 'define', self.cf]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        cmd = ['virsh', 'start', self.name]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def container_stop(self):
        cmd = ['virsh', 'shutdown', self.name]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def container_forcestop(self):
        cmd = ['virsh', 'destroy', self.name]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def is_up_on(self, nodename):
        return self.is_up(nodename)

    def is_up(self, nodename=None):
        cmd = ['virsh', 'dominfo', self.name]
        if nodename is not None:
            cmd = rcEnv.rsh.split() + [nodename] + cmd
        (ret, out, err) = self.call(cmd, errlog=False)
        if ret != 0:
            return False
        if "running" in out.split():
            return True
        return False

    def get_container_info(self):
        cmd = ['virsh', 'dominfo', self.name]
        (ret, out, err) = self.call(cmd, errlog=False, cache=True)
        self.info = {'vcpus': '0', 'vmem': '0'}
        if ret != 0:
            return self.info
        for line in out.split('\n'):
            if "CPU(s):" in line: self.info['vcpus'] = line.split(':')[1].strip()
            if "Used memory:" in line: self.info['vmem'] = line.split(':')[1].strip()
        return self.info

    def check_manual_boot(self):
        cf = os.path.join(os.sep, 'etc', 'libvirt', 'qemu', 'autostart', self.name+'.xml')
        if os.path.exists(cf):
            return False
        return True

    def install_drp_flag(self):
        flag_disk_path = os.path.join(rcEnv.pathvar, 'drp_flag.vdisk')

        from xml.etree.ElementTree import ElementTree, SubElement
        tree = ElementTree()
        tree.parse(self.cf)

        """ create the vdisk if it does not exist yet
        """
        if not os.path.exists(flag_disk_path):
            with open(flag_disk_path, 'w') as f:
                f.write('')
                f.close()

        """ check if drp flag is already set up
        """
        for disk in tree.getiterator("disk"):
            e = disk.find('source')
            if e is None:
                continue
            (dev, path) = e.items()[0]
            if path == flag_disk_path:
                self.log.info("flag virtual disk already exists")
                return

        """ add vdisk to the vm xml config
        """
        self.log.info("install drp flag virtual disk")
        devices = tree.find("devices")
        e = SubElement(devices, "disk", {'device': 'disk', 'type': 'file'})
        SubElement(e, "driver", {'name': 'qemu'})
        SubElement(e, "source", {'file': flag_disk_path})
        SubElement(e, "target", {'bus': 'virtio', 'dev': 'vdosvc'})
        tree.write(self.cf)

    def provision(self):
        m = __import__("provKvm")
        prov = m.ProvisioningKvm(self)
        prov.provisioner()

    def devlist(self):
        if hasattr(self, 'devs') and self.devs != set():
            return self.devs

        devmapping = self.devmap()
        self.devs = set(map(lambda x: x[0], devmapping))
        return self.devs

    def disklist(self):
        if hasattr(self, 'disks'):
            return self.disks
        if not hasattr(self, 'devs'):
            self.devlist()
        devps = self.devs
        try:
            u = __import__('rcUtilities'+rcEnv.sysname)
            self.disks = u.devs_to_disks(self, devps)
        except:
            self.disks = devps

        return self.disks

    def devmap(self):
        if hasattr(self, "devmapping"):
            return self.devmapping

        self.devmapping = []

        from xml.etree.ElementTree import ElementTree, SubElement
        tree = ElementTree()
        tree.parse(self.cf)
        for dev in tree.getiterator('disk'):
            s = dev.find('source')
            if s is None:
                 continue
            if 'dev' not in s.attrib:
                 continue
            src = s.attrib['dev']
            s = dev.find('target')
            if s is None:
                 continue
            if 'dev' not in s.attrib:
                 continue
            dst = s.attrib['dev']
            self.devmapping.append((src, dst))
        return self.devmapping


  0707010000bc20000081a400000000000000000000000154fc7bfb000015ef000000660000000300000000000000000000002900000000root/opt/opensvc/lib/resContainerLdom.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import resources as Res
import rcExceptions as ex
from rcUtilities import qcall
from rcUtilitiesSunOS import check_ping
import resContainer
from rcGlobalEnv import rcEnv
import os

class Ldom(resContainer.Container):
    def __init__(self,
                 rid,
                 name,
                 guestos="SunOS",
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name=name,
                                        type="container.ldom",
                                        guestos=guestos,
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=restart,
                                        subset=subset,
                                        tags=tags,
                                        always_on=always_on)
        self.shutdown_timeout = 240
        self.sshbin = '/usr/local/bin/ssh'


    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def files_to_sync(self):
        import glob
        a = []
        ldomf = os.path.join(rcEnv.pathvar, 'ldom_'+self.name+'.*')
        files = glob.glob(ldomf)
        if len(files) > 0:
            a += files
        return a

    def check_capabilities(self):
        cmd = ['/usr/sbin/ldm', 'list' ]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            return False
        return True

    def state(self):
        """ ldm state : None/inactive/bound/active
            ldm list -p domainname outputs:
                VERSION
                DOMAIN|[varname=varvalue]*
        """
        cmd = ['/usr/sbin/ldm', 'list', '-p', self.name]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            return None
        for word in out.split("|"):
            a=word.split('=')
            if len(a) == 2:
                if a[0] == 'state':
                    return a[1]
        return None

    def ping(self):
        return check_ping(self.addr)

    def container_action(self,action):
        cmd = ['/usr/sbin/ldm', action, self.name]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        return None

    def container_start(self):
        """ ldm bind domain
            ldm start domain
        """
        state = self.state()
        if state == 'None':
            raise ex.excError
        if state == 'inactive':
            self.container_action('bind')
            self.container_action('start')
        if state == 'bound' :
            self.container_action('start')

    def container_forcestop(self):
        """ ldm unbind domain
            ldm stop domain
        """
        if self.state == 'active':
            try:
                self.container_action('stop')
            except ex.excError:
                pass
        self.container_action('unbind')

    def container_stop(self):
        """ launch init 5 into container
            wait_for_shutdown
            ldm stop domain
            ldm unbind domain
        """
        state = self.state()
        if state == 'None':
            raise ex.excError
        if state == 'inactive':
            return None
        if state == 'bound' :
            self.container_action('unbind')
        if state == 'active' :
            cmd = rcEnv.rsh.split() + [ self.name, '/usr/sbin/init', '5' ]
            (ret, buff, err) = self.vcall(cmd)
            if ret == 0:
                try:
                    self.log.info("wait for container shutdown")
                    self.wait_for_fn(self.is_shutdown, self.shutdown_timeout, 2)
                except ex.excError:
                    pass
            self.container_forcestop()

    def check_manual_boot(self):
        cmd = ['/usr/sbin/ldm', 'list-variable', 'auto-boot?', self.name]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            return False
        if out != 'auto-boot?=False' :
            return True
        self.log.info("Auto boot should be turned off")
        return False

    def is_shutdown(self):
        state = self.state()
        if state == 'inactive' or state == 'bound':
            return True
        return False

    def is_down(self):
        if self.state() == 'inactive':
            return True
        return False

    def is_up(self):
        if self.state() == 'active':
            return True
        return False

 0707010000bc21000081a400000000000000000000000154fc7bfb00002759000000660000000300000000000000000000002800000000root/opt/opensvc/lib/resContainerLxc.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
from datetime import datetime
from subprocess import *

import sys
import rcStatus
import resources as Res
from rcUtilitiesLinux import check_ping
from rcUtilities import which, justcall
from rcGlobalEnv import rcEnv
import resContainer
import rcExceptions as ex

class Lxc(resContainer.Container):
    """
     container status transition diagram :
       ---------
      | STOPPED |<---------------
       ---------                 |
           |                     |
         start                   |
           |                     |
           V                     |
       ----------                |
      | STARTING |--error-       |
       ----------         |      |
           |              |      |
           V              V      |
       ---------    ----------   |
      | RUNNING |  | ABORTING |  |
       ---------    ----------   |
           |              |      |
      no process          |      |
           |              |      |
           V              |      |
       ----------         |      |
      | STOPPING |<-------       |
       ----------                |
           |                     |
            ---------------------
    """

    def files_to_sync(self):
        # the config file might be in a umounted fs resource
        # in which case, no need to ask for its sync as the sync won't happen
        try:
            self.find_cf()
        except:
            return []
        if self.cf is None:
            return []

        l = [self.cf]
        try:
            fstab = self.get_cf_value("lxc.mount")
            if os.path.exists(fstab):
                l.append(fstab)
        except:
            pass
        return l

    def rcp_from(self, src, dst):
        rootfs = self.get_rootfs()
        if len(rootfs) == 0:
            raise ex.excError()
        src = rootfs + src
        cmd = ['cp', src, dst]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def rcp(self, src, dst):
        rootfs = self.get_rootfs()
        if len(rootfs) == 0:
            raise ex.excError()
        dst = rootfs + dst
        cmd = ['cp', src, dst]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def lxc(self, action):
        self.find_cf()
        outf = '/var/tmp/svc_'+self.name+'_lxc_'+action+'.log'
        if action == 'start':
            cmd = ['lxc-start', '-d', '-n', self.name, '-o', outf, '-f', self.cf]
        elif action == 'stop':
            cmd = ['lxc-stop', '-n', self.name, '-o', outf]
        else:
            self.log.error("unsupported lxc action: %s" % action)
            return 1

        t = datetime.now()
        (ret, out, err) = self.vcall(cmd)
        len = datetime.now() - t
        self.log.info('%s done in %s - ret %i - logs in %s' % (action, len, ret, outf))
        if ret != 0:
            raise ex.excError

    def get_cf_value(self, param):
        self.find_cf()
        value = None
        if not os.path.exists(self.cf):
            return None
        with open(self.cf, 'r') as f:
            for line in f.readlines():
                if param not in line:
                    continue
                if line.strip()[0] == '#':
                    continue
                l = line.replace('\n', '').split('=')
                if len(l) < 2:
                    continue
                if l[0].strip() != param:
                    continue
                value = ' '.join(l[1:]).strip()
                break
        return value

    def get_rootfs(self):
        rootfs = self.get_cf_value("lxc.rootfs")
        if rootfs is None:
            self.log.error("could not determine lxc container rootfs")
            raise ex.excError
        return rootfs

    def install_drp_flag(self):
        rootfs = self.get_rootfs()
        flag = os.path.join(rootfs, ".drp_flag")
        self.log.info("install drp flag in container : %s"%flag)
        with open(flag, 'w') as f:
            f.write(' ')
            f.close()

    def container_start(self):
        self.lxc('start')

    def container_stop(self):
        self.lxc('stop')

    def container_forcestop(self):
        """ no harder way to stop a lxc container, raise to signal our
            helplessness
        """
        raise ex.excError

    def _ping(self):
        return check_ping(self.addr, timeout=1)

    def is_up_on(self, nodename):
        return self.is_up(nodename)

    def is_up(self, nodename=None):
        if which("lxc-ps"):
            return self.is_up_ps(nodename=nodename)
        else:
            return self.is_up_info(nodename=nodename)

    def is_up_info(self, nodename=None):
        cmd = ['lxc-info', '--name', self.name]
        if nodename is not None:
            cmd = rcEnv.rsh.split() + [nodename] + cmd
        out, err, ret = justcall(cmd)
        if ret != 0:
            return False
        if 'RUNNING' in out:
            return True
        return False

    def is_up_ps(self, nodename=None):
        cmd = ['lxc-ps', '--name', self.name]
        if nodename is not None:
            cmd = rcEnv.rsh.split() + [nodename] + cmd
        out, err, ret = justcall(cmd)
        if ret != 0:
            return False
        if self.name in out:
            return True
        return False

    def get_container_info(self):
        cpu_set = self.get_cf_value("lxc.cgroup.cpuset.cpus")
        if cpu_set is None:
            vcpus = 0
        else:
            vcpus = len(cpu_set.split(','))
        return {'vcpus': str(vcpus), 'vmem': '0'}

    def check_manual_boot(self):
        return True

    def check_capabilities(self):
        if not which('lxc-info'):
            self.log.debug("lxc-info is not in PATH")
            return False
        return True

    def find_cf(self):
        if self.cf is not None:
            return

        d_lxc = os.path.join('var', 'lib', 'lxc')

        # seen on debian squeeze : prefix is /usr, but containers'
        # config files paths are /var/lib/lxc/$name/config
        # try prefix first, fallback to other know prefixes
        prefixes = [os.path.join(os.sep),
                    os.path.join(os.sep, 'usr'),
                    os.path.join(os.sep, 'usr', 'local')]
        for prefix in [self.prefix] + [p for p in prefixes if p != self.prefix]:
            cf = os.path.join(prefix, d_lxc, self.name, 'config')
            if os.path.exists(cf):
                cf_d = os.path.dirname(cf)
                if not os.path.exists(cf_d):
                    os.makedirs(cf_d)
                self.cf = cf
                return

        # on Oracle Linux, config is in /etc/lxc
        cf = os.path.join(os.sep, 'etc', 'lxc', self.name, 'config')
        if os.path.exists(cf):
            self.cf = cf
            return

        self.cf = None
        raise ex.excError("unable to find the container configuration file")

    def find_prefix(self):
        prefixes = [os.path.join(os.sep),
                    os.path.join(os.sep, 'usr'),
                    os.path.join(os.sep, 'usr', 'local')]
        for prefix in prefixes:
             if os.path.exists(os.path.join(prefix, 'bin', 'lxc-start')):
                 return prefix
        return None

    def __init__(self,
                 rid,
                 name,
                 guestos="Linux",
                 cf=None,
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name=name,
                                        type="container.lxc",
                                        guestos=guestos,
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=restart,
                                        subset=subset,
                                        tags=tags,
                                        always_on=always_on)

        if which('lxc-attach') and os.path.exists('/proc/1/ns/pid'):
            self.runmethod = ['lxc-attach', '-n', name, '--']
            # override getaddr from parent class with a noop
            self.getaddr = self.dummy
        else:
            self.runmethod = rcEnv.rsh.split() + [name]
            # enable ping test on start
            self.ping = self._ping
        self.cf = cf

    def dummy(self):
        pass

    def on_add(self):
        self.prefix = self.find_prefix()
        if self.prefix is None:
            self.log.error("lxc install prefix not found")
            raise ex.excInitError

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def provision(self):
        m = __import__("provLxc")
        prov = m.ProvisioningLxc(self)
        prov.provisioner()
   0707010000bc2a000081a400000000000000000000000154fc7bfb00000bf1000000660000000300000000000000000000001e00000000root/opt/opensvc/lib/resDg.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@opensvc.com>
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.
"""Module providing Generic device group resources
"""

import resources as Res
import rcStatus
import rcExceptions as exc
from rcGlobalEnv import rcEnv

class Dg(Res.Resource):
    """ basic Dg resource, must be extend for LVM / Veritas / ZFS
    """
    def __init__(self,
                 rid=None,
                 name=None,
                 type=None,
                 always_on=set([]),
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 subset=None,
                 monitor=False,
                 restart=0):
        Res.Resource.__init__(self,
                              rid, type,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset,
                              monitor=monitor,
                              restart=restart,
                              always_on=always_on)
        self.name = name
        self.disks = set()
        self.devs = set()

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def disklist(self):
        return self.disks

    def has_it(self): return False
    def is_up(self): return False
    def do_start(self): return False
    def do_stop(self): return False

    def stop(self):
        self.do_stop()

    def start(self):
        self.do_start()

    def _status(self, verbose=False):
        if rcEnv.nodename in self.always_on:
            if self.is_up(): return rcStatus.STDBY_UP
            else: return rcStatus.STDBY_DOWN
        else:
            if self.is_up(): return rcStatus.UP
            else: return rcStatus.DOWN

if __name__ == "__main__":
    for c in (Dg,) :
        help(c)

    print("""d=Dg("aGenericDg")""")
    d=Dg("aGenericDg")
    print("show d", d)
    print("""d.do_action("start")""")
    d.do_action("start")
    print("""d.do_action("stop")""")
    d.do_action("stop")
   0707010000bc22000081a400000000000000000000000154fc7bfb0000308a000000660000000300000000000000000000002e00000000root/opt/opensvc/lib/resContainerOpenstack.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import rcStatus
import resources as Res
import time
import os
import rcExceptions as ex
from rcGlobalEnv import rcEnv
from rcUtilities import justcall
from rcUtilitiesLinux import check_ping
import resContainer

class CloudVm(resContainer.Container):
    startup_timeout = 240
    shutdown_timeout = 120
    save_timeout = 240

    def __init__(self,
                 rid,
                 name,
                 guestos=None,
                 cloud_id=None,
                 size="tiny",
                 key_name=None,
                 shared_ip_group=None,
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name=name,
                                        type="container.openstack",
                                        guestos=guestos,
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=restart,
                                        subset=subset,
                                        tags=tags,
                                        always_on=always_on)
        self.cloud_id = cloud_id
        self.save_name = name + '.save'
        self.size_name = size
        self.key_name = key_name
        self.shared_ip_group = shared_ip_group
        self.addr = None

    def keyfile(self):
        kf = [os.path.join(rcEnv.pathetc, self.key_name+'.pem'),
              os.path.join(rcEnv.pathetc, self.key_name+'.pub'),
              os.path.join(rcEnv.pathvar, self.key_name+'.pem'),
              os.path.join(rcEnv.pathvar, self.key_name+'.pub')]
        for k in kf:
            if os.path.exists(k):
                return k
        raise ex.excError("key file for key name '%s' not found"%self.key_name)

    def rcp_from(self, src, dst):
        if self.guestos == "Windows":
            """ Windows has no sshd.
            """
            raise ex.excNotSupported("remote copy not supported on Windows")

        self.getaddr()
        if self.addr is None:
            raise ex.excError('no usable public ip to send files to')

        timeout = 5
        cmd = [ 'scp', '-o', 'StrictHostKeyChecking=no',
                       '-o', 'ConnectTimeout='+str(timeout),
                       '-i', self.keyfile(),
                        self.addr+':'+src, dst]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def rcp(self, src, dst):
        if self.guestos == "Windows":
            """ Windows has no sshd.
            """
            raise ex.excNotSupported("remote copy not supported on Windows")

        self.getaddr()
        if self.addr is None:
            raise ex.excError('no usable public ip to send files to')

        timeout = 5
        cmd = [ 'scp', '-o', 'StrictHostKeyChecking=no',
                       '-o', 'ConnectTimeout='+str(timeout),
                       '-i', self.keyfile(),
                        src, self.addr+':'+dst]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def rcmd(self, cmd):
        if self.guestos == "Windows":
            """ Windows has no sshd.
            """
            raise ex.excNotSupported("remote commands not supported on Windows")

        self.getaddr()
        if self.addr is None:
            raise ex.excError('no usable public ip to send command to')

        if type(cmd) == str:
            cmd = cmd.split(" ")

        timeout = 5
        cmd = [ 'ssh', '-o', 'StrictHostKeyChecking=no',
                       '-o', 'ForwardX11=no',
                       '-o', 'BatchMode=yes',
                       '-n',
                       '-o', 'ConnectTimeout='+str(timeout),
                       '-i', self.keyfile(),
                        self.addr] + cmd
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def get_size(self):
        c = self.get_cloud()
        for size in c.driver.list_sizes():
            if size.name == self.size_name:
                return size
        raise ex.excError("%s size not found"%self.size_name)

    def get_cloud(self):
        if hasattr(self, 'cloud'):
            return self.cloud
        c = self.svc.node.cloud_get(self.cloud_id)
        self.cloud = c
        return self.cloud

    def get_node(self):
        c = self.get_cloud()
        l = c.list_nodes()
        for n in l:
            if n.name == self.name:
                return n
        return

    def get_save_name(self):
        import datetime
        now = datetime.datetime.now()
        save_name = self.save_name + now.strftime(".%Y-%m-%d.%H:%M:%S")
        return save_name

    def purge_saves(self):
        c = self.get_cloud()
        l = c.driver.list_images()
        d = {}
        for image in l:
            if image.name.startswith(self.save_name):
                d[image.name] = image
        if len(d) == 0:
             raise ex.excError("no save image found")
        elif len(d) == 1:
             self.log.info("no previous save image to delete")
        for k in sorted(d.keys())[:-1]:
             self.log.info("delete previous save image %s"%d[k].name)
             c.driver.ex_delete_image(d[k])

    def get_last_save(self):
        return self.get_image(self.save_name)

    def get_template(self):
        template = self.svc.config.defaults()['template']
        return self.get_image(template)

    def get_image(self, name):
        c = self.get_cloud()
        l = c.driver.list_images()
        d = {}
        for image in l:
            if image.name == name:
                # exact match
                return image
            elif image.name.startswith(name):
                d[image.name] = image
        if len(d) == 0:
             raise ex.excError("image %s not found"%name)
        for k in sorted(d.keys()):
             last = d[k]
        return last

    def has_image(self, name):
        c = self.get_cloud()
        l = c.driver.list_images()
        for image in l:
            if image.name == name:
                return True
        return False

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def getaddr(self):
        if self.addr is not None:
            return
        n = self.get_node()
        if n is None:
            raise ex.excError("could not get node details")
        ips = set(n.public_ips+n.private_ips)
        if len(ips) == 0:
            return 0

        # find first pinging ip
        for ip in ips:
            if check_ping(ip, timeout=1, count=1):
                self.addr = ip
                break

        return 0

    def files_to_sync(self):
        return []

    def check_capabilities(self):
        return True

    def ping(self):
        if self.addr is None:
            return 0
        return check_ping(self.addr, timeout=1, count=1)

    def start(self):
        if self.is_up():
            self.log.info("container %s already started" % self.name)
            return
        if rcEnv.nodename in self.svc.drpnodes:
            self.install_drp_flag()
        self.container_start()
        self.can_rollback = True
        self.wait_for_startup()

    def container_start(self):
        n = self.get_node()
        if n is not None:
            if n.state == 4:
                self.log.info("reboot %s"%self.name)
                self.container_reboot()
            else:
                raise ex.excError("abort reboot because vm is in state %d (!=4)"%n.state)
        else:
            self.container_restore()

    def container_reboot(self):
        c = self.get_cloud()
        n = self.get_node()
        try:
            c.driver.reboot_node(n)
        except Exception as e:
            raise ex.excError(str(e))

    def container_restore(self):
        c = self.get_cloud()
        image = self.get_last_save()
        size = self.get_size()
        self.log.info("create instance %s, size %s, image %s, key %s"%(self.name, size.name, image.name, self.key_name))
        n = c.driver.create_node(name=self.name, size=size, image=image, ex_keyname=self.key_name, ex_shared_ip_group_id=self.shared_ip_group)
        self.log.info("wait for container up status")
        self.wait_for_fn(self.is_up, self.startup_timeout, 5)
        #n = c.driver.ex_update_node(n, accessIPv4='46.231.128.84')

    def wait_for_startup(self):
        pass

    def stop(self):
        if self.is_down():
            self.log.info("container %s already stopped" % self.name)
            return
        self.container_stop()
        try:
            self.wait_for_shutdown()
        except ex.excError:
            self.container_forcestop()
            self.wait_for_shutdown()

    def container_stop(self):
        cmd = "shutdown -h now"
        self.log.info("remote command: %s"%cmd)
        self.rcmd(cmd)

    def container_forcestop(self):
        c = self.get_cloud()
        n = self.get_node()
        self.container_save()
        c.driver.destroy_node(n)
        self.purge_saves()

    def print_obj(self, n):
        for k in dir(n):
            if '__' in k:
                continue
            print(k, "=", getattr(n, k))

    def container_save(self):
        c = self.get_cloud()
        n = self.get_node()
        save_name = self.get_save_name()
        if self.has_image(save_name):
            return
        #self.print_obj(n)
        if n.state == 9999:
            self.log.info("a save is already in progress")
            return
        self.log.info("save new image %s"%save_name)
        try:
            image = c.driver.ex_save_image(n, save_name)
        except Exception as e:
            raise ex.excError(str(e))
        import time
        delay = 5
        for i in range(self.save_timeout//delay):
            img = c.driver.ex_get_image(image.id)
            if img.extra['status'] != 'SAVING':
                break
            time.sleep(delay)
        if img.extra['status'] != 'ACTIVE':
            raise ex.excError("save failed, image status %s"%img.extra['status'])

    def is_up(self):
        n = self.get_node()
        if n is not None and n.state == 0:
            return True
        return False

    def get_container_info(self):
        self.info = {'vcpus': '0', 'vmem': '0'}
        c = self.get_cloud()
        n = self.get_node()
        try:
            size = c.driver.ex_get_size(n.extra['flavorId'])
            self.info['vmem'] = str(size.ram)
        except:
            pass
        return self.info

    def check_manual_boot(self):
        return True

    def install_drp_flag(self):
        pass

    def provision(self):
        c = self.get_cloud()
        image = self.get_template()
        size = self.get_size()
        self.log.info("create instance %s, size %s, image %s, key %s"%(self.name, size.name, image.name, self.key_name))
        c.driver.create_node(name=self.name, size=size, image=image, ex_keyname=self.key_name, ex_shared_ip_group_id=self.shared_ip_group)
        #self.wait_for_startup()
        self.wait_for_fn(self.is_up, self.startup_timeout, 5)

  0707010000bc23000081a400000000000000000000000154fc7bfb00001a83000000660000000300000000000000000000002800000000root/opt/opensvc/lib/resContainerOvm.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import resources as Res
import os
import rcExceptions as ex
import resContainer
from rcGlobalEnv import rcEnv

rcU = __import__("rcUtilities" + os.uname()[0])

class Ovm(resContainer.Container):
    startup_timeout = 180
    shutdown_timeout = 120

    def __init__(self,
                 rid,
                 name,
                 uuid,
                 guestos=None,
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name=name,
                                        type="container.ovm",
                                        guestos=guestos,
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=restart,
                                        subset=subset,
                                        tags=tags,
                                        always_on=always_on)
        self.uuid = uuid
        self.xen_d = os.path.join(os.sep, 'etc', 'xen')
        self.xen_auto_d = os.path.join(self.xen_d, 'auto')

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def list_conffiles(self):
        cf = os.path.join(self.xen_d, self.uuid)
        if os.path.exists(cf):
            return [cf]
        return []

    def files_to_sync(self):
        return self.list_conffiles()

    def check_capabilities(self):
        cmd = ['xm', 'info']
        (ret, out, err) = self.call(cmd, errlog=False)
        if ret != 0:
            self.status_log("can not fetch xm info")
            return False
        return True

    def ping(self):
        return rcU.check_ping(self.addr, timeout=1, count=1)

    def find_vmcf(self):
        import glob
        l = glob.glob('/OVS/Repositories/*/VirtualMachines/'+self.uuid+'/vm.cfg')+glob.glob(os.path.join(self.xen_d, self.uuid))
        if len(l) > 1:
            self.log.warning("%d configuration files found in repositories (%s)"%(len(l), str(l)))
        elif len(l) == 0:
            raise ex.excError("no configuration file found in repositories")
        return l[0]

    def _migrate(self):
        cmd = ['xm', 'migrate', '-l', self.uuid, self.svc.destination_node]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def container_start(self):
        cf = self.find_vmcf()
        cmd = ['xm', 'create', cf]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def container_stop(self):
        cmd = ['xm', 'shutdown', self.uuid]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def container_forcestop(self):
        cmd = ['xm', 'destroy', self.uuid]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def is_up_on(self, nodename):
        return self.is_up(nodename)

    def is_up(self, nodename=None):
        cmd = ['xm', 'list', '--state=running']
        if nodename is not None:
            cmd = rcEnv.rsh.split() + [nodename] + cmd
        (ret, out, err) = self.call(cmd, errlog=False)
        if ret != 0:
            return False
        for line in out.split('\n'):
            l = line.split()
            if len(l) < 4:
                continue
            if self.uuid == l[0]:
                return True
        return False

    def get_container_info(self):
        cmd = ['xm', 'list', self.uuid]
        (ret, out, err) = self.call(cmd, errlog=False, cache=True)
        self.info = {'vcpus': '0', 'vmem': '0'}
        if ret != 0:
            return self.info
        for line in out.split('\n'):
            l = line.split()
            if len(l) < 4:
                continue
            if self.uuid != l[0]:
                continue
            self.info['vcpus'] = l[3]
            self.info['vmem'] = l[2]
            return self.info           
        self.log.error("malformed 'xm list %s' output: %s"%(self.uuid, line))
        self.info = {'vcpus': '0', 'vmem': '0'}
        return self.info           

    def check_manual_boot(self):
        f = os.path.join(self.xen_auto_d, self.uuid)
        if os.path.exists(f):
            return False
        return True

    def devmap(self):
        if hasattr(self, "devmapping"):
            return self.devmapping

        self.devmapping = []

        cf = self.find_vmcf()
        with open(cf, 'r') as f:
            buff = f.read()

        for line in buff.split('\n'):
            if not line.startswith('disk'):
                continue
            disks = line[line.index('['):]
            if len(line) <= 2:
                break
            disks = disks[1:-1]
            disks = disks.split(', ')
            for disk in disks:
                disk = disk.strip("'")
                d = disk.split(',')
                if not d[0].startswith('phy:'):
                    continue
                l = [d[0].strip('phy:'), d[1]]
                self.devmapping.append(l)
            break

        return self.devmapping

    def devlist(self):
        if hasattr(self, 'devs') and self.devs != set():
            return self.devs
        self.devs = set(map(lambda x: x[0], self.devmap()))
        return self.devs

    def disklist(self):
        if hasattr(self, 'disks') and self.disks != set():
            return self.disks

        self.disks = set()
        devps = self.devlist()

        try:
            u = __import__('rcUtilities'+rcEnv.sysname)
            self.disks = u.devs_to_disks(self, devps)
        except:
            self.disks = devps

        return self.disks
 0707010000bc24000081a400000000000000000000000154fc7bfb00002415000000660000000300000000000000000000002800000000root/opt/opensvc/lib/resContainerSrp.py   #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os

from rcGlobalEnv import rcEnv
import rcStatus
import resources as Res
from rcUtilities import which, qcall, justcall
import resContainer
import rcExceptions as ex

class Srp(resContainer.Container):
    def files_to_sync(self):
        return [self.export_file]

    def get_rootfs(self):
        return self.get_state()['state']

    def rcp_from(self, src, dst):
        rootfs = self.get_rootfs()
        if len(rootfs) == 0:
            raise ex.excError()
        src = rootfs + src
        cmd = ['cp', src, dst]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def rcp(self, src, dst):
        rootfs = self.get_rootfs()
        if len(rootfs) == 0:
            raise ex.excError()
        dst = rootfs + dst
        cmd = ['cp', src, dst]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def install_drp_flag(self):
        rootfs = self.get_rootfs()
        flag = os.path.join(rootfs, ".drp_flag")
        self.log.info("install drp flag in container : %s"%flag)
        with open(flag, 'w') as f:
            f.write(' ')
            f.close()

    def container_start(self):
        cmd = ['srp', '-start', self.name]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError()

    def container_stop(self):
        cmd = ['srp', '-stop', self.name]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError()

    def container_forcestop(self):
        raise ex.excError

    def operational(self):
        cmd = self.runmethod + ['pwd']
        ret = qcall(cmd)
        if ret == 0:
            return True
        return False

    def get_verbose_list(self):
        """
        Name: iisdevs1  Template: system Service: provision ID: 1
        ----------------------------------------------------------------------

        autostart=0
        srp_name=iisdevs1
        ...
        """
        cmd = ['srp', '-list', self.name, '-v']
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("srp -list returned %d:\n%s"%(ret, err))

        data = {}

        words = out.split()
        for i, w in enumerate(words):
            if w == "Service:":
                service = words[i+1]
            if '=' in w:
                key = service + '.' + w[:w.index('=')]
                val = w[w.index('=')+1:]
                data[key] = val
        return data

    def get_verbose_status(self):
        """
        SRP Status:

        ----------------- Status for SRP:iisdevs1 ----------------------
            Status:MAINTENANCE

            Type:system    Subtype:private    Rootpath:/var/hpsrp/iisdevs1

            IP:10.102.184.12      Interface:lan0:1 (DOWN)   id: 1
            MEM Entitle:50.00%    MEM Max:(none)    Usage:0.00%
            CPU Entitle:9.09%    CPU Max:(none)    Usage:0.00%
        """
        cmd = ['srp', '-status', self.name, '-v']
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("srp -status returned %d:\n%s"%(ret, err))

        data = {'ip': [], 'mem': {}, 'cpu': {}}

        words = out.split()
        for i, w in enumerate(words):
            if w.startswith('IP'):
                ip = w.replace('IP:','')
                intf = words[i+1].replace('Interface:','')
                state = words[i+2].strip('(').strip(')')
                id = words[i+4]
                data['ip'].append({
                  'ip': ip,
                  'intf': intf,
                  'state': state,
                  'id': id,
                })
            elif w == "MEM" and words[i+1].startswith("Entitle"):
                entitle = words[i+1].replace('Entitle:','')
                max = words[i+3].replace('Max:','')
                usage = words[i+4].replace('Usage:','')
                data['mem'] = {
                  'entitle': entitle,
                  'max': max,
                  'usage': usage,
                }
            elif w == "CPU" and words[i+1].startswith("Entitle"):
                entitle = words[i+1].replace('Entitle:','')
                max = words[i+3].replace('Max:','')
                usage = words[i+4].replace('Usage:','')
                data['cpu'] = {
                  'entitle': entitle,
                  'max': max,
                  'usage': usage,
                }
        return data

    def get_status(self, nodename=None):
        """
        NAME         TYPE      STATE       SUBTYPE    ROOTPATH
        iisdevs1     system    maintenance private    /var/hpsrp/iisdevs1
        """
        cmd = ['srp', '-status', self.name]
        if nodename is not None:
            cmd = rcEnv.rsh.split() + [nodename] + cmd

        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("srp -status returned %d:\n%s"%(ret, err))
        lines = out.split('\n')
        if len(lines) < 2:
            raise ex.excError("srp -status output too short:\n%s"%out)
        l = lines[1].split()
        if l[0] != self.name:
            raise ex.excError("srp -status second line, first entry does not match container name")
        if len(l) != 5:
            raise ex.excError("unexpected number of entries in %s"%str(l))
        _type, _state, _subtype, _rootpath = l[1:]
        return {
          'type': l[1],
          'state': l[2],
          'subtype': l[3],
          'rootpath': l[4],
        }

    def is_down(self):
        d = self.get_status()
        if d['state'] == 'stopped':
            return True
        return False

    def is_up_on(self, nodename):
        return self.is_up(nodename)

    def is_up(self, nodename=None):
        d = self.get_status(nodename)
        if d['state'] == 'started':
            return True
        return False

    def get_container_info(self):
        return {'vcpus': '0', 'vmem': '0'}

    def check_manual_boot(self):
        try:
            val = self.get_verbose_list()['init.autostart']
        except ex.excError:
            return False
        if val == 'yes' or val == '1':
            return False
        return True

    def check_capabilities(self):
        if not which('srp'):
            self.log.debug("srp is not in PATH")
            return False
        return True

    def presync(self):
        self.container_export()

    def postsync(self):
        self.container_import()

    def container_import(self):
        if not os.path.exists(self.export_file):
            raise ex.excError("%s does not exist"%self.export_file)
        cmd = ['srp', '-batch', '-import', '-xfile', self.export_file, 'allow_sw_mismatch=yes', 'autostart=no']
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError()

    def container_export(self):
        cmd = ['srp', '-batch', '-export', self.name, '-xfile', self.export_file]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError()

    def __init__(self,
                 rid,
                 name,
                 guestos="HP-UX",
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name=name,
                                        type="container.srp",
                                        guestos=guestos,
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=restart,
                                        subset=subset,
                                        tags=tags,
                                        always_on=always_on)
        self.export_file = os.path.join(rcEnv.pathvar, name + '.xml')
        self.runmethod = ['srp_su', name, 'root', '-c']

    def provision(self):
        m = __import__("provSrp")
        prov = m.ProvisioningSrp(self)
        prov.provisioner()

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

   0707010000bc25000081a400000000000000000000000154fc7bfb00001484000000660000000300000000000000000000002900000000root/opt/opensvc/lib/resContainerVbox.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import resources as Res
import rcExceptions as ex
from rcUtilities import qcall
import resContainer
from rcGlobalEnv import rcEnv
import os

utilities = __import__('rcUtilities'+rcEnv.sysname)

class Vbox(resContainer.Container):
    def __init__(self,
                 rid,
                 name,
                 guestos=None,
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name=name,
                                        type="container.vbox",
                                        guestos=guestos,
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=restart,
                                        subset=subset,
                                        tags=tags,
                                        always_on=always_on)
        self.shutdown_timeout = 240
        #self.sshbin = '/usr/local/bin/ssh'
        self.vminfo = None

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def get_vminfo(self):
        if self.vminfo is not None:
            return self.vminfo
        cmd = ['VBoxManage', 'showvminfo', '--machinereadable', self.name]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            return None
        h = {}
        for line in out.split('\n'):
            l = line.split('=')
            if len(l) != 2:
                continue
            key = l[0].strip('"')
            val = l[1].strip('"')
            h[key] = val
        self.vminfo = h
        return self.vminfo

    def files_to_sync(self):
        a = []
        vminfo = self.get_vminfo()
        if vminfo is None:
            return []
        a.append(vminfo['CfgFile'])
        a.append(vminfo['SnapFldr'])
        a.append(vminfo['LogFldr'])
        return a

    def check_capabilities(self):
        cmd = ['VBoxManage', '-v']
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            return False
        return True

    def state(self, nodename):
        cmd = ['VBoxManage', 'list', 'runningvms']
        if nodename is not None:
            cmd = rcEnv.rsh.split() + [nodename] + cmd
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            return None
        for line in out.split('\n'):
            l = line.split('"')
            if len(l) < 2:
                continue
            if l[1] == self.name:
                return 'on'
        return 'off'

    def ping(self):
        return utilities.check_ping(self.addr)

    def container_action(self, action, add=[]):
        cmd = ['VBoxManage', action, self.name] + add
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def container_start(self):
        state = self.state()
        if state == 'None':
            raise ex.excError
        elif state == 'off':
            self.container_action('startvm')
        elif state == 'on':
            self.log.info("container is already up")

    def container_forcestop(self):
        self.container_action('controlvm', ['poweroff'])

    def container_stop(self):
        state = self.state()
        if state == 'None':
            raise ex.excError
        elif state == 'off':
            self.log.info("container is already down")
        if state == 'on' :
            self.container_action('controlvm', ['acpipowerbutton'])
            try:
                self.log.info("wait for container shutdown")
                self.wait_for_fn(self.is_shutdown, self.shutdown_timeout, 2)
            except ex.excError:
                self.container_forcestop()

    def check_manual_boot(self):
        return True

    def is_shutdown(self):
        state = self.state()
        if state == 'off':
            return True
        return False

    def is_down(self):
        if self.state() == 'off':
            return True
        return False

    def is_up_on(self, nodename):
        return self.is_up(nodename)

    def is_up(self, nodename=None):
        if self.state(nodename) == 'on':
            return True
        return False

0707010000bc26000081a400000000000000000000000154fc7bfb0000285e000000660000000300000000000000000000002b00000000root/opt/opensvc/lib/resContainerVcloud.py    #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import rcStatus
import resources as Res
import time
import os
import rcExceptions as ex
from rcGlobalEnv import rcEnv
from rcUtilities import qcall
from rcUtilitiesLinux import check_ping
import resContainer

from libcloud.utils.py3 import urlparse

urlparse = urlparse.urlparse

def get_url_path(url):
    return urlparse(url.strip()).path

class CloudVm(resContainer.Container):
    startup_timeout = 240
    shutdown_timeout = 120
    save_timeout = 240

    def __init__(self,
                 rid,
                 name,
                 vapp=None,
                 guestos=None,
                 cloud_id=None,
                 size="tiny",
                 key_name=None,
                 shared_ip_group=None,
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name=name,
                                        type="container.vcloud",
                                        guestos=guestos,
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=restart,
                                        subset=subset,
                                        tags=tags,
                                        always_on=always_on)
        self.cloud_id = cloud_id
        self.save_name = name + '.save'
        self.size_name = size
        self.key_name = key_name
        self.vapp = vapp
        self.shared_ip_group = shared_ip_group

    def _vm_perform_power_operation(self, vapp_or_vm_id, operation):
        drv = self.get_cloud().driver
        vms = drv._get_vm_elements(vapp_or_vm_id)
        for vm in vms:
            res = drv.connection.request(
                '%s/power/action/%s' % (get_url_path(vm.get('href')), operation),
                method='POST')
            drv._wait_for_task_completion(res.object.get('href'))
            res = drv.connection.request(get_url_path(vm.get('href')))

    def get_size(self):
        c = self.get_cloud()
        for size in c.driver.list_sizes():
            if size.name == self.size_name:
                return size
        raise ex.excError("%s size not found"%self.size_name)

    def get_cloud(self):
        if hasattr(self, 'cloud'):
            return self.cloud
        c = self.svc.node.cloud_get(self.cloud_id)
        self.cloud = c
        return self.cloud

    def get_vapp(self):
        c = self.get_cloud()
        try:
            vapp = c.driver.ex_find_node(self.vapp)
        except Exception as e:
            print(e)
            raise
        return vapp

    def get_node(self):
        c = self.get_cloud()
        try:
            vapp = c.driver.ex_find_node(self.vapp)
            vms = vapp.extra['vms']
        except Exception as e:
            print(e)
            raise
        for vm in vms:
            if vm['name'] == self.name:
                return vm
        return

    def get_save_name(self):
        import datetime
        now = datetime.datetime.now()
        save_name = self.save_name + now.strftime(".%Y-%m-%d.%H:%M:%S")
        return save_name

    def purge_saves(self):
        c = self.get_cloud()
        l = c.driver.list_images()
        d = {}
        for image in l:
            if image.name.startswith(self.save_name):
                d[image.name] = image
        if len(d) == 0:
             raise ex.excError("no save image found")
        elif len(d) == 1:
             self.log.info("no previous save image to delete")
        for k in sorted(d.keys())[:-1]:
             self.log.info("delete previous save image %s"%d[k].name)
             c.driver.ex_delete_image(d[k])

    def get_last_save(self):
        return self.get_image(self.save_name)

    def get_template(self):
        template = self.svc.config.defaults()['template']
        return self.get_image(template)

    def get_image(self, name):
        c = self.get_cloud()
        l = c.driver.list_images()
        d = {}
        for image in l:
            if image.name == name:
                # exact match
                return image
            elif image.name.startswith(name):
                d[image.name] = image
        if len(d) == 0:
             raise ex.excError("image %s not found"%name)
        for k in sorted(d.keys()):
             last = d[k]
        return last

    def has_image(self, name):
        c = self.get_cloud()
        l = c.driver.list_images()
        for image in l:
            if image.name == name:
                return True
        return False

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def getaddr(self):
        if hasattr(self, 'addr'):
            return
        n = self.get_node()

        ips = set(n['public_ips']+n['private_ips'])
        if len(ips) == 0:
            return 0

        # find first pinging ip
        for ip in ips:
            if check_ping(ip, timeout=1, count=1):
                self.addr = ip
                break

    def files_to_sync(self):
        return []

    def check_capabilities(self):
        return True

    def ping(self):
        if not hasattr(self, "addr"):
            return 0
        return check_ping(self.addr, timeout=1, count=1)

    def start(self):
        if self.is_up():
            self.log.info("container %s already started" % self.name)
            return
        if rcEnv.nodename in self.svc.drpnodes:
            self.install_drp_flag()
        self.container_start()
        self.can_rollback = True
        self.wait_for_startup()

    def container_start(self):
        c = self.get_cloud()
        n = self.get_node()
        self.log.info("power on container %s"%self.name)
        self._vm_perform_power_operation(n['id'], 'powerOn')
        self.log.info("wait for container up status")
        self.wait_for_fn(self.is_up, self.startup_timeout, 5)

    def wait_for_startup(self):
        pass

    def stop(self):
        if self.is_down():
            self.log.info("container %s already stopped" % self.name)
            return
        self.container_stop()
        try:
            self.wait_for_shutdown()
        except ex.excError:
            self.container_forcestop()
            self.wait_for_shutdown()

    def container_stop(self):
        c = self.get_cloud()
        n = self.get_node()
        self.log.info("shutdown container %s"%self.name)
        self._vm_perform_power_operation(n['id'], 'shutdown')

    def print_obj(self, n):
        for k in dir(n):
            if '__' in k:
                continue
            print(k, "=", getattr(n, k))

    def container_save(self):
        c = self.get_cloud()
        n = self.get_node()
        save_name = self.get_save_name()
        if self.has_image(save_name):
            return
        #self.print_obj(n)
        if n['state'] == 9999:
            self.log.info("a save is already in progress")
            return
        self.log.info("save new image %s"%save_name)
        try:
            image = c.driver.ex_save_image(n, save_name)
        except Exception as e:
            raise ex.excError(str(e))
        import time
        delay = 5
        for i in range(self.save_timeout//delay):
            img = c.driver.ex_get_image(image.id)
            if img.extra['status'] != 'SAVING':
                break
            time.sleep(delay)
        if img.extra['status'] != 'ACTIVE':
            raise ex.excError("save failed, image status %s"%img.extra['status'])

    def container_forcestop(self):
        c = self.get_cloud()
        n = self.get_node()
        self.log.info("power off container %s"%self.name)
        self._vm_perform_power_operation(n['id'], 'powerOff')

    def is_up(self):
        n = self.get_node()
        if n is not None and n['state'] == 0:
            return True
        return False

    def get_container_info(self):
        self.info = {'vcpus': '0', 'vmem': '0'}
        c = self.get_cloud()
        n = self.get_node()
        top = c.driver._get_vm_elements(n['id'])[0]
        def recurse(x, info={}, desc=None):
            if x.tag == '{http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData}Description':
                desc = x.text
                info[desc] = {}
                return info, desc
            if desc is not None:
                info[desc][x.tag[x.tag.index('}')+1:]] = x.text
            for c in x._children:
                info, desc = recurse(c, info, desc)
            return info, desc
        info, desc = recurse(top)
        self.info['vcpus'] = info['Number of Virtual CPUs']['VirtualQuantity']
        self.info['vmem'] = info['Memory Size']['VirtualQuantity']
        #print(self.info)
        return self.info

    def check_manual_boot(self):
        return True

    def install_drp_flag(self):
        pass

    def provision(self):
        c = self.get_cloud()
        image = self.get_template()
        size = self.get_size()
        self.log.info("create instance %s, size %s, image %s, key %s"%(self.name, size.name, image.name, self.key_name))
        c.driver.create_node(name=self.name, size=size, image=image, ex_key_name=self.key_name, ex_shared_ip_group_id=self.shared_ip_group)
        self.wait_for_startup()

  0707010000bc27000081a400000000000000000000000154fc7bfb0000178b000000660000000300000000000000000000002700000000root/opt/opensvc/lib/resContainerVz.py    #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os

import rcStatus
import resources as Res
from rcUtilities import which, qcall, justcall
import resContainer
import rcExceptions as ex
from rcGlobalEnv import rcEnv

class Vz(resContainer.Container):
    def files_to_sync(self):
        return [self._cf]

    def get_cf_value(self, param):
        value = None
        try:
            cf = self.cf()
        except:
            return value
        with open(cf, 'r') as f:
            for line in f.readlines():
                if param not in line:
                    continue
                if line.strip()[0] == '#':
                    continue
                l = line.replace('\n', '').split('=')
                if len(l) < 2:
                    continue
                if l[0].strip() != param:
                    continue
                value = ' '.join(l[1:]).strip().rstrip('/')
                break
        return value

    def get_rootfs(self):
        with open(self.cf(), 'r') as f:
            for line in f:
                if 'VE_PRIVATE' in line:
                    return line.strip('\n').split('=')[1].strip('"').replace('$VEID', self.name)
        self.log.error("could not determine lxc container rootfs")
        return ex.excError

    def rcp_from(self, src, dst):
        rootfs = self.get_rootfs()
        if len(rootfs) == 0:
            raise ex.excError()
        src = rootfs + src
        cmd = ['cp', src, dst]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def rcp(self, src, dst):
        rootfs = self.get_rootfs()
        if len(rootfs) == 0:
            raise ex.excError()
        dst = rootfs + dst
        cmd = ['cp', src, dst]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def install_drp_flag(self):
        rootfs = self.get_rootfs()
        flag = os.path.join(rootfs, ".drp_flag")
        self.log.info("install drp flag in container : %s"%flag)
        with open(flag, 'w') as f:
            f.write(' ')
            f.close()

    def vzctl(self, action, options=[]):
        cmd = ['vzctl', action, self.name] + options
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        return out

    def container_start(self):
        self.vzctl('start')

    def container_stop(self):
        self.vzctl('stop')

    def container_forcestop(self):
        raise ex.excError

    def operational(self):
        cmd = self.runmethod + ['/sbin/ifconfig', '-a']
        ret = qcall(cmd)
        if ret == 0:
            return True
        return False

    def is_up_on(self, nodename):
        return self.is_up(nodename)

    def is_up(self, nodename=None):
        """ CTID 101 exist mounted running
        """
        cmd = ['vzctl', 'status', self.name]
        if nodename is not None:
            cmd = rcEnv.rsh.split() + [nodename] + cmd
        ret, out, err = self.call(cmd)
        if ret != 0:
            return False
        l = out.split()
        if len(l) != 5:
            return False
        if l[2] != 'exist' or \
           l[3] != 'mounted' or \
           l[4] != 'running':
            return False
        return True

    def get_container_info(self):
        return {'vcpus': '0', 'vmem': '0'}

    def check_manual_boot(self):
        try:
            cf = self.cf()
        except:
            return True
        with open(self.cf(), 'r') as f:
            for line in f:
                if 'ONBOOT' in line and 'yes' in line:
                    return False
        return True

    def check_capabilities(self):
        if not which('vzctl'):
            self.log.debug("vzctl is not in PATH")
            return False
        return True

    def cf(self):
        if not os.path.exists(self._cf):
            self.log.error("%s does not exist"%self._cf)
            raise ex.excError
        return self._cf

    def __init__(self,
                 rid,
                 name,
                 guestos="Linux",
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name=name,
                                        type="container.vz",
                                        guestos=guestos,
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=restart,
                                        tags=tags,
                                        subset=subset,
                                        always_on=always_on)
        self._cf = os.path.join(os.sep, 'etc', 'vz', 'conf', name+'.conf')
        self.runmethod = ['vzctl', 'exec', name]

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

 0707010000bc28000081a400000000000000000000000154fc7bfb0000124f000000660000000300000000000000000000002800000000root/opt/opensvc/lib/resContainerXen.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import resources as Res
import os
import rcExceptions as ex
import resContainer

rcU = __import__("rcUtilities" + os.uname()[0])

class Xen(resContainer.Container):
    startup_timeout = 180
    shutdown_timeout = 120

    def __init__(self,
                 rid,
                 name,
                 guestos=None,
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name=name,
                                        type="container.xen",
                                        guestos=guestos,
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=0,
                                        tags=tags,
                                        subset=subset,
                                        always_on=always_on)

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def list_conffiles(self):
        cf = os.path.join(os.sep, 'opt', 'opensvc', 'var', self.name+'.xml')
        if os.path.exists(cf):
            return [cf]
        return []

    def files_to_sync(self):
        return self.list_conffiles()

    def check_capabilities(self):
        cmd = ['virsh', 'capabilities']
        (ret, out, err) = self.call(cmd, errlog=False)
        if ret != 0:
            self.status_log("can not fetch capabilities")
            return False
        return True

    def ping(self):
        return rcU.check_ping(self.addr, timeout=1, count=1)

    def container_start(self):
        cf = os.path.join(os.sep, 'opt', 'opensvc', 'var', self.name+'.xml')
        if os.path.exists(cf):
            cmd = ['virsh', 'define', cf]
            (ret, buff, err) = self.vcall(cmd)
            if ret != 0:
                raise ex.excError
        cmd = ['virsh', 'start', self.name]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def container_stop(self):
        cmd = ['virsh', 'shutdown', self.name]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def container_forcestop(self):
        cmd = ['virsh', 'destroy', self.name]
        (ret, buff, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def is_up_on(self, nodename):
        return self.is_up(nodename)

    def is_up(self, nodename=None):
        cmd = ['virsh', 'dominfo', self.name]
        if nodename is not None:
            cmd = rcEnv.rsh.split() + [nodename] + cmd
        (ret, out, err) = self.call(cmd, errlog=False)
        if ret != 0:
            return False
        if "running" in out.split() or "idle" in out.split() :
            return True
        return False

    def get_container_info(self):
        cmd = ['virsh', 'dominfo', self.name]
        (ret, out, err) = self.call(cmd, errlog=False, cache=True)
        self.info = {'vcpus': '0', 'vmem': '0'}
        if ret != 0:
            return self.info
        for line in out.split('\n'):
            if "CPU(s):" in line: self.info['vcpus'] = line.split(':')[1].strip()
            if "Max memory" in line: self.info['vmem'] = line.split(':')[1].strip()
            if "Autostart:" in line: self.info['autostart'] = line.split(':')[1].strip()
        return self.info           

    def check_manual_boot(self):
        self.get_container_info()
        if self.info['autostart'] == 'disable' :
                return True
        else:
                return False
 0707010000bc29000081a400000000000000000000000154fc7bfb00003d99000000660000000300000000000000000000002900000000root/opt/opensvc/lib/resContainerZone.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from datetime import datetime
import rcStatus
import resources as Res
import time
import os
from rcUtilities import justcall, qcall
from stat import *
import resContainer
from rcExceptions import excError
from rcZfs import zfs_setprop
from rcGlobalEnv import rcEnv

ZONECFG="/usr/sbin/zonecfg"
PGREP="/usr/bin/pgrep"
PWAIT="/usr/bin/pwait"
INIT="/sbin/init"
SVCS="/usr/bin/svcs"

MULTI_USER_SMF="svc:/milestone/multi-user:default"

class Zone(resContainer.Container):
    def __init__(self,
                 rid,
                 name,
                 guestos="SunOS",
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([]),
                 always_on=set([])):
        """define Zone object attribute :
                name
                label
                state
                zonepath
        """
        resContainer.Container.__init__(self,
                                        rid=rid,
                                        name=name,
                                        type="container.zone",
                                        guestos=guestos,
                                        optional=optional,
                                        disabled=disabled,
                                        monitor=monitor,
                                        restart=restart,
                                        subset=subset,
                                        tags=tags,
                                        always_on=always_on)
        self.label = name
        self.state = None
        self.zonepath = os.path.realpath(os.path.join(os.sep, 'zones', self.name))
        self.zone_refresh()
        self.runmethod = [ '/usr/sbin/zlogin', '-S', name ]

    def files_to_sync(self):
        return ["/etc/zones/"+self.name+".xml"]

    def zonecfg(self, zonecfg_args=[]):
        cmd = [ZONECFG, '-z', self.name] + zonecfg_args
        (ret, out, err) = self.vcall(cmd,err_to_info=True)
        if ret != 0:
            msg = '%s failed status: %i\n%s' % (" ".join(cmd), ret, out)
            self.log.error(msg)
            raise excError(msg)
        else:
            msg = '%s done status: %i\n%s' % (" ".join(cmd), ret, out)
            self.log.info(msg)
        self.zone_refresh()
        return ret

    def zoneadm(self, action, option=None):
        if action in [ 'ready' , 'boot' ,'shutdown' , 'halt' ,'attach', 'detach', 'install', 'clone' ] :
            cmd = ['zoneadm', '-z', self.name, action ]
        else:
            self.log.error("unsupported zone action: %s" % action)
            return 1
        if option is not None:
            cmd += option

        t = datetime.now()
        (ret, out, err) = self.vcall(cmd,err_to_info=True)
        len = datetime.now() - t
        if ret != 0:
            msg = '%s failed status: %i in %s logs in %s' % (' '.join(cmd), ret, len, out)
            self.log.error(msg)
            raise excError(msg)
        else:
            self.log.info('%s done in %s - ret %i - logs in %s'
                            % (' '.join(cmd), len, ret, out))
        self.zone_refresh()
        return ret

    def set_zonepath_perms(self):
        if not os.path.exists(self.zonepath):
            os.makedirs(self.zonepath)
        s = os.stat(self.zonepath)
        if s.st_uid != 0 or s.st_gid != 0:
            self.log.info("set %s ownership to uid 0 gid 0"%self.zonepath)
            os.chown(self.zonepath, 0, 0)
        mode = s[ST_MODE]
        if (S_IWOTH&mode) or (S_IXOTH&mode) or (S_IROTH&mode) or \
           (S_IWGRP&mode) or (S_IXGRP&mode) or (S_IRGRP&mode):
            self.vcall(['chmod', '700', self.zonepath])

    def rcp_from(self, src, dst):
        src = os.path.realpath(self.zonepath + '/root/' + src)
        cmd = ['cp', src, dst]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def rcp(self, src, dst):
        dst = os.path.realpath(self.zonepath + '/root/' + dst)
        cmd = ['cp', src, dst]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise excError("'%s' execution error:\n%s"%(' '.join(cmd), err))
        return out, err, ret

    def attach(self):
        self.zone_refresh()
        if self.state in ('installed' , 'ready', 'running'):
            self.log.info("zone container %s already installed" % self.name)
            return 0
        try:
            self.umount_fs_in_zonepath()
            self.zoneadm('attach')
        except excError:
            self.zoneadm('attach', ['-F'] )
        self.can_rollback = True

    def detach(self):
        self.zone_refresh()
        if self.state == "configured" :
            self.log.info("zone container %s already detached/configured" % self.name)
            return 0
        return self.zoneadm('detach')

    def ready(self):
        self.zone_refresh()
        if self.state == 'ready' or self.state == "running" :
            self.log.info("zone container %s already ready" % self.name)
            return 0
        self.set_zonepath_perms()
        return self.zoneadm('ready')

    def install_drp_flag(self):
        flag = os.path.join(self.zonepath, ".drp_flag")
        self.log.info("install drp flag in container : %s"%flag)
        with open(flag, 'w') as f:
            f.write(' ')
            f.close()

    def get_smf_state(self, smf=None):
        cmd = self.runmethod + [SVCS, '-H', '-o', 'state', smf]
        (out, err, status) = justcall(cmd)
        if status == 0:
            return out.split('\n')[0]
        else:
            return False

    def is_smf_state(self, smf=None, value=None):
        current_value = self.get_smf_state(smf)
        if current_value is False:
            return False
        elif current_value == value:
            return True
        else:
            return False

    def is_multi_user(self):
        return self.is_smf_state(MULTI_USER_SMF, "online")

    def wait_multi_user(self):
        self.log.info("wait for smf state on on %s", MULTI_USER_SMF)
        self.wait_for_fn(self.is_multi_user, self.startup_timeout, 2)
        
    def boot(self):
        "return 0 if zone is running else return self.zoneadm('boot')"
        self.zone_refresh()
        if self.state == "running" :
            self.log.info("zone container %s already running" % self.name)
            return 0
        self.zoneadm('boot')
        if self.state == "running":
            return(0)
        else:
            raise(excError("zone should be running"))
        self.log.info("wait for zone operational")
        self.wait_for_fn(self.operational, self.startup_timeout, 2)

    def halt(self):
        """ Need wait poststat after returning to installed state on ipkg
            example : /bin/ksh -p /usr/lib/brand/ipkg/poststate zonename zonepath 5 4
        """
        self.zone_refresh()
        if self.state in [ 'installed', 'configured'] :
            self.log.info("zone container %s already stopped" % self.name)
            return 0
        if self.state == 'running':
            (ret, out, err) = self.vcall(['zlogin' , self.name , '/sbin/init' , '0'])
            for t in range(self.shutdown_timeout):
                self.zone_refresh()
                if self.state == 'installed':
                    for t2 in range(self.shutdown_timeout):
                        time.sleep(1)
                        (out,err,st) = justcall([ 'pgrep', '-fl', 'ipkg/poststate.*'+ self.name])
                        if st == 0 : 
                            self.log.info("Waiting for ipkg poststate complete: %s" % out)
                        else:
                            break
                    return 0
                time.sleep(1)
            self.log.info("timeout out waiting for %s shutdown", self.name)
        ret = self.zoneadm('halt')
        if ret != 0:
            return ret
        return self.detach()

    def container_start(self):
        return self.boot()

    def _status(self, verbose=False):
        self.zone_refresh()
        if self.state == 'running' :
            return rcStatus.UP
        else:
            return rcStatus.DOWN

    def zone_refresh(self):
        """ refresh Zone object attributes:
                state
                zonepath
                brand
            from zoneadm -z zonename list -p
            zoneid:zonename:state:zonepath:uuid:brand:ip-type
        """

        (out,err,st) = justcall([ 'zoneadm', '-z', self.name, 'list', '-p' ])

        if st == 0 :
            out = out.strip()
            l = out.split(':')
            n_fields = len(l)
            if n_fields == 9:
                (zoneid,zonename,state,zonepath,uuid,brand,iptype,rw,macp) = l
            elif n_fields == 10:
                (zoneid,zonename,state,zonepath,uuid,brand,iptype,rw,macp,dummy) = l
            elif n_fields == 7:
                (zoneid,zonename,state,zonepath,uuid,brand,iptype) = l
            else:
                raise excError("Unexpected zoneadm list output: %s"%out)
            if zonename == self.name :
                self.state = state
                self.zonepath = zonepath
                self.brand = brand
                return True
            else:
                return False
        else:
            return False

    def is_running(self):
        "return True if zone is running else False"
        self.zone_refresh()
        if self.state == 'running' :
            return True
        else:
            return False

    def is_up(self):
        "return self.is_running status"
        return self.is_running()

    def operational(self):
        "return status of: zlogin zone pwd"
        cmd = self.runmethod + ['pwd']
        if qcall(cmd) == 0:
            return True
        else:
            return False

    def boot_and_wait_reboot(self):
        """boot zone, then wait for automatic zone reboot
            boot zone
            wait for zone init process end
            wait for zone running
            wait for zone operational
        """
        self.log.info("wait for zone boot and reboot...")
        self.boot()
        if self.is_running is False:
            raise(excError("zone is not running"))
        cmd = [PGREP, "-z", self.name, "-f", INIT]
        (out, err, st) = justcall(cmd)
        if st != 0:
            raise(excError("fail to detect zone init process"))
        pids = " ".join(out.split("\n")).rstrip()
        cmd = [PWAIT, pids]
        self.log.info("wait for zone init process %s termination" % (pids))
        if qcall(cmd) != 0:
            raise(excError("failed " + " ".join(cmd)))
        self.log.info("wait for zone running again")
        self.wait_for_fn(self.is_up, self.startup_timeout, 2)
        self.log.info("wait for zone operational")
        self.wait_for_fn(self.operational, self.startup_timeout, 2)
 
    def umount_fs_in_zonepath(self):
        """zone boot will fail if some fs linger under the zonepath.
           those fs might be datasets automounted upon zpool import.
           umount them.
           if they are needed, them still may be mounted by opensvc
           if declared as zoned fs or encap fs.
        """
        if self.zonepath == "/":
            # sanity check
            return

        m = __import__('rcMounts'+rcEnv.sysname)
        mounts = m.Mounts()
        mounts.sort(reverse=True)
        mntpts = []
        for resource in self.svc.get_resources('fs'):
            mntpts.append(resource.mountPoint)
        for mount in mounts.mounts:
            if not mount.mnt.startswith(self.zonepath):
                continue
            # don't umount fs not handled by the service
            if mount.mnt not in mntpts:
                continue
            self.vcall(['umount', mount.mnt])
            self.vcall(['rmdir', mount.mnt])
            if mount.type == 'zfs':
                zfs_setprop(mount.dev, 'canmount', 'noauto')

    def start(self):
        self.attach()
        self.ready()
        self.svc.sub_set_action("ip", "start", tags=set([self.name]))
        self.boot()
        self.svc.sub_set_action("disk.scsireserv", "start", tags=set([self.name]))
        self.svc.sub_set_action("disk.zpool", "start", tags=set([self.name]))
        self.svc.sub_set_action("fs", "start", tags=set([self.name]))

    def stop(self):
        self.svc.sub_set_action("fs", "stop", tags=set([self.name]))
        self.svc.sub_set_action("disk.zpool", "stop", tags=set([self.name]))
        self.svc.sub_set_action("disk.scsireserv", "stop", tags=set([self.name]))
        self.svc.sub_set_action("ip", "stop", tags=set([self.name]))
        self.halt()
        self.detach()

    def provision(self):
        self._provision()
        self.svc.sub_set_action("disk.scsireserv", "provision", tags=set([self.name]))
        self.svc.sub_set_action("disk.zpool", "provision", tags=set([self.name]))
        self.svc.sub_set_action("fs", "provision", tags=set([self.name]))

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def _provision(self):
        m = __import__("provZone")
        m.ProvisioningZone(self).provisioner()

    def get_container_info(self):
        vcpus = "0"
        vmem = "0"
        cmd = [ZONECFG, "-z", self.name, "info", "rctl", "name=zone.cpu-cap"]
        (out, err, status) = justcall(cmd)
        if status == 0:
            lines = out.split('\n')
            for line in lines:
                if "value:" not in line:
                    continue
                l = line.split("limit=")
                if len(l) == 2:
                    vcpus = l[-1][:l[-1].index(',')]
                    vcpus = str(float(vcpus)/100)
                    break
                
        cmd = [ZONECFG, "-z", self.name, "info", "capped-memory"]
        (out, err, status) = justcall(cmd)
        if status == 0:
            lines = out.split('\n')
            for line in lines:
                if "physical:" not in line:
                    continue
                l = line.split(": ")
                if len(l) == 2:
                    vmem = l[-1].strip()
                    if vmem.endswith('T'):
                        vmem = str(float(vmem[:-1])*1024*1024)
                    elif vmem.endswith('G'):
                        vmem = str(float(vmem[:-1])*1024)
                    elif vmem.endswith('M'):
                        vmem = vmem[:-1]
                    elif vmem.endswith('K'):
                        vmem = str(float(vmem[:-1])/1024)
                    break
                
        return {'vcpus': vcpus, 'vmem': vmem}


if __name__ == "__main__":
    for c in (Zone,) :
        help(c)
   0707010000bc2b000081a400000000000000000000000154fc7bfb00001af1000000660000000300000000000000000000002000000000root/opt/opensvc/lib/resDrbd.py   #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import os
import resources as Res
import rcStatus
import rcExceptions as ex
from rcUtilities import which, justcall
from rcGlobalEnv import rcEnv

class Drbd(Res.Resource):
    """ Drbd device resource

        The tricky part is that drbd devices can be used as PV
        and LV can be used as drbd base devices. Treat the ordering
        with 'prevg' and 'postvg' tags.

        Start 'ups' and promotes the drbd devices to primary.
        Stop 'downs' the drbd devices.
    """
    def __init__(self,
                 rid=None,
                 res=None,
                 always_on=set([]),
                 optional=False,
                 disabled=False,
                 subset=None,
                 tags=set([]),
                 monitor=False,
                 restart=0):
        Res.Resource.__init__(self,
                              rid,
                              "disk.drbd",
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset,
                              monitor=monitor,
                              restart=restart)
        self.res = res
        self.label = res
        self.drbdadm = None
        self.always_on = always_on
        self.disks = set()
        if 'prevg' not in self.tags and 'postvg' not in self.tags:
            tags |= set(['postvg'])

    def __str__(self):
        return "%s resource=%s" % (Res.Resource.__str__(self),\
                                 self.res)

    def files_to_sync(self):
        cf = os.path.join(os.sep, 'etc', 'drbd.d', self.res+'.res')
        if os.path.exists(cf):
            return [cf]
        self.log.error("%s does not exist"%cf)
        return []

    def drbdadm_cmd(self, cmd):
        if self.drbdadm is None:
            if which('drbdadm'):
                self.drbdadm = 'drbdadm'
            else:
                self.log("drbdadm command not found")
                raise exc.excError
        return [self.drbdadm] + cmd.split() + [self.res]

    def disklist(self):
        if self.disks != set():
            return self.disks

        self.disks = set()
        devps = set()

        (ret, out, err) = self.call(self.drbdadm_cmd('dump-xml'))
        if ret != 0:
            raise ex.excError

        from xml.etree.ElementTree import XML, fromstring
        tree = fromstring(out)
        
        for res in tree.getiterator('resource'):
            if res.attrib['name'] != self.res:
                continue
            for host in res.getiterator('host'):
                if host.attrib['name'] != rcEnv.nodename:
                    continue
                d = host.find('disk')
                if d is None:
                    continue
                devps |= set([d.text])

        try:
            u = __import__('rcUtilities'+rcEnv.sysname)
            self.disks = u.devs_to_disks(self, devps)
        except:
            self.disks = devps

        return self.disks

    def drbdadm_down(self):
        (ret, out, err) = self.vcall(self.drbdadm_cmd('down'))
        if ret != 0:
            raise ex.excError

    def drbdadm_up(self):
        (ret, out, err) = self.vcall(self.drbdadm_cmd('up'))
        if ret != 0:
            raise ex.excError

    def get_cstate(self):
        self.prereq()
        (out, err, ret) = justcall(self.drbdadm_cmd('cstate'))
        if ret != 0:
            if "Device minor not allocated" in err:
                return "Unattached"
            else:
                raise ex.excError
        return out.strip()

    def prereq(self):
        if not os.path.exists("/proc/drbd"):
            (ret, out, err) = self.vcall(['modprobe', 'drbd'])
            if ret != 0: raise ex.excError

    def start_connection(self):
        cstate = self.get_cstate()
        if cstate == "Connected":
            self.log.info("drbd resource %s is already up"%self.res)
        elif cstate == "StandAlone":
            self.drbdadm_down()
            self.drbdadm_up()
        elif cstate == "WFConnection":
            self.log.info("drbd resource %s peer node is not listening"%self.res)
            pass
        else:
            self.drbdadm_up()

    def get_roles(self):
        (ret, out, err) = self.call(self.drbdadm_cmd('role'))
        if ret != 0:
            raise ex.excError
        out = out.strip().split('/')
        if len(out) != 2:
            raise ex.excError
        return out

    def start_role(self, role):
        roles = self.get_roles()
        if roles[0] != role:
            (ret, out, err) = self.vcall(self.drbdadm_cmd(role.lower()))
            if ret != 0:
                raise ex.excError
        else:
            self.log.info("drbd resource %s is already %s"%(self.res, role))

    def startstandby(self):
        self.start_connection()
        roles = self.get_roles()
        if roles[0] == "Primary":
            return
        self.start_role('Secondary')
        self.can_rollback = True

    def stopstandby(self):
        self.start_connection()
        roles = self.get_roles()
        if roles[0] == "Secondary":
            return
        self.start_role('Secondary')

    def start(self):
        self.start_connection()
        self.start_role('Primary')
        self.can_rollback = True

    def stop(self):
        self.drbdadm_down()

    def _status(self, verbose=False):
        (ret, out, err) = self.call(self.drbdadm_cmd('dstate'))
        if ret != 0:
            self.status_log("drbdadm dstate %s failed"%self.res)
            return rcStatus.WARN
        out = out.strip()
        if out == "UpToDate/UpToDate":
            return self.status_stdby(rcStatus.UP)
        elif out == "Unconfigured":
            return self.status_stdby(rcStatus.DOWN)
        self.status_log("unexpected drbd resource %s state: %s"%(self.res, out))
        return rcStatus.WARN

if __name__ == "__main__":
    help(Drbd)
    v = Drbd(res='test')
    print(v)

   0707010000bc2c000081a400000000000000000000000154fc7bfb00000a75000000660000000300000000000000000000001e00000000root/opt/opensvc/lib/resHb.py #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import resources as Res
from rcGlobalEnv import rcEnv
import os
import rcExceptions as ex
import rcStatus

class Hb(Res.Resource):
    """ HeartBeat ressource
    """
    def cluster_files(self):
        svcfile = os.path.join(rcEnv.pathetc, self.svc.svcname)
        svcmgr = os.path.join('..', 'bin', 'svcmgr')
        svcmgr_real = os.path.join(rcEnv.pathbin, 'svcmgr')
        cluster_f = '.'.join((svcfile, 'cluster'))
        stonith_f = '.'.join((svcfile, 'stonith'))

        for f in (cluster_f, stonith_f):
            if os.path.islink(f):
                if os.path.exists(f):
                    if os.path.realpath(f) == svcmgr_real:
                        self.log.debug("%s: symlink ok."%f)
                        pass
                    else:
                        self.log.info("%s: symlink exists but points to wrong file. fix."%f)
                        os.unlink(f)
                        os.symlink(svcmgr, f)
                else:
                    self.log.info("%s: broken link. fix."%f)
                    os.unlink(f)
                    os.symlink(svcmgr, f)
            else:
                if os.path.exists(f):
                    self.log.info("%s: regular file. fix."%f)
                    os.unlink(f)
                    os.symlink(svcmgr, f)
                else:
                    self.log.info("%s: not regular file nor symlink. fix."%f)
                    os.symlink(svcmgr, f)

    def __str__(self):
        return "%s" % (Res.Resource.__str__(self))

    def freeze(self):
        pass

    def thaw(self):
        pass

    def stop(self):
        pass

    def start(self):
        pass

    def __status(self, verbose=False):
        return rcStatus.UNDEF

    def _status(self, verbose=False):
        self.cluster_files()
        return self.__status(verbose)
   0707010000bc2d000081a400000000000000000000000154fc7bfb00000911000000660000000300000000000000000000002500000000root/opt/opensvc/lib/resHbLinuxHA.py  #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import resHb
from rcGlobalEnv import rcEnv
import os
import rcStatus
import rcExceptions as ex
from rcUtilities import justcall, which

class Hb(resHb.Hb):
    """ HeartBeat ressource
    """
    def __init__(self,
                 rid=None,
                 name=None,
                 always_on=set([]),
                 optional=False,
                 disabled=False,
                 restart=0,
                 subset=subset,
                 tags=set([])):
        resHb.Hb.__init__(self,
                          rid,
                          "hb.linuxha",
                          optional=optional,
                          disabled=disabled,
                          restart=restart,
                          subset=subset,
                          always_on=always_on,
                          tags=tags)
        self.status_cmd = 'cl_status'
        self.name = name

    def process_running(self):
        cmd = [self.status_cmd, 'hbstatus']
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return False
        if not 'is running' in out:
            return False
        return True

    def __status(self, verbose=False):
        if not which(self.status_cmd):
            self.status_log("heartbeat is not installed")
            return rcStatus.WARN
        if not self.process_running():
            self.status_log("heartbeat daemons are not running")
            return rcStatus.WARN
        return rcStatus.NA

   0707010000bc2e000081a400000000000000000000000154fc7bfb00004699000000660000000300000000000000000000002400000000root/opt/opensvc/lib/resHbOpenHA.py   #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2015 Arnaud Veron <arnaud.veron@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import resHb
from rcGlobalEnv import rcEnv
import os
import rcStatus
import rcExceptions as ex
from rcUtilities import justcall, which
import time

class excDoLocalAfterRemote(Exception):
    pass

class Hb(resHb.Hb):
    """ HeartBeat ressource
    """
    def __init__(self,
                 rid=None,
                 name=None,
                 always_on=set([]),
                 optional=False,
                 disabled=False,
                 restart=0,
                 subset=None,
                 tags=set([])):
        resHb.Hb.__init__(self,
                          rid,
                          "hb.openha",
                          optional=optional,
                          disabled=disabled,
                          restart=restart,
                          subset=subset,
                          tags=tags,
                          always_on=always_on)
        self.basedir = os.path.join(os.sep, 'usr', 'local', 'cluster')
        self.bindir = os.path.join(self.basedir, 'bin')
        self.logdir = os.path.join(self.basedir, 'log')
        self.svcdir = os.path.join(self.basedir, 'services')
        self.cfsvc = os.path.join(self.basedir, 'conf', 'services')
        self.cfnod = os.path.join(self.basedir, 'conf', 'nodes')
        self.cfmon = os.path.join(self.basedir, 'conf', 'monitor')
        os.environ['EZ'] = self.basedir
        os.environ['EZ_BIN'] = self.bindir
        os.environ['EZ_SERVICES'] = self.cfsvc
        os.environ['EZ_NODES'] = self.cfnod
        os.environ['EZ_MONITOR'] = self.cfmon
        os.environ['EZ_LOG'] = self.logdir
        self.service_cmd = os.path.join(self.bindir, 'service')
        self.heartc = os.path.join(self.bindir, 'heartc')
        self.heartd = os.path.join(self.bindir, 'heartd')
        self.nmond = os.path.join(self.bindir, 'nmond')
        self.name = name
        self.state = {'0': 'stopped',
                      '1': 'stopping',
                      '2': 'started',
                      '3': 'starting',
                      '4': 'start_failed',
                      '5': 'stop_failed',
                      '6': 'frozen_stop',
                      '7': 'start_ready',
                      '8': 'unknown',
                      '9': 'force_stop'}

    def cluster_name(self):
        if self.name is None:
            return self.svc.svcname
        else:
            return self.name

    def service_local_status(self):
        return self.service_status(rcEnv.nodename)

    def get_peer(self):
        if len(self.svc.nodes) != 2:
            self.log.error("HA cluster must have 2 nodes")
            raise ex.excError
        nodes = [n for n in self.svc.nodes if n != rcEnv.nodename]
        if len(nodes) != 1:
            self.log.error("local node is not in cluster")
            raise ex.excError
        peer = nodes[0]
        return peer

    def service_remote_status(self):
        peer = self.get_peer()
        return self.service_status(peer)

    def service_status(self, nodename):
        if not self.process_running():
            self.log.error("open-ha daemons are not running")
            return 'unknown'
        service_state = os.path.join(self.svcdir, self.cluster_name(), 'STATE.'+nodename)
        if not os.path.exists(service_state):
            self.status_log("%s does not exist"%service_state)
            return 'unknown'
        try:
            f = open(service_state, 'r')
            buff = f.read().strip(' \n')
            f.close()
        except Exception as e:
            self.status_log(str(e))
            return 'unknown'
        if len(buff) < 1:
            self.status_log("%s is corrupted"%service_state)
            return 'unknown'
        if buff[0] in self.state:
            return self.state[buff[0]]
        else:
            return 'unknown'

    def service_action(self, command):
        cmd = [self.service_cmd, '-A', self.cluster_name(), command]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def freezestop(self):
        cmd = ['env']
        vars = ('EZ', 'EZ_BIN', 'EZ_SERVICES', 'EZ_NODES', 'EZ_MONITOR', 'EZ_LOG')
        for var in vars:
            cmd.append(var+'='+os.environ[var])
        cmd += [self.service_cmd, '-A', self.cluster_name(), 'freeze-stop']
        self.svc.node.cmdworker.enqueue(cmd)

    def process_running(self):
        # self.cfmon exist if OpenHA setup is done
        # os.path.exists(self.cfmon) return true if OpenHA setup is done
        # _not_ os.path.exists(self.cfmon) = True mean that setup is _not_ done
        # in this case, self.process_running() have to return _False_
        if not os.path.exists(self.cfmon):
            return False
        buff = ""
        with open(self.cfmon) as f:
            buff = f.read()
        daemons = []
        for line in buff.split('\n'):
            l = line.split()
            if len(l) < 4:
                continue
            if '#' in l[0]:
                continue
            if l[1] == 'net':
                suffix = ''
            elif l[1] == 'disk':
                suffix = '_raw'
            else:
                suffix = '_dio'
            if rcEnv.nodename == l[0]:
                daemon = self.heartd + suffix
                string = daemon + ' ' + ' '.join(l[2:-1])
                daemons.append(string)
            else:
                daemon = self.heartc + suffix
                string = daemon + ' ' + ' '.join(l[2:])
                daemons.append(string)
        daemons.append(self.nmond)
        (out, err, ret) = justcall(['ps', '-ef'])
        if ret != 0:
            return False
        h = {}
        for d in daemons:
            h[d] = 1
        # ckecking running daemons 
        lines = [ l for l in out.split('\n') if "heart" in l or "nmond" in l ]
        if which("pargs"):
            # solaris ps command truncates long lines
            # disk-based hb tend to have long args
            import re
            regex = re.compile('argv.*: ')
            for line in lines:
                v = line.split()
                if len(v) < 3:
                    continue
                pid = v[1]
                cmd = ['pargs', pid]
                out, err, ret = justcall(cmd)
                if ret != 0:
                    continue
                s = ' '.join([ regex.sub('', l) for l in out.split('\n') if l.startswith('argv') ])
		if s in h:
                    h[s] = 0
        else:
            for line in lines:
                for d in daemons:
                    if line.endswith(d):
                        h[d] = 0
        # now counting daemons not found as running
        total = 0
        for d in daemons:
            total += h[d]
        if total > 0:
            return False
        return True

    def need_stonith(self):
        status = self.service_remote_status()
        if status == 'unknown':
            return True
        return False

    def print_remote(self, out, err):
        peer = self.get_peer()
        s = out+err
        s = s.replace(self.svc.svcname.upper(), peer.upper()+"."+self.svc.svcname.upper())
        print(s)

    def freeze(self):
        """
          --remote disables the remote node status check
          it is set on remote actions
        """
        do_local = True
        do_remote = True
        local_status = self.service_local_status()
        remote_status = self.service_remote_status()
        peer = self.get_peer()
	self.log.debug('freeze: local_status=%s remote_status=%s'%(local_status, remote_status))

        if local_status in ['frozen_stop', 'start_ready']:
            self.log.info("local already frozen")
            do_local = False

        if not self.svc.remote and remote_status in ['frozen_stop', 'start_ready']:
            self.log.info("remote already frozen")
            do_remote = False
        if self.svc.remote:
            do_remote = False

        if not do_local and not do_remote:
            return

        if not self.svc.remote and remote_status == 'stopped' and local_status == 'started':
            out, err, ret = self.svc.remote_action(peer, "freeze --remote", sync=True)
            self.print_remote(out, err)
            do_remote = False
            if ret != 0:
                raise ex.excError(err)

        if local_status in ['stopped', 'start_failed', 'stop_failed']:
            self.service_action('freeze-stop')
            time.sleep(2)
        elif local_status in ['started']:
            self.service_action('freeze-start')
            time.sleep(2)

        if do_remote:
            out, err, ret = self.svc.remote_action(peer, "freeze --remote", sync=True)
            self.print_remote(out, err)
            if ret != 0:
                raise ex.excError(err)

        self.status(refresh=True)


    def thaw(self):
        """
          --remote disables the remote node status check
          it is set on remote actions
        """
        do_local = True
        do_remote = True
        local_status = self.service_local_status()
        remote_status = self.service_remote_status()
	self.log.debug('thaw: local_status=%s remote_status=%s'%(local_status, remote_status))
        peer = self.get_peer()

        if local_status not in ['frozen_stop', 'start_ready']:
            self.log.info("local already unfrozen")
            do_local = False

        if not self.svc.remote and remote_status not in ['frozen_stop', 'start_ready']:
            self.log.info("remote already unfrozen")
            do_remote = False
        if self.svc.remote:
            do_remote = False

        if not do_local and not do_remote:
            return

        if not self.svc.remote and remote_status == 'start_ready' and local_status == 'frozen_stop':
            out, err, ret = self.svc.remote_action(peer, "thaw --remote", sync=True)
            self.print_remote(out, err)
            do_remote = False
            if ret != 0:
                raise ex.excError(err)

        if local_status in ['frozen_stop', 'start_ready']:
            self.service_action('unfreeze')
            time.sleep(2)

        if do_remote:
            out, err, ret = self.svc.remote_action(peer, "thaw --remote", sync=True)
            self.print_remote(out, err)
            if ret != 0:
                raise ex.excError(err)

        self.status(refresh=True)


    def wait_for_state(self, states, timeout=10, remote=False):
        if remote:
            node = "remote"
        else:
            node = "local"
        self.log.info("waiting for %s state to become either %s"%(node, ' or '.join(states)))
        for i in range(timeout):
            if remote:
                status = self.service_remote_status()
            else:
                status = self.service_local_status()
            if status in states:
                return
            time.sleep(1)
        raise ex.excError("waited %d seconds for %s state to become either %s. current state: %s"%(timeout, node, ' or '.join(states), status))

    def switch(self):
        if self.svc.cluster:
            """
            if called by the heartbeat daemon, don't drive the hb service
            """
	    self.log.debug('switch : called by heartbeat daemon, returning.')
            return

        local_status = self.service_local_status()
        remote_status = self.service_remote_status()
	self.log.debug('switch: local_status=%s remote_status=%s'%(local_status, remote_status))
        peer = self.get_peer()
        if local_status == "started":
            try:
                self.stop()
            except ex.excEndAction:
                pass
            self.wait_for_state(["stopped", "stop_failed", "stopping", "frozen_stop"])
            local_status = self.service_local_status()
	    self.log.debug('switch [local was started]: local_status=%s'%(local_status))
            if local_status == "stop_failed":
                raise ex.excError("local state is stop_failed")
            remote_status = self.service_remote_status()
	    self.log.debug('switch [local was started]: remote_status=%s'%(remote_status))
            if not self.svc.remote and remote_status in ['stopped', 'frozen_stop']:
                out, err, ret = self.svc.remote_action(peer, "start --remote", sync=True)
                self.print_remote(out, err)
                if ret != 0:
                    raise ex.excError(err)
            self.wait_for_state(["started", "start_failed", "starting"], remote=True)
        elif remote_status == "started":
            if not self.svc.remote:
                out, err, ret = self.svc.remote_action(peer, "stop --remote", sync=True)
                self.print_remote(out, err)
                if ret != 0:
                    raise ex.excError(err)
            self.wait_for_state(["stopped", "stop_failed", "stopping", "frozen_stop"], remote=True)
            remote_status = self.service_remote_status()
	    self.log.debug('switch [remote was started]: remote_status=%s'%(remote_status))
            if remote_status == "stop_failed":
                raise ex.excError("remote state is stop_failed")
            self.start()
            self.wait_for_state(["started", "start_failed", "starting"])
        else:
            raise ex.excError("cannot switch in current state: %s/%s"%(local_state,remote_state))

        self.thaw()
        raise ex.excEndAction("heartbeat actions done")

    def start(self):
        if self.svc.cluster:
            """
            if called by the heartbeat daemon, don't drive the hb service
            """
	    self.log.debug('start : called by heartbeat daemon, returning.')
            return

        local_status = self.service_local_status()
        remote_status = self.service_remote_status()
	self.log.debug('start: local_status=%s remote_status=%s'%(local_status, remote_status))
        peer = self.get_peer()

        if remote_status == 'started':
            raise ex.excError("already started on peer node")

        if remote_status == 'start_failed':
            raise ex.excError("start_failed on peer node. please investigate the reason and freeze-stop the service on peer node before trying to start.")

        if remote_status == 'start_ready':
            raise ex.excError("start_ready on peer node. please stop the service on the peer node before trying to start.")

        if local_status == 'started':
            raise ex.excEndAction("already started")

        if local_status == 'start_failed':
            raise ex.excError("start_failed on local node. please investigate the reason and freeze-stop the service on local node before trying to start.")

        if not self.svc.remote and remote_status == 'stopped':
            out, err, ret = self.svc.remote_action(peer, "freeze --remote", sync=True)
            self.print_remote(out, err)
            do_remote = False
            if ret != 0:
                raise ex.excError(err)
            
        if local_status not in ('frozen_stop', 'start_ready'):
            raise ex.excError("%s on local node. unexpected state"%local_status)

        self.service_action("unfreeze")

        self.wait_for_state(['starting', 'started', 'start_failed'])
        self.thaw()
        
        raise ex.excEndAction("heartbeat actions done")

    def stop(self):
        if self.svc.cluster:
            """
            if called by the heartbeat daemon, don't drive the hb service
            """
	    self.log.debug('stop : called by heartbeat daemon, returning.')
            return

        local_status = self.service_local_status()
        remote_status = self.service_remote_status()
	self.log.debug('stop: local_status=%s remote_status=%s'%(local_status, remote_status))
        peer = self.get_peer()

        if not self.svc.remote and remote_status != 'frozen_stop':
            out, err, ret = self.svc.remote_action(peer, "stop --remote", sync=True)
            if ret != 0:
                raise ex.excError(err)

        if local_status == 'frozen_stop':
            raise ex.excEndAction("already frozen stop")

        self.svc.svcunlock()
        self.service_action('freeze-stop')

        raise ex.excEndAction("heartbeat actions done")

    def __status(self, verbose=False):
        if not os.path.exists(self.service_cmd):
            self.status_log("open-ha is not installed")
            return rcStatus.WARN
        if not self.process_running():
            self.status_log("open-ha daemons are not running")
            return rcStatus.WARN
        status = self.service_local_status()
        if status == 'unknown':
            self.status_log("unable to determine cluster service state")
            return rcStatus.WARN
        elif status in ['frozen_stop', 'start_failed', 'stop_failed', 'starting', 'start_ready', 'stopping', 'force_stop']:
            self.status_log("cluster service status is %s"%status)
            return rcStatus.WARN
        elif status in ['stopped']:
            return rcStatus.DOWN
        elif status in ['started']:
            return rcStatus.UP
        else:
            self.status_log("unknown cluster service status: %s"%status)
            return rcStatus.WARN
   0707010000bc2f000081a400000000000000000000000154fc7bfb00000884000000660000000300000000000000000000002200000000root/opt/opensvc/lib/resHbRhcs.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import resHb
import rcStatus
from rcGlobalEnv import rcEnv

class Hb(resHb.Hb):
    """ HeartBeat ressource
    """
    def __init__(self,
                 rid=None,
                 name=None,
                 always_on=set([]),
                 optional=False,
                 disabled=False,
                 restart=0,
                 subset=None,
                 tags=set([])):
        resHb.Hb.__init__(self,
                          rid, "hb.rhcs",
                          optional=optional,
                          disabled=disabled,
                          restart=restart,
                          subset=subset,
                          tags=tags,
                          always_on=always_on)
        self.label = name

    def _status(self, verbose=False):
        marker = 'service:'+self.svc.pkg_name
        for line in self.svc.clustat:
            l = line.split()
            if len(l) < 3:
                continue
            if marker != l[0].strip():
                continue

            # package found
            if rcEnv.nodename != self.svc.member_to_nodename(l[1].strip()):
                return rcStatus.DOWN
            elif l[-1].strip() != "started":
                return rcStatus.DOWN
            return rcStatus.UP

        # package not found
        return rcStatus.DOWN
0707010000bc30000081a400000000000000000000000154fc7bfb000007b4000000660000000300000000000000000000002000000000root/opt/opensvc/lib/resHbSg.py   #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import resHb
import rcStatus
from rcGlobalEnv import rcEnv

class Hb(resHb.Hb):
    """ HeartBeat ressource
    """
    def __init__(self,
                 rid=None,
                 name=None,
                 always_on=set([]),
                 optional=False,
                 disabled=False,
                 restart=0,
                 subset=None,
                 tags=set([])):
        resHb.Hb.__init__(self,
                          rid,
                          "hb.sg",
                          optional=optional,
                          disabled=disabled,
                          restart=restart,
                          subset=subset,
                          tags=tags,
                          always_on=always_on)
        self.label = name

    def __status(self, verbose=False):
        if 'node' in self.svc.cmviewcl and \
           rcEnv.nodename in self.svc.cmviewcl['node'] and \
           'status' in self.svc.cmviewcl['node'][rcEnv.nodename] and \
           self.svc.cmviewcl['node'][rcEnv.nodename]['status'] == "up":
            return rcStatus.UP
        return rcStatus.DOWN
0707010000bc31000081a400000000000000000000000154fc7bfb00000809000000660000000300000000000000000000002100000000root/opt/opensvc/lib/resHbVcs.py  #
# Copyright (c) 2013 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import resHb
import rcStatus
from rcGlobalEnv import rcEnv
import rcExceptions as ex

class Hb(resHb.Hb):
    def __init__(self,
                 rid=None,
                 name=None,
                 always_on=set([]),
                 optional=False,
                 disabled=False,
                 restart=0,
                 subset=None,
                 disabled=False,
                 tags=set([])):
        resHb.Hb.__init__(self,
                          rid,
                          "hb.vcs",
                          optional=optional,
                          disabled=disabled,
                          restart=restart,
                          subset=subset,
                          tags=tags,
                          always_on=always_on)
        self.label = name

    def _status(self, verbose=False):
        try:
            s = self.svc.get_grp_val('State').strip('|')
        except ex.excError as e:
            self.status_log(str(e))
            return rcStatus.WARN

        if s == "ONLINE":
            return rcStatus.UP
        elif s == "OFFLINE":
            return rcStatus.DOWN
        else:
            self.status_log(s)
            return rcStatus.WARN

   0707010000bc32000081a400000000000000000000000154fc7bfb00000d7f000000660000000300000000000000000000002100000000root/opt/opensvc/lib/resHbovm.py  #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import resHb
from rcGlobalEnv import rcEnv
import os
import rcStatus
import rcExceptions as ex
from rcUtilities import justcall, which
import rcOvm

class Hb(resHb.Hb):
    """ HeartBeat ressource
    """
    def __init__(self,
                 rid=None,
                 name=None,
                 always_on=set([]),
                 optional=False,
                 disabled=False,
                 restart=0,
                 subset=None,
                 tags=set([])):
        resHb.Hb.__init__(self,
                          rid,
                          "hb.ovm",
                          optional=optional,
                          disabled=disabled,
                          restart=restart,
                          subset=subset,
                          tags=tags,
                          always_on=always_on)
        self.ovsinit = os.path.join(os.sep, 'etc', 'init.d', 'ovs-agent')

    def process_running(self):
        cmd = [self.ovsinit, 'status']
        (out, err, ret) = justcall(cmd)
        if ret != 0:
            return False
        for line in out.split('\n'):
            if len(line) == 0:
                continue
            if not line.startswith('ok!'):
                return False
        return True

    def stop(self):
        try:
            self.manager = rcOvm.Ovm(log=self.log)
            for r in self.svc.get_resources('container.ovm'):
                self.manager.vm_disable_ha(r.name)
        except ex.excError as e:
            self.log.error(str(e))
            raise

    def start(self):
        try:
            self.manager = rcOvm.Ovm(log=self.log)
            for r in self.svc.get_resources('container.ovm'):
                self.manager.vm_enable_ha(r.name)
        except ex.excError as e:
            self.log.error(str(e))
            raise
        self.can_rollback = True

    def __status(self, verbose=False):
        if not os.path.exists(self.ovsinit):
            self.status_log("OVM agent is not installed")
            return rcStatus.WARN
        if not self.process_running():
            self.status_log("OVM agent daemons are not running")
            return rcStatus.WARN
        try:
            self.manager = rcOvm.Ovm(log=self.log)
            for r in self.svc.get_resources('container.ovm'):
                ha_enabled = self.manager.vm_ha_enabled(r.name)
        except ex.excError as e:
            self.status_log(str(e))
            return rcStatus.WARN
        if not ha_enabled:
            self.status_log("HA not enabled for this VM")
            return rcStatus.WARN
        return rcStatus.UP

 0707010000bc33000081a400000000000000000000000154fc7bfb00002c78000000660000000300000000000000000000001e00000000root/opt/opensvc/lib/resIp.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import resources as Res
from rcGlobalEnv import *

from rcUtilities import qcall, which
rcIfconfig = __import__('rcIfconfig'+rcEnv.sysname)
import socket
import rcStatus
import rcExceptions as ex
import os

class Ip(Res.Resource):
    """ basic ip resource
    """
    def __init__(self,
                 rid=None,
                 ipDev=None,
                 ipName=None, 
                 mask=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 always_on=set([]),
                 monitor=False,
                 restart=0,
                 subset=None,
                 gateway=None):
        Res.Resource.__init__(self,
                              rid,
                              "ip",
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset,
                              monitor=monitor,
                              restart=restart)
        self.ipDev=ipDev
        self.ipName=ipName
        self.mask=mask
        self.label = ipName + '@' + ipDev
        self.always_on = always_on
        self.gateway = gateway

    def getaddr(self):
        if hasattr(self, 'addr'):
            return
        try:
            a = socket.getaddrinfo(self.ipName, None)
            if len(a) == 0:
                raise Exception
            self.addr = a[0][4][0]
        except:
            if not self.disabled:
                raise ex.excError("could not resolve name %s"%self.ipName)

    def __str__(self):
        return "%s ipdev=%s ipname=%s" % (Res.Resource.__str__(self),\
                                         self.ipDev, self.ipName)
    def setup_environ(self):
        os.environ['OPENSVC_IPDEV'] = str(self.ipDev)
        os.environ['OPENSVC_IPNAME'] = str(self.ipName)
        os.environ['OPENSVC_MASK'] = str(self.mask)
        try:
            self.getaddr()
            os.environ['OPENSVC_IPADDR'] = str(self.addr)
        except:
            pass
        l = self.rid.split('#')
        if len(l) == 2:
            index = l[1]
        else:
            index = ''
        var = 'OPENSVC_IP'+index
        l = []
        for p in ['ipName', 'ipDev', 'addr', 'mask']:
            if hasattr(self, p):
                l.append(str(getattr(self, p)))
            else:
                l.append('unknown')
        val = ' '.join(l)
        os.environ[var] = val

    def _status(self, verbose=False):
        try:
            self.getaddr()
        except:
            self.status_log("could not resolve %s to an ip address"%self.ipName)
            return rcStatus.WARN
        try:
            if self.is_up():
                return self.status_stdby(rcStatus.UP)
            else:
                return self.status_stdby(rcStatus.DOWN)
        except ex.excNotSupported:
            self.status_log("not supported")
            return rcStatus.UNDEF

    def arp_announce(self):
        if ':' in self.addr:
            return
        if not which("arping"):
            self.log.warning("arp announce skipped. install 'arping'")
            return
        cmd = ["arping", "-U", "-c", "1", "-I", self.ipDev, "-s", self.addr, self.addr]
        self.log.info(' '.join(cmd))
        qcall(cmd)

    def abort_start(self):
        self.abort_start_done = True
        if 'nonrouted' in self.tags or 'noaction' in self.tags:
            return False
	if not hasattr(self, "addr"):
            return False
        if self.check_ping() and not self.is_up():
            return True
        return False

    def check_ping(self, count=1, timeout=5):
        raise ex.MissImpl('check_ping')

    def startip_cmd(self):
        raise ex.MissImpl('startip_cmd')

    def stopip_cmd(self):
        raise ex.MissImpl('stopip_cmd')

    def is_up(self):
        ifconfig = self.get_ifconfig()
        if ifconfig.has_param("ipaddr", self.addr) is not None or \
           ifconfig.has_param("ip6addr", self.addr) is not None:
            self.log.debug("%s@%s is up" % (self.addr, self.ipDev))
            return True
        self.log.debug("%s@%s is down" % (self.addr, self.ipDev))
        return False

    def allow_start(self):
        ifconfig = rcIfconfig.ifconfig()
        intf = ifconfig.interface(self.ipDev)
        if intf is None:
            self.log.error("interface %s not found. Cannot stack over it." % self.ipDev)
            raise ex.IpDevDown(self.ipDev)
        if not intf.flag_up:
            if hasattr(intf, 'groupname') and intf.groupname != "":
                l = [ i for i in ifconfig.get_matching_interfaces('groupname', intf.groupname) if i.flag_up]
                if len(l) == 1:
                    self.log.info("switch %s to valid alternate path %s" % (self.ipDev, l[0].name))
                    intf = l[0]
                    self.ipDev = l[0].name
            else:
                self.log.error("interface %s is not up. Cannot stack over it." % self.ipDev)
                raise ex.IpDevDown(self.ipDev)
        if self.is_up() is True:
            self.log.info("%s is already up on %s" % (self.addr, self.ipDev))
            raise ex.IpAlreadyUp(self.addr)
        if not hasattr(self, 'abort_start_done') and self.check_ping():
            self.log.error("%s is already up on another host" % (self.addr))
            raise ex.IpConflict(self.addr)
        return

    def lock(self, timeout=60, delay=1):
        import lock
        lockfile = os.path.join(rcEnv.pathlock, 'startip')
        lockfd = None
        try:
            lockfd = lock.lock(timeout=timeout, delay=delay, lockfile=lockfile)
        except lock.lockTimeout:
            self.log.error("timed out waiting for lock")
            raise ex.excError
        except lock.lockNoLockFile:
            self.log.error("lock_nowait: set the 'lockfile' param")
            raise ex.excError
        except lock.lockCreateError:
            self.log.error("can not create lock file %s"%lockfile)
            raise ex.excError
        except lock.lockAcquire as e:
            self.log.warn("another action is currently running (pid=%s)"%e.pid)
            raise ex.excError
        except ex.excSignal:
            self.log.error("interrupted by signal")
            raise ex.excError
        except:
            self.log.error("unexpected locking error")
            import traceback
            traceback.print_exc()
            raise ex.excError
        self.lockfd = lockfd

    def unlock(self):
        import lock
        lock.unlock(self.lockfd)

    def get_ifconfig(self):
        return rcIfconfig.ifconfig()

    def start(self):
        try:
            self.getaddr()
        except:
            self.log.error("could not resolve %s to an ip address"%self.ipName)
            raise ex.excError
        try:
            self.allow_start()
        except (ex.IpConflict, ex.IpDevDown):
            raise ex.excError
        except (ex.IpAlreadyUp, ex.IpNoActions):
            return
        self.log.debug('pre-checks passed')

        self.lock()
        ifconfig = self.get_ifconfig()
        if self.mask is None:
            intf = ifconfig.interface(self.ipDev)
            if intf is None:
                self.log.error("netmask parameter is mandatory with 'noalias' tag")
                self.unlock()
                raise ex.excError
            self.mask = intf.mask
        if self.mask == '':
            self.log.error("No netmask set on parent interface %s" % self.ipDev)
            self.unlock()
            raise ex.excError
        elif isinstance(self.mask, list):
            if len(self.mask) > 0:
                self.mask = self.mask[0]
            else:
                self.log.error("No netmask set on parent interface %s" % self.ipDev)
                self.unlock()
                raise ex.excError
        if 'noalias' in self.tags:
            self.stacked_dev = self.ipDev
        else:
            self.stacked_dev = ifconfig.get_stacked_dev(self.ipDev,\
                                                        self.addr,\
                                                        self.log)
        if self.stacked_dev is None:
            self.log.error("could not determine a stacked dev for parent interface %s" % self.ipDev)
            self.unlock()
            raise ex.excError

        arp_announce = True
        try:
            (ret, out, err) = self.startip_cmd()
            self.can_rollback = True
        except ex.excNotSupported:
            self.log.info("start ip not supported")
            ret = 0
            out = ""
            err = ""
            arp_announce = False
            pass

        self.unlock()
        if ret != 0:
            self.log.error("failed")
            raise ex.excError

        if arp_announce:
            self.arp_announce()

    def stop(self):
        try:
            self.getaddr()
        except:
            self.log.error("could not resolve %s to an ip address"%self.ipName)
            raise ex.excError
        if self.is_up() is False:
            self.log.info("%s is already down on %s" % (self.addr, self.ipDev))
            return
        ifconfig = self.get_ifconfig()
        if 'noalias' in self.tags:
            self.stacked_dev = self.ipDev
        else:
            self.stacked_dev = ifconfig.get_stacked_dev(self.ipDev,\
                                                        self.addr,\
                                                        self.log)
        if self.stacked_dev is None:
            raise ex.excError

        try:
            (ret, out, err) = self.stopip_cmd()
        except ex.excNotSupported:
            self.log.info("stop ip not supported")
            return

        if ret != 0:
            self.log.error("failed")
            raise ex.excError

        import time
        tmo = 15
        for i in range(tmo):
            if not self.check_ping(count=1, timeout=1):
                break
            time.sleep(1)

        if i == tmo-1:
            self.log.error("%s refuse to go down"%self.addr)
            raise ex.excError

    def provision(self):
        m = __import__("provIp")
        prov = m.ProvisioningIp(self)
        prov.provisioner()


if __name__ == "__main__":
    for c in (Ip,) :
        help(c)

    print("""i1=Ip("eth0","192.168.0.173")""")
    i=Ip("eth0","192.168.0.173")
    print("show i", i)
    print("""i.do_action("start")""")
    i.do_action("start")

0707010000bc34000081a400000000000000000000000154fc7bfb000007ec000000660000000300000000000000000000002100000000root/opt/opensvc/lib/resIpAIX.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.
"Module implement Linux specific ip management"

import resIp as Res
import rcExceptions as ex
from rcUtilitiesAIX import check_ping

class Ip(Res.Ip):
    def check_ping(self, count=1, timeout=5):
        self.log.info("checking %s availability"%self.addr)
        return check_ping(self.addr, count=count, timeout=timeout)

    def arp_announce(self):
        return

    def startip_cmd(self):
        if ':' in self.addr:
            if '.' in self.mask:
                self.log.error("netmask parameter is mandatory for ipv6 adresses")
                raise ex.excError
            cmd = ['ifconfig', self.ipDev, 'inet6', 'alias', '/'.join([self.addr, self.mask])]
        else:
            cmd = ['ifconfig', self.ipDev, self.addr, 'netmask', self.mask, 'alias']
        return self.vcall(cmd)

    def stopip_cmd(self):
        if ':' in self.addr:
            cmd = ['ifconfig', self.ipDev, 'inet6', self.addr, 'delete']
        else:
            cmd = ['ifconfig', self.ipDev, self.addr, 'delete']
        return self.vcall(cmd)



if __name__ == "__main__":
    for c in (Ip,) :
        help(c)

0707010000bc35000081a400000000000000000000000154fc7bfb0000188a000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resIpCrossbow.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.
"Module implement SunOS specific ip management"

import time
import resIpSunOS as Res
import rcExceptions as ex
from subprocess import *
from rcGlobalEnv import rcEnv
from rcUtilities import which
rcIfconfig = __import__('rcIfconfig'+rcEnv.sysname)

class Ip(Res.Ip):
    def __init__(self,
                 rid=None,
                 ipDev=None,
                 ipName=None,
                 mask=None,
                 always_on=set([]),
                 monitor=False,
                 restart=0,
                 subset=None,
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 gateway=None,
                 ipDevExt="v4"):
        self.ipDevExt = ipDevExt
        Res.Ip.__init__(self,
                        rid=rid,
                        ipDev=ipDev,
                        ipName=ipName,
                        mask=mask,
                        always_on=always_on,
                        disabled=disabled,
                        tags=tags,
                        optional=optional,
                        monitor=monitor,
                        restart=restart,
                        subset=subset,
                        gateway=gateway)
        self.label = self.label + "/" + self.ipDevExt
        self.type = "ip"
        if not which('ipadm'):
            raise ex.excInitError("crossbow ips are not supported on this system")
        if 'noalias' not in self.tags:
            self.tags.add('noalias')

    def stopip_cmd(self):
        ret, out, err = (0, '', '')
        if self.gateway is not None:
            cmd=['route', '-q', 'delete', 'default', self.gateway]
            r, o, e = self.call(cmd, info=True, outlog=False, errlog=False)
            ret += r
        cmd=['ipadm', 'delete-addr', self.stacked_dev+'/'+self.ipDevExt]
        r, o, e =  self.vcall(cmd)
        ret += r
        out += o
        err += e
        cmd = ['ipadm', 'show-addr', self.stacked_dev+'/'+self.ipDevExt ]
        p = Popen(cmd, stdin=None, stdout=PIPE, stderr=PIPE, close_fds=True)
        _out = p.communicate()[0].split("\n")
        if len(_out) >= 2:
            return ret, out, err
        cmd=['ipadm', 'delete-ip', self.stacked_dev]
        r, o, e =  self.vcall(cmd)
        ret += r
        out += o
        err += e
        return ret, out, err

    def _hexmask_to_str(self, mask):
        mask = mask.replace('0x', '')
        s = [str(int(mask[i:i+2], 16)) for i in range(0, len(mask), 2)]
        return '.'.join(s)

    def _dotted2cidr(self):
        if self.mask is None:
            return ''
        cnt = 0
        if '.' in self.mask:
            l = self.mask.split(".")
        else:
            l = self._hexmask_to_str(self.mask).split(".")
        for v in l:
            b = int(v)
            while b != 0:
                if b % 2 == 1:
                    cnt = cnt + 1
                    b = b / 2
        return '/'+str(cnt)
        
    def wait_net_smf(self, max_wait=30):
        r = 0
        prev_s = None
        while True:
            s = self.get_smf_status("network/routing-setup")
            if s == "online":
                break
            if s != prev_s or prev_s is None:
                self.log.info("waiting for network/routing-setup online state. current state: %s" % s)
            prev_s = s
            r += 1
            if r > max_wait:
                self.log.error("timeout waiting for network/routing-setup online state")
                break
            time.sleep(1)

    def get_smf_status(self, fmri):
        cmd = ["/usr/bin/svcs", "-H", "-o", "state", fmri]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return "undef"

    def startip_cmd(self):
        self.wait_net_smf()
        ret, out, err = (0, '', '')
        cmd = ['ipadm', 'show-if', self.stacked_dev]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
        _out = p.communicate()[0].split("\n")
        if len(_out) < 2:
            cmd=['ipadm', 'create-ip', '-t', self.stacked_dev ]
            r, o, e = self.vcall(cmd)
        cmd=['ipadm', 'create-addr', '-t', '-T', 'static', '-a', self.addr+self._dotted2cidr(), self.stacked_dev+'/'+self.ipDevExt]
        r, o, e = self.vcall(cmd)
        if r != 0:
            cmd=['ipadm', 'show-if' ]
            self.vcall(cmd)
            raise ex.excError("Interface %s is not up. ipadm cannot create-addr over it. Retrying..." % self.stacked_dev)
        ret += r
        out += o
        err += e
        if self.gateway is not None:
            cmd=['route', '-q', 'add', 'default', self.gateway]
            r, o, e = self.call(cmd, info=True, outlog=False, errlog=False)
            ret += r
        return ret, out, err

    def allow_start(self):
        if 'noaction' in self.tags:
            raise ex.IpNoActions(self.addr)
        retry = 10
        interval = 3
        import time
        ok = False
        if self.is_up() is True:
            self.log.info("%s is already up on %s" % (self.addr, self.ipDev))
            raise ex.IpAlreadyUp(self.addr)
        if not hasattr(self, 'abort_start_done') and 'nonrouted' not in self.tags and self.check_ping():
            self.log.error("%s is already up on another host" % (self.addr))
            raise ex.IpConflict(self.addr)

if __name__ == "__main__":
    for c in (Ip,) :
        help(c)

  0707010000bc36000081a400000000000000000000000154fc7bfb000007b2000000660000000300000000000000000000002400000000root/opt/opensvc/lib/resIpDarwin.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@opensvc.com>'
# Copyright (c) 2014 Arnaud Veron <arnaud.veron@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import resIp as Res
import rcExceptions as ex
from rcUtilitiesFreeBSD import check_ping

class Ip(Res.Ip):
    def check_ping(self, count=1, timeout=5):
        self.log.info("checking %s availability"%self.addr)
        return check_ping(self.addr, count=count, timeout=timeout)

    def arp_announce(self):
        return

    def startip_cmd(self):
        if ':' in self.addr:
            if '.' in self.mask:
                self.log.error("netmask parameter is mandatory for ipv6 adresses")
                raise ex.excError
            cmd = ['ifconfig', self.ipDev, 'inet6', '/'.join([self.addr, self.mask]), 'add']
        else:
            cmd = ['ifconfig', self.ipDev, 'inet', self.addr, 'netmask', '0xffffffff', 'add']
        return self.vcall(cmd)

    def stopip_cmd(self):
        if ':' in self.addr:
            cmd = ['ifconfig', self.ipDev, 'inet6', self.addr, 'delete']
        else:
            cmd = ['ifconfig', self.ipDev, 'inet', self.addr, 'delete']
        return self.vcall(cmd)



if __name__ == "__main__":
    for c in (Ip,) :
        help(c)

  0707010000bc37000081a400000000000000000000000154fc7bfb000007f4000000660000000300000000000000000000002500000000root/opt/opensvc/lib/resIpFreeBSD.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.
"Module implement Linux specific ip management"

import resIp as Res
import rcExceptions as ex
from rcUtilitiesFreeBSD import check_ping

class Ip(Res.Ip):
    def check_ping(self, count=1, timeout=5):
        self.log.info("checking %s availability"%self.addr)
        return check_ping(self.addr, count=count, timeout=timeout)

    def arp_announce(self):
        return

    def startip_cmd(self):
        if ':' in self.addr:
            if '.' in self.mask:
                self.log.error("netmask parameter is mandatory for ipv6 adresses")
                raise ex.excError
            cmd = ['ifconfig', self.ipDev, 'inet6', '/'.join([self.addr, self.mask]), 'add']
        else:
            cmd = ['ifconfig', self.ipDev, 'inet', self.addr, 'netmask', self.mask, 'add']
        return self.vcall(cmd)

    def stopip_cmd(self):
        if ':' in self.addr:
            cmd = ['ifconfig', self.ipDev, 'inet6', self.addr, 'delete']
        else:
            cmd = ['ifconfig', self.ipDev, 'inet', self.addr, 'delete']
        return self.vcall(cmd)



if __name__ == "__main__":
    for c in (Ip,) :
        help(c)

0707010000bc38000081a400000000000000000000000154fc7bfb00000804000000660000000300000000000000000000002300000000root/opt/opensvc/lib/resIpHP-UX.py    #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.
"Module implement Linux specific ip management"

import resIp as Res
u = __import__('rcUtilitiesHP-UX')

class Ip(Res.Ip):
    def check_ping(self, count=1, timeout=5):
        self.log.info("checking %s availability"%self.addr)
        return u.check_ping(self.addr, count=count, timeout=timeout)

    def arp_announce(self):
        """ arp_announce job is done by HP-UX ifconfig... """
        return

    def startip_cmd(self):
        if ':' in self.addr:
            cmd = ['ifconfig', self.ipDev, 'inet6', 'up']
            (ret, out, err) = self.vcall(cmd)
            if ret != 0:
                raise ex.excError
            cmd = ['ifconfig', self.stacked_dev, 'inet6', self.addr+'/'+self.mask, 'up']
        else:
            cmd = ['ifconfig', self.stacked_dev, self.addr, 'netmask', self.mask, 'up']
        return self.vcall(cmd)

    def stopip_cmd(self):
        if ':' in self.addr:
            cmd = ['ifconfig', self.stacked_dev, "inet6", "::"]
        else:
            cmd = ['ifconfig', self.stacked_dev, "0.0.0.0"]
        return self.vcall(cmd)



if __name__ == "__main__":
    for c in (Ip,) :
        help(c)

0707010000bc39000081a400000000000000000000000154fc7bfb000009c9000000660000000300000000000000000000002300000000root/opt/opensvc/lib/resIpLinux.py    #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.
"Module implement Linux specific ip management"

import resIp as Res
import rcExceptions as ex
from rcUtilitiesLinux import check_ping

class Ip(Res.Ip):
    def check_ping(self, timeout=5, count=1):
        self.log.info("checking %s availability"%self.addr)
        return check_ping(self.addr, timeout=timeout, count=count)

    def startip_cmd(self):
        if ':' in self.addr:
            if '.' in self.mask:
                self.log.error("netmask parameter is mandatory for ipv6 adresses")
                raise ex.excError
            cmd = ['ifconfig', self.ipDev, 'inet6', 'add', '/'.join([self.addr, self.mask])]
        else:
            cmd = ['ifconfig', self.stacked_dev, self.addr, 'netmask', self.mask, 'up']

        ret, out, err = self.vcall(cmd)
        if ret != 0:
            return ret, out, err

        # ip activation may still be incomplete
        # wait for activation, to avoid startapp scripts to fail binding their listeners
        for i in range(5, 0, -1):
            if check_ping(self.addr, timeout=1, count=1):
                return ret, out, err
        self.log.error("timed out waiting for ip activation")
        raise ex.excError

    def stopip_cmd(self):
        if ':' in self.addr:
            cmd = ['ifconfig', self.ipDev, 'inet6', 'del', '/'.join([self.addr, self.mask])]
        else:
            if self.stacked_dev is None:
                return 1, "", "no stacked dev found"
            cmd = ['ifconfig', self.stacked_dev, 'down']
        return self.vcall(cmd)



if __name__ == "__main__":
    for c in (Ip,) :
        help(c)

   0707010000bc3a000081a400000000000000000000000154fc7bfb000007b8000000660000000300000000000000000000002200000000root/opt/opensvc/lib/resIpOSF1.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.
"Module implement Linux specific ip management"

import resIp as Res
import rcExceptions as ex
from rcUtilitiesOSF1 import check_ping

class Ip(Res.Ip):
    def check_ping(self, count=1, timeout=5):
        self.log.info("checking %s availability"%self.addr)
        return check_ping(self.addr, count=count, timeout=timeout)

    def arp_announce(self):
        return

    def startip_cmd(self):
        if ':' in self.addr:
            if '.' in self.mask:
                self.log.error("netmask parameter is mandatory for ipv6 adresses")
                raise ex.excError
            cmd = ['ifconfig', self.ipDev, 'inet6', '/'.join([self.addr, self.mask]), 'add']
        else:
            cmd = ['ifconfig', self.ipDev, 'inet', 'alias', self.addr, 'netmask', self.mask]
        return self.vcall(cmd)

    def stopip_cmd(self):
        if ':' in self.addr:
            cmd = ['ifconfig', self.ipDev, 'inet6', self.addr, 'delete']
        else:
            cmd = ['ifconfig', self.ipDev, 'inet', '-alias', self.addr]
        return self.vcall(cmd)



if __name__ == "__main__":
    for c in (Ip,) :
        help(c)

0707010000bc3b000081a400000000000000000000000154fc7bfb0000058e000000660000000300000000000000000000002700000000root/opt/opensvc/lib/resIpRhcsLinux.py    #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

Res = __import__("resIpLinux")

import rcStatus
from rcUtilities import justcall

class Ip(Res.Ip):

    def start(self):
        return 0

    def stop(self):
        return 0

    def _status(self, verbose=False):
        cmd = ['ip', 'addr', 'ls']
        out, err, ret = justcall(cmd)
        if ret != 0:
            self.rstatus_log("%s exec failed"%' '.join(cmd))
            return rcStatus.WARN
        if " "+self.addr+"/" in out:
            return rcStatus.UP
        return rcStatus.DOWN

if __name__ == "__main__":
    for c in (Ip,) :
        help(c)

  0707010000bc3c000081a400000000000000000000000154fc7bfb00000418000000660000000300000000000000000000002500000000root/opt/opensvc/lib/resIpSgHP-UX.py  #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

Res = __import__("resIpHP-UX")

class Ip(Res.Ip):

    def start(self):
        return 0

    def stop(self):
        return 0

if __name__ == "__main__":
    for c in (Ip,) :
        help(c)

0707010000bc3d000081a400000000000000000000000154fc7bfb00000418000000660000000300000000000000000000002500000000root/opt/opensvc/lib/resIpSgLinux.py  #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

Res = __import__("resIpLinux")

class Ip(Res.Ip):

    def start(self):
        return 0

    def stop(self):
        return 0

if __name__ == "__main__":
    for c in (Ip,) :
        help(c)

0707010000bc3e000081a400000000000000000000000154fc7bfb000006f7000000660000000300000000000000000000002300000000root/opt/opensvc/lib/resIpSunOS.py    #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.
"Module implement SunOS specific ip management"

import resIp as Res
from subprocess import *
from rcUtilitiesSunOS import check_ping
import rcExceptions as ex

class Ip(Res.Ip):
    """ define ip SunOS start/stop doAction """

    def arp_announce(self):
        """ arp_announce job is done by SunOS ifconfig... """
        return

    def check_ping(self, count=1, timeout=2):
        self.log.info("checking %s availability"%self.addr)
        return check_ping(self.addr, timeout=timeout)

    def startip_cmd(self):
        cmd=['/usr/sbin/ifconfig', self.stacked_dev, 'plumb', self.addr, \
            'netmask', '+', 'broadcast', '+', 'up']
        return self.vcall(cmd)

    def stopip_cmd(self):
        cmd = ['/usr/sbin/ifconfig', self.stacked_dev, 'unplumb']
        return self.vcall(cmd)

if __name__ == "__main__":
    for c in (Ip,) :
        help(c)

 0707010000bc3f000081a400000000000000000000000154fc7bfb00000418000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resIpVcsLinux.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

Res = __import__("resIpLinux")

class Ip(Res.Ip):

    def start(self):
        return 0

    def stop(self):
        return 0

if __name__ == "__main__":
    for c in (Ip,) :
        help(c)

0707010000bc40000081a400000000000000000000000154fc7bfb00000e04000000660000000300000000000000000000002200000000root/opt/opensvc/lib/resIpZone.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@opensvc.com>
# Copyright (c) 2014 Arnaud Veron <arnaud.veron@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.
"Module implement SunOS specific ip management"

import time
import resIpSunOS as Res
import rcExceptions as ex
from subprocess import *
from rcGlobalEnv import rcEnv
rcIfconfig = __import__('rcIfconfig'+rcEnv.sysname)

class Ip(Res.Ip):
    def __init__(self,
                 rid=None,
                 ipDev=None,
                 ipName=None,
                 zone=None,
                 mask=None,
                 always_on=set([]),
                 monitor=False,
                 restart=0,
                 subset=None,
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 gateway=None):
        Res.Ip.__init__(self,
                        rid=rid,
                        ipDev=ipDev,
                        ipName=ipName,
                        mask=mask,
                        always_on=always_on,
                        disabled=disabled,
                        tags=tags,
                        optional=optional,
                        monitor=monitor,
                        restart=restart,
                        subset=subset,
                        gateway=gateway)
        self.zone = zone
        self.tags.add(zone)
        self.tags.add('zone')

    def startip_cmd(self):
        cmd=['ifconfig', self.stacked_dev, 'plumb', self.addr, \
             'netmask', '+', 'broadcast', '+', 'up' , 'zone' , self.zone ]
        return self.vcall(cmd)

    def stopip_cmd(self):
        cmd=['ifconfig', self.stacked_dev, 'unplumb']
        return self.vcall(cmd)

    def allow_start(self):
        retry = 1
        interval = 0
        import time
        ok = False
        if 'noalias' not in self.tags:
            for i in range(retry):
                ifconfig = rcIfconfig.ifconfig()
                intf = ifconfig.interface(self.ipDev)
                if intf is not None and intf.flag_up:
                    ok = True
                    break
                time.sleep(interval)
            if not ok:
                self.log.error("Interface %s is not up. Cannot stack over it." % self.ipDev)
                raise ex.IpDevDown(self.ipDev)
        if self.is_up() is True:
            self.log.info("%s is already up on %s" % (self.addr, self.ipDev))
            raise ex.IpAlreadyUp(self.addr)
        if not hasattr(self, 'abort_start_done') and 'nonrouted' not in self.tags and self.check_ping():
            self.log.error("%s is already up on another host" % (self.addr))
            raise ex.IpConflict(self.addr)
        return

if __name__ == "__main__":
    for c in (Ip,) :
        help(c)

0707010000bc41000081a400000000000000000000000154fc7bfb00000881000000660000000300000000000000000000002000000000root/opt/opensvc/lib/resLoop.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import resources as Res
from rcGlobalEnv import rcEnv

class Loop(Res.Resource):
    """ basic loopback device resource
    """
    def __init__(self,
                 rid=None,
                 loopFile=None,
                 always_on=set([]),
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        Res.Resource.__init__(self,
                              rid,
                              "disk.loop",
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              monitor=monitor,
                              restart=restart,
                              subset=subset)
        self.loopFile = loopFile
        self.label = loopFile
        self.always_on = always_on

    def __str__(self):
        return "%s loopfile=%s" % (Res.Resource.__str__(self),\
                                 self.loopFile)

if __name__ == "__main__":
    for c in (Loop,) :
        help(c)

    print("""v1=vg("myvg")""")
    v=vg("myvg")
    print("show v", v)
    print("""v.do_action("start")""")
    v.do_action("start")

   0707010000bc42000081a400000000000000000000000154fc7bfb00000bb1000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resLoopDarwin.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@opensvc.com>'
# Copyright (c) 2014 Arnaud Veron <arnaud.veron@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import re

from rcGlobalEnv import *
from rcUtilities import call, which
import rcStatus
import resLoop as Res
import rcExceptions as ex
from rcLoopDarwin import file_to_loop

class Loop(Res.Loop):
    def is_up(self):
        """Returns True if the loop group is present and activated
        """
        self.loop = file_to_loop(self.loopFile)
        if len(self.loop) == 0:
            return False
        return True

    def start(self):
        if self.is_up():
            self.log.info("%s is already up" % self.loopFile)
            return
        cmd = ['hdiutil', 'attach', self.loopFile]
        (ret, out, err) = self.call(cmd, info=True, outlog=False)
        if ret != 0:
            raise ex.excError
        self.loop = file_to_loop(self.loopFile)
        self.log.info("%s now loops to %s" % (', '.join(self.loop), self.loopFile))
        self.can_rollback = True

    def stop(self):
        if not self.is_up():
            self.log.info("%s is already down" % self.loopFile)
            return 0
        for loop in self.loop:
            cmd = ['hdiutil', 'detach', loop.strip('md')]
            (ret, out, err) = self.vcall(cmd)
            if ret != 0:
                raise ex.excError

    def _status(self, verbose=False):
        if self.is_up():
            return self.status_stdby(rcStatus.UP)
        else:
            return self.status_stdby(rcStatus.DOWN)

    def __init__(self,
                 rid,
                 loopFile,
                 always_on=set([]),
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        Res.Loop.__init__(self,
                          rid,
                          loopFile,
                          always_on=always_on,
                          disabled=disabled,
                          tags=tags,
                          optional=optional,
                          monitor=monitor,
                          restart=restart,
                          subset=subset)

   0707010000bc43000081a400000000000000000000000154fc7bfb00000e18000000660000000300000000000000000000002700000000root/opt/opensvc/lib/resLoopFreeBSD.py    #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import re

from rcGlobalEnv import *
from rcUtilities import call, which
import rcStatus
import resLoop as Res
import rcExceptions as ex

def file_to_loop(f):
    """Given a file path, returns the loop device associated. For example,
    /path/to/file => /dev/loop0
    """
    if which('mdconfig') is None:
        return []
    if not os.path.isfile(f):
        return []
    (ret, out, err) = call(['mdconfig', '-l', '-v'])
    if ret != 0:
        return []
    """ It's possible multiple loopdev are associated with the same file
    """
    devs= []
    for line in out.split('\n'):
        l = line.split()
        if len(l) < 4:
            continue
        path = ' '.join(l[3:])
        if path != f:
            continue
        if not os.path.exists('/dev/'+l[0]):
            continue
        devs.append(l[0])
    return devs

class Loop(Res.Loop):
    def is_up(self):
        """Returns True if the loop group is present and activated
        """
        self.loop = file_to_loop(self.loopFile)
        if len(self.loop) == 0:
            return False
        return True

    def start(self):
        if self.is_up():
            self.log.info("%s is already up" % self.loopFile)
            return
        cmd = ['mdconfig', '-a', '-t', 'vnode', '-f', self.loopFile]
        (ret, out, err) = self.call(cmd, info=True, outlog=False)
        if ret != 0:
            raise ex.excError
        self.loop = file_to_loop(self.loopFile)
        self.log.info("%s now loops to %s" % (', '.join(self.loop), self.loopFile))
        self.can_rollback = True

    def stop(self):
        if not self.is_up():
            self.log.info("%s is already down" % self.loopFile)
            return 0
        for loop in self.loop:
            cmd = ['mdconfig', '-d', '-u', loop.strip('md')]
            (ret, out, err) = self.vcall(cmd)
            if ret != 0:
                raise ex.excError

    def _status(self, verbose=False):
        if self.is_up():
            return self.status_stdby(rcStatus.UP)
        else:
            return self.status_stdby(rcStatus.DOWN)

    def __init__(self,
                 rid,
                 loopFile,
                 always_on=set([]),
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        Res.Loop.__init__(self,
                          rid,
                          loopFile,
                          always_on=always_on,
                          disabled=disabled,
                          tags=tags,
                          optional=optional,
                          monitor=monitor,
                          restart=restart,
                          subset=subset)
0707010000bc44000081a400000000000000000000000154fc7bfb00000b89000000660000000300000000000000000000002500000000root/opt/opensvc/lib/resLoopLinux.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import re

from rcGlobalEnv import *
from rcUtilities import call, which
import rcStatus
import resLoop as Res
import rcExceptions as ex
from rcLoopLinux import file_to_loop

class Loop(Res.Loop):
    def is_up(self):
        """Returns True if the loop group is present and activated
        """
        self.loop = file_to_loop(self.loopFile)
        if len(self.loop) == 0:
            return False
        return True

    def start(self):
        if self.is_up():
            self.log.info("%s is already up" % self.loopFile)
            return
        cmd = [ 'losetup', '-f', self.loopFile ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        self.loop = file_to_loop(self.loopFile)
        self.log.info("%s now loops to %s" % (', '.join(self.loop), self.loopFile))
        self.can_rollback = True

    def stop(self):
        if not self.is_up():
            self.log.info("%s is already down" % self.loopFile)
            return 0
        for loop in self.loop:
            cmd = [ 'losetup', '-d', loop ]
            (ret, out, err) = self.vcall(cmd)
            if ret != 0:
                raise ex.excError

    def _status(self, verbose=False):
        if self.is_up(): return rcStatus.UP
        else: return rcStatus.DOWN

    def __init__(self,
                 rid,
                 loopFile,
                 always_on=set([]),
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        Res.Loop.__init__(self,
                          rid,
                          loopFile,
                          always_on=always_on,
                          disabled=disabled,
                          tags=tags,
                          subset=subset,
                          optional=optional,
                          monitor=monitor,
                          restart=restart)

    def provision(self):
        m = __import__("provLoopLinux")
        prov = m.ProvisioningLoop(self)
        prov.provisioner()

   0707010000bc45000081a400000000000000000000000154fc7bfb0000087e000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resLvVcsLinux.py #
# Copyright (c) 2013 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import resources as Res
import rcStatus
import rcExceptions as ex

class Lv(Res.Resource):
    def __init__(self,
                 rid=None,
                 vgname=None,
                 lvname=None,
                 always_on=set([]),
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        Res.Resource.__init__(self,
                              rid,
                              "disk.lv",
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              monitor=monitor,
                              restart=restart,
                              subset=subset)
        self.name = '@'.join((lvname, vgname))
        self.label = self.name
        self.always_on = always_on

    def start(self):
        pass

    def stop(self):
        pass

    def _status(self, verbose=False):
        try:
            s = self.svc.get_res_val(self.vcs_name, 'State')
        except ex.excError as e:
            self.status_log(str(e))
            return rcStatus.WARN

        if s == "ONLINE":
            return rcStatus.UP
        elif s == "OFFLINE":
            return rcStatus.DOWN
        else:
            self.status_log(s)
            return rcStatus.WARN

  0707010000bc46000081a400000000000000000000000154fc7bfb00001aae000000660000000300000000000000000000002100000000root/opt/opensvc/lib/resMount.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import resources as Res
import os
import rcExceptions as ex
import rcStatus
from rcGlobalEnv import rcEnv
from rcUtilities import which

class Mount(Res.Resource):
    """Define a mount resource 
    """
    def __init__(self,
                 rid=None,
                 mountPoint=None,
                 device=None,
                 fsType=None,
                 mntOpt=None,
                 snap_size=None,
                 always_on=set([]),
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        Res.Resource.__init__(self,
                              rid=rid,
                              type="fs",
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              monitor=monitor,
                              restart=restart,
                              subset=subset)
        self.mountPoint = mountPoint
        self.device = device
        self.fsType = fsType
        self.mntOpt = mntOpt
        self.snap_size = snap_size
        self.always_on = always_on
        self.label = device + '@' + mountPoint
        self.fsck_h = {}
        self.testfile = os.path.join(mountPoint, '.opensvc')
        self.netfs = ['nfs', 'nfs4', 'cifs', 'smbfs', '9pfs', 'gpfs', 'afs', 'ncpfs']

    def start(self):
        if self.fsType in ["zfs", "advfs"] + self.netfs:
            return
        if self.device == "none":
            # for pseudo fs
            return
        if not os.path.exists(self.device):
            self.log.error("device does not exist %s" % self.device)
            raise ex.excError
        if not os.path.exists(self.mountPoint):
            try:
                os.makedirs(self.mountPoint)
            except:
                self.log.info("failed to create missing mountpoint %s" % self.mountPoint)
                raise
            self.log.info("create missing mountpoint %s" % self.mountPoint)

    def fsck(self):
        if self.fsType in ("", "none"):
            # bind mounts are in this case
            return
        if self.fsType not in self.fsck_h:
            self.log.info("fsck not implemented for %s"%self.fsType)
            return
        bin = self.fsck_h[self.fsType]['bin']
        if which(bin) is None:
            self.log.warning("%s not found. bypass."%self.fsType)
            return
        if self.fsck_h[self.fsType].has_key('reportcmd'):
            cmd = self.fsck_h[self.fsType]['reportcmd']
            (ret, out, err) = self.vcall(cmd, err_to_info=True)
            if ret not in self.fsck_h[self.fsType]['reportclean']:
                return
        cmd = self.fsck_h[self.fsType]['cmd']
        (ret, out, err) = self.vcall(cmd)
        if 'allowed_ret' in self.fsck_h[self.fsType]:
            allowed_ret = self.fsck_h[self.fsType]['allowed_ret']
        else:
            allowed_ret = [0]
        if ret not in allowed_ret:
            raise ex.excError 

    def need_check_writable(self):
        if 'ro' in self.mntOpt.split(','):
            return False
        if self.fsType in self.netfs:
            return False
        return True

    def can_check_writable(self):
        """ orverload in child classes to check os-specific conditions
            when a write test might hang (solaris lockfs, linux multipath
            with queueing on and no active path)
        """
        return True

    def check_writable(self):
        if not self.can_check_writable():
            return False
        try:
            f = open(self.testfile, 'w')
            f.write(' ')
            f.close()
        except IOError as e:
            if e.errno == 28:
                self.log.error('No space left on device. Invalidate writable test.')
                return True
            return False
        except:
            return False
        return True

    def _status(self, verbose=False):
        if rcEnv.nodename in self.always_on:
            if self.is_up():
                if self.need_check_writable() and not self.check_writable():
                    self.status_log("fs is not writable")
                    return rcStatus.WARN
                return rcStatus.STDBY_UP
            else:
                return rcStatus.STDBY_DOWN
        else:
            if self.is_up():
                if self.need_check_writable() and not self.check_writable():
                    self.status_log("fs is not writable")
                    return rcStatus.WARN
                return rcStatus.UP
            else:
                return rcStatus.DOWN

    def devlist(self):
        pseudofs = [
          'lofs',
          'none',
          'proc',
          'sysfs',
        ]
        if self.fsType in pseudofs + self.netfs:
            return set([])
        for res in self.svc.get_resources():
            if hasattr(res, "is_child_dev") and res.is_child_dev(self.device):
                # don't account fs device if the parent resource is driven by the service
                return set([])
        return set([self.device])

    def __str__(self):
        return "%s mnt=%s dev=%s fsType=%s mntOpt=%s" % (Res.Resource.__str__(self),\
                self.mountPoint, self.device, self.fsType, self.mntOpt)

    def __cmp__(self, other):
        """order so that deepest mountpoint can be umount first
        """
        return cmp(self.mountPoint, other.mountPoint)

    def provision(self):
        t = self.fsType[0].upper()+self.fsType[1:].lower()
        m = __import__("provFs"+t)
        prov = getattr(m, "ProvisioningFs"+t)(self)
        prov.provisioner()

if __name__ == "__main__":
    for c in (Mount,) :
        help(c)
    print("""   m=Mount("/mnt1","/dev/sda1","ext3","rw")   """)
    m=Mount("/mnt1","/dev/sda1","ext3","rw")
    print("show m", m)


  0707010000bc47000081a400000000000000000000000154fc7bfb000016cd000000660000000300000000000000000000002400000000root/opt/opensvc/lib/resMountAIX.py   #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import os

import rcMountsAIX as rcMounts
import resMount as Res
from rcUtilities import qcall, protected_mount, getmount
from rcGlobalEnv import rcEnv
import rcExceptions as ex
from stat import *

def try_umount(self):
    cmd = ['umount', self.mountPoint]
    (ret, out, err) = self.vcall(cmd, err_to_info=True)
    if ret == 0:
        return 0

    """ don't try to kill process using the source of a 
        protected bind mount
    """
    if protected_mount(self.mountPoint):
        return 1

    """ best effort kill of all processes that might block
        the umount operation. The priority is given to mass
        action reliability, ie don't contest oprator's will
    """
    cmd = ['sync']
    (ret, out, err) = self.vcall(cmd, err_to_info=True)

    for i in range(4):
        cmd = ['fuser', '-k', '-x', '-c', self.mountPoint]
        (ret, out, err) = self.vcall(cmd, err_to_info=True)
        self.log.info('umount %s'%self.mountPoint)
        cmd = ['umount', self.mountPoint]
        ret = qcall(cmd)
        if ret == 0:
            break

    return ret


class Mount(Res.Mount):
    """ define Linux mount/umount doAction """
    def __init__(self,
                 rid,
                 mountPoint,
                 device,
                 fsType,
                 mntOpt,
                 snap_size=None,
                 always_on=set([]),
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        self.Mounts = None
        Res.Mount.__init__(self,
                           rid,
                           mountPoint,
                           device,
                           fsType,
                           mntOpt,
                           snap_size,
                           always_on,
                           disabled=disabled,
                           tags=tags,
                           optional=optional,
                           monitor=monitor,
                           restart=restart,
                           subset=subset)
        self.fsck_h = {
            'jfs': {'bin': 'fsck', 'cmd': ['fsck', '-p', '-V', 'jfs', self.device]},
            'jfs2': {'bin': 'fsck', 'cmd': ['fsck', '-p', '-V', 'jfs2', self.device]},
        }

    def is_up(self):
        self.Mounts = rcMounts.Mounts()
        return self.Mounts.has_mount(self.device, self.mountPoint)

    def realdev(self):
        try:
            mode = os.stat(self.device)[ST_MODE]
        except:
            self.log.debug("can not stat %s" % self.device)
            return None
        if S_ISBLK(mode):
            dev = self.device
        else:
            mnt = getmount(self.device)
            if self.Mounts is None:
                self.Mounts = rcMounts.Mounts()
            m = self.Mounts.has_param("mnt", mnt)
            if m is None:
                self.log.debug("can't find dev %(dev)s mounted in %(mnt)s in mnttab"%dict(mnt=mnt, dev=self.device))
                return None
            dev = m.dev

        return dev

    def mplist(self):
        dev = self.realdev()
        if dev is None:
            return set([])

        return self._mplist([dev])

    def _mplist(self, devs):
        mps = set([])
        return mps

    def disklist(self):
        dev = self.realdev()
        if dev is None:
            return set([])
        return set([])

    def can_check_writable(self):
        return True

    def start(self):
        if self.Mounts is None:
            self.Mounts = rcMounts.Mounts()
        Res.Mount.start(self)
        if self.is_up() is True:
            self.log.info("fs(%s %s) is already mounted"%
                (self.device, self.mountPoint))
            return 0
        self.fsck()
        if not os.path.exists(self.mountPoint):
            os.makedirs(self.mountPoint, 0o755)
        if self.fsType != "":
            fstype = ['-v', self.fsType]
        else:
            fstype = []
        if self.mntOpt != "":
            mntopt = ['-o', self.mntOpt]
        else:
            mntopt = []
        cmd = ['mount']+fstype+mntopt+[self.device, self.mountPoint]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        self.Mounts = None
        self.can_rollback = True

    def stop(self):
        if self.Mounts is None:
            self.Mounts = rcMounts.Mounts()
        if self.is_up() is False:
            self.log.info("fs(%s %s) is already umounted"%
                    (self.device, self.mountPoint))
            return
        for i in range(3):
            ret = try_umount(self)
            if ret == 0: break
        if ret != 0:
            self.log.error('failed to umount %s'%self.mountPoint)
            raise ex.excError
        self.Mounts = None

if __name__ == "__main__":
    for c in (Mount,) :
        help(c)

   0707010000bc48000081a400000000000000000000000154fc7bfb00001fc9000000660000000300000000000000000000002700000000root/opt/opensvc/lib/resMountDarwin.py    #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@opensvc.com>'
# Copyright (c) 2014 Arnaud Veron <arnaud.veron@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os

import rcMountsDarwin as rcMounts
import resMount as Res
from rcUtilities import qcall, protected_mount, getmount
from rcGlobalEnv import rcEnv
from rcLoopDarwin import file_to_loop
import rcExceptions as ex
from stat import *

def try_umount(self):
    cmd = ['diskutil', 'umount', self.mountPoint]
    (ret, out, err) = self.vcall(cmd, err_to_info=True)
    if ret == 0:
        return 0

    cmd = ['diskutil', 'umount', 'force', self.mountPoint]
    (ret, out, err) = self.vcall(cmd, err_to_info=True)
    if ret == 0:
        return 0

    cmd = ['umount', self.mountPoint]
    (ret, out, err) = self.vcall(cmd, err_to_info=True)
    if ret == 0:
        return 0

    """ don't try to kill process using the source of a 
        protected bind mount
    """
    if protected_mount(self.mountPoint):
        return 1

    """ best effort kill of all processes that might block
        the umount operation. The priority is given to mass
        action reliability, ie don't contest oprator's will
    """
    cmd = ['sync']
    (ret, out, err) = self.vcall(cmd, err_to_info=True)

    for i in range(4):
        nb_killed = self.killfuser(self.mountPoint)
        self.log.info('umount %s'%self.mountPoint)
        cmd = ['umount', self.mountPoint]
        ret = qcall(cmd)
        if ret == 0 or nb_killed == 0:
            break

    if ret != 0:
        self.log.info("no more process using %s, yet umount fails. try forced umount."%self.mountPoint)
        cmd = ['umount', '-f', self.mountPoint]
        (ret, out, err) = self.vcall(cmd, err_to_info=True)

    return ret


class Mount(Res.Mount):
    """ define FreeBSD mount/umount doAction """
    def __init__(self,
                 rid,
                 mountPoint,
                 device,
                 fsType,
                 mntOpt,
                 snap_size=None,
                 always_on=set([]),
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        self.Mounts = None
        self.loopdevice = None
        self.isloop = False
        Res.Mount.__init__(self,
                           rid,
                           mountPoint,
                           device,
                           fsType,
                           mntOpt,
                           snap_size,
                           always_on,
                           disabled=disabled,
                           tags=tags,
                           optional=optional,
                           monitor=monitor,
                           restart=restart,
                           subset=subset)
        self.fsck_h = {
            'hfs': {'bin': 'fsck', 'cmd': ['diskutil', 'repairVolume', self.device]},
        }

    def killfuser(self, dir):
        cmd = ['fuser', '-kmc', dir]
        (ret, out, err) = self.vcall(cmd, err_to_info=True)

        """ return the number of process we sent signal to
        """
        l = out.split(':')
        if len(l) < 2:
            return 0
        return len(l[1].split())

    def is_up(self):
        self.Mounts = rcMounts.Mounts()
        ret = self.Mounts.has_mount(self.device, self.mountPoint)
        if ret:
            return True

        if self.fsType not in self.netfs:
            try:
                st = os.stat(self.device)
                mode = st[ST_MODE]
            except:
                self.log.debug("can not stat %s" % self.device)
                return False

            if S_ISREG(mode):
                # might be a loopback mount
                devs = file_to_loop(self.device)
                for dev in devs:
                    ret = self.Mounts.has_mount(dev, self.mountPoint)
                    if ret:
                        return True

        return False

    def realdev(self):
        dev = None
        try:
            mode = os.stat(self.device)[ST_MODE]
        except:
            self.log.debug("can not stat %s" % self.device)
            return None
        if S_ISCHR(mode):
            dev = self.device
        else:
            mnt = getmount(self.device)
            if self.Mounts is None:
                self.Mounts = rcMounts.Mounts()
            m = self.Mounts.has_param("mnt", mnt)
            if m is None:
                self.log.debug("can't find dev %(dev)s mounted in %(mnt)s in mnttab"%dict(mnt=mnt, dev=self.device))
                return None
            dev = m.dev

        return dev

    def disklist(self):
        dev = self.realdev()
        if dev is None:
            return set([])

        try:
            statinfo = os.stat(dev)
        except:
            self.log.error("can not stat %s" % dev)
            raise ex.excError

        return set([dev])

    def can_check_writable(self):
        return True

    def start(self):
        if self.Mounts is None:
            self.Mounts = rcMounts.Mounts()
        Res.Mount.start(self)

        if self.fsType in self.netfs or self.device == "none":
            # TODO showmount -e
            pass
        else:
            try:
                mode = os.stat(self.device)[ST_MODE]
                if S_ISREG(mode):
                    devs = file_to_loop(self.device)
                    if len(devs) > 0:
                        self.loopdevice = devs[0]
                    self.isloop = True
            except:
                self.log.debug("can not stat %s" % self.device)
                return False

        if self.is_up() is True:
            self.log.info("fs(%s %s) is already mounted"%
                (self.device, self.mountPoint))
            return 0
        
        if not os.path.exists(self.mountPoint):
            os.makedirs(self.mountPoint, 0o755)

        if self.isloop is True:
            cmd = ['hdiutil', 'attach', '-mountpoint', self.mountPoint , self.device]
            (ret, out, err) = self.vcall(cmd)
        else:
            self.fsck()
            try:
                cmd = ['diskutil', 'mount', '-mountPoint', self.mountPoint , self.device]
                (ret, out, err) = self.vcall(cmd)
            except:
                if self.fsType != "":
                    fstype = ['-t', self.fsType]
                else:
                    fstype = []
                if self.mntOpt != "":
                    mntopt = ['-o', self.mntOpt]
                else:
                    mntopt = []
                cmd = ['mount']+fstype+mntopt+[self.device, self.mountPoint]
                (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        self.Mounts = None
        self.can_rollback = True

    def stop(self):
        if self.Mounts is None:
            self.Mounts = rcMounts.Mounts()
        if self.is_up() is False:
            self.log.info("fs(%s %s) is already umounted"%
                    (self.device, self.mountPoint))
            return
        for i in range(3):
            ret = try_umount(self)
            if ret == 0: break
        if ret != 0:
            self.log.error('failed to umount %s'%self.mountPoint)
            raise ex.excError
        self.Mounts = None

if __name__ == "__main__":
    for c in (Mount,) :
        help(c)

   0707010000bc49000081a400000000000000000000000154fc7bfb0000183c000000660000000300000000000000000000002800000000root/opt/opensvc/lib/resMountFreeBSD.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import os

import rcMountsFreeBSD as rcMounts
import resMount as Res
from rcUtilities import qcall, protected_mount, getmount
from rcGlobalEnv import rcEnv
import rcExceptions as ex
from stat import *

def try_umount(self):
    cmd = ['umount', self.mountPoint]
    (ret, out, err) = self.vcall(cmd, err_to_info=True)
    if ret == 0:
        return 0

    """ don't try to kill process using the source of a 
        protected bind mount
    """
    if protected_mount(self.mountPoint):
        return 1

    """ best effort kill of all processes that might block
        the umount operation. The priority is given to mass
        action reliability, ie don't contest oprator's will
    """
    cmd = ['sync']
    (ret, out, err) = self.vcall(cmd, err_to_info=True)

    for i in range(4):
        nb_killed = self.killfuser(self.mountPoint)
        self.log.info('umount %s'%self.mountPoint)
        cmd = ['umount', self.mountPoint]
        ret = qcall(cmd)
        if ret == 0 or nb_killed == 0:
            break

    if ret != 0:
        self.log.info("no more process using %s, yet umount fails. try forced umount."%self.mountPoint)
        cmd = ['umount', '-f', self.mountPoint]
        (ret, out, err) = self.vcall(cmd, err_to_info=True)

    return ret


class Mount(Res.Mount):
    """ define FreeBSD mount/umount doAction """
    def __init__(self,
                 rid,
                 mountPoint,
                 device,
                 fsType,
                 mntOpt,
                 snap_size=None,
                 always_on=set([]),
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        self.Mounts = None
        Res.Mount.__init__(self,
                           rid,
                           mountPoint,
                           device,
                           fsType,
                           mntOpt,
                           snap_size,
                           always_on=always_on,
                           disabled=disabled,
                           tags=tags,
                           optional=optional,
                           monitor=monitor,
                           restart=restart,
                           subset=subset)
        self.fsck_h = {
            'ufs': {'bin': 'fsck', 'cmd': ['fsck', '-t', 'ufs', '-p', self.device]},
        }

    def killfuser(self, dir):
        cmd = ['fuser', '-kmc', dir]
        (ret, out, err) = self.vcall(cmd, err_to_info=True)

        """ return the number of process we sent signal to
        """
        l = out.split(':')
        if len(l) < 2:
            return 0
        return len(l[1].split())

    def is_up(self):
        self.Mounts = rcMounts.Mounts()
        return self.Mounts.has_mount(self.device, self.mountPoint)

    def realdev(self):
        dev = None
        try:
            mode = os.stat(self.device)[ST_MODE]
        except:
            self.log.debug("can not stat %s" % self.device)
            return None
        if S_ISCHR(mode):
            dev = self.device
        else:
            mnt = getmount(self.device)
            if self.Mounts is None:
                self.Mounts = rcMounts.Mounts()
            m = self.Mounts.has_param("mnt", mnt)
            if m is None:
                self.log.debug("can't find dev %(dev)s mounted in %(mnt)s in mnttab"%dict(mnt=mnt, dev=self.device))
                return None
            dev = m.dev

        return dev

    def disklist(self):
        dev = self.realdev()
        if dev is None:
            return set([])

        try:
            statinfo = os.stat(dev)
        except:
            self.log.error("can not stat %s" % dev)
            raise ex.excError

        return set([dev])

    def can_check_writable(self):
        return True

    def start(self):
        if self.Mounts is None:
            self.Mounts = rcMounts.Mounts()
        Res.Mount.start(self)
        if self.is_up() is True:
            self.log.info("fs(%s %s) is already mounted"%
                (self.device, self.mountPoint))
            return 0
        self.fsck()
        if not os.path.exists(self.mountPoint):
            os.makedirs(self.mountPoint, 0o755)
        if self.fsType != "":
            fstype = ['-t', self.fsType]
        else:
            fstype = []
        if self.mntOpt != "":
            mntopt = ['-o', self.mntOpt]
        else:
            mntopt = []
        cmd = ['mount']+fstype+mntopt+[self.device, self.mountPoint]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        self.Mounts = None
        self.can_rollback = True

    def stop(self):
        if self.Mounts is None:
            self.Mounts = rcMounts.Mounts()
        if self.is_up() is False:
            self.log.info("fs(%s %s) is already umounted"%
                    (self.device, self.mountPoint))
            return
        for i in range(3):
            ret = try_umount(self)
            if ret == 0: break
        if ret != 0:
            self.log.error('failed to umount %s'%self.mountPoint)
            raise ex.excError
        self.Mounts = None

if __name__ == "__main__":
    for c in (Mount,) :
        help(c)

0707010000bc4a000081a400000000000000000000000154fc7bfb0000115c000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resMountHP-UX.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import os

import rcStatus
from rcGlobalEnv import rcEnv
rcMounts = __import__('rcMounts'+rcEnv.sysname)
import resMount as Res
from rcUtilities import qcall, protected_mount
import rcExceptions as ex

def try_umount(self):
    cmd = ['umount', self.mountPoint]
    (ret, out, err) = self.vcall(cmd, err_to_info=True)
    if ret == 0:
        return 0

    """ don't try to kill process using the source of a
        protected bind mount
    """
    if protected_mount(self.mountPoint):
        return 1

    """ best effort kill of all processes that might block
        the umount operation. The priority is given to mass
        action reliability, ie don't contest oprator's will
    """
    cmd = ['sync']
    (ret, out, err) = self.vcall(cmd, err_to_info=True)

    for i in range(4):
        cmd = ['fuser', '-kc', self.mountPoint]
        (ret, out, err) = self.vcall(cmd, err_to_info=True)
        self.log.info('umount %s'%self.mountPoint)
        cmd = ['umount', self.mountPoint]
        ret = qcall(cmd)
        if ret == 0:
            break

    return ret


class Mount(Res.Mount):
    """ define HP-UX mount/umount doAction """
    def __init__(self,
                 rid,
                 mountPoint,
                 device,
                 fsType,
                 mntOpt,
                 snap_size=None,
                 always_on=set([]),
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        Res.Mount.__init__(self,
                           rid,
                           mountPoint,
                           device,
                           fsType,
                           mntOpt,
                           snap_size,
                           always_on=always_on,
                           disabled=disabled,
                           tags=tags,
                           optional=optional,
                           monitor=monitor,
                           restart=restart,
                           subset=subset)
        self.fsck_h = {
            'vxfs': {'bin': 'fsck', 'cmd': ['fsck', '-F', 'vxfs', '-y', self.device]},
        } 

    def is_up(self):
        return rcMounts.Mounts().has_mount(self.device, self.mountPoint)

    def start(self):
        Res.Mount.start(self)
        if self.is_up() is True:
            self.log.info("fs(%s %s) is already mounted"%
                (self.device, self.mountPoint))
            return 0
        self.fsck()
        if not os.path.exists(self.mountPoint):
            os.makedirs(self.mountPoint, 0o755)
        if self.fsType != "":
            fstype = ['-F', self.fsType]
        else:
            fstype = []
        if self.mntOpt != "":
            mntopt = ['-o', self.mntOpt]
        else:
            mntopt = []
        cmd = ['mount']+fstype+mntopt+[self.device, self.mountPoint]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        self.can_rollback = True

    def stop(self):
        if self.is_up() is False:
            self.log.info("fs(%s %s) is already umounted"%
                    (self.device, self.mountPoint))
            return 0
        for i in range(3):
            ret = try_umount(self)
            if ret == 0: break
        if ret != 0:
            self.log.error('failed to umount %s'%self.mountPoint)
            raise ex.excError

if __name__ == "__main__":
    for c in (Mount,) :
        help(c)

0707010000bc4b000081a400000000000000000000000154fc7bfb00003105000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resMountLinux.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import os

import rcMountsLinux as rcMounts
import resMount as Res
from rcUtilities import qcall, protected_mount, getmount
from rcUtilitiesLinux import major, get_blockdev_sd_slaves, lv_exists
from rcGlobalEnv import rcEnv
from rcLoopLinux import file_to_loop
import rcExceptions as ex
from stat import *

def try_umount(self):
    cmd = ['umount', self.mountPoint]
    (ret, out, err) = self.vcall(cmd, err_to_warn=True)
    if ret == 0:
        return 0

    if "not mounted" in err:
        return 0

    """ don't try to kill process using the source of a 
        protected bind mount
    """
    if protected_mount(self.mountPoint):
        return 1

    """ best effort kill of all processes that might block
        the umount operation. The priority is given to mass
        action reliability, ie don't contest oprator's will
    """
    cmd = ['sync']
    (ret, out, err) = self.vcall(cmd)

    for i in range(4):
        cmd = ['fuser', '-kmv', self.mountPoint]
        (ret, out, err) = self.vcall(cmd, err_to_info=True)
        self.log.info('umount %s'%self.mountPoint)
        cmd = ['umount', self.mountPoint]
        ret = qcall(cmd)
        if ret == 0:
            break

    return ret


class Mount(Res.Mount):
    """ define Linux mount/umount doAction """
    def __init__(self,
                 rid,
                 mountPoint,
                 device,
                 fsType,
                 mntOpt,
                 always_on=set([]),
                 snap_size=None,
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        self.Mounts = None
        Res.Mount.__init__(self,
                           rid,
                           mountPoint,
                           device,
                           fsType,
                           mntOpt,
                           snap_size,
                           always_on=always_on,
                           disabled=disabled,
                           tags=tags,
                           optional=optional,
                           monitor=monitor,
                           restart=restart,
                           subset=subset)
        """
            0    - No errors
            1    - File system errors corrected
            32   - E2fsck canceled by user request
        """
        dev_realpath = os.path.realpath(self.device)
        if self.device.startswith("/dev/disk/by-") or dev_realpath.startswith("/dev/rbd"):
            self.device = dev_realpath


        self.fsck_h = {
            'ext2': {'bin': 'e2fsck', 'cmd': ['e2fsck', '-p', self.device], 'allowed_ret': [0, 1, 32, 33]},
            'ext3': {'bin': 'e2fsck', 'cmd': ['e2fsck', '-p', self.device], 'allowed_ret': [0, 1, 32, 33]},
            'ext4': {'bin': 'e2fsck', 'cmd': ['e2fsck', '-p', self.device], 'allowed_ret': [0, 1, 32, 33]},
        }
        self.loopdevice = None

    def is_up(self):
        self.Mounts = rcMounts.Mounts()
        ret = self.Mounts.has_mount(self.device, self.mountPoint)
        if ret:
            return True

        # might be defined as a symlink. Linux display realpaths in /proc/mounts
        ret = self.Mounts.has_mount(self.device, os.path.realpath(self.mountPoint))
        if ret:
            return True

        # might be mount using a /dev/mapper/ name too
        l = self.device.split('/')
        if len(l) == 4 and l[2] != "mapper":
            dev = "/dev/mapper/%s-%s"%(l[2].replace('-','--'),l[3].replace('-','--'))
            ret = self.Mounts.has_mount(dev, self.mountPoint)
            if ret:
                return True

        if self.fsType not in self.netfs:
            try:
                st = os.stat(self.device)
                mode = st[ST_MODE]
            except:
                self.log.debug("can not stat %s" % self.device)
                return False

            if S_ISREG(mode):
                # might be a loopback mount
                devs = file_to_loop(self.device)
                for dev in devs:
                    ret = self.Mounts.has_mount(dev, self.mountPoint)
                    if ret:
                        return True
            elif S_ISBLK(mode):
                # might be a mount using a /dev/dm-<minor> name too
                from rcUtilitiesLinux import major
                dm_major = major('device-mapper')
                if os.major(st.st_rdev) == dm_major:
                    dev = '/dev/dm-' + str(os.minor(st.st_rdev))
                    ret = self.Mounts.has_mount(dev, self.mountPoint)
                    if ret:
                        return True

        return False

    def realdev(self):
        try:
            mode = os.stat(self.device)[ST_MODE]
        except:
            self.log.debug("can not stat %s" % self.device)
            return None
        if S_ISBLK(mode):
            dev = self.device
        else:
            mnt = getmount(self.device)
            if self.Mounts is None:
                self.Mounts = rcMounts.Mounts()
            m = self.Mounts.has_param("mnt", mnt)
            if m is None:
                self.log.debug("can't find dev %(dev)s mounted in %(mnt)s in mnttab"%dict(mnt=mnt, dev=self.device))
                return None
            dev = m.dev

        return dev

    def mplist(self):
        dev = self.realdev()
        if dev is None:
            return set([])

        try:
            self.dm_major = major('device-mapper')
        except:
            return set([])

        return self._mplist([dev])

    def devname_to_dev(self, x):
        if 'cciss!' in x:
            return '/dev/cciss/'+x.replace('cciss!', '')
        return '/dev/'+x

    def _mplist(self, devs):
        mps = set([])
        for dev in devs:
            devmap = False
            if 'dm-' in dev:
                minor = int(dev.replace('/dev/dm-', ''))
                dm = dev.replace('/dev/', '')
                devmap = True
            else:
                try:
                    statinfo = os.stat(dev)
                except:
                    self.log.warning("can not stat %s" % dev)
                    continue
                minor = os.minor(statinfo.st_rdev)
                dm = 'dm-%i'%minor
                devmap = self.is_devmap(statinfo)

            if self.is_multipath(minor):
                mps |= set([dev])
            elif devmap:
                syspath = '/sys/block/' + dm + '/slaves'
                if not os.path.exists(syspath):
                    continue
                slaves = os.listdir(syspath)
                mps |= self._mplist(map(self.devname_to_dev, slaves))
        return mps

    def is_multipath(self, minor):
        cmd = ['dmsetup', '-j', str(self.dm_major),
                          '-m', str(minor),
                          'table'
              ]
        (ret, buff, err) = self.call(cmd, errlog=False, cache=True)
        if ret != 0:
            return False
        l = buff.split()
        if len(l) < 3:
            return False
        if l[2] != 'multipath':
            return False
        if 'queue_if_no_path' not in l:
            return False
        cmd = ['dmsetup', '-j', str(self.dm_major),
                          '-m', str(minor),
                          'status'
              ]
        (ret, buff, err) = self.call(cmd, errlog=False, cache=True)
        if ret != 0:
            return False
        l = buff.split()
        if l.count('A') > 1:
            return False
        return True

    def is_devmap(self, statinfo):
        if os.major(statinfo.st_rdev) == self.dm_major:
            return True
        return False

    def disklist(self):
        dev = self.realdev()
        if dev is None:
            return set([])

        if dev.startswith("/dev/rbd"):
            return set([])

        try:
            self.dm_major = major('device-mapper')
        except:
            return set([dev])

        try:
            statinfo = os.stat(dev)
        except:
            self.log.error("can not stat %s" % dev)
            raise ex.excError

        if not self.is_devmap(statinfo):
            return set([dev])

        if lv_exists(self, dev):
            """ if the fs is built on a lv of a private vg, its
                disks will be given by the vg resource.
                if the fs is built on a lv of a shared vg, we
                don't want to account its disks : don't reserve
                them, don't account their size multiple times.
            """
            return set([])

        dm = 'dm-' + str(os.minor(statinfo.st_rdev))
        syspath = '/sys/block/' + dm + '/slaves'
        devs = get_blockdev_sd_slaves(syspath)
        return devs

    def can_check_writable(self):
        if len(self.mplist()) > 0:
            self.log.debug("a multipath under fs has queueing enabled and no active path")
            return False
        return True

    def start(self):
        if self.Mounts is None:
            self.Mounts = rcMounts.Mounts()
        Res.Mount.start(self)

        """ loopback mount
            if the file has already been binded to a loop re-use
            the loopdev to avoid allocating another one
        """
        if self.fsType in self.netfs or self.device == "none":
            # TODO showmount -e
            pass
        else:
            try:
                mode = os.stat(self.device)[ST_MODE]
                if S_ISREG(mode):
                    devs = file_to_loop(self.device)
                    if len(devs) > 0:
                        self.loopdevice = devs[0]
                        mntopt_l = self.mntOpt.split(',')
                        mntopt_l.remove("loop")
                        self.mntOpt = ','.join(mntopt_l)
            except:
                self.log.debug("can not stat %s" % self.device)
                return False

        if self.is_up() is True:
            self.log.info("fs(%s %s) is already mounted"%
                (self.device, self.mountPoint))
            return 0

        if self.fsType == "btrfs":
            cmd = ['btrfs', 'device', 'scan']
            ret, out, err = self.vcall(cmd)

        self.fsck()
        if not os.path.exists(self.mountPoint):
            try:
                os.makedirs(self.mountPoint, 0o755)
            except Exception as e:
                raise ex.excError(str(e))
        if self.fsType != "":
            fstype = ['-t', self.fsType]
        else:
            fstype = []
        if self.mntOpt != "":
            mntopt = ['-o', self.mntOpt]
        else:
            mntopt = []
        if self.loopdevice is None:
            device = self.device
        else:
            device = self.loopdevice
        cmd = ['mount']+fstype+mntopt+[device, self.mountPoint]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        self.Mounts = None
        self.can_rollback = True

    def stop(self):
        if self.Mounts is None:
            self.Mounts = rcMounts.Mounts()
        if self.is_up() is False:
            self.log.info("fs(%s %s) is already umounted"%
                    (self.device, self.mountPoint))
            return
        if not os.path.exists(self.mountPoint):
            raise ex.excError('mount point %s does not exist' % self.mountPoint)
        for i in range(3):
            ret = try_umount(self)
            if ret == 0: break
        if ret != 0:
            raise ex.excError('failed to umount %s'%self.mountPoint)
        self.Mounts = None

if __name__ == "__main__":
    for c in (Mount,) :
        help(c)

   0707010000bc4c000081a400000000000000000000000154fc7bfb00001669000000660000000300000000000000000000002500000000root/opt/opensvc/lib/resMountOSF1.py  #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import os

import rcMountsOSF1 as rcMounts
import resMount as Res
from rcUtilities import qcall, protected_mount, getmount
from rcGlobalEnv import rcEnv
import rcExceptions as ex
from stat import *

def try_umount(self):
    cmd = ['umount', self.mountPoint]
    (ret, out, err) = self.vcall(cmd, err_to_warn=True)
    if ret == 0:
        return 0

    if "not currently mounted" in err:
        return 0

    """ don't try to kill process using the source of a 
        protected bind mount
    """
    if protected_mount(self.mountPoint):
        return 1

    """ best effort kill of all processes that might block
        the umount operation. The priority is given to mass
        action reliability, ie don't contest oprator's will
    """
    cmd = ['sync']
    (ret, out, err) = self.vcall(cmd)

    for i in range(4):
        cmd = ['fuser', '-kcv', self.mountPoint]
        (ret, out, err) = self.vcall(cmd, err_to_info=True)
        self.log.info('umount %s'%self.mountPoint)
        cmd = ['umount', self.mountPoint]
        ret = qcall(cmd)
        if ret == 0:
            break

    return ret


class Mount(Res.Mount):
    def __init__(self,
                 rid,
                 mountPoint,
                 device,
                 fsType,
                 mntOpt,
                 always_on=set([]),
                 snap_size=None,
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        self.Mounts = None
        Res.Mount.__init__(self,
                           rid,
                           mountPoint,
                           device,
                           fsType,
                           mntOpt,
                           snap_size,
                           always_on,
                           disabled=disabled,
                           tags=tags,
                           optional=optional,
                           monitor=monitor,
                           restart=restart,
                           subset=subset)
        self.fsck_h = {
            'ufs': {'bin': 'fsck', 'cmd': ['fsck', '-p', self.device], 'allowed_ret': []},
        }

    def is_up(self):
        self.Mounts = rcMounts.Mounts()
        ret = self.Mounts.has_mount(self.device, self.mountPoint)
        if ret:
            return True

        if self.fsType not in ["advfs"] + self.netfs:
            # might be a loopback mount
            try:
                mode = os.stat(self.device)[ST_MODE]
            except:
                self.log.debug("can not stat %s" % self.device)
                return False

        return False

    def devlist(self):
        return self.disklist()

    def disklist(self):
        if '#' in self.device:
            dom, fset = self.device.split('#')
            for r in self.svc.get_resources('disk.vg'):
                if r.name == dom:
                    # no need to compute device list: the vg resource will do the job
                    return set([])
            import rcAdvfs
            try:
                o = rcAdvfs.Fdmns()
                d = o.get_fdmn(dom)
            except rcAdvfs.ExInit as e:
                return set([])
            if d is None:
                return set([])
            return set(d.list_volnames())
        else:
            return set([self.device])

    def can_check_writable(self):
        return True

    def start(self):
        if self.Mounts is None:
            self.Mounts = rcMounts.Mounts()
        Res.Mount.start(self)

        if self.is_up() is True:
            self.log.info("fs(%s %s) is already mounted"%
                (self.device, self.mountPoint))
            return 0

        self.fsck()
        if not os.path.exists(self.mountPoint):
            os.makedirs(self.mountPoint, 0o755)
        if self.fsType != "":
            fstype = ['-t', self.fsType]
        else:
            fstype = []
        if self.mntOpt != "":
            mntopt = ['-o', self.mntOpt]
        else:
            mntopt = []
        cmd = ['mount']+fstype+mntopt+[self.device, self.mountPoint]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        self.Mounts = None
        self.can_rollback = True

    def stop(self):
        if self.Mounts is None:
            self.Mounts = rcMounts.Mounts()
        if self.is_up() is False:
            self.log.info("fs(%s %s) is already umounted"%
                    (self.device, self.mountPoint))
            return
        for i in range(3):
            ret = try_umount(self)
            if ret == 0: break
        if ret != 0:
            self.log.error('failed to umount %s'%self.mountPoint)
            raise ex.excError
        self.Mounts = None

if __name__ == "__main__":
    for c in (Mount,) :
        help(c)

   0707010000bc4d000081a400000000000000000000000154fc7bfb00000805000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/resMountRhcsLinux.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

from rcGlobalEnv import rcEnv
Res = __import__("resMountLinux")

class Mount(Res.Mount):
    def __init__(self,
                 rid,
                 mountPoint,
                 device,
                 fsType,
                 mntOpt,
                 always_on=set([]),
                 snap_size=None,
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        Res.Mount.__init__(self,
                           rid,
                           mountPoint,
                           device,
                           fsType,
                           mntOpt,
                           always_on=always_on,
                           snap_size=snap_size,
                           disabled=disabled,
                           tags=tags,
                           optional=optional,
                           monitor=monitor,
                           restart=restart,
                           subset=subset)

    def start(self):
        pass

    def stop(self):
        pass

if __name__ == "__main__":
    for c in (Mount,) :
        help(c)

   0707010000bc4e000081a400000000000000000000000154fc7bfb00000997000000660000000300000000000000000000002800000000root/opt/opensvc/lib/resMountSgHP-UX.py   #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

from rcGlobalEnv import rcEnv
Res = __import__("resMountHP-UX")

class Mount(Res.Mount):
    def __init__(self,
                 rid,
                 mountPoint,
                 device,
                 fsType,
                 mntOpt,
                 snap_size=None,
                 always_on=set([]),
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        self.sgname = device
        Res.Mount.__init__(self,
                           rid,
                           mountPoint,
                           device,
                           fsType,
                           mntOpt,
                           snap_size,
                           always_on=always_on,
                           disabled=disabled,
                           tags=tags,
                           optional=optional,
                           monitor=monitor,
                           restart=restart,
                           subset=subset)

    def is_up(self):
        if 'resource' in self.svc.cmviewcl and \
           self.mon_name in self.svc.cmviewcl['resource']:
            state = self.svc.cmviewcl['resource'][self.mon_name][('status', rcEnv.nodename)]
            if state == "up":
                return True
            else:
                return False
        else:
            return Res.Mount.is_up(self)

    def start(self):
        pass

    def stop(self):
        pass

if __name__ == "__main__":
    for c in (Mount,) :
        help(c)

 0707010000bc4f000081a400000000000000000000000154fc7bfb000009a1000000660000000300000000000000000000002800000000root/opt/opensvc/lib/resMountSgLinux.py   #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

from rcGlobalEnv import rcEnv
Res = __import__("resMountLinux")

class Mount(Res.Mount):
    def __init__(self,
                 rid,
                 mountPoint,
                 device,
                 fsType,
                 mntOpt,
                 always_on=set([]),
                 snap_size=None,
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        self.sgname = device
        Res.Mount.__init__(self,
                           rid,
                           mountPoint,
                           device,
                           fsType,
                           mntOpt,
                           always_on=always_on,
                           snap_size=snap_size,
                           disabled=disabled,
                           tags=tags,
                           optional=optional,
                           monitor=monitor,
                           restart=restart,
                           subset=subset)

    def is_up(self):
        if 'resource' in self.svc.cmviewcl and \
           self.mon_name in self.svc.cmviewcl['resource']:
            state = self.svc.cmviewcl['resource'][self.mon_name][('status', rcEnv.nodename)]
            if state == "up":
                return True
            else:
                return False
        else:
            return Res.Mount.is_up(self)

    def start(self):
        pass

    def stop(self):
        pass

if __name__ == "__main__":
    for c in (Mount,) :
        help(c)

   0707010000bc50000081a400000000000000000000000154fc7bfb0000195c000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resMountSunOS.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.
"""SunOS specific mounts"""

import os
import time

import rcStatus
import rcMountsSunOS as rcMounts
import resMount as Res
import rcExceptions as ex
from rcZfs import zfs_getprop, zfs_setprop
from rcUtilities import justcall

class Mount(Res.Mount):
    """ define SunOS mount/umount doAction """
    def __init__(self,
                 rid,
                 mountPoint,
                 device,
                 fsType,
                 mntOpt,
                 snap_size=None,
                 always_on=set([]),
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        self.rdevice = device.replace('/dsk/','/rdsk/',1)
        self.Mounts = rcMounts.Mounts()
        Res.Mount.__init__(self,
                           rid,
                           mountPoint,
                           device,
                           fsType,
                           mntOpt,
                           snap_size,
                           always_on,
                           disabled=disabled,
                           tags=tags,
                           optional=optional,
                           monitor=monitor,
                           restart=restart,
                           subset=subset)
        self.fsck_h = {
            'ufs': {'bin': 'fsck',
                    'cmd':       ['fsck', '-F', 'ufs', '-y', self.rdevice],
                    'reportcmd': ['fsck', '-F', 'ufs', '-m', self.rdevice],
                    'reportclean': [ 32 ],
            },
            'vxfs': {'bin': 'fsck',
                    'cmd':       ['fsck', '-F', 'vxfs', '-y', self.rdevice],
                    'reportcmd': ['fsck', '-F', 'vxfs', '-m', self.rdevice],
                    'reportclean': [ 32 ],
            },
        }

    def is_up(self):
        self.Mounts = rcMounts.Mounts()
        return self.Mounts.has_mount(self.device, self.mountPoint)

    def start(self):
        self.Mounts = None
        Res.Mount.start(self)

        if self.fsType == 'zfs' :
            if 'noaction' not in self.tags and zfs_getprop(self.device, 'canmount' ) != 'noauto' :
                self.log.info("fs(%s %s) should be set to canmount=noauto (zfs set canmount=noauto %s)"%(self.device, self.mountPoint, self.device))

        if self.is_up() is True:
            self.log.info("fs(%s %s) is already mounted"%
                (self.device, self.mountPoint))
            return

        if self.fsType == 'zfs' :
            if zfs_getprop(self.device, 'mountpoint' ) != self.mountPoint :
                if zfs_setprop(self.device, 'zoned', 'off') :
                    if not zfs_setprop(self.device, 'mountpoint', self.mountPoint) :
                        raise ex.excError

            self.Mounts = None
            if self.is_up() is True:
                return

            (stdout,stderr,returncode)= justcall(['rm', self.mountPoint+"/.opensvc" ])
            ret, out, err = self.vcall(['zfs', 'mount', self.device ])
            if ret != 0:
                ret, out, err = self.vcall(['zfs', 'mount', '-O', self.device ])
                if ret != 0:
                    raise ex.excError
            return
        elif self.fsType != "":
            fstype = ['-F', self.fsType]
            self.fsck()
        else:
            fstype = []

        if self.mntOpt != "":
            mntopt = ['-o', self.mntOpt]
        else:
            mntopt = []

        if not os.path.exists(self.mountPoint):
            os.makedirs(self.mountPoint, 0o755)

        for i in range(3):
            ret = self.try_mount(fstype, mntopt)
            if ret == 0: break
            time.sleep(1)

        self.Mounts = None

        if ret != 0:
            raise ex.excError

        self.can_rollback = True

    def try_mount(self, fstype, mntopt):
        cmd = ['mount'] + fstype + mntopt + [self.device, self.mountPoint]
        ret, out, err = self.vcall(cmd)
        return ret

    def try_umount(self):
        if self.fsType == 'zfs' :
            ret, out, err = self.vcall(['zfs', 'umount', self.device ], err_to_info=True)
            if ret != 0 :
                ret, out, err = self.vcall(['zfs', 'umount', '-f', self.device ], err_to_info=True)
                if ret != 0 :
                    raise ex.excError
            return
        (ret, out, err) = self.vcall(['umount', self.mountPoint], err_to_info=True)
        if ret == 0 :
            return
        for i in range(4):
            (ret, out, err) = self.vcall(['fuser', '-ck', self.mountPoint],
                                    err_to_info=True)
            (ret, out, err) = self.vcall(['umount', self.mountPoint],
                                    err_to_info=True)
            if ret == 0 :
                return
            if self.fsType != 'lofs' :
                (ret, out, err) = self.vcall(['umount', '-f', self.mountPoint],
                                        err_to_info=True)
                if ret == 0 :
                    return
        raise ex.excError

    def stop(self):
        self.Mounts = None
        if self.is_up() is False:
            self.log.info("fs(%s %s) is already umounted"%
                    (self.device, self.mountPoint))
            return

        try: 
            self.try_umount()
        except:
            self.Mounts = None
            self.log.error("failed")
            raise ex.excError
        self.Mounts = None

if __name__ == "__main__":
    for c in (Mount,) :
        help(c)

0707010000bc51000081a400000000000000000000000154fc7bfb00000987000000660000000300000000000000000000002900000000root/opt/opensvc/lib/resMountVcsLinux.py  #
# Copyright (c) 2013 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import rcStatus
import rcExceptions as ex
from rcGlobalEnv import rcEnv
Res = __import__("resMountLinux")

class Mount(Res.Mount):
    def __init__(self,
                 rid,
                 mountPoint,
                 device,
                 fsType,
                 mntOpt,
                 always_on=set([]),
                 snap_size=None,
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        Res.Mount.__init__(self,
                           rid,
                           mountPoint,
                           device,
                           fsType,
                           mntOpt,
                           always_on=always_on,
                           snap_size=snap_size,
                           disabled=disabled,
                           tags=tags,
                           optional=optional,
                           monitor=monitor,
                           restart=restart,
                           subset=subset)

    def _status(self, verbose=False):
        try:
            s = self.svc.get_res_val(self.vcs_name, 'State')
        except ex.excError as e:
            self.status_log(str(e))
            return rcStatus.WARN

        if s == "ONLINE":
            return rcStatus.UP
        elif s == "OFFLINE":
            return rcStatus.DOWN
        else:
            self.status_log(s)
            return rcStatus.WARN

if __name__ == "__main__":
    for c in (Mount,) :
        help(c)

 0707010000bc52000081a400000000000000000000000154fc7bfb00001c12000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resScsiReserv.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import resources as Res
import uuid
import re
import time
import rcStatus
import rcExceptions as ex
from rcUtilities import which
from subprocess import *
from rcGlobalEnv import rcEnv
hostId = __import__('hostid'+rcEnv.sysname)


class ScsiReserv(Res.Resource):
    """Define method to acquire and release scsi SPC-3 persistent reservations
    on disks held by a service
    """
    def __init__(self,
                 rid=None,
                 disks=set([]),
                 no_preempt_abort=False,
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 subset=None):
        self.no_preempt_abort = no_preempt_abort
        self.hostid = '0x'+hostId.hostid()
        self.disks = disks
        if len(disks) == 0:
            self.label = 'preserv 0 scsi disk'
        elif len(', '.join(disks)) > 248:
            self.label = 'preserv '+', '.join(disks)[0:248]
            self.label += " ..."
        else:
            self.label = ', '.join(disks)
        self.preempt_timeout = 10
        self.prtype = '5'
        Res.Resource.__init__(self,
                              rid=rid+"pr",
                              type="disk.scsireserv",
                              disabled=disabled,
                              tags=tags,
                              optional=optional,
                              subset=subset)

    def scsireserv_supported(self):
        return False

    def ack_unit_attention(self, d):
        raise ex.notImplemented

    def disk_registered(self, disk):
        raise ex.notImplemented

    def disk_register(self, disk):
        raise ex.notImplemented

    def disk_unregister(self, disk):
        raise ex.notImplemented

    def get_reservation_key(self, disk):
        raise ex.notImplemented

    def disk_reserved(self, disk):
        raise ex.notImplemented

    def disk_release(self, disk):
        raise ex.notImplemented

    def disk_reserve(self, disk):
        raise ex.notImplemented

    def disk_preempt_reservation(self, disk, oldkey):
        if not hasattr(self, '_disk_preempt_reservation'):
            raise ex.notImplemented
        if not self.svc.force and not self.svc.cluster:
            self.log.error("%s is already reserved. use --force to override this safety net"%disk)
            raise ex.excError
        return self._disk_preempt_reservation(disk, oldkey)

    def ack_all_unit_attention(self):
        for d in self.disks:
            try:
                if self.ack_unit_attention(d) != 0:
                    return 1
            except ex.excScsiPrNotsupported:
                continue
        return 0

    def register(self):
        r = 0
        for d in self.disks:
            try:
                r += self.ack_unit_attention(d)
                r += self.disk_register(d)
            except ex.excScsiPrNotsupported:
                continue
        return r

    def unregister(self):
        r = 0
        for d in self.disks:
            try:
                r += self.ack_unit_attention(d)
                if not self.disk_registered(d):
                    continue
                r += self.disk_unregister(d)
            except ex.excScsiPrNotsupported:
                continue
        return r

    def disk_wait_reservation(self, disk):
        for i in range(3, 0, -1):
            if self.disk_reserved(disk):
                self.log.info("reservation acquired for disk %s" % disk)
                return 0
            if i > 0:
                time.sleep(1)
        self.log.error("timed out waiting for reservation for disk %s" % disk)
        return 1

    def reserve(self):
        r = 0
        for d in self.disks:
            try:
                r += self.ack_unit_attention(d)
                key = self.get_reservation_key(d)
                if key is None:
                    r += self.disk_reserve(d)
                elif key == self.hostid:
                    continue
                else:
                    r += self.disk_preempt_reservation(d, key)
                    r += self.disk_wait_reservation(d)
            except ex.excScsiPrNotsupported:
                continue
        return r

    def release(self):
        r = 0
        for d in self.disks:
            try:
                r += self.ack_unit_attention(d)
                if not self.disk_reserved(d):
                    continue
                r += self.disk_release(d)
            except ex.excScsiPrNotsupported:
                continue
        return r

    def checkreserv(self):
        if self.ack_all_unit_attention() != 0:
            return rcStatus.WARN
        r = rcStatus.Status()
        for d in self.disks:
            try:
                key = self.get_reservation_key(d)
                if key is None:
                    self.log.debug("disk %s is not reserved" % d)
                    r += rcStatus.DOWN
                elif key != self.hostid:
                    self.log.debug("disk %s is reserved by another host whose key is %s" % (d, key))
                    r += rcStatus.DOWN
                else:
                    self.log.debug("disk %s is correctly reserved" % d)
                    r += rcStatus.UP
            except ex.excScsiPrNotsupported:
                continue
        return r.status

    def scsireserv(self):
        if not self.scsireserv_supported():
            return
        r = 0
        r += self.register()
        r += self.reserve()
        return r

    def scsirelease(self):
        if not self.scsireserv_supported():
            return
        r = 0
        r += self.release()
        r += self.unregister()
        return r

    def scsicheckreserv(self):
        if not self.scsireserv_supported():
            return
        return self.checkreserv()

    def _status(self, verbose=False):
        if not self.scsireserv_supported():
            return rcStatus.NA
        return self.scsicheckreserv()

    def start(self):
        if not self.scsireserv_supported():
            return
        self.can_rollback = True
        if self.scsireserv() != 0:
            raise ex.excError

    def stop(self):
        if not self.scsireserv_supported():
            return
        if self.scsirelease() != 0:
            raise ex.excError


  0707010000bc53000081a400000000000000000000000154fc7bfb00001810000000660000000300000000000000000000002b00000000root/opt/opensvc/lib/resScsiReservHP-UX.py    #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import resources as Res
import uuid
import re
import os
import time
import rcStatus
import rcExceptions as ex
from rcUtilities import which
from subprocess import *
import resScsiReserv

def mpath_to_path(disks):
    l = []
    for disk in disks:
        if "/dev/dsk" in disk:
            l.append(disk.replace("/dev/dsk", "/dev/rdsk"))
            continue
        if "/dev/disk" not in disk and "/dev/rdisk" not in disk:
            continue
        if not os.path.exists(disk):
            continue
        cmd = ['ioscan', '-F', '-m', 'dsf', disk]
        p = Popen(cmd, stderr=None, stdout=PIPE, close_fds=True)
        buff = p.communicate()
        ret = p.returncode
        if ret != 0:
            continue
        a = buff[0].split(':')
        if len(a) != 2:
            continue
        b = a[1].split()
        for d in b:
            l.append(d.replace("/dev/dsk", "/dev/rdsk"))
    return l
    
class ScsiReserv(resScsiReserv.ScsiReserv):
    def __init__(self,
                 rid=None,
                 disks=set([]),
                 no_preempt_abort=False,
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 subset=None):
        resScsiReserv.ScsiReserv.__init__(self,
                                          rid=rid,
                                          disks=disks,
                                          no_preempt_abort=no_preempt_abort,
                                          disabled=disabled,
                                          tags=tags,
                                          optional=optional,
                                          subset=subset)
        self.prtype = 'wero'
        self.disks = mpath_to_path(disks)
        self.leg_mpath_disable()

    def scsireserv_supported(self):
        if which('scu') is None:
            return False
        return True

    def leg_mpath_disable(self):
        cmd = ['scsimgr', 'get_attr', '-p', '-a', 'leg_mpath_enable']
        p = Popen(cmd, stderr=None, stdout=PIPE, close_fds=True)
        buff = p.communicate()
        ret = p.returncode
        if ret != 0:
            self.log.error("can not fetch 'leg_mpath_enable' value")
            raise ex.excError
        if 'false' in buff[0]:
            return
        cmd = ['scsimgr', 'save_attr', '-a', 'leg_mpath_enable=false']
        self.log.info(' '.join(cmd))
        p = Popen(cmd, stderr=None, stdout=PIPE, close_fds=True)
        buff = p.communicate()
        ret = p.returncode
        if ret != 0:
            self.log.error("can not set 'leg_mpath_enable' value")
            raise ex.excError

    def ack_unit_attention(self, d):
        return 0

    def disk_registered(self, disk):
        cmd = [ 'scu', '-f', disk, 'show', 'keys' ]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            self.log.error("failed to read registrations for disk %s" % disk)
        if self.hostid in out:
            return True
        return False

    def disk_register(self, disk):
        cmd = [ 'scu', '-f', disk, 'preserve', 'register', 'skey', self.hostid ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            self.log.error("failed to register key %s with disk %s" % (self.hostid, disk))
        return ret

    def disk_unregister(self, disk):
        cmd = [ 'scu', '-f', disk, 'preserve', 'register', 'skey', '0', 'key', self.hostid ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            self.log.error("failed to unregister key %s with disk %s" % (self.hostid, disk))
        return ret

    def get_reservation_key(self, disk):
        cmd = [ 'scu', '-f', disk, 'show', 'reservation' ]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            self.log.error("failed to list reservation for disk %s" % disk)
        if 'Reservation Key' not in out:
            return None
        for line in out.split('\n'):
            if 'Reservation Key' in line:
                return line.split()[-1]
        raise Exception()

    def disk_reserved(self, disk):
        cmd = [ 'scu', '-f', disk, 'show', 'reservation' ]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            self.log.error("failed to read reservation for disk %s" % disk)
        if self.hostid in out:
            return True
        return False

    def disk_release(self, disk):
        cmd = [ 'scu', '-f', disk, 'preserve', 'release', 'key', self.hostid, 'type', self.prtype ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            self.log.error("failed to release disk %s" % disk)
        return ret

    def disk_reserve(self, disk):
        cmd = [ 'scu', '-f', disk, 'preserve', 'reserve', 'key', self.hostid, 'type', self.prtype ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            self.log.error("failed to reserve disk %s" % disk)
        return ret

    def _disk_preempt_reservation(self, disk, oldkey):
        cmd = [ 'scu', '-f', disk, 'preserve', 'preempt', 'key', self.hostid, 'skey', oldkey, 'type', self.prtype ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            self.log.error("failed to preempt reservation for disk %s" % disk)
        return ret

0707010000bc54000081a400000000000000000000000154fc7bfb00001571000000660000000300000000000000000000002b00000000root/opt/opensvc/lib/resScsiReservLinux.py    #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import os
import resources as Res
import uuid
import re
import time
import rcStatus
import rcExceptions as ex
from rcUtilities import which
from subprocess import *
import resScsiReserv
from rcGlobalEnv import rcEnv


class ScsiReserv(resScsiReserv.ScsiReserv):
    def scsireserv_supported(self):
        if which('sg_persist') is None:
            self.log.debug("sg_persist must be installed to use scsi-3 reservations" )
            return False
        return True

    def set_read_only(self, val):
        if rcEnv.sysname != "Linux":
            return
        os.environ["SG_PERSIST_O_RDONLY"] = str(val)

    def ack_unit_attention(self, d):
        i = self.preempt_timeout
        self.set_read_only(0)
        while i>0:
            i -= 1
            cmd = [ 'sg_persist', '-n', '-r', d ]
            p = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
            out = p.communicate()
            ret = p.returncode
            if "unsupported service action" in out[1]:
                self.log.error("disk %s does not support persistent reservation" % d)
                raise ex.excScsiPrNotsupported
            if "Unit Attention" in out[0] or ret != 0:
                self.log.debug("disk %s reports 'Unit Attention' ... waiting" % d)
                time.sleep(1)
                continue
            break
        if i == 0:
            self.log.error("timed out waiting for 'Unit Attention' to go away on disk %s" % d)
            return 1
        return 0

    def disk_registered(self, disk):
        self.set_read_only(1)
        cmd = [ 'sg_persist', '-n', '-k', disk ]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            self.log.error("failed to read registrations for disk %s" % disk)
        if self.hostid in out:
            return True
        return False

    def disk_register(self, disk):
        self.set_read_only(0)
        cmd = [ 'sg_persist', '-n', '--out', '--register-ignore', '--param-sark='+self.hostid, disk ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            self.log.error("failed to register key %s with disk %s" % (self.hostid, disk))
        return ret

    def disk_unregister(self, disk):
        self.set_read_only(0)
        cmd = [ 'sg_persist', '-n', '--out', '--register-ignore', '--param-rk='+self.hostid, disk ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            self.log.error("failed to unregister key %s with disk %s" % (self.hostid, disk))
        return ret

    def get_reservation_key(self, disk):
        self.set_read_only(1)
        cmd = [ 'sg_persist', '-n', '-r', disk ]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            self.log.error("failed to list reservation for disk %s" % disk)
        if 'Key=' not in out:
            return None
        for w in out.split():
            if 'Key=' in w:
                return w.split('=')[1]
        raise Exception()

    def disk_reserved(self, disk):
        self.set_read_only(1)
        cmd = [ 'sg_persist', '-n', '-r', disk ]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            self.log.error("failed to read reservation for disk %s" % disk)
        if self.hostid in out:
            return True
        return False

    def disk_release(self, disk):
        self.set_read_only(0)
        cmd = [ 'sg_persist', '-n', '--out', '--release', '--param-rk='+self.hostid, '--prout-type='+self.prtype, disk ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            self.log.error("failed to release disk %s" % disk)
        return ret

    def disk_reserve(self, disk):
        self.set_read_only(0)
        cmd = [ 'sg_persist', '-n', '--out', '--reserve', '--param-rk='+self.hostid, '--prout-type='+self.prtype, disk ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            self.log.error("failed to reserve disk %s" % disk)
        return ret

    def _disk_preempt_reservation(self, disk, oldkey):
        m = __import__("rcDiskInfo"+rcEnv.sysname)
        if self.no_preempt_abort or m.diskInfo(deferred=True).disk_vendor(disk).strip() in ["VMware"]:
            preempt_opt = '--preempt'
        else:
            preempt_opt = '--preempt-abort'
        self.set_read_only(0)
        cmd = [ 'sg_persist', '-n', '--out', preempt_opt, '--param-sark='+oldkey, '--param-rk='+self.hostid, '--prout-type='+self.prtype, disk ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            self.log.error("failed to preempt reservation for disk %s" % disk)
        return ret

   0707010000bc55000081a400000000000000000000000154fc7bfb00001c88000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/resScsiReservOSF1.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import resources as Res
import uuid
import re
import os
import time
import rcStatus
import rcExceptions as ex
from rcUtilities import which
from subprocess import *
import resScsiReserv

class ScsiReserv(resScsiReserv.ScsiReserv):
    def __init__(self, rid=None, disks=set([]), no_preempt_abort=False,
                 disabled=False, tags=set([]), optional=False):
        resScsiReserv.ScsiReserv.__init__(self, rid=rid, disks=disks,
                                          no_preempt_abort=no_preempt_abort,
                                          disabled=disabled, tags=tags,
                                          optional=optional)
        self.prtype = 'wero'
        self.disks = map(lambda x: str(x.replace('/disk/', '/rdisk/')), disks)
        self.disk_id = {}
        self.itn = {}

    def scsireserv_supported(self):
        if which('scu') is None:
            return False
        return True

    def ack_unit_attention(self, d):
        return 0

    def get_disk_ids(self):
        if len(self.disk_id) > 0:
            return
        cmd = [ 'hwmgr', 'show', 'scsi' ]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError(err)
        for line in out.split('\n'):
            v = line.split()
            if len(v) < 7:
                continue
            if v[3] != "disk":
                continue
            if not v[7].startswith("dsk"):
                continue
            self.disk_id[v[7]+'c'] = v[0].strip(":")

    def get_itns(self):
        if len(self.itn) > 0:
            return
        self.get_disk_ids()
        for disk in self.disk_id:
            self.get_itn(disk)

    def get_itn(self, disk):
        if disk in self.itn:
            return
        self.itn[disk] = []
        if disk not in self.disk_id:
            return
        id = self.disk_id[disk]
        cmd = [ 'hwmgr', 'show', 'scsi', '-id', id, '-full' ]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            return
        for line in out.split('\n'):
            v = line.split()
            if len(v) != 4:
                continue
            data = {}
            for i, s in enumerate(("bus", "target", "lun")):
                try:
                    j = int(v[i])
                    data[s] = v[i]
                except:
                    continue
            self.itn[disk].append(data)

    def set_nexus(self, itn):
        return "set nexus bus %(bus)s target %(target)s lun %(lun)s ; " % itn

    def pipe_scu(self, cmd):
        self.log.info(cmd + ' | scu')
        p = Popen(['scu'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate(input=cmd)
        #if len(out) > 0:
        #    self.log.info(out)
        if len(err) > 0:
            self.log.error(out)
        if p.returncode:
            raise ex.excError

    def disk_registered(self, disk):
        cmd = [ 'scu', '-f', disk, 'show', 'keys' ]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            self.log.error("failed to read registrations for disk %s" % disk)
        if out.count(self.hostid) == 0:
            return False
        return True

    def disk_register(self, disk):
        self.get_itns()
        basedisk = os.path.basename(disk)
        if basedisk not in self.itn:
            self.log.error("no nexus information for disk %s"%disk)
            return 1
        r = 0
        for itn in self.itn[basedisk]:
            r += self.__disk_register(itn)
        if r > 0:
            r = 1
        return r

    def __disk_register(self, itn):
        cmd = self.set_nexus(itn) + 'preserve register skey ' + self.hostid
        try:
            self.pipe_scu(cmd)
        except ex.excError as e:
            self.log.error("failed to register key %s with nexus %s" % (self.hostid, ':'.join(itn.values())))
            return 1
        return 0

    def disk_unregister(self, disk):
        self.get_itns()
        basedisk = os.path.basename(disk)
        if basedisk not in self.itn:
            self.log.error("no nexus information for disk %s"%disk)
            return 1
        r = 0
        for itn in self.itn[basedisk]:
            r += self.__disk_unregister(itn)
        if r > 0:
            r = 1
        return r

    def __disk_unregister(self, itn):
        cmd = self.set_nexus(itn) +  'preserve register skey 0 key ' + self.hostid
        try:
            self.pipe_scu(cmd)
        except ex.excError as e:
            self.log.error("failed to unregister key %s with nexus %s" % (self.hostid, ':'.join(itn.values())))
            return 1
        return 0

    def get_reservation_key(self, disk):
        cmd = [ 'scu', '-f', disk, 'show', 'reservation' ]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            self.log.error("failed to list reservation for disk %s" % disk)
        if 'Reservation Key' not in out:
            return None
        for line in out.split('\n'):
            if 'Reservation Key' in line:
                return line.split()[-1]
        raise Exception()

    def disk_reserved(self, disk):
        cmd = [ 'scu', '-f', disk, 'show', 'reservation' ]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            self.log.error("failed to read reservation for disk %s" % disk)
        if self.hostid in out:
            return True
        return False

    def disk_release(self, disk):
        cmd = [ 'scu', '-f', disk, 'preserve', 'release', 'key', self.hostid, 'type', self.prtype ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            self.log.error("failed to release disk %s" % disk)
        return ret

    def disk_reserve(self, disk):
        cmd = [ 'scu', '-f', disk, 'preserve', 'reserve', 'key', self.hostid, 'type', self.prtype ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            self.log.error("failed to reserve disk %s" % disk)
        return ret

    def _disk_preempt_reservation(self, disk, oldkey):
        cmd = [ 'scu', '-f', disk, 'preserve', 'preempt', 'key', self.hostid, 'skey', oldkey, 'type', self.prtype ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            self.log.error("failed to preempt reservation for disk %s" % disk)
        return ret

if __name__ == "__main__":
    o = ScsiReserv(rid="vg#0", disks=set(["/dev/disk/dsk46c"]))
    print(o.get_reservation_key("/dev/disk/dsk46c"))
0707010000bc56000081a400000000000000000000000154fc7bfb00001433000000660000000300000000000000000000002900000000root/opt/opensvc/lib/resShareNfsHP-UX.py  #
# Copyright (c) 2013 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os

from rcGlobalEnv import rcEnv
from rcUtilities import justcall, which
import rcStatus
import rcExceptions as ex
from resources import Resource

class Share(Resource):
    def get_opts(self):
        if not os.path.exists(self.sharetab):
            return self.data
        with open(self.sharetab, 'r') as f:
            buff = f.read()
        for line in buff.split('\n'):
            words = line.split()
            if len(words) != 4:
                continue
            path = words[0]
            if path != self.path:
                continue
            res = words[1]
            fstype = words[2]
            if fstype != "nfs":
                continue
            opts = words[3]
            return self.parse_opts(opts)
        return ""
        
    def is_up(self):
        self.issues = ""
        opts = self.get_opts()
        if len(opts) == 0:
            return False
        if opts != self.opts:
            self.issues = "%s exported with unexpected options: %s, expected %s"%(self.path, opts, self.opts)
            return False
        return True

    def start(self):
        try:
            up = self.is_up()
        except ex.excError as e:
            self.log.error("skip start because the share is in unknown state")
            return
        if up:
            self.log.info("%s is already up" % self.path)
            return
        if "unexpected options" in self.issues:
            self.log.info("reshare %s because unexpected options were detected"%self.path)
            cmd = [ 'unshare', '-F', 'nfs', self.path ]
            ret, out, err = self.vcall(cmd)
            if ret != 0:
                raise ex.excError(err)
        self.can_rollback = True
        cmd = [ 'share', '-F', 'nfs', '-o', self.opts, self.path ]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError(err)

    def stop(self):
        try:
            up = self.is_up()
        except ex.excError as e:
            self.log.error("continue with stop even if the share is in unknown state")
        if not up:
            self.log.info("%s is already down" % self.path)
            return 0
        cmd = [ 'unshare', '-F', 'nfs', self.path ]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def _status(self, verbose=False):
        try:
            up = self.is_up()
        except ex.excError as e:
            self.status_log(str(e))
            return rcStatus.WARN
        if len(self.issues) > 0:
            self.status_log(self.issues)
            return rcStatus.WARN
        if rcEnv.nodename in self.always_on:
            if up: return rcStatus.STDBY_UP
            else: return rcStatus.STDBY_DOWN
        else:
            if up: return rcStatus.UP
            else: return rcStatus.DOWN

    def parse_opts(self, opts):
        o = sorted(opts.split(','))
        out = []
        for e in o:
            if e.startswith('ro=') or e.startswith('rw=') or e.startswith('access='):
                opt, clients = e.split('=')
                clients = ':'.join(sorted(clients.split(':')))
                if len(clients) == 0:
                    continue
                out.append('='.join((opt, clients)))
            else:
                out.append(e)
        return ','.join(out)

    def __init__(self,
                 rid,
                 path,
                 opts,
                 always_on=set([]),
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        Resource.__init__(self,
                          rid,
                          type="share.nfs",
                          always_on=always_on,
                          disabled=disabled,
                          tags=tags,
                          optional=optional,
                          monitor=monitor,
                          restart=restart,
                          subset=subset)
        self.sharetab = "/etc/dfs/sharetab"
        self.dfstab = "/etc/dfs/dfstab"

        if not which("share"):
            raise ex.excInitError("share is not installed")
        self.label = "nfs:"+path
        self.path = path
        try:
            self.opts = self.parse_opts(opts)
        except ex.excError as e:
            raise ex.excInitError(str(e))


 0707010000bc57000081a400000000000000000000000154fc7bfb00001725000000660000000300000000000000000000002900000000root/opt/opensvc/lib/resShareNfsLinux.py  #
# Copyright (c) 2013 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os

from rcGlobalEnv import rcEnv
from rcUtilities import justcall, which
import rcStatus
import rcExceptions as ex
from resources import Resource

class Share(Resource):
    def get_exports(self):
        self.data = {}
        cmd = [ 'exportfs', '-v' ]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError(err)
        out = out.replace('\n ', '').replace('\n\t', '')
        for line in out.split('\n'):
            words = line.split()
            if len(words) != 2:
                continue
            path = words[0]
            e = words[1]
            if path not in self.data:
                self.data[path] = {}
            try:
                client, opts = self.parse_entry(e)
            except ex.excError as e:
                continue
            if client == '<world>':
                client = '*'
            self.data[path][client] = opts
        return self.data
        
    def is_up(self):
        self.issues = {}
        self.issues_missing_client = []
        self.issues_wrong_opts = []
        self.issues_none = []
        exports = self.get_exports()
        if self.path not in exports:
            return False
        for client in self.opts:
            if client not in exports[self.path]:
                self.issues[client] = "%s not exported to client %s"%(self.path, client)
                self.issues_missing_client.append(client)
            elif self.opts[client] > exports[self.path][client]:
                self.issues[client] = "%s is exported to client %s with missing options: current '%s', minimum required '%s'"%(self.path, client, ','.join(exports[self.path][client]), ','.join(self.opts[client]))
                self.issues_wrong_opts.append(client)
            else:
                self.issues_none.append(client)
        return True

    def start(self):
        try:
            up = self.is_up()
        except ex.excError as e:
            self.log.error("skip start because the share is in unknown state")
            return

        if up and len(self.issues) == 0:
            self.log.info("%s is already up" % self.path)
            return

        self.can_rollback = True
        for client, opts in self.opts.items():
            if client in self.issues_none:
                continue

            if client in self.issues_wrong_opts:
                cmd = [ 'exportfs', '-u', ':'.join((client, self.path)) ]
                ret, out, err = self.vcall(cmd)

            cmd = [ 'exportfs', '-o', ','.join(opts), ':'.join((client, self.path)) ]
            ret, out, err = self.vcall(cmd)
            if ret != 0:
                raise ex.excError

    def stop(self):
        try:
            up = self.is_up()
        except ex.excError as e:
            self.log.error("continue with stop even if the share is in unknown state")
        if not up:
            self.log.info("%s is already down" % self.path)
            return 0
        for client in self.opts:
            cmd = [ 'exportfs', '-u', ':'.join((client, self.path)) ]
            ret, out, err = self.vcall(cmd)
            if ret != 0:
                raise ex.excError

    def _status(self, verbose=False):
        try:
            up = self.is_up()
        except ex.excError as e:
            self.status_log(str(e))
            return rcStatus.WARN
        if len(self.issues) > 0:
            self.status_log('\n'.join(self.issues.values()))
            return rcStatus.WARN
        if rcEnv.nodename in self.always_on:
            if up: return rcStatus.STDBY_UP
            else: return rcStatus.STDBY_DOWN
        else:
            if up: return rcStatus.UP
            else: return rcStatus.DOWN

    def parse_entry(self, e):
        if '(' not in e or ')' not in e:
            raise ex.excError("malformed share opts: '%s'. must be in client(opts) client(opts) format"%e)
        _l = e.split('(')
        client = _l[0]
        opts = _l[1].strip(')')
        return client, set(opts.split(','))

    def __init__(self,
                 rid,
                 path,
                 opts,
                 always_on=set([]),
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        Resource.__init__(self,
                          rid,
                          type="share.nfs",
                          always_on=always_on,
                          disabled=disabled,
                          tags=tags,
                          optional=optional,
                          monitor=monitor,
                          restart=restart,
                          subset=subset)
        if not which("exportfs"):
            raise ex.excInitError("exportfs is not installed")
        self.label = "nfs:"+path
        self.path = path
        l = opts.replace('\\', '').split()
        self.opts = {}
        for e in l:
            try:
                client, opts = self.parse_entry(e)
            except ex.excError as e:
                raise ex.excInitError(str(e))
            self.opts[client] = opts
            

   0707010000bc58000081a400000000000000000000000154fc7bfb00000b06000000660000000300000000000000000000002300000000root/opt/opensvc/lib/resStonith.py    import resources as Res
import rcStatus
import re
import os
from rcGlobalEnv import rcEnv
import rcExceptions as ex

class Stonith(Res.Resource):
    def __init__(self,
                 rid=None,
                 type=None,
                 always_on=set([]),
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 subset=None,
                 tags=set([])):
        Res.Resource.__init__(self,
                              rid,
                              type,
                              optional=optional,
                              disabled=disabled,
                              monitor=monitor,
                              always_on=always_on,
                              restart=restart,
                              subset=subset,
                              tags=tags)
        self.name = None
        self.re_login = re.compile("(login\s*: )|(Login Name:  )|(username: )|(User Name :)", re.IGNORECASE)
        self.re_pass  = re.compile("password", re.IGNORECASE)

    def creds(self):
        import ConfigParser
        c = ConfigParser.RawConfigParser()
        c.read(os.path.join(rcEnv.pathetc, 'auth.conf'))

        username = None
        password = None
        key = None

        if not c.has_section(self.name):
            raise ex.excError("No credentials in node.conf for %s"%self.name)

        if c.has_option(self.name, "username"):
            username = c.get(self.name, 'username')
        else:
            raise ex.excError("No username in node.conf for %s"%self.name)

        if c.has_option(self.name, "password"):
            password = c.get(self.name, 'password')
        if c.has_option(self.name, "key"):
            key = c.get(self.name, 'key')
            if not os.path.exists(key):
                raise ex.excError("key in node.conf for %s does not exist"%self.name)

        if password is None and key is None:
            raise ex.excError("No password nor key in node.conf for %s"%self.name)

        return username, password, key

    def start(self):
        if self.sanity():
            self.log.info("sanity checks passed. trigger stonith method")
        else:
            self.log.info("stonith bypassed")
            return
        self._start()

    def _start(self):
        pass

    def _status(self, verbose=False):
        return rcStatus.NA

    def sanity(self):
        for rs in self.svc.get_res_sets(['hb.ovm', 'hb.openha', 'hb.linuxha']):
            for r in rs.resources:
                if not r.disabled and hasattr(r, 'need_stonith') and r.need_stonith():
                    self.log.info("heartbeat %s asks for stonith"%r.rid)
                    return True
        self.log.debug("no heartbeat asks for stonith")
        return False

  0707010000bc59000081a400000000000000000000000154fc7bfb00000359000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/resStonithCallout.py import resStonith
import rcStatus
from rcGlobalEnv import rcEnv

class Stonith(resStonith.Stonith):
    def __init__(self,
                 rid=None,
                 cmd="/bin/false",
                 always_on=set([]),
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 subset=None):
        resStonith.Stonith.__init__(self,
                                    rid,
                                    "stonith.callout",
                                    optional=optional,
                                    disabled=disabled,
                                    always_on=always_on,
                                    tags=tags,
                                    subset=subset)
        self.cmd = cmd

    def _start(self):
        cmd = self.cmd.split(' ')
        self.vcall(cmd)

   0707010000bc5a000081a400000000000000000000000154fc7bfb000003d0000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resStonithIlo.py import resStonith
import rcStatus
from rcGlobalEnv import rcEnv

class Stonith(resStonith.Stonith):
    def __init__(self,
                 rid=None,
                 name=None,
                 always_on=set([]),
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 subset=None):
        resStonith.Stonith.__init__(self,
                                    rid,
                                    "stonith.ilo",
                                    optional=optional,
                                    disabled=disabled,
                                    always_on=always_on,
                                    tags=tags,
                                    subset=subset)
        self.name = name
        self.username, self.password, self.key = self.creds()

    def _start(self):
        cmd = rcEnv.rsh.split() + ['-l', self.username, '-i', self.key, self.name, 'power', 'reset']
        self.vcall(cmd)
0707010000bc5b000081a400000000000000000000000154fc7bfb00001464000000660000000300000000000000000000002000000000root/opt/opensvc/lib/resSync.py   #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import logging

import rcExceptions as ex
import resources as Res
import datetime
import time
from rcGlobalEnv import rcEnv
from rcScheduler import *

cache_remote_node_type = {}

class Sync(Res.Resource, Scheduler):
    def __init__(self,
                 rid=None,
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 type=type,
                 subset=None):
        if sync_max_delay is None:
            self.sync_max_delay = 1500
        else:
            self.sync_max_delay = sync_max_delay

        if schedule is None:
            self.schedule = "03:59-05:59@121"
        else:
            self.schedule = schedule

        Res.Resource.__init__(self,
                              rid=rid,
                              type=type,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)

    def check_timestamp(self, ts, comp='more', delay=10):
        """ Return False if timestamp is fresher than now-interval
            Return True otherwize.
            Zero is a infinite interval
        """
        if delay == 0:
            raise
        limit = ts + datetime.timedelta(minutes=delay)
        if comp == "more" and datetime.datetime.now() < limit:
            return False
        elif comp == "less" and datetime.datetime.now() < limit:
            return False
        else:
            return True
        return True

    def skip_sync(self, ts):
        if not self.svc.cron:
            return False
        if self.skip_action_schedule(self.rid, "sync_schedule", ts):
            return True
        return False

    def alert_sync(self, ts):
        if ts is None:
            return True
        if not self.check_timestamp(ts, comp="less", delay=self.sync_max_delay):
            return False
        return True

    def remote_fs_mounted(self, node):
        """
        Verify the remote fs is mounted. Some sync resource might want to abort in
        this case.
        """
        if self.dstfs is None:
            # No dstfs check has been configured. Assume the admin knows better.
            return True
        ruser = self.svc.node.get_ruser(node)
        cmd = rcEnv.rsh.split(' ')+['-l', ruser, node, '--', 'df', self.dstfs]
        (ret, out, err) = self.call(cmd, cache=True)
        if ret != 0:
            raise ex.excError

        """
        # df /zones
        /zones             (rpool/zones       ):131578197 blocks 131578197 files
               ^
               separator !

        # df /zones/frcp03vrc0108/root
        /zones/frcp03vrc0108/root(rpool/zones/frcp03vrc0108/rpool/ROOT/solaris-0):131578197 blocks 131578197 files
                                 ^
                                 no separator !
        """
        if self.dstfs+'(' not in out and self.dstfs not in out.split():
            self.log.error("The destination fs %s is not mounted on node %s. refuse to sync %s to protect parent fs"%(self.dstfs, node, self.dst))
            return False
        return True

    def remote_node_type(self, node, target):
        if target == 'drpnodes':
            expected_type = list(set(rcEnv.allowed_svctype) - set(['PRD']))
        elif target == 'nodes':
            expected_type = [self.svc.svctype]
        else:
            self.log.error('unknown sync target: %s'%target)
            raise ex.excError

        ruser = self.svc.node.get_ruser(node)
        rcmd = [os.path.join(rcEnv.pathbin, 'nodemgr'), 'get', '--param', 'node.host_mode']
        if ruser != "root":
            rcmd = ['sudo'] + rcmd

        if node not in cache_remote_node_type:
            cmd = rcEnv.rsh.split(' ')+['-l', ruser, node, '--'] + rcmd
            (ret, out, err) = self.call(cmd, cache=True)
            if ret != 0:
                return False
            words = out.split()
            if len(words) == 1:
                cache_remote_node_type[node] = words[0]
            else:
                cache_remote_node_type[node] = out

        if cache_remote_node_type[node] in expected_type:
            return True
        self.log.error("incompatible remote node '%s' host mode: '%s' (expected in %s)"%\
                       (node, cache_remote_node_type[node], str(expected_type)))
        return False

0707010000bc5c000081a400000000000000000000000154fc7bfb00004201000000660000000300000000000000000000002500000000root/opt/opensvc/lib/resSyncBtrfs.py  #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
from rcGlobalEnv import rcEnv
import datetime
from subprocess import *
import rcExceptions as ex
import rcStatus
import resSync
from rcUtilities import justcall
import rcBtrfs

class SyncBtrfs(resSync.Sync):
    """define btrfs sync resource to be btrfs send/btrfs receive between nodes
    """
    def sort_rset(self, rset):
        rset.resources.sort(lambda x, y: cmp(x.src_subvol, y.src_subvol))

    def __init__(self,
                 rid=None,
                 target=None,
                 src=None,
                 dst=None,
                 delta_store=None,
                 sender=None,
                 recursive=False,
                 snap_size=0,
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 subset=None):
        resSync.Sync.__init__(self,
                              rid=rid,
                              type="sync.btrfs",
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)

        self.label = "btrfs of %s to %s"%(src, ", ".join(target))
        self.target = target
        self.sender = sender
        self.recursive = recursive

        if ":" not in src or src.index(":") == len(src) - 1:
            raise ex.excInitError("malformed src value")
        if ":" not in dst or dst.index(":") == len(dst) - 1:
            raise ex.excInitError("malformed dst value")

        self.src_label = src[:src.index(":")]
        self.src_subvol = src[src.index(":")+1:]
        if dst is None:
            self.dst_label = self.src_label
            self.dst_subvol = self.src_subvol
        else:
            self.dst_label = dst[:dst.index(":")]
            self.dst_subvol = dst[dst.index(":")+1:]

        if delta_store is None:
            self.delta_store = rcEnv.pathvar
        else:
            self.delta_store = delta_store
        self.dst_btrfs = {}
        self.src_btrfs = None

    def init_src_btrfs(self):
        if self.src_btrfs is not None:
            return
        try:
            self.src_btrfs = rcBtrfs.Btrfs(label=self.src_label, log=self.log)
        except rcBtrfs.ExecError as e:
            raise ex.excError(str(e))

    def pre_action(self, rset, action):
        """Prepare snapshots
        Don't sync PRD services when running on !PRD node
        skip snapshot creation if delay_snap in tags
        delay_snap should be used for oracle archive datasets
        """
        resources = [ r for r in rset.resources if not r.skip and not r.is_disabled() ]

        if len(resources) == 0:
            return

        if not action.startswith('sync'):
            return

        s = self.svc.group_status(excluded_groups=set(["sync", "hb", "app"]))
        if not self.svc.force and s['overall'].status != rcStatus.UP:
            self.log.debug("won't sync this resource for a service not up")
            raise ex.excAbortAction

        if self.svc.svctype == 'PRD' and rcEnv.host_mode != 'PRD':
            self.log.debug("won't sync a PRD service running on a !PRD node")
            raise ex.excAbortAction

        self.init_src_btrfs()
        for i, r in enumerate(resources):
            if 'delay_snap' in r.tags:
                continue
            r.get_targets(action)
            tgts = r.targets.copy()
            if len(tgts) == 0:
                continue

            r.get_src_info()

            if not r.src_btrfs.has_subvol(r.src_snap_tosend):
                r.create_snap(r.src, r.src_snap_tosend)

    def __str__(self):
        return "%s target=%s src=%s" % (resSync.Sync.__str__(self),\
                self.target, self.src)

    def create_snap(self, snap_orig, snap):
        self.init_src_btrfs()
        try:
            self.src_btrfs.snapshot(snap_orig, snap, readonly=True, recursive=self.recursive)
        except rcBtrfs.ExistError:
            self.log.error('%s should not exist'%snap)
            raise ex.excError
        except rcBtrfs.ExecError:
            raise ex.excError

    def get_src_info(self):
        self.init_src_btrfs()
        subvol = self.src_subvol.replace('/','_')
        base = self.src_btrfs.snapdir + '/' + subvol
        self.src_snap_sent = base + '@sent'
        self.src_snap_tosend = base + '@tosend'
        self.src = os.path.join(self.src_btrfs.rootdir, self.src_subvol)

    def get_dst_info(self, node):
        if node not in self.dst_btrfs:
            try:
                self.dst_btrfs[node] = rcBtrfs.Btrfs(label=self.dst_label, log=self.log, node=node)
            except rcBtrfs.ExecError as e:
                raise ex.excError(str(e))
            #self.dst_btrfs[node].setup_snap()
        subvol = self.src_subvol.replace('/','_')
        base = self.dst_btrfs[node].snapdir + '/' + subvol
        self.dst_snap_sent = base + '@sent'
        self.dst_snap_tosend = base + '@tosend'
        self.dst = os.path.join(self.dst_btrfs[node].rootdir, self.dst_subvol)

    def get_peersenders(self):
        self.peersenders = set([])
        if 'nodes' == self.sender:
            self.peersenders |= self.svc.nodes
            self.peersenders -= set([rcEnv.nodename])

    def get_targets(self, action=None):
        self.targets = set()
        if 'nodes' in self.target and action in (None, 'syncnodes'):
            self.targets |= self.svc.nodes
        if 'drpnodes' in self.target and action in (None, 'syncdrp'):
            self.targets |= self.svc.drpnodes
        self.targets -= set([rcEnv.nodename])

    def syncnodes(self):
        self._syncupdate('syncnodes')

    def syncdrp(self):
        self._syncupdate('syncdrp')

    def sanity_checks(self):
        if not self.svc.force:
            s = self.svc.group_status(excluded_groups=set(["sync", "hb", "app"]))
            if s['overall'].status != rcStatus.UP:
                if not self.svc.cron:
                    self.log.info("won't sync this resource for a service not up")
                raise ex.excError

        """ Refuse to sync from a flex non-primary node
        """
        if self.svc.clustertype in ["flex", "autoflex"] and \
           self.svc.flex_primary != rcEnv.nodename:
            if not self.svc.cron:
                self.log.info("won't sync this resource from a flex non-primary node")
            raise ex.excError

    def syncfullsync(self):
        self.init_src_btrfs()
        try:
            self.sanity_checks()
        except ex.excError:
            return
        self.get_src_info()
        if not self.src_btrfs.has_subvol(self.src_snap_tosend):
            self.create_snap(self.src, self.src_snap_tosend)
        self.get_targets()
        for n in self.targets:
            self.get_dst_info(n)
            self.btrfs_send_initial(n)
            self.rotate_snaps(n)
            self.install_snaps(n)
        self.rotate_snaps()
        self.write_statefile()
        for n in self.targets:
            self.push_statefile(n)

    def btrfs_send_incremental(self, node):
        if self.recursive :  
            send_cmd = ['btrfs', 'send', '-R',
                        '-c', self.src_snap_sent,
                        '-p', self.src_snap_sent,
                        self.src_snap_tosend]
        else:
            send_cmd = ['btrfs', 'send',
                        '-c', self.src_snap_sent,
                        '-p', self.src_snap_sent,
                        self.src_snap_tosend]

        receive_cmd = ['btrfs', 'receive', self.dst_btrfs[node].snapdir]
        if node is not None:
            receive_cmd = rcEnv.rsh.strip(' -n').split() + [node] + receive_cmd

        self.log.info(' '.join(send_cmd + ["|"] + receive_cmd))
        p1 = Popen(send_cmd, stdout=PIPE)
        p2 = Popen(receive_cmd, stdin=p1.stdout, stdout=PIPE)
        buff = p2.communicate()
        if p2.returncode != 0:
            if buff[1] is not None and len(buff[1]) > 0:
                self.log.error(buff[1])
            self.log.error("sync update failed")
            raise ex.excError
        if buff[0] is not None and len(buff[0]) > 0:
            self.log.info(buff[0])

    def btrfs_send_initial(self, node=None):
        if self.recursive :  
            send_cmd = ['btrfs', 'send', '-R', self.src_snap_tosend]
        else:
            send_cmd = ['btrfs', 'send', self.src_snap_tosend]

        receive_cmd = ['btrfs', 'receive', self.dst_btrfs[node].snapdir]
        if node is not None:
            receive_cmd = rcEnv.rsh.strip(' -n').split() + [node] + receive_cmd

        self.log.info(' '.join(send_cmd + ["|"] + receive_cmd))
        p1 = Popen(send_cmd, stdout=PIPE)
        p2 = Popen(receive_cmd, stdin=p1.stdout, stdout=PIPE)
        buff = p2.communicate()
        if p2.returncode != 0:
            if buff[1] is not None and len(buff[1]) > 0:
                self.log.error(buff[1])
            self.log.error("full sync failed")
            raise ex.excError
        if buff[0] is not None and len(buff[0]) > 0:
            self.log.info(buff[0])

    def remove_snap(self, node=None):
        self.init_src_btrfs()
        if node is not None:
            o = self.dst_btrfs[node]
            subvol = self.dst_snap_sent
        else:
            o = self.src_btrfs
            subvol = self.src_snap_sent

        try:
            o.subvol_delete(subvol, recursive=self.recursive)
        except rcBtrfs.ExecError:
            raise ex.excError()

    def rename_snap(self, node=None):
        self.init_src_btrfs()
        if node is None:
            o = self.src_btrfs
            src = self.src_snap_tosend
            dst = self.src_snap_sent
        else:
            o = self.dst_btrfs[node]
            src = self.dst_snap_tosend
            dst = self.dst_snap_sent

        if o.has_subvol(dst):
            self.log.error("%s should not exist"%self.dst_snap_sent)
            raise ex.excError

        if self.recursive :
            # ??
            cmd = ['mv', src, dst]
        else:
            cmd = ['mv', src, dst]

        if node is not None:
            cmd = rcEnv.rsh.split() + [node] + cmd
        ret, out, err = self.vcall(cmd)

        if ret != 0:
            raise ex.excError

    def remove_dst(self, node=None):
        if node is None:
            return

        subvols = self.dst_btrfs[node].get_subvols_in_path(self.dst)

        try:
            self.dst_btrfs[node].subvol_delete(subvols)
        except rcBtrfs.ExecError:
            raise ex.excError()
            
    def install_dst(self, node=None):
        if node is None:
            return
        try:
            self.dst_btrfs[node].snapshot(self.dst_snap_sent, self.dst, readonly=False)
        except rcBtrfs.ExistError:
            self.log.error('%s should not exist'%snap)
            raise ex.excError()
        except rcBtrfs.ExecError:
            self.log.error("failed to install snapshot %s on node %s"%(self.dst, node))
            raise ex.excError()

    def install_snaps(self, node=None):
        self.remove_dst(node)
        self.install_dst(node)

    def rotate_snaps(self, node=None):
        self.remove_snap(node)
        self.rename_snap(node)

    def _syncupdate(self, action):
        self.init_src_btrfs()
        try:
            self.sanity_checks()
        except ex.excError:
            return
        self.get_targets(action)
        if len(self.targets) == 0:
            return
        self.get_src_info()
        if not self.src_btrfs.has_subvol(self.src_snap_tosend):
            self.create_snap(self.src, self.src_snap_tosend)
        if self.src_btrfs.has_subvol(self.src_snap_sent):
            for n in self.targets:
                self.get_dst_info(n)
                self.btrfs_send_incremental(n)
                self.rotate_snaps(n)
                self.install_snaps(n)
        else:
            for n in self.targets:
                self.get_dst_info(n)
                self.btrfs_send_initial(n)
                self.rotate_snaps(n)
                self.install_snaps(n)
        self.rotate_snaps()
        self.write_statefile()
        for n in self.targets:
            self.push_statefile(n)

    def start(self):
        pass

    def stop(self):
        pass

    def can_sync(self, target=None):
        try:
            ls = self.get_local_state()
            ts = datetime.datetime.strptime(ls['date'], "%Y-%m-%d %H:%M:%S.%f")
        except IOError:
            self.log.error("btrfs state file not found")
            return True
        except:
            import sys
            import traceback
            e = sys.exc_info()
            print(e[0], e[1], traceback.print_tb(e[2]))
            return False
        if self.skip_sync(ts):
            self.status_log("Last sync on %s older than %i minutes"%(ts, self.sync_max_delay))
            return False
        return True

    def _status(self, verbose=False):
        try:
            ls = self.get_local_state()
            now = datetime.datetime.now()
            last = datetime.datetime.strptime(ls['date'], "%Y-%m-%d %H:%M:%S.%f")
            delay = datetime.timedelta(minutes=self.sync_max_delay)
        except IOError:
            self.status_log("btrfs state file not found")
            return rcStatus.WARN
        except:
            import sys
            import traceback
            e = sys.exc_info()
            print(e[0], e[1], traceback.print_tb(e[2]))
            return rcStatus.WARN
        if last < now - delay:
            self.status_log("Last sync on %s older than %i minutes"%(last, self.sync_max_delay))
            return rcStatus.WARN
        return rcStatus.UP

    def check_remote(self, node):
        rs = self.get_remote_state(node)
        if self.snap1_uuid != rs['uuid']:
            self.log.error("%s last update uuid doesn't match snap1 uuid"%(node))
            raise ex.excError

    def get_remote_state(self, node):
        self.set_statefile()
        cmd1 = ['cat', self.statefile]
        cmd = rcEnv.rsh.split() + [node] + cmd1
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            self.log.error("could not fetch %s last update uuid"%node)
            raise ex.excError
        return self.parse_statefile(out, node=node)

    def get_local_state(self):
        self.set_statefile()
        with open(self.statefile, 'r') as f:
            out = f.read()
        return self.parse_statefile(out)

    def get_snap_uuid(self, snap):
        self.init_src_btrfs()
        self.snap_uuid = self.src_btrfs.get_transid(snap)

    def set_statefile(self):
        self.statefile = os.path.join(rcEnv.pathvar,
                                      self.svc.svcname+'_'+self.rid+'_btrfs_state')

    def write_statefile(self):
        self.set_statefile()
        self.get_snap_uuid(self.src_snap_sent)
        self.log.info("update state file with snap uuid %s"%self.snap_uuid)
        with open(self.statefile, 'w') as f:
             f.write(str(datetime.datetime.now())+';'+self.snap_uuid+'\n')

    def _push_statefile(self, node):
        cmd = rcEnv.rcp.split() + [self.statefile, node+':'+self.statefile.replace('#', '\#')]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def push_statefile(self, node):
        self.set_statefile()
        self._push_statefile(node)
        self.get_peersenders()
        for s in self.peersenders:
            self._push_statefile(s)

    def parse_statefile(self, out, node=None):
        self.set_statefile()
        if node is None:
            node = rcEnv.nodename
        lines = out.strip().split('\n')
        if len(lines) != 1:
            self.log.error("%s:%s is corrupted"%(node, self.statefile))
            raise ex.excError
        fields = lines[0].split(';')
        if len(fields) != 2:
            self.log.error("%s:%s is corrupted"%(node, self.statefile))
            raise ex.excError
        return dict(date=fields[0], uuid=fields[1])

   0707010000bc5d000081a400000000000000000000000154fc7bfb0000161b000000660000000300000000000000000000002300000000root/opt/opensvc/lib/resSyncDcs.py    #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import rcExceptions as ex
import subprocess
import resSync
from rcGlobalEnv import rcEnv
from rcUtilities import justcall

class SyncDcs(resSync.Sync):
    def wait_for_devs_ready(self):
        pass

    def get_active_dcs(self):
        if self.active_dcs is not None:
            return
        for d in self.dcs:
            try:
                self.log.debug("try dcs", d)
                self.dcscmd("get-dcsserver", dcs=d)
                self.active_dcs = d
                self.log.debug("set active dcs", self.active_dcs)
                return
            except:
                pass
        if self.active_dcs is None:
            self.log.error("no usable dcs server")
            raise ex.excError

    def get_auth(self):
        if self.username is not None and \
           self.password is not None:
            return
        self.get_active_manager()
        if self.active_manager is None:
            raise ex.excError("no active manager")
        import ConfigParser
        if not os.path.exists(self.conf):
            raise ex.excError("missing %s"%self.conf)
        self.config = ConfigParser.RawConfigParser()
        self.config.read(self.conf)
        if not self.config.has_section(self.active_manager):
            raise ex.excError("no credentials for manager %s in %s"%(self.active_manager, self.conf))
        if not self.config.has_option(self.active_manager, "username"):
            raise ex.excError("no username set for manager %s in %s"%(self.active_manager, self.conf))
        if not self.config.has_option(self.active_manager, "password"):
            raise ex.excError("no password set for manager %s in %s"%(self.active_manager, self.conf))
        self.username = self.config.get(self.active_manager, "username")
        self.password = self.config.get(self.active_manager, "password")
 
    def dcscmd(self, cmd="", verbose=False, check=True, dcs=None):
        if len(cmd) == 0:
            return

        self.get_active_manager()
        if dcs is None:
            self.get_active_dcs()
            dcs = self.active_dcs
        self.get_auth()
        cmd = self.ssh + [self.active_manager,
               "connect-dcsserver -server %s -username %s -password %s -connection %s ; "%(dcs, self.username, self.password, self.conn)+\
               cmd+\
               " ; disconnect-dcsserver -connection %s"%self.conn]
        if verbose:
            import re
            from copy import copy
            _cmd = copy(cmd)
            _cmd[2] = re.sub(r'password \S+', 'password xxxxx', _cmd[2])
            self.log.info(subprocess.list2cmdline(_cmd))
            ret, out, err = self.call(cmd)
        else:
            ret, out, err = self.call(cmd, errlog=False)
        if check and "ErrorId" in err:
            raise ex.excError("dcscmd command execution error")
        return ret, out, err

    def get_active_manager(self):
        if self.active_manager is not None:
            return
        for manager in self.manager:
            cmd = self.ssh + [manager, 'id']
            out, err, ret = justcall(cmd)
            if ret != 0:
                continue
            self.active_manager = manager
            self.log.debug("set active manager", self.active_manager)
            return
        if self.active_manager is None:
            self.log.error("no usable manager")
            raise ex.excError

    def __init__(self,
                 rid=None,
                 manager=set([]),
                 dcs=set([]),
                 type="sync.dcsunknown",
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 subset=None,
                 internal=False):
        resSync.Sync.__init__(self,
                              rid=rid,
                              type=type,
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              subset=subset,
                              tags=tags)
        self.ssh = rcEnv.rsh.split()
        self.active_dcs = None
        self.active_manager = None
        self.username = None
        self.password = None
        self.dcs = dcs
        self.manager = manager
        self.conf = os.path.join(rcEnv.pathetc, 'auth.conf')

    def on_add(self):
        self.get_conn()

    def get_conn(self):
        from hashlib import md5
        import uuid
        o = md5()
        o.update(uuid.uuid1().hex)
        o.update(self.svc.svcname)
        self.conn = o.hexdigest()

    def __str__(self):
        return "%s dcs=%s manager=%s" % (
                 resSync.Sync.__str__(self),
                 ' '.join(self.dcs),
                 ' '.join(self.manager))

 0707010000bc5e000081a400000000000000000000000154fc7bfb00001f6d000000660000000300000000000000000000002700000000root/opt/opensvc/lib/resSyncDcsCkpt.py    #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import rcExceptions as ex
import rcStatus
import datetime
import resSyncDcs
from rcGlobalEnv import rcEnv
from rcUtilities import justcall

class syncDcsCkpt(resSyncDcs.SyncDcs):
    def can_sync(self, target=None):
        return True

    def checkpoint(self):
        cmd = ""
        vars = ""
        for i, d in enumerate(self.pairs):
            cmd += '$v%d=Get-DcsVirtualDisk -VirtualDisk %s -connection %s;'%(i, d['src'], self.conn)
            vars += '$v%d '%i

        cmd += "echo %s|Set-DcsReplicationCheckPoint -connection %s"%(vars, self.conn)
        self.dcscmd(cmd, verbose=True)
        self.update_tsfile()

    def get_snap(self, snap):
        if snap in self._info:
            return self._info[snap]

        cmd = 'get-dcssnapshot -snapshot %s -connection %s;'%(snap, self.conn)
        try:
            ret, out, err = self.dcscmd(cmd)
        except:
            return None

        info = {}
        lines = out.split('\n')
        for line in lines:
            l = line.split(': ')
            if len(l) != 2:
                continue
            var = l[0].strip()
            val = l[1].strip()
            if var == 'TimeStamp':
                info['TimeStamp'] = datetime.datetime.strptime(val, "%d/%m/%Y %H:%M:%S")
            elif var in ['Type', 'State', 'ActiveOperation', 'Failure']:
                info[var] = val
        if len(info) > 0:
            self._info[snap] = info
        return info

    def drpnodes_status(self, verbose=False, skip_prereq=False):
        err = False
        errlog = []
        log = []
        try:
            self.get_auth()
        except ex.excError as e:
            self.status_log(str(e))
            return rcStatus.WARN
        for snap in [p['dst_ckpt'] for p in self.pairs]:
            info = self.get_snap(snap)
            if info is None:
                errlog.append("checkpoint snapshot %s does not exists"%snap)
                err |= True
                continue
            if info['State'] not in ['Healthy']:
                errlog.append("checkpoint snapshot %s state is %s"%(snap, info['State']))
                err |= True
            if info['Failure'] not in ['NoFailure']:
                errlog.append("checkpoint snapshot %s failure state is %s"%(snap, info['Failure']))
                err |= True
            if info['TimeStamp'] < datetime.datetime.now() - datetime.timedelta(minutes=self.sync_max_delay):
                errlog.append("checkpoint snapshot %s too old"%snap)
                err |= True
            log.append("last update on %s"%info['TimeStamp'])
        if err:
            self.status_log('\n'.join(errlog))
            return rcStatus.WARN
        self.status_log('\n'.join(log))
        return rcStatus.UP

    def nodes_status(self, verbose=False, skip_prereq=False):
        err = False
        ts = self.read_tsfile()
        if ts < datetime.datetime.now() - datetime.timedelta(minutes=self.sync_max_delay):
            self.status_log("checkpoint too old")
            err |= True
        self.status_log("last update on %s"%str(ts))
        if err:
            return rcStatus.WARN
        return rcStatus.UP

    def _status(self, verbose=False, skip_prereq=False):
        if rcEnv.nodename in self.svc.nodes:
            return self.nodes_status(verbose, skip_prereq)
        else:
            return self.drpnodes_status(verbose, skip_prereq)

    def pause_checkpoint(self):
        cmd = ""
        for d in self.pairs:
            cmd += 'Disable-DcsTask -Task %s -connection %s ; '%(self.task_name(d['dst_ckpt']), self.conn)
        self.dcscmd(cmd, verbose=True)

    def create_task(self):
        cmd = "Get-DcsTask -connection %s"%self.conn
        tasks = []
        ret, out, err = self.dcscmd(cmd, verbose=False)
        for line in out.split('\n'):
             if line.startswith("Caption"):
                 tasks.append(line.split(':')[-1].strip())

        cmd = ""
        for d in self.pairs:
            if self.task_name(d['dst_ckpt']) in tasks:
                continue
            cmd += "Add-DcsTask -Disabled -Name %s -connection %s; "%(self.task_name(d['dst_ckpt']), self.conn)
            cmd += 'Add-DcsTrigger -Task %s -VirtualDisk "%s" -connection %s; '%(self.task_name(d['dst_ckpt']), d['dst'], self.conn)
            cmd += 'Add-DcsAction -Task %s -MethodActionType UpdateSnapshot -connection %s -TargetId (Get-DcsSnapshot -snapshot "%s" -connection %s).Id ; '%(self.task_name(d['dst_ckpt']), self.conn, d['dst_ckpt'], self.conn)
        self.dcscmd(cmd, verbose=True)

    def resume_checkpoint(self):
        self.create_task()
        cmd = ""
        for d in self.pairs:
            cmd += 'Enable-DcsTask -Task %s -connection %s; '%(self.task_name(d['dst_ckpt']), self.conn)
        self.dcscmd(cmd, verbose=True)

    def task_name(self, id):
        return '-'.join((self.svc.svcname, self.rid, id))

    def syncbreak(self):
        self.pause_checkpoint()

    def start(self):
        if rcEnv.nodename not in self.svc.drpnodes:
            return
        self.pause_checkpoint()

    def syncresume(self):
        self.resume_checkpoint()

    def syncupdate(self):
        self.checkpoint()

    def refresh_svcstatus(self):
        self.svcstatus = self.svc.group_status(excluded_groups=set(["sync", 'hb', 'app']))

    def get_svcstatus(self):
        if len(self.svcstatus) == 0:
            self.refresh_svcstatus()

    def __init__(self,
                 rid=None,
                 pairs=[],
                 manager=set([]),
                 dcs=set([]),
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 internal=False,
                 subset=None):
        resSyncDcs.SyncDcs.__init__(self,
                                    rid=rid, type="sync.dcsckpt",
                                    manager=manager,
                                    dcs=dcs,
                                    sync_max_delay=sync_max_delay,
                                    schedule=schedule,
                                    optional=optional,
                                    disabled=disabled,
                                    tags=tags,
                                    subset=subset)

        self.label = "DCS checkpoint snapshot of %s"%' ,'.join(map(lambda x: x['src'], pairs))
        self.pairs = pairs
        self._info = {}

    def tsfile(self):
        return os.path.join(rcEnv.pathvar, '.'.join((self.svc.svcname, self.rid, 'ts')))

    def update_tsfile(self):
        import datetime
        now = datetime.datetime.now()
        with open(self.tsfile(), 'w') as f:
            f.write(str(now)+'\n')

    def read_tsfile(self):
        import datetime
        try:
            with open(self.tsfile(), 'r') as f:
                ts = datetime.datetime.strptime(f.read(),"%Y-%m-%d %H:%M:%S.%f\n")
        except:
            ts = datetime.datetime(year=2000, month=01, day=01)
        return ts

    def __str__(self):
        return "%s dcs=%s manager=%s pairs=%s" % (
                 resSync.Sync.__str__(self),
                 ' '.join(self.dcs),
                 ' '.join(self.manager),
                 str(pairs))

   0707010000bc5f000081a400000000000000000000000154fc7bfb00001cf8000000660000000300000000000000000000002700000000root/opt/opensvc/lib/resSyncDcsSnap.py    #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import rcExceptions as ex
import rcStatus
import datetime
import resSyncDcs
from rcGlobalEnv import rcEnv
from rcUtilities import justcall

class syncDcsSnap(resSyncDcs.SyncDcs):
    def can_sync(self, target=None):
        ts = None
 
        """ get oldest snap
        """
        for snap in self.snapname:
            info = self.get_snap(snap)
            if info is None:
                self.log.debug("snap %s missing"%snap)
                return True
            _ts = info['TimeStamp']
            if ts is None or _ts < ts:
                ts = _ts
        return not self.skip_sync(ts)

    def update_snap(self):
        cmd = ""
        vars = ""
        for i, snap in enumerate(self.snapname):
            cmd += '$v%d=get-dcssnapshot -snapshot %s -connection %s;'%(i, snap, self.conn)
            vars += '$v%d '%i

        cmd += "echo %s|update-dcssnapshot -Y -connection %s"%(vars, self.conn)
        self.dcscmd(cmd, verbose=True)

    def get_snaps(self):
        cmd = ""
        for i, snap in enumerate(self.snapname):
            cmd += 'get-dcssnapshot -snapshot %s -connection %s;'%(snap, self.conn)
        try:
            ret, out, err = self.dcscmd(cmd)
        except:
            return

        """
SourceLogicalDiskId      : ef989edf-4a6d-4af6-8b9d-bc6d2070a36c
DestinationLogicalDiskId : 24a6a20f-59f3-4c4d-b4de-9a4f45afff44
Type                     : Full
TimeStamp                : 17/01/2013 14:16:49
ActiveOperation          : NoOperation
State                    : Migrated
Failure                  : NoFailure
SequenceNumber           : 4677814244
Id                       : V.{06C86883-CF53-11E1-9203-441EA14CCCC6}-00000177--V
                           .{06C86883-CF53-11E1-9203-441EA14CCCC6}-000001D7
Caption                  : S64lmwbic6f-22-clone-02
ExtendedCaption          : S64lmwbic6f-22-clone-02 on SDSLMW03
Internal                 : False

SourceLogicalDiskId      : f0450ff7-076f-4dbc-bee0-25f6a586f5b2
DestinationLogicalDiskId : 17e98dbf-1457-41c9-aaad-5b6d7d8cc81c
Type                     : Full
TimeStamp                : 17/01/2013 14:16:49
ActiveOperation          : NoOperation
State                    : Migrated
Failure                  : NoFailure
SequenceNumber           : 4677814247
Id                       : V.{06C86883-CF53-11E1-9203-441EA14CCCC6}-0000017A--V
                           .{06C86883-CF53-11E1-9203-441EA14CCCC6}-000001DA
Caption                  : S64lmwbic6f-25-clone-02
ExtendedCaption          : S64lmwbic6f-25-clone-02 on SDSLMW03
Internal                 : False

"""
        info = {}
        lines = out.split('\n')
        for line in lines:
            l = line.split(': ')
            if len(l) != 2:
                continue
            var = l[0].strip()
            val = l[1].strip()
            if var == 'Internal' and len(info) > 0:
                self._info[info['Caption']] = info
                info = {}
            if var == 'TimeStamp':
                info['TimeStamp'] = datetime.datetime.strptime(val, "%d/%m/%Y %H:%M:%S")
            elif var in ['Type', 'State', 'ActiveOperation', 'Failure', 'Caption']:
                info[var] = val

    def get_snap(self, snap):
        if len(self._info) == 0:
            self.get_snaps()
        if snap not in self._info:
            return None
        return self._info[snap]

    def no_status(self):
        if self.svc.clustertype in ["flex", "autoflex"] and \
           self.svc.flex_primary != rcEnv.nodename:
            return True
        s = self.svc.group_status(excluded_groups=set(["sync", "hb", "app"]))
        if s['overall'].status not in [rcStatus.UP, rcStatus.NA]:
            return True
        return False

    def _status(self, verbose=False, skip_prereq=False):
        if self.no_status():
            self.status_log("skip on secondary node")
            return rcStatus.NA

        err = False
        errlog = []
        log = []
        try:
            self.get_auth()
        except ex.excError as e:
            self.status_log(str(e))
            return rcStatus.WARN
        for snap in self.snapname:
            info = self.get_snap(snap)
            if info is None:
                errlog.append("snapshot %s does not exists"%snap)
                err |= True
                continue
            if info['State'] not in ['Healthy','Migrated']:
                errlog.append("snapshot %s state is %s"%(snap, info['State']))
                err |= True
            if info['Failure'] not in ['NoFailure']:
                errlog.append("snapshot %s failure state is %s"%(snap, info['Failure']))
                err |= True
            if info['TimeStamp'] < datetime.datetime.now() - datetime.timedelta(minutes=self.sync_max_delay):
                errlog.append("snapshot %s too old"%snap)
                err |= True
            log.append("last update on %s"%info['TimeStamp'])
        if err:
            self.status_log('\n'.join(errlog))
            return rcStatus.WARN
        self.status_log('\n'.join(log))
        return rcStatus.UP

    def syncresync(self):
        self.update_snap()

    def refresh_svcstatus(self):
        self.svcstatus = self.svc.group_status(excluded_groups=set(["sync", 'hb', 'app']))

    def get_svcstatus(self):
        if len(self.svcstatus) == 0:
            self.refresh_svcstatus()

    def __init__(self,
                 rid=None,
                 snapname=set([]),
                 manager=set([]),
                 dcs=set([]),
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 subset=None,
                 internal=False):
        resSyncDcs.SyncDcs.__init__(self,
                                    rid=rid,
                                    type="sync.dcssnap",
                                    manager=manager,
                                    dcs=dcs,
                                    sync_max_delay=sync_max_delay,
                                    schedule=schedule,
                                    optional=optional,
                                    disabled=disabled,
                                    subset=subset,
                                    tags=tags)

        self.label = "DCS snapshot %s"%', '.join(snapname)
        self.snapname = snapname
        self._info = {}

    def __str__(self):
        return "%s dcs=%s manager=%s snapname=%s" % (
                 resSync.Sync.__str__(self),
                 ' '.join(self.dcs),
                 ' '.join(self.manager),
                 ' '.join(self.snapname))

0707010000bc60000081a400000000000000000000000154fc7bfb00003c71000000660000000300000000000000000000002300000000root/opt/opensvc/lib/resSyncDds.py    #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import logging

from rcGlobalEnv import rcEnv
from rcUtilities import which
from rcUtilitiesLinux import lv_info
from subprocess import *
import rcExceptions as ex
import rcStatus
import datetime
import resSync

class syncDds(resSync.Sync):
    def pre_action(self, rset, action):
        resources = [ r for r in rset.resources if not r.skip and not r.is_disabled() ]

        if len(resources) == 0:
            return

        """Don't sync PRD services when running on !PRD node
        """
        if self.svc.svctype == 'PRD' and rcEnv.host_mode != 'PRD':
            self.log.debug("won't sync a PRD service running on a !PRD node")
            raise ex.excAbortAction

        for i, r in enumerate(resources):
            if not r.svc_syncable():
                return
            r.get_info()
            if action == 'syncfullsync':
                r.create_snap1()
            elif action in ['syncupdate', 'syncresync', 'syncdrp', 'syncnodes']:
                if action == 'syncnodes' and self.target != ['nodes']:
                    return
                if action == 'syncdrp' and self.target != ['drpnodes']:
                    return
                r.get_info()
                r.get_snap1_uuid()
                nb = 0
                tgts = r.targets.copy()
                for n in tgts:
                    try:
                        r.check_remote(n)
                        nb += 1
                    except:
                        self.targets -= set([n])
                if nb != len(tgts):
                    self.log.error('all destination nodes must be present for dds-based synchronization to proceed')
                    raise ex.excError
                r.create_snap2()

    def snap_exists(self, dev):
        if not os.path.exists(dev):
            self.log.debug('dev path does not exist')
            return False
        cmd = ['lvs', '--noheadings', '-o', 'snap_percent', dev]
        (ret, out, err) = self.call(cmd, errlog=False)
        if ret != 0:
            return False
        if len(out.strip()) == 0:
            self.log.debug('dev is not a snapshot')
            return False
        return True

    def create_snap(self, dev, lv):
        if self.snap_exists(dev):
            self.log.error('%s should not exist'%dev)
            raise ex.excError
        cmd = ['lvcreate', '-s', '-n', lv,
               '-L', str(self.snap_size)+'M',
               os.path.join(os.sep, 'dev', self.src_vg, self.src_lv)
              ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def set_statefile(self):
        self.statefile = os.path.join(rcEnv.pathvar,
                                      self.svc.svcname+'_'+self.rid+'_dds_state')

    def create_snap1(self):
        if self.snap_exists(self.snap2):
            self.log.error('%s should not exist'%self.snap2)
            raise ex.excError
        self.create_snap(self.snap1, self.snap1_lv)
        self.write_statefile()

    def create_snap2(self):
        self.create_snap(self.snap2, self.snap2_lv)

    def snap_name(self, snap):
        return os.path.basename(self.src_lv).replace('-', '_')+'_osvc_'+snap

    def get_src_info(self):
        (self.src_vg, self.src_lv, self.src_size) = lv_info(self, self.src)
        if self.src_lv is None:
            self.log.error("unable to fetch source logical volume information")
            raise ex.excError
        if self.snap_size == 0:
            self.snap_size = self.src_size//10
        self.snap1_lv = self.snap_name('snap1')
        self.snap2_lv = self.snap_name('snap2')
        self.snap1 = os.path.join(os.sep, 'dev', self.src_vg, self.snap1_lv)
        self.snap2 = os.path.join(os.sep, 'dev', self.src_vg, self.snap2_lv)
        self.snap1_cow = os.path.join(os.sep, 'dev', 'mapper',
                                      '-'.join([self.src_vg.replace('-', '--'),
                                                self.snap1_lv,
                                                'cow'])
                                     )

    def get_peersenders(self):
        self.peersenders = set([])
        if 'nodes' not in self.target:
            self.peersenders |= self.svc.nodes
            self.peersenders -= set([rcEnv.nodename])

    def get_targets(self):
        self.targets = set()
        if 'nodes' in self.target:
            self.targets |= self.svc.nodes
        if 'drpnodes' in self.target:
            self.targets |= self.svc.drpnodes
        self.targets -= set([rcEnv.nodename])

    def get_info(self):
        self.get_targets()
        self.get_src_info()

    def svc_syncable(self):
        s = self.svc.group_status(excluded_groups=set(["sync", "hb"]))
        if s['overall'].status != rcStatus.UP:
            self.log.debug("won't sync this resource for a service not up")
            return False
        if self.svc.clustertype in ["flex", "autoflex"] and \
           self.svc.flex_primary != rcEnv.nodename:
            self.log.debug("won't sync this resource from a flex non-primary node")
            return set([])

        return True

    def syncfullsync(self):
        if not self.svc_syncable():
            return
        for n in self.targets:
            self.do_fullsync(n)

    def do_fullsync(self, node):
        dst = self.dsts[node]
        cmd1 = ['dd', 'if='+self.snap1, 'bs=1M']
        cmd2 = rcEnv.rsh.split() + [node, 'dd', 'bs=1M', 'of='+dst]
        self.log.info(' '.join(cmd1 + ["|"] + cmd2))
        p1 = Popen(cmd1, stdout=PIPE)
        p2 = Popen(cmd2, stdin=p1.stdout, stdout=PIPE)
        p2.communicate()[0]
        if p2.returncode != 0:
            self.log.error("full sync failed")
            raise ex.excError
        self.push_statefile(node)

    def get_snap1_uuid(self):
        cmd = ['lvs', '--noheadings', '-o', 'uuid', self.snap1]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError
        self.snap1_uuid = out.strip()

    def write_statefile(self):
        self.set_statefile()
        self.get_snap1_uuid()
        self.log.info("update state file with snap uuid %s"%self.snap1_uuid)
        with open(self.statefile, 'w') as f:
             f.write(str(datetime.datetime.now())+';'+self.snap1_uuid+'\n')

    def _push_statefile(self, node):
        cmd = rcEnv.rcp.split() + [self.statefile, node+':'+self.statefile]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def push_statefile(self, node):
        self.set_statefile()
        self._push_statefile(node)
        self.get_peersenders()
        for s in self.peersenders:
            self._push_statefile(s)

    def apply_delta(self, node):
        if not which('dds'):
            raise ex.excError("dds executable not found")
        dst = self.dsts[node]
        extract_cmd = ['dds', '--extract', '--cow', self.snap1_cow, '--source',
                       self.snap2]
        merge_cmd = ['dds', '--merge', '--dest', dst, '-v']
        merge_cmd = rcEnv.rsh.split() + [node] + merge_cmd
        self.log.info(' '.join(extract_cmd + ["|"] + merge_cmd))
        p1 = Popen(extract_cmd, stdout=PIPE)
        p2 = Popen(merge_cmd, stdin=p1.stdout, stdout=PIPE)
        buff = p2.communicate()
        if p2.returncode != 0:
            if buff[1] is not None and len(buff[1]) > 0:
                self.log.error(buff[1])
            self.log.error("sync update failed")
            raise ex.excError
        if buff[0] is not None and len(buff[0]) > 0:
            self.log.info(buff[0])

    def do_update(self, node):
        self.apply_delta(node)

    def remove_snap1(self):
        if not self.snap_exists(self.snap1):
            return
        cmd = ['lvremove', '-f', self.snap1]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def rename_snap2(self):
        if not self.snap_exists(self.snap2):
            self.log.error("%s should exist"%self.snap2)
            raise ex.excError
        if self.snap_exists(self.snap1):
            self.log.error("%s should not exist"%self.snap1)
            raise ex.excError
        cmd = ['lvrename', self.src_vg, self.snap2_lv, self.snap1_lv]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def rotate_snaps(self):
        self.remove_snap1()
        self.rename_snap2()

    def check_remote(self, node):
        rs = self.get_remote_state(node)
        if self.snap1_uuid != rs['uuid']:
            self.log.error("%s last update uuid doesn't match snap1 uuid"%(node))
            raise ex.excError

    def get_remote_state(self, node):
        self.set_statefile()
        cmd1 = ['env', 'LANG=C', 'cat', self.statefile]
        cmd = rcEnv.rsh.split() + [node] + cmd1
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            self.log.error("could not fetch %s last update uuid"%node)
            raise ex.excError
        return self.parse_statefile(out, node=node)

    def get_local_state(self):
        self.set_statefile()
        with open(self.statefile, 'r') as f:
            out = f.read()
        return self.parse_statefile(out)

    def parse_statefile(self, out, node=None):
        self.set_statefile()
        if node is None:
            node = rcEnv.nodename
        lines = out.strip().split('\n')
        if len(lines) != 1:
            self.log.error("%s:%s is corrupted"%(node, self.statefile))
            raise ex.excError
        fields = lines[0].split(';')
        if len(fields) != 2:
            self.log.error("%s:%s is corrupted"%(node, self.statefile))
            raise ex.excError
        return dict(date=fields[0], uuid=fields[1])

    def syncnodes(self):
        if self.target != ['nodes']:
            return
        self.syncupdate()

    def syncdrp(self):
        if self.target != ['drpnodes']:
            return
        self.syncupdate()

    def syncupdate(self):
        if not self.svc_syncable():
            return
        for n in self.targets:
            self.do_update(n)
        self.rotate_snaps()
        self.write_statefile()
        for n in self.targets:
            self.push_statefile(n)

    def checksum(self, node, bdev, q=None):
        cmd = ['md5sum', bdev]
        if node != rcEnv.nodename:
            cmd = rcEnv.rsh.split() + [node] + cmd
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            return ""
        o = out.split()
        if q is not None:
            q.put(o[0])
        else:
            self.checksums[node] = o[0]

    def syncverify(self):
        s = self.svc.group_status(excluded_groups=set(["sync", "hb"]))
        if s['overall'].status != rcStatus.UP:
            self.log.debug("won't verify this resource for a service not up")
            return
        if self.svc.clustertype in ["flex", "autoflex"] and \
           self.svc.flex_primary != rcEnv.nodename:
            self.log.debug("won't verify this resource from a flex non-primary node")
            return set([])

        self.get_info()
        from multiprocessing import Process, Queue
        self.checksums = {}
        queues = {}
        ps = []
        self.log.info("start checksum threads. please be patient.")
        for n in self.targets:
            dst = self.dsts[n]
            queues[n] = Queue()
            p = Process(target=self.checksum, args=(n, dst, queues[n]))
            p.start()
            ps.append(p)
        self.checksum(rcEnv.nodename, self.snap1)
        self.log.info("md5 %s: %s"%(rcEnv.nodename, self.checksums[rcEnv.nodename]))
        for p in ps:
            p.join()
        for n in self.targets:
            self.checksums[n] = queues[n].get()
            self.log.info("md5 %s: %s"%(n, self.checksums[n]))
        if len(self.checksums) < 2:
            self.log.error("not enough checksums collected")
            raise ex.excError
        err = False
        for n in self.targets:
            if self.checksums[rcEnv.nodename] != self.checksums[n]:
                self.log.error("src/dst checksums differ for %s/%s"%(rcEnv.nodename, n))
                err = True
        if not err:
            self.log.info("src/dst checksums verified")

    def start(self):
        pass

    def stop(self):
        pass

    def can_sync(self, target=None):
        try:
            ls = self.get_local_state()
            last = datetime.datetime.strptime(ls['date'], "%Y-%m-%d %H:%M:%S.%f")
        except IOError:
            return True
        return not self.skip_sync(last)

    def _status(self, verbose=False):
        try:
            ls = self.get_local_state()
            now = datetime.datetime.now()
            last = datetime.datetime.strptime(ls['date'], "%Y-%m-%d %H:%M:%S.%f")
            delay = datetime.timedelta(minutes=self.sync_max_delay)
        except ex.excError:
            self.status_log("failed to get status")
            return rcStatus.WARN
        except IOError:
            self.status_log("dds state file not found")
            return rcStatus.WARN
        except:
            import sys
            import traceback
            e = sys.exc_info()
            print(e[0], e[1], traceback.print_tb(e[2]))
            return rcStatus.WARN
        if last < now - delay:
            self.status_log("Last sync on %s older than %i minutes"%(last, self.sync_max_delay))
            return rcStatus.WARN
        return rcStatus.UP

    def __init__(self,
                 rid=None,
                 target=None,
                 src=None,
                 dsts={},
                 delta_store=None,
                 sender=None,
                 snap_size=0,
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 subset=None):
        resSync.Sync.__init__(self,
                              rid=rid,
                              type="sync.dds",
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)

        self.label = "dds of %s to %s"%(src, target)
        self.target = target
        self.src = src
        self.dsts = dsts
        self.snap_size = snap_size
        if delta_store is None:
            self.delta_store = rcEnv.pathvar
        else:
            self.delta_store = delta_store

    def __str__(self):
        return "%s target=%s src=%s" % (resSync.Sync.__str__(self),\
                self.target, self.src)

   0707010000bc61000081a400000000000000000000000154fc7bfb00001af8000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resSyncDocker.py #
# Copyright (c) 2014 Christophe Varoqui <christophe.varoqui@opensvc.com>'
# Copyright (c) 2014 Arnaud Veron <arnaud.veron@opensvc.com>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
from subprocess import *

import rcStatus
import rcDocker
from rcUtilities import justcall
from rcGlobalEnv import rcEnv
import resSync
import rcExceptions as ex

os.environ['LANG'] = 'C'

class SyncDocker(resSync.Sync, rcDocker.DockerLib):
    def __init__(self,
                 rid=None,
                 target=None,
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 subset=None):
        resSync.Sync.__init__(self,
                              rid=rid,
                              type="sync.docker",
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)

        self.label = "docker img sync to %s" % ", ".join(target)
        self.target = target
        self.images = []
        self.image_id_name = {}

    def get_docker_data_dir_svc_fs(self):
        l = []
        for r in self.svc.get_resources("fs"):
            l.append(r.mountPoint)
        l = sorted(l)
        v = self.docker_data_dir.split("/")
        while len(v) > 0:
            path = "/".join(v)
            if path in l:
                return path
            v = v[:-1]

    def get_images(self):
        for r in self.svc.get_resources("container.docker"):
            image = r.run_image
            image_id = self.get_run_image_id(image)
            self.image_id_name[image_id] = r.run_image
            self.images.append(image_id)

    def get_remote_images(self, node):
        ruser = self.svc.node.get_ruser(node)
        cmd = rcEnv.rsh.split()+['-l', ruser, node, '--', '/opt/opensvc/bin/svcmgr', '-s', self.svc.svcname, "docker", "images", "-a"]
        out, err, ret = justcall(cmd)
        images = []
        for line in out.split('\n'):
            l = line.split()
            if len(l) < 3:
                continue
            if l[0] == "REPOSITORY":
                continue
            images.append(l[2])
        return images

    def on_add(self):
        rcDocker.DockerLib.on_add(self)
        self.dstfs = self.get_docker_data_dir_svc_fs()
        self.dst = "docker images"

    def get_targets(self, action=None):
        self.targets = set()
        if 'nodes' in self.target and action in (None, 'syncnodes'):
            self.targets |= self.svc.nodes
        if 'drpnodes' in self.target and action in (None, 'syncdrp'):
            self.targets |= self.svc.drpnodes
        self.targets -= set([rcEnv.nodename])
        for node in self.targets.copy():
            if node in self.svc.nodes:
                target = 'nodes'
            elif node in self.svc.drpnodes:
                target = 'drpnodes'
            else:
                continue
            if not self.remote_node_type(node, target):
                self.targets -= set([node])
                continue
            try:
                mounted = self.remote_fs_mounted(node)
            except ex.excError:
                mounted = False
            if not mounted:
                self.targets -= set([node])

    def can_sync(self, target=None):
        return True

    def syncnodes(self):
        self._syncupdate('syncnodes')

    def syncdrp(self):
        self._syncupdate('syncdrp')

    def sanity_checks(self):
        if not self.svc.force:
            s = self.svc.group_status(excluded_groups=set(["sync", "hb", "app"]))
            if s['overall'].status != rcStatus.UP:
                raise ex.excError("won't sync this resource for a service not up")

        """ Refuse to sync from a flex non-primary node
        """
        if self.svc.clustertype in ["flex", "autoflex"] and \
           self.svc.flex_primary != rcEnv.nodename:
            raise ex.excError("won't sync this resource from a flex non-primary node")

    def _syncupdate(self, action):
        try:
            self.sanity_checks()
        except ex.excError:
            return
        self.get_targets(action)
        if len(self.targets) == 0:
            return
        self.get_images()
        for node in self.targets:
            remote_images = self.get_remote_images(node)
            missing = set(self.images) - set(remote_images)
            for image in missing:
                self.save_load(node, image)

    def save_load(self, node, image):
        ruser = self.svc.node.get_ruser(node)
        save_cmd = ["/opt/opensvc/bin/svcmgr", "-s", self.svc.svcname, "docker", "save", self.image_id_name[image]]
        load_cmd = rcEnv.rsh.split(' ')+['-l', ruser, node, '--', "/opt/opensvc/bin/svcmgr", "-s", self.svc.svcname, "docker", "load"]
        self.log.info(' '.join(save_cmd) + " | " + ' '.join(load_cmd))
        p1 = Popen(save_cmd, stdout=PIPE)
        p2 = Popen(load_cmd, stdin=p1.stdout, stdout=PIPE)
        out, err = p2.communicate()
        if p2.returncode != 0:
            if err is not None and len(err) > 0:
                self.log.error(err)
            raise ex.excError("sync update failed")
        if out is not None and len(out) > 0:
            self.log.info(out)

    def _status(self, verbose=False):
        self.get_targets()
        if len(self.targets) == 0:
            self.status_log("no target nodes")
            return rcStatus.NA
        self.get_images()
        total_missing = 0
        for node in self.targets:
            remote_images = self.get_remote_images(node)
            missing = set(self.images) - set(remote_images)
            n_missing = len(missing)
            total_missing += n_missing
            if n_missing > 0:
                if n_missing > 1:
                    plural = "s"
                else:
                    plural = ""
                self.status_log("target node %s miss image%s %s" % (node, plural, ','.join(missing)))
        if total_missing > 0:
            return rcStatus.WARN
        return rcStatus.UP


0707010000bc62000081a400000000000000000000000154fc7bfb00002d1f000000660000000300000000000000000000002700000000root/opt/opensvc/lib/resSyncEvasnap.py    #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import logging

from rcGlobalEnv import rcEnv
from rcUtilities import which
import rcExceptions as ex
import rcStatus
import time
import datetime
import xml.etree.ElementTree as ET
import subprocess
import resSync

class syncEvasnap(resSync.Sync):
    def wait_for_devs_ready(self):
        pass

    def can_sync(self, target=None):
        ts = None
 
        """ get oldest snap
        """
        for pair in self.pairs:
            info = self.lun_info(pair['dst'])
            if info is None:
                self.log.debug("snap %s missing"%pair['dst'])
                return True
            _ts = info['creationdatetime']
            if ts is None or _ts < ts:
                ts = _ts
        return not self.skip_sync(ts)

    def recreate(self):
        def snapname(info):
            return info['objectname'].split('\\')[-2]+'_'+self.snap_name

        try:
            self.prereq()
        except ex.excError as e:
            self.log.error(str(e))
            raise ex.excError

        status = self._status(skip_prereq=True)

        if not self.can_sync():
            return

        if not self.svc.force and status == rcStatus.UP:
            self.log.info("snapshots are already fresh. use --force to bypass")
            return

        cmd = []
        for pair in self.pairs:
            if pair['dst'] in self._lun_info:
                info = self._lun_info[pair['dst']]
                for mask in pair['mask']:
                    lunid = int(mask.split('\\')[-1])
                    hostpath = '\\'.join(mask.split('\\')[:-1])
                    if hostpath in info['mask'] and info['mask'][hostpath] == lunid:
                        cmd += ['delete lun "%s"'%mask]
                cmd += ['delete vdisk "%s" wait_for_completion'%info['objectname']]
        self.sssu(cmd, verbose=True)

        cmd = []
        for pair in self.pairs:
            info = self.lun_info(pair['src'])
            if 'allocation_policy' in pair:
                policy = str(pair['allocation_policy']).lower()
            else:
                policy = 'demand'
            if policy not in ['demand', 'fully']:
                policy = 'demand'
            if 'vraid' in pair and pair['vraid'] in ['vraid6', 'vraid5', 'vraid0', 'vraid1']:
                force_vraid = "redundancy=%s"%pair['vraid']
            else:
                force_vraid = ""
            cmd += ['add snapshot %s vdisk="%s" allocation_policy=%s world_wide_lun_name=%s %s'%(snapname(info), info['objectname'], policy, self.convert_wwid(pair['dst']), force_vraid)]
        self.sssu(cmd, verbose=True)

        cmd = []
        for pair in self.pairs:
            info = self.lun_info(pair['dst'])
            for mask in pair['mask']:
                lunid = mask.split('\\')[-1]
                hostpath = '\\'.join(mask.split('\\')[:-1])
                cmd += ['add lun %s host="%s" vdisk="%s"'%(lunid, hostpath, snapname(info))]
        self.sssu(cmd, verbose=True)

    def sssu(self, cmd=[], verbose=False, check=True):
        os.chdir(rcEnv.pathtmp)
        cmd = [self.sssubin,
               "select manager %s username=%s password=%s"%(self.manager, self.username, self.password),
               "select system %s"%self.eva_name] + cmd
        if verbose:
            import re
            from copy import copy
            _cmd = copy(cmd)
            _cmd[1] = re.sub(r'password=.*', 'password=xxxxx', _cmd[1])
            self.log.info(subprocess.list2cmdline(_cmd))
            ret, out, err = self.call(cmd)
            if 'Error:' in out > 0:
                self.log.error(out)
            else:
                self.log.info(out)
        else:
            ret, out, err = self.call(cmd)
        if check and "Error" in out:
            raise ex.excError("sssu command execution error")
        return ret, out, err

    def lun_info(self, wwid):
        if wwid in self._lun_info:
            return self._lun_info[wwid]

        if '-' not in wwid:
            _wwid = wwid
            wwid = self.convert_wwid(wwid)

        info = {
            'oxuid': 'unknown',
            'lunid': -1,
            'creationdatetime': datetime.datetime(year=datetime.MINYEAR, month=1, day=1),
            'mask': {}
        }
        try:
            ret, out, err = self.sssu(["find vdisk lunwwid="+wwid+" xml"])
        except:
            return None
        l = out.split('\n')
        for i, line in enumerate(l):
            if line == '<object>':
                l = l[i:]
                break
        e = ET.fromstring('\n'.join(l))

        for p in e.findall("presentations/presentation"):
            host = p.find("hostname").text
            lunid = p.find("lunnumber").text
            info['mask'][host] = int(lunid)
        
        e_oxuid = e.find("objectparenthexuid")
        if e_oxuid is not None:
            info['oxuid'] = e_oxuid.text.replace('-','')

        e_objectname = e.find("objectname")
        if e_objectname is not None:
            info['objectname'] = e_objectname.text

        e_creationdatetime = e.find("creationdatetime")
        if e_oxuid is not None:
            try:
                creationdatetime = datetime.datetime.strptime(e_creationdatetime.text, "%d-%b-%Y %H:%M:%S")
                info['creationdatetime'] = creationdatetime
            except:
                self.log.error("failed to parse snapshot creation datetime")
                pass

        self._lun_info[_wwid] = info

        return info

    def _status(self, verbose=False, skip_prereq=False):
        err = False
        errlog = []
        try:
            if not skip_prereq:
                self.prereq()
        except ex.excError as e:
            self.status_log(str(e))
            return rcStatus.WARN
        for pair in self.pairs:
            info_s = self.lun_info(pair['src'])
            info_d = self.lun_info(pair['dst'])
            if info_s is None:
                errlog.append("snapshot source %s does not exists"%pair['src'])
                err |= True
                continue
            if info_d is None:
                errlog.append("snapshot %s does not exists"%pair['dst'])
                err |= True
                continue
            if info_s['oxuid'].lower() != info_d['oxuid'].lower():
                errlog.append("snapshot %s exists but incorrect parent object uid: %s"%(pair['dst'], info_d['oxuid'].lower()))
                err |= True
            if info_d['creationdatetime'] < datetime.datetime.now() - datetime.timedelta(minutes=self.sync_max_delay):
                errlog.append("snapshot %s too old"%pair['dst'])
                err |= True
            for mask in pair['mask']:
                hostpath = '\\'.join(mask.split('\\')[:-1])
                hostname = mask.split('\\')[-2]
                dstlunid = mask.split('\\')[-1]
                if hostpath not in info_d['mask']:
                    errlog.append("snapshot %s exists but not presented to host %s"%(pair['dst'], hostname))
                    err |= True
                    continue
                try:
                    dstlunid = int(dstlunid)
                    if info_d['mask'][hostpath] != dstlunid:
                        errlog.append("snapshot %s exists but incorrect lunid for host %s"%(pair['dst'], hostname))
                        err |= True
                except ValueError:
                    pass
        if err:
            self.status_log('. '.join(errlog))
            return rcStatus.WARN
        return rcStatus.UP

    def syncresync(self):
        self.recreate()

    def refresh_svcstatus(self):
        self.svcstatus = self.svc.group_status(excluded_groups=set(["sync", 'hb']))

    def get_svcstatus(self):
        if len(self.svcstatus) == 0:
            self.refresh_svcstatus()

    def convert_wwid(self, wwid):
        s = ""
        for i, c in enumerate(wwid):
            s += c
            if (i+1) % 4 == 0: s += '-'
        return s.strip('-')

    def prereq(self):
        import ConfigParser
        if not which(self.sssubin):
            raise ex.excError("missing %s"%self.sssubin)
        if not os.path.exists(self.conf):
            raise ex.excError("missing %s"%self.conf)
        self.config = ConfigParser.RawConfigParser()
        self.config.read(self.conf)
        if not self.config.has_section(self.eva_name):
            raise ex.excError("no credentials for array %s in %s"%(self.eva_name, self.conf))
        if not self.config.has_option(self.eva_name, "manager"):
            raise ex.excError("no manager set for array %s in %s"%(self.eva_name, self.conf))
        if not self.config.has_option(self.eva_name, "username"):
            raise ex.excError("no username set for array %s in %s"%(self.eva_name, self.conf))
        if not self.config.has_option(self.eva_name, "password"):
            raise ex.excError("no password set for array %s in %s"%(self.eva_name, self.conf))
        self.manager = self.config.get(self.eva_name, "manager")
        self.username = self.config.get(self.eva_name, "username")
        self.password = self.config.get(self.eva_name, "password")
        for pair in self.pairs:
            if 'src' not in pair or 'dst' not in pair or 'mask' not in pair:
                raise ex.excError("missing parameter in pair %s"%str(pair))
        ret, out, err = self.sssu(check=False)
        if "Error opening https connection" in out:
            raise ex.excError("error login to %s"%self.manager)
        elif "Error" in out:
            raise ex.excError("eva %s is not managed by %s"%(self.eva_name, self.manager))
        

    def __init__(self,
                 rid=None,
                 pairs=[],
                 eva_name="",
                 snap_name="",
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 subset=None,
                 internal=False):
        resSync.Sync.__init__(self,
                              rid=rid, type="sync.evasnap",
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)

        self.label = "EVA snapshot %s"%(rid)
        self.eva_name = eva_name
        self.snap_name = snap_name
        self.pairs = pairs
        self.sssubin = os.path.join(rcEnv.pathbin, 'sssu')
        self.conf = os.path.join(rcEnv.pathetc, 'sssu.conf')
        self._lun_info = {}

    def __str__(self):
        return "%s eva_name=%s pairs=%s" % (resSync.Sync.__str__(self),\
                self.eva_name, str(self.pairs))

 0707010000bc63000081a400000000000000000000000154fc7bfb000031d3000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resSyncHp3par.py #
# Copyright (c) 2014 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os

from rcGlobalEnv import rcEnv
import rcExceptions as ex
import rcStatus
import time
import resSync
import datetime
import rcHp3par as rc

class syncHp3par(resSync.Sync):
    def __init__(self,
                 rid=None,
                 array=None,
                 method=None,
                 mode=None,
                 rcg_names={},
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 internal=False,
                 subset=None):
        resSync.Sync.__init__(self,
                              rid=rid,
                              type="sync.hp3par",
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)
        self.array = array
        self.rcg_names = rcg_names
        self.rcg = rcg_names[array]
        self.mode = mode
        self.method = method
        self.label = "hp3par %s %s"%(mode, self.rcg)
        try:
            arrays = rc.Hp3pars(objects=[self.array])
        except Exception as e:
            raise ex.excInitError(str(e))
        if len(arrays.arrays) == 1:
            self.array_obj = arrays.arrays[0]
        else:
            self.array_obj = None

    def __str__(self):
        return "%s array=%s method=%s mode=%s rcg=%s" % (
                resSync.Sync.__str__(self),
                self.array,
                self.method,
                self.mode,
                self.rcg)

    def on_add(self):
        self.array_obj.svcname = self.svc.svcname

    def _cmd(self, cmd, target=None, log=False):
        if target is None:
            array_name = self.array
            array_obj = self.array_obj
        else:
            array_name = target
            if not hasattr(self, 'remote_array_obj'):
                try:
                    self.remote_array_obj = rc.Hp3pars(objects=[target]).arrays[0]
                except Exception as e:
                    raise ex.excError(str(e))
            array_obj = self.remote_array_obj

        if array_obj is None:
            raise ex.excError("array %s is not accessible" % array_name)
        if log:
            if target is not None:
                suffix = " (on " + target + ")"
            else:
                suffix = ""
            self.log.info(cmd+suffix)
        out, err = array_obj.rcmd(cmd, log=self.log)
        if not log:
            return out, err
        if len(out) > 0:
            self.log.info(out)
        if len(err) > 0:
            self.log.error(err)
            raise ex.excError()
        return out, err

    def can_sync(self, target=None, s=None):
        data = self.showrcopy()
        last = data['vv'][0]['LastSyncTime']
        if self.skip_sync(datetime.datetime.now()-last):
            return False
        return True

    def syncresync(self):
        self.syncupdate()

    def syncswap(self):
        data = self.showrcopy()
        if data['rcg']['Role'] == 'Primary':
            self.log.error("rcopy group %s role is Primary. refuse to swap")
            raise ex.excError()
        self.stoprcopygroup()
        self.setrcopygroup_reverse()
        self.startrcopygroup()

    def syncupdate(self):
        self.syncrcopygroup()

    def syncrevert(self):
        self.setrcopygroup_revert()

    def syncresume(self):
        self.startrcopygroup()

    def syncquiesce(self):
        self.stoprcopygroup()

    def syncbreak(self):
        self.stoprcopygroup()

    def start(self):
        data = self.showrcopy()
        target = data['rcg']['Target']
        if self.is_splitted(target):
            self.log.info("we are split from %s array" % target)
            self.start_splitted()
        else:
            self.log.info("we are joined with %s array" % target)
            self.start_joined()

    def start_joined(self):
        data = self.showrcopy()
        if data['rcg']['Role'] == 'Primary':
            self.log.info("rcopy group %s role is already Primary. skip" % self.rcg)
            return
        self.stoprcopygroup()
        self.setrcopygroup_reverse()

    def start_splitted(self):
        self.setrcopygroup_failover()

    def stop(self):
        pass

    def setrcopygroup_revert(self):
        data = self.showrcopy()
        if data['rcg']['Role'] != 'Primary-Rev':
           self.log.error("rcopy group %s role is not Primary-Rev. refuse to setrcopygroup revert" % self.rcg)
           return
        self._cmd("setrcopygroup reverse -f -waittask -stopgroups -local -current %s" % self.rcg, log=True)

    def setrcopygroup_failover(self):
        data = self.showrcopy()
        if data['rcg']['Role'] == 'Primary-Rev':
           self.log.info("rcopy group %s role is already Primary-Rev. skip setrcopygroup failover" % self.rcg)
           return
        self._cmd("setrcopygroup failover -f -waittask %s" % self.rcg, log=True)

    def setrcopygroup_reverse(self):
        data = self.showrcopy()
        if data['rcg']['Role'] == 'Primary':
            self.log.info("rcopy group %s role is already Primary. skip setrcopygroup reverse" % self.rcg)
            return
        self._cmd("setrcopygroup reverse -f -waittask %s" % self.rcg, log=True)

    def syncrcopygroup(self):
        data = self.showrcopy()
        if data['rcg']['Role'] != 'Primary':
            self.log.error("rcopy group %s role is not Primary. refuse to sync" % self.rcg)
            raise ex.excError()
        self._cmd("syncrcopy -w %s" % self.rcg, log=True)

    def startrcopygroup(self):
        data = self.showrcopy()
        if data['rcg']['Status'] == "Started":
            self.log.info("rcopy group %s is already started. skip startrcopygroup" % self.rcg)
            return
        if data['rcg']['Role'] != 'Primary':
            self.log.error("rcopy group %s role is not Primary. refuse to start rcopy" % self.rcg)
            raise ex.excError()
        self._cmd("startrcopygroup %s" % self.rcg, log=True)

    def stoprcopygroup(self):
        data = self.showrcopy()
        if data['rcg']['Status'] == "Stopped":
            self.log.info("rcopy group %s is already stopped. skip stoprcopygroup" % self.rcg)
            return
        if data['rcg']['Role'] == "Primary":
            self._cmd("stoprcopygroup -f %s" % self.rcg, log=True)
        else:
            target = data['rcg']['Target']
            self._cmd("stoprcopygroup -f %s" % self.rcg_names[target], target=target, log=True)

    def is_splitted(self, target):
        data = self.showrcopy_links()
        for h in data:
            if h['Target'] != target:
                continue
            if h['Status'] == "Up":
                return False
        return True

    def showrcopy_links(self):
        """
        Target,Node,Address,Status,Options
        baie-pra.cgr.fr,0:2:4,20240002AC00992B,Down,
        baie-pra.cgr.fr,1:2:3,21230002AC00992B,Down,
        receive,0:2:4,20240002AC00992B,Up,
        receive,1:2:3,21230002AC00992B,Up,
        """
        out, err = self._cmd("showrcopy links")
        cols = ["Target", "Node", "Address", "Status", "Options"]
        lines = out.split('\n')
        data = []
        for line in lines:
            v = line.strip().split(",")
            if len(v) != len(cols):
                continue
            h = {}
            for a, b in zip(cols, v):
                h[a] = b
            data.append(h)
        return data

    def showrcopy(self, refresh=False):
        """
	Remote Copy System Information
	Status: Started, Normal

	Group Information

	Name        ,Target    ,Status  ,Role      ,Mode    ,Options
	RCG.SVCTEST1,baie-pra.cgr.fr,Started,Primary,Periodic,"Last-Sync 2014-03-05 10:19:42 CET , Period 5m, auto_recover,over_per_alert"
	 ,LocalVV     ,ID  ,RemoteVV    ,ID  ,SyncStatus   ,LastSyncTime
	 ,LXC.SVCTEST1.DATA01,2706,LXC.SVCTEST1.DATA01,2718,Synced,2014-03-05 10:19:42 CET
	 ,LXC.SVCTEST1.DATA02,2707,LXC.SVCTEST1.DATA02,2719,Synced,2014-03-05 10:19:42 CET

        """
        if not refresh and hasattr(self, "showrcopy_cache"):
            return self.showrcopy_cache

        out, err = self._cmd("showrcopy groups %s" % self.rcg)
        cols_rcg = ["Name", "Target", "Status", "Role", "Mode"]
        cols_vv = ["LocalVV", "ID", "RemoteVV", "ID", "SyncStatus", "LastSyncTime"]
        lines = out.split('\n')

        if "does not exist" in err:
            raise ex.excError("rcg does not exist")

        if len(out) == 0:
            raise ex.excError("unable to fetch rcg status")

        # RCG status
        rcg_s = lines[0]
        options_start = rcg_s.index('"')
        rcg_options = rcg_s[options_start+1:-1].split(",")
        rcg_options = map(lambda x: x.strip(), rcg_options)
        rcg_v = rcg_s[:options_start].split(",")
        rcg_data = {}
        for a, b in zip(cols_rcg, rcg_v):
            rcg_data[a] = b
        rcg_data["Options"] = rcg_options

        # VV status
        vv_l = []
        for line in lines[1:]:
            v = line.strip().strip(",").split(",")
            if len(v) != len(cols_vv):
                continue
            vv_data = {}
            for a, b in zip(cols_vv, v):
                vv_data[a] = b
            vv_data['LastSyncTime'] = self.lastsync_s_to_datetime(vv_data['LastSyncTime'])
            vv_l.append(vv_data)
        data = {'rcg': rcg_data, 'vv': vv_l}   
        self.showrcopy_cache = data
        return self.showrcopy_cache

    def lastsync_s_to_datetime(self, s):
        try:
            d = datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%S %Z")
        except ValueError:
            # workaround hp-ux python 2.6
            s = s.replace("CET", "MET")
            d = datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%S %Z")
        return d

    def _status(self, verbose=False):
        if self.array_obj is None:
            self.status_log("array %s is not accessible" % self.array)
            return rcStatus.WARN

        try:
            data = self.showrcopy(refresh=True)
        except ex.excError as e:
            self.status_log(str(e))
            return rcStatus.UNDEF

        elapsed = datetime.datetime.now() - datetime.timedelta(minutes=self.sync_max_delay)
        r = None
        if data['rcg']['Status'] != "Started":
            self.status_log("rcopy group status is not Started (%s)"%data['rcg']['Status'])
            r = rcStatus.WARN
        if self.mode == "async" and data['rcg']['Mode'] != "Periodic":
            self.status_log("rcopy group mode is not Periodic (%s)"%data['rcg']['Mode'])
            r = rcStatus.WARN
        if self.mode == "sync" and data['rcg']['Mode'] != "Sync":
            self.status_log("rcopy group mode is not Sync (%s)"%data['rcg']['Mode'])
            r = rcStatus.WARN
        if self.mode == "async":
            l = [o for o in data['rcg']['Options'] if o.startswith('Period ')]
            if len(l) == 0:
                self.status_log("rcopy group period option is not set")
                r = rcStatus.WARN
        if 'auto_recover' not in data['rcg']['Options']:
            self.status_log("rcopy group auto_recover option is not set")
            r = rcStatus.WARN
        for vv in data['vv']:
            if vv['SyncStatus'] != 'Synced':
                self.status_log("vv %s SyncStatus is not Synced (%s)"%(vv['LocalVV'], vv['SyncStatus']))
                r = rcStatus.WARN
            if vv['LastSyncTime'] < elapsed:
                self.status_log("vv %s last sync too old (%s)"%(vv['LocalVV'], vv['LastSyncTime'].strftime("%Y-%m-%d %H:%M")))
                r = rcStatus.WARN

        if r is not None:
            return r
        
        return rcStatus.UP

if __name__ == "__main__":
    o = syncHp3par(rid="sync#1", mode="async", array="baie-pra", rcg="RCG.SVCTEST1")
    print(o)
 0707010000bc64000081a400000000000000000000000154fc7bfb000025ac000000660000000300000000000000000000002900000000root/opt/opensvc/lib/resSyncIbmdsSnap.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import logging

from rcGlobalEnv import rcEnv
from rcUtilities import which
import rcExceptions as ex
import rcStatus
import time
import datetime
import resSync
import rcIbmDs

class syncIbmdsSnap(resSync.Sync):
    def resyncflash(self):
        if self.array is None:
            self.array = rcIbmDs.IbmDss().get(self.arrayname)
        data = self.lsflash()
        ese_pairs = []
        other_pairs = []
        
        for d in data:
            if d['isTgtSE'] == 'ESE':
                ese_pairs.append(d['ID'])
            else:
                other_pairs.append(d['ID'])

        present_pairs = set(map(lambda x: x['ID'], data))
        missing_pairs = list(set(self.pairs) - present_pairs)
        if len(missing_pairs) > 0:
            missing_pairs.sort()
            raise ex.excError("refuse to resync as %s pairs are not currently configured"%', '.join(missing_pairs))

        self._resyncflash(ese_pairs, '-tgtse')
        self._resyncflash(other_pairs)

    def _resyncflash(self, pairs, options=None):
        if len(pairs) == 0:
            return
        if self.recording:
            self._resyncflash_recording(pairs, options=options)
        else:
            self._resyncflash_norecording(pairs, options=options)

    def _resyncflash_norecording(self, pairs, options=None):
        s = 'rmflash -dev %s -quiet' % self.arrayname
        l = [s]
        l.append(' '.join(pairs))
        cmd = ' '.join(l)
        out, err = self.array.dscli(cmd, log=self.log)
        if len(err) > 0:
            raise ex.excError(err)
        s = 'mkflash -dev %s -persist' % self.arrayname
        if self.bgcopy:
            s += ' -cp'
        else:
            s += ' -nocp'
        l = [s]
        if options is not None:
            l.append(options)
        l.append(' '.join(pairs))
        cmd = ' '.join(l)
        out, err = self.array.dscli(cmd, log=self.log)
        if len(err) > 0:
            raise ex.excError(err)

    def _resyncflash_recording(self, pairs, options=None):
        s = 'resyncflash -dev %s -persist -record' % self.arrayname
        if self.bgcopy:
            s += ' -cp'
        else:
            s += ' -nocp'
        l = [s]
        if options is not None:
            l.append(options)
        l.append(' '.join(pairs))
        cmd = ' '.join(l)
        out, err = self.array.dscli(cmd, log=self.log)
        if len(err) > 0:
            raise ex.excError(err)

    def can_sync(self, target=None):
        self.get_last()
        if skip_sync(self.last):
            return False
        return True

    def get_last(self, data=None):
        if data is None:
            data = self.lsflash()
        if len(data) == 0:
            return
        lastsync = datetime.datetime.now()
        for _data in data:
            _lastsync = _data['DateSynced']
            try:
                _lastsync = datetime.datetime.strptime(_lastsync, "%a %b %d %H:%M:%S %Z %Y")
            except ValueError:
                # workaround hp-ux python 2.6
                _lastsync = _lastsync.replace("CET", "MET")
                _lastsync = datetime.datetime.strptime(_lastsync, "%a %b %d %H:%M:%S %Z %Y")

            if _lastsync < lastsync:
                lastsync = _lastsync
        self.last = lastsync

    def _status(self, verbose=False):
        try:
            data = self.lsflash()
            self.get_last(data)
        except ex.excError as e:
            self.status_log(str(e))
            return rcStatus.WARN
        r = rcStatus.UP

        record_disabled = []
        persist_disabled = []
        record_enabled = []
        state_invalid = []

        for _data in data:
            if _data['Recording'] == "Disabled":
                record_disabled.append(_data['ID'])
            elif _data['Recording'] == "Enabled":
                record_enabled.append(_data['ID'])
            if _data['State'] != "Valid":
                state_invalid.append(_data['ID'])
            if _data['Persistent'] == "Disabled":
                persist_disabled.append(_data['ID'])

        if self.recording and len(record_disabled) > 0:
            self.status_log("Recording disabled on %s"%','.join(record_disabled))
            r = rcStatus.WARN
        elif not self.recording and len(record_enabled) > 0:
            self.status_log("Recording enabled on %s"%','.join(record_enabled))
            r = rcStatus.WARN
        if len(state_invalid) > 0:
            self.status_log("State not valid on %s"%','.join(state_invalid))
            r = rcStatus.WARN
        if len(persist_disabled) > 0:
            self.status_log("Persistent disabled on %s"%','.join(persist_disabled))
            r = rcStatus.WARN

        pairs = []
        for d in data:
            if 'ID' not in d:
                continue
            pairs.append(d['ID'])
        missing = set(self.pairs) - set(pairs)
        missing = sorted(list(missing))
        if len(missing) > 0:
            self.status_log("Missing flashcopy on %s"%','.join(missing))
            r = rcStatus.WARN

        if self.last is None:
            return rcStatus.WARN
        elif self.last < datetime.datetime.now() - datetime.timedelta(minutes=self.sync_max_delay):
            self.status_log("Last sync on %s older than %d minutes"%(self.last, self.sync_max_delay))
            return rcStatus.WARN
        elif r == rcStatus.WARN:
            return rcStatus.WARN
        self.status_log("Last sync on %s"%self.last)
        return rcStatus.UP

    def syncbreak(self):
        pass

    def syncresync(self):
        self.resyncflash()

    def start(self):
        self.syncbreak()

    def __init__(self,
                 rid=None,
                 pairs=[],
                 array=None,
                 bgcopy=True,
                 recording=True,
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 subset=None,
                 internal=False):
        resSync.Sync.__init__(self,
                              rid=rid,
                              type="sync.ibmdssnap",
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)

        self.label = "flash copy %s"%','.join(pairs)
        self.pairs = pairs
        self.arrayname = array
        self.recording = recording
        self.bgcopy = bgcopy
        self.array = None
        self.last = None
        self.params = "setenv -banner off -header on -format delim\n"

    def lsflash(self):
        if self.array is None:
            self.array = rcIbmDs.IbmDss().get(self.arrayname)
        out, err = self.array.dscli(self.params+'lsflash -l -dev %s ' % self.arrayname + ' '.join(self.pairs))
        if 'No Flash Copy found' in out:
            return []
        data = self.parseblock(0, out)
        return data

    def getblock(self, n, s):
        lines = s.replace('dscli> ', '').split('\n')
        begin = None
        end = None
        met = 0
        for i, line in enumerate(lines):
             if line.startswith("==="):
                 met += 1
                 if met < n:
                     continue
                 if begin is None:
                     begin = i-1
                 else:
                     end = i-1
                     break
        if end is None:
           end = i
        return lines[begin:end]

    def parseblock(self, n, out):
        data = []
        lines = self.getblock(n, out)
        if len(lines) < 3:
            return
        headers = lines[0].split(',')
        headers_multipliers = []
        for i, h in enumerate(headers):
            if '^' not in h:
                headers_multipliers.append(None)
                continue
            x = h[h.index('^')+1:h.index('B)')]
            x = int(x)
            headers_multipliers.append((2**x)/1024/1024)
            stripped_header = headers[i][:headers[i].index(' (')]
            while stripped_header in headers:
                stripped_header += "_"
            headers[i] = stripped_header
        for line in lines[2:]:
            d = {}
            l = line.split(',')
            for i, key in enumerate(headers):
                key = key.strip()
                if headers_multipliers[i] is not None:
                    try:
                        d[key] = int(float(l[i]) * headers_multipliers[i])
                    except:
                        d[key] = l[i]
                else:
                    d[key] = l[i]
            data.append(d)
        return data

    def __str__(self):
        return "%s pairs=%s" % (resSync.Sync.__str__(self), ','.join(self.pairs))

0707010000bc65000081a400000000000000000000000154fc7bfb00001a34000000660000000300000000000000000000002a00000000root/opt/opensvc/lib/resSyncNecIsmSnap.py #
# Copyright (c) 2014 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import logging

from rcGlobalEnv import rcEnv
import rcExceptions as ex
import rcStatus
import time
import datetime
import resSync
import rcNecIsm

class syncNecIsmSnap(resSync.Sync):
    def __init__(self,
                 rid=None,
                 array_name=None,
                 devs=[],
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 subset=None,
                 internal=False):
        resSync.Sync.__init__(self,
                              rid=rid, type="sync.necismsnap",
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)

        self.label = "NecIsm snapshot %s"%(rid)
        self.devs = devs
        self.array = rcNecIsm.NecIsm(array_name)
        self.parse_devs(devs)

    def on_add(self):
        self.array.log = self.log

    def parse_devs(self, devs):
        self.svld = []
        self.sv = {}
        self.ld = {}
        devs = devs.replace(' ', ',')
        for e in devs.split(','):
            try:
                sv, ld = e.split(':')
            except:
                raise ex.excInitError("%s: malformed devs" % self.rid)
            if sv in self.sv:
                raise ex.excInitError("%s: duplicate sv %s in devs" % (self.rid, sv))
            if ld in self.ld:
                raise ex.excInitError("%s: duplicate ld %s in devs" % (self.rid, ld))
            self.sv[sv] = sv
            self.ld[ld] = ld
            self.svld.append((sv, ld))

    def wait_for_devs_ready(self):
        pass

    def get_sv_ts(self, sv):
        try:
            data = self.array.sc_query_ld(sv)
        except ex.excError:
            self.status_log("unable to query SV:%s" % sv)
            return
        svinfo = data['sv'][0]
        try:
            begin = svinfo.index('[')+1
        except:
            self.status_log("unable to get timestamp for SV:%s" % sv)
            return
        end = svinfo.index(']')
        ts_s = svinfo[begin:end]
        ts = datetime.datetime.strptime(ts_s, "%Y/%m/%d %H:%M:%S")
        return ts

    def can_sync(self, target=None):
        ts = self.get_oldest_ts()
        return not self.skip_sync(ts)

    def get_oldest_ts(self):
        ts = None
        for sv, ld in self.svld:
            _ts = self.get_sv_ts(sv)
            if _ts is None:
                return
            if ts is None or _ts < ts:
                ts = _ts
        return ts

    def _status(self, verbose=False, skip_prereq=False):
        ret = 0
        ret += self._status_ts()
        ret += self._status_link()
        if ret > 0:
            return rcStatus.WARN
        return rcStatus.UP

    def _status_ts(self):
        ts = self.get_oldest_ts()
        if ts is None:
            return 1
        now = datetime.datetime.now()
        limit = now - datetime.timedelta(minutes=self.sync_max_delay)
        if ts < limit:
            self.status_log("snap too old (%s)" % ts.strftime("%Y-%m-%d %H:%M"))
            return rcStatus.WARN
        self.status_log("snap at %s" % ts.strftime("%Y-%m-%d %H:%M"))
        return 0

    def _status_link(self):
        r = 0
        for sv, ld in self.svld:
            r += self.__status_link(sv, ld)
        return r

    def __status_link(self, sv, ld):
        try:
            li = self.array.sc_linkinfo_ld(sv)
        except ex.excError:
            self.status_log("unable to get SV:%s linkinfo" % sv)
            return 1
        l = [ dst for dst in li['dst'] if ld in dst.split() and 'link' in dst.split()]
        if len(l) != 1:
            self.status_log("LD:%s is not linked to SV:%s" % (ld, sv))
            return 1
        return 0

    def syncresync(self):
        self.unlink()
        self.create()
        self.link()

    def unlink(self):
        for sv, ld in self.svld:
            if self.__status_link(sv, ld) == 1:
                self.log.info("SV:%s is already unlinked from LD:%s" % (sv, ld))
            else:
                self.array.sc_unlink_ld(ld)

    def get_changing_snap(self, src):
        bv_detail = self.array.sc_query_bv_detail(src)
        l = []
        for sv, data in bv_detail['sv'].items():
            if data['Snap State'].endswith('ing'):
                l.append(':'.join((sv, data['Snap State'])))
        return l

    def wait_for_changing_snap(self, src):
        retry = 40
        while retry > 0:
            changing_snap = self.get_changing_snap(src)
            if len(changing_snap) == 0:
                return
            self.log.info("SV are changing states : %s. Wait 30sec before retry" % ', '.join(changing_snap))
            retry -= 1
            try:
                time.sleep(30)
            except:
                return

    def create(self):
        for sv, ld in self.svld:
            try:
                src = self.array.sc_query_ld(sv)['LD Name']
            except:
                raise ex.excError("can not determine source LD for SV:%s" % sv)
            self.wait_for_changing_snap(src)
            self.array.sc_create_ld(src, sv)

    def link(self):
        for sv, ld in self.svld:
            if self.__status_link(sv, ld) == 0:
                self.log.info("SV:%s is already linked to LD:%s" % (sv, ld))
            else:
                self.array.sc_link_ld(sv, ld)

    def refresh_svcstatus(self):
        self.svcstatus = self.svc.group_status(excluded_groups=set(["sync", 'hb']))

    def get_svcstatus(self):
        if len(self.svcstatus) == 0:
            self.refresh_svcstatus()

    def __str__(self):
        return "%s array_name=%s devs=%s" % (resSync.Sync.__str__(self),\
                self.array_name, str(self.devs))

0707010000bc66000081a400000000000000000000000154fc7bfb00002c4e000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resSyncNetapp.py #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import logging

from rcGlobalEnv import rcEnv
from rcUtilities import which, justcall
import rcExceptions as ex
import rcStatus
import time
import resSync
import datetime

class syncNetapp(resSync.Sync):
    def master(self):
        s = self.local_snapmirror_status()
        return s['master']

    def slave(self):
        s = self.local_snapmirror_status()
        return s['slave']

    def local(self):
        if rcEnv.nodename in self.filers:
            return self.filers[rcEnv.nodename]
        return None

    def _cmd(self, cmd, target, info=False):
        if target == "local":
            filer = self.local()
        elif target == "master":
            filer = self.master()
        elif target == "slave":
            filer = self.slave()
        elif target in self.filers.values():
            filer = target
        else:
            raise ex.excError("unable to find the %s filer"%target)

        _cmd = rcEnv.rsh.split() + [self.user+'@'+filer] + cmd

        if info:
            self.log.info(' '.join(_cmd))

        out, err, ret = justcall(rcEnv.rsh.split() + [self.user+'@'+filer] + cmd)

        if info:
            if len(out) > 0:
                self.log.info(out)
            if len(err) > 0:
                self.log.error(err)

        return ret, out, err

    def cmd_master(self, cmd, info=False):
        return self._cmd(cmd, "master", info=info)

    def cmd_slave(self, cmd, info=False):
        return self._cmd(cmd, "slave", info=info)

    def cmd_local(self, cmd, info=False):
        return self._cmd(cmd, "local", info=info)

    def lag_to_ts(self, lag):
        now = datetime.datetime.now()
        l = lag.split(":")
        if len(l) != 3:
            raise ex.excError("unexpected lag format")
        delta = datetime.timedelta(hours=int(l[0]),
                                   minutes=int(l[1]),
                                   seconds=int(l[2]))
        return now - delta

    def can_sync(self, target=None, s=None):
        if s is None:
            s = self.snapmirror_status(self.slave())
        ts = self.lag_to_ts(s['lag'])
        if self.skip_sync(ts):
            return False
        return True

    def lagged(self, lag, max=None):
        if max is None:
            max = self.sync_max_delay
        l = lag.split(":")
        if len(l) != 3:
            raise ex.excError("unexpected lag format")
        if int(l[0]) * 60 + int(l[1]) > max:
            return True
        return False

    def syncresync(self):
        (ret, buff, err) = self.cmd_slave(['snapmirror', 'resync', '-f', self.slave()+':'+self.path_short], info=True)
        if ret != 0:
            raise ex.excError

    def syncswap(self):
        master = self.master()
        slave = self.slave()
        s = self.snapmirror_status(self.local())
        if s['state'] != "Broken-off":
            self.log.error("can not swap: snapmirror is not in state Broken-off")
            raise ex.excError
        src = slave+':'+self.path_short
        dst = master+':'+self.path_short

        (ret, buff, err) = self._cmd(['snapmirror', 'resync', '-f', '-S', src, dst], master, info=True)
        if ret != 0:
            raise ex.excError(err)
        (ret, buff, err) = self._cmd(['snapmirror', 'release', self.path_short, src], master, info=True)
        if ret != 0:
            raise ex.excError(err)
        (ret, buff, err) = self._cmd(['snapmirror', 'status', '-l', dst], slave, info=False)
        if ret != 0:
            raise ex.excError(err)
        snap = ""
        state = ""
        for line in buff.split('\n'):
            l = line.split()
            if len(l) < 2:
                continue
            if l[0] == "State:":
                state = l[1]
            if state != "Broken-off":
                continue
            if l[0] == "Base" and l[1] == "Snapshot:":
                snap = l[-1]
                break
        if len(snap) == 0:
            self.log.error("can not determine base snapshot name to remove on %s"%slave)
            raise ex.excError
        import time
        time.sleep(5)
        (ret, buff, err) = self._cmd(['snap', 'delete', self.path_short, snap], slave, info=True)
        if ret != 0:
            raise ex.excError(err)

    def syncupdate(self):
        s = self.snapmirror_status(self.slave())
        if not self.can_sync(s=s):
            return
        if s['state'] == "Quiesced":
            self.log.error("update not applicable: quiesced")
            return
        if s['state'] == "Snapmirrored" and s['status'] == "Transferring":
            self.log.info("update not applicable: transfer in progress")
            return
        if s['state'] != "Snapmirrored" or s['status'] != "Idle":
            self.log.error("update not applicable: not in snapmirror idle status")
            return
        (ret, buff, err) = self.cmd_slave(['snapmirror', 'update', self.slave()+':'+self.path_short], info=True)
        if ret != 0:
            raise ex.excError

    def syncresume(self):
        s = self.snapmirror_status(self.slave())
        if s['state'] != "Quiesced":
            self.log.info("resume not applicable: not quiesced")
            return
        (ret, buff, err) = self.cmd_slave(['snapmirror', 'resume', self.slave()+':'+self.path_short], info=True)
        if ret != 0:
            raise ex.excError

    def syncquiesce(self):
        s = self.snapmirror_status(self.slave())
        if s['state'] == "Quiesced":
            self.log.info("already quiesced")
            return
        elif s['state'] != "Snapmirrored":
            self.log.error("Can not quiesce: volume not in Snapmirrored state")
            raise ex.excError
        if s['status'] == "Pending":
            self.log.error("Can not quiesce: volume in snapmirror Pending status")
            raise ex.excError
        (ret, buff, err) = self.cmd_slave(['snapmirror', 'quiesce', self.slave()+':'+self.path_short], info=True)
        if ret != 0:
            raise ex.excError
        self.wait_quiesce()

    def syncbreak(self):
        (ret, buff, err) = self.cmd_slave(['snapmirror', 'break', self.slave()+':'+self.path_short], info=True)
        if ret != 0:
            raise ex.excError
        self.wait_break()

    def wait_quiesce(self):
        timeout = 60
        self.log.info("start waiting quiesce to finish (max %s seconds)"%(timeout*5))
        for i in range(timeout):
            s = self.snapmirror_status(self.slave())
            if s['state'] == "Quiesced" and s['status'] == "Idle":
                return
            time.sleep(5)
        self.log.error("timed out waiting for quiesce to finish")
        raise ex.excError

    def wait_break(self):
        timeout = 20
        for i in range(timeout):
            s = self.snapmirror_status(self.slave())
            if s['state'] == "Broken-off" and s['status'] == "Idle":
                return
            time.sleep(5)
        self.log.error("timed out waiting for break to finish")
        raise ex.excError

    def snapmirror_status(self, filer):
        (ret, buff, err) = self._cmd(['snapmirror', 'status'], filer, info=False)
        if ret != 0:
            raise ex.excError("can get snapmirror status from %s: %s"%(filer, err))
        key = ':'.join([filer, self.path_short])
        list = []
        for line in buff.split('\n'):
            l = line.split()
            if len(l) < 5:
                continue
            if l[2] == "Uninitialized":
                continue
            if l[0] == key or l[1] == key:
                list.append(l)
        if len(list) == 0:
            raise ex.excError("%s not found in snapmirror status"%self.path_short)
        elif len(list) == 1:
            l = list[0]
            master = l[0].split(':')[0]
            slave = l[1].split(':')[0]
            return dict(master=master, slave=slave, state=l[2], lag=l[3], status=l[4])
        else:
            raise ex.excError("%s is in an unsupported state. Please repair manually."%filer)

    def local_snapmirror_status(self):
        return self.snapmirror_status(self.local())

    def start(self):
        if self.local() == self.master():
            self.log.info("%s is already replication master"%self.local())
            return
        s = self.snapmirror_status(self.slave())
        if s['state'] != "Broken-off":
            try:
                self.syncquiesce()
            except:
                if self.svc.force:
                    self.log.warning("force mode is on. bypass failed quiesce.")
                    pass
                else:
                    self.log.error("set force mode to bypass")
                    raise ex.excError
            self.syncbreak()
        if rcEnv.host_mode == "PRD":
            self.syncswap()

    def stop(self):
        pass

    def _status(self, verbose=False):
        try:
            s = self.snapmirror_status(self.slave())
        except ex.excError as e:
            self.status_log(str(e))
            return rcStatus.UNDEF
        if s['state'] == "Snapmirrored":
            if "Transferring" in s['status']:
                self.log.debug("snapmirror transfer in progress")
                return rcStatus.WARN
            elif self.lagged(s['lag']):
                self.log.debug("snapmirror lag beyond sync_max_delay")
                return rcStatus.WARN
            else:
                return rcStatus.UP
        return rcStatus.DOWN

    def __init__(self,
                 rid=None,
                 filers={},
                 path=None,
                 user=None,
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 internal=False,
                 subset=None):
        resSync.Sync.__init__(self,
                              rid=rid,
                              type="sync.netapp",
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)
        self.label = "netapp %s on %s"%(path, ', '.join(filers.values()))
        self.filers = filers
        self.path = path
        self.user = user
        self.path_short = self.path.replace('/vol/','')

    def __str__(self):
        return "%s filers=%s user=%s path=%s" % (resSync.Sync.__str__(self),\
                self.filers, self.user, self.path)

  0707010000bc67000081a400000000000000000000000154fc7bfb00002528000000660000000300000000000000000000002700000000root/opt/opensvc/lib/resSyncNexenta.py    #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import logging

from rcGlobalEnv import rcEnv
import rcExceptions as ex
import rcStatus
import resSync
import datetime
from rcNexenta import Nexenta 

class syncNexenta(resSync.Sync):
    def can_sync(self, target=None):
        try:
            self.get_endpoints()
        except ex.excError as e:
            self.log.error(str(e))
            raise ex.excError

        if self.ts is None:
            self.get_props()
        if self.skip_sync(self.ts):
            return False
        return True

    def syncswap(self):
        # only available from CLI ?
        pass

    def syncupdate(self):
        try:
            self.get_endpoints()
        except ex.excError as e:
            self.log.error(str(e))
            raise ex.excError

        if not self.can_sync() and not self.svc.force:
            return
        s = self.master.autosync_get_state(self.autosync)
        if s == "disabled":
            self.log.error("update not applicable: disabled")
            return
        if s == "running":
            self.log.info("update not applicable: transfer in progress")
            return
        if s != "online":
            self.log.error("update not applicable: %s state"%s)
            return
        self.master.autosync_execute(self.autosync)
        self.log.info("autosync runner execution submitted")

    def bind(self):
        b = self.local.ssh_list_bindings()
        found = False
        for k in b:
            user, hostport = k.split('@')
            if hostport == self.remote.head:
                found = True
                break
        if found:
            self.log.info("%s head already bound"%self.remote.head)
        else:
            self.local.ssh_bind(self.remote.username, self.remote.head, self.remote.password)
            self.log.info("%s head bound"%self.remote.head)

    def unbind(self):
        b = self.local.ssh_list_bindings()
        done = False
        for k in b:
            user, hostport = k.split('@')
            if hostport != self.remote.head:
                continue
            self.local.ssh_unbind(user, hostport, '1')
            self.log.info("%s head unbound"%hostport)
            done = True
        if not done:
            self.log.info("%s head already unbound"%self.remote.head)

    def syncresync(self):
        try:
            self.get_endpoints()
            self.bind()
            self.master.autosync_enable(self.autosync)
            self.log.info("autosync enable submitted")
        except ex.excError as e:
            self.log.error(str(e))
            raise ex.excError

    def syncbreak(self):
        try:
            self.get_endpoints()
            self.unbind()
            self.master.autosync_disable(self.autosync)
            self.log.info("autosync disable submitted")
            self.wait_break()
        except ex.excError as e:
            self.log.error(str(e))
            raise ex.excError

    def wait_break(self):
        import time
        timeout = 5
        for i in range(timeout, 0, -1):
            s = self.master.autosync_get_state(self.autosync)
            if s == "disabled":
                return
            if i > 1:
                time.sleep(2)
        self.log.error("timed out waiting for disable to finish")
        raise ex.excError

    def start(self):
        try:
            self.get_endpoints()
            self.local.set_can_mount(self.path)
            self.log.info("set 'canmount = on' on %s"%self.path)
        except ex.excError as e:
            self.log.error(str(e))
            raise ex.excError

    def stop(self):
        pass

    def get_props(self):
        self.props = self.master.autosync_get_props(self.autosync)

        # timestamp format : 15:34:09,May27
        now = datetime.datetime.now()
        try:
            self.ts = datetime.datetime.strptime(str(now.year)+' '+self.props['zfs/time_started'], "%Y %H:%M:%S,%b%d")
            if now < self.ts:
                self.ts = datetime.datetime.strptime(str(now.year-1)+' '+self.props['zfs/time_started'], "%Y %H:%M:%S,%b%d")
        except ValueError:
            raise ex.excError("can not parse last sync date: %s"%self.props['zfs/time_started'])
        self.age = now - self.ts

    def _status(self, verbose=False):
        ret = rcStatus.UP
        try:
            self.get_endpoints()
            self.status_log("master head is %s"%self.master.head)
            self.get_props()
        except ex.excError as e:
            if 'message' in e.value:
                msg = e.value['message']
            else:
                msg = str(e)
            self.status_log(msg)
            return rcStatus.WARN
        except:
            self.status_log("unexpected error")
            self.save_exc()
            return rcStatus.UNDEF

        limit = datetime.timedelta(minutes=self.sync_max_delay)
        if self.age > limit:
            self.status_log("last sync too old: %s ago"%str(self.age))
            ret = rcStatus.WARN
        s = self.master.autosync_get_state(self.autosync)
        if s not in ['online', 'running']:
            self.status_log("runner in '%s' state"%s)
            ret = rcStatus.WARN
        if ret == rcStatus.UP:
            self.status_log("last sync %s ago"%str(self.age))
        return ret

    def get_endpoints(self):
        """ determine which head is the replication master and
            which is replication slave.
        """
        if self.local is not None and self.remote is not None:
            return

        heads = list(set(self.filers.values()) - set([self.filer]))
        if len(heads) != 1:
            raise ex.excError("two heads need to be setup")

        self.local = Nexenta(self.filer, self.log)
        self.remote = Nexenta(heads[0], self.log)

        prop = 'zfs/to-host'
        try:
            localdown = False
            props = self.local.autosync_get_props(self.autosync)
            if prop in props and props[prop] == self.filer:
                self.slave = self.local
                self.master = self.remote
            else:
                self.slave = self.remote
                self.master = self.local
            return
        except ex.excError as e:
            if 'does not exist' in str(e):
                path_props = self.local.get_props(self.path)
                if path_props is None:
                    raise ex.excError("path '%s' not found on local head '%s'"%(self.path, self.filer))
                self.slave = self.local
                self.master = self.remote
            else:
                # local head is down
                localdown = True

        try:
            props = self.remote.autosync_get_props(self.autosync)
            if prop in props and props[prop] == self.filer:
                self.slave = self.local
                self.master = self.remote
            else:
                self.slave = self.remote
                self.master = self.local
            return
        except ex.excError as e:
            if 'does not exist' in str(e):
                path_props = self.remote.get_props(self.path)
                if path_props is None:
                    raise ex.excError("path '%s' not found on remote head '%s'"%(self.path, self.filer))
                self.slave = self.remote
                self.master = self.local
            elif localdown:
                raise ex.excError("both heads unreachable")

         
    def __init__(self,
                 rid=None,
                 name=None,
                 path=None,
                 filers={},
                 reversible=False,
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 internal=False,
                 subset=None):
        resSync.Sync.__init__(self,
                              rid=rid,
                              type="sync.nexenta",
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)
        self.label = "nexenta autosync %s"%name
        self.autosync = name
        self.filers = filers
        self.path = path
        self.reversible = reversible
        self.filer = filers[rcEnv.nodename]
        self.master = None
        self.slave = None
        self.ts = None
        self.age = None
        self.props = None
        self.local = None
        self.remote = None

    def __str__(self):
        return "%s autosync=%s" % (resSync.Sync.__str__(self),\
                self.autosync)

0707010000bc68000081a400000000000000000000000154fc7bfb00002dbe000000660000000300000000000000000000002500000000root/opt/opensvc/lib/resSyncRados.py  #
# Copyright (c) 2014 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import json

from rcUtilities import which, justcall
import rcExceptions as ex
import rcStatus
import datetime
import resSync

class syncRadosSnap(resSync.Sync):
    def recreate(self):
        self.validate_image_fmt()
        for image in self.images:
            self._recreate(image)

    def _recreate(self, image):
        snapnames = self._get_all(image)
        last_date, last_name = self._get_last(image)
        if self.skip_sync(last_date):
            self.log.info("skip resync for image %s: last resync on %s"%(image, str(last_date)))
            return
        snapname = self.snap_basename() + datetime.datetime.now().strftime(self.date_fmt)
        cmd = self.rbd_cmd()+['snap', 'create', image, '--snap', snapname]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

        for snapname in snapnames:
            self.rm(snapname)

    def rm(self, image):
        cmd = self.rbd_cmd()+['snap', 'rm', image]
        ret, out, err = self.vcall(cmd)

    def unprotect(self, image):
        cmd = self.rbd_cmd()+['snap', 'unprotect', image]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def get_all(self):
        data = {}
        for image in self.images:
            data[image] = self._get_all(image)
        return data

    def _get_all(self, image):
        data = self.list()
        retained = []
        prefix = image+"@"+self.snap_basename()
        for name in data:
            if not name.startswith(prefix):
                continue
            retained.append(name)
        return retained

    def get_last(self):
        data = {}
        for image in self.images:
            data[image] = self._get_last(image)
        return data

    def _get_last(self, image):
        data = self.list()
        retained = []
        prefix = image+"@"+self.snap_basename()
        for name in data:
            if not name.startswith(prefix):
                continue
            try:
                date = datetime.datetime.strptime(name, prefix+self.date_fmt)
            except:
                continue
            retained.append((date, name))
        if len(retained) == 0:
            return None, None
        last_date, last_name = sorted(retained)[-1]
        return last_date, last_name

    def rbd_cmd(self):
        l = ["rbd"]
        if self.client_id:
            l += ["-n", self.client_id]
        if self.keyring:
            l += ["--keyring", self.keyring]
        return l

    def snap_basename(self):
        return self.rid+"."

    def get_pools(self):
        l = set([])
        for image in self.images:
            pool = image.split("/")[0]
            l.add(pool)
        return l

    def list(self):
        if self.list_data is not None:
            return self.list_data
        data = {}
        for pool in self.get_pools():
            data.update(self._list(pool))

        self.list_data = data
        return data

    def _list(self, pool):
        cmd = self.rbd_cmd() + ["ls", "-l", pool, "--format", "json"]
        out, err, ret = justcall(cmd)
        data = {}
        try:
            _data = json.loads(out)
        except Exception as e:
            self.status_log(str(e))
            _data = []
        for img_data in _data:
            idx = pool+"/"+img_data['image']
            if "snapshot" in img_data:
                idx += "@"+img_data['snapshot']
            data[idx] = img_data
        return data

    def _status(self, verbose=False):
        try:
            self.validate_image_fmt()
        except Exception as e:
            self.status_log(str(e))
            return rcStatus.WARN

        try:
            data = self.get_last()
        except Exception as e:
            self.status_log(str(e))
            return rcStatus.WARN

        nosnap = []
        expired = []
        ok = []

        for image in self.images:
            date, snapname = data[image]

            if date is None:
                nosnap.append(image)
            elif date < datetime.datetime.now() - datetime.timedelta(minutes=self.sync_max_delay):
                expired.append(image)
            else:
                ok.append(image)

        r = rcStatus.UP

        if len(nosnap) > 0:
            self.status_log("no snap found for images: "+", ".join(nosnap))
            r = rcStatus.WARN
        if len(expired) > 0:
            self.status_log("snap too old for images: "+", ".join(expired))
            r = rcStatus.WARN

        return r

    def syncupdate(self):
        self.recreate()

    def syncresync(self):
        self.recreate()

    def __init__(self,
                 rid=None,
                 images=[],
                 client_id=None,
                 keyring=None,
                 type="sync.rados",
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 internal=False,
                 subset=None):
        resSync.Sync.__init__(self,
                              rid=rid,
                              type=type,
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)

        self.fmt_label("snap", images)
        self.images = images
        if not client_id.startswith("client."):
            client_id = "client."+client_id
        self.client_id = client_id
        self.keyring = keyring
        self.list_data = None
        self.date_fmt = "%Y-%m-%d.%H:%M:%S"

    def validate_image_fmt(self):
        l = []
        for image in self.images:
            if image.count("/") != 1:
                l.append(image)
        if len(l) > 0:
            raise ex.excError("wrong format (expected pool/image): "+", ".join(l))

    def fmt_label(self, t, l):
        self.label = t+" rados %s"%', '.join(l)
        if len(self.label) > 80:
            self.label = self.label[:76]+"..."

    def __str__(self):
        return "%s images=%s" % (resSync.Sync.__str__(self),\
                ', '.join(self.images))

class syncRadosClone(syncRadosSnap):
    def __init__(self,
                 rid=None,
                 pairs=[],
                 client_id=None,
                 keyring=None,
                 type="sync.rados",
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 internal=False,
                 subset=None):
        images = map(lambda x: x.split(":")[0], pairs)
        syncRadosSnap.__init__(self,
                              rid=rid,
                              images=images,
                              client_id=client_id,
                              keyring=keyring,
                              type=type,
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)

        self.pairs = pairs
        self.fmt_label("clone", pairs)

    def recreate(self):
        self.validate_pair_fmt()
        for pair in self.pairs:
            self._recreate(pair)

    def _recreate(self, pair):
        image, clone = pair.split(":")
        snapnames = self._get_all(image)
        last_date, last_name = self._get_last(image)
        if self.skip_sync(last_date):
            self.log.info("skip resync for image %s: last resync on %s"%(image, str(last_date)))
            return
        snapname = self.snap_basename() + datetime.datetime.now().strftime(self.date_fmt)
        cmd = self.rbd_cmd()+['snap', 'create', image, '--snap', snapname]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        cmd = self.rbd_cmd()+['snap', 'protect', image+"@"+snapname]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

        list_data = self.list()
        if clone in list_data:
            cmd = self.rbd_cmd()+['rm', clone]
            ret, out, err = self.vcall(cmd)
            if ret != 0:
                raise ex.excError

        cmd = self.rbd_cmd()+['clone', image+"@"+snapname, clone]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

        for snapname in snapnames:
            try:
                self.unprotect(snapname)
                self.rm(snapname)
            except:
                pass

    def validate_pair_fmt(self):
        l = []
        for pair in self.pairs:
            try:
                image, clone = pair.split(":")
            except:
                l.append(image)
                continue
            if image.count("/") != 1 or clone.count("/") != 1:
                l.append(image)
        if len(l) > 0:
            raise ex.excError("wrong format (expected pool/image:pool/image): "+", ".join(l))

    def snap_basename(self):
        return self.rid+".cloneref."

    def _status(self, verbose=False):
        try:
            self.validate_pair_fmt()
        except Exception as e:
            self.status_log(str(e))
            return rcStatus.WARN

        try:
            data = self.get_last()
        except Exception as e:
            self.status_log(str(e))
            return rcStatus.WARN

        nosnap = []
        noclone = []
        expired = []
        invclone = []
        ok = []

        for image in self.images:
            date, snapname = data[image]

            if date is None:
                nosnap.append(image)
            elif date < datetime.datetime.now() - datetime.timedelta(minutes=self.sync_max_delay):
                expired.append(image)
            else:
                ok.append(image)

        list_data = self.list()
        for pair in self.pairs:
            image, clone = pair.split(":")
            if clone not in list_data:
                noclone.append(pair)
            elif not list_data[clone].get("parent"):
                invclone.append(pair)

        r = rcStatus.UP

        if len(nosnap) > 0:
            self.status_log("no snap found for images: "+", ".join(nosnap))
            r = rcStatus.WARN
        if len(expired) > 0:
            self.status_log("snap too old for images: "+", ".join(expired))
            r = rcStatus.WARN
        if len(noclone) > 0:
            self.status_log("no clone found for pairs: "+", ".join(noclone))
            r = rcStatus.WARN
        if len(invclone) > 0:
            self.status_log("clone invalid for pairs: "+", ".join(invclone))
            r = rcStatus.WARN

        return r
  0707010000bc69000081a400000000000000000000000154fc7bfb0000413f000000660000000300000000000000000000002500000000root/opt/opensvc/lib/resSyncRsync.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import logging

from rcGlobalEnv import rcEnv
from rcUtilities import which
import rcExceptions as ex
import rcStatus
import datetime
import resSync

def lookup_snap_mod():
    if rcEnv.sysname == 'Linux':
        return __import__('snapLvmLinux')
    elif rcEnv.sysname == 'HP-UX':
        return __import__('snapVxfsHP-UX')
    elif rcEnv.sysname == 'AIX':
        return __import__('snapJfs2AIX')
    elif rcEnv.sysname in ['SunOS', 'FreeBSD']:
        return __import__('snapZfsSunOS')
    elif rcEnv.sysname in ['OSF1']:
        return __import__('snapAdvfsOSF1')
    else:
        raise ex.excError

def get_timestamp_filename(self, node):
    sync_timestamp_d = os.path.join(rcEnv.pathvar, 'sync', node)
    sync_timestamp_f = os.path.join(sync_timestamp_d, self.svc.svcname+'!'+self.rid)
    return sync_timestamp_f

def add_sudo_rsync_path(options):
    if "--rsync-path" not in " ".join(options):
        options += ['--rsync-path', 'sudo rsync']
        return options

    new = []
    skip = False
    for i, w in enumerate(options):
        if skip:
            skip = False
            continue
        if w.startswith('--rsync-path'):
            if "=" in w:
                l = w.split("=")
                if len(l) == 2:
                    val = l[1]
            elif len(options) > i+1:
                val = options[i+1]
                skip = True
            else:
                raise ex.excError("malformed --rsync-path value")
            if not "sudo " in val:
                val = val.strip("'")
                val = val.strip('"')
                val = "sudo "+val
            new += ['--rsync-path', val]
        else:
            new.append(w)
    return new

def get_timestamp(self, node):
    ts = None
    sync_timestamp_f = get_timestamp_filename(self, node)
    if not os.path.exists(sync_timestamp_f):
        return None
    try:
        with open(sync_timestamp_f, 'r') as f:
            d = f.read()
            ts = datetime.datetime.strptime(d,"%Y-%m-%d %H:%M:%S.%f\n")
            f.close()
    except:
        self.log.info("failed get last sync date for %s to %s"%(self.src, node))
        return ts
    return ts

class Rsync(resSync.Sync):
    """Defines a rsync job from local node to its remote nodes. Target nodes
    can be restricted to production sibblings or to disaster recovery nodes,
    or both.
    """
    def node_can_sync(self, node):
        ts = get_timestamp(self, node)
        return not self.skip_sync(ts)

    def node_need_sync(self, node):
        ts = get_timestamp(self, node)
        return self.alert_sync(ts)

    def can_sync(self, target=None):
        targets = set([])
        if target is None:
            targets = self.nodes_to_sync('nodes')
            targets |= self.nodes_to_sync('drpnodes')
        else:
            targets = self.nodes_to_sync(target)

        if len(targets) == 0:
            return False
        return True

    def nodes_to_sync(self, target=None, state="syncable", status=False):
        """ Checks are ordered by cost
        """

        if self.skip or self.is_disabled():
            return set([])

        """ DRP nodes are not allowed to sync nodes nor drpnodes
        """
        if rcEnv.nodename in self.svc.drpnodes:
            return set([])

        """ Refuse to sync from a flex non-primary node
        """
        if self.svc.clustertype in ["flex", "autoflex"] and \
           self.svc.flex_primary != rcEnv.nodename:
            if not self.svc.cron:
                self.log.info("won't sync this resource from a flex non-primary node")
            return set([])

        """Discard the local node from the set
        """
        if target in self.target.keys():
            targets = self.target[target].copy()
        else:
            return set([])

        targets -= set([rcEnv.nodename])
        if len(targets) == 0:
            return set([])

        for node in targets.copy():
            if state == "syncable" and not self.node_can_sync(node):
                targets -= set([node])
                continue
            elif state == "late" and not self.node_need_sync(node):
                targets -= set([node])
                continue

        if len(targets) == 0:
            return set([])

        """Accept to sync from here only if the service is up
           Also accept n/a status, because it's what the overall status
           ends up to be when only sync#* are specified using --rid

           sync#i1 is an exception, because we want all prd nodes to
           sync their system files to all drpnodes regardless of the service
           state
        """
        s = self.svc.group_status(excluded_groups=set(["sync", "hb", "app"]))
        if not self.svc.force and \
           s['overall'].status not in [rcStatus.UP, rcStatus.NA] and \
           self.rid != "sync#i1":
            if s['overall'].status == rcStatus.WARN:
                if not self.svc.cron:
                    self.log.info("won't sync this resource service in warn status")
            if not self.svc.cron:
                self.log.info("won't sync this resource for a service not up")
            return set([])

        for node in targets.copy():
            if not status and not self.remote_node_type(node, target):
                targets -= set([node])
                continue
            if not status and not self.remote_fs_mounted(node):
                targets -= set([node])
                continue

        if len(targets) == 0:
            return set([])

        return targets

    def bwlimit_option(self):
        if self.bwlimit is not None:
            bwlimit = [ '--bwlimit='+str(self.bwlimit) ]
        elif self.svc.bwlimit is not None:
            bwlimit = [ '--bwlimit='+str(self.svc.bwlimit) ]
        else:
            bwlimit = []
        return bwlimit

    def mangle_options(self, ruser):
        if ruser != "root":
            options = add_sudo_rsync_path(self.options)
        else:
            options = self.options
        options += self.bwlimit_option()
        if '-e' in options:
            return options

        if rcEnv.rsh.startswith("/usr/bin/ssh") and rcEnv.sysname == "SunOS":
            # SunOS "ssh -n" doesn't work with rsync
            rsh = rcEnv.rsh.replace("-n", "")
        else:
            rsh = rcEnv.rsh
        options += ['-e', rsh]
        return options

    def sync_timestamp(self, node):
        sync_timestamp_f = get_timestamp_filename(self, node)
        sync_timestamp_d = os.path.dirname(sync_timestamp_f)
        sync_timestamp_d_src = os.path.join(rcEnv.pathvar, 'sync', rcEnv.nodename)
        sync_timestamp_f_src = os.path.join(sync_timestamp_d_src, self.svc.svcname+'!'+self.rid)
        sched_timestamp_f = os.path.join(rcEnv.pathvar, '_'.join(('last_sync', self.svc.svcname, self.rid)))
        if not os.path.isdir(sync_timestamp_d):
            os.makedirs(sync_timestamp_d, 0o755)
        if not os.path.isdir(sync_timestamp_d_src):
            os.makedirs(sync_timestamp_d_src, 0o755)
        with open(sync_timestamp_f, 'w') as f:
            f.write(str(self.svc.action_start_date)+'\n')
        import shutil
        shutil.copy2(sync_timestamp_f, sync_timestamp_d_src)
        shutil.copy2(sync_timestamp_f, sched_timestamp_f)
        ruser = self.svc.node.get_ruser(node)
        options = self.mangle_options(ruser)
        cmd = ['rsync'] + options
        cmd += ['-R', sync_timestamp_f, sync_timestamp_f_src, ruser+'@'+node+':/']
        self.call(cmd)

    def sync(self, target):
        if target not in self.target.keys():
            if not self.svc.cron:
                self.log.info('%s => %s sync not applicable to %s'%(self.src, self.dst, target))
            return 0

        targets = self.nodes_to_sync(target)

        if len(targets) == 0:
            if not self.svc.cron:
                self.log.info("no nodes to sync")
            raise ex.syncNoNodesToSync

        if "delay_snap" in self.tags:
            if not hasattr(self.rset, 'snaps'):
                Snap = lookup_snap_mod()
                self.rset.snaps.log = self.log
                self.rset.snaps = Snap.Snap(self.rid)
            self.rset.snaps.try_snap(self.rset, target, rid=self.rid)

        if hasattr(self, "alt_src"):
            """ The pre_action() has provided us with a better source
                to sync from. Use that
            """
            src = self.alt_src
        else:
            src = self.src

        if len(src) == 0:
            if not self.svc.cron:
                self.log.info("no files to sync")
            raise ex.syncNoFilesToSync

        for node in targets:
            ruser = self.svc.node.get_ruser(node)
            dst = ruser + '@' + node + ':' + self.dst
            options = self.mangle_options(ruser)
            cmd = ['rsync'] + options + src
            cmd.append(dst)
            if self.rid.startswith("sync#i"):
                (ret, out, err) = self.call(cmd)
            else:
                (ret, out, err) = self.vcall(cmd)
            if ret != 0:
                self.log.error("node %s synchronization failed (%s => %s)" % (node, src, dst))
                continue
            self.sync_timestamp(node)
            self.svc.need_postsync |= set([node])
        return

    def pre_action(self, rset, action):
        """Actions to do before resourceSet iterates through the resources to
           trigger action() on each one
        """

        resources = [ r for r in rset.resources if not r.skip and not r.is_disabled() ]

        if len(resources) == 0:
            return

        """Don't sync PRD services when running on !PRD node
        """
        if self.svc.svctype == 'PRD' and rcEnv.host_mode != 'PRD':
            if not self.svc.cron:
                self.log.info("won't sync a PRD service running on a !PRD node")
            raise ex.excAbortAction

        """ Is there at least one node to sync ?
        """
        targets = set([])
        rtargets = {0: set([])}
        need_snap = False
        for i, r in enumerate(resources):
            if self.skip or r.is_disabled():
                continue
            rtargets[i] = set([])
            if action == "syncnodes":
                rtargets[i] |= r.nodes_to_sync('nodes')
            else:
                rtargets[i] |= r.nodes_to_sync('drpnodes')
            for node in rtargets[i].copy():
                if not r.node_can_sync(node):
                    rtargets[i] -= set([node])
                elif r.snap:
                    need_snap = True
        for i in rtargets:
            targets |= rtargets[i]

        if len(targets) == 0:
            if not self.svc.cron:
                self.log.info("no node to sync")
            raise ex.excAbortAction

        if not need_snap:
            self.log.debug("snap not needed")
            return

        Snap = lookup_snap_mod()
        try:
            rset.snaps = Snap.Snap(self.rid)
            rset.snaps.log = self.log
            rset.snaps.try_snap(rset, action)
        except ex.syncNotSnapable:
            raise ex.excError

    def post_action(self, rset, action):
        """Actions to do after resourceSet has iterated through the resources to
           trigger action() on each one
        """
        resources = [ r for r in rset.resources if not r.skip and not r.is_disabled() ]

        if len(rset.resources) == 0:
            return

        if hasattr(rset, 'snaps'):
            rset.snaps.snap_cleanup(rset)

    def syncnodes(self):
        try:
            self.sync("nodes")
        except ex.syncNoFilesToSync:
            if not self.svc.cron:
                self.log.info("no file to sync")
            pass
        except ex.syncNoNodesToSync:
            if not self.svc.cron:
                self.log.info("no node to sync")
            pass

    def syncdrp(self):
        try:
            self.sync("drpnodes")
        except ex.syncNoFilesToSync:
            if not self.svc.cron:
                self.log.info("no file to sync")
            pass
        except ex.syncNoNodesToSync:
            if not self.svc.cron:
                self.log.info("no node to sync")
            pass

    def _status(self, verbose=False):
        """ mono-node service should return n/a as a sync state
        """
        target = set([])
        for i in self.target:
            target |= self.target[i]
        if len(target - set([rcEnv.nodename])) == 0:
            self.status_log("no destination nodes")
            return rcStatus.NA

        """ sync state on nodes where the service is not UP
        """
        s = self.svc.group_status(excluded_groups=set(["sync", "hb", "app"]))
        if s['overall'].status != rcStatus.UP or \
           (self.svc.clustertype in ['flex', 'autoflex'] and \
            rcEnv.nodename != self.svc.flex_primary and \
            s['overall'].status == rcStatus.UP):
            if rcEnv.nodename not in target:
                self.status_log("passive node not in sync destination nodes")
                return rcStatus.NA
            if self.node_need_sync(rcEnv.nodename):
                self.status_log("passive node needs update")
                return rcStatus.WARN
            else:
                return rcStatus.UP

        """ sync state on DRP nodes where the service is UP
        """
        if 'drpnodes' in self.target and rcEnv.nodename in self.target['drpnodes']:
            self.status_log("service up on drp node, sync disabled")
            return rcStatus.NA

        """ sync state on nodes where the service is UP
        """
        nodes = []
        nodes += self.nodes_to_sync('nodes', state="late", status=True)
        nodes += self.nodes_to_sync('drpnodes', state="late", status=True)
        if len(nodes) == 0:
            return rcStatus.UP

        self.status_log("%s need update"%', '.join(nodes))
        return rcStatus.DOWN

    def __init__(self,
                 rid=None,
                 src=[],
                 dst=None,
                 options=[],
                 target={},
                 dstfs=None,
                 snap=False,
                 bwlimit=None,
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 internal=False,
                 subset=None):
        resSync.Sync.__init__(self,
                              rid=rid,
                              type="sync.rsync",
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)

        if internal:
            if rcEnv.drp_path in dst:
                self.label = "rsync system files to drpnodes"
            else:
                self.label = "rsync svc config to %s"%(', '.join(target.keys()))
        else:
            _src = ', '.join(src)
            if len(_src) > 300:
                _src = _src[0:300]
            _dst = ', '.join(target.keys())
            self.label = "rsync %s to %s"%(_src, _dst)
        self.src = src
        self.dst = dst
        self.dstfs = dstfs
        self.snap = snap
        self.target = target
        self.bwlimit = bwlimit
        self.internal = internal
        self.timeout = 3600
        self.options = ['-HpogDtrlvx',
                        '--stats',
                        '--delete',
                        '--force',
                        '--timeout='+str(self.timeout)]
        self.options += options

    def __str__(self):
        return "%s src=%s dst=%s options=%s target=%s" % (resSync.Sync.__str__(self),\
                self.src, self.dst, self.options, str(self.target))

 0707010000bc6a000081a400000000000000000000000154fc7bfb0000447b000000660000000300000000000000000000002800000000root/opt/opensvc/lib/resSyncSymSrdfS.py   #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import logging

from rcGlobalEnv import rcEnv
from rcUtilities import which
from xml.etree.ElementTree import ElementTree, XML

import rcExceptions as ex
import rcStatus
import time
import datetime
import resSync
os.environ['PATH'] += ":/usr/symcli/bin"

class syncSymSrdfS(resSync.Sync):

    def postsync(self):
        self.do_dgimport()

    def presync(self):
        s = self.svc.group_status(excluded_groups=set(["sync", "hb"]))
        if self.svc.force or s['overall'].status == rcStatus.UP:
            self.do_rdf_dgexport()

    def files_to_sync(self):
        return [self.dgfile_rdf_name()]
   
    def do_local_dgexport(self):
        cmd = ['symdg', 'export', self.symdg, '-f', self.dgfile_local_name()]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError("Failed to run command %s"% ' '.join(cmd) )
        return out
        
    def do_rdf_dgexport(self):
        cmd = ['symdg', 'export', self.symdg, '-f', self.dgfile_rdf_name(), '-rdf']
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError("Failed to run command %s"% ' '.join(cmd) )
        return out
        
    def do_dgremove(self):
        cmd = ['symdg', 'delete', self.symdg, '-force']
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError("Failed to run command %s"% ' '.join(cmd) )
        return out

    def is_dgimport_needed(self):
        self.do_local_dgexport()
        import filecmp
        if filecmp.cmp(self.dgfile_local_name(), self.dgfile_rdf_name(), shallow=False):
            return False
        return True

    def do_dgimport(self):
        if self.symdg in self.get_dg_list():
            if not self.is_dgimport_needed():
                self.log.info("symrdf dg %s is already up to date"%self.symdg)
                return
            else:
                self.do_dgremove()
        self.log.info("symrdf dg %s will be imported from file"%self.symdg)
        cmd = ['symdg', 'import', self.symdg, '-f', self.dgfile_rdf_name()]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError("Failed to run command %s"% ' '.join(cmd) )
        return out

    def dgfile_local_name(self):
        return os.path.join(rcEnv.pathvar, 'symrdf_' + self.svc.svcname + '.dg.local')

    def dgfile_rdf_name(self):
        return os.path.join(rcEnv.pathvar, 'symrdf_' + self.svc.svcname + '.dg.rdf')

    def flush_cache(self):
        self.rdf_query_cache = None

    def get_symdevs(self):
        for symdev in self.symdevs:
            l = symdev.split(':')
            if len(l) != 2:
                self.log.error("symdevs must be in symid:symdev ... format")
                raise ex.excError
            self.symdev[l[0],l[1]] = dict(symid=l[0], symdev=l[1])

    def rdf_query(self, cache=True):
        if cache and self.rdf_query_cache is not None:
            return self.rdf_query_cache
        cmd = ['symrdf', '-g', self.symdg, '-rdfg', str(self.rdfg), 'query', '-output', 'xml_e']
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError("Failed to run command %s"% ' '.join(cmd) )
        self.rdf_query_cache = out
        return out

    def dg_query(self):
        cmd = ['symdg', 'list', '-output', 'xml_e']
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError("Failed to run command %s"% ' '.join(cmd) )
        return out

    # browse local device groups and build dict with list
    def get_dg_list(self):
        try:
            rdf_query = self.dg_query()
        except:
            return {}
        self.xmldg = XML(rdf_query)
        self.dglist = {} 
        for dg in self.xmldg.iter("DG_Info"):
	        name = dg.find('name').text
	        self.dglist[name] = None
        
        return self.dglist

    def get_dg_rdf_type(self):
        rdf_query = self.rdf_query()
        self.xmldg = XML(rdf_query)
        rdftype = self.xmldg.find('DG/DG_Info/type').text
        return rdftype

    def is_rdf1_dg(self):
        if self.get_dg_rdf_type() == "RDF1":
            return True
        return False

    def is_rdf2_dg(self):
        if self.get_dg_rdf_type() == "RDF2":
            return True
        return False

    def is_rdf21_dg(self):
        if self.get_dg_rdf_type() == "RDF21":
            return True
        return False


    def get_dg_state(self):
        h = {}
        for pair in self.xmldg.iter("RDF_Pair"):
            mode = pair.find('mode').text
            state = pair.find('pair_state').text
            key = mode + "/" + state
            h[key] = None
        if len(h) == 1:
            retmsg = h.keys()[0]
        else:
            retmsg = "mixed srdf pairs state"
        return retmsg

    def get_rdfpairs_from_dg(self):
        cmd = ['symrdf', '-g', self.symdg, '-rdfg', str(self.rdfg), 'query', '-output', 'xml_e']
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError

	    self.rdfpairs = {}   # remote_symm;remote_dev;rdfg
	    self.xmldg = XML(out)

        for pair in self.xmldg.iter("RDF_Pair"):
	        source = pair.find('Source/dev_name').text
	        target = pair.find('Target/dev_name').text
	        self.rdfpairs[source] = target
        print self.rdfpairs

    def is_synchronous_mode(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg', str(self.rdfg), 'verify', '-synchronous']
        (ret, out, err) = self.call(cmd)
        if ret == 0:
            return True
        return False

    def is_asynchronous_mode(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg', str(self.rdfg), 'verify', '-asynchronous']
        (ret, out, err) = self.call(cmd)
        if ret == 0:
            return True
        return False

    def is_acp_disk_mode(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg', str(self.rdfg), 'verify', '-acp_disk']
        (ret, out, err) = self.call(cmd)
        if ret == 0:
            return True
        return False

    def is_synchronized_state(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg', str(self.rdfg), 'verify', '-synchronized']
        (ret, out, err) = self.call(cmd)
        if ret == 0:
            return True
        return False

    def is_synchronous_and_synchronized_state(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg', str(self.rdfg), 'verify', '-synchronous', '-synchronized']
        (ret, out, err) = self.call(cmd)
        if ret == 0:
            return True
        return False

    def is_syncinprog_state(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg', str(self.rdfg), 'verify', '-syncinprog']
        (ret, out, err) = self.call(cmd)
        if ret == 0:
            return True
        return False

    def is_suspend_state(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg', str(self.rdfg), 'verify', '-suspended']
        (ret, out, err) = self.call(cmd)
        if ret == 0:
            return True
        return False

    def is_split_state(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg', str(self.rdfg), 'verify', '-split']
        (ret, out, err) = self.call(cmd)
        if ret == 0:
            return True
        return False

    def is_failedover_state(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg', str(self.rdfg), 'verify', '-failedover']
        (ret, out, err) = self.call(cmd)
        if ret == 0:
            return True
        return False

    def is_partitioned_state(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg', str(self.rdfg), 'verify', '-partitioned']
        (ret, out, err) = self.call(cmd)
        if ret == 0:
            return True
        return False

    # SRDF/A expected state is consistent AND enabled
    def is_consistent_state(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg', str(self.rdfg), 'verify', '-consistent']
        (ret, out, err) = self.call(cmd)
        if ret == 0:
            return True
        return False

    def is_enabled_state(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg', str(self.rdfg), 'verify', '-enabled']
        (ret, out, err) = self.call(cmd)
        if ret == 0:
            return True
        return False

    def can_sync(self, target=None):
        return True

    def resume(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg' , str(self.rdfg), '-noprompt', 'resume']
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError("Failed to run command %s"% ' '.join(cmd) )
        self.flush_cache()

    def suspend(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg' , str(self.rdfg), '-noprompt', 'suspend']
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError("Failed to run command %s"% ' '.join(cmd) )
        self.flush_cache()

    def establish(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg' , str(self.rdfg), '-noprompt', 'establish']
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError("Failed to run command %s"% ' '.join(cmd) )
        self.flush_cache()

    def failover(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg' , str(self.rdfg), '-noprompt', 'failover']
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError("Failed to run command %s"% ' '.join(cmd) )
        self.flush_cache()

    def failoverestablish(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg' , str(self.rdfg), '-noprompt', 'failover', '-establish']
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError("Failed to run command %s"% ' '.join(cmd) )
        self.flush_cache()

    def split(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg' , str(self.rdfg), '-noprompt', 'split']
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        self.flush_cache()

    def swap(self):
        cmd = ['/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg' , str(self.rdfg), '-noprompt', 'swap']
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        self.flush_cache()

    def get_syminfo(self):
        # self.get_symdevs()
	    self.get_dg_rdf_type()
        #self.get_symld()
       # self.get_pairs()

    def get_last(self):
        if self.last is not None:
            return
        for symid, symdev in self.symdev:
            ld = self.symld[symid,symdev]
            # format: Thu Feb 25 10:20:56 2010
            last = datetime.datetime.strptime(ld['clone_lastaction'], "%a %b %d %H:%M:%S %Y")
            if self.last is None or last > self.last:
                self.last = last

    def _status(self, verbose=False):
        try:
            self.get_syminfo()
        except ex.excError as e:
            self.status_log(str(e))
            return rcStatus.WARN
        state = self.get_dg_state()
        self.status_log("current state %s"%state)
        if self.is_synchronous_and_synchronized_state():
            return rcStatus.UP
        self.status_log("expecting synchronous/synchronized")
        return rcStatus.WARN

    # SRDF split 
    def syncsplit(self):
        self.split()

    # SRDF suspend 
    def syncquiesce(self):
        self.suspend()

    # SRDF swap
    def syncswap(self):
        self.swap()

    def syncbreak(self):
        self.split()

    # SRDF establish
    def syncresync(self):
        self.establish()

    def syncestablish(self):
        self.establish()

    def start(self):
        if rcEnv.nodename in self.svc.drpnodes:
            if self.is_rdf2_dg():
                if self.is_synchronous_and_synchronized_state():
                    self.split()
                    return
                elif self.is_partitioned_state():
                    self.log.warning("symrdf dg %s is RDF2 and partitioned. failover is preferred action."%self.symdg)
                    self.failover()
                    return
                elif self.is_failedover_state():
                    self.log.info("symrdf dg %s is already RDF2 and FailedOver."%self.symdg)
                    return
                elif self.is_suspend_state():
                    self.log.warning("symrdf dg %s is RDF2 and suspended: R2 data may be outdated"%self.symdg)
                    self.split()
                    return
                elif self.is_split_state():
                    self.log.info("symrdf dg %s is RDF2 and already splitted."%self.symdg)
                    return
                else:
                    raise ex.excError("symrdf dg %s is RDF2 on drp node and unexpected SRDF state, you have to manually return to a sane SRDF status.")
            elif self.is_rdf1_dg():
                if self.is_synchronous_and_synchronized_state():
                    return
                else:
                    raise ex.excError("symrdf dg %s is RDF1 on drp node, you have to manually return to a sane SRDF status.")
        elif rcEnv.nodename in self.svc.nodes:
            if self.is_rdf1_dg():
                if self.is_synchronous_and_synchronized_state():
                    self.log.info("symrdf dg %s is RDF1 and synchronous/synchronized."%self.symdg)
                    return
                elif self.is_partitioned_state():
                    self.log.warning("symrdf dg %s is RDF1 and partitioned."%self.symdg)
                    return
                elif self.is_failedover_state():
                    raise ex.excError("symrdf dg %s is RDF1 and write protected, you have to manually run either syncsplit+syncestablish (ie loosing R2 data), or syncfailback (ie loosing R1 data)"%self.symdg)
                elif self.is_suspend_state():
                    self.log.warning("symrdf dg %s is RDF1 and suspended."%self.symdg)
                    return
                elif self.is_split_state():
                    self.log.warning("symrdf dg %s is RDF1 and splitted."%self.symdg)
                    return
                else:
                    raise ex.excError("symrdf dg %s is RDF1 on primary node and unexpected SRDF state, you have to manually return to a sane SRDF status.")
            elif self.is_rdf2_dg():         # start on metrocluster passive node
                if self.is_synchronous_and_synchronized_state():
                    self.failoverestablish() 
                elif self.is_partitioned_state():
                    self.log.warning("symrdf dg %s is RDF2 and partitioned, failover is preferred action."%self.symdg)
                    self.failover() 
                    return
                else:
                    raise ex.excError("symrdf dg %s is RDF2 on primary node, you have to manually return to a sane SRDF status.")

# self.svc.force = True ou False            

    def refresh_svcstatus(self):
        self.svcstatus = self.svc.group_status(excluded_groups=set(["sync", 'hb']))

    def get_svcstatus(self):
        if len(self.svcstatus) == 0:
            self.refresh_svcstatus()

    def __init__(self,
                 rid=None,
                 symdg=None,
                 rdfg=None,
                 symdevs=[],
                 precopy_timeout=300,
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 internal=False,
                 subset=None):
        resSync.Sync.__init__(self,
                              rid=rid,
                              type="sync.symsrdfs",
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)

        self.rdf_query_cache = None
        self.label = "srdf/s symdg %s"%(symdg)
        self.symdg = symdg
        self.rdfg = rdfg
        self.symdevs = symdevs
        self.precopy_timeout = precopy_timeout
        self.disks = set([])
        self.symdev = {}
        self.pdevs = {}
        self.svcstatus = {}
        self.symld = {}
        self.pairs = []
        self._pairs = []
        self.active_pairs = []
        self.last = None

    def __str__(self):
        return "%s symdg=%s symdevs=%s rdfg=%s" % (resSync.Sync.__str__(self),\
                self.symdg, self.symdevs, self.rdfg)

 0707010000bc6b000081a400000000000000000000000154fc7bfb00002706000000660000000300000000000000000000002800000000root/opt/opensvc/lib/resSyncSymclone.py   #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import logging

from rcGlobalEnv import rcEnv
from rcUtilities import which, justcall
import rcExceptions as ex
import rcStatus
import time
import datetime
import resSync

class syncSymclone(resSync.Sync):
    def wait_for_devs_ready(self):
        pass

    def get_symdevs(self):
        for symdev in self.symdevs:
            l = symdev.split(':')
            if len(l) != 2:
                self.log.error("symdevs must be in symid:symdev ... format")
                raise ex.excError
            self.symdev[l[0],l[1]] = dict(symid=l[0], symdev=l[1])

    def get_symld(self):
        cmd = ['/usr/symcli/bin/symld', '-g', self.symdg, 'list', '-v']
        out, err, ret = justcall(cmd)
        if ret != 0:
            if len(err) > 0:
                self.status_log(err.strip())
            raise ex.excError
        ld = {}
        for line in out.split('\n'):
            if len(line) == 0:
                continue
            l = line.split(': ')
            if len(l) != 2:
                continue
            if "Device Physical Name" in l[0]:
                """ New logical device
                """
                if ld != {}:
                    self.symld[ld['symid'],ld['symdev']] = ld
                    ld = {}
                ld['pdev'] = l[1].strip()
            elif "  Symmetrix ID" in l[0]:
                ld['symid'] = l[1].strip()
            elif "  Device Symmetrix Name" in l[0]:
                ld['symdev'] = l[1].strip()
            elif "  Device Logical Name" in l[0]:
                ld['symld'] = l[1].strip()
            elif "  Product Revision" in l[0]:
                ld['symrev'] = l[1].strip()
            elif "  Source (SRC) Device Symmetrix Name" in l[0]:
                ld['clone_srcdev'] = l[1].strip()
            elif "  Target (TGT) Device Symmetrix Name" in l[0]:
                ld['clone_tgtdev'] = l[1].strip()
            elif "  State of Session" in l[0]:
                ld['clone_state'] = l[1].strip()
            elif "  Time of Last Clone Action" in l[0]:
                ld['clone_lastaction'] = l[1].strip()
            elif "  Changed Tracks for SRC Device" in l[0]:
                ld['clone_srcchangedtracks'] = l[1].strip()
            elif "  Changed Tracks for TGT Device" in l[0]:
                ld['clone_tgtchangedtracks'] = l[1].strip()
        if ld != {}:
            self.symld[ld['symid'],ld['symdev']] = ld

    def is_active(self):
        for pair in self._pairs:
            if pair in self.active_pairs:
                continue
            cmd = ['/usr/symcli/bin/symclone', '-g', self.symdg, 'verify', '-copied']+pair
            out, err, ret = justcall(cmd)
            if ret == 0:
                self.active_pairs.append(pair)
                continue
            cmd = ['/usr/symcli/bin/symclone', '-g', self.symdg, 'verify', '-copyinprog']+pair
            out, err, ret = justcall(cmd)
            if ret == 0:
                self.active_pairs.append(pair)
                continue
        if len(self.active_pairs) == len(self._pairs):
            return True
        return False

    def is_copied(self):
        cmd = ['/usr/symcli/bin/symclone', '-g', self.symdg, 'verify', '-copied']+self.pairs
        (ret, out, err) = self.call(cmd)
        if ret == 0:
            return True
        return False

    def is_activable(self):
        cmd = ['/usr/symcli/bin/symclone', '-g', self.symdg, 'verify', '-precopy']+self.pairs
        (ret, out, err) = self.call(cmd)
        if ret == 0:
            return True
        return False

    def get_pairs(self):
        for symid, dev in self.symdev:
            ld = self.symld[symid,dev]
            if dev == ld['clone_tgtdev']:
                tgtld = ld['symld']
                srcld = self.symld[ld['symid'],ld['clone_srcdev']]['symld']
            else:
                self.log.error("device %s not a clone target"%(dev))
                raise ex.excError
            pair = [srcld, 'sym', 'ld', tgtld]
            self.pairs += pair
            self._pairs += [pair]

    def wait_for_active(self):
        delay = 20
        timeout = 300
        self.active_pairs = []
        for i in range(timeout/delay):
            if self.is_active():
                return
            if i == 0:
                self.log.info("waiting for copied or copyinprog state (max %i secs)"%timeout)
            time.sleep(delay)
        self.log.error("timed out waiting for copied or copyinprog state (%i secs)"%timeout)
        ina = set(self._pairs) - set(self.active_pairs)
        ina = map(lambda x: ' '.join(x), ina)
        ina = ", ".join(ina)
        self.log.error("%s still not in copied or copyinprod state"%ina)
        raise ex.excError

    def wait_for_activable(self):
        delay = 30
        for i in range(self.precopy_timeout/delay):
            if self.is_activable():
                return
            if i == 0:
                self.log.info("waiting for precopied state (max %i secs)"%self.precopy_timeout)
            time.sleep(delay)
        self.log.error("timed out waiting for precopied state (%i secs)"%self.precopy_timeout)
        raise ex.excError

    def activate(self):
        self.get_syminfo()
        if self.is_active():
            self.log.info("symclone dg %s is already active"%self.symdg)
            return
        self.wait_for_activable()
        cmd = ['/usr/symcli/bin/symclone', '-g', self.symdg, '-noprompt', 'activate', '-i', '20', '-c', '30']+self.pairs
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        self.wait_for_active()
        self.wait_for_devs_ready()

    def can_sync(self, target=None):
        self.get_syminfo()
        self.get_last()
        if skip_sync(self.last):
            return False
        return True

    def recreate(self):
        self.get_syminfo()
        self.get_last()
        if self.skip_sync(self.last):
            return
        self.get_svcstatus()
        if self.svcstatus['overall'].status != rcStatus.DOWN:
            self.log.error("the service (sync excluded) is in '%s' state. Must be in 'down' state"%self.svcstatus['overall'])
            raise ex.excError
        if not self.is_copied():
            self.log.info("symclone dg %s is not fully copied"%self.symdg)
            return
        cmd = ['/usr/symcli/bin/symclone', '-g', self.symdg, '-noprompt', 'recreate', '-precopy', '-i', '20', '-c', '30']+self.pairs
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def get_syminfo(self):
        self.get_symdevs()
        self.get_symld()
        self.get_pairs()

    def get_last(self):
        if self.last is not None:
            return
        for symid, symdev in self.symdev:
            ld = self.symld[symid,symdev]
            # format: Thu Feb 25 10:20:56 2010
            last = datetime.datetime.strptime(ld['clone_lastaction'], "%a %b %d %H:%M:%S %Y")
            if self.last is None or last > self.last:
                self.last = last

    def _status(self, verbose=False):
        try:
            self.get_syminfo()
            self.get_last()
        except:
            return rcStatus.WARN

        if self.last is None:
            return rcStatus.DOWN
        elif self.last < datetime.datetime.now() - datetime.timedelta(minutes=self.sync_max_delay):
            self.status_log("Last sync on %s older than %d minutes"%(self.last, self.sync_max_delay))
            return rcStatus.WARN
        else:
            return rcStatus.UP

    def syncbreak(self):
        self.activate()

    def syncresync(self):
        self.recreate()

    def start(self):
        self.syncbreak()

    def refresh_svcstatus(self):
        self.svcstatus = self.svc.group_status(excluded_groups=set(["sync", 'hb']))

    def get_svcstatus(self):
        if len(self.svcstatus) == 0:
            self.refresh_svcstatus()

    def __init__(self,
                 rid=None,
                 symdg=None,
                 symdevs=[],
                 precopy_timeout=300,
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 internal=False,
                 subset=None):
        resSync.Sync.__init__(self,
                              rid=rid,
                              type="sync.symclone",
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)

        self.label = "clone symdg %s"%(symdg)
        self.symdg = symdg
        self.symdevs = symdevs
        self.precopy_timeout = precopy_timeout
        self.disks = set([])
        self.symdev = {}
        self.pdevs = {}
        self.svcstatus = {}
        self.symld = {}
        self.pairs = []
        self._pairs = []
        self.active_pairs = []
        self.last = None

    def __str__(self):
        return "%s symdg=%s symdevs=%s" % (resSync.Sync.__str__(self),\
                self.symdg, self.symdevs)

  0707010000bc6c000081a400000000000000000000000154fc7bfb00000ce5000000660000000300000000000000000000002d00000000root/opt/opensvc/lib/resSyncSymcloneLinux.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import logging

from rcGlobalEnv import rcEnv
from rcUtilities import which
import rcExceptions as ex
import rcStatus
import resources as Res
import time
import datetime
import resSyncSymclone as symclone

class syncSymclone(symclone.syncSymclone):
    def dev_rescan(self, dev):
        dev = dev.replace('/dev/', '')
        sysdev = "/sys/block/%s/device/rescan"%dev
        self.log.info("echo 1>%s"%sysdev)
        with open(sysdev, 'w') as s:
            s.write("1")

    def refresh_multipath(self, dev):
        cmd = ['multipath', '-v0', '-r', dev]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def dev_ready(self, dev):
        cmd = ['sg_turs', dev]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            return False
        return True

    def wait_for_dev_ready(self, dev):
        delay = 1
        timeout = 5
        for i in range(timeout/delay):
            if self.dev_ready(dev):
                return
            if i == 0:
                self.log.info("waiting for device %s to become ready (max %i secs)"%(dev,timeout))
            time.sleep(delay)
        self.log.error("timed out waiting for device %s to become ready (max %i secs)"%(dev,timeout))
        raise ex.excError

    def wait_for_devs_ready(self):
        for (symid, devid) in self.symld:
            dev = self.symld[symid, devid]['pdev']
            self.dev_rescan(dev)
            self.wait_for_dev_ready(dev)
            self.refresh_multipath(dev)

    def __init__(self,
                 rid=None,
                 symdg=None,
                 symdevs=[],
                 precopy_timeout=300,
                 sync_max_delay=1440,
                 sync_min_delay=30,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 internal=False,
                 subset=None):
        symclone.syncSymclone.__init__(self,
                                       rid,
                                       symdg,
                                       symdevs,
                                       precopy_timeout,
                                       sync_max_delay,
                                       sync_min_delay,
                                       optional=optional,
                                       disabled=disabled,
                                       tags=tags,
                                       internal=internal,
                                       subset=subset)

   0707010000bc6d000081a400000000000000000000000154fc7bfb0000370a000000660000000300000000000000000000002300000000root/opt/opensvc/lib/resSyncZfs.py    #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
from rcGlobalEnv import rcEnv
import datetime
from subprocess import *
import rcExceptions as ex
import rcStatus
import resSync
from rcZfs import a2pool_dataset, Dataset

class SyncZfs(resSync.Sync):
    """define zfs sync resource to be zfs send/zfs receive between nodes
    """
    def __init__(self,
                 rid=None,
                 target=None,
                 src=None,
                 dst=None,
                 delta_store=None,
                 sender=None,
                 recursive = True,
                 snap_size=0,
                 sync_max_delay=None,
                 schedule=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 subset=None):
        resSync.Sync.__init__(self,
                              rid=rid,
                              type="sync.zfs",
                              sync_max_delay=sync_max_delay,
                              schedule=schedule,
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              subset=subset)

        self.label = "zfs of %s to %s"%(src, target)
        self.target = target
        self.sender = sender
        self.recursive = recursive
        (self.src_pool, self.src_ds) = a2pool_dataset(src)
        (self.dst_pool, self.dst_ds) = a2pool_dataset(dst)
        if delta_store is None:
            self.delta_store = rcEnv.pathvar
        else:
            self.delta_store = delta_store

    def pre_action(self, rset, action):
        """Prepare dataset snapshots
        Don't sync PRD services when running on !PRD node
        skip snapshot creation if delay_snap in tags
        delay_snap should be used for oracle archive datasets
        """
        resources = [ r for r in rset.resources if not r.skip and not r.is_disabled() ]

        if len(resources) == 0:
            return

        if self.svc.svctype == 'PRD' and rcEnv.host_mode != 'PRD':
            self.log.debug("won't sync a PRD service running on a !PRD node")
            raise ex.excAbortAction

        for i, r in enumerate(resources):
            if 'delay_snap' in r.tags:
                continue
            r.get_info()
            if action in ['syncupdate', 'syncresync', 'syncdrp', 'syncnodes']:
                if action == 'syncnodes' and self.target != ['nodes']:
                    return
                if action == 'syncdrp' and self.target != ['drpnodes']:
                    return
                nb = 0
                tgts = r.targets.copy()
                if len(tgts) == 0 :
                    continue
            r.get_info()
            if not r.snap_exists(r.src_snap_tosend):
                r.create_snap(r.src_snap_tosend)

    def __str__(self):
        return "%s target=%s src=%s" % (resSync.Sync.__str__(self),\
                self.target, self.src)

    def snap_exists(self, snapname, node=None):
        cmd = ['env', 'PATH=/usr/sbin:/sbin', 'zfs', 'list', '-t', 'snapshot', snapname]
        if node is not None:
            cmd = rcEnv.rsh.split() + [node] + cmd
        (ret, out, err) = self.call(cmd, errlog=False)
        if ret == 0:
            return True
        else:
            return False

    def create_snap(self, snap):
        snapds=Dataset(snap)
        if snapds.exists():
            self.log.error('%s should not exist'%snap)
            raise ex.excError
        if self.recursive :
            cmd = ['zfs', 'snapshot' , '-r' , snap ]
        else:
            cmd = ['zfs', 'snapshot' , snap ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def get_src_info(self):
        self.src_snap_sent = self.src_ds + '@sent'
        self.src_snap_tosend = self.src_ds + '@tosend'
        self.tosend = "tosend"

    def get_dst_info(self):
        self.dst_snap_sent = self.dst_ds + '@sent'
        self.dst_snap_tosend = self.dst_ds + '@tosend'

    def get_peersenders(self):
        self.peersenders = set([])
        if 'nodes' == self.sender:
            self.peersenders |= self.svc.nodes
            self.peersenders -= set([rcEnv.nodename])

    def get_targets(self):
        self.targets = set()
        if 'nodes' in self.target:
            self.targets |= self.svc.nodes
        if 'drpnodes' in self.target:
            self.targets |= self.svc.drpnodes
        self.targets -= set([rcEnv.nodename])

    def get_info(self):
        self.get_targets()
        self.get_src_info()
        self.get_dst_info()

    def syncnodes(self):
        """alias to syncupdate"""
        self.syncupdate()

    def syncfullsync(self):
        """alias to syncupdate"""
        self.syncupdate()

    def zfs_send_incremental(self, node):
        if self.recursive :  
            send_cmd = ['zfs', 'send', '-R', '-i',
                            self.src_snap_sent, self.src_snap_tosend]
        else:
            send_cmd = ['zfs', 'send', '-i',
                            self.src_snap_sent, self.src_snap_tosend]

        receive_cmd = ['env', 'PATH=/usr/sbin:/sbin', 'zfs', 'receive', '-dF', self.dst_pool]
        if node is not None:
            receive_cmd = rcEnv.rsh.strip(' -n').split() + [node] + receive_cmd

        self.log.info(' '.join(send_cmd + ["|"] + receive_cmd))
        p1 = Popen(send_cmd, stdout=PIPE)
        p2 = Popen(receive_cmd, stdin=p1.stdout, stdout=PIPE)
        buff = p2.communicate()
        if p2.returncode != 0:
            if buff[1] is not None and len(buff[1]) > 0:
                self.log.error(buff[1])
            self.log.error("sync update failed")
            raise ex.excError
        if buff[0] is not None and len(buff[0]) > 0:
            self.log.info(buff[0])

    def zfs_send_initial(self, node=None):
        if self.recursive :  
            send_cmd = ['zfs', 'send', '-R', self.src_snap_tosend]
        else:
            send_cmd = ['zfs', 'send', self.src_snap_tosend]

        receive_cmd = ['env', 'PATH=/usr/sbin:/sbin', 'zfs', 'receive', '-dF', self.dst_pool ]
        if node is not None:
            receive_cmd = rcEnv.rsh.strip(' -n').split() + [node] + receive_cmd

        self.log.info(' '.join(send_cmd + ["|"] + receive_cmd))
        p1 = Popen(send_cmd, stdout=PIPE)
        p2 = Popen(receive_cmd, stdin=p1.stdout, stdout=PIPE)
        buff = p2.communicate()
        if p2.returncode != 0:
            if buff[1] is not None and len(buff[1]) > 0:
                self.log.error(buff[1])
            self.log.error("full sync failed")
            raise ex.excError
        if buff[0] is not None and len(buff[0]) > 0:
            self.log.info(buff[0])

    def remove_snap(self, snap, node=None):
        if not self.snap_exists(snap, node=node):
            return
        if self.recursive :
            cmd = ['zfs', 'destroy', '-r', snap]
        else:
            cmd = ['zfs', 'destroy', snap]
        if node is not None:
            cmd = rcEnv.rsh.split() + [node, 'env', 'PATH=/usr/sbin:/sbin'] + cmd
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def rename_snap(self, src, dst,  node=None):
        if self.snap_exists(dst, node):
            self.log.error("%s should not exist"%dst)
            raise ex.excError
        if self.recursive :
            cmd = ['zfs', 'rename', '-r', src, dst]
        else:
            cmd = ['zfs', 'rename', src, dst]

        if node is not None:
            cmd = rcEnv.rsh.split() + [node, 'env', 'PATH=/usr/sbin:/sbin'] + cmd
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def rotate_snaps(self, src, dst, node=None):
        self.remove_snap(dst, node)
        self.rename_snap(src, dst, node)

    def syncupdate(self):
        """
        test if service status is UP else return
        create the snap_tosend if not already created (during pre_action)
        if a snap has already been sent
        then for all targets
             zfs_send_incremental
             rotate snap
        else for all targets
            zfs_send_initial
            rotate snap
        rotate snap on local node
        """
        s = self.svc.group_status(excluded_groups=set(["sync", "hb"]))
        if s['overall'].status != rcStatus.UP:
            self.log.debug("won't sync this resource for a service not up")
            return

        """ Refuse to sync from a flex non-primary node
        """
        if self.svc.clustertype in ["flex", "autoflex"] and \
           self.svc.flex_primary != rcEnv.nodename:
            self.log.debug("won't sync this resource from a flex non-primary node")
            return set([])

        self.get_info()
        if not self.snap_exists(self.src_snap_tosend):
            self.create_snap(self.src_snap_tosend)
        if self.snap_exists(self.src_snap_sent):
            for n in self.targets:
                self.zfs_send_incremental(n)
                self.rotate_snaps(self.dst_snap_tosend, self.dst_snap_sent, n)
        else:
            for n in self.targets:
                self.zfs_send_initial(n)
                self.rotate_snaps(self.dst_snap_tosend, self.dst_snap_sent, n)
        self.rotate_snaps(self.src_snap_tosend, self.src_snap_sent)
        self.write_statefile()
        for n in self.targets:
            self.push_statefile(n)

    def start(self):
        pass

    def stop(self):
        pass

    def can_sync(self, target=None):
        try:
            ls = self.get_local_state()
            ts = datetime.datetime.strptime(ls['date'], "%Y-%m-%d %H:%M:%S.%f")
        except IOError:
            self.log.error("zfs state file not found")
            return True
        except:
            import sys
            import traceback
            e = sys.exc_info()
            print(e[0], e[1], traceback.print_tb(e[2]))
            return False
        if self.skip_sync(ts):
            self.status_log("Last sync on %s older than %i minutes"%(ts, self.sync_max_delay))
            return False
        return True

    def _status(self, verbose=False):
        try:
            ls = self.get_local_state()
            now = datetime.datetime.now()
            last = datetime.datetime.strptime(ls['date'], "%Y-%m-%d %H:%M:%S.%f")
            delay = datetime.timedelta(minutes=self.sync_max_delay)
        except IOError:
            self.status_log("zfs state file not found")
            return rcStatus.WARN
        except:
            import sys
            import traceback
            e = sys.exc_info()
            print(e[0], e[1], traceback.print_tb(e[2]))
            return rcStatus.WARN
        if last < now - delay:
            self.status_log("Last sync on %s older than %i minutes"%(last, self.sync_max_delay))
            return rcStatus.WARN
        return rcStatus.UP

    def check_remote(self, node):
        rs = self.get_remote_state(node)
        if self.snap1_uuid != rs['uuid']:
            self.log.error("%s last update uuid doesn't match snap1 uuid"%(node))
            raise ex.excError

    def get_remote_state(self, node):
        self.set_statefile()
        cmd1 = ['env', 'LANG=C', 'cat', self.statefile]
        cmd = rcEnv.rsh.split() + [node] + cmd1
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            self.log.error("could not fetch %s last update uuid"%node)
            raise ex.excError
        return self.parse_statefile(out, node=node)

    def get_local_state(self):
        self.set_statefile()
        with open(self.statefile, 'r') as f:
            out = f.read()
        return self.parse_statefile(out)

    def get_snap_uuid(self, snap):
        cmd = ['zfs', 'list', '-H', '-o', 'creation', '-t', 'snapshot', snap]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError
        self.snap_uuid = out.strip()

    def set_statefile(self):
        self.statefile = os.path.join(rcEnv.pathvar,
                                      self.svc.svcname+'_'+self.rid+'_zfs_state')

    def write_statefile(self):
        self.set_statefile()
        self.get_snap_uuid(self.src_snap_sent)
        self.log.info("update state file with snap uuid %s"%self.snap_uuid)
        with open(self.statefile, 'w') as f:
             f.write(str(datetime.datetime.now())+';'+self.snap_uuid+'\n')

    def _push_statefile(self, node):
        cmd = rcEnv.rcp.split() + [self.statefile, node+':'+self.statefile.replace('#', '\#')]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def push_statefile(self, node):
        self.set_statefile()
        self._push_statefile(node)
        self.get_peersenders()
        for s in self.peersenders:
            self._push_statefile(s)

    def parse_statefile(self, out, node=None):
        self.set_statefile()
        if node is None:
            node = rcEnv.nodename
        lines = out.strip().split('\n')
        if len(lines) != 1:
            self.log.error("%s:%s is corrupted"%(node, self.statefile))
            raise ex.excError
        fields = lines[0].split(';')
        if len(fields) != 2:
            self.log.error("%s:%s is corrupted"%(node, self.statefile))
            raise ex.excError
        return dict(date=fields[0], uuid=fields[1])

  0707010000bc6e000081a400000000000000000000000154fc7bfb00000ca3000000660000000300000000000000000000002100000000root/opt/opensvc/lib/resVdisk.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

""" Module providing device path remapping for libvirt VMs
"""

import resources as Res
import rcStatus
import rcExceptions as ex
from rcGlobalEnv import rcEnv

class Vdisk(Res.Resource):
    def __init__(self,
                 rid=None,
                 name=None,
                 devpath={},
                 type=None,
                 always_on=set([]),
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        Res.Resource.__init__(self,
                              rid,
                              "disk.vdisk",
                              optional=optional,
                              disabled=disabled,
                              tags=tags,
                              monitor=monitor,
                              restart=restart,
                              subset=subset)
        self.name = name
        self.always_on = always_on
        self.disks = set()
        self.devpath = devpath

    def __str__(self):
        return "%s name=%s" % (Res.Resource.__str__(self), self.name)

    def disklist(self):
        return self.disks

    def remap(self):
        path = self.devpath[rcEnv.nodename]
        paths = set(self.devpath.values()) - set(self.devpath[rcEnv.nodename])
        from xml.etree.ElementTree import ElementTree, SubElement
        tree = ElementTree()
        try:
            tree.parse(self.svc.resources_by_id['container'].cf)
        except:
            self.log.error("failed to parse %s"%self.svc.resources_by_id['container'].cf)
            raise ex.excError
        for dev in tree.getiterator('disk'):
            s = dev.find('source')
            if s is None:
                 continue
            il = s.items()
            if len(il) != 1:
                 continue
            attr, devp = il[0]
            if devp in paths:
                self.log.info("remapping device path: %s -> %s"%(devp,path))
                s.set('dev', path)
                #SubElement(dev, "source", {'dev': path})
                tree.write(self.svc.resources_by_id['container'].cf)

    def stop(self):
        pass

    def start(self):
        self.remap()

    def _status(self, verbose=False):
        return rcStatus.NA

 0707010000bc6f000081a400000000000000000000000154fc7bfb00002618000000660000000300000000000000000000002100000000root/opt/opensvc/lib/resVgAIX.py  #
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import rcExceptions as ex
import resDg
import json
import os
import logging
import shutil

from rcGlobalEnv import rcEnv
from subprocess import *


# ajouter un dump regulier de la config des vg (pour ne pas manquer les extensions de vol)

class Vg(resDg.Dg):
    def __init__(self,
                 rid=None,
                 name=None,
                 type=None,
                 always_on=set([]),
                 dsf=True,
                 monitor=False,
                 restart=0,
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 subset=None):
        self.label = name
        self.dsf = dsf
        resDg.Dg.__init__(self,
                          rid=rid,
                          name=name,
                          type='disk.vg',
                          always_on=always_on,
                          optional=optional,
                          disabled=disabled,
                          tags=tags,
                          monitor=monitor,
                          restart=restart,
                          subset=subset)

    def has_it(self):
        """ returns True if the volume is present
        """
        if self.is_active():
            return True
        if self.is_imported():
            return True
        return False

    def is_active(self):
        cmd = [ 'lsvg', self.name ]
        process = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
        buff = process.communicate()
        if not "active" in buff[0]:
            return False
        return True

    def is_imported(self):
        cmd = ['lsvg']
        process = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
        buff = process.communicate()
        for vg in buff[0].split('\n'):
            if vg == self.name:
                return True
        return False

    def is_up(self):
        """Returns True if the volume group is present and activated
        """
        if not self.is_imported():
            return False
        if not self.is_active():
            return False
        return True

    def pvid2hdisk(self,mypvid):
        cmd = ['lspv']
        process = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
        buff = process.communicate()
        hdisk = "notfound"
        for line in buff[0].split('\n'):
            if mypvid in line:
                elem = line.split()
                #print("<%s> {%s}"%(line, elem[0]))
                return elem[0]    # first hdisk name matching requested pvid

    def dumped_pvids(self, p):
        if not os.path.exists(p):
            return []
        with open(p) as f:
            s = f.read()
        try:
            data = json.loads(s)
        except:
            return []
        l = []
        for line in data:
            pvid = line.get('pvid')
            if pvid is not None:
                l.append(pvid)
        return l

    def dump_changed(self):
        pvids1 = self.dumped_pvids(self.vgfile_name())
        pvids2 = self.dumped_pvids(self.vgimportedfile_name())
        if set(pvids1) == set(pvids2):
            return False
        return True

    def do_import(self):
        if not os.path.exists(self.vgfile_name()):
            raise ex.excError("%s should exist" % self.vgfile_name())
        if not self.dump_changed() and self.is_imported():
            self.log.info("%s is already imported" % self.name)
            return
        if self.dump_changed() and self.is_imported():
            if self.is_active():
                self.log.warning("%s is active. can't reimport." % self.name)
                return
            self.do_export()
        with open(self.vgfile_name()) as f:
            s = f.read()
        try:
            data = json.loads(s)
        except:
            raise ex.excError("%s is misformatted" % self.vgfile_name())
        self.pvids = {}
        missing = []
        for l in data:
            pvid = l.get('pvid')
            if pvid is None:
                continue
            hdisk = self.pvid2hdisk(pvid)
            self.pvids[pvid] = hdisk
            if hdisk == "notfound":
                missing.append(pvid)

        # check for missing devices
        if len(missing) > 1:
            raise ex.excError("Missing hdisks for pvids %s to be able to import vg" % ','.join(missing))
        elif len(missing) == 1:
            raise ex.excError("Missing hdisk for pvid %s to be able to import vg" % ','.join(missing))

        myhdisks = self.pvids.values()
        cmd = ['importvg', '-n', '-y', self.name, myhdisks[0]]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        shutil.copy2(self.vgfile_name(), self.vgimportedfile_name())

    def do_export(self):
        if not self.is_imported():
            self.log.info("%s is already exported" % self.name)
            return
        cmd = ['exportvg', self.name]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def do_activate(self):
        if self.is_active():
            self.log.info("%s is already available" % self.name)
            return
        cmd = ['varyonvg', self.name]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def do_deactivate(self):
        if not self.is_active():
            self.log.info("%s is already unavailable" % self.name)
            return
        cmd = ['varyoffvg', self.name]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def do_start(self):
        self.do_import()
        self.do_activate()
        self.do_dumpcfg()
        self.can_rollback = True

    def do_stop(self):
        self.do_dumpcfg()
        self.do_deactivate()

    def vgfile_name(self):
        return os.path.join(rcEnv.pathvar, self.name + '.vginfo')

    def vgimportedfile_name(self):
        return os.path.join(rcEnv.pathvar, self.name + '.vginfo.imported')

    def files_to_sync(self):
        return [self.vgfile_name()]

    def do_dumpcfg(self):
        cmd = ['lspv']
        p = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
        out, err = p.communicate()
        if p.returncode != 0:
            return
        data = []
        for line in out.split('\n'):
            l = line.split()
            n = len(l)
            h = {}
            for i, key in enumerate(['hdisk', 'pvid', 'vg', 'state']):
                if i >= n -1:
                    break
                h[key] = l[i]
            vg = h.get('vg')
            if vg is not None and vg == self.name:
                data.append(h)
        if len(data) == 0:
            # don't overwrite existing dump file with an empty dataset
            return
        s = json.dumps(data)
        with open(self.vgfile_name(), 'w') as f:
            f.write(s)
                
	"""
	root@host:/$ lspv
	hdisk0          00078e0b282e417a                    rootvg          active      
	hdisk1          none                                None                        
	hdisk2          00078e0bb1618c92                    tstvg           active      
	hdisk3          00078e0bb161b59e                    tstvg           active      
	hdisk4          none                                None                        
	hdisk5          none                                None                        

        =>

        [{'hdisk': 'hdisk0', 'pvid': '00078e0b282e417a', 'vg': 'rootvg', 'state': 'active'},
         {'hdisk': 'hdisk1', 'pvid': 'none', 'vg': 'None'},
         {'hdisk': 'hdisk2', 'pvid': '00078e0bb1618c92', 'vg': 'testvg', 'state': 'active'},
         {'hdisk': 'hdisk3', 'pvid': '00078e0bb161b59e', 'vg': 'testvg', 'state': 'active'},
         {'hdisk': 'hdisk4', 'pvid': 'none', 'vg': 'None'},
         {'hdisk': 'hdisk5', 'pvid': 'none', 'vg': 'None'}]
	"""
    
    def disklist(self):
        if self.is_active():
            return self.disklist_active()
        return self.disklist_inactive()

    def disklist_active(self):
        cmd = ['lsvg', '-p', self.name]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError

        for e in out.split('\n'):
            x = e.split()
            if len(x) != 5:
                continue
            self.disks |= set([x[0]])

        return self.disks

    def disklist_inactive(self):
        self.disks = set([])
        if not os.path.exists(self.vgfile_name()):
            return self.disks
        with open(self.vgfile_name()) as f:
            s = f.read()
        try:
            data = json.loads(s)
        except:
            return self.disks
        for l in data:
            pvid = l.get('pvid')
            if pvid is None:
                continue
            hdisk = self.pvid2hdisk(pvid)
            if hdisk == "notfound":
                continue
            self.disks.add(hdisk)
        return self.disks

0707010000bc70000081a400000000000000000000000154fc7bfb00001132000000660000000300000000000000000000002300000000root/opt/opensvc/lib/resVgAdvfs.py    #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.
"""Module providing ZFS resources
"""

from rcGlobalEnv import rcEnv
import resDg
from rcUtilities import justcall
import os
import rcExceptions as ex

import re

class Vg(resDg.Dg):
    def __init__(self,
                 rid=None,
                 name=None,
                 type=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 always_on=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        self.label = 'fdmn ' + name
        resDg.Dg.__init__(self,
                          rid=rid,
                          name=name,
                          type='disk.vg',
                          always_on=always_on,
                          optional=optional,
                          disabled=disabled,
                          tags=tags,
                          monitor=monitor,
                          restart=restart,
                          subset=subset)

    def disklist_name(self):
        return os.path.join(rcEnv.pathvar, 'vg_' + self.svc.svcname + '_' + self.name + '.disklist')

    def files_to_sync(self):
        return [self.disklist_name()]

    def presync(self):
        """ this one is exported as a service command line arg
        """
        dl = self._disklist()
        import json
        with open(self.disklist_name(), 'w') as f:
            f.write(json.dumps(list(dl)))

    def has_it(self):
        """Returns True if the pool is present
        """
        if os.path.exists("/etc/fdmns/"+self.name):
            return True
        return False

    def is_up(self):
        """Returns True if the fdmn is present and activated
        """
        if not self.has_it():
            return False
        cmd = [ 'showfdmn', self.name ]
        out, err, ret = justcall(cmd)
        if ret != 0:
            if len(err) > 0:
                self.status_log(err)
            return False
        if 'not active' in out:
            return False
        return True

    def do_start(self):
        pass

    def do_stop(self):
        pass

    def disklist(self):
        if not os.path.exists(self.disklist_name()):
            s = self.svc.group_status(excluded_groups=set(["sync", "hb"]))
            import rcStatus
            if s['overall'].status == rcStatus.UP:
                self.log.debug("no disklist cache file and service up ... refresh disklist cache")
                self.presync()
            else:
                self.log.debug("no disklist cache file and service not up ... unable to evaluate disklist")
                return set([])
        with open(self.disklist_name(), 'r') as f:
            buff = f.read()
        import json
        try:
            dl = set(json.loads(buff))
        except:
            self.log.error("corrupted disklist cache file %s"%self.disklist_name())
            raise ex.excError
        return dl


    def _disklist(self):
        # return cache if initialized
        if len(self.disks) > 0 :
            return self.disks

        disks = set([])
        if not os.path.exists("/etc/fdmns/"+self.name):
            return disks

        import glob
        dl = glob.glob("/etc/fdmns/"+self.name+"/*")
        dl = map(lambda x: os.readlink(x), dl)
        self.disks = set(dl)

        self.log.debug("found disks %s held by pool %s" % (disks, self.name))
        return self.disks

if __name__ == "__main__":
    p=Vg(name="dom1")
    print p._disklist()
  0707010000bc71000081a400000000000000000000000154fc7bfb00001d8a000000660000000300000000000000000000002300000000root/opt/opensvc/lib/resVgGandi.py    #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import resDg
import os
import rcStatus
import re
import pwd
import grp
import stat
from rcGlobalEnv import rcEnv
import rcExceptions as ex

class Vg(resDg.Dg):
    def __init__(self,
                 rid=None,
                 name=None,
                 node=None,
                 cloud_id=None,
                 user="root",
                 group="root",
                 perm="660",
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 always_on=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        self.label = "gandi volume %s"%str(name)
        resDg.Dg.__init__(self,
                          rid=rid,
                          name="gandi",
                          type='disk.gandi',
                          always_on=always_on,
                          optional=optional,
                          disabled=disabled,
                          tags=tags,
                          monitor=monitor,
                          restart=restart,
                          subset=subset)

        self.name = name
        self.node = node
        self.cloud_id = cloud_id
        self.user = user
        self.group = group
        self.perm = perm
        
        self.get_uid()
        self.get_gid()

    def print_obj(self, n):
        for k in dir(n):
            if '__' in k:
                continue
            print(k, "=", getattr(n, k))

    def get_cloud(self):
        if hasattr(self, 'cloud'):
            return self.cloud
        try:
            self.cloud = self.svc.node.cloud_get(self.cloud_id)
        except ex.excInitError as e:
            raise ex.excError(str(e))
        return self.cloud

    def get_uid(self):
        self.uid = self.user
        if isinstance(self.uid, (str, unicode)):
            try:
                info=pwd.getpwnam(self.uid)
                self.uid = info[2]
            except:
                pass

    def get_gid(self):
        self.gid = self.group
        if isinstance(self.gid, (str, unicode)):
            try:
                info=grp.getgrnam(self.gid)
                self.gid = info[2]
            except:
                pass

    def check_uid(self, rdev, verbose=False):
        if not os.path.exists(rdev):
            return True
        uid = os.stat(rdev).st_uid
        if uid != self.uid:
            if verbose:
                self.status_log('%s uid should be %d but is %d'%(rdev, self.uid, uid))
            return False
        return True

    def check_gid(self, rdev, verbose=False):
        if not os.path.exists(rdev):
            return True
        gid = os.stat(rdev).st_gid
        if gid != self.gid:
            if verbose:
                self.status_log('%s gid should be %d but is %d'%(rdev, self.gid, gid))
            return False
        return True

    def check_perm(self, rdev, verbose=False):
        if not os.path.exists(rdev):
            return True
        try:
            perm = oct(stat.S_IMODE(os.stat(rdev).st_mode))
        except:
            self.log.error('%s can not stat file'%rdev)
            return False
        perm = str(perm).lstrip("0")
        if perm != str(self.perm):
            if verbose:
                self.status_log('%s perm should be %s but is %s'%(rdev, str(self.perm), perm))
            return False
        return True

    def has_it(self):
        """Returns True if all devices are present
        """
        try:
            node = self.get_node()
        except ex.excError as e:
            raise ex.excError("can't find cloud node to list volumes (%s)"%str(e))

        c = self.get_cloud()
        disks = c.driver._node_info(node.id)['disks']
        for disk in disks:
            if disk['name'] == self.name:
                return True
        return False

    def is_up(self):
        """Returns True if the volume group is present and activated
        """
        return self.has_it()

    def _status(self, verbose=False):
        try:
            s = self.is_up()
        except ex.excError as e:
            self.status_log(str(e))
            return rcStatus.WARN
        if rcEnv.nodename in self.always_on:
            if s:
                return rcStatus.STDBY_UP
            else:
                return rcStatus.STDBY_DOWN
        else:
            if s:
                return rcStatus.UP
            else:
                return rcStatus.DOWN

    def get_node(self):
        c = self.get_cloud()
        if self.node is not None:
            n = self.node
        else:
            n = rcEnv.nodename
        try:
            nodes = c.driver.list_nodes()
        except Exception as e:
            raise ex.excError(str(e))
        for node in nodes:
            if node.name == n:
                return node
        raise ex.excError()

    def get_disk(self):
        c = self.get_cloud()
        disks = c.driver.ex_list_disks()
        _disk = None
        for disk in disks:
            if disk.name == self.name:
                _disk = disk
        if _disk is None:
            raise ex.excError()
        return _disk
 
    def do_start(self):
        try:
            node = self.get_node()
        except ex.excError as e:
            raise ex.excError("can't find cloud node to attach volume %s to (%s)"%(self.name, str(e)))

        try:
            disk = self.get_disk()
        except:
            raise ex.excError("volume %s not found in %s"%(self.name, self.cloud_id))

        try:
            status = self.is_up()
        except ex.excError as e:
            self.log.error("abort gandi volume %s attach: %s"%(self.name, str(e)))

        if status:
            self.log.info("gandi volume %s is already attached"%self.name)
            return

        self.log.info("attach gandi volume %s"%self.name)
        c = self.get_cloud()
        c.driver.ex_node_attach_disk(node, disk)
        self.can_rollback = True

    def do_stop(self):
        try:
            node = self.get_node()
        except ex.excError as e:
            raise ex.excError("can't find cloud node to detach volume %s from"%(self.name, str(e)))

        try:
            disk = self.get_disk()
        except:
            raise ex.excError("volume %s not found in %s"%(self.name, self.cloud_id))

        try:
            status = self.is_up()
        except ex.excError as e:
            self.log.error("abort gandi volume %s detach: %s"%(self.name, str(e)))

        if not status:
            self.log.info("gandi volume %s is already detached"%self.name)
            return

        self.log.info("detach gandi volume %s"%self.name)
        c = self.get_cloud()
        c.driver.ex_node_detach_disk(node, disk)

    def shutdown(self):
        pass

    def disklist(self):
        return []
  0707010000bc72000081a400000000000000000000000154fc7bfb000033a0000000660000000300000000000000000000002300000000root/opt/opensvc/lib/resVgHP-UX.py    #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import re
import os
import rcExceptions as ex
import resDg
from subprocess import *
from rcUtilities import qcall
from rcGlobalEnv import rcEnv

class Vg(resDg.Dg):
    def __init__(self,
                 rid=None,
                 name=None,
                 type=None,
                 always_on=set([]),
                 dsf=True,
                 disabled=False,
                 tags=set([]),
                 optional=False,
                 monitor=False,
                 restart=0,
                 subset=None):
        self.label = name
        self.dsf = dsf
        resDg.Dg.__init__(self,
                          rid=rid,
                          name=name,
                          type='disk.vg',
                          always_on=always_on,
                          optional=optional,
                          disabled=disabled,
                          tags=tags,
                          monitor=monitor,
                          restart=restart,
                          subset=subset)

    def is_child_dev(self, device):
        l = device.split("/")
        if len(l) != 4 or l[1] != "dev":
            return False
        vgname = l[2]
        if vgname == self.name:
            return True
        return False

    def files_to_sync(self):
        return [self.mapfile_name(), self.mkfsfile_name()]

    def mapfile_name(self):
        return os.path.join(rcEnv.pathvar, 'vg_' + self.svc.svcname + '_' + self.name + '.map')

    def mkfsfile_name(self):
        return os.path.join(rcEnv.pathvar, 'vg_' + self.svc.svcname + '_' + self.name + '.mksf')

    def has_it(self):
        """ returns True if the volume is present
        """
        if self.is_active():
            return True
        if not os.path.exists(self.mapfile_name()):
            return False
        if self.is_imported():
            return True
        return False

    def dev2char(self, dev):
        dev = dev.replace("/dev/disk", "/dev/rdisk")
        dev = dev.replace("/dev/dsk", "/dev/rdsk")
        return dev

    def dsf_name(self, dev):
        cmd = ['scsimgr', 'get_attr', '-D', self.dev2char(dev), '-a', 'device_file', '-p']
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError
        return out.split()[0]

    def write_mksf(self):
        cmd = ['ioscan', '-F', '-m', 'dsf']
        (ret, buff, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError
        if len(buff) == 0:
            return
        mksf = {}
        if len(self.disks) == 0:
            self.disks = self.disklist()
        dsf_names = map(self.dsf_name, self.disks)
        with open(self.mkfsfile_name(), 'w') as f:
            for line in buff.split('\n'):
                if len(line) == 0:
                    return
                a = line.split(':')[0]
                if '/dev/pt/pt' not in a and '/dev/rdisk/disk' not in a and not a.endswith(".pt") and self.dsf_name(a) in dsf_names:
                    cmd = ['scsimgr', 'get_attr', '-D', self.dev2char(a), '-a', 'wwid', '-p']
                    (ret, out, err) = self.call(cmd)
                    if ret != 0:
                        raise ex.excError
                    f.write(":".join([a, out.split()[0].replace('0x', '')])+'\n')

    def do_mksf(self):
        if not os.path.exists(self.mkfsfile_name()):
            return

        instance = {}
        cmd = ['scsimgr', 'get_attr', 'all_lun', '-a', 'wwid', '-a', 'instance', '-p']
        (ret, buff, err) = self.call(cmd)
        for line in buff.split('\n'):
            l = line.split(':')
            if len(l) != 2:
                continue
            instance[l[0].replace('0x', '')] = l[1]

        r = 0
        with open(self.mkfsfile_name(), 'r') as f:
            for line in f.readlines():
                a = line.replace('\n', '').split(':')
                if len(a) == 0:
                    continue
                if os.path.exists(a[0]):
                    continue
                if a[1] not in instance.keys():
                    self.log.error("expected lun %s not present on node %s"%(a[1], rcEnv.nodename))
                    r += 1
                    continue
                cmd = ['mksf', '-r', '-C', 'disk', '-I', instance[a[1]], a[0]]
                (ret, buff, err) = self.vcall(cmd)
                if ret != 0:
                    r += 1
                    continue
        if r > 0:
            raise ex.excError

    def presync(self):
        """ this one is exported as a service command line arg
        """
        cmd = [ 'vgexport', '-m', self.mapfile_name(), '-p', '-s', self.name ]
        ret = qcall(cmd)
        if ret != 0:
            raise ex.excError
        self.write_mksf()

    def is_active(self):
        cmd = [ 'vgdisplay', self.name ]
        process = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
        buff = process.communicate()
        if not "available" in buff[0]:
            return False
        return True

    def is_imported(self):
        r = self.is_imported_lvm2()
        if r:
            return True
        return self.is_imported_lvm1()

    def is_imported_lvm2(self):
        if not os.path.exists('/etc/lvmtab_p'):
            return False
        cmd = ['strings', '/etc/lvmtab_p']
        process = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
        out, err = process.communicate()
        l = out.split('\n')
        map(lambda x: x.strip(), l)
        s = '/dev/'+self.name
        if s in l:
            return True
        return False

    def is_imported_lvm1(self):
        if not os.path.exists('/etc/lvmtab'):
            return False
        cmd = ['strings', '/etc/lvmtab']
        process = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
        out, err = process.communicate()
        l = out.split('\n')
        map(lambda x: x.strip(), l)
        s = '/dev/'+self.name
        if s in l:
            return True
        return False

    def is_up(self):
        """Returns True if the volume group is present and activated
        """
        if not os.path.exists(self.mapfile_name()):
            try:
                self.do_export(force_preview=True)
            except ex.excError:
                # vg does not exist
                return False
        if not self.is_imported():
            return False
        if not self.is_active():
            return False
        return True

    def clean_group(self):
        gp = os.path.join(os.sep, "dev", self.name, "group")
        if not os.path.exists(gp):
            return
        cmd = ["rmsf", gp]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            self.log.error("failed to remove pre-existing %s"%gp)
            raise ex.excError

    def do_import(self):
        if self.is_imported():
            self.log.info("%s is already imported" % self.name)
            return
        if self.dsf:
            dsfflag = '-N'
        else:
            dsfflag = ''
        self.lock()
        self.clean_group()
        cmd = [ 'vgimport', '-m', self.mapfile_name(), '-s', dsfflag, self.name ]
        self.log.info(' '.join(cmd))
        process = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
        buff = process.communicate()
        self.unlock()

        # we will modify buff[1], so convert from tuple to list
        buff = list(buff)

        # test string for warnings
        #buff[1] = """Warning: Cannot determine block size of Physical Volume "/dev/rdisk/disk394".
        #Assuming a default value of 1024 bytes. Continuing.
        #Warning: Cannot determine block size of Physical Volume "/dev/rdisk/disk395".
        #Assuming a default value of 1024 bytes. Continuing.
        #vgimport:"""

        if len(buff[1]) > 0:
            import re
            regex = re.compile("Warning:.*\n.*Continuing.\n", re.MULTILINE)
            w = regex.findall(buff[1])
            if len(w) > 0:
                warnings = '\n'.join(w)
                self.log.warning(warnings)
                buff[1] = regex.sub('', buff[1])
            if buff[1] != "vgimport: " and buff[1] != "vgimport:":
                self.log.error('error:\n' + buff[1])

        if len(buff[0]) > 0:
            self.log.debug('output:\n' + buff[0])

        if process.returncode != 0:
            raise ex.excError

    def do_export(self, force_preview=False):
        preview = False
        if os.path.exists(self.mapfile_name()):
            if not self.is_imported():
                self.log.info("%s is already exported" % self.name)
                return
        elif self.is_active():
            preview = True
        if preview or force_preview:
            cmd = [ 'vgexport', '-p', '-m', self.mapfile_name(), '-s', self.name ]
        else:
            cmd = [ 'vgexport', '-m', self.mapfile_name(), '-s', self.name ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def do_activate(self):
        if self.is_active():
            self.log.info("%s is already available" % self.name)
            return
        cmd = ['vgchange', '-c', 'n', self.name]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError
        cmd = ['vgchange', '-a', 'y', self.name]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def do_deactivate(self):
        if not self.is_active():
            self.log.info("%s is already unavailable" % self.name)
            return
        cmd = ['vgchange', '-a', 'n', self.name]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def do_start(self):
        self.do_import()
        self.do_activate()

    def do_stop(self):
        self.do_deactivate()
        self.do_export()

    def start(self):
        self.do_mksf()
        self.can_rollback = True
        self.do_start()

    def disklist(self):
        need_export = False
        if not self.is_active() and not self.is_imported():
            self.do_import()
            need_export = True

        self.disks = set([])
        if os.path.exists('/etc/lvmtab'):
            self.disks |= self._disklist('/etc/lvmtab')
        if os.path.exists('/etc/lvmtab_p'):
            self.disks |= self._disklist('/etc/lvmtab_p')

        if need_export:
            self.do_export()

        return self.disks

    def _disklist(self, tabp):
        cmd = ['strings', tabp]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError

        tab = out.split('\n')
        insection = False
        disks = set([])
        for e in tab:
            """ move to the first disk of the vg
            """
            if e == "/dev/" + self.name:
                insection = True
                continue
            if not insection:
                continue
            if not  e.startswith('/dev/'):
                continue
            if not e.startswith('/dev/disk') and not e.startswith('/dev/dsk'):
                break
            disks |= set([e])

        return disks

    def lock(self, timeout=30, delay=1):
        import lock
        lockfile = os.path.join(rcEnv.pathlock, 'vgimport')
        lockfd = None
        try:
            lockfd = lock.lock(timeout=timeout, delay=delay, lockfile=lockfile)
        except lock.lockTimeout:
            self.log.error("timed out waiting for lock (%s)"%lockfile)
            raise ex.excError
        except lock.lockNoLockFile:
            self.log.error("lock_nowait: set the 'lockfile' param")
            raise ex.excError
        except lock.lockCreateError:
            self.log.error("can not create lock file %s"%lockfile)
            raise ex.excError
        except lock.lockAcquire as e:
            self.log.warn("another action is currently running (pid=%s)"%e.pid)
            raise ex.excError
        except ex.excSignal:
            self.log.error("interrupted by signal")
            raise ex.excError
        except:
            self.log.error("unexpected locking error")
            import traceback
            traceback.print_exc()
            raise ex.excError
        self.lockfd = lockfd

    def unlock(self):
        import lock
        lock.unlock(self.lockfd)

    def provision(self):
        m = __import__("provVgHP-UX")
        prov = getattr(m, "ProvisioningVg")(self)
        prov.provisioner()


0707010000bc73000081a400000000000000000000000154fc7bfb00001610000000660000000300000000000000000000002200000000root/opt/opensvc/lib/resVgHpVm.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import re
import os
import rcExceptions as ex
import rcStatus
resVg = __import__("resVgHP-UX")
from subprocess import *
from rcUtilities import qcall
from rcGlobalEnv import rcEnv
from subprocess import *

class Vg(resVg.Vg):
    def __init__(self,
                 rid=None,
                 name=None,
                 container_name=None,
                 type=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        self.label = name
        self.container_name = container_name
        resVg.Vg.__init__(self,
                          rid=rid,
                          name=name,
                          type='disk.vg',
                          monitor=monitor,
                          restart=restart,
                          optional=optional,
                          disabled=disabled,
                          tags=tags,
                          subset=subset)

    def has_it(self):
        return True

    def is_up(self):
        return True

    def _status(self, verbose=False):
        return rcStatus.NA

    def do_start(self):
        self.do_mksf()

    def do_stop(self):
        pass

    def files_to_sync(self):
        return [self.sharefile_name(), self.mkfsfile_name()]

    def postsync(self):
        s = self.svc.group_status(excluded_groups=set(["sync", "hb"]))
        if s['overall'].status != rcStatus.UP:
            self.do_mksf()
            self.do_share()

    def presync(self):
        s = self.svc.group_status(excluded_groups=set(["sync", "hb"]))
        if self.svc.force or s['overall'].status == rcStatus.UP:
            self.write_mksf()
            self.write_share()

    def sharefile_name(self):
        return os.path.join(rcEnv.pathvar, 'vg_' + self.svc.svcname + '_' + self.name + '.share')

    def get_devs(self):
        cmd = ['/opt/hpvm/bin/hpvmdevmgmt', '-l', 'all']
        (ret, buff, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError
        if len(buff) == 0:
            return []
        a = {}
        for line in buff.split('\n'):
            if len(line) == 0:
                continue
            if "DEVTYPE=FILE" not in line and "DEVTYPE=DISK" not in line:
                continue
            if "SHARE=YES" in line:
                share = "YES"
            else:
                share = "NO"
            devs = line.split(":")[0]
            for dev in devs.split(","):
                a[dev] = {'share': share}
        return a

    def write_share(self):
        devs = self.get_devs()
        disklist = self.disklist()
        with open(self.sharefile_name(), 'w') as f:
            for dev in devs:
                if dev not in disklist:
                    continue
                f.write("%s:%s\n"%(dev, devs[dev]['share']))

    def do_share(self):
        if not os.path.exists(self.sharefile_name()):
            return
        devs = self.get_devs()
        errors = 0
        with open(self.sharefile_name(), 'r') as f:
            for line in f.readlines():
                l = line.split(':')
                if len(l) != 2:
                    continue
                dev = l[0]
                share = l[1].strip()
                if len(dev) == 0:
                    continue
                if not os.path.exists(dev):
                    continue
                if dev not in devs:
                    cmd = ['/opt/hpvm/bin/hpvmdevmgmt', '-a', 'gdev:'+dev]
                    (ret, out, err) = self.vcall(cmd)
                    if ret != 0:
                        self.log.error("error adding device %s hpvm device table"%dev)
                        raise ex.excError
                if dev in devs and share == devs[dev]['share']:
                    self.log.debug("skip set sharing of %s: already set to %s"%(dev, devs[dev]['share']))
                    continue
                cmd = ['/opt/hpvm/bin/hpvmdevmgmt', '-m', 'gdev:'+dev+':attr:SHARE='+share]
                (ret, buff, err) = self.vcall(cmd)
                if ret != 0:
                    self.log.error("error setting the shared attribute for %s"%dev)
                    errors += 1
                    continue
        if errors > 0:
            raise ex.excError

    def disklist(self):
        cmd = ['/opt/hpvm/bin/hpvmstatus', '-d', '-P', self.container_name]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
        buff = p.communicate()
        if p.returncode != 0:
            raise ex.excError

        for line in buff[0].split('\n'):
            l = line.split(':')
            if len(l) < 5:
                continue
            if l[3] != 'disk':
                continue
            self.disks |= set([l[4]])
        return self.disks
0707010000bc74000081a400000000000000000000000154fc7bfb000011d8000000660000000300000000000000000000002200000000root/opt/opensvc/lib/resVgLdom.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import rcExceptions as ex
import rcStatus
resVg = __import__("resDg")
from subprocess import *
import re

class Vg(resVg.Dg):
    def __init__(self,
                 rid=None,
                 name=None,
                 container_id=None,
                 type=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        self.label = name
        self.container_id = container_id
        resVg.Dg.__init__(self,
                          rid=rid,
                          name=name,
                          type='disk.vg',
                          optional=optional,
                          disabled=disabled,
                          tags=tags,
                          monitor=monitor,
                          restart=restart,
                          subset=subset)

    def has_it(self):
        return True

    def is_up(self):
        return True

    def _status(self, verbose=False):
        return rcStatus.NA

    def do_start(self):
        pass

    def do_stop(self):
        pass

    def disklist(self):
        """ VCC|name=vccname|...
            VDS|name=vdsname|...
            |vol=volname|..|dev=/dev/...|....
            |vol=volname1|..|dev=/dev/...|....
            VDS|name=vdsname1|..
            |vol=volname2|..|dev=/dev/...|....

            ldm list -o disk -p domname
            VERSION
            DOMAIN|..
            VDISK|name=...|vol=volname@vds|...
            VDISK|name=...|vol=volname2@vds2|...
            """
        vdevname2dev = {}

        cmd = [ '/usr/sbin/ldm', 'list-services' , '-p' ]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
        buff = p.communicate()
        if p.returncode != 0:
            raise ex.excError
        vds = ''
        for line in buff[0].split('\n'):
            keys = line.split('|')
            if keys[0] == 'VDS' and len(keys) > 1 :
                for k in keys :
                    name_value = k.split('=')
                    if name_value[0] == 'name' and len(name_value) == 2 :
                        vds = name_value[1]
            elif vds != '' and keys[0] == '':
                volname = ''
                dev = ''
                for k in keys :
                    name_value = k.split('=')
                    if name_value[0] == 'vol' and len(name_value) == 2 :
                        volname = name_value[1]
                    elif name_value[0] == 'dev' and len(name_value) == 2 :
                        dev = name_value[1]
                        if re.match('^/dev/dsk/', dev) is None:
                            continue
                        dev = dev.replace('/dev/dsk/','/dev/rdsk/',1)
                        if re.match('^.*s[0-9]$',dev) is None:
                            dev = dev + 's2'
                        vdevname2dev[volname + '@' + vds ] = dev
            else:
                vds = ''

        cmd = ['/usr/sbin/ldm', 'list', '-o', 'disk', '-p', self.svc.resources_by_id[self.container_id].name]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
        buff = p.communicate()
        if p.returncode != 0:
            raise ex.excError
        for line in buff[0].split('\n'):
            keys = line.split('|')
            if keys[0] == 'VDISK' and len(keys) > 1 :
                for k in keys :
                    name_value = k.split('=')
                    if name_value[0] == 'vol' and len(name_value) == 2 :
                        vol = name_value[1]
                        if vdevname2dev.has_key(vol):
                            self.disks |= set([ vdevname2dev[vol] ])
        return self.disks
0707010000bc75000081a400000000000000000000000154fc7bfb00001ab5000000660000000300000000000000000000002300000000root/opt/opensvc/lib/resVgLinux.py    #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import re
import os
import rcExceptions as ex
import resDg
from rcGlobalEnv import rcEnv
from rcUtilitiesLinux import major, get_blockdev_sd_slaves, \
                             devs_to_disks
from rcUtilities import which

class Vg(resDg.Dg):
    def __init__(self,
                 rid=None,
                 name=None,
                 type=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 always_on=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        self.label = name
        self.tag = '@'+rcEnv.nodename
        resDg.Dg.__init__(self,
                          rid=rid,
                          name=name,
                          type='disk.vg',
                          always_on=always_on,
                          optional=optional,
                          disabled=disabled,
                          tags=tags,
                          monitor=monitor,
                          restart=restart,
                          subset=subset)

    def is_child_dev(self, device):
        l = device.split("/")
        if len(l) != 4 or l[1] != "dev":
            return False
        if l[2] == "mapper":
            dmname = l[3]
            if "-" not in dmname:
                return False
            i = 0
            dmname.replace("--", "#")
            _l = dmname.split("-")
            if len(_l) != 2:
                return False
            vgname = _l[0].replace("#", "-")
        else:
            vgname = l[2]
        if vgname == self.name:
            return True
        return False  

    def has_it(self):
        """Returns True if the volume is present
        """
        cmd = ['vgdisplay', self.name]
        (ret, out, err) = self.call(cmd, cache=True, errlog=False)
        if ret == 0:
            return True
        return False

    def is_up(self):
        """Returns True if the volume group is present and activated
        """
        if not self.has_it():
            return False
        cmd = [ 'lvs', '--noheadings', '-o', 'lv_attr', self.name ]
        (ret, out, err) = self.call(cmd)
        if len(out) == 0 and ret == 0:
            # no lv ... happens in provisioning, where lv are not created yet
            return True
        if re.search(' ....a.', out, re.MULTILINE) is not None:
            return True
        return False

    def remove_tag(self, tag):
        cmd = [ 'vgchange', '--deltag', '@'+tag, self.name ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def remove_tags(self, tags=[]):
        cmd = ['vgs', '-o', 'tags', '--noheadings', self.name]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError
        out = out.strip(' \n')
        curtags = out.split(',')
        if len(tags) > 0:
            """ remove only specified tags
            """
            for tag in tags:
                tag = tag.lstrip('@')
                if tag in curtags:
                   self.remove_tag(tag)
        else:
            """ remove all tags
            """
            for tag in curtags:
                if len(tag) == 0:
                    continue
                self.remove_tag(tag)

    def add_tags(self):
        cmd = [ 'vgchange', '--addtag', self.tag, self.name ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def do_start(self):
        if self.is_up():
            self.log.info("%s is already up" % self.name)
            return 0
        self.remove_tags()
        self.add_tags()
        self.can_rollback = True
        cmd = [ 'vgchange', '-a', 'y', self.name ]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

    def remove_parts(self):
        if not which('partx'):
            return
        cmd = ['lvs', '-o', 'name', '--noheadings', self.name]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            self.log.error("can not fetch logical volume list from %s"%self.name)
            return
        base_cmd = ['kpartx', '-d']
        for lv in out.split():
             self.vcall(base_cmd+[os.path.join(os.sep, 'dev', self.name, lv)])

    def do_stop(self):
        if not self.is_up():
            self.log.info("%s is already down" % self.name)
            return
        self.remove_tags([self.tag])
        self.remove_parts()
        cmd = [ 'vgchange', '-a', 'n', self.name ]
        (ret, out, err) = self.vcall(cmd, err_to_info=True)
        if ret == 0:
            return

        import time
        for i in range(3, 0, -1):
            if self.is_up() and i > 0:
                time.sleep(1)
                (ret, out, err) = self.vcall(cmd, err_to_info=True)
                if ret == 0:
                    return
                continue
            break
        if i == 0:
            self.log.error("deactivation failed to release all logical volumes")
            raise ex.excError

    def devlist(self):
        if not self.has_it():
            return set()
        if self.devs != set():
            return self.devs

        self.devs = set()

        cmd = ['vgs', '--noheadings', '-o', 'pv_name', self.name]
        (ret, out, err) = self.call(cmd, cache=True)
        if ret != 0:
            return self.devs
        self.devs = set(out.split())
        self.log.debug("found devs %s held by vg %s" % (self.devs, self.name))
        return self.devs

    def disklist(self):
        if not self.has_it():
            return set()
        if self.disks != set():
            return self.disks

        self.disks = set()

        pvs = self.devlist()
        self.disks = devs_to_disks(self, pvs)
        self.log.debug("found disks %s held by vg %s" % (self.disks, self.name))
        return self.disks

    def provision(self):
        m = __import__("provVgLinux")
        prov = getattr(m, "ProvisioningVg")(self)
        prov.provisioner()

   0707010000bc76000081a400000000000000000000000154fc7bfb000026cc000000660000000300000000000000000000002800000000root/opt/opensvc/lib/resVgRadosLinux.py   #
# Copyright (c) 2014 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import resDg
import os
import rcStatus
import rcExceptions as ex
import json
from rcGlobalEnv import *
from rcUtilities import justcall

class Vg(resDg.Dg):
    def __init__(self,
                 rid=None,
                 type="disk.vg",
                 images=set([]),
                 client_id=None,
                 keyring=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 always_on=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        
        resDg.Dg.__init__(self,
                          rid=rid,
                          type=type,
                          optional=optional,
                          disabled=disabled,
                          tags=tags,
                          always_on=always_on,
                          monitor=monitor,
                          restart=restart,
                          subset=subset)

        self.images = images
        self.keyring = keyring
        if not client_id.startswith("client."):
            client_id = "client."+client_id
        self.client_id = client_id
        self.label = self.fmt_label()
        self.modprobe_done = False

    def validate_image_fmt(self):
        l = []
        for image in self.images:
            if "/" not in image:
                l.append(image)
        if len(l):
            raise ex.excError("wrong format (expected pool/image): "+", ".join(l))
        
    def fmt_label(self):
        s = "rados images: "
        s += ", ".join(self.images)
        return s

    def modprobe(self):
        if self.modprobe_done:
            return
        cmd = ["lsmod"]
        ret, out, err = self.call(cmd)
        if ret != 0:
            raise ex.excError("lsmod failed")
        if "rbd" in out.split():
            # no need to load (already loaded or compiled-in)
            return
        cmd = ["modprobe", "rbd"]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError("failed to load rbd device driver")
        self.modprobe_done = True

    def showmapped(self, refresh=False):
        if not refresh and hasattr(self, "mapped_data"):
            return self.mapped_data
        self.modprobe()
        cmd = ["rbd", "showmapped", "--format", "json"]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("rbd showmapped failed: "+err)
        try:
            _data = json.loads(out)
        except Exception as e:
            raise ex.excError(str(e))
        data = {}
        for id, img_data in _data.items():
            data[img_data["pool"]+"/"+img_data["name"]] = img_data
        self.mapped_data = data
        return data

    def rbd_rcmd(self):
        l = ["rbd", "-n", self.client_id]
        if self.keyring:
            l += ["--keyring", self.keyring]
        return l

    def exists(self, image):
        cmd = self.rbd_rcmd()+["info", image]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return False
        return True

    def has_it(self, image):
        mapped = self.showmapped()
        if image in mapped:
            return True
        return False

    def up_count(self):
        mapped = self.showmapped()
        l = []
        for image in self.images:
            if image in mapped:
                l.append(image)
        return l

    def _status(self, verbose=False):
        try:
            self.validate_image_fmt()
        except Exception as e:
            self.status_log(str(e))
            return rcStatus.WARN
        l = self.up_count()
        n = len(l)
        unmapped = sorted(list(set(self.images) - set(l)))
        if n == len(self.images):
            if rcEnv.nodename in self.always_on:
                return rcStatus.STDBY_UP
            return rcStatus.UP
        elif n == 0:
            if rcEnv.nodename in self.always_on:
                return rcStatus.STDBY_DOWN
            return rcStatus.DOWN
        else:
            self.status_log("unmapped: "+", ".join(unmapped))
            return rcStatus.WARN

    def devname(self, image):
        return os.path.join(os.sep, "dev", "rbd", image)

    def do_start_one(self, image):
        mapped = self.showmapped()
        if image in mapped:
            self.log.info(image+" is already mapped")
            return
        cmd = self.rbd_rcmd()+["map", image]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError("failed to map %s"%self.devname(image))

    def do_start(self):
        self.validate_image_fmt()
        for image in self.images:
            self.do_start_one(image)
            self.can_rollback = True
        self.showmapped(refresh=True)

    def do_stop_one(self, image):
        mapped = self.showmapped()
        if image not in mapped:
            self.log.info(image+" is already unmapped")
            return
        cmd = ["rbd", "unmap", self.devname(image)]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError("failed to unmap %s"%self.devname(image))


    def do_stop(self):
        self.validate_image_fmt()
        for image in self.images:
            self.do_stop_one(image)
        self.showmapped(refresh=True)

    def disklist(self):
        l = set([])
        for image in self.images:
            s = ".".join(("rbd", image.replace("/", ".")))
            l.add(s)
        return l

    def devlist(self):
        l = set([])
        for image in self.images:
            s = self.devname(image)
            s = os.path.realpath(s)
            l.add(s)
        return l

    def provision(self):
        m = __import__("provVgRadosLinux")
        prov = getattr(m, "ProvisioningVg")(self)
        prov.provisioner()

class VgLock(Vg):
    def __init__(self,
                 rid=None,
                 type="disk.lock",
                 images=set([]),
                 client_id=None,
                 keyring=None,
                 lock=None,
                 lock_shared_tag=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 always_on=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        
        self.lock = lock
        self.lock_shared_tag = lock_shared_tag

        Vg.__init__(self,
                    rid=rid,
                    type=type,
                    images=images,
                    client_id=client_id,
                    keyring=keyring,
                    optional=optional,
                    disabled=disabled,
                    tags=tags,
                    always_on=always_on,
                    monitor=monitor,
                    restart=restart,
                    subset=subset)

        self.label = self.fmt_label()
        self.unlocked = []


    def fmt_label(self):
        return str(self.lock) + " lock on " + Vg.fmt_label(self)

    def locklist(self, image):
        if not self.has_it(image):
            return {}
        cmd = self.rbd_rcmd()+["lock", "list", image, "--format", "json"]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError("rbd lock list failed")
        data = {}
        try:
            data = json.loads(out)
        except Exception as e:
            raise ex.excError(str(e))
        return data


    def has_lock(self, image):
        data = self.locklist(image)
        if rcEnv.nodename in data:
            return True
        self.unlocked.append(image)
        return False

    def up_count(self):
        n = 0
        for image in self.images:
            if self.has_lock(image):
                n += 1
        return n

    def _status(self, verbose=False):
        n = self.up_count()
        if n == len(self.images):
            if rcEnv.nodename in self.always_on:
                return rcStatus.STDBY_UP
            return rcStatus.UP
        elif n == 0:
            if rcEnv.nodename in self.always_on:
                return rcStatus.STDBY_DOWN
            return rcStatus.DOWN
        else:
            self.status_log("unlocked: "+", ".join(self.unlocked))
            return rcStatus.WARN

    def do_stop_one(self, image):
        data = self.locklist(image)
        if rcEnv.nodename not in data:
            self.log.info(image+" is already unlocked")
            return
        cmd = self.rbd_rcmd()+["lock", "remove", image, rcEnv.nodename, data[rcEnv.nodename]["locker"]]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError("failed to unlock %s"%self.devname(image))

    def do_start_one(self, image):
        data = self.locklist(image)
        if rcEnv.nodename in data:
            self.log.info(image+" is already locked")
            return
        cmd = self.rbd_rcmd()+["lock", "add", image, rcEnv.nodename]
        if self.lock_shared_tag:
            cmd += ["--shared", self.lock_shared_tag]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            raise ex.excError("failed to lock %s"%self.devname(image))

    def provision(self):
        return

0707010000bc77000081a400000000000000000000000154fc7bfb000012ae000000660000000300000000000000000000002100000000root/opt/opensvc/lib/resVgRaw.py  #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import resDg
import os
import rcStatus
import re
import pwd
import grp
import stat
import sys
import glob

class Vg(resDg.Dg):
    def __init__(self,
                 rid=None,
                 devs=set([]),
                 user="root",
                 group="root",
                 perm="660",
                 type=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 always_on=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        self.label = "raw"
        resDg.Dg.__init__(self,
                          rid=rid,
                          name="raw",
                          type='disk.vg',
                          always_on=always_on,
                          optional=optional,
                          disabled=disabled,
                          tags=tags,
                          monitor=monitor,
                          restart=restart,
                          subset=subset)

        self.devs = set([])
        self.devs_not_found = set([])
        self.user = user
        self.group = group
        self.perm = perm

        if sys.version_info[0] < 3:
            self.str_types = (str, unicode)
        else:
            self.str_types = (str)
        
        for dev in devs:
            l = set(glob.glob(dev))
            if len(l) > 0:
                self.devs |= l
            else:
                self.devs_not_found.add(dev)

        self.get_uid()
        self.get_gid()

    def on_add(self):
        try:
            n = self.rid.split('#')[1]
        except:
            n = "0"
        self.name = self.svc.svcname+".raw"+n
        self.label = self.name

    def get_uid(self):
        self.uid = self.user
        if isinstance(self.uid, self.str_types):
            try:
                info=pwd.getpwnam(self.uid)
                self.uid = info[2]
            except:
                pass

    def get_gid(self):
        self.gid = self.group
        if isinstance(self.gid, self.str_types):
            try:
                info=grp.getgrnam(self.gid)
                self.gid = info[2]
            except:
                pass

    def check_uid(self, rdev, verbose=False):
        if not os.path.exists(rdev):
            return True
        uid = os.stat(rdev).st_uid
        if uid != self.uid:
            if verbose:
                self.status_log('%s uid should be %d but is %d'%(rdev, self.uid, uid))
            return False
        return True

    def check_gid(self, rdev, verbose=False):
        if not os.path.exists(rdev):
            return True
        gid = os.stat(rdev).st_gid
        if gid != self.gid:
            if verbose:
                self.status_log('%s gid should be %d but is %d'%(rdev, self.gid, gid))
            return False
        return True

    def check_perm(self, rdev, verbose=False):
        if not os.path.exists(rdev):
            return True
        try:
            perm = oct(stat.S_IMODE(os.stat(rdev).st_mode))
        except:
            self.log.error('%s can not stat file'%rdev)
            return False
        perm = str(perm).lstrip("0")
        if perm != str(self.perm):
            if verbose:
                self.status_log('%s perm should be %s but is %s'%(rdev, str(self.perm), perm))
            return False
        return True

    def has_it(self):
        """Returns True if all devices are present
        """
        if len(self.devs_not_found) > 0:
            self.status_log("%s not found"%', '.join(self.devs_not_found))
            return False
        return True

    def is_up(self):
        """Returns True if the volume group is present and activated
        """
        return self.has_it()

    def _status(self, verbose=False):
        if self.is_up():
            return rcStatus.NA
        else:
            return rcStatus.WARN

    def do_start(self):
        pass

    def do_stop(self):
        pass

    def disklist(self):
        return self.devs
  0707010000bc78000081a400000000000000000000000154fc7bfb0000083d000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resVgRawHP-UX.py #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import resVgRaw
import os
import rcStatus
import re
from rcUtilities import justcall

class Vg(resVgRaw.Vg):
    def __init__(self,
                 rid=None,
                 devs=set([]),
                 type=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 always_on=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        
        resVgRaw.Vg.__init__(self,
                             rid=rid,
                             devs=devs,
                             type=type,
                             optional=optional,
                             disabled=disabled,
                             tags=tags,
                             always_on=always_on,
                             monitor=monitor,
                             restart=restart,
                             subset=subset)

        self.devs = set([])
        self.devs_not_found = set([])

        for dev in devs:
            if os.path.exists(dev) and self.verify_dev(dev):
                self.devs.add(dev)
            else:
                self.devs_not_found.add(dev)

    def verify_dev(self, dev):
        cmd = ["diskinfo", dev]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return False
        return True
   0707010000bc79000081a400000000000000000000000154fc7bfb00002f8e000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resVgRawLinux.py #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import resVgRaw
import os
import rcStatus
import re
import rcExceptions as ex
from rcGlobalEnv import *

class Vg(resVgRaw.Vg):
    def __init__(self,
                 rid=None,
                 devs=set([]),
                 user="root",
                 group="root",
                 perm="660",
                 dummy=False,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 always_on=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        
        resVgRaw.Vg.__init__(self,
                             rid=rid,
                             devs=devs,
                             user=user,
                             group=group,
                             perm=perm,
                             type=type,
                             optional=optional,
                             disabled=disabled,
                             tags=tags,
                             always_on=always_on,
                             monitor=monitor,
                             restart=restart,
                             subset=subset)
        self.dummy = dummy
        self.min_raw = 1
        self.raws = {}
        self.sys_devs = {}

    def get_devs_t(self):
        self.devs_t = {}
        for dev in self.devs:
            d = os.stat(dev)
            self.devs_t[dev] = (os.major(d.st_rdev), os.minor(d.st_rdev))

    def get_rdevnames(self):
        self.rdevs_t = {}
        for dirpath, dirnames, filenames in os.walk('/dev/raw'):
            for filename in filenames:
                f = os.path.join(dirpath, filename)
                d = os.stat(f)
                self.rdevs_t[(os.major(d.st_rdev), os.minor(d.st_rdev))] = filename

    def modprobe(self):
        cmd = ["raw", "-qa"]
        ret, out, err = self.call(cmd)
        if ret == 0:
            # no need to load (already loaded or compiled-in)
            return
        cmd = ["lsmod"]
        ret, out, err = self.call(cmd)
        if ret != 0:
            raise ex.excError
        if "raw" in out.split():
            return
        cmd = ["modprobe", "raw"]
        ret, out, err = self.vcall(cmd)
        if ret != 0:
            self.log.error("failed to load raw device driver")
            raise ex.excError

    def get_raws(self):
        self.modprobe()
        self.sysfs_raw_path = os.path.join(os.sep, 'sys', 'class', 'raw')
        for dirpath, dirnames, filenames in os.walk(self.sysfs_raw_path):
            for dirname in dirnames:
                if dirname == 'rawctl':
                    continue
                dev = os.path.join(dirpath, dirname, 'dev')
                if not os.path.exists(dev):
                    continue
                with open(dev, 'r') as f:
                    buff = f.read()
                try:
                    major, minor = buff.strip().split(':')
                except:
                    continue
                self.raws[dirname] = {'rdev': (int(major), int(minor))}

        cmd = ['raw', '-qa']
        ret, out, err = self.call(cmd)
        if ret != 0:
            self.log.error('failed to fetch raw device bindings')
            raise ex.excError
        for line in out.split('\n'):
            l = line.split()
            if len(l) != 7:
                continue
            raw = l[0].strip(':').replace('/dev/raw/','')
            major = int(l[4].strip(','))
            minor = int(l[6])
            if raw in self.raws:
                self.raws[raw]['bdev'] = (major, minor)

        self.get_devs_t()
        for dev, dev_t in self.devs_t.items():
            for raw, d in self.raws.items():
                if dev_t == d['bdev']:
                    self.raws[raw]['devname'] = dev

        self.get_rdevnames()
        for raw, d in self.raws.items():
            if d['rdev'] in self.rdevs_t:
                self.raws[raw]['rdevname'] = self.rdevs_t[d['rdev']]


    def find_next_raw(self):
        allocated = set(map(lambda x: x['rdev'][1], self.raws.values()))
        candidates = set(range(1, 255))
        candidates -= allocated
        if len(candidates) == 0:
            self.log.error("no more raw device can be allocated")
            raise ex.excError
        return '/dev/raw/raw%d'%sorted(list(candidates))[0]
        
    def devname_to_rdevname(self, devname):
        b = os.path.basename(devname)
        return '/dev/raw/'+self.svc.svcname+"."+b

    def find_raw(self, dev):
        for raw, d in self.raws.items():
            if 'devname' in d and dev == d['devname']:
                return raw
        return None

    def lock(self, timeout=30, delay=1):
        import lock
        lockfile = os.path.join(rcEnv.pathlock, 'startvgraw')
        lockfd = None
        try:
            lockfd = lock.lock(timeout=timeout, delay=delay, lockfile=lockfile)
        except lock.lockTimeout:
            self.log.error("timed out waiting for lock")
            raise ex.excError
        except lock.lockNoLockFile:
            self.log.error("lock_nowait: set the 'lockfile' param")
            raise ex.excError
        except lock.lockCreateError:
            self.log.error("can not create lock file %s"%lockfile)
            raise ex.excError
        except lock.lockAcquire as e:
            self.log.warn("another action is currently running (pid=%s)"%e.pid)
            raise ex.excError
        except ex.excSignal:
            self.log.error("interrupted by signal")
            raise ex.excError
        except:
            self.log.error("unexpected locking error")
            import traceback
            traceback.print_exc()
            raise ex.excError
        self.lockfd = lockfd

    def unlock(self):
        import lock
        lock.unlock(self.lockfd)

    def has_it(self):
        """Returns True if all raw devices are present and correctly
           named
        """
        r = False
        if len(self.devs_not_found) > 0:
            self.status_log("%s not found"%', '.join(self.devs_not_found))
            r |= True
        if self.dummy:
            return not r
        self.get_raws()
        for dev in self.devs:
            raw = self.find_raw(dev)
            if raw is None:
                self.status_log("%s is not mapped to a raw device"%dev)
                r |= True
            elif '/dev/raw/'+self.raws[raw]['rdevname'] != self.devname_to_rdevname(dev):
                self.status_log("%s raw device is named %s, expected %s"%(dev, '/dev/raw/'+self.raws[raw]['rdevname'], self.devname_to_rdevname(dev)))
                r |= True
            elif not self.check_uid('/dev/raw/'+self.raws[raw]['rdevname'], verbose=True):
                r |= True
            elif not self.check_gid('/dev/raw/'+self.raws[raw]['rdevname'], verbose=True):
                r |= True
            elif not self.check_perm('/dev/raw/'+self.raws[raw]['rdevname'], verbose=True):
                r |= True
        return not r

    def _status(self, verbose=False):
        if self.dummy:
            if not self.has_it():
                return rcStatus.WARN
            return rcStatus.NA
        if rcEnv.nodename in self.always_on:
            if self.is_up(): return rcStatus.STDBY_UP
            else: return rcStatus.STDBY_DOWN
        else:
            if self.is_up(): return rcStatus.UP
            else: return rcStatus.DOWN

    def do_start(self):
        if self.dummy:
            return
        self.lock()
        self.get_raws()
        for dev in self.devs:
            raw = self.find_raw(dev)
            if raw is not None:
                self.log.info("%s is already mapped to a raw device"%dev)
                rdevname = '/dev/raw/'+self.raws[raw]['rdevname']
                if rdevname != self.devname_to_rdevname(dev):
                    cmd = ['mv', rdevname, self.devname_to_rdevname(dev)]
                    ret, out, err = self.vcall(cmd)
                    if ret != 0:
                        self.unlock()
                        raise ex.excError
                else:
                    self.log.info("%s is correctly named"%rdevname)
            else:
                raw = self.find_next_raw()
                cmd = ['raw', raw, dev]
                ret, out, err = self.vcall(cmd)
                if ret != 0:
                    self.unlock()
                    raise ex.excError
                s = os.stat(raw)
                self.raws[raw] = {'rdevname': self.devname_to_rdevname(dev),
                                  'rdev': (os.major(s.st_rdev), os.minor(s.st_rdev)),
                                  'devname': dev,
                                  'bdev': self.devs_t[dev]}
                cmd = ['mv', raw, self.devname_to_rdevname(dev)]
                ret, out, err = self.vcall(cmd)
                if ret != 0:
                    self.unlock()
                    raise ex.excError

            rdevname = self.raws[raw]['rdevname']
            if not self.check_uid(rdevname) or not self.check_gid(rdevname):
                self.vcall(['chown', ':'.join((str(self.uid),str(self.gid))), rdevname])
            else:
                self.log.info("%s has correct ownership"%rdevname)
            if not self.check_perm(rdevname):
                self.vcall(['chmod', self.perm, rdevname])
            else:
                self.log.info("%s has correct permissions"%rdevname)

        self.unlock()

    def do_stop(self):
        if self.dummy:
            return
        self.get_raws()
        for dev in self.devs:
            raw = self.find_raw(dev)
            if raw is not None:
                cmd = ['raw', '/dev/raw/raw%d'%self.raws[raw]['rdev'][1], '0', '0']
                ret, out, err = self.vcall(cmd)
                if ret != 0:
                    raise ex.excError
            rdevname = self.devname_to_rdevname(dev)
            if os.path.exists(rdevname):
                if '*' in rdevname:
                    self.log.error("no wildcard allow in raw device names")
                    raise ex.excError
                cmd = ['rm', '-f', rdevname]
                ret, out, err = self.vcall(cmd)
                if ret != 0:
                    raise ex.excError

    def load_sys_devs(self):
        import glob
        if self.sys_devs != {}:
            return
        for e in glob.glob('/sys/block/*/dev'):
            with open(e, 'r') as f:
                dev = e.replace('/sys/block/', '').replace('/dev', '')
                dev_t = f.read().strip()
                self.sys_devs[dev_t] = '/dev/'+dev

    def disklist(self):
        sys_devs = self.devlist()
        from rcUtilitiesLinux import devs_to_disks
        return devs_to_disks(self, sys_devs)

    def devlist(self):
        """ Admins can set arbitrary named devices, for example /dev/oracle/DGREDO_MYSID.
            Resolve those names into well known systems device names, so that they can be
            found in the DevTree
        """
        if not self.dummy:
            return self.devs
        sys_devs = set([])
        self.load_sys_devs()
        self.get_devs_t()
        for dev in self.devs:
            if dev not in self.devs_t:
                continue
            dev_t = ':'.join(map(str, self.devs_t[dev]))
            if dev_t not in self.sys_devs:
                continue
            sys_dev = self.sys_devs[dev_t]
            sys_devs.add(sys_dev)
        return sys_devs
  0707010000bc7a000081a400000000000000000000000154fc7bfb00000a66000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resVgRawSunOS.py #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import resVgRaw
import os
import rcStatus
import re

class Vg(resVgRaw.Vg):
    def __init__(self,
                 rid=None,
                 devs=set([]),
                 type=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 always_on=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        
        resVgRaw.Vg.__init__(self,
                             rid=rid,
                             devs=devs,
                             type=type,
                             optional=optional,
                             disabled=disabled,
                             tags=tags,
                             always_on=always_on,
                             monitor=monitor,
                             restart=restart,
                             subset=subset)

        self.devs = set([])
        self.devs_not_found = set([])

        for dev in devs:
            if re.match("^.*[sp][0-9]*$", dev) is not None:
                # partition, substitute s2 to given part
                regex = re.compile("[sp][0-9]*$", re.UNICODE)
                dev = regex.sub("s2", dev)
            else:
                # base device, append s2
                dev += 's2'

            if os.path.exists(dev):
                self.devs.add(dev)
            else:
                self.devs_not_found.add(dev)

    def disklist(self):
        l = set([])
        for dev in self.devs:
            if re.match("^/dev/rdsk/c[0-9]*", dev) is not None:
                if os.path.exists(dev):
                    if re.match('^.*s[0-9]*$', dev) is None:
                        dev += "s2"
                    else:
                        regex = re.compile('s[0-9]*$', re.UNICODE)
                        dev = regex.sub('s2', dev)
                    l.add(dev)
        return l

  0707010000bc7b000081a400000000000000000000000154fc7bfb000003b6000000660000000300000000000000000000002500000000root/opt/opensvc/lib/resVgSgHP-UX.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
Res = __import__("resVgHP-UX")

class Vg(Res.Vg):

    def start(self):
        return 0

    def stop(self):
        return 0

  0707010000bc7c000081a400000000000000000000000154fc7bfb000003b6000000660000000300000000000000000000002500000000root/opt/opensvc/lib/resVgSgLinux.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
Res = __import__("resVgLinux")

class Vg(Res.Vg):

    def start(self):
        return 0

    def stop(self):
        return 0

  0707010000bc7d000081a400000000000000000000000154fc7bfb0000053a000000660000000300000000000000000000002600000000root/opt/opensvc/lib/resVgVcsLinux.py #
# Copyright (c) 2013 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
Res = __import__("resVgLinux")
import rcStatus
import rcExceptions as ex

class Vg(Res.Vg):

    def start(self):
        pass

    def stop(self):
        pass

    def _status(self, verbose=False):
        try:
            s = self.svc.get_res_val(self.vcs_name, 'State')
        except ex.excError as e:
            self.status_log(str(e))
            return rcStatus.WARN

        if s == "ONLINE":
            return rcStatus.UP
        elif s == "OFFLINE":
            return rcStatus.DOWN
        else:
            self.status_log(s)
            return rcStatus.WARN

  0707010000bc7e000081a400000000000000000000000154fc7bfb00001218000000660000000300000000000000000000002500000000root/opt/opensvc/lib/resVgVeritas.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.
"""Module providing Veritas Volume group resources
"""

import resDg
import re
from rcUtilities import qcall

class Vg(resDg.Dg):
    """ basic Veritas Volume group resource
    """
    def __init__(self,
                 rid=None,
                 name=None,
                 type=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 always_on=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        self.label = name
        resDg.Dg.__init__(self,
                          rid=rid,
                          name=name,
                          type='disk.vg',
                          always_on=always_on,
                          optional=optional,
                          disabled=disabled,
                          tags=tags,
                          monitor=monitor,
                          restart=restart,
                          subset=subset)

    def has_it(self):
        """Returns True if the vg is present
        """
        ret = qcall( [ 'vxdg', 'list', self.name ] )
        if ret == 0 :
            return True
        else:
            return False

    def is_up(self):
        """Returns True if the vg is present and not disabled
        """
        if not self.has_it():
            return False
        cmd = [ 'vxprint', '-ng', self.name ]
        ret = qcall(cmd)
        if ret == 0 :
                return True
        else:
                return False

    def do_startvol(self):
        cmd = [ 'vxvol', '-g', self.name, '-f', 'startall' ]
        (ret, out, err) = self.vcall(cmd)
        return ret

    def do_stopvol(self):
        cmd = [ 'vxvol', '-g', self.name, '-f', 'stopall' ]
        (ret, out, err) = self.vcall(cmd)
        return ret

    def do_start(self):
        if self.is_up():
            self.log.info("%s is already up" % self.name)
            ret = self.do_startvol()
            if ret == 0 :
                return 0
            else:
                return ret
        self.can_rollback = True
        for flag in [ '-t', '-tC', '-tCf']:
            cmd = [ 'vxdg', flag, 'import', self.name ]
            (ret, out, err) = self.vcall(cmd)
            if ret == 0 :
                ret = self.do_startvol()
                return ret
        return ret

    def do_stop(self):
        if not self.is_up():
            self.log.info("%s is already down" % self.name)
            return 0
        ret = self.do_stopvol()
        cmd = [ 'vxdg', 'deport', self.name ]
        (ret, out, err) = self.vcall(cmd)
        return ret

    def disklist(self):
        """disklist() search vg disks from
        output of : vxdisk -g vgname -q  path

        disklist(self) update self.disks[]
        """
        if len(self.disks) > 0 :
            return self.disks

        disks = set([])
        cmd = [ 'vxdisk', '-g', self.name, '-q', 'list' ]
        (ret, out, err) = self.call(cmd, errlog=False)
        if ret != 0 :
            self.disks = disks
            return disks
        for line in out.split('\n'):
            disk = line.split(" ")[0]
            if disk != '' :
                if re.match('^.*s[0-9]$', disk) is None:
                    disk += "s2"
                disks.add("/dev/rdsk/" + disk )
       
        self.log.debug("found disks %s held by pool %s" % (disks, self.name))
        self.disks = disks

        return disks

if __name__ == "__main__":
    for c in (Pool,) :
        help(c)

    print("""p=Vg("vzone1vg")""")
    p=Pool("vzone1vg")
    print("show p", p)
    print("""p.do_action("start")""")
    p.do_action("start")
    print("""p.do_action("stop")""")
    p.do_action("stop")
0707010000bc7f000081a400000000000000000000000154fc7bfb0000195a000000660000000300000000000000000000002100000000root/opt/opensvc/lib/resVgZfs.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.
"""Module providing ZFS resources
"""

from rcGlobalEnv import rcEnv
import resDg
from rcUtilities import qcall
import os
import rcExceptions as ex

import re

class Pool(resDg.Dg):
    """ basic pool resource
    """
    def __init__(self,
                 rid=None,
                 name=None,
                 type=None,
                 optional=False,
                 disabled=False,
                 tags=set([]),
                 always_on=set([]),
                 monitor=False,
                 restart=0,
                 subset=None):
        self.label = 'pool ' + name
        resDg.Dg.__init__(self,
                          rid=rid,
                          name=name,
                          type='disk.zpool',
                          always_on=always_on,
                          optional=optional,
                          disabled=disabled,
                          tags=tags,
                          monitor=monitor,
                          restart=restart,
                          subset=subset)

    def disklist_name(self):
        return os.path.join(rcEnv.pathvar, 'vg_' + self.svc.svcname + '_' + self.name + '.disklist')

    def files_to_sync(self):
        return [self.disklist_name()]

    def presync(self):
        """ this one is exported as a service command line arg
        """
        dl = self._disklist()
        import json
        with open(self.disklist_name(), 'w') as f:
            f.write(json.dumps(list(dl)))

    def has_it(self):
        """Returns True if the pool is present
        """
        ret = qcall( [ 'zpool', 'list', self.name ] )
        if ret == 0 :
            return True
        return False

    def is_up(self):
        """Returns True if the pool is present and activated
        """
        if not self.has_it():
            return False
        cmd = [ 'zpool', 'list', '-H', '-o', 'health', self.name ]
        (ret, out, err) = self.call(cmd)
        if out.strip() == "ONLINE" :
            return True
        return False

    def do_start(self):
        if self.is_up():
            self.log.info("%s is already up" % self.name)
            return 0
        devzp = os.path.join(rcEnv.pathvar, self.svc.svcname, 'dev', 'dsk')
        if os.path.isdir(devzp):
            cmd = [ 'zpool', 'import', '-f', '-o', 'cachefile='+os.path.join(rcEnv.pathvar, 'zpool.cache'), '-d', devzp, self.name ]
            (ret, out, err) = self.vcall(cmd)
            if ret == 0:
                return ret
            else:
                self.log.info("import %s: FallBack Long Way" %self.name)
        cmd = [ 'zpool', 'import', '-f', '-o', 'cachefile='+os.path.join(rcEnv.pathvar, 'zpool.cache'), self.name ]
        (ret, out, err) = self.vcall(cmd)
        self.can_rollback = True
        return ret

    def do_stop(self):
        if not self.is_up():
            self.log.info("%s is already down" % self.name)
            return 0
        cmd = [ 'zpool', 'export', self.name ]
        (ret, out, err) = self.vcall(cmd)
        return ret

    def disklist(self):
        if not os.path.exists(self.disklist_name()):
            s = self.svc.group_status(excluded_groups=set(["sync", "hb"]))
            import rcStatus
            if s['overall'].status == rcStatus.UP:
                self.log.debug("no disklist cache file and service up ... refresh disklist cache")
                self.presync()
            else:
                self.log.debug("no disklist cache file and service not up ... unable to evaluate disklist")
                return set([])
        with open(self.disklist_name(), 'r') as f:
            buff = f.read()
        import json
        try:
            dl = set(json.loads(buff))
        except:
            self.log.error("corrupted disklist cache file %s"%self.disklist_name())
            raise ex.excError
        return dl

    def _disklist(self):
        """disklist() search zpool vdevs from
        output of : zpool status poolname if status cmd == 0
        else
        output of : zpool import output if status cmd == 0

        disklist(self) update self.disks[]
        """

        # return cache if initialized
        if len(self.disks) > 0 :
            return self.disks

        disks = set([])
        cmd = [ 'zpool', 'status', self.name ]
        (ret, out, err) = self.call(cmd)
        if ret != 0:
            raise ex.excError

        for line in out.split('\n'):
            if re.match('^\t  ', line) is not None:
                if re.match('^\t  mirror', line) is not None:
                    continue
                if re.match('^\t  raid', line) is not None:
                    continue
                # vdev entry
                disk = line.split()[0]
                if disk.startswith(rcEnv.pathvar):
                    disk = disk.split('/')[-1]
                if re.match("^.*", disk) is not None :
                    disks.add("/dev/rdsk/" + disk )

        self.log.debug("found disks %s held by pool %s" % (disks, self.name))
        for d in disks:
            if re.match('^.*s[0-9]*$', d) is None:
                d += "s2"
            else:
                regex = re.compile('s[0-9]*$', re.UNICODE)
                d = regex.sub('s2', d)
            self.disks.add(d)

        return self.disks

if __name__ == "__main__":
    for c in (Pool,) :
        help(c)

    # return cache if initialized
    print("""p=Pool("svczfs1")""")
    p=Pool("svczfs1")
    print("show p", p)
    print("""p.do_action("start")""")
    p.do_action("start")
    print("""p.do_action("stop")""")
    p.do_action("stop")
  0707010000bc80000081a400000000000000000000000154fc7bfb0000572c000000660000000300000000000000000000002200000000root/opt/opensvc/lib/resources.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
# Copyright (c) 2010 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2010 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import os
import rcExceptions as exc
import rcStatus
import logging
import rcUtilities
import sys
import time
from rcGlobalEnv import rcEnv

class Resource(object):
    """Define basic resource
    properties: type, optional=optional, disabled=disabled, tags=tags
    a Resource should provide do_action(action):
    with action into (start/stop/status)
    """
    label = None

    def __init__(self,
                 rid=None,
                 type=None,
                 subset=None,
                 optional=False,
                 disabled=False,
                 monitor=False,
                 restart=0,
                 tags=set([]),
                 always_on=set([])):
        self.rid = rid
        self.tags = tags
        self.type = type
        self.subset = subset
        self.optional = optional
        self.disabled = disabled
        self.skip = False
        self.monitor = monitor
        self.nb_restart = restart
        self.log = logging.getLogger(self.log_label())
        self.rstatus = None
        self.always_on = always_on
        if self.label is None: self.label = type
        self.status_log_str = ""
        self.can_rollback = False

    def log_label(self):
        s = ""
        if hasattr(self, "svc"):
            s += self.svc.svcname + '.'

        if self.rid is None:
            s += self.type
            return s.upper()

        if self.subset is None:
            s += self.rid
            return s.upper()

        v = self.rid.split('#')
        if len(v) != 2:
            s += rid
            return s.upper()

        s += "%s:%s#%s" % (self.type, self.subset, v[1])
        return s.upper()
       
    def __str__(self):
        output="object=%s rid=%s type=%s" % (self.__class__.__name__,
                                             self.rid, self.type)
        if self.optional : output+=" opt="+str(self.optional)
        if self.disabled : output+=" disa="+str(self.disabled)
        return output

    def __cmp__(self, other):
        """resources needed to be started or stopped in a specific order
        should redefine that.
        """
        return cmp(self.rid, other.rid)

    def print_exc(self):
        import traceback
        try:
            self.log.error(traceback.format_exc())
        except:
            self.log.error("unexpected error")
            traceback.print_exc()

    def save_exc(self):
        import traceback
        try:
            import tempfile
            import datetime
            now = str(datetime.datetime.now()).replace(' ', '-')
            f = tempfile.NamedTemporaryFile(dir=rcEnv.pathtmp,
                                            prefix='exc-')
            f.close()
            f = open(f.name, 'w')
            traceback.print_exc(file=f)
            self.log.error("unexpected error. stack saved in %s"%f.name)
            f.close()
        except:
            self.log.error("unexpected error")
            traceback.print_exc()

    def setup_environ(self):
        """ setup environement variables for use by triggers and startup
            scripts. This method needs defining in each class with their
            class variable.
            Env vars names should, by convention, be prefixed by OPENSVC_
        """
        pass

    def is_optional(self): return self.optional
    def is_disabled(self): return self.disabled

    def set_optional(self): self.optional=True
    def unset_optional(self): self.optional=False

    def disable(self): self.disabled=True
    def enable(self):  self.disabled=False

    def action_triggers(self, type, action):
        attr = type+"_"+action
        if hasattr(self, attr):
            if self.svc.options.dry_run:
                self.log.info("exec trigger %s" % getattr(self, attr))
                return
            self.vcall(getattr(self, attr))

    def action_main(self, action):
        if self.svc.options.dry_run:
            if self.rset.parallel:
                header = "+ "
            else:
                header = ""
            self.log.info("%s%s %s"%(header, action, self.label))
            return
        getattr(self, action)()

    def do_action(self, action):
        self.log.debug('do_action: action=%s res=%s'%(action, self.rid))
        if hasattr(self, action):
            if "stop" in action and rcEnv.nodename in self.always_on and not self.svc.force:
                standby_action = action+'standby'
                if hasattr(self, standby_action):
                    self.action_main(standby_action)
                    return
                else:
                    self.log.info("skip '%s' on standby resource (--force to override)"%action)
                    return
            self.setup_environ()
            self.action_triggers("pre", action)
            self.action_main(action)
            self.action_triggers("post", action)
            if not self.svc.options.dry_run and \
               ("start" in action or "stop" in action or "rollback" in action or "sync" in action or action == "provision"):
                """ refresh resource status cache after changing actions
                """
                self.status(refresh=True, restart=False)
            return

        """Every class inheriting resource should define start() stop() status()
        Alert on these minimal implementation requirements
        """
        if action in ("start","stop","status") :
            raise exc.excUndefined(action,self.__class__.__name__,\
                                    "Resource.do_action")

    def action(self, action=None):
        """ action() try to call do_action() on self
        return if action is not None or if self is disabled
        return status depends on optional property value:
        if self is optional then return True
        else return do_action() return value
        """
        if not self.rid == "app" and not self.svc.encap and 'encap' in self.tags:
            self.log.debug('skip encap resource action: action=%s res=%s'%(action, self.rid))
            return

        if 'noaction' in self.tags:
            self.log.debug('skip resource action (noaction tag): action=%s res=%s'%(action, self.rid))
            return

        self.log.debug('action: action=%s res=%s'%(action, self.rid))
        if action == None:
            self.log.debug('action: action cannot be None')
            return True
        if self.skip and (\
             action.startswith("start") or \
             action.startswith("stop") or \
             action.startswith("sync") or \
             action == "provision"
           ):
            self.log.debug('action: skip action on filtered-out resource')
            return True
        if self.disabled:
            self.log.debug('action: skip action on disabled resource')
            return True
        try :
            self.do_action(action)
        except exc.excUndefined as ex:
            print(ex)
            return False
        except exc.excError:
            if self.optional:
                pass
            else:
                raise

    def status_stdby(self, s):
        """ This function modifies the passed status according
            to this node inclusion in the always_on nodeset
        """
        if rcEnv.nodename not in self.always_on:
            return s
        if s == rcStatus.UP:
            return rcStatus.STDBY_UP
        elif s == rcStatus.DOWN:
            return rcStatus.STDBY_DOWN
        return s

    def _status(self, verbose=False):
        return rcStatus.UNDEF

    def force_status(self, s):
        self.rstatus = s
        self.status_log_str = "forced"
        self.write_status()

    def status(self, verbose=False, refresh=False, restart=True):
        # refresh param: used by do_action() to force a res status re-eval
        # self.svc.options.refresh: used to purge disk cache
        if self.disabled:
            self.status_log("disabled")
            return rcStatus.NA

        if self.rstatus is not None and not refresh:
            return self.rstatus

        last_status = self.load_status_last()

        if self.svc.options.refresh or refresh:
            self.purge_status_last()
        else:
            self.rstatus = last_status

        if self.rstatus is None or refresh:
            self.status_log_str = ""
            self.rstatus = self._status(verbose)
            self.write_status()

        if restart:
            self.do_restart(last_status)

        return self.rstatus

    def do_restart(self, last_status):
        restart_last_status = (
          rcStatus.UP,
          rcStatus.STDBY_UP,
          rcStatus.STDBY_UP_WITH_UP,
          rcStatus.STDBY_UP_WITH_DOWN
        )
        no_restart_status = (
          rcStatus.UP,
          rcStatus.STDBY_UP,
          rcStatus.NA,
          rcStatus.UNDEF,
          rcStatus.STDBY_UP_WITH_UP,
          rcStatus.STDBY_UP_WITH_DOWN,
          rcStatus.TODO
        )
        if self.nb_restart == 0:
            return
        if self.rstatus in no_restart_status:
            return
        if last_status not in restart_last_status:
            return

        if not hasattr(self, 'start'):
            self.log.error("resource restart configured on resource %s with no 'start' action support"%self.rid)
            return

        for i in range(self.nb_restart):
            try:
                self.log.info("restart resource %s. try number %d/%d"%(self.rid, i+1, self.nb_restart))
                self.action("start")
            except Exception as e:
                self.log.error("restart resource failed: " + str(e))
            self.rstatus = self._status()
            self.write_status()
            if self.rstatus == rcStatus.UP:
                self.log.info("monitored resource %s restarted."%self.rid)
                return
            if i + 1 < self.nb_restart:
                time.sleep(1)
        return

    def write_status(self):
        self.write_status_last()
        self.write_status_history()

    def fpath_status_last(self):
        dirname = os.path.join(rcEnv.pathvar, self.svc.svcname)
        fname = "resource.status.last." + self.rid
        fpath = os.path.join(dirname, fname)
        if not os.path.exists(dirname):
            os.makedirs(dirname)
        return fpath

    def fpath_status_history(self):
        dirname = os.path.join(rcEnv.pathvar, self.svc.svcname)
        fname = "resource.status.history." + self.rid
        fpath = os.path.join(dirname, fname)
        if not os.path.exists(dirname):
            os.makedirs(dirname)
        return fpath

    def purge_status_last(self):
        try:
            os.unlink(self.fpath_status_last())
        except:
            pass

    def load_status_last(self):
        try:
            with open(self.fpath_status_last(), 'r') as f:
                lines = f.read().split("\n")
                status_str = lines[0]
                s = rcStatus.status_value(status_str)
                if len(lines) > 1:
                    self.status_log_str = '\n'.join(lines[1:])
        except:
            s = None
        return s

    def write_status_last(self):
        with open(self.fpath_status_last(), 'w') as f:
            s = rcStatus.status_str(self.rstatus)+'\n'
            if len(self.status_log_str) > 1:
                s += self.status_log_str+'\n'
            f.write(s)

    def write_status_history(self):
        fpath = self.fpath_status_history()
        try:
            with open(fpath, 'r') as f:
                lines = f.readlines()
                last = lines[-1].split(" | ")[-1].strip("\n")
        except:
            last = None
        current = rcStatus.status_str(self.rstatus)
        if current == last:
            return
        import logging
        log = logging.getLogger("status_history")
        logformatter = logging.Formatter("%(asctime)s | %(message)s")
        logfilehandler = logging.handlers.RotatingFileHandler(
          fpath,
          maxBytes=512000,
          backupCount=1,
        )
        logfilehandler.setFormatter(logformatter)
        log.addHandler(logfilehandler)
        log.error(current)
        logfilehandler.close()
        log.removeHandler(logfilehandler)

    def status_log(self, text):
        msg = "# " + text + "\n"
        if msg in self.status_log_str:
            return
        self.status_log_str += msg

    def status_quad(self):
        r = self.status(verbose=True)
        if 'encap' in self.tags:
            encap = True
        else:
            encap = False
        return (self.rid,
                rcStatus.status_str(r),
                self.label,
                self.status_log_str,
                self.monitor,
                self.disabled,
                self.optional,
                encap)

    def call(self, cmd=['/bin/false'], cache=False, info=False,
             errlog=True, err_to_warn=False, err_to_info=False,
             outlog=False):
        """Use subprocess module functions to do a call
        """
        return rcUtilities.call(cmd, log=self.log,
                                cache=cache,
                                info=info, errlog=errlog,
                                err_to_warn=err_to_warn,
                                err_to_info=err_to_info,
                                outlog=outlog)

    def vcall(self, cmd, err_to_warn=False, err_to_info=False):
        """Use subprocess module functions to do a call and
        log the command line using the resource logger
        """
        return rcUtilities.vcall(cmd, log=self.log,
                                 err_to_warn=err_to_warn,
                                 err_to_info=err_to_info)

    def devlist(self):
        return self.disklist()

    def disklist(self):
        """List disks the resource holds. Some resource have none,
        and can leave this function as is.
        """
        return set()

    def presync(self):
        pass

    def postsync(self):
        pass

    def provision(self):
        pass

    def files_to_sync(self):
        return []

    def rollback(self):
        if self.can_rollback:
            self.stop()

    def stop(self):
        pass

    def startstandby(self):
        if rcEnv.nodename in self.always_on:
             self.start()

    def start(self):
        pass

    def shutdown(self):
        self.stop()

class ResourceSet(Resource):
    """ Define Set of same type resources
    Example 1: ResourceSet("fs",[m1,m2])
    Example 2: r=ResourceSet("fs",[ip1])
    It define the resource type
    """
    def __init__(self,
                 type=None,
                 resources=[],
                 parallel=False,
                 optional=False,
                 disabled=False,
                 tags=set([])):
        self.parallel = parallel
        self.resources = []
        Resource.__init__(self,
                          type=type,
                          optional=optional,
                          disabled=disabled,
                          tags=tags)
        for r in resources:
            self += r

    def __cmp__(self, other):
        return cmp(self.type, other.type)

    def __iadd__(self,r):
        """Example 1 iadd another ResourceSet: R+=ResSet ... R+=[m1,m2]
        Example 2 : iadd a single Resource : R+=ip1
        """
        if isinstance(r,ResourceSet) :
            self.resources.extend(r.resources)
        elif isinstance(r,Resource) :
            """ Setup a back pointer to the resource set
            """
            r.rset = self
            self.resources.append(r)
            if hasattr(r, 'pre_action'):
                r.log.debug("install pre_action")
                self.pre_action = r.pre_action
            if hasattr(r, 'post_action'):
                r.log.debug("install post_action")
                self.post_action = r.post_action
            if hasattr(r, 'sort_rset'):
                r.sort_rset(self)
        return (self)

    def __str__(self):
        output="resSet %s [" % ( Resource.__str__(self) )
        for r in self.resources:
            output+= " (%s)" % (r.__str__())
        return "%s]" % (output)

    def pre_action(self, rset=None, action=None):
        pass

    def post_action(self, rset=None, action=None):
        pass

    def purge_status_last(self):
        for r in self.resources:
            r.purge_status_last()

    def status(self, verbose=False):
        """aggregate status a ResourceSet
        """
        s = rcStatus.Status()
        for r in self.resources:
            if r.is_disabled():
                continue
            if not r.svc.encap and 'encap' in r.tags:
                # don't evaluate encap service resources
                continue
            try:
                status = r.status()
            except:
                import sys
                import traceback
                e = sys.exc_info()
                print(e[0], e[1], traceback.print_tb(e[2]))

                status = rcStatus.NA

            s += status
        return s.status

    def tag_match(self, rtags, keeptags):
        if len(keeptags) == 0:
            return True
        for tag in rtags:
            if tag in keeptags:
                return True
        return False

    def has_encap_resources(self):
        resources = [r for r in self.resources if self.tag_match(r.tags, set(['encap']))]
        if len(resources) == 0:
            return False
        return True
        
    def action(self, action=None, tags=set([]), xtags=set([])):
        """Call action on each resource of the ResourceSet
        """
        if self.parallel:
            # verify we can actually do parallel processing, fallback to serialized
            try:
                from multiprocessing import Process
                if rcEnv.sysname == "Windows":
                    import sys
                    import os
                    from multiprocessing import set_executable
                    set_executable(os.path.join(sys.exec_prefix, 'pythonw.exe'))
            except:
                self.parallel = False

        if len(xtags) > 0:
            resources = [r for r in self.resources if not self.tag_match(r.tags, xtags)]
        else:
            resources = self.resources

        resources = [r for r in resources if self.tag_match(r.tags, tags)]
        self.log.debug("resources after tags[%s] filter: %s"%(str(tags), ','.join([r.rid for r in resources])))

        resources = [r for r in resources if not r.disabled]
        self.log.debug("resources after 'disable' filter: %s"% ','.join([r.rid for r in resources]))

        if action == "startstandby":
            # filter out resource not in standby mode
            resources = [r for r in resources if rcEnv.nodename in r.always_on]

        if hasattr(self, "sort_resources"):
            resources = self.sort_resources(resources, action)
        elif action in ["fs", "start", "startstandby", "provision"]:
            # CODE TO KILL ASAP
            resources.sort()
        else:
            # CODE TO KILL ASAP
            resources.sort(reverse=True)

        if not self.svc.options.dry_run and self.parallel and len(resources) > 1 and action not in ["presync", "postsync"]:
            ps = {}
            for r in resources:
                if not r.can_rollback and action == "rollback":
                    continue
                if r.skip or r.disabled:
                    continue
                p = Process(target=self.action_job, args=(r, action,))
                p.start()
                r.log.info("action %s started in child process %d"%(action, p.pid))
                ps[r.rid] = p
            for p in ps.values():
                p.join()
            err = 0
            for r in resources:
                if r.rid not in ps:
                    continue
                p = ps[r.rid]
                if p.exitcode == 1 and not r.optional:
                    err += 1
                elif p.exitcode == 2:
                    # can_rollback resource property is lost with the thread
                    # the action_job tells us what to do with it through its exitcode
                    r.can_rollback = True
            if err > 0:
                raise exc.excError
        else:
            if self.svc.options.dry_run and self.parallel and len(resources) > 1 and action not in ["presync", "postsync"]:
                r.log.info("entering parallel subset")
            for r in resources:
                try:
                    r.action(action)
                except exc.excAbortAction:
                    break

    def action_job(self, r, action):
        try:
            getattr(r, 'action')(action)
        except:
            sys.exit(1)
        if r.can_rollback:
            sys.exit(2)
        sys.exit(0)


if __name__ == "__main__":
    for c in (Resource,ResourceSet) :
        help(c)
    print("""m1=Resource("Type1")""")
    m1=Resource("Type1")
    print("m1=",m1)
    print("""m2=Resource("Type1")""")
    m2=Resource("Type1")
    print("m1=",m1)
    print("m2=",m2)
    print("""sets=ResourceSet("TypeRes2")""")
    sets=ResourceSet("TypeRes2")
    print("sets=", sets)
    print("""sets+=m1""")
    sets+=m1
    print("sets=", sets)
    print("""sets+=m2""")
    sets+=m2
    print("sets=", sets)


0707010000bc81000081a400000000000000000000000154fc7bfb0000141b000000660000000300000000000000000000001d00000000root/opt/opensvc/lib/snap.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
import resources as Res
import rcExceptions as ex


def find_mount(rs, dir):
    """Sort mounts from deepest to shallowest and return the
       first mount whose 'mountPoint' is matching 'dir'
    """
    for m in sorted(rs.resources, reverse=True):
        if m.is_disabled():
            continue
        if m.mountPoint in dir:
            return m
    return None

def find_mounts(self, mounts_h):
    rs = self.svc.get_res_sets("fs")[0]
    if rs is None:
        self.log.error("can not find fs resources encapsulating %s to snap (no fs resources)"%self.src)
        raise ex.syncNotSnapable
    for src in self.src:
        m = find_mount(rs, src)
        if m is None:
            self.log.error("can not find fs resources encapsulating %s to snap"%src)
            raise ex.syncNotSnapable
        mounts_h[src] = m
    return mounts_h

class Snap(Res.Resource):
    """Defines a snap object
    """
    def __init__(self, rid, optional=False, disabled=False, tags=set([])):
        self.snaps = {}
        Res.Resource.__init__(self, rid, "sync.snap", optional=optional,\
                            disabled=disabled, tags=tags)

    def try_snap(self, rset, action, rid=None):
        if action == "nodes":
            action = "syncnodes"
        if action == "drpnodes":
            action = "syncdrp"

        mounts_h = {}
        for r in rset.resources:
            """ if rid is set, snap only the specified resource.
                Used by resources tagged 'delay_snap' on sync()

                if rid is not set, don't snap resources tagged 'delay_snap'
                (pre_action() code path)
            """
            if rid is None:
                if "delay_snap" in r.tags:
                    continue
            elif rid != r.rid:
                continue

            if r.is_disabled():
                continue

            if r.snap is not True and r.snap is not False:
                self.log.error("service configuration error: 'snap' must be 'true' or 'false'. default is 'false'")
                raise ex.syncConfigSyntaxError

            if not r.snap:
                continue

            if (action == "syncnodes" and not 'nodes' in r.target) or \
               (action == "syncdrp" and not 'drpnodes' in r.target):
                self.log.debug("action %s but resource target is %s"%(action, r.target))
                continue

            mounts_h = find_mounts(r, mounts_h)

        mounts = set(mounts_h.values())
        for m in mounts:
            try:
                self.snapcreate(m)
            except ex.syncNotSnapable:
                self.log.error("Resource not snapable: "+m.__str__())
                continue
            except (ex.syncNotSnapable, ex.syncSnapExists, ex.syncSnapMountError,
                ex.syncSnapCreateError, ex.syncSnapDestroyError):
                """Clean up the mess
                """
                self.snap_cleanup(rset)
                raise ex.excError
            except:
                raise

        """Update src dirs of every sync resource to point to an
           existing snap
        """
        for i, r in enumerate(rset.resources):
            r.alt_src = list(r.src)
            for j, src in enumerate(r.alt_src):
                if not mounts_h.has_key(src):
                    continue
                mnt = mounts_h[src].mountPoint
                if not self.snaps.has_key(mnt):
                    continue
                snap_mnt = self.snaps[mnt]['snap_mnt']
                rset.resources[i].alt_src[j] = src.replace(os.path.join(mnt), os.path.join(snap_mnt), 1)

    def snap_cleanup(self, rset=None):
        if not hasattr(self, 'snaps'):
            return
        if len(self.snaps) == 0 :
            return
        for s in self.snaps.keys():
            self.snapdestroykey(s)
        if rset is None:
            return
        for i, r in enumerate(rset.resources):
            if hasattr(rset.resources[i], 'alt_src'):
                delattr(rset.resources[i], 'alt_src')

    def snapcreate(self, m):
        """ create a snapshot for m
        add self.snaps[m] with
            dict(snapinfo key val)
        """
        raise ex.MissImpl

    def snapdestroykey(self, snaps_key):
        """ destroy a snapshot for a snap key
        """
        raise ex.MissImpl

 0707010000bc82000081a400000000000000000000000154fc7bfb00000f37000000660000000300000000000000000000002600000000root/opt/opensvc/lib/snapAdvfsOSF1.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
from rcUtilities import justcall, protected_mount
import rcExceptions as ex
import snap
import rcAdvfs
from rcMountsOSF1 import Mounts

class Snap(snap.Snap):
    """Defines a snap object with ZFS
    """

    def snapcreate(self, m):
        """ create a snapshot for m
        add self.snaps[m] with
            dict(snapinfo key val)
        """
        dom, fset = m.device.split('#')
        o = rcAdvfs.Fdmns()
        try:
            d = o.get_fdmn(dom)
        except rcAdvfs.ExInit:
            raise ex.syncNotSnapable
        if fset not in d.fsets:
            raise ex.syncNotSnapable
        clonefset = fset +'@osvc_sync'
        mountPoint = m.mountPoint
        snapMountPoint = '/opt/opensvc/tmp/clonefset/%s/%s/osvc_sync'%(m.svc.svcname,mountPoint)
        snapMountPoint = os.path.normpath(snapMountPoint)
        if not os.path.exists(snapMountPoint):
            try:
                os.makedirs(snapMountPoint)
                self.log.info('create directory %s'%snapMountPoint)
            except:
                self.log.error('failed to create directory %s'%snapMountPoint)
                raise ex.syncSnapCreateError
        clonedev = '#'.join((dom, clonefset))
        if Mounts().has_mount(clonedev, snapMountPoint):
            cmd = ['fuser', '-kcv', snapMountPoint]
            (ret, out, err) = self.vcall(cmd, err_to_info=True)
            cmd = ['umount', snapMountPoint]
            (ret, out, err) = self.vcall(cmd)
            if ret != 0:
                raise ex.excError
        if clonefset in d.fsets:
            (ret, buff, err) = self.vcall(['rmfset', '-f', dom, clonefset])
            if ret != 0:
                raise ex.syncSnapDestroyError
        (ret, buff, err) = self.vcall(['clonefset', dom, fset, clonefset])
        if ret != 0:
            raise ex.syncSnapCreateError
        (ret, buff, err) = self.vcall(['mount', '-t', 'advfs', clonedev, snapMountPoint])
        if ret != 0:
            raise ex.syncSnapCreateError
        self.snaps[mountPoint]={'snap_mnt' : snapMountPoint, \
                                'snapdev' : clonedev }

    def snapdestroykey(self, snap_key):
        """ destroy a snapshot for a mountPoint
        """
        clonedev = self.snaps[snap_key]['snapdev']
        dom, clonefset = clonedev.split('#')
        o = rcAdvfs.Fdmns()
        try:
            d = o.get_fdmn(dom)
        except rcAdvfs.ExInit:
            raise ex.syncSnapDestroyError
        if clonefset not in d.fsets:
            return

        if protected_mount(self.snaps[snap_key]['snap_mnt']):
            self.log.error("the clone fset is no longer mounted in %s. panic."%self.snaps[snap_key]['snap_mnt'])
            raise ex.excError
        cmd = ['fuser', '-kcv', self.snaps[snap_key]['snap_mnt']]
        (ret, out, err) = self.vcall(cmd, err_to_info=True)
        cmd = ['umount', self.snaps[snap_key]['snap_mnt']]
        (ret, out, err) = self.vcall(cmd)
        if ret != 0:
            raise ex.excError

        (ret, buff, err) = self.vcall(['rmfset', '-f', dom, clonefset])
        if ret != 0:
            raise ex.syncSnapDestroyError
 0707010000bc83000081a400000000000000000000000154fc7bfb0000133f000000660000000300000000000000000000002400000000root/opt/opensvc/lib/snapJfs2AIX.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os

from rcGlobalEnv import rcEnv
from rcUtilities import which, qcall, protected_mount
import rcExceptions as ex
import snap

class Snap(snap.Snap):
    def lv_exists(self, device):
        device = device.split("/")[-1]
        ret = qcall(['lslv', device], cache=True)
        if ret == 0:
            return True
        return False

    def lv_info(self, device):
        device = device.split("/")[-1]
        (ret, buff, err) = self.call(['lslv', device], cache=True)
        if ret != 0:
            return (None, None, None)
        vg_name = None
        lv_name = None
        lv_size = 0
        prev = ''
        prevprev = ''
        pp_unit = ''
        pps = 0
        pp_size = 0
        for word in buff.split():
            if prev == "GROUP:":
                vg_name = word
            if prev == "VOLUME:":
                lv_name = word
            if prev == "SIZE:":
                pp_size = int(word)
            if prevprev == "SIZE:":
                pp_unit = word
            if prev == "PPs:" and prevprev != "STALE":
                pps = int(word)
            prevprev = prev
            prev = word

        if pps == 0 or pp_size == 0 or pp_unit == '' or vg_name is None:
            self.log.error("logical volume %s information fetching error"%device)
            print("pps = ", pps)
            print("pp_size = ", pp_size)
            print("pp_unit = ", pp_unit)
            print("vg_name = ", vg_name)
            raise ex.excError

        if pp_unit == 'megabyte(s)':
            mult = 1
        elif pp_unit == 'gigabyte(s)':
            mult = 1024
        elif pp_unit == 'terabyte(s)':
            mult = 1024*1024
        else:
            self.log.error("unexpected logical volume PP size unit: %s"%pp_unit)
            raise ex.excError

        return (vg_name, lv_name, pps*pp_size*mult)

    def snapcreate(self, m):
        snap_name = ''
        snap_mnt = ''
        (vg_name, lv_name, lv_size) = self.lv_info(m.device)
        if lv_name is None:
            self.log.error("can not snap %s: not a logical volume"%m.device)
            raise ex.syncNotSnapable
        if len(lv_name) > 12:
            self.log.error("can not snap lv with name >12 chars")
            raise ex.excError
        snap_name = 'sy_'+os.path.basename(lv_name)
        if self.lv_exists(os.path.join(vg_name, snap_name)):
            self.log.error("snap of %s already exists"%(lv_name))
            raise ex.syncSnapExists
        print(lv_size)
        print(lv_size//10)
        (ret, buff, err) = self.vcall(['mklv', '-t', 'jfs2', '-y', snap_name, vg_name, str(lv_size//10)+'M'])
        if ret != 0:
            raise ex.syncSnapCreateError
        snap_mnt = '/service/tmp/osvc_sync_'+os.path.basename(vg_name)+'_'+os.path.basename(lv_name)
        if not os.path.exists(snap_mnt):
            os.makedirs(snap_mnt, 0o755)
        snap_dev = os.path.join(os.sep, 'dev', snap_name)
        (ret, buff, err) = self.vcall(['snapshot', '-o', 'snapfrom='+m.mountPoint, snap_dev])
        if ret != 0:
            raise ex.syncSnapMountError
        (ret, buff, err) = self.vcall(['mount', '-o', 'snapshot', snap_dev, snap_mnt])
        if ret != 0:
            raise ex.syncSnapMountError
        self.snaps[m.mountPoint] = dict(lv_name=lv_name,
                                        vg_name=vg_name,
                                        snap_name=snap_name,
                                        snap_mnt=snap_mnt,
                                        snap_dev=snap_dev)

    def snapdestroykey(self, s):
        if protected_mount(self.snaps[s]['snap_mnt']):
            self.log.error("the snapshot is no longer mounted in %s. panic."%self.snaps[s]['snap_mnt'])
            raise ex.excError

        """ fuser on HP-UX outs to stderr ...
        """
        cmd = ['fuser', '-c', '-x', '-k', self.snaps[s]['snap_mnt']]
        ret = qcall(cmd)

        cmd = ['umount', self.snaps[s]['snap_mnt']]
        (ret, out, err) = self.vcall(cmd)
        cmd = ['snapshot', '-d', self.snaps[s]['snap_dev']]
        (ret, buff, err) = self.vcall(cmd)

 0707010000bc84000081a400000000000000000000000154fc7bfb0000147f000000660000000300000000000000000000002500000000root/opt/opensvc/lib/snapLvmLinux.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os

from rcGlobalEnv import rcEnv
from rcUtilities import protected_mount, justcall, which
from rcUtilitiesLinux import lv_info, lv_exists
import rcExceptions as ex
import snap

class Snap(snap.Snap):
    def mntopt_and_ro(self, m):
        opt_set = set([])
        if m.fsType == "xfs":
            opt_set.add("nouuid")
        if m.mntOpt is None:
            opt_set.add("ro")
            return ','.join(opt_set)
        opt_set |= set(m.mntOpt.split(','))
        opt_set -= set(['rw', 'ro'])
        opt_set |= set(['ro'])
        return ','.join(opt_set)

    def snapcreate(self, m):
        snap_name = ''
        snap_mnt = ''
        (vg_name, lv_name, lv_size) = lv_info(self, m.device)
        if lv_name is None:
            self.log.error("can not snap %s: not a logical volume"%m.device)
            raise ex.syncNotSnapable
        snap_name = 'osvc_sync_'+lv_name
        if lv_exists(self, os.path.join(os.sep, 'dev', vg_name, snap_name)):
            self.log.error("snap of %s already exists"%(lv_name))
            raise ex.syncSnapExists

        if m.snap_size is not None:
            snap_size = m.snap_size
        else:
            snap_size = int(lv_size//10)

        cmd = ['lvcreate', '-A', 'n', '-s', '-L'+str(snap_size)+'M', '-n', snap_name, os.path.join(vg_name, lv_name)]
        self.log.info(' '.join(cmd))
        out, err, ret = justcall(cmd)
        err_l1 = err.split('\n')
        err_l2 = []
        out_l = out.split('\n')
        for e in err_l1:
            if 'This metadata update is NOT backed up' in e:
                pass
            else:
                err_l2.append(e)
        err = '\n'.join(err_l2)
        out = '\n'.join(out_l)
        if len(out) > 0:
            self.log.info(out)
        if len(err) > 0:
            self.log.error(err)
        if ret != 0:
            raise ex.syncSnapCreateError
        snap_mnt = os.path.join(rcEnv.pathtmp,
                                'osvc_sync_'+vg_name+'_'+lv_name)
        if not os.path.exists(snap_mnt):
            os.makedirs(snap_mnt, 0o755)
        snap_dev = os.path.join(os.sep, 'dev', vg_name, snap_name)
        if m.fsType != "xfs":
            self.vcall(['fsck', '-a', snap_dev], err_to_warn=True)
        (ret, buff, err) = self.vcall(['mount', '-t', m.fsType, '-o', self.mntopt_and_ro(m), snap_dev, snap_mnt])
        if ret != 0:
            raise ex.syncSnapMountError
        self.snaps[m.mountPoint] = dict(lv_name=lv_name,
                                        vg_name=vg_name,
                                        snap_name=snap_name,
                                        snap_mnt=snap_mnt,
                                        snap_dev=snap_dev)

    def udev_settle(self):
        if which('udevadm') is None:
            return
        cmd = ['udevadm', 'settle']
        self.vcall(cmd)

    def snapdestroykey(self, s):
        if protected_mount(self.snaps[s]['snap_mnt']):
            self.log.error("the snapshot is no longer mounted in %s. panic."%self.snaps[s]['snap_mnt'])
            raise ex.excError
        cmd = ['fuser', '-kmv', self.snaps[s]['snap_mnt']]
        (ret, out, err) = self.vcall(cmd, err_to_info=True)
        cmd = ['umount', self.snaps[s]['snap_mnt']]
        (ret, out, err) = self.vcall(cmd)

        self.udev_settle()
        cmd = ['lvremove', '-A', 'n', '-f', self.snaps[s]['snap_dev']]
        self.log.info(' '.join(cmd))
        for i in range(1, 30):
            out, err, ret = justcall(cmd)
            if ret == 0:
                break
        err_l1 = err.split('\n')
        err_l2 = []
        out_l = out.split('\n')
        for e in err_l1:
            if 'This metadata update is NOT backed up' in e:
                pass
            elif 'Falling back to direct link removal.' in e:
                out_l.append(e)
            elif 'Falling back to direct node removal.' in e:
                out_l.append(e)
            else:
                err_l2.append(e)
        err = '\n'.join(err_l2)
        out = '\n'.join(out_l)
        if len(out) > 0:
            self.log.info(out)
        if len(err) > 0:
            self.log.error(err)
        if ret != 0:
            self.log.error("failed to remove snapshot %s (attempts: %d)"%(self.snaps[s]['snap_dev'], i))
        elif i > 1:
            self.log.info("successfully removed snapshot %s (attempts: %d)"%(self.snaps[s]['snap_dev'], i))
        del(self.snaps[s])

 0707010000bc85000081a400000000000000000000000154fc7bfb00000df2000000660000000300000000000000000000002600000000root/opt/opensvc/lib/snapVxfsHP-UX.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os

from rcGlobalEnv import rcEnv
from rcUtilities import which, qcall, protected_mount
import rcExceptions as ex
import snap

class Snap(snap.Snap):
    def lv_exists(self, device):
        if qcall(['lvdisplay', device]) == 0:
            return True
        return False

    def lv_info(self, device):
        (ret, buff, err) = self.call(['lvdisplay', device])
        if ret != 0:
            return (None, None, None)
        vg_name = None
        lv_name = None
        lv_size = 0
        for line in buff.split('\n'):
            if "VG Name" in line:
                vg_name = line.split()[-1]
            if "LV Name" in line:
                lv_name = line.split()[-1]
            if "LV Size" in line:
                lv_size = int(line.split()[-1])
        return (vg_name, lv_name, lv_size)

    def snapcreate(self, m):
        snap_name = ''
        snap_mnt = ''
        (vg_name, lv_name, lv_size) = self.lv_info(m.device)
        if lv_name is None:
            self.log.error("can not snap %s: not a logical volume"%m.device)
            raise ex.syncNotSnapable
        snap_name = 'osvc_sync_'+os.path.basename(lv_name)
        if self.lv_exists(os.path.join(vg_name, snap_name)):
            self.log.error("snap of %s already exists"%(lv_name))
            raise ex.syncSnapExists
        (ret, buff, err) = self.vcall(['lvcreate', '-L', str(lv_size//10)+'M', '-n', snap_name, vg_name])
        if ret != 0:
            raise ex.syncSnapCreateError
        snap_mnt = '/service/tmp/osvc_sync_'+os.path.basename(vg_name)+'_'+os.path.basename(lv_name)
        if not os.path.exists(snap_mnt):
            os.makedirs(snap_mnt, 0o755)
        snap_dev = os.path.join(vg_name, snap_name)
        (ret, buff, err) = self.vcall(['mount', '-F', 'vxfs', '-o', 'ro,snapof='+m.device, snap_dev, snap_mnt])
        if ret != 0:
            raise ex.syncSnapMountError
        self.snaps[m.mountPoint] = dict(lv_name=lv_name,
                                        vg_name=vg_name,
                                        snap_name=snap_name,
                                        snap_mnt=snap_mnt,
                                        snap_dev=snap_dev)

    def snapdestroykey(self, s):
        if protected_mount(self.snaps[s]['snap_mnt']):
            self.log.error("the snapshot is no longer mounted in %s. panic."%self.snaps[s]['snap_mnt'])
            raise ex.excError

        """ fuser on HP-UX outs to stderr ...
        """
        cmd = ['fuser', '-kc', self.snaps[s]['snap_mnt']]
        ret = qcall(cmd)

        cmd = ['umount', self.snaps[s]['snap_mnt']]
        (ret, out, err) = self.vcall(cmd)
        cmd = ['lvremove', '-f', self.snaps[s]['snap_dev']]
        (ret, buff, err) = self.vcall(cmd)

  0707010000bc86000081a400000000000000000000000154fc7bfb0000088d000000660000000300000000000000000000002500000000root/opt/opensvc/lib/snapZfsSunOS.py  #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from rcUtilities import justcall
import rcExceptions as ex
import snap
from rcZfs import dataset_exists

class Snap(snap.Snap):
    """Defines a snap object with ZFS
    """

    def snapcreate(self, m):
        """ create a snapshot for m
        add self.snaps[m] with
            dict(snapinfo key val)
        """
        dataset = m.device
        if not dataset_exists(dataset, 'filesystem'):
            raise ex.syncNotSnapable
        snapdev = dataset +'@osvc_sync'
        mountPoint = m.mountPoint
        snapMountPoint= mountPoint + '/.zfs/snapshot/osvc_sync/'
        if dataset_exists(snapdev, 'snapshot'):
            (ret, buff, err) = self.vcall(['zfs', 'destroy', snapdev ])
            if ret != 0:
                raise ex.syncSnapDestroyError
        (ret, buff, err) = self.vcall(['zfs', 'snapshot', snapdev ])
        if ret != 0:
            raise ex.syncSnapCreateError
        self.snaps[mountPoint]={'snap_mnt' : snapMountPoint, \
                                'snapdev' : snapdev }

    def snapdestroykey(self, snap_key):
        """ destroy a snapshot for a mountPoint
        """
        snapdev = self.snaps[snap_key]['snapdev']
        if not dataset_exists(snapdev, 'snapshot'):
            return
        (ret, buff, err) = self.vcall(['zfs', 'destroy', snapdev ])
        if ret != 0:
            raise ex.syncSnapDestroyError
   0707010000bc87000081a400000000000000000000000154fc7bfb000166c9000000660000000300000000000000000000001c00000000root/opt/opensvc/lib/svc.py   #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

from __future__ import print_function
from resources import Resource, ResourceSet
from freezer import Freezer
import rcStatus
from rcGlobalEnv import rcEnv
from rcUtilities import justcall
from svcBuilder import conf_get_string_scope, conf_get_boolean_scope
import rcExceptions as ex
import xmlrpcClient
import sys
import os
import signal
import lock
import rcLogger
import logging
import datetime
import node

def signal_handler(signum, frame):
    raise ex.excSignal

class Options(object):
    def __init__(self):
        self.slaves = False
        self.slave = None
        self.master = False
        self.cron = False
        self.force = False
        self.remote = False
        self.ignore_affinity = False
        self.debug = False
        self.disable_rollback = False
        self.moduleset = ""
        self.module = ""
        self.ruleset_date = ""
        self.dry_run = False
        self.refresh = False
        os.environ['LANG'] = 'C'

class Svc(Resource):
    """Service class define a Service Resource
    It contain list of ResourceSet where each ResourceSets contain same resource
    type
    """

    def __init__(self, svcname=None, type="hosted", optional=False, disabled=False, tags=set([])):
        """usage : aSvc=Svc(type)"""
        self.encap = False
        self.encapnodes = set([])
        self.has_encap_resources = False
        self.options = Options()
        self.node = None
        self.ha = False
        self.sync_dblogger = False
        self.svcname = svcname
        self.containerize = True
        self.hostid = rcEnv.nodename
        self.resSets = []
        self.type2resSets = {}
        self.disks = set([])
        self.devs = set([])
        self.cron = False
        self.force = False
        self.cluster = False
        self.pathenv = os.path.join(rcEnv.pathetc, self.svcname+'.env')
        self.push_flag = os.path.join(rcEnv.pathvar, svcname+'.push')
        self.status_types = ["container.hpvm",
                             "container.kvm",
                             "container.amazon",
                             "container.openstack",
                             "container.vcloud",
                             "container.xen",
                             "container.esx",
                             "container.ovm",
                             "container.lxc",
                             "container.docker",
                             "container.vz",
                             "container.srp",
                             "container.zone",
                             "container.jail",
                             "container.ldom",
                             "container.vbox",
                             "disk.drbd",
                             "disk.loop",
                             "disk.gandi",
                             "disk.scsireserv",
                             "disk.lock",
                             "disk.vg",
                             "disk.lv",
                             "disk.zpool",
                             "share.nfs",
                             "fs",
                             "ip",
                             "sync.rsync",
                             "sync.symclone",
                             "sync.rados",
                             "sync.symsrdfs",
                             "sync.hp3par",
                             "sync.ibmdssnap",
                             "sync.evasnap",
                             "sync.necismsnap",
                             "sync.dcssnap",
                             "sync.dcsckpt",
                             "sync.dds",
                             "sync.zfs",
                             "sync.btrfs",
                             "sync.docker",
                             "sync.netapp",
                             "sync.nexenta",
                             "app",
                             "hb.openha",
                             "hb.sg",
                             "hb.rhcs",
                             "hb.vcs",
                             "hb.ovm",
                             "hb.linuxha"]
        Resource.__init__(self, type=type, optional=optional,
                          disabled=disabled, tags=tags)
        self.log = rcLogger.initLogger(self.svcname.upper())
        self.freezer = Freezer(svcname)
        self.scsirelease = self.prstop
        self.scsireserv = self.prstart
        self.scsicheckreserv = self.prstatus
        self.resources_by_id = {}
        self.rset_status_cache = None
        self.presync_done = False
        self.presnap_trigger = None
        self.postsnap_trigger = None
        self.lockfd = None
        self.action_start_date = datetime.datetime.now()
        self.monitor_action = None
        self.group_status_cache = None

    def __cmp__(self, other):
        """order by service name
        """
        return cmp(self.svcname, other.svcname)

    def purge_status_last(self):
        for rset in self.resSets:
            rset.purge_status_last()

    def get_subset_parallel(self, rtype):
        subset_section = 'subset#' + rtype
        if not hasattr(self, "config"):
            self.load_config()
        if not self.config.has_section(subset_section):
            return False
        try:
            return conf_get_boolean_scope(self, self.config, subset_section, "parallel")
        except Exception as e:
            return False

    def __iadd__(self, r):
        """svc+=aResourceSet
        svc+=aResource
        """
        if r.subset is not None:
            # the resource wants to be added to a specific resourceset
            # for action grouping, parallel execution or sub-resource
            # triggers
            rtype = "%s:%s" % (r.type, r.subset)
        else:
            rtype = r.type

        if rtype in self.type2resSets:
            self.type2resSets[rtype] += r

        elif hasattr(r, 'resources'):
            # this is a ResourceSet or ResourceSet-derived class
            self.resSets.append(r)
            self.type2resSets[rtype] = r

        elif isinstance(r, Resource):
            parallel = self.get_subset_parallel(rtype)
            if hasattr(r, 'rset_class'):
                R = r.rset_class(type=rtype, resources=[r], parallel=parallel)
            else:
                R = ResourceSet(type=rtype, resources=[r], parallel=parallel)
            self.__iadd__(R)

        else:
            # Error
            pass

        if isinstance(r, Resource):
            self.resources_by_id[r.rid] = r

        r.svc = self
        import logging
        r.log = logging.getLogger(r.log_label())

        if r.type.startswith("hb"):
            self.ha = True

        if not r.disabled and hasattr(r, "on_add"):
            r.on_add()

        return self

    def dblogger(self, action, begin, end, actionlogfile):
        self.node.collector.call('end_action', self, action, begin, end, actionlogfile, sync=self.sync_dblogger)
        g_vars, g_vals, r_vars, r_vals = self.svcmon_push_lists()
        self.node.collector.call('svcmon_update_combo', g_vars, g_vals, r_vars, r_vals, sync=self.sync_dblogger)
        os.unlink(actionlogfile)
        try:
            logging.shutdown()
        except:
            pass

    def svclock(self, action=None, timeout=30, delay=5):
        suffix = None
        list_actions_no_lock = [
          'docker',
          'push',
          'push_appinfo',
          'print_status',
          'print_resource_status',
          'status',
          'freeze',
          'frozen',
          'thaw',
          'get',
          'freezestop',
          'print_env_mtime',
          'print_disklist',
          'print_devlist',
          'print_config',
          'edit_config',
          'json_status',
          'json_disklist',
          'json_devlist',
          'json_env'
        ]
        if action in list_actions_no_lock:
            # no need to serialize this action
            return
        if action.startswith("collector"):
            # no need to serialize collector gets
            return
        if action.startswith("compliance"):
            # compliance modules are allowed to execute actions on the service
            # so give them their own lock
            suffix = "compliance"
        if self.lockfd is not None:
            # already acquired
            return
        lockfile = os.path.join(rcEnv.pathlock, self.svcname)
        if suffix is not None:
            lockfile = ".".join((lockfile, suffix))
        try:
            lockfd = lock.lock(timeout=timeout, delay=delay, lockfile=lockfile)
        except lock.lockTimeout:
            raise ex.excError("timed out waiting for lock")
        except lock.lockNoLockFile:
            raise ex.excError("lock_nowait: set the 'lockfile' param")
        except lock.lockCreateError:
            raise ex.excError("can not create lock file %s"%lockfile)
        except lock.lockAcquire as e:
            raise ex.excError("another action is currently running (pid=%s)"%e.pid)
        except ex.excSignal:
            raise ex.excError("interrupted by signal")
        except:
            import traceback
            traceback.print_exc()
            raise ex.excError("unexpected locking error")
        if lockfd is not None:
            self.lockfd = lockfd

    def svcunlock(self):
        lock.unlock(self.lockfd)
        self.lockfd = None

    def setup_signal_handlers(self):
        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

    def get_resources(self, _type=None, strict=False, discard_disabled=True):
         if _type is None:
             rsets = self.resSets
         else:
             rsets = self.get_res_sets(_type, strict=strict)

         resources = []
         for rs in rsets:
             for r in rs.resources:
                 if not self.encap and 'encap' in r.tags:
                     continue
                 if discard_disabled and r.disabled:
                     continue
                 resources.append(r)
         return resources

    def get_res_sets(self, _type, strict=False):
         if not isinstance(_type, list):
             l = [_type]
         else:
             l = _type
         rsets = []
         for t in l:
             for rs in self.resSets:
                 if ':' in rs.type:
                     # remove subset suffix
                     rstype = rs.type[:rs.type.index(':')]
                 else:
                     rstype = rs.type
                 if '.' in t:
                     # exact match
                     if rstype == t:
                         rsets.append(rs)
                 elif '.' in rstype and not strict:
                     # group match
                     _t = rstype.split('.')
                     if _t[0] == t:
                         rsets.append(rs)
                 else:
                     if rstype == t:
                         rsets.append(rs)
         rsets.sort()
         return rsets

    def has_res_set(self, type, strict=False):
        if len(self.get_res_sets(type, strict=strict)) > 0:
            return True
        else:
            return False

    def all_set_action(self, action=None, tags=set([])):
        """Call action on each member of the subset of specified type
        """
        self.set_action(self.resSets, action=action, tags=tags)

    def sub_set_action(self, type=None, action=None, tags=set([]), xtags=set([]), strict=False):
        """Call action on each member of the subset of specified type
        """
        self.set_action(self.get_res_sets(type, strict=strict), action=action, tags=tags, xtags=xtags)

    def need_snap_trigger(self, sets, action):
        if action not in ["syncnodes", "syncdrp", "syncresync", "syncupdate"]:
            return False
        for rs in sets:
            for r in rs.resources:
                """ avoid to run pre/post snap triggers when there is no
                    resource flagged for snap and on drpnodes
                """
                if hasattr(r, "snap") and r.snap is True and \
                   rcEnv.nodename in self.nodes:
                    return True
        return False

    def set_action(self, sets=[], action=None, tags=set([]), xtags=set([]), strict=False):
        """ TODO: r.is_optional() not doing what's expected if r is a rset
        """
        list_actions_no_pre_action = [
          "delete",
          "enable",
          "disable",
          "status",
          "print_status",
          'print_resource_status',
          "print_disklist",
          "print_devlist",
          'print_config',
          'edit_config',
          "json_disklist",
          "json_devlist",
          "json_status",
          "json_env",
          "push_appinfo",
          "push",
          "group_status",
          "presync",
          "postsync",
          "freezestop",
          "resource_monitor"
        ]
        list_actions_no_post_action = list_actions_no_pre_action

        ns = self.need_snap_trigger(sets, action)

        """ snapshots are created in pre_action and destroyed in post_action
            place presnap and postsnap triggers around pre_action
        """
        if ns and self.presnap_trigger is not None:
            (ret, out, err) = self.vcall(self.presnap_trigger)
            if ret != 0:
                raise ex.excError

        """ Multiple resourcesets of the same type need to be sorted
            so that the start and stop action happen in a predictible order.
            Sort alphanumerically on reseourceset type.

            Exemple, on start:
             app
             app.1
             app.2
            on stop:
             app.2
             app.1
             app
        """
        if "stop" in action or action in ("rollback", "shutdown"):
            reverse = True
        else:
            reverse = False
        sets = sorted(sets, lambda x, y: cmp(x.type, y.type), reverse=reverse)

        for r in sets:
            if action in list_actions_no_pre_action or r.skip:
                break
            try:
                r.log.debug("start %s pre_action"%r.type)
                r.pre_action(r, action)
            except ex.excError:
                raise
            except ex.excAbortAction:
                continue
            except:
                self.save_exc()
                raise ex.excError

        if ns and self.postsnap_trigger is not None:
            (ret, out, err) = self.vcall(self.postsnap_trigger)
            if ret != 0:
                raise ex.excError(err)

        for r in sets:
            self.log.debug('set_action: action=%s rset=%s'%(action, r.type))
            r.action(action, tags=tags, xtags=xtags)

        for r in sets:
            if action in list_actions_no_post_action or r.skip:
                break
            try:
                r.log.debug("start %s post_action"%r.type)
                r.post_action(r, action)
            except ex.excError:
                raise
            except ex.excAbortAction:
                continue
            except:
                self.save_exc()
                raise ex.excError


    def __str__(self):
        output="Service %s available resources:" % (Resource.__str__(self))
        for k in self.type2resSets.keys() : output += " %s" % k
        output+="\n"
        for r in self.resSets:  output+= "  [%s]" % (r.__str__())
        return output

    def status(self):
        """aggregate status a service
        """
        ss = rcStatus.Status()
        for r in self.get_res_sets(self.status_types, strict=True):
            if not self.encap and 'encap' in r.tags:
                continue
            if "sync." not in r.type:
                ss += r.status()
            else:
                """ sync are expected to be up
                """
                s = r.status()
                if s == rcStatus.UP:
                    ss += rcStatus.UNDEF
                elif s in [rcStatus.NA, rcStatus.UNDEF, rcStatus.TODO]:
                    ss += s
                else:
                    ss += rcStatus.WARN
        if ss.status == rcStatus.STDBY_UP_WITH_UP:
            ss.status = rcStatus.UP
        elif ss.status == rcStatus.STDBY_UP_WITH_DOWN:
            ss.status = rcStatus.STDBY_UP
        return ss.status

    def json_status(self):
        import json
        d = {
              'resources': {},
            }

        containers = self.get_resources('container')
        if len(containers) > 0:
            d['encap'] = {}
            for container in containers:
                try:
                    d['encap'][container.name] = self.encap_json_status(container)
                except:
                    d['encap'][container.name] = {'resources': {}}

        for rs in self.get_res_sets(self.status_types, strict=True):
            for r in rs.resources:
                rid, status, label, log, monitor, disable, optional, encap = r.status_quad()
                d['resources'][rid] = {'status': status,
                                       'label': label,
                                       'log':log,
                                       'monitor':monitor,
                                       'disable': disable,
                                       'optional': optional,
                                       'encap': encap}
        ss = self.group_status()
        for g in ss:
            d[g] = str(ss[g])
        print(json.dumps(d))

    def json_env(self):
        import json
        svcenv = {}
        tmp = {}
        self.load_config()
        config = self.config

        defaults = config.defaults()
        for key in defaults.iterkeys():
            tmp[key] = defaults[key]

        svcenv['DEFAULT'] = tmp
        config._defaults = {}

        sections = config.sections()
        for section in sections:
            options = config.options(section)
            tmpsection = {}
            for option in options:
                if config.has_option(section, option):
                    tmpsection[option] = config.get(section, option)
            svcenv[section] = tmpsection
        print(json.dumps(svcenv))

    def print_resource_status(self):
        if len(self.action_rid) != 1:
            print("only one resource id is allowed", file=sys.stderr)
            return 1
        for rid in self.action_rid:
            if rid not in self.resources_by_id:
                print("resource not found")
                continue
            r = self.resources_by_id[rid]
            print(rcStatus.colorize(rcStatus.status_str(r.status())))
        return 0

    def print_status(self):
        """print() each resource status for a service
        """
        from textwrap import wrap

        def print_res(e, fmt, pfx, subpfx=None):
            if subpfx is None:
                subpfx = pfx
            rid, status, label, log, monitor, disabled, optional, encap = e
            flags = ''
            flags += 'M' if monitor else '.'
            flags += 'D' if disabled else '.'
            flags += 'O' if optional else '.'
            flags += 'E' if encap else '.'
            print(fmt%(rid, flags, rcStatus.colorize(status), label))
            if len(log) > 0:
                print('\n'.join(wrap(log,
                                     initial_indent = subpfx,
                                     subsequent_indent = subpfx,
                                     width=78
                                    )
                               )
                )

        print(self.svcname)
        fmt = "%-20s %4s %-10s %s"
        print(fmt%("overall", '', rcStatus.colorize(self.group_status()['overall']), ''))
        fmt = "|- %-17s %4s %-10s %s"
        print(fmt%("avail", '', rcStatus.colorize(self.group_status()['avail']), ''))

        encap_res_status = {}
        for container in self.get_resources('container'):
            try:
                res = self.encap_json_status(container)['resources']
                encap_res_status[container.rid] = res
            except ex.excNotAvailable as e:
                encap_res_status[container.rid] = {}
            except Exception as e:
                print(e)
                encap_res_status[container.rid] = {}

        l = []
        cr = {}
        for rs in self.get_res_sets(self.status_types, strict=True):
            for r in [_r for _r in sorted(rs.resources) if not _r.rid.startswith('sync') and not _r.rid.startswith('hb')]:
                rid, status, label, log, monitor, disable, optional, encap = r.status_quad()
                l.append((rid, status, label, log, monitor, disable, optional, encap))
                if rid.startswith("container") and rid in encap_res_status:
                    _l = []
                    for _rid, val in encap_res_status[rid].items():
                        _l.append((_rid, val['status'], val['label'], val['log'], val['monitor'], val['disable'], val['optional'], val['encap']))
                    cr[rid] = _l

        last = len(l) - 1
        if last >= 0:
            for i, e in enumerate(l):
                if i == last:
                    fmt = "|  '- %-14s %4s %-10s %s"
                    pfx = "|     %-14s %4s %-10s "%('','','')
                    print_res(e, fmt, pfx)
                else:
                    fmt = "|  |- %-14s %4s %-10s %s"
                    pfx = "|  |  %-14s %4s %-10s "%('','','')
                    if e[0] in cr:
                        subpfx = "|  |  |  %-11s %4s %-10s "%('','','')
                    else:
                        subpfx = None
                    print_res(e, fmt, pfx, subpfx=subpfx)
                if e[0] in cr:
                    _last = len(cr[e[0]]) - 1
                    if _last >= 0:
                        for _i, _e in enumerate(cr[e[0]]):
                            if _i == _last:
                                fmt = "|  |  '- %-11s %4s %-10s %s"
                                pfx = "|  |     %-11s %4s %-10s "%('','','')
                                print_res(_e, fmt, pfx)
                            else:
                                fmt = "|  |  |- %-11s %4s %-10s %s"
                                pfx = "|  |  |  %-11s %4s %-10s "%('','','')
                                print_res(_e, fmt, pfx)

        fmt = "|- %-17s %4s %-10s %s"
        print(fmt%("sync", '', rcStatus.colorize(str(self.group_status()['sync'])), ''))

        l = []
        for rs in self.get_res_sets(self.status_types, strict=True):
            for r in [_r for _r in sorted(rs.resources) if _r.rid.startswith('sync')]:
                rid, status, label, log, monitor, disable, optional, encap = r.status_quad()
                if rid in encap_res_status:
                    status = rcStatus.Status(rcStatus.status_value(encap_res_status[rid]['status']))
                l.append((rid, status, label, log, monitor, disable, optional, encap))
        last = len(l) - 1
        if last >= 0:
            for i, e in enumerate(l):
                if i == last:
                    fmt = "|  '- %-14s %4s %-10s %s"
                    pfx = "|     %-14s %4s %-10s "%('','','')
                    print_res(e, fmt, pfx)
                else:
                    fmt = "|  |- %-14s %4s %-10s %s"
                    pfx = "|  |  %-14s %4s %-10s "%('','','')
                    print_res(e, fmt, pfx)

        fmt = "'- %-17s %4s %-10s %s"
        print(fmt%("hb", '', rcStatus.colorize(str(self.group_status()['hb'])), ''))

        l = []
        for rs in self.get_res_sets(self.status_types):
            for r in [_r for _r in sorted(rs.resources) if _r.rid.startswith('hb')]:
                rid, status, label, log, monitor, disable, optional, encap = r.status_quad()
                if rid in encap_res_status:
                    status = rcStatus.Status(rcStatus.status_value(encap_res_status[rid]['status']))
                l.append((rid, status, label, log, monitor, disable, optional, encap))
        last = len(l) - 1
        if last >= 0:
            for i, e in enumerate(l):
                if i == last:
                    fmt = "   '- %-14s %4s %-10s %s"
                    pfx = "      %-14s %4s %-10s "%('','','')
                    print_res(e, fmt, pfx)
                else:
                    fmt = "   |- %-14s %4s %-10s %s"
                    pfx = "   |  %-14s %4s %-10s "%('','','')
                    print_res(e, fmt, pfx)

    def svcmon_push_lists(self, status=None):
        if status is None:
            status = self.group_status()

        if self.frozen():
            frozen = "1"
        else:
            frozen = "0"

        r_vars=["svcname",
                "nodename",
                "vmname",
                "rid",
                "res_desc",
                "res_status",
                "updated",
                "res_log"]
        r_vals = []
        import datetime
        now = datetime.datetime.now()

        for rs in self.resSets:
            for r in rs.resources:
                if 'encap' in r.tags:
                    continue
                rstatus = rcStatus.status_str(r.rstatus)
                r_vals.append([repr(self.svcname),
                               repr(rcEnv.nodename),
                               "",
                               repr(r.rid),
                               repr(r.label),
                               repr(str(rstatus)),
                               repr(str(now)),
                               r.status_log_str])

        g_vars=["mon_svcname",
                "mon_svctype",
                "mon_nodname",
                "mon_vmname",
                "mon_vmtype",
                "mon_nodtype",
                "mon_ipstatus",
                "mon_diskstatus",
                "mon_syncstatus",
                "mon_hbstatus",
                "mon_containerstatus",
                "mon_fsstatus",
                "mon_sharestatus",
                "mon_appstatus",
                "mon_availstatus",
                "mon_overallstatus",
                "mon_updated",
                "mon_prinodes",
                "mon_frozen"]

        containers = self.get_resources('container')
        if len(containers) == 0:
            g_vals=[self.svcname,
                    self.svctype,
                    rcEnv.nodename,
                    "",
                    "hosted",
                    rcEnv.host_mode,
                    str(status["ip"]),
                    str(status["disk"]),
                    str(status["sync"]),
                    str(status["hb"]),
                    str(status["container"]),
                    str(status["fs"]),
                    str(status["share"]),
                    str(status["app"]),
                    str(status["avail"]),
                    str(status["overall"]),
                    str(now),
                    ' '.join(self.nodes),
                    frozen]
        else:
            g_vals = []
            for container in containers:
                encap_res_status = {}
                try:
                    encap_res_status = self.encap_json_status(container)
                except ex.excNotAvailable as e:
                    encap_res_status = {'resources': [],
                                        'ip': 'n/a',
                                        'disk': 'n/a',
                                        'sync': 'n/a',
                                        'hb': 'n/a',
                                        'container': 'n/a',
                                        'fs': 'n/a',
                                        'share': 'n/a',
                                        'app': 'n/a',
                                        'avail': 'n/a',
                                        'overall': 'n/a'}
                except Exception as e:
                    print(e)
                    continue

                for rid in encap_res_status['resources']:
                    rstatus = encap_res_status['resources'][rid]['status']
                    r_vals.append([repr(self.svcname),
                                   repr(rcEnv.nodename),
                                   repr(container.name),
                                   repr(str(rid)),
                                   repr(str(encap_res_status['resources'][rid]['label'])),
                                   repr(str(rstatus)),
                                   repr(str(now)),
                                   repr(str(encap_res_status['resources'][rid]['log']))])

                if 'avail' not in status or 'avail' not in encap_res_status:
                    continue

                g_vals.append([self.svcname,
                               self.svctype,
                               rcEnv.nodename,
                               container.name,
                               container.type.replace('container.', ''),
                               rcEnv.host_mode,
                               str(status["ip"]+rcStatus.Status(encap_res_status['ip'])),
                               str(status["disk"]+rcStatus.Status(encap_res_status['disk'])),
                               str(status["sync"]+rcStatus.Status(encap_res_status['sync'])),
                               str(status["hb"]+rcStatus.Status(encap_res_status['hb'])),
                               str(status["container"]+rcStatus.Status(encap_res_status['container'])),
                               str(status["fs"]+rcStatus.Status(encap_res_status['fs'])),
                               str(status["share"]+rcStatus.Status(encap_res_status['share'] if 'share' in encap_res_status else 'n/a')),
                               str(status["app"]+rcStatus.Status(encap_res_status['app'])),
                               str(status["avail"]+rcStatus.Status(encap_res_status['avail'])),
                               str(status["overall"]+rcStatus.Status(encap_res_status['overall'])),
                               str(now),
                               ' '.join(self.nodes),
                               frozen,
                               str(container.name)])

        return g_vars, g_vals, r_vars, r_vals

    def get_rset_status(self, groups):
        self.setup_environ()
        rset_status = {}
        for t in self.status_types:
            g = t.split('.')[0]
            if g not in groups:
                continue
            for rs in self.get_res_sets(t, strict=True):
                rset_status[rs.type] = rs.status()
        return rset_status

    def resource_monitor(self):
        self.options.refresh = True
        if self.group_status_cache is None:
            self.group_status(excluded_groups=set(['sync']))
        if not self.ha:
            self.log.debug("no active heartbeat resource. no need to check monitored resources.")
            return
        hb_status = self.group_status_cache['hb']
        if hb_status.status != rcStatus.UP:
            self.log.debug("heartbeat status is not up. no need to check monitored resources.")
            return

        monitored_resources = []
        for r in self.get_resources():
            if r.monitor:
                monitored_resources.append(r)

        if len(monitored_resources) == 0:
            self.log.debug("no monitored resource")
            return

        for r in monitored_resources:
            if r.rstatus != rcStatus.UP:
                if self.monitor_action is not None and \
                   hasattr(self, self.monitor_action):
                    if len(r.status_log_str) > 0:
                        rstatus_log = ''.join((' ', '(', r.status_log_str.strip().strip("# "), ')'))
                    else:
                        rstatus_log = ''
                    self.log.info("monitored resource %s is in state %s%s"%(r.rid, rcStatus.status_str(r.rstatus), rstatus_log))
                    raise self.exMonitorAction
                else:
                    self.log.info("Would TOC but no (or unknown) resource monitor action set.")
                return

        self.log.debug("monitored resources are up")

    class exMonitorAction(Exception):
        pass

    def reboot(self):
        self.node.os.reboot()

    def crash(self):
        self.node.os.crash()

    def freezestop(self):
        self.sub_set_action('hb.openha', 'freezestop')

    def stonith(self):
        self.sub_set_action('stonith.ilo', 'start')
        self.sub_set_action('stonith.callout', 'start')

    def toc(self):
        self.log.info("start monitor action '%s'"%self.monitor_action)
        getattr(self, self.monitor_action)()

    def encap_cmd(self, cmd, verbose=False, error="raise"):
        for container in self.get_resources('container'):
            try:
                out, err, ret = self._encap_cmd(cmd, container, verbose=verbose)
            except ex.excEncapUnjoignable as e:
                if error != "continue":
                    self.log.error("container %s is not joinable to execute action '%s'"%(container.name, ' '.join(cmd)))
                    raise
                elif verbose:
                    self.log.warning("container %s is not joinable to execute action '%s'"%(container.name, ' '.join(cmd)))

    def _encap_cmd(self, cmd, container, verbose=False):
        vmhostname = container.vm_hostname()
        try:
            autostart_node = conf_get_string_scope(self, self.config, 'DEFAULT', 'autostart_node', impersonate=vmhostname).split()
        except:
            autostart_node = []
        if cmd == ["start"] and container.booted and vmhostname in autostart_node:
            self.log.info("skip encap service start in container %s: already started on boot"%vmhostname)
            return '', '', 0
        if not self.has_encap_resources or container.status() == rcStatus.DOWN:
            # no need to run encap cmd (no encap resource)
            return '', '', 0

        if self.options.slave is not None and not \
           (container.name in self.options.slave or \
            container.rid in self.options.slave):
            # no need to run encap cmd (container not specified in --slave)
            return '', '', 0

        if cmd == ['start'] and not self.need_start_encap(container):
            self.log.info("skip start in container %s: the encap service is configured to start on container boot."%container.name)
            return '', '', 0

        options = []
        if self.options.dry_run:
            options.append('--dry-run')
        if self.options.refresh:
            options.append('--refresh')

        cmd = ['/opt/opensvc/bin/svcmgr', '-s', self.svcname] + options + cmd

        if container is not None and hasattr(container, "rcmd"):
            out, err, ret = container.rcmd(cmd)
        elif hasattr(container, "runmethod"):
            cmd = container.runmethod + cmd
            out, err, ret = justcall(cmd)
        else:
            raise ex.excEncapUnjoignable("undefined rcmd/runmethod in resource %s"%container.rid)

        if verbose:
            self.log.info('logs from %s child service:'%container.name)
            print(out)
            if len(err) > 0:
                print(err)
        if ret != 0:
            raise ex.excError("error from encap service command '%s': %d\n%s\n%s"%(' '.join(cmd), ret, out, err))
        return out, err, ret

    def encap_json_status(self, container, refresh=False):
        if not refresh and hasattr(self, 'encap_json_status_cache') and container.rid in self.encap_json_status_cache:
            return self.encap_json_status_cache[container.rid]
        if container.guestos == 'windows':
            raise ex.excNotAvailable
        if container.status() == rcStatus.DOWN:
            """
               passive node for the vservice => forge encap resource status
                 - encap sync are n/a
                 - other encap res are down
            """
            gs = {
              'avail': 'down',
              'overall': 'down',
              'resources': {},
            }
            groups = set(["container", "ip", "disk", "fs", "share", "hb"])
            for g in groups:
                gs[g] = 'down'
            for rs in self.get_res_sets(self.status_types, strict=True):
                g = rs.type.split('.')[0]
                if g not in groups:
                    continue
                for r in rs.resources:
                    if not self.encap and 'encap' in r.tags:
                        gs['resources'][r.rid] = {'status': 'down'}

            groups = set(["app", "sync"])
            for g in groups:
                gs[g] = 'n/a'
            for rs in self.get_res_sets(groups):
                g = rs.type.split('.')[0]
                if g not in groups:
                    continue
                for r in rs.resources:
                    if not self.encap and 'encap' in r.tags:
                        gs['resources'][r.rid] = {'status': 'n/a'}

            return gs

        gs = {
          'avail': 'n/a',
          'overall': 'n/a',
          'resources': {},
        }
        groups = set(["container", "ip", "disk", "fs", "share", "hb", "app", "sync"])
        for g in groups:
            gs[g] = 'n/a'

        cmd = ['json', 'status']
        try:
            out, err, ret = self._encap_cmd(cmd, container)
        except ex.excError as e:
            return gs
        except Exception as e:
            print(e)
            return gs

        import json
        try:
            gs = json.loads(out)
        except:
            pass

        # feed cache
        if not hasattr(self, 'encap_json_status_cache'):
            self.encap_json_status_cache = {}
        self.encap_json_status_cache[container.rid] = gs

        return gs

    def group_status(self,
                     groups=set(["container", "ip", "disk", "fs", "share", "sync", "app", "hb"]),
                     excluded_groups=set([])):
        from copy import copy
        """print() each resource status for a service
        """
        status = {}
        moregroups = groups | set(["overall", "avail"])
        groups = groups.copy() - excluded_groups
        rset_status = self.get_rset_status(groups)

        # initialise status of each group
        for group in moregroups:
            status[group] = rcStatus.Status(rcStatus.NA)

        for t in [_t for _t in self.status_types if not _t.startswith('sync') and not _t.startswith('hb')]:
            group = t.split('.')[0]
            if group not in groups:
                continue
            for r in self.get_res_sets(t, strict=True):
                s = rcStatus.Status(rset_status[r.type])
                status[group] += s
                status["avail"] += s

        if status["avail"].status == rcStatus.STDBY_UP_WITH_UP:
            status["avail"].status = rcStatus.UP
            # now that we now the avail status we can promote
            # stdbyup to up
            for g in status:
                if status[g] == rcStatus.STDBY_UP:
                    status[g] = rcStatus.UP
        elif status["avail"].status == rcStatus.STDBY_UP_WITH_DOWN:
            status["avail"].status = rcStatus.STDBY_UP

        # overall status is avail + sync status
        # seed overall with avail
        status["overall"] = copy(status["avail"])

        for t in [_t for _t in self.status_types if _t.startswith('hb')]:
            if 'hb' not in groups:
                continue
            for r in self.get_res_sets(t, strict=True):
                s = rset_status[r.type]
                status['hb'] += s
                status["overall"] += s

        for t in [_t for _t in self.status_types if _t.startswith('sync')]:
            if 'sync' not in groups:
                continue
            for r in self.get_res_sets(t, strict=True):
                """ sync are expected to be up
                """
                s = rset_status[r.type]
                status['sync'] += s
                if s == rcStatus.UP:
                    status["overall"] += rcStatus.UNDEF
                elif s in [rcStatus.NA, rcStatus.UNDEF, rcStatus.TODO]:
                    status["overall"] += s
                else:
                    status["overall"] += rcStatus.WARN

        self.group_status_cache = status
        return status

    def print_disklist(self):
        print('\n'.join(self.disklist()))

    def print_devlist(self):
        print('\n'.join(self.devlist()))

    def json_disklist(self):
        import json
        print(json.dumps(list(self.disklist())))

    def json_devlist(self):
        import json
        print(json.dumps(list(self.devlist())))

    def disklist(self):
        if len(self.disks) == 0:
            self.disks = self._disklist()
        return self.disks

    def _disklist(self):
        """List all disks held by all resources of this service
        """
        disks = set()
        for r in self.get_resources():
            disks |= r.disklist()
        self.log.debug("found disks %s held by service" % disks)
        return disks

    def devlist(self):
        if len(self.devs) == 0:
            self.devs = self._devlist()
        return self.devs

    def _devlist(self):
        """List all devs held by all resources of this service
        """
        devs = set()
        for r in self.get_resources():
            devs |= r.devlist()
        self.log.debug("found devs %s held by service" % devs)
        return devs

    def get_non_affine_svc(self):
        if not hasattr(self, "anti_affinity"):
            return []
        self.node.build_services(svcnames=self.anti_affinity)
        running_af_svc = []
        for svc in self.node.svcs:
            if svc.svcname == self.svcname:
                continue
            avail = svc.group_status()['avail']
            if str(avail) != "down":
                running_af_svc.append(svc.svcname)
        return running_af_svc

    def print_env_mtime(self):
        mtime = os.stat(self.pathenv).st_mtime
        print(mtime)

    def need_start_encap(self, container):
        self.load_config()
        defaults = self.config.defaults()
        if defaults.get('autostart_node@'+container.name) in (container.name, 'encapnodes'):
            return False
        elif defaults.get('autostart_node@encapnodes') in (container.name, 'encapnodes'):
            return False
        elif defaults.get('autostart_node') in (container.name, 'encapnodes'):
            return False
        return True

    def boot(self):
        if rcEnv.nodename not in self.autostart_node:
            self.startstandby()
            return

        l = self.get_resources('hb')
        if len(l) > 0:
            self.log.warning("cluster nodes should not be in autostart_nodes for HA configuration")
            self.startstandby()
            return

        try:
            self.start()
        except ex.excError as e:
            self.log.error(str(e))
            self.log.info("start failed. try to start standby")
            self.startstandby()

    def shutdown(self):
        self.force = True
        self.master_shutdownhb()
        self.slave_shutdown()
        try:
            self.master_shutdownapp()
        except ex.excError:
            pass
        self.shutdowncontainer()
        self.master_shutdownfs()
        self.master_shutdownip()

    def _slave_action(fn):
        def _fn(self):
            if self.encap or not self.has_encap_resources:
                return
            if self.running_action not in ('migrate', 'boot', 'shutdown', 'prstart', 'prstop', 'restart', 'start', 'stop', 'startstandby', 'stopstandby') and \
               (not self.options.master and not self.options.slaves and self.options.slave is None):
                raise ex.excError("specify either --master, --slave(s) or both (%s)"%fn.__name__)
            if self.options.slaves or \
               self.options.slave is not None or \
               (not self.options.master and not self.options.slaves and self.options.slave is None):
                try:
                    fn(self)
                except Exception as e:
                    raise ex.excError(str(e))
        return _fn

    def _master_action(fn):
        def _fn(self):
            if not self.encap and \
               self.running_action not in ('migrate', 'boot', 'shutdown', 'restart', 'start', 'stop', 'startstandby', 'stopstandby') and \
               self.has_encap_resources and \
               (not self.options.master and not self.options.slaves and self.options.slave is None):
                raise ex.excError("specify either --master, --slave(s) or both (%s)"%fn.__name__)
            if self.options.master or \
               (not self.options.master and not self.options.slaves and self.options.slave is None):
                fn(self)
        return _fn

    def start(self):
        self.master_starthb()
        self.abort_start()
        af_svc = self.get_non_affine_svc()
        if len(af_svc) != 0:
            if self.options.ignore_affinity:
                self.log.error("force start of %s on the same node as %s despite anti-affinity settings"%(self.svcname, ', '.join(af_svc)))
            else:
                self.log.error("refuse to start %s on the same node as %s"%(self.svcname, ', '.join(af_svc)))
                return
        self.master_startip()
        self.master_startfs()
        self.master_startshare()
        self.startcontainer()
        self.master_startapp()
        self.slave_start()

    @_slave_action
    def slave_start(self):
        self.encap_cmd(['start'], verbose=True)

    def rollback(self):
        self.encap_cmd(['rollback'], verbose=True)
        try:
            self.rollbackapp()
        except ex.excError:
            pass
        self.rollbackcontainer()
        self.rollbackshare()
        self.rollbackfs()
        self.rollbackip()

    def stop(self):
        self.master_stophb()
        self.slave_stop()
        try:
            self.master_stopapp()
        except ex.excError:
            pass
        self.stopcontainer()
        self.master_stopshare()
        self.master_stopfs()
        self.master_stopip()

    @_slave_action
    def slave_shutdown(self):
        self.encap_cmd(['shutdown'], verbose=True, error="continue")

    @_slave_action
    def slave_stop(self):
        self.encap_cmd(['stop'], verbose=True, error="continue")

    def hb_handled_action(self, action):
        """
        Used to determine if we can allow this action on a frozen service.
        """
        for r in self.get_resources("hb"):
            if hasattr(r, action):
                return True
        return False

    def cluster_mode_safety_net(self, action):
        if not self.has_res_set(['hb.ovm', 'hb.openha', 'hb.linuxha', 'hb.sg', 'hb.rhcs', 'hb.vcs']):
            return
        n_hb = 0
        n_hb_enabled = 0
        for r in self.get_resources('hb', discard_disabled=False):
            n_hb += 1
            if not r.disabled:
                n_hb_enabled += 1
        if n_hb > 0 and n_hb_enabled == 0 and self.cluster:
            raise ex.excAbortAction("this service has heartbeat resources, but all disabled. this state is interpreted as a maintenance mode. actions submitted with --cluster are not allowed to inhibit actions triggered by the heartbeat daemon.")
        if n_hb_enabled == 0:
            return
        if not self.cluster:
            for r in self.get_resources("hb"):
                if hasattr(r, action):
                    getattr(r, action)()
            raise ex.excError("this service is managed by a clusterware, thus direct service manipulation is disabled. the --cluster option circumvent this safety net.")

    def starthb(self):
        self.master_starthb()
        self.slave_starthb()

    @_slave_action
    def slave_starthb(self):
        self.encap_cmd(['starthb'], verbose=True)

    @_master_action
    def master_starthb(self):
        self.master_hb('start')

    @_master_action
    def master_startstandbyhb(self):
        self.master_hb('startstandby')

    @_master_action
    def master_shutdownhb(self):
        self.master_hb('shutdown')

    @_master_action
    def master_stophb(self):
        self.master_hb('stop')

    def master_hb(self, action):
        self.sub_set_action("hb", action)

    def stophb(self):
        self.slave_stophb()
        self.master_stophb()

    @_slave_action
    def slave_stophb(self):
        self.encap_cmd(['stophb'], verbose=True)

    def startdrbd(self):
        self.master_startdrbd()
        self.slave_startdrbd()

    @_slave_action
    def slave_startdrbd(self):
        self.encap_cmd(['startdrbd'], verbose=True)

    @_master_action
    def master_startdrbd(self):
        self.sub_set_action("disk.drbd", "start")

    def stopdrbd(self):
        self.slave_stopdrbd()
        self.master_stopdrbd()

    @_slave_action
    def slave_stopdrbd(self):
        self.encap_cmd(['stopdrbd'], verbose=True)

    @_master_action
    def master_stopdrbd(self):
        self.sub_set_action("disk.drbd", "stop")

    def startloop(self):
        self.master_startloop()
        self.slave_startloop()

    @_slave_action
    def slave_startloop(self):
        self.encap_cmd(['startloop'], verbose=True)

    @_master_action
    def master_startloop(self):
        self.sub_set_action("disk.loop", "start")

    def stoploop(self):
        self.slave_stoploop()
        self.master_stoploop()

    @_slave_action
    def slave_stoploop(self):
        self.encap_cmd(['stoploop'], verbose=True)

    @_master_action
    def master_stoploop(self):
        self.sub_set_action("disk.loop", "stop")

    def stopvg(self):
        self.slave_stopvg()
        self.master_stopvg()

    @_slave_action
    def slave_stopvg(self):
        self.encap_cmd(['stopvg'], verbose=True)

    @_master_action
    def master_stopvg(self):
        self.sub_set_action("disk.vg", "stop")
        self.sub_set_action("disk.lock", "stop")
        self.sub_set_action("disk.scsireserv", "stop", xtags=set(['zone']))

    def startvg(self):
        self.master_startvg()
        self.slave_startvg()

    @_slave_action
    def slave_startvg(self):
        self.encap_cmd(['startvg'], verbose=True)

    @_master_action
    def master_startvg(self):
        self.sub_set_action("disk.scsireserv", "start", xtags=set(['zone']))
        self.sub_set_action("disk.lock", "start")
        self.sub_set_action("disk.vg", "start")

    def startpool(self):
        self.master_startpool()
        self.slave_startpool()

    @_slave_action
    def slave_startpool(self):
        self.encap_cmd(['startpool'], verbose=True)

    @_master_action
    def master_startpool(self):
        self.sub_set_action("disk.scsireserv", "start", xtags=set(['zone']))
        self.sub_set_action("disk.zpool", "start", xtags=set(['zone']))

    def stoppool(self):
        self.slave_stoppool()
        self.master_stoppool()

    @_slave_action
    def slave_stoppool(self):
        self.encap_cmd(['stoppool'], verbose=True)

    @_master_action
    def master_stoppool(self):
        self.sub_set_action("disk.zpool", "stop", xtags=set(['zone']))
        self.sub_set_action("disk.scsireserv", "stop", xtags=set(['zone']))

    def startdisk(self):
        self.master_startdisk()
        self.slave_startdisk()

    @_slave_action
    def slave_startdisk(self):
        self.encap_cmd(['startdisk'], verbose=True)

    @_master_action
    def master_startstandbydisk(self):
        self.sub_set_action("sync.netapp", "startstandby")
        self.sub_set_action("sync.dcsckpt", "startstandby")
        self.sub_set_action("sync.nexenta", "startstandby")
        self.sub_set_action("sync.symclone", "startstandby")
        self.sub_set_action("sync.ibmdssnap", "startstandby")
        self.sub_set_action("disk.loop", "startstandby")
        self.sub_set_action("disk.gandi", "startstandby")
        self.sub_set_action("disk.scsireserv", "startstandby", xtags=set(['zone']))
        self.sub_set_action("disk.drbd", "startstandby", tags=set(['prevg']))
        self.sub_set_action("disk.zpool", "startstandby", xtags=set(['zone']))
        self.sub_set_action("disk.lock", "startstandby")
        self.sub_set_action("disk.vg", "startstandby")
        self.sub_set_action("disk.drbd", "startstandby", tags=set(['postvg']))

    @_master_action
    def master_startdisk(self):
        self.sub_set_action("sync.netapp", "start")
        self.sub_set_action("sync.dcsckpt", "start")
        self.sub_set_action("sync.nexenta", "start")
        self.sub_set_action("sync.symclone", "start")
        self.sub_set_action("sync.symsrdfs", "start")
        self.sub_set_action("sync.hp3par", "start")
        self.sub_set_action("sync.ibmdssnap", "start")
        self.sub_set_action("disk.loop", "start")
        self.sub_set_action("disk.gandi", "start")
        self.sub_set_action("disk.scsireserv", "start", xtags=set(['zone']))
        self.sub_set_action("disk.drbd", "start", tags=set(['prevg']))
        self.sub_set_action("disk.zpool", "start", xtags=set(['zone']))
        self.sub_set_action("disk.lock", "start")
        self.sub_set_action("disk.vg", "start")
        self.sub_set_action("disk.drbd", "start", tags=set(['postvg']))

    def stopdisk(self):
        self.slave_stopdisk()
        self.master_stopdisk()

    @_slave_action
    def slave_stopdisk(self):
        self.encap_cmd(['stopdisk'], verbose=True)

    @_master_action
    def master_stopdisk(self):
        self.sub_set_action("disk.drbd", "stop", tags=set(['postvg']))
        self.sub_set_action("disk.vg", "stop")
        self.sub_set_action("disk.lock", "stop")
        self.sub_set_action("disk.zpool", "stop", xtags=set(['zone']))
        self.sub_set_action("disk.drbd", "stop", tags=set(['prevg']))
        self.sub_set_action("disk.scsireserv", "stop", xtags=set(['zone']))
        self.sub_set_action("disk.loop", "stop")
        self.sub_set_action("disk.gandi", "stop")

    @_master_action
    def master_shutdowndisk(self):
        self.sub_set_action("disk.drbd", "shutdown", tags=set(['postvg']))
        self.sub_set_action("disk.vg", "shutdown")
        self.sub_set_action("disk.lock", "shutdown")
        self.sub_set_action("disk.zpool", "shutdown", xtags=set(['zone']))
        self.sub_set_action("disk.drbd", "shutdown", tags=set(['prevg']))
        self.sub_set_action("disk.scsireserv", "shutdown", xtags=set(['zone']))
        self.sub_set_action("disk.loop", "shutdown")
        self.sub_set_action("disk.gandi", "shutdown")

    def rollbackdisk(self):
        self.sub_set_action("disk.drbd", "rollback", tags=set(['postvg']))
        self.sub_set_action("disk.vg", "rollback")
        self.sub_set_action("disk.lock", "rollback")
        self.sub_set_action("disk.zpool", "rollback", xtags=set(['zone']))
        self.sub_set_action("disk.drbd", "rollback", tags=set(['prevg']))
        self.sub_set_action("disk.scsireserv", "rollback", xtags=set(['zone']))
        self.sub_set_action("disk.loop", "rollback")
        self.sub_set_action("disk.gandi", "rollback")

    def abort_start(self):
        for r in self.get_resources():
            if r.skip or r.disabled:
                continue
            if hasattr(r, 'abort_start') and r.abort_start():
                raise ex.excError("start aborted due to resource %s conflict"%r.rid)

    def startip(self):
        self.master_startip()
        self.slave_startip()

    @_slave_action
    def slave_startip(self):
        self.encap_cmd(['startip'], verbose=True)

    @_master_action
    def master_startstandbyip(self):
        self.sub_set_action("ip", "startstandby", xtags=set(['zone']))

    @_master_action
    def master_startip(self):
        self.sub_set_action("ip", "start", xtags=set(['zone']))

    def stopip(self):
        self.slave_stopip()
        self.master_stopip()

    @_slave_action
    def slave_stopip(self):
        self.encap_cmd(['stopip'], verbose=True)

    @_master_action
    def master_stopip(self):
        self.sub_set_action("ip", "stop", xtags=set(['zone']))

    @_master_action
    def master_shutdownip(self):
        self.sub_set_action("ip", "shutdown", xtags=set(['zone']))

    def rollbackip(self):
        self.sub_set_action("ip", "rollback", xtags=set(['zone']))

    def startshare(self):
        self.master_startshare()
        self.slave_startshare()

    @_master_action
    def master_startshare(self):
        self.sub_set_action("share.nfs", "start")

    @_master_action
    def master_startstandbyshare(self):
        self.sub_set_action("share", "startstandby")

    @_slave_action
    def slave_startshare(self):
        self.encap_cmd(['startshare'], verbose=True)

    def stopshare(self):
        self.slave_stopshare()
        self.master_stopshare()

    @_master_action
    def master_stopshare(self):
        self.sub_set_action("share", "stop")

    @_master_action
    def master_shutdownshare(self):
        self.sub_set_action("share", "shutdown")

    @_slave_action
    def slave_stopshare(self):
        self.encap_cmd(['stopshare'], verbose=True)

    def rollbackshare(self):
        self.sub_set_action("share", "rollback")

    def startfs(self):
        self.master_startfs()
        self.slave_startfs()

    @_master_action
    def master_startfs(self):
        self.master_startdisk()
        self.sub_set_action("fs", "start", xtags=set(['zone']))

    @_master_action
    def master_startstandbyfs(self):
        self.master_startstandbydisk()
        self.sub_set_action("fs", "startstandby", xtags=set(['zone']))

    @_slave_action
    def slave_startfs(self):
        self.encap_cmd(['startfs'], verbose=True)

    def stopfs(self):
        self.slave_stopfs()
        self.master_stopfs()

    @_master_action
    def master_stopfs(self):
        self.sub_set_action("fs", "stop", xtags=set(['zone']))
        self.master_stopdisk()

    @_master_action
    def master_shutdownfs(self):
        self.sub_set_action("fs", "shutdown", xtags=set(['zone']))
        self.master_shutdowndisk()

    @_slave_action
    def slave_stopfs(self):
        self.encap_cmd(['stopfs'], verbose=True)

    def rollbackfs(self):
        self.sub_set_action("fs", "rollback", xtags=set(['zone']))
        self.rollbackdisk()

    @_master_action
    def startstandbycontainer(self):
        self.sub_set_action("container", "startstandby")
        self.refresh_ip_status()

    @_master_action
    def startcontainer(self):
        self.sub_set_action("container", "start")
        self.refresh_ip_status()

    def refresh_ip_status(self):
        """ Used after start/stop container because the ip resource
            status change after its own start/stop
        """
        for r in self.get_resources("ip"):
            r.status(refresh=True, restart=False)

    @_master_action
    def shutdowncontainer(self):
        self.sub_set_action("container.vbox", "shutdown")
        self.sub_set_action("container.ldom", "shutdown")
        self.sub_set_action("container.hpvm", "shutdown")
        self.sub_set_action("container.xen", "shutdown")
        self.sub_set_action("container.esx", "shutdown")
        self.sub_set_action("container.ovm", "shutdown")
        self.sub_set_action("container.kvm", "shutdown")
        self.sub_set_action("container.amazon", "shutdown")
        self.sub_set_action("container.openstack", "shutdown")
        self.sub_set_action("container.vcloud", "shutdown")
        self.sub_set_action("container.jail", "shutdown")
        self.sub_set_action("container.lxc", "shutdown")
        self.sub_set_action("container.docker", "shutdown")
        self.sub_set_action("container.vz", "shutdown")
        self.sub_set_action("container.srp", "shutdown")
        self.refresh_ip_status()

    @_master_action
    def stopcontainer(self):
        self.sub_set_action("container", "stop")
        self.refresh_ip_status()

    def rollbackcontainer(self):
        self.sub_set_action("container", "rollback")
        self.refresh_ip_status()

    def provision(self):
        self.sub_set_action("disk", "provision", xtags=set(['zone']))
        self.sub_set_action("fs", "provision", xtags=set(['zone']))
        self.sub_set_action("container", "provision")
        self.sub_set_action("ip", "provision", xtags=set(['zone']))
        self.push()

    def startapp(self):
        self.master_startapp()
        self.slave_startapp()

    @_slave_action
    def slave_startapp(self):
        self.encap_cmd(['startapp'], verbose=True)

    @_master_action
    def master_startstandbyapp(self):
        self.sub_set_action("app", "startstandby")

    @_master_action
    def master_startapp(self):
        self.sub_set_action("app", "start")

    def stopapp(self):
        self.slave_stopapp()
        self.master_stopapp()

    @_slave_action
    def slave_stopapp(self):
        self.encap_cmd(['stopapp'], verbose=True)

    @_master_action
    def master_stopapp(self):
        self.sub_set_action("app", "stop")

    @_master_action
    def master_shutdownapp(self):
        self.sub_set_action("app", "shutdown")

    def rollbackapp(self):
        self.sub_set_action("app", "rollback")

    def prstop(self):
        self.slave_prstop()
        self.master_prstop()

    @_slave_action
    def slave_prstop(self):
        self.encap_cmd(['prstop'], verbose=True)

    @_master_action
    def master_prstop(self):
        self.sub_set_action("disk.scsireserv", "scsirelease")

    def prstart(self):
        self.master_prstart()
        self.slave_prstart()

    @_slave_action
    def slave_prstart(self):
        self.encap_cmd(['prstart'], verbose=True)

    @_master_action
    def master_prstart(self):
        self.sub_set_action("disk.scsireserv", "scsireserv")

    def prstatus(self):
        self.sub_set_action("disk.scsireserv", "scsicheckreserv")

    def startstandby(self):
        self.master_startstandby()
        self.slave_startstandby()

    @_master_action
    def master_startstandby(self):
        self.master_startstandbyip()
        self.master_startstandbyfs()
        self.master_startstandbyshare()
        self.startstandbycontainer()
        self.master_startstandbyapp()

    @_slave_action
    def slave_startstandby(self):
        cmd = ['startstandby']
        for container in self.get_resources('container'):
            if not container.is_up() and not rcEnv.nodename in container.always_on:
                # no need to try to startstandby the encap service on a container we not activated
                continue
            try:
                out, err, ret = self._encap_cmd(cmd, container, verbose=True)
            except ex.excError:
                self.log.error("container %s is not joinable to execute action '%s'"%(container.name, ' '.join(cmd)))
                raise

    def postsync(self):
        """ action triggered by a remote master node after
            syncnodes and syncdrp. Typically make use of files
            received in var/
        """
        self.all_set_action("postsync")

    def remote_postsync(self):
        """ run the remote exec of postsync async because the
            waitlock timeout is long, and we ourselves still
            hold the service lock we want to release early.
        """
        """ action triggered by a remote master node after
            syncnodes and syncdrp. Typically make use of files
            received in var/.
            use a long waitlock timeout to give a chance to
            remote syncs to finish
        """
        for n in self.need_postsync:
            self.remote_action(n, 'postsync', waitlock=3600)

        self.need_postsync = set([])

    def remote_action(self, node, action, waitlock=60, sync=False):
        rcmd = [os.path.join(rcEnv.pathetc, self.svcname)]
	if self.log.isEnabledFor(logging.DEBUG):
	    rcmd += ['--debug']
        if self.cluster:
            rcmd += ['--cluster']
        if self.cron:
            rcmd += ['--cron']
        rcmd += ['--waitlock', str(waitlock)] + action.split()
        cmd = rcEnv.rsh.split() + [node] + rcmd
        self.log.info("exec '%s' on node %s"%(' '.join(rcmd), node))
        if sync:
            out, err, ret = justcall(cmd)
            return out, err, ret
        else:
            self.node.cmdworker.enqueue(cmd)

    def presync(self):
        """ prepare files to send to slave nodes in var/.
            Each resource can prepare its own set of files.
        """
        self.need_postsync = set([])
        if self.presync_done:
            return
        self.all_set_action("presync")
        self.presync_done = True

    def syncnodes(self):
        self.presync()
        self.sub_set_action("sync.rsync", "syncnodes")
        self.sub_set_action("sync.zfs", "syncnodes")
        self.sub_set_action("sync.btrfs", "syncnodes")
        self.sub_set_action("sync.docker", "syncnodes")
        self.sub_set_action("sync.dds", "syncnodes")
        self.sub_set_action("sync.symsrdfs", "syncnodes")
        self.remote_postsync()

    def syncdrp(self):
        self.presync()
        self.sub_set_action("sync.rsync", "syncdrp")
        self.sub_set_action("sync.zfs", "syncdrp")
        self.sub_set_action("sync.btrfs", "syncdrp")
        self.sub_set_action("sync.docker", "syncdrp")
        self.sub_set_action("sync.dds", "syncdrp")
        self.sub_set_action("sync.symsrdfs", "syncdrp")
        self.remote_postsync()

    def syncswap(self):
        self.sub_set_action("sync.netapp", "syncswap")
        self.sub_set_action("sync.symsrdfs", "syncswap")
        self.sub_set_action("sync.hp3par", "syncswap")
        self.sub_set_action("sync.nexenta", "syncswap")

    def syncrevert(self):
        self.sub_set_action("sync.hp3par", "syncrevert")

    def syncresume(self):
        self.sub_set_action("sync.netapp", "syncresume")
        self.sub_set_action("sync.symsrdfs", "syncresume")
        self.sub_set_action("sync.hp3par", "syncresume")
        self.sub_set_action("sync.dcsckpt", "syncresume")
        self.sub_set_action("sync.nexenta", "syncresume")

    def syncquiesce(self):
        self.sub_set_action("sync.netapp", "syncquiesce")
        self.sub_set_action("sync.nexenta", "syncquiesce")

    def resync(self):
        self.stop()
        self.syncresync()
        self.start()

    def syncresync(self):
        self.sub_set_action("sync.netapp", "syncresync")
        self.sub_set_action("sync.nexenta", "syncresync")
        self.sub_set_action("sync.symclone", "syncresync")
        self.sub_set_action("sync.rados", "syncresync")
        self.sub_set_action("sync.ibmdssnap", "syncresync")
        self.sub_set_action("sync.evasnap", "syncresync")
        self.sub_set_action("sync.necismsnap", "syncresync")
        self.sub_set_action("sync.dcssnap", "syncresync")
        self.sub_set_action("sync.dds", "syncresync")

    def syncbreak(self):
        self.sub_set_action("sync.netapp", "syncbreak")
        self.sub_set_action("sync.nexenta", "syncbreak")
        self.sub_set_action("sync.symclone", "syncbreak")
        self.sub_set_action("sync.hp3par", "syncbreak")
        self.sub_set_action("sync.ibmdssnap", "syncbreak")
        self.sub_set_action("sync.dcsckpt", "syncbreak")

    def syncupdate(self):
        self.sub_set_action("sync.netapp", "syncupdate")
        self.sub_set_action("sync.hp3par", "syncupdate")
        self.sub_set_action("sync.nexenta", "syncupdate")
        self.sub_set_action("sync.dcsckpt", "syncupdate")
        self.sub_set_action("sync.dds", "syncupdate")
        self.sub_set_action("sync.zfs", "syncnodes")

    def syncfullsync(self):
        self.sub_set_action("sync.dds", "syncfullsync")
        self.sub_set_action("sync.zfs", "syncnodes")
        self.sub_set_action("sync.btrfs", "syncfullsync")

    def syncsplit(self):
        self.sub_set_action("sync.symsrdfs", "syncsplit")

    def syncestablish(self):
        self.sub_set_action("sync.symsrdfs", "syncestablish")

    def syncverify(self):
        self.sub_set_action("sync.dds", "syncverify")

    def print_config(self):
        try:
            with open(self.pathenv, 'r') as f:
                print(f.read())
        except Exception as e:
            print(s, file=sys.stderr)

    def edit_config(self):
        if "EDITOR" in os.environ:
            editor = os.environ["EDITOR"]
        elif os.name == "nt":
            editor = "notepad"
        else:
            editor = "vi"
        from rcUtilities import which
        if not which(editor):
            print("%s not found" % editor, file=sys.stderr)
            return 1
        return os.system(' '.join((editor, self.pathenv)))

    def can_sync(self, target=None):
        ret = False
        rtypes = ["sync.netapp", "sync.nexenta", "sync.dds", "sync.zfs",
                  "sync.rsync", "sync.docker", "sync.btrfs", "sync.hp3par"]
        for rt in rtypes:
            for r in self.get_resources(rt):
                try:
                    ret |= r.can_sync(target)
                except ex.excError:
                    return False
                if ret: return True
        return False

    def syncall(self):
        self.presync()
        self.sub_set_action("sync.rsync", "syncnodes")
        self.sub_set_action("sync.zfs", "syncnodes")
        self.sub_set_action("sync.btrfs", "syncnodes")
        self.sub_set_action("sync.docker", "syncnodes")
        self.sub_set_action("sync.dds", "syncnodes")
        self.sub_set_action("sync.symsrdfs", "syncnodes")
        self.sub_set_action("sync.rsync", "syncdrp")
        self.sub_set_action("sync.zfs", "syncdrp")
        self.sub_set_action("sync.btrfs", "syncdrp")
        self.sub_set_action("sync.docker", "syncdrp")
        self.sub_set_action("sync.dds", "syncdrp")
        self.syncupdate()
        self.remote_postsync()

    def push_appinfo(self):
        self.node.collector.call('push_appinfo', [self])

    def push(self):
        if self.encap:
            return
        self.push_encap_env()
        self.node.collector.call('push_all', [self])
        print("send %s to collector ... OK"%self.pathenv)
        try:
            import time
            with open(self.push_flag, 'w') as f:
                f.write(str(time.time()))
            ret = "OK"
        except:
            ret = "ERR"
        print("update %s timestamp"%self.push_flag, "...", ret)

    def push_encap_env(self):
        if self.encap or not self.has_encap_resources:
            return

        for r in self.get_resources('container'):
            if r.status() != rcStatus.UP:
                continue
            self._push_encap_env(r)

    def _push_encap_env(self, r):
        cmd = ['print', 'env', 'mtime']
        try:
            out, err, ret = self._encap_cmd(cmd, r)
        except ex.excError:
            ret = 1
        if ret == 0:
            encap_mtime = int(float(out.strip()))
            local_mtime = int(os.stat(self.pathenv).st_mtime)
            if encap_mtime > local_mtime:
                if hasattr(r, 'rcp_from'):
                    out, err, ret = r.rcp_from(self.pathenv, rcEnv.pathetc+'/')
                else:
                    cmd = rcEnv.rcp.split() + [r.name+':'+self.pathenv, rcEnv.pathetc+'/']
                    out, err, ret = justcall(cmd)
                os.utime(self.pathenv, (encap_mtime, encap_mtime))
                print("fetch %s from %s ..."%(self.pathenv, r.name), "OK" if ret == 0 else "ERR\n%s"%err)
                if ret != 0:
                    raise ex.excError()
                return
            elif encap_mtime == local_mtime:
                return

        if hasattr(r, 'rcp'):
            out, err, ret = r.rcp(self.pathenv, rcEnv.pathetc+'/')
        else:
            cmd = rcEnv.rcp.split() + [self.pathenv, r.name+':'+rcEnv.pathetc+'/']
            out, err, ret = justcall(cmd)
        print("send %s to %s ..."%(self.pathenv, r.name), "OK" if ret == 0 else "ERR\n%s"%err)
        if ret != 0:
            raise ex.excError()

        cmd = ['install', '--envfile', self.pathenv]
        out, err, ret = self._encap_cmd(cmd, container=r)
        print("install %s slave service ..."%r.name, "OK" if ret == 0 else "ERR\n%s"%err)
        if ret != 0:
            raise ex.excError()

    def tag_match(self, rtags, keeptags):
        for tag in rtags:
            if tag in keeptags:
                return True
        return False

    def set_skip_resources(self, keeprid=[], keeptags=set([]), xtags=set([])):
        if len(keeprid) > 0:
            ridfilter = True
        else:
            ridfilter = False

        if len(keeptags) > 0 or len(xtags) > 0:
            tagsfilter = True
        else:
            tagsfilter = False

        if not tagsfilter and not ridfilter:
            return

        for r in self.get_resources():
            if self.tag_match(r.tags, xtags):
                r.skip = True
            if ridfilter and r.rid in keeprid:
                continue
            if tagsfilter and self.tag_match(r.tags, keeptags):
                continue
            r.skip = True

    def setup_environ(self):
        """ Those are available to startup scripts and triggers
        """
        os.environ['OPENSVC_SVCNAME'] = self.svcname
        for r in self.get_resources():
            r.setup_environ()

    def expand_rid(self, rid):
        l = []
        for e in self.resources_by_id.keys():
            if e is None:
                continue
            if '#' not in e:
                if e == rid:
                    l.append(e)
                else:
                    continue
            elif e[:e.index('#')] == rid:
                l.append(e)
        return l

    def expand_rids(self, rid):
        l = []
        for e in set(rid):
            if '#' in e:
                l.append(e)
                continue
            l += self.expand_rid(e)
        return l

    def action(self, action, rid=[], tags=set([]), xtags=set([]), waitlock=60):
        rid = self.expand_rids(rid)
        self.action_rid = rid
        if self.node is None:
            self.node = node.Node()
        self.action_start_date = datetime.datetime.now()
        if self.svctype != 'PRD' and rcEnv.host_mode == 'PRD':
            self.log.error("Abort action for non PRD service on PRD node")
            return 1

        actions_list_allow_on_frozen = [
          'get',
          'set',
          'unset',
          'update',
          'enable',
          'disable',
          'delete',
          'freeze',
          'thaw',
          'status',
          'frozen',
          'push',
          'push_appinfo',
          'edit_config',
          'print_config',
          'print_env_mtime',
          'print_status',
          'print_resource_status',
          'print_disklist',
          'print_devlist',
          'json_env',
          'json_status',
          'json_disklist',
          'json_devlist'
        ]
        actions_list_allow_on_cluster = actions_list_allow_on_frozen + [
          'docker',
          'boot',
          'toc',
          'startstandby',
          'resource_monitor',
          'presync',
          'postsync',
          'syncdrp',
          'syncnodes',
          'syncall'
        ]
        if action not in actions_list_allow_on_frozen and \
           not self.hb_handled_action(action) and \
           'compliance' not in action and \
           'collector' not in action:
            if self.frozen():
                self.log.info("Abort action for frozen service")
                return 1
            try:
                if action not in actions_list_allow_on_cluster:
                    self.cluster_mode_safety_net(action)
            except ex.excAbortAction as e:
                self.log.info(str(e))
                return 0
            except ex.excError as e:
                self.log.error(str(e))
                return 1
            #
            # here we know we will run a resource state-changing action
            # purge the resource status file cache, so that we don't take
            # decision on outdated information
            #
            if not self.options.dry_run and action != "resource_monitor":
                self.log.debug("purge all resource status file caches")
                self.purge_status_last()

        self.setup_environ()
        self.setup_signal_handlers()
        self.set_skip_resources(keeprid=rid, keeptags=tags, xtags=xtags)
        actions_list_no_log = [
          'get',
          'set',
          'push',
          'push_appinfo',
          'print_env_mtime',
          'print_status',
          'print_resource_status',
          'print_disklist',
          'print_devlist',
          'print_config',
          'edit_config',
          'json_status',
          'json_disklist',
          'json_devlist',
          'json_env',
          'status',
          'group_status',
          'resource_monitor'
        ]
        if action in actions_list_no_log or \
           action.startswith("compliance") or \
           action.startswith("collector") or \
           action.startswith("docker") or \
           self.options.dry_run:
            err = self.do_action(action, waitlock=waitlock)
        elif action in ["syncall", "syncdrp", "syncnodes", "syncupdate"]:
            if action == "syncall" or "syncupdate": kwargs = {}
            elif action == "syncnodes": kwargs = {'target': 'nodes'}
            elif action == "syncdrp": kwargs = {'target': 'drpnodes'}
            if not self.can_sync(**kwargs):
                self.log.debug("nothing to sync for the service for now")
                return 0
            try:
                # timeout=1, delay=1 => immediate response
                self.svclock(action, timeout=1, delay=1)
            except:
                if not self.cron:
                    self.log.info("%s action is already running"%action)
                return 0
            err = self.do_logged_action(action, waitlock=waitlock)
        else:
            err = self.do_logged_action(action, waitlock=waitlock)
        return err

    def do_action(self, action, waitlock=60):
        """Trigger action
        """
        err = 0
        try:
            self.svclock(action, timeout=waitlock)
        except:
            return 1

        try:
            if action.startswith("compliance_"):
                from compliance import Compliance
                o = Compliance(self.node.skip_action, self.options, self.node.collector, self.svcname)
                getattr(o, action)()
            elif action.startswith("collector_"):
                from collector import Collector
                o = Collector(self.options, self.node.collector, self.svcname)
                getattr(o, action)()
            elif hasattr(self, action):
                self.running_action = action
                err = getattr(self, action)()
                if err is None:
                    err = 0
                self.running_action = None
            else:
                self.log.error("unsupported action")
                err = 1
        except ex.excEndAction as e:
            s = "'%s' action ended by last resource"%action
            if len(str(e)) > 0:
                s += ": %s"%str(e)
            self.log.info(s)
            err = 0
        except ex.excAbortAction as e:
            s = "'%s' action aborted by last resource"%action
            if len(str(e)) > 0:
                s += ": %s"%str(e)
            self.log.info(s)
            err = 0
        except ex.excError as e:
            s = "'%s' action stopped on execution error"%action
            if len(str(e)) > 0:
                s += ": %s"%str(e)
            self.log.error(s)
            err = 1
            self.rollback_handler(action)
        except ex.excSignal:
            self.log.error("interrupted by signal")
            err = 1
        except self.exMonitorAction:
            self.svcunlock()
            raise
        except:
            err = 1
            self.save_exc()

        self.svcunlock()

        if action == "start" and self.cluster and self.ha:
            """ This situation is typical of a hb-initiated service start.
                While the hb starts the service, its resource status is warn from
                opensvc point of view. So after a successful startup, the hb res
                status would stay warn until the next svcmon.
                To avoid this drawback we can force from here the hb status.
            """
            if err == 0:
                for r in self.get_resources(['hb']):
                    if r.disabled:
                        continue
                    r.force_status(rcStatus.UP)

        return err

    def rollback_handler(self, action):
	if self.options.disable_rollback:
            self.log.info("skip rollback %s: as instructed by --disable-rollback"%action)
            return
        if 'start' not in action:
            return
        rids = [r.rid for r in self.get_resources() if r.can_rollback]
        if len(rids) == 0:
            self.log.info("skip rollback %s: no resource activated"%action)
            return
        self.log.info("trying to rollback %s on %s"%(action, ', '.join(rids)))
        try:
            self.rollback()
        except:
            self.log.error("rollback %s failed"%action)

    def do_logged_action(self, action, waitlock=60):
        from datetime import datetime
        import tempfile
        import logging
        begin = datetime.now()

        """Provision a database entry to store action log later
        """
        if action in ('postsync', 'shutdown'):
            # don't loose the action log on node shutdown
            # no background dblogger for remotely triggered postsync
            self.sync_dblogger = True
        self.node.collector.call('begin_action', self, action, begin, sync=self.sync_dblogger)

        """Per action logfile to push to database at the end of the action
        """
        f = tempfile.NamedTemporaryFile(delete=False, dir=rcEnv.pathtmp, prefix=self.svcname+'.'+action)
        actionlogfile = f.name
        f.close()
        log = logging.getLogger()
        actionlogformatter = logging.Formatter("%(asctime)s;;%(name)s;;%(levelname)s;;%(message)s;;%(process)d;;EOL")
        actionlogfilehandler = logging.FileHandler(actionlogfile)
        actionlogfilehandler.setFormatter(actionlogformatter)
        log.addHandler(actionlogfilehandler)

        err = self.do_action(action, waitlock=waitlock)

        """ Push result and logs to database
        """
        actionlogfilehandler.close()
        log.removeHandler(actionlogfilehandler)
        end = datetime.now()
        self.dblogger(action, begin, end, actionlogfile)
        return err

    def restart(self):
        self.stop()
        self.start()

    def _migrate(self):
        self.sub_set_action("container.ovm", "_migrate")
        self.sub_set_action("container.hpvm", "_migrate")
        self.sub_set_action("container.esx", "_migrate")

    @_master_action
    def migrate(self):
        if not hasattr(self, "destination_node"):
            self.log.error("a destination node must be provided for the switch action")
            raise ex.excError
        if self.destination_node not in self.nodes:
            self.log.error("destination node %s is not in service node list"%self.destination_node)
            raise ex.excError
        self.master_prstop()
        try:
            self.remote_action(node=self.destination_node, action='startfs --master')
            self._migrate()
        except:
            if self.has_res_set(['disk.scsireserv']):
                self.log.error("scsi reservations where dropped. you have to acquire them now using the 'prstart' action either on source node or destination node, depending on your problem analysis.")
            raise
        self.master_stopfs()
        self.remote_action(node=self.destination_node, action='prstart --master')

    def switch(self):
        self.sub_set_action("hb", "switch")
        if not hasattr(self, "destination_node"):
            self.log.error("a destination node must be provided for the switch action")
            raise ex.excError
        if self.destination_node not in self.nodes:
            self.log.error("destination node %s is not in service node list"%self.destination_node)
            raise ex.excError
        self.stop()
        self.remote_action(node=self.destination_node, action='start')

    def collector_outdated(self):
        """ return True if the env file has changed since last push
            else return False
        """
        import datetime
        if not os.path.exists(self.push_flag):
            self.log.debug("no last push timestamp found")
            return True
        try:
            mtime = os.stat(self.pathenv).st_mtime
            f = open(self.push_flag)
            last_push = float(f.read())
            f.close()
        except:
            self.log.error("can not read timestamp from %s or %s"%(self.pathenv, self.push_flag))
            return True
        if mtime > last_push:
            self.log.debug("env file changed since last push")
            return True
        return False

    def write_config(self):
        try:
            fp = open(self.pathenv, 'w')
            self.config.write(fp)
            fp.close()
        except:
            print("failed to write new %s"%self.nodeconf, file=sys.stderr)
            raise ex.excError()

    def load_config(self):
        import ConfigParser
        self.config = ConfigParser.RawConfigParser()
        self.config.read(self.pathenv)

    def unset(self):
        self.load_config()
        if self.options.param is None:
            print("no parameter. set --param", file=sys.stderr)
            return 1
        l = self.options.param.split('.')
        if len(l) != 2:
            print("malformed parameter. format as 'section.key'", file=sys.stderr)
            return 1
        section, option = l
        if section != 'DEFAULT' and not self.config.has_section(section):
            print("section '%s' not found"%section, file=sys.stderr)
            return 1
        if not self.config.has_option(section, option):
            print("option '%s' not found in section '%s'"%(option, section), file=sys.stderr)
            return 1
        try:
            self.config.remove_option(section, option)
            self.write_config()
        except:
            return 1
        return 0

    def get(self):
        self.load_config()
        if self.options.param is None:
            print("no parameter. set --param", file=sys.stderr)
            return 1
        l = self.options.param.split('.')
        if len(l) != 2:
            print("malformed parameter. format as 'section.key'", file=sys.stderr)
            return 1
        section, option = l
        if section != 'DEFAULT' and not self.config.has_section(section):
            print("section '%s' not found"%section, file=sys.stderr)
            return 1
        if not self.config.has_option(section, option):
            print("option '%s' not found in section '%s'"%(option, section), file=sys.stderr)
            return 1
        print(self.config.get(section, option))
        return 0

    def set(self):
        self.load_config()
        if self.options.param is None:
            print("no parameter. set --param", file=sys.stderr)
            return 1
        if self.options.value is None:
            print("no value. set --value", file=sys.stderr)
            return 1
        l = self.options.param.split('.')
        if len(l) != 2:
            print("malformed parameter. format as 'section.key'", file=sys.stderr)
            return 1
        section, option = l
        if section != 'DEFAULT' and not self.config.has_section(section):
            self.config.add_section(section)
        if self.config.has_option(section, option) and \
           self.config.get(section, option) == self.options.value:
            return
        self.config.set(section, option, self.options.value)
        try:
            self.write_config()
        except:
            return 1
        return 0

    def set_disable(self, rids=[], disable=True):
        if len(rids) == 0:
            rids = ['DEFAULT']
        for rid in rids:
            if rid != 'DEFAULT' and not self.config.has_section(rid):
                self.log.error("service", svcname, "has not resource", rid)
                continue
            self.log.info("set %s.disable = %s" % (rid, str(disable)))
            self.config.set(rid, "disable", disable)
        try:
            f = open(self.pathenv, 'w')
        except:
            self.log.error("failed to open", self.pathenv, "for writing")
            return 1

        #
        # if we set DEFAULT.disable = True,
        # we don't want res#n.disable = False
        #
        if len(rids) == 0 and disable:
            for s in self.config.sections():
                if self.config.has_option(s, "disable") and \
                   self.config.getboolean(s, "disable") == False:
                    self.log.info("remove %s.disable = false" % s)
                    self.config.remove_option(s, "disable")

        self.config.write(f)
        return 0

    def enable(self):
        return self.set_disable(self.action_rid, False)

    def disable(self):
        return self.set_disable(self.action_rid, True)

    def delete(self):
        from svcBuilder import delete
        return delete([self.svcname], self.action_rid)

    def docker(self):
        import subprocess
        containers = self.get_resources('container')
        if not hasattr(self, "docker_argv"):
            print("no docker command arguments supplied", file=sys.stderr)
            return 1
        for r in containers:
            if hasattr(r, "docker_cmd"):
                r.docker_start(verbose=False)
                cmd = r.docker_cmd + self.docker_argv
                p = subprocess.Popen(cmd)
                p.communicate()
                return p.returncode
        print("this service has no docker resource", file=sys.stderr)
        return 1

    def freeze(self):
        for r in self.get_resources("hb"):
            r.freeze()
        self.freezer.freeze()

    def thaw(self):
        for r in self.get_resources("hb"):
            r.thaw()
        self.freezer.thaw()

    def frozen(self):
        return self.freezer.frozen()

if __name__ == "__main__" :
    for c in (Svc,) :
        help(c)
    print("""s1=Svc("Zone")""")
    s1=Svc("Zone")
    print("s1=",s1)
    print("""s2=Svc("basic")""")
    s2=Svc("basic")
    print("s2=",s2)
    print("""s1+=Resource("ip")""")
    s1+=Resource("ip")
    print("s1=",s1)
    print("""s1+=Resource("ip")""")
    s1+=Resource("ip")
    print("""s1+=Resource("fs")""")
    s1+=Resource("fs")
    print("""s1+=Resource("fs")""")
    s1+=Resource("fs")
    print("s1=",s1)

    print("""s1.action("status")""")
    s1.action("status")
   0707010000bc88000081a400000000000000000000000154fc7bfb00017898000000660000000300000000000000000000002300000000root/opt/opensvc/lib/svcBuilder.py    #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from __future__ import print_function
import os
import sys
import logging
import re
import socket
import glob

from rcGlobalEnv import *
from rcNode import discover_node
from rcUtilities import *
import rcLogger
import resSyncRsync
import rcExceptions as ex
import rcUtilities
import platform

try:
    import ConfigParser
except ImportError:
    import configparser as ConfigParser

check_privs()

#
# file tree abstraction
#
rcEnv.pathsvc = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
rcEnv.pathbin = os.path.join(rcEnv.pathsvc, 'bin')
rcEnv.pathetc = os.path.join(rcEnv.pathsvc, 'etc')
rcEnv.pathlib = os.path.join(rcEnv.pathsvc, 'lib')
rcEnv.pathlog = os.path.join(rcEnv.pathsvc, 'log')
rcEnv.pathtmp = os.path.join(rcEnv.pathsvc, 'tmp')
rcEnv.pathvar = os.path.join(rcEnv.pathsvc, 'var')
rcEnv.pathlock = os.path.join(rcEnv.pathvar, 'lock')

if 'PATH' not in os.environ:
    os.environ['PATH'] = ""
os.environ['LANG'] = 'C'
os.environ['PATH'] += ':/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin'

def conf_get(svc, conf, s, o, t, scope=False, impersonate=None):
    if t == 'string':
        f = conf.get
    elif t == 'boolean':
        f = conf.getboolean
    elif t == 'integer':
        f = conf.getint
    else:
        raise Exception()

    if not scope:
        if conf.has_option(s, o):
            return f(s, o)
        else:
            raise ex.OptNotFound
    if type(svc) != dict:
        d = {
         'nodes': svc.nodes,
         'drpnodes': svc.drpnodes,
         'encapnodes': svc.encapnodes,
        }
    else:
        d = svc

    if impersonate is None:
        nodename = rcEnv.nodename
    else:
        nodename = impersonate

    if conf.has_option(s, o+"@"+nodename):
        return f(s, o+"@"+nodename)
    elif conf.has_option(s, o+"@nodes") and \
         nodename in d['nodes']:
        return f(s, o+"@nodes")
    elif conf.has_option(s, o+"@drpnodes") and \
         nodename in d['drpnodes']:
        return f(s, o+"@drpnodes")
    elif conf.has_option(s, o+"@encapnodes") and \
         nodename in d['encapnodes']:
        return f(s, o+"@encapnodes")
    elif conf.has_option(s, o):
        return f(s, o)
    else:
        raise ex.OptNotFound

def conf_get_string(svc, conf, s, o):
    return conf_get(svc, conf, s, o, 'string', scope=False)

def conf_get_string_scope(svc, conf, s, o, impersonate=None):
    return conf_get(svc, conf, s, o, 'string', scope=True, impersonate=impersonate)

def conf_get_boolean(svc, conf, s, o):
    return conf_get(svc, conf, s, o, 'boolean', scope=False)

def conf_get_boolean_scope(svc, conf, s, o, impersonate=None):
    return conf_get(svc, conf, s, o, 'boolean', scope=True, impersonate=impersonate)

def conf_get_int(svc, conf, s, o):
    return conf_get(svc, conf, s, o, 'integer', scope=False)

def conf_get_int_scope(svc, conf, s, o, impersonate=None):
    return conf_get(svc, conf, s, o, 'integer', scope=True, impersonate=impersonate)

def svcmode_mod_name(svcmode=''):
    """Returns (moduleName, serviceClassName) implementing the class for
    a given service mode. For example:
    hosted => ('svcHosted', 'SvcHosted')
    """
    if svcmode == 'hosted':
        return ('svcHosted', 'SvcHosted')
    elif svcmode == 'sg':
        return ('svcSg', 'SvcSg')
    elif svcmode == 'rhcs':
        return ('svcRhcs', 'SvcRhcs')
    elif svcmode == 'vcs':
        return ('svcVcs', 'SvcVcs')
    raise ex.excError("unknown service mode: %s"%svcmode)

def get_tags(conf, section, svc):
    try:
        s = conf_get_string_scope(svc, conf, section, 'tags')
    except ex.OptNotFound:
        s = ""
    return set(s.split())

def get_optional(conf, section, svc):
    if not conf.has_section(section):
        try:
            return conf_get_boolean_scope(svc, conf, "DEFAULT", "optional")
        except:
            return False

    # deprecated
    if conf.has_option(section, 'optional_on'):
        nodes = set([])
        l = conf.get(section, "optional_on").split()
        for i in l:
            if i == 'nodes': nodes |= svc.nodes
            elif i == 'drpnodes': nodes |= svc.drpnodes
            else: nodes |= set([i])
        if rcEnv.nodename in nodes:
            return True
        return False

    try:
        return conf_get_boolean_scope(svc, conf, section, "optional")
    except:
        return False

def get_monitor(conf, section, svc):
    if not conf.has_section(section):
        try:
            return conf_get_boolean_scope(svc, conf, "DEFAULT", "monitor")
        except:
            return False

    # deprecated 
    if conf.has_option(section, 'monitor_on'):
        nodes = set([])
        l = conf.get(section, "monitor_on").split()
        for i in l:
            if i == 'nodes': nodes |= svc.nodes
            elif i == 'drpnodes': nodes |= svc.drpnodes
            else: nodes |= set([i])
        if rcEnv.nodename in nodes:
            return True
        return False

    try:
        return conf_get_boolean_scope(svc, conf, section, "monitor")
    except:
        return False

def get_subset(conf, section, svc):
    if not conf.has_section(section):
        return
    try:
        return conf_get_string_scope(svc, conf, section, 'subset')
    except ex.OptNotFound:
        return
    return

def get_restart(conf, section, svc):
    if not conf.has_section(section):
        if conf.has_option('DEFAULT', 'restart'):
            try:
                return conf_get_int_scope(svc, conf, section, 'restart')
            except ex.OptNotFound:
                return 0
        else:
            return 0
    try:
        return conf_get_int_scope(svc, conf, section, 'restart')
    except ex.OptNotFound:
        return 0
    return 0

def get_disabled(conf, section, svc):
    if conf.has_option('DEFAULT', 'disable'):
        svc_disable = conf.getboolean("DEFAULT", "disable")
    else:
        svc_disable = False

    if svc_disable is True:
        return True

    if section == "":
        return svc_disable

    try:
        r = conf_get_boolean_scope(svc, conf, section, 'disable')
        return r
    except ex.OptNotFound:
        pass

    # deprecated
    nodes = set([])
    if conf.has_option(section, 'disable_on'):
        l = conf.get(section, "disable_on").split()
        for i in l:
            if i == 'nodes': nodes |= svc.nodes
            elif i == 'drpnodes': nodes |= svc.drpnodes
            else: nodes |= set([i])
    if rcEnv.nodename in nodes:
        return True
    return False

def need_scsireserv(svc, conf, section):
    """scsireserv = true can be set globally or in a specific
    resource section
    """
    r = False
    try:
        r = conf_get_boolean_scope(svc, conf, section, 'scsireserv')
    except ex.OptNotFound:
        defaults = conf.defaults()
        if 'scsireserv' in defaults:
            r = bool(defaults['scsireserv'])
    return r

def add_scsireserv(svc, resource, conf, section):
    if not need_scsireserv(svc, conf, section):
        return
    try:
        sr = __import__('resScsiReserv'+rcEnv.sysname)
    except ImportError:
        sr = __import__('resScsiReserv')

    try:
        pa = conf_get_boolean_scope(svc, conf, resource.rid, 'no_preempt_abort')
    except ex.OptNotFound:
        defaults = conf.defaults()
        if 'no_preempt_abort' in defaults:
            pa = bool(defaults['no_preempt_abort'])
        else:
            pa = False

    kwargs = {}
    kwargs['rid'] = resource.rid
    kwargs['tags'] = resource.tags
    kwargs['disks'] = resource.disklist()
    kwargs['no_preempt_abort'] = pa
    kwargs['disabled'] = resource.is_disabled()
    kwargs['optional'] = resource.is_optional()

    r = sr.ScsiReserv(**kwargs)
    svc += r

def add_triggers(svc, resource, conf, section):
    triggers = ['pre_stop', 'pre_start', 'pre_syncnodes', 'pre_syncdrp',
                'post_stop', 'post_start', 'post_syncnodes', 'post_syncdrp',
                'post_syncresync', 'pre_syncresync']
    for trigger in triggers:
        try:
            s = conf_get_string_scope(svc, conf, resource.rid, trigger)
        except ex.OptNotFound:
            continue
        setattr(resource, trigger, s.split())

def always_on_nodes_set(svc, conf, section):
    try:
        always_on_opt = conf.get(section, "always_on").split()
    except:
        always_on_opt = []
    always_on = set([])
    if 'nodes' in always_on_opt:
        always_on |= svc.nodes
    if 'drpnodes' in always_on_opt:
        always_on |= svc.drpnodes
    always_on |= set(always_on_opt) - set(['nodes', 'drpnodes'])
    return always_on

def get_sync_args(conf, s, svc):
    kwargs = {}
    defaults = conf.defaults()
    if conf.has_option(s, 'sync_max_delay'):
        kwargs['sync_max_delay'] = conf_get_int_scope(svc, conf, s, 'sync_max_delay')
    elif 'sync_max_delay' in defaults:
        kwargs['sync_max_delay'] = conf_get_int_scope(svc, conf, 'DEFAULT', 'sync_max_delay')
    if conf.has_option(s, 'schedule'):
        kwargs['schedule'] = conf_get_string_scope(svc, conf, s, 'schedule')
    elif conf.has_option(s, 'sync_period'):
        # old schedule syntax compatibility
        from rcScheduler import Scheduler
        kwargs['schedule'] = Scheduler().sched_convert_to_schedule(conf, s, prefix='sync_')
    elif 'sync_schedule' in defaults:
        kwargs['schedule'] = conf_get_string_scope(svc, conf, 'DEFAULT', 'sync_schedule')
    return kwargs

def add_resources(restype, svc, conf):
    for s in conf.sections():
        if s != 'app' and s != restype and re.match(restype+'#[0-9]', s, re.I) is None:
            continue
        if svc.encap and 'encap' not in get_tags(conf, s, svc):
            continue
        if not svc.encap and 'encap' in get_tags(conf, s, svc):
            svc.has_encap_resources = True
            continue
        globals()['add_'+restype](svc, conf, s)
 
def add_ip(svc, conf, s):
    """Parse the configuration file and add an ip object for each [ip#n]
    section. Ip objects are stored in a list in the service object.
    """
    kwargs = {}

    try:
        kwargs['ipName'] = conf_get_string_scope(svc, conf, s, 'ipname')
    except ex.OptNotFound:
        svc.log.error("nor ipname and ipname@%s defined in config file section %s"%(rcEnv.nodename, s))
        return

    try:
        kwargs['ipDev'] = conf_get_string_scope(svc, conf, s, 'ipdev')
    except ex.OptNotFound:
        svc.log.debug('ipdev not found in ip section %s'%s)
        return

    try:
        kwargs['mask'] = conf_get_string_scope(svc, conf, s, 'netmask')
    except ex.OptNotFound:
        pass

    try:
        kwargs['gateway'] = conf_get_string_scope(svc, conf, s, 'gateway')
    except ex.OptNotFound:
        pass

    try:
        kwargs['zone'] = conf_get_string_scope(svc, conf, s, 'zone')
    except ex.OptNotFound:
        pass

    try:
        rtype = conf_get_string_scope(svc, conf, s, 'type')
    except ex.OptNotFound:
        rtype = None

    if rtype == "crossbow":
        if 'zone' in kwargs:
            svc.log.error("'zone' and 'type=crossbow' are incompatible in section %s"%s)
            return
        ip = __import__('resIp'+'Crossbow')
    elif 'zone' in kwargs:
        ip = __import__('resIp'+'Zone')
    else:
        ip = __import__('resIp'+rcEnv.sysname)

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)
    r = ip.Ip(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_drbd(svc, conf, s):
    """Parse the configuration file and add a drbd object for each [drbd#n]
    section. Drbd objects are stored in a list in the service object.
    """
    kwargs = {}

    try:
        kwargs['res'] = conf_get_string(svc, conf, s, 'res')
    except ex.OptNotFound:
        svc.log.error("res must be set in section %s"%s)
        return

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)
    drbd = __import__('resDrbd')
    r = drbd.Drbd(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_vdisk(svc, conf, s):
    kwargs = {}
    devpath = {}

    for attr, val in conf.items(s):
        if 'path@' in attr:
            devpath[attr.replace('path@','')] = val

    if len(devpath) == 0:
        svc.log.error("path@node must be set in section %s"%s)
        return

    kwargs['devpath'] = devpath
    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)
    vdisk = __import__('resVdisk')
    r = vdisk.Vdisk(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_stonith(svc, conf, s):
    if rcEnv.nodename in svc.drpnodes:
        # no stonith on DRP nodes
        return

    kwargs = {}

    try:
        _type = conf_get_string(svc, conf, s, 'type')
        if len(_type) > 1:
            _type = _type[0].upper()+_type[1:].lower()
    except ex.OptNotFound:
        svc.log.error("type must be set in section %s"%s)
        return

    if _type in ('Ilo'):
        try:
            kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
        except ex.OptNotFound:
            pass
        try:
            kwargs['name'] = conf_get_string_scope(svc, conf, s, 'target')
        except ex.OptNotFound:
            pass
    
        if 'name' not in kwargs:
            svc.log.error("target must be set in section %s"%s)
            return
    elif _type in ('Callout'):
        try:
            kwargs['cmd'] = conf_get_string_scope(svc, conf, s, 'cmd')
        except ex.OptNotFound:
            pass
    
        if 'cmd' not in kwargs:
            svc.log.error("cmd must be set in section %s"%s)
            return

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)

    st = __import__('resStonith'+_type)
    try:
        st = __import__('resStonith'+_type)
    except ImportError:
        svc.log.error("resStonith%s is not implemented"%_type)
        return

    r = st.Stonith(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_hb(svc, conf, s):
    if rcEnv.nodename in svc.drpnodes:
        # no heartbeat on DRP nodes
        return

    kwargs = {}

    try:
        hbtype = conf_get_string(svc, conf, s, 'type').lower()
    except ex.OptNotFound:
        svc.log.error("type must be set in section %s"%s)
        return

    try:
        kwargs['name'] = conf_get_string(svc, conf, s, 'name')
    except ex.OptNotFound:
        pass

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)

    if hbtype == 'openha':
        hbtype = 'OpenHA'
    elif hbtype == 'linuxha':
        hbtype = 'LinuxHA'

    try:
        hb = __import__('resHb'+hbtype)
    except ImportError:
        svc.log.error("resHb%s is not implemented"%hbtype)
        return

    r = hb.Hb(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_loop(svc, conf, s):
    """Parse the configuration file and add a loop object for each [loop#n]
    section. Loop objects are stored in a list in the service object.
    """
    kwargs = {}

    try:
        kwargs['loopFile'] = conf_get_string_scope(svc, conf, s, 'file')
    except ex.OptNotFound:
        svc.log.error("file must be set in section %s"%s)
        return

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    try:
        loop = __import__('resLoop'+rcEnv.sysname)
    except ImportError:
        svc.log.error("resLoop%s is not implemented"%rcEnv.sysname)
        return

    r = loop.Loop(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_vg(svc, conf, s):
    """Parse the configuration file and add a vg object for each [vg#n]
    section. Vg objects are stored in a list in the service object.
    """
    kwargs = {}
    lock = None

    try:
        vgtype = conf_get_string_scope(svc, conf, s, 'vgtype')
        if len(vgtype) > 2:
            vgtype = vgtype[0].upper() + vgtype[1:].lower()
    except ex.OptNotFound:
        vgtype = rcEnv.sysname

    try:
        vgtype = conf_get_string_scope(svc, conf, s, 'type')
        if len(vgtype) > 2:
            vgtype = vgtype[0].upper() + vgtype[1:].lower()
    except ex.OptNotFound:
        vgtype = rcEnv.sysname

    if vgtype == 'Rados':
        vgtype += rcEnv.sysname
        try:
            kwargs['images'] = conf_get_string_scope(svc, conf, s, 'images').split()
        except ex.OptNotFound:
            pass
        try:
            kwargs['keyring'] = conf_get_string_scope(svc, conf, s, 'keyring')
        except ex.OptNotFound:
            pass
        try:
            kwargs['client_id'] = conf_get_string_scope(svc, conf, s, 'client_id')
        except ex.OptNotFound:
            pass
        try:
            lock_shared_tag = conf_get_string_scope(svc, conf, s, 'lock_shared_tag')
        except ex.OptNotFound:
            lock_shared_tag = None
        try:
            lock = conf_get_string_scope(svc, conf, s, 'lock')
        except ex.OptNotFound:
            pass
    if vgtype == 'Raw':
        vgtype += rcEnv.sysname
        try:
            kwargs['user'] = conf_get_string_scope(svc, conf, s, 'user')
        except ex.OptNotFound:
            pass
        try:
            kwargs['group'] = conf_get_string_scope(svc, conf, s, 'user')
        except ex.OptNotFound:
            pass
        try:
            kwargs['perm'] = conf_get_string_scope(svc, conf, s, 'perm')
        except ex.OptNotFound:
            pass
        try:
            kwargs['dummy'] = conf_get_boolean_scope(svc, conf, s, 'dummy')
        except ex.OptNotFound:
            pass
    elif vgtype == 'Gandi':
        try:
            kwargs['cloud_id'] = conf_get_string_scope(svc, conf, s, 'cloud_id')
        except ex.OptNotFound:
            svc.log.error("cloud_id must be set in section %s"%s)
            return
        try:
            kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
        except ex.OptNotFound:
            svc.log.error("name must be set in section %s"%s)
            return
        try:
            kwargs['node'] = conf_get_string_scope(svc, conf, s, 'node')
        except ex.OptNotFound:
            pass
        try:
            kwargs['user'] = conf_get_string_scope(svc, conf, s, 'user')
        except ex.OptNotFound:
            pass
        try:
            kwargs['group'] = conf_get_string_scope(svc, conf, s, 'user')
        except ex.OptNotFound:
            pass
        try:
            kwargs['perm'] = conf_get_string_scope(svc, conf, s, 'perm')
        except ex.OptNotFound:
            pass

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'vgname')
    except ex.OptNotFound:
        if not vgtype.startswith("Raw") and not vgtype.startswith("Rados") and vgtype != "Gandi":
            svc.log.error("vgname must be set in section %s"%s)
            return

    try:
        kwargs['dsf'] = conf_get_boolean_scope(svc, conf, s, 'dsf')
    except ex.OptNotFound:
        pass

    try:
        kwargs['devs'] = set(conf_get_string_scope(svc, conf, s, 'devs').split())
    except ex.OptNotFound:
        if vgtype == "Raw":
            svc.log.error("devs must be set in section %s"%s)
            return

    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    try:
        vg = __import__('resVg'+vgtype)
    except ImportError:
        svc.log.error("vg type %s is not implemented"%vgtype)
        return

    r = vg.Vg(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

    if not lock:
        return

    # rados locking resource
    kwargs["rid"] = kwargs["rid"]+"lock"
    kwargs["lock"] = lock
    kwargs["lock_shared_tag"] = lock_shared_tag
    r = vg.VgLock(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_vmdg(svc, conf, s):
    kwargs = {}

    try:
        kwargs['container_id'] = conf_get_string_scope(svc, conf, s, 'container_id')
    except ex.OptNotFound:
        svc.log.error("container_id must be set in section %s"%s)
        return

    if not conf.has_section(kwargs['container_id']):
        svc.log.error("%s.container_id points to an invalid section"%kwargs['container_id'])
        return

    try:
        container_type = conf_get_string_scope(svc, conf, kwargs['container_id'], 'type')
    except ex.OptNotFound:
        svc.log.error("type must be set in section %s"%kwargs['container_id'])
        return

    if container_type == 'ldom':
        vg = __import__('resVgLdom')
    else:
        return

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['name'] = s
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = vg.Vg(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_pool(svc, conf, s):
    """Parse the configuration file and add a pool object for each [pool#n]
    section. Pools objects are stored in a list in the service object.
    """
    kwargs = {}

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'poolname')
    except ex.OptNotFound:
        svc.log.error("poolname must be set in section %s"%s)
        return

    try:
        zone = conf_get_string_scope(svc, conf, s, 'zone')
    except ex.OptNotFound:
        zone = None

    pool = __import__('resVgZfs')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = pool.Pool(**kwargs)

    if zone is not None:
        r.tags.add('zone')
        r.tags.add(zone)

    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_share(svc, conf, s):
    try:
        _type = conf_get_string_scope(svc, conf, s, 'type')
    except ex.OptNotFound:
        svc.log.error("type must be set in section %s"%s)
        return

    fname = 'add_share_'+_type
    if fname not in globals():
        svc.log.error("type '%s' not supported in section %s"%(_type, s)) 
    globals()[fname](svc, conf, s)

def add_share_nfs(svc, conf, s):
    kwargs = {}

    try:
        kwargs['path'] = conf_get_string_scope(svc, conf, s, 'path')
    except ex.OptNotFound:
        svc.log.error("path must be set in section %s"%s)
        return

    try:
        kwargs['opts'] = conf_get_string_scope(svc, conf, s, 'opts')
    except ex.OptNotFound:
        svc.log.error("opts must be set in section %s"%s)
        return

    try:
        share = __import__('resShareNfs'+rcEnv.sysname)
    except ImportError:
        svc.log.error("resShareNfs%s is not implemented"%rcEnv.sysname)
        return

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = share.Share(**kwargs)

    add_triggers(svc, r, conf, s)
    svc += r

def add_fs(svc, conf, s):
    """Parse the configuration file and add a fs object for each [fs#n]
    section. Fs objects are stored in a list in the service object.
    """
    kwargs = {}

    try:
        kwargs['device'] = conf_get_string_scope(svc, conf, s, 'dev')
    except ex.OptNotFound:
        svc.log.error("dev must be set in section %s"%s)
        return

    try:
        kwargs['mountPoint'] = conf_get_string_scope(svc, conf, s, 'mnt')
    except ex.OptNotFound:
        svc.log.error("mnt must be set in section %s"%s)
        return

    if kwargs['mountPoint'][-1] == '/':
        """ Remove trailing / to not risk losing rsync src trailing /
            upon snap mountpoint substitution.
        """
        kwargs['mountPoint'] = kwargs['mountPoint'][0:-1]

    try:
        kwargs['fsType'] = conf_get_string_scope(svc, conf, s, 'type')
    except ex.OptNotFound:
        kwargs['fsType'] = ""

    try:
        kwargs['mntOpt'] = conf_get_string_scope(svc, conf, s, 'mnt_opt')
    except ex.OptNotFound:
        kwargs['mntOpt'] = ""

    try:
        kwargs['snap_size'] = conf_get_int_scope(svc, conf, s, 'snap_size')
    except ex.OptNotFound:
        pass

    try:
        zone = conf_get_string_scope(svc, conf, s, 'zone')
    except:
        zone = None

    if zone is not None:
        zp = None
        for r in svc.get_resources("container.zone"):
            if r.name == zone:
                zp = r.zonepath
                break
        if zp is None:
            svc.log.error("zone %s, referenced in %s, not found"%(zone, s))
            raise ex.excError()
        kwargs['mountPoint'] = os.path.realpath(zp+'/root/'+kwargs['mountPoint'])

    try:
        mount = __import__('resMount'+rcEnv.sysname)
    except ImportError:
        svc.log.error("resMount%s is not implemented"%rcEnv.sysname)
        return

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = mount.Mount(**kwargs)

    if zone is not None:
        r.tags.add(zone)
        r.tags.add('zone')

    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers_esx(svc, conf, s):
    kwargs = {}

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
    except ex.OptNotFound:
        kwargs['name'] = svc.svcname

    try:
        kwargs['guestos'] = conf_get_string_scope(svc, conf, s, 'guestos')
    except ex.OptNotFound:
        pass

    m = __import__('resContainerEsx')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.Esx(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers_hpvm(svc, conf, s):
    kwargs = {}

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
    except ex.OptNotFound:
        kwargs['name'] = svc.svcname

    try:
        kwargs['guestos'] = conf_get_string_scope(svc, conf, s, 'guestos')
    except ex.OptNotFound:
        pass

    m = __import__('resContainerHpVm')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.HpVm(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers_ldom(svc, conf, s):
    kwargs = {}

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
    except ex.OptNotFound:
        kwargs['name'] = svc.svcname

    try:
        kwargs['guestos'] = conf_get_string_scope(svc, conf, s, 'guestos')
    except ex.OptNotFound:
        pass

    m = __import__('resContainerLdom')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.Ldom(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers_vbox(svc, conf, s):
    kwargs = {}

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
    except ex.OptNotFound:
        kwargs['name'] = svc.svcname

    try:
        kwargs['guestos'] = conf_get_string_scope(svc, conf, s, 'guestos')
    except ex.OptNotFound:
        pass

    m = __import__('resContainerVbox')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.Vbox(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers_xen(svc, conf, s):
    kwargs = {}

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
    except ex.OptNotFound:
        kwargs['name'] = svc.svcname

    try:
        kwargs['guestos'] = conf_get_string_scope(svc, conf, s, 'guestos')
    except ex.OptNotFound:
        pass

    m = __import__('resContainerXen')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.Xen(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers_zone(svc, conf, s):
    kwargs = {}

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
    except ex.OptNotFound:
        kwargs['name'] = svc.svcname

    try:
        kwargs['guestos'] = conf_get_string_scope(svc, conf, s, 'guestos')
    except ex.OptNotFound:
        pass

    m = __import__('resContainerZone')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.Zone(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)



def add_containers_vcloud(svc, conf, s):
    kwargs = {}

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
    except ex.OptNotFound:
        kwargs['name'] = svc.svcname

    try:
        kwargs['guestos'] = conf_get_string_scope(svc, conf, s, 'guestos')
    except ex.OptNotFound:
        pass

    try:
        kwargs['cloud_id'] = conf_get_string_scope(svc, conf, s, 'cloud_id')
    except ex.OptNotFound:
        svc.log.error("cloud_id must be set in section %s"%s)
        return

    try:
        kwargs['vapp'] = conf_get_string_scope(svc, conf, s, 'vapp')
    except ex.OptNotFound:
        svc.log.error("vapp must be set in section %s"%s)
        return

    m = __import__('resContainerVcloud')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.CloudVm(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers_amazon(svc, conf, s):
    kwargs = {}

    # mandatory keywords
    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
    except ex.OptNotFound:
        kwargs['name'] = svc.svcname

    try:
        kwargs['cloud_id'] = conf_get_string_scope(svc, conf, s, 'cloud_id')
    except ex.OptNotFound:
        svc.log.error("cloud_id must be set in section %s"%s)
        return

    # optional keywords
    try:
        kwargs['guestos'] = conf_get_string_scope(svc, conf, s, 'guestos')
    except ex.OptNotFound:
        pass

    # provisioning keywords
    try:
        kwargs['image_id'] = conf_get_string_scope(svc, conf, s, 'image_id')
    except ex.OptNotFound:
        pass

    try:
        kwargs['size'] = conf_get_string_scope(svc, conf, s, 'size')
    except ex.OptNotFound:
        pass

    try:
        kwargs['key_name'] = conf_get_string_scope(svc, conf, s, 'key_name')
    except ex.OptNotFound:
        pass

    try:
        kwargs['subnet'] = conf_get_string_scope(svc, conf, s, 'subnet')
    except ex.OptNotFound:
        pass

    m = __import__('resContainerAmazon')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.CloudVm(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers_openstack(svc, conf, s):
    kwargs = {}

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
    except ex.OptNotFound:
        kwargs['name'] = svc.svcname

    try:
        kwargs['guestos'] = conf_get_string_scope(svc, conf, s, 'guestos')
    except ex.OptNotFound:
        pass

    try:
        kwargs['cloud_id'] = conf_get_string_scope(svc, conf, s, 'cloud_id')
    except ex.OptNotFound:
        svc.log.error("cloud_id must be set in section %s"%s)
        return

    try:
        kwargs['size'] = conf_get_string_scope(svc, conf, s, 'size')
    except ex.OptNotFound:
        svc.log.error("size must be set in section %s"%s)
        return

    try:
        kwargs['key_name'] = conf_get_string_scope(svc, conf, s, 'key_name')
    except ex.OptNotFound:
        svc.log.error("key_name must be set in section %s"%s)
        return

    try:
        kwargs['shared_ip_group'] = conf_get_string_scope(svc, conf, s, 'shared_ip_group')
    except ex.OptNotFound:
        pass

    m = __import__('resContainerOpenstack')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.CloudVm(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers_vz(svc, conf, s):
    kwargs = {}

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
    except ex.OptNotFound:
        kwargs['name'] = svc.svcname

    try:
        kwargs['guestos'] = conf_get_string_scope(svc, conf, s, 'guestos')
    except ex.OptNotFound:
        pass

    m = __import__('resContainerVz')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.Vz(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers_kvm(svc, conf, s):
    kwargs = {}

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
    except ex.OptNotFound:
        svc.log.error("name must be set in section %s"%s)
        return

    try:
        kwargs['guestos'] = conf_get_string_scope(svc, conf, s, 'guestos')
    except ex.OptNotFound:
        pass

    m = __import__('resContainerKvm')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.Kvm(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers_srp(svc, conf, s):
    kwargs = {}

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
    except ex.OptNotFound:
        kwargs['name'] = svc.svcname

    try:
        kwargs['guestos'] = conf_get_string_scope(svc, conf, s, 'guestos')
    except ex.OptNotFound:
        pass

    m = __import__('resContainerSrp')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.Srp(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers_lxc(svc, conf, s):
    kwargs = {}

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
    except ex.OptNotFound:
        kwargs['name'] = svc.svcname

    try:
        kwargs['guestos'] = conf_get_string_scope(svc, conf, s, 'guestos')
    except ex.OptNotFound:
        pass

    try:
        kwargs['cf'] = conf_get_string_scope(svc, conf, s, 'cf')
    except ex.OptNotFound:
        pass

    m = __import__('resContainerLxc')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.Lxc(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers_docker(svc, conf, s):
    kwargs = {}

    try:
        kwargs['run_image'] = conf_get_string_scope(svc, conf, s, 'run_image')
    except ex.OptNotFound:
        svc.log.error("'run_image' parameter is mandatory in section %s"%s)
        return

    try:
        kwargs['run_command'] = conf_get_string_scope(svc, conf, s, 'run_command')
    except ex.OptNotFound:
        pass

    try:
        kwargs['run_args'] = conf_get_string_scope(svc, conf, s, 'run_args')
    except ex.OptNotFound:
        pass

    m = __import__('resContainerDocker')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.Docker(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers_ovm(svc, conf, s):
    kwargs = {}

    try:
        kwargs['uuid'] = conf_get_string_scope(svc, conf, s, 'uuid')
    except ex.OptNotFound:
        svc.log.error("uuid must be set in section %s"%s)
        return

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
    except ex.OptNotFound:
        svc.log.error("name must be set in section %s"%s)
        return

    try:
        kwargs['guestos'] = conf_get_string_scope(svc, conf, s, 'guestos')
    except ex.OptNotFound:
        pass

    m = __import__('resContainerOvm')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.Ovm(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers_jail(svc, conf, s):
    kwargs = {}

    try:
        kwargs['jailroot'] = conf_get_string_scope(svc, conf, s, 'jailroot')
    except ex.OptNotFound:
        svc.log.error("jailroot must be set in section %s"%s)
        return

    try:
        kwargs['name'] = conf_get_string_scope(svc, conf, s, 'name')
    except ex.OptNotFound:
        svc.log.error("name must be set in section %s"%s)
        return

    try:
        kwargs['ips'] = conf_get_string_scope(svc, conf, s, 'ips').split()
    except ex.OptNotFound:
        pass

    try:
        kwargs['ip6s'] = conf_get_string_scope(svc, conf, s, 'ip6s').split()
    except ex.OptNotFound:
        pass

    m = __import__('resContainerJail')

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)

    r = m.Jail(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r
    add_scsireserv(svc, r, conf, s)

def add_containers(svc, conf):
    for t in rcEnv.vt_supported:
        add_containers_resources(t, svc, conf)

def add_containers_resources(subtype, svc, conf):
    add_sub_resources('container', subtype, svc, conf)

def add_mandatory_syncs(svc, conf):
    """Mandatory files to sync:
    1/ to all nodes: service definition
    2/ to drpnodes: system files to replace on the drpnode in case of startdrp
    """

    """1
    """
    if len(svc.nodes|svc.drpnodes) > 1:
        kwargs = {}
        src = []
        src.append(os.path.join(rcEnv.pathetc, svc.svcname))
        src.append(os.path.join(rcEnv.pathetc, svc.svcname+'.env'))
        src.append(os.path.join(rcEnv.pathetc, svc.svcname+'.d'))
        localrc = os.path.join(rcEnv.pathetc, svc.svcname+'.dir')
        cluster = os.path.join(rcEnv.pathetc, svc.svcname+'.cluster')
        if os.path.exists(cluster):
            src.append(cluster)
        if os.path.exists(localrc):
            src.append(localrc)
        for rs in svc.resSets:
            for r in rs.resources:
                src += r.files_to_sync()
        dst = os.path.join("/")
        exclude = ['--exclude=*.core']
        targethash = {'nodes': svc.nodes, 'drpnodes': svc.drpnodes}
        kwargs['rid'] = "sync#i0"
        kwargs['src'] = src
        kwargs['dst'] = dst
        kwargs['options'] = ['-R']+exclude
        if conf.has_option(kwargs['rid'], 'options'):
            kwargs['options'] += rcUtilities.cmdline2list(conf.get(kwargs['rid'], 'options'))
        kwargs['target'] = targethash
        kwargs['internal'] = True
        kwargs['disabled'] = get_disabled(conf, kwargs['rid'], svc)
        kwargs['optional'] = get_optional(conf, kwargs['rid'], svc)
        kwargs.update(get_sync_args(conf, 'sync', svc))
        r = resSyncRsync.Rsync(**kwargs)
        svc += r

    """2
    """
    if len(svc.drpnodes) == 0:
        return

    targethash = {'drpnodes': svc.drpnodes}
    """ Reparent all PRD backed-up file in drp_path/node on the drpnode
    """
    dst = os.path.join(rcEnv.drp_path, rcEnv.nodename)
    i = 0
    for src, exclude in rcEnv.drp_sync_files:
        """'-R' triggers rsync relative mode
        """
        kwargs = {}
        src = [ s for s in src if os.path.exists(s) ]
        if len(src) == 0:
            continue
        i += 1
        kwargs['rid'] = "sync#i"+str(i)
        kwargs['src'] = src
        kwargs['dst'] = dst
        kwargs['options'] = ['-R']+exclude
        if conf.has_option(kwargs['rid'], 'options'):
            kwargs['options'] += rcUtilities.cmdline2list(conf.get(kwargs['rid'], 'options'))
        kwargs['target'] = targethash
        kwargs['internal'] = True
        kwargs['disabled'] = get_disabled(conf, kwargs['rid'], svc)
        kwargs['optional'] = get_optional(conf, kwargs['rid'], svc)
        kwargs.update(get_sync_args(conf, 'sync', svc))
        r = resSyncRsync.Rsync(**kwargs)
        svc += r

def add_syncs_resources(subtype, svc, conf):
    add_sub_resources('sync', subtype, svc, conf, default_subtype="rsync")

def add_sub_resources(restype, subtype, svc, conf, default_subtype=None):
    for s in conf.sections():
        if re.match(restype+'#i[0-9]', s, re.I) is None and \
           re.match(restype+'#[0-9]', s, re.I) is None:
            continue
        if svc.encap and 'encap' not in get_tags(conf, s, svc):
            continue
        if not svc.encap and 'encap' in get_tags(conf, s, svc):
            svc.has_encap_resources = True
            continue
        try:
            res_subtype = conf_get_string_scope(svc, conf, s, "type")
        except ex.OptNotFound:
            res_subtype = default_subtype

        if subtype != res_subtype:
            continue

        globals()['add_'+restype+'s_'+subtype](svc, conf, s)

def add_syncs(svc, conf):
    add_syncs_resources('rsync', svc, conf)
    add_syncs_resources('netapp', svc, conf)
    add_syncs_resources('nexenta', svc, conf)
    add_syncs_resources('radossnap', svc, conf)
    add_syncs_resources('radosclone', svc, conf)
    add_syncs_resources('symclone', svc, conf)
    add_syncs_resources('symsrdfs', svc, conf)
    add_syncs_resources('hp3par', svc, conf)
    add_syncs_resources('ibmdssnap', svc, conf)
    add_syncs_resources('evasnap', svc, conf)
    add_syncs_resources('necismsnap', svc, conf)
    add_syncs_resources('dcssnap', svc, conf)
    add_syncs_resources('dcsckpt', svc, conf)
    add_syncs_resources('dds', svc, conf)
    add_syncs_resources('zfs', svc, conf)
    add_syncs_resources('btrfs', svc, conf)
    add_syncs_resources('docker', svc, conf)
    add_mandatory_syncs(svc, conf)

def add_syncs_docker(svc, conf, s):
    kwargs = {}

    try:
        kwargs['target'] = conf_get_string_scope(svc, conf, s, 'target').split(' ')
    except ex.OptNotFound:
        return

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))

    m = __import__('resSyncDocker')
    r = m.SyncDocker(**kwargs)
    svc += r

def add_syncs_btrfs(svc, conf, s):
    kwargs = {}

    try:
        kwargs['src'] = conf_get_string_scope(svc, conf, s, 'src')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have src set" % s)
        return

    try:
        kwargs['dst'] = conf_get_string_scope(svc, conf, s, 'dst')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have dst set" % s)
        return

    try:
        kwargs['target'] = conf_get_string_scope(svc, conf, s, 'target').split()
    except ex.OptNotFound:
        svc.log.error("config file section %s must have target set" % s)
        return

    try:
        kwargs['recursive'] = conf_get_boolean(svc, conf, s, 'recursive')
    except ex.OptNotFound:
        pass

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))
    btrfs = __import__('resSyncBtrfs')
    r = btrfs.SyncBtrfs(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_syncs_zfs(svc, conf, s):
    kwargs = {}

    try:
        kwargs['src'] = conf_get_string_scope(svc, conf, s, 'src')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have src set" % s)
        return

    try:
        kwargs['dst'] = conf_get_string_scope(svc, conf, s, 'dst')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have dst set" % s)
        return

    try:
        kwargs['target'] = conf_get_string_scope(svc, conf, s, 'target').split()
    except ex.OptNotFound:
        svc.log.error("config file section %s must have target set" % s)
        return

    try:
        kwargs['recursive'] = conf_get_boolean(svc, conf, s, 'recursive')
    except ex.OptNotFound:
        pass

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))
    zfs = __import__('resSyncZfs')
    r = zfs.SyncZfs(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_syncs_dds(svc, conf, s):
    kwargs = {}

    try:
        kwargs['src'] = conf_get_string_scope(svc, conf, s, 'src')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have src set" % s)
        return

    dsts = {}
    for node in svc.nodes | svc.drpnodes:
        dst = conf_get_string_scope(svc, conf, s, 'dst', impersonate=node)
        dsts[node] = dst

    if len(dsts) == 0:
        for node in svc.nodes | svc.drpnodes:
            dsts[node] = kwargs['src']
    
    kwargs['dsts'] = dsts

    try:
        kwargs['target'] = conf_get_string_scope(svc, conf, s, 'target').split()
    except ex.OptNotFound:
        svc.log.error("config file section %s must have target set" % s)
        return

    try:
        kwargs['sender'] = conf_get_string(svc, conf, s, 'sender')
    except ex.OptNotFound:
        pass

    try:
        kwargs['snap_size'] = conf_get_int_scope(svc, conf, s, 'snap_size')
    except ex.OptNotFound:
        pass

    try:
        kwargs['delta_store'] = conf_get_string_scope(svc, conf, s, 'delta_store')
    except ex.OptNotFound:
        pass

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))
    dds = __import__('resSyncDds')
    r = dds.syncDds(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_syncs_dcsckpt(svc, conf, s):
    kwargs = {}

    try:
        kwargs['dcs'] = set(conf_get_string_scope(svc, conf, s, 'dcs').split())
    except ex.OptNotFound:
        svc.log.error("config file section %s must have 'dcs' set" % s)
        return

    try:
        kwargs['manager'] = set(conf_get_string_scope(svc, conf, s, 'manager').split())
    except ex.OptNotFound:
        svc.log.error("config file section %s must have 'manager' set" % s)
        return

    import json
    pairs = []
    if 'pairs' in conf.options(s):
        try:
            pairs = json.loads(conf.get(s, 'pairs'))
            if len(pairs) == 0:
                svc.log.error("config file section %s must have 'pairs' set" % s)
                return
        except:
            svc.log.error("json error parsing 'pairs' in section %s" % s)
    kwargs['pairs'] = pairs

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))
    try:
        sc = __import__('resSyncDcsCkpt'+rcEnv.sysname)
    except:
        sc = __import__('resSyncDcsCkpt')
    r = sc.syncDcsCkpt(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_syncs_dcssnap(svc, conf, s):
    kwargs = {}

    try:
        kwargs['dcs'] = set(conf_get_string(svc, conf, s, 'dcs').split())
    except ex.OptNotFound:
        svc.log.error("config file section %s must have 'dcs' set" % s)
        return

    try:
        kwargs['manager'] = set(conf_get_string(svc, conf, s, 'manager').split())
    except ex.OptNotFound:
        svc.log.error("config file section %s must have 'manager' set" % s)
        return

    try:
        kwargs['snapname'] = set(conf_get_string(svc, conf, s, 'snapname').split())
    except ex.OptNotFound:
        svc.log.error("config file section %s must have 'snapname' set" % s)
        return

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))
    try:
        sc = __import__('resSyncDcsSnap'+rcEnv.sysname)
    except:
        sc = __import__('resSyncDcsSnap')
    r = sc.syncDcsSnap(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_syncs_necismsnap(svc, conf, s):
    kwargs = {}

    try:
        kwargs['array'] = conf_get_string_scope(svc, conf, s, 'array')
    except ex.OptNotFound:
        pass

    try:
        kwargs['devs'] = conf_get_string_scope(svc, conf, s, 'devs')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have devs set" % s)
        return

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))
    try:
        sc = __import__('resSyncNecIsmSnap'+rcEnv.sysname)
    except:
        sc = __import__('resSyncNecIsmSnap')
    r = sc.syncNecIsmSnap(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_syncs_evasnap(svc, conf, s):
    kwargs = {}

    try:
        kwargs['eva_name'] = conf_get_string(svc, conf, s, 'eva_name')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have eva_name set" % s)
        return

    try:
        kwargs['snap_name'] = conf_get_string(svc, conf, s, 'snap_name')
    except ex.OptNotFound:
        kwargs['snap_name'] = svc.svcname

    import json
    pairs = []
    if 'pairs' in conf.options(s):
        pairs = json.loads(conf.get(s, 'pairs'))
    if len(pairs) == 0:
        svc.log.error("config file section %s must have pairs set" % s)
        return
    else:
        kwargs['pairs'] = pairs

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))
    try:
        sc = __import__('resSyncEvasnap'+rcEnv.sysname)
    except:
        sc = __import__('resSyncEvasnap')
    r = sc.syncEvasnap(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_syncs_hp3par(svc, conf, s):
    kwargs = {}

    try:
        kwargs['mode'] = conf_get_string_scope(svc, conf, s, 'mode')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have mode set" % s)
        return

    try:
        kwargs['array'] = conf_get_string_scope(svc, conf, s, 'array')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have array set" % s)
        return

    rcg_names = {}
    for node in svc.nodes | svc.drpnodes:
        array = conf_get_string_scope(svc, conf, s, 'array', impersonate=node)
        rcg = conf_get_string_scope(svc, conf, s, 'rcg', impersonate=node)
        rcg_names[array] = rcg

    if len(rcg_names) == 0:
        svc.log.error("config file section %s must have rcg set" % s)
        return

    kwargs['rcg_names'] = rcg_names

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))
    try:
        sc = __import__('resSyncHp3par'+rcEnv.sysname)
    except:
        sc = __import__('resSyncHp3par')
    r = sc.syncHp3par(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_syncs_symsrdfs(svc, conf, s):
    kwargs = {}

    try:
        kwargs['symdg'] = conf_get_string(svc, conf, s, 'symdg')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have symdg set" % s)
        return

    try:
        kwargs['rdfg'] = conf_get_int(svc, conf, s, 'rdfg')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have rdfg number set" % s)
        return


    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))
    try:
        sc = __import__('resSyncSymSrdfS'+rcEnv.sysname)
    except:
        sc = __import__('resSyncSymSrdfS')
    r = sc.syncSymSrdfS(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r


def add_syncs_radosclone(svc, conf, s):
    kwargs = {}

    try:
        kwargs['client_id'] = conf_get_string_scope(svc, conf, s, 'client_id')
    except ex.OptNotFound:
        pass

    try:
        kwargs['keyring'] = conf_get_string_scope(svc, conf, s, 'keyring')
    except ex.OptNotFound:
        pass

    try:
        kwargs['pairs'] = conf_get_string_scope(svc, conf, s, 'pairs').split()
    except ex.OptNotFound:
        svc.log.error("config file section %s must have pairs set" % s)
        return

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))
    try:
        sc = __import__('resSyncRados'+rcEnv.sysname)
    except:
        sc = __import__('resSyncRados')
    r = sc.syncRadosClone(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_syncs_radossnap(svc, conf, s):
    kwargs = {}

    try:
        kwargs['client_id'] = conf_get_string_scope(svc, conf, s, 'client_id')
    except ex.OptNotFound:
        pass

    try:
        kwargs['keyring'] = conf_get_string_scope(svc, conf, s, 'keyring')
    except ex.OptNotFound:
        pass

    try:
        kwargs['images'] = conf_get_string_scope(svc, conf, s, 'images').split()
    except ex.OptNotFound:
        svc.log.error("config file section %s must have images set" % s)
        return

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))
    try:
        sc = __import__('resSyncRados'+rcEnv.sysname)
    except:
        sc = __import__('resSyncRados')
    r = sc.syncRadosSnap(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_syncs_symclone(svc, conf, s):
    kwargs = {}

    try:
        kwargs['symdg'] = conf_get_string(svc, conf, s, 'symdg')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have symdg set" % s)
        return

    try:
        kwargs['symdevs'] = conf_get_string_scope(svc, conf, s, 'symdevs').split()
    except ex.OptNotFound:
        svc.log.error("config file section %s must have symdevs set" % s)
        return

    try:
        kwargs['precopy_timeout'] = conf_get_int(svc, conf, s, 'precopy_timeout')
    except ex.OptNotFound:
        pass

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))
    try:
        sc = __import__('resSyncSymclone'+rcEnv.sysname)
    except:
        sc = __import__('resSyncSymclone')
    r = sc.syncSymclone(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_syncs_ibmdssnap(svc, conf, s):
    kwargs = {}

    try:
        kwargs['pairs'] = conf_get_string(svc, conf, s, 'pairs').split()
    except ex.OptNotFound:
        svc.log.error("config file section %s must have pairs set" % s)
        return

    try:
        kwargs['array'] = conf_get_string(svc, conf, s, 'array')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have array set" % s)
        return

    try:
        kwargs['bgcopy'] = conf_get_boolean(svc, conf, s, 'bgcopy')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have bgcopy set" % s)
        return

    try:
        kwargs['recording'] = conf_get_boolean(svc, conf, s, 'recording')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have recording set" % s)
        return

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))
    try:
        m = __import__('resSyncIbmdsSnap'+rcEnv.sysname)
    except:
        m = __import__('resSyncIbmdsSnap')
    r = m.syncIbmdsSnap(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_syncs_nexenta(svc, conf, s):
    kwargs = {}

    try:
        kwargs['name'] = conf_get_string(svc, conf, s, 'name')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have 'name' set" % s)
        return

    try:
        kwargs['path'] = conf_get_string_scope(svc, conf, s, 'path')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have path set" % s)
        return

    try:
        kwargs['reversible'] = conf.getboolean(s, "reversible")
    except:
        pass

    filers = {}
    if 'filer' in conf.options(s):
        for n in svc.nodes | svc.drpnodes:
            filers[n] = conf.get(s, 'filer')
    if 'filer@nodes' in conf.options(s):
        for n in svc.nodes:
            filers[n] = conf.get(s, 'filer@nodes')
    if 'filer@drpnodes' in conf.options(s):
        for n in svc.nodes:
            filers[n] = conf.get(s, 'filer@drpnodes')
    for o in conf.options(s):
        if 'filer@' not in o:
            continue
        (filer, node) = o.split('@')
        if node in ('nodes', 'drpnodes'):
            continue
        filers[node] = conf.get(s, o)
    if rcEnv.nodename not in filers:
        svc.log.error("config file section %s must have filer@%s set" %(s, rcEnv.nodename))

    kwargs['filers'] = filers
    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))

    import resSyncNexenta
    r = resSyncNexenta.syncNexenta(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_syncs_netapp(svc, conf, s):
    kwargs = {}

    try:
        kwargs['path'] = conf_get_string_scope(svc, conf, s, 'path')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have path set" % s)
        return

    try:
        kwargs['user'] = conf_get_string_scope(svc, conf, s, 'user')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have user set" % s)
        return

    filers = {}
    if 'filer' in conf.options(s):
        for n in svc.nodes | svc.drpnodes:
            filers[n] = conf.get(s, 'filer')
    if 'filer@nodes' in conf.options(s):
        for n in svc.nodes:
            filers[n] = conf.get(s, 'filer@nodes')
    if 'filer@drpnodes' in conf.options(s):
        for n in svc.nodes:
            filers[n] = conf.get(s, 'filer@drpnodes')
    for o in conf.options(s):
        if 'filer@' not in o:
            continue
        (filer, node) = o.split('@')
        if node in ('nodes', 'drpnodes'):
            continue
        filers[node] = conf.get(s, o)
    if rcEnv.nodename not in filers:
        svc.log.error("config file section %s must have filer@%s set" %(s, rcEnv.nodename))

    kwargs['filers'] = filers
    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))

    import resSyncNetapp
    r = resSyncNetapp.syncNetapp(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_syncs_rsync(svc, conf, s):
    if s.startswith("sync#i"):
        # internal syncs have their own dedicated add function
        return

    options = []
    kwargs = {}
    kwargs['src'] = []
    try:
        _s = conf_get_string_scope(svc, conf, s, 'src')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have src set" % s)
        return

    for src in _s.split():
        kwargs['src'] += glob.glob(src)

    try:
        kwargs['dst'] = conf_get_string_scope(svc, conf, s, 'dst')
    except ex.OptNotFound:
        svc.log.error("config file section %s must have dst set" % s)
        return

    try:
        kwargs['dstfs'] = conf_get_string_scope(svc, conf, s, 'dstfs')
    except ex.OptNotFound:
        pass

    try:
        _s = conf_get_string_scope(svc, conf, s, 'options')
        options += _s.split()
    except ex.OptNotFound:
        pass

    try:
        # for backward compat (use options keyword now)
        _s = conf_get_string_scope(svc, conf, s, 'exclude')
        options += _s.split()
    except ex.OptNotFound:
        pass

    kwargs['options'] = options

    try:
        kwargs['snap'] = conf_get_boolean_scope(svc, conf, s, 'snap')
    except ex.OptNotFound:
        pass

    try:
        _s = conf_get_string_scope(svc, conf, s, 'target')
        target = _s.split()
    except ex.OptNotFound:
        target = []

    try:
        kwargs['bwlimit'] = conf_get_int_scope(svc, conf, s, 'bwlimit')
    except ex.OptNotFound:
        pass

    targethash = {}
    if 'nodes' in target: targethash['nodes'] = svc.nodes
    if 'drpnodes' in target: targethash['drpnodes'] = svc.drpnodes
    kwargs['target'] = targethash
    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs.update(get_sync_args(conf, s, svc))

    r = resSyncRsync.Rsync(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_app(svc, conf, s):
    resApp = ximport('resApp')
    kwargs = {}

    try:
        kwargs['script'] = conf_get_string_scope(svc, conf, s, 'script')
    except ex.OptNotFound:
        svc.log.error("'script' is not defined in config file section %s"%s)
        return

    try:
        kwargs['start'] = conf_get_int_scope(svc, conf, s, 'start')
    except ex.OptNotFound:
        pass
    except:
        svc.log.error("config file section %s param %s must be an integer" % (s, 'start'))
        return

    try:
        kwargs['stop'] = conf_get_int_scope(svc, conf, s, 'stop')
    except ex.OptNotFound:
        pass
    except:
        svc.log.error("config file section %s param %s must be an integer" % (s, 'stop'))
        return

    try:
        kwargs['check'] = conf_get_int_scope(svc, conf, s, 'check')
    except ex.OptNotFound:
        pass
    except:
        svc.log.error("config file section %s param %s must be an integer" % (s, 'check'))
        return

    try:
        kwargs['info'] = conf_get_int_scope(svc, conf, s, 'info')
    except ex.OptNotFound:
        pass
    except:
        svc.log.error("config file section %s param %s must be an integer" % (s, 'info'))
        return

    try:
        kwargs['timeout'] = conf_get_int_scope(svc, conf, s, 'timeout')
    except ex.OptNotFound:
        pass

    kwargs['rid'] = s
    kwargs['subset'] = get_subset(conf, s, svc)
    kwargs['tags'] = get_tags(conf, s, svc)
    kwargs['always_on'] = always_on_nodes_set(svc, conf, s)
    kwargs['disabled'] = get_disabled(conf, s, svc)
    kwargs['optional'] = get_optional(conf, s, svc)
    kwargs['monitor'] = get_monitor(conf, s, svc)
    kwargs['restart'] = get_restart(conf, s, svc)
 
    r = resApp.App(**kwargs)
    add_triggers(svc, r, conf, s)
    svc += r

def add_apps_sysv(svc, conf):
    """
    SysV-like launchers support.
    Translate to app#n resources.
    """
    resApp = ximport('resApp')

    s = "app"
    try:
        initd = conf_get_string_scope(svc, conf, s, 'dir')
    except ex.OptNotFound:
        initd = svc.initd

    disabled = get_disabled(conf, s, svc)
    #optional = get_optional(conf, s, svc)
    monitor = get_monitor(conf, s, svc)
    restart = get_restart(conf, s, svc)

    allocated = []

    def get_next_rid():
        rid_index = 0
        _rid = "app#%d" % rid_index
        while _rid in svc.resources_by_id.keys() + allocated:
            rid_index += 1
            _rid = "app#%d" % rid_index
        allocated.append(_rid)
        return _rid

    def init_app(script):
        d = {
          'script': script,
          'rid': get_next_rid(),
          'info': 50, 
          'optional': True, 
          'disabled': disabled, 
          'monitor': monitor, 
          'restart': restart, 
        }
        return d

    def find_app(script):
        for r in svc.get_resources("app"):
            if r.script.startswith(os.sep):
                rscript = r.script
            else:
                rscript = os.path.join(initd, r.script)
            rscript = os.path.realpath(rscript)
            if rscript == script:
                return r
        return

    def get_seq(s):
        s = s[1:]
        i = 0
        while s[i].isdigit() or i == len(s):
            i += 1
        s = int(s[:i])
        return s

    h = {}
    for f in glob.glob(os.path.join(initd, 'S[0-9]*')):
        script = os.path.realpath(f)
        if find_app(script) is not None:
            continue
        if script not in h:
            h[script] = init_app(script)
        h[script]['start'] = get_seq(os.path.basename(f))

    for f in glob.glob(os.path.join(initd, 'K[0-9]*')):
        script = os.path.realpath(f)
        if find_app(script) is not None:
            continue
        if script not in h:
            h[script] = init_app(script)
        h[script]['stop'] = get_seq(os.path.basename(f))

    for f in glob.glob(os.path.join(initd, 'C[0-9]*')):
        script = os.path.realpath(f)
        if find_app(script) is not None:
            continue
        if script not in h:
            h[script] = init_app(script)
        h[script]['check'] = get_seq(os.path.basename(f))

    if "app" not in svc.type2resSets:
        svc += resApp.RsetApps("app")

    for script, kwargs in h.items():
        r = resApp.App(**kwargs)
        svc += r

def setup_logging():
    """Setup logging to stream + logfile, and logfile rotation
    class Logger instance name: 'log'
    """
    global log
    log = rcLogger.initLogger('INIT')

def build(name):
    """build(name) is in charge of Svc creation
    it return None if service Name is not managed by local node
    else it return new Svc instance
    """
    svcconf = os.path.join(rcEnv.pathetc, name) + '.env'
    svcinitd = os.path.join(rcEnv.pathetc, name) + '.d'
    logfile = os.path.join(rcEnv.pathlog, name) + '.log'
    rcEnv.logfile = logfile

    setup_logging()

    #
    # node discovery is hidden in a separate module to
    # keep it separate from the framework stuff
    #
    discover_node()

    #
    # parse service configuration file
    # class RawConfigParser instance name: 'conf'
    #
    svcmode = "hosted"
    conf = None
    kwargs = {'svcname': name}
    if os.path.isfile(svcconf):
        conf = ConfigParser.RawConfigParser()
        conf.read(svcconf)
        defaults = conf.defaults()
        if "mode" in defaults:
            svcmode = conf_get_string_scope({}, conf, 'DEFAULT', "mode")

        if "encapnodes" in defaults:
            encapnodes = set(conf_get_string_scope({}, conf, 'DEFAULT', "encapnodes").split())
            encapnodes -= set([''])
            encapnodes = set(map(lambda x: x.lower(), encapnodes))
        else:
            encapnodes = set([])

        d_nodes = {'encapnodes': encapnodes}

        if "nodes" in defaults:
            nodes = set(conf_get_string_scope(d_nodes, conf, 'DEFAULT', "nodes").split())
            nodes -= set([''])
            nodes = set(map(lambda x: x.lower(), nodes))
        else:
            nodes = set([])

        if "drpnodes" in defaults:
            drpnodes = set(conf_get_string_scope(d_nodes, conf, 'DEFAULT', "drpnodes").split())
            drpnodes -= set([''])
            drpnodes = set(map(lambda x: x.lower(), drpnodes))
        else:
            drpnodes = set([])

        if "drpnode" in defaults:
            drpnode = conf_get_string_scope(d_nodes, conf, 'DEFAULT', "drpnode").lower()
            drpnodes |= set([drpnode])
            drpnodes -= set([''])
        else:
            drpnode = ''

        d_nodes['nodes'] = nodes
        d_nodes['drpnodes'] = drpnodes

        kwargs['disabled'] = get_disabled(conf, "", "")

        if "pkg_name" in defaults:
            if svcmode not in ["sg", "rhcs", "vcs"]:
                log.error("can not set 'pkg_name' with '%s' mode in %s env"%(svcmode, name))
                return None
            kwargs['pkg_name'] = defaults["pkg_name"]


    #
    # dynamically import the module matching the service mode
    # and instanciate a service
    #
    log.debug('service mode = ' + svcmode)
    mod , svc_class_name = svcmode_mod_name(svcmode)
    svcMod = __import__(mod)
    svc = getattr(svcMod, svc_class_name)(**kwargs)
    svc.svcmode = svcmode
    if "presnap_trigger" in defaults:
        svc.presnap_trigger = defaults["presnap_trigger"].split()
    if "postsnap_trigger" in defaults:
        svc.postsnap_trigger = defaults["postsnap_trigger"].split()

    #
    # Store useful properties
    #
    svc.logfile = logfile
    svc.conf = svcconf
    svc.initd = svcinitd
    svc.config = conf

    #
    # Setup service properties from config file content
    #
    if not hasattr(svc, "nodes"):
        svc.nodes = nodes
    if not hasattr(svc, "drpnodes"):
        svc.drpnodes = drpnodes
    if not hasattr(svc, "drpnode"):
        svc.drpnode = drpnode

    try:
        svc.encapnodes = set(conf_get_string_scope(svc, conf, 'DEFAULT', 'encapnodes').split())
    except ex.OptNotFound:
        pass

    if rcEnv.nodename in svc.encapnodes:
        svc.encap = True
    else:
        svc.encap = False

    #
    # containerization options
    #
    try:
        svc.containerize = conf_get_boolean_scope(svc, conf, "DEFAULT", 'containerize')
    except ex.OptNotFound:
        svc.containerize = False

    try:
        svc.container_cpus = conf_get_string_scope(svc, conf, "DEFAULT", "container_cpus")
    except ex.OptNotFound:
        pass

    try:
        svc.container_cpu_shares = conf_get_string_scope(svc, conf, "DEFAULT", "container_cpu_shares")
    except ex.OptNotFound:
        pass

    try:
        svc.container_mems = conf_get_string_scope(svc, conf, "DEFAULT", "container_mems")
    except ex.OptNotFound:
        pass

    try:
        svc.container_mem_limit = conf_get_string_scope(svc, conf, "DEFAULT", "container_mem_limit")
    except ex.OptNotFound:
        pass

    try:
        svc.container_vmem_limit = conf_get_string_scope(svc, conf, "DEFAULT", "container_vmem_limit")
    except ex.OptNotFound:
        pass

    try:
        svc.autostart_node = conf_get_string_scope(svc, conf, 'DEFAULT', 'autostart_node').split()
    except ex.OptNotFound:
        svc.autostart_node = []

    try:
        anti_affinity = conf_get_string_scope(svc, conf, 'DEFAULT', 'anti_affinity')
        svc.anti_affinity = set(conf_get_string_scope(svc, conf, 'DEFAULT', 'anti_affinity').split())
    except ex.OptNotFound:
        pass
    
    """ prune not managed service
    """
    if svc.svcmode not in rcEnv.vt_cloud and rcEnv.nodename not in svc.nodes | svc.drpnodes:
        svc.log.error('service %s not managed here' % name)
        del(svc)
        return None

    if not hasattr(svc, "clustertype"):
        try:
            svc.clustertype = conf_get_string_scope(svc, conf, 'DEFAULT', 'cluster_type')
        except ex.OptNotFound:
            svc.clustertype = 'failover'

    if 'flex' in svc.clustertype:
        svc.ha = True
    allowed_clustertype = ['failover', 'flex', 'autoflex']
    if svc.clustertype not in allowed_clustertype:
        svc.log.error("invalid cluster type '%s'. allowed: %s"%(svc.svcname, svc.clustertype, ', '.join(allowed_clustertype)))
        del(svc)
        return None

    try:
        svc.flex_primary = conf_get_string_scope(svc, conf, 'DEFAULT', 'flex_primary')
    except ex.OptNotFound:
        svc.flex_primary = ''

    try:
        svc.flex_min_nodes = conf_get_int_scope(svc, conf, 'DEFAULT', 'flex_min_nodes')
    except ex.OptNotFound:
        svc.flex_min_nodes = 1
    if svc.flex_min_nodes < 0:
        svc.log.error("invalid flex_min_nodes '%d' (<0)."%svc.flex_min_nodes)
        del(svc)
        return None
    nb_nodes = len(svc.autostart_node)
    if nb_nodes == 0:
        nb_nodes = 1
    if nb_nodes > 0 and svc.flex_min_nodes > nb_nodes:
        svc.log.error("invalid flex_min_nodes '%d' (>%d nb of nodes)."%(svc.flex_min_nodes, nb_nodes))
        del(svc)
        return None

    try:
        svc.flex_max_nodes = conf_get_int_scope(svc, conf, 'DEFAULT', 'flex_max_nodes')
    except ex.OptNotFound:
        svc.flex_max_nodes = nb_nodes
    if svc.flex_max_nodes < 0:
        svc.log.error("invalid flex_max_nodes '%d' (<0)."%svc.flex_max_nodes)
        del(svc)
        return None
    if svc.flex_max_nodes < svc.flex_min_nodes:
        svc.log.error("invalid flex_max_nodes '%d' (<flex_min_nodes)."%svc.flex_max_nodes)
        del(svc)
        return None

    try:
        svc.flex_cpu_low_threshold = conf_get_int_scope(svc, conf, 'DEFAULT', 'flex_cpu_low_threshold')
    except ex.OptNotFound:
        svc.flex_cpu_low_threshold = 10
    if svc.flex_cpu_low_threshold < 0:
        svc.log.error("invalid flex_cpu_low_threshold '%d' (<0)."%svc.flex_cpu_low_threshold)
        del(svc)
        return None
    if svc.flex_cpu_low_threshold > 100:
        svc.log.error("invalid flex_cpu_low_threshold '%d' (>100)."%svc.flex_cpu_low_threshold)
        del(svc)
        return None

    try:
        svc.flex_cpu_high_threshold = conf_get_int_scope(svc, conf, 'DEFAULT', 'flex_cpu_high_threshold')
    except ex.OptNotFound:
        svc.flex_cpu_high_threshold = 90
    if svc.flex_cpu_high_threshold < 0:
        svc.log.error("invalid flex_cpu_high_threshold '%d' (<0)."%svc.flex_cpu_high_threshold)
        del(svc)
        return None
    if svc.flex_cpu_high_threshold > 100:
        svc.log.error("invalid flex_cpu_high_threshold '%d' (>100)."%svc.flex_cpu_high_threshold)
        del(svc)
        return None

    if not hasattr(svc, "service_type"):
        if "service_type" in defaults:
            svc.svctype = defaults["service_type"]
        else:
            svc.svctype = ''

    if svc.svctype not in rcEnv.allowed_svctype:
        svc.log.error('service %s type %s is not a known service type (%s)'%(svc.svcname, svc.svctype, ', '.join(rcEnv.allowed_svctype)))
        del(svc)
        return None

    """ prune service whose service type does not match host mode
    """
    if svc.svctype != 'PRD' and rcEnv.host_mode == 'PRD':
        svc.log.error('service %s type %s is not allowed to run on this node (host mode %s)' % (svc.svcname, svc.svctype, rcEnv.host_mode))
        del(svc)
        return None

    if "drp_type" in defaults:
        svc.drp_type = defaults["drp_type"]
    else:
        svc.drp_type = ''

    if "comment" in defaults:
        svc.comment = defaults["comment"]
    else:
        svc.comment = ''

    if "monitor_action" in defaults:
        svc.monitor_action = defaults["monitor_action"]

    if "app" in defaults:
        svc.app = defaults["app"]
    else:
        svc.app = ''

    if "drnoaction" in defaults:
        svc.drnoaction = defaults["drnoaction"]
    else:
        svc.drnoaction = False

    if "bwlimit" in defaults:
        svc.bwlimit = defaults["bwlimit"]
    else:
        svc.bwlimit = None

    if "cluster" in defaults:
        svc.clustername = defaults["cluster"]

    #
    # instanciate resources
    #
    try:
        add_containers(svc, conf)
        add_resources('hb', svc, conf)
        add_resources('stonith', svc, conf)
        add_resources('ip', svc, conf)
        add_resources('drbd', svc, conf)
        add_resources('loop', svc, conf)
        add_resources('vdisk', svc, conf)
        add_resources('vg', svc, conf)
        add_resources('vmdg', svc, conf)
        add_resources('pool', svc, conf)
        add_resources('fs', svc, conf)
        add_resources('share', svc, conf)
        add_resources('app', svc, conf)
        add_apps_sysv(svc, conf)
        add_syncs(svc, conf)
    except (ex.excInitError, ex.excError) as e:
        log.error(str(e))
        return None
    rcLogger.set_streamformatter(svc)
    return svc

def is_service(f):
    if os.name == 'nt':
        return True
    svcmgr = os.path.join(rcEnv.pathsvc, 'bin', 'svcmgr')
    if os.path.realpath(f) != os.path.realpath(svcmgr):
        return False
    if not os.path.exists(f + '.env'):
        return False
    return True

def list_services():
    if not os.path.exists(rcEnv.pathetc):
        print("create dir %s"%rcEnv.pathetc)
        os.makedirs(rcEnv.pathetc)

    s = glob.glob(os.path.join(rcEnv.pathetc, '*.env'))
    s = map(lambda x: os.path.basename(x).replace('.env',''), s)

    l = []
    for name in s:
        if not is_service(os.path.join(rcEnv.pathetc, name)):
            continue
        l.append(name)
    return l

def build_services(status=None, svcnames=[],
                   onlyprimary=False, onlysecondary=False):
    """returns a list of all services of status matching the specified status.
    If no status is specified, returns all services
    """
    services = {}
    if type(svcnames) == str:
        svcnames = [svcnames]
    for name in list_services():
        if len(svcnames) > 0 and name not in svcnames:
            continue
        fix_default_section([name])
        try:
            svc = build(name)
        except (ex.excError, ex.excInitError) as e:
            log.error(str(e))
            continue
        except ex.excAbortAction:
            continue
        except:
            import traceback
            traceback.print_exc()
            continue
        if svc is None :
            continue
        if status is not None and svc.status() != status:
            continue
        if onlyprimary and rcEnv.nodename not in svc.autostart_node:
            continue
        if onlysecondary and rcEnv.nodename in svc.autostart_node:
            continue
        services[svc.svcname] = svc
    return [ s for n ,s in sorted(services.items()) ]

def delete_one(svcname, rids=[]):
    if len(svcname) == 0:
        print("service name must not be empty", file=sys.stderr)
        return 1
    if svcname not in list_services():
        print("service", svcname, "does not exist", file=sys.stderr)
        return 0
    envfile = os.path.join(rcEnv.pathetc, svcname+'.env')
    conf = ConfigParser.RawConfigParser()
    conf.read(envfile)
    for rid in rids:
        if not conf.has_section(rid):
            print("service", svcname, "has not resource", rid, file=sys.stderr)
            continue
        conf.remove_section(rid)
    try:
       f = open(envfile, 'w')
    except:
        print("failed to open", envfile, "for writing", file=sys.stderr)
        return 1
    conf.write(f)
    return 0

def delete(svcnames, rid=[]):
    fix_default_section(svcnames)
    if len(rid) == 0:
        print("no resource flagged for deletion")
        return 0
    r = 0
    for svcname in svcnames:
        r |= delete_one(svcname, rid)
    return r

def create(svcname, resources=[], interactive=False, provision=False):
    if not isinstance(svcname, list):
        print("ouch, svcname should be a list object", file=sys.stderr)
        return {"ret": 1}
    if len(svcname) != 1:
        print("you must specify a single service name with the 'create' action", file=sys.stderr)
        return {"ret": 1}
    svcname = svcname[0]
    if len(svcname) == 0:
        print("service name must not be empty", file=sys.stderr)
        return {"ret": 1}
    if svcname in list_services():
        print("service", svcname, "already exists", file=sys.stderr)
        return {"ret": 1}
    envfile = os.path.join(rcEnv.pathetc, svcname+'.env')
    if os.path.exists(envfile):
        print(envfile, "already exists", file=sys.stderr)
        return {"ret": 1}
    try:
       f = open(envfile, 'w')
    except:
        print("failed to open", envfile, "for writing", file=sys.stderr)
        return {"ret": 1}

    defaults = {}
    sections = {}
    rtypes = {}

    import json
    for r in resources:
        try:
            d = json.loads(r)
        except:
            print("can not parse resource:", r, file=sys.stderr)
            return {"ret": 1}
        if 'rid' in d:
            section = d['rid']
            if '#' not in section:
                print(section, "must be formatted as 'rtype#n'", file=sys.stderr)
                return {"ret": 1}
            l = section.split('#')
            if len(l) != 2:
                print(section, "must be formatted as 'rtype#n'", file=sys.stderr)
                return {"ret": 1}
            rtype = l[1]
            if rtype in rtypes:
                rtypes[rtype] += 1
            else:
                rtypes[rtype] = 0
            del(d['rid'])
            if section in sections:
                sections[section].update(d)
            else:
                sections[section] = d
        elif 'rtype' in d:
            if 'rid' in d:
               del(d['rid'])
            rtype = d['rtype']
            if rtype in rtypes:
                section = '%s#%d'%(rtype, rtypes[rtype])
                rtypes[rtype] += 1
            else:
                section = '%s#0'%rtype
                rtypes[rtype] = 1
            if section in sections:
                sections[section].update(d)
            else:
                sections[section] = d
        else:
            defaults.update(d)

    from svcDict import KeyDict, MissKeyNoDefault, KeyInvalidValue
    try:
        keys = KeyDict(provision=provision)
        defaults.update(keys.update('DEFAULT', defaults))
        for section, d in sections.items():
            sections[section].update(keys.update(section, d))
    except (MissKeyNoDefault, KeyInvalidValue):
        if not interactive:
            return {"ret": 1}

    try:
        if interactive:
            defaults, sections = keys.form(defaults, sections)
    except KeyboardInterrupt:
        sys.stderr.write("Abort\n")
        return {"ret": 1}

    conf = ConfigParser.RawConfigParser(defaults)
    for section, d in sections.items():
        conf.add_section(section)
        for key, val in d.items():
            if key == 'rtype':
                continue
            conf.set(section, key, val)

    conf.write(f)

    initdir = svcname+'.dir'
    svcinitdir = os.path.join(rcEnv.pathetc, initdir)
    if not os.path.exists(svcinitdir):
        os.makedirs(svcinitdir)
    fix_app_link(svcname)
    fix_exe_link(os.path.join('..', 'bin', 'svcmgr'), svcname)
    return {"ret": 0}

def update(svcname, resources=[], interactive=False, provision=False):
    fix_default_section(svcname)
    if not isinstance(svcname, list):
        print("ouch, svcname should be a list object", file=sys.stderr)
        return {"ret": 1}
    if len(svcname) != 1:
        print("you must specify a single service name with the 'update' action", file=sys.stderr)
        return {"ret": 1}
    svcname = svcname[0]
    if len(svcname) == 0:
        print("service name must not be empty", file=sys.stderr)
        return {"ret": 1}
    if svcname not in list_services():
        print("service", svcname, "does not exist", file=sys.stderr)
        return {"ret": 1}
    envfile = os.path.join(rcEnv.pathetc, svcname+'.env')
    sections = {}
    rtypes = {}
    conf = ConfigParser.RawConfigParser()
    conf.read(envfile)
    defaults = conf.defaults()
    for section in conf.sections():
        sections[section] = {}
        l = section.split('#')
        if len(l) == 2:
            rtype = l[0]
            ridx = l[1]
            if rtype not in rtypes:
                rtypes[rtype] = set([])
            rtypes[rtype].add(ridx)
        for o, v in conf.items(section):
            if o in defaults.keys() + ['rtype']:
                continue
            sections[section][o] = v

    from svcDict import KeyDict, MissKeyNoDefault, KeyInvalidValue
    keys = KeyDict(provision=provision)

    import json
    rid = []
    for r in resources:
        try:
            d = json.loads(r)
        except:
            print("can not parse resource:", r, file=sys.stderr)
            return {"ret": 1}
        is_resource = False
        if 'rid' in d:
            section = d['rid']
            if '#' not in section:
                print(section, "must be formatted as 'rtype#n'", file=sys.stderr)
                return {"ret": 1}
            l = section.split('#')
            if len(l) != 2:
                print(section, "must be formatted as 'rtype#n'", file=sys.stderr)
                return {"ret": 1}
            del(d['rid'])
            if section in sections:
                sections[section].update(d)
            else:
                sections[section] = d
            is_resource = True
        elif 'rtype' in d:
            # new resource allocation, auto-allocated rid index
            if d['rtype'] in rtypes:
                ridx = 1
                while str(ridx) in rtypes[d['rtype']]:
                    ridx += 1
                ridx = str(ridx)
                rtypes[d['rtype']].add(ridx)
            else:
                ridx = '1'
                rtypes[d['rtype']] = set([ridx])
            section = '#'.join((d['rtype'], ridx))
            del(d['rtype'])
            sections[section] = d
            is_resource = True
        else:
            defaults.update(d)

        if is_resource:
            try:
                sections[section].update(keys.update(section, d))
            except (MissKeyNoDefault, KeyInvalidValue):
                if not interactive:
                    return {"ret": 1}
            rid.append(section)

    conf = ConfigParser.RawConfigParser(defaults)
    for section, d in sections.items():
        conf.add_section(section)
        for key, val in d.items():
            conf.set(section, key, val)

    try:
        f = open(envfile, 'w')
    except:
        print("failed to open", envfile, "for writing", file=sys.stderr)
        return {"ret": 1}

    conf.write(f)

    fix_app_link(svcname)
    fix_exe_link(os.path.join('..', 'bin', 'svcmgr'), svcname)
    return {"ret": 0, "rid": rid}

def fix_app_link(svcname):
    os.chdir(rcEnv.pathetc)
    src = svcname+'.d'
    dst = svcname+'.dir'
    if os.name != 'posix':
        return
    try:
        p = os.readlink(src)
    except:
        if not os.path.exists(dst):
            os.makedirs(dst)
        os.symlink(dst, src)

def fix_exe_link(dst, src):
    if os.name != 'posix':
        return
    os.chdir(rcEnv.pathetc)
    try:
        p = os.readlink(src)
    except:
        os.symlink(dst, src)
        p = dst
    if p != dst:
        os.unlink(src)
        os.symlink(dst, src)

def _fix_default_section(svcname):
    """ [default] section is not returned by ConfigParser.defaults()
        [DEFAULT] is. Just replace when this occurs.
    """
    envfile = os.path.join(rcEnv.pathetc, svcname+'.env')
    if not os.path.exists(envfile):
        # nothing to fix
        return
    try:
        f = open(envfile, 'r')
    except:
        print("failed to open", envfile, "for reading", file=sys.stderr)
        return 1
    found = False
    lines = []
    for line in f.readlines():
        if line.startswith('[default]'):
            line = '[DEFAULT]\n'
            found = True
        lines.append(line)
    f.close()
    if found:
        try:
            f = open(envfile, 'w')
        except:
            print("failed to open", envfile, "for writing", file=sys.stderr)
            return 1
        f.write(''.join(lines))
        f.close()

def fix_default_section(svcnames):
    for svcname in svcnames:
        _fix_default_section(svcname)
0707010000bc89000081a400000000000000000000000154fc7bfb000192d7000000660000000300000000000000000000002000000000root/opt/opensvc/lib/svcDict.py   #!/opt/opensvc/bin/python

import sys
import os
from rcGlobalEnv import rcEnv
from rcNode import node_get_hostmode
from textwrap import TextWrapper

class MissKeyNoDefault(Exception):
     pass

class KeyInvalidValue(Exception):
     pass

class Keyword(object):
    def __init__(self, section, keyword,
                 rtype=None,
                 order=100,
                 required=False,
                 at=False,
                 default=None,
                 validator=None,
                 candidates=None,
                 depends=[],
                 text="",
                 example="foo",
                 provisioning=False):
        self.section = section
        self.keyword = keyword
        if rtype is None or type(rtype) == list:
            self.rtype = rtype
        else:
            self.rtype = [rtype]
        self.order = order
        self.at = at
        self.required = required
        self.default = default
        self.candidates = candidates
        self.depends = depends
        self.text = text
        self.example = example
        self.provisioning = provisioning

    def __cmp__(self, o):
        if o.order > self.order:
            return -1
        elif o.order == self.order:
            return 0
        return 1

    def template(self):
        wrapper = TextWrapper(subsequent_indent="#%15s"%"", width=78)

        depends = " && ".join(map(lambda d: "%s in %s"%(d[0], d[1]), self.depends))
        if depends == "":
            depends = None

        if type(self.candidates) in (list, tuple, set):
            candidates = " | ".join(map(lambda x: str(x), self.candidates))
        else:
            candidates = str(self.candidates)
        s = '#\n'
        s += "# keyword:       %s\n"%self.keyword
        s += "# ----------------------------------------------------------------------------\n"
        s += "#  required:     %s\n"%str(self.required)
        s += "#  provisioning: %s\n"%str(self.provisioning)
        s += "#  default:      %s\n"%str(self.default)
        s += "#  candidates:   %s\n"%candidates
        s += "#  depends:      %s\n"%depends
        s += "#  scopable:     %s\n"%str(self.at)
        s += '#\n'
        if self.text:
            wrapper = TextWrapper(subsequent_indent="#%9s"%"", width=78)
            s += wrapper.fill("#  desc:  "+self.text) + "\n"
        s += '#\n'
        if self.default is not None:
            val = self.default
        elif self.candidates and len(self.candidates) > 0:
            val = self.candidates[0]
        else:
            val = self.example
        s += ";" + self.keyword + " = " + str(val) + "\n\n"
        return s

    def __str__(self):
        wrapper = TextWrapper(subsequent_indent="%15s"%"", width=78)

        depends = ""
        for d in self.depends:
            depends += "%s in %s\n"%(d[0], d[1])
        if depends == "":
            depends = None

        s = ''
        s += "------------------------------------------------------------------------------\n"
        s += "section:       %s\n"%self.section
        s += "keyword:       %s\n"%self.keyword
        s += "------------------------------------------------------------------------------\n"
        s += "  required:    %s\n"%str(self.required)
        s += "  default:     %s\n"%str(self.default)
        s += "  candidates:  %s\n"%str(self.candidates)
        s += "  depends:     %s\n"%depends
        s += "  scopable:    %s\n"%str(self.at)
        if self.text:
            s += wrapper.fill("  help:        "+self.text)
        if self.at:
            s += "\n\nPrefix the value with '@<node> ', '@nodes ', '@drpnodes ' or '@encapnodes '\n"
            s += "to specify a scope-specific value.\n"
            s += "You will be prompted for new values until you submit an empty value.\n"
        s += "\n"
        return s

    def form(self, d):
        # skip this form if dependencies are not met
        for d_keyword, d_value in self.depends:
            if d_keyword not in d:
                return d
            if d[d_keyword] not in d_value:
                return d

        # print() the form
        print(self)

        # if we got a json seed, use its values as default
        # else use the Keyword object default
        if self.keyword in d:
            default = d[self.keyword]
        elif self.default is not None:
            default = self.default
        else:
            default = None

        if default is not None:
            default_prompt = " [%s] "%str(default)
        else:
            default_prompt = ""

        req_satisfied = False
        while True:
            try:
                val = raw_input(self.keyword+default_prompt+"> ")
            except EOFError:
                break
            if len(val) == 0:
                if req_satisfied:
                    return d
                if default is None and self.required:
                    print("value required")
                    continue
                # keyword is optional, leave dictionary untouched
                return d
            elif self.at and val[0] == '@':
                l = val.split()
                if len(l) < 2:
                    print("invalid value")
                    continue
                val = ' '.join(l[1:])
                d[self.keyword+l[0]] = val
                req_satisfied = True
            else:
                d[self.keyword] = val
                req_satisfied = True
            if self.at:
                # loop for more key@<scope> = values
                print("More '%s' ? <enter> to step to the next parameter."%self.keyword)
                continue
            else:
                return d

class KeywordInteger(Keyword):
    def validator(self, val, d=None):
        try:
             val = int(val)
        except:
             return False
        return True


class KeywordProvision(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="provision",
                  keyword="provision",
                  default="no",
                  candidates=('yes', 'no'),
                  text="Say yes to provision this resource. Warning, provisioning implies destructive operations like formating."
                )

class KeywordMode(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="mode",
                  required=False,
                  order=10,
                  default="hosted",
                  candidates=["hosted", "sg", "vcs", "rhcs"],
                  text="The mode decides upon disposition OpenSVC takes to bring a service up or down : virtualized services need special actions to prepare and boot the container for example, which is not needed for 'hosted' services."
                )

class KeywordPkgName(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="pkg_name",
                  required=False,
                  order=11,
                  depends=[('mode', ["vcs", "sg", "rhcs"])],
                  text="The wrapped cluster package name, as known to the cluster manager in charge."
                )

class KeywordDockerDataDir(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="docker_data_dir",
                  at=True,
                  required=False,
                  order=12,
                  text="if the service has docker-type container resources, the service handles the startup of a private docker daemon. Its socket is /opt/opensvc/var/<svcname>/docker.sock, and its data directory must be specified using this parameter. This organization is necessary to enable service relocalization.",
                  example="/srv/svc1/data/docker"
                )

class KeywordDockerDaemonArgs(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="docker_daemon_args",
                  at=True,
                  required=False,
                  order=12,
                  text="If the service has docker-type container resources, the service handles the startup of a private docker daemon. OpenSVC sets the socket and data dir parameters. Admins can set extra parameters using this keyword. For example, it can be useful to set the --ip parameter for a docker registry service.",
                  example="--ip 1.2.3.4"
                )

class KeywordSubsetParallel(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="subset",
                  keyword="parallel",
                  candidates=(True, False),
                  default=False,
                  text="If set to true, actions are executed in parallel amongst the subset member resources.",
                  required=False,
                  order=2
                )

class KeywordStonithType(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="stonith",
                  keyword="type",
                  candidates=["ilo", "callout"],
                  text="The type of stonith.",
                  required=True,
                  order=1
                )

class KeywordStonithTarget(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="stonith",
                  keyword="target",
                  text="The server management console to pass the stonith command to, as defined in the corresponding auth.conf section title.",
                  required=True,
                  order=2
                )

class KeywordStonithCalloutCmd(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="stonith",
                  rtype="callout",
                  keyword="cmd",
                  text="The command to execute on target to stonith.",
                  required=True,
                  order=3
                )

class KeywordContainerType(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="type",
                  candidates=rcEnv.vt_supported,
                  text="The type of container.",
                  required=True,
                  order=1
                )

class KeywordDockerRunImage(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="run_image",
                  order=9,
                  required=False,
                  rtype="docker",
                  text="The docker image pull, and run the container with.",
                  example="83f2a3dd2980"
                )

class KeywordDockerRunCommand(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="run_command",
                  order=1,
                  required=False,
                  rtype="docker",
                  text="The command to execute in the docker container on run.",
                  example="/opt/tomcat/bin/catalina.sh"
                )

class KeywordDockerRunArgs(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="run_args",
                  order=2,
                  required=False,
                  rtype="docker",
                  text="Extra arguments to pass to the docker run command, like volume and port mappings.",
                  example="-v /opt/docker.opensvc.com/vol1:/vol1:rw -p 37.59.71.25:8080:8080"
                )

class KeywordVirtinst(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="virtinst",
                  rtype=["kvm", "xen", "ovm"],
                  text="The virt-install command to use to create the container.",
                  required=True,
                  provisioning=True
                )

class KeywordSnap(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="snap",
                  rtype=["kvm", "xen", "ovm", "zone", "esx"],
                  text="The target snapshot/clone full path containing the new container disk files.",
                  required=True,
                  provisioning=True
                )

class KeywordSnapof(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="snapof",
                  rtype=["kvm", "xen", "ovm", "zone", "esx"],
                  text="The snapshot origin full path containing the reference container disk files.",
                  required=True,
                  provisioning=True
                )

class KeywordContainerOrigin(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="container_origin",
                  rtype="zone",
                  text="The origin container having the reference container disk files.",
                  required=True,
                  provisioning=True
                )

class KeywordJailRoot(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="jailroot",
                  rtype="jail",
                  text="Sets the root fs directory of the container",
                  required=True,
                  provisioning=False
                )

class KeywordLxcCf(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="cf",
                  rtype="lxc",
                  text="Defines a lxc configuration file in a non-standard location.",
                  required=False,
                  provisioning=True,
                  example="/srv/mycontainer/config"
                )

class KeywordRootfs(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="rootfs",
                  rtype=["lxc", "vz", "zone"],
                  text="Sets the root fs directory of the container",
                  required=True,
                  provisioning=True
                )

class KeywordTemplate(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="template",
                  rtype=["lxc", "vz", "zone"],
                  text="Sets the url of the template unpacked into the container root fs.",
                  required=True,
                  provisioning=True
                )

class KeywordVmName(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="name",
                  order=2,
                  rtype=rcEnv.vt_supported,
                  text="This need to be set if the virtual machine name is different from the service name."
                )

class KeywordGuestos(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="guestos",
                  rtype=rcEnv.vt_supported,
                  order=11,
                  candidates=["unix", "windows"],
                  default=None,
                  text="The operating system in the virtual machine."
                )

class KeywordJailIps(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="ips",
                  rtype="jail",
                  order=11,
                  text="The ipv4 addresses of the jail."
                )

class KeywordJailIp6s(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="ip6s",
                  rtype="jail",
                  order=11,
                  text="The ipv6 addresses of the jail."
                )

class KeywordSharedIpGroup(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="shared_ip_group",
                  order=11,
                  rtype=rcEnv.vt_cloud,
                  text="The cloud shared ip group name to allocate a public ip from."
                )

class KeywordSize(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="size",
                  order=11,
                  rtype=rcEnv.vt_cloud,
                  text="The cloud vm size, as known to the cloud manager. Example: tiny."
                )

class KeywordKeyName(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="key_name",
                  order=11,
                  rtype=rcEnv.vt_cloud,
                  text="The key name, as known to the cloud manager, to trust in the provisioned vm."
                )

class KeywordSrpPrmCores(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="prm_cores",
                  order=11,
                  rtype="srp",
                  default=1,
                  provisioning=True,
                  text="The number of core to bind the SRP container to."
                )

class KeywordSrpIp(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="ip",
                  order=11,
                  rtype="srp",
                  provisioning=True,
                  text="The ip name or addr used to create the SRP container."
                )

class KeywordSrpRootpath(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="rootpath",
                  order=11,
                  rtype="srp",
                  provisioning=True,
                  text="The path of the SRP container root filesystem."
                )

class KeywordCloudId(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="cloud_id",
                  order=11,
                  rtype=rcEnv.vt_cloud,
                  text="The cloud id as configured in node.conf. Example: cloud#1."
                )

class KeywordVmUuid(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="container",
                  keyword="uuid",
                  order=11,
                  rtype="ovm",
                  text="The virtual machine unique identifier used to pass commands on the VM."
                )

class KeywordAntiAffinity(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="anti_affinity",
                  order=15,
                  required=False,
                  default=None,
                  text="A whitespace separated list of services this service is not allowed to be started on the same node. The svcmgr --ignore-affinity option can be set to override this policy.",
                  example="svc1 svc2"
                )

class KeywordNoPreemptAbort(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="no_preempt_abort",
                  order=15,
                  at=True,
                  required=False,
                  candidates=[True, False],
                  default=False,
                  text="If set to 'true', OpenSVC will preempt scsi reservation with a preempt command instead of a preempt and and abort. Some scsi target implementations do not support this last mode (esx). If set to 'false' or not set, 'no_preempt_abort' can be activated on a per-resource basis."
                )

class KeywordCluster(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="cluster",
                  order=15,
                  required=False,
                  default=None,
                  text="The symbolic name of the cluster. Used to label shared disks represented to tiers-2 consumers like containers.",
                  example="cluster1"
                )

class KeywordClusterType(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="cluster_type",
                  order=15,
                  required=False,
                  default="failover",
                  candidates=["failover", "flex", "autoflex"],
                  text="failover: the service is allowed to be up on one node at a time. allactive: the service must be up on all nodes. flex: the service can be up on n out of m nodes (n <= m), n/m must be in the [flex_min_nodes, flex_max_nodes] range. autoflex: same as flex, but charge the collector to start the service on passive nodes when the average %cpu usage on active nodes > flex_cpu_high_threshold and stop the service on active nodes when the average %cpu usage on active nodes < flex_cpu_low_threshold."
                )

class KeywordFlexMinNodes(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="flex_min_nodes",
                  order=16,
                  required=False,
                  default=1,
                  depends=[('cluster_type', ['flex', 'autoflex'])],
                  text="Minimum number of active nodes in the cluster. Below this number alerts are raised by the collector, and the collector won't stop any more service instances."
                )

class KeywordFlexMaxNodes(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="flex_max_nodes",
                  order=16,
                  required=False,
                  default=10,
                  depends=[('cluster_type', ['flex', 'autoflex'])],
                  text="Maximum number of active nodes in the cluster. Above this number alerts are raised by the collector, and the collector won't start any more service instances. 0 means unlimited."
                )

class KeywordFlexCpuMinThreshold(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="flex_cpu_min_threshold",
                  order=16,
                  required=False,
                  default=10,
                  depends=[('cluster_type', ['flex', 'autoflex'])],
                  text="Average CPU usage across the active cluster nodes below which the collector raises alerts and decides to stop service instances with autoflex cluster type."
                )

class KeywordFlexCpuMaxThreshold(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="flex_cpu_max_threshold",
                  order=16,
                  required=False,
                  default=70,
                  depends=[('cluster_type', ['flex', 'autoflex'])],
                  text="Average CPU usage across the active cluster nodes above which the collector raises alerts and decides to start new service instances with autoflex cluster type."
                )

class KeywordServiceType(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="service_type",
                  order=15,
                  required=True,
                  default=node_get_hostmode(),
                  candidates=rcEnv.allowed_svctype,
                  text="A non-PRD service can not be brought up on a PRD node, but a PRD service can be startup on a non-PRD node (in a DRP situation)."
                )

class KeywordNodes(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="nodes",
                  order=20,
                  required=True,
                  default=rcEnv.nodename,
                  text="List of cluster local nodes able to start the service.  Whitespace separated."
                )

class KeywordAutostartNode(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="autostart_node",
                  order=20,
                  required=False,
                  default=rcEnv.nodename,
                  text="A whitespace-separated list subset of 'nodes'. Defines the nodes where the service will try to start on upon node reboot. On a failover cluster there should only be one autostart node and the start-up will fail if the service is already up on another node though. If not specified, the service will never be started at node boot-time, which is rarely the expected behaviour."
                )

class KeywordDrpnode(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="drpnode",
                  order=21,
                  text="The backup node where the service is activated in a DRP situation. This node is also a data synchronization target for 'sync' resources.",
                  example="node1"
                )

class KeywordDrpnodes(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="drpnodes",
                  order=21,
                  text="Alternate backup nodes, where the service could be activated in a DRP situation if the 'drpnode' is not available. These nodes are also data synchronization targets for 'sync' resources.",
                  example="node1 node2"
                )

class KeywordApp(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="app",
                  order=24,
                  default="DEFAULT",
                  text="Used to identify who is responsible for is service, who is billable and provides a most useful filtering key. Better keep it a short code."
                )

class KeywordComment(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="comment",
                  order=25,
                  text="Helps users understand the role of the service, which is nice to on-call support people having to operate on a service they are not usualy responsible for."
                )

class KeywordScsireserv(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="scsireserv",
                  at=True,
                  order=25,
                  default=False,
                  candidates=(True, False),
                  text="If set to 'true', OpenSVC will try to acquire a type-5 (write exclusive, registrant only) scsi3 persistent reservation on every path to disks of every disk group attached to this service. Existing reservations are preempted to not block service start-up. If the start-up was not legitimate the data are still protected from being written over from both nodes. If set to 'false' or not set, 'scsireserv' can be activated on a per-resource basis."
                )

class KeywordBwlimit(KeywordInteger):
    def __init__(self):
        KeywordInteger.__init__(
                  self,
                  section="DEFAULT",
                  keyword="bwlimit",
                  order=25,
                  text="Bandwidth limit in KB applied to all rsync transfers. Leave empty to enforce no limit.",
                  example="3000"
                )

class KeywordSyncInterval(KeywordInteger):
    def __init__(self):
        KeywordInteger.__init__(
                  self,
                  section="DEFAULT",
                  keyword="sync_interval",
                  order=26,
                  default=121,
                  text="Set the minimum delay between syncs in minutes. If a sync is triggered through crond or manually, it is skipped if last sync occured less than 'sync_min_delay' ago. The mecanism is enforced by a timestamp created upon each sync completion in /opt/opensvc/var/sync/[service]![dst]"
                )

class KeywordSyncMaxDelay(KeywordInteger):
    def __init__(self):
        KeywordInteger.__init__(
                  self,
                  section="DEFAULT",
                  keyword="sync_max_delay",
                  order=27,
                  default=1440,
                  text="Unit is minutes. This sets to delay above which the sync status of the resource is to be considered down. Should be set according to your application service level agreement. The cron job frequency should be set between 'sync_min_delay' and 'sync_max_delay'"
                )

class KeywordPresnapTrigger(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="presnap_trigger",
                  order=28,
                  text="Define a command to run before creating snapshots. This is most likely what you need to use plug a script to put you data in a coherent state (alter begin backup and the like).",
                  example="/srv/svc1/etc/init.d/pre_snap.sh"
                )

class KeywordPostsnapTrigger(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="postsnap_trigger",
                  order=29,
                  text="Define a command to run after snapshots are created. This is most likely what you need to use plug a script to undo the actions of 'presnap_trigger'.",
                  example="/srv/svc1/etc/init.d/post_snap.sh"
                )

class KeywordMonitorAction(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="monitor_action",
                  at=True,
                  order=30,
                  default=None,
                  candidates=("reboot", "crash", "freezestop"),
                  text="Use process containers when possible. Containers allow capping memory, swap and cpu usage per service. Lxc containers are naturally containerized, so skip containerization of their startapp.",
                  example="reboot"
                )

class KeywordContainerize(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="DEFAULT",
                  keyword="containerize",
                  order=30,
                  default=True,
                  candidates=(True, False),
                  text="Use process containers when possible. Containers allow capping memory, swap and cpu usage per service. Lxc containers are naturally containerized, so skip containerization of their startapp."
                )

class KeywordContainerCpus(KeywordInteger):
    def __init__(self):
        KeywordInteger.__init__(
                  self,
                  section="DEFAULT",
                  keyword="container_cpus",
                  order=31,
                  depends=[('containerize', [True])],
                  text="Allow service process to bind only the specified cpus. Cpus are specified as list or range : 0,1,2 or 0-2",
                  example="0-2"
                )

class KeywordContainerMems(KeywordInteger):
    def __init__(self):
        KeywordInteger.__init__(
                  self,
                  section="DEFAULT",
                  keyword="container_mems",
                  order=31,
                  depends=[('containerize', [True])],
                  text="Allow service process to bind only the specified memory nodes. Memory nodes are specified as list or range : 0,1,2 or 0-2",
                  example="0-2"
                )

class KeywordContainerCpuShare(KeywordInteger):
    def __init__(self):
        KeywordInteger.__init__(
                  self,
                  section="DEFAULT",
                  keyword="container_cpu_share",
                  order=31,
                  depends=[('containerize', [True])],
                  text="Kernel default value is used, which usually is 1024 shares. In a cpu-bound situation, ensure the service does not use more than its share of cpu ressource. The actual percentile depends on shares allowed to other services.",
                  example="512"
                )

class KeywordContainerMemLimit(KeywordInteger):
    def __init__(self):
        KeywordInteger.__init__(
                  self,
                  section="DEFAULT",
                  keyword="container_mem_limit",
                  order=31,
                  depends=[('containerize', [True])],
                  text="Ensures the service does not use more than specified memory (in bytes). The Out-Of-Memory killer get triggered in case of tresspassing.",
                  example="512000000"
                )

class KeywordContainerVmemLimit(KeywordInteger):
    def __init__(self):
        KeywordInteger.__init__(
                  self,
                  section="DEFAULT",
                  keyword="container_vmem_limit",
                  order=31,
                  depends=[('containerize', [True])],
                  text="Ensures the service does not use more than specified memory+swap (in bytes). The Out-Of-Memory killer get triggered in case of tresspassing. The specified value must be greater than container_mem_limit.",
                  example="1024000000"
                )

class KeywordAppScript(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="app",
                  keyword="script",
                  at=True,
                  order=9,
                  required=True,
                  text="Full path to the app launcher script. Or its basename if the file is hosted in the <svcname>.d path."
                )

class KeywordAppTimeout(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="app",
                  keyword="timeout",
                  order=9,
                  at=True,
                  required=True,
                  text="Wait for <n> seconds max before declaring the app launcher action a failure. If no timeout is specified, the agent waits indefinitely for the app launcher to return. The timeout parameter can be coupled with optional=True to not abort a service start when an app launcher did not return.",
                  example="180"
                )

class KeywordAppStart(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="app",
                  keyword="start",
                  at=True,
                  order=10,
                  required=False,
                  text="Start up sequencing number."
                )

class KeywordAppStop(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="app",
                  keyword="stop",
                  at=True,
                  order=11,
                  required=False,
                  text="Stop sequencing number."
                )

class KeywordAppCheck(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="app",
                  keyword="check",
                  at=True,
                  order=11,
                  required=False,
                  text="Check up sequencing number."
                )

class KeywordAppInfo(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="app",
                  keyword="info",
                  at=True,
                  order=12,
                  required=False,
                  text="Info up sequencing number."
                )

class KeywordSyncType(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="type",
                  order=10,
                  required=True,
                  candidates=("rsync", "docker", "dds", "netapp", "symsrdfs", "zfs", "btrfs", "symclone", "hp3par", "evasnap", "ibmdssnap", "dcssnap", "dcsckpt", "necismsnap"),
                  default="rsync",
                  text="Point a sync driver to use."
                )

class KeywordSyncDockerTarget(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="target",
                  rtype="docker",
                  order=11,
                  at=True,
                  required=True,
                  default=None,
                  candidates=["nodes", "drpnodes", "nodes drpnodes"],
                  text="Destination nodes of the sync."
                )

class KeywordSyncBtrfsSrc(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="src",
                  rtype="btrfs",
                  order=10,
                  at=True,
                  required=True,
                  text="Source subvolume of the sync."
                )

class KeywordSyncBtrfsDst(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="dst",
                  rtype="btrfs",
                  order=10,
                  at=True,
                  required=True,
                  text="Destination subvolume of the sync."
                )

class KeywordSyncBtrfsTarget(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="target",
                  rtype="btrfs",
                  order=11,
                  at=True,
                  required=True,
                  default=None,
                  candidates=["nodes", "drpnodes", "nodes drpnodes"],
                  text="Destination nodes of the sync."
                )

class KeywordSyncBtrfsRecursive(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="recursive",
                  rtype="btrfs",
                  order=10,
                  at=True,
                  required=False,
                  default=False,
                  candidates=[True, False],
                  text="Also replicate subvolumes in the src tree."
                )

class KeywordSyncZfsSrc(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="src",
                  rtype="zfs",
                  order=10,
                  at=True,
                  required=True,
                  text="Source dataset of the sync."
                )

class KeywordSyncZfsDst(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="dst",
                  rtype="zfs",
                  order=11,
                  at=True,
                  required=True,
                  text="Destination dataset of the sync."
                )

class KeywordSyncZfsTarget(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="target",
                  rtype="zfs",
                  order=12,
                  required=True,
                  candidates=['nodes', 'drpnodes', 'nodes drpnodes'],
                  text="Describes which nodes should receive this data sync from the PRD node where the service is up and running. SAN storage shared 'nodes' must not be sync to 'nodes'. SRDF-like paired storage must not be sync to 'drpnodes'."
                )

class KeywordSyncZfsRecursive(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="recursive",
                  rtype="zfs",
                  order=13,
                  default=True,
                  candidates=(True, False),
                  text="Describes which nodes should receive this data sync from the PRD node where the service is up and running. SAN storage shared 'nodes' must not be sync to 'nodes'. SRDF-like paired storage must not be sync to 'drpnodes'."
                )

class KeywordSyncZfsTags(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="tags",
                  rtype="zfs",
                  text="The zfs sync resource supports the 'delay_snap' tag. This tag is used to delay the snapshot creation just before the sync, thus after 'postsnap_trigger' execution. The default behaviour (no tags) is to group all snapshots creation before copying data to remote nodes, thus between 'presnap_trigger' and 'postsnap_trigger'."
                )

class KeywordSyncRsyncSrc(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="src",
                  rtype="rsync",
                  order=10,
                  at=True,
                  required=True,
                  text="Source of the sync. Can be a whitespace-separated list of files or dirs passed as-is to rsync. Beware of the meaningful ending '/'. Refer to the rsync man page for details."
                )

class KeywordSyncRsyncDst(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="dst",
                  rtype="rsync",
                  order=11,
                  required=True,
                  text="Destination of the sync. Beware of the meaningful ending '/'. Refer to the rsync man page for details."
                )

class KeywordSyncRsyncTags(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="tags",
                  rtype="rsync",
                  text="The sync resource supports the 'delay_snap' tag. This tag is used to delay the snapshot creation just before the rsync, thus after 'postsnap_trigger' execution. The default behaviour (no tags) is to group all snapshots creation before copying data to remote nodes, thus between 'presnap_trigger' and 'postsnap_trigger'."
                )

class KeywordSyncRsyncExclude(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="exclude",
                  rtype="rsync",
                  text="!deprecated!. A whitespace-separated list of --exclude params passed unchanged to rsync. The 'options' keyword is preferred now."
                )

class KeywordSyncRsyncOptions(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="options",
                  rtype="rsync",
                  text="A whitespace-separated list of params passed unchanged to rsync. Typical usage is ACL preservation activation."
                )

class KeywordSyncRsyncTarget(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="target",
                  rtype="rsync",
                  order=12,
                  required=True,
                  candidates=['nodes', 'drpnodes', 'nodes drpnodes'],
                  text="Describes which nodes should receive this data sync from the PRD node where the service is up and running. SAN storage shared 'nodes' must not be sync to 'nodes'. SRDF-like paired storage must not be sync to 'drpnodes'."
                )

class KeywordSyncRsyncSnap(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="snap",
                  rtype="rsync",
                  order=14,
                  candidates=(True, False),
                  default=False,
                  text="If set to true, OpenSVC will try to snapshot the first snapshottable parent of the source of the sync and try to sync from the snap."
                )

class KeywordSyncRsyncDstfs(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="dstfs",
                  rtype="rsync",
                  order=13,
                  text="If set to a remote mount point, OpenSVC will verify that the specified mount point is really hosting a mounted FS. This can be used as a safety net to not overflow the parent FS (may be root)."
                )

class KeywordSyncRsyncBwlimit(KeywordInteger):
    def __init__(self):
        KeywordInteger.__init__(
                  self,
                  section="sync",
                  keyword="bwlimit",
                  rtype="rsync",
                  text="Bandwidth limit in KB applied to this rsync transfer. Leave empty to enforce no limit. Takes precedence over 'bwlimit' set in [DEFAULT]."
                )

class KeywordSyncSchedule(KeywordInteger):
    def __init__(self):
        KeywordInteger.__init__(
                  self,
                  section="sync",
                  keyword="schedule",
                  default=None,
                  at=True,
                  text="Set the this resource synchronization schedule. See usr/share/doc/node.conf for the schedule syntax reference.",
                  example='["00:00-01:00@61 mon", "02:00-03:00@61 tue-sun"]'
                )

class KeywordSyncSyncMaxDelay(KeywordInteger):
    def __init__(self):
        KeywordInteger.__init__(
                  self,
                  section="sync",
                  keyword="sync_max_delay",
                  default=1440,
                  text="Unit is minutes. This sets to delay above which the sync status of the resource is to be considered down. Should be set according to your application service level agreement. The cron job frequency should be set between 'sync_min_delay' and 'sync_max_delay'."
                )

class KeywordIpIpname(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="ip",
                  keyword="ipname",
                  order=12,
                  at=True,
                  required=True,
                  text="The DNS name of the ip resource. Can be different from one node to the other, in which case '@nodename' can be specified. This is most useful to specify a different ip when the service starts in DRP mode, where subnets are likely to be different than those of the production datacenter."
                )

class KeywordIpZone(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="ip",
                  keyword="zone",
                  order=12,
                  at=True,
                  required=False,
                  text="The zone name the ip resource is linked to. If set, the ip is plumbed from the global in the zone context.",
                  example="zone1"
                )

class KeywordIpType(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="ip",
                  keyword="type",
                  candidates=[None, 'crossbow'],
                  text="The opensvc ip driver name.",
                  required=False,
                  order=10,
                  example="crossbow",
                )

class KeywordIpIpdev(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="ip",
                  keyword="ipdev",
                  order=11,
                  at=True,
                  required=True,
                  text="The interface name over which OpenSVC will try to stack the service ip. Can be different from one node to the other, in which case the '@nodename' can be specified."
                )

class KeywordIpIpdevext(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="ip",
                  rtype="crossbow",
                  keyword="ipdevext",
                  order=12,
                  at=True,
                  required=False,
                  example="v4",
                  text="The interface name extension for crossbow ipadm configuration."
                )

class KeywordIpNetmask(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="ip",
                  keyword="netmask",
                  at=True,
                  order=13,
                  text="If an ip is already plumbed on the root interface (in which case the netmask is deduced from this ip). Mandatory if the interface is dedicated to the service (dummy interface are likely to be in this case). The format is decimal for IPv4, ex: 255.255.252.0, and octal for IPv6, ex: 64.",
                  example="255.255.255.0"
                )

class KeywordIpGateway(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="ip",
                  keyword="gateway",
                  order=14,
                  required=False,
                  text="A zone ip provisioning parameter used in the sysidcfg formatting. The format is decimal for IPv4, ex: 255.255.252.0, and octal for IPv6, ex: 64.",
                  provisioning=True
                )

class KeywordVgType(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vg",
                  keyword="type",
                  order=9,
                  required=False,
                  candidates=['veritas', 'raw', 'rados'],
                  text="The volume group driver to use. Leave empty to activate the native volume group manager."
                )

class KeywordVgRawDevs(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vg",
                  rtype="raw",
                  keyword="devs",
                  order=10,
                  at=True,
                  required=True,
                  text="a list of device paths, whitespace separated. Those devices are listed as owned by the service and scsi reservation policy is applied to them.",
                  example="/dev/mapper/svc.d0 /dev/mapper/svc.d1"
                )

class KeywordVgVgname(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vg",
                  keyword="vgname",
                  order=10,
                  required=True,
                  text="The name of the volume group"
                )

class KeywordVgOptions(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vg",
                  keyword="options",
                  default="",
                  required=False,
                  provisioning=True,
                  text="The vgcreate options to use upon vg provisioning."
                )

class KeywordVgClientId(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vg",
                  keyword="client_id",
                  rtype="rados",
                  text="Client id to use for authentication with the rados servers"
                )

class KeywordVgKeyring(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vg",
                  keyword="keyring",
                  required=False,
                  rtype="rados",
                  text="keyring to look for the client id secret for authentication with the rados servers"
                )

class KeywordVgLock(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vg",
                  keyword="lock",
                  candidates=["exclusive", "shared", "None"],
                  rtype="rados",
                  text="Locking mode for the rados images"
                )

class KeywordVgLockSharedTag(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vg",
                  keyword="lock_shared_tag",
                  rtype="rados",
                  depends=[('lock', ['shared'])],
                  text="The tag to use upon rados image locking in shared mode"
                )

class KeywordVgImageFormat(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vg",
                  keyword="image_format",
                  provisioning=True,
                  rtype="rados",
                  default="2",
                  text="The rados image format"
                )

class KeywordVgSize(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vg",
                  keyword="size",
                  provisioning=True,
                  rtype="rados",
                  text="The rados image size in MB"
                )

class KeywordVgImages(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vg",
                  keyword="images",
                  rtype="rados",
                  text="The rados image names handled by this vg resource. whitespace separated."
                )

class KeywordVgDsf(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vg",
                  keyword="dsf",
                  candidates=(True, False),
                  default=True,
                  text="HP-UX only. 'dsf' must be set to false for LVM to use never-multipathed /dev/dsk/... devices. Otherwize, ad-hoc multipathed /dev/disk/... devices."
                )

class KeywordVgScsireserv(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vg",
                  keyword="scsireserv",
                  default=False,
                  candidates=(True, False),
                  text="If set to 'true', OpenSVC will try to acquire a type-5 (write exclusive, registrant only) scsi3 persistent reservation on every path to disks of every disk group attached to this service. Existing reservations are preempted to not block service start-up. If the start-up was not legitimate the data are still protected from being written over from both nodes. If set to 'false' or not set, 'scsireserv' can be activated on a per-resource basis."
                )

class KeywordVgPvs(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vg",
                  keyword="pvs",
                  required=True,
                  text="The list of paths to the physical volumes of the volume group.",
                  provisioning=True
                )

class KeywordPoolPoolname(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="pool",
                  keyword="poolname",
                  order=10,
                  at=True,
                  text="The name of the zfs pool"
                )

class KeywordVmdgContainerid(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vmdg",
                  keyword="container_id",
                  at=True,
                  required=False,
                  text="The id of the container whose configuration to extract the disk mapping from."
                )

class KeywordVmdgContainerType(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vmdg",
                  keyword="type",
                  at=True,
                  required=False,
                  candidates=["ldom"],
                  default=None,
                  text="The type of the container whose configuration to extract the disk mapping from."
                )

class KeywordVmdgScsireserv(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vmdg",
                  keyword="scsireserv",
                  default=False,
                  candidates=(True, False),
                  text="If set to 'true', OpenSVC will try to acquire a type-5 (write exclusive, registrant only) scsi3 persistent reservation on every path to disks of every disk group attached to this service. Existing reservations are preempted to not block service start-up. If the start-up was not legitimate the data are still protected from being written over from both nodes. If set to 'false' or not set, 'scsireserv' can be activated on a per-resource basis."
                )

class KeywordDrbdScsireserv(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="drbd",
                  keyword="scsireserv",
                  default=False,
                  candidates=(True, False),
                  text="If set to 'true', OpenSVC will try to acquire a type-5 (write exclusive, registrant only) scsi3 persistent reservation on every path to disks of every disk group attached to this service. Existing reservations are preempted to not block service start-up. If the start-up was not legitimate the data are still protected from being written over from both nodes. If set to 'false' or not set, 'scsireserv' can be activated on a per-resource basis."
                )

class KeywordDrbdRes(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="drbd",
                  keyword="res",
                  order=11,
                  text="The name of the drbd resource associated with this service resource. OpenSVC expect the resource configuration file to reside in '/etc/drbd.d/resname.res'. The 'sync#i0' resource will take care of replicating this file to remote nodes."
                )

class KeywordShareType(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="share",
                  keyword="type",
                  candidates=["nfs"],
                  text="The type of share.",
                  required=True,
                  order=1
                )

class KeywordSharePath(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="share",
                  keyword="path",
                  rtype="nfs",
                  order=10,
                  at=True,
                  required=True,
                  text="The fullpath of the directory to share."
                )

class KeywordShareNfsOpts(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="share",
                  keyword="opts",
                  rtype="nfs",
                  order=11,
                  at=True,
                  required=True,
                  text="The NFS share export options, as they woud be set in /etc/exports or passed to Solaris share command."
                )

class KeywordFsDev(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="fs",
                  keyword="dev",
                  order=11,
                  at=True,
                  required=True,
                  text="The block device file or filesystem image file hosting the filesystem to mount. Different device can be set up on different nodes using the dev@nodename syntax"
                )

class KeywordFsZone(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="fs",
                  keyword="zone",
                  order=11,
                  at=True,
                  required=False,
                  text="The zone name the fs refers to. If set, the fs mount point is reparented into the zonepath rootfs."
                )

class KeywordFsVg(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="fs",
                  keyword="vg",
                  required=True,
                  text="The name of the disk group the filesystem device should be provisioned from.",
                  provisioning=True
                )

class KeywordFsSize(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="fs",
                  keyword="size",
                  required=True,
                  text="The size in MB of the logical volume to provision for this filesystem.",
                  provisioning=True
                )

class KeywordFsMnt(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="fs",
                  keyword="mnt",
                  order=12,
                  required=True,
                  text="The mount point where to mount the filesystem."
                )

class KeywordFsMntOpt(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="fs",
                  keyword="mnt_opt",
                  order=13,
                  text="The mount options."
                )

class KeywordFsType(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="fs",
                  keyword="type",
                  order=14,
                  required=True,
                  text="The filesystem type. Used to determine the fsck command to use."
                )

class KeywordFsSnapSize(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="fs",
                  keyword="snap_size",
                  order=14,
                  text="If this filesystem is build on a snapable logical volume or is natively snapable (jfs, vxfs, ...) this setting overrides the default 10% of the filesystem size to compute the snapshot size. The snapshot is created by snap-enabled rsync-type sync resources. The unit is Megabytes."
                )

class KeywordLoopSize(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="loop",
                  keyword="size",
                  required=True,
                  default=10,
                  text="The size of the loop file to provision.",
                  provisioning=True
                )

class KeywordLoopFile(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="loop",
                  keyword="file",
                  required=True,
                  text="The file hosting the disk image to map."
                )

class KeywordSyncNetappFiler(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="filer",
                  rtype="netapp",
                  required=True,
                  at=True,
                  text="The Netapp filer resolvable host name used by the node.  Different filers can be set up for each node using the filer@nodename syntax."
                )

class KeywordSyncNetappPath(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="path",
                  rtype="netapp",
                  required=True,
                  text="Specifies the volume or qtree to drive snapmirror on."
                )

class KeywordSyncNetappUser(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="user",
                  rtype="netapp",
                  required=True,
                  default="nasadm",
                  text="Specifies the user used to ssh connect the filers. Nodes should be trusted by keys to access the filer with this user."
                )

class KeywordSyncIbmdssnapPairs(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="pairs",
                  at=True,
                  rtype="ibmdssnap",
                  required=True,
                  text="Whitespace-separated list of device pairs.",
                  example="0065:0073 0066:0074"
                )

class KeywordSyncIbmdssnapArray(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="array",
                  at=True,
                  rtype="ibmdssnap",
                  required=True,
                  text="The name of the array holding the source devices and their paired devices.",
                  example="IBM.2243-12ABC00"
                )

class KeywordSyncIbmdssnapBgcopy(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="bgcopy",
                  at=True,
                  rtype="ibmdssnap",
                  candidates=[True, False],
                  required=True,
                  text="Initiate a background copy of the source data block to the paired devices upon resync."
                )

class KeywordSyncIbmdssnapRecording(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="recording",
                  at=True,
                  rtype="ibmdssnap",
                  candidates=[True, False],
                  required=True,
                  text="Track only changed data blocks instead of copying the whole source data to the paired devices."
                )

class KeywordSyncNexentaName(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="name",
                  at=True,
                  rtype="nexenta",
                  required=True,
                  text="The name of the Nexenta autosync configuration."
                )

class KeywordSyncNexentaFiler(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="filer",
                  at=True,
                  rtype="nexenta",
                  required=True,
                  text="The name of the Nexenta local head. Must be set for each node using the scoping syntax."
                )

class KeywordSyncNexentaPath(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="path",
                  at=True,
                  rtype="nexenta",
                  required=True,
                  text="The path of the zfs to synchronize, as seen by the Nexenta heads."
                )

class KeywordSyncNexentaReversible(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="reversible",
                  at=True,
                  rtype="nexenta",
                  candidates=[True, False],
                  required=True,
                  text="Defines if the replication link can be reversed. Set to no for prd to drp replications to protect production data."
                )

class KeywordSyncHp3parArray(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="array",
                  rtype="hp3par",
                  required=True,
                  text="Name of the HP 3par array to send commands to."
                )

class KeywordSyncHp3parMode(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="mode",
                  rtype="hp3par",
                  required=True,
                  candidates=["async", "sync"],
                  default="async",
                  text="Replication mode: Synchronous or Asynchronous"
                )

class KeywordSyncHp3parMethod(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="method",
                  rtype="hp3par",
                  required=False,
                  candidates=["ssh", "cli"],
                  default="ssh",
                  text="The method to use to submit commands to the arrays."
                )

class KeywordSyncHp3parRcg(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="rcg",
                  rtype="hp3par",
                  required=True,
                  text="Name of the HP 3par remote copy group. The scoping syntax must be used to fully describe the replication topology."
                )

class KeywordSyncDcsckptDcs(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="dcs",
                  rtype="dcsckpt",
                  required=True,
                  text="Whitespace-separated list of DataCore heads, as seen by the manager."
                )

class KeywordSyncDcsckptManager(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="manager",
                  rtype="dcsckpt",
                  required=True,
                  text="The DataCore manager name runing a ssh daemon, as set in the auth.conf section title."
                )

class KeywordSyncDcsckptPairs(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="pairs",
                  rtype="dcsckpt",
                  required=True,
                  text="A json-formatted list of dictionaries representing the source and destination device pairs. Each dictionary must have the 'src', 'dst_ckpt' keys."
                )

class KeywordSyncDcssnapDcs(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="dcs",
                  rtype="dcssnap",
                  required=True,
                  text="Whitespace-separated list of DataCore heads, as seen by the manager."
                )

class KeywordSyncDcssnapManager(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="manager",
                  rtype="dcssnap",
                  required=True,
                  text="The DataCore manager name runing a ssh daemon, as set in the auth.conf section title."
                )

class KeywordSyncDcssnapSnapname(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="snapname",
                  rtype="dcssnap",
                  required=True,
                  text="Whitespace-separated list of snapshot device names, as seen by the DataCore manager."
                )

class KeywordSyncEvasnapEvaname(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="eva_name",
                  rtype="evasnap",
                  required=True,
                  text="Name of the HP EVA array hosting the source and snapshot devices."
                )

class KeywordSyncEvasnapSnapname(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="snap_name",
                  rtype="evasnap",
                  required=True,
                  text="Name of the snapshot objectname as seen in sssu."
                )

class KeywordSyncEvasnapPairs(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="pairs",
                  rtype="evasnap",
                  required=True,
                  text="A json-formatted list of dictionaries representing the device pairs. Each dict must have the 'src', 'dst' and 'mask' keys. The mask key value is a list of \\<hostpath>\\<lunid> strings."
                )

class KeywordSyncNecismsnapArray(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="array",
                  rtype="necism",
                  required=True,
                  text="Name of the NEC ISM array to send commands to."
                )

class KeywordSyncNecismsnapDevs(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="devs",
                  rtype="necism",
                  required=True,
                  text="A whitespace-separated list of SV:LD."
                )

class KeywordSyncSymcloneSymdg(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="symdg",
                  rtype="symclone",
                  required=True,
                  text="Name of the symmetrix device group where the source and target devices are grouped."
                )

class KeywordSyncSymSrdfsSymdg(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="symdg",
                  at=True,
                  rtype="symsrdfs",
                  required=True,
                  text="Name of the symmetrix device group where the source and target devices are grouped."
                )

class KeywordSyncSymSrdfsRdfg(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="rdfg",
                  at=True,
                  rtype="symsrdfs",
                  required=True,
                  text="Name of the RDF group paring the source and target devices."
                )

class KeywordSyncSymclonePrecopyTimeout(KeywordInteger):
    def __init__(self):
        KeywordInteger.__init__(
                  self,
                  section="sync",
                  keyword="precopy_timeout",
                  at=True,
                  rtype="symclone",
                  required=True,
                  default=300,
                  text="Seconds to wait for a precopy (syncresync) to finish before returning with an error. In this case, the precopy proceeds normally, but the opensvc leftover actions must be retried. The precopy time depends on the amount of changes logged at the source, which is context-dependent. Tune to your needs."
                )

class KeywordSyncSymcloneSymdevs(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="symdevs",
                  rtype="symclone",
                  required=True,
                  at=True,
                  default=None,
                  text="Whitespace-separated list of devices to drive with this resource. Devices are specified as 'symmetrix identifier:symmetrix device identifier. Different symdevs can be setup on each node using the symdevs@nodename.",
                  example="000000000198:0060 000000000198:0061",
                )

class KeywordSyncDdsSrc(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="src",
                  rtype="dds",
                  required=True,
                  text="Points the origin of the snapshots to replicate from."
                )

class KeywordSyncDdsDst(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="dst",
                  rtype="dds",
                  required=True,
                  text="Target file or block device. Optional. Defaults to src. Points the media to replay the binary-delta received from source node to. This media must have a size superior or equal to source."
                )

class KeywordSyncDdsTarget(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="target",
                  rtype="dds",
                  required=True,
                  candidates=['nodes', 'drpnodes', 'nodes drpnodes'],
                  text="Accepted values are 'drpnodes', 'nodes' or both, whitespace-separated. Points the target nodes to replay the binary-deltas on. Be warned that starting the service on a target node without a 'stop-syncupdate-start cycle, will break the synchronization, so this mode is usually restricted to drpnodes sync, and should not be used to replicate data between nodes with automated services failover."
                )

class KeywordSyncDdsSnapSize(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="sync",
                  keyword="snap_size",
                  rtype="dds",
                  text="Default to 10% of origin. In MB, rounded to physical extent boundaries by lvm tools. Size of the snapshots created by OpenSVC to extract binary deltas from. Opensvc creates at most 2 snapshots : one short-lived to gather changed data from, and one long-lived to gather changed chunks list from. Volume groups should have the necessary space always available."
                )

class KeywordVdiskPath(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="vdisk",
                  keyword="path",
                  required=True,
                  at=True,
                  text="Path of the device or file used as a virtual machine disk. The path@nodename can be used to to set up different path on each node."
                )

class KeywordHbType(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="hb",
                  keyword="type",
                  required=True,
                  candidates=('OpenHA', 'LinuxHA'),
                  text="Specify the heartbeat driver to use."
                )

class KeywordHbName(Keyword):
    def __init__(self):
        Keyword.__init__(
                  self,
                  section="hb",
                  keyword="name",
                  rtype="OpenHA",
                  text="Specify the service name used by the heartbeat. Defaults to the service name."
                )

class Section(object):
    def __init__(self, section):
        self.section = section
        self.keywords = []

    def __iadd__(self, o):
        if not isinstance(o, Keyword):
            return self
        self.keywords.append(o)
        return self

    def __str__(self):
        s = ''
        for keyword in sorted(self.keywords):
            s += str(keyword)
        return s

    def template(self):
        k = self.getkey("type")
        if k is None:
            return self._template()
        if k.candidates is None:
            return self._template()
        s = ""
        for t in k.candidates:
            s += self._template(t)
        return s

    def _template(self, rtype=None):
        section = self.section
        dpath = os.path.join(os.path.dirname(__file__), "..", "usr", "share", "doc")
        fpath = os.path.join(dpath, "template."+section+".env")
        if rtype:
            section += ", type "+rtype
            fpath = os.path.join(dpath, "template."+self.section+"."+rtype+".env")
        s = "#"*78 + "\n"
        s += "# %-74s #\n" % " "
        s += "# %-74s #\n" % section
        s += "# %-74s #\n" % " "
        s += "#"*78 + "\n\n"
        if section == "DEFAULT":
            s += "[%s]\n" % self.section
        else:
            s += "[%s#0]\n" % self.section
        if rtype is not None:
            s += ";type = " + rtype + "\n\n"
        for keyword in sorted(self.getkeys(rtype)):
            s += keyword.template()
        for keyword in sorted(self.getprovkeys(rtype)):
            s += keyword.template()
        if rtype is not None:
            for keyword in sorted(self.getkeys()):
                if keyword.keyword == "type":
                    continue
                s += keyword.template()
        with open(fpath, "w") as f:
            f.write(s)
        return s

    def getkeys(self, rtype=None):
        if rtype is None:
            return [k for k in self.keywords if k.rtype is None and not k.provisioning]
        else:
            return [k for k in self.keywords if k.rtype and rtype in k.rtype and not k.provisioning]

    def getprovkeys(self, rtype=None):
        if rtype is None:
            return [k for k in self.keywords if k.rtype is None and k.provisioning]
        else:
            return [k for k in self.keywords if k.rtype and rtype in k.rtype and k.provisioning]

    def getkey(self, keyword, rtype=None):
        if '@' in keyword:
            l = keyword.split('@')
            if len(l) != 2:
                return None
            keyword, node = l
        if rtype:
            for k in self.keywords:
                if k.keyword == keyword and k.rtype and rtype in k.rtype:
                    return k
        else:
            for k in self.keywords:
                if k.keyword == keyword:
                    return k
        return None

class KeywordStore(dict):
    def __init__(self, provision=False):
        self.sections = {}
        self.provision = provision

    def __iadd__(self, o):
        if not isinstance(o, Keyword):
            return self
        o.top = self
        if o.section not in self.sections:
             self.sections[o.section] = Section(o.section)
        self.sections[o.section] += o
        return self

    def __getattr__(self, key):
        return self.sections[str(key)]

    def __getitem__(self, key):
        return self.sections[str(key)]

    def __str__(self):
        s = ''
        for section in self.sections:
            s += str(self.sections[section])
        return s

    def print_templates(self):
        for section in sorted(self.sections.keys()):
            print(self.sections[section].template())

    def required_keys(self, section, rtype=None):
        if section not in self.sections:
            return []
        return [k for k in sorted(self.sections[section].getkeys(rtype)) if k.required is True]

    def purge_keywords_from_dict(self, d, section):
        if 'type' in d:
            rtype = d['type']
        else:
            rtype = None
        for keyword, value in d.items():
            key = self.sections[section].getkey(keyword)
            if key is None and rtype is not None:
                key = self.sections[section].getkey(keyword, rtype)
            if key is None:
                if keyword != "rtype":
                    print("Remove unknown keyword '%s' from section '%s'"%(keyword, section))
                    del d[keyword]
        return d

    def update(self, rid, d):
        """ Given a resource dictionary, spot missing required keys
            and provide a new dictionary to merge populated by default
            values
        """
        import copy
        completion = copy.copy(d)

        # decompose rid into section and rtype
        if rid == 'DEFAULT':
            section = rid
            rtype = None
        else:
            if '#' not in rid:
                return {}
            l = rid.split('#')
            if len(l) != 2:
                return {}
            section = l[0]
            if 'type' in d:
                 rtype = d['type']
            elif self[section].getkey('type') is not None and \
                  self[section].getkey('type').default is not None:
                rtype = self[section].getkey('type').default
            else:
                rtype = None

        # validate command line dictionary
        for keyword, value in d.items():
            key = self.sections[section].getkey(keyword)
            if key is None and rtype is not None:
                key = self.sections[section].getkey(keyword, rtype)
            if key is None:
                continue
            if key.candidates is not None and value not in key.candidates:
                print("'%s' keyword has invalid value '%s' in section '%s'"%(keyword, str(value), rid))
                raise KeyInvalidValue()

        # add missing required keys if they have a known default value
        for key in self.required_keys(section, rtype):
            if key.keyword in d:
                continue
            if key.keyword in map(lambda x: x.split('@')[0], d.keys()):
                continue
            if key.default is None:
                sys.stderr.write("No default value for required key '%s' in section '%s'\n"%(key.keyword, rid))
                raise MissKeyNoDefault()
            print("Implicitely add [%s] %s = %s" % (rid, key.keyword, str(key.default)))
            completion[key.keyword] = key.default

        # purge unknown keywords and provisioning keywords
        completion = self.purge_keywords_from_dict(completion, section)

        return completion

    def form_sections(self, sections):
        wrapper = TextWrapper(subsequent_indent="%18s"%"", width=78)
        candidates = set(self.sections.keys()) - set(['DEFAULT'])

        print("------------------------------------------------------------------------------")
        print("Choose a resource type to add or a resource to edit.")
        print("Enter 'quit' to finish the creation.")
        print("------------------------------------------------------------------------------")
        print(wrapper.fill("resource types: "+', '.join(candidates)))
        print(wrapper.fill("resource ids:   "+', '.join(sections.keys())))
        print
        return raw_input("resource type or id> ")

    def free_resource_index(self, section, sections):
        indices = []
        for s in sections:
            l = s.split('#')
            if len(l) != 2:
                continue
            sname, sindex = l
            if section != sname:
                continue
            try:
                indices.append(int(sindex))
            except:
                continue
        i = 0
        while True:
            if i not in indices:
                return i
            i += 1

    def form(self, defaults, sections):
        for key in sorted(self.DEFAULT.getkeys()):
            defaults = key.form(defaults)
        while True:
            try:
                section = self.form_sections(sections)
            except EOFError:
                break
            if section == "quit":
                break
            if '#' in section:
                rid = section
                section = section.split('#')[0]
            else:
                index = self.free_resource_index(section, sections)
                rid = '#'.join((section, str(index)))
            if section not in self.sections:
                 print("unsupported resource type")
                 continue
            for key in sorted(self.sections[section].getkeys()):
                if rid not in sections:
                    sections[rid] = {}
                sections[rid] = key.form(sections[rid])
            if 'type' in sections[rid]:
                specific_keys = self.sections[section].getkeys(rtype=sections[rid]['type'])
                if len(specific_keys) > 0:
                    print("\nKeywords specific to the '%s' driver\n"%sections[rid]['type'])
                for key in sorted(specific_keys):
                    if rid not in sections:
                        sections[rid] = {}
                    sections[rid] = key.form(sections[rid])

            # purge the provisioning keywords
            sections[rid] = self.purge_keywords_from_dict(sections[rid], section)

        return defaults, sections

class KeyDict(KeywordStore):
    def __init__(self, provision=False):
        KeywordStore.__init__(self, provision)

        import os

        def kw_tags(resource):
            return Keyword(
                  section=resource,
                  keyword="tags",
                  at=True,
                  candidates=None,
                  default=None,
                  text="A list of tags. Arbitrary tags can be used to limit action scope to resources with a specific tag. Some tags can influence the driver behaviour. For example the 'encap' tag assigns the resource to the encapsulated service."
                )
        def kw_subset(resource):
            return Keyword(
                  section=resource,
                  keyword="subset",
                  at=True,
                  default=None,
                  text="Assign the resource to a specific subset."
                )
        def kw_restart(resource):
            return Keyword(
                  section=resource,
                  keyword="restart",
                  at=True,
                  default=0,
                  text="The agent will try to restart a resource n times before falling back to the monitor action."
                )
        def kw_monitor(resource):
            return Keyword(
                  section=resource,
                  keyword="monitor",
                  at=True,
                  candidates=(True, False),
                  default=False,
                  text="A monitored resource will trigger a node suicide if the service has a heartbeat resource in up state"
                )
        def kw_disable(resource):
            return Keyword(
                  section=resource,
                  keyword="disable",
                  at=True,
                  candidates=(True, False),
                  default=False,
                  text="A disabled resource will be ignored on service startup and shutdown."
                )
        def kw_optional(resource):
            return Keyword(
                  section=resource,
                  keyword="optional",
                  at=True,
                  candidates=(True, False),
                  default=False,
                  text="Possible values are 'true' or 'false'. Actions on resource will be tried upon service startup and shutdown, but action failures will be logged and passed over. Useful for resources like dump filesystems for example."
                )
        def kw_always_on(resource):
            return Keyword(
                  section=resource,
                  keyword="always_on",
                  candidates=['nodes', 'drpnodes', 'nodes drpnodes'],
                  text="Possible values are 'nodes', 'drpnodes' or 'nodes drpnodes', or a list of nodes. Sets the nodes on which the resource is always kept up. Primary usage is file synchronization receiving on non-shared disks. Don't set this on shared disk !! danger !!"
                )
        def kw_pre_start(resource):
            return Keyword(
                  section=resource,
                  keyword="pre_start",
                  at=True,
                  text="A script to execute before the resource start action"
                )
        def kw_post_start(resource):
            return Keyword(
                  section=resource,
                  keyword="post_start",
                  at=True,
                  text="A script to execute after the resource start action"
                )
        def kw_pre_stop(resource):
            return Keyword(
                  section=resource,
                  keyword="pre_stop",
                  at=True,
                  text="A script to execute before the resource stop action"
                )
        def kw_post_stop(resource):
            return Keyword(
                  section=resource,
                  keyword="post_stop",
                  at=True,
                  text="A script to execute after the resource stop action"
                )
        def kw_pre_syncnodes(resource):
            return Keyword(
                  section=resource,
                  keyword="pre_syncnodes",
                  at=True,
                  text="A script to execute before the resource syncnodes action"
                )
        def kw_post_syncnodes(resource):
            return Keyword(
                  section=resource,
                  keyword="post_syncnodes",
                  at=True,
                  text="A script to execute after the resource syncnodes action"
                )
        def kw_pre_syncdrp(resource):
            return Keyword(
                  section=resource,
                  keyword="pre_syncdrp",
                  at=True,
                  text="A script to execute before the resource syncdrp action"
                )
        def kw_post_syncdrp(resource):
            return Keyword(
                  section=resource,
                  keyword="post_syncdrp",
                  at=True,
                  text="A script to execute after the resource syncdrp action"
                )
        def kw_pre_syncresync(resource):
            return Keyword(
                  section=resource,
                  keyword="pre_syncresync",
                  at=True,
                  text="A script to execute before the resource syncresync action"
                )
        def kw_post_syncresync(resource):
            return Keyword(
                  section=resource,
                  keyword="post_syncresync",
                  at=True,
                  text="A script to execute after the resource syncresync action"
                )

        for r in ["sync", "ip", "fs", "vg", "hb", "pool", "vmdg", "drbd",
                  "loop", "disk", "share", "vdisk", "container", "pool",
                  "app"]:
            self += kw_restart(r)
            self += kw_tags(r)
            self += kw_subset(r)
            self += kw_monitor(r)
            self += kw_disable(r)
            self += kw_optional(r)
            self += kw_always_on(r)
            self += kw_pre_start(r)
            self += kw_post_start(r)
            self += kw_pre_stop(r)
            self += kw_post_stop(r)
            self += kw_pre_syncnodes(r)
            self += kw_post_syncnodes(r)
            self += kw_pre_syncdrp(r)
            self += kw_post_syncdrp(r)
            self += kw_pre_syncresync(r)
            self += kw_post_syncresync(r)

        self += KeywordMode()
        self += KeywordPkgName()
        self += KeywordDockerDataDir()
        self += KeywordDockerDaemonArgs()
        self += KeywordAntiAffinity()
        self += KeywordNoPreemptAbort()
        self += KeywordCluster()
        self += KeywordClusterType()
        self += KeywordFlexMinNodes()
        self += KeywordFlexMaxNodes()
        self += KeywordFlexCpuMinThreshold()
        self += KeywordFlexCpuMaxThreshold()
        self += KeywordServiceType()
        self += KeywordNodes()
        self += KeywordAutostartNode()
        self += KeywordDrpnode()
        self += KeywordDrpnodes()
        self += KeywordApp()
        self += KeywordComment()
        self += KeywordScsireserv()
        self += KeywordBwlimit()
        self += KeywordSyncInterval()
        self += KeywordSyncMaxDelay()
        self += KeywordPresnapTrigger()
        self += KeywordPostsnapTrigger()
        self += KeywordMonitorAction()
        self += KeywordContainerize()
        self += KeywordContainerCpus()
        self += KeywordContainerMems()
        self += KeywordContainerCpuShare()
        self += KeywordContainerMemLimit()
        self += KeywordContainerVmemLimit()
        self += KeywordSyncType()
        self += KeywordSyncDockerTarget()
        self += KeywordSyncBtrfsSrc()
        self += KeywordSyncBtrfsDst()
        self += KeywordSyncBtrfsTarget()
        self += KeywordSyncBtrfsRecursive()
        self += KeywordSyncZfsSrc()
        self += KeywordSyncZfsDst()
        self += KeywordSyncZfsTarget()
        self += KeywordSyncZfsRecursive()
        self += KeywordSyncZfsTags()
        self += KeywordSyncRsyncSrc()
        self += KeywordSyncRsyncDst()
        self += KeywordSyncRsyncTags()
        self += KeywordSyncRsyncExclude()
        self += KeywordSyncRsyncOptions()
        self += KeywordSyncRsyncTarget()
        self += KeywordSyncRsyncSnap()
        self += KeywordSyncRsyncDstfs()
        self += KeywordSyncRsyncBwlimit()
        self += KeywordSyncSchedule()
        self += KeywordSyncSyncMaxDelay()
        self += KeywordIpType()
        self += KeywordIpIpname()
        self += KeywordIpIpdev()
        self += KeywordIpIpdevext()
        self += KeywordIpNetmask()
        self += KeywordIpGateway()
        self += KeywordIpZone()
        self += KeywordVgType()
        self += KeywordVgRawDevs()
        self += KeywordVgVgname()
        self += KeywordVgDsf()
        self += KeywordVgImages()
        self += KeywordVgClientId()
        self += KeywordVgKeyring()
        self += KeywordVgLock()
        self += KeywordVgLockSharedTag()
        self += KeywordVgSize()
        self += KeywordVgImageFormat()
        self += KeywordVgOptions()
        self += KeywordVgScsireserv()
        self += KeywordVgPvs()
        self += KeywordPoolPoolname()
        self += KeywordVmdgScsireserv()
        self += KeywordVmdgContainerid()
        self += KeywordVmdgContainerType()
        self += KeywordDrbdScsireserv()
        self += KeywordDrbdRes()
        self += KeywordFsType()
        self += KeywordFsDev()
        self += KeywordFsZone()
        self += KeywordFsMnt()
        self += KeywordFsMntOpt()
        self += KeywordFsSnapSize()
        self += KeywordFsVg()
        self += KeywordFsSize()
        self += KeywordLoopFile()
        self += KeywordLoopSize()
        self += KeywordAppScript()
        self += KeywordAppTimeout()
        self += KeywordAppStart()
        self += KeywordAppStop()
        self += KeywordAppCheck()
        self += KeywordAppInfo()
        self += KeywordSyncNexentaName()
        self += KeywordSyncNexentaFiler()
        self += KeywordSyncNexentaPath()
        self += KeywordSyncNexentaReversible()
        self += KeywordSyncNetappFiler()
        self += KeywordSyncNetappPath()
        self += KeywordSyncNetappUser()
        self += KeywordSyncIbmdssnapPairs()
        self += KeywordSyncIbmdssnapArray()
        self += KeywordSyncIbmdssnapBgcopy()
        self += KeywordSyncIbmdssnapRecording()
        self += KeywordSyncSymSrdfsSymdg()
        self += KeywordSyncSymSrdfsRdfg()
        self += KeywordSyncSymcloneSymdg()
        self += KeywordSyncSymclonePrecopyTimeout()
        self += KeywordSyncSymcloneSymdevs()
        self += KeywordSyncDcsckptDcs()
        self += KeywordSyncDcsckptManager()
        self += KeywordSyncDcsckptPairs()
        self += KeywordSyncDcssnapDcs()
        self += KeywordSyncDcssnapManager()
        self += KeywordSyncDcssnapSnapname()
        self += KeywordSyncNecismsnapArray()
        self += KeywordSyncNecismsnapDevs()
        self += KeywordSyncEvasnapEvaname()
        self += KeywordSyncEvasnapSnapname()
        self += KeywordSyncEvasnapPairs()
        self += KeywordSyncHp3parArray()
        self += KeywordSyncHp3parRcg()
        self += KeywordSyncHp3parMode()
        self += KeywordSyncHp3parMethod()
        self += KeywordSyncDdsSrc()
        self += KeywordSyncDdsDst()
        self += KeywordSyncDdsTarget()
        self += KeywordSyncDdsSnapSize()
        self += KeywordVdiskPath()
        self += KeywordHbType()
        self += KeywordHbName()
        self += KeywordSubsetParallel()
        self += KeywordStonithType()
        self += KeywordStonithTarget()
        self += KeywordStonithCalloutCmd()
        self += KeywordContainerType()
        self += KeywordVmName()
        self += KeywordGuestos()
        self += KeywordRootfs()
        self += KeywordLxcCf()
        self += KeywordJailRoot()
        self += KeywordJailIps()
        self += KeywordJailIp6s()
        self += KeywordTemplate()
        self += KeywordSharedIpGroup()
        self += KeywordSize()
        self += KeywordKeyName()
        self += KeywordCloudId()
        self += KeywordVmUuid()
        self += KeywordVirtinst()
        self += KeywordDockerRunCommand()
        self += KeywordDockerRunImage()
        self += KeywordDockerRunArgs()
        self += KeywordSnap()
        self += KeywordSnapof()
        self += KeywordContainerOrigin()
        self += KeywordSrpIp()
        self += KeywordSrpRootpath()
        self += KeywordSrpPrmCores()
        self += KeywordShareType()
        self += KeywordSharePath()
        self += KeywordShareNfsOpts()

if __name__ == "__main__":
    store = KeyDict()
    store.print_templates()
    #print(store.DEFAULT.app)
    #print(store['DEFAULT'])
 0707010000bc8a000081a400000000000000000000000154fc7bfb000006dd000000660000000300000000000000000000002200000000root/opt/opensvc/lib/svcHosted.py #
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import svc

class SvcHosted(svc.Svc):
    """Define hosted services
    """

    def __init__(self, svcname, optional=False, disabled=False, tags=set([])):
        svc.Svc.__init__(self, svcname, "Hosted", optional=optional, disabled=disabled, tags=tags)

if __name__ == "__main__":
    for c in (SvcHosted,) :
        help(c)
    import mountSunOS as mount
    import ipSunOS as ip
    print("""
    S=SvcHosted()
    S+=mount.Mount("/mnt1","/dev/sda")
    S+=mount.Mount("/mnt2","/dev/sdb")
    S+=ip.Ip("eth0","192.168.0.173")
    S+=ip.Ip("eth0","192.168.0.174")
    """)

    S=SvcSone()
    S+=mount.Mount("/mnt1","/dev/sda")
    S+=mount.Mount("/mnt2","/dev/sdb")
    S+=ip.Ip("eth0","192.168.0.173")
    S+=ip.Ip("eth0","192.168.0.174")

    print("Show S: ", S)
    print("start S:")
    S.start()

    print("stop S:")
    S.stop()

   0707010000bc8b000081a400000000000000000000000154fc7bfb0000193f000000660000000300000000000000000000002000000000root/opt/opensvc/lib/svcRhcs.py   #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import os
import svc
import socket
import rcExceptions as ex
from rcUtilities import justcall
from rcGlobalEnv import rcEnv
from xml.etree.ElementTree import ElementTree, SubElement
import rcIfconfigLinux as rcIfconfig

class SvcRhcs(svc.Svc):

    def __init__(self, svcname, pkg_name=None, optional=False, disabled=False, tags=set([])):
        svc.Svc.__init__(self, svcname, "rhcs", optional=optional, disabled=disabled, tags=tags)
        self.cf = "/etc/cluster/cluster.conf"
        self.pkg_name = pkg_name
        ifconfig = rcIfconfig.ifconfig()
        self.node_ips = []
        self.member_to_nodename_h = {}
        for i in ifconfig.intf:
            self.node_ips += i.ipaddr
        self.builder()

    def getaddr(self, ipname):
        a = socket.getaddrinfo(ipname, None)
        if len(a) == 0:
            raise ex.excError("unable to resolve %s ip address" % ipname)
        addr = a[0][4][0]
        return addr

    def member_to_nodename(self, member):
        if member in self.member_to_nodename_h:
            return self.member_to_nodename_h[member]

        try:
            addr = self.getaddr(member)
        except:
            self.member_to_nodename_h[member] = member
            return self.member_to_nodename_h[member]

        if addr in self.node_ips:
            self.member_to_nodename_h[member] = rcEnv.nodename
            return self.member_to_nodename_h[member]

        cmd = rcEnv.rsh.split() + [member, "hostname"]
        out, err, ret = justcall(cmd)
        if ret != 0:
            self.member_to_nodename_h[member] = member
            return self.member_to_nodename_h[member]

        self.member_to_nodename_h[member] = out.strip()
        return self.member_to_nodename_h[member]

    def load_cluster_conf(self):
        self.tree = ElementTree()
        self.tree.parse(self.cf)
        e = self.tree.getiterator('cluster')
        if len(e) != 1:
            raise ex.excInitError()
        self.xml = e[0]

    def builder(self):
        if self.pkg_name is None:
            self.error("pkg_name is not set")
            raise ex.excInitError()
        self.load_cluster_conf()
        self.load_service()
        self.load_nodes()
        self.load_clustat()
        self.load_hb()

    def load_clustat(self):
        cmd = ['clustat']
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excInitError()
        self.clustat = out.split('\n')

    def load_hb(self):
        rid = 'hb#sg0'
        m = __import__("resHbRhcs")
        r = m.Hb(rid, self.pkg_name)
        self += r

    def load_vg(self, e):
        """
        r = m.Vg(rid, name)
        self += r
        r.monitor = True
        self.n_vg += 1
        """
        pass

    def load_ip(self, e):
        """
        <ip address="10.105.133.5" monitor_link="0">
        """
        if 'ref' in e.attrib:
            # load ref xml node and recurse
            return
        if not 'address' in e.attrib:
            return
        ipname = e.attrib['address']

        n = self.n_ip
        rid = 'ip#rhcs%d'%n
        m = __import__("resIpRhcs"+rcEnv.sysname)
        r = m.Ip(rid, "", ipname, "")
        r.monitor = True
        self += r
        self.n_ip += 1

    def load_fs(self, e):
        """
        <fs device="/dev/communvg/lv_test"
            force_fsck="0"
            force_unmount="1"
            fsid="13584"
            fstype="ext3"
            mountpoint="/mnt/hgfs"
            name="testfs"
            self_fence="0"/>
        """
        if 'device' not in e.attrib:
            return
        dev = e.attrib['device']

        if 'mountpoint' not in e.attrib:
            return
        mnt = e.attrib['mountpoint']

        if 'fstype' not in e.attrib:
            return
        fstype = e.attrib['fstype']

        mntopt = ""
        n = self.n_fs
        rid = 'fs#rhcs%d'%n
        m = __import__("resMountRhcs"+rcEnv.sysname)
        r = m.Mount(rid, mnt, dev, fstype, mntopt)
        r.monitor = True
        self += r
        self.n_fs += 1

    def load_nodes(self):
        nodes = set([])
        for m in self.xml.findall("clusternodes/clusternode"):
            member = m.attrib['name']
            nodename = self.member_to_nodename(member)
            nodes.add(nodename)
        self.nodes = nodes

    def get_ref(self, refname):
        for m in self.xml.findall("rm/resources/*"):
            if m.attrib.get('name') == refname or \
               m.attrib.get('address') == refname:
                return m
        return None

    def iter_rtype(self, head):
        for r in head.findall('*'):
            if r.tag == 'ip':
                rtype = 'ip'
            elif r.tag == 'fs':
                rtype = 'fs'
            else:
                rtype = None
            if rtype is not None:
                if 'ref' in r.attrib:
                    refname = r.attrib['ref']
                    ref = self.get_ref(refname)
                    if ref is not None:
                        getattr(self, "load_"+rtype)(ref)
                else:
                    getattr(self, "load_"+rtype)(r)
            self.iter_rtype(r)

    def load_service(self):
        self.n_ip = 0
        self.n_fs = 0
        self.n_vg = 0
        found = False
        for m in self.xml.findall("rm/service"):
            if m.attrib['name'] != self.pkg_name:
                continue
            found = True
            self.iter_rtype(m)
        if not found:
            raise ex.excInitError("service %s not found in cluster configuration"%self.pkg_name)

    def resource_monitor(self):
        pass

 0707010000bc8c000081a400000000000000000000000154fc7bfb00002829000000660000000300000000000000000000001e00000000root/opt/opensvc/lib/svcSg.py #
# Copyright (c) 2012 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import os
import svc
import rcExceptions as ex
from rcUtilities import justcall
from rcGlobalEnv import rcEnv

class SvcSg(svc.Svc):

    def __init__(self, svcname, pkg_name=None, optional=False, disabled=False, tags=set([])):
        svc.Svc.__init__(self, svcname, "ServiceGuard", optional=optional, disabled=disabled, tags=tags)
        self.pkg_name = pkg_name
        self.load_paths()
        self.builder()

    def load_paths(self):
        p = '/usr/local/cmcluster/bin/'
        if os.path.exists(p):
            self.prefix = p
        else:
            self.prefix = ''
        self.cmviewcl_bin = self.prefix + 'cmviewcl'
        self.cmgetconf_bin = self.prefix + 'cmgetconf'
        self.cntl = {"vg": {}, "ip": {}, "fs": {}}

    def load_cmviewcl(self):
        self.cmviewcl = {}

        cmd = [self.cmviewcl_bin, "-p", self.pkg_name, "-v", "-f", "line"]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excInitError(err)
        for line in out.split("\n"):
            if "=" not in line:
                continue
            i = line.index('=')
            param = line[:i]
            value = line[i+1:]

            if '|' in param:
                l = param.split('|')
                if len(l) == 2:
                    res, param = l
                    node = None
                elif len(l) == 3:
                    res, node, param = l
                    node = node.replace('node:', '')
                else:
                    print l
                    continue
                restype, resname = res.split(':')
                if restype not in self.cmviewcl:
                    self.cmviewcl[restype] = {}
                if resname not in self.cmviewcl[restype]:
                    self.cmviewcl[restype][resname] = {}
                if node is not None:
                    self.cmviewcl[restype][resname][(param,node)] = value
                else:
                    self.cmviewcl[restype][resname][param] = value
            else:
                self.cmviewcl[param] = value
        #print self.cmviewcl

    def load_cmgetconf(self):
        if self.cmviewcl.get('style') != "modular":
            return
        cmd = [self.cmgetconf_bin, "-p", self.pkg_name]
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excInitError(err)
        lines = out.split("\n")
        l = []
        for line in lines:
            if len(line) == 0:
                continue
            if line.startswith("#"):
                continue
            if '\t' not in line:
                continue
            i = line.index('\t')
            param = line[:i]
            value = line[i+1:]
            value = value.strip().strip('"')
            l.append((param, value))

        index1 = None
        for i, (param, value) in enumerate(l):
            if param in ("vg", "cvm_vg", "vxvm_vg"):
                self.cntl["vg"][value] = {param.upper(): value}
                continue
            if param == "ip_subnet":
                index1 = "ip"
                index2 = 0
                ip_subnet = value
                continue
            if param == "ip_address":
                self.cntl[index1][index2] = {
                  'IP': value,
                  'SUBNET': ip_subnet,
                }
                index2 += 1
                continue
            if param == "fs_name":
                index1 = "fs"
                index2 = value
                self.cntl[index1][index2] = {}
            if index1 is None:
                continue
            try:
                self.cntl[index1][index2][param] = value
            except:
                continue
            if index1 == "fs" and param == "fs_fsck_opt":
                index1 = None
        #print self.cntl

    def load_cntl(self):
        if 'run_script' not in self.cmviewcl:
            # modular package
            return
        p = self.cmviewcl['run_script']
        try:
            f = open(p, 'r')
            buff = f.read()
            f.close()
        except:
            self.log.error("failed to load %s"%p)
            raise ex.excError
        for line in buff.split('\n'):
             line = line.strip()
             if line.startswith("#"):
                 continue
             if len(line) == 0:
                 continue
             for _line in line.split(';'):
                 _line = _line.strip()
                 if _line.startswith("VG[") or \
                    _line.startswith("CVM_DG[") or \
                    _line.startswith("VXVM_DG[") or \
                    _line.startswith("IP[") or \
                    _line.startswith("SUBNET[") or \
                    _line.startswith("LV[") or \
                    _line.startswith("FS_MOUNT_OPT[") or \
                    _line.startswith("FS_TYPE[") or \
                    _line.startswith("FS["):
                     self.cntl_parse(_line)

    def cntl_parse(self, s):
        i = s.index('[')
        param = s[:i]
        s = s[i+1:]
        i = s.index(']')
        index = s[:i]
        try:
            int(index)
        except:
            return
        value = s[i+2:].strip('"')

        if param in ["VG", "CVM_DG", "VXVM_DG"]:
            if index not in self.cntl['vg']:
                self.cntl['vg'][index] = {}
            self.cntl['vg'][index][param] = value.replace('/dev/', '')

        if param in ["IP", "SUBNET"]:
            if index not in self.cntl['ip']:
                self.cntl['ip'][index] = {}
            self.cntl['ip'][index][param] = value

        if param in ["FS", "LV", "FS_MOUNT_OPT", "FS_TYPE"]:
            if index not in self.cntl['fs']:
                self.cntl['fs'][index] = {}
            self.cntl['fs'][index][param] = value

    def builder(self):
        if self.pkg_name is None:
            self.error("pkg_name is not set")
            raise ex.excInitError()
        self.load_cmviewcl()
        if len(self.cmviewcl) == 0:
            raise ex.excInitError()
        self.load_cntl()
        self.load_cmgetconf()
        self.nodes = set(self.cmviewcl['node'].keys())
        self.load_hb()
        self.load_resources()
        self.load_ip_addresses()
        self.load_vgs()

    def load_hb(self):
        if self.cmviewcl['highly_available'] != "yes":
            return
        rid = 'hb#sg0'
        m = __import__("resHbSg")
        r = m.Hb(rid, self.cmviewcl['name'])
        self += r

    def load_vgs(self):
        self.n_vg = 0
        for i in self.cntl['vg']:
            data = self.cntl['vg'][i]
            self.load_vg(data)

    def load_vg(self, data):
        if 'VG' in data:
            name = data['VG'].replace('/dev/', '')
            type = ""
        elif 'CVM_DG' in data:
            name = data['CVM_DG']
            type = "Cvm"
        elif 'VXVM_DG' in data:
            name = data['VXVM_DG']
            type = "VxVm"
        n = self.n_vg
        rid = 'vg#sg%d'%n
        modname = "resVg"+type+"Sg"+rcEnv.sysname
        try:
            m = __import__(modname)
        except ImportError:
            self.log.error("module %s is not implemented"%modname)
            return
        r = m.Vg(rid, name)
        if 'service' in self.cmviewcl:
           for data in self.cmviewcl['service'].values():
               if 'command' not in data:
                   continue
               if name in data['command'].split():
                   r.monitor = True
        self += r
        self.n_vg += 1

    def load_ip_addresses(self):
        self.n_ip_address = 0
        for i in self.cntl['ip']:
            data = self.cntl['ip'][i]
            self.load_ip_address(data)

    def load_ip_address(self, data):
        ipname = data['IP']
        subnet = data['SUBNET']
        n = self.n_ip_address
        rid = 'ip#sg%d'%n
        m = __import__("resIpSg"+rcEnv.sysname)
        r = m.Ip(rid, "", ipname, "")
        if 'subnet' in self.cmviewcl and \
           subnet in self.cmviewcl['subnet']:
            r.monitor = True
        self += r
        self.n_ip_address += 1

    def load_resources(self):
        self.n_resource = 0
        for i in self.cntl['fs']:
            data = self.cntl['fs'][i]
            self.load_resource(data)

    def load_resource(self, data):
        if 'LV' in data:
            dev = data['LV']
            mnt = data['FS']
            mntopt = data['FS_MOUNT_OPT']
            fstype = data['FS_TYPE']
        else:
            if data['fs_server'] != "":
                dev = data['fs_server'] + ":" + data['fs_name']
            else:
                dev = data['fs_name']
            mnt = data['fs_directory']
            mntopt = data['fs_mount_opt']
            fstype = data['fs_type']
        vgname = dev.split('/')[2]
        lvname = dev.split('/')[3]
        n = self.n_resource
        rid = 'fs#sg%d'%n
        m = __import__("resMountSg"+rcEnv.sysname)
        r = m.Mount(rid, mnt, dev, fstype, mntopt)
        r.mon_name = '/vg/%s/lv/status/%s'%(vgname, lvname)
        if 'resource' in self.cmviewcl and \
           r.mon_name in self.cmviewcl['resource']:
            r.monitor = True
        if 'service' in self.cmviewcl:
           for data in self.cmviewcl['service'].values():
               if 'command' not in data:
                   continue
               if dev in data['command']:
                   r.monitor = True
        self += r
        self.n_resource += 1
   0707010000bc8d000081a400000000000000000000000154fc7bfb00001df6000000660000000300000000000000000000001f00000000root/opt/opensvc/lib/svcVcs.py    #
# Copyright (c) 2013 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# To change this template, choose Tools | Templates
# and open the template in the editor.

import os
import svc
import rcExceptions as ex
from rcUtilities import justcall
from rcGlobalEnv import rcEnv

class SvcVcs(svc.Svc):

    def __init__(self, svcname, pkg_name=None, optional=False, disabled=False, tags=set([])):
        svc.Svc.__init__(self, svcname, "vcs", optional=optional, disabled=disabled, tags=tags)
        self.pkg_name = pkg_name
        self.domainname = None
        self.vcs_operational = True

        self.n_ip = 0
        self.n_fs = 0
        self.n_vg = 0
        self.n_lv = 0

        self.builder()

    def get_res_val(self, res, p):
        if not self.vcs_operational:
            raise ex.excError("VCS is not operational")
        cmd = ['/opt/VRTSvcs/bin/hares', '-value', res, p]
        out, err, ret = justcall(cmd)
        if ret != 0:
            if "Cannot connect" in out:
                self.vcs_operational = False
            raise ex.excError(out)
        return out.strip()

    def get_grp_val(self, p):
        if not self.vcs_operational:
            raise ex.excError("VCS is not operational")
        cmd = ['/opt/VRTSvcs/bin/hagrp', '-value', self.pkg_name, p]
        out, err, ret = justcall(cmd)
        if ret != 0:
            if "Cannot connect" in out:
                self.vcs_operational = False
            raise ex.excError(out)
        return out.strip()

    def get_domainname(self):
        if self.domainname is not None:
            return self.domainname
        cmd = ['hostname', '-d']
        out, err, ret = justcall(cmd)
        if ret != 0:
            raise ex.excError(err)
        self.domainname = out
        return out.strip()

    def set_nodes(self):
        try:
            s = self.get_grp_val('SystemList')
        except ex.excError as e:
            self.nodes = set([rcEnv.nodename])
            return

        l = s.split()

        # SystemList goes in system/weight pairs
        if len(l) < 2 or len(l) % 2 != 0:
            raise ex.excError("unexpected SystemList value: %s"%s)

        self.nodes = set([])
        domainname = self.get_domainname()
        for i, w in enumerate(l):
            if i % 2 == 1:
                continue
            if len(domainname) > 0 and not w.endswith(domainname):
                w += '.' + domainname
            self.nodes.add(w)

    def builder(self):
        if self.pkg_name is None:
            raise ex.excInitError("pkg name is not set")
        self.set_nodes()
        self.load_hb()
        self.load_resources()

    def load_hb(self):
        rid = 'hb#vcs0'
        m = __import__("resHbVcs")
        r = m.Hb(rid, self.pkg_name)
        self += r

    def load_resources(self):
        if not self.vcs_operational:
            return
        cmd = ['/opt/VRTSvcs/bin/hagrp', '-resources', self.pkg_name]
        out, err, ret = justcall(cmd)
        if ret != 0:
            return
        resource_names = out.strip().split('\n')
        for resource_name in resource_names:
            self.load_resource(resource_name)

    def load_resource(self, name):
        s = self.get_res_val(name, 'Type')
        if s == 'Mount':
            self.load_fs(name)
            self.n_fs += 1
        elif s == 'CFSMount':
            self.load_cfs(name)
            self.n_fs += 1
        elif s  == 'DiskGroup':
            self.load_vg(name)
            self.n_vg += 1
        elif s == 'CVMVolDg':
            self.load_cvg(name)
            self.n_vg += 1
        elif s == 'Volume':
            self.load_lv(name)
            self.n_lv += 1
        elif s == 'IP':
            self.load_ip(name)
            self.n_ip += 1

    def load_lv(self, name):
        lvname = self.get_res_val(name, 'Volume')
        vgname = self.get_res_val(name, 'DiskGroup')
        disabled = True if self.get_res_val(name, 'Enabled') == "0" else False
        monitor = True if self.get_res_val(name, 'Critical') == "1" else False
        rid = 'lv#vcs%d'%self.n_lv
        m = __import__("resLvVcs"+rcEnv.sysname)
        r = m.Lv(rid, vgname, lvname, disabled=disabled, monitor=monitor)
        r.vcs_name = name
        self += r

    def load_cvg(self, name):
        vgname = self.get_res_val(name, 'CVMDiskGroup')
        disabled = True if self.get_res_val(name, 'Enabled') == "0" else False
        monitor = True if self.get_res_val(name, 'Critical') == "1" else False
        rid = 'vg#vcs%d'%self.n_vg
        m = __import__("resVgVcs"+rcEnv.sysname)
        r = m.Vg(rid, vgname, disabled=disabled, monitor=monitor)
        r.vcs_name = name
        self += r

    def load_vg(self, name):
        vgname = self.get_res_val(name, 'DiskGroup')
        disabled = True if self.get_res_val(name, 'Enabled') == "0" else False
        monitor = True if self.get_res_val(name, 'Critical') == "1" else False
        rid = 'vg#vcs%d'%self.n_vg
        m = __import__("resVgVcs"+rcEnv.sysname)
        r = m.Vg(rid, vgname, disabled=disabled, monitor=monitor)
        r.vcs_name = name
        self += r

    def load_ip(self, name):
        dev = self.get_res_val(name, 'Device')
        ipaddr = self.get_res_val(name, 'Address')
        netmask = self.get_res_val(name, 'NetMask')
        disabled = True if self.get_res_val(name, 'Enabled') == "0" else False
        monitor = True if self.get_res_val(name, 'Critical') == "1" else False
        rid = 'ip#vcs%d'%self.n_ip
        m = __import__("resIpVcs"+rcEnv.sysname)
        r = m.Ip(rid, dev, ipaddr, netmask, disabled=disabled, monitor=monitor)
        r.vcs_name = name
        self += r

    def load_fs(self, name):
        dev = self.get_res_val(name, 'BlockDevice')
        mnt = self.get_res_val(name, 'MountPoint')
        mntopt = self.get_res_val(name, 'MountOpt')
        fstype = self.get_res_val(name, 'FSType')
        disabled = True if self.get_res_val(name, 'Enabled') == "0" else False
        monitor = True if self.get_res_val(name, 'Critical') == "1" else False
        rid = 'fs#vcs%d'%self.n_fs
        m = __import__("resMountVcs"+rcEnv.sysname)
        r = m.Mount(rid, mnt, dev, fstype, mntopt,
                    disabled=disabled, monitor=monitor)
        r.vcs_name = name
        self += r

    def load_cfs(self, name):
        dev = self.get_res_val(name, 'BlockDevice')
        mnt = self.get_res_val(name, 'MountPoint')
        mntopt = self.get_res_val(name, 'MountOpt')
        fstype = self.get_res_val(name, 'AMFMountType')
        disabled = True if self.get_res_val(name, 'Enabled') == "0" else False
        monitor = True if self.get_res_val(name, 'Critical') == "1" else False
        rid = 'fs#vcs%d'%self.n_fs
        m = __import__("resMountVcs"+rcEnv.sysname)
        r = m.Mount(rid, mnt, dev, fstype, mntopt,
                    disabled=disabled, monitor=monitor)
        r.vcs_name = name
        self += r

    def resource_monitor(self):
        pass

  0707010000bc8e000081a400000000000000000000000154fc7bfb000066ea000000660000000300000000000000000000002100000000root/opt/opensvc/lib/tabulate.py  # -*- coding: utf-8 -*-

"""Pretty-print tabular data."""

from __future__ import print_function
from __future__ import unicode_literals
from collections import namedtuple
from platform import python_version_tuple
import re


if python_version_tuple()[0] < "3":
    from itertools import izip_longest
    _none_type = type(None)
    _int_type = int
    _float_type = float
    _text_type = unicode
    _binary_type = str
else:
    from itertools import zip_longest as izip_longest
    from functools import reduce
    _none_type = type(None)
    _int_type = int
    _float_type = float
    _text_type = str
    _binary_type = bytes

__all__ = ["tabulate"]
__version__ = "0.6"

def __text_type(s):
    try:
       return unicode(s, errors="ignore")
    except:
       return s

Line = namedtuple("Line", ["begin", "hline", "sep", "end"])


DataRow = namedtuple("DataRow", ["begin", "sep", "end"])


TableFormat = namedtuple("TableFormat", ["lineabove", "linebelowheader",
                                         "linebetweenrows", "linebelow",
                                         "headerrow", "datarow",
                                         "padding", "usecolons", "usehtmlattrs",
                                         "with_header_hide",
                                         "without_header_hide"])


_format_defaults = {"padding": 0,
                    "usecolons": False,
                    "usehtmlattrs": False,
                    "with_header_hide": [],
                    "without_header_hide": []}


_table_formats = {"simple":
                  TableFormat(lineabove=None,
                              linebelowheader=Line("", "-", "  ", ""),
                              linebetweenrows=None,
                              linebelow=Line("", "-", "  ", ""),
                              headerrow=DataRow("", "  ", ""),
                              datarow=DataRow("", "  ", ""),
                              padding=0,
                              usecolons=False,
                              usehtmlattrs=False,
                              with_header_hide=["linebelow"],
                              without_header_hide=[]),
                  "plain":
                  TableFormat(None, None, None, None,
                              DataRow("", "  ", ""), DataRow("", "  ", ""),
                              padding=_format_defaults["padding"],
                              usecolons=_format_defaults["usecolons"],
                              usehtmlattrs=_format_defaults["usehtmlattrs"],
                              with_header_hide=_format_defaults["with_header_hide"],
                              without_header_hide=_format_defaults["without_header_hide"]),
                  "grid":
                  TableFormat(lineabove=Line("+", "-", "+", "+"),
                              linebelowheader=Line("+", "=", "+", "+"),
                              linebetweenrows=Line("+", "-", "+", "+"),
                              linebelow=Line("+", "-", "+", "+"),
                              headerrow=DataRow("|", "|", "|"),
                              datarow=DataRow("|", "|", "|"),
                              padding=1,
                              usecolons=False,
                              usehtmlattrs=False,
                              with_header_hide=[],
                              without_header_hide=["linebelowheader"]),
                  "pipe":
                  TableFormat(lineabove=None,
                              linebelowheader=Line("|", "-", "|", "|"),
                              linebetweenrows=None,
                              linebelow=None,
                              headerrow=DataRow("|", "|", "|"),
                              datarow=DataRow("|", "|", "|"),
                              padding=1,
                              usecolons=True,
                              usehtmlattrs=False,
                              with_header_hide=[],
                              without_header_hide=[]),
                  "orgtbl":
                  TableFormat(lineabove=None,
                              linebelowheader=Line("|", "-", "+", "|"),
                              linebetweenrows=None,
                              linebelow=None,
                              headerrow=DataRow("|", "|", "|"),
                              datarow=DataRow("|", "|", "|"),
                              padding=1,
                              usecolons=False,
                              usehtmlattrs=False,
                              with_header_hide=[],
                              without_header_hide=["linebelowheader"]),
                  "rst":
                  TableFormat(lineabove=Line("", "=", "  ", ""),
                              linebelowheader=Line("", "=", "  ", ""),
                              linebetweenrows=None,
                              linebelow=Line("", "=", "  ", ""),
                              headerrow=DataRow("", "  ", ""),
                              datarow=DataRow("", "  ", ""),
                              padding=0,
                              usecolons=False,
                              usehtmlattrs=False,
                              with_header_hide=[],
                              without_header_hide=["linebelowheader"]),
                  "mediawiki":
                  TableFormat(lineabove=Line("{| class=\"wikitable\" style=\"text-align: left;\"",
                                             "", "", "\n|+ <!-- caption -->\n|-"),
                              linebelowheader=Line("|-", "", "", ""),
                              linebetweenrows=Line("|-", "", "", ""),
                              linebelow=Line("|}", "", "", ""),
                              headerrow=DataRow("!", "!!", ""),
                              datarow=DataRow("|", "||", ""),
                              padding=1,
                              usecolons=False,
                              usehtmlattrs=True,
                              with_header_hide=[],
                              without_header_hide=["linebelowheader"])}


_invisible_codes = re.compile("\x1b\[\d*m")  # ANSI color codes


def simple_separated_format(separator):
    """Construct a simple TableFormat with columns separated by a separator.

    >>> tsv = simple_separated_format("\t") ; \
        tabulate([["foo", 1], ["spam", 23]], tablefmt=tsv) == u'foo \\t 1\\nspam\\t23'
    True

    """
    return TableFormat(None, None, None, None,
                       headerrow=None, datarow=DataRow('', '\t', ''),
                       padding=_format_defaults["padding"],
                       usecolons=_format_defaults["usecolons"],
                       usehtmlattrs=_format_defaults["usehtmlattrs"],
                       with_header_hide=_format_defaults["with_header_hide"],
                       without_header_hide=_format_defaults["without_header_hide"])


def _isconvertible(conv, string):
    try:
        n = conv(string)
        return True
    except ValueError:
        return False


def _isnumber(string):
    """
    >>> _isnumber("123.45")
    True
    >>> _isnumber("123")
    True
    >>> _isnumber("spam")
    False
    """
    return _isconvertible(float, string)


def _isint(string):
    """
    >>> _isint("123")
    True
    >>> _isint("123.45")
    False
    """
    return type(string) is int or \
           (isinstance(string, _binary_type) or isinstance(string, _text_type)) and \
           _isconvertible(int, string)


def _type(string, has_invisible=True):
    """The least generic type (type(None), int, float, str, unicode).

    >>> _type(None) is type(None)
    True
    >>> _type("foo") is type("")
    True
    >>> _type("1") is type(1)
    True
    >>> _type(u'\x1b[31m42\x1b[0m') is type(42)
    True
    >>> _type('\x1b[31m42\x1b[0m') is type(42)
    True

    """

    if has_invisible and \
       (isinstance(string, _text_type) or isinstance(string, _binary_type)):
        string = _strip_invisible(string)

    if string is None:
        return _none_type
    elif _isint(string):
        return int
    elif _isnumber(string):
        return float
    elif isinstance(string, _binary_type):
        return _binary_type
    else:
        return _text_type


def _afterpoint(string):
    """Symbols after a decimal point, -1 if the string lacks the decimal point.

    >>> _afterpoint("123.45")
    2
    >>> _afterpoint("1001")
    -1
    >>> _afterpoint("eggs")
    -1
    >>> _afterpoint("123e45")
    2

    """
    if _isnumber(string):
        if _isint(string):
            return -1
        else:
            pos = string.rfind(".")
            pos = string.lower().rfind("e") if pos < 0 else pos
            if pos >= 0:
                return len(string) - pos - 1
            else:
                return -1  # no point
    else:
        return -1  # not a number


def _padleft(width, s, has_invisible=True):
    """Flush right.

    >>> _padleft(6, u'\u044f\u0439\u0446\u0430') == u'  \u044f\u0439\u0446\u0430'
    True

    """
    iwidth = width + len(s) - len(_strip_invisible(s)) if has_invisible else width
    fmt = u"{0:>%ds}" % iwidth
    return fmt.format(s)


def _padright(width, s, has_invisible=True):
    """Flush left.

    >>> _padright(6, u'\u044f\u0439\u0446\u0430') == u'\u044f\u0439\u0446\u0430  '
    True

    """
    iwidth = width + len(s) - len(_strip_invisible(s)) if has_invisible else width
    fmt = u"{0:<%ds}" % iwidth
    return fmt.format(s)


def _padboth(width, s, has_invisible=True):
    """Center string.

    >>> _padboth(6, u'\u044f\u0439\u0446\u0430') == u' \u044f\u0439\u0446\u0430 '
    True

    """
    iwidth = width + len(s) - len(_strip_invisible(s)) if has_invisible else width
    fmt = u"{0:^%ds}" % iwidth
    return fmt.format(s)


def _strip_invisible(s):
    "Remove invisible ANSI color codes."
    return re.sub(_invisible_codes, "", s)


def _visible_width(s):
    """Visible width of a printed string. ANSI color codes are removed.

    >>> _visible_width('\x1b[31mhello\x1b[0m'), _visible_width("world")
    (5, 5)

    """
    if isinstance(s, _text_type) or isinstance(s, _binary_type):
        return len(_strip_invisible(s))
    else:
        return len(_text_type(s))


def _align_column(strings, alignment, minwidth=0, has_invisible=True):
    """[string] -> [padded_string]

    >>> list(map(str,_align_column(["12.345", "-1234.5", "1.23", "1234.5", "1e+234", "1.0e234"], "decimal")))
    ['   12.345  ', '-1234.5    ', '    1.23   ', ' 1234.5    ', '    1e+234 ', '    1.0e234']

    """
    if alignment == "right":
        strings = [s.strip() for s in strings]
        padfn = _padleft
    elif alignment in "center":
        strings = [s.strip() for s in strings]
        padfn = _padboth
    elif alignment in "decimal":
        decimals = [_afterpoint(s) for s in strings]
        maxdecimals = max(decimals)
        strings = [s + (maxdecimals - decs) * " "
                   for s, decs in zip(strings, decimals)]
        padfn = _padleft
    else:
        strings = [s.strip() for s in strings]
        padfn = _padright

    if has_invisible:
        width_fn = _visible_width
    else:
        width_fn = len

    maxwidth = max(max(map(width_fn, strings)), minwidth)
    padded_strings = [padfn(maxwidth, s, has_invisible) for s in strings]
    return padded_strings


def _more_generic(type1, type2):
    types = { _none_type: 0, int: 1, float: 2, _text_type: 4 }
    invtypes = { 4: _text_type, 2: float, 1: int, 0: _none_type }
    moregeneric = max(types.get(type1, 4), types.get(type2, 4))
    return invtypes[moregeneric]


def _column_type(strings, has_invisible=True):
    """The least generic type all column values are convertible to.

    >>> _column_type(["1", "2"]) is _int_type
    True
    >>> _column_type(["1", "2.3"]) is _float_type
    True
    >>> _column_type(["1", "2.3", "four"]) is _text_type
    True
    >>> _column_type(["four", u'\u043f\u044f\u0442\u044c']) is _text_type
    True
    >>> _column_type([None, "brux"]) is _text_type
    True
    >>> _column_type([1, 2, None]) is _int_type
    True

    """
    types = [_type(s, has_invisible) for s in strings ]
    return reduce(_more_generic, types, int)


def _format(val, valtype, floatfmt, missingval=u""):
    """Format a value accoding to its type.

    Unicode is supported:

    >>> hrow = [u'\u0431\u0443\u043a\u0432\u0430', u'\u0446\u0438\u0444\u0440\u0430'] ; \
        tbl = [[u'\u0430\u0437', 2], [u'\u0431\u0443\u043a\u0438', 4]] ; \
        good_result = u'\\u0431\\u0443\\u043a\\u0432\\u0430      \\u0446\\u0438\\u0444\\u0440\\u0430\\n-------  -------\\n\\u0430\\u0437             2\\n\\u0431\\u0443\\u043a\\u0438           4' ; \
        tabulate(tbl, headers=hrow) == good_result
    True

    """
    if val is None:
        return missingval

    if valtype in [int, _binary_type, _text_type]:
        return u"{0}".format(__text_type(val))
    elif valtype is float:
        return format(float(val), floatfmt)
    else:
        return u"{0}".format(val)


def _align_header(header, alignment, width):
    if alignment == "left":
        return _padright(width, header)
    elif alignment == "center":
        return _padboth(width, header)
    else:
        return _padleft(width, header)


def _normalize_tabular_data(tabular_data, headers):
    """Transform a supported data type to a list of lists, and a list of headers.

    Supported tabular data types:

    * list-of-lists or another iterable of iterables

    * 2D NumPy arrays

    * dict of iterables (usually used with headers="keys")

    * pandas.DataFrame (usually used with headers="keys")

    The first row can be used as headers if headers="firstrow",
    column indices can be used as headers if headers="keys".

    """

    if hasattr(tabular_data, "keys") and hasattr(tabular_data, "values"):
        # dict-like and pandas.DataFrame?
        if hasattr(tabular_data.values, "__call__"):
            # likely a conventional dict
            keys = tabular_data.keys()
            rows = list(izip_longest(*tabular_data.values()))  # columns have to be transposed
        elif hasattr(tabular_data, "index"):
            # values is a property, has .index => it's likely a pandas.DataFrame (pandas 0.11.0)
            keys = tabular_data.keys()
            vals = tabular_data.values  # values matrix doesn't need to be transposed
            names = tabular_data.index
            rows = [[v]+list(row) for v,row in zip(names, vals)]
        else:
            raise ValueError("tabular data doesn't appear to be a dict or a DataFrame")

        if headers == "keys":
            headers = list(map(_text_type,keys))  # headers should be strings

    else:  # it's a usual an iterable of iterables, or a NumPy array
        rows = list(tabular_data)

        if headers == "keys" and len(rows) > 0:  # keys are column indices
            headers = list(map(_text_type, range(len(rows[0]))))

    # take headers from the first row if necessary
    if headers == "firstrow" and len(rows) > 0:
        headers = list(map(_text_type, rows[0])) # headers should be strings
        rows = rows[1:]

    headers = list(headers)
    rows = list(map(list,rows))

    # pad with empty headers for initial columns if necessary
    if headers and len(rows) > 0:
       nhs = len(headers)
       ncols = len(rows[0])
       if nhs < ncols:
           headers = [u""]*(ncols - nhs) + headers

    return rows, headers


def tabulate(tabular_data, headers=[], tablefmt="simple",
             floatfmt="g", numalign="decimal", stralign="left",
             missingval=u""):
    """Format a fixed width table for pretty printing.

    >>> print(tabulate([[1, 2.34], [-56, "8.999"], ["2", "10001"]]))
    ---  ---------
      1      2.34
    -56      8.999
      2  10001
    ---  ---------

    The first required argument (`tabular_data`) can be a
    list-of-lists (or another iterable or iterables), a dictionary of
    iterables, a two-dimensional NumPy array, or a Pandas' dataframe.


    Table headers
    -------------

    To print nice column headers, supply the second argument (`headers`):

      - `headers` can be an explicit list of column headers
      - if `headers="firstrow"`, then the first row of data is used
      - if `headers="keys"`, then dictionary keys or column indices are used

    Otherwise a headerless table is produced.

    If the number of headers is less than the number of columns, they
    are supposed to be names of the last columns. This is consistent
    with the plain-text format of R and Pandas' dataframes.

    >>> print(tabulate([["sex","age"],["Alice","F",24],["Bob","M",19]],
    ...       headers="firstrow"))
           sex      age
    -----  -----  -----
    Alice  F         24
    Bob    M         19


    Column alignment
    ----------------

    `tabulate` tries to detect column types automatically, and aligns
    the values properly. By default it aligns decimal points of the
    numbers (or flushes integer numbers to the right), and flushes
    everything else to the left. Possible column alignments
    (`numalign`, `stralign`) are: right, center, left, decimal (only
    for `numalign`).


    Table formats
    -------------

    `floatfmt` is a format specification used for columns which
    contain numeric data with a decimal point.

    `None` values are replaced with a `missingval` string:

    >>> print(tabulate([["spam", 1, None],
    ...                 ["eggs", 42, 3.14],
    ...                 ["other", None, 2.7]], missingval="?"))
    -----  --  ----
    spam    1  ?
    eggs   42  3.14
    other   ?  2.7
    -----  --  ----

    Various plain-text table formats (`tablefmt`) are supported:
    'plain', 'simple', 'grid', 'pipe', 'orgtbl', 'rst', and 'mediawiki'.

    "plain" format doesn't use any pseudographics to draw tables,
    it separates columns with a double space:

    >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
    ...                 ["strings", "numbers"], "plain"))
    strings      numbers
    spam         41.9999
    eggs        451

    >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="plain"))
    spam   41.9999
    eggs  451

    "simple" format is like Pandoc simple_tables:

    >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
    ...                 ["strings", "numbers"], "simple"))
    strings      numbers
    ---------  ---------
    spam         41.9999
    eggs        451

    >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="simple"))
    ----  --------
    spam   41.9999
    eggs  451
    ----  --------

    "grid" is similar to tables produced by Emacs table.el package or
    Pandoc grid_tables:

    >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
    ...                ["strings", "numbers"], "grid"))
    +-----------+-----------+
    | strings   |   numbers |
    +===========+===========+
    | spam      |   41.9999 |
    +-----------+-----------+
    | eggs      |  451      |
    +-----------+-----------+

    >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="grid"))
    +------+----------+
    | spam |  41.9999 |
    +------+----------+
    | eggs | 451      |
    +------+----------+

    "pipe" is like tables in PHP Markdown Extra extension or Pandoc
    pipe_tables:

    >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
    ...                ["strings", "numbers"], "pipe"))
    | strings   |   numbers |
    |:----------|----------:|
    | spam      |   41.9999 |
    | eggs      |  451      |

    >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="pipe"))
    |:-----|---------:|
    | spam |  41.9999 |
    | eggs | 451      |

    "orgtbl" is like tables in Emacs org-mode and orgtbl-mode. They
    are slightly different from "pipe" format by not using colons to
    define column alignment, and using a "+" sign to indicate line
    intersections:

    >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
    ...                ["strings", "numbers"], "orgtbl"))
    | strings   |   numbers |
    |-----------+-----------|
    | spam      |   41.9999 |
    | eggs      |  451      |


    >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="orgtbl"))
    | spam |  41.9999 |
    | eggs | 451      |

    "rst" is like a simple table format from reStructuredText; please
    note that reStructuredText accepts also "grid" tables:

    >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
    ...                ["strings", "numbers"], "rst"))
    =========  =========
    strings      numbers
    =========  =========
    spam         41.9999
    eggs        451
    =========  =========

    >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="rst"))
    ====  ========
    spam   41.9999
    eggs  451
    ====  ========

    "mediawiki" produces a table markup used in Wikipedia and on other
    MediaWiki-based sites:

    >>> print(tabulate([["strings", "numbers"], ["spam", 41.9999], ["eggs", "451.0"]],
    ...                headers="firstrow", tablefmt="mediawiki"))
    {| class="wikitable" style="text-align: left;"
    |+ <!-- caption -->
    |-
    ! strings   !! align="right"|   numbers
    |-
    | spam      || align="right"|   41.9999
    |-
    | eggs      || align="right"|  451
    |}

    >>> print(tabulate([["eggs", 42], ["spam", 23]], tablefmt="mediawiki", stralign="left"))
    {| class="wikitable" style="text-align: left;"
    |+ <!-- caption -->
    |-
    | eggs || align="right"| 42
    |-
    | spam || align="right"| 23
    |}


    """

    list_of_lists, headers = _normalize_tabular_data(tabular_data, headers)

    # optimization: look for ANSI control codes once,
    # enable smart width functions only if a control code is found
    plain_text = u'\n'.join(['\t'.join(map(_text_type, headers))] + \
                            [u'\t'.join(map(__text_type, row)) for row in list_of_lists])
    has_invisible = re.search(_invisible_codes, plain_text)
    if has_invisible:
        width_fn = _visible_width
    else:
        width_fn = len

    # format rows and columns, convert numeric values to strings
    cols = list(zip(*list_of_lists))
    coltypes = list(map(_column_type, cols))
    cols = [[_format(v, ct, floatfmt, missingval) for v in c]
             for c,ct in zip(cols, coltypes)]

    # align columns
    aligns = [numalign if ct in [int,float] else stralign for ct in coltypes]
    minwidths = [width_fn(h)+2 for h in headers] if headers else [0]*len(cols)
    cols = [_align_column(c, a, minw, has_invisible)
            for c, a, minw in zip(cols, aligns, minwidths)]

    if headers:
        # align headers and add headers
        minwidths = [max(minw, width_fn(c[0])) for minw, c in zip(minwidths, cols)]
        headers = [_align_header(h, a, minw)
                   for h, a, minw in zip(headers, aligns, minwidths)]
        rows = list(zip(*cols))
    else:
        minwidths = [width_fn(c[0]) for c in cols]
        rows = list(zip(*cols))

    if not isinstance(tablefmt, TableFormat):
        tablefmt = _table_formats.get(tablefmt, _table_formats["simple"])

    return _format_table(tablefmt, headers, rows, minwidths, aligns)


def _build_row(cells, padding, begin, sep, end):
    "Return a string which represents a row of data cells."
    pad = u" "*padding
    padded_cells = [pad + cell + pad for cell in cells]
    return (begin + sep.join(padded_cells) + end).rstrip()


def _build_line(colwidths, padding, begin, fill, sep,  end):
    "Return a string which represents a horizontal line."
    cells = [fill*(w + 2*padding) for w in colwidths]
    return _build_row(cells, 0, begin, sep, end)


def _mediawiki_cell_attrs(row, colaligns):
    "Prefix every cell in a row with an HTML alignment attribute."
    alignment = { "left":    '',
                  "right":   'align="right"| ',
                  "center":  'align="center"| ',
                  "decimal": 'align="right"| ' }
    row2 = [alignment[a] + c for c, a in zip(row, colaligns)]
    return row2


def _line_segment_with_colons(linefmt, align, colwidth):
    """Return a segment of a horizontal line with optional colons which
    indicate column's alignment (as in `pipe` output format)."""
    fill = linefmt.hline
    w = colwidth
    if align in ["right", "decimal"]:
        return (fill[0] * (w - 1)) + ":"
    elif align == "center":
        return ":" + (fill[0] * (w - 2)) + ":"
    elif align == "left":
        return ":" + (fill[0] * (w - 1))
    else:
        return fill[0] * w


def _format_table(fmt, headers, rows, colwidths, colaligns):
    """Produce a plain-text representation of the table."""
    lines = []
    hidden = fmt.with_header_hide if headers else fmt.without_header_hide
    pad = fmt.padding
    headerrow = fmt.headerrow if fmt.headerrow else fmt.datarow

    if fmt.usehtmlattrs:
        headers = _mediawiki_cell_attrs(headers, colaligns)
        rows = [_mediawiki_cell_attrs(row, colaligns) for row in rows]

    if fmt.lineabove and "lineabove" not in hidden:
        lines.append(_build_line(colwidths, pad, *fmt.lineabove))

    if headers:
        lines.append(_build_row(headers, pad, *headerrow))

    if fmt.linebelowheader and "linebelowheader" not in hidden:
        begin, fill, sep, end = fmt.linebelowheader
        if fmt.usecolons:
            segs = [_line_segment_with_colons(fmt.linebelowheader, a, w + 2*pad)
                    for w,a in zip(colwidths, colaligns)]
            lines.append(_build_row(segs, 0, begin, sep, end))
        else:
            lines.append(_build_line(colwidths, pad, *fmt.linebelowheader))

    if rows and fmt.linebetweenrows and "linebetweenrows" not in hidden:
        # initial rows with a line below
        for row in rows[:-1]:
            lines.append(_build_row(row, pad, *fmt.datarow))
            lines.append(_build_line(colwidths, pad, *fmt.linebetweenrows))
        # the last row without a line below
        lines.append(_build_row(rows[-1], pad, *fmt.datarow))
    else:
        for row in rows:
            lines.append(_build_row(row, pad, *fmt.datarow))

    if fmt.linebelow and "linebelow" not in hidden:
        lines.append(_build_line(colwidths, pad, *fmt.linebelow))

    return "\n".join(lines)
  0707010000bc8f000081a4000000000000000000000001550eba8900000014000000660000000300000000000000000000002000000000root/opt/opensvc/lib/version.py   version = "1.6-222"
0707010000bc90000081a400000000000000000000000154fc7bfb0000b756000000660000000300000000000000000000001c00000000root/opt/opensvc/lib/wmi.py   """
Windows Management Instrumentation (WMI) is Microsoft's answer to
the DMTF's Common Information Model. It allows you to query just
about any conceivable piece of information from any computer which
is running the necessary agent and over which have you the
necessary authority.

Since the COM implementation doesn't give much away to Python
programmers, I've wrapped it in some lightweight classes with
some getattr / setattr magic to ease the way. In particular:

* The :class:`_wmi_namespace` object itself will determine its classes
  and allow you to return all instances of any of them by
  using its name as an attribute::

    disks = wmi.WMI ().Win32_LogicalDisk ()

* In addition, you can specify what would become the WHERE clause
  as keyword parameters::

    fixed_disks = wmi.WMI ().Win32_LogicalDisk (DriveType=3)

* The objects returned by a WMI lookup are wrapped in a Python
  class which determines their methods and classes and allows
  you to access them as though they were Python classes. The
  methods only allow named parameters::

    for p in wmi.WMI ().Win32_Process (Name="notepad.exe"):
      p.Terminate (Result=1)

* Doing a print() on one of the WMI objects will result in its
  `GetObjectText\_` method being called, which usually produces
  a meaningful printout of current values.
  The repr of the object will include its full WMI path,
  which lets you get directly to it if you need to.

* You can get the associators and references of an object as
  a list of python objects by calling the associators () and
  references () methods on a WMI Python object::

    for p in wmi.WMI ().Win32_Process (Name="notepad.exe"):
      for r in p.references ():
        print(r)

  ..  note::
      Don't do this on a Win32_ComputerSystem object; it will
      take all day and kill your machine!


* WMI classes (as opposed to instances) are first-class
  objects, so you can get hold of a class, and call
  its methods or set up a watch against it::

    process = wmi.WMI ().Win32_Process
    process.Create (CommandLine="notepad.exe")

* To make it easier to use in embedded systems and py2exe-style
  executable wrappers, the module will not force early Dispatch.
  To do this, it uses a handy hack by Thomas Heller for easy access
  to constants.

Typical usage will be::

  import wmi

  vodev1 = wmi.WMI ("vodev1")
  for disk in vodev1.Win32_LogicalDisk ():
    if disk.DriveType == 3:
      space = 100 * long (disk.FreeSpace) / long (disk.Size)
      print("%s has %d%% free" % (disk.Name, space))

Many thanks, obviously to Mark Hammond for creating the win32all
extensions, but also to Alex Martelli and Roger Upole, whose
c.l.py postings pointed me in the right direction.
Thanks especially in release 1.2 to Paul Tiemann for his code
contributions and robust testing.
"""
__VERSION__ = __version__ = "1.4.9"

_DEBUG = False

import sys
import datetime
import re
import struct
import warnings

from win32com.client import GetObject, Dispatch
import pywintypes

def signed_to_unsigned (signed):
  """Convert a (possibly signed) long to unsigned hex. Useful
  when converting a COM error code to the more conventional
  8-digit hex::

    print("%08X" % signed_to_unsigned (-2147023174))
  """
  unsigned, = struct.unpack ("L", struct.pack ("l", signed))
  return unsigned

class SelfDeprecatingDict (object):
  """Provides for graceful degradation of objects which
  are currently dictionaries (and therefore accessed via
  `.keys`, `.items`, etc.) into lists. Wraps an existing
  `dict` and allows it to be addressed as a `dict` or as a
  `list` during an interregnum, issuing a `DeprecationWarning`
  if accessed as a `dict`.
  """

  dict_only = set (dir (dict)).difference (dir (list))

  def __init__ (self, dictlike):
    self.dict = dict (dictlike)
    self.list = list (self.dict)

  def __getattr__ (self, attribute):
    if attribute in self.dict_only:
      warnings.warn ("In future this will be a list and not a dictionary", DeprecationWarning)
      return getattr (self.dict, attribute)
    else:
      return getattr (self.list, attribute)

  def __iter__ (self):
    return iter (self.list)

  def __str__ (self):
    return str (self.list)

  def __repr__ (self):
    return repr (self.list)

  def __getitem__ (self, item):
    try:
      return self.list[item]
    except TypeError:
      return self.dict[item]

class ProvideConstants (object):
  """When called on a ``win32com.client.Dispatch`` object,
  provides lazy access to constants defined in the typelib.
  They can then be accessed as attributes of the :attr:`_constants`
  property. (From Thomas Heller on c.l.py).
  """
  def __init__(self, comobj):
    comobj.__dict__["_constants"] = self
    self.__typecomp = \
    comobj._oleobj_.GetTypeInfo().GetContainingTypeLib()[0].GetTypeComp()

  def __getattr__(self, name):
    if name.startswith("__") and name.endswith("__"):
     raise AttributeError (name)
    result = self.__typecomp.Bind(name)
    if not result[0]:
     raise AttributeError (name)
    return result[1].value

obj = GetObject ("winmgmts:")
ProvideConstants (obj)

wbemErrInvalidQuery = obj._constants.wbemErrInvalidQuery
wbemErrTimedout = obj._constants.wbemErrTimedout
wbemFlagReturnImmediately = obj._constants.wbemFlagReturnImmediately
wbemFlagForwardOnly = obj._constants.wbemFlagForwardOnly

#
# Exceptions
#
class x_wmi (Exception):
  """Ancestor of all wmi-related exceptions. Keeps track of
  an info message and the underlying COM error if any, exposed
  as the :attr:`com_error` attribute.
  """
  def __init__ (self, info="", com_error=None):
    self.info = info
    self.com_error = com_error

  def __str__ (self):
    return "<x_wmi: %s %s>" % (
      self.info or "Unexpected COM Error",
      self.com_error or "(no underlying exception)"
    )

class x_wmi_invalid_query (x_wmi):
  "Raised when a WMI returns `wbemErrInvalidQuery`"
  pass

class x_wmi_timed_out (x_wmi):
  "Raised when a watcher times out"
  pass

class x_wmi_no_namespace (x_wmi):
  """Raised when an attempt is made to query or watch
  from a class without a namespace.
  """
  pass

class x_access_denied (x_wmi):
  "Raised when WMI raises 80070005"
  pass

class x_wmi_authentication (x_wmi):
  "Raised when an invalid combination of authentication properties is attempted when connecting"
  pass

class x_wmi_uninitialised_thread (x_wmi):
  """Raised when WMI returns 800401E4 on connection, usually
  indicating that no COM threading model has been initialised
  """
  pass

WMI_EXCEPTIONS = {
  signed_to_unsigned (wbemErrInvalidQuery) : x_wmi_invalid_query,
  signed_to_unsigned (wbemErrTimedout) : x_wmi_timed_out,
  0x80070005 : x_access_denied,
  0x80041003 : x_access_denied,
  0x800401E4 : x_wmi_uninitialised_thread,
}

def handle_com_error (err=None):
  """Convenience wrapper for displaying all manner of COM errors.
  Raises a :exc:`x_wmi` exception with more useful information attached

  :param err: The structure attached to a `pywintypes.com_error`
  """
  if err is None:
    _, err, _ = sys.exc_info ()
  hresult_code, hresult_name, additional_info, parameter_in_error = err.args
  hresult_code = signed_to_unsigned (hresult_code)
  exception_string = ["%s - %s" % (hex (hresult_code), hresult_name)]
  scode = None
  if additional_info:
    wcode, source_of_error, error_description, whlp_file, whlp_context, scode = additional_info
    scode = signed_to_unsigned (scode)
    exception_string.append ("  Error in: %s" % source_of_error)
    exception_string.append ("  %s - %s" % (hex (scode), (error_description or "").strip ()))
  for error_code, klass in WMI_EXCEPTIONS.items ():
    if error_code in (hresult_code, scode):
      break
  else:
    klass = x_wmi
  raise klass (com_error=err)


BASE = datetime.datetime (1601, 1, 1)
def from_1601 (ns100):
  return BASE + datetime.timedelta (microseconds=int (ns100) / 10)

def from_time (year=None, month=None, day=None, hours=None, minutes=None, seconds=None, microseconds=None, timezone=None):
  """Convenience wrapper to take a series of date/time elements and return a WMI time
  of the form `yyyymmddHHMMSS.mmmmmm+UUU`. All elements may be int, string or
  omitted altogether. If omitted, they will be replaced in the output string
  by a series of stars of the appropriate length.

  :param year: The year element of the date/time
  :param month: The month element of the date/time
  :param day: The day element of the date/time
  :param hours: The hours element of the date/time
  :param minutes: The minutes element of the date/time
  :param seconds: The seconds element of the date/time
  :param microseconds: The microseconds element of the date/time
  :param timezone: The timeezone element of the date/time

  :returns: A WMI datetime string of the form: `yyyymmddHHMMSS.mmmmmm+UUU`
  """
  def str_or_stars (i, length):
    if i is None:
      return "*" * length
    else:
      return str (i).rjust (length, "0")

  wmi_time = ""
  wmi_time += str_or_stars (year, 4)
  wmi_time += str_or_stars (month, 2)
  wmi_time += str_or_stars (day, 2)
  wmi_time += str_or_stars (hours, 2)
  wmi_time += str_or_stars (minutes, 2)
  wmi_time += str_or_stars (seconds, 2)
  wmi_time += "."
  wmi_time += str_or_stars (microseconds, 6)
  if timezone >= 0:
    wmi_time += "+"
  else:
    wmi_time += "-"
    timezone = abs (timezone)
  wmi_time += str_or_stars (timezone, 3)

  return wmi_time

def to_time (wmi_time):
  """Convenience wrapper to take a WMI datetime string of the form
  yyyymmddHHMMSS.mmmmmm+UUU and return a 9-tuple containing the
  individual elements, or None where string contains placeholder
  stars.

  :param wmi_time: The WMI datetime string in `yyyymmddHHMMSS.mmmmmm+UUU` format

  :returns: A 9-tuple of (year, month, day, hours, minutes, seconds, microseconds, timezone)
  """
  def int_or_none (s, start, end):
    try:
      return int (s[start:end])
    except ValueError:
      return None

  year = int_or_none (wmi_time, 0, 4)
  month = int_or_none (wmi_time, 4, 6)
  day = int_or_none (wmi_time, 6, 8)
  hours = int_or_none (wmi_time, 8, 10)
  minutes = int_or_none (wmi_time, 10, 12)
  seconds = int_or_none (wmi_time, 12, 14)
  microseconds = int_or_none (wmi_time, 15, 21)
  timezone = wmi_time[22:]
  if timezone == "***":
    timezone = None

  return year, month, day, hours, minutes, seconds, microseconds, timezone

def _set (obj, attribute, value):
  """Helper function to add an attribute directly into the instance
  dictionary, bypassing possible `__getattr__` calls

  :param obj: Any python object
  :param attribute: String containing attribute name
  :param value: Any python object
  """
  obj.__dict__[attribute] = value

class _wmi_method:
  """A currying sort of wrapper around a WMI method name. It
  abstract's the method's parameters and can be called like
  a normal Python object passing in the parameter values.

  Output parameters are returned from the call as a tuple.
  In addition, the docstring is set up as the method's
  signature, including an indication as to whether any
  given parameter is expecting an array, and what
  special privileges are required to call the method.
  """

  def __init__ (self, ole_object, method_name):
    """
    :param ole_object: The WMI class/instance whose method is to be called
    :param method_name: The name of the method to be called
    """
    try:
      self.ole_object = Dispatch (ole_object)
      self.method = ole_object.Methods_ (method_name)
      self.qualifiers = {}
      for q in self.method.Qualifiers_:
        self.qualifiers[q.Name] = q.Value
      self.provenance = "\n".join (self.qualifiers.get ("MappingStrings", []))

      self.in_parameters = self.method.InParameters
      self.out_parameters = self.method.OutParameters
      if self.in_parameters is None:
        self.in_parameter_names = []
      else:
        self.in_parameter_names = [(i.Name, i.IsArray) for i in self.in_parameters.Properties_]
      if self.out_parameters is None:
        self.out_parameter_names = []
      else:
        self.out_parameter_names = [(i.Name, i.IsArray) for i in self.out_parameters.Properties_]

      doc = "%s (%s) => (%s)" % (
        method_name,
        ", ".join ([name + ("", "[]")[is_array] for (name, is_array) in self.in_parameter_names]),
        ", ".join ([name + ("", "[]")[is_array] for (name, is_array) in self.out_parameter_names])
      )
      privileges = self.qualifiers.get ("Privileges", [])
      if privileges:
        doc += " | Needs: " + ", ".join (privileges)
      self.__doc__ = doc
    except pywintypes.com_error:
      handle_com_error ()

  def __call__ (self, *args, **kwargs):
    """Execute the call to a WMI method, returning
    a tuple (even if is of only one value) containing
    the out and return parameters.
    """
    try:
      if self.in_parameters:
        parameter_names = {}
        for name, is_array in self.in_parameter_names:
          parameter_names[name] = is_array

        parameters = self.in_parameters

        #
        # Check positional parameters first
        #
        for n_arg in range (len (args)):
          arg = args[n_arg]
          parameter = parameters.Properties_[n_arg]
          if parameter.IsArray:
            try: list (arg)
            except TypeError: raise TypeError ("parameter %d must be iterable" % n_arg)
          parameter.Value = arg

        #
        # If any keyword param supersedes a positional one,
        # it'll simply overwrite it.
        #
        for k, v in kwargs.items ():
          is_array = parameter_names.get (k)
          if is_array is None:
            raise AttributeError ("%s is not a valid parameter for %s" % (k, self.__doc__))
          else:
            if is_array:
              try: list (v)
              except TypeError: raise TypeError ("%s must be iterable" % k)
          parameters.Properties_ (k).Value = v

        result = self.ole_object.ExecMethod_ (self.method.Name, self.in_parameters)
      else:
        result = self.ole_object.ExecMethod_ (self.method.Name)

      results = []
      for name, is_array in self.out_parameter_names:
        value = result.Properties_ (name).Value
        if is_array:
          #
          # Thanks to Jonas Bjering for bug report and patch
          #
          results.append (list (value or []))
        else:
          results.append (value)
      return tuple (results)

    except pywintypes.com_error:
      handle_com_error ()

  def __repr__ (self):
    return "<function %s>" % self.__doc__

class _wmi_property (object):

  def __init__ (self, property):
    self.property = property
    self.name = property.Name
    self.value = property.Value
    self.qualifiers = dict ((q.Name, q.Value) for q in property.Qualifiers_)
    self.type = self.qualifiers.get ("CIMTYPE", None)

  def set (self, value):
    self.property.Value = value

  def __repr__ (self):
    return "<wmi_property: %s>" % self.name

  def __getattr__ (self, attr):
    return getattr (self.property, attr)

#
# class _wmi_object
#
class _wmi_object:
  """The heart of the WMI module: wraps the objects returned by COM
  ISWbemObject interface and provide readier access to their properties
  and methods resulting in a more Pythonic interface. Not usually
  instantiated directly, rather as a result of calling a :class:`_wmi_class`
  on the parent :class:`_wmi_namespace`.

  If you get hold of a WMI-related COM object from some other
  source than this module, you can wrap it in one of these objects
  to get the benefits of the module::

    import win32com.client
    import wmi

    wmiobj = win32com.client.GetObject ("winmgmts:Win32_LogicalDisk.DeviceID='C:'")
    c_drive = wmi._wmi_object (wmiobj)
    print(c_drive)
  """

  def __init__ (self, ole_object, instance_of=None, fields=[], property_map={}):
    try:
      _set (self, "ole_object", ole_object)
      _set (self, "id", ole_object.Path_.DisplayName.lower ())
      _set (self, "_instance_of", instance_of)
      _set (self, "properties", {})
      _set (self, "methods", {})
      _set (self, "property_map", property_map)
      _set (self, "_associated_classes", None)
      _set (self, "_keys", None)

      if fields:
        for field in fields:
          self.properties[field] = None
      else:
        for p in ole_object.Properties_:
          self.properties[p.Name] = None

      for m in ole_object.Methods_:
        self.methods[m.Name] = None

      _set (self, "_properties", self.properties.keys ())
      _set (self, "_methods", self.methods.keys ())
      _set (self, "qualifiers", dict ((q.Name, q.Value) for q in self.ole_object.Qualifiers_))

    except pywintypes.com_error:
      handle_com_error ()

  def __lt__ (self, other):
    return self.id < other.id

  def __str__ (self):
    """For a call to print([object]) return the OLE description
    of the properties / values of the object
    """
    try:
      return self.ole_object.GetObjectText_ ()
    except pywintypes.com_error:
      handle_com_error ()

  def __repr__ (self):
    """
    Indicate both the fact that this is a wrapped WMI object
    and the WMI object's own identifying class.
    """
    try:
      return "<%s: %s>" % (self.__class__.__name__, self.Path_.Path.encode ("ascii", "backslashreplace"))
    except pywintypes.com_error:
      handle_com_error ()

  def _cached_properties (self, attribute):
    if self.properties[attribute] is None:
      self.properties[attribute] = _wmi_property (self.ole_object.Properties_ (attribute))
    return self.properties[attribute]

  def _cached_methods (self, attribute):
    if self.methods[attribute] is None:
      self.methods[attribute] = _wmi_method (self.ole_object, attribute)
    return self.methods[attribute]

  def __getattr__ (self, attribute):
    """
    Attempt to pass attribute calls to the proxied COM object.
    If the attribute is recognised as a property, return its value;
    if it is recognised as a method, return a method wrapper which
    can then be called with parameters; otherwise pass the lookup
    on to the underlying object.
    """
    try:
      if attribute in self.properties:
        property = self._cached_properties (attribute)
        factory = self.property_map.get (attribute, self.property_map.get (property.type, lambda x: x))
        value = factory (property.value)
        #
        # If this is an association, certain of its properties
        # are actually the paths to the aspects of the association,
        # so translate them automatically into WMI objects.
        #
        if property.type.startswith ("ref:"):
          return WMI (moniker=value)
        else:
          return value
      elif attribute in self.methods:
        return self._cached_methods (attribute)
      else:
        return getattr (self.ole_object, attribute)
    except pywintypes.com_error:
      handle_com_error ()

  def __setattr__ (self, attribute, value):
    """If the attribute to be set is valid for the proxied
    COM object, set that objects's parameter value; if not,
    raise an exception.
    """
    try:
      if attribute in self.properties:
        self._cached_properties (attribute).set (value)
        if self.ole_object.Path_.Path:
          self.ole_object.Put_ ()
      else:
        raise AttributeError (attribute)
    except pywintypes.com_error:
      handle_com_error ()

  def __eq__ (self, other):
    return self.id == other.id

  def __hash__ (self):
    return hash (self.id)

  def _getAttributeNames (self):
     """Return list of methods/properties for IPython completion"""
     attribs = [str (x) for x in self.methods.keys ()]
     attribs.extend ([str (x) for x in self.properties.keys ()])
     return attribs

  def _get_keys (self):
    """A WMI object is uniquely defined by a set of properties
    which constitute its keys. Lazily retrieves the keys for this
    instance or class.

    :returns: list of key property names
    """
    # NB You can get the keys of an instance more directly, via
    # Path\_.Keys but this doesn't apply to classes. The technique
    # here appears to work for both.
    if self._keys is None:
      _set (self, "_keys", [])
      for property in self.ole_object.Properties_:
        for qualifier in property.Qualifiers_:
          if qualifier.Name == "key" and qualifier.Value:
            self._keys.append (property.Name)
    return self._keys
  keys = property (_get_keys)

  def wmi_property (self, property_name):
    """Return the cached object representing one property
    of this object
    """
    return _wmi_property (self.ole_object.Properties_ (property_name))

  def put (self):
    """Push all outstanding property updates back to the
    WMI database.
    """
    self.ole_object.Put_ ()

  def set (self, **kwargs):
    """Set several properties of the underlying object
    at one go. This is particularly useful in combination
    with the new () method below. However, an instance
    which has been spawned in this way won't have enough
    information to write pack, so only try if the
    instance has a path.
    """
    if kwargs:
      try:
        for attribute, value in kwargs.items ():
          if attribute in self.properties:
            self._cached_properties (attribute).set (value)
          else:
            raise AttributeError (attribute)
        #
        # Only try to write the attributes
        #  back if the object exists.
        #
        if self.ole_object.Path_.Path:
          self.ole_object.Put_ ()
      except pywintypes.com_error:
        handle_com_error ()

  def path (self):
    """Return the WMI URI to this object. Can be used to
    determine the path relative to the parent namespace::

      pp0 = wmi.WMI ().Win32_ParallelPort ()[0]
      print(pp0.path ().RelPath)

    ..  Do more with this
    """
    try:
      return self.ole_object.Path_
    except pywintypes.com_error:
      handle_com_error ()

  def derivation (self):
    """Return a tuple representing the object derivation for
    this object, with the most specific object first::

      pp0 = wmi.WMI ().Win32_ParallelPort ()[0]
      print(' <- '.join (pp0.derivation ()))
    """
    try:
      return self.ole_object.Derivation_
    except pywintypes.com_error:
      handle_com_error ()

  def _cached_associated_classes (self):
    if self._associated_classes is None:
      if isinstance (self, _wmi_class):
        params = {'bSchemaOnly' : True}
      else:
        params = {'bClassesOnly' : True}
      try:
        associated_classes = dict (
          (assoc.Path_.Class, _wmi_class (self._namespace, assoc)) for
            assoc in self.ole_object.Associators_ (**params)
        )
        _set (self, "_associated_classes", associated_classes)
      except pywintypes.com_error:
        handle_com_error ()

    return self._associated_classes
  associated_classes = property (_cached_associated_classes)

  def associators (self, wmi_association_class="", wmi_result_class=""):
    """Return a list of objects related to this one, optionally limited
    either by association class (ie the name of the class which relates
    them) or by result class (ie the name of the class which would be
    retrieved)::

      c = wmi.WMI ()
      pp = c.Win32_ParallelPort ()[0]

      for i in pp.associators (wmi_association_class="Win32_PortResource"):
        print(i)

      for i in pp.associators (wmi_result_class="Win32_PnPEntity"):
        print(i)
    """
    try:
      return [
        _wmi_object (i) for i in \
          self.ole_object.Associators_ (
           strAssocClass=wmi_association_class,
           strResultClass=wmi_result_class
         )
      ]
    except pywintypes.com_error:
      handle_com_error ()

  def references (self, wmi_class=""):
    """Return a list of associations involving this object, optionally
    limited by the result class (the name of the association class).

    NB Associations are treated specially; although WMI only returns
    the string corresponding to the instance of each associated object,
    this module will automatically convert that to the object itself::

      c =  wmi.WMI ()
      sp = c.Win32_SerialPort ()[0]

      for i in sp.references ():
        print(i)

      for i in sp.references (wmi_class="Win32_SerialPortSetting"):
        print(i)
    """
    #
    # FIXME: Allow an actual class to be passed in, using
    # its .Path_.RelPath property to determine the string
    #
    try:
      return [_wmi_object (i) for i in self.ole_object.References_ (strResultClass=wmi_class)]
    except pywintypes.com_error:
      handle_com_error ()

#
# class _wmi_event
#
class _wmi_event (_wmi_object):
  """Slight extension of the _wmi_object class to allow
  objects which are the result of events firing to return
  extra information such as the type of event.
  """
  event_type_re = re.compile ("__Instance(Creation|Modification|Deletion)Event")
  def __init__ (self, event, event_info, fields=[]):
    _wmi_object.__init__ (self, event, fields=fields)
    _set (self, "event_type", None)
    _set (self, "timestamp", None)
    _set (self, "previous", None)

    if event_info:
      event_type = self.event_type_re.match (event_info.Path_.Class).group (1).lower ()
      _set (self, "event_type", event_type)
      if hasattr (event_info, "TIME_CREATED"):
        _set (self, "timestamp", from_1601 (event_info.TIME_CREATED))
      if hasattr (event_info, "PreviousInstance"):
        _set (self, "previous", event_info.PreviousInstance)

#
# class _wmi_class
#
class _wmi_class (_wmi_object):
  """Currying class to assist in issuing queries against
   a WMI namespace. The idea is that when someone issues
   an otherwise unknown method against the WMI object, if
   it matches a known WMI class a query object will be
   returned which may then be called with one or more params
   which will form the WHERE clause::

    c = wmi.WMI ()
    c_drives = c.Win32_LogicalDisk (Name='C:')
  """
  def __init__ (self, namespace, wmi_class):
    _wmi_object.__init__ (self, wmi_class)
    _set (self, "_class_name", wmi_class.Path_.Class)
    if namespace:
      _set (self, "_namespace", namespace)
    else:
      class_moniker = wmi_class.Path_.DisplayName
      winmgmts, namespace_moniker, class_name = class_moniker.split (":")
      namespace = _wmi_namespace (GetObject (winmgmts + ":" + namespace_moniker), False)
      _set (self, "_namespace", namespace)

  def __getattr__ (self, attribute):
    try:
      if attribute in self.properties:
        return _wmi_property (self.Properties_ (attribute))
      else:
        return _wmi_object.__getattr__ (self, attribute)
    except pywintypes.com_error:
      handle_com_error ()


  def query (self, fields=[], **where_clause):
    """Make it slightly easier to query against the class,
     by calling the namespace's query with the class preset.
     Won't work if the class has been instantiated directly.
    """
    #
    # FIXME: Not clear if this can ever happen
    #
    if self._namespace is None:
      raise x_wmi_no_namespace ("You cannot query directly from a WMI class")

    try:
      field_list = ", ".join (fields) or "*"
      wql = "SELECT " + field_list + " FROM " + self._class_name
      if where_clause:
        wql += " WHERE " + " AND ". join (["%s = %r" % (k, str (v)) for k, v in where_clause.items ()])
      return self._namespace.query (wql, self, fields)
    except pywintypes.com_error:
      handle_com_error ()

  __call__ = query

  def watch_for (
    self,
    notification_type="operation",
    delay_secs=1,
    fields=[],
    **where_clause
  ):
    if self._namespace is None:
      raise x_wmi_no_namespace ("You cannot watch directly from a WMI class")

    valid_notification_types = ("operation", "creation", "deletion", "modification")
    if notification_type.lower () not in valid_notification_types:
      raise x_wmi ("notification_type must be one of %s" % ", ".join (valid_notification_types))

    return self._namespace.watch_for (
      notification_type=notification_type,
      wmi_class=self,
      delay_secs=delay_secs,
      fields=fields,
      **where_clause
    )

  def instances (self):
    """Return a list of instances of the WMI class
    """
    try:
      return [_wmi_object (instance, self) for instance in self.Instances_ ()]
    except pywintypes.com_error:
      handle_com_error ()

  def new (self, **kwargs):
    """This is the equivalent to the raw-WMI SpawnInstance\_
    method. Note that there are relatively few uses for
    this, certainly fewer than you might imagine. Most
    classes which need to create a new *real* instance
    of themselves, eg Win32_Process, offer a .Create
    method. SpawnInstance\_ is generally reserved for
    instances which are passed as parameters to such
    `.Create` methods, a common example being the
    `Win32_SecurityDescriptor`, passed to `Win32_Share.Create`
    and other instances which need security.

    The example here is `Win32_ProcessStartup`, which
    controls the shown/hidden state etc. of a new
    `Win32_Process` instance::

      import win32con
      import wmi
      c = wmi.WMI ()
      startup = c.Win32_ProcessStartup.new (ShowWindow=win32con.SW_SHOWMINIMIZED)
      pid, retval = c.Win32_Process.Create (
        CommandLine="notepad.exe",
        ProcessStartupInformation=startup
      )

    ..  warning::
        previous versions of this docstring illustrated using this function
        to create a new process. This is *not* a good example of its use;
        it is better handled with something like the example above.
    """
    try:
      obj = _wmi_object (self.SpawnInstance_ (), self)
      obj.set (**kwargs)
      return obj
    except pywintypes.com_error:
      handle_com_error ()

#
# class _wmi_result
#
class _wmi_result:
  """Simple, data only result for targeted WMI queries which request
  data only result classes via fetch_as_classes.
  """
  def __init__(self, obj, attributes):
    if attributes:
      for attr in attributes:
        self.__dict__[attr] = obj.Properties_ (attr).Value
    else:
      for p in obj.Properties_:
        attr = p.Name
        self.__dict__[attr] = obj.Properties_(attr).Value

#
# class WMI
#
class _wmi_namespace:
  """A WMI root of a computer system. The classes attribute holds a list
  of the classes on offer. This means you can explore a bit with
  things like this::

    c = wmi.WMI ()
    for i in c.classes:
      if "user" in i.lower ():
        print(i)
  """
  def __init__ (self, namespace, find_classes):
    _set (self, "_namespace", namespace)
    #
    # wmi attribute preserved for backwards compatibility
    #
    _set (self, "wmi", namespace)

    self._classes = None
    self._classes_map = {}
    #
    # Pick up the list of classes under this namespace
    #  so that they can be queried, and used as though
    #  properties of the namespace by means of the __getattr__
    #  hook below.
    # If the namespace does not support SubclassesOf, carry on
    #  regardless
    #
    if find_classes:
      _ = self.classes

  def __repr__ (self):
    return "<_wmi_namespace: %s>" % self.wmi

  def __str__ (self):
    return repr (self)

  def _get_classes (self):
    if self._classes is None:
      self._classes = self.subclasses_of ()
    return SelfDeprecatingDict (dict.fromkeys (self._classes))
  classes = property (_get_classes)

  def get (self, moniker):
    try:
      return _wmi_object (self.wmi.Get (moniker))
    except pywintypes.com_error:
      handle_com_error ()

  def handle (self):
    """The raw OLE object representing the WMI namespace"""
    return self._namespace

  def subclasses_of (self, root="", regex=r".*"):
    try:
      SubclassesOf = self._namespace.SubclassesOf
    except AttributeError:
      return set ()
    else:
      return set (
        c.Path_.Class
          for c in SubclassesOf (root)
          if re.match (regex, c.Path_.Class)
      )

  def instances (self, class_name):
    """Return a list of instances of the WMI class. This is
    (probably) equivalent to querying with no qualifiers::

      wmi.WMI ().instances ("Win32_LogicalDisk")
      # should be the same as
      wmi.WMI ().Win32_LogicalDisk ()
    """
    try:
      return [_wmi_object (obj) for obj in self._namespace.InstancesOf (class_name)]
    except pywintypes.com_error:
      handle_com_error ()

  def new (self, wmi_class, **kwargs):
    """This is now implemented by a call to :meth:`_wmi_class.new`"""
    return getattr (self, wmi_class).new (**kwargs)

  new_instance_of = new

  def _raw_query (self, wql):
    """Execute a WQL query and return its raw results.  Use the flags
    recommended by Microsoft to achieve a read-only, semi-synchronous
    query where the time is taken while looping through.
    NB Backslashes need to be doubled up.
    """
    flags = wbemFlagReturnImmediately | wbemFlagForwardOnly
    wql = wql.replace ("\\", "\\\\")
    try:
      return self._namespace.ExecQuery (strQuery=wql, iFlags=flags)
    except pywintypes.com_error:
      handle_com_error ()

  def query (self, wql, instance_of=None, fields=[]):
    """Perform an arbitrary query against a WMI object, and return
    a list of _wmi_object representations of the results.
    """
    return [ _wmi_object (obj, instance_of, fields) for obj in self._raw_query(wql) ]

  def fetch_as_classes (self, wmi_classname, fields=(), **where_clause):
    """Build and execute a wql query to fetch the specified list of fields from
    the specified wmi_classname + where_clause, then return the results as
    a list of simple class instances with attributes matching field_list.

    If fields is left empty, select * and pre-load all class attributes for
    each class returned.
    """
    wql = "SELECT %s FROM %s" % (fields and ", ".join (fields) or "*", wmi_classname)
    if where_clause:
      wql += " WHERE " + " AND ".join (["%s = '%s'" % (k, v) for k, v in where_clause.items()])
    return [_wmi_result (obj, fields) for obj in self._raw_query(wql)]

  def fetch_as_lists (self, wmi_classname, fields, **where_clause):
    """Build and execute a wql query to fetch the specified list of fields from
    the specified wmi_classname + where_clause, then return the results as
    a list of lists whose values correspond to field_list.
    """
    wql = "SELECT %s FROM %s" % (", ".join (fields), wmi_classname)
    if where_clause:
      wql += " WHERE " + " AND ".join (["%s = '%s'" % (k, v) for k, v in where_clause.items()])
    results = []
    for obj in self._raw_query(wql):
        results.append ([obj.Properties_ (field).Value for field in fields])
    return results

  def watch_for (
    self,
    raw_wql=None,
    notification_type="operation",
    wmi_class=None,
    delay_secs=1,
    fields=[],
    **where_clause
  ):
    """Set up an event tracker on a WMI event. This function
    returns an wmi_watcher which can be called to get the
    next event::

      c = wmi.WMI ()

      raw_wql = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_Process'"
      watcher = c.watch_for (raw_wql=raw_wql)
      while 1:
        process_created = watcher ()
        print(process_created.Name)

      # or

      watcher = c.watch_for (
        notification_type="Creation",
        wmi_class="Win32_Process",
        delay_secs=2,
        Name='calc.exe'
      )
      calc_created = watcher ()

    Now supports timeout on the call to watcher::

      import pythoncom
      import wmi
      c = wmi.WMI (privileges=["Security"])
      watcher1 = c.watch_for (
        notification_type="Creation",
        wmi_class="Win32_NTLogEvent",
        Type="error"
      )
      watcher2 = c.watch_for (
        notification_type="Creation",
        wmi_class="Win32_NTLogEvent",
        Type="warning"
      )

      while 1:
        try:
          error_log = watcher1 (500)
        except wmi.x_wmi_timed_out:
          pythoncom.PumpWaitingMessages ()
        else:
          print(error_log)

        try:
          warning_log = watcher2 (500)
        except wmi.x_wmi_timed_out:
          pythoncom.PumpWaitingMessages ()
        else:
          print(warning_log)
    """
    if isinstance (wmi_class, _wmi_class):
      class_name = wmi_class._class_name
    else:
      class_name = wmi_class
      wmi_class = getattr (self, class_name)
    is_extrinsic = "__ExtrinsicEvent" in wmi_class.derivation ()
    if raw_wql:
      wql = raw_wql
    else:
      fields = set (['TargetInstance'] + (fields or ["*"]))
      field_list = ", ".join (fields)
      if is_extrinsic:
        if where_clause:
          where = " WHERE " + " AND ".join (["%s = '%s'" % (k, v) for k, v in where_clause.items ()])
        else:
          where = ""
        wql = "SELECT " + field_list + " FROM " + class_name + where
      else:
        if where_clause:
          where = " AND " + " AND ".join (["TargetInstance.%s = '%s'" % (k, v) for k, v in where_clause.items ()])
        else:
          where = ""
        wql = \
          "SELECT %s FROM __Instance%sEvent WITHIN %d WHERE TargetInstance ISA '%s' %s" % \
          (field_list, notification_type, delay_secs, class_name, where)

    try:
      return _wmi_watcher (
        self._namespace.ExecNotificationQuery (wql),
        is_extrinsic=is_extrinsic,
        fields=fields
      )
    except pywintypes.com_error:
      handle_com_error ()

  def __getattr__ (self, attribute):
    """Offer WMI classes as simple attributes. Pass through any untrapped
    unattribute to the underlying OLE object. This means that new or
    unmapped functionality is still available to the module user.
    """
    #
    # Don't try to match against known classes as was previously
    # done since the list may not have been requested
    # (find_classes=False).
    #
    try:
      return self._cached_classes (attribute)
    except pywintypes.com_error:
      return getattr (self._namespace, attribute)

  def _cached_classes (self, class_name):
    """Standard caching helper which keeps track of classes
    already retrieved by name and returns the existing object
    if found. If this is the first retrieval, store it and
    pass it back
    """
    if class_name not in self._classes_map:
      self._classes_map[class_name] = _wmi_class (self, self._namespace.Get (class_name))
    return self._classes_map[class_name]

  def _getAttributeNames (self):
    """Return list of classes for IPython completion engine"""
    return [x for x in self.classes if not x.startswith ('__')]

#
# class _wmi_watcher
#
class _wmi_watcher:
  """Helper class for WMI.watch_for below (qv)"""

  _event_property_map = {
    "TargetInstance" : _wmi_object,
    "PreviousInstance" : _wmi_object
  }
  def __init__ (self, wmi_event, is_extrinsic, fields=[]):
    self.wmi_event = wmi_event
    self.is_extrinsic = is_extrinsic
    self.fields = fields

  def __call__ (self, timeout_ms=-1):
    """When called, return the instance which caused the event. Supports
     timeout in milliseconds (defaulting to infinite). If the watcher
     times out, :exc:`x_wmi_timed_out` is raised. This makes it easy to support
     watching for multiple objects.
    """
    try:
      event = self.wmi_event.NextEvent (timeout_ms)
      if self.is_extrinsic:
        return _wmi_event (event, None, self.fields)
      else:
        return _wmi_event (
          event.Properties_ ("TargetInstance").Value,
          _wmi_object (event, property_map=self._event_property_map),
          self.fields
        )
    except pywintypes.com_error:
      handle_com_error ()

PROTOCOL = "winmgmts:"
def connect (
  computer="",
  impersonation_level="",
  authentication_level="",
  authority="",
  privileges="",
  moniker="",
  wmi=None,
  namespace="",
  suffix="",
  user="",
  password="",
  find_classes=False,
  debug=False
):
  """The WMI constructor can either take a ready-made moniker or as many
  parts of one as are necessary. Eg::

    c = wmi.WMI (moniker="winmgmts:{impersonationLevel=Delegate}//remote")
    # or
    c = wmi.WMI (computer="remote", privileges=["!RemoteShutdown", "Security"])

  I daren't link to a Microsoft URL; they change so often. Try Googling for
  WMI construct moniker and see what it comes back with.

  For complete control, a named argument "wmi" can be supplied, which
  should be a SWbemServices object, which you create yourself. Eg::

    loc = win32com.client.Dispatch("WbemScripting.SWbemLocator")
    svc = loc.ConnectServer(...)
    c = wmi.WMI(wmi=svc)

  This is the only way of connecting to a remote computer with a different
  username, as the moniker syntax does not allow specification of a user
  name.

  If the `wmi` parameter is supplied, all other parameters are ignored.
  """
  global _DEBUG
  _DEBUG = debug

  try:
    try:
      if wmi:
        obj = wmi

      elif moniker:
        if not moniker.startswith (PROTOCOL):
          moniker = PROTOCOL + moniker
        obj = GetObject (moniker)

      else:
        if user:
          if privileges or suffix:
            raise x_wmi_authentication ("You can't specify privileges or a suffix as well as a username")
          elif computer in (None, '', '.'):
            raise x_wmi_authentication ("You can only specify user/password for a remote connection")
          else:
            obj = connect_server (
              server=computer,
              namespace=namespace,
              user=user,
              password=password,
              authority=authority,
              impersonation_level=impersonation_level,
              authentication_level=authentication_level
            )

        else:
          moniker = construct_moniker (
            computer=computer,
            impersonation_level=impersonation_level,
            authentication_level=authentication_level,
            authority=authority,
            privileges=privileges,
            namespace=namespace,
            suffix=suffix
          )
          obj = GetObject (moniker)

      wmi_type = get_wmi_type (obj)

      if wmi_type == "namespace":
        return _wmi_namespace (obj, find_classes)
      elif wmi_type == "class":
        return _wmi_class (None, obj)
      elif wmi_type == "instance":
        return _wmi_object (obj)
      else:
        raise x_wmi ("Unknown moniker type")

    except pywintypes.com_error:
      handle_com_error ()

  except x_wmi_uninitialised_thread:
    raise x_wmi_uninitialised_thread ("WMI returned a syntax error: you're probably running inside a thread without first calling pythoncom.CoInitialize[Ex]")

WMI = connect

def construct_moniker (
  computer=None,
  impersonation_level=None,
  authentication_level=None,
  authority=None,
  privileges=None,
  namespace=None,
  suffix=None
):
  security = []
  if impersonation_level: security.append ("impersonationLevel=%s" % impersonation_level)
  if authentication_level: security.append ("authenticationLevel=%s" % authentication_level)
  #
  # Use of the authority descriptor is invalid on the local machine
  #
  if authority and computer: security.append ("authority=%s" % authority)
  if privileges: security.append ("(%s)" % ", ".join (privileges))

  moniker = [PROTOCOL]
  if security: moniker.append ("{%s}!" % ",".join (security))
  if computer: moniker.append ("//%s/" % computer)
  if namespace:
    parts = re.split (r"[/\\]", namespace)
    if parts[0] != 'root':
      parts.insert (0, "root")
    moniker.append ("/".join (parts))
  if suffix: moniker.append (":%s" % suffix)
  return "".join (moniker)

def get_wmi_type (obj):
  try:
    path = obj.Path_
  except AttributeError:
    return "namespace"
  else:
    if path.IsClass:
      return "class"
    else:
      return "instance"

def connect_server (
  server,
  namespace = "",
  user = "",
  password = "",
  locale = "",
  authority = "",
  impersonation_level="",
  authentication_level="",
  security_flags = 0x80,
  named_value_set = None
):
  """Return a remote server running WMI

  :param server: name of the server
  :param namespace: namespace to connect to - defaults to whatever's defined as default
  :param user: username to connect as, either local or domain (dom\\name or user@domain for XP)
  :param password: leave blank to use current context
  :param locale: desired locale in form MS_XXXX (eg MS_409 for Am En)
  :param authority: either "Kerberos:" or an NT domain. Not needed if included in user
  :param impersonation_level: valid WMI impersonation level
  :param security_flags: if 0, connect will wait forever; if 0x80, connect will timeout at 2 mins
  :param named_value_set: typically empty, otherwise a context-specific `SWbemNamedValueSet`

  Example::

    remote_connetion = wmi.connect_server (
      server="remote_machine", user="myname", password="mypassword"
    )
    c = wmi.WMI (wmi=remote_connection)
  """
  #
  # Thanks to Matt Mercer for example code to set
  # impersonation & authentication on ConnectServer
  #
  if impersonation_level:
    try:
      impersonation = getattr (obj._constants, "wbemImpersonationLevel%s" % impersonation_level.title ())
    except AttributeError:
      raise x_wmi_authentication ("No such impersonation level: %s" % impersonation_level)
  else:
    impersonation = None

  if authentication_level:
    try:
      authentication = getattr (obj._constants, "wbemAuthenticationLevel%s" % authentication_level.title ())
    except AttributeError:
      raise x_wmi_authentication ("No such impersonation level: %s" % impersonation_level)
  else:
    authentication = None

  server = Dispatch ("WbemScripting.SWbemLocator").\
    ConnectServer (
      server,
      namespace,
      user,
      password,
      locale,
      authority,
      security_flags,
      named_value_set
    )
  if impersonation:
    server.Security_.ImpersonationLevel  = impersonation
  if authentication:
    server.Security_.AuthenticationLevel  = authentication
  return server

def Registry (
  computer=None,
  impersonation_level="Impersonate",
  authentication_level="Default",
  authority=None,
  privileges=None,
  moniker=None
):

  warnings.warn ("This function can be implemented using wmi.WMI (namespace='DEFAULT').StdRegProv", DeprecationWarning)
  if not moniker:
    moniker = construct_moniker (
      computer=computer,
      impersonation_level=impersonation_level,
      authentication_level=authentication_level,
      authority=authority,
      privileges=privileges,
      namespace="default",
      suffix="StdRegProv"
    )

  try:
    return _wmi_object (GetObject (moniker))

  except pywintypes.com_error:
    handle_com_error ()

#
# Typical use test
#
if __name__ == '__main__':
  system = WMI ()
  for my_computer in system.Win32_ComputerSystem ():
    print("Disks on", my_computer.Name)
    for disk in system.Win32_LogicalDisk ():
      print(disk.Caption, disk.Description, disk.ProviderName or "")

  0707010000bc91000081a400000000000000000000000154fc7bfb0000c70d000000660000000300000000000000000000002500000000root/opt/opensvc/lib/xmlrpcClient.py  #!/usr/bin/python
#
# Copyright (c) 2009 Christophe Varoqui <christophe.varoqui@free.fr>'
# Copyright (c) 2009 Cyril Galibern <cyril.galibern@free.fr>'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
from __future__ import print_function
import socket
socket.setdefaulttimeout(180)

try:
    import xmlrpclib
except ImportError:
    import xmlrpc.client as xmlrpclib

try:
    import httplib
except ImportError:
    import http.client as httplib

from datetime import datetime, timedelta
import os
import sys
from rcGlobalEnv import rcEnv
import rcStatus
import rcExceptions as ex

hostId = __import__('hostid'+rcEnv.sysname)
hostid = hostId.hostid()
rcEnv.warned = False
pathosvc = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))

import logging
import logging.handlers
logfile = os.path.join(pathosvc, 'log', 'xmlrpc.log')
fileformatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
filehandler = logging.handlers.RotatingFileHandler(os.path.join(logfile),
                                                   maxBytes=5242880,
                                                   backupCount=5)
filehandler.setFormatter(fileformatter)
log = logging.getLogger("xmlrpc")
log.addHandler(filehandler)
log.setLevel(logging.INFO)

try:
    if sys.version_info[0] >= 3:
        from multiprocessing import queue as Queue, Process
    else:
        from multiprocessing import Queue, Process
    from Queue import Empty
    if rcEnv.sysname == 'Windows':
        from multiprocessing import set_executable
        set_executable(os.path.join(sys.exec_prefix, 'pythonw.exe'))
    mp = True
except:
    mp = False

def call_worker(q):
    e = "foo"
    o = Collector(worker=True)
    o.init()
    try:
        while e is not None:
            e = q.get()
            if e is None:
                break
            fn, args, kwargs = e
            o.log.info("xmlrpc async %s"%fn)
            try:
                _b = datetime.now()
                getattr(o.proxy, fn)(*args, **kwargs)
                _e = datetime.now()
                _d = _e - _b
                o.log.info("xmlrpc async %s done in %d.%03d seconds"%(fn, _d.seconds, _d.microseconds//1000))
                continue
            except Exception as _e:
                _e = datetime.now()
                _d = _e - _b
                err = str(_e)
            o.log.error("xmlrpc async %s error after %d.%03d seconds: %s"%(fn, _d.seconds, _d.microseconds//1000, err))
        o.log.info("shutdown")
    except ex.excSignal:
        o.log.info("interrupted on signal")
 
class Collector(object):
    def split_url(self, url):
        if url == 'None':
            return 'https', '127.0.0.1', '443', '/'
        if url.startswith('https'):
            transport = 'https'
            url = url.replace('https://', '')
        elif url.startswith('http'):
            transport = 'http'
            url = url.replace('http://', '')
        l = url.split('/')
        if len(l) < 2:
            raise
        app = l[1]
        l = l[0].split(':')
        if len(l) == 1:
            host = l[0]
            if transport == 'http':
                port = '80'
            else:
                port = '443'
        elif len(l) == 2:
            host = l[0]
            port = l[1]
        else:
            raise
        return transport, host, port, app
    
    def setNodeEnv(self):
        try:
            import ConfigParser
        except ImportError:
            import configparser as ConfigParser
        pathetc = os.path.join(os.path.dirname(__file__), '..', 'etc')
        nodeconf = os.path.join(pathetc, 'node.conf')
        config = ConfigParser.RawConfigParser()
        config.read(nodeconf)
        if config.has_option('node', 'dbopensvc'):
            rcEnv.dbopensvc = config.get('node', 'dbopensvc')
            try:
                rcEnv.dbopensvc_transport, rcEnv.dbopensvc_host, rcEnv.dbopensvc_port, rcEnv.dbopensvc_app = self.split_url(rcEnv.dbopensvc)
            except:
                self.log.error("malformed dbopensvc url: %s"%rcEnv.dbopensvc)
        if config.has_option('node', 'dbcompliance'):
            rcEnv.dbcompliance = config.get('node', 'dbcompliance')
            try:
                rcEnv.dbcompliance_transport, rcEnv.dbcompliance_host, rcEnv.dbcompliance_port, rcEnv.dbcompliance_app = self.split_url(rcEnv.dbcompliance)
            except:
                self.log.error("malformed dbcompliance url: %s"%rcEnv.dbcompliance)
        if config.has_option('node', 'uuid'):
            rcEnv.uuid = config.get('node', 'uuid')
        else:
            rcEnv.uuid = ""
        del(config)

    def submit(self, fn, *args, **kwargs):
        self.init_worker()
        self.log.info("enqueue %s"%fn)
        self.queue.put((fn, args, kwargs), block=True)

    def call(self, *args, **kwargs):
        fn = args[0]
        self.init(fn)
        if len(self.proxy_methods) == 0:
            return
        self.log.info("call %s"%fn)
        if len(args) > 1:
            args = args[1:]
        else:
            args = []
        if fn == "register_node" and \
           'register_node' not in self.proxy_methods:
            print("collector does not support node registration", file=sys.stderr)
            return
        if rcEnv.uuid == "" and \
           rcEnv.dbopensvc != "None" and \
           not rcEnv.warned and \
           self.auth_node and \
           fn != "register_node":
            print("this node is not registered. try 'nodemgr register'", file=sys.stderr)
            print("to disable this warning, set 'dbopensvc = None' in node.conf", file=sys.stderr)
            rcEnv.warned = True
            return
        try:
            _b = datetime.now()
            buff = getattr(self, fn)(*args, **kwargs)
            _e = datetime.now()
            _d = _e - _b
            self.log.info("call %s done in %d.%03d seconds"%(fn, _d.seconds, _d.microseconds//1000))
            return buff
        except Exception as e:
            _e = datetime.now()
            _d = _e - _b
            err = str(e)
        self.log.error("call %s error after %d.%03d seconds: %s"%(fn, _d.seconds, _d.microseconds//1000, err))
    
    def __init__(self, worker=False):
        self.proxy = None
        self.proxy_methods = []
        self.comp_proxy = None
        self.comp_proxy_methods = []

        self._worker = worker
        self.worker = None
        self.queue = None
        self.comp_fns = ['comp_get_data_moduleset',
                         'comp_get_svc_data_moduleset',
                         'comp_get_data',
                         'comp_get_svc_data',
                         'comp_attach_moduleset',
                         'comp_attach_svc_moduleset',
                         'comp_detach_moduleset',
                         'comp_detach_svc_moduleset',
                         'comp_get_ruleset',
                         'comp_get_svc_ruleset',
                         'comp_get_ruleset_md5',
                         'comp_attach_ruleset',
                         'comp_attach_svc_ruleset',
                         'comp_detach_ruleset',
                         'comp_detach_svc_ruleset',
                         'comp_list_ruleset',
                         'comp_list_moduleset',
                         'comp_show_status',
                         'comp_log_actions']
        self.auth_node = True
        self.log = logging.getLogger("xmlrpc%s"%('.worker' if worker else ''))

    def get_methods_dbopensvc(self):
        if rcEnv.dbopensvc == "None":
            self.proxy_methods = []
            return
        self.log.debug("get dbopensvc method list")
        try:
            if self.proxy is None:
                self.proxy = xmlrpclib.ServerProxy(rcEnv.dbopensvc)
            self.proxy_methods = self.proxy.system.listMethods()
        except:
            self.proxy = xmlrpclib.ServerProxy("https://127.0.0.1/")
            self.proxy_methods = []
        self.log.debug("%d feed methods"%len(self.proxy_methods))

    def get_methods_dbcompliance(self):
        if rcEnv.dbcompliance == "None":
            self.comp_proxy_methods = []
            return
        self.log.debug("get dbcompliance method list")
        try:
            if self.comp_proxy is None:
                self.comp_proxy = xmlrpclib.ServerProxy(rcEnv.dbcompliance)
            self.comp_proxy_methods = self.comp_proxy.system.listMethods()
        except:
            self.comp_proxy = xmlrpclib.ServerProxy("https://127.0.0.1/")
            self.comp_proxy_methods = []
        self.log.debug("%d compliance methods"%len(self.comp_proxy_methods))

    def init(self, fn=None):
        if fn is not None:
            if fn in self.comp_fns:
                if self.comp_proxy is not None:
                    return
            elif self.proxy is not None:
                return

        self.setNodeEnv()
    
        try:
            a = socket.getaddrinfo(rcEnv.dbopensvc_host, None)
            if len(a) == 0:
                raise Exception
            dbopensvc_ip = a[0][-1][0]
        except:
            self.log.error("could not resolve %s to an ip address. disable collector updates."%rcEnv.dbopensvc_host)

        try:
            a = socket.getaddrinfo(rcEnv.dbcompliance_host, None)
            if len(a) == 0:
                raise Exception
            dbcompliance_ip = a[0][-1][0]
        except:
            self.log.error("could not resolve %s to an ip address. disable collector updates."%rcEnv.dbcompliance_host)

        try:
            self.proxy = xmlrpclib.ServerProxy(rcEnv.dbopensvc)
            self.get_methods_dbopensvc()
        except:
            self.proxy = xmlrpclib.ServerProxy("https://127.0.0.1/")

        if fn in self.comp_fns:
            try:
                self.comp_proxy = xmlrpclib.ServerProxy(rcEnv.dbcompliance)
                self.get_methods_dbcompliance()
            except:
                self.comp_proxy = xmlrpclib.ServerProxy("https://127.0.0.1/")

        self.log.info("feed proxy %s"%str(self.proxy))
        self.log.info("compliance proxy %s"%str(self.comp_proxy))

        if "register_node" not in self.proxy_methods:
            self.auth_node = False

    def stop_worker(self):
        if self.queue is None:
            self.log.debug("worker already stopped (queue is None)")
            return
        if self.worker is None:
            self.log.debug("worker already stopped (worker is None)")
            return
        try:
            if not self.worker.is_alive():
                self.log.debug("worker already stopped (not alive)")
                return
        except AssertionError:
            self.log.error("don't stop worker (not a child of this process)")
            return
            
        self.log.debug("give poison pill to worker")
        self.queue.put(None)
        self.worker.join()
        self.queue = None
        self.worker = None

    def init_worker(self):
        if self._worker:
            return
       
        if self.worker is not None:
            return

        try:
            self.queue = Queue()
        except:
            self.log.error("Queue not supported. disable async mode")
            self.queue = None
            return
        self.worker = Process(target=call_worker, name="xmlrpc", args=(self.queue,))
        self.worker.start()
        self.log.info("worker started")

    def begin_action(self, svc, action, begin, sync=True):
        try:
            import version
            version = version.version
        except:
            version = "0";
    
        args = [['svcname',
             'action',
             'hostname',
             'hostid',
             'version',
             'begin',
             'cron'],
            [repr(svc.svcname),
             repr(action),
             repr(rcEnv.nodename),
             repr(hostid),
             repr(version),
             repr(str(begin)),
             '1' if svc.cron else '0']
        ]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        if sync or not mp:
            self.proxy.begin_action(*args)
        else:
            self.submit("begin_action", *args)
    
    def end_action(self, svc, action, begin, end, logfile, sync=True):
        err = 'ok'
        dateprev = None
        lines = open(logfile, 'r').read()
        pids = set([])
    
        """Example logfile line:
        2009-11-11 01:03:25,252;;DISK.VG;;INFO;;unxtstsvc01_data is already up;;10200;;EOL
        """
        vars = ['svcname',
                'action',
                'hostname',
                'hostid',
                'pid',
                'begin',
                'end',
                'status_log',
                'status',
                'cron']
        vals = []
        for line in lines.split(';;EOL\n'):
            if line.count(';;') != 4:
                continue
            if ";;status_history;;" in line:
                continue
            date = line.split(';;')[0]
    
            """Push to database the previous line, so that begin and end
            date are available.
            """
            if dateprev is not None:
                res = res.lower()
                res = res.replace(svc.svcname+'.','')
                vals.append([svc.svcname,
                             res+' '+action,
                             rcEnv.nodename,
                             hostid,
                             pid,
                             dateprev,
                             date,
                             msg,
                             res_err,
                             '1' if svc.cron else '0'])
    
            res_err = 'ok'
            (date, res, lvl, msg, pid) = line.split(';;')
    
            # database overflow protection
            trim_lim = 10000
            trim_tag = ' <trimmed> '
            trim_head = int(trim_lim/2)
            trim_tail = trim_head-len(trim_tag)
            if len(msg) > trim_lim:
                msg = msg[:trim_head]+' <trimmed> '+msg[-trim_tail:]
    
            pids |= set([pid])
            if lvl is None or lvl == 'DEBUG':
                continue
            if lvl == 'ERROR':
                err = 'err'
                res_err = 'err'
            if lvl == 'WARNING' and err != 'err':
                err = 'warn'
            if lvl == 'WARNING' and res_err != 'err':
                res_err = 'warn'
            dateprev = date
    
        """Push the last log entry, using 'end' as end date
        """
        if dateprev is not None:
            res = res.lower()
            res = res.replace(svc.svcname+'.','')
            vals.append([svc.svcname,
                         res+' '+action,
                         rcEnv.nodename,
                         hostid,
                         pid,
                         dateprev,
                         date,
                         msg,
                         res_err,
                         '1' if svc.cron else '0'])
    
        if len(vals) > 0:
            args = [vars, vals]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            if sync or not mp:
                self.proxy.res_action_batch(*args)
            else:
                self.submit("res_action_batch", *args)
    
        """Complete the wrap-up database entry
        """
    
        """ If logfile is empty, default to current process pid
        """
        if len(pids) == 0:
            pids = set([os.getpid()])
    
        args = [
            ['svcname',
             'action',
             'hostname',
             'hostid',
             'pid',
             'begin',
             'end',
             'time',
             'status',
             'cron'],
            [repr(svc.svcname),
             repr(action),
             repr(rcEnv.nodename),
             repr(hostid),
             repr(','.join(map(str, pids))),
             repr(str(begin)),
             repr(str(end)),
             repr(str(end-begin)),
             repr(str(err)),
             '1' if svc.cron else '0']
        ]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        if sync or not mp:
            self.proxy.end_action(*args)
        else:
            self.submit("end_action", *args)
    
    def svcmon_update_combo(self, g_vars, g_vals, r_vars, r_vals, sync=True):
        if 'svcmon_update_combo' in self.proxy_methods:
            args = [g_vars, g_vals, r_vars, r_vals]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            if sync or not mp:
                self.proxy.svcmon_update_combo(*args)
            else:
                self.submit("svcmon_update_combo", *args)
        else:
            args = [g_vars, g_vals]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            if sync or not mp:
                self.proxy.svcmon_update(*args)
            else:
                self.submit("svcmon_update", *args)
            args = [r_vars, r_vals]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            if sync or not mp:
                self.proxy.resmon_update(*args)
            else:
                self.submit("resmon_update", *args)
    
    def _push_appinfo(self, svc, sync=True):
        if 'update_appinfo' not in self.proxy_methods:
            return

        vars = ['app_svcname',
                'app_nodename',
                'cluster_type',
                'app_launcher',
                'app_key',
                'app_value']
        vals = []
        for r in svc.get_resources('app'):
            vals += r.info()
        if len(vals) == 0:
            return

        for val in vals:
            print("%s: %s"%(val[4], val[5]))

        args = [vars, vals]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        self.proxy.update_appinfo(*args)

    def push_service(self, svc, sync=True):
        def envfile(svc):
            envfile = os.path.join(rcEnv.pathsvc, 'etc', svc+'.env')
            if not os.path.exists(envfile):
                return
            with open(envfile, 'r') as f:
                buff = ""
                for line in f.readlines():
                    l = line.strip()
                    if len(l) == 0:
                        continue
                    if l[0] == '#' or l[0] == ';':
                        continue
                    buff += line
                return buff
            return
    
        vars = ['svc_hostid',
                'svc_name',
                'svc_cluster_type',
                'svc_flex_min_nodes',
                'svc_flex_max_nodes',
                'svc_flex_cpu_low_threshold',
                'svc_flex_cpu_high_threshold',
                'svc_type',
                'svc_nodes',
                'svc_drpnode',
                'svc_drpnodes',
                'svc_comment',
                'svc_drptype',
                'svc_autostart',
                'svc_app',
                'svc_containertype',
                'svc_envfile',
                'svc_drnoaction',
                'svc_ha']
    
        vals = [repr(hostid),
                repr(svc.svcname),
                repr(svc.clustertype),
                repr(svc.flex_min_nodes),
                repr(svc.flex_max_nodes),
                repr(svc.flex_cpu_low_threshold),
                repr(svc.flex_cpu_high_threshold),
                repr(svc.svctype),
                repr(' '.join(svc.nodes)),
                repr(svc.drpnode),
                repr(' '.join(svc.drpnodes)),
                repr(svc.comment),
                repr(svc.drp_type),
                repr(' '.join(svc.autostart_node)),
                repr(svc.app),
                repr(svc.svcmode),
                repr(envfile(svc.svcname)),
                repr(svc.drnoaction),
                '1' if svc.ha else '0']
    
        args = [vars, vals]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        self.proxy.update_service(*args)

        vars = ['mon_svcname',
                'mon_nodname',
                'mon_vmname',
                'mon_guestos',
                'mon_vmem',
                'mon_vcpus',
                'mon_containerpath']
        vals = []

        for container in svc.get_resources('container'):
            container_info = container.get_container_info()
            vals += [[svc.svcname,
                      rcEnv.nodename,
                      container.name,
                      container.guestos if hasattr(container, 'guestos') and container.guestos is not None else "",
                      container_info['vmem'],
                      container_info['vcpus'],
                      container.zonepath if hasattr(container, 'zonepath') else ""]]

        if len(vals) > 0:
            args = [vars, vals]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            self.proxy.svcmon_update(*args)
    
    def push_disks(self, node, sync=True):
        di = __import__('rcDiskInfo'+rcEnv.sysname)
        disks = di.diskInfo()
        try:
            m = __import__("rcDevTree"+rcEnv.sysname)
        except ImportError:
            return
        tree = m.DevTree()
        tree.load(di=disks)
        vars = ['disk_id',
                'disk_svcname',
                'disk_size',
                'disk_used',
                'disk_vendor',
                'disk_model',
                'disk_dg',
                'disk_nodename',
                'disk_region']
        vals = []

        # hash to add up disk usage across all services
        dh = {}
        served_disks = []

        svcs = node.svcs

        for svc in svcs:
            # hash to add up disk usage inside a service
            valsh = {}
            for r in svc.get_resources():
                if hasattr(r, "name"):
                    disk_dg = r.name
                elif hasattr(r, "dev"):
                    disk_dg = r.dev
                else:
                    disk_dg = r.rid

                if hasattr(r, 'devmap') and hasattr(r, 'vm_hostname'):
                    if hasattr(svc, "clustername"):
                        cluster = svc.clustername
                    else:
                        cluster = ','.join(sorted(list(svc.nodes)))
                    served_disks += map(lambda x: (x[0], r.vm_hostname()+'.'+x[1], cluster), r.devmap())

                try:
                    devpaths = r.devlist()
                except Exception as e:
                    print(e)
                    devpaths = []

                for devpath in devpaths:
                    for d, used, region in tree.get_top_devs_usage_for_devpath(devpath):
                        disk_id = disks.disk_id(d)
                        if disk_id is None or disk_id == "":
                            """ no point pushing to db an empty entry
                            """
                            continue
                        disk_size = disks.disk_size(d)
                        if disk_id in dh:
                            dh[disk_id] += used
                        else:
                            dh[disk_id] = used
                        if dh[disk_id] > disk_size:
                            dh[disk_id] = disk_size

                        if disk_id not in valsh or used == disk_size:
                            valsh[disk_id] = [
                             disk_id,
                             svc.svcname,
                             disk_size,
                             used,
                             disks.disk_vendor(d),
                             disks.disk_model(d),
                             disk_dg,
                             rcEnv.nodename,
                             region
                            ]
                        elif disk_id in valsh and valsh[disk_id][3] < disk_size:
                            valsh[disk_id][3] += used
                            valsh[disk_id][6] = ""
                            valsh[disk_id][8] = ""

                        if valsh[disk_id][3] > disk_size:
                            valsh[disk_id][3] = disk_size

            for l in valsh.values():
                vals += [map(lambda x: repr(x), l)]
                print(l[1], "disk", l[0], "%d/%dM"%(l[3], l[2]), "region", region)

        done = []
        region = 0

        try:
            devpaths = node.devlist()
        except Exception as e:
            print(e)
            devpaths = []

        for d in devpaths:
            disk_id = disks.disk_id(d)
            if disk_id is None or disk_id == "":
                """ no point pushing to db an empty entry
                """
                continue

            # Linux Node:devlist() reports paths, so we can have duplicate
            # disks here.
            if disk_id in done:
                continue
            done.append(disk_id)

            if disks.disk_id(d) in dh:
                left = disks.disk_size(d) - dh[disk_id]
            else:
                left = disks.disk_size(d)
            if left == 0:
                continue
            print(rcEnv.nodename, "disk", disks.disk_id(d), "%d/%dM"%(left, disks.disk_size(d)), "region", region)
            vals.append([
                 repr(disks.disk_id(d)),
                 "",
                 repr(disks.disk_size(d)),
                 repr(left),
                 repr(disks.disk_vendor(d)),
                 repr(disks.disk_model(d)),
                 "",
                 repr(rcEnv.nodename),
                 repr(region)
            ])


        args = [vars, vals]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        self.proxy.register_disks(*args)

        #
        # register disks this node provides to its VM
        #
        vars = ['disk_id',
                'disk_arrayid',
                'disk_devid',
                'disk_size',
                'disk_raid',
                'disk_group']
        vals = []

        for dev_id, vdisk_id, cluster in served_disks:
            disk_id = disks.disk_id(dev_id)
            try:
                disk_size = disks.disk_size(dev_id)
            except:
                continue
            vals.append([
              vdisk_id,
              cluster,
              disk_id,
              str(disk_size),
              "virtual",
              "virtual"
            ])
            print("register served disk", disk_id, "as", vdisk_id, "from varray", cluster)

        args = [vars, vals]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        self.proxy.register_diskinfo(*args)

    def push_stats_fs_u(self, l, sync=True):
        args = [l[0], l[1]]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        self.proxy.insert_stats_fs_u(*args)
    
    def push_pkg(self, sync=True):
        p = __import__('rcPkg'+rcEnv.sysname)
        vars = ['pkg_nodename',
                'pkg_name',
                'pkg_version',
                'pkg_arch']
        vals = p.listpkg()
        if len(vals) == 0:
            return
        if len(vals[0]) >= 5:
            vars.append('pkg_type')
        if len(vals[0]) >= 6:
            vars.append('pkg_install_date')
        if len(vals[0]) >= 7:
            vars.append('pkg_sig')
        args = [rcEnv.nodename]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        args = [vars, vals]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        self.proxy.insert_pkg(*args)
    
    def push_patch(self, sync=True):
        p = __import__('rcPkg'+rcEnv.sysname)
        vars = ['patch_nodename',
                'patch_num',
                'patch_rev']
        vals = p.listpatch()
        if len(vals) == 0:
            return
        if len(vals[0]) == 4:
            vars.append('patch_install_date')
        args = [rcEnv.nodename]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        args = [vars, vals]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        self.proxy.insert_patch(*args)
    
    def push_stats(self, interval=None, stats_dir=None,
                   stats_start=None, stats_end=None, sync=True, disable=None):
        try:
            s = __import__('rcStats'+rcEnv.sysname)
        except ImportError:
            return

        try:
            sp = s.StatsProvider(interval=interval,
                                 stats_dir=stats_dir,
                                 stats_start=stats_start,
                                 stats_end=stats_end)
        except ValueError as e:
            print(str(e))
            return 1
        except Exception as e:
            print(e)
            raise
        h = {}
        for stat in ['cpu', 'mem_u', 'proc', 'swap', 'block',
                     'blockdev', 'netdev', 'netdev_err', 'svc', 'fs_u']:
            if disable is not None and stat in disable:
                print("%s collection is disabled in node configuration"%stat)
                continue
            h[stat] = sp.get(stat)
            print("%s stats: %d samples" % (stat, len(h[stat][1])))
        import cPickle
        args = [cPickle.dumps(h)]
        if self.auth_node:
             args += [(rcEnv.uuid, rcEnv.nodename)]
        print("pushing")
        self.proxy.insert_stats(*args)

    def sysreport_lstree(self, sync=True):
        args = []
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        data = self.proxy.sysreport_lstree(*args)
        return data

    def send_sysreport(self, fpath, deleted, sync=True):
        args = []
        if fpath is None:
            args += ["", ""]
        else:
            with open(fpath, 'rb') as f:
                binary = xmlrpclib.Binary(f.read())
            args = [os.path.basename(fpath), binary]
            print("archive length:", len(binary.data))
        args += [deleted]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        self.proxy.send_sysreport(*args)
    
    def push_asset(self, node, sync=True):
        try:
            m = __import__('rcAsset'+rcEnv.sysname)
        except ImportError:
            print("pushasset methods not implemented on", rcEnv.sysname)
            return
        if "update_asset" not in self.proxy_methods:
            print("'update_asset' method is not exported by the collector")
            return
        d = m.Asset(node).get_asset_dict()

        gen = {}
        if 'hba' in d:
            vars = ['nodename', 'hba_id', 'hba_type']
            vals = []
            for hba_id, hba_type in d['hba']:
               vals.append([rcEnv.nodename, hba_id, hba_type])
            del(d['hba'])
            gen.update({'hba': [vars, vals]})

        if 'targets' in d:
            import copy
            vars = ['hba_id', 'tgt_id']
            vals = copy.copy(d['targets'])
            del(d['targets'])
            gen.update({'targets': [vars, vals]})

        if 'lan' in d:
            vars = ['mac', 'intf', 'type', 'addr', 'mask']
            vals = []
            for mac, l in d['lan'].items():
                for _d in l:
                    vals.append([mac, _d['intf'], _d['type'], _d['addr'], _d['mask']])
            del(d['lan'])
            gen.update({'lan': [vars, vals]})

        if 'uids' in d:
            vars = ['user_name', 'user_id']
            vals = d['uids']
            del(d['uids'])
            gen.update({'uids': [vars, vals]})

        if 'gids' in d:
            vars = ['group_name', 'group_id']
            vals = d['gids']
            del(d['gids'])
            gen.update({'gids': [vars, vals]})

        if len(gen) > 0:
            args = [gen]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            self.proxy.insert_generic(*args)

        args = [d.keys(), d.values()]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        if node.options.syncrpc:
            self.proxy.update_asset_sync(*args)
        else:
            self.proxy.update_asset(*args)
    
    def push_brocade(self, objects=[], sync=True):
        if 'update_brocade' not in self.proxy_methods:
            print("'update_brocade' method is not exported by the collector")
            return
        m = __import__('rcBrocade')
        try:
            brocades = m.Brocades(objects)
        except:
            return
        for brocade in brocades:
            vals = []
            for key in brocade.keys:
                try:
                    vals.append(getattr(brocade, 'get_'+key)())
                except:
                    print("error fetching", key)
                    continue
            args = [brocade.name, brocade.keys, vals]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            self.proxy.update_brocade(*args)

    def push_vioserver(self, objects=[], sync=True):
        if 'update_vioserver' not in self.proxy_methods:
            print("'update_vioserver' method is not exported by the collector")
            return
        m = __import__('rcVioServer')
        try:
            vioservers = m.VioServers(objects)
        except:
            return
        for vioserver in vioservers:
            vals = []
            for key in vioserver.keys:
                vals.append(getattr(vioserver, 'get_'+key)())
            args = [vioserver.name, vioserver.keys, vals]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            self.proxy.update_vioserver(*args)

    def push_hds(self, objects=[], sync=True):
        if 'update_hds' not in self.proxy_methods:
            print("'update_hds' method is not exported by the collector")
            return
        m = __import__('rcHds')
        try:
            hdss = m.Hdss(objects)
        except:
            return
        for hds in hdss:
            vals = []
            for key in hds.keys:
                vals.append(getattr(hds, 'get_'+key)())
            args = [hds.name, hds.keys, vals]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            self.proxy.update_hds(*args)

    def push_necism(self, objects=[], sync=True):
        if 'update_necism' not in self.proxy_methods:
            print("'update_necism' method is not exported by the collector")
            return
        m = __import__('rcNecIsm')
        try:
            necisms = m.NecIsms(objects)
        except:
            return
        for necism in necisms:
            vals = []
            for key in necism.keys:
                vals.append(getattr(necism, 'get_'+key)())
            args = [necism.name, necism.keys, vals]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            self.proxy.update_necism(*args)

    def push_hp3par(self, objects=[], sync=True):
        if 'update_hp3par' not in self.proxy_methods:
            print("'update_hp3par' method is not exported by the collector")
            return
        m = __import__('rcHp3par')
        try:
            hp3pars = m.Hp3pars(objects)
        except:
            return
        for hp3par in hp3pars:
            vals = []
            for key in hp3par.keys:
                vals.append(getattr(hp3par, 'get_'+key)())
            args = [hp3par.name, hp3par.keys, vals]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            self.proxy.update_hp3par(*args)

    def push_ibmsvc(self, objects=[], sync=True):
        if 'update_ibmsvc' not in self.proxy_methods:
            print("'update_ibmsvc' method is not exported by the collector")
            return
        m = __import__('rcIbmSvc')
        try:
            ibmsvcs = m.IbmSvcs(objects)
        except:
            return
        for ibmsvc in ibmsvcs:
            vals = []
            for key in ibmsvc.keys:
                vals.append(getattr(ibmsvc, 'get_'+key)())
            args = [ibmsvc.name, ibmsvc.keys, vals]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            self.proxy.update_ibmsvc(*args)
    
    def push_nsr(self, sync=True):
        if 'update_nsr' not in self.proxy_methods:
           print("'update_nsr' method is not exported by the collector")
           return
        m = __import__('rcNsr')
        try:
            nsr = m.Nsr()
        except:
            return
        vals = []
        for key in nsr.keys:
            vals.append(getattr(nsr, 'get_'+key)())
        args = [rcEnv.nodename, nsr.keys, vals]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        try:
            self.proxy.update_nsr(*args)
        except:
            print("error pushing nsr index")

    def push_ibmds(self, objects=[], sync=True):
        if 'update_ibmds' not in self.proxy_methods:
           print("'update_ibmds' method is not exported by the collector")
           return
        m = __import__('rcIbmDs')
        try:
            ibmdss = m.IbmDss(objects)
        except:
            return
        for ibmds in ibmdss:
            vals = []
            for key in ibmds.keys:
                vals.append(getattr(ibmds, 'get_'+key)())
            args = [ibmds.name, ibmds.keys, vals]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            try:
                self.proxy.update_ibmds(*args)
            except:
                print("error pushing", ibmds.name)

    def push_dcs(self, objects=[], sync=True):
        if 'update_dcs' not in self.proxy_methods:
           print("'update_dcs' method is not exported by the collector")
           return
        m = __import__('rcDcs')
        try:
            dcss = m.Dcss(objects)
        except:
            return
        for dcs in dcss:
            vals = []
            for key in dcs.keys:
                vals.append(getattr(dcs, 'get_'+key)())
            args = [dcs.name, dcs.keys, vals]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            try:
                self.proxy.update_dcs(*args)
            except:
                print("error pushing", dcs.name)

    def push_eva(self, objects=[], sync=True):
        if 'update_eva_xml' not in self.proxy_methods:
            print("'update_eva_xml' method is not exported by the collector")
            return
        m = __import__('rcEva')
        try:
            evas = m.Evas(objects)
        except:
            return
        for eva in evas:
            vals = []
            for key in eva.keys:
                vals.append(getattr(eva, 'get_'+key)())
            args = [eva.name, eva.keys, vals]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            self.proxy.update_eva_xml(*args)
    
    def push_sym(self, objects=[], sync=True):
        if 'update_sym_xml' not in self.proxy_methods:
            print("'update_sym_xml' method is not exported by the collector")
            return 1
        m = __import__('rcSymmetrix')
        try:
            syms = m.Syms(objects)
        except Exception as e:
            print(e)
            return 1
        for sym in syms:
            # can be too big for a single rpc
            for key in sym.keys:
                vars = [key]
                vals = [getattr(sym, 'get_'+key)()]
                args = [sym.sid, vars, vals]
                if self.auth_node:
                    args += [(rcEnv.uuid, rcEnv.nodename)]
                self.proxy.update_sym_xml(*args)
            # signal all files are received
            args = [sym.sid, [], []]
            if self.auth_node:
                args += [(rcEnv.uuid, rcEnv.nodename)]
            self.proxy.update_sym_xml(*args)
    
    def push_all(self, svcs, sync=True):
        args = [[svc.svcname for svc in svcs]]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        for svc in svcs:
            self.push_service(svc, sync=sync)

    def push_appinfo(self, svcs, sync=True):
        args = [[svc.svcname for svc in svcs]]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        for svc in svcs:
            self._push_appinfo(svc, sync=sync)
    
    def push_checks(self, vars, vals, sync=True):
        if "push_checks" not in self.proxy_methods:
            print("'push_checks' method is not exported by the collector")
            return
        args = [vars, vals]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        self.proxy.push_checks(*args)
    
    def register_node(self, sync=True):
        return self.proxy.register_node(rcEnv.nodename)
    
    def comp_get_data(self, modulesets=[], sync=True):
        args = [rcEnv.nodename, modulesets]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_get_data(*args)

    def comp_get_svc_data(self, svcname, modulesets=[], sync=True):
        args = [rcEnv.nodename, svcname, modulesets]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_get_svc_data(*args)

    def comp_get_data_moduleset(self, sync=True):
        args = [rcEnv.nodename]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_get_data_moduleset(*args)
    
    def comp_get_svc_data_moduleset(self, svc, sync=True):
        args = [svc]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_get_svc_data_moduleset(*args)
    
    def comp_attach_moduleset(self, moduleset, sync=True):
        args = [rcEnv.nodename, moduleset]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_attach_moduleset(*args)

    def comp_attach_svc_moduleset(self, svc, moduleset, sync=True):
        args = [svc, moduleset]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_attach_svc_moduleset(*args)
    
    def comp_detach_svc_moduleset(self, svcname, moduleset, sync=True):
        args = [svcname, moduleset]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_detach_svc_moduleset(*args)

    def comp_detach_moduleset(self, moduleset, sync=True):
        args = [rcEnv.nodename, moduleset]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_detach_moduleset(*args)
    
    def comp_get_svc_ruleset(self, svcname, sync=True):
        args = [svcname]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_get_svc_ruleset(*args)

    def comp_get_ruleset(self, sync=True):
        args = [rcEnv.nodename]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_get_ruleset(*args)
    
    def comp_get_ruleset_md5(self, rset_md5, sync=True):
        args = [rset_md5]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_get_ruleset_md5(*args)
    
    def comp_attach_ruleset(self, ruleset, sync=True):
        args = [rcEnv.nodename, ruleset]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_attach_ruleset(*args)
    
    def comp_detach_svc_ruleset(self, svcname, ruleset, sync=True):
        args = [svcname, ruleset]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_detach_svc_ruleset(*args)
    
    def comp_attach_svc_ruleset(self, svcname, ruleset, sync=True):
        args = [svcname, ruleset]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_attach_svc_ruleset(*args)

    def comp_detach_ruleset(self, ruleset, sync=True):
        args = [rcEnv.nodename, ruleset]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_detach_ruleset(*args)
    
    def comp_list_ruleset(self, pattern='%', sync=True):
        args = [pattern, rcEnv.nodename]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_list_rulesets(*args)
    
    def comp_list_moduleset(self, pattern='%', sync=True):
        args = [pattern]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_list_modulesets(*args)
    
    def comp_log_actions(self, vars, vals, sync=True):
        args = [vars, vals]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_log_actions(*args)

    def comp_show_status(self, svcname, pattern='%', sync=True):
        args = [svcname, pattern]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.comp_proxy.comp_show_status(*args)

    def collector_update_root_pw(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_update_root_pw(*args)

    def collector_ack_unavailability(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_ack_unavailability(*args)

    def collector_list_unavailability_ack(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_list_unavailability_ack(*args)

    def collector_list_actions(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_list_actions(*args)

    def collector_ack_action(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_ack_action(*args)

    def collector_status(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_status(*args)

    def collector_asset(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_asset(*args)

    def collector_networks(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_networks(*args)

    def collector_checks(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_checks(*args)

    def collector_disks(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_disks(*args)

    def collector_alerts(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_alerts(*args)

    def collector_show_actions(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_show_actions(*args)

    def collector_events(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_events(*args)

    def collector_tag(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_tag(*args)

    def collector_untag(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_untag(*args)

    def collector_create_tag(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_create_tag(*args)

    def collector_show_tags(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_show_tags(*args)

    def collector_list_tags(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_list_tags(*args)

    def collector_list_nodes(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_list_nodes(*args)

    def collector_list_services(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_list_services(*args)

    def collector_list_filtersets(self, opts, sync=True):
        args = [opts]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_list_filtersets(*args)

    def collector_get_action_queue(self, sync=True):
        args = [rcEnv.nodename]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_get_action_queue(*args)

    def collector_update_action_queue(self, data, sync=True):
        args = [data]
        if self.auth_node:
            args += [(rcEnv.uuid, rcEnv.nodename)]
        return self.proxy.collector_update_action_queue(*args)


if __name__ == "__main__":
    x = Collector()
    x.init()
    print(x.proxy_methods)
    print(x.comp_proxy_methods)
   0707010000bc92000041ed000000000000000000000003550eba8a00000000000000660000000300000000000000000000001500000000root/opt/opensvc/usr  0707010000bc93000041ed000000000000000000000004550eba8a00000000000000660000000300000000000000000000001b00000000root/opt/opensvc/usr/share    0707010000bc94000081a400000000000000000000000154fc7bfb00000030000000660000000300000000000000000000002300000000root/opt/opensvc/usr/share/AUTHORS    Christophe Varoqui <christophe.varoqui@free.fr>
0707010000bc95000081a400000000000000000000000154fc7bfb0000033c000000660000000300000000000000000000002200000000root/opt/opensvc/usr/share/BANNER #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
# Copyright (c) 2011 Cyril Galibern <cyril.galibern@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
0707010000bc96000081a400000000000000000000000154fc7bfb00006744000000660000000300000000000000000000002300000000root/opt/opensvc/usr/share/COPYING    		  GNU LESSER GENERAL PUBLIC LICENSE
		       Version 2.1, February 1999

 Copyright (C) 1991, 1999 Free Software Foundation, Inc.
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.

[This is the first released version of the Lesser GPL.  It also counts
 as the successor of the GNU Library Public License, version 2, hence
 the version number 2.1.]

			    Preamble

  The licenses for most software are designed to take away your
freedom to share and change it.  By contrast, the GNU General Public
Licenses are intended to guarantee your freedom to share and change
free software--to make sure the software is free for all its users.

  This license, the Lesser General Public License, applies to some
specially designated software packages--typically libraries--of the
Free Software Foundation and other authors who decide to use it.  You
can use it too, but we suggest you first think carefully about whether
this license or the ordinary General Public License is the better
strategy to use in any particular case, based on the explanations below.

  When we speak of free software, we are referring to freedom of use,
not price.  Our General Public Licenses are designed to make sure that
you have the freedom to distribute copies of free software (and charge
for this service if you wish); that you receive source code or can get
it if you want it; that you can change the software and use pieces of
it in new free programs; and that you are informed that you can do
these things.

  To protect your rights, we need to make restrictions that forbid
distributors to deny you these rights or to ask you to surrender these
rights.  These restrictions translate to certain responsibilities for
you if you distribute copies of the library or if you modify it.

  For example, if you distribute copies of the library, whether gratis
or for a fee, you must give the recipients all the rights that we gave
you.  You must make sure that they, too, receive or can get the source
code.  If you link other code with the library, you must provide
complete object files to the recipients, so that they can relink them
with the library after making changes to the library and recompiling
it.  And you must show them these terms so they know their rights.

  We protect your rights with a two-step method: (1) we copyright the
library, and (2) we offer you this license, which gives you legal
permission to copy, distribute and/or modify the library.

  To protect each distributor, we want to make it very clear that
there is no warranty for the free library.  Also, if the library is
modified by someone else and passed on, the recipients should know
that what they have is not the original version, so that the original
author's reputation will not be affected by problems that might be
introduced by others.

  Finally, software patents pose a constant threat to the existence of
any free program.  We wish to make sure that a company cannot
effectively restrict the users of a free program by obtaining a
restrictive license from a patent holder.  Therefore, we insist that
any patent license obtained for a version of the library must be
consistent with the full freedom of use specified in this license.

  Most GNU software, including some libraries, is covered by the
ordinary GNU General Public License.  This license, the GNU Lesser
General Public License, applies to certain designated libraries, and
is quite different from the ordinary General Public License.  We use
this license for certain libraries in order to permit linking those
libraries into non-free programs.

  When a program is linked with a library, whether statically or using
a shared library, the combination of the two is legally speaking a
combined work, a derivative of the original library.  The ordinary
General Public License therefore permits such linking only if the
entire combination fits its criteria of freedom.  The Lesser General
Public License permits more lax criteria for linking other code with
the library.

  We call this license the "Lesser" General Public License because it
does Less to protect the user's freedom than the ordinary General
Public License.  It also provides other free software developers Less
of an advantage over competing non-free programs.  These disadvantages
are the reason we use the ordinary General Public License for many
libraries.  However, the Lesser license provides advantages in certain
special circumstances.

  For example, on rare occasions, there may be a special need to
encourage the widest possible use of a certain library, so that it becomes
a de-facto standard.  To achieve this, non-free programs must be
allowed to use the library.  A more frequent case is that a free
library does the same job as widely used non-free libraries.  In this
case, there is little to gain by limiting the free library to free
software only, so we use the Lesser General Public License.

  In other cases, permission to use a particular library in non-free
programs enables a greater number of people to use a large body of
free software.  For example, permission to use the GNU C Library in
non-free programs enables many more people to use the whole GNU
operating system, as well as its variant, the GNU/Linux operating
system.

  Although the Lesser General Public License is Less protective of the
users' freedom, it does ensure that the user of a program that is
linked with the Library has the freedom and the wherewithal to run
that program using a modified version of the Library.

  The precise terms and conditions for copying, distribution and
modification follow.  Pay close attention to the difference between a
"work based on the library" and a "work that uses the library".  The
former contains code derived from the library, whereas the latter must
be combined with the library in order to run.

		  GNU LESSER GENERAL PUBLIC LICENSE
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  0. This License Agreement applies to any software library or other
program which contains a notice placed by the copyright holder or
other authorized party saying it may be distributed under the terms of
this Lesser General Public License (also called "this License").
Each licensee is addressed as "you".

  A "library" means a collection of software functions and/or data
prepared so as to be conveniently linked with application programs
(which use some of those functions and data) to form executables.

  The "Library", below, refers to any such software library or work
which has been distributed under these terms.  A "work based on the
Library" means either the Library or any derivative work under
copyright law: that is to say, a work containing the Library or a
portion of it, either verbatim or with modifications and/or translated
straightforwardly into another language.  (Hereinafter, translation is
included without limitation in the term "modification".)

  "Source code" for a work means the preferred form of the work for
making modifications to it.  For a library, complete source code means
all the source code for all modules it contains, plus any associated
interface definition files, plus the scripts used to control compilation
and installation of the library.

  Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope.  The act of
running a program using the Library is not restricted, and output from
such a program is covered only if its contents constitute a work based
on the Library (independent of the use of the Library in a tool for
writing it).  Whether that is true depends on what the Library does
and what the program that uses the Library does.
  
  1. You may copy and distribute verbatim copies of the Library's
complete source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty; keep intact
all the notices that refer to this License and to the absence of any
warranty; and distribute a copy of this License along with the
Library.

  You may charge a fee for the physical act of transferring a copy,
and you may at your option offer warranty protection in exchange for a
fee.

  2. You may modify your copy or copies of the Library or any portion
of it, thus forming a work based on the Library, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:

    a) The modified work must itself be a software library.

    b) You must cause the files modified to carry prominent notices
    stating that you changed the files and the date of any change.

    c) You must cause the whole of the work to be licensed at no
    charge to all third parties under the terms of this License.

    d) If a facility in the modified Library refers to a function or a
    table of data to be supplied by an application program that uses
    the facility, other than as an argument passed when the facility
    is invoked, then you must make a good faith effort to ensure that,
    in the event an application does not supply such function or
    table, the facility still operates, and performs whatever part of
    its purpose remains meaningful.

    (For example, a function in a library to compute square roots has
    a purpose that is entirely well-defined independent of the
    application.  Therefore, Subsection 2d requires that any
    application-supplied function or table used by this function must
    be optional: if the application does not supply it, the square
    root function must still compute square roots.)

These requirements apply to the modified work as a whole.  If
identifiable sections of that work are not derived from the Library,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works.  But when you
distribute the same sections as part of a whole which is a work based
on the Library, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote
it.

Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Library.

In addition, mere aggregation of another work not based on the Library
with the Library (or with a work based on the Library) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.

  3. You may opt to apply the terms of the ordinary GNU General Public
License instead of this License to a given copy of the Library.  To do
this, you must alter all the notices that refer to this License, so
that they refer to the ordinary GNU General Public License, version 2,
instead of to this License.  (If a newer version than version 2 of the
ordinary GNU General Public License has appeared, then you can specify
that version instead if you wish.)  Do not make any other change in
these notices.

  Once this change is made in a given copy, it is irreversible for
that copy, so the ordinary GNU General Public License applies to all
subsequent copies and derivative works made from that copy.

  This option is useful when you wish to copy part of the code of
the Library into a program that is not a library.

  4. You may copy and distribute the Library (or a portion or
derivative of it, under Section 2) in object code or executable form
under the terms of Sections 1 and 2 above provided that you accompany
it with the complete corresponding machine-readable source code, which
must be distributed under the terms of Sections 1 and 2 above on a
medium customarily used for software interchange.

  If distribution of object code is made by offering access to copy
from a designated place, then offering equivalent access to copy the
source code from the same place satisfies the requirement to
distribute the source code, even though third parties are not
compelled to copy the source along with the object code.

  5. A program that contains no derivative of any portion of the
Library, but is designed to work with the Library by being compiled or
linked with it, is called a "work that uses the Library".  Such a
work, in isolation, is not a derivative work of the Library, and
therefore falls outside the scope of this License.

  However, linking a "work that uses the Library" with the Library
creates an executable that is a derivative of the Library (because it
contains portions of the Library), rather than a "work that uses the
library".  The executable is therefore covered by this License.
Section 6 states terms for distribution of such executables.

  When a "work that uses the Library" uses material from a header file
that is part of the Library, the object code for the work may be a
derivative work of the Library even though the source code is not.
Whether this is true is especially significant if the work can be
linked without the Library, or if the work is itself a library.  The
threshold for this to be true is not precisely defined by law.

  If such an object file uses only numerical parameters, data
structure layouts and accessors, and small macros and small inline
functions (ten lines or less in length), then the use of the object
file is unrestricted, regardless of whether it is legally a derivative
work.  (Executables containing this object code plus portions of the
Library will still fall under Section 6.)

  Otherwise, if the work is a derivative of the Library, you may
distribute the object code for the work under the terms of Section 6.
Any executables containing that work also fall under Section 6,
whether or not they are linked directly with the Library itself.

  6. As an exception to the Sections above, you may also combine or
link a "work that uses the Library" with the Library to produce a
work containing portions of the Library, and distribute that work
under terms of your choice, provided that the terms permit
modification of the work for the customer's own use and reverse
engineering for debugging such modifications.

  You must give prominent notice with each copy of the work that the
Library is used in it and that the Library and its use are covered by
this License.  You must supply a copy of this License.  If the work
during execution displays copyright notices, you must include the
copyright notice for the Library among them, as well as a reference
directing the user to the copy of this License.  Also, you must do one
of these things:

    a) Accompany the work with the complete corresponding
    machine-readable source code for the Library including whatever
    changes were used in the work (which must be distributed under
    Sections 1 and 2 above); and, if the work is an executable linked
    with the Library, with the complete machine-readable "work that
    uses the Library", as object code and/or source code, so that the
    user can modify the Library and then relink to produce a modified
    executable containing the modified Library.  (It is understood
    that the user who changes the contents of definitions files in the
    Library will not necessarily be able to recompile the application
    to use the modified definitions.)

    b) Use a suitable shared library mechanism for linking with the
    Library.  A suitable mechanism is one that (1) uses at run time a
    copy of the library already present on the user's computer system,
    rather than copying library functions into the executable, and (2)
    will operate properly with a modified version of the library, if
    the user installs one, as long as the modified version is
    interface-compatible with the version that the work was made with.

    c) Accompany the work with a written offer, valid for at
    least three years, to give the same user the materials
    specified in Subsection 6a, above, for a charge no more
    than the cost of performing this distribution.

    d) If distribution of the work is made by offering access to copy
    from a designated place, offer equivalent access to copy the above
    specified materials from the same place.

    e) Verify that the user has already received a copy of these
    materials or that you have already sent this user a copy.

  For an executable, the required form of the "work that uses the
Library" must include any data and utility programs needed for
reproducing the executable from it.  However, as a special exception,
the materials to be distributed need not include anything that is
normally distributed (in either source or binary form) with the major
components (compiler, kernel, and so on) of the operating system on
which the executable runs, unless that component itself accompanies
the executable.

  It may happen that this requirement contradicts the license
restrictions of other proprietary libraries that do not normally
accompany the operating system.  Such a contradiction means you cannot
use both them and the Library together in an executable that you
distribute.

  7. You may place library facilities that are a work based on the
Library side-by-side in a single library together with other library
facilities not covered by this License, and distribute such a combined
library, provided that the separate distribution of the work based on
the Library and of the other library facilities is otherwise
permitted, and provided that you do these two things:

    a) Accompany the combined library with a copy of the same work
    based on the Library, uncombined with any other library
    facilities.  This must be distributed under the terms of the
    Sections above.

    b) Give prominent notice with the combined library of the fact
    that part of it is a work based on the Library, and explaining
    where to find the accompanying uncombined form of the same work.

  8. You may not copy, modify, sublicense, link with, or distribute
the Library except as expressly provided under this License.  Any
attempt otherwise to copy, modify, sublicense, link with, or
distribute the Library is void, and will automatically terminate your
rights under this License.  However, parties who have received copies,
or rights, from you under this License will not have their licenses
terminated so long as such parties remain in full compliance.

  9. You are not required to accept this License, since you have not
signed it.  However, nothing else grants you permission to modify or
distribute the Library or its derivative works.  These actions are
prohibited by law if you do not accept this License.  Therefore, by
modifying or distributing the Library (or any work based on the
Library), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Library or works based on it.

  10. Each time you redistribute the Library (or any work based on the
Library), the recipient automatically receives a license from the
original licensor to copy, distribute, link with or modify the Library
subject to these terms and conditions.  You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties with
this License.

  11. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License.  If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Library at all.  For example, if a patent
license would not permit royalty-free redistribution of the Library by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Library.

If any portion of this section is held invalid or unenforceable under any
particular circumstance, the balance of the section is intended to apply,
and the section as a whole is intended to apply in other circumstances.

It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system which is
implemented by public license practices.  Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.

This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.

  12. If the distribution and/or use of the Library is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Library under this License may add
an explicit geographical distribution limitation excluding those countries,
so that distribution is permitted only in or among countries not thus
excluded.  In such case, this License incorporates the limitation as if
written in the body of this License.

  13. The Free Software Foundation may publish revised and/or new
versions of the Lesser General Public License from time to time.
Such new versions will be similar in spirit to the present version,
but may differ in detail to address new problems or concerns.

Each version is given a distinguishing version number.  If the Library
specifies a version number of this License which applies to it and
"any later version", you have the option of following the terms and
conditions either of that version or of any later version published by
the Free Software Foundation.  If the Library does not specify a
license version number, you may choose any version ever published by
the Free Software Foundation.

  14. If you wish to incorporate parts of the Library into other free
programs whose distribution conditions are incompatible with these,
write to the author to ask for permission.  For software which is
copyrighted by the Free Software Foundation, write to the Free
Software Foundation; we sometimes make exceptions for this.  Our
decision will be guided by the two goals of preserving the free status
of all derivatives of our free software and of promoting the sharing
and reuse of software generally.

			    NO WARRANTY

  15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
LIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.

  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.

		     END OF TERMS AND CONDITIONS

           How to Apply These Terms to Your New Libraries

  If you develop a new library, and you want it to be of the greatest
possible use to the public, we recommend making it free software that
everyone can redistribute and change.  You can do so by permitting
redistribution under these terms (or, alternatively, under the terms of the
ordinary General Public License).

  To apply these terms, attach the following notices to the library.  It is
safest to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.

    <one line to give the library's name and a brief idea of what it does.>
    Copyright (C) <year>  <name of author>

    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

Also add information on how to contact you by electronic and paper mail.

You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the library, if
necessary.  Here is a sample; alter the names:

  Yoyodyne, Inc., hereby disclaims all copyright interest in the
  library `Frob' (a library for tweaking knobs) written by James Random Hacker.

  <signature of Ty Coon>, 1 April 1990
  Ty Coon, President of Vice

That's all there is to it!


0707010000bc97000041ed000000000000000000000002550eba8a00000000000000660000000300000000000000000000002d00000000root/opt/opensvc/usr/share/bash_completion.d  0707010000bc98000081a400000000000000000000000154fc7bfb0000296a000000660000000300000000000000000000003500000000root/opt/opensvc/usr/share/bash_completion.d/opensvc  svcs=$(ls /opt/opensvc/etc/*env 2>/dev/null|xargs -n1 basename 2>/dev/null|sed -e "s/.env//")
svcmgr_opts="--force --debug --waitlock --rid --tags --service"
nodemgr_opts="--force --debug --cron"

_svcmgr() 
{
    local a b c d prev
    COMPREPLY=()
    a="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    #
    #  Complete the arguments to some of the basic commands.
    #
    case "${prev}" in
	--tags)
	    local tags=$(grep -h "tag" /opt/opensvc/etc/*env|cut -d= -f2)
	    COMPREPLY=( $(compgen -W "${tags}" -- ${a}) )
            return 0
            ;;
        --rid)
	    local rid=$(grep -h "\[.*\]" /opt/opensvc/etc/*env|sed -e "s/\[//"|sed -e "s/\]//")
	    COMPREPLY=( $(compgen -W "${rid}" -- ${a}) )
            return 0
            ;;
        --service)
	    COMPREPLY=( $(compgen -W "${svcs}" -- ${a}) )
            return 0
            ;;
        *)
        ;;
    esac

    if [ $COMP_CWORD -eq 1 ]; then
        local opts="configure create disklist freeze frozen mount postsync presync print_status print_resource_status print_service edit_service prstart prstatus prstop push restart start startapp startcontainer startdisk startip startloop startpool startstandby startvg status stop stopapp stopcontainer stopdisk stopip stoploop stoppool stopvg syncall syncbreak syncdrp syncfullsync syncnodes syncquiesce syncresync syncupdate syncverify thaw umount collector compliance"
    elif [ $COMP_CWORD -eq 2 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        case "${b}" in
            collector)
		local opts="json ack alerts checks events status list show asset networks disks"
		;;
	    compliance)
		local opts="check fixable fix show list attach detach env"
		;;
	    push)
		local opts="appinfo"
		;;
	    set)
		local opts="--param --value"
		;;
	    get|unset)
		local opts="--param"
		;;
	    resource)
		local opts="monitor"
		;;
	    print|json)
		local opts="disklist status resource"
		;;
            *)
                ;;
        esac
    elif [ $COMP_CWORD -eq 3 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        c="${COMP_WORDS[COMP_CWORD-2]}"
        case "${c} ${b}" in
            "collector json")
		local opts="alerts checks events status list show asset networks disks"
		;;
            "collector ack")
		local opts="action"
		;;
            "collector list")
		local opts="actions"
		;;
            "collector show")
		local opts="actions"
		;;
            "collector disks")
		local opts="--table"
		;;
            "collector asset")
		local opts="--table"
		;;
            "collector networks")
		local opts="--table"
		;;
            "collector status")
		local opts="--table"
		;;
            "collector checks")
		local opts="--table"
		;;
            "collector events")
		local opts="--begin --end --table"
		;;
            "compliance list")
		local opts="ruleset moduleset"
		;;
            "compliance show")
		local opts="ruleset moduleset"
		;;
            "compliance fixable")
		local opts="--module --moduleset"
		;;
            "compliance fix")
		local opts="--module --moduleset --attach"
		;;
            "compliance check")
		local opts="--module --moduleset"
		;;
            "compliance env")
		local opts="--module --moduleset"
		;;
            "compliance attach")
		local opts="moduleset ruleset"
		;;
            "compliance detach")
		local opts="moduleset ruleset"
		;;
            "print resource")
		local opts="status"
		;;
            *)
                ;;
        esac
    elif [ $COMP_CWORD -eq 4 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        c="${COMP_WORDS[COMP_CWORD-2]}"
        d="${COMP_WORDS[COMP_CWORD-3]}"
        case "${d} ${c} ${b}" in
            "collector json list")
		local opts="actions"
		;;
            "collector json show")
		local opts="actions"
		;;
            "collector json events")
		local opts="--begin --end"
		;;
            "collector ack action")
		local opts="--comment --author --duration"
		;;
            "collector list actions")
		local opts="--begin --end --table"
		;;
            "collector show action")
		local opts="--id --begin --end --table"
		;;
            "compliance detach moduleset")
		local opts="--moduleset"
		;;
            "compliance detach ruleset")
		local opts="--ruleset"
		;;
            "compliance attach moduleset")
		local opts="--moduleset"
		;;
            "compliance attach ruleset")
		local opts="--ruleset"
		;;
            "compliance list moduleset")
		local opts="--moduleset"
		;;
            "compliance list ruleset")
		local opts="--ruleset"
		;;
            "print resource status")
		local opts="--rid"
		;;
        esac
    elif [ $COMP_CWORD -eq 5 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        c="${COMP_WORDS[COMP_CWORD-2]}"
        d="${COMP_WORDS[COMP_CWORD-3]}"
        e="${COMP_WORDS[COMP_CWORD-4]}"
        case "${e} ${d} ${c} ${b}" in
            "collector json ack action")
		local opts="--comment --author --duration"
		;;
            "collector json list actions")
		local opts="--begin --end"
		;;
            "collector json show action")
		local opts="--id --begin --end"
		;;
	esac
     fi

     COMPREPLY=($(compgen -W "${opts}" -- ${a}))  
     return 0
}

_nodemgr() 
{
    local a b c d prev
    COMPREPLY=()
    a="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    #
    #  Complete the arguments to some of the basic commands.
    #
    case "${prev}" in
	--module)
	    local opts=$(ls /opt/opensvc/var/compliance/[0-9]* | xargs -n1 basename | sed "s/^[0-9]*[-]//")
	    COMPREPLY=( $(compgen -W "${opts}" -- ${a}) )
            return 0
            ;;
        *)
        ;;
    esac

    if [ $COMP_CWORD -eq 1 ]; then
        local opts="compliance checks collector pushasset pushnsr pusheva pushdcs pushvioserver pushnecism pushibmsvc pushhp3par pushibmds pushhds pushdisks pushbrocade pushsym pushservices syncservices pushstats pushpkg pushpatch push get set unset register reboot shutdown provision prkey updateservices updatepkg updatecomp scanscsi dequeue schedule unschedule"
    elif [ $COMP_CWORD -eq 2 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        case "${b}" in
            dequeue)
		local opts="actions"
		;;
            collector)
		local opts="json ack alerts checks events status list show asset networks disks"
		;;
	    compliance)
		local opts="check fixable fix show list attach detach env"
		;;
	    push)
		local opts="appinfo"
		;;
	    schedule)
		local opts="reboot"
		;;
	    unschedule)
		local opts="reboot"
		;;
	    set)
		local opts="--param --value"
		;;
	    get|unset)
		local opts="--param"
		;;
	    pushstats)
		local opts="--stats-dir --begin --end"
		;;
	    pushnsr|pusheva|pushdcs|pushvioserver|pushnecism|pushibmsvc|pushhp3par|pushibmds|pushhds|pushdisks|pushbrocade)
		local opts="--object"
		;;
	    pushsym)
		local opts="--symcli-db-file --object"
		;;
            *)
                ;;
        esac
    elif [ $COMP_CWORD -eq 3 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        c="${COMP_WORDS[COMP_CWORD-2]}"
        case "${c} ${b}" in
            "collector json")
		local opts="alerts checks events status list show asset networks disks"
		;;
            "collector ack")
		local opts="action"
		;;
            "collector list")
		local opts="actions filtersets nodes services"
		;;
            "collector show")
		local opts="actions"
		;;
            "collector events")
		local opts="--begin --end --table"
		;;
            "collector checks")
		local opts="--table"
		;;
            "collector events")
		local opts="--table"
		;;
            "collector status")
		local opts="--table"
		;;
            "collector asset")
		local opts="--table"
		;;
            "collector networks")
		local opts="--table"
		;;
            "collector disks")
		local opts="--table"
		;;
            "compliance list")
		local opts="ruleset moduleset"
		;;
            "compliance show")
		local opts="ruleset moduleset"
		;;
            "compliance fixable")
		local opts="--module --moduleset"
		;;
            "compliance fix")
		local opts="--module --moduleset --attach"
		;;
            "compliance check")
		local opts="--module --moduleset"
		;;
            "compliance env")
		local opts="--module --moduleset"
		;;
            "compliance attach")
		local opts="moduleset ruleset"
		;;
            "compliance detach")
		local opts="moduleset ruleset"
		;;
            "schedule reboot")
		local opts="status"
		;;
            *)
                ;;
        esac
    elif [ $COMP_CWORD -eq 4 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        c="${COMP_WORDS[COMP_CWORD-2]}"
        d="${COMP_WORDS[COMP_CWORD-3]}"
        case "${d} ${c} ${b}" in
            "collector json list")
                local opts="actions"
                ;;
            "collector json show")
                local opts="actions"
                ;;
            "collector json events")
                local opts="--begin --end"
                ;;
            "collector ack action")
		local opts="--comment --author --duration"
		;;
            "collector list actions")
		local opts="--begin --end"
		;;
            "collector list nodes")
		local opts="--filterset"
		;;
            "collector list services")
		local opts="--filterset"
		;;
            "collector show action")
		local opts="--id --begin --end"
		;;
            "compliance detach moduleset")
		local opts="--moduleset"
		;;
            "compliance detach ruleset")
		local opts="--ruleset"
		;;
            "compliance attach moduleset")
		local opts="--moduleset"
		;;
            "compliance attach ruleset")
		local opts="--ruleset"
		;;
            "compliance list moduleset")
		local opts="--moduleset"
		;;
            "compliance list ruleset")
		local opts="--ruleset"
		;;
        esac
    elif [ $COMP_CWORD -eq 5 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        c="${COMP_WORDS[COMP_CWORD-2]}"
        d="${COMP_WORDS[COMP_CWORD-3]}"
        e="${COMP_WORDS[COMP_CWORD-4]}"
        case "${e} ${d} ${c} ${b}" in
            "collector json ack action")
                local opts="--comment --author --duration"
                ;;
            "collector json list actions")
                local opts="--begin --end"
                ;;
            "collector json show action")
                local opts="--id --begin --end"
                ;;
        esac
    fi

    COMPREPLY=($(compgen -W "${opts}" -- ${a}))  
    return 0
}

opts=$svcmgr_opts
complete -F _svcmgr svcmgr

opts=$svcmgr_opts
for s in ${svcs} ; do
    complete -F _svcmgr $s
done

opts=$nodemgr_opts
complete -F _nodemgr nodemgr

  0707010000bc99000041ed000000000000000000000003550eba8b00000000000000660000000300000000000000000000001f00000000root/opt/opensvc/usr/share/doc    0707010000bc9a000081a400000000000000000000000154fc7bfb000021fa000000660000000300000000000000000000002900000000root/opt/opensvc/usr/share/doc/node.conf  #
# Template etc/node.conf
#
#   This configuration file describes node-wide parameters.
#
#   This file can be left empty except for node.host_mode. The sync
#   schedule is autogenerated, based on the schedules described in the
#   sync#* resources defined in the service configurations. So no
#   sync schedule should be explicitely defined here.
#
#   The internal schedule default for  push actions is:
#     push actions          00:00-06:00@361 mon-sun
#     sync actions          04:00-06:00@121 mon-sun
#     compliance actions    02:00-06:00@241 sun
#     inventory actions     none
#
#   Schedule parameters details:
#     [!] <timeranges> [<days> [<weeks> [<months>]]]
#
#     !
#        desc: exclusion pattern. ommiting the ! implies an inclusion
#Â              pattern
#
#     <timeranges> := <timerange>[,<timerange>]
#       <timerange> := <begin>:<end>@<interval>
#          <begin> <end> := <hour>:<minute>
#          <interval>
#            type: integer
#            unit: minutes
#
#     <days> := <day>[-<day>][,<day>[-<day>]]
#        <day> := <day_of_week>[:<day_of_month>]
#          <day_of_week>
#             * iso week day format
#               type: integer between 0 and 6
#             * literal format
#               type: string in ("mon", "tue", "wed", "thu", "fri", "sat",
#                     "sun", "monday", "tuesday", "wednesday", "thursday",
#                     "friday", "saturday", "sunday")
#          <day_of_month> := <literal> | +<nth> | -<nth> | <nth>
#             <nth>
#               type: integer
#             <literal>
#               type: string in ("first", "1st", "second", "2nd", "third",
#                     "3rd", "fourth", "4th", "fifth", "5th", "last")
#
#     <weeks> := <week>[-<week>][,<week>[-<week>]]
#        <week>
#          type: integer between 1 and 53
#
#     <months> := <monthrange>[,<monthrange>]
#        <monthrange> := <month>[-<month>] | <month_filter>
#          <month>
#            * numeric month format
#              type: integer between 1 and 12
#            * literal format
#              type: string in ("jan", "feb", "mar", "apr", "may", "jun",
#                    "jul", "aug", "sep", "oct", "nov", "dec", "january",
#                    "february", "march", "april", "may", "june", "july",
#                    "august", "september", "october", "november",
#                    "december")
#          <month_filter> := %<modulo>[+<shift>]
#            <modulo>
#              type: integer
#            <shift>
#              type: integer
#
# Example schedule:
# * schedule = 16:00-17:00@1 sat:last,tue-mon:last * %2+1,feb-apr
#
#   reads as "once a minute between 16:00 and 17:00 on last monday,
#   tuesday and saturday of every even months plus february and
#   april".
#
# * schedule = ["06:00-07:00@61 *:1,-1", "! * * * feb"]
#
#   reads as "once between 6 and 7am every first and last day of every
#   month except february".
#

[node]
#
# A PRD host mode will only allow PRD services to run. A not-PRD mode
# allows any service to run.
#
;host_mode = DEV

#
# The team responsible set here will be pushed to the collector's asset
# table. An admin allowed to edit opensvc configuration files on a node
# can set this team_responsible and gain the edit privilege on the asset
# in the collector.
#
;team_responsible = Homies

#
# The clusters this node is a member of. If the node is member of a
# single cluster, the services inherit from the clustername. Otherwise,
# each service can set its own cluster name using the 'cluster' service
# parameter. This parameter is optional.
#
;clusters = clu1 clu2

#
# You can optionally define asset information. They will be pushed to the
# collector, so that the compliance ruleset can use this information just
# after opensvc is bootstraped.
#
# A postinstall workflow would look like this: 
#
#   vanilla install
#   install opensvc
#   nodemgr set --param node.loc_city = Paris
#   nodemgr set --param node.team_responsible = Homies
#   nodemgr pushasset --force
#   nodemgr compliance attach moduleset --moduleset sys.core
#   nodemgr compliance fix
#
;loc_country = France
;loc_city = Paris
;loc_zip = 75017
;loc_addr = 7 rue blanche
;loc_building = crystal
;loc_floor = 21
;loc_room = 102
;loc_rack = R42

#
# Set the uri of the collectors' xmlrpc servers
# Default values as example.
#
;dbopensvc = https://collector.opensvc.com/feed/default/call/xmlrpc
;dbcompliance = https://collector.opensvc.com/init/compliance/call/xmlrpc

#
# 'repo'
#   Set the uri of the opensvc agent package repository and compliance
#   modules gzipped tarball repository. This parameter is used by
#   'nodemgr updatepkg' and 'nodemgr updatecomp' commands
#
# ROOT
# +- compliance
#  +- compliance.tar.gz
#  +- current -> compliance.tar.gz
# +- packages
#  +- deb
#  +- depot
#  +- pkg
#  +- rpms
#   +- current -> opensvc-1.4-49.rpm
#   +- opensvc-1.4-48.rpm
#   +- opensvc-1.4-49.rpm
#   +- opensvc-1.4-50.rpm
#  +- tbz
#
;repo = http://opensvc.repo.corp

#
# 'repopkg'
#   Set the uri of the opensvc agent package repository. This parameter
#   is used by'nodemgr updatepkg' command. The repository file tree must
#   be organized as:
#
# ROOT
# +- deb
# +- depot
# +- pkg
# +- rpms
#  +- current -> opensvc-1.4-49.rpm
#  +- opensvc-1.4-48.rpm
#  +- opensvc-1.4-49.rpm
#  +- opensvc-1.4-50.rpm
# +- tbz
#
;repopkg = http://repo.opensvc.com

#
# 'repocomp'
#   Set the uri of the opensvc compliance modules gzipped tarbal repository.
#   This parameter is used by'nodemgr updatecomp' command.
#
# ROOT
# +- compliance.tar.gz
# +- current -> compliance.tar.gz
#
;repocomp = http://repo.opensvc.com

#
# 'ruser'
#   Set the remote user to use to login to a remote node with ssh and
#   rsync. The remote user must have the privileges to run as root the
#   following commands on the remote node:
#    - nodemgr
#    - svcmgr
#    - rsync
#   The default ruser is root for all nodes. ruser accepts a list of
#   user[@node] ... If @node is ommited, user is considered the new
#   default user
#
;ruser = opensvc
;ruser = root opensvc@node1
;ruser = usr1@node1 usr2@node2 usr3@node3

#
# Schedule parameters for the 'compliance check' node action
#
[compliance]
;interval = 1439
;period = ["00:00", "23:59"]
;days = ["saturday", "sunday"]

#
# 'auto_update'
#   Boolean. Default False.
#   If set to True, and if the execution context indicates a scheduled run,
#   execute 'updatecomp' upon 'compliance check'. 
#   This toggle helps keep the compliance modules in sync with the reference
#   repository. Beware of the security impact of this setting: you must be
#   careful your module repository is kept secure from malevolents.
#
;auto_update = False

[stats]
;interval = 30
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]
;disable = blockdev, mem_u

[checks]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[packages]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[patches]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[asset]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[nsr]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[dcs]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[hds]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[necism]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[eva]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[ibmsvc]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[vioserver]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[brocade]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[disks]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[sym]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[svcconf]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[appinfo]
;interval = 120
;period = [["04:00", "08:00"], ["23:00", "01:30"]]
;days = ["saturday", "sunday"]

[rotate_root_pw]
;interval = 361
;period = ["08:00", "13:00"]
;days = ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"]

  0707010000bc9b000081a400000000000000000000000154fc7bfb000001fa000000660000000300000000000000000000003200000000root/opt/opensvc/usr/share/doc/opensvc.haresource #!/bin/sh -e
#
# This script is to be copied to /etc/ha.d/resource.d/ in
# LinuxHA cluster setup and referenced in /etc/ha.d/haresources
# with the line:
#
# node1 opensvc MailTo::root@localhost::Alert_Cluster_Failover
#
# (replace node1 with the appropriate node name)
#

osvc_path=/opt/opensvc/bin
osvc_opts="--parallel --cluster"
allservices=${osvc_path}/allservices

case $1 in
start)
        ${allservices} ${osvc_opts} start
        ;;
stop)
        ${allservices} ${osvc_opts} stop
        ;;
esac

  0707010000bc9c000041ed000000000000000000000002550eba8a00000000000000660000000300000000000000000000002c00000000root/opt/opensvc/usr/share/doc/provisioning   0707010000bc9d000081ed00000000000000000000000154fc7bfb00000a2a000000660000000300000000000000000000004600000000root/opt/opensvc/usr/share/doc/provisioning/provisioning.agent.debian #!/usr/bin/python

import os
import sys
from subprocess import Popen

class provAgent(object):
    dev = "/dev/fd0"
    data = {}

    def __init__(self):
        self.get_cfdisk()

    def get_cfdisk(self):
        if not os.path.exists(self.dev):
            print >>sys.stderr, 'no cfdisk'
            sys.exit(1)

        with open(self.dev) as f:
            buff = f.read(1024)

        if not buff.startswith('todo'):
            print >>sys.stderr, 'nothing to do'
            sys.exit(1)

        for line in buff.split('\n'):
            if len(line) == 0:
                continue
            l = line.split(';')
            if len(l) < 2:
                print >>sys.stderr, 'malformed line:', line
                continue
            self.data[l[0]] = l[1:]

    def set_done(self):
        with open(self.dev) as f:
            buff = f.read(1024)
        buff = "done"+buff[4:]
        with open(self.dev, "w") as f:
            f.write(buff)
        
    def set_vm(self):
        if 'vm' not in self.data:
            print >>sys.stderr, 'no vm in cfdisk'
            return
        h = self.data['vm'][0]
        with open('/etc/hostname', 'w') as f:
            f.write(h+'\n')

    def set_ns(self):
        if 'ns' not in self.data:
            print >>sys.stderr, 'no ns in cfdisk'
            return
        h = self.data['ns'][0]
        with open('/etc/resolv.conf', 'w') as f:
            f.write('nameserver %s\n'%h)

    def set_pub(self):
        if 'hv_root_pubkey' not in self.data:
            print >>sys.stderr, 'no hv_root_pubkey in cfdisk'
            return
        try:
            os.makedirs('/root/.ssh')
        except:
            pass
        h = self.data['hv_root_pubkey'][0]
        with open('/root/.ssh/authorized_keys', 'a') as f:
            f.write('%s\n'%h)

    def set_ip(self, l):
        if len(l) != 3:
            return
        dev, addr, mask = l
        buff = """
auto %(dev)s
iface %(dev)s inet static
    address %(addr)s
    netmask %(mask)s
    gateway %(gw)s
"""%dict(dev=dev, addr=addr, mask=mask, gw=self.data['gw'][0])

        with open('/etc/network/interfaces', 'w')as f:
            f.write(buff)

    def set_ips(self):
        for i, l in self.data.items():
            if not i.startswith('ip#'):
                continue
            self.set_ip(l)

    def reboot(self):
        cmd = ['reboot']
        p = Popen(cmd)
        p.communicate()

    def setup(self):
        self.set_vm()
        self.set_ns()
        self.set_pub()
        self.set_ips()
        self.set_done()
        self.reboot()

provAgent().setup()
  0707010000bc9e000081a400000000000000000000000154fc7bfb0000016a000000660000000300000000000000000000004100000000root/opt/opensvc/usr/share/doc/provisioning/provisioning.example  /opt/opensvc/bin/svcmgr -s test create --resource '{"mode": "lxc", "template": "http://download.openvz.org/template/precreated/debian-6.0-x86_64.tar.gz", "rootfs": "/opt/lxc/rootfs.test"}' --resource '{"rtype": "loop", "file": "/tmp/fooloop", "size": 11}' --resource '{"rtype": "fs", "type": "ext4", "dev": "/tmp/fooloop", "mnt": "/tmp/fooloopmnt"}' --provision
  0707010000bc9f000081a400000000000000000000000154fc7bfb00000ded000000660000000300000000000000000000003700000000root/opt/opensvc/usr/share/doc/template.DEFAULT.env.gz    ‹û{üT template.DEFAULT.env í[k·ýž_Á"(d£’ö¶I Ç4~¤C F”ÄîÌpJr¤UÐßs/ÉG’7g7‘Ó,líj4ÃÇ=ç¾©O?½ÍŸO>·øCÃ=~òôá›g¯ok¸[üÁp·+»OÞÆÍ¾û„–z©6kcgqºÊÌ®Žnñ‡äaÕZmUœæ©,Í"kVÚiSëzq±½<SsÙ–>®iiœW3º^Èz¦gÒ+wÑ]ÿn—Uáðj—…#4ªž¹8Â¦æq]a9-UoávWàâë¥bà}¡qM´©ÅL»Æ8í±LñÃ¾úñ‘ðò{#¦KR8eWºPöm#Œ3³®Å…Xië[YêŸ°àx«µ¢wæ’¥MÀc6V5ÒæcaßbjŒ+,Lí¥®•sL ®dÕ”j(ÖK],…Æ°¸†ÆàtÃ Èi—V0ÆîÿÎþ*Iù'šËÅ¤–Õ±ñ"¡ÚgEºÚg oR×âí <ÅÀ-è•è2xwSj¬­lHµ([ÈÊŠF—r¡Éf(¤—5¡¦xW%kÜd1{†@±”v¡XþIºÀ`nÌA f¦¸Tv‚=ÊÉLÛ‡ÃšøÚ¶}ië9‹0òT,!Û ƒ‘ß4¹XåLkAåaþ@&è%U’ªÒ§^B¡–s(lcõ
kãŠ™T•©Çâ[ï„£Kª”sbÿªv«âd%íÉ—øƒ`ûú$Œ0¦§†¬©cV°VÞØ¨À…l´©
z?× SëÈ„ø%Tj!Áš1‡÷Æ.d­’l{Hµ¶ê$ô&NÕ$ËN\V•¦ cÃÏ1Év®8»:ÁÎNèbÜÄõì#	MÀ[÷Ç'à·¿“‡qÊóC¹Ù¼$Š%zmãÆâá¬Òð =Ïà­ÌnÈ}Ó–rì±xš;íyð´ujÞ–dÔhLÚÅh¤›l¬nö62íÇª…vL"éÓ°£˜Hã‰³ñùø³ñƒƒ”µ×9ŸëZûÍŽ{û¾æ!ùrŒÿBŒ"S SBžD.~`üE£ã—eiÖ!ZŠ?¤DêaØf"9œNqÌžv ZXBbQ«FIØVÈW`sÁ³RÖ"RŠÖË”ºØ0Ð=È€1z9?pm&ˆyTÕø‰œëãîrd29³‡>ÌÐDY<>CP’ÀZ—¥ˆbÁ(N“­ØÁ¬µ_
™«a¼³0UÅN¨Fô!gÉÖ„éþOR‹W,à¡=…#¹‡Òd*3ÂÒ™az¹¶iðhÀ»” $GV÷”»º?Þn&h0'ù(æ”GÜµø ñŠ¢à•ŒDÅºap³±’±Sétˆ^wG× ‡xC²#£×]˜VêM5%äø”xE¥cñÆAÈ ])§ªñ(Þ#Õ¹t=7DºÚ‡{½†“ûsm…¿a“.3o IïWi²³ëÀ˜C=2DæR—dÚöQIŸ@ïç¥ºÂ/ÙzC~8ViÌ‹^ÈýŠ¶<ZpŠjüv[Hh3 ©È±vxá	Ö Õ‹¢Ïí¸‰‡@Ä@¿xOà’ü??ƒ‰[v>UxTÜ«Å—_‰êþPÔ'U7®wB–·4ÃaÉ„Šð^^…÷ï„•5ò NŠÁ3!Ü¢wC1ms£²¦˜—%ÇÖ™sëíKn¤sD\ïz©v×&!sJßþZ4-Âú“¤Ãò‹O}Ö‹&K½XNüê°4%[Ñ<2ó¦Ù¿7Pšþð¤ù¢ò§¾ÜN&lgÏÕŒõ‡rÇÄÙCº—õÛèZðq7TµÇO~Ü×²/C³^„_/Ÿ<Âë·?¼Æ+îÆëëWô÷ëï_Ð•—ôúôùKzàåC~ýç¯	½jShâ„$©9.°{jM»Xú¤F¼LŠiÞïYºˆ)Åþôdš„5ùž¦K´§}Ëþö>#œ£„IT‡Àí«Ø‘™Ò³›z¶¹RCû¢"M²7+Ô|³ja¦Új
;MáO®RÁBmÝà7È•×!œ	Oô¬©²HÂàa¦4ùËé¦o|B¾ß·GkS|°²ÞdÃUˆ¬·æá™¬S®!°>ì2ûÆóØ>ýÍ¡–W¿ê‡SØÇ;ZÚÜKõ÷a-NE¥$Âé¶.u…o–ÁŸp%øOß?y¢Jçþïyð0úÔG/ÞD—*k\¨¹DJ¤ 4HxÊZ*æý `.óÀuÌ ¢Kì
pÄõÞÃ—³²œ	qÝì´Ó-â{HÞy°äX‘ÿâ#@^²øUÈS¬Y«õ>üÙ`!=¿ü9œ€ÿ‹Ãðÿ6öþnE(èÛÏ]ŸÅ:W’³;øJu0âñ¯®hÖ‹Âbù,V‚}Í°ààx6ÈG¦ew'ø¼î8Ú©;ºvJ¥À2`!Æâ±šcA×A;þ/•U½ŒˆËYž;=DÂÜ_å Ø*êoŽÅóºWÕêòíN…x`¨K[R¨ÜPlMIq‡YLƒ³îåhøp„œWBï¶™8å¶JÎ6)ÄGà¿Œ¦#¯¨.)àr—´R'g¸¿áZÑâ³ú«ô»£Ñ¾G”Èg-\‹@¤ˆºÂðôäT-åJ›Ö†k¢?Oé™mŽËwUúšÊâ²hö©H8wuÆýä+/¨pŽ‡ar8Cê½¸M],­éšs¡€îôYÞnïzD¡ dôëp ï9¶ÿNš%Ôº¦FXš€­`ï‰ußZô`LÚ=ˆÂt-’tŸ–Ä½—Ò…„õ5H»êÃ »„0¿n|È¦92ˆãÙ ÛF9U›EÁFÍ7€×°¡ƒ·1µÓäÖI Û†Ö0Ý2…¥¡{Ö,ì†qC²û”sM|¢î&DMé½'¯q©TC=MIž£—¬ÔcƒÚ4\c	Û?X°6YØ#ì.tòªlIÔR¦:Ã/Ÿ’_kÊ®—!•Ž‘žR_°ÎÛ6UœSç¨Q¦)©£¾â4îmÅ¡0–tÃo8‡Âf#µ®•ð‹;¬	åÐ€Î5§h¨ÑZhGáïØ_Œ™,xõÔQ@Š2ú\Ü[[D‚™äÕE`È§†©³/kÏØ}ìgÔ«s¸NäJü£(/„5Éfån‘‰oà·ba|€ô^ËØtÊºÜcñä
s@˜}÷šfœØsí)ÄeÇ­Žã)ƒ1¢¾¢Ä^›?‰ñìh
¬»³Æ‡àlnM×Ô_Éòà.y¼ñdê½dÈì·H³Q[Þ^×&=ÐÝ>xmWtºæZÔ‘)Å]Øµo0ÖZÏ€ï˜Â‡ï¾!'\ê@2T–|º ~»9«y¦ä
q8µwì
Æ§ Æc¡GiBâŸžÎæi‚‰®=±¶<2±Ÿß¸bS©¿Šç†ªX™Ÿ©RRÚæ×
ÊAÂàZ->n}TŒuõOày«E]j¿äFŒ(•ÍHk*YÃ/”>·DÖâRóAMD€|FËF2EÑÒ8¥âJTŠã8®ÅñâB.L8S)ˆB»Š†xSm8F¸ËQ5X‘b­ä¼VÁ„…}À7Áç… 4oöí*¤»OÞFCõî/ogÎ¿cMÎCUÂóÃyCØ†¼
Û86j=xpãáãÊ: Þ‘çuðžkui;%¿ 	ðò-9Ÿ^^¯¶GL´‹}w:ì€@“G˜u=¯–)!û-‹ „X&kƒQ‚e)‚LÞ§„³+Á/òQð•eÄdño3…—€€U]äs½ù’ÞìSº0è`Ðq§»Dä!žóÆækÙL¢¦}îÂ!„T8šä¶%;§«3AKb>Œgz'zé LIiô„}8ÛÑD‹¦lù+…Õ;ü¦7q8±“Ã†ßšÛJˆ:îIÊg° î‹rŠÅiÎÐ)ÞA®w¼Wùâ„Žág'¸mB÷Ýò0°§?Às{‡;}ÑºßW™®MA‘kÏv˜wðy/Î¸ïZ +Ú;	s0‡1ò¡ÂJ·¤óW° ?)jÄ}prô†€±†ÎÔg‡ØÂaÈÌ±x´ý„OdaaMÓô+U‹ðÃ­ePÏíéž†h”²—gWE>'¸ðC–‚—žˆ·Ð÷…0ªáˆ&¿s+Î_´yV¿Ð“ƒŽÑ½§¦±éÈhºQ¿>)>XŽúŸ?ØY€×‚N-È¹ÆÜÛ0µ‘iË7k3ž)&KT €O×±¯Ä¡b÷%’×Bñöéâ}N5Šq:<žÓ…ÓÑy!–<0¢ë×C†ý	Ñ 
ºË+Bßg×CñæÃ #$n5öù@õÿ%nß)[#Ã‰S$¬-gSˆ‚f©TÌu\ ˆËg§çÂñsJýwbá¦M"¥mWgˆ,Üµ;¡™Q¡\G‘½â¬^û¼~Àsð¹˜hÌò¼ÁUbAd²êO”*IƒêkaqyÆ¿=—úÃ½ïÓàXóùÙ}ò±â>œ5OUwX÷4þ^¯¨2Ý`‰÷®Ï[?z>Ep©ËhQŸv[LÂ*éÔN%€øÁÈëÅxÏ
LR… žòÏÏ@»úÛÄöoXÝ1ÀáÛ2ÝÌÁb¥¯4,8é³¼¼C‘Ú–;DYåL!+—¨ò?º^ZC     0707010000bca0000081a400000000000000000000000154fc7bfb00000500000000660000000300000000000000000000003300000000root/opt/opensvc/usr/share/doc/template.app.env.gz    ‹û{üT template.app.env å˜Moã6†ïùäàKÖHoEºY`ÑE€½´lŠŠbAI#‹MjùaÇE|g(R‘Ù1
kD:$2IÉg^r†¼¸8æsvG|ØœhÛcš;âCæŽËîì/šìÅÕßg<Ð{Ü¬­®Sg®´²õTþîˆó°ø=H‹©£;K[kVÒI£¥^\Ã­P.WX‹ |ÔoFÇÒRèJVÂ£»”VØ¢®Ü“¶®4­(»ëZ»’ÊnƒRÐ
ß€7àŒîW"è²A› ÌáwÒ;(„C-–²Žmk©èÝ¼Üç±©cý{·*¹ý‡yû˜S×¿t6ájcÎÆÈ{¹DÞ>ú?…ôÁÂ{ý–†,ÀR<@TŠÔ¬TÂÒ žûE”žÆb@¾R‹sø\ƒ6"y\‹¥¬%V—¡jkÁ•šf)µô¨6C[4¨g}’>,ú`õî¨.wÐ
KöÔ€ÐÐÐ¡4¡UXÌ­%ÉË´<d¡n˜ÓÆƒ(Œ¥¿4{»’%‚ó‚~¯¤¹é­îæˆü8†5•s?ý|5*ªhùÕ%Õ‹çGiêkZ"ú= .Y=:,´ÝÚ‹Õ»—žó¦$³UíæCJ,ïß> _yš»U)ì¡$umÞ>¤Ï4ËÝŒ˜ÁDOqCº:2¢»Çp#)ÍðvÓE‘nüf‚¥}_Çpârä«…RÌ³qµå'e(Kš„79FÚÙäŒïþ^,Ü‰±~E~%SÇùÎá£-¤·‚©ÓïŸƒ£P*¹¤ü#el™£ò€vvŽëâ·ÈyDÉÖæðÕPØÛ%¹+^=TY¹Ú
Ù6b%ÉÜnÉsø –”#Ä¶3úJ´36Â9¹Ð.–÷Úè¼>0?pA	Î4SæÐe<š=Á-OmÉ½†"ÅgìHt/¾î¡–VÛ‰aë‹·¹ÅÜòßáGÿk¥9“¼z€i“‹§ß”V”­YÊª?å$¶Ã‘€ik*PøGkt>âÈáiÜì„¼£Ý¤‘ù¡’ŽÇ<)?¤9?umb$ðèÞº†g¢J×_™u’•½tóédBx¿Ú'˜ËJÐÖí@Pàyj5’â¬fã3
"1@8&ýÔÃÐlé|Iš~Á—P„>æ¤«1§­³*~ÚRP`GS@™Ãë âùô1<)yO!',Ûxá6Îãr¸ëÇˆõ}½WB­ÅÆ}3úÄ1/x;r¤ˆÊ¶ùµûŸUIêõ%™D›³K˜eûf¶û¤zÎÒ†nMéI¬¦}ë»ïHëF–Ívä’.ù‚<ïãòÚ9|±rÉùMp”frÓx#å6ºl,qüGD¡Y,Q®8¡¤Úèw®¡Yo(h‹¸§}2zæã¡odP×2ÖÃù9TBóþ~Î:ê%BBê 	©µøísýWÉ?ÓÝeø€eð˜3ú-—¦ô?î²G´'ióSæ(j¾bÛ±G´cd=…[žÃÕhÚgbÜ{”@Oâ¨·f@/	‘6êž&rTÉSIöå/érÚLÇÄ9†tÕ2¥¤fš@w‰”€ŒH”Kèdiîç˜LH“,ÑßiòÜ¥ÎŽÉˆ@SÅ2Ö2}NuVÆúþ¤„”ã"  0707010000bca1000081ed00000000000000000000000154fc7bfb000004d3000000660000000300000000000000000000003700000000root/opt/opensvc/usr/share/doc/template.comp_module.py    #!/opt/opensvc/bin/python

import os
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), 'com.opensvc'))

from comp import *

import files
import packages

syntax = """syntax: %s check|fixable|fix"""%sys.argv[0]

if len(sys.argv) != 2:
    print >>sys.stderr, "wrong number of arguments"
    print >>sys.stderr, syntax
    sys.exit(RET_ERR)

objs = []

try:
    o = packages.CompPackages(prefix='OSVC_COMP_BDC_DHCPD_PACKAGE')
    objs.append(o)
except NotApplicable:
    pass

try:
    o = files.CompFiles(prefix='OSVC_COMP_BDC_DHCPD_FILE')
    objs.append(o)
except NotApplicable:
    pass

def check():
    r = 0
    for o in objs:
        r |= o.check()
    return r

def fixable():
    return RET_NA

def fix():
    r = 0
    for o in objs:
        r |= o.fix()
    return r

try:
    if sys.argv[1] == 'check':
        RET = check()
    elif sys.argv[1] == 'fix':
        RET = fix()
    elif sys.argv[1] == 'fixable':
        RET = fixable()
    else:
        print >>sys.stderr, "unsupported argument '%s'"%sys.argv[1]
        print >>sys.stderr, syntax
        RET = RET_ERR
except NotApplicable:
    sys.exit(RET_NA)
except:
    import traceback
    traceback.print_exc()
    sys.exit(RET_ERR)

sys.exit(RET)

 0707010000bca2000081ed00000000000000000000000154fc7bfb000001e8000000660000000300000000000000000000003700000000root/opt/opensvc/usr/share/doc/template.comp_module.sh    #!/bin/bash

PATH_SCRIPT="$(cd $(/usr/bin/dirname $(type -p -- $0 || echo $0));pwd)"
PATH_LIB=$PATH_SCRIPT/com.opensvc
PREFIX=OSVC_COMP_FOO

typeset -i r=0

case $1 in
check)
	$PATH_LIB/files.py ${PREFIX}_FILES check
	[ $? -eq 1 ] && r=1
	$PATH_LIB/packages.py ${PREFIX}_PKG check
	[ $? -eq 1 ] && r=1
	exit $r
	;;
fix)
	$PATH_LIB/files.py ${PREFIX}_FILES fix
	[ $? -eq 1 ] && exit 1
	$PATH_LIB/packages.py ${PREFIX}_PKG fix
	[ $? -eq 1 ] && exit 1
	exit 0
	;;
fixable)
	exit 2
	;;
esac

0707010000bca3000081a400000000000000000000000154fc7bfb00000506000000660000000300000000000000000000004000000000root/opt/opensvc/usr/share/doc/template.container.amazon.env.gz   ‹û{üT template.container.amazon.env å˜Moã6†ïùäàKÖØ^ä°@wÅÝžŠÂ ¥‘5ˆD²ü°ã ?¾3å•E	’H*›"‡ä3/g†º¸xËÏÙ¼áGÌFEÝ%„½EP­º7úÅæÞvuoËîìÏÃf/>ÿuvö{“w|v&Ë¿ÅýÎ¸ò*/A«¹õÓ~„‘Ã¿#9ÌÓ|S—YÀ:³%OF“Þ\ýl.±R±	yM¿Z¥K*U@5h-Ñ¢.ýI__«ÖÍ×u÷7þ¨ÉƒF,!X#x@„aK.DÕ@«ŠšÁ%"À½Kª*t¨TÎ´ÇË nKE×yÉ3]§Q7P3ŠyÑã?é¨éþéÒìük˜#‹N^ø½Øé1è	afÄe£}­x«+²«3Ñ~œ¯.BÑ˜Xæ½YH{ïtÊjVMc
žØ¸n¨."Ùõ„Ø„D=ÝÏ#ô@·mÚó%(·Úì´Ð‡Ç­Òjƒn	_ïTkÅ ëxßAåa$ùçj6qe»IŒO€¼L­.úƒÀ ¤Öâ–Ä¸‡8Á9™_Q9ÎM*3§ùŠ61Ú”¸”¦XSï‹_ËÔKÇ±W¹ðÎP~~Ç.>ÄÈZäœ¾£¦a5îE”yï@ù›‰Nò:ö=—
•qYIbkUÜ+Næ-o"ª¼ŸD¼7yÃ[ÃÔæc¯Âýâãnª´ß%|qk
N	uþ-3I-}W–5ÔRÈ“eIoÚ½s<;/Ôì-o± Š[[ÂïFòao—tÕDÔìJñPéh‹n`kµÚ›[Â7öv§&õ]ð(ebŠOš§ö©ý Îëci€3+ûÊ0© ­f"ãÆ5×¡3A¢ø âÀ‰.·¦¯Ôòi{gØÍÇÜ„×ÒƒA/:FyÏ,¯ÀÀhÃ©•QJ
`ŒTÿÏ7›þ’R+?P«‚94­Q…ŸÖ8‰pqÈ1+ÈÔ×}D»É+óCI^Ö<+?ä=Ÿºƒ<ùGBWÆž2 Så…pñß¦ºä­LÒ5VÂ jf„÷»á8!\¶ŠC·¾¸À‚F\ KqQ‰ñ'‘” ¼>õÂ05;boDû„?.a9§RÔpIåGÌ5†ÏY™†ZN
âè­ÜþðXÅ†c•¤§†n9åÄ–¯aÔ`wñžÀêgÆIzè}=)ÕìÔÞ¯Œ~gŠÏŽ<+¢t¶ÿÚýï^^
Ê$Ù\\Â¢·ßiæxN~.UÚÐ­¹<I¹„ÀÐåúnKbWSQg.òÙì!Æ¢]ÂwG­Ô7Ñs™)]Eà÷º¨s¼WIh¤­”FŠwý©»¥lqˆ¸åýjô"¤—UA^`‰˜»· òÎÏ¡TZâÿù¹èè RyLHÖáê=ÖúÿIýéG6HµwXÄ€}EäÒ\þ§(  ˆ&Êk|˜3GUÞ4Æ¢)Ž‰µùÿ½Ë{…} F3õê.ƒž+ÄQ)3ì=%DÔ}zš!ÈQ5öHN%yhJ—óf:&Î1¤G¨ž!S.jæ	ô1‘2‰Jë3:[šÈóæ Ó3¤É–øï<y>¦ÎŽÉˆ@óƒghtÎX‘éCªÇ°z¬ÿCA£"    0707010000bca4000081a400000000000000000000000154fc7bfb00000523000000660000000300000000000000000000004000000000root/opt/opensvc/usr/share/doc/template.container.docker.env.gz   ‹û{üT template.container.docker.env å˜Moã6†ïùäàKâd,6› ‡Ú=t{*Š€–(‹E²ü°“¢?¾/II‘Ù	²)V:X6EŽ8Ï¼šùèè-ƒ#zÃ#š+´òL(nÉ?N¥.î¸}µ¹·ÝÝÛ²;ø½wöèìƒëäïMëñÁAÜþXk[^µ[°AÝºi˜*qñäˆÊò?ƒ°¼½ÛW&ÃÆê•pB+¡–WÃ%¯X¾ÝÚ/Z¥Ñ[%óÜ]FKn¸*ÝÖ\WhÃ’oÜ/Ow¿×œZoÉkâ÷¼ž“Päë^=BÒ*ò™ÃÄõ€€žjãO½n
æOBâÌ¤PlîêQÊŠ5|x…#Åy‚»àä¸'Q%¶+a}`’VÔ`›ˆf—¢ª¸åÊSeu3x¼â"ÇíJyrŠBZuC•Ö;ÅÌìÒM õÏ÷Þ2‚³¡<æÒy e éô~LRÜ!ZbåÇ½;Œ¶‘1^¸^î‘$`Ÿ¬²Ü³É¹ÆnÝª˜Ãì)Œ}¸ÊŸvM'†.>Í?~žú0?ÿxuyvy–>v†J4l9Ç¢Hr™Lò8Æ È§¤Ôåœµðu‚<ý†./ªsvQ–çŸwà\î¼þo?(qOÃQUêµû1¬P£e{ ÷à<oºT¾•nÐ–pÆŒ«3˜õïãÙ	ò»OÁAWÈ¹k!%yûÓFë;±øMó.yÑp‡T^iË©bRFÔVÜ3uN9œðÚ+<üÉnMÞÐ¸j=ûæê'¸¿ ó:OºJþÎé‹]$ïH¿ãb­.—M)á[ˆÉ2Çè€v—2¢å/D%ŠhmN¿jd÷Þ®P•\!”©(X±BøhkÁk¶07§¯ˆ¿g‘yî«˜™ES„º"–Ê¥ñ^9êciÌÊ®r'¤Ýì.Ú.,Ð'L@‰âˆƒ f‰Yþº‡Zû´½3lýð&·È°èUQë3äÕl˜X.Q:)]¢k¢8·g×DÖÌÔÊ¨æHMÎü£5`9ËÇ[_wí¦ÝùXJáâž'‡Öçí0 ‰Aà)>1uµØS ÕØí¸:xÔü\Z+{é¢ï„§LNï7<¹¬R·C‹dì1kFâ¬ŠÆg("©@¸ôŠº…ai¶Ñæ™xÓ"ô5§bB›êË¶9©ñœ•ii|ÙˆFA™ÓoŽWA"WÙAyJ¯ehUBòÜžŸÀê±â$=t±Þ+&×ìÁÝjõÎ1^/b:rPDiM÷5Ÿ»×7¿£2I6gÇ4ëìgÍlÞ×c—6kÛž¤Ëh!¸Ïµ>¯ƒ$Öµ(êÍÊ…W÷DÈø±`æôÍâÅýMpñõS£Ð¡«¢¶àøKB³¼àbJüPZ¸^ßK‘"î°¡Ÿ´šùôg‚0D1§™é:RÉTÌÿ‡‡QG½D ¤yLHÆòÛ÷Øëÿ+ý§+¬0~øWÛÑo„´mÿSˆ {D{Ú£Ÿ2GVyo?ÆÑ>Ž‰µ6Ó¤8®FmžˆCÏjqªG¥¸É°ôœ‘¨»ò4A£jìlK²N—Óf:&Î1¤¨^ S45ÓºK¤ 2"Ñ8úN–æynÁ`z4a	ŸÓä¹K™Éˆ@Û/Ðè”±îéSª›°:¬ÿ NNEy©!   0707010000bca5000081a400000000000000000000000154fc7bfb000004b1000000660000000300000000000000000000003d00000000root/opt/opensvc/usr/share/doc/template.container.esx.env.gz  ‹û{üT template.container.esx.env å˜MoÛ8†ïþäàKêí9A=.
l{Z,
ZYD(’å‡/öÇïEÙ²#+Fã,\È[¦È!ùÌË™‘no/ù™ÝÂ?l®0:©ÑÝAØZôÏo1wÙÕ]–Ýì¯Ýfo?þ={Hû}äÏf¼ö'ÜnŒ+ïóüZ4H­.øa@Dé0OóY(Ï³€uf-½4ZêÕý¾¹ÄJDòšþ0:µB—²ý}¯µD‹ºôG}}a¬X*<˜¯íîjüZK±„``‰à1€¬ ÔkéB
QÔD-ê]ÊªB‡:@åLÓó:òèÖ²h;/h¦‡4ê*c1¯"ú`ü/A:jùÿÂFêÒlü[˜#‹NZø­Ø€ÔCÐÂÌˆ(ò
1z-ì»3üêâ Â®õÿÐ*BnEåýúÚ„ß
Eƒ¡ŠJ¡îƒeœ7û×“j)ýTR¡O„ÙÜˆHù¶©&Á·ÆÉiò$Y‡)ÐQ?“¯©F;R¸páÊÂÀÇ·n=wX¬8rn$anËQ7ï_™è8zBz
È•q$o¡s_Šâ©oMòDC›Æ(í'ñîL>Ò†p±ú5Bî›p%=)¹Jû]À'·”Á	¦Nÿy&ÎxÑ·ÉOÉF†1Y¦`cz´;çxrÞb!+Y°µüiÜÛ•ºR1öPéäúàx,±kIæð™<‡Ï¢±ªí;§QÂÎÙïåJû|Þ²6Z¯÷Œ¥>*bVvù7© ­f$ªÅ%eû	È Q|±çÀ–D©Òåµ|Ú®Û®ù³ Š¥7è§ŽQÞ3Ék00¹Z!EÐ¦¤Üe!é7×])XßS«€)4-Q„½5J4ÑÅ¬ÀS?tí1¯|È”axÍ“òCÞó±(ˆ‘À“8teì)UZø:ªYÛä­ŒÒ5–Ã PÂûÅPœ`.kA¡Ûƒ Ä;ÔkNµÌ+6>§$’„gÒÇ^è§f'ÉÑ¾â;XÆ]Î©„TÑ¥ürlN:gej))°£)¡,à›G*Ð(V¹^zRò‰RNll[€¥Ç‹þ	¬ö'é¡óõ¨ „Úˆ­ÿnô•)b8_p8ò¤ˆÒÙî²ýí~¾H”I²9¿ƒyg¿ÕÌáœtŸ«´¾[sy’nS	¡Íõí8’Ä¦–E}˜¹è¼õyÈ†ž±hðÅÉ†ë›è©Ìä®¬ zÂÔEíˆã?"	ÍarÍ%ýÑFð5í¢<*ßiA¿=é•@à×,æÔ³-ïon šãÿÍëh'RyHHÖá÷k¬õß¥þô…“6pµÏXÄ€]EàÒ\þ§(À wˆFÊk|˜2GQÞ8Æ¢1Ž‰µyÿ—'WIqXÆ¾£{=’AOâ v€^"ê.=Mä ;$Ç’Üµ¿¦Ëi3çÒTgÈ”Šši=%R2 Qn=C “¥yBžG0{˜Î&Y¢ïiò<¥Î–É€@ó34:e¬'dú’ê!¬ë ƒ%ò      0707010000bca6000081a400000000000000000000000154fc7bfb00000456000000660000000300000000000000000000003e00000000root/opt/opensvc/usr/share/doc/template.container.hpvm.env.gz ‹û{üT template.container.hpvm.env å—MoÛ8†ïþäàKjôœ ‡‹‹Ûž‹‚–FÖ ÉòÃŽûãw†úˆìÈNÐxòÁ–Gä|æÕÌèúú’ŸÅ5\ð#î
k¢"ƒþâÞ!ÔnÛ¼ÁÝewwYv‹¿†Ã^ü{q—Ï{ŸO¼XÈæp¿³¾¼í6`TƒlýpÁòø3‘Çn™ÏJYœ·[
d™Íí“¹ÄJ%»=}±&[eJ*UÄp;²–èÐ”áhl(¬SkëµÃCÁÆo50ˆ%Dk„€¨‚X#lÉÇ¤44ª¨[&<º¤ªB&Båm3
»L
è·T´ƒW¼Ò]žu•µ“˜7	C´á· =Â¿°#SÚ]xsëÐ«È{€° 3=#ì1EÙÁ$FÏ#”ïãÇ·©õ›OÏÁ©(oGZCô{QmwvPre“õA¤º²¡RZêµ*ÆzµyÃ‡ˆÖƒ*"Ÿ'ï]Þó¦pGµù=$û&ÜŸ@Sˆ`«|Þ|òkŠ^	uþ/+IÆH¡MšŠÄìÙ:¢Ý'pðbÍÑ
ª¨o+øÓržü’©tBÃ¡”•ž¶èG¾ÖX«-±»|æÈá£jœnÇ.y–rKq*Ú˜íƒ6Ú¨œå	!ifVöù+« ïætê
iÍÙr2ÈŸA°%‘™µ—g¨uOÛ;Ã6˜¹	Îø£I¿ôugfy »F›ræcK®‰
âß®þö¥´Va¤V5rjZ£ŠOÞ¸x$œ³¢,}×g´ûnçSq()Èžg‡îÌÇaà$ÆÏñ‘ÔÕaÏ€©òF Ô)rÍo‹Cçå,]ë$*=#¼_-ç	á²Uœº(.¼ËÈ£–ÀR\Vâ|ÉE$ˆ ¤£0.Íž8É½X§¡æTŠtò¹¾»Ó–Ÿ³2Ou\$Ð\PVð=`•4ç*?*Oš¸ä¤ÆAEÛölüVO'ë¡õYA(½SûðÃšw¦ˆéz!é(°"JïúËö·7üzó;)“ìsyËÞ«™Ã5ù¾tiã°víI¾Í-Æ¶Ö·óX»šŠú°rñLŽ‹#gÉ­à«§Fú›¸Í”¡¢ îÐMQ{æøÊBóX m¥¡ä?Æš¡æS”#_œ"xCX³Œù•*Êk–ˆ9Ì÷áê
Je$ÿ_]‰Ž‰°ZÈSBr¼Ç^ÿé?CáÉEé6ð‹±ïèBÚµÿ9ÈÑ™6ÄÙçÌQU‘…wã€èÇÌÚºyRœV£uÏÄÈ¦µ8Wˆ“R<dØzIˆœ¨ûò4C“jì‘Kr°¿¤Ëy3çÒT¯)75ózJ¤dB¢b}…@gKó„<`Ž0½Bšì‰¿çÉó”:[&ín¼B£sÆzB¦Ï©Âê±þßêz<G    0707010000bca7000081a400000000000000000000000154fc7bfb000004ac000000660000000300000000000000000000003e00000000root/opt/opensvc/usr/share/doc/template.container.jail.env.gz ‹û{üT template.container.jail.env å˜MoÛ8†ïþäàKjô°È!A=.
´{Z,
ZY³‘H.?ìd±?~g¨ÈŽ¬dM+›"‡œg^ÎŒ}~~Ê×âNøs™ÑA‘FwáÞ"ü¥¨z¹Óžî´ìôÎž¿ÿsqü½I/rø[¼ß—_µÐªF}wÂ—røw$‡í6Uåe°ÎlÉ“Ñ¤7WÃ9*V¡=ÓoF§ÑLéœrÐ_Fs´¨s0×gÆªu…{û5Ó}Æƒ_Jò sÖP¡DØ’QUP«¬dl‰ðìœŠê …3õ ì²È£ÛRÖL^ñN×iÕÆŒbÞDôÁø‚tÔtÿÂŽtnvþ%ÌŒE§Ÿü½Xé1è	aËˆ)Ê	F1’ý1¾T¬,@»ýTž;ô=˜F«r+1¡6²—3ât9Éér
”ÌsÆ„W‡õÅÅïËê3ŸÐˆ»PHzs˜ãî;f}Ýn˜	vL<(÷úèþŸÎÞ¿\§©TUæÅ¤õ”¼3ÑI%€@5+p…q…ª*I{k•Ýk‡I¬kv‚ÙƒÊû“”Ú™¼aÆpµùùîô#Ü "’$Ùß|pk
N	uþ,;IõŽ¾)äÕZˆÉ2òè€vÏÁ%GË[Ì¨ L¬­à³ášÝÛ%]T5‡R"”;ÚÊUèm­±T[bs+øÈ‘Ã;UÛª™»äUÊ.Å(ïi£Û«Öi£‰úÀXZàcÅÌò®—H*H§9~ç|\sç2$Š ØHÌš·ÔÚÛöÆ°õÃûÜ„w_ƒEßtZŸY^=À6Ñfƒœy@›œûØHñÿ¶îÚÚRùZ”È©i*<XãF.Zàœdëë.£Ý´'‹CN^Î<«8´>†“<ÅGRW‹=U ¦Ê_ÆÀýwSZ+“t•4¨ªáýd8O—­âÔíAqá]žµ–â²ãK."©@x!}…aivÄÑˆö‰x\À:ö5§à6)ºT_ÍU†ïYž–Z.
h.(+øÝc+ÎUnPž*ºå’kUØ|UÞÀâ¡â$=t±ž„ªvêÞ5ú)b¼^H:ò¬ˆÜÙîmó¿øöÞwT&Éæò–ýF3û{òséÒ†amÛ“ôxõÐV7ëX»’²r¿r‘ocÁ²a`,Ú|rTK=·™2UÀß–uV:æøJBã^i+%ÐF¿ó%{‘lqŠ¸åýjô2¤Ÿ7‚üä!bN3Ós8;ƒ\iÉÿgg¢£^",¤ò˜¬Ã¯o±×•þÓgŽlnï0‹»Ž~/¤mûŸ²€€ìM´!Öø0gŽª,¼iŒ=¢)Ž‰µ±ó¤8®Fc‰‘‡žÔâ\!ŽJqŸaè)!r¢îÊÓAŽª±Cr(É~ü)]Î›é˜8Çî¡z†L¹©™'Ðc"e #•Ñgt¶4Èó æ Ó3¤É–øï<ySgÃdD íƒghtÎXÈô1Õ}XÖÿ ¬³DÓ   0707010000bca8000081a400000000000000000000000154fc7bfb000004d8000000660000000300000000000000000000003d00000000root/opt/opensvc/usr/share/doc/template.container.kvm.env.gz  ‹û{üT template.container.kvm.env å˜MoÛ8†ïùäàKâöœ ‡‹Ú=--¬%’Ë;YìßŠrdGVÒ&Y¤±C“Cò™w>¬‹‹×|]À+¾Ä\atP¤Ñ]B¸·›mûs¯{º×ewöçþ²ÿ:»I÷½•ŸÉÙ7x¿3®¼ÎûkÕ"^½âK 9ü;’Ã¼ÍgÕxÙ¬3[òd4éõõÃp‰•ŠMÈgúÝè4Z(]R©úëÁh‰uéæúÂXµjð`¿nº/xð[M4b	ÁÀ
Ác ª Ô[r!ªZUÔL-ž]RU¡C r¦x]yt[*ºÉKÞé&­º…Ê˜QÌëˆ>ÿKŽšîà_Ø‘.ÍÎ¿„9‚±èTà3€¿÷[ ==!ÌŒ˜¢œ`£,#íÃ›süæâÆ~ôÿÐkÇèJn«š†3XÛ²}‘oô(o…CÞ+ÑÜG|âØCš£×ÊÎ‚aPnÍ¡.÷õµ	Š†C™¨U¡îÉ‰@¤ÆÝÌAÈ—ä7PQƒ>sOÐ5Õ,øö`Á8ZslŸ$ë0%ÓŸË×T„g
åÞ>üX:ýø2ÂçŽ «µT 1Öàî%îóÝAÉ'T!Ô¢çÂVÇòæ„!ÜWªØë–IžhùÁ8PEàû$Þ½É[¾Âî Ö¿FézîOÐgMžÜw	ŸÜŠ‚SBÿ—¤sàì›²pC-…1Y–¤< Ý;Ç³ó8x‹UTˆµ%|5->Ø%]51E‡x¨t´=ÖjKln	ŸÙsx§ZÛts¼JÙ…˜å=­µÏñ–µÑy}`,-ð±afeßÇ$¤ÓLdµ¸â®i2HA8°#Ñeªôq‚ZŽ¶w†m?|ÈMXpç7XôSa”ïÌòÚÌ	ŒÖkäÌÚ”\;"Äï¹ï[êZùZÔÈ©iÅ½Îƒ5.4Ñç¬ [ßôí6Ÿ|Ì\aäÌ³òC¾ó±8‰±À“$ueì©0Ui2}÷þ]qÈV&é+iP53ÂûÅpž.[Å©ÛƒâÂ»<kÁ½,*1¾à"’
„ÒÇ^–fGìhŸðÇ%¬â¾æTŠšèR}96×Ž³2-µ\ÄÑ\P–ð‡GnÐ8W¹AyjhÃ%'¶¶kÀÒÏ´aV'é¡÷õ¤ T³S÷þ»ÑïLãõBÒ‘gE”Îö»÷~àç›äQ™$›‹KXôö;ÍîÉßK—6tknOÒ×ÜB`èj}·Ž%±«©¨+ùìöcÑ.á‹£Vú›è¹Í”©¢ þ¥®‹Ú1ÇTšÃi+%ÿ£¾ò5ß¢<jßù@¿½éÑJÇ-"æ4³kïÏÏ¡TZòÿù¹èh/RyLHÖá÷÷Øë¿IÿéG6H·wXDþuŸ;ú—æö?e¹G4Ñ†XãÃœ9ª*°ð¦1îMqL¬ÍÛ?<y—ÇÕhì#1š©Ç#ô\!ŽJñaè)!r¢îËÓAŽª±Gr,ÉýøSºœ7Ó1qŽ!=@õ™rS3O §DÊ@F$*£ÏèližçÌ¦gH“-ñßyò<¥ÎŽÉˆ@óÏÐèœ±žécª‡°z¬ÿ(†CrN!  0707010000bca9000081a400000000000000000000000154fc7bfb00000456000000660000000300000000000000000000003e00000000root/opt/opensvc/usr/share/doc/template.container.ldom.env.gz ‹û{üT template.container.ldom.env å—MoÛ8†ïþäàKjôœ ‡‹‹Ûž‹‚–FÖ ÉòÃŽûãw†úˆìÈNÐxòÁ–Gä|æÕÌèúú’ŸÅ5\ð#î
k¢"ƒþâÞ!èÒ6opwÙÝ]–Ýâ¯á°×ÿ^ÜåóÞç/²ùÜï¬/o»Õ [?\ð#„<þLä±[æ³ÒAVçí–YCfsûd.±RIÇnO_¬ÉÖB™’J1ÜŽ¬%:4e8
ëÔZãÁzíðP°ñ[Mb	ÑÂ!`ª Ö[ò1)*jÆ–‰ .©ªÐ£‰Py†÷v™Ðo©h¯x¥»<ë*k'1o†hÃoA:z„aG¦´»ðæÖ¡W‘÷ a"6@f
zFØ1bŠ²ƒIŒžG(ßÆoSë7ŸžƒSQÞŽ´†è÷¢Úîì äÊ&/êƒHte=B¥´ÔkU<Œõj3ò†­UD>O&Þ»¼ç#LáŽjó{HöM¸?¦ÁVù¼+øä×½êü_V’Œ‘B›<45;ˆÙ3²uD»NààÅš£TQ!ÞVð§å<1ø%Sé„†C)*=mÑ|­±V[bw+øÌ‘ÃGÕ8ÝŽ]ò,å–â
T´1!Ûm´Q9ËBÒÌ¬ìóWVAÞÍéÔÒš³åd)>ƒ8
`K"3k/ÏPëž¶w†m0rœñG“~é1êÎÌò v	Œ6äÌÆ–\;Ä¿]ýíKi­ÂH­
jäÔ´FŸ¼qñH8gEYú®Ïh÷ÝÎ§âPR=Ï*Ý™ÃÀIŒžã#©«Ãž+ Så@¨Säšß‡ÎËYºÖITzFx¿ZÎÂe«8uP\x—‘G-¥¸¬Äù’‹H.AHGa\š=q4’{!7°NCÍ©éäs}9v§-?gežê¸(H ¹ ¬à{À*iÎU~Tž4=pÉIƒŠ4¶íÙø	¬ž*NÖCë³‚Pz§öá‡5ïLÓõBÒQ`E”Þõ—íooøõæwR&Ùçò–½ÿV3‡kò}éÒÆaíÚ“|›[Œm­oç±$v5õaåâ˜6!GÎ’[ÁWOô7)p›)CEÜ¡›¢öÌñ•…æ±@ÚJCÉŒ5BÍ§(G¾8E<ð†þ°fó+U”×,s™ïÃÕ”ÊHþ¿ºa!µ§„ä<þx½þÿÒ†Â“‹Òmà#)bßÑ„´kÿs¢3mˆ³!Î™£ª"ï<ÆÑ9Ž™µuó¤8­Fëž‰‘M/jq®'¥xÈ°ô’9Q÷åi† 'ÕØ#9–ä`I—óf:%Î)¤¨^!Snjæ	ô”HÈ„DÅú
Î–æ	yÁaz…4ÙÏ“ç)u¶L&ÚÝx…FçŒõ„LŸS=„ÕcýDÐ G    0707010000bcaa000081a400000000000000000000000154fc7bfb000004df000000660000000300000000000000000000003d00000000root/opt/opensvc/usr/share/doc/template.container.lxc.env.gz  ‹û{üT template.container.lxc.env å˜MoÛ8†ïþäàKêôœ ‡E‹íž‹‚–HkšäòÃŽûãw†”Ù‘ qvSH[¦È!ùÌË™‘//ÏyÍ.áŒ›«¬‰ôWwN‚~¨^cî¼«;/»Ùýf/?þ9»Íû½ãÏf¼ö{¹ÛZ_ß´ó±–ÔúáŒòò¯„^¶Ó|:ð,à¼Ý`@kÐ¬n›k©DÒ±]ÓoÖäÖJ˜ke¸´ÖÒIS‡ƒ¾¡²N,µÜ›¯t5~o0€‘²†ha)!È¨ 66ècÖ¢jˆZ&Ô»F¥¤—&‚òv=ð:
Òo°*4ÓmuÊÚQÌ«$C´á— >À?°ESÛmxs	ÖI/"­Â.D¹4cÐ3Â–QäŒbôÖFõö¿û4±k}{µ~“1dL¼_P¬E/«hýlmÆ\¡rB}•ú¿„÷ß1û,ñ rp'>
W‰µg(Ô’•'ÀXó!DšVø´­òó¬¾JÀëà7×ë]O÷ºØ…JjvšÖ>5&¯;ýu›§£êDuOqEÖ=ivêÍxûÇUêéøßYŒüø:œÅMQQ¬8­lQkˆt¦	\»wR(ÝÙä9µ@Ä5	z)•õ”Ðšãè’p“Q¾¦MP| Qõ‚îLÞÑFõ+V¿F>zîO 1Ä,[Úï>ù%F/˜:ýæ™¸H¡T×[ˆÙ2)ÝhwÎ	ä¼Ø·‚“*¬ØÚ¾Y*z»h”NÒ+ÙCµÇ‡ëÞÖR6bƒdn_ÈsòAÐ	)}ç4J¸9›®L›:m¯Œå!ñ	«»â¤œ;^Íñ3Ò’J¡	È S|qàÀB"3+·'¨µ§íaë›÷¹1*çƒ~êµ{&yõ Û †«{Î­5Æ	+¤ï¶¸îêäF„Z4’BÓRŠøhòsr@1‹Sêì¶‹hwíÊÇüPcà5OÊížÝ@AŒžýÃ¡«Åž3 Q¥…@hR¤‚¾$‡ÖÊIºÖqzBx¿ZŠÌe#(tS1I‰w©×HŠsÅÆç”Dr‚LúÐÃÔì‘¼‘Ü3þ¸‚eêsŽ¨“ÏùåÐœ¶tÎê<ÔQR`GSBYÀïAª¤)VùAzÒxO)'­]®}Ë»×ðªÇŒ“õÐùú¤ „ÞŠ]øaÍ;SÄx¾àpHµwÝmùî~¾"•I¶9¿‚yg¿hfNzÎUÚÐ­my’/‹í2Ž$±m°jö3†Öä!Æ’[ÀWk®oR 2“»æ·Ÿ°3Uã‰ãßå•ˆÞ'%n¸ ¤ù­¨¡]Ô["îiAŸ­™ÇüIäÿPXÌ¹g~PÃñÿâ‚uÔK„„T 	Éyùã=ÖúoR†Ê£‹\mÈY%zj+ú=—¶åŽ²Gt¢q6Ä)s*’ðNcìâ˜Y[7MŠãj´î‰©éY-Nâ¨÷v€ž"ê.=Mä¨;$‡’ìÛŸÓå´™Ž‰séªÈ”Šši=&R2"Qn}@'Kóˆ<`0½@šd‰>§Éó˜:“¶^ Ñ)c="Ó§T÷auXÿ1¸ #!   0707010000bcab000081a400000000000000000000000154fc7bfb0000050a000000660000000300000000000000000000004300000000root/opt/opensvc/usr/share/doc/template.container.openstack.env.gz    ‹û{üT template.container.openstack.env å˜MoãF†ïþrð%kl¯	rX »ÇbnOEaŒ%Ê",Í¨óaÇ‹þø’£‘";Š$)T>$öHâ¾Crtyù–ŸÅ%¼áGÌeF{Eíøcƒ`ÔÎ«l÷2so»º·e·ø³wöòó_‹›èïí½Ç‹…x°ÃãÁØü:­B«yôÓ~“Å¿YLÓ|S•“Y ±fOŽŒ&½½¾Î±P¡òiM¿G3¥sÊ•Gw=Í‘ýÉÝÙ½.3ÚTx2_{»ËxðGI4bÞÀÁ¡*À—{²>¨
j••Ì.¾;§¢@‹ÚCaM=ˆ½<äÐî)ko^ñL7ñ©[(ŒÅ¼è¼q‚tÐtÿÀtnî5Ìãv³ÊóÀÇHA#¦(+ÅèJÅ®®©Yo­	Í‡ÀùZá"d•	yò¨è{«SV³ª*“ñd  	›Š2¹E$¡ž›¨£ŸóÈÐ}}¾å`§ÍAMß_®•V[´+øz§êF²Ž-T~l‚$ÿ\Ï&¯¢¸ÅøÈ«8jƒó)	Rj¿~”°DÆÄ	ÎÑüšò™pniR.˜¹Ò´1!hÐ&Ç•Äï¾ü%²ì@M°´œ{•õïåç×qüaÃCŒ¬E®éª*VãQD™|çÊßL°R×y³×è¸U(ŒE(8ÉJÛH5èZ™×ì„7TæÙŸH¼3yË.Œáöjû1šWáþñv7Eôw_ì†¼UBËLÒ‹×¶eÕäÄhYÊÛ€vÇÁó%GË5˜QÁ­­àw#õ°³Kº¨j¥D(·´G;°µÁRí‰Í­àGÛ]ï]òSªYŠ)ÞiŽ¶ÚÅñ^mÔÆâ.TÌ,ï:Ã¨‚¸š‰Š6Ü‡Î@‘âˆƒ ¶$ÚÚ¿NPK»íaë‡O¹	î¥½h%ŸY^=À”ÀhË¥•QJ	`Œ”ÿO'›îR*7P«‚95mPù{k\D¸9äœåeê›.£Ý¦•Å!''kžU’Ïçaà$Æñ‘Ô•°Ç
ÀTy!Ü¼Ï§©¶8$+“tM#iPU3ÂûÝpž.{Å©Û\`É#.¥¸,Äø’‹H,NHŸGaXš-q4BóD<®`úšS(ª¸¥r#æ*Ãû,6\$Ð{9üá°ç*;(Oí¸ä„šaTa{ðîÀâ¾âD=t±ž„ªêèÖF¿3EŒ×IGŽ‘Û¦ûÚþï^Þ
Ê$Ú\^Á²³ßjætN¾.]Ú0¬©=‰—¹…@ßÖúö9–Ä¡¤¬<­\äR,8BÍ
¾[ª¥¿	ŽÛL¹U î¨³Ò2ÇŸ*
Íb†´—†ÒHó®?µ§ô-N;^Ð¯F/}|Yåå–ˆ¹} ×áâr¥%ÿ_\ˆŽz‰°ZÈcBj,®ßc¯ÿŸôŸ.³Ôxé6ð³à±ëèOBšÚÿ˜dh¢iŒósæ¨
ÏÂ›ÆØ#šâY›ÿß»¼W¨Ñ4Äh¦^Ý%Ðs…8*ÅS† §„È‰º+O39ªÆÉ¹$ûñ§t9o¦câCz‚ê2å¦fž@)‘¨Œ>C ³¥ùˆ<Ï`0=Cšl‰ÿÎ“çcêl™Œ4]x†FçŒõ™>¤z
«Ãú/ÅÏ6¾¦"    0707010000bcac000081a400000000000000000000000154fc7bfb00000502000000660000000300000000000000000000003d00000000root/opt/opensvc/usr/share/doc/template.container.ovm.env.gz  ‹û{üT template.container.ovm.env å˜Moã6†ïùäàKâÝs‚(öÖbÝöRZ¢,"Éå‡ýñ}‡¢ÙQ”t“	äƒ?hrH>órf¨³³—|œÑ¾Ø\atJKwNag%™Mûs/»º—ewòç~³gÿ:¹Jû½æŸœðÚoänk\y™ç×¢•h½xÁròGTNæi>‹Æó,dÙ(¯ŒVz}y×\ÊJÄ&ä5ýftj-„.U)‚ô—ƒÖRZ©KÔ×ÆŠU#æëºûßjåIKYR0´’äe UQ¨%m”Q4ÔŠ¢µD„Ð»TU%Ô*gÚ×y—n£Š®ó3]¥Q×T3Šy¥Æ¿ÒQ«[ú‡¶J—fëŸÃÍJ'Ö@~çƒlIé1è	afŠ¼‚QŒ1ªò]0|®ZïËL~D³„"U¥¤£è;9[á="\ÛbžL<ë¿&ºŒnB <Ò>¼:Ýo.ŽÀí[ÿ?¶¼[Ñ4=9&	¢üQ8‰¹’N÷±41ì!MpôZØY0Â­Dy¿¾6áCÑ`0UD­uOŽ>ƒÔr{s ÎRùªT#}"Ìæ¡kªYðíÁ’qj¨ù Y'Sš*äSùšj‚°CîõÃÀ²ŸG¸óÜ`±æÜ¾UÀÜŽÏ}Þ;	þf¢ãüNAµÒ£d¨Œƒ¼0˜ûJ7ÃŠÀ$O´ØD0ŽD°ŸÄ»7y-Œábý>Š‚gáþDBÔ„ðx¿KúäV*8ÁÔñ›gâš¬ÏgjUÈ“eÊÚ½s<œ‡ã È[Y %lmI_M+ïì*]51öPéÔæàx¬d-6
æ–ôž“·¢µM×wQÂ.Ø!Ãªµöù¼emt^K|lÀ¬ì+Ä¤‚´š‰¨W¨Gg ƒDñÄ;]¤J_'¨åÓöÆ°í›¹1ÔÔƒA?uŒòž!¯=ÀÀÔz’P6%rGT
Åþ†Ó_VjájTK„¦j;kH4ÑbVà©¯úˆvW>æd^ó¬ü÷|ì1<ù‡CWÆž2 ¨r‘éëp«ê’C¶2I×Xƒ¢™Þ/q‚¹lB·'Ä»èµ@-D‹Š/DR‚Hžc/S³SðF´øãœVqŸs*¡šèR~96×œ³2åk;	eI¿{‰±ÊÒS£nrbk»,]€‡'°ºË8I½¯'!š­ØùïF¿1EŒçGŠ(í¿vŸ}ÃÏÉ£2I6ç´èíwš9œÿs•6tk.OÒß(!dèr}7’ØÖª¨3—òÙðcÑ.é‹S-×7Ñ£Ìä®¬ ò;]ÔÿIhNRm¸ Ämô…¯±‹ò¨|Ç‚~1zÒC«À²XÌ©gWÞŸžR)4ÇÿÓSÖÑ^"RyLHÖÉïo±Ö•úÓNÙÀÕ†¼•EÄí>Wô.ÍåŠrh¢±Æ‡9sU€ð¦1îMqL¬Íë?<y“ÇÕhì=1š©Ç#ô\!ŽJñaè1!"P÷éi† GÕØ#9–ä¾ý1]Î›é˜8Ç z‚LQÔÌèC"‰rë:[šÈóæ Ó¤	KxŸ'Ï‡ÔÙ1hþã	3ÖdzŸê!¬ë¿-¤îŽ¨"    0707010000bcad000081a400000000000000000000000154fc7bfb000004c2000000660000000300000000000000000000003d00000000root/opt/opensvc/usr/share/doc/template.container.srp.env.gz  ‹û{üT template.container.srp.env å˜MoÛ8†ïþäàKj´×9XôXÛîi±(‰²‘H.?ìx±?~gHÊ‘Ù	wãB>$6EÉg^ÎuyyÊÏìNøas¥V^ ’ö
üÆHpÖ¼ÅÜiWwZv³?·›½üø×ì&î÷–w<›ñÚäf­muçW¢“Ôúá„dåß­ÌÓ|­ãYÀX½B‡Z¡Z^?5W²¡õyM_µŠ­¥PVÂKw=h­¤‘ªr{}]©(Z¹3_êîJjüÞ %e^CA°ßHX¡õA´Ð‰²!j‘Pï
ëZZ©<ÔVw¯ó 'í
ËÔyA3ÝÄQ·Pk=Šy¤óÚý¤ƒÂGøÖ¨*½voa.Ai…§5€Û8/;@5="ÌŒˆ"¯`#š÷"øÝ†ÿMª$?“d¨-ˆª²\Rni%M	~ûýî)¬E~4è° ­ÖÞßLƒïtýTä 5¶2é1‚ëÙÁglw_j+ßíóûô3à©ÐDŠðñŽcÀ¤P0ÂÒëˆoË†ø}laý™E¿oƒ—<²ÇN,9a¬±mÁÛ³Ë{Áßt°œ4ÀcG¸
Y3àZ´-GÈB”Ã4£#òŽ6á9”žö“ôšMÞÒÆp{±ü52Í›p†§œö»€Ï¶@oS§ß<'ú>r¶Ø¡Ï£eö€vïGÎ£H ÀYb%[[À7MÑxkUÝ©Ê‰+‹+i¶
Ùˆ’¹|!ÏÉGÑ™6õÓ(aæl
„s¸T.¶oµ‘¼>0¸Ð³ª/;¢
âjG,
*r& ƒHñÄ‰È,}=B-Ÿ¶3Ã¶mÞåÆ,¨Pú¡c”÷LòÚÌ—K
ó”®¨äX"ýÏes_7ÂÔ* ‘š
*Sž¬QÍPÌò<õMÑnóÊÇüP¡ã5OÊyÏûn  FþáÐ•±Ç@Ti!àšà©TOÉ![9JWƒ¢Þ;Mq‚¹¬…n‚ïÜS¯9ÙóšÏ)‰Äá˜ô¾†©Ù"y#˜üqEØæœZ`lÌ/ûæZMç¬ŠC%v4%”üádZŠUvžZ| ”:3¨b‡'°~Ê8Q½¯
B´k±q÷Z™"Æó‡#GŠ¨¬é¿¦ÿ}Ã×¿£2‰6çW0ïí'ÍìÎIÏ¹Jº5—'ñ1•Ò§\ŸÆ‘$Ö–ÍnæB—}A2~`,˜ÜYì¸¾	ŽÊLîÊ
 ‹µ*KÿQhV–W\PÒ¥Õ×Ð.ª-
´ ß´šûø&ÄóÛsìŸÃÅTBqü¿¸`m%BBJÇïIòþkýŸRºÒ¢ñ\mÈGYº˜çŠ~Ç¥¹üQ ]—2¢c×Míü”9ŠÚóýò(Æ-¢£×vf­ßí­Ñ9ªQ›gbÔÇÞeÐS…8*Å]†= —„HºOO9ªÆÉ¾$·í/érÚLÇÄ9†tÕ+dJEÍ4)‘(·¾B “¥y@ž{0˜^!M²D§Éó:“æ¯Ðè”±ésª»°z¬ÿöÛÐý     0707010000bcae000081a400000000000000000000000154fc7bfb00000456000000660000000300000000000000000000003e00000000root/opt/opensvc/usr/share/doc/template.container.vbox.env.gz ‹û{üT template.container.vbox.env å—MoÛ8†ïþäàKjôœ ‡‹‹Ûž‹‚–FÖ ÉòÃŽûãw†úˆìÈNÐxòÁ–Gä|æÕÌèúú’ŸÅ5\ð#î
k¢"ƒþâÞ!l×öñî.»»Ë²[ü5öúãß‹»|Þû|âÅB6ÿ€ûõåm·£dë‡~„ÇŸ‰<vË|V:È*à¼ÝR kÈlnŸÌ%V*éØíé‹5ÙZ(SR©"†Û‘µD‡¦GcCaZk<X¯
6~«)€A,!ZX#Œ@ÄšÅ@>&¥¡QEÍØ2àÑ%Uz4*o›QØeR@¿¥¢¼â•îò¬{¨¬Ä¼I¢¿édèþ…™ÒîÂ[˜#X‡^EÞ„}ˆØ ™)èaÇˆ)Ê&1z¡||g?¾M­ß|zNmDy;Ò¢ß‹j»³ƒ’+›¼¨"5XÐ•õ•ÒZP¯Uñ0Ö«ÍÈ>D´Tù<™xïòž0…;ªÍï!Ù7áþšB[åó®à“_SôJ¨óYI2F
mòÐÔPì fÏÈÖí>8ƒkŽVpXPE…x[ÁŸ–óÄà—L¥¥D¨ô´E?òµÆZm‰Ý­à3GUãt;vÉ³”[Š+P!ÐÆ„l´ÑF}ä,OI3³²Ï_Yy7§SWHkÎ–3A¦øâ(€-‰Ì¬½<C­{ÚÞ¶Á|ÈMXpÆMú¥Ç¨;3Ëk Ø%0Úl3[ríLTÿvõ·/¥µ
#µ*¨‘SÓU|òÆÅ#9àœeé»>£Ýw;ŸŠCIAö<«8tg>'1xŽ¤®{® L•7¡N‘k~[:/géZ'iPéáýj9O—­âÔ@qá]Fµ–â²çK."¹@!}…qiöÄÑHî…xÜÀ:5§R¤“ÏõåØ¶üœ•yªã¢ æ‚²‚ï«¤9WùQyÒôÀ%'5*ÒØ¶gã'°zª8Y}¬Ï
BéÚ‡Ö¼3EL×IGQz×_¶¿½á×›ßI™dŸËXöþ[Í®É÷¥K‡µkOòmn!0¶µ¾Ç’ØÕTÔ‡•‹_`ÚXp„\9Kn_=5Òß¤Àm¦p‡nŠÚ3ÇTšÇi+%ÿ1Ö|5Ÿ¢ùâñÀúÃšeÌ¯TQ^³DÌyd¾WWP*#ùÿêJt4H„…ÔBž’óøã=öúÿKÿ
O.J·X¤ˆ}GÒ®ýÏY@@ˆÎ´!Î†8gŽªŠ,¼óDç8fÖÖÍ“â´­{&F6½¨Å¹Bœ”â!ÃÐKBäDÝ—§‚œTcäX’ƒý%]Î›é”8§ z…L¹©™'ÐS"e ë+:[š'äys„éÒdOü=Ož§ÔÙ2™hwã3Ö2}NõVõ?ÄEé¿G    0707010000bcaf000081a400000000000000000000000154fc7bfb00000504000000660000000300000000000000000000004000000000root/opt/opensvc/usr/share/doc/template.container.vcloud.env.gz   ‹û{üT template.container.vcloud.env å˜Moã6†ïùäàKÖØ^ä°@wÅÝžŠÂ ¥‘5ˆD²ü°ãE|gHÉ‘E	’H*›"‡ä3/g†º¸xËÏÙ¼áGÌFEÝ%„½EØ‰å‹Í½íêÞ–ÝÙŸ‡Í^|þëì:í÷¦ÛñÙ™,ÿ÷;ãÊ«n	ZµÈ­ŸÞð#ŒþÉa7Í7Õx™¬3[òd4éÍÕ}s‰•ŠMèÖô›Ñ©µPº¤RôWƒÖ-êÒŸôõ…±jÝàÑ|¹»/¸ñGM4b	ÁÀÁc ª Ô,r!ªZUÔ.î]RU¡C r¦8^yt[*rç%ÏtFÝ@eÌ(æMDŒÿ¤£¦;øv¤K³ó¯aŽ`,:xà÷>`¤Ç '„#¦(+ÅèkÅ[]‘]mœ‰öCà|­pr¼Ê{²öžuÊjVMc
žØ¸n¨."Ùõ„Ø„D=ýœG$ènÛ´çKPnµÙi¡[¥ÕÝ¾Þ©ÖŠAÖñ>Cåa$ùçj6qe»IŒO€¼L­.úÐAH=¬Å-‰qq‚s2¿¢r&œ3M*3§ùŠ61Ú”¸”¦XSï‹_ËÔKÇ±W¹ðÎP~~Ç.>ÄÈZäœ¾£¦a5îE”ÝÞ9€ò7äu>ì-z.*ã*²’ÄÖª¸VYæ-o"ª¼ŸD¼7yÃ[ÃÔæc¯Âýâãnª´ß%|qk
N	uþ-3I-}.Ëj)t“eIoÚ½s<;/Ôì-o± Š[[ÂïFòao—tÕDÔìJñPéh‹n`kµÚ›[Â7öæS“ú.x”²1Å'ÍÓFûÔ~ÐFöúÀXàcÃÌÊ¾2L*H«™È¸qÍuèd(>€8p`&‘skú:A­;mïÛ¡ù˜›°àZz0èEÇ¨Û3Ëë °`´áÔÊ(%0F*ˆÿw7›þ’R+?P«‚94­Q…{kœD¸8ä˜dêë>¢Ýt+óCI^Ö<+?t{>u1xò„®{Ê L•ÂÅ{|›ÊÉ¡³2I×X	ƒª™Þï†ã„pÙ*Ýøâ.q,ÅE%ÆœDR‚ðBúÔÃÔìˆ½íþ¸„u<äœJQÃ%•1×>gej9)ˆ£·r7øÃcŽUnžºå”[¾†Qƒùâ;<Õ}ÆIzè}=)ÕìÔÞ¯Œ~gŠÏŽ<+¢t¶ÿšÿ÷//…Ge’l..aÑÛÏš9ž“ŸK•6tkWž¤Ç\B`È¹>cIìj*êãÌE¾ó{È†±h—ðÝQ+õMô\fJWQ ø½.jÇª$4‡ÒV
J#Å»þ”oé["nyA¿½éeUX"æü@žÃù9”JKü??$ÂBÊÇ„d®Þc­ÿŸÔŸ¾pdƒTx‡EØWôG.íÊÿäÑDbsæ¨ªÀÂ›Æx@4Å1±6ÿ¿wy¯P£±Äh¦^Ýu ç
qTŠÇ{@O	‘uŸžfrT=’SIÚŸÒå¼™Ž‰séªgÈ”‹šy}L¤dD¢ÒúÎ–æ#ò<9Àôi²%þ;Ož©33h÷à3ÖGdúê1¬ë¿c¹Ø£"  0707010000bcb0000081a400000000000000000000000154fc7bfb000004a2000000660000000300000000000000000000003c00000000root/opt/opensvc/usr/share/doc/template.container.vz.env.gz   ‹û{üT template.container.vz.env å—MoÛ8†ïþäàKjôœ ‡‹Ú=--QšäòÃNŠýñû))²+;A“ )¤ƒ?(rH>órfxyùšÏâ’^ñas•5Q(#ýÅ'i÷ãEæ^ñ¹×e·ø{ØìåÇ7y¿·ØñbÁK¿“{ëëënz#¶­^ña>^þ›”—Ý4Ÿ…<9ow*(k”Ù\?6×²IÇnMZ“[+ajU‹(Ãõ¨µ–Nš:õ•ub­åÁ|¥{¨Ðø­UŒ”5EKkIAFRÅRP>&¡i+ªÐ2BïZ5ôÒDj¼ÝŽœÎƒ‚ô;U•Î+Ìt“GÝRcí$æM’!Úð[NFÝÓ´W¦¶ûðæ’¬“^D¬ÂCˆrKÊLAÏ;F È+˜Äè­ÍÛSüæÓÄ¾õíÕúUÆ1ñ~©a-zYEëÈÑGœÁ*gÔðNció—¼îQõ›‡ªœ¨î”A8 ØƒÎ:œê¡Táã;;Î_†³¸éè ‹GÀ½Òš"äpÝÞIð/›<GAŠj+kc½¤FhÍG~Üã¸Y o±	H™D±ŸL¼7y‹-LêWl~Ðù"ÜŸH«³l±ß}òk½`êøÏ3qæJ¡$1­¶*v³e(ÝŽh÷Î	p^lá­àd¥U±µ}µÈWƒ]e¤+ÙCµW;Ž,ƒ­µlÅNÁÜŠ>Ãsò^à„”¾KŒnÉ¦H„ 6¦‹\½6Š×GÆò€ø„Õ}-çŽWsúÌ…´FÖž2ÅŸ ŽXHdfåçjÝi{gØ†æCnÌ•ÇhÐ/£nÏ× °`j³A°dl.©Já»«û’®a¤VA­DhZK­¡ˆIŽ³8¥.núˆvÛ­|Êµ
¼æYù¡Ûó±Ä ðì]öœ@¡Ð¦ˆÚ³$‡ÎÊYºÖqzFx¿XÄ	æ²Ýï2¢×’ ÅeÃÆ—H"9A&}ì…qjö
ÞHî	\Ñ:9§J'ŸóË±9mqÎê<Ô!)°£‘PVôWMÒˆU~”ž´ºCÊI[GÒ²\Æ'°yÌ8Y½¯Ï
Bè½xß­ygŠ˜ÎŽQ{×ÿ,ß}Ã¯WÄ“2É6—W´ìíÍÎ‰÷\¥ÝÚ•'ùõê±Ø.ã ‰}«ªö0sá"]|¹82–ÜŠ¾xµåú&”™Ü•€›¢©ZŽ?D®>Rí¸ ÄcÍ‡ÐbõÈBÄô‡5Ë˜¯ö‘¯û,æÜ3¿§‹ª…áøqÁ:$!ÈSBr^~µþ›ÔŸ¡òÊE®6ä½¬®@]EàÒ®üÏQ€AˆÎ”!Î†8gŽ¢‰ÞyŒ¢s3këæIqZÖý$F4=©Å¹Bœ”â!ÃÐSBD îÓÓANª±Gr,É¡ý)]Î›é”8§ z†LQÔÌè)‘È„D¹õ-Íò<‚9ÂôiÂ>çÉó”:“	v/ž¡Ñ9c=!ÓŸ©Âê±þõ(	=Í    0707010000bcb1000081a400000000000000000000000154fc7bfb000004d7000000660000000300000000000000000000003d00000000root/opt/opensvc/usr/share/doc/template.container.xen.env.gz  ‹û{üT template.container.xen.env å˜MoÛ8†ïùäàKâöœ ‡‹Ú=--¬A$’Ë;YìßŠrdGVÒÆY¤±C“Cò™w>¬‹‹S¾Î.à„/1Wit—,Â=ê×˜;íéNËîìÏÝe/>þuv“î{+7>;“³ßáÃÖ¸ò:ï¯U‹<zuÂ— røw$‡y›Ïªñ²Xg6äÉhÒëëÇá+›Ïô»Ñi´Pº¤Rô×ƒÑ-êÒÌõ…±jÕàÞ~Ýt_ðà·š<hÄ‚‚Ç TA¨6äBT´ª¨™Z"<»¤ªB‡:@åL;ðº,òè6Tt“—¼ÓMZu•1£˜×}0þ— 5ÝÃ¿°%]š­scÑ©Àg ÿà¶@zzB˜1E9Á(FYFÚ‡7çøÍÅŒýèÿ¡×ŽÑ•ÜV5g°¶eû"ßèQÞ
‡¼W¢¹‹øÄ±‡4!G¯•Ã ÜšC]îëk>/†*2Q«BÝ“
HÛG˜ƒ/ÉßAEúDXÌ=C×T³àÛƒãhÍ±}”¬Ã”L|)_SMvœ)”{û4ðcéôãëwž; ¬ÖR¶ÄXƒ{¸Ïw%ŸLtR… P‹ž[eË›†p_©ânX·LòDË—Æ*ß'ñîMÞòÆpµþ5J×«p‚†8k²ðä¾KøäVœêü¿ì$gß”…j)dˆÉ²$åíÞ9žÇá À[,¨¢B¬-á«iñÑ.éª‰):ÄC¥£Í^x¬°VbsKøÌžÃ{ÕÚ¦›»àUÊ.Ä(ïi­}Ž·¬Îëci3+û>&© f"«ÅwM3A¢øâÀ‰.S¥Ôr´½3l»á}nÂ‚;¿Á¢Ÿ
£|g–×`N`´^#gÐ¦äÚ© ~Ï}xßR×ÊÔª FNM+îu­q¡‰8gÙú¦Ïh·ùäc~à
#gž•òÝÀIŒžü#©+cO€©J“éë¸÷ïŠC¶2I×XIƒª™Þ/†ó„pÙ(NÝÞEàYî…`Q‰ñ‘T ¼>ôÂ°4;boDûŒ?.aw5§RÔD—êË¡¹Æpœ•i©å¢ Žæ‚²„?<rƒÆ¹ÊÊSCw\rbk»,ýLF`õXq’z_O
B5[õà¿ýÎ1^/$yVDélÿ±{ï~¾I•I²¹¸„Eo¿ÓÌþžü½tiC·æö$}Í-†®ÖwëXÛšŠz¿r‘Ï¾`Ù00í¾8j¥¿‰žÛL™*
à_êº¨süG%¡9,6ÒPò?Úè+_ó-Êƒöô›Ñ‹­yÜ"bN3»öþüJ¥%ÿŸŸ‹Žva!uÇ„d~½þ›ôŸ¾pdƒtxEä_÷¹£ßsinÿS;Dmˆ5>Ì™£ªoãÑÇÄÚ¼ýÃ“wIq\Æ>£™z<’AÏâ¨÷ö€ž"'ê¾<Íä¨{$‡’Ü?§Ëy3çÒ=T/)75ózL¤dD¢2úÎ–æyÀ`z4Ùÿ'Ïcêì˜Œ4ñÎë™>¥º«Çú¼ ®N!   0707010000bcb2000081a400000000000000000000000154fc7bfb00000509000000660000000300000000000000000000003e00000000root/opt/opensvc/usr/share/doc/template.container.zone.env.gz ‹û{üT template.container.zone.env å˜MoÛ8†ïþäàKêöœ ‡‹Ú=--,"2ÉòÃŽ‹ýñ;CŠ²ìÈŠÑ8@ºòÁ´4"Ÿy9ïH××—|Í®á‚/Whå…ThoÀïÂO­ðá.;»Ë²›ýÝ-öúÓ?³»¸Þû¸âÙŒ'ÿˆ»­¶åm;%ÖH£.øbBi±½ÌÑ8¾
«7ÒI­¤ZÝî‡K¬Dh|;§?y®4ZUÊRxt·½ÑªÒë
mÄ²Áƒë¥Ã]Aƒßké@!–à5,zøa#­¢µ(jÂ‰ ]ÊªB‹ÊCeõº—v>É¡ÝÈ"¼ +ÝÅ³î¡Òzó* óÚý¤ƒ’Oð/l¥*õÖ½†9‚6h…§9€Û9kjzDØ2"Š<ƒAŒVk_½=Åï6@Ì£o¯Öoè]ÄÄë…Šµh±ðÚî@'Ñv›œÁ%*#ê#ð¦¡©M\°MF•Oª2¢x¤ ŠtÔawÂi N	ó¿‡Éû×»¢RÉëuµö‹†­³
MFø:äÎ@nûÒìJf)Ý#T²ÁD˜Ã½@WW“à›Á‚¶rEµñ$Y‹ÑŒ
<—/©ÿ4á.ÆCºî$X·ˆ÷›¾›¼ÇP™àµddÂúwæöŸ^5%ëˆ©Xqƒ´•¤ZOîDuµ];þ¦ƒå&	¼\££¾«Ò–ª‡hæ¾¤jÜo«RM^Ó"Èé@žÖÉç÷´„A{«ß£³zîÏÐHç£«ÑzðÙ.¥·‚©Óo¾7¶Á¥·‘ké[ˆ12ÕrÝ£“ã(yTm8ƒ…¬dÁÑðM¯qWªª	qwp†J+7Õg‰¼“(Ü¾PæðI¦cçt–0sÂ9¹Rmc“µ‘²ÞOp¸Ìmv²ežÍˆi„%5õA¤øb/‰D2‚øu„Z»ÛÞ¶nø³ “ÞI¿´Ú5“¼:€m“«Õ}J—dÍA’>ÛÛÄ|ÇW×S«€©4-Qø}4ò†`€jwÜ³»\ÑîÛ™å¼†ç<©<´k>N1xÌ—®{t ¢JWO·¦ÉÚ(£tµá2(š	áýª©N0— Òí@ñÎ=5§>æŸ“‰DƒpLú8}k¶’²Ìù¸eè<§²	6úËq¸FÓ>+ã©†LM†²€¿RÿKµÊöì©‘d9amR+Ÿ"ôw`µwœ¨‡œëQAˆf+vîA¿}ã{	¿àräH¥5ùkúÌ¿ÞÊ$ÆœßÀ<ÇOš9¼&ýÏ]Z?­m{ÿ^ìïÅÓy$‰m-‹úÐ¹¤ksA2¾,˜|µrÍýMpÔfò¡¬ p;UÔ–8þQh”±‘§J«®¦U”GwG4¡?´šûøäÏóÓ@s<25úWWP
ÅõÿêŠuÔI„„” 	ÉX|x½þ›ôŸ®°Òxî6ð	‹à1wô)mÛÿXd‡h¤1Úù)s•'ácìqŒ¬õÛ?›z—‡Õ¨Í31ê±§O-è©B”â!Ãè%!R¡Îö4AƒjÌHŽ%Ù¿¤Ëi3çÒTgÈ”šši=%R2 Q=C “¥yBžG0{˜Î&E¢÷iò<¥ÎÄd@ íghtÊXOÈô9ÕCXë$t/Þî#     0707010000bcb3000081a400000000000000000000000154fc7bfb000003db000000660000000300000000000000000000003400000000root/opt/opensvc/usr/share/doc/template.disk.env.gz   ‹û{üT template.disk.env å—ÁŽÛ8†ïóÌÁ—6è¹Å
,z,
´{Z,
Å¦caI+JISôá—”åŒ“ñ$A‘ìCâÐ%}üM2×¼îàŠ—¸k4=]ÑÝ/vw]vwÿÈaÞý{';}ÂÝÖ…æ}Y- E"?x{ÅKˆü/é€e¥OÊŠÙ·Ñ¤ÕvõþÙÜ`«’‰e[ïÄT+ÛèFE$±~v¶ôh*+ÕÎ«¥ÁÞü-$Ì‡åÑT³í[‡ Vh#lµ1Ã¢ÎJî\
5‚…¨×H°ÄÖ„VÃû„¥ªŸFQæ¹‘=®ùÑPuäó,xÅƒËG>Âî¨Vtc¬„WÄýŒ¦®Íç]ÀÇ°Ô1(¡Î¿e%‰°”F¯u,³gdëˆöâàÅŽ£EkÝêZ¼-à«[ã³_m[“Ðr(%BMÐ#_KìÔF³»|âÈáµö¦[ñ,å+qŠH¯,eû^}ÔGÎòJ†™5@6ºÆ¬‚¼›Gh›¥%á­½rB™âˆ£ ö$2³þöµò¶Ý¶½ù›°€_ãI¿õ•3³¼ö KÓ«ræëdŒºÖü­Û»H:E#µ*èSÓU|ö¦-$œ³¢,ýaÈheçSqàb"{žUÊ™ÃÀIŒžã#©«`Ï€©òF€º·í‹Cñr’®ó’•™Þ/Žó„pÙ(NÝŠoyT,Åªç‘\ HHGa\šƒæh$&o`™ö5§UÚ¤ëË±;ãø=kòTÏEAÍe¶Ép®
£òdô—œ´öÐjƒ´£ˆëñØ>Wœ¬‡!Ö'¡ÌVíè»³7¦ˆéz!éˆXMðÃmÿ=.UIYõœL²ÏêTƒÿ^3‡kòséÒÆa-íI~Ì-Æ¾Ö÷óXÛN×ÝaåÒTbÁòqä,ù|	z-ýM"n3e¨( hgë.0ÇŸ*-`z#%ÿ°Î¾¥ŽOÑŒ|I§ÎúËÙ*‚ÔÃØé¼¡~dÿ·åþe%ÿßß‹Žöa!õ§„ä~¿Å^ÿôŸTí£tøëqèèBZÚÿœäÑ‰6Ä;Šsæ¨ÚÈÂ;qèÇÌÚùyRœV£ó/ÄÈ¦³Zœ+ÄI)2 "'ê¡<Íä¤$Ç’ÜÛÏérÞL§Ä9…ô Õ2å¦fž@_)™¨X/èli¾"Ï#˜#LH“=ñç<y¾¦ÎžÉ„@Ëƒ4:g¬¯Èô%ÕCXÖÿv‘dM†   0707010000bcb4000081a400000000000000000000000154fc7bfb00000558000000660000000300000000000000000000003400000000root/opt/opensvc/usr/share/doc/template.drbd.env.gz   ‹û{üT template.drbd.env åY]k#7}Ï¯PÈÃ´8Ò—,ymö¥]È¶/eYdÆ‘%UÒØqéï¹ÒŒ­ñWÂÖ¦Blk¤+ÝsÏýtuuÌçâŠñ!qµ×GwÄâŽ‹ÝÅŸ¤ìÕÝçZé³\.¬¯ï»Ù¼h¼9âChxùW«¼ìfyÏuÔì¼« ¬Qfr¿n®eÃ[»%ýjMjÜÔªæQ†û¢µ–Nš:lôÂ:>Ör0_î?M%3|&™mXÄ÷d}¨n[/$ã!X¡0SÍ*NÑC¤Ÿ+¼ì{
+ÿ†5<ýñ“/NŠ˜$®„	k5i=Ð“5JK-½VµdÊRª[Å--eTß¢-p„Ïj”Ö[…¥Wê®ZË^(­YäÏ˜…{R¦æ¥ÓJ`V3É
¬§žÙõ-°€ï Ž=°ÆÚ‹=„ˆÜÇ#ÅÝcÄ'ßn‚O¤‰¦~ÙY‰tg|¹aQÍ ØX67\kBxÌÅs>Æ	fP"ZÏ¸ ã÷`'‘PaÜ‘Oþ¸÷#Ó*Ää}ÐwÄýXEÏ	uü¦™ 0k<Pj5S±1I&¨ž²»rà†ªQ‚¤Ø“…£¯ä*ÓèV˜2;¾šK_ÈË)Ÿ+ˆ±÷°œ|á3§sß
£¸«H…51aèêÙê…°4 ´:…’.€$¤Õì÷¹ÐŽƒ<5—û4H(nX0#‘0Ë_ ÖyÛ‰Á¶jâFX°ÊA_åFÎ²ÞL
^M&‘'ÅyÀ¨DÊ79Ýõ¹lÊCÁVÎ¦¡i,y\KS†µŽ!fEšú]Ñº•ï²C­­ù¬ìÐé¼i1<Ù‡BW{Ê @aaÚÆÚ.rrè¤D×:
ƒ\Ÿ¼-âá2çÝQ­SEôª¨X5$EÒcJÞ´B™š½‚5Z÷Š=®Ù¸]åœ†+Ýú”_6Åi?«ÓP‡¤@†FB±ßƒlZXå‹ô¤Jµº¹TŒ…eˆrVz`³Î8‰½­‚ë_†/Öœ#vç‹Tv‚µwý×üÙ7|}Q¿“&IfuÍª^~æÌpN¼§*­4kWžä*™=É˜s}J,¦JL‡™5v¶,äb!¬u#öÑ«Õ7m@™ÉúrœJú©Žçí—Bª9”øa¬¹	ShQ²"ž± Ÿ­©"£|˜j{"sê™Þ³ËKVsCñÿò’x´¢ˆ”AÞE$çå—S¬õ¿Iý„W.Rµ!_¤h±!ê*úI»ò?ErÑ2ÄÙÏGÞDï0Œ+ˆá˜°¶î<QÜÍFë¶Èˆ¦W¹x® î¤âÃ ×ˆˆ@Ý§§3r'{H6)¹j—çé.rî‚t ÕhŠ¢æ<ÝGR ²ƒ¢Ôú‚ž-š{è¹fÓ¨	IøžxîcgÆdA»oàè9Ãº‡¦Û¨Á:tø*t¦³CôˆG,Û›çMÞMÚîdåzu©U^ˆp‘–Ï8‹K'o~dß-¼Šƒ#ï¡Û æòºN°‹öÜ`'môòû„ìÌIÐNw-æ¼õÅŸœK¿,d9N—o6ïyi7ž:ä-îÄ[:±‰‘‹i¾%(oéFì—Ì4qÅlùp ž&g.æáÆF6ÖV<…nhóþ¡Ù8Øï_±i¤†®;üØ]+ðÈÓXÐƒy£4Qãí¡`¸0Âx¤“£®‡…æÝÙCa™|ÒEg4'Z¯)Õš¸UƒBüŸ§ÛÀË	ú›•›Œ9ÌOÖW×MÿÉm°  0707010000bcb5000081a400000000000000000000000154fc7bfb000005c9000000660000000300000000000000000000003200000000root/opt/opensvc/usr/share/doc/template.fs.env.gz ‹û{üT template.fs.env åšMoã6†ïù…[ 1ÒëºÅb¶X ÛSQ´DYl(’å‡ûã;CR
å(J°ˆS£ÖÁq(iH>órf(ùìì5“3xÅƒÌ5îUÍ½âæ^—ÝÉŸ;»üë„ÆyÃ··ÚÖïr_5ß`ãÅ+ÄÂò‚°<÷òÅN­ÆêpB+¡Öïà#“.6×¼aAú<¢ß´Š­Sµ¨™çî]ÑZsÃUív®u•6l%yÙ]ºÚUØö¥å°’ºº¡ÙŠŠC#$mã_·užw :¶Î'Zí<Ž|Ë¿×zÊ/áƒhn¹ò½e5¬88î!Ð
êþŠÂ”Ò80.wB÷þLmŠuxëVyv‡ãÿ	›á
­O¦wŸæ¾_Ï>ú/]G3…È†`á²µ‘:tÃ~mˆõy&ºŒø)\ÜrÃÈ¼<§ãõd×0ß‚ÕÚ7nIÌc_OCï¢÷ËüWK6¼Ã¼yÛ¢€Ñ'ÔÃbˆÔË<´kmöî­Å:Gç‹ý»žÍ†ßšý¯åƒÐUEqÒKøÃá’DmÕÜsÛ	Õ/rÖ•î:ìžÎÇ#Jºg†£SÌ\;qÿÿŒibäó­p£æ`„¬)ï°ƒ€ÔkQ1	-CóÞ(4*æÅ†ËíÃßÿÝ¸sØÜÑçr¹ü!u„q6¦E½áÖ
Ê`ã™§?^~ºÙ	@^!O¢SMðÉËÔ¡kµ'—¤ÂÜp»®,gºWi˜)qœ5XL–ÕE}C9lÅÝ®± D4ô‰¯Ùj‹ü£š¹ÌH
Mzf-x]î!ÍbýCÁ^H	ÞnÉWyî¨¥,(ð¢C×¯x£1)4LJÒÄŠU7îœ`;œ„G¹±Š‚aDÞ›¼Â)LFB¶vÆzUÍ{Âù¸Jp¾KxoWÂ[FÔñÿ¾ˆ94JÑ¡zÄh™Ò¨¶ÉªGça5ƒ+ßðJ4¢"kKø]SåÔÛª‘«*­ÀÚâÒ·…­oÙF ¹%|DÏñ;Ö™®]à]Ì,È0çÄZÅð äõÂX¼Á×®ã–*äÆi43a<¬0Ø"ÅG&)XÅ¯seVZm†mhs#ðµ¼é›–Qž3¥‚``b½æyâ^1Š
3ˆ”–²¡eedÐrM+L5Ö„¢}Æ,Ïc)—#ÚUùäŽZ8óQù!Ïy×ÄPàÑ?º2ö˜*Õv®¾Ö·)9d+³tSaÍäáý¬1N—ÃÐí 7²°ðxÕ‚j¹ECÆ˜Db‚pDz×ej¶½Ì3þ8ÇJrÈ92Ø˜_vÍam¹F{t«Á¤@ŽÆ„Ëú&HŒU¶HORÜ`Ê	)
Ãr6'ê¡÷õ¬ ˜¼e[w­Õ)b:_¤G?_1ñšþkúÛ7|û†`R&Ñæâ½ý¤™qŸxžª´Ò­¹<‰§±„à>åútJâ¶U;Î\X^'_ ‡Lù¸+˜%|¶¢£ú&8zÌ–·-±ho-r¼gQh–W\lâ&Ca_êÂµ8‹ºÜdwƒú ÕÂÇ§lqkBbŽWÆópz
5SÿOOIGƒDPH	ò”Œå×‡Xëï¥þt•ÆSµÁïxE²\Ñ\šËÿä€h¦1ÚùcæÈÂ›Ç8 šãYksœ§Õ¨Í#1bÓ³Z<Vˆ“R3ì='DÔ}z:B“jì‘ìJrhN—ÇÍtJœSHG¨^ S,jŽèS"E ¥Öôhi>!Ï˜¦H-áçqò|J‰É„@ó‰hô˜±>!ÓÇTÇ°f°nÖ{'9ý&²o}›‘ñ÷ù5UÜ4®­f÷­UþU‡kuõøÉÇ0xÜu6V§á›õÜcí·x1ylã+5¡àÓ/=áW(àa|ñ©íâËAãŸ”¯èþ;¦ÊXV&     0707010000bcb6000081a400000000000000000000000154fc7bfb000003f2000000660000000300000000000000000000003a00000000root/opt/opensvc/usr/share/doc/template.hb.LinuxHA.env.gz ‹û{üT template.hb.LinuxHA.env å–ÁŽÓ0†ï}ŠYí!—¥âÌj+!Ä!$à„r’Ic­kÝnÏØqºi7ÛVP¤JÉ¡M&öØþæÏÌ\_Ÿóš]Ã¯è®-oÀo,Â©ÃãûûtwÆ‹Ý—Ýì[[^¿þ>»Mç½ëO<›Å?àfm\ý&/î¼pž_¼:ã9ü¤Ã¼Ò;¡£Ù:³’$–zñæÉ\c#‚òy[¯£©º–µðHÑúÑè<Ð¢®)ì­T+J…ù‹˜Ë£©bÛ—A,P{XK¥À»xÓŸD¼3ÁU¼\"A‰qPŠ÷	¥¨Aç¹ž=.ùÞ8•çóÌyÅÛÞåa·º0Ö=Â3â¾%ÉƒiÒyçpïJéˆÔù9®Ä€!Ö¥’Ké3ÄäÙ: Ý‡8x¾åh‘ÅJ6²ŠÞæðÙ,ñÉ¯Ô
¨9”1Bµ“+t_%¶b%ÙÝÞqäðQ,­êÆ<KØ"ºA$š’}«.êgiÅÌj t+YaRAÚÍ4ÆŒŠ€BIxiŸÜÿA¢øâ €‰Ä¬»=@-m†mkÞåYÀïá¤¿úŒò™Y^[€9ÉÅ9ó€652FYIþ—M‚¥­ Z´È©©DáŸ¼IÁç,—¾í3Ú]ÞùXjIqÏ“ŠC>ó~8‰±ÀS|bêÊØS`ª¼ 6øÚ¬»â½¤klLƒBMï'Ãy"rY	NÝ‚oáyT,Å¢‰Î."©@P$½…aiv’£ì‘xÜ@¶5§R—êË¾;eø;«ÓTËE!šÊ¾6Aq®rƒò¤ä—œ°´ÐH…´!ËáØ<Uœ¤‡>Ö!ÔZlè‡Ñ¦ˆñzÓ±"jgûÛî¿7œª’¼ê1™$ŸÅ½ÿN3»kòûØ¥ÃšÛ“ôš[ô]­ïæ±$Ö­¬ÚÝÊ%)Ç‚#dýÀY°søää2ö7¸ÍŒC£€6ºjsü%’ÐV(W±¡ämô+jùõÀ§ˆÞÐ[£±úV¦u#Ó{¸º‚Zè˜ÿ¯®¢Ž¶a!uÇ„dþ¸Ä^ÿ¿ôŸT9i}ì6ð«à±ïèwBšÛÿ”"È-¢mˆ5ä§ÌQ4ž…wãÑ!Ž‰µ±Ó¤8®FcŸ‰‘MGµ8Uˆ£RÜeØ:&DNÔ}yš ÈQ5öHö%¹µÓå´™Ž‰séªdÊMÍ4¾$R2"Ñh=A “¥ù‚<÷`0 MöÄ¿Óäù’:;&#Í/NÐè”±¾ ÓçTwaõXÿ Ouù•    0707010000bcb7000081a400000000000000000000000154fc7bfb00000417000000660000000300000000000000000000003900000000root/opt/opensvc/usr/share/doc/template.hb.OpenHA.env.gz  ‹û{üT template.hb.OpenHA.env å–MoÛ<Çïù,zð¥vnÑCaxNÏl;Ã Ût,T‘4QJ–a~”üR;qÒbÍ€ ö!/´DI?þEòúúœÏâÎøDwu~~g>XÔÿ=¼ÖÝvw^v‹¯u~ýöÛâ.÷¾=ñb÷ýˆ»­qåm»¶kdë›3>ŽÃA:l—y/ÅUÀ:³‘$–zuûd.±AùvOÿ¬…Ð¥,…GºXKäÃ”´7–
cE®p´^3œ
6~²XÈj¾F tY`::ÂòæEÂù…_Â»fGÞÄWƒxg/y»äæ*c&;$Ïn/ŒñÛ×þìÂ˜ïgÆ'V¨=l¥RàÝ.’kÏ"þ2ÁElàå	r¬ŒC¨„R¼OÈEñ8€ÜP‡5Â¢ð|žD»syÏG˜ÂíÅŠ.Œõëõ|€û”$¦Jç]ÂƒË¥w"Rçÿq%Üh›Q*¹–¾…˜<#[´»àÏ×-J·EÑÛ>xïWêJÔÊ¡ÒÉº¯k±‘ìn	ï9røS¬­jÆf<KØ,ºA$Wš’½×ÆÁ]K((fVv/© íæø£^Ú•û2H ØHÌšŸ'¨µ·íÂ°õæ1·È~'ýÕ5jÏÌòê¶	L®VÈ™´)¹dYHþ–Õ¨‚Ô‚jOäÉ›Ô,pÎòqé».£Ý·;ŸŠC))îyVqhÏ¼Nb,ðŸ˜ºZì©0UÞP|i¶Mqh½œ¤klLƒBÍïGÃy"rÙNÝ‚oæyT,Å¬ŠÎ3."©@P$½…aiv’£ì3ñ¸<ô5§R—êË¾;eøž•iªå¢Íe	_« 8W¹AyRò‘KNX[¨¤BÚ‘ÇõðVO'é¡‹õIAµ;únô…)bº^ÄtD¬ˆÒÙîgóÝþ¾Gž”Iò™Ý@Öùo43^“ßÇ.mÖ¶=I¯¹…@ßÔúfKb[Ë¢W.Im,8BÖœ»„N®cˆÛÌ84* h§‹Ú1Ç_"	ÍarJþ£~C5Ÿ¢øâñÈzgtæ!ÖC_Ë´¡fdzWWP
óÿÕUÔQ/RyJHÖá÷KìõÿIÿI…“ÖÇnb<vý(¤mûŸ²@Ù#:Ñ†XC~ÎEåYx§1öˆNqL¬'Åi5{ F6=«Å¹Bœ”â˜aè9!r¢îÊÓANª±C²/ÉÞþœ.çÍtJœSHG¨^ Snjæ	ô˜HÈ„D£õ-Í#òÜƒ9Àôi²'þœ'Ïcêl˜L´}ñÎë™RÃê°þÏ?2õ   0707010000bcb8000081a400000000000000000000000154fc7bfb000005ca000000660000000300000000000000000000003b00000000root/opt/opensvc/usr/share/doc/template.ip.crossbow.env.gz    ‹û{üT template.ip.crossbow.env åXÑnÛ6}ÏW0Èƒ;À²¢íCŠ +ZèÃº`íö2-]ÙD$’#)».öñ;—’lÚ‘`K c2Û¡ÈKòœÃ{.sqñ˜¯³ñˆ/§ìT„µ%‘;ãýÌ¬þ[¸G|!Üãbwö‡²—ž½‰û½ÞìøìŒW~Kë•qÅU7»²-é[À“çøbˆýÕ(GÝTdå‰›­3Kå•ÑJÏ¯¶Í•²©B·®OFÇÖ\êB2¿JZ²¤¿××çÆÊYEmó×PÜ0zûm_$”äJ™“Ð²&}“æ¥ˆÒ¸­2”•E-r£K5oœè!Ô›+`º|yÍ'‡²ÝÚI!i–äÄj¡ò…ø?ÿþN¬TU‰àÖ"áƒÌoEÀ8On©rJŽ²™x'µ˜‘(TY’#DéL-°¡MAÇ¼¹)¦îfÊ¥'~›üÄxIÞ1õ–rU**¶‚ÀÒ˜òàQPøþÓçŽ¼2Â«,éMãrz #	è¹9ÊÖ ¼ÀOm|H¢5žÊ¦Šª‰}×B&ËÀ"WÒ©–X].xžûý¯7I¤“O¹»CŸf¦	$¾Wê–ª¨Ë†…ä¸Æ÷ˆ$¡ÀSÑäœ	€ìYþ "†‡õ½eäiõtÙõ{”£±''f¹Rú–
€ž‰%¨Ó¾—ò)ÐUSÏÐ/Ê;Ì+3““ú)™Òp„?6\Çöáëµô·ÿ€*ôñ²r$‹õMÓ¢çŒ	Iæ~v,“v¸q´‚ ý-'|jí™øk—Á Ã«.l"ïPŠá*Ç‹>÷çöYÑÔõ:vçpFÖw‚ñJÈ¢ÜàÙµísUC$ìâo–/§pö+ñâÕ«¬ý}‘]NpÈƒ¬’hÝ€×í€×/£¤ú_'^e—ƒê‚Â9ùœ˜º.ŸàpË9çÈÔÔ»½#=oÎ9èR5yPdÁ¬pìçb÷Oý¢•AM@>Bæ›
«y-†árîOë'8Éoq|ˆ&„ýfâ­›©à$£Ž¿{'…OÆU©Z…ÄyÏ{r<È°ÕYpÎÑ2ñÙp¾îã*]Vé¼Má…S¨ë’X3ZÈ¥B¸L| sôMÖ¶jûN0JÚ	‡Ò{5×¾Í9½6ZÖ“`q€oª˜ºœUWsØPaå°È ¢xÄ„À‰ˆYûõjÝi;1Ø6Í»¸1âïtÐ¿:FÝž!¯€]Só9.,²-f}£r…ÏÎÆz{ZÈÔx¤XRÓŒ`9ÛºF‹Ærýxê7}F»îV>ÄC¡<¯yT<t{Þ§~®#?œºÒj¨²_ûE
³jÍ¡‹r]c9F¼7y‚qYJ¤îö†3	è5â¤äà˜H4ÏHï³Z³ÃÕš¾‡©˜5Ï)¥ªýe?\epÎŠ8ÔÂ˜hJ&~kïx\~mí‰+?’ÐŠRUä×>PžÀrë8Q=×G!«•\û¯FŸ˜"†ý‚Ó‘‡"
gû¯ígßðP•t³Þ'“s2“>~«™Ý9ñœ«´”Ö®<‰QBðÅš3g;Îô·ŠçŠ·æÙû¾ÍÄCýŽú¦ñ(3¹++@øµÎ8~ÿƒC¬œÔ’Jü¡~îØE‘^5”¿Å‚Þ=	|Çlo+,æØ3>çç¸ÃkÎÿçç¬£D ¤ä!!YG_O±Ö’úÓçNÙÀÕ}£¼	ÔWô;”våÌä¢#eˆ5>ŒGYâ²{ŒˆŽá±6vœ(«ÑØ;bDÓ½Z+ˆƒRÜÅ°è>!"Q÷ö4B ÕØC²/ÉMû}º7¦Câ‚tªÈEÍ8=$R 2 Qn}€@G‹æyî™Àô i"ÞÇ‰ç!u¶˜´{ð ŽÖ2½‹ê.X=¬ÿ Ã†åÒÕ#    0707010000bcb9000081a400000000000000000000000154fc7bfb000005f3000000660000000300000000000000000000003200000000root/opt/opensvc/usr/share/doc/template.ip.env.gz ‹û{üT template.ip.env å™_oÛ6Åßû)äÁ-à
YÑô!E€-
ôa]°t{†‚–(›ˆ$r$eÇÅ>üÎ%)‡²e'h,˜Œ6hò’úÃ{Éøôô˜¯g§ìˆ/
'õQÃñ…pÇe÷ìO©OÏþzFë¼ë•2ÅEœË­µ@ëË#¾†·Òˆ8ÍG^Yš…i£–ÒJÕÈf~q×\ˆ’·•‹kú¬ßšó¦wÂ^ÄVöË²v¦Va˜Ma·†Ù\i>«DoêÐÝæhü²La ]æäÂÈ¥0¬áµÈÐï-a—!Ú0©±|tb_L{`¥¦Û‚$'LÉsáá0E˜V™/Ø¯ˆxýÇ{¶’UÅœY3§˜u<¿aã¬0K™‹dOH±÷¼a3Á
Y–ÂˆÆ±Ò¨š‘¨*E ±
_ÌSÇ™rn½‘›üLhIzb
jµÈe)Eáô
AÂR©=
ÒàQHøáóu¯ôxáw#¬jM. H}¯6ÕÀ¤eøW+ë’h­e[y×ø¾kÆ“e`‘«…hR/‘»Œ³4÷‡ß®’H5&ŸRwƒ>í¬èÄñs%oDå}Ù{B·àWÙŽH
:mî ƒ °=Ù?Ê3Üï¨oA‘ÇõÓ±’èê›·ÑØ²©\ÉæF€ž±O%¤sÓ®—´)èª­gèçíFæ•šñŠÄuÝ¹ý[çñû†KßþÓ ~¨^s{óÿW Tá_Ï“ñÊ^¬74U g”rIæ~~(“Fn­ðþ&´kõ‹Œý‚µs§áeL ›È=I1\æxÄ¢ËãÝ¾}^´u½N†ílN¯z/­ôEæíV*Ss˜Ë&Aûtµ|=eâö‚½:?ÏÂÿWÙÙpÀ!w¼J¢ÅoÂ€7¯½¥º'¿L"œggƒî‚Ã)ù<1w=ÂææsÊ‘iQÏŽô¼ÙçKÖÂB:…B¼Â¶Ÿ³ªZ/‚j<ìÃ¸Ï«}ò’ãv|nŸëGØÉï°	¬óEÏ›±wf&áD¿w•uÒï¨JÖÒEˆ>òVuîÄ±Ï- V,Á9EËØµ¢|ÝÅ•MYµ¢ÉC
Çß$ÖL,øR"\Æ>B9qËk]…¾ŒâzB¡·VÎrNç zÌ°måSCÌ	á”M«Ù_PQÊQ>F`Oqb"` á™…P‹»í‰aÛ4÷¹Üç’Aßµâ3Ã^€1Éù³¶•¹Ä÷XÆºò´àiáál!šf%çî\Ó°VÓùÓÑÔo»ŒvW>¤C!-­yT:ÄgÞ–
ü¼ñúPêJOó JõÚ.ZW¨U(1ÊAºJSô5~,x¯òqYr¤îpÃ™8ôš0XqRRð	Šˆ/–Ho«–fƒ«<}S6k75§ä²j¯/Ûá*…}Vø¡E„FAÉØïáŽGÇ¯»òD'?†#¡f¥¬„]['êt–wÇû¡Óú !xµâkûU5OÌÃõ‚Ò‘…#
£»Ã÷®áûÿ²5hs2e“.~ðLN¼O§´TÖx<ñoãAkÊœaœên½Êåo%¤Ò½û¾ÎØ•Áùç›Öâ˜I]ÉÌ®›|aÀñ÷F3"rIJüÒ¨æ¥]à)Šôª!íôA5GwÌp[!3ûžþ}vr‚;|Cùÿä„|´±Œ Iñõ)žõåüis#µ£Ó†¸yëDw¢ïIÿ>È¢Ç­¬3G^â²{Æ¢C=k¥ÇIqØJï˜M÷zq¬­ØgØºÏˆHÔ]y!ÈA7vH¶-¹i¿Ï—ãf:dÎ!¤=T°)5ãºÏ¤ 2`Qj}€AGKs=·`&˜`MDÂ×qòÜçÎÀdÀ ñxtÌX÷Øt—jÖ¬s¬×¥ÿ
g÷¹ù±iîÞ—ß…Ï¥î­€inx-ˆªÿû{üXÒ®­,òr?“rè™%×Òù´Šº%¡v>­Š‚t’ý?PƒEY%   0707010000bcba000081a400000000000000000000000154fc7bfb00000432000000660000000300000000000000000000003400000000root/opt/opensvc/usr/share/doc/template.loop.env.gz   ‹û{üT template.loop.env å™ÁnÛ8†ïyŠ	rÐ¥5º×9XôXh{Z,
ZYD(’åPv]ìÃïE9²£ØÆ®Ó‰<"‡ä7¿f(éîî’ÇÍ\ðwÆ9Aw<ØÝeÙÝü%‹½{ó÷Ìô·ª·y´€Uˆ|áõ!ð{§æ‘>(C(fÜZ“vVÛÕÛGs…µêLÌÓz#¦RÙJW*"‰õ£³¹¡G[Qn8X©t^-öæ/¡Ã´XnM%Û¾4j…6ÂF1l!ºaí äÌu¡D°u‹K¬]@¨•1<OXªòaeîÙcË‹ˆ.€*#¯gÁ#¾\Þó¦pGµ¢+c= ¼ î÷`4EpuZïÞ‡¥ŽA	uþ-#1`è+Ait«c†˜<#[G´‡à/6-òXêZ—âmŸ]‹~µ­M‡–C)ª‚^cùZb£ÖšÝ-àG¨Ö›¾mÁ½”/Ä("½²”ì;môQ9K¨3Ì¬Â°Ö%&¤ÙÜCíÜ¤¨[^Û-÷2HŸ@°'‘˜õ§G¨å»íÊ°íÌûÜ„ü3îôŸn£¼f–×`N`zµBÎ<`]…ŒQ—šÿë:ÁÎR„FÑH­
äÔ´D½iÎYQ†~7d´û<ó©8TšdÎ³ŠC^óa8‰±ÀS|$ueì©0UžPÓÅÊmúâ½¥ë¼¤Aef„÷“ã<!\ÖŠS7âÂ[DnU K±¨ÅyÁE$Ò‡Q—æ 9?W°ìv5§VÚt!Õ—CwÆñ}V¥®ž‹‚šÊ¾Öá\FåÉè.9]ë¡ÖiKÛñX?Vœ¤‡!ÖG¡ÌFmé›³W¦ˆéz!éˆXUðÃiÿ0œ«’<ê)™$ŸÅ+(ÿ½föÇäë²K‡5oOÒeÞB`ìk}ß%±itÙìW.M9!GÎ:¿€OA·²¿éˆ·™ÒT ´µe˜ãO•„°D½–%ÿ°Î¾¦†WQ|qŠxà	ýélAêaltšPß2]‡Û[¨”•ü{+:ÚI„…ÔCž’øí÷ú/²ÿ¤2he·?°ì";ú½æíÊr‡èÈ6Ä;Šsæ¨êÈÂ;Žq‡èÇÄÚùyRœV£óOÄÈ¦“Zœ+ÄI)î3 "'ê¡<Íä¤$‡’ÜÙOérÞL§Ä9…tÕ2åMÍ<>'R2!Q±ž!ÐÙÒ|Fž0G˜Î&{â¿óäùœ:{&ÍÎÐèœ±>#Ó§T÷aÁ*f/Î²_Û/CùôaY>x¤GÐ†ÁÈgz.üœºJ¯a[åÓ[ˆÔìÈËjýówñ¬û¸þøŸ‡¦aÉ2Ó>OßÆ´›Rÿ–ZšÝËnþh½ù÷â    0707010000bcbb000081a400000000000000000000000154fc7bfb0000044a000000660000000300000000000000000000003400000000root/opt/opensvc/usr/share/doc/template.pool.env.gz   ‹û{üT template.pool.env í™MÛ6†ïû+f±]#çö È1ÐôT-,bi‘åPvôÇw†’¼²Wþ@ë-Tˆ:Ø2EÉg^ÎÐ˜‡‡[^wpÃKÌ9kÍÍÝðbs·ew÷»,öáÃw2ÓgÜm­/»ÑäQ­ÖÈOÞßð$ÿl´Çn¨ÏÊŒÎÛ&mk]¯_š,UcB7¯/¶Ž­¹ª]¨€ô8h-Ða]ÐQ_Ê­SKƒmó7ß`\0÷¦œÛ¾U²R°%¾ÿYR\=wúØS€'(­½å‘‚òabœ>¼$µÂ:ÀVÁï Ø~í äÎ6>g’ô	–XZP*cxž°Tùó`;ð»‚zÍ‹ÖƒÊ¯g!Ä{“O¼„1ÜA­hb¬ß@“ŸÀh
Q‘¼Þ|òK¼êü[FbÀÐ‚ÒèµÄh¹u@»w±óBÅÞ"‡¹.u.Öð«eïíêº4ÖìJñPáõýÀÖ+µÑlnŸÙsøC­iûfü–r™˜E¤W5Åö½6Z¯ŒÅ¨1Ì¬ B¿Ñ9FÄÙœÞsÔ,	§¶åÞB‘â+ˆ¶$"³ööµn·MÛ¾ù›°€¿†/ý£mÔ­™åµØ0½Z!G¨mŒQçš¿u›:)B¥h VrhZ¢
/ÖtŽYA†þØG´§næc~(4Éœgå‡nÍÇnà Æþ‘ÐÕa€©òD€ª&vÛ&‡ÎÊYºÖITfFx¿ZŽÂe£8t(N¼Yà^°³RŒgœDb‚ !}ì…ajöš½Ñ¸þxËfŸsJ¥Mãc~96g,ï³"¾ê8)ˆ£9¡,à7Â²1«ü =ýÌ)§Y;(µAÚQÀõp–/'ê¡÷õYA(³U;únë‰)b<_H8"VDá]Û~÷×ª¤õ’L¢Íìd½ýV3‡còs9¥ÝÚOâc>B`hs}ûKb[é¼:Ì\š:_°‡\kÜ¾z½–óMC|Ì”®¢  ]Wž9þTQhsÔ9PòÚÖï©âU["žyB¿Ø: ù0T:N¨íŸÃý=ª–ø/:ÚK„…ÔBýKäñûÏúorþ¤Ükä´?0oö'ú—vÇÿâÿ¥Ñ™cˆ³æÌQ•…wãÑ9Ž‘µuó¤8®Fë^‰‘›.jq®G¥xÈ°tIˆ¨ûô4C£jì‘Krß~I—óf:&Î1¤¨®)jæ	ô”HÈˆD¥õ
Î–æ	yÁ`ºBšl‰?çÉó”:[&#í\¡Ñ9c=!Ó×Ta¥‚G*x¤‚‡\©à‘
7Å–
sðC*x¤‚G*x¤‚G*xüWBJ‰rLTð˜ÄTðHÉ2MTð˜0ÍTðHÿÖYðøX\2 4    0707010000bcbc000081a400000000000000000000000154fc7bfb00000452000000660000000300000000000000000000003900000000root/opt/opensvc/usr/share/doc/template.share.nfs.env.gz  ‹û{üT template.share.nfs.env å—MoÛ8†ïþä Kêöœ ‡‹‹mO‹EAK#‹%²ÒŽûãwø!GväîÚ@ é`K9$Ÿy93º½½ä5»…^Á5Ââ¸Aèjú¿æ.x±¹Ë²›ý7{ûé¯ÙCÜïcØñlÖýŒ›µ¶Õ}žÛ×pë‡^ŽÅ_^ZÌÓ|·C«±z%IêNvË{xŠbs…µðÊå%}Ñ]l-EWÉJ8¤ûAk…»ŠöúR©X(N—zSÉmß„Ú+6ºÇÏ/¯tÚnÀé¤9yˆ]¡Öz—6Ž&ëËÓ·DðÅhëÂÎy%t‚¿¬µ¯`@è@vð]ù1õ%ÐvpBŒ Â*`þ¦•°’²áR·-/:b\`·HNXwuò[Æg¡ÿtîb‰ƒµT
\ÒfÞ;ˆp§½-9|“-Ó¯5ƒ¬…R¼NXˆòyÀÇ¡·¼	Ö9ˆ280ÒîM>òÆp;±¼¾Êõdþ”$ãïwŸíB:+u~3yû¬]%[é2Äh¹u@»w±ó8„ ƒ¥¬e¬ÍYú-¾Ú•]­<vìÊŠ¬\áðÄ,°+ÉææðÄžÃÑ•ú<J˜"˜â“HrÙÅãøªäõ±8€¼bfV»’e
uq5‡Ïùí	È R|qàÀD"2K·G¨åÓöÎ°m›w¹ðÏpÐ:FyÏ,¯-ÀÀär‰y Ó'
/KÉÿ2%à,EhÔ* AMîÕ§o€c–S?ôí1¯|Ì•¤°æIù!ïyßÄXàÑ?!teì10U^çcï*½NÉ![9J7BMïWÍq"pY	Ý¡‚)÷*¸Ú¢ÆN"1AP ½ï…aj¶’½áÍ	ÜÁÂosN-¤ò6æ—}sJó9«âÐ\liN(søAÈe/Ç*;HOJ>sÊñ­Z*¤9l‡'°~Í8}i}}TB­Å†~êî)b<_„pD¬ˆÊšþ6ý÷çª$ÏzJ&ÑfqEo?ifwN~ª´¡[sy_s	.åú4Ž%±ndÙìf.®®“/ØCÆŒy3‡¯V¶¡¾ñÄefè ´éÊÆ2Ç¿E¡\…‚’:Ý}ˆåz5°Å!â™ô‡î
ë×È¸ Ô3¾‡›¨DâÿÍMÐÑV"ák4Býµøó=ÖúW©?©´Ò¸Pmà–Þa_Ñï¸4—ÿ1
ÄOÔÑ‘2ÄhrSæ(jÇÂ;Žq‹èÇÈZ›iRW£6oÄÈM'µ8Uˆ£RÜeØ:%DÔ}zš ÈQ5öHö%¹m?¥Ëi3çÒTgÈ”‹ši=$R2"ÑÐz†@'Kó€<÷`0!M¶Ä¿ÓäyH‰Éˆ@ó‹34:e¬dú–ê.¬ë¿éLDn:    0707010000bcbd000081a400000000000000000000000154fc7bfb0000013e000000660000000300000000000000000000003f00000000root/opt/opensvc/usr/share/doc/template.stonith.callout.env.gz    ‹û{üT template.stonith.callout.env å“1OÄ0…÷þ
K]ÄÜ+#Ómˆ!—¸½ˆ4±spÿ§R…˜P7<>ÙÎó÷”¾ß³ºv¬ºŽ…¢—Ëä–¬	Šü}Ý¾îöe×½´cû‡×î¸Üûø}q×U÷oxû ì†æÀÎNÅ»«Êø^|ÆöÊ)¬jÊtõìÕ_œx2ÙáhJæè™â¢ZwF‡ê0atü£—-%s¸ÊmñÚÎVÅÓEc§yÖ• ø‰¶E“'”*6n÷:xT(Šm$úÙ:ó?¨1æ+fPrfÂ£(ÇÈ°"K†¤v­ì6Œ`xóUÔ¬èÀÇ¥ßRÎÈ‰ÔkœÀÅ®kG}ÍŠ¯©x	¸$Ñòia|“N<i    0707010000bcbe000081a400000000000000000000000154fc7bfb0000011a000000660000000300000000000000000000003b00000000root/opt/opensvc/usr/share/doc/template.stonith.ilo.env.gz    ‹û{üT template.stonith.ilo.env ­Q1nÃ0Üý
^Ó ³ƒ®;e+:°m•EU¤Sä÷¥lA×šãñp¼;öý‘ÓõpàT9QNAçè=„Èÿ“;pLîØîº÷¶þè.[Þ—š¸ëªó/ºÿpñC»®X&RÃŸœZP¡ï5j‡®e¥ŠæÂ· Áì¥i€WŒ²ÁžF\£6Soœ6ÔaòÁ£’¨§LÉË®8Îøi‡›ðNgàu&*7*°`Â‰J
Ž“p$P†Œ" •µ—g»Å˜Þv'@yø¹™‰<„´ñ—B’Ù¼¦	pÕùl²£]sjAAƒF:›™ËÞµ=cd{Æ/ñæû2    0707010000bcbf000081a400000000000000000000000154fc7bfb000000f7000000660000000300000000000000000000003600000000root/opt/opensvc/usr/share/doc/template.subset.env.gz ‹û{üT template.subset.env ­R»N1ìï+Vºu"Z$*:D±gOŸ}ìÚ¤||lb]Bï)g=3ûð8öÄ0RGT;Í“"u³ëˆb×wwÃûeØññc¨½~â÷'ŠÝ´¼……½‡/•ûŽ¨K|e'hQÏì•^$~;u1¸°ß\i‹gŸZ_+m8Xg9AkåM2èt+Z¬6Ñk¬š¸ðäñ/øò\M!_vT¯Ÿ"¥âwGlRiG‰„#LN°äÂºâ9†½ÅíëÜ}Æ<AÊ´³èCÉÚ®Ò§–?œ×Ï˜ú   0707010000bcc0000081a400000000000000000000000154fc7bfb0000056e000000660000000300000000000000000000003a00000000root/opt/opensvc/usr/share/doc/template.sync.btrfs.env.gz ‹û{üT template.sync.btrfs.env å™Moã6†ïþ“ä Kâ8í¢‡`±ÇbÝ=-%,64©òÃŽ‹þøÎ’#9Ž“víÖ­}pJ’Ï¼šÎÎvùÁ?lÎ-uq~Ù äÞVî;ÍíðCævËnô•7{6ù6º‰û½M;xá¸\[^·“;[ÐàÅ?ÇâoAZlgùlòhcÍ\:i´ÔÓkø ”‹Ã%V"(ß®è£ãh!t)KáÑ]÷FKlP—ní\W˜Fä
ûÓ¥³]AcŸL°‚ùÜ¨0C0ø£$ÆtæA H•1•Îÿï½Gç¥žfÞÂ‰Hlád±ÖÉ9îÖŠË›p­†‡¼˜üÑ¿èop»SÎÐâ%²û„ÎÔ‰IË[ÄÈoEˆ(¦y7qôÂNñ?!9m1,mÓýL»è1™\×b¢D ÓD›éè¬ý“ükrœìøÖýLTÄµ‡…TŠÄ¶oº½ƒà_)þ‘ %+3ÇÊX„J(Eë„\½„F×2çmÂ¢`´êM&oi›u;uÆz‘ò”¤0Èr¤ýŽáÎæÒ[ÁÔéž‰ CpX2J%gÒ·£e¤ÑíÎ9Žœçkò–k°•,ØÚ˜2Eá•]©+P“+ÙC¥¥@b{¶r¬Å\’¹1| Ïá£˜5*›ÑU¢ÉØçäT»8¾ÒFòzÏX¼ÀEÌJphç²Àö¾£Õ¼œ(º xýû2ˆŸAì90‘HõEü¹…Z{·¶½&ÎnÏ$¯À6€Éé)òÄÈNe!é¯lã’"ÔÂõÔ* F
M9
ÿd2ph€b–ç©oºˆ¶-õ–ÒñšÊíž×Ý@AŒýÃ¡«Å3 Q¥…€«ƒ/Í"%‡ÖÊVº¦á0(Ôáýh(N0—¹ Ðí@PâÍ<•I1«ØxFI$&Ç¤×½ÐOÍV’7BóŠ?Î!«œS	©‚ùeÝœ2tŸ•ñÒ†’;šÊ¾8¬‚¢Xe{éIÉJ9aÖ@%º¥ó8ëßÕSÆ‰zè|½UB-ÄÒÝ}`ŠØsµÛÎúšL¢Íì²Î~ÒÌpN:ÎUZß­myS	>åú¶ŒÖ°¨eQ3—t­/ÈCïÍ>Z9ãú&8*3ùTV@,ÄkKOU:=Û œsAKv}ájÚEÙ³E!âôÞèÌçC_Ë¸ tf<''P
Íñÿä„u´’ÈÖJ¿±xˆµþ^êOWXÙx®6ð‘(é±³­è.mËÿä
Ñ–2¤1Î3GQyÞvŒ+DÛ8FÖ¦9NŠ›Õhšgb¤¡Wµx¬7JqÈ°ôš)PwééAnTc‡d]’«ñ×tyÜL7‰sÒª7È”Ššãú’H	È‰òèz´4_çÌ¦7H“,Ñ÷qò|I‰É¶Þ ÑcÆú‚LŸSÂÚÖ|-j,ÃÁõ¯öñJ7>>bz„àë?“v<ø)¸ßùÎ^Æ‡ÎËÒ—œÆ…ÑUlbÄ&c{ôâ‘&¨Ðrã=us»£·ðõt2¹žL.&WôýóOWÜÔ<=‡ÓÉqôÇvÔ¼pAŸ~Ûì5ZõýL<Þ—¨ÄòÀ|wõîÝw¾°zÞíø¢¥ç>ÂLjºÜ>³]lTˆ@äfŽ½VET>÷r¿
ì7ÅÖúd!G w:Ybl+p‹>Õ&¨’qçA9€›ƒ·1K²¢Iose¯Í¦pŽ
ÄÔ"ÎPûq|éVÒàW“CÅ€IËž)7˜/G¿@Ô%_K|Åæ[6@–D6#©EGŒþ_µ†sf#    0707010000bcc1000081a400000000000000000000000154fc7bfb000005d9000000660000000300000000000000000000003c00000000root/opt/opensvc/usr/share/doc/template.sync.dcsckpt.env.gz   ‹û{üT template.sync.dcsckpt.env å™ßOä6Çßù+xÈ,K{ê'¤¢^ï±:‰;õátBŽ3!>;õ…­úÇwÆN–ì’]èjò°?œdbæë{rxø’ÇÞ!¼àÁæÜRË#ðË¡NÞ´þYæ^ð s/Ënï+öpþmï}ïy?â½=îú.o-ÎºÇÓ9j<~ÁƒñXü+(‹ÝS>Û€ÜÚZ³PN­ôõ|µ‹Í–"Ô¾ëÑFÇV)t¡
áÑZlQnãZ'M+òSsg8]î$5þY)²Ó
‰Ç[aÉjµrL	„¿‹P¡(ÜQC¾_!4B‹k´32øž`ÎÒ˜Q”Ý•ÿœŸ	ËŠZ7jÐ¢A°;œ« ØÝõ 4óL|5“F—tZzxåkŒ¤{«Ûi·BÙ	H÷¾;£Kcá‡²-Td&¬BGn-:Ôžé³j	V"PGÀÉ$] "êŠ.ˆgð»Õ½Å%4R‰Fc™³2;Ê
ç¯8¤dì=-íðõÏë_Ýc+ß<Éeóçù+Éccjpµ‡[U×à‰¥7ýØi^Ð¯ä{CžË±äYTŠºfßåBÞ@Ó½1Ñ ¼± ¢ƒ"îÞä9a·×¯?;þëçO¸/VÓÇ;ƒ›+oYÁüŸŸD€!8š7„²VòÄh™äm´{ç8rž¯8Šµ(U©$[›Á¥¡·²«tYÔ2M‘ÂªEýý‘#MEæfð‘<‡w¢iën:Ñ]¢ÍØÅF§®µ‹í+m$¯ŒÅ\¨cÞrhyêFÄÞlŸs.äy' ƒHñÄ‰È,ýÜ•ÇÓl{cØVÍëÜ˜ü3¼é‡¦Q7f’×
`ÀÔ5çaÚ”V‚’Š¾U™²L’"e
7Ìë¼’²>Gáï­Qö-PÌòó{ÑÎ»ž.M•ã>OÊÝ˜7Ý@AŒýÃ¡«Ã3 Q¥Ž€«‚/ÌmJ•tM“}=!¼ŸÅ	æ²ºJ¼™§«2 )f%Ï(‰Äá˜ô¦†©™^iúAV9§ª6æ—Msµ¡yVÄ[[J
ìhJ(3øâ°5Å*;HOµº¡”šJU£[:Íp–÷'ê¡÷õNAˆúV,Ý•ÑoLãù‚Ã‘#E¶í¦ï¾áÇ—Ü£2‰6³#Zúvö“fÖŸIçy•6tk·<‰§i	>åútIâ¶R´ô^Ë\Êu¾ µ~`,´3ødUÃë›àh™É—²by£²Äñï´º·(Q-xAI4í\E£XÛ(wCú`tæãöÌW*v(]ÏÃþ>mâ4Çÿý}ÖÑJ"$¤ytgfñê-®õ_eýé¤U­çÕÞ¡ûýšK»åŒq³Ô#ÚµÁ5´Éš0GQzÞnŒ+D»8FÖ¦&Åq5šö©éQ-Nâ¨×ö€"ê>=Mä¨{$›’\µ?¦Ëi3çÒ5TO)-j¦	t›H	ÈˆD¹õ	,Í-òÜ€9Àôi’%úœ&ÏmêLLFÚx‚F§Œu‹LR]‡µ«ø*+,Â›«_½ÏË¸}Ä´…\Ã7Ü“ö<x<¬|gOâ¦ó¤0ò„³Sz)ÈEŒXdìîcƒ^ÜÑJ´\xOÕÜþì9|=˜ÏÏæóãù)}þúË)5Žà`þSlý¹kõ]ÐßÆ½F½¾jÄÝUµX¾1ß¾{÷ÌV«_´ò\Gh”¦yàfð™½èb¡Â@¤ "7”*¢ò¹–˜rXÛ¨c…ÜéT±¬À%J¸¬L¨>Å•!%9 ¾¹¾Y’m[+Ù	¨+³Õ¸ÀÄµElPûY|é&IiðÝäP2`’Çr`Ê­=/GË/ø³äk¥“¯³X|ËÖ%‘­µ‘Ô¢#öþV«Ñõ"     0707010000bcc2000081a400000000000000000000000154fc7bfb00000598000000660000000300000000000000000000003c00000000root/opt/opensvc/usr/share/doc/template.sync.dcssnap.env.gz   ‹û{üT template.sync.dcssnap.env å™KoÛ8Çïþ“ä Kâ8»E)l±E‹i±‡¢(idqC“\>ìx±~g¨Ge[q²Mµ~PÔˆüÍŸ3Ôèää9É	<ãÁæüZ§Ö¡,¼×Â>ÉÜ3dîyÙM¾òdOfß&ïÒ|¯ºO&<ô[\¯Œ+/ÛÛÓ9j<{Æƒñ8ü;J‡í]>»ˆÜjYJ/–z~	…ò©¹ÄJDÚýatj-„.e)úËAk‰ué·úúÂX‘+lš[ÃMw_PãŸµ$;VxæÑ
GVKPÒ0|AünB¢ô§ <xDùB°ZÌÑMÉà;‚E8+cFQ¶=~œŸ	KO­5h±@p‘¼¯¡¸0º@jæ9X<"†zZ]Ñé"Ð< È 0‘î¬ÞO›Í÷üùqïU/cðµ	Ôy)LnØñ ú¶ßíŽåÜ}.¼8ížë£pÏžÆºqí–²‰Š°’JAp„Ïts'YÓ/c&­hÈ±b˜•PŠ…Ÿ‹âv ›®M!„&Œ‘Tžˆw&¯h
c¸ƒ˜¿|\þ¬Ÿ.íÜï{ó|§ðÞå28ÁÔé?ß‰ Cô¤wB©äB†b²ŒÔ: Ý9Ç“óBÍAÈb!+Y°µ)\xoWêJEÔäJöPéä2EîîÈ±KIæ¦ð‘<‡wbaUÓ7£«„ÍØ-3/çÚ§ö^×ÆÒ>ª´p=:^¨Ii4{B\Ì)p€Åˆ6$šX•~îKÃÍj{eØúæMnÌþ^ôCË¨3É«Ø09ç4*@›	£,$}Ë*Án¥µðÃ´Ì!rá»5JÞÑÅ¬€)=·íªùèÎRzóAù¡ó¶(ˆ‘À“8tµØS ª4ðu¥Y5É¡µ²—®±…: ¼ŸÅ	æ²º=J¼Y ^³Šg”DR‚ðLzÛÃÔì$y#Úüq
yìsN%¤Š.å—msÊÐ:+Ó¥–’;šÊ¾x¬¢¢XåéIÉ[J9qa¡’
ýÚ\W`õ=ã$=t¾Þ+¡VbíoŒ~eŠÏŽ<)¢t¶ûÙ|w?¾]•I²™BÖÙo4³yO:Ï»´¡[ÛíI:M[M®o®#I¬jYÔ›™KúÖä!Æ¢Â''¼¿‰ž¶™Ü•ªµ#Žÿˆ$4‡Ê%o(é6úÌ×4‹r`‹BÄ-èƒÑYHOW¡–i@MÏtŽŽèLsü?:bõ!!5Ç„dÞ¼Æ½þ‹ì?}á¤¼ÛÀ;,bÀnG¿áÒvûŸ¢ ƒìíÙ†XãÃ!sU áíÇØ#ÚÇ1±6ö0)Ž«ÑØ1RÓƒZ<Tˆ£RÜdØzHˆ¨»ôt€ GÕØ!Ù–dßþ.›é˜8Çn z„LiSs˜@ï)‘(·>B KóynÁ`z„4É}&ÏûÔÙ0h{â=d¬÷Èt—ê&¬}Å×¢Æ2¾ºúÕð¼NØ<Bnà>“v<ø)xXùˆÞ§‡ÎóÒçœšwz\ÄHEÆö:6ÄÝ BÇ…÷¦šÛ½‚¯Ç³Ùålv6» ÏßÞ^pQóøŽg¿¤Ö_ÛÖñÌG}ümÜk4ê›…¸»)Q‰õ+óÝÅ›7O|aµ[íø¢eà:ÂBjZ~
ŸÙ‹>*$
 r³ÄA©")Ÿk¹Ñƒ©†E±­:YÈÈ^–˜Ê
\¢„ëÚDUò)®<ˆ¢ p±bãmÌš¬€°VÉ¢P[fS¸Dbî¨Ã4½t+Hið—É¡bÀ$õÀ”ß¸_ŽaÅ¯6³Æ×R7¾ÎRñ-Û@Öˆl£¤–1ùº¿^´"  0707010000bcc3000081a400000000000000000000000154fc7bfb00000720000000660000000300000000000000000000003800000000root/opt/opensvc/usr/share/doc/template.sync.dds.env.gz   ‹û{üT template.sync.dds.env åZ[O7~çWPµ/ìfI£>€š4ŠÔ‡6‘HòEÈëñì¸xì©íYØ¨?¾ß±gï…’Ðî
qñØÇÇßù|nÃáác~öÙ#~HœŸqÄÂ¼‘¬(üƒÅ=ââ»½OtØÃñç½ÓxÞ3:ñÞ©})çWÖ'ÝÖÞ	ñCÐ8ùw«œìvyïZI£³3å•5ÊLOØ®}.dÉ[:þ´&Ž
n
Uð ýI6ZÈFšÂ¯ÌõÂ6|¢eî§é^`ðU&x*É¬SSe˜-ã_ÞðÆW–žY(Ýh%°#+­Gp
t€]iíFì
þÿØ½çn*+•&ðØD[q‰g3%äˆ½mTâzÄ^'E"@m”ažÝ•ZŠ÷Xóy´ÁDîæÃBêÀ1.¤šÉ"š€yÛ:!™±…Ä¢{_)¿&­n}`ŸIÆ™W_`Ô¶‘NAW|K®£NQT4*Ì¶Å¨!ž÷¿`W‚Å³Xášþ×ô³øz£¿B6f˜qÝB"w’z±ƒ#6H¿DBØP±«JA«†9ÌäeÃÔ-r:twºÞÎÏ¬eÂ^IvÅR¡âyˆ	 -ÝcéˆXdòÙ•
•mã™ ¶’ƒlsE11Zâ$Jk6q’_&É˜V9˜ç'ªH™¨ B²š6ÂÏÖƒkzŽóøà” øpº…uRø<N«(Ø$¿­OKnÔã˜®¤4`t"ÆÛ`k¶?y~-J®´IÉÞq–Vop‚ts¾9éôþaÞ¬sS„òñø'
) ŒØï†ýñêˆ9Ûš"™¡©æfÐL^iB†ð„&q§`ŽÉœéYéVû;'ÿ³[ØD¶ÂÜ·Púüão˜Ÿ‰ƒ|ÇEèØÏ:öÇ ø™èdà&ðìy&ÿ$“ˆ_.uô£8Ç”CÇDÅÍ#‘S$<‘‘hk¦‹ù™°••¢jÍ¥gZaÿ¤ÝG«ÛZ²)Pk|Ïëè‹—ý¾[÷ž}ãúŠÏq˜¨JæJÁ¶çáïLw—õ‰1uü0š¦XÇ\Ð‡OÁ¹äp‹n!9*Š’}ddAÕÄBYZ¸èþ‡\â„‹ËÜKÙHÈ‡ðØ šŠ®uï´y†#l†SÿÄ°~¸WXƒûe¢4Ý[œwÄ^º‰Â$Ôñ7í€ÞY«Z…Ä(Y._šÞ8½«å¥P¥$.ÂÖòF®2%¬ñÂ VàºÜÑH\&q#ö†²™k^7:Í`o$ŠqïÕÔ¤èºàF²zîfhou7ºmn¿s¾øïýxD×@Ì˜HÎ*þºµî¶=1ØÃË¸H³E_uº3ƒ^ ;¦¦Sž’2ß*¡(g*—’·ŠçYg•„kš ìÝHCåÖ6”úÚú´÷hgæ‹4åIç²CwæU3À‰àÑ>äº:Øc ª)3mCa¯Rpè¤lE×vàÁûÎÂO.ym0+–Cƒ’„Db€ "fÍ
yhFY€ÓwØãˆMÚEÌ¡ü¾u1¾¬ŠÓvJ‰-mÈÐT°^–­†¯rYxÒê!§­›XÜû¹²^ª#n"NäCoë­„H™Ý…5OŒß½„ÞH“Eý¼¨¥#g–÷ÄsÊÒr³véI|ŒBv•tZJ üÕräBAÚeÙ—(åó*³A5îTMùMë‘fÒÔØÞY©u»f%”–ªO3ôNQd²à".¡Ðkk¨ÈcALdŽ3ãs¶¿šÃÿßß'-(²µ*mœ¼xŠ¹þ7É?½pª‰U©¼–¢EåßeôK&íÒÿèÈD[ÒÅâ.ãÈË âm‡qÑ6#Ö¶ÙM7³Ñ6kdÄÐ\ÜU7RqÃ »ˆGÝ‡§r#{HV)¹¿‹—»é&rn‚t	ª{ÐIÍnzIÈŠÒè=º³hÞBÏ03˜îAMHÂ÷ÝÄó6v&L6´{pŽî2¬·ÐtÕe°¶5_E%‹öÉõ¯¾žç±|”©„\‚/¯I{<¨
^~wêžÅ¢óYaÅ3ŠN#aM›±ÉØ­#_cƒR:j¼§nnÿôŒ}:OÆãáøßýå˜ššGì`ü<ŽþÜ†V}k>o¶´¾¨ùõE!5Ÿ?1Û¿xñÀVëÝŽFê#ÔÊàøô¯Ôˆoø#
ŒOìLf­ŠÈ|êå¶žÙ2oŠ­ô1 a"ÌéU!c[Z”ì<½nœÈØyàBÀ ñò·1sHa¼I/ÔUÖfÓr&5ãS'e-MÅ—nLcÙ	+	`Ðcž‰òKûõoåÉÖÊ$[bóm°L€A"ÙÒ¨±÷/X;Ð7'  0707010000bcc4000081a400000000000000000000000154fc7bfb00000529000000660000000300000000000000000000003b00000000root/opt/opensvc/usr/share/doc/template.sync.docker.env.gz    ‹û{üT template.sync.docker.env å™Moã6†ïþ“ä Kâ8í¢‡ºÀbÅÙ=-%,64©òÃŽ‹þøÎP”#9Šl’Ö€}pJ’Ï¼šNNÞò397ü°9·ÖÅ)øuƒPšâíëÌ½á‡Ì½-»ÉwÞìÉìÇä*î÷:íx2á•ßázely™f÷ÂÎÑÓøÙ~Å¿‚´˜&újòhcÍR:i´ÔóKø,”‹Ã%V"(Ÿõ‡Ñq´º”¥ðèø€6%:øJÛt?Û¿Ý@k¨A]º-C®0Èö×Òží
û„ÎK-<-+™4ø£h¦têUK‰H¶´dDØ÷'¹aö"”³Ç;(?…ê+QsÔVR)ðvÞt{Á¿L°‚/D2ÇÊX„J(Eë„\w½Û‡®eÎÚ„7DÁ>ˆÄ;“×´…qÝÎÝž±—í«p%r¤ýNá£Í¥·‚©Óÿ<†à°d”J.¤O£e¤ÑíÎ9Žœçkò–k°•,ØÚnÌìJ]©€š\É*­\Ry°•c-–’ÌMá3yïÅ¢Qí¹]%šŒMpNÎµ‹ãm´^ï‹¸ ˆY	íR˜î;ZÍ5TÆŒŠÀ…ÜýÁëÿ—A¤øbÏ-‰È¬ý¹ƒZºÛöÛfxÈYP¤ï]ôS·QÚ3Ék009ŸS1 bd'Œ²ôW¦øßJjázjP#…¦…°&5„(fyžúª‹h×iåc~(¥ã5”Òž·Ý@AŒýÃ¡+a€¨ÒBÀÕÁ—fÕ&‡de']Ópê€ð~1'˜ËRPèv (ñfžÎÊ€¤˜Ul<£$„cÒÛ^è§f+É¡yÆ§‡MÎ©„TÁÆü²mNºÏÊxiCIM	e
ßVAQ¬²½ô¤ä¥œ°h ’
ÝÚy\ôïÀê!ãD=t¾Þ)¡VbínÞ3E¼sµ›f}N&Ñfv
Yg¿ÕÌpN:ÎUZß­©<‰‡©„@ßæúTFkXÕ²¨‡™KºäòPã{ÆB3…/V.¸¾	ŽÊL>•ñÚÇ¿Û*ÝbrÉe,Ùõ™«ieÏ…ˆ;ZÐ'£3œ}-ã‚Ú3ãq8:‚RhŽÿGG¬£DvVúÅÛ}¬õß¥þt…•çjï±»Š~àÒTþÇ(À 7ˆv”!qþ9ŠÊ“ðvcÜ ÚÅ1²6ÍaRW£i‰‘†žÕâ¡B•âaè9!R îÒÓ‚Uc‡d[’›ñçtyØLÇÄ9†t€ê2¥¢æ0>%R2"Q}@–æòÜ‚ÙÃôi’%ú>LžO©³e2"Ðtà=d¬OÈô1Õ!¬]Í×¢Æ2ì]ÿêxÞÄÇGl!øúÏ¤~
îw>‚³çñ¡ó¼4Å9g§iat›±É˜®cƒ^ÜÓZn¼·ÝÜîè5|?žÍ.g³³Ù}ÿþÛ75OáxöKý5ú€g.èãã^£Uß.Äým‰J¬÷Ìw>¼ò…ÕãnÇ7-=÷RÓ}à¦ð•½èb£Â@¤ "7Kìµ*¢ò¹—øU`¿)¶ÕÇ 9¹ÓÉc[[”pS› J>ÄQä nVÞÆ¬É
ˆ¦Q²HJm6…KT æqÚOãK·‚”š*LòX÷L¹Á|9ú¢†¬õµÔ­¯³Ø|Ë†ÈZ‘ÆHjÑ“gò¹;Ô     0707010000bcc5000081a400000000000000000000000154fc7bfb000005de000000660000000300000000000000000000003c00000000root/opt/opensvc/usr/share/doc/template.sync.evasnap.env.gz   ‹û{üT template.sync.evasnap.env åYMoÜ6½ûWŒíƒ.öfÓ=8qPMÐS I/I`PÒhÅX"U¹öùñ¡>¢]Ëk7¶£»{—Gä{3ÃáááC~öá?bŽV&;¿jp©È¨æ^æðÃæ»½Ï²ØÃù×½—q½§ýŠ÷ödê¸º´.?é^Ï}çFÕÈ=ÇøŒþ´ÃîU]—@ãìR“¶F›Å	¼UÅæ*ßMë5±5S&×¹òH'£Ö49m<K™mTZaÛÜn§Œßñ*ÁàK„?ßÃ›¿Î@9§VPZò<™ØA6¸_•Öóø¥Îflíe£ZX;‰¨ŒÛAH¸lú3AR„h@ ¢àÙa£´£ÿ?|gð¬9.¬«•÷˜C¥Éž¹Î<ÏG9Ä“nšA£­[f#gôFee
u`;¥Zb|>!—%Gää“¨ì¤Vt‘è4ƒü„ü–Ÿ#cKUM †Y}y%;¥Q¾|ýåUŒÎ_yÇÓj÷FœÐVy^9ÿè¼Þ‰ØùýXmE4"UðT¦.uUw+ð¶_;£ÉßZcÀëšùM‘€P¨ª†S•]Œxà±ÂaÍ‹ðÖŠÂˆp÷&Oy	Sp{µxü=ôß°¾ÿ&º÷Ù OYïÎ\ª½S‚:ÿ–71ÀˆwCYéZûÄh™wˆ¡Ý“CLž/™-j0Ó…ÎÄÚ>Xv[ƒ]m
Þ#&kwYîôÝÈVŠ¼5››Á[f¯TÝTÝŽäQªIÄûHÒC±}ÐFËúÈX@¡Râ&x€¨‚8›-Á(¤„OmË=†"Š×@Ø"Ñ† øujÝn{b°Íë¸	ð}<è§¶Q·f–× `çÀôbìyÀØœÃ|Ð™æÿº‹ú­9ØÐH­
Jd×”¢ò?¬q<o!¡÷h§ÝÌ§xÈ5ÉœwŠ‡nÍ›4°cG~Äuu°ÇÀ¨Æ|µ>·—mpè¬lE×61É¨vÞ÷–ý„àÓNn8ð&žŸJ€¥˜b<á 	Ò›,ŒC3§g9kú>Ž CÌ)”®‚‹ñeÓ\eyŸåqhÃAAˆæ€2ƒO„E¨ØW¹Qxªô‡œP7Pè
iEëñ,~Dœ¨‡žë­‚PÕ¥ZÑ¹5OLÓñBÜ±"r×ô_Ûÿ}ÃÏ'æ“2‰6cÝÙo5³þNî—,mLk—žÄnN!Ð·±¾Ç’¸,5çík‘KÒîÈ3Ôø‘±ÐÌà½Óµä78Í”GE±ÀQ:Æñ…æ0C½”„’>ePÉ«ÈG¶ØE\ð„þ°&ñ ñÐ—:N¨}2öÃþ>äÊˆÿßßa!µ Ožßž?Å\ÿQòOÊœn¼dx…YðØgôk”véôñ°ÔC´íÌÇ­]ÆQž…·Æ¢m8F¬m³›(N«Ñ6×ÄÈM·jqWAœ”â:†=@·	‘užvÈI5ölJrh¿M—»é”8§ ]ƒê2å¤f7½I¤È„D¥õÝY4oç˜#˜î M¶ÄwÏ›ÔÙb2!Ð®ãÝeXoéuT×ÁÚV|ÍJÌÃ“«_=žâñÛ#ä|ã3i‡œ‚Ç•@îY<t>ËmöL¢Ó,³¦ˆEŒXdìÆ‰A¯®ø:)¼·ÕÜ¾÷>Ìç'óùñü9ÿýý·çRÔ<8‚ƒù/±õ×®Õ<¦`¾N³Æ³>¯ÕÕyŽ•Z=1îž¿xqÏ«ëÕŽOF{©#ÔÚð>ˆW‚Z.n¥Pa!¢ *µK•*¢ò¥–l1.ŠmÔ1ØBŠÀt’Î1–¤D	Jª\º¤ò ²Œ	ˆ÷›ãÛ˜[Õ4•Î:ue¶
—XZ8Äo/13V|³)0Ëc|IkïKÑ_Ê½tÒr­MËuw;º.€¤ÙZK-±÷/‚ÅÝ÷"    0707010000bcc6000081a400000000000000000000000154fc7bfb000005db000000660000000300000000000000000000003b00000000root/opt/opensvc/usr/share/doc/template.sync.hp3par.env.gz    ‹û{üT template.sync.hp3par.env å™MoÛ8†ïù“äàKâ:Û¢‡¶À¢ØSlÚSQ”4²¸¡H.I9ñbüÎ’#ÙŠ“ÍÇÂ€}HlJ‘Ï¼œ!‡ÇÇ¯ù98†Wü°9¿Ôù	„¥E¨ì{+ÜËÌ½â‡Ì½.»ƒ<ØãÙÏƒOq¼íˆ¸ç7¸¼5®8oß.œKj>}ÅóqøW#¶ïùæäVëÌBzi´Ôósø"”Í–¢Q¡íÓW£ck.t!ÐŸ÷Z´¨¿v¯Ï™ÂÔÜN·ûœ¿ŠÁ”*„ß/!
 ‚O!7uM/ôÔ0¥G?¥«P3
ÎåóýÄæ°6‰—]ÂÜ™ÆNá]fSÔ=žhAÜAÝø ö¦Jã±`Úe£Ô2¾ÀÉ£i‡VÉ\!Ýa2óetAÞâ‚Ú¸[><ó6›áè®>Ïô(ñÐÏáŠìUÎhÓx0>ûûßLo"|©S£ 1T¦xs„+XObè}µI‰_®äóñ±HÓ€Y…¤Æ8õ›¬–¡?ù£ ãä÷Q‚í#±_£ }.ìÅÙËBARý=1GàV*ÁÅÈÙŽ3ËhË=ÍýÒ8„R(Åa!ùM/´œkD åŠœU¦|kò‚†0†;ˆ¹ß1Ö/¼¸?ƒ’@9ôÒx§ðÙe28ÁÔé7¿‰ ¯Bª’,â1Zfm÷hwÎñä¼P‘·¼Å\–2gkS¸2åWv¥.Uƒ:O¡¹pr®g+ÃJ,$™›ÂòÞ‰Úªtï„žvÂ¦(èx9×¾ï­6’×{Æâ¾QÄ¬ 4ì2Ç¨‚Ø›‡#?M[»6åÞB‘âÄž‰È,}Ýš/ãlÛ1l«æ!7fA!¿÷Ð³¦Q;f’×
`Àä|Žy@s–ôÌ%ý—i­ÓJ*á{jP!…¦E¸·&54(fL97E´‹¶çc~(¤ç>ï•Ú1¯»‚	<ú‡CW‹=f ¢J_5¡0·)9´V¶Ò5–Ã P{„÷ÒPœ`.A¡ÛÓò…‚q »&¼,œ”l|BI$&Ï¤×½ÐOÍN’7ûˆ?N kV9§R5.æ—us´–Ÿ“=~ÔRR`GSB™Âw´ XåzéIÉJ9Mm¡”
ýÒ¬û3°¼Ï8Q¯·
B¨[ZÌ]½cŠÏŽ<)¢p¶ûšþwÏ_Ê$ÚœœÀ¤³Ÿ43|']çUZß­íò$^¦%†”ëÓs$‰ÛJæÕ0sIßú‚<dCkH{ÈK'k^ß4ž–™|++ ºü;í{æ(¼ ¤ÚèS_Ñ(Šž-
7Ô¡ßŒžà|*;”îŒ×áð
¡9þÆÍ~'R‚<&$ëðz×úo²þäí¹¼ÚÀ;ÌÚñ·+úKÛåŒr…hË2Äö™£(	o;Æ¢m#kc÷“â¸Ý#5=ªÅ}…8*Å!ÃÐcB¤@Ý¥§=9ªÆÉº$Wíér¿™Ž‰sé ÕdJ‹šýúH	ÈˆD¹õ	Ý[šÈsfÓ¤I–ÒÁò|H‰Éˆ@ÛOÐè>c}@¦›T‡°¶_ó
‹fçêWoÀó*n1m!øú{ÒŽï‚‡žî]Üt¾+LþŽ³Ó47ºŒEŒXdlŸëŽL–è¸ðžª¹ÝÕøq4›Ïf§³3úûëÇ3.jÀÑì—Øú¾mžúFý÷õúºw×ªÿáÄÿ¿ùîìÃ‡XmV;¾k¸ŽPKMóÀó)5ýò±Pa R ‘™öJQù\ËåÕ²_[«c…ŒÏÀµ—Æ²—(áª2*øWDž“¸X18Y’öþ0·+³)\ 1wˆ5êÎÕsRüi2(0ÉcÙ3åïË0Ü"j˜$_K|=‰Å·ÉP “$²AI-:âà_göîÄÍ#   0707010000bcc7000081a400000000000000000000000154fc7bfb000005f6000000660000000300000000000000000000003e00000000root/opt/opensvc/usr/share/doc/template.sync.ibmdssnap.env.gz ‹û{üT template.sync.ibmdssnap.env å™QoÛ6Çßó).Éƒ^WI³H`é†}Ø0 )öP%,.4©‘”ûð»#%Gr']ã!›ýÈ”t$÷×Ýé|xøœŸ½CxÆ›sK_Ö2›ÎiQƒ¹ç]Ýó²ÛûÌ›=L¿ì½û½¸ÛñÞ/þ—c‹óvµÖÑðñ3~‘Å?i±çÊ6È£µ5sé¤ÑROÏá½P.XŠFùvM¿Fs¡Yî¼7Z`ºpk×ºÜÔ"SØŸ.^írû­’d¦9;¬…%£(é<˜’®™Ë#‰	Ýö6¹4}óÝyš~ÿšÞðÑÙ(Ca­Xþï^UZÌ‘y:»†Ê¨‚Fœi,Œ<ÐÔ<,m@‹EïÉi/	´£øðîçÉééÙëã“ÓËw?¦é(êlJ‹üO°fðWÿžÀüƒ–^’E‰üfjMCPAç…Ž¹ðt‰2ùxN<ÄšÚhÚ.‡‰À?"%„ÙÇ [Ìé!ØîW–hƒÑj	y%ô”¢ÅaR;¢`0»Nÿz:ôÁó¯ô¿¢»ÙÎë·î€¶'y ÝBˆSÔR)ðvÉÜÚ½Ó3@GQî¼œ‘–3,E(…RŸ‘ø–ùŒ6á‘{ÚOË<š¤?ŠÛ‹éö“â×±ÞBD¿\¥@Þï.m&½L¾óLG‚%”JÎ¤o!ËH£=Ús9ÏWä-Wc.K™³µ	|4”:Vv¥.Uƒš\É*¬œ£íÙÊ°sIæ&ðž<‡·bV«xmBw‰:aS œ“SíÂøJÑë=cá×¨ôZ~ô‚
Âj. 4fT®É¾´Gn2ïAì90’ÌâájíÓöÂ°­†·,.»=“¼V Û &§S¤ÈÚ”™Kú/ÛÔ¥•p=µ
¨BS†ÂßY“šÒ6PÌò<õÛ.¢]´+óC!¯y§üÐîyÝÄHàÁ?ºZì!U.S]ÕøÂ,brh­l¤kjƒBíÞ_Å	æ2º©¼§Ä›xº*’bR²ñ„’HHb‘9ðB?5[IÞ¥è&AÖ¬rN)¤jlÈ/ëæ”™rÆ·Ö”ØÑ”P&ðÉaÙ(ŠU¶—ž”¼¡”ÓÌj(¥¢:˜j¹Yÿ	,ï2NÐCçë‚j!–îÚè¦ˆñ|ÁáÈ‘"
[w‡ñ7ðT•´³>&“`39‚¤³53œ“Îs•Öwk[ž„ÓTB ¹>ÞG’XT2¯†™KºÖä¡Ú÷Œ5õ~µrÆõMã¨ÌäKY¡=TYâø§B£ŠåœJú¢>v•X{—’î†ô“Ñ‰Î‡¾’aAñÊpö÷©ô×ÿ÷÷Ã‹n'R„<Ú±xýký­ÔŸ.·²ö\mà-æ½è¶ýÀ¥mù¢@èÏtˆ6”!µq~—9ŠÒ“ð6c\!ÚÄ1°6õnRW£©ï‰‘†Õâ®B•âaè1!R îÒÓ‚Uc‡d]’«ñÇt¹ÛLÇÄ9†t€ê	2¥¢f7>$R2"Q}‚@w–æò\ƒÙÃôiÆÞÿnò|H‘Éˆ@ÛOÐè.c}@¦÷©amj¾æÍ‹ë_mçÇðúˆñr€¯ÿNÚñà·à~ç£qöUxé|U˜üg§Intš¡ÉØÞÇ½¸¥	J´ÜxÝÜîì|>HÓó4=NOèïoN¸©ypéi}ÝŽú]£¾Œ{V}=·×ªá'ñ¯óÝÉÙÙ7þ`u¿ÛñIKÏ}„™Ôô¸	\±]hT@dfŽ½VEP>÷r¦ì7ÅÖúd!C w:Y`h+p‹>V¦QŸâÎƒÈ»Ÿ¿Æ,É
ˆºV2oÔ¶ÙÎQ˜ZÄj?	?ºå¤4øÝdP2`’Ç²gÊæËÐ/5$Ñ×RG_'¡ù–D‘ÆHjÁ{Ål½Œ5$    0707010000bcc8000081a400000000000000000000000154fc7bfb0000050e000000660000000300000000000000000000003f00000000root/opt/opensvc/usr/share/doc/template.sync.necismsnap.env.gz    ‹û{üT template.sync.necismsnap.env å™Moã6†ïþ“ä Kâ8í¢‡º@±ÇbdO‹E@I#‹Eªü°ã¢?¾3”äHŽâ[0 ü1’†ä3¯f¨ñÙÙ!_³38à‹Ý¹ÎÎÁoj™t•Ó¢þyw‡ÝaÙÍ¾óbÏ?f7q½·½Ïf<ûGÜ¬Í¯ÛXt^XO.øbJÿÒb;Ò¡²¹¶f%4Zêåõ³9ÇBåÛi-Ø”	Ë\xtlýÓèöÄuîÚ;«ËL-R…ùÞŒ‹¥³]F¶ûA,Q{XK¥ÀÛxÓ­3Áf¤ð²B)Æ"B)š'¤"{ìEž®õä±¢ExcAdžÖ3§o:—·´„1Ü^,Ý‘±î÷gPÒy0E\ï>ÛTz+˜:ýæ‘0‡9£T²’¾…=#Y{´»à8
ž/)Z®&M2cos¸3>û•ºP5…’#”[¹BÛó•b)V’ÜÍáEŸDU«æÜ„®uÂ®@8'—ÚEûVMÔ{Îâ.(b–ƒC»’FÄÙÜBaÌ¨\HÛ-÷2ˆ_@ì°!™5_÷Pkï¶#Ã¶5¹1ø·ÑOÝFíšI^[€m“Ë%Ræmr$Œ2“ô)‹»•"”ÂõÔ* DJM)
ÿìMj5PÎò<ôM—ÑnÛ™Å!—Žç<©8´kÞ%1xŒ§®{¬ D•&®>7ë¦8´^öÒ55§A¡&„÷«¡<Á\V‚R·A…7ñtV$Å¤`ç	‘X “ÞB¿4[IÑõñ8‡4lkN!¤
6Ö—]wÊÐ}–ÇKk*
h*(søæ°Šr•í•'%©ä„ª†B*tç±êßÅsÅ‰zèb½WB­ÅÆ=}dŠ¯œŽ)"·u÷µùìïUI;ê[2‰>“sH:ÿf†cÒqÞ¥õÃÚnOâaÚB oj}sIb]Ê¬V.éÚXP„jßsê9|µ²âýMp´ÍäSYñÁ£´Äñ…f1C¹â%ýÐF_¸’V‘÷|QŠx¤	ýatâë¡/eœPsf<''Íùÿä„u´•o÷#ä1!ÕŽq¯ÿ!ûO—YY{ÞmàfÁc·£„´ÝþÇ,À ·ˆölCjãü”9ŠÂ“ðöcÜ"ÚÇ1²6õ4)Ž«ÑÔ/ÄH¦7µ8Uˆ£R2ì ½%DJÔ]yš ÈQ5vHv%¹µ¿¥Ëi3çÒªwÈ”65ÓúšH	ÈˆDÙúN–æ+òÜÙÃôi’'zŸ&Ï×ÔÙ0h{à2ÖWdú’êÖ¾ækVbŽ®õ<ïâã#6|ýgÒŽ?÷;ÁÙËøÐy™›ì’«Ó<3ºˆMŒØdl¯c‡^<Ñ Zn¼7ÝÜîè-|?],®‹‹Å½ÿþÛ75OÏátñK´þÚZ}Àôéñ¨Ñ¬*ñô£›#‹ÝÕ§Oÿó«—ÝŽoZzî#TRÓ}àæpÏQt±Qa R ‘šöZQùÜËLÑoŠíô1ÈCŠ@át2ÇØVà%Ü•&¨œqçAd€›ƒc6äD]+™µjÛl
W¨@,-b…ÚÏãŸn)þ2)˜ä±é¹rƒñRôkDIk©›X'±ù–4"ØHj1³ÿ ™9z    0707010000bcc9000081a400000000000000000000000154fc7bfb000005c5000000660000000300000000000000000000003b00000000root/opt/opensvc/usr/share/doc/template.sync.netapp.env.gz    ‹û{üT template.sync.netapp.env åYMoÛ8½çWL’ƒ.‰ãì=$0ÐEE¤§¢(jdqC‘*?œxÑ¿Ã‘äÈŽâmkì„‡ä{3Ãáññk~Žá?Éœ_yaÙ ¢iþÌÜ+~ÈÜëbwð=-öxúãà’×;ëV|pf~‹Ë;ëŠ‹nôRitÔ|úŠŸ„ÃŸQ9ìÆ¹vSkãìBye2óø,´çæKuèæôÅn•ÂªýÅ µÀMá7ÞõÒ6"×8®}ÛKj»®¾´´ó‚izÞêEê•õŒ¨¢Çò%zÛØ' ŸTY¢Cœ³Ÿ¦9‚Ç ‘ÌZ(dÅÉ-íðËR#AÌq?¡É]¶™QO;ÊL#Bõ#¦3<`æªA©J…žÑZX	%Âògpˆ,N-7#šZ9GO¬aø<[Ð#.wLÖFxQÔo‹_Zu+cBÎû
¤5ex¢Ÿiê¾²Q“Üq ìà¢í 8}2"¤DïúÃ"ÜC¥<ÆTð¨³~clÐvÂ…7'dý‹™¾—sr’Ö„æ2!Ø­;è$9ª&r$·AÀ
­“¿È…¼²aöšH÷BÕi¿79£%ŒÁÄÜïÖoàÔ?‚VäºmÉëÀG—«àDBþïýs¿´ªUè@dËÉ»ÐîÉñ­ÂøvgÉdmW–üÒÊ®2¥ŽhˆÊÄ»(7°•c%ŠÌMàsŠ÷¢ntûnF½D“%S ¼WsÓn®•6ZÖÆ¸ƒZ¤I;m¡$²
x6O{@sŠM{ Fñˆ[$³öÏ-¨u»mÇ`[5¯ã–°€_ÃN¿µº5“¼V vLÍçäÚE›Ôø¨¤¢_U2Ø¡~ V’kÊQ„kÊ¤‰|VHC_ömÖÍ|Œ‡Bù4ç½â¡[ó&äÄHàÌOr]ìUšÅò
{×‡ÎÊVtm“Ü Ð{ïWK~"á²äº=
¼%<˜¥t3+“ñŒ‚ŸÞda-QRÄFlžáãò¸Š9¥P::Ž/›æ´¥}Vp×†‚B"šÊ¾y,£æ³ÅCxÒê–BN¬Û£Œ_RÆVw`ùqX=×[!ôXúkvLãñÂpû‹oÓÿÙþö¿ŸOÊ„mf'õö[Í¬IÏS–6¤µKOø1¥üêlÉ
»«”¬Ö#åÕ-ÄP3<uÆf_ªS~=¥™éÕ¤ .mTŽpüW°ÐJT‹”PÒ?ÆšS_Ñ*Š-r·4¡OÖd°œÏ'1ó›ü¡&ùÿÃÃ¤£•DRªÏ žZÞìb®ÿ&ù§—N5!ex2ì3ú5J»ôŸ½ Ÿ\{ˆ¶þ­ûŒ£(	o;Œ+ˆ¶áÈXÛf?QW£m‰‘šžÕâ¾‚8*Åu{€ž"9ê><í!£jì!Ù”äªý9]î7¦câƒtªÈ”’šýô)‘ #M­/èÞ¢ù„<7ÀÀôi’%úÞO<ŸRg‹Éˆ@»/Ðè>Ãú„L£ºÖ¶â«¬°ˆ;W¿z<¯°½]â#ä|Ã3iG:+Ñ»3>tžVžñ¥«´¦ä"»~ÝÅ)À×±]|õtß¦Ó‹éôtzNßÞŸ§¢æÑ	MÿâÖ¿»ÖñÔGsôcœ5šõM-îo
Ôb¹cÜ¿{÷‡V«ßŒ
©ŽP+CûÀOà:±è¹PaQ ‘ÛJ¬üTËl9,ŠmÔ1ÈBŽéÒ«¹¬J”pÕ_<råAHIðmùð6fIV@4V²PWfÓ¸@bîk4aÂ—n’”ÿØÊ0Éc90å×ÆË1Ü!ÈZ®•i¹Î¸ø–­ kE¶ÖFRc"þ=­ñ"     0707010000bcca000081a400000000000000000000000154fc7bfb00000812000000660000000300000000000000000000003a00000000root/opt/opensvc/usr/share/doc/template.sync.rsync.env.gz ‹û{üT template.sync.rsync.env åZ]oÜ6}÷¯`’µ€gâl‹>$P'^‹Ò vúGºšáZ"µ$åñýñ=—¤Æ”=ˆÓxwæ!VôqIsî¯øìÙCþöž‰ü±9·Òå¾ð«Ž„åã¯4÷€?˜{Xìö>ó>;ø²÷*¼ïëøÆ{{<ñsZ-­^¦Á-qrò€?ÇÒ{e)rj{â³5Ê)£•ž¿Ç²qátEµìŸfôÞèp¶”ºR•ôä^fg+êHWîÚ½®4œ5”ïv%Î˜Þ–$L-ü‚‚¦â­ÔbFBŠåBaŒN–4qÔI‹+Ñ(çùþZ5ä„±á•²NtÒ9Ü&ÝD9áMÄw*ÞÐRÚõ@-I~ÓºoæŒ#Q</¦™©T“åÇùî(ÊÓêäœDm,¦ï¥jÙ{žÀcmÌF+çÿßXL†3Èy¥¥ÇÐc.ï{;ÿ~°Ø-°{içô?¼6 Qü)*Û‡ñïpâ«h)­šÁ\©\$»naú¦Â‹”¤.PÃQ0(µ5m€ÿÃÇ£Œ~VÈRd—ì…‚ãâÑ¾xa{Í/?'‡ï…óÆ2gnö+Q„‹Ì\Ûƒ=m<;zœ§»`áãÑñ¤Qçâêµ¹ì©ÌÖúù°"è#* ‰ Þâ›µûæY«á»¹ç¿jæ#‰¹µÆJÓk/:£´ß¿ÁèÉïoÅR5¸ «êX–ž©Îî¨Tµ!ÙÓ,K²iVba8
Ì1H¸ŽûŽO¦â”õU†˜žÙêcÆ½NÖäWBÇ	2»S¨³JCÜ'Œr|"~håŠõbñ?! v[‚€Ó²{dô®Oùå8ÇÏú:ž=ì]ãÕÛ_aL·H¢5œòl;ã(A?ô©:øý`v;ØbÌ8 Aqª›ãô|<ðt€ÌRÂÑõ]g¬w¯¢¢F®Î­‚!I^ƒ£aÙW¼É+¾}t ³„†üû’3Bº¤«4ŠòzÑÃÛj?Ê´E—egÞªùœl!è’ÊžMÃ¤40¸
S?hXûQ„+³6·†“´6ÌÊ]M+Í8­8F„„ÃåYG!Fïç5 OwF~I¤1MÀ6š%+ïæäSÜŸo‹	tY6}E; ¹'xIž«æ'Sq¸½œžL0ìõ²êè<bër!õ<Hq¨ªY!…é˜`WH³.00j:ÎÝÚ,-ƒùÛ™Ivv€™;¸1°	÷Œ•ÓU§JÙ Fpäß¾cô¹@‹Ž'K¯âaà!¡¼…‡Ù²Q­úöµó÷çál-Uå"¼±PZüû]×¨w¨ã:Ä[©4=ïH¢l¦¶ó«qü#Wr0‹öàòœ‚3”T‘æì‰ÒF	á"dkúùèŸÇ‡ŸÞ~	¥«[½¨nCÜààQ£Éë—ôî¡’M©T¯ZrCŠ©‘8ÅÌdy~sUÙâ%°œn‘\b0ù¯°35Ê¸×ñ‡ß	ÃÎDÏ¨sFüP‹$‡‰ Ë4ö…,=üK‹‡2–7'¦¥+»J×Müƒª,–¥y•²®=¦âÌÑ¥l»&Þ[à©T3a5áÔ\Ç‚j­ÈúÈIñ€ë›qÓ2ö>uƒëgîoh(|o€˜‘ˆå}8Ü‚Zò¶GÛ.Â6¸Qzgª® L,T©€24S\¯J…¿ªuTÒej•bAM3ÑWÖ1Pg#fyúÕÑ¶-³*åÒânwxHï|1<ðÃ¡+Á2@je¹Eï+³ŒÉ!YÙŠn¬©d³Cð~0ˆŒË…DèÆÊ‰·à®C! Å¢fã’H«M }…<5[®·úî>öÅ¬_çœZª¦·!¿\7×˜9—Ëühª¡¹ìšŠOŽ¸õÌä«ôúŒUßvñ‹ÂÊyjs¬¯2NV?£âÞ&Ù,åÊýÈñ·w 7Ê$6i÷³†mÐÌxL\ç*-§5•'á2JJÍ“ø$›Ü£Ì…ò=r†:Ÿ¯f»©ø`UËõÍzíÄ
mš…ŽÄõSì“sA‰ÿh£'±©wc”;Ç„ŽŒ.|lÀñ²ÅÛß|]<y"*^ÆY±ŽÖÙÚ¡ÆÒáì1Öúß¤þä]h_Æ&ÚXëÆY,ÿC` ×m)C¸S´Ë8†¾ß0®!Ú†cÀÚ<¶¦úwU£énˆÑtwkqWAÜ(Å1†@w	zHO;äF5\—äúü]ºÜmL7‰s¤#¨î!S5»	èm" $Êgï!ÐEóy^3ƒéÒ„%Þ€¶“xÞ¦ÎˆÉ¦÷Ðè.Ãz‹Lo¢:k[óµ\PÕ?ºþÕ·ØIqgFüò”Ã—¯I<xœw>zgŸ‡EçóÊ”Ï9;MK£ëÐÄMÆôôòR„¯´ÜxÝÜáêkñùéÁÁËƒƒÉÁüûë//¸©ùt_<=øG8ûS:ë{š¸^?ý²™5Ìú¬•—ga»Â#ãîÅÏ?å«›ÝŽOZ…ýP­Òð—vq¸Ð¨0iÓ†œ™ÊZAùÜËí0uÞ»ÖÇ€…ïŸÐNUÚ
Ü¢'q7ï£#^Ï• €›£¯1+Þ¸¾k–I@©ÍÖÐ5BÎ-QKÚÇ/ú¥åm$f&jòXe¦Üh¼õÈµÒ‘ë´Ac,€¸=c|RDìýFD¤.    0707010000bccb000081a400000000000000000000000154fc7bfb00000687000000660000000300000000000000000000003d00000000root/opt/opensvc/usr/share/doc/template.sync.symclone.env.gz  ‹û{üT template.sync.symclone.env åYÝoÛ6Ï_qM¼‰ë¬E±¥ÐE½ÒbEÐÒÉâ"‘?ìxè¿;’RhÇN²&X‰L‘Gòw¿ûàñèè)Ÿƒ#xÂ‡ÅÙ¥*ŽÁ-;¤×¶h´ÂÇˆ{ÚÕ=-vßx³G“ïoÃ~Ï‡ðÚ¯p¹Ð¦<KóÓÇrFÍ'Oø0BÿñÒ`šç‹ñÈ­Ñsi¥VRÍÎà£hlh.±¾qiMŸx­ÔZUÊR8´gYk‰ªÒ®õµ…îÄ´ÁØœÇî¶ ÆO¢EÐ¸:0 Egä5}œËaf´ï`Q£ÁØA{CÍ4=8afè2•Ç!„Iã°ÓLoŽv¥õFœ;ƒ´Äå¥“-jïvñW“Éã ³ex_Ðni8!Áb@ô À/LQƒü÷WîRI%mS¤n˜mÐyÃ+‡…t5)ÐmÆð§"EIK+¶xtÖ‹¦ýˆ¥Í¤(mZÑ4Ëc˜z:kÚ‘Ð`åôii…#ˆ,´Þ:ZOl$i¾ÔùzúIX‰=. U)Zí•cŽµP3¢H£g3,A¸ŒTÇ™´E-‹xZ9¼v'Q"*GózòP„Ì’Fâ–­±ˆøÆªÛf×8·»Å³Ç[ö-¢ýUKÓ‰O,vÂÐiƒ&zc% K#çi˜Cì
g
ùÙ¶í°•dýYÝ¸ÉúáælÝd’nzáƒ¬*ò,Ä¤Þ>“Ì»;¦
âÒ%‚·Lv·Bº4êwPäÆwÃ¢ÎaÒ?§ü~6™¼™Üj9ÝH€œÛó;¢
bÈSû¡/l™3!›œY2ÒÞÉ#õ4 ŒÚ&9VÅTW™.h,ÛrK›pºwA!½HRÈF¸˜=¿1þ?¬ŸÁß¦ÇûÃ{3•ÎF~÷Ä÷–ÌŠ ldK¡!‚$³Ë[qÿQ96¹ÿÞ(–6†M^x+UÕxTEáÁÜM&kŠµ˜K7†¤9¼m×Ä¾#%º‹"c·rF€ÛnD­gÂÂ ë›àl,¶þÀ‚°šíI€õS‹»frÏAƒ€â-3F$¢+¯w –¬mÇ`šWqc,àG>è§Ì(í™è5 ˜˜¤¤‚s©/¬—Å)³T„Zäù€É5M‘2‘AšT@Á‡|–ã©ßöí<­|“JiyÍ{¥‡´çu5#‚ý°ëJ°‡@¨òÁÁÖÞ•zƒC’r'ººc7(š=‚÷³&?Á¸Ì¹î˜wõQqT±ð‘”œÒëZÈC3'ìÄé{ô)æTB6Þ„ø².®OÞihGA=çlî«ÅÊ7á<sžyE!Ç·d:ÖX‡mnÕMÄ	|èu}'!D³K{©ÕŽ1bs¼`wd‰¥éú×ø¿oøù³ûFš™£cõò#gVç¤ïœ¥åjMéIøL)ºëã8¢D<”­D.:-D]†º¼à»1|6²åüÆ[J3¹+3 Ô›jC8þ+ÑèØ†rÎ	%ýPZØšvQæui¯hA´9>ÄS
“9ôßáÅ(ùlièy4P„ˆAÞRz¸ÜÅ\ÿYòO[Ù9Î6ðï°ÏèWTšÒÿàÒ¹ú²Ïß·Vp´uûŒ£¨ïnˆî®„QÝí'Š›Ù¨»[d¤¦{¹¸¯ n¤â*†=@÷‘užöÈlì!Y§äÐ~/÷ÓMäÜé
T )%5û	è6’ (Ê­ èÞ¢¹…žk`f0=€šñ*g?ñÜÆÎˆÉ‚¦àè>Ãº…¦·Q]ë®âkQcéw®~õx^`¼r\¹èºu&íñàSp^ùðÖ¼‡Î—¥.^rtZU¡ˆŠŒitâš&×[©>|=‡o‡“ÉÙdr29¥¿ïÞœrQóð'¿…ÖW©Õy<±^~ßr©ŠËV\_–ØˆåŽéîôõëG^XÝ®v|UÒq¡•ŠìÀò4ý²¡P¡!  bªù2s(Uæs-×[ÐU^[«c„)ò}³•%†²—(á¢Ö¾)Ó$WA
‘ùmL¸Š]×È"(•ÙœcbfÛxyM³Ä4ø[O¡b€‰ËL”]™oŠn¨ø–•u-UÔõ(ßF«¥ÛÏ¼¨qðÇKíW†$   0707010000bccc000081a400000000000000000000000154fc7bfb0000055d000000660000000300000000000000000000003d00000000root/opt/opensvc/usr/share/doc/template.sync.symsrdfs.env.gz  ‹û{üT template.sync.symsrdfs.env å™MoÛ8†ïþ“ä Kâ8»E	l"Ç¢Ø¤§¢(idq#‘Z~ØñbüÎP”+;²lÀ€|HlJ’Ï¼œ¡Fgg‡üLÎà€6gW*;·j¾ÖÖä…ýs‡ÝaÙM¾óbÏf?&7a½·ëO&<÷'\-µÉ¯ãøt1ŸSóÅ?LÈàß^Œã<ÜÚ½Vj%ÕüîDeCsŽ…ð•‹sú¢UhÍ„Êe.Úë^kŽªÜnÝk3Ýˆ´ÂþpíÝ6£¶/¢FÐ¸2 Fgä3]\Èan´o`Y¢Áöí5Óèà„™£ëy¼íbA˜Øó)t0ëBëAÌÄ\”ÿü|Á6ÂÐ<†Év<Cf´!ZêäÞâš×›0ÎÌð0‰9*KYUàÌ
œîÖ‚¿µ8Y“S,4‰±UÅ˜S‘=õôJ}|M‹pÚ€È­§¥MÞÒ†p;1·GÆú$û	*i]-­w
ŸL*L~óH¼ÅœQV²–.B–)`èíÎ9–œçJò–m0“…ÌØÚî5íµ]©ŠÊ£ÊÚ “¹@Ó³•b)’ÌMáŽ<‡Ï¢nªöÞ„z‰&aS ¬•seCûZ­×{ÆBë+b–ƒEÃ›.¨ Ìf÷ž³>µxl[î=d(¾€Øs`K¢õáëjq·¶uó&7fÿö;ý¯m×LòZŒLÎçH‘”Î)x™Iú/c.n¥¥°=µ
(‘BSŠÂý´&P2¡˜åxè›.¢ÝÆ™ù!—–ç<*?Ä5o»‚	<ø‡CWÄ2 Qå„lKïr½l“C´²—®n8ŠjDx¿jŠÌe!(t·§ÀÄÑ]	“‚'”DB‚°LzÛýÔl$yÃ7¯øãR¿Î9…•7!¿l›«4í³<tm()°£)¡Lá›ÅÂW«L/=Uò‰RŽ¯(d…veÖýXüÌ8A¯÷
BTK±²Z™"†ó‡#KŠÈMÓ}mÿwoUIõ5™›É9$ýV3›cÒu>¥õÝ'á2!Ðµ¹¾íG’X–2+73—´Ñä¡¦ÿôâ›)|5²æó·tÌä[Yá¹4Äñ„f0C¹à%ýPZ]Ø’V‘÷Ÿ„¤}¢	}Ö*qÀùÐ•2L¨½3\‡“È…âørÂ:ZK„„ÔBRcðñÏúïrþ´™‘ãÓ>cæv'ú—ÆãˆrhÏ1¤ÑÖ™£(	o?Æ5¢}kÝŒ“â°uóBŒÔôªÇ
qPŠ›;@¯	‘u—žFrP’mI®Û_Óå¸™‰séª7È”5ãºK¤d@¢ÜúŽ–æynÁìazƒ4Éý'Ï]êl™4^xƒFÇŒu‡L_RÝ„µ¯øš•˜û£«_½Ïûðøˆí#ä¾þ3iÇƒŸ‚û•oÍexè¼ÌuvÉÙišiU„"F(2Æ~lÐ‰g @Ã…÷¶šÛ]½…ï§³Ùõlv1»¢¿|¼â¢æé9œÎ~­¿ÇVçñÂzuúcÇU•=Öâù1ÇJ¬ŽÌwW>üâ«—ÕŽoJ:®#ÔRÑ>°Sx`/ÚP¨Ð(€Hõ{¥Š |®åzºèÅ¶êd!E wZ™c(+p‰îKí«œ/qåAd9 ¼dì¿Y‘MSÉ,
(–Ù*\`bnkTn^ºe¤4øK§P0`’ÇªgÊnŒ—¢["*HZ_KÕú:	Å·dS I|=Üo#©GLþ©:ií7!     0707010000bccd000081a400000000000000000000000154fc7bfb00000653000000660000000300000000000000000000003800000000root/opt/opensvc/usr/share/doc/template.sync.zfs.env.gz   ‹û{üT template.sync.zfs.env íZ]oÛ6}Ï¯`’o@âº[±‡ ècQ4íSQ”teq‘HN\ìÇï^’’)[q\$é²y~HRZ¼"Ï9¼_ìññS~ŽÙ~ÈœYÊü„Ùeì[imî	?hîi±;øB›=ž}=xë÷{N;>8 eßÀòVéâ,¾ÚèOŸðCÐhøÓ	ñ-Ÿ´mµZ#”r~ÆÞñÚøáJîjWô^I?šsYˆ‚[0gÉh-ÈÂ¬=krÕò¬†ôuái“ãØ•r:†Æ¸ËTÉl^S|î-B€ •JTûŸèŒ’[|ó½(![P²\Ïá_”T¸iö+tÛý~w»¢ß:„1×"C[·•È«h×TÊÕn$± U²‡–•Z5è/§BSÑ
h,€^”0Nu-Ãý0í$m~Ê®.Þ3c•æs|¬â›øOsCö¤²,Œ2«º§ÐÂÇËw§µ¸Ör‚º7—ÌJlõó;À&^A(‘ â˜H§‚ðì:é±“P:U…B£(ŽdÎÿšøNMô„£,<Èã®cn^˜ ïb7…ð©
i‡‡Jƒ	1É¸¶UÚÏè¤€š/¯äí„`™â$dÿB’´ABmÿxÐNAMY–knüâ)ƒRuj	ÉOåã¥˜›´ÊX²pmµ˜ÏAOÜ!sdgê×ÑA{_\9ûI*OÜÏ$?«ks­HŽuÝ/Ê¬V„P-ÿ yÜˆ†FYgã$±å—›½¸LDm°JÒüæâ£7š›-á
-¡¿zþxõ}²›=qX'êðÌJËnòaõ2€í÷ÎøJ…’YÑ€éè)‘=¢'ãùMÊ†òJjpèÏ½@Â9&Ïq{sÄ7à¾`µ0!qÂýNÙ…Î„ÕœP'5â›ÈÕv‡·°Do†ç¨#‡°²eZÈE)òà®T+»B–µ™‡³^ht¹é	ïÏí”½CæàŽ7mžà¬èn7FÌeðE½6ë‰1?Á¸1+º ´Ë™3.3? EüçeàQÜ 1!0 jÿçÔâi{a°õÃO™FŽQÜ3+ £ó¡ô©q"ø[”ƒ|¨âi¬ä¬tM •5!)eBŸeéÕo;vW>Z
CkÞ+âž×i@'†÷üëŠ°ûQS9[¨Û¢•­èª–Ü ¯÷Þ
ýá²àèº1+ÃÀ;±øÔ„¡'%ÇDüÂCH¯³†f-×>ÀÇ	Ë\sJ.j§}|Y7W+<g…ŸÚbP ¢1 LÙg¥«ÑWé$<ù*¡pMËJQƒYMzËUÄñzè¸Þ*^ßò¥¹Vò…)â‡÷FeJ¬“¤Üòš¾¿§,-¥5¦'þkL! ÖaJ"”¨ƒÈ…é}àjmZ„´SöA‹†òg¨4ÄGI¾Ô¨4âø-$ý¡Ê¥„ÿ!•<%ib]Ä.èRÉ‰e}ALbÅ+}Ï±^äÿIG½D¶ö°h¸~‰¹þ³äŸÔjh-e¡€ƒ´\Õœ!ý÷^€€ì!Ú’†P•µÏ8úšù{ˆ¶áè±Ví~¢8®FÕnˆ‡Ôâ¾‚8*Å!†@	užöÈQ5v¬K²H—ûé˜8Ç @µƒL1©ÙO@ï)2"QÝA {‹æ=ò\3ii¢%ü¹ŸxÞ§Î€Éˆ@ã;htŸa½G¦›¨ÁÚÖ|Í+(Ü‹ë_=Çöðåc¼SÀ—Ö¤T§gô+_t¾*TþŠ¢Ó4W²ôMßdŒóÈ åwø‚45ÞC7·ûöœ}9šÍÎf³ÓÙküùûo¯©©ytÂŽf¿øÑ_ã¨upjœ<ú:Î®úºáw×þ¦ï…q÷úÍ›G^Xmv;>Ka©Ð‰çÀÄPãÝ}'ÏÔ’V…W>õraªL›bk}´ÑÝ£4¢ ßV %»
wñtTÏåH 5+·1Kºôäm[‹<
(¶ÙjX@Íø\4 m¸/Í5ÝÀªŒ•0Êc™˜2ƒ÷õ·›k!×ñrs(€pµ9C©y"þˆúÜ!~'   0707010000bcce000081a400000000000000000000000154fc7bfb000002fd000000660000000300000000000000000000003700000000root/opt/opensvc/usr/share/doc/template.sysreport.conf    #
# sysreport configlets go into /opt/opensvc/etc/sysreport.conf.d/
#
# each line must begin by either:
# - DIR
#   snapshot all the files under the specified directory
# - FILE
#   snapshot the specified file
# - GLOB
#   snapshot the matching the globing pattern
# - CMD
#   snapshot the specified command (full path), its stdout, stderr and
#   return code
#
# configlets are merged with the internal default collection
#
# sysreports are scheduled (see nodemgr print schedule) or can be
# triggered by :
# - nodemgr sysreport
#   send to the collector only the changed files
# - nodemgr sysreport --force
#   send all collected files to the collector, even if they were not
#   changed
# 
DIR /lib/udev/rules.d
FILE /etc/groups
CMD rpm -qa
GLOB /etc/init.d/my*
   0707010000bccf000081a400000000000000000000000154fc7bfb00000439000000660000000300000000000000000000003500000000root/opt/opensvc/usr/share/doc/template.vdisk.env.gz  ‹û{üT template.vdisk.env å™ÁnÛ8†ïyŠ	rð¥5zn`,z\h÷´X´4²ˆP$Ë!íz±¿3åÈŽb…ƒ5 !ˆå9$¿ù5CÉww—<nîà‚‡¸ÛÔš/çî‚»»,»›¿òbï>ü}#S}ÄÝÖ…úc. E"_xÁCü‘tÀ2ÒeÅìƒÛhÒÎj»þød®±QÉÄ2­bª”­u­"’Xÿp¶4ôhk*+UÎ«•ÁÞü-$Ì‹åÖT±í[‹ Öh#lµ1Ã¢ÖJÎ\
‚…¨;$XaãB£ŒáyÂJU£0sßÈ;^DtTy=KñÓàòž—0…;ª5]ëáq£)‚kòz—ð9¬tJ¨ów‰C"¬¥ÑŽböŒlÑ‚C¼Ør´Èc¥]‰·%|u>ùÕ¶1	-‡R"T½Á0òµÂVm4»[ÂŽþT7}Û÷R~!®@éµ¥lßk£úÈYî@É0³ÃFW˜Ugss“" ´"¼¶[î-d)>ƒ8
`O"3ëOOP+wÛ•aÛ›¹	øwÜé—n£²f–×`I`z½FÎ<`]ŒQWš?u“a)B«h¤V-rjZ¡ŠOÞ´…äsV”¡?í¾Ì|*\LdÎ³ŠCYóq8‰±Às|$uì¹0UžP›bí¶}q(^NÒu^Ò 23Âûà8O—âÔM ¸ð."·Z KqÑˆó‘\ HHGa\šƒæh$&ï`•ö5§QÚ¤ëË±;ãø>«sWÏEAÍe	6Ép®
£òdô#—œÔyh´AÚQÄn|6O'ëaˆõIA(³U;úîì•)bº^H:"VDüpÚ†×ª¤ŒzN&Ùçâ,ÿ½fÇäë²K‡µlOòeÞB`ìk}ß%±muÕV.M%!GÎ’_ÂCÐìoñ6SšŠ€v¶jsüGe¡¬PodCÉ_¬³ï©åUÔ#_²Sç	ýîì"‚ÔÃØê<¡¾e¾··P++ùÿöVt´—©‡<%$ðû5îõßdÿIUÐ>Ênb•";úƒ–íÎrèÄ6Ä;Šsæ¨šÈÂ;qèÇÌÚùyRœV£óÏÄÈ¦³Zœ+ÄI)2 "'ê¡<Íä¤$Ç’ÜÛÏérÞL§Ä9…ô Õ+dÊ›šy}I¤dB¢b}…@gKóyÁaz…4ÙÿŸ'Ï—ÔÙ3™h¹ð
Îë2}NõÖ)¬*¶oÎ²_Ûÿ‡ò™_¿Ë;pÌoAø	8?“æ·îŠKltˆIèTÕj‹ùÙr)?•ŒžG…ÖoRš¬êðøÅ=ÿÉ#jòÜµi0Èï+>lÙi~Ío:²µÄä?_}ŽÃ     0707010000bcd0000081a400000000000000000000000154fc7bfb0000068f000000660000000300000000000000000000003800000000root/opt/opensvc/usr/share/doc/template.vg.rados.env.gz   ‹û{üT template.vg.rados.env åšooÛ6ÆßçS0Èm€íö¦E_tÝŠh×ýƒEÐÒÉ&B‰IÙq±¿çHI‘mÙ	šqg£hlJ<’¿{|w¤|vv—¯“3q‡/6·˜„_U$¬ÌŒ»µ¹;|ÁÜÝ²;ù²˜=þzò,¬÷y\ñÉ	Oû‚VKc³§ÍÐª3rhßá‹éXú§V–š^Iíˆ›+kÊ)Sªröôª9£\ÖÚ7³úË”¡5•e¦2éÉ=íµfTQ™¹{]j*9Õ´6^¼Ý¥hü8o–,JYsŒ )Ó•ðså LÜ™Ú¦4Ë¹ÂÐ•L©çnG•´˜R6ùgçÆòMµ¢ÒŸ«ì¿k*ÞˆÚ X!k?G«J¥Ç”ÄRù9P·¾pdd£ìHí¡‰«:–ÍJ™¤6æ" dliÇØQjÉïcÜSííÖþnÖÚ¤?hºLuíÔ‚Ä¿ÂÍ%lãÍíè¿ÁÚ™Na2êÐ÷¢G@È€À¯~'Åó8«s/då†åªR|Iâr’¯7»Xwê
úì‡aÝ†áÆwÌ¼ÅÛ#·G©N}£‡ûÑÖ”ÀxÕŒííoL+|Ú(t9‡’éÕ“ûæÔ.ºIÖ°z2Hj1ãÊàÞµ0´â•
“‡è¶0ºÆ§™5uÅ°"‡=’Bä¥}05í@õøvœ¢[60A5H¹K¥µðvÅ¡¬Y»]µ(JáW”S‚À :©5Çµ©Y´}¡/Ã.°Ï‰;å„*ÉÖäs,a7Bàÿ¯LßÂýBhå|Ð$Ö;/ìTy+™:>óH Ì™$Ô˜ZÊ7ƒeäÓ¯zç¸XIá*JU®R¶6LAWvU™ëšJ¸’=”YdtÛ³5¥¹\(˜›ˆWð]Ê¢ÒñÞ½d•„<'S³ÒÅ‚¡ÕFôzÏXèàjÍûˆP•)ì7Xa6{2]=uth_¹ûA ¸±çÀH"0‹o÷Pk¾m†­k^çÆ,P¿ö:}××¨Y3äÕl˜šÍ‘G”\ÛºZ¥
UL±-v=µJ1'„¦)!mvÖPyÔ•@Ìò¡Zk#ÚófæC~È”ã9•š5oºAþáÐÕ` T1TÃµÏÌ2&‡ÆÊ^º¦â0(õá}o'˜ËB"t;½‚H<îJ¤˜äl<A		Â1éM/ôS³UðFØ ìóÇHLë.çäRéÚ†ü²iN|Ï²ÐµBR`G#¡LÄ'Gy­Ã¦ò*=iu”S•È•&·ržŠþ70¿Ê8A­¯÷
Bê¥\¹sS˜"†ó‡#Ed¶jßÆ¿mÃ÷—ºƒ2	6“‘HZûQ3ëcâ:Wi}·6åI¸Œ‚|Ìõ±¾ÌU:_Ï\Ê5¾€‡*ß3VWñÞb[‚ú¦v¼YÁ­¬ áVe:·àø-éXJI-¸ Ä‡Ò”ã¸1îÙBˆ¸À„~7eâçÃpžiºm5_§§"“%ÇÿÓSÖQ')BReéüký{©?]jUå¹Ú KJkOmE¿æÒ¦üQ€Avˆö”!•qþ˜9ÊÜ“½c‡hÇÀÚTÇIqX¦Ú#š®Õâ±B”â:ÃÐuBD nÓÓ‚Tc‹dS’]ûuº<n¦CâBº†ê2EQsœ@w‰@$Ê­7èÑÒÜ!Ï˜=L7&,áÿãä¹K‘É€@›7Ðè1cÝ!Ómªë°ö`Í\~`(Û‡H·?ZÙÞ4ÿù~üéoìõj‚=²ËQÔØúN)n.§+ádâÍç·íóÝ’dÇ½miÙªJÂ™x”ÑâQæ.M&ÃG,Ø±¾Ã5»Tßh$d6ž›´ß§g)öVÝÃé˜Ëá¶ cðÐ<u Ìg:æ¾;<Ûößë¼uT<‰w0óáóËµY2Ó2ü2nü«øii•_{TÑþ¼a„µÎ”óV–>èâç@öQ‘uhçgds<²À?Ãªg‹}Ê£†³
>E	7Ä£‰ðàQHïe:OwÂFûpDüq‰1âoÚWo´x¨ƒIEåc÷Ò@­áGkÇyc>tyo<i/‰¥t¡§ÆZ½*à“ø8Hz†À@îõ”ò@¹5¾ëc†àNüš;VÞœõ<O(ùŒ‰ÇDë¨g%¹nÒ>ùâ¸µO€W2úqÞ¦nˆODºŽW„ÿ!äŒâž)   0707010000bcd1000081a400000000000000000000000154fc7bfb000005e5000000660000000300000000000000000000003600000000root/opt/opensvc/usr/share/doc/template.vg.raw.env.gz ‹û{üT template.vg.raw.env åYQoÛ6~Ï¯¸ Þ [é0ì¥EŠmÅ
lkµÅ€a(h‰²ˆH"GRr\ìÇï;RR$Gq²Î‚Ùb™"äwïŽwÇüœ]Ð?,®Ý,ÉïŒ$+¶GwÄÄ»³?ÚÍÅ³?Ï^„ý¾äŸñ¢¯än«mö¼›8“­CëêˆFÆÊ¿ee7Í{ÛHn5V·Ê)]«zóœ^‹Ò…æLæ¢)}·¤_uZSQg*^ºç£ÖLYgn¯¯KµëRŽ§‹½]Š6A¥ržtÎÛU©$#|á–´-Ä'°˜,Kè}¡ìz:VŽTÍrdFÂ‘ÞÖxXïÈ<Ú¹X2ÖâöÏMÂc¯dt©Ò)7’#Œ)Æ{Íã«Ë}Áª€¦.ñ}Y¡ƒ´—®M“ìÙí¦ofuÙnjQÉÿ›6;Á#u¾â¼SV(£ßê²Á¯Õa #€2×z(hÇë©“AõìÈ¬g˜ÄFÖž¶ª,ÉÛÓ­Û;ÎžtcÁÚš¼ª@õµÌµ•”‹²Ä:i-Ò«e#U©Â&¼¶$R&w n/ò%¶0·›Ç·1ÿëG02¯#ÃûMè•]+o£Žß< ¦ÆÅS_ªJùÄ Y¢u„v¯åùÚrF¦*W)KKè7‚rU—¬¡JÖPfU+íHÖZ¢U—ÐkhN^‹Ê”±ï£„Y°(5§6µí7¢ÖGÂÂ ×”l+{»XVs÷™sÍÚÉ§väƒÅ[ Ž‘˜ÅÇ¨u§í‰Á64Oqc,èïñ /:FÝžA¯ÀÎ€©ÍFÂòP­3øÜF¥
ß*Ÿ¸àBLü,¦i-…¿‘¦jjÁfyžúEoÑ^v+Ÿ“”ã5Ÿ”º=ï«FúaÓÕÁ< PPÑøñQŒj¢”ƒèjÃfP”'ï;;Á¸´¦;„™´ðèµ Pq‘³ðœHpŽ‘Þ×ÂØ5[Ž%s>–´nŸ“U66ø—}q¥Æ9ËÂP§ÀŠ†CIèƒ“ySÂVÙ‘{*Õ\NSÊU)Ýñq5>ùÇ	|èu}¢ÜŠû¤ë'ÆˆyÁæÈ™5ýcüî¾<Ô¥I¹XÒ¢—93ï9JÛ»»px^#„>úú8”Àu(-¦žK¹NÐñ#aIèUÇ7C˜É]™ävuZXàø9^¬L¥j9 ÄZ×+W`ÙHLÄôƒ®žØúB…Åžá=ŸS&j¶ÿççÌ£" RyŽHÆÊOO1Ö”øÓ¥VÏÑ†¼–iãeÑOTÚ…ÿÁ
0DÂ£?eEîA¼Ã0Â1`­Íi¢8ÏFmn‘M÷rñTAœ¥âÃ ûˆCÝ»§r–=$û”Úïãåic:GÎ9H'P=€¦jNÐ»H
@f(Ê­ èÉ¢y=÷ÀÁô jBþŸ&žw±3b2CÐîÅ8zÊ°ÞAÓÛ¨NÁ: kæò'e_Dúï©•Û—æŸÞ­>üŽûc¹KpGvù‚ªWßµŒ—KM!»2?ü…ü¬e+íjt-­°ZÅ•B\AC.sW—I’ô¥Á„ÞâÝªÏrI"[:IŠ£ÕÞðs9ÔÀ˜Mš§NÅRâSßSc·õ÷&ï3bKz1¿}ü~RÈiX>‰PÒ_}G_m­ò“RÅuZ6NµP•å¼µ¼ø: û-iWtk?©ØâÉ°Ébò¬!WÁY”Ð!¦&Bá‘„÷"-úš®rCq„~¼Æ@s$n4[LêÀBÊÊø8¼Ö`k©Ó«i:oÅI—7ù^A¦E[áÂÈ{õª‚Nb9Hx¦À€ÔëeÊåVW8Ó…1†À#düº;ïrF#ÍÄ%ç˜xN´.GR7Ä]ô•/¶[m¨^ÁÐ¯ó¶PC¬ˆo„ÿ µ3Ÿ‚W"     0707010000bcd2000081a400000000000000000000000154fc7bfb00000589000000660000000300000000000000000000003a00000000root/opt/opensvc/usr/share/doc/template.vg.veritas.env.gz ‹û{üT template.vg.veritas.env å™ooÛ6ÆßçS\Þ Û)0ìMƒ¼(¶+°­ú†¡ ¥“ED5’’ãb¾Ï‘’#;Š4	`ÀFÑØ”x$÷èŽ:ž=ççäŒžñ#æÚå”üºfjÙj¯ÜÍ=ãæž—ÝÉ?íòìÕ¿'a½—ýŠONdâ×¼^›¾îo—•*í³güËÿ5Úr7ÐGÛÈ T[Ój§M¥«åkz«
šSÎTSønR™*´&ªJuª<»×ƒÖ”k®R·s¯KL­ÇæÎp¼Ý%hü˜3ÉJÉdäñ½5Eƒ_Kkš7^D€•3
Ê²óÊú'µaò(T¯žÆ)ºe“Zråi¥‹‚¼]“7ýÚIÉ7ÓØ,Éë’-83–)SEyÒB%×ƒ§}v‰ExcI%ë™ïÞä%–0†Û«¥;0ÖO—åÜo¨ÐÎMb½szcÚ[%Ôñ[F`j§‚²Ð¥öÄ`™Ñ: Ý;ÇÁy>‡·\Í‰Ît"ÖæôÁ@à»ºÊŠ†+¸R<”Z(1°µà\µææôžãUÖE¼w‚^ªžˆ)RÎéeåBûFÑëc¡ƒk
0KÉ±muÂAa6÷?s®Y8>´Gî%d(Þ8p`$˜Å¯{¨uOÛaÛ4osôÿ°Ów=FÝš!¯À.€éå’y¨2)£N4þê˜:)R®Ü@­ŠrFhZ°ò·ÖtEMMˆY^†¾è#Úe7ó1?¤ÚÉœÊÝšwÝ€ ÿHèê°‡ ª˜¹¼ñ©YÅäÐYÙK×ÔUqDx¯â„piB·#…Ä;ñ¸kBâ$ã$‘ œÞõÂ05[o4õþ˜Ò¢ÙäœLé¢±!¿ìš+ž³4t­‘ÄÑH(súä8k
Ä*;HO…¾FÊiÊš2]°[;Ïåð	Ìn3NÐCïë½‚PÅJ­ÝS˜"Æó…„#E¤¶î¿Æ¿}Ã÷ouGelN¦4éíGÍl‰ë²KºµÛž„ËØB°¹>öƒ$V¹NòíÌ¥]çx¨öcM=§+«KÙß4ÛL¹U@n]%¹Ç¯*ÍrÂº•%~T¦š¹«H¶"®1¡_M5ñ$ùÐç:L(Þ®Óé)¥ª’øz*:ÚHBŠÇ„T[þrˆ{ýÙºÄêÚËnƒo8i<÷;ú-—vÛÿäÑžmHmœ?fŽ*óÞ~ŒDû8Ö¦>NŠãj4õ1¢éA-+ÄQ)n3ì=$Dê>=!ÈQ5öHv%¹iH—ÇÍtLœcH·P=B¦ØÔ'ÐûD
 #•ÖGôhiÞ#Ï˜L&,áÿãäyŸ:#“v¡ÑcÆzLïRÝ†µkê²CÙ"=½´r÷¥ù÷«Ù§¿ñþX¬çxGvÙ„Ê¯¾Ž/—†Bu%T&þøü§44øYqËv6x--1[]+ø!¥ó”ÛóÔ]ŸÏçs#%¼±¾Ç5»Ò_yJ*å&öXŠ½õN÷PsÜ`ŒÍÀRÓ90÷=ciì®ÿÞe½£bElJïaæÃç_¶²T¦O*Ï~¦VVû­£Š›¤hœnáËKí¼U•ºø1ý‰j¶írF1Ç’þ‰Ö[âS5Ô*¤Šnˆ¥‰pðHÊ{•äñt'0úÃúíc€æÀÜ`´XÔA„ä²ö±{e ÖÂ$×Ûå¼™]Þe;2ý%Z)zX«×%|ƒ”WaÌôà^Ï‰”YSâ¡Øž˜0Pñëî0XyW3x&V(¥Æ$c¢u:°2¹î¤?ù’¸Õ†S#àU‚~¶	o7Ä‘MÇÛá7â&      0707010000bcd3000081a400000000000000000000000154fc7bfb00000532000000660000000300000000000000000000003900000000root/opt/opensvc/usr/share/doc/template.vmdg.ldom.env.gz  ‹û{üT template.vmdg.ldom.env å™ßk#7ÇßóWLÈƒ[pÌAéË…<×ÜK{p×¾”dí¬WD+©’ÖŽKÿøÎHZg×Ù8¡uÀ`s\l­4’>óÝý¸º:æçâ
Žøasë¶ZÍ!n‚®lûÍñCæŽËîâžìÕ»?/nÒ|oÓŒ/.xÜ÷¸ÝX_½/}{QøH®øa>ÿê”ÇÒÓ'¡r±óv­‚²F™ÕûÇâ
kÑéX†õŽ‹¤0•ªDÄÀ¥¿XS*:4U(ûÒ ­K¹ø›ï0M–jIeß±Ba£´†è·m?wüÍv^"ˆªÅ K¬­G¨…Ö4NX
y?ð9µd±¥IDëAÈHóYP7½É[šÂî(VáÄX÷ˆûh"Ø:ÍwüRE/˜:ýæž0t+F©U«b˜,#•h÷Î	ä¼Ø·‚C©j%ÙÚ¾Úí*Së¹’=TyµF?°µÄF¬™[À'ò>ˆÖé\wF­„›±)!¨•	©|§ìõ±Ô tš˜UÐ¯•Ä¤‚4š[¨­Aè–Oí•{$ŠO ˜I$fùëjåm;1l»â17fÿý§×¨Ì™äµX˜Z­"[!aTRÑ_U'ØEŠÐˆ0P«€)4-QÄGkÊ@ç€bVä®oúˆv[F>å‡JóYù¡ÌyßÄHàÉ?º
ö”ˆ*BÓÅÊnrr(VÒµŽÃ Ðg„÷‹¥8Á\Ö‚Bw A‰w©ÖHŠ³šÏ(‰¤˜ô¾†©Ù+òFç^ðÇ–Ý.çÔBéÎ§ü²oN[zÏªÔÔQR`GSBYÀoëNS¬òƒô¤Õ=¥œ®uP+a"¶Ã7°~Ì8I½¯
BèØ†;kNLÓù‚ÃQ ETÞõ_óß¾àµ*)½¾$“ds6‡Yo?kfÜ'=çUÚÐ­ey’ÓcÎõ¹IbÓ(ÙŒ3—
Åä!Æ:·€/^µ¼¾é-3¹*+ ÂÖÈÆÇ¿EšG‰jÍJúa¬¹Í¢Ø¢qOúÉšYÎ‡±Qi@¹fz——P	Ãñÿò’u´“	)Cž’óxwŠký7Yé•‹¼ÚÀ”]Ä~E?riYþ§(À wˆ,Cœñœ9Š:’ðcÜ!:Ä1±¶î<)N«Ñº'b¤¢µx®'¥8fØzIˆ¨ûôt† 'ÕØ#Ù—ä®ü%]ž7Ó)qN!¡z…LiQsž@Ÿ)™(—¾B gKóyîÁ`z…4Éýž<ŸSgf2!Ðòà=g¬ÏÈô)Õ1¬C‡¯2ÐœùlàÄˆñˆåéæùsw“¶œ¬ÌáW2óõ÷£!ÓðA¤k²ëá»Wqtäý uÔç4×í¢½0´“6zû}"û8ôÊù®%cÎ[_ú‡kôÛ-'bÃ½¦=/ïÆS…¼Å]yË'61
Ùä[‚´îÙáçêƒhÌzË‡ô¦aëbnnl„¥¶ò~|,tÍ›÷ÏõÞÁ~ÿ6"¤–šæi‡Ëµ‚ˆ"uAc zäÞˆ’;ª½m)ŒÆ‰G:9*5,Í¼œ=<“Oºø¬‚û¤ÒùÀÊìQ¸³þ…õ¿N·„W0úëÝk²ä†|²¾kxð IZ…2èïTub¯Æ¾TUº¢¢o»¹Ã¦±!ý®ÕªóY»)‘ÒeÌÞ'Ó
çøH‡=›xAöè_™d3    0707010000bcd4000041ed000000000000000000000003550eba8b00000000000000660000000300000000000000000000001500000000root/opt/opensvc/var  0707010000bcd5000041ed000000000000000000000003550eba8b00000000000000660000000300000000000000000000002000000000root/opt/opensvc/var/compliance   0707010000bcd6000041ed000000000000000000000002550eba8b00000000000000660000000300000000000000000000002c00000000root/opt/opensvc/var/compliance/com.opensvc   0707010000bcd7000081ed00000000000000000000000154fc7bfb00002a56000000660000000300000000000000000000003800000000root/opt/opensvc/var/compliance/com.opensvc/authkeys.py   #!/usr/bin/env /opt/opensvc/bin/python
""" 
module use OSVC_COMP_AUTHKEY_... vars
which define
{
 "action": "add",                # optional, defaults to "add"
 "authfile": "authorized_keys",  # optional, defaults to "authorized_keys2"
 "user": "foo",                  # mandatory
 "key": "XXXX..."                # mandatory
}
where:
- user: the username the key authorize to log as
- key: a single pub key to authorize
"""

import os
import sys
import json
import pwd
import codecs
import re
from subprocess import *

sys.path.append(os.path.dirname(__file__))

from comp import *

class CompAuthKeys(object):
    def __init__(self, prefix='OSVC_COMP_AUTHKEY_', authfile="authorized_keys2"):
        self.prefix = prefix.upper()
        self.authkeys = []
        for k in [key for key in os.environ if key.startswith(self.prefix)]:
            s = self.subst(os.environ[k])
            try:
                self.authkeys += [json.loads(s)]
            except ValueError:
                print >>sys.stderr, 'failed to concatenate', os.environ[k], 'to authkey list'

        if len(self.authkeys) == 0:
            raise NotApplicable()

        for ak in self.authkeys:
            ak['key'] = ak['key'].replace('\n', '')

        self.installed_keys_d = {}
        if authfile not in ("authorized_keys", "authorized_keys2"):
            print >>sys.stderr, "unsupported authfile:", authfile, "(use authorized_keys or authorized_keys2)"
            raise NotApplicable()
        self.authfile = authfile

    def subst(self, v):
        if type(v) == list:
            l = []
            for _v in v:
                l.append(self.subst(_v))
            return l
        if type(v) != str and type(v) != unicode:
            return v

        p = re.compile('%%ENV:\w+%%')
        for m in p.findall(v):
            s = m.strip("%").replace('ENV:', '')
            if s in os.environ:
                _v = os.environ[s]
            elif 'OSVC_COMP_'+s in os.environ:
                _v = os.environ['OSVC_COMP_'+s]
            else:
                print >>sys.stderr, s, 'is not an env variable'
                raise NotApplicable()
            v = v.replace(m, _v)
        return v

    def sanitize(self, ak):
        if 'user' not in ak:
            print >>sys.stderr, "no user set in rule"
            return False
        if 'key' not in ak:
            print >>sys.stderr, "no key set in rule"
            return False
        if 'action' not in ak:
            ak['action'] = 'add'
        if 'authfile' not in ak:
            ak['authfile'] = self.authfile
        if ak['authfile'] not in ("authorized_keys", "authorized_keys2"):
            print >>sys.stderr, "unsupported authfile:", ak['authfile'], "(default to", self.authfile+")"
            ak['authfile'] = self.authfile
        for key in ('user', 'key', 'action', 'authfile'):
            ak[key] = ak[key].strip()
        return ak

    def fixable(self):
        return RET_NA

    def truncate_key(self, key):
        if len(key) < 50:
            s = key
        else:
            s = "'"+key[0:17] + "..." + key[-30:]+"'"
        return s.encode('utf8')

    def _get_authkey_file(self, key):
        if key == "authorized_keys":
            # default
            return ".ssh/authorized_keys"
        elif key == "authorized_keys2":
            key = "AuthorizedKeysFile"
        else:
            print >>sys.stderr, "unknown key", key
            return None

        cfs = []

        cmd = ['ps', '-eo', 'comm']
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode == 0:
            l = out.split('\n')
            if '/usr/local/sbin/sshd' in l:
                cfs.append(os.path.join(os.sep, 'usr', 'local', 'etc', 'sshd_config'))
            if '/usr/sfw/sbin/sshd' in l:
                cfs.append(os.path.join(os.sep, 'etc', 'sshd_config'))

        cfs += [os.path.join(os.sep, 'etc', 'ssh', 'sshd_config'),
                os.path.join(os.sep, 'opt', 'etc', 'sshd_config'),
                os.path.join(os.sep, 'etc', 'opt', 'ssh', 'sshd_config'),
                os.path.join(os.sep, 'usr', 'local', 'etc', 'sshd_config')]
        cf = None
        for _cf in cfs:
            if os.path.exists(_cf):
                cf = _cf
                break
        if cf is None:
            print >>sys.stderr, "sshd_config not found"
            return None
        with open(cf, 'r') as f:
            buff = f.read()
        for line in buff.split('\n'):
            l = line.split()
            if len(l) != 2:
                continue
            if l[0].strip() == key:
                return l[1]
        # not found, return default
        return ".ssh/authorized_keys2"

    def get_authkey_file(self, key, user):
        p = self._get_authkey_file(key)
        if p is None:
            return None
        p = p.replace('%u', user)
        p = p.replace('%h', os.path.expanduser('~'+user))
        if not p.startswith('/'):
            p = os.path.join(os.path.expanduser('~'+user), p)
        return p

    def get_authkey_files(self, user):
        l = []
        p = self.get_authkey_file('authorized_keys', user)
        if p is not None:
            l.append(p)
        p = self.get_authkey_file('authorized_keys2', user)
        if p is not None:
            l.append(p)
        return l

    def get_installed_keys(self, user):
        if user in self.installed_keys_d:
            return self.installed_keys_d[user]
        else:
            self.installed_keys_d[user] = []

        ps = self.get_authkey_files(user)
        for p in ps:
            if not os.path.exists(p):
                continue
            with codecs.open(p, 'r', encoding="utf8", errors="ignore") as f:
                self.installed_keys_d[user] += f.read().split('\n')
        return self.installed_keys_d[user]

    def check_authkey(self, ak, verbose=True):
        ak = self.sanitize(ak)
        installed_keys = self.get_installed_keys(ak['user'])
        if ak['action'] == 'add':
            if ak['key'] not in installed_keys:
                if verbose:
                    print >>sys.stderr, 'key', self.truncate_key(ak['key']), 'must be installed for user', ak['user']
                r = RET_ERR
            else:
                if verbose:
                    print 'key', self.truncate_key(ak['key']), 'is correctly installed for user', ak['user']
                r = RET_OK
        elif ak['action'] == 'del':
            if ak['key'] in installed_keys:
                if verbose:
                    print >>sys.stderr, 'key', self.truncate_key(ak['key']), 'must be uninstalled for user', ak['user']
                r = RET_ERR
            else:
                if verbose:
                    print 'key', self.truncate_key(ak['key']), 'is correctly not installed for user', ak['user']
                r = RET_OK
        else:
            print >>sys.stderr, "unsupported action:", ak['action']
            return RET_ERR
        return r

    def fix_authkey(self, ak):
        ak = self.sanitize(ak)
        if ak['action'] == 'add':
            return self.add_authkey(ak)
        elif ak['action'] == 'del':
            return self.del_authkey(ak)
        else:
            print >>sys.stderr, "unsupported action:", ak['action']
            return RET_ERR

    def add_authkey(self, ak):
        if self.check_authkey(ak, verbose=False) == RET_OK:
            return RET_OK

        try:
            userinfo=pwd.getpwnam(ak['user'])
        except KeyError:
            print >>sys.stderr, 'user', ak['user'], 'does not exist'
            return RET_ERR

        p = self.get_authkey_file(ak['authfile'], ak['user'])
        if p is None:
            print >>sys.stderr, "could not determine", ak['authfile'], "location"
            return RET_ERR
        base = os.path.dirname(p)

        if not os.path.exists(base):
            os.makedirs(base, 0700)
            print base, "created"
            if p.startswith(os.path.expanduser('~'+ak['user'])):
                os.chown(base, userinfo.pw_uid, userinfo.pw_gid)
                print base, "ownership set to %d:%d"%(userinfo.pw_uid, userinfo.pw_gid)

        if not os.path.exists(p):
            with open(p, 'w') as f:
                f.write("")
                print p, "created"
                os.chmod(p, 0600)
                print p, "mode set to 0600"
                os.chown(p, userinfo.pw_uid, userinfo.pw_gid)
                print p, "ownetship set to %d:%d"%(userinfo.pw_uid, userinfo.pw_gid)

        with open(p, 'a') as f:
            f.write(ak['key'].encode('utf8'))
            if not ak['key'].endswith('\n'):
                f.write('\n')
            print 'key', self.truncate_key(ak['key']), 'installed for user', ak['user']

        return RET_OK

    def del_authkey(self, ak):
        if self.check_authkey(ak, verbose=False) == RET_OK:
            print 'key', self.truncate_key(ak['key']), 'is already not installed for user', ak['user']
            return RET_OK

        ps = self.get_authkey_files(ak['user'])

        for p in ps:
            base = os.path.basename(p)
            if not os.path.exists(p):
                continue

            with codecs.open(p, 'r', encoding="utf8", errors="ignore") as f:
                l = f.read().split('\n')

            n = len(l)
            while True:
                try:
                    l.remove(ak['key'].replace('\n', ''))
                except ValueError:
                    break
            if len(l) == n:
                # nothing changed
                continue

            with open(p, 'w') as f:
                f.write('\n'.join(l).encode('utf8'))
                print 'key', self.truncate_key(ak['key']), 'uninstalled for user', ak['user']

        return RET_OK

    def check(self):
        r = 0
        for ak in self.authkeys:
            r |= self.check_authkey(ak)
        return r

    def fix(self):
        r = 0
        for ak in self.authkeys:
            r |= self.fix_authkey(ak)
        return r

if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = CompAuthKeys(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

  0707010000bcd8000081ed00000000000000000000000154fc7bfb00000967000000660000000300000000000000000000003400000000root/opt/opensvc/var/compliance/com.opensvc/bios.py   #!/usr/bin/env /opt/opensvc/bin/python

import os
import sys
from subprocess import *

sys.path.append(os.path.dirname(__file__))

from comp import *

class CompBios(object):
    def __init__(self, target):
        self.target = target
        self.sysname, self.nodename, x, x, self.machine = os.uname()

        if self.sysname not in ['Linux']:
            print >>sys.stderr, 'module not supported on', self.sysname
            raise NotApplicable()

    def get_bios_version_Linux(self):
        p = os.path.join(os.sep, 'sys', 'class', 'dmi', 'id', 'bios_version')
        try:
            f = open(p, 'r')
            ver = f.read().strip()
            f.close()
            return ver
        except:
            pass

        try:
            cmd = ['dmidecode', '-t', 'bios']
            p = Popen(cmd, stdout=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                raise
            for line in out.split('\n'):
                if 'Version:' in line:
                    return line.split(':')[-1].strip()
            raise
        except:
            print >>sys.stderr, 'can not fetch bios version'
            return None
        return ver

    def fixable(self):
        return RET_NA

    def check(self):
        ver = self.get_bios_version_Linux()
        if ver is None:
            return RET_NA
        if ver == self.target:
            print "bios version is %s, on target"%ver
            return RET_OK
        print >>sys.stderr, "bios version is %s, target %s"%(ver, self.target)
        return RET_ERR

    def fix(self):
        return RET_NA

if __name__ == "__main__":
    syntax = """syntax:
      %s TARGET check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = CompBios(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

 0707010000bcd9000081ed00000000000000000000000154fc7bfb00000dec000000660000000300000000000000000000003900000000root/opt/opensvc/var/compliance/com.opensvc/chkconfig.py  from subprocess import *
import sys
import os

os.environ['LANG'] = 'C'

class InitError(Exception):
    pass

class UnknownService(Exception):
    pass

class SetError(Exception):
    pass

class Chkconfig(object):
    def __init__(self):
        self.load()

    def __str__(self):
        s = ""
        for svc in self.services:
            s += "%-20s %s\n"%(svc, ' '.join(map(lambda x: '%-4s'%x,  self.services[svc])))
        return s

    def load(self):
        self.services = {}

        p = Popen(['/sbin/chkconfig', '--list'], stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            raise InitError()
        for line in out.split('\n'):
            words = line.split()
            if len(words) != 8:
                continue
            self.services[words[0]] = []
            for w in words[1:]:
                level, state = w.split(':')
                self.services[words[0]].append(state)

    def load_one(self, service):
        p = Popen(['/sbin/chkconfig', '--list', service], stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            if 'not referenced' in out:
                self.services[service] = ['off', 'off', 'off', 'off', 'off', 'off']
                return
            raise InitError()

    def activate(self, service):
        p = Popen(['chkconfig', service, 'on'], stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            raise SetError()

    def set_state(self, service, level, state):
        curstate = self.get_state(service, level)
        if curstate == state:
            return
        p = Popen(['chkconfig', '--level', level, service, state], stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            raise SetError()

    def get_state(self, service, level):
        if service not in self.services:
            try:
                self.load_one(service)
            except InitError:
                pass

        if service not in self.services:
            raise UnknownService()

        return self.services[service][level]

    def check_state(self, service, levels, state, seq=None, verbose=False):
        r = 0
        for level in levels:
            try:
                level = int(level)
            except:
                continue
            try:
                curstate = self.get_state(service, level)
            except UnknownService:
                if verbose:
                    print >>sys.stderr, "can not get service", service, "runlevels"
                return 1
            if curstate != state:
                if verbose:
                    print >>sys.stderr, "service", service, "at runlevel", level, "is in state", curstate, "! target state is", state
                r |= 1
            else:
                if verbose:
                    print "service", service, "at runlevel", level, "is in state", curstate
        return r
            
    def fix_state(self, service, levels, state, seq=None):
        cmd = ['chkconfig', '--level', levels, service, state]
        print "exec:", ' '.join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, "failed to set", service, "runlevels"
            print out
            print >>sys.stderr, err
            return 1
        return 0

if __name__ == "__main__":
    o = Chkconfig()
    print o
    print 'xfs@rc3 =', o.get_state('xfs', 3)
0707010000bcda000081a400000000000000000000000154fc7bfb000004b4000000660000000300000000000000000000003400000000root/opt/opensvc/var/compliance/com.opensvc/comp.py   #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

RET_OK = 0
RET_ERR = 1
RET_NA = 2

RET = RET_OK

class NotApplicable(Exception):
     def __init__(self, s=""):
         self.s = s
     def __str__(self):
         return self.s

class Unfixable(Exception):
     def __init__(self, s=""):
         self.s = s
     def __str__(self):
         return self.s

class ComplianceError(Exception):
     def __init__(self, s=""):
         self.s = s
     def __str__(self):
         return self.s

0707010000bcdb000081ed00000000000000000000000154fc7bfb00001d52000000660000000300000000000000000000003400000000root/opt/opensvc/var/compliance/com.opensvc/cron.py   #!/usr/bin/env /opt/opensvc/bin/python

import os
import sys
import shutil
import glob
from subprocess import *

sys.path.append(os.path.dirname(__file__))

from comp import *

class CompCron(object):
    def __init__(self, prefix='OSVC_COMP_CRON_ENTRY_'):
        self.prefix = prefix.upper()
        self.ce = []
        self.sysname, self.nodename, x, x, self.machine = os.uname()

        if self.sysname == 'SunOS' :
            self.crontab_locs = [
                '/var/spool/cron/crontabs'
            ]
        else:
            self.crontab_locs = [
                '/etc/cron.d',
                '/var/spool/cron/crontabs',
                '/var/spool/cron',
                '/var/cron/tabs',
            ]

        for k in os.environ:
            if k[:len(prefix)] == prefix:
                e = os.environ[k].split(':')
                if len(e) < 5:
                    print >>sys.stderr, "malformed variable %s. format: action:user:sched:cmd:[file]"%k
                    continue
                if e[0] not in ('add', 'del'):
                    print >>sys.stderr, "unsupported action in variable %s. set 'add' or 'del'"%k
                    continue
                if len(e[2].split()) != 5:
                    print >>sys.stderr, "malformed schedule in variable %s"%k
                    continue
                self.ce += [{
                        'var': k,
                        'action': e[0],
                        'user': e[1],
                        'sched': e[2],
                        'cmd': e[3],
                        'file': e[4],
                       }]

        if len(self.ce) == 0:
            raise NotApplicable()


    def activate_cron(self, cron_file):
        """ Activate changes (actually only needed on HP-UX)
        """
        if '/var/spool/' in cron_file:
            print "tell crond about the change"
            cmd = ['crontab', cron_file]
            process = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
            buff = process.communicate()

    def fixable(self):
        r = RET_OK
        for e in self.ce:
            try:
                self._fixable_cron(e)
            except ComplianceError, e:
                print >>sys.stderr, str(e)
                r = RET_ERR
            except Unfixable, e:
                print >>sys.stderr, str(e)
                return r
        return r

    def fix(self):
        r = RET_OK
        for e in self.ce:
            try:
                if e['action'] == 'add':
                    self._add_cron(e)
                elif e['action'] == 'del':
                    self._del_cron(e)
            except ComplianceError, e:
                print >>sys.stderr, str(e)
                r = RET_ERR
            except Unfixable, e:
                print >>sys.stderr, str(e)
                return r
        return r

    def check(self):
        r = RET_OK
        for e in self.ce:
            try:
                self._check_cron(e)
            except ComplianceError, e:
                print >>sys.stderr, str(e)
                r = RET_ERR
            except Unfixable, e:
                print >>sys.stderr, str(e)
                return r
        return r

    def get_cron_file(self, e):
        """ order of preference
        """
        cron_file = None
        for loc in self.crontab_locs:
            if not os.path.exists(loc):
                continue
            if loc == '/etc/cron.d':
                 cron_file = os.path.join(loc, e['file'])
            else:
                 cron_file = os.path.join(loc, e['user'])
            break
        return cron_file

    def format_entry(self, cron_file, e):
        if 'cron.d' in cron_file:
            s = ' '.join([e['sched'], e['user'], e['cmd']])
        else:
            s = ' '.join([e['sched'], e['cmd']])
        return s

    def _fixable_cron(self, e):
        cron_file = self.get_cron_file(e)

        if cron_file is None:
            raise Unfixable("no crontab usual location found (%s)"%str(self.crontab_locs))

    def _check_cron(self, e):
        cron_file = self.get_cron_file(e)

        if cron_file is None:
            raise Unfixable("no crontab usual location found (%s)"%str(self.crontab_locs))

        s = self.format_entry(cron_file, e)

        if not os.path.exists(cron_file):
            raise ComplianceError("cron entry not found '%s' in '%s'"%(s, cron_file))

        with open(cron_file, 'r') as f:
            new = f.readlines()
            found = False
            for line in new:
                if s == line[:-1]:
                     found = True
                     break
            if not found and e['action'] == 'add':
                raise ComplianceError("wanted cron entry not found: '%s' in '%s'"%(s, cron_file))
            if found and e['action'] == 'del':
                raise ComplianceError("unwanted cron entry found: '%s' in '%s'"%(s, cron_file))

    def _del_cron(self, e):
        cron_file = self.get_cron_file(e)

        if cron_file is None:
            raise Unfixable("no crontab usual location found (%s)"%str(self.crontab_locs))

        s = self.format_entry(cron_file, e)

        if not os.path.exists(cron_file):
            return

        new = []
        with open(cron_file, 'r') as f:
            lines = f.readlines()
            for line in lines:
                if s == line[:-1]:
                    print "delete entry '%s' from '%s'"%(s, cron_file)
                    continue
                new.append(line)

        if len(new) == 0:
            print 'deleted last entry of %s. delete file too.'%cron_file
            os.unlink(cron_file)
        else:
            with open(cron_file, 'w') as f:
                f.write(''.join(new))
            self.activate_cron(cron_file)

    def _add_cron(self, e):
        cron_file = self.get_cron_file(e)

        if cron_file is None:
            raise Unfixable("no crontab usual location found (%s)"%str(self.crontab_locs))

        s = self.format_entry(cron_file, e)

        new = False
        if os.path.exists(cron_file):
            with open(cron_file, 'r') as f:
                new = f.readlines()
                found = False
                for line in new:
                    if s == line[:-1]:
                        found = True
                        break
                if not found:
                    new.append(s+'\n')
        else:
            new = [s+'\n']

        if not new:
            raise ComplianceError("problem preparing the new crontab '%s'"%cron_file)

        print "add entry '%s' to '%s'"%(s, cron_file)
        with open(cron_file, 'w') as f:
            f.write(''.join(new))
        self.activate_cron(cron_file)

if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = CompCron(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

  0707010000bcdc000081ed00000000000000000000000154fc7bfb00001c3e000000660000000300000000000000000000003900000000root/opt/opensvc/var/compliance/com.opensvc/etcsystem.py  #!/usr/bin/env /opt/opensvc/bin/python

"""
OSVC_COMP_ETCSYSTEM='[{"key": "fcp:fcp_offline_delay", "op": ">=", "value": 21}, {"key": "ssd:ssd_io_time", "op": "=", "value": "0x3C"}]' ./etcsystem.py OSVC_COMP_ETCSYSTEM check
"""

import os
import sys
import json
from subprocess import *

sys.path.append(os.path.dirname(__file__))

from comp import *

class EtcSystem(object):
    def __init__(self, prefix='OSVC_COMP_FILELINE_'):
        self.prefix = prefix.upper()
        self.data = {}

        self.cf = os.path.join(os.sep, 'etc', 'system')
        self.keys = []
        self.load_file(self.cf)

        for k in [ key for key in os.environ if key.startswith(self.prefix)]:
            try:
                self.keys += json.loads(os.environ[k])
            except ValueError:
                print >>sys.stderr, 'key syntax error on var[', k, '] = ',os.environ[k]

        if len(self.keys) == 0:
            raise NotApplicable()

    def fixable(self):
        return RET_OK

    def load_file(self, p):
        if not os.path.exists(p):
            print >>sys.stderr, p, "does not exist"
            return
        with open(p, 'r') as f:
            buff = f.read()
        self.lines = buff.split('\n')
        for i, line in enumerate(self.lines):
            line = line.strip()
            if line.startswith('*'):
                continue
            if len(line) == 0:
                continue
            l = line.split()
            if l[0] != "set":
                continue
            if len(l) < 2:
                continue
            line = ' '.join(l[1:]).split('*')[0]
            var, val = line.split('=')
            var = var.strip()
            val = val.strip()
            try:
                val = int(val)
            except:
                pass
            if var in self.data:
                self.data[var].append([val, i])
            else:
                self.data[var] = [[val, i]]

    def set_val(self, keyname, target, op):
        newline = 'set %s = %s'%(keyname, str(target))
        if keyname not in self.data:
            print "add '%s' to /etc/system"%newline
            self.lines.insert(-1, newline + " * added by opensvc")
        else:
            ok = 0
            for value, ref in self.data[keyname]:
                r = self._check_key(keyname, target, op, value, ref, verbose=False)
                if r == RET_ERR:
                    print "comment out line %d: %s"%(ref, self.lines[ref])
                    self.lines[ref] = '* '+self.lines[ref]+' * commented out by opensvc'
                else:
                    ok += 1
            if ok == 0:
                print "add '%s' to /etc/system"%newline
                self.lines.insert(-1, newline + " * added by opensvc")

    def get_val(self, keyname):
        if keyname not in self.data:
            return []
        return self.data[keyname]

    def _check_key(self, keyname, target, op, value, ref, verbose=True):
        r = RET_OK
        if value is None:
            if verbose:
                print >>sys.stderr, "%s not set"%keyname
            r |= RET_ERR
        if op == '=':
            if str(value) != str(target):
                if verbose:
                    print >>sys.stderr, "%s=%s, target: %s"%(keyname, str(value), str(target))
                r |= RET_ERR
            elif verbose:
                print "%s=%s on target"%(keyname, str(value))
        else:
            if type(value) != int:
                if verbose:
                    print >>sys.stderr, "%s=%s value must be integer"%(keyname, str(value))
                r |= RET_ERR
            elif op == '<=' and value > target:
                if verbose:
                    print >>sys.stderr, "%s=%s target: <= %s"%(keyname, str(value), str(target))
                r |= RET_ERR
            elif op == '>=' and value < target:
                if verbose:
                    print >>sys.stderr, "%s=%s target: >= %s"%(keyname, str(value), str(target))
                r |= RET_ERR
            elif verbose:
                print "%s=%s on target"%(keyname, str(value))
        return r

    def check_key(self, key, verbose=True):
        if 'key' not in key:
            if verbose:
                print >>sys.stderr, "'key' not set in rule %s"%str(key)
            return RET_NA
        if 'value' not in key:
            if verbose:
                print >>sys.stderr, "'value' not set in rule %s"%str(key)
            return RET_NA
        if 'op' not in key:
            op = "="
        else:
            op = key['op']
        target = key['value']

        if op not in ('>=', '<=', '='):
            if verbose:
                print >>sys.stderr, "'value' list member 0 must be either '=', '>=' or '<=': %s"%str(key)
            return RET_NA

        keyname = key['key']
        data = self.get_val(keyname)

        if len(data) == 0:
            print >>sys.stderr, "%s key is not set"%keyname
            return RET_ERR

        r = RET_OK
        ok = 0
        for value, ref in data:
            r |= self._check_key(keyname, target, op, value, ref, verbose)
            if r == RET_OK:
                ok += 1

        if ok > 1:
            print >>sys.stderr, "duplicate lines for key %s"%keyname
            r |= RET_ERR
        return r

    def fix_key(self, key):
        self.set_val(key['key'], key['value'], key['op'])

    def check(self):
        r = 0
        for key in self.keys:
            r |= self.check_key(key, verbose=True)
        return r

    def fix(self):
        for key in self.keys:
            if self.check_key(key, verbose=False) == RET_ERR:
                self.fix_key(key)
        if len(self.keys) > 0:
            import datetime
            backup = self.cf+str(datetime.datetime.now())
            try:
                import shutil
                shutil.copy(self.cf, backup)
            except:
                print >>sys.stderr, "failed to backup %s"%self.cf
                return RET_ERR
            try:
                with open(self.cf, 'w') as f:
                    f.write('\n'.join(self.lines))
            except:
                print >>sys.stderr, "failed to write %s"%self.cf
                return RET_ERR
        return RET_OK

if __name__ == "__main__":
    """ test: OSVC_COMP_TEST_ETCSYSTEM_1='[{"value": "0x3C", "key": "ssd:ssd_io_time", "op": "="}, {"value": 21, "key": "ssd:ssd_max_throttle", "op": "="}]' ./etcsystem.py OSVC_COMP_TEST check
    """
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = EtcSystem(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

  0707010000bcdd000081ed00000000000000000000000154fc7bfb000026f9000000660000000300000000000000000000003700000000root/opt/opensvc/var/compliance/com.opensvc/fileinc.py    #!/usr/bin/env /opt/opensvc/bin/python
""" 
Verify file content. The collector provides the format with
wildcards. The module replace the wildcards with contextual
values.

The variable format is json-serialized:

{
  "path": "/some/path/to/file",
  "check": "some pattern into the file",
  "fmt": "full added content with %%HOSTNAME%%@corp.com",
 - or - 
  "ref": "http:// ..."
}

Wildcards:
%%ENV:VARNAME%%		Any environment variable value
%%HOSTNAME%%		Hostname
%%SHORT_HOSTNAME%%	Short hostname

"""

import os
import sys
import json
import stat
import re
import urllib
import tempfile
import codecs

sys.path.append(os.path.dirname(__file__))

from comp import *

MAXSZ = 8*1024*1024

class FileInc(object):
    def __init__(self, prefix='OSVC_COMP_FILEINC_'):
        self.prefix = prefix.upper()
        self.files = {}
        self.ok = {}
        self.checks = []
        self.upds = {}

        self.sysname, self.nodename, x, x, self.machine = os.uname()

        for k in [ key for key in os.environ if key.startswith(self.prefix)]:
            try:
                self.add_file(os.environ[k])
            except ValueError:
                print >>sys.stderr, 'key syntax error on var[', k, '] = ',os.environ[k]

        if len(self.checks) == 0:
            raise NotApplicable()

    def fixable(self):
        return RET_NA

    def parse_fmt(self, x):
        if isinstance(x, int):
            x = str(x)
        x = x.replace('%%HOSTNAME%%', self.nodename)
        x = x.replace('%%SHORT_HOSTNAME%%', self.nodename.split('.')[0])
        return x

    def parse_ref(self, url):
        f = tempfile.NamedTemporaryFile()
        tmpf = f.name
        try:
            fname, headers = urllib.urlretrieve(url, tmpf)
            if 'invalid file' in headers.values():
                print >>sys.stderr, url, "not found on collector"
                return RET_ERR
            content = unicode(f.read())
            f.close()
        except:
             print >>sys.stderr, url, "not found on collector"
             return ''
        if '<title>404 Not Found</title>' in content:
            print >>sys.stderr, url, "not found on collector"
            return ''
        return self.parse_fmt(content)

    def subst(self, v):
        if type(v) == list:
            l = []
            for _v in v:
                l.append(self.subst(_v))
            return l
        if type(v) != str and type(v) != unicode:
            return v
        p = re.compile('%%ENV:\w+%%')
        for m in p.findall(v):
            s = m.strip("%").replace('ENV:', '')
            if s in os.environ:
                _v = os.environ[s]
            elif 'OSVC_COMP_'+s in os.environ:
                _v = os.environ['OSVC_COMP_'+s]
            else:
                print >>sys.stderr, s, 'is not an env variable'
                raise NotApplicable()
            v = v.replace(m, _v)
        return v

    def read_file(self, path):
        if not os.path.exists(path):
            return ''
        out = ''
        try :
            f = codecs.open(path, 'r', encoding="utf8", errors="ignore")
            out = f.read().rstrip('\n')
            f.close()
        except IOError as (errno, strerror):
            print "cannot read '%s', error=%d - %s" %(path, errno, strerror)
            raise
        except:
            print >>sys.stderr, "Cannot open '%s', unexpected error: %s"%(path, sys.exc_info()[0])
            raise
        return out

    def add_file(self,v):
        r = RET_OK
        d = json.loads(v)
        if 'path' not in d:
            print >>sys.stderr, "'path' should be defined:", d
            r |= RET_ERR
        if 'fmt' in d and 'ref' in d:
            print >>sys.stderr, "'fmt' and 'ref' are exclusive:", d
            r |= RET_ERR
        for k in ('path', 'check', 'fmt', 'ref'):
            if k in d:
                d[k] = self.subst(d[k])
        if 'path' in d:
            d['path'] = d['path'].strip()
        if 'ref' in d:
            d['ref'] = d['ref'].strip()
        if not d['path'] in self.upds:
            self.upds[d['path']] = 0
        if not d['path'] in self.files:
            try:
                fsz = os.path.getsize(d['path'])
            except:
                fsz = 0
            if fsz > MAXSZ:
                self.ok[d['path']] = 0
                self.files[d['path']] = ''
                print >>sys.stderr, "file '%s' is too large [%.2f Mb] to fit" %(d['path'], fsz/(1024.*1024))
                r |= RET_ERR
            else:
                try:
                    self.files[d['path']] = self.read_file(d['path'])
                    self.ok[d['path']] = 1
                except:
                    self.files[d['path']] = ""
                    self.ok[d['path']] = 0
                    r |= RET_ERR
        c = ''
        if 'fmt' in d:
            c = self.parse_fmt(d['fmt'])
        elif 'ref' in d:
            c = self.parse_ref(d['ref'])
        else:
            print >>sys.stderr, "'fmt' or 'ref' should be defined:", d
            r |= RET_ERR
        c = c.strip()
        if re.match(d['check'], c) is not None or len(c) == 0:
            val = True
        else:
            val = False
            r |= RET_ERR
        self.checks.append({'check':d['check'], 'path':d['path'], 'add':c, 'valid':val})
        return r
        
    def check(self):
        r = RET_OK
        for ck in self.checks:
            if not ck['valid']:
                print >>sys.stderr, "rule error: '%s' does not match target content" % ck['check']
                r |= RET_ERR
                continue
            if self.ok[ck['path']] != 1:
                r |= RET_ERR
                continue
            pr = RET_OK
            m = 0
            ok = 0
            lines = self.files[ck['path']].split('\n')
            for line in lines:
                if re.match(ck['check'], line):
                    m += 1
                    if len(ck['add']) > 0 and line == ck['add']:
                        print "line '%s' found in '%s'" %(line, ck['path'])
                        ok += 1
                    if m > 1:
                        print >>sys.stderr, "duplicate match of pattern '%s' in '%s'"%(ck['check'], ck['path'])
                        pr |= RET_ERR
            if len(ck['add']) == 0:
                if m > 0:
                    print >>sys.stderr, "pattern '%s' found in %s"%(ck['check'], ck['path'])
                    pr |= RET_ERR
                else:
                    print "pattern '%s' not found in %s"%(ck['check'], ck['path'])
            elif ok == 0:
                print >>sys.stderr, "line '%s' not found in %s"%(ck['add'], ck['path'])
                pr |= RET_ERR
            elif m == 0:
                print >>sys.stderr, "pattern '%s' not found in %s"%(ck['check'], ck['path'])
                pr |= RET_ERR
            r |= pr
        return r

    def rewrite_files(self):
        r = RET_OK
        for path in self.files:
            if self.upds[path] == 0:
                continue
            if self.ok[path] != 1:
                r |= RET_ERR
                continue
            if not os.path.exists(path):
                print >>sys.stderr, "'%s' will be created, please check owner and permissions" %path
            try:
                f = codecs.open(path, 'w', encoding="utf8")
                f.write(self.files[path])
                f.close()
                print "'%s' successfully rewritten" %path
            except:
                print >>sys.stderr, "failed to rewrite '%s'" %path
                r |= RET_ERR
        return r

    def fix(self):
        r = RET_OK
        for ck in self.checks:
            if not ck['valid']:
                print >>sys.stderr, "rule error: '%s' does not match target content" % ck['check']
                r |= RET_ERR
                continue
            if self.ok[ck['path']] != 1:
                r |= RET_ERR
                continue
            need_rewrite = False
            m = 0
            lines = self.files[ck['path']].rstrip('\n').split('\n')
            for i, line in enumerate(lines):
                if re.match(ck['check'], line):
                    m += 1
                    if m == 1:
                        if line != ck['add']:
                            # rewrite line
                            print "rewrite %s:%d:'%s', new content: '%s'" %(ck['path'], i, line, ck['add'])
                            lines[i] = ck['add']
                            need_rewrite = True
                    elif m > 1:
                        # purge dup
                        print "remove duplicate line %s:%d:'%s'" %(ck['path'], i, line)
                        lines[i] = ""
                        need_rewrite = True
            if m == 0 and len(ck['add']) > 0:
                print "add line '%s' to %s"%(ck['add'], ck['path'])
                lines.append(ck['add'])
                need_rewrite = True

            if need_rewrite:
                self.files[ck['path']] = '\n'.join(lines).rstrip("\n")+"\n"
                self.upds[ck['path']] = 1

        r |= self.rewrite_files()
        return r


if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = FileInc(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except ComplianceError:
        sys.exit(RET_ERR)
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

   0707010000bcde000081ed00000000000000000000000154fc7bfb00002ddc000000660000000300000000000000000000003500000000root/opt/opensvc/var/compliance/com.opensvc/files.py  #!/usr/bin/env /opt/opensvc/bin/python
""" 
Verify file content. The collector provides the format with
wildcards. The module replace the wildcards with contextual
values.

The variable format is json-serialized:

{
  "path": "/some/path/to/file",
  "fmt": "root@corp.com		%%HOSTNAME%%@corp.com",
  "uid": 500,
  "gid": 500,
}

Wildcards:
%%ENV:VARNAME%%		Any environment variable value
%%HOSTNAME%%		Hostname
%%SHORT_HOSTNAME%%	Short hostname

"""

import os
import sys
import json
import stat
import re
import urllib
import ssl
import tempfile
import pwd
import grp
from subprocess import *

sys.path.append(os.path.dirname(__file__))

from comp import *

class InitError(Exception):
    pass

class CompFiles(object):
    def __init__(self, prefix='OSVC_COMP_FILES_'):
        self.prefix = prefix.upper()
        self._usr = {}
        self._grp = {}
        self.sysname, self.nodename, x, x, self.machine = os.uname()
        self.files = []

        if "OSVC_COMP_SERVICES_SVC_NAME" not in os.environ:
            os.environ["OSVC_COMP_SERVICES_SVC_NAME"] = ""

        for k in [ key for key in os.environ if key.startswith(self.prefix)]:
            try:
                self.files += self.add_file(os.environ[k])
            except InitError:
                continue
            except ValueError:
                print >>sys.stderr, 'failed to parse variable', os.environ[k]

        if len(self.files) == 0:
            raise NotApplicable()

    def subst(self, v):
        if type(v) == list:
            l = []
            for _v in v:
                l.append(self.subst(_v))
            return l
        if type(v) != str and type(v) != unicode:
            return v

        p = re.compile('%%ENV:\w+%%')
        for m in p.findall(v):
            s = m.strip("%").replace('ENV:', '')
            if s in os.environ:
                _v = os.environ[s]
            elif 'OSVC_COMP_'+s in os.environ:
                _v = os.environ['OSVC_COMP_'+s]
            else:
                print >>sys.stderr, s, 'is not an env variable'
                raise NotApplicable()
            v = v.replace(m, _v)
        return v

    def parse_fmt(self, d, add_linefeed=True):
        if isinstance(d['fmt'], int):
            d['fmt'] = str(d['fmt'])
        d['fmt'] = d['fmt'].replace('%%HOSTNAME%%', self.nodename)
        d['fmt'] = d['fmt'].replace('%%SHORT_HOSTNAME%%', self.nodename.split('.')[0])
        d['fmt'] = self.subst(d['fmt'])
        if add_linefeed and not d['fmt'].endswith('\n'):
            d['fmt'] += '\n'
        return [d]

    def parse_ref(self, d):
        f = tempfile.NamedTemporaryFile()
        tmpf = f.name
        try:
            if d['ref'].startswith("https"):
                try:
                    context = ssl._create_unverified_context()
                    fname, headers = urllib.urlretrieve(d['ref'], tmpf, context=context)
                except:
                    fname, headers = urllib.urlretrieve(d['ref'], tmpf)
            else:
                fname, headers = urllib.urlretrieve(d['ref'], tmpf)
        except IOError:
            import traceback
            e = sys.exc_info()
            print >>sys.stderr, "can not download", d['ref'], ":", e[1]
            raise InitError()
        if 'invalid file' in headers.values():
            print >>sys.stderr, d['ref'], "not found on collector"
            raise InitError()
        d['fmt'] = f.read()
        if '<title>404 Not Found</title>' in d['fmt']:
            print >>sys.stderr, url, "not found on collector"
            raise InitError()
        f.close()
        return self.parse_fmt(d, add_linefeed=False)

    def get_env_item(self, d, item):
        if item not in d:
            return d
        if type(d[item]) != str and type(d[item]) != unicode:
            return d
        if not d[item].startswith("%%"):
            return d
        s = d[item].strip('%').replace('ENV:', '')
        if not s.startswith('OSVC_COMP_'):
            s = 'OSVC_COMP_'+s
        if s not in os.environ:
            print >>sys.stderr, "%s is not set"%s
            raise
        d[item] = os.environ[s]
        try:
            d[item] = int(d[item])
        except:
            pass
        return d

    def add_file(self, v):
        d = json.loads(v)
        if 'path' not in d:
            print >>sys.stderr, 'path should be in the dict:', d
            RET = RET_ERR
            return []
        if 'fmt' not in d and 'ref' not in d and not d['path'].endswith("/"):
            print >>sys.stderr, 'fmt or ref should be in the dict:', d
            RET = RET_ERR
            return []
        if 'fmt' in d and 'ref' in d:
            print >>sys.stderr, 'fmt and ref are exclusive:', d
            RET = RET_ERR
            return []
        try:
            d = self.get_env_item(d, 'uid')
            d = self.get_env_item(d, 'gid')
        except:
            return []
        for k in ('ref', 'path', 'uid', 'gid', 'fmt'):
            if k in d:
                d[k] = self.subst(d[k])
        if 'fmt' in d:
            return self.parse_fmt(d)
        if 'ref' in d:
            return self.parse_ref(d)
        return [d]

    def fixable(self):
        return RET_NA

    def check_file_fmt(self, f, verbose=False):
        if not os.path.exists(f['path']):
            return RET_ERR
        if f['path'].endswith('/'):
            # don't check content if it's a directory
            return RET_OK
        tmpf = tempfile.NamedTemporaryFile()
        tmpfname = tmpf.name
        tmpf.close()
        with open(tmpfname, 'w') as tmpf:
            tmpf.write(f['fmt'])
        if "OSVC_COMP_NODES_OS_NAME" in os.environ and os.environ['OSVC_COMP_NODES_OS_NAME'] in ("Linux"):
            cmd = ['diff', '-u', f['path'], tmpfname]
        else:
            cmd = ['diff', f['path'], tmpfname]
        p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        os.unlink(tmpfname)
        if verbose and len(out) > 0:
            #print >>sys.stderr, " ".join(cmd)
            print >>sys.stderr, out.strip('\n')
        if p.returncode != 0:
            return RET_ERR
        return RET_OK

    def check_file_mode(self, f, verbose=False):
        if 'mode' not in f:
            return RET_OK
        try:
            mode = oct(stat.S_IMODE(os.stat(f['path']).st_mode))
        except:
            if verbose: print >>sys.stderr, f['path'], 'can not stat file'
            return RET_ERR
        mode = str(mode).lstrip("0")
        if mode != str(f['mode']):
            if verbose: print >>sys.stderr, f['path'], 'mode should be %s but is %s'%(f['mode'], mode)
            return RET_ERR
        return RET_OK

    def get_uid(self, uid):
        if uid in self._usr:
            return self._usr[uid]
        tuid = uid
        if isinstance(uid, (str, unicode)):
            try:
                info=pwd.getpwnam(uid)
                tuid = info[2]
                self._usr[uid] = tuid
            except:
                print >>sys.stderr, "user %s does not exist"%uid
                raise ComplianceError()
        return tuid

    def get_gid(self, gid):
        if gid in self._grp:
            return self._grp[gid]
        tgid = gid
        if isinstance(gid, (str, unicode)):
            try:
                info=grp.getgrnam(gid)
                tgid = info[2]
                self._grp[gid] = tgid
            except:
                print >>sys.stderr, "group %s does not exist"%gid
                raise ComplianceError()
        return tgid

    def check_file_uid(self, f, verbose=False):
        if 'uid' not in f:
            return RET_OK
        tuid = self.get_uid(f['uid'])
        uid = os.stat(f['path']).st_uid
        if uid != tuid:
            if verbose: print >>sys.stderr, f['path'], 'uid should be %s but is %s'%(tuid, str(uid))
            return RET_ERR
        return RET_OK

    def check_file_gid(self, f, verbose=False):
        if 'gid' not in f:
            return RET_OK
        tgid = self.get_gid(f['gid'])
        gid = os.stat(f['path']).st_gid
        if gid != tgid:
            if verbose: print >>sys.stderr, f['path'], 'gid should be %s but is %s'%(tgid, str(gid))
            return RET_ERR
        return RET_OK

    def check_file(self, f, verbose=False):
        if not os.path.exists(f['path']):
            print >>sys.stderr, f['path'], "does not exist"
            return RET_ERR
        r = 0
        r |= self.check_file_fmt(f, verbose)
        r |= self.check_file_mode(f, verbose)
        r |= self.check_file_uid(f, verbose)
        r |= self.check_file_gid(f, verbose)
        if r == 0 and verbose:
            print f['path'], "is ok"
        return r

    def fix_file_mode(self, f):
        if 'mode' not in f:
            return RET_OK
        if self.check_file_mode(f) == RET_OK:
            return RET_OK
        try:
            print "%s mode set to %s"%(f['path'], str(f['mode']))
            os.chmod(f['path'], int(str(f['mode']), 8))
        except:
            return RET_ERR
        return RET_OK

    def fix_file_owner(self, f):
        uid = -1
        gid = -1

        if 'uid' not in f and 'gid' not in f:
            return RET_OK
        if 'uid' in f and self.check_file_uid(f) != RET_OK:
            uid = self.get_uid(f['uid'])
        if 'gid' in f and self.check_file_gid(f) != RET_OK:
            gid = self.get_gid(f['gid'])
        if uid == -1 and gid == -1:
            return RET_OK
        try:
            os.chown(f['path'], uid, gid)
        except:
            print >>sys.stderr, "failed to set %s ownership to %d:%d"%(f['path'], uid, gid)
            return RET_ERR
        print "%s ownership set to %d:%d"%(f['path'], uid, gid)
        return RET_OK

    def fix_file_fmt(self, f):
        if f['path'].endswith("/") and not os.path.exists(f['path']):
            try:
                os.makedirs(f['path'])
                print f['path'], "created"
            except:
                print >>sys.stderr, "failed to create", f['path']
                return RET_ERR
            return RET_OK
        if self.check_file_fmt(f) == RET_OK:
            return RET_OK
        d = os.path.dirname(f['path'])
        if not os.path.exists(d):
           os.makedirs(d)
           try:
               os.chown(d, f['uid'], f['gid'])
           except:
               pass
        try:
            with open(f['path'], 'w') as fi:
                fi.write(f['fmt'])
        except:
            return RET_ERR
        print f['path'], "rewritten"
        return RET_OK

    def check(self):
        r = 0
        for f in self.files:
            r |= self.check_file(f, verbose=True)
        return r

    def fix(self):
        r = 0
        for f in self.files:
            r |= self.fix_file_fmt(f)
            r |= self.fix_file_mode(f)
            r |= self.fix_file_owner(f)
        return r


if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = CompFiles(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except ComplianceError:
        sys.exit(RET_ERR)
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

0707010000bcdf000081ed00000000000000000000000154fc7bfb00001a6b000000660000000300000000000000000000003800000000root/opt/opensvc/var/compliance/com.opensvc/firmware.py   #!/usr/bin/env /opt/opensvc/bin/python

import os
import sys
import json
from distutils.version import LooseVersion as V
from subprocess import *

sys.path.append(os.path.dirname(__file__))

from comp import *

class CompFirmware(object):
    def __init__(self, var):
        self.versions = {}

        if var not in os.environ:
            print var, 'not found in environment'
            raise NotApplicable()

        try:
            self.target_versions = json.loads(os.environ[var])
        except:
            print >>sys.stderr, var, 'mis-formatted variable:', os.environ[var]
            raise NotApplicable()
        for key in self.target_versions:
            if type(self.target_versions[key]) != list:
                continue
            self.target_versions[key] = map(lambda x: str(x), self.target_versions[key])

        self.sysname, self.nodename, x, x, self.machine = os.uname()

        if self.sysname not in ['Linux']:
            print >>sys.stderr, 'module not supported on', self.sysname
            raise NotApplicable()

    def get_versions(self):
        self.get_bios_version_Linux()
        self.get_qla_version_Linux()
        self.get_lpfc_version_Linux()

    def get_qla_version_Linux(self):
        self.versions['qla2xxx'] = None
        self.versions['qla2xxx_fw'] = None
        import glob
        hosts = glob.glob('/sys/bus/pci/drivers/qla2*/*:*:*/host*')
        if len(hosts) == 0:
            return
        hosts_proc = map(lambda x: '/proc/scsi/qla2xxx/'+os.path.basename(x).replace('host', ''), hosts)
        hosts = map(lambda x: '/sys/class/fc_host/'+os.path.basename(x)+'/symbolic_name', hosts)
        for i, host in enumerate(hosts):
            if os.path.exists(host):
                with open(host, 'r') as f:
                    buff = f.read()
                l = buff.split()
                for e in l:
                    if e.startswith("DVR:"):
                        self.versions['qla2xxx'] = e.replace("DVR:", "")
                    elif e.startswith("FW:"):
                        v = e.replace("FW:", "")
                        # store the lowest firmware version
                        if self.versions['qla2xxx_fw'] is None or V(self.versions['qla2xxx_fw']) > V(v):
                            self.versions['qla2xxx_fw'] = v
            elif os.path.exists(hosts_proc[i]):
                with open(hosts_proc[i], 'r') as f:
                    buff = f.read()
                for line in buff.split('\n'):
                    if "Firmware version" not in line:
                        continue
                    l = line.split()
                    n_words = len(l)
                    idx = l.index("Driver") + 2
                    if idx <= n_words:
                        self.versions['qla2xxx'] = l[idx]
                    idx = l.index("Firmware") + 2
                    if idx <= n_words:
                        v = l[idx]
                        if self.versions['qla2xxx_fw'] is None or V(self.versions['qla2xxx_fw']) > V(v):
                            self.versions['qla2xxx_fw'] = v

    def get_lpfc_version_Linux(self):
        self.versions['lpfc'] = None
        self.versions['lpfc_fw'] = None
        import glob
        hosts = glob.glob('/sys/class/scsi_host/host*/fwrev')
        if len(hosts) == 0:
            return
        for host in hosts:
            with open(host, 'r') as f:
                buff = f.read()
            l = buff.split()
            if self.versions['lpfc_fw'] is None or V(self.versions['lpfc_fw']) > V(l[0]):
                self.versions['lpfc_fw'] = l[0]

        if self.versions['lpfc_fw'] is None:
            # no need to fetch module version if no hardware
            return

        cmd = ['modinfo', 'lpfc']
        p = Popen(cmd, stdout=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return
        for line in out.split('\n'):
            if line.startswith('version:'):
                self.versions['lpfc'] = line.split()[1]
                return

    def get_bios_version_Linux(self):
        p = os.path.join(os.sep, 'sys', 'class', 'dmi', 'id', 'bios_version')
        try:
            f = open(p, 'r')
            ver = f.read().strip()
            f.close()
            self.versions['server'] = ver
            return
        except:
            pass

        try:
            cmd = ['dmidecode']
            p = Popen(cmd, stdout=PIPE, stderr=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                raise
            for line in out.split('\n'):
                if 'Version:' in line:
                    self.versions['server'] = line.split(':')[-1].strip()
                    return
            raise
        except:
            print 'can not fetch bios version'
            return

    def fixable(self):
        return RET_NA

    def check(self):
        self.get_versions()
        r = RET_OK
        for key in self.target_versions:
            if key not in self.versions:
                print >>sys.stderr, "TODO: get", key, "version"
                continue
            if type(self.versions[key]) not in (str, unicode):
                print "no", key
                continue
            if type(self.target_versions[key]) == list and \
               self.versions[key] not in self.target_versions[key]:
                print >>sys.stderr, key, "version is %s, target %s"%(self.versions[key], ' or '.join(self.target_versions[key]))
                r |= RET_ERR
            elif type(self.target_versions[key]) != list and \
                 self.versions[key] != self.target_versions[key]:
                print >>sys.stderr, key, "version is %s, target %s"%(self.versions[key], self.target_versions[key])
                r |= RET_ERR
            else:
                print key, "version is %s, on target"%self.versions[key]
                continue
        return r

    def fix(self):
        return RET_NA

if __name__ == "__main__":
    syntax = """syntax:
      %s TARGET check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = CompFirmware(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

 0707010000bce0000081ed00000000000000000000000154fc7bfb0000577a000000660000000300000000000000000000003200000000root/opt/opensvc/var/compliance/com.opensvc/fs.py #!/usr/bin/env /opt/opensvc/bin/python
""" 
Verify file content. The collector provides the format with
wildcards. The module replace the wildcards with contextual
values.

The variable format is json-serialized:

[{
 "dev": "lv_applisogm",
 "size": "1024M",
 "mnt": "/%%ENV:SVCNAME%%/applis/ogm",
 "vg": ["%%ENV:SVCNAME%%", "vgAPPLIS", "vgCOMMUN01", "vgLOCAL"]
}]

Wildcards:
%%ENV:VARNAME%%		Any environment variable value

Toggle:
%%ENV:FS_STRIP_SVCNAME_FROM_DEV_IF_IN_VG%%

"""

import os
import sys
import json
import stat
import re
from subprocess import *
from stat import *

sys.path.append(os.path.dirname(__file__))

from comp import *
from utilities import which

class CompFs(object):
    def __init__(self, prefix='OSVC_COMP_FS_'):
        self.prefix = prefix.upper()
        self.sysname, self.nodename, x, x, self.machine = os.uname()
        self.sysname = self.sysname.replace('-', '')
        self.fs = []
        self.res = {}
        self.res_status = {}

        if 'OSVC_COMP_SERVICES_SVC_NAME' in os.environ:
            self.svcname = os.environ['OSVC_COMP_SERVICES_SVC_NAME']
            self.osvc_service = True
        else:
            os.environ['OSVC_COMP_SERVICES_SVC_NAME'] = ""
            self.svcname = None
            self.osvc_service = False

        keys = [key for key in os.environ if key.startswith(self.prefix)]
        if len(keys) == 0:
            raise NotApplicable()

        self.vglist()

        for k in keys:
            try:
                self.fs += self.add_fs(os.environ[k])
            except ValueError:
                print >>sys.stderr, 'failed to parse variable', os.environ[k]

        if len(self.fs) == 0:
            raise NotApplicable()

        self.fs.sort(lambda x, y: cmp(x['mnt'], y['mnt']))


    def vglist_HPUX(self):
        import glob
        l = glob.glob("/dev/*/group")
        l = map(lambda x: x.split('/')[2], l)
        self.vg = l

    def vglist_Linux(self):
        if not which("vgs"):
            print >>sys.stderr, 'vgs command not found'
            raise ComplianceError()
        cmd = ['vgs', '-o', 'vg_name', '--noheadings']
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, 'failed to list volume groups'
            raise ComplianceError()
        self.vg = out.split()

    def vglist(self):
        if not hasattr(self, 'vglist_'+self.sysname):
            print >>sys.stderr, self.sysname, 'not supported'
            raise NotApplicable()
        getattr(self, 'vglist_'+self.sysname)()
        
    def subst(self, v):
        if type(v) == list:
            l = []
            for _v in v:
                l.append(self.subst(_v))
            return l
        if type(v) != str and type(v) != unicode:
            return v

        p = re.compile('%%ENV:\w+%%')
        for m in p.findall(v):
            s = m.strip("%").replace('ENV:', '')
            if s in os.environ:
                _v = os.environ[s]
            elif 'OSVC_COMP_'+s in os.environ:
                _v = os.environ['OSVC_COMP_'+s]
            else:
                print >>sys.stderr, s, 'is not an env variable'
                raise NotApplicable()
            v = v.replace(m, _v)
        return v.strip()

    def add_fs(self, v):
        if type(v) == str or type(v) == unicode:
            d = json.loads(v)
        else:
            d = v
        l = []

        # recurse if multiple fs are specified in a list of dict
        if type(d) == list:
            for _d in d:
                l += self.add_fs(_d)
            return l

        if type(d) != dict:
            print >>sys.stderr, "not a dict:", d
            return l

        if 'dev' not in d:
            print >>sys.stderr, 'dev should be in the dict:', d
            return l
        if 'mnt' not in d:
            print >>sys.stderr, 'mnt should be in the dict:', d
            return l
        if 'size' not in d:
            print >>sys.stderr, 'size should be in the dict:', d
            return l
        if 'vg' not in d:
            print >>sys.stderr, 'vg should be in the dict:', d
            return l
        if 'type' not in d:
            print >>sys.stderr, 'type should be in the dict:', d
            return l
        if 'opts' not in d:
            print >>sys.stderr, 'opts should be in the dict:', d
            return l
        if type(d['vg']) != list:
            d['vg'] = [d['vg']]

        d['vg_orig'] = d['vg']
        d['vg'] = self.subst(d['vg'])
        d['prefvg'] = self.prefvg(d)
        d['dev'] = self.strip_svcname(d)

        for k in ('dev', 'mnt', 'size', 'type', 'opts'):
            d[k] = self.subst(d[k])

        d['mnt'] = self.normpath(d['mnt'])
        d['devpath'] = self.devpath(d)
        d['rdevpath'] = self.rdevpath(d)
        try:
            d['size'] = self.size_to_mb(d)
        except ComplianceError:
            return []

        return [d]

    def strip_svcname(self, fs):
        key = "OSVC_COMP_FS_STRIP_SVCNAME_FROM_DEV_IF_IN_VG"
        if key not in os.environ or os.environ[key] != "true":
            return fs['dev']
        if "%%ENV:SERVICES_SVC_NAME%%" not in fs['vg_orig'][fs['prefvg_idx']]:
            return fs['dev']

        # the vg is dedicated to the service. no need to embed
        # the service name in the lv name too
        s = fs['dev'].replace("%%ENV:SERVICES_SVC_NAME%%", "")
        if s == "lv_":
            s = "root"
        return s

    def normpath(self, p):
        l = p.split('/')
        p = os.path.normpath(os.path.join(os.sep, *l))
        return p

    def rdevpath(self, d):
        return '/dev/%s/r%s'%(d['prefvg'], d['dev'])

    def devpath(self, d):
        return '/dev/%s/%s'%(d['prefvg'], d['dev'])

    def prefvg(self, d):
        lc_candidate_vg = map(lambda x: x.lower(), d['vg'])
        lc_existing_vg = map(lambda x: x.lower(), self.vg)
        for i, vg in enumerate(lc_candidate_vg):
            if vg in lc_existing_vg:
                d['prefvg_idx'] = i
                # return capitalized vg name
                return self.vg[lc_existing_vg.index(vg)]
        print >>sys.stderr, "no candidate vg is available on this node for dev %s"%d['dev']
        raise NotApplicable()

    def check_fs_mnt(self, fs, verbose=False):
        if not os.path.exists(fs['mnt']):
            if verbose:
                print >>sys.stderr, "mount point", fs['mnt'], "does not exist"
            return 1
        if verbose:
            print "mount point", fs['mnt'], "exists"
        return 0

    def check_fs_dev_exists(self, fs, verbose=False):
        if not os.path.exists(fs['devpath']):
            if verbose:
                print >>sys.stderr, "device", fs['devpath'], "does not exist"
            return 1
        if verbose:
            print "device", fs['devpath'], "exists"
        return 0

    def check_fs_dev_stat(self, fs, verbose=False):
        mode = os.stat(fs['devpath'])[ST_MODE]
        if not S_ISBLK(mode):
            if verbose:
                print >>sys.stderr, "device", fs['devpath'], "is not a block device"
            return 1
        if verbose:
            print "device", fs['devpath'], "is a block device"
        return 0

    def find_vg_rid(self, vgname):
        rids = [ rid for rid in self.res_status.keys() if rid.startswith('vg#') ]
        for rid in rids:
            if self.get_res_item(rid, 'vgname') == vgname:
                return rid
        return None

    def private_svc_vg_down(self, fs):
        if self.svcname is None or not self.osvc_service:
            return False
        rid = self.find_vg_rid(fs['prefvg'])
        if rid is None:
            # vg is not driven by the service
            return False
        if self.res_status[rid] not in ('up', 'stdby up'):
            return False
        return True

    def check_fs_dev(self, fs, verbose=False):
        if self.private_svc_vg_down(fs):
            # don't report error on passive node with private svc prefvg
            return 0
        if self.check_fs_dev_exists(fs, verbose) == 1:
            return 1
        if self.check_fs_dev_stat(fs, verbose) == 1:
            return 1
        return 0

    def fix_fs_dev(self, fs):
        if self.check_fs_dev(fs, False) == 0:
            return 0
        if self.check_fs_dev_exists(fs, False) == 0:
            print >>sys.stderr, "device", fs['devpath'], "already exists. won't fix."
            return 1
        return self.createlv(fs)

    def createlv(self, fs):
        if not hasattr(self, 'createlv_'+self.sysname):
            print >>sys.stderr, self.sysname, 'not supported'
            raise NotApplicable()
        return getattr(self, 'createlv_'+self.sysname)(fs)

    def size_to_mb(self, fs):
        s = fs['size']
        unit = s[-1]
        size = int(s[:-1])
        if unit == 'T':
            s = str(size*1024*1024)
        elif unit == 'G':
            s = str(size*1024)
        elif unit == 'M':
            s = str(size)
        elif unit == 'K':
            s = str(size//1024)
        else:
            print >>sys.stderr, "unknown size unit in rule: %s (use T, G, M or K)"%s
            raise ComplianceError()
        return s

    def createlv_HPUX(self, fs):
        cmd = ['lvcreate', '-n', fs['dev'], '-L', fs['size'], fs['prefvg']]
        print ' '.join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if len(out) > 0:
            print out
        if len(err) > 0:
            print err
        if p.returncode != 0:
            return 1
        return 0

    def createlv_Linux(self, fs):
        os.environ["LVM_SUPPRESS_FD_WARNINGS"] = "1"
        cmd = ['lvcreate', '-n', fs['dev'], '-L', fs['size']+'M', fs['prefvg']]
        print ' '.join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if len(out) > 0:
            print out
        if len(err) > 0:
            print err
        if p.returncode != 0:
            return 1
        return 0

    def fix_fs_mnt(self, fs, verbose=False):
        if self.check_fs_mnt(fs, False) == 0:
            return 0
        print "create", fs['mnt'], "mount point"
        os.makedirs(fs['mnt'])
        return 0

    def check_fs_fmt_HPUX_vxfs(self, fs, verbose=False):
        cmd = ['fstyp', fs['devpath']]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0 or "vxfs" not in out:
            if verbose:
                print >>sys.stderr, fs['devpath'], "is not formatted"
            return 1
        if verbose:
            print fs['devpath'], "is correctly formatted"
        return 0

    def check_fs_fmt_HPUX(self, fs, verbose=False):
        if fs['type'] == 'vxfs':
            return self.check_fs_fmt_HPUX_vxfs(fs, verbose)
        print >>sys.stderr, "unsupported fs type: %s"%fs['type']
        return 1

    def check_fs_fmt_Linux(self, fs, verbose=False):
        if fs['type'] in ('ext2', 'ext3', 'ext4'):
            return self.check_fs_fmt_Linux_ext(fs, verbose)
        print >>sys.stderr, "unsupported fs type: %s"%fs['type']
        return 1

    def check_fs_fmt_Linux_ext(self, fs, verbose=False):
        cmd = ['tune2fs', '-l', fs['devpath']]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            if verbose:
                print >>sys.stderr, fs['devpath'], "is not formatted"
            return 1
        if verbose:
            print fs['devpath'], "is correctly formatted"
        return 0

    def fix_fs_fmt_Linux_ext(self, fs):
        cmd = ['mkfs.'+fs['type'], '-q', '-b', '4096', fs['devpath']]
        print ' '.join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if len(out) > 0:
            print out
        if len(err) > 0:
            print err
        if p.returncode != 0:
            return 1

        cmd = ['tune2fs', '-m', '0', '-c', '0', '-i', '0', fs['devpath']]
        print ' '.join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if len(out) > 0:
            print out
        if len(err) > 0:
            print err
        if p.returncode != 0:
            return 1

        return 0

    def fix_fs_fmt_Linux(self, fs):
        if fs['type'] in ('ext2', 'ext3', 'ext4'):
            return self.fix_fs_fmt_Linux_ext(fs)
        print >>sys.stderr, "unsupported fs type: %s"%fs['type']
        return 1

    def check_fs_fmt(self, fs, verbose=False):
        if not hasattr(self, 'check_fs_fmt_'+self.sysname):
            print >>sys.stderr, self.sysname, 'not supported'
            raise NotApplicable()
        return getattr(self, 'check_fs_fmt_'+self.sysname)(fs, verbose)

    def fix_fs_fmt_HPUX_vxfs(self, fs):
        cmd = ['newfs', '-F', 'vxfs', '-b', '8192', fs['rdevpath']]
        print ' '.join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if len(out) > 0:
            print out
        if len(err) > 0:
            print err
        if p.returncode != 0:
            return 1
        return 0

    def fix_fs_fmt_HPUX(self, fs):
        if fs['type'] == 'vxfs':
            return self.fix_fs_fmt_HPUX_vxfs(fs)
        print >>sys.stderr, "unsupported fs type: %s"%fs['type']
        return 1

        if not hasattr(self, 'check_fs_fmt_'+self.sysname):
            print >>sys.stderr, self.sysname, 'not supported'
            raise NotApplicable()
        return getattr(self, 'check_fs_fmt_'+self.sysname)(fs, verbose)

    def fix_fs_fmt(self, fs):
        if self.check_fs_fmt(fs) == 0:
            return 0
        if not hasattr(self, 'fix_fs_fmt_'+self.sysname):
            print >>sys.stderr, self.sysname, 'not supported'
            raise NotApplicable()
        return getattr(self, 'fix_fs_fmt_'+self.sysname)(fs)

    def get_res_item(self, rid, item):
        cmd = ['/opt/opensvc/bin/svcmgr', '-s', self.svcname, 'get', '--param', '.'.join((rid, item))]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, ' '.join(cmd), 'failed'
            return 1
        return out.strip()
 
    def get_res(self, rid):
        if rid in self.res:
            return self.res[rid]
        d = {}
        d['mnt'] = self.get_res_item(rid, 'mnt')
        d['dev'] = self.get_res_item(rid, 'dev')
        self.res[rid] = d
        return d

    def get_fs_rids(self, refresh=False):
        if not refresh and hasattr(self, 'rids'):
            return self.rids
        cmd = ['/opt/opensvc/bin/svcmgr', '-s', self.svcname, 'json_status']
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        for line in out.split('\n'):
            if line.startswith('{'):
                out = line
                break
        try:
            # json_status returns 0, even when it outs no data
            self.res_status = json.loads(out)['resources']
        except Exception as e:
            print e
            print out
            self.rids = []
            self.osvc_service = False
            return self.rids
        self.rids = [ k for k in self.res_status.keys() if k.startswith('fs#') ]
        return self.rids

    def find_rid(self, fs):
        found = False
        for rid in self.rids:
            d = self.get_res(rid)
            if d['mnt'] == fs['mnt'] and d['dev'] == fs['devpath']:
                return rid
        return None

    def fix_fs_local(self, fs):
        if self.svcname is not None and self.osvc_service:
            return 0
        if self.check_fs_local(fs, False) == 0:
            return 0
        with open("/etc/fstab", "r") as f:
            lines = f.read().split('\n')
        if len(lines[-1]) == 0:
            del(lines[-1])
        p = re.compile(r'\s*%s\s+'%(fs['devpath']))
        newline = "%s %s %s %s 0 2"%(fs['devpath'], fs['mnt'], fs['type'], fs['opts'])
        for i, line in enumerate(lines):
            if line == newline:
                return 0
            if re.match(p, line) is not None:
                print "remove '%s' from fstab"%line
                del lines[i]
        lines.append(newline)
        print "append '%s' to fstab"%newline
        try:
            with open("/etc/fstab", "w") as f:
                f.write("\n".join(lines)+'\n')
        except:
            print >>sys.stderr, "failed to rewrite fstab"
            return 1
        print "fstab rewritten"
        return 0

    def check_fs_local(self, fs, verbose=False):
        if self.svcname is not None and self.osvc_service:
            return 0
        p = re.compile(r'\s*%s\s+%s'%(fs['devpath'], fs['mnt']))
        with open("/etc/fstab", "r") as f:
            buff = f.read()
        if re.search(p, buff) is not None:
            if verbose:
                print "%s@%s resource correctly set in fstab"%(fs['mnt'], fs['devpath'])
                return 0
        if verbose:
            print >>sys.stderr, "%s@%s resource correctly set in fstab"%(fs['mnt'], fs['devpath'])
        return 1

    def check_fs_svc(self, fs, verbose=False):
        if self.svcname is None:
            return 0
        rids = self.get_fs_rids()
        if not self.osvc_service:
            return 0
        rid = self.find_rid(fs)
        if rid is None:
            if verbose:
                print >>sys.stderr, "%s@%s resource not found in service %s"%(fs['mnt'], fs['devpath'], self.svcname)
            return 1
        if verbose:
            print "%s@%s resource correctly set in service %s"%(fs['mnt'], fs['devpath'], self.svcname)
        return 0
            
    def fix_fs_svc(self, fs):
        if not self.osvc_service or self.check_fs_svc(fs, False) == 0:
            return 0
        cmd = ['/opt/opensvc/bin/svcmgr', '-s', self.svcname, 'get', '--param', 'DEFAULT.encapnodes']
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if self.nodename in out.strip().split():
            tags = "encap"
        else:
            tags = ''
        cmd = ['/opt/opensvc/bin/svcmgr', '-s', self.svcname, 'update', '--resource',
               '{"rtype": "fs", "mnt": "%s", "dev": "%s", "type": "%s", "mnt_opt": "%s", "tags": "%s"}'%(fs['mnt'], fs['devpath'], fs['type'], fs['opts'], tags)]
        print ' '.join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, "unable to fetch %s json status"%self.svcname
            return 1
        return 0

    def check_fs_mounted(self, fs, verbose=False):
        if os.path.ismount(fs['mnt']):
            if verbose:
                print fs['mnt'], "is mounted"
            return 0
        if verbose:
            print >>sys.stderr, fs['mnt'], "is not mounted"
        return 1

    def fix_fs_mounted(self, fs):
        if self.check_fs_mounted(fs, False) == 0:
            return 0
        if self.svcname is None or not self.osvc_service:
            return self.fix_fs_mounted_local(fs)
        else:
            return self.fix_fs_mounted_svc(fs)

    def fix_fs_mounted_svc(self, fs):
        rids = self.get_fs_rids(refresh=True)
        rid = self.find_rid(fs)
        if rid is None:
            print >>sys.stderr, "fs resource with mnt=%s not found in service %s"%(fs['mnt'], self.svcname)
            return 1
        cmd = ['/opt/opensvc/bin/svcmgr', '-s', self.svcname, '--rid', rid, 'mount', '--cluster']
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0 and "unsupported action" in err:
            cmd = ['/opt/opensvc/bin/svcmgr', '-s', self.svcname, '--rid', rid, 'startfs', '--cluster']
            p = Popen(cmd, stdout=PIPE, stderr=PIPE)
            out, err = p.communicate()
        print ' '.join(cmd)
        if p.returncode != 0:
            print >>sys.stderr, "unable to mount %s"%fs['mnt']
            return 1
        return 0

    def fix_fs_mounted_local(self, fs):
        cmd = ['mount', fs['mnt']]
        print ' '.join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if len(out) > 0:
            print out
        if len(err) > 0:
            print >>sys.stderr, err
        if p.returncode != 0:
            print >>sys.stderr, "unable to mount %s"%fs['mnt']
            return 1
        return 0

    def check_fs(self, fs, verbose=False):
        r = 0
        r |= self.check_fs_mnt(fs, verbose)
        r |= self.check_fs_dev(fs, verbose)
        r |= self.check_fs_fmt(fs, verbose)
        r |= self.check_fs_svc(fs, verbose)
        r |= self.check_fs_local(fs, verbose)
        r |= self.check_fs_mounted(fs, verbose)
        return r

    def fix_fs(self, fs):
        if self.fix_fs_mnt(fs) != 0:
            return 1
        if self.fix_fs_dev(fs) != 0:
            return 1
        if self.fix_fs_fmt(fs) != 0:
            return 1
        if self.fix_fs_svc(fs) != 0:
            return 1
        if self.fix_fs_local(fs) != 0:
            return 1
        if self.fix_fs_mounted(fs) != 0:
            return 1
        return 0

    def fixable(self):
        return RET_NA

    def check(self):
        r = 0
        for f in self.fs:
            r |= self.check_fs(f, verbose=True)
        return r

    def fix(self):
        r = 0
        for f in self.fs:
            r |= self.fix_fs(f)
        return r


if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = CompFs(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except ComplianceError:
        sys.exit(RET_ERR)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

  0707010000bce1000081ed00000000000000000000000154fc7bfb00002081000000660000000300000000000000000000003600000000root/opt/opensvc/var/compliance/com.opensvc/groups.py #!/usr/bin/env /opt/opensvc/bin/python
""" 
module use OSVC_COMP_GROUP_... vars
which define {'groupname':{'propname':'propval',... }, ...}

example: 
{
 "tibco":{"gid":1000,"members":"tibco,tibadm",},
 "tibco1":{"gid":1001,"members":"tibco",},
}

supported dictionnary keys:
- gid

dictionnary keys used by another module:
- members
"""

import os
import sys
import json
import grp
import re
from subprocess import Popen 

sys.path.append(os.path.dirname(__file__))

from comp import *

blacklist = [
 "root",
 "bin",
 "daemon",
 "sys",
 "adm",
 "tty",
 "disk",
 "lp",
 "mem",
 "kmem",
 "wheel",
 "mail",
 "uucp",
 "man",
 "games",
 "gopher",
 "video",
 "dip",
 "ftp",
 "lock",
 "audio",
 "nobody",
 "users",
 "utmp",
 "utempter",
 "floppy",
 "vcsa",
 "cdrom",
 "tape",
 "dialout",
 "saslauth",
 "postdrop",
 "postfix",
 "sshd",
 "opensvc",
 "mailnull",
 "smmsp",
 "slocate",
 "rpc",
 "rpcuser",
 "nfsnobody",
 "tcpdump",
 "ntp"
]

class CompGroup(object):
    def __init__(self, prefix='OSVC_COMP_GROUP_'):
        self.prefix = prefix.upper()
        self.groupmod = 'groupmod'
        self.groupadd = 'groupadd'
        self.groupdel = 'groupdel'
        self.grt = {
            'gid': 'gr_gid',
        }

        self.groupmod_p = {
            'gid': '-g',
        }

        self.sysname, self.nodename, x, x, self.machine = os.uname()

        if self.sysname == 'AIX':
            self.groupmod = 'chgroup'
            self.groupadd = 'mkgroup'
            self.groupdel = 'rmgroup'
            self.groupmod_p = {
                'gid': 'id',
            }

        if self.sysname not in ['SunOS', 'Linux', 'HP-UX', 'AIX', 'OSF1']:
            print >>sys.stderr, 'module not supported on', self.sysname
            raise NotApplicable

        self.groups = {}
        for k in [key for key in os.environ if key.startswith(self.prefix)]:
            try:
                self.groups.update(json.loads(os.environ[k]))
            except ValueError:
                print >>sys.stderr, 'group syntax error on var[', k, '] = ', os.environ[k]

        if len(self.groups) == 0:
            raise NotApplicable

        p = re.compile('%%ENV:\w+%%')
        for group, d in self.groups.items():
            for k in d:
                if type(d[k]) not in [str, unicode]:
                    continue
                for m in p.findall(d[k]):
                    s = m.strip("%").replace('ENV:', '')
                    if s in os.environ:
                        v = os.environ[s]
                    elif 'OSVC_COMP_'+s in os.environ:
                        v = os.environ['OSVC_COMP_'+s]
                    else:
                        print >>sys.stderr, s, 'is not an env variable'
                        raise NotApplicable()
                    d[k] = d[k].replace(m, v)
                if k in ('uid', 'gid'):
                    d[k] = int(d[k])


    def fixable(self):
        return RET_NA

    def fmt_opt_gen(self, item, target):
        return [item, target]

    def fmt_opt_aix(self, item, target):
        return ['='.join((item, target))]

    def fmt_opt(self, item, target):
        if self.sysname == 'AIX':
            return self.fmt_opt_aix(item, target)
        else:
            return self.fmt_opt_gen(item, target)
        
    def fix_item(self, group, item, target):
        if item in self.groupmod_p:
            cmd = [getattr(self, 'groupmod')]
            cmd += self.fmt_opt(self.groupmod_p[item], str(target))
            cmd += [group]
            print ' '.join(cmd)
            p = Popen(cmd)
            out, err = p.communicate()
            r = p.returncode
            if r == 0:
                return RET_OK
            else:
                return RET_ERR
        else:
            print >>sys.stderr, 'no fix implemented for', item
            return RET_ERR

    def check_item(self, group, item, target, current, verbose=False):
        if type(current) == int and current < 0:
            current += 4294967296
        if target == current:
            if verbose:
                print 'group', group, item+':', current
            return RET_OK
        else:
            if verbose:
                print >>sys.stderr, 'group', group, item+':', current, 'target:', target
            return RET_ERR 

    def try_create_group(self, props):
        #
        # don't try to create group if passwd db is not 'files'
        # beware: 'files' db is the implicit default
        #
        if 'db' in props and props['db'] != 'files':
            return False
        if set(self.grt.keys()) <= set(props.keys()):
            return True
        return False

    def check_group_del(self, group):
        try:
            groupinfo = grp.getgrnam(group)
        except KeyError:
            print 'group', group, 'does not exist, on target'
            return RET_OK
        print >>sys.stderr, 'group', group, "exists, shouldn't"
        return RET_ERR

    def check_group(self, group, props):
        if group.startswith('-'):
            return self.check_group_del(group.lstrip('-'))
        r = 0
        try:
            groupinfo = grp.getgrnam(group)
        except KeyError:
            if self.try_create_group(props):
                print >>sys.stderr, 'group', group, 'does not exist'
                return RET_ERR
            else:
                print 'group', group, 'does not exist and not enough info to create it'
                return RET_OK
        for prop in self.grt:
            if prop in props:
                r |= self.check_item(group, prop, props[prop], getattr(groupinfo, self.grt[prop]), verbose=True)
        return r

    def create_group(self, group, props):
        cmd = [getattr(self, 'groupadd')]
        for item in self.grt:
            cmd += self.fmt_opt(self.groupmod_p[item], str(props[item]))
        cmd += [group]
        print ' '.join(cmd)
        p = Popen(cmd)
        out, err = p.communicate()
        r = p.returncode
        if r == 0:
            return RET_OK
        else:
            return RET_ERR

    def fix_group_del(self, group):
        if group in blacklist:
            print >>sys.stderr, "delete", group, "... cowardly refusing"
            return RET_ERR
        try:
            groupinfo = grp.getgrnam(group)
        except KeyError:
            return RET_OK
        cmd = [self.groupdel, group]
        print ' '.join(cmd)
        p = Popen(cmd)
        out, err = p.communicate()
        r = p.returncode
        if r == 0:
            return RET_OK
        else:
            return RET_ERR

    def fix_group(self, group, props):
        if group.startswith('-'):
            return self.fix_group_del(group.lstrip('-'))
        r = 0
        try:
            groupinfo = grp.getgrnam(group)
        except KeyError:
            if self.try_create_group(props):
                return self.create_group(group, props)
            else:
                print >>sys.stderr, 'group', group, 'does not exist'
                return RET_OK
        for prop in self.grt:
            if prop in props and \
               self.check_item(group, prop, props[prop], getattr(groupinfo, self.grt[prop])) != RET_OK:
                r |= self.fix_item(group, prop, props[prop])
        return r

    def check(self):
        r = 0
        for group, props in self.groups.items():
            r |= self.check_group(group, props)
        return r

    def fix(self):
        r = 0
        for group, props in self.groups.items():
            r |= self.fix_group(group, props)
        return r

if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = CompGroup(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

   0707010000bce2000081ed00000000000000000000000154fc7bfb00001ee7000000660000000300000000000000000000004100000000root/opt/opensvc/var/compliance/com.opensvc/groups_membership.py  #!/usr/bin/env /opt/opensvc/bin/python
""" 
module use OSVC_COMP_GROUP_... vars
which define {'groupname':{'propname':'propval',... }, ...}

example: 
{"tibco":{"gid":1000,"members":"tibco,tibadm",},
 "tibco1":{"gid":1001,"members":"tibco",},
}

supported dictionnary keys:
- members

dictionnary keys used by another module:
- gid
"""

import os
import sys
import json
import grp
from subprocess import *
from utilities import which

sys.path.append(os.path.dirname(__file__))

from comp import *

class CompGroupMembership(object):
    def __init__(self, prefix='OSVC_COMP_GROUP_'):
        self.member_of_h = {}
        self.prefix = prefix.upper()
        self.grt = {
            'members': 'gr_mem',
        }
        self.sysname, self.nodename, x, x, self.machine = os.uname()

        if self.sysname not in ['SunOS', 'Linux', 'HP-UX', 'AIX', 'OSF1']:
            print >>sys.stderr, 'module not supported on', self.sysname
            raise NotApplicable

        self.groups = {}
        for k in [ key for key in os.environ if key.startswith(self.prefix)]:
            try:
                self.groups.update(json.loads(os.environ[k]))
            except ValueError:
                print 'group syntax error on var[', k, '] = ', os.environ[k]

        if len(self.groups) == 0:
            raise NotApplicable

        if os.path.exists('/usr/xpg4/bin/id'):
            self.id_bin = '/usr/xpg4/bin/id'
        else:
            self.id_bin = 'id'

    def get_primary_group(self, user):
        cmd = [self.id_bin, "-gn", user]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return
        return out.strip()

    def member_of(self, user, refresh=False):
        if not refresh and user in self.member_of_h:
            # cache hit
            return self.member_of_h[user]

        eg = self.get_primary_group(user)
        if eg is None:
            self.member_of_h[user] = []
            return []

        cmd = [self.id_bin, "-Gn", user]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            self.member_of_h[user] = []
            return self.member_of_h[user]
        ag = set(out.strip().split())
        ag -= set([eg])
        self.member_of_h[user] = ag
        return self.member_of_h[user]

    def fixable(self):
        return RET_NA

    def del_member(self, group, user):
        ag = self.member_of(user)
        if len(ag) == 0:
            return 0
        g = ag - set([group])
        g = ','.join(g)
        return self.fix_member(g, user)

    def add_member(self, group, user):
        if 0 != self._check_member_accnt(user):
            print >>sys.stderr, 'group', group+':', 'cannot add inexistant user "%s"'%user
            return RET_ERR
        if self.get_primary_group(user) == group:
            print "%s is already the primary group of user %s: skip declaration as a secondary group (you may want to change your rule)" % (group, user)
            return RET_OK
        ag = self.member_of(user)
        g = ag | set([group])
        g = ','.join(g)
        return self.fix_member(g, user)

    def fix_member(self, g, user):
        cmd = ['usermod', '-G', g, user]
        print ' '.join(cmd)
        p = Popen(cmd)
        out, err = p.communicate()
        r = p.returncode
        ag = self.member_of(user, refresh=True)
        if r == 0:
            return RET_OK
        else:
            return RET_ERR

    def fix_members(self, group, target):
        r = 0
        for user in target:
            if group in self.member_of(user):
                continue
            r += self.add_member(group, user)
        return r

    def fix_item(self, group, item, target):
        if item == 'members':
            return self.fix_members(group, target)
        else:
            print >>sys.stderr, 'no fix implemented for', item
            return RET_ERR

    def _check_member_accnt(self, user):
        if which('getent'):
            xcmd = ['getent', 'passwd', user]
        elif which('pwget'):
            xcmd = ['pwget', '-n', user]
        else:
            return 0
        xp = Popen(xcmd, stdout=PIPE, stderr=PIPE, close_fds=True)
        xout, xerr = xp.communicate()
        return xp.returncode

    def _check_members_accnts(self, group, list, which, verbose):
        r = RET_OK
        for user in list:
            rc = self._check_member_accnt(user)
            if rc != 0:
                r |= RET_ERR
                if verbose:
                    print >>sys.stderr, 'group', group, 'inexistant user "%s" which is %s'%(user, which)
        return r

    def filter_target(self, group, target):
        new_target = []
        for user in target:
            pg = self.get_primary_group(user)
            if pg == group:
                continue
            new_target.append(user)
        discarded = set(target)-set(new_target)
        if len(discarded) > 0:
            print "discarded %s from members of group %s, as they already use this group as primary (you may want to change your rule)" % (', '.join(discarded), group)
        return new_target
                
    def check_item(self, group, item, target, current, verbose=False):
        r = RET_OK
        if item == 'members':
            r |= self._check_members_accnts(group, current, 'INCLUDED', verbose)
            r |= self._check_members_accnts(group, target, 'REQUIRED', verbose)
        if not isinstance(current, list):
            current = [current]
        target = self.filter_target(group, target)
        if set(target) <= set(current):
            if verbose:
                print 'group', group, item+':', ', '.join(current)
            return r
        else:
            if verbose:
                print >>sys.stderr, 'group', group, item+':', ', '.join(current), '| target:', ', '.join(target)
            return r|RET_ERR

    def check_group(self, group, props):
        r = 0
        try:
            groupinfo = grp.getgrnam(group)
        except KeyError:
            print 'group', group, 'does not exist'
            return RET_OK
        for prop in self.grt:
            if prop in props:
                r |= self.check_item(group, prop, props[prop], getattr(groupinfo, self.grt[prop]), verbose=True)
        return r

    def fix_group(self, group, props):
        r = 0
        try:
            groupinfo = grp.getgrnam(group)
        except KeyError:
            print 'group', group, 'does not exist'
            return RET_OK
        for prop in self.grt:
            if prop in props and \
               self.check_item(group, prop, props[prop], getattr(groupinfo, self.grt[prop])) != RET_OK:
                r |= self.fix_item(group, prop, props[prop])
        return r

    def check(self):
        r = 0
        for group, props in self.groups.items():
            r |= self.check_group(group, props)
        return r

    def fix(self):
        r = 0
        for group, props in self.groups.items():
            r |= self.fix_group(group, props)
        return r

if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = CompGroupMembership(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

 0707010000bce3000081ed00000000000000000000000154fc7bfb0000268d000000660000000300000000000000000000003600000000root/opt/opensvc/var/compliance/com.opensvc/keyval.py #!/usr/bin/env /opt/opensvc/bin/python

"""
OSVC_COMP_SSH='[{"key": "PermitRootLogin", "op": "=", "value": "yes"}]' /opt/opensvc/bin/python /opt/opensvc/var/compliance/com.opensvc/keyval.py OSVC_COMP_SSH check /etc/ssh/sshd_config
"""

import os
import sys
import json

sys.path.append(os.path.dirname(__file__))

from comp import *
from keyval_parser import Parser, ParserError

class KeyVal(object):
    def __init__(self, prefix='OSVC_COMP_KEYVAL_', path=None):
        self.prefix = prefix.upper()
        self.cf = path
        self.nocf = False
        if path is None:
            print >>sys.stderr, "no file path specified"
            raise NotApplicable()

        self.keys = []
        for k in [ key for key in os.environ if key.startswith(self.prefix)]:
            try:
                self.keys += json.loads(os.environ[k])
            except ValueError:
                print >>sys.stderr, 'key syntax error on var[', k, '] = ',os.environ[k]

        if len(self.keys) == 0:
            raise NotApplicable()

        self.target_n_key = {}
        for i, key in enumerate(self.keys):
             if self.keys[i]['op'] == 'IN':
                 self.keys[i]['value'] = json.loads(self.keys[i]['value'])
             if 'op' in key and 'key' in key and key['op'] not in ("unset", "reset"):
                 if key['key'] not in self.target_n_key:
                     self.target_n_key[key['key']] = 1
                 else:
                     self.target_n_key[key['key']] += 1
        try:
            self.conf = Parser(path)
        except ParserError as e:
            print >>sys.stderr, e
            raise ComplianceError()


    def fixable(self):
        return RET_OK

    def _check_key(self, keyname, target, op, value, instance=0, verbose=True):
        r = RET_OK
        if op == "reset":
            if value is not None:
                current_n_key = len(value)
                target_n_key = self.target_n_key[keyname] if keyname in self.target_n_key else 0
                if current_n_key > target_n_key:
                    if verbose:
                        print >>sys.stderr, "%s is set %d times, should be set %d times"%(keyname, current_n_key, target_n_key)
                    return RET_ERR
                else:
                    if verbose:
                        print "%s is set %d times, on target"%(keyname, current_n_key)
                    return RET_OK
            else:
                return RET_OK
        elif op == "unset":
            if value is not None:
                if target.strip() == "":
                    if verbose:
                        print >>sys.stderr, "%s is set, should not be"%keyname
                    return RET_ERR
                target_found = False
                for i, val in enumerate(value):
                    if target == val:
                        target_found = True
                        break

                if target_found:
                    if verbose:
                        print >>sys.stderr, "%s[%d] is set to value %s, should not be"%(keyname, i, target)
                    return RET_ERR
                else:
                    if verbose:
                        print "%s is not set to value %s, on target"%(keyname, target)
                    return RET_OK
            else:
                if target.strip() != "":
                    if verbose:
                        print "%s=%s is not set, on target"%(keyname, target)
                else:
                    if verbose:
                        print "%s is not set, on target"%keyname
                return RET_OK

        if value is None:
            if op == 'IN' and "unset" in map(str, target):
                if verbose:
                    print "%s is not set, on target"%(keyname)
                return RET_OK
            else:
                if verbose:
                    print >>sys.stderr, "%s[%d] is not set, target: %s"%(keyname, instance, str(target))
                return RET_ERR

        if type(value) == list:
            if str(target) in value:
                if verbose:
                    print "%s[%d]=%s on target"%(keyname, instance, str(value))
                return RET_OK
            else:
                if verbose:
                    print >>sys.stderr, "%s[%d]=%s is not set"%(keyname, instance, str(target))
                return RET_ERR

        if op == '=':
            if str(value) != str(target):
                if verbose:
                    print >>sys.stderr, "%s[%d]=%s, target: %s"%(keyname, instance, str(value), str(target))
                r |= RET_ERR
            elif verbose:
                print "%s=%s on target"%(keyname, str(value))
        elif op == 'IN':
            if str(value) not in map(str, target):
                if verbose:
                    print >>sys.stderr, "%s[%d]=%s, target: %s"%(keyname, instance, str(value), str(target))
                r |= RET_ERR
            elif verbose:
                print "%s=%s on target"%(keyname, str(value))
        else:
            if type(value) != int:
                if verbose:
                    print >>sys.stderr, "%s[%d]=%s value must be integer"%(keyname, instance, str(value))
                r |= RET_ERR
            elif op == '<=' and value > target:
                if verbose:
                    print >>sys.stderr, "%s[%d]=%s target: <= %s"%(keyname, instance, str(value), str(target))
                r |= RET_ERR
            elif op == '>=' and value < target:
                if verbose:
                    print >>sys.stderr, "%s[%d]=%s target: >= %s"%(keyname, instance, str(value), str(target))
                r |= RET_ERR
            elif verbose:
                print "%s[%d]=%s on target"%(keyname, instance, str(value))
        return r

    def check_key(self, key, instance=0, verbose=True):
        if 'key' not in key:
            if verbose:
                print >>sys.stderr, "'key' not set in rule %s"%str(key)
            return RET_NA
        if 'value' not in key:
            if verbose:
                print >>sys.stderr, "'value' not set in rule %s"%str(key)
            return RET_NA
        if 'op' not in key:
            op = "="
        else:
            op = key['op']
        target = key['value']

        allowed_ops = ('>=', '<=', '=', 'unset', 'reset', 'IN')
        if op not in allowed_ops:
            if verbose:
                print >>sys.stderr, key['key'], "'op' value must be one of", ", ".join(allowed_ops)
            return RET_NA

        keyname = key['key']
        value = self.conf.get(keyname, instance=instance)

        r = self._check_key(keyname, target, op, value, instance=instance, verbose=verbose)

        return r

    def fix_key(self, key, instance=0):
        if key['op'] == "unset" or (key['op'] == "IN" and key['value'][0] == "unset"):
            print "%s unset"%key['key']
            if key['op'] == "IN":
                target = None
            else:
                target = key['value']
            self.conf.unset(key['key'], target)
        elif key['op'] == "reset":
            target_n_key = self.target_n_key[key['key']] if key['key'] in self.target_n_key else 0
            print "%s truncated to %d definitions"%(key['key'], target_n_key)
            self.conf.truncate(key['key'], target_n_key)
        else:
            if key['op'] == "IN":
                target = key['value'][0]
            else:
                target = key['value']
            print "%s=%s set"%(key['key'], target)
            self.conf.set(key['key'], target, instance=instance)

    def check(self):
        r = 0
        key_instance = {}
        for key in self.keys:
            if 'key' not in key or 'op' not in key:
                continue
            if key['op'] in ('reset', 'unset'):
                instance = None
            else:
                if key['key'] not in key_instance:
                    key_instance[key['key']] = 0
                else:
                    key_instance[key['key']] += 1
                instance = key_instance[key['key']]
            r |= self.check_key(key, instance=instance, verbose=True)
        return r

    def fix(self):
        key_instance = {}
        for key in self.keys:
            if 'key' not in key or 'op' not in key:
                continue
            if key['op'] in ('reset', 'unset'):
                instance = None
            else:
                if key['key'] not in key_instance:
                    key_instance[key['key']] = 0
                else:
                    key_instance[key['key']] += 1
                instance = key_instance[key['key']]
            if self.check_key(key, instance=instance, verbose=False) == RET_ERR:
                self.fix_key(key, instance=instance)
        if not self.conf.changed:
            return
        try:
            self.conf.write()
        except ParserError as e:
            print >>sys.stderr, e
            return RET_ERR
        return RET_OK

if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix configfile_path"""%sys.argv[0]
    if len(sys.argv) != 4:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = KeyVal(sys.argv[1], sys.argv[3])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except ComplianceError:
        sys.exit(RET_ERR)
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

   0707010000bce4000081ed00000000000000000000000154fc7bfb0000128c000000660000000300000000000000000000003d00000000root/opt/opensvc/var/compliance/com.opensvc/keyval_parser.py  #!/usr/bin/env /opt/opensvc/bin/python

import os
import sys
import datetime
import shutil

class ParserError(Exception):
    pass

class Parser(object):
    def __init__(self, path):
        self.path = path
        self.data = {}
        self.changed = False
        self.nocf = False
        self.keys = []
        self.lastkey = '__lastkey__'
        self.comments = {self.lastkey: []}
        self.load()
        self.bkp = path + '.' + str(datetime.datetime.now())

    def __str__(self):
        s = ""
        for k in self.keys:
            if k in self.comments:
                s += '\n'.join(self.comments[k]) + '\n'
            for v in self.data[k]:
                s += k + " " + str(v) + '\n'
        if len(self.comments[self.lastkey]) > 0:
            s += '\n'.join(self.comments[self.lastkey]) + '\n'
        return s

    def truncate(self, key, max):
        if key not in self.data:
            return
        n = len(self.data[key])
        if n <= max:
            return
        self.data[key] = self.data[key][:max]
        self.changed = True

    def set(self, key, value, instance=0):
        if key not in self.data:
            self.data[key] = [value]
            self.keys.append(key)
        elif instance >= len(self.data[key]):
            extra = instance + 1 - len(self.data[key])
            for i in range(len(self.data[key]), instance-1):
                self.data[key].append(None)
            self.data[key].append(value)
        else:
            self.data[key].insert(instance, value)
        self.changed = True

    def unset(self, key, value=None):
        if key in self.data:
            if value is not None and value.strip() != "":
                self.data[key].remove(value)
            else:
                self.data[key] = []
            if len(self.data[key]) == 0:
                del(self.data[key])
                if key in self.keys:
                    self.keys.remove(key)
        self.changed = True

    def get(self, key, instance=0):
        if key not in self.data:
            return
        if instance is None:
            return self.data[key]
        if instance < len(self.data[key]):
            return self.data[key][instance]
        return

    def load(self):
        if not os.path.exists(self.path):
            raise ParserError("%s does not exist"%self.path)
            self.nocf = True
            return
        with open(self.path, 'r') as f:
            buff = f.read()
        self.parse(buff)

    def backup(self):
        if self.nocf:
            return
        try:
            shutil.copy(self.path, self.bkp)
        except Exception as e:
            print e
            raise ParserError("failed to backup %s"%self.path)
        print "%s backup up as %s" % (self.path, self.bkp)

    def restore(self):
        if self.nocf:
            return
        try:
            shutil.copy(self.bkp, self.path)
        except:
            raise ParserError("failed to restore %s"%self.path)
        print "%s restored from %s" % (self.path, self.bkp)


    def write(self):
        self.backup()
        try:
            with open(self.path, 'w') as f:
                f.write(str(self))
            print "%s rewritten"%self.path
        except Exception as e:
            print >>sys.stderr, e
            self.restore()
            raise ParserError()

    def parse(self, buff):
        for line in buff.split("\n"):
            line = line.strip()

            # store comment line and continue
            if line.startswith('#') or len(line) == 0: 
                self.comments[self.lastkey].append(line)
                continue

            # strip end-of-line comment
            try:
                i = line.index('#')
                line = line[:i]
                line = line.strip()
            except ValueError:
                pass

            # discard empty line
            if len(line) == 0:
                continue

            l = line.split()
            if len(l) < 2:
                 continue
            key = l[0]
            value = line[len(key):].strip()

            self.comments[key] = self.comments[self.lastkey]
            self.comments[self.lastkey] = []

            try:
                value = int(value)
            except:
                pass

            if key not in self.keys:
                self.keys.append(key)
            if key not in self.data:
                self.data[key] = []
            self.data[key].append(value)

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print >>sys.stderr, "wrong number of arguments"
        sys.exit(1)
    o = Parser(sys.argv[1])
    o.get("Subsystem")
    o.set("Subsystem", "foo")
    o.unset("PermitRootLogin")
    o.backup()
    print o

0707010000bce5000081ed00000000000000000000000154fc7bfb00003e1d000000660000000300000000000000000000003b00000000root/opt/opensvc/var/compliance/com.opensvc/linux.mpath.py    #!/usr/bin/env /opt/opensvc/bin/python

"""
OSVC_COMP_MPATH='[{"key": "defaults.polling_interval", "op": ">=", "value": 20}, {"key": "device.HP.HSV210.prio", "op": "=", "value": "alua"}]' ./linux.mpath.py OSVC_COMP_MPATH check
"""

import os
import sys
import json
import re
from subprocess import *

sys.path.append(os.path.dirname(__file__))

from comp import *

comment_chars = "#;"
sections_tree = {
  'defaults': {},
  'blacklist': {
    'device': {},
  },
  'blacklist_exceptions': {
    'device': {},
  },
  'devices': {
    'device': {},
  },
  'multipaths': {
    'multipath': {},
  },
}

class Blacklist(object):
    def __init__(self, name=""):
        self.name = name
        self.wwid = []
        self.devnode = []
        self.devices = []

    def __str__(self):
        s = ""
        if len(self.devices) + len(self.wwid) + len(self.devnode) == 0:
            return s
        s += self.name + " {\n"
        for wwid in self.wwid:
            s += "\twwid " + str(wwid) + "\n"
        for devnode in self.devnode:
            s += "\tdevnode " + str(devnode) + "\n"
        for device in self.devices:
            s += str(device)
        s += "}\n"
        return s

class Section(object):
    def __init__(self, name="", indent=1):
        self.name = name
        self.attr = {}
        self.indent = ""
        for i in range(indent):
            self.indent += '\t'

    def __str__(self):
        s = ""
        s += self.indent + self.name + " {\n"
        for a, v in self.attr.items():
            v = str(v)
            if ' ' in v:
                v = '"' + v + '"'
            s += self.indent + "\t" + a + " " + v + "\n"
        s += self.indent + "}\n"
        return s

class Conf(object):
    def __init__(self):
        self.blacklist = Blacklist("blacklist")
        self.blacklist_exceptions = Blacklist("blacklist_exceptions")
        self.defaults = Section("defaults", indent=0)
        self.devices = []
        self.multipaths = []
        self.changed = False

    def __str__(self):
        s = ""
        s += str(self.defaults)
        s += str(self.blacklist)
        s += str(self.blacklist_exceptions)
        if len(self.devices) > 0:
            s += "devices {\n"
            for device in self.devices:
                s += str(device)
            s += "}\n"
        if len(self.multipaths) > 0:
            s += "multipaths {\n"
            for multipath in self.multipaths:
                s += str(multipath)
            s += "}\n"
        return s

    def set(self, key, value):
        index = self.parse_key(key)
        key = re.sub(r'\{([^\}]+)\}\.', '', key)
        l = key.split('.')
        if key.endswith('}'):
            a = None
        else:
            a = l[-1]
        if l[1] == "device":
            o = self.find_device(l[0], index)
            if o is None:
                o = Section("device")
                o.attr['vendor'] = index[0]
                o.attr['product'] = index[1]
                _l = self.get_device_list(l[0])
                _l.append(o)
            if a is not None:
                o.attr[a] = value
            self.changed = True
        elif l[1] == "multipath":
            o = self.find_multipath(index)
            if o is None:
                o = Section("multipath")
                o.attr['wwid'] = index
                self.multipaths.append(o)
            o.attr[a] = value
            self.changed = True
        elif l[-1] == "wwid":
            o = getattr(self, l[0])
            o.wwid.append(str(value))
            self.changed = True
        elif l[-1] == "devnode":
            o = getattr(self, l[0])
            o.devnode.append(str(value))
            self.changed = True
        elif l[0] == "defaults":
            self.defaults.attr[a] = value
            self.changed = True

    def get(self, key):
        index = self.parse_key(key)
        key = re.sub(r'\{([^\}]+)\}\.', '', key)
        l = key.split('.')
        if key.endswith('}'):
            a = None
        else:
            a = l[-1]
        if len(l) < 2:
            print >>sys.stderr, "malformed key", key
            return
        if l[1] == "device":
            o = self.find_device(l[0], index)
            if o:
                if a is None:
                    return ""
                elif a in o.attr:
                    return o.attr[a]
        elif l[1] == "multipath":
            o = self.find_multipath(index)
            if o and a in o.attr:
                return o.attr[a]
        elif l[-1] == "wwid":
            return getattr(self, l[0]).wwid
        elif l[-1] == "devnode":
            return getattr(self, l[0]).devnode
        elif l[0] == "defaults":
            if a in self.defaults.attr:
                return self.defaults.attr[a]

    def find_multipath(self, index):
        wwid = index
        for multipath in self.multipaths:
            if multipath.attr['wwid'] == wwid:
                return multipath

    def get_device_list(self, section):
        l = getattr(self, section)
        if type(l) != list and hasattr(l, "devices"):
            l = getattr(l, "devices")
        if type(l) != list:
            return
        return l
 
    def find_device(self, section, index):
        vendor, product = index
        l = self.get_device_list(section)
        if not l:
            return
        for device in l:
            if 'vendor' not in device.attr or \
               'product' not in device.attr:
                continue
            if device.attr['vendor'] == vendor and \
               device.attr['product'] == product:
                return device

    def parse_key(self, key):
        key = key.strip()
        m = re.search(r'device\.\{([^\}]+)\}\.\{([^\}]+)\}', key)
        if m:
            return m.group(1), m.group(2)

        m = re.search(r'multipath\.\{([^\}]+)\}', key)
        if m:
            return m.group(1)

class LinuxMpath(object):
    def __init__(self, prefix='OSVC_COMP_MPATH_'):
        self.prefix = prefix.upper()
        self.need_restart = False
        self.cf = os.path.join(os.sep, 'etc', 'multipath.conf')
        self.nocf = False
        self.conf = Conf()

        self.keys = []
        for k in [ key for key in os.environ if key.startswith(self.prefix)]:
            try:
                self.keys += json.loads(os.environ[k])
            except ValueError:
                print >>sys.stderr, 'key syntax error on var[', k, '] = ',os.environ[k]

        if len(self.keys) == 0:
            raise NotApplicable()

        self.load_file(self.cf)

    def fixable(self):
        return RET_OK

    def load_file(self, p):
        if not os.path.exists(p):
            print >>sys.stderr, p, "does not exist"
            self.nocf = True
            return
        with open(p, 'r') as f:
            buff = f.read()
        buff = self.strip_comments(buff)
        self._load_file(buff, sections_tree)

    def strip_comments(self, buff):
        lines = buff.split('\n')
        l = []
        for line in lines:
            line = line.strip()
            if len(line) == 0:
                continue
            discard = False
            for c in comment_chars:
                if line[0] == c:
                    discard = True
                    break
                try:
                    i = line.index(c)
                    line = line[:i]
                except ValueError:
                    pass
            if not discard and len(line) > 0:
                l.append(line)
        return "\n".join(l)

    def _load_file(self, buff, sections, chain=[]):
        for section, subsections in sections.items():
            _chain = chain + [section]
            _buff = buff
            while True:
                data = self.load_section(_buff, section)
                if data is None:
                    break
                _buff = data[1]
                self.load_keywords(data[0], subsections, _chain)
                self._load_file(data[0], subsections, _chain)

    def load_keywords(self, buff, subsections, chain):
        keywords = {}
        keyword = None
        for line in buff.split('\n'):
            if len(line) == 0:
                continue
            keyword = line.split()[0]
            if keyword in subsections:
                continue
            value = line[len(keyword):].strip().strip('"')
            if len(value) == 0:
                continue
            if keyword in ('wwid', 'devnode') and chain[-1].startswith('blacklist'):
                if keyword not in keywords:
                    keywords[keyword] = [value]
                else:
                    keywords[keyword] += [value]
            else:
                keywords[keyword] = value
        if chain[-1] == 'device' and chain[0] == 'devices':
            s = Section("device")
            s.attr = keywords
            self.conf.devices.append(s)
        elif chain[-1] == 'multipath':
            s = Section("multipath")
            s.attr = keywords
            self.conf.multipaths.append(s)
        elif chain[-1] == 'device' and chain[0] == 'blacklist':
            s = Section("device")
            s.attr = keywords
            self.conf.blacklist.devices.append(s)
        elif chain[-1] == 'device' and chain[0] == 'blacklist exceptions':
            s = Section("device")
            s.attr = keywords
            self.conf.blacklist_exceptions.devices.append(s)
        elif chain[-1] == 'blacklist':
            if 'wwid' in keywords:
                self.conf.blacklist.wwid = keywords['wwid']
            if 'devnode' in keywords:
                self.conf.blacklist.devnode = keywords['devnode']
        elif chain[-1] == 'blacklist_exceptions':
            if 'wwid' in keywords:
                self.conf.blacklist_exceptions.wwid = keywords['wwid']
            if 'devnode' in keywords:
                self.conf.blacklist_exceptions.devnode = keywords['devnode']
        elif chain[-1] == 'defaults':
            self.conf.defaults.attr = keywords

    def load_section(self, buff, section):
        l = []
        try:
            start = buff.index(section)
        except ValueError:
            return
        buff = buff[start:]
        try:
            buff = buff[buff.index('{')+1:]
        except ValueError:
            return
        depth = 1
        for i, c in enumerate(buff):
            if c == '{':
                depth += 1
            elif c == '}':
                depth -= 1
            if depth == 0:
                return buff[:i], buff[i+1:]
        return
                
    def _check_key(self, keyname, target, op, value, verbose=True):
        r = RET_OK
        if value is None:
            if verbose:
                print >>sys.stderr, "%s is not set"%keyname
            return RET_ERR

        if type(value) == list:
            if str(target) in value:
                if verbose:
                    print "%s=%s on target"%(keyname, str(value))
                return RET_OK
            else:
                if verbose:
                    print >>sys.stderr, "%s=%s is not set"%(keyname, str(target))
                return RET_ERR

        if op == '=':
            target = str(target).strip()
            if str(value) != target:
                if verbose:
                    print >>sys.stderr, "%s=%s, target: %s"%(keyname, str(value), target)
                r |= RET_ERR
            elif verbose:
                print "%s=%s on target"%(keyname, str(value))
        else:
            if type(value) != int:
                if verbose:
                    print >>sys.stderr, "%s=%s value must be integer"%(keyname, str(value))
                r |= RET_ERR
            elif op == '<=' and value > target:
                if verbose:
                    print >>sys.stderr, "%s=%s target: <= %s"%(keyname, str(value), str(target))
                r |= RET_ERR
            elif op == '>=' and value < target:
                if verbose:
                    print >>sys.stderr, "%s=%s target: >= %s"%(keyname, str(value), str(target))
                r |= RET_ERR
            elif verbose:
                print "%s=%s on target"%(keyname, str(value))
        return r

    def check_key(self, key, verbose=True):
        if 'key' not in key:
            if verbose:
                print >>sys.stderr, "'key' not set in rule %s"%str(key)
            return RET_NA
        if 'value' not in key:
            if verbose:
                print >>sys.stderr, "'value' not set in rule %s"%str(key)
            return RET_NA
        if 'op' not in key:
            op = "="
        else:
            op = key['op']
        target = key['value']

        if op not in ('>=', '<=', '='):
            if verbose:
                print >>sys.stderr, "'op' must be either '=', '>=' or '<=': %s"%str(key)
            return RET_NA

        keyname = key['key']
        value = self.conf.get(keyname)

        if value is None:
            if verbose:
                print >>sys.stderr, "%s key is not set"%keyname
            return RET_ERR

        r = self._check_key(keyname, target, op, value, verbose=verbose)

        return r

    def fix_key(self, key):
        print "%s=%s set"%(key['key'], key['value'])
        self.conf.set(key['key'], key['value'])

    def check(self):
        r = 0
        for key in self.keys:
            r |= self.check_key(key, verbose=True)
        return r

    def fix(self):
        for key in self.keys:
            if self.check_key(key, verbose=False) == RET_ERR:
                self.fix_key(key)

        if not self.conf.changed:
            return

        if not self.nocf:
            import datetime
            backup = self.cf+'.'+str(datetime.datetime.now())
            try:
                import shutil
                shutil.copy(self.cf, backup)
            except:
                print >>sys.stderr, "failed to backup %s"%self.cf
                return RET_ERR
            print self.cf, "backed up as %s"%backup
        try:
            with open(self.cf, 'w') as f:
                f.write(str(self.conf))
            print self.cf, "rewritten"
            self.need_restart = True
        except:
            print >>sys.stderr, "failed to write %s"%self.cf
            if not self.nocf:
                shutil.copy(backup, self.cf)
                print "backup restored"
            return RET_ERR

        self.restart_daemon()

        return RET_OK

    def restart_daemon(self):
        if not self.need_restart:
            return
        candidates = [
          "/etc/init.d/multipathd",
          "/etc/init.d/multipath-tools",
        ]
        fpath = None
        for i in candidates:
            if os.path.exists(i):
                fpath = i
                break
        if fpath is None:
            print >>sys.stderr, "multipath tools startup script not found"
            return RET_ERR
        print "restarting multipath daemon"
        cmd = [fpath, "restart"]
        p = Popen(cmd, stdin=None, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if len(err) > 0:
            print >>sys.stderr, err

if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = LinuxMpath(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

   0707010000bce6000081ed00000000000000000000000154fc7bfb000016de000000660000000300000000000000000000003800000000root/opt/opensvc/var/compliance/com.opensvc/nodeconf.py   #!/usr/bin/env /opt/opensvc/bin/python

"""
OSVC_COMP_NODECONF='[{"key": "node.repopkg", "op": "=", "value": "ftp://iisprddistrib.dsit.sncf.fr/opensvc"}, {"key": "node.repocomp", "op": "=", "value": "ftp://master:MasteR0@iisprddistrib.dsit.sncf.fr/"}]' ./nodeconf.py OSVC_COMP_NODECONF check
"""

import os
import sys
import json
import re
from subprocess import *

sys.path.append(os.path.dirname(__file__))

from comp import *

class NodeConf(object):
    def __init__(self, prefix='OSVC_COMP_NODECONF_'):
        self.prefix = prefix.upper()
        self.keys = []

        for k in [ key for key in os.environ if key.startswith(self.prefix)]:
            try:
                self.keys += json.loads(os.environ[k])
            except ValueError:
                print >>sys.stderr, 'key syntax error on var[', k, '] = ',os.environ[k]

        if len(self.keys) == 0:
            raise NotApplicable()

        for key in self.keys:
            if "value" in key:
                key['value'] = self.subst(key['value'])

    def subst(self, v):
        if type(v) == list:
            l = []
            for _v in v:
                l.append(self.subst(_v))
            return l
        if type(v) != str and type(v) != unicode:
            return v

        p = re.compile('%%ENV:\w+%%')
        for m in p.findall(v):
            s = m.strip("%").replace('ENV:', '')
            if s in os.environ:
                _v = os.environ[s]
            elif 'OSVC_COMP_'+s in os.environ:
                _v = os.environ['OSVC_COMP_'+s]
            else:
                print >>sys.stderr, s, 'is not an env variable'
                raise NotApplicable()
            v = v.replace(m, _v)
        return v

    def fixable(self):
        return RET_OK

    def set_val(self, keyname, target):
        if type(target) == int:
            target = str(target)
        cmd = ['/opt/opensvc/bin/nodemgr', 'set', '--param', keyname, '--value', target]
        print ' '.join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        return p.returncode

    def get_val(self, keyname):
        cmd = ['/opt/opensvc/bin/nodemgr', 'get', '--param', keyname]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            #print >>sys.stderr, '\n'.join((' '.join(cmd), out, err))
            return
        out = out.strip()
        try:
            out = int(out)
        except:
            pass
        return out

    def _check_key(self, keyname, target, op, value, verbose=True):
        r = RET_OK
        if value is None:
            if verbose:
                print >>sys.stderr, "%s not set"%keyname
            r |= RET_ERR
        if op == '=':
            if str(value) != str(target):
                if verbose:
                    print >>sys.stderr, "%s=%s, target: %s"%(keyname, str(value), str(target))
                r |= RET_ERR
            elif verbose:
                print "%s=%s on target"%(keyname, str(value))
        else:
            if type(value) != int:
                if verbose:
                    print >>sys.stderr, "%s=%s value must be integer"%(keyname, str(value))
                r |= RET_ERR
            elif op == '<=' and value > target:
                if verbose:
                    print >>sys.stderr, "%s=%s target: <= %s"%(keyname, str(value), str(target))
                r |= RET_ERR
            elif op == '>=' and value < target:
                if verbose:
                    print >>sys.stderr, "%s=%s target: >= %s"%(keyname, str(value), str(target))
                r |= RET_ERR
            elif verbose:
                print "%s=%s on target"%(keyname, str(value))
        return r

    def check_key(self, key, verbose=True):
        if 'key' not in key:
            if verbose:
                print >>sys.stderr, "'key' not set in rule %s"%str(key)
            return RET_NA
        if 'value' not in key:
            if verbose:
                print >>sys.stderr, "'value' not set in rule %s"%str(key)
            return RET_NA
        if 'op' not in key:
            op = "="
        else:
            op = key['op']
        target = key['value']

        if op not in ('>=', '<=', '='):
            if verbose:
                print >>sys.stderr, "'value' list member 0 must be either '=', '>=' or '<=': %s"%str(key)
            return RET_NA

        keyname = key['key']
        value = self.get_val(keyname)

        if value is None:
            print >>sys.stderr, "%s key is not set"%keyname
            return RET_ERR

        return self._check_key(keyname, target, op, value, verbose)

    def fix_key(self, key):
        return self.set_val(key['key'], key['value'])

    def check(self):
        r = 0
        for key in self.keys:
            r |= self.check_key(key, verbose=True)
        return r

    def fix(self):
        r = 0
        for key in self.keys:
            if self.check_key(key, verbose=False) == RET_ERR:
                r += self.fix_key(key)
        return r

if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = NodeConf(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

  0707010000bce7000081ed00000000000000000000000154fc7bfb000064ee000000660000000300000000000000000000003800000000root/opt/opensvc/var/compliance/com.opensvc/packages.py   #!/usr/bin/env /opt/opensvc/bin/python
""" 
module use OSVC_COMP_PACKAGES_... vars
which define ['pkg1', 'pkg2', ...]
"""

import os
import re
import sys
import json
import pwd
import tempfile
from subprocess import *
from utilities import which

sys.path.append(os.path.dirname(__file__))

from comp import *

class CompPackages(object):
    def __init__(self, prefix='OSVC_COMP_PACKAGES_', uri=None):
        self.combo_fix = False
        self.uri = uri
        self.prefix = prefix.upper()
        self.sysname, self.nodename, x, x, self.machine = os.uname()
        self.known_archs = ['i386', 'i586', 'i686', 'x86_64', 'noarch', '*']

        if self.sysname not in ['Linux', 'AIX', 'HP-UX', 'SunOS']:
            print >>sys.stderr, __file__, 'module not supported on', self.sysname
            raise NotApplicable()

        if 'OSVC_COMP_PACKAGES_PKG_TYPE' in os.environ and \
           os.environ['OSVC_COMP_PACKAGES_PKG_TYPE'] == "bundle":
            self.pkg_type = 'bundle'
        else:
            self.pkg_type = 'product'

        self.packages = []
        for k in [key for key in os.environ if key.startswith(self.prefix)]:
            try:
                self.packages += json.loads(self.subst(os.environ[k]))
            except ValueError:
                print >>sys.stderr, 'failed to concatenate', os.environ[k], 'to package list'

        if len(self.packages) == 0:
            raise NotApplicable()

        self.data = {}
        l = []
        for pkg in self.packages:
            if type(pkg) == dict:
                l.append(pkg['pkgname'])
                self.data[pkg['pkgname']] = pkg
        if len(l) > 0:
            self.packages = l

        vendor = os.environ.get('OSVC_COMP_NODES_OS_VENDOR', 'unknown')
        release = os.environ.get('OSVC_COMP_NODES_OS_RELEASE', 'unknown')
        if vendor in ['Debian', 'Ubuntu']:
            self.get_installed_packages = self.deb_get_installed_packages
            self.pkg_add = self.apt_fix_pkg
            self.pkg_del = self.apt_del_pkg
        elif vendor in ['CentOS', 'Redhat', 'Red Hat'] or \
             (vendor == 'Oracle' and self.sysname == 'Linux'):
            if which("yum") is None:
                print >>sys.stderr, "package manager not found (yum)"
                raise ComplianceError()
            self.combo_fix = True
            self.get_installed_packages = self.rpm_get_installed_packages
            self.pkg_add = self.yum_fix_pkg
            self.pkg_del = self.yum_del_pkg
        elif vendor == "SuSE":
            if which("zypper") is None:
                print >>sys.stderr, "package manager not found (zypper)"
                raise ComplianceError()
            self.get_installed_packages = self.rpm_get_installed_packages
            self.pkg_add = self.zyp_fix_pkg
            self.pkg_del = self.zyp_del_pkg
        elif vendor in ['IBM']:
            self.get_installed_packages = self.aix_get_installed_packages
            self.pkg_add = self.aix_fix_pkg
            self.pkg_del = self.aix_del_pkg
            if self.uri is None:
                print >>sys.stderr, "resource must be set"
                raise NotApplicable()
        elif vendor in ['HP']:
            self.get_installed_packages = self.hp_get_installed_packages
            self.pkg_add = self.hp_fix_pkg
            self.pkg_del = self.hp_del_pkg
        elif vendor in ['Oracle']:
            self.get_installed_packages = self.sol_get_installed_packages
            self.pkg_add = self.sol_fix_pkg
            self.pkg_del = self.sol_del_pkg
        else:
            print >>sys.stderr, vendor, "not supported"
            raise NotApplicable()

        self.load_reloc()
        self.packages = map(lambda x: x.strip(), self.packages)
        self.expand_pkgnames()
        self.installed_packages = self.get_installed_packages()

    def subst(self, v):
        if type(v) == list:
            l = []
            for _v in v:
                l.append(self.subst(_v))
            return l
        if type(v) != str and type(v) != unicode:
            return v

        p = re.compile('%%ENV:\w+%%')
        for m in p.findall(v):
            s = m.strip("%").replace('ENV:', '')
            if s in os.environ:
                _v = os.environ[s]
            elif 'OSVC_COMP_'+s in os.environ:
                _v = os.environ['OSVC_COMP_'+s]
            else:
                print >>sys.stderr, s, 'is not an env variable'
                raise NotApplicable()
            v = v.replace(m, _v)
        return v.strip()

    def load_reloc(self):
        self.reloc = {}
        for i, pkgname in enumerate(self.packages):
            l = pkgname.split(':')
            if len(l) != 2:
                continue
            self.packages[i] = l[0]
            self.reloc[l[0]] = l[1]

    def expand_pkgnames(self):
        """ Expand wildcards and implicit arch
        """
        l = []
        for pkgname in self.packages:
            if (pkgname.startswith('-') or pkgname.startswith('+')) and len(pkgname) > 1:
                prefix = pkgname[0]
                pkgname = pkgname[1:]
            else:
                prefix = ''
            l += map(lambda x: prefix+x, self.expand_pkgname(pkgname, prefix))
        self.packages = l

    def expand_pkgname(self, pkgname, prefix):
        vendor = os.environ.get('OSVC_COMP_NODES_OS_VENDOR', 'unknown')
        release = os.environ.get('OSVC_COMP_NODES_OS_RELEASE', 'unknown')
        if vendor in ['CentOS', 'Redhat', 'Red Hat'] or (vendor == 'Oracle' and release.startswith('VM ')):
            return self.yum_expand_pkgname(pkgname, prefix)
        elif vendor == 'SuSE':
            return self.zyp_expand_pkgname(pkgname, prefix)
        elif vendor in ['IBM']:
            return self.aix_expand_pkgname(pkgname, prefix)
        return [pkgname]

    def aix_expand_pkgname(self, pkgname, prefix=''):
        """
LGTOnw.clnt:LGTOnw.clnt.rte:8.1.1.6::I:C:::::N:NetWorker Client::::0::
LGTOnw.man:LGTOnw.man.rte:8.1.1.6::I:C:::::N:NetWorker Man Pages::::0::

or for rpm lpp_source:

zlib                                                               ALL  @@R:zlib _all_filesets
   @@R:zlib-1.2.7-2 1.2.7-2

        """
        if not hasattr(self, "nimcache"):
            cmd = ['nimclient', '-o', 'showres', '-a', 'resource=%s'%self.uri, '-a', 'installp_flags=L']
            p = Popen(cmd, stdout=PIPE, stderr=PIPE)
            out, err = p.communicate()
            self.lpp_type = "installp"
            if "0042-175" in err:
                # not a native installp lpp_source
                cmd = ['nimclient', '-o', 'showres', '-a', 'resource=%s'%self.uri]
                p = Popen(cmd, stdout=PIPE, stderr=PIPE)
                out, err = p.communicate()
                self.lpp_type = "rpm"
            self.nimcache = out.split('\n')

        l = []
        if self.lpp_type == "rpm":
            l = self.aix_expand_pkgname_rpm(pkgname, prefix=prefix)
        elif self.lpp_type == "native":
            l = self.aix_expand_pkgname_native(pkgname, prefix=prefix)

        if len(l) == 0:
            l = [pkgname]
        return l

    def aix_expand_pkgname_rpm(self, pkgname, prefix=''):
        import fnmatch
        l = []
        for line in self.nimcache:
            line = line.strip()
            if len(line) == 0:
                continue
            words = line.split()
            if line.startswith("@@") and len(words) > 1:
                _pkgvers = words[1]
                if fnmatch.fnmatch(_pkgname, pkgname) and _pkgname not in l:
                    l.append(_pkgname)
            else:
                _pkgname = words[0]
        return l

    def aix_expand_pkgname_native(self, pkgname, prefix=''):
        import fnmatch
        l = []
        for line in self.nimcache:
            words = line.split(':')
            if len(words) < 5:
                continue
            _pkgvers = words[2]
            _pkgname = words[1].replace('-'+_pkgvers, '')
            if fnmatch.fnmatch(_pkgname, pkgname) and _pkgname not in l:
                l.append(_pkgname)
        return l

    def zyp_expand_pkgname(self, pkgname, prefix=''):
        arch_specified = False
        for arch in self.known_archs:
            if pkgname.endswith(arch):
                arch_specified = True
        cmd = ['zypper', 'packages']
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            if prefix != '-':
                print >>sys.stderr, 'can not expand (cmd error)', pkgname, err
                return []
            else:
                return [pkgname]
        lines = out.split('\n')
        if len(lines) < 2:
            if prefix != '-':
                print >>sys.stderr, 'can not expand', pkgname
                return []
            else:
                return [pkgname]
        for i, line in enumerate(lines):
            if "--+--" in line:
                break
        lines = lines[i+1:]
        l = []
        for line in lines:
            words = map(lambda x: x.strip(), line.split(" | "))
            if len(words) != 5:
                continue
            _status, _repo, _name, _version, _arch = words
            if arch_specified:
                if _name != pkgname or (arch != '*' and arch != _arch):
                    continue
            else:
                if _name != pkgname:
                    continue
            _pkgname = '.'.join((_name, _arch))
            if _pkgname in l:
                continue
            l.append(_pkgname)

        if arch_specified or len(l) == 1:
            return l

        if os.environ['OSVC_COMP_NODES_OS_ARCH'] in ('i386', 'i586', 'i686', 'ia32'):
            archs = ('i386', 'i586', 'i686', 'ia32', 'noarch')
        else:
            archs = (os.environ['OSVC_COMP_NODES_OS_ARCH'], 'noarch')

        ll = []
        for pkgname in l:
            if pkgname.split('.')[-1] in archs:
                # keep only packages matching the arch
                ll.append(pkgname)

        return ll

    def yum_expand_pkgname(self, pkgname, prefix=''):
        arch_specified = False
        for arch in self.known_archs:
            if pkgname.endswith(arch):
                arch_specified = True
        cmd = ['yum', 'list', pkgname]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            if prefix != '-':
                print >>sys.stderr, 'can not expand (cmd error)', pkgname, err
                return []
            else:
                return [pkgname]
        lines = out.split('\n')
        if len(lines) < 2:
            if prefix != '-':
                print >>sys.stderr, 'can not expand', pkgname
                return []
            else:
                return [pkgname]
        lines = lines[1:]
        l = []
        for line in lines:
            words = line.split()
            if len(words) != 3:
                continue
            if words[0] in ("Installed", "Available", "Loaded", "Updating"):
                continue
            if words[0] in l:
                continue
            l.append((words[0], words[1]))

        ll = []
        ix86_added = False
        from distutils.version import LooseVersion as V
        for _pkgname, _version in sorted(l, key=lambda x: V(x[1]), reverse=True):
            pkgarch = _pkgname.split('.')[-1]
            if pkgarch not in ('i386', 'i586', 'i686', 'ia32'):
                #print "add", _pkgname, "because", pkgarch, "not in ('i386', 'i586', 'i686', 'ia32')"
                ll.append(_pkgname)
            elif not ix86_added:
                #print "add", _pkgname, "because", pkgarch, "not ix86_added"
                ll.append(_pkgname)
                ix86_added = True
        l = ll

        if arch_specified or len(l) == 1:
            return l

        if os.environ['OSVC_COMP_NODES_OS_ARCH'] in ('i386', 'i586', 'i686', 'ia32'):
            archs = ('i386', 'i586', 'i686', 'ia32', 'noarch')
        else:
            archs = (os.environ['OSVC_COMP_NODES_OS_ARCH'], 'noarch')

        ll = []
        for pkgname in l:
            pkgarch = pkgname.split('.')[-1]
            if pkgarch not in archs:
                # keep only packages matching the arch
                continue
            ll.append(pkgname)

        return ll

    def hp_parse_swlist(self, out):
        l = {}
        for line in out.split('\n'):
            if line.startswith('#') or len(line) == 0:
                continue
            v = line.split()
            if len(v) < 2:
                continue
            if v[0] in l:
                l[v[0]] += [(v[1], "")]
            else:
                l[v[0]] = [(v[1], "")]
        return l

    def hp_del_pkg(self, pkg):
        print >>sys.stderr, "TODO:", __fname__
        return RET_ERR

    def hp_fix_pkg(self, pkg):
        if pkg in self.reloc:
            pkg = ':'.join((pkg, self.reloc[pkg]))
        cmd = ['swinstall',
               '-x', 'allow_downdate=true',
               '-x', 'mount_all_filesystems=false',
               '-s', self.uri, pkg]
        print " ".join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if len(out) > 0:
            print out
        if len(err) > 0:
            print >>sys.stderr, err
        if p.returncode != 0:
            return RET_ERR
        return RET_OK

    def hp_get_installed_packages(self):
        p = Popen(['swlist', '-l', self.pkg_type], stdout=PIPE)
        (out, err) = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, 'can not fetch installed packages list'
            return []
        return self.hp_parse_swlist(out).keys()

    def get_free(self, c):
        if not os.path.exists(c):
            return 0
        cmd = ["df", "-k", c]
        p = Popen(cmd, stdout=PIPE, stderr=None)
        out, err = p.communicate()
        for line in out.split():
            if "%" in line:
                l = out.split()
                for i, w in enumerate(l):
                    if '%' in w:
                        break
                try:
                    f = int(l[i-1])
                    return f
                except:
                    return 0
        return 0

    def get_temp_dir(self):
        if hasattr(self, "tmpd"):
            return self.tmpd
        candidates = ["/tmp", "/var/tmp", "/root"]
        free = {}
        for c in candidates:
            free[self.get_free(c)] = c
        max = sorted(free.keys())[-1]
        self.tmpd = free[max]
        print "selected %s as temp dir (%d KB free)" % (self.tmpd, max)
        return self.tmpd

    def download(self, pkg_name):
        import urllib
        import tempfile
        f = tempfile.NamedTemporaryFile(dir=self.get_temp_dir())
        dname = f.name
        f.close()
        try:
            os.makedirs(dname)
        except:
            pass
        fname = os.path.join(dname, "file")
        try:
            fname, headers = urllib.urlretrieve(pkg_name, fname)
        except IOError:
            import traceback
            e = sys.exc_info()
            try:
                os.unlink(fname)
                os.unlink(dname)
            except:
                pass
            raise Exception("download failed: %s" % str(e[1]))
        if 'invalid file' in headers.values():
            try:
                os.unlink(fname)
                os.unlink(dname)
            except:
                pass
            raise Exception("invalid file")
        with open(fname, 'r') as f:
            content = f.read(500)
        if content.startswith('<') and '404' in content and 'Not Found' in content:
            try:
                os.unlink(fname)
                os.unlink(dname)
            except:
                pass
            raise Exception("not found")
        import tarfile
        os.chdir(dname)
        try:
            tar = tarfile.open(fname)
        except:
            print "not a tarball"
            return fname
        try:
            tar.extractall()
        except:
            try:
                os.unlink(fname)
                os.unlink(dname)
            except:
                pass
            # must be a pkg
            return dname
        tar.close()
        os.unlink(fname)
        return dname

    def get_os_ver(self):
        cmd = ['uname', '-v']
        p = Popen(cmd, stdout=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return 0
        lines = out.split('\n')
        if len(lines) == 0:
            return 0
        try:
            osver = float(lines[0])
        except:
            osver = 0
        return osver

    def sol_get_installed_packages(self):
        p = Popen(['pkginfo', '-l'], stdout=PIPE)
        (out, err) = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, 'can not fetch installed packages list'
            return []
        l = []
        for line in out.split('\n'):
            v = line.split(':')
            if len(v) != 2:
                continue
            f = v[0].strip()
            if f == "PKGINST":
                pkgname = v[1].strip()
                l.append(pkgname)
        return l

    def sol_del_pkg(self, pkg):
        if pkg not in self.installed_packages:
            return RET_OK
        yes = os.path.dirname(__file__) + "/yes"
        cmd = '%s | pkgrm %s' % (yes, pkg)
        print(cmd)
        r = os.system(cmd)
        if r != 0:
            return RET_ERR
        return RET_OK

    def sol_fix_pkg(self, pkg):
        data = self.data[pkg]
        if 'repo' not in data or len(data['repo']) == 0:
            print >>sys.stderr, "no repo specified in the rule"
            return RET_NA

        pkg_url = data['repo']+"/"+pkg
        print "download", pkg_url
        try:
            dname = self.download(pkg_url)
        except Exception as e:
            print >>sys.stderr, e
            return RET_ERR

        if os.path.isfile(dname):
            d = dname
        else:
            d = "."
            os.chdir(dname)

        if self.get_os_ver() < 10:
            opts = ''
        else:
            opts = '-G'
        if 'resp' in data and len(data['resp']) > 0:
            f = tempfile.NamedTemporaryFile(dir=self.get_temp_dir())
            resp = f.name
            f.close()
            with open(resp, "w") as f:
                f.write(data['resp'])
        else:
            resp = "/dev/null"
        yes = os.path.dirname(__file__) + "/yes"
        cmd = '%s | pkgadd -r %s %s -d %s all' % (yes, resp, opts, d)
        print(cmd)
        r = os.system(cmd)

        os.chdir("/")
        if os.path.isdir(dname):
            import shutil
            shutil.rmtree(dname)
        if r != 0:
            return RET_ERR
        return RET_OK

    def aix_del_pkg(self, pkg):
        cmd = ['installp', '-u', pkg]
        print " ".join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if len(out) > 0:
            print out
        if len(err) > 0:
            print >>sys.stderr, err
        if p.returncode != 0:
            return RET_ERR
        return RET_OK

    def aix_fix_pkg(self, pkg):
        cmd = ['nimclient', '-o', 'cust',
               '-a', 'lpp_source=%s'%self.uri,
               '-a', 'installp_flags=Y',
               '-a', 'filesets=%s'%pkg]
        s = " ".join(cmd)
        print s
        r = os.system(s)
        if r != 0:
            return RET_ERR
        return RET_OK

    def aix_get_installed_packages(self):
        cmd = ['lslpp', '-Lc']
        p = Popen(cmd, stdout=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, 'can not fetch installed packages list'
            return []
        pkgs = []
        for line in out.split('\n'):
            l = line.split(':')
            if len(l) < 5:
                continue
            pkgvers = l[2]
            pkgname = l[1].replace('-'+pkgvers, '')
            pkgs.append(pkgname)
        return pkgs

    def rpm_get_installed_packages(self):
        p = Popen(['rpm', '-qa', '--qf', '%{NAME}.%{ARCH}\n'], stdout=PIPE)
        (out, err) = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, 'can not fetch installed packages list'
            return []
        return out.split('\n')

    def deb_get_installed_packages(self):
        p = Popen(['dpkg', '-l'], stdout=PIPE)
        (out, err) = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, 'can not fetch installed packages list'
            return []
        l = []
        for line in out.split('\n'):
            if not line.startswith('ii'):
                continue
            pkgname = line.split()[1]
            pkgname = pkgname.split(':')[0]
            l.append(pkgname)
        return l

    def zyp_del_pkg(self, pkg):
        cmd = ['zypper', 'remove', '-y', pkg]
        print ' '.join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            if len(err) > 0:
                print err
            return RET_ERR
        return RET_OK

    def zyp_fix_pkg(self, pkg):
        cmd = ['zypper', 'install', '-y', pkg]
        print ' '.join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            if len(err) > 0:
                print err
            return RET_ERR
        return RET_OK

    def yum_del_pkg(self, pkg):
        if type(pkg) == list:
            cmd = ['yum', '-y', 'remove'] + pkg
        else:
            cmd = ['yum', '-y', 'remove', pkg]
        print ' '.join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            if len(err) > 0:
                print err
            return RET_ERR
        return RET_OK

    def yum_fix_pkg(self, pkg):
        cmd = ['yum', '-y', 'install'] + pkg
        print ' '.join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            if len(err) > 0:
                print err
            return RET_ERR
        return RET_OK

    def apt_del_pkg(self, pkg):
        r = call(['apt-get', 'remove', '-y', pkg])
        if r != 0:
            return RET_ERR
        return RET_OK

    def apt_fix_pkg(self, pkg):
        r = call(['apt-get', 'install', '--allow-unauthenticated', '-y', pkg])
        if r != 0:
            return RET_ERR
        return RET_OK

    def fixable(self):
        return RET_NA

    def fix_pkg_combo(self):
        l_add = []
        l_del = []
        for pkg in self.packages:
            if pkg.startswith('-') and len(pkg) > 1:
                l_del.append(pkg[1:])
            elif pkg.startswith('+') and len(pkg) > 1:
                l_add.append(pkg[1:])
            else:
                l_add.append(pkg)
        if len(l_add) > 0:
            r = self.pkg_add(l_add)
            if r != RET_OK:
                return r
        if len(l_del) > 0:
            r = self.pkg_del(l_del)
            if r != RET_OK:
                return r
        return RET_OK
        
    def fix_pkg(self, pkg):
        if pkg.startswith('-') and len(pkg) > 1:
            return self.pkg_del(pkg[1:])
        if pkg.startswith('+') and len(pkg) > 1:
            return self.pkg_add(pkg[1:])
        else:
            return self.pkg_add(pkg)

    def check_pkg(self, pkg, verbose=True):
        if pkg.startswith('-') and len(pkg) > 1:
            return self.check_pkg_del(pkg[1:], verbose)
        if pkg.startswith('+') and len(pkg) > 1:
            return self.check_pkg_add(pkg[1:], verbose)
        else:
            return self.check_pkg_add(pkg, verbose)

    def check_pkg_del(self, pkg, verbose=True):
        if pkg in self.installed_packages:
            if verbose:
                print >>sys.stderr, 'package', pkg, 'is installed'
            return RET_ERR
        if verbose:
            print 'package', pkg, 'is not installed'
        return RET_OK

    def check_pkg_add(self, pkg, verbose=True):
        if not pkg in self.installed_packages:
            if verbose:
                print >>sys.stderr, 'package', pkg, 'is not installed'
            return RET_ERR
        if verbose:
            print 'package', pkg, 'is installed'
        return RET_OK

    def check(self):
        r = 0
        for pkg in self.packages:
            r |= self.check_pkg(pkg)
        return r

    def fix(self):
        r = 0
        if self.combo_fix:
            return self.fix_pkg_combo()
        for pkg in self.packages:
            if self.check_pkg(pkg, verbose=False) == RET_OK:
                continue
            r |= self.fix_pkg(pkg)
        return r

if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix [uri]"""%sys.argv[0]
    if len(sys.argv) not in [3, 4]:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        argv = [sys.argv[1]]
        if len(sys.argv) == 4:
            argv.append(sys.argv[3])
        o = CompPackages(*argv)
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except ComplianceError:
        sys.exit(RET_ERR)
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

  0707010000bce8000081ed00000000000000000000000154fc7bfb00003545000000660000000300000000000000000000003700000000root/opt/opensvc/var/compliance/com.opensvc/process.py    #!/usr/bin/env /opt/opensvc/bin/python
""" 
Checks if a process is present, specifying its comm,
and optionnaly its owner's uid and/or username.

[{"command": "foo", "uid": "2345", "user": "foou"},
 {"command": "bar", "uid": "2345"},
 ...]
"""

import os
import sys
import json
import re
from subprocess import *

sys.path.append(os.path.dirname(__file__))

from comp import *
from utilities import which

class CompProcess(object):
    def __init__(self, prefix='OSVC_COMP_PROCESS_'):
        self.prefix = prefix.upper()
        self.sysname, self.nodename, x, x, self.machine = os.uname()

        if self.sysname not in ['Linux', 'AIX', 'SunOS', 'FreeBSD', 'Darwin', 'HP-UX']:
            print >>sys.stderr, 'module not supported on', self.sysname
            raise NotApplicable()

        if self.sysname == 'HP-UX' and 'UNIX95' not in os.environ:
            os.environ['UNIX95'] = ""

        self.process = []
        for k in [key for key in os.environ if key.startswith(self.prefix)]:
            try:
                self.process += json.loads(os.environ[k])
            except ValueError:
                print >>sys.stderr, 'failed to concatenate', os.environ[k], 'to process list'

        self.validate_process()

        if len(self.process) == 0:
            raise NotApplicable()

        self.load_ps()

    def subst(self, v):
        if type(v) == list:
            l = []
            for _v in v:
                l.append(self.subst(_v))
            return l
        if type(v) != str and type(v) != unicode:
            return v
        p = re.compile('%%ENV:\w+%%')
        for m in p.findall(v):
            s = m.strip("%").replace('ENV:', '')
            if s in os.environ:
                _v = os.environ[s]
            elif 'OSVC_COMP_'+s in os.environ:
                _v = os.environ['OSVC_COMP_'+s]
            else:
                print >>sys.stderr, s, 'is not an env variable'
                raise NotApplicable()
            v = v.replace(m, _v)
        return v

    def load_ps_args(self):
        self.ps_args = {}
        cmd = ['ps', '-e', '-o', 'pid,uid,user,args']
        p = Popen(cmd, stdout=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, "unable to fetch ps"
            raise ComplianceError
        lines = out.split('\n')
        if len(lines) < 2:
            return
        for line in lines[1:]:
            l = line.split()
            if len(l) < 4:
                continue
            pid, uid, user = l[:3]
            args = " ".join(l[3:])
            if args not in self.ps_args:
                self.ps_args[args] = [(pid, int(uid), user)]
            else:
                self.ps_args[args].append((pid, int(uid), user))

    def load_ps_comm(self):
        self.ps_comm = {}
        cmd = ['ps', '-e', '-o', 'comm,pid,uid,user']
        p = Popen(cmd, stdout=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, "unable to fetch ps"
            raise ComplianceError
        lines = out.split('\n')
        if len(lines) < 2:
            return
        for line in lines[1:]:
            l = line.split()
            if len(l) != 4:
                continue
            comm, pid, uid, user = l
            if comm not in self.ps_comm:
                self.ps_comm[comm] = [(pid, int(uid), user)]
            else:
                self.ps_comm[comm].append((pid, int(uid), user))

    def load_ps(self):
        self.load_ps_comm()
        self.load_ps_args()

    def validate_process(self):
        l = []
        for process in self.process:
            if self._validate_process(process) == RET_OK:
                l.append(process)
        self.process = l

    def _validate_process(self, process):
        for i in 'comm', 'uid', 'args', 'user':
            if i not in process:
                continue
            process[i] = self.subst(process[i])
        if 'comm' not in process and 'args' not in process:
            print >>sys.stderr, process, 'rule is malformed ... nor comm nor args key present'
            return RET_ERR
        if 'uid' in process and type(process['uid']) != int:
            print >>sys.stderr, process, 'rule is malformed ... uid value must be integer'
            return RET_ERR
        return RET_OK

    def get_keys_args(self, args):
        found = []
        for key in self.ps_args:
            if re.match(args, key) is not None:
                found.append(key)
        return found

    def get_keys_comm(self, comm):
        found = []
        for key in self.ps_comm:
            if re.match(comm, key) is not None:
                found.append(key)
        return found

    def check_present_args(self, args, verbose):
        if len(args.strip()) == 0:
            return RET_OK
        found = self.get_keys_args(args)
        if len(found) == 0:
            if verbose:
                print >>sys.stderr, 'process with args', args, 'is not started ... should be'
            return RET_ERR
        else:
            if verbose:
                print 'process with args', args, 'is started ... on target'
        return RET_OK

    def check_present_comm(self, comm, verbose):
        if len(comm.strip()) == 0:
            return RET_OK
        found = self.get_keys_comm(comm)
        if len(found) == 0:
            if verbose:
                print >>sys.stderr, 'process with command', comm, 'is not started ... should be'
            return RET_ERR
        else:
            if verbose:
                print 'process with command', comm, 'is started ... on target'
        return RET_OK

    def check_present(self, process, verbose):
        r = RET_OK
        if 'comm' in process:
            r |= self.check_present_comm(process['comm'], verbose)
        if 'args' in process:
            r |= self.check_present_args(process['args'], verbose)
        return r

    def check_not_present_comm(self, comm, verbose):
        if len(comm.strip()) == 0:
            return RET_OK
        found = self.get_keys_comm(comm)
        if len(found) == 0:
           if verbose:
               print 'process with command', comm, 'is not started ... on target'
           return RET_OK
        else:
           if verbose:
               print >>sys.stderr, 'process with command', comm, 'is started ... shoud be'
        return RET_ERR

    def check_not_present_args(self, args, verbose):
        if len(args.strip()) == 0:
            return RET_OK
        found = self.get_keys_args(args)
        if len(found) == 0:
           if verbose:
               print 'process with args', args, 'is not started ... on target'
           return RET_OK
        else:
           if verbose:
               print >>sys.stderr, 'process with args', args, 'is started ... shoud be'
        return RET_ERR

    def check_not_present(self, process, verbose):
        r = 0
        if 'comm' in process:
            r |= self.check_not_present_comm(process['comm'], verbose)
        if 'args' in process:
            r |= self.check_not_present_args(process['args'], verbose)
        return r

    def check_process(self, process, verbose=True):
        r = RET_OK
        if process['state'] == 'on':
            r |= self.check_present(process, verbose)
            if r == RET_ERR:
                return RET_ERR
            if 'uid' in process:
                r |= self.check_uid(process, process['uid'], verbose)
            if 'user' in process:
                r |= self.check_user(process, process['user'], verbose)
        else:
            r |= self.check_not_present(process, verbose)

        return r

    def check_uid(self, process, uid, verbose):
        if 'args' in process:
            return self.check_uid_args(process['args'], uid, verbose)
        if 'comm' in process:
            return self.check_uid_comm(process['comm'], uid, verbose)

    def check_uid_comm(self, comm, uid, verbose):
        if len(comm.strip()) == 0:
            return RET_OK
        found = False
        keys = self.get_keys_comm(comm)
        for key in keys:
            for _pid, _uid, _user in self.ps_comm[key]:
                if uid == _uid:
                    found = True
                    continue
        if found:
            if verbose:
                print 'process with command', comm, 'runs with uid', _uid, '... on target'
        else:
            if verbose:
                print >>sys.stderr, 'process with command', comm, 'does not run with uid', _uid, '... should be'
            return RET_ERR
        return RET_OK

    def check_uid_args(self, args, uid, verbose):
        if len(args.strip()) == 0:
            return RET_OK
        found = False
        keys = self.get_keys_args(args)
        for key in keys:
            for _pid, _uid, _user in self.ps_args[key]:
                if uid == _uid:
                    found = True
                    continue
        if found:
            if verbose:
                print 'process with args', args, 'runs with uid', _uid, '... on target'
        else:
            if verbose:
                print >>sys.stderr, 'process with args', args, 'does not run with uid', _uid, '... should be'
            return RET_ERR
        return RET_OK

    def check_user(self, process, user, verbose):
        if 'args' in process:
            return self.check_user_args(process['args'], user, verbose)
        if 'comm' in process:
            return self.check_user_comm(process['comm'], user, verbose)

    def check_user_comm(self, comm, user, verbose):
        if len(comm.strip()) == 0:
            return RET_OK
        if user is None or len(user) == 0:
            return RET_OK
        found = False
        keys = self.get_keys_comm(comm)
        for key in keys:
            for _pid, _uid, _user in self.ps_comm[key]:
                if user == _user:
                    found = True
                    continue
        if found:
            if verbose:
                print 'process with command', comm, 'runs with user', _user, '... on target'
        else:
            if verbose:
                print >>sys.stderr, 'process with command', comm, 'runs with user', _user, '... should run with user', user
            return RET_ERR
        return RET_OK

    def check_user_args(self, args, user, verbose):
        if len(args.strip()) == 0:
            return RET_OK
        if user is None or len(user) == 0:
            return RET_OK
        found = False
        keys = self.get_keys_args(args)
        for key in keys:
            for _pid, _uid, _user in self.ps_args[key]:
                if user == _user:
                    found = True
                    continue
        if found:
            if verbose:
                print 'process with args', args, 'runs with user', _user, '... on target'
        else:
            if verbose:
                print >>sys.stderr, 'process with args', args, 'runs with user', _user, '... should run with user', user
            return RET_ERR
        return RET_OK

    def fix_process(self, process):
        if process['state'] == 'on':
            if self.check_present(process, verbose=False) == RET_OK:
                if ('uid' in process and self.check_uid(process, process['uid'], verbose=False) == RET_ERR) or \
                   ('user' in process and self.check_user(process, process['user'], verbose=False) == RET_ERR):
                    print >>sys.stderr, process, "runs with the wrong user. can't fix."
                    return RET_ERR
                return RET_OK
        elif process['state'] == 'off':
            if self.check_not_present(process, verbose=False) == RET_OK:
                return RET_OK

        if 'start' not in process or len(process['start'].strip()) == 0:
            print >>sys.stderr, "undefined fix method for process", process['comm']
            return RET_ERR

        v = process['start'].split(' ')
        if not which(v[0]):
            print >>sys.stderr, "fix command", v[0], "is not present or not executable"
            return RET_ERR
        print 'exec:', process['start']
        try:
            p = Popen(v, stdout=PIPE, stderr=PIPE)
            out, err = p.communicate()
        except Exception as e:
            print >>sys.stderr, e
            return RET_ERR
        if len(out) > 0:
            print out
        if len(err) > 0:
            print >>sys.stderr, err
        if p.returncode != 0:
            print >>sys.stderr, "fix up command returned with error code", p.returncode
            return RET_ERR
        return RET_OK

    def check(self):
        r = 0
        for process in self.process:
            r |= self.check_process(process)
        return r

    def fix(self):
        r = 0
        for process in self.process:
            r |= self.fix_process(process)
        return r

if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = CompProcess(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

   0707010000bce9000081ed00000000000000000000000154fc7bfb00001218000000660000000300000000000000000000003200000000root/opt/opensvc/var/compliance/com.opensvc/rc.py #!/usr/bin/env /opt/opensvc/bin/python
""" 
[{"service": "foo", "level": "2345", "state": "on"},
 {"service": "foo", "level": "016", "state": "off"},
 {"service": "bar", "state": "on"},
 ...]
"""

import os
import sys
import json
import pwd
import re
from subprocess import *

sys.path.append(os.path.dirname(__file__))

from comp import *

class CompRc(object):
    def __init__(self, prefix='OSVC_COMP_RC_'):
        self.prefix = prefix.upper()
        self.sysname, self.nodename, x, x, self.machine = os.uname()

        self.services = []
        for k in [key for key in os.environ if key.startswith(self.prefix)]:
            try:
                l = json.loads(os.environ[k])
                for i, d in enumerate(l):
                    for key, val in d.items():
                        d[key] = self.subst(val)
                    l[i] = d
                self.services += l
            except ValueError:
                print >>sys.stderr, 'failed to concatenate', os.environ[k], 'to service list'

        self.validate_svcs()

        if len(self.services) == 0:
            raise NotApplicable()

        if self.sysname not in ['Linux', 'HP-UX']:
            print >>sys.stderr, __file__, 'module not supported on', self.sysname
            raise NotApplicable()

        vendor = os.environ.get('OSVC_COMP_NODES_OS_VENDOR', 'unknown')
        release = os.environ.get('OSVC_COMP_NODES_OS_RELEASE', 'unknown')
        if vendor in ['CentOS', 'Redhat', 'Red Hat'] or \
           (vendor == 'Oracle' and self.sysname == 'Linux'):

            import chkconfig
            self.o = chkconfig.Chkconfig()
        elif vendor in ['Ubuntu', 'HP']:
            import sysvinit
            self.o = sysvinit.SysVInit()
        else:
            print >>sys.stderr, vendor, "not supported"
            raise NotApplicable()

    def subst(self, v):
        if type(v) == list:
            l = []
            for _v in v:
                l.append(self.subst(_v))
            return l
        if type(v) != str and type(v) != unicode:
            return v
        p = re.compile('%%ENV:\w+%%')
        for m in p.findall(v):
            s = m.strip("%").replace('ENV:', '')
            if s in os.environ:
                _v = os.environ[s]
            elif 'OSVC_COMP_'+s in os.environ:
                _v = os.environ['OSVC_COMP_'+s]
            else:
                print >>sys.stderr, s, 'is not an env variable'
                raise NotApplicable()
            v = v.replace(m, _v)
        return v

    def validate_svcs(self):
        l = []
        for i, svc in enumerate(self.services):
            if self.validate_svc(svc) == RET_OK:
                l.append(svc)
        self.svcs = l

    def validate_svc(self, svc):
        if 'service' not in svc:
            print >>sys.stderr, svc, ' rule is malformed ... service key not present'
            return RET_ERR
        if 'state' not in svc:
            print >>sys.stderr, svc, ' rule is malformed ... state key not present'
            return RET_ERR
        return RET_OK

    def check_svc(self, svc, verbose=True):
        if 'seq' in svc:
            seq = svc['seq']
        else:
            seq = None
        return self.o.check_state(svc['service'], svc['level'], svc['state'], seq=seq, verbose=verbose)

    def fix_svc(self, svc, verbose=True):
        if 'seq' in svc:
            seq = svc['seq']
        else:
            seq = None
        if self.check_svc(svc, verbose=False) == RET_OK:
            return RET_OK
        return self.o.fix_state(svc['service'], svc['level'], svc['state'], seq=seq)

    def check(self):
        r = 0
        for svc in self.services:
            r |= self.check_svc(svc)
        return r

    def fix(self):
        r = 0
        for svc in self.services:
            r |= self.fix_svc(svc)
        return r

if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = CompRc(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

0707010000bcea000081ed00000000000000000000000154fc7bfb000033fc000000660000000300000000000000000000003700000000root/opt/opensvc/var/compliance/com.opensvc/smfcfgs.py    #!/opt/opensvc/bin/python

"""
The ENV variable format is json-serialized [list of dict]:
[
 {
  "fmri": "svc:/network/ntp"
  "prop": "config/slew_always"
  "type": "boolean"
  "value": "true"
  "inorder": 0
  "create": 1
  "reload": 0
  "sleep": 0
 }
 {
  "fmri": "svc:/network/dns/client"
  "prop": "config/nameserver"
  "type": "net_address"
  "value": "172.30.65.165 172.30.65.164"
  "inorder": 0
  "create": 1
  "reload": 0
  "sleep": 6
 }
 {
  "fmri": "svc:/network/dns/client"
  "prop": "config/search"
  "type": "astring"
  "value": "cpdev.local cpprod.root.local cpgrp.root.local"
  "inorder": 1
  "create": 1
  "reload": 0
  "sleep": 9
 }
]
"""

import os
import sys
import json
import re

from subprocess import *

sys.path.append(os.path.dirname(__file__))

from comp import *

class AutoInst(dict):
    """autovivification feature."""
    def __getitem__(self, item):
        try:
            return dict.__getitem__(self, item)
        except KeyError:
            value = self[item] = type(self)()
            return value

class SmfCfgS(object):
    def __init__(self, prefix='OSVC_COMP_SMF_CFGS_'):
        self.prefix = prefix.upper()
        self.sysname, self.nodename, self.osn, self.solv, self.machine = os.uname()
        self.data = []
        self.smfs = AutoInst()
        self.osver = float(self.osn)

        if self.osver < 5.11:
            print 'Only used on Solaris 11 and behond'
            return

        if "OSVC_COMP_SERVICES_SVC_NAME" not in os.environ:
            os.environ["OSVC_COMP_SERVICES_SVC_NAME"] = ""

        for k in [ key for key in os.environ if key.startswith(self.prefix)]:
            try:
                self.data += self.add_fmri(os.environ[k])
            except ValueError:
                print >>sys.stderr, 'failed to parse variable', os.environ[k]

        for f in self.data:
            s,p,t,v = self.get_fmri(f['fmri'], f['prop'])
            if s is None:
                continue
            cre = False
            if p is None:
                if f['create'] == 0:
                    print >>sys.stderr, 'FMRI:%s, PROP:%s is absent and create is False' %(s,f['prop'])
                    continue
                else:
                    p = f['prop']
                    cre = True
            if f['inorder'] == 0:
                ino = False
            else:
                ino = True
            if f['reload'] == 0:
                rel = False
            else:
                rel = True
            
            self.smfs[f['fmri']][p] = { 'val': f['value'], 'rval': v,
                                        'typ': f['type'] , 'rtyp': t,
                                        'ino': ino,
                                        'cre': cre,
                                        'rel': rel,
                                        'slp': f['sleep']
                                      }

    def subst(self, v):
        if type(v) == list:
            l = []
            for _v in v:
                l.append(self.subst(_v))
            return l
        if type(v) != str and type(v) != unicode:
            return v
	p = re.compile('%%ENV:\w+%%')
        for m in p.findall(v):
            s = m.strip("%").replace('ENV:', '')
            if s in os.environ:
                _v = os.environ[s]
            elif 'OSVC_COMP_'+s in os.environ:
                _v = os.environ['OSVC_COMP_'+s]
            else:
                print >>sys.stderr, s, 'is not an env variable'
                raise NotApplicable()
            v = v.replace(m, _v)
        return v

    def add_fmri(self, v):
        if type(v) == str or type(v) == unicode:
            d = json.loads(v)
        else:
            d = v
        l = []

        # recurse if multiple FMRI are specified in a list of dict
        if type(d) == list:
            for _d in d:
                l += self.add_fmri(_d)
            return l

        if type(d) != dict:
            print >>sys.stderr, "not a dict:", d
            return l

        if 'fmri' not in d:
            print >>sys.stderr, 'FMRI should be in the dict:', d
            RET = RET_ERR
            return l
        if 'prop' not in d:
            print >>sys.stderr, 'prop should be in the dict:', d
            RET = RET_ERR
            return l
        if 'value' not in d:
            print >>sys.stderr, 'value should be in the dict:', d
            RET = RET_ERR
            return l
        if 'create' in d:
            if d['create'] == 1:
                if not 'type' in d:
                    print >>sys.stderr, 'create True[1] needs a type:', d
                    RET = RET_ERR
                    return l
        for k in ('fmri', 'prop', 'value', 'inorder', 'type', 'create', 'sleep'):
            if k in d:
                d[k] = self.subst(d[k])
        return [d]
            
    def fixable(self):
        return RET_NA

    def get_fmri(self, s, p):
        cmd = ['/usr/sbin/svccfg','-s', s, 'listprop', p]
        po = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out,err = po.communicate()
        if po.returncode != 0:
            if "doesn't match" in err:
                print '%s is absent => IGNORED' %self.service
                return None,None,None,None
            else:
                print >>sys.stderr, ' '.join(cmd)
                raise ComplianceError()
        if len(out) < 2:
                return s,None,None,None

        x = out.strip('\n').split()
        if x[0] != p:
            print >>sys.stderr, ' '.join([s, 'wanted:%s'%p, 'got:%s'%x[0]])
            raise ComplianceError()
        return s,p,x[1],x[2:]

    def check_smf_prop_cre(self, s, p, verbose=True):
        r = RET_OK
        if self.smfs[s][p]['cre']:
            if verbose:
                print >>sys.stderr, 'NOK: %s Prop %s shall be created' %(s,p)
            r |= RET_ERR
            if self.smfs[s][p]['typ'] == '' or self.smfs[s][p]['typ'] == None:
                if verbose:
                    print >>sys.stderr, 'NOK: %s type must be specified to create %s' %(s,p)
        return r,self.smfs[s][p]['cre']

    def check_smf_prop_typ(self, s, p, verbose=True):
        r = RET_OK
        if self.smfs[s][p]['typ'] == '' or self.smfs[s][p]['typ'] == None:
            if verbose:
                print '%s Prop %s type is not checked' %(s,p)
        elif self.smfs[s][p]['typ'] != self.smfs[s][p]['rtyp']:
            if verbose:
                print >>sys.stderr, 'NOK: %s Prop %s type Do Not match, got:%s, expected:%s' %(s,p,self.smfs[s][p]['rtyp'],self.smfs[s][p]['typ'])
            r |= RET_ERR
        else:
            if verbose:
                print '%s Prop %s type %s is OK' %(s,p,self.smfs[s][p]['typ'])
            if self.smfs[s][p]['typ'] == '' or self.smfs[s][p]['typ'] == None:
                if verbose:
                    print >>sys.stderr, 'NOK: %s type must be specified to create %s' %(s,p)
        return r

    def check_smf_prop_val(self, s, p, verbose=True):
        r = RET_OK
        rvs = ' '.join(self.smfs[s][p]['rval'])
        if self.smfs[s][p]['ino']:
            if self.smfs[s][p]['val'] == rvs:
                if verbose:
                    print '%s Prop %s values match in right order [%s]' %(s,p,rvs)
            else:
                if verbose:
                    print >>sys.stderr, 'NOK: %s Prop %s values Do Not match, got:[%s], expected:[%s]' %(s,p,rvs,self.smfs[s][p]['val'])
                r |= RET_ERR
        else:
            vv = self.smfs[s][p]['val'].split()
            m = True
            for v in vv:
                if not v in self.smfs[s][p]['rval']:
                    if verbose and len(self.smfs[s][p]['rval']) > 1 :
                        print >>sys.stderr, '%s Prop %s notfound %s' %(s,p,v)
                    m = False
                else:
                    if verbose and len(self.smfs[s][p]['rval']) > 1 :
                        print '%s Prop %s found %s' %(s,p,v)
            if m:
                if verbose:
                    print '%s Prop %s values match [%s]' %(s,p,rvs)
            else:
                if verbose:
                    print >>sys.stderr, 'NOK: %s Prop %s values Do Not match, got:[%s], expected:[%s]' %(s,p,rvs,self.smfs[s][p]['val'])
                r |= RET_ERR
        return r

    def check_smfs(self, verbose=True):
        r = RET_OK
        for s in self.smfs:
            for p in self.smfs[s]:
                """
                print 'FMRI: ', s, 'PROP: ', p, 'TYP: ', self.smfs[s][p]['typ'], 'RTYP: ', self.smfs[s][p]['rtyp'], type(self.smfs[s][p]['val']), type(self.smfs[s][p]['rval'])
                print '	', 'VALS: ', self.smfs[s][p]['val']
                print '	', 'RVALS: ', self.smfs[s][p]['rval']
                """
                rx,c = self.check_smf_prop_cre(s, p, verbose=verbose)
                r |= rx
                if not c:
                    r |= self.check_smf_prop_typ(s, p, verbose=verbose)
                r |= self.check_smf_prop_val(s, p, verbose=verbose)
        return r

    def fix_smfs(self, verbose=False):
        r = RET_OK
        cmds = []
        for s in self.smfs:
            for p in self.smfs[s]:
                added = False
                rx,c = self.check_smf_prop_cre(s, p, verbose=verbose)
                vx = self.smfs[s][p]['val'].split()
                if c:
                   if rx == 0 :
                       print '%s try to add %s %s: = %s' %(s,p,self.smfs[s][p]['typ'],self.smfs[s][p]['val'])
                       if len(vx) > 1:
                           sxok = True
                           for v in vx:
                               if not (v.startswith('"') and v.endswith('"')):
                                    """
                                    sxok = False
                                    break
                                    """
                           if sxok:
                                cmds.append(['/usr/sbin/svccfg', '-s', s, 'setprop', p, '=', self.smfs[s][p]['typ']+':', '(%s)'%self.smfs[s][p]['val']])
                                added = True
                           else:
                                print >>sys.stderr, 'NOK: %s prop %s values must be within double quotes [%s]' %(s,p,self.smfs[s][p]['val'])
                                r |= RET_ERR
                       else:
                           cmds.append(['/usr/sbin/svccfg', '-s', s, 'setprop', p, '=', self.smfs[s][p]['typ']+':', self.smfs[s][p]['val']])
                           added = True
                   else:
                       print >>sys.stderr, 'NOK: %s cannot add prop %s without a valid type' %(s,p)
                       r |= RET_ERR 
                else:
                   ry = self.check_smf_prop_val(s, p, verbose=verbose)
                   if ry != 0:
                       print '%s try to fix %s = %s' %(s,p,self.smfs[s][p]['val'])
                       if len(vx) > 1:
                           sxok = True
                           for v in vx:
                               if not (v.startswith('"') and v.endswith('"')):
                                    """
                                    sxok = False
                                    break
                                    """
                           if sxok:
                                cmds.append(['/usr/sbin/svccfg', '-s', s, 'setprop', p, '=', '(%s)'%self.smfs[s][p]['val']])
                                added = True
                           else:
                                print >>sys.stderr, 'NOK: %s prop %s values must be within double quotes [%s]' %(s,p,self.smfs[s][p]['val'])
                                r |= RET_ERR
                       else:
                           cmds.append(['/usr/sbin/svccfg', '-s', s, 'setprop', p, '=', self.smfs[s][p]['val']])
                           added = True
                if added:
                   if self.smfs[s][p]['rel']:
                       cmds.append(['/usr/sbin/svcadm', 'refresh' ,s])
                       if self.smfs[s][p]['slp'] != 0:
                           cmds.append(['/usr/bin/sleep' , '%d'%self.smfs[s][p]['slp']])
        for cmd in cmds:
            print 'EXEC:', ' '.join(cmd)
            p = Popen(cmd, stdout=PIPE, stderr=PIPE)
            out,err = p.communicate()
            if p.returncode != 0:
               print >>sys.stderr, 'Code=%s %s' %(p.returncode,err)
               r |= RET_ERR
        return r

    def check(self):
        if self.osver < 5.11:
            return RET_NA
        r = self.check_smfs()
        return r

    def fix(self):
        if self.osver < 5.11:
            return RET_NA
        r = self.fix_smfs()
        return r

if __name__ == "__main__":
    syntax = """syntax:
      %s check|fixable|fix]"""%sys.argv[0]
    try:
        action = sys.argv[1]
        o = SmfCfgS()
        if action == 'check':
            RET = o.check()
        elif action == 'fix':
            RET = o.fix()
        elif action == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

0707010000bceb000081ed00000000000000000000000154fc7bfb0000098b000000660000000300000000000000000000003700000000root/opt/opensvc/var/compliance/com.opensvc/sudoers.py    #!/opt/opensvc/bin/python
""" 
Same as files compliance object, but verifies the sudoers
declaration syntax using visudo in check mode.

The variable format is json-serialized:

{
  "path": "/some/path/to/file",
  "fmt": "root@corp.com		%%HOSTNAME%%@corp.com",
  "uid": 500,
  "gid": 500,
}

Wildcards:
%%ENV:VARNAME%%		Any environment variable value
%%HOSTNAME%%		Hostname
%%SHORT_HOSTNAME%%	Short hostname

"""

import os
import sys
from subprocess import *

sys.path.append(os.path.dirname(__file__))

from comp import *
from files import CompFiles

class CompSudoers(CompFiles):
    def check_file_syntax(self, f, verbose=False):
        cmd = ['visudo', '-c', '-f', '-']
        p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate(input=f['fmt'])
        if p.returncode != 0:
            if verbose:
                print >>sys.stderr, "target sudoers rules syntax error."
            else:
                print >>sys.stderr, "target sudoers rules syntax error. abort installation."
        return p.returncode

    def check(self):
        r = 0
        for f in self.files:
            r |= self.check_file_syntax(f, verbose=True)
            r |= self.check_file(f, verbose=True)
        return r

    def fix(self):
        r = 0
        for f in self.files:
            if self.check_file_syntax(f):
                r |= 1
                # refuse to install a corrupted sudoers file
                continue
            r |= self.fix_file_fmt(f)
            r |= self.fix_file_mode(f)
            r |= self.fix_file_owner(f)
        return r


if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = CompSudoers(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except ComplianceError:
        sys.exit(RET_ERR)
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

 0707010000bcec000081ed00000000000000000000000154fc7bfb00002a90000000660000000300000000000000000000003700000000root/opt/opensvc/var/compliance/com.opensvc/svcconf.py    #!/opt/opensvc/bin/python

"""
OSVC_COMP_SERVICES_SVC_NAME=app2.prd OSVC_COMP_SVCCONF_APP2_PRD='[{"value": "fd5373b3d938", "key": "container#1.run_image", "op": "="}, {"value": "/bin/sh", "key": "container#1.run_command", "op": "="}, {"value": "/opt/%%ENV:SERVICES_SVC_NAME%%", "key": "DEFAULT.docker_data_dir", "op": "="}, {"value": "no", "key": "container(type=docker).disable", "op": "="}, {"value": 123, "key": "container(type=docker&&run_command=/bin/sh).newvar", "op": "="}]' ./svcconf.py OSVC_COMP_SVCCONF check
"""

import os
import sys
import json
import re
import copy
from subprocess import *

sys.path.append(os.path.dirname(__file__))

from comp import *

class SvcConf(object):
    def __init__(self, prefix='OSVC_COMP_SVCCONF_'):
        self.prefix = prefix.upper()
        self.keys = []

        if "OSVC_COMP_SERVICES_SVC_NAME" not in os.environ:
            print "SERVICES_SVC_NAME is not set"
            raise NotApplicable()

        self.svcname = os.environ['OSVC_COMP_SERVICES_SVC_NAME']

        for k in [ key for key in os.environ if key.startswith(self.prefix)]:
            try:
                self.keys += json.loads(os.environ[k])
            except ValueError:
                print >>sys.stderr, 'key syntax error on var[', k, '] = ',os.environ[k]

        if len(self.keys) == 0:
            raise NotApplicable()

        for key in self.keys:
            if "value" in key:
                key['value'] = self.subst(key['value'])

        try:
            self.get_env_file(refresh=True)
        except Exception as e:
            print >>sys.stderr, "unable to load service configuration: %s", str(e)
            raise ComplianceError()

        self.sanitize_keys()
        self.expand_keys()

    def get_env_file(self, refresh=False):
       if not refresh:
           return self.svcenv
       cmd = ['/opt/opensvc/bin/svcmgr', '-s', self.svcname, 'json_env']
       p = Popen(cmd, stdout=PIPE, stderr=PIPE)
       out, err = p.communicate()
       for line in out.split('\n'):
           if line.startswith('{'):
               out = line
               break
       self.svcenv = json.loads(out)
       return self.svcenv

    def subst(self, v):
        if type(v) == list:
            l = []
            for _v in v:
                l.append(self.subst(_v))
            return l
        if type(v) != str and type(v) != unicode:
            return v

        p = re.compile('%%ENV:\w+%%')
        for m in p.findall(v):
            s = m.strip("%").replace('ENV:', '')
            if s in os.environ:
                _v = os.environ[s]
            elif 'OSVC_COMP_'+s in os.environ:
                _v = os.environ['OSVC_COMP_'+s]
            else:
                print >>sys.stderr, s, 'is not an env variable'
                raise NotApplicable()
            v = v.replace(m, _v)
        return v

    def fixable(self):
        return RET_NA

    def set_val(self, keyname, target):
        if type(target) == int:
            target = str(target)
        cmd = ['/opt/opensvc/bin/svcmgr', '-s', self.svcname, 'set', '--param', keyname, '--value', target]
        print ' '.join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        return p.returncode

    def get_val(self, keyname):
        section, var = keyname.split('.')
        if section not in self.svcenv:
            return None
        return self.svcenv[section].get(var)

    def _check_key(self, keyname, target, op, value, verbose=True):
        r = RET_OK
        if value is None:
            if verbose:
                print >>sys.stderr, "%s not set"%keyname
            r |= RET_ERR
        if op == '=':
            if str(value) != str(target):
                if verbose:
                    print >>sys.stderr, "%s=%s, target: %s"%(keyname, str(value), str(target))
                r |= RET_ERR
            elif verbose:
                print "%s=%s on target"%(keyname, str(value))
        else:
            if type(value) != int:
                if verbose:
                    print >>sys.stderr, "%s=%s value must be integer"%(keyname, str(value))
                r |= RET_ERR
            elif op == '<=' and value > target:
                if verbose:
                    print >>sys.stderr, "%s=%s target: <= %s"%(keyname, str(value), str(target))
                r |= RET_ERR
            elif op == '>=' and value < target:
                if verbose:
                    print >>sys.stderr, "%s=%s target: >= %s"%(keyname, str(value), str(target))
                r |= RET_ERR
            elif verbose:
                print "%s=%s on target"%(keyname, str(value))
        return r

    def check_filter(self, section, filter):
        op = None
        i = 0
        try:
            i = filter.index("&&")
            op = "and"
        except ValueError:
            pass
        try:
            i = filter.index("||")
            op = "or"
        except ValueError:
            pass

        if i == 0:
            _filter = filter
            _tail = ""
        else:
            _filter = filter[:i]
            _tail = filter[i:].lstrip("&&").lstrip("||")

        r = self._check_filter(section, _filter)
        #print " _check_filter('%s', '%s') => %s" % (section, _filter, str(r))

        if op == "and":
            r &= self.check_filter(section, _tail)
        elif op == "or":
            r |= self.check_filter(section, _tail)

        return r

    def _check_filter(self, section, filter):
        if "~=" in filter:
            return self._check_filter_reg(section, filter)
        elif "=" in filter:
            return self._check_filter_eq(section, filter)
        print >>sys.stderr, "invalid filter syntax: %s" % filter
        return False

    def _check_filter_eq(self, section, filter):
        l = filter.split("=")
        if len(l) != 2:
            print >>sys.stderr, "invalid filter syntax: %s" % filter
            return False
        key, val = l
        cur_val = self.svcenv[section].get(key)
        if cur_val is None:
            return False
        if str(cur_val) == str(val):
            return True
        return False

    def _check_filter_reg(self, section, filter):
        l = filter.split("~=")
        if len(l) != 2:
            print >>sys.stderr, "invalid filter syntax: %s" % filter
            return False
        key, val = l
        val = val.strip("/")
        cur_val = self.svcenv[section].get(key)
        if cur_val is None:
            return False
        reg = re.compile(val)
        if reg.match(cur_val):
            return True
        return False

    def resolve_sections(self, s, filter):
        """
        s is a ressource section name (fs, container, app, sync, ...)
        filter is a regexp like expression
           container(type=docker)
           fs(mnt~=/.*tools/)
           container(type=docker&&run_image~=/opensvc\/collector_web:build.*/)
           fs(mnt~=/.*tools/||mnt~=/.*moteurs/)
        """
        result = [];
        eligiblesections = [];
        for section in self.svcenv.keys():
            if section.startswith(s+'#') or section == s:
                eligiblesections.append(section)
        for section in eligiblesections:
            if self.check_filter(section, filter):
                #print "   =>", section, "matches filter"
                result.append(section)
        result.sort()
        return result

    def sanitize_keys(self, verbose=True):
        r = RET_OK
        for key in self.keys:
            if 'key' not in key:
                if verbose:
                    print >>sys.stderr, "'key' not set in rule %s"%str(key)
                r |= RET_NA
            if 'value' not in key:
                if verbose:
                    print >>sys.stderr, "'value' not set in rule %s"%str(key)
                r |= RET_NA
            if 'op' not in key:
                op = "="
            else:
                op = key['op']

            if op not in ('>=', '<=', '='):
                if verbose:
                    print >>sys.stderr, "'value' list member 0 must be either '=', '>=' or '<=': %s"%str(key)
                r |= RET_NA

        if r is not RET_OK:
            sys.exit(r)

    def expand_keys(self):
        expanded_keys = []

        for key in self.keys:
            keyname = key['key']
            target = key['value']
            op = key['op']
            sectionlist = [];
            reg1 = re.compile(r'(.*)\((.*)\)\.(.*)')
            reg2 = re.compile(r'(.*)\.(.*)')
            m = reg1.search(keyname)
            if m:
                section = m.group(1)
                filter = m.group(2)
                var = m.group(3)
                sectionlist = self.resolve_sections(section, filter)
                for resolvedsection in sectionlist:
                    newdict = {
                     'key': '.'.join([resolvedsection, var]),
                     'op': op,
                     'value': target
                    }
                    expanded_keys.append(newdict)
                continue
            m = reg2.search(keyname)
            if m:
                section = m.group(1)
                var = m.group(2)
                expanded_keys.append(copy.copy(key))
                continue

            # drop key

        self.keys = expanded_keys

    def check_key(self, key, verbose=True):
        op = key['op']
        target = key['value']
        keyname = key['key']

        value = self.get_val(keyname)

        if value is None:
            if verbose:
                print >>sys.stderr, "%s key is not set"%keyname
            return RET_ERR

        return self._check_key(keyname, target, op, value, verbose)

    def fix_key(self, key):
        return self.set_val(key['key'], key['value'])

    def check(self):
        r = 0
        for key in self.keys:
            r |= self.check_key(key, verbose=True)
        return r

    def fix(self):
        r = 0
        for key in self.keys:
            if self.check_key(key, verbose=False) == RET_ERR:
                r += self.fix_key(key)
        return r

if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = SvcConf(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

0707010000bced000081ed00000000000000000000000154fc7bfb00001efe000000660000000300000000000000000000003600000000root/opt/opensvc/var/compliance/com.opensvc/sysctl.py #!/opt/opensvc/bin/python
""" 
module use OSVC_COMP_SYSCTL_... vars
"""

import os
import sys
import json
import pwd
from subprocess import *

sys.path.append(os.path.dirname(__file__))

from comp import *

class Sysctl(object):
    def __init__(self, prefix='OSVC_COMP_SYSCTL_'):
        self.prefix = prefix.upper()
        self.need_reload = False
        self.cf = os.path.join(os.sep, "etc", "sysctl.conf")
        if not os.path.exists(self.cf):
            print >>sys.stderr, self.cf, 'does not exist'
            raise NotApplicable()

        self.keys = []
        self.cache = None

        for k in [ key for key in os.environ if key.startswith(self.prefix)]:
            try:
                self.keys += json.loads(os.environ[k])
            except ValueError:
                print >>sys.stderr, 'sysctl key syntax error on var[', k, '] = ',os.environ[k]

        if len(self.keys) == 0:
            raise NotApplicable()

        self.convert_keys()


    def fixable(self):
        return RET_OK

    def parse_val(self, val):
        val = map(lambda x: x.strip(), val.strip().split())
        for i, e in enumerate(val):
            try:
                val[i] = int(e)
            except:
                pass
        return val

    def get_keys(self):
        with open(self.cf, 'r') as f:
            buff = f.read()

        if self.cache is None:
            self.cache = {}

        for line in buff.split('\n'):
            line = line.strip()
            if line.startswith('#'):
                continue
            l = line.split('=')
            if len(l) != 2:
                continue
            key = l[0].strip()
            val = self.parse_val(l[1])
            self.cache[key] = val

    def get_live_key(self, key):
        p = Popen(['sysctl', key], stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return None
        l = out.split('=')
        if len(l) != 2:
            return None
        val = self.parse_val(l[1])
        return val
 
    def get_key(self, key):
        if self.cache is None:
            self.get_keys()
        if key not in self.cache:
            return None
        return self.cache[key]

    def fix_key(self, key):
        done = False
        target = key['value']
        index = key['index']

        with open(self.cf, 'r') as f:
            buff = f.read()

        lines = buff.split('\n')
        for i, line in enumerate(lines):
            line = line.strip()
            if line.startswith('#'):
                continue
            l = line.split('=')
            if len(l) != 2:
                continue
            keyname = l[0].strip()
            if key['key'] != keyname:
                continue
            if done:
                print "sysctl: remove redundant key %s"%keyname
                del lines[i]
                continue
            val = self.parse_val(l[1])
            if target == val[index]:
                continue
            print "sysctl: set %s[%d] = %s"%(keyname, index, str(target))
            val[index] = target
            lines[i] = "%s = %s"%(keyname, " ".join(map(str, val)))
            done = True

        if not done:
            # if key is not in sysctl.conf, get the value from kernel
            val = self.get_live_key(key['key'])
            if target != val[index]:
                val[index] = target
            print "sysctl: set %s = %s"%(key['key'], " ".join(map(str, val)))
            lines += ["%s = %s"%(key['key'], " ".join(map(str, val)))]

        try:
            with open(self.cf, 'w') as f:
                f.write('\n'.join(lines))
        except:
            print >>sys.stderr, "failed to write sysctl.conf"
            return RET_ERR

        return RET_OK

    def convert_keys(self):
        keys = []
        for key in self.keys:
            keyname = key['key']
            value = key['value']
            if type(value) == list:
                if len(value) > 0 and type(value[0]) != list:
                    value = [value]
                for i, v in enumerate(value):
                    keys.append({
                      "key": keyname,
                      "index": i,
                      "op": v[0],
                      "value": v[1],
                    })
            elif 'key' in key and 'index' in key and 'op' in key and 'value' in key:
               keys.append(key)

        self.keys = keys

    def check_key(self, key, verbose=False):
        r = RET_OK
        keyname = key['key']
        target = key['value']
        op = key['op']
        i = key['index']
        current_value = self.get_key(keyname)
        current_live_value = self.get_live_key(keyname)

        if current_value is None:
            if verbose:
                print >>sys.stderr, "key '%s' not found in sysctl.conf"%keyname
            return RET_ERR

        if op == "=" and str(current_value[i]) != str(target):
            if verbose:
                print >>sys.stderr, "sysctl err: %s[%d] = %s, target: %s"%(keyname, i, str(current_value[i]), str(target))
            r |= RET_ERR
        elif op == ">=" and type(target) == int and current_value[i] < target:
            if verbose:
                print >>sys.stderr, "sysctl err: %s[%d] = %s, target: >= %s"%(keyname, i, str(current_value[i]), str(target))
            r |= RET_ERR
        elif op == "<=" and type(target) == int and current_value[i] > target:
            if verbose:
                print >>sys.stderr, "sysctl err: %s[%d] = %s, target: <= %s"%(keyname, i, str(current_value[i]), str(target))
            r |= RET_ERR
        else:
            if verbose:
                print "sysctl ok: %s[%d] = %s, on target"%(keyname, i, str(current_value[i]))

        if r == RET_OK and current_live_value is not None and current_value != current_live_value:
            if verbose:
                print >>sys.stderr, "sysctl err: %s on target in sysctl.conf but kernel value is different"%(keyname)
            self.need_reload = True
            r |= RET_ERR

        return r

    def check(self):
        r = 0
        for key in self.keys:
            r |= self.check_key(key, verbose=True)
        return r

    def reload_sysctl(self):
        cmd = ['sysctl', '-e', '-p']
        print "sysctl:", " ".join(cmd)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, "reload failed"
            return RET_ERR
        return RET_OK

    def fix(self):
        r = 0
        for key in self.keys:
            if self.check_key(key, verbose=False) == RET_ERR:
                self.need_reload = True
                r |= self.fix_key(key)
        if self.need_reload:
            r |= self.reload_sysctl()
        return r

if __name__ == "__main__":
    """ test: OSVC_COMP_TEST_SYSCTL_1='[{"index": 0, "value": 0, "key": "vm.laptop_mode", "op": "="}, {"index": 0, "value": 0, "key": "kernel.ctrl-alt-del", "op": "="}, {"index": 0, "value": 4, "key": "kernel.printk", "op": "="}, {"index": 3, "value": 5, "key": "kernel.printk", "op": "="}]' ./sysctl.py OSVC_COMP_TEST_SYSCTL check
    """
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = Sysctl(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

  0707010000bcf1000081ed00000000000000000000000154fc7bfb00005b6c000000660000000300000000000000000000003400000000root/opt/opensvc/var/compliance/com.opensvc/vuln.py   #!/usr/bin/env /opt/opensvc/bin/python
""" 
OSVC_COMP_VULN_11_117=\
[{"pkgname": "kernel", "minver": "2.6.18-238.19.1.el5", "firstver": "2.6.18-238"},\
 {"pkgname": "kernel-xen", "minver": "2.6.18-238.19.1.el5"}]
"""

import os
import sys
import json
import pwd
import sys
import re
import tempfile
from subprocess import *
from distutils.version import LooseVersion as V
from utilities import which


sys.path.append(os.path.dirname(__file__))

from comp import *

def repl(matchobj):
    return '.0'+matchobj.group(0)[1:]

class LiveKernVulnerable(Exception):
    pass

class CompVuln(object):
    def __init__(self, prefix='OSVC_COMP_VULN_', uri=None):
        self.uri = uri
        self.prefix = prefix.upper()
        self.highest_avail_version = "0"
        self.fix_list = []
        self.need_pushpkg = False
        self.sysname, self.nodename, x, x, self.machine = os.uname()

        if 'OSVC_COMP_VULN_STRICT' in os.environ and \
           os.environ['OSVC_COMP_VULN_STRICT'] == "true":
            self.strict = True
        else:
            self.strict = False

        if 'OSVC_COMP_VULN_PKG_TYPE' in os.environ and \
           os.environ['OSVC_COMP_VULN_PKG_TYPE'] == "bundle":
            self.pkg_type = 'bundle'
        else:
            self.pkg_type = 'product'

        self.packages = []
        for k in [key for key in os.environ if key.startswith(self.prefix)]:
            try:
                o = json.loads(self.subst(os.environ[k]))
            except ValueError:
                print >>sys.stderr, 'failed to concatenate', os.environ[k], 'to rules list'
                continue
            if type(o) != list:
                print >>sys.stderr, 'failed to concatenate', os.environ[k], 'to rules list'
                continue
            for i, d in enumerate(o):
                o[i]["rule"] = k.replace("OSVC_COMP_", "")
            self.packages += o

        if len(self.packages) == 0:
            raise NotApplicable()

        if self.sysname not in ['Linux', 'HP-UX', 'AIX', 'SunOS']:
            print >>sys.stderr, 'module not supported on', self.sysname
            raise NotApplicable()

        if 'OSVC_COMP_NODES_OS_VENDOR' not in os.environ:
            print >>sys.stderr, "OS_VENDOR is not set. Check your asset"
            raise NotApplicable()

        vendor = os.environ['OSVC_COMP_NODES_OS_VENDOR']
        if vendor in ['Debian', 'Ubuntu']:
            self.get_installed_packages = self.deb_get_installed_packages
            self.fix_pkg = self.apt_fix_pkg
            self.fixable_pkg = self.apt_fixable_pkg
            self.fix_all = None
        elif vendor in ['CentOS', 'Redhat', 'Red Hat'] or \
             (vendor == 'Oracle' and self.sysname == 'Linux'):
            self.get_installed_packages = self.rpm_get_installed_packages
            self.fix_pkg = self.yum_fix_pkg
            self.fixable_pkg = self.yum_fixable_pkg
            self.fix_all = None
        elif vendor in ['SuSE']:
            self.get_installed_packages = self.rpm_get_installed_packages
            self.fix_pkg = self.zyp_fix_pkg
            self.fixable_pkg = self.zyp_fixable_pkg
            self.fix_all = None
        elif vendor in ['HP']:
            if self.uri is None:
                print >>sys.stderr, "URI is not set"
                raise NotApplicable()
            self.get_installed_packages = self.hp_get_installed_packages
            self.fix_pkg = self.hp_fix_pkg
            self.fixable_pkg = self.hp_fixable_pkg
            self.fix_all = self.hp_fix_all
        elif vendor in ['IBM']:
            self.get_installed_packages = self.aix_get_installed_packages
            self.fix_pkg = self.aix_fix_pkg
            self.fixable_pkg = self.aix_fixable_pkg
            self.fix_all = None
        elif vendor in ['Oracle']:
            self.get_installed_packages = self.sol_get_installed_packages
            self.fix_pkg = self.sol_fix_pkg
            self.fixable_pkg = self.sol_fixable_pkg
            self.fix_all = None
        else:
            print >>sys.stderr, vendor, "not supported"
            raise NotApplicable()

        self.installed_packages = self.get_installed_packages()

    def subst(self, v):
        if type(v) == list:
            l = []
            for _v in v:
                l.append(self.subst(_v))
            return l
        if type(v) != str and type(v) != unicode:
            return v

        p = re.compile('%%ENV:\w+%%')
        for m in p.findall(v):
            s = m.strip("%").replace('ENV:', '')
            if s in os.environ:
                _v = os.environ[s]
            elif 'OSVC_COMP_'+s in os.environ:
                _v = os.environ['OSVC_COMP_'+s]
            else:
                print >>sys.stderr, s, 'is not an env variable'
                raise NotApplicable()
            v = v.replace(m, _v)
        return v.strip()

    def get_free(self, c):
        if not os.path.exists(c):
            return 0
        cmd = ["df", "-k", c]
        p = Popen(cmd, stdout=PIPE, stderr=None)
        out, err = p.communicate()
        for line in out.split():
            if "%" in line:
                l = out.split()
                for i, w in enumerate(l):
                    if '%' in w:
                        break
                try:
                    f = int(l[i-1])
                    return f
                except:
                    return 0
        return 0

    def get_temp_dir(self):
        if hasattr(self, "tmpd"):
            return self.tmpd
        candidates = ["/tmp", "/var/tmp", "/root"]
        free = {}
        for c in candidates:
            free[self.get_free(c)] = c
        max = sorted(free.keys())[-1]
        self.tmpd = free[max]
        print "selected %s as temp dir (%d KB free)" % (self.tmpd, max)
        return self.tmpd

    def download(self, pkg_name):
        import urllib
        import tempfile
        f = tempfile.NamedTemporaryFile(dir=self.get_temp_dir())
        dname = f.name
        f.close()
        try:
            os.makedirs(dname)
        except:
            pass
        fname = os.path.join(dname, "file")
        try:
            fname, headers = urllib.urlretrieve(pkg_name, fname)
        except IOError:
            import traceback
            e = sys.exc_info()
            try:
                os.unlink(fname)
                os.unlink(dname)
            except:
                pass
            raise Exception("download failed: %s" % str(e[1]))
        if 'invalid file' in headers.values():
            try:
                os.unlink(fname)
                os.unlink(dname)
            except:
                pass
            raise Exception("invalid file")
        with open(fname, 'r') as f:
            content = f.read(500)
        if content.startswith('<') and '404' in content and 'Not Found' in content:
            try:
                os.unlink(fname)
                os.unlink(dname)
            except:
                pass
            raise Exception("not found")
        import tarfile
        os.chdir(dname)
        try:
            tar = tarfile.open(fname)
        except:
            print "not a tarball"
            return fname
        try:
            tar.extractall()
        except:
            try:
                os.unlink(fname)
                os.unlink(dname)
            except:
                pass
            # must be a pkg
            return dname
        tar.close()
        os.unlink(fname)
        return dname

    def get_os_ver(self):
        cmd = ['uname', '-v']
        p = Popen(cmd, stdout=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return 0
        lines = out.split('\n')
        if len(lines) == 0:
            return 0
        try:
            osver = float(lines[0])
        except:
            osver = 0
        return osver

    def sol_fix_pkg(self, pkg):
        r = self.check_pkg(pkg)
        if r == RET_OK:
            return RET_NA

        if 'repo' not in pkg or len(pkg['repo']) == 0:
            print >>sys.stderr, "no repo specified in the rule"
            return RET_NA

        pkg_url = pkg['repo']+"/"+pkg['pkgname']
        print "download", pkg_url
        try:
            dname = self.download(pkg_url)
        except Exception as e:
            print >>sys.stderr, e
            return RET_ERR

        if pkg["pkgname"] in self.installed_packages:
            os.chdir("/")
            yes = os.path.dirname(__file__) + "/yes"
            cmd = '%s | pkgrm %s' % (yes, pkg['pkgname'])
            print(cmd)
            r = os.system(cmd)
            if r != 0:
                return RET_ERR

        if os.path.isfile(dname):
            d = dname
        else:
            d = "."
            os.chdir(dname)

        if self.get_os_ver() < 10:
            opts = ''
        else:
            opts = '-G'

        if 'resp' in pkg and len(pkg['resp']) > 0:
            f = tempfile.NamedTemporaryFile(dir=self.get_temp_dir())
            resp = f.name
            f.close()
            with open(resp, "w") as f:
                f.write(pkg['resp'])
        else:
            resp = "/dev/null"
        yes = os.path.dirname(__file__) + "/yes"
        cmd = '%s | pkgadd -r %s %s -d %s all' % (yes, resp, opts, d)
        print(cmd)
        r = os.system(cmd)
        os.chdir("/")

        if os.path.isdir(dname):
            import shutil
            shutil.rmtree(dname)

        if r != 0:
            return RET_ERR
        return RET_OK

    def sol_fixable_pkg(self, pkg):
        return 0

    def sol_fix_all(self):
        return RET_NA

    def sol_get_installed_packages(self):
        p = Popen(['pkginfo', '-l'], stdout=PIPE)
        (out, err) = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, 'can not fetch installed packages list'
            return {}
        return self.sol_parse_pkginfo(out)

    def sol_parse_pkginfo(self, out):
        l = {}
        for line in out.split('\n'):
            v = line.split(':')
            if len(v) != 2:
                continue
            f = v[0].strip()
            if f == "PKGINST":
                pkgname = v[1].strip()
            elif f == "ARCH":
                pkgarch = v[1].strip()
            elif f == "VERSION":
                pkgvers = v[1].strip()
                if pkgname in l:
                    l[pkgname] += [(pkgvers, pkgarch)]
                else:
                    l[pkgname] = [(pkgvers, pkgarch)]
        return l

    def aix_fix_pkg(self, pkg):
        cmd = ['nimclient', '-o', 'cust',
               '-a', 'lpp_source=%s'%self.uri,
               '-a', 'installp_flags=agQY',
               '-a', 'filesets=%s'%pkg['pkgname']]
        s = " ".join(cmd)
        print s
        r = os.system(s)
        if r != 0:
            return RET_ERR
        return RET_OK

    def aix_fixable_pkg(self, pkg):
        return RET_NA

    def aix_fix_all(self):
        return RET_NA

    def aix_get_installed_packages(self):
        p = Popen(['lslpp', '-L', '-c'], stdout=PIPE)
        (out, err) = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, 'can not fetch installed packages list'
            return {}
        return self.aix_parse_lslpp(out)

    def aix_parse_lslpp(self, out):
        l = {}
        for line in out.split('\n'):
            if line.startswith('#') or len(line) == 0:
                continue
            v = line.split(':')
            if len(v) < 3:
                continue
            pkgname = v[1].replace('-'+v[2], '')
            if pkgname in l:
                l[pkgname] += [(v[2], "")]
            else:
                l[pkgname] = [(v[2], "")]
        return l

    def hp_fix_pkg(self, pkg):
        if self.check_pkg(pkg, verbose=False) == RET_OK:
            return RET_OK
        if self.fixable_pkg(pkg) == RET_ERR:
            return RET_ERR
        if self.highest_avail_version == "0":
            return RET_ERR
        if self.strict:
            self.fix_list.append(pkg["pkgname"]+',r='+pkg["minver"])
        else:
            self.fix_list.append(pkg["pkgname"]+',r='+self.highest_avail_version)
        self.need_pushpkg = True
        self.installed_packages = self.get_installed_packages()
        return RET_OK

    def hp_fix_all(self):
        r = call(['swinstall', '-x', 'allow_downdate=true', '-x', 'autoreboot=true', '-x', 'mount_all_filesystems=false', '-s', self.uri] + self.fix_list)
        if r != 0:
            return RET_ERR
        return RET_OK

    def hp_fixable_pkg(self, pkg):
        self.highest_avail_version = "0"
        if self.check_pkg(pkg, verbose=False) == RET_OK:
            return RET_OK
        cmd = ['swlist', '-l', self.pkg_type, '-s', self.uri, pkg['pkgname']]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        (out, err) = p.communicate()
        if p.returncode != 0:
            if "not found on host" in err:
                print >>sys.stderr, '%s > %s not available in repositories'%(pkg['pkgname'], pkg['minver'])
            else:
                print >>sys.stderr, 'can not fetch available packages list'
            return RET_ERR
        l = self.hp_parse_swlist(out)
        if len(l) == 0:
            print >>sys.stderr, '%s > %s not available in repositories'%(pkg['pkgname'], pkg['minver'])
            return RET_ERR
        for v in map(lambda x: x[0], l.values()[0]):
            if V(v) > V(self.highest_avail_version):
                self.highest_avail_version = v
        if V(self.highest_avail_version) < V(pkg['minver']):
            print >>sys.stderr, '%s > %s not available in repositories'%(pkg['pkgname'], pkg['minver'])
            return RET_ERR
        return RET_OK

    def hp_get_installed_packages(self):
        p = Popen(['swlist', '-l', self.pkg_type], stdout=PIPE)
        (out, err) = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, 'can not fetch installed packages list'
            return {}
        return self.hp_parse_swlist(out)

    def hp_parse_swlist(self, out):
        l = {}
        for line in out.split('\n'):
            if line.startswith('#') or len(line) == 0:
                continue
            v = line.split()
            if len(v) < 2:
                continue
            if v[0] in l:
                l[v[0]] += [(v[1], "")]
            else:
                l[v[0]] = [(v[1], "")]
        return l

    def rpm_get_installed_packages(self):
        p = Popen(['rpm', '-qa', '--qf', '%{NAME} %{VERSION}-%{RELEASE} %{ARCH}\n'], stdout=PIPE)
        (out, err) = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, 'can not fetch installed packages list'
            return {}
        l = {}
        for line in out.split('\n'):
            v = line.split(' ')
            if len(v) != 3:
                continue
            if v[0] in l:
                l[v[0]] += [(v[1], v[2])]
            else:
                l[v[0]] = [(v[1], v[2])]
        return l

    def deb_get_installed_packages(self):
        p = Popen(['dpkg', '-l'], stdout=PIPE)
        (out, err) = p.communicate()
        if p.returncode != 0:
            print >>sys.stderr, 'can not fetch installed packages list'
            return {}
        l = {}
        for line in out.split('\n'):
            if not line.startswith('ii'):
                continue
            v = line.split()[1:3]
            l[v[0]] = [(v[1], "")]
        return l

    def apt_fixable_pkg(self, pkg):
        # TODO
        return RET_NA

    def zyp_fixable_pkg(self, pkg):
        return RET_NA

    def yum_fixable_pkg(self, pkg):
        try:
            r = self.check_pkg(pkg, verbose=False)
        except LiveKernVulnerable:
            r = RET_OK

        if r == RET_OK:
            return RET_OK

        cmd = ['yum', 'list', 'available', pkg['pkgname']]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        (out, err) = p.communicate()
        if p.returncode != 0:
            if "No matching Packages" in err:
                print >>sys.stderr, '%s > %s not available in repositories'%(pkg['pkgname'], pkg['minver'])
            else:
                print >>sys.stderr, 'can not fetch available packages list'
            return RET_ERR
        highest_avail_version = "0"
        for line in out.split('\n'):
            l = line.split()
            if len(l) != 3:
                continue
            if V(l[1]) > V(highest_avail_version):
                highest_avail_version = l[1]
        if V(highest_avail_version) < V(pkg['minver']):
            print >>sys.stderr, '%s > %s not available in repositories'%(pkg['pkgname'], pkg['minver'])
            return RET_ERR
        return RET_OK

    def zyp_fix_pkg(self, pkg):
        try:
            r = self.check_pkg(pkg, verbose=False)
        except LiveKernVulnerable:
            r = RET_OK
        if r == RET_OK:
            return RET_OK
        if self.fixable_pkg(pkg) == RET_ERR:
            return RET_ERR
        r = call(['zypper', 'install', '-y', pkg["pkgname"]])
        if r != 0:
            return RET_ERR
        self.need_pushpkg = True
        self.installed_packages = self.get_installed_packages()
        return RET_OK

    def yum_fix_pkg(self, pkg):
        try:
            r = self.check_pkg(pkg, verbose=False)
        except LiveKernVulnerable:
            r = RET_OK
        if r == RET_OK:
            return RET_OK
        if self.fixable_pkg(pkg) == RET_ERR:
            return RET_ERR
        r = call(['yum', '-y', 'install', pkg["pkgname"]])
        if r != 0:
            return RET_ERR
        self.need_pushpkg = True
        self.installed_packages = self.get_installed_packages()
        return RET_OK

    def apt_fix_pkg(self, pkg):
        if self.check_pkg(pkg, verbose=False) == RET_OK:
            return RET_OK
        r = call(['apt-get', 'install', '-y', '--allow-unauthenticated', pkg["pkgname"]])
        if r != 0:
            return RET_ERR
        self.need_pushpkg = True
        self.installed_packages = self.get_installed_packages()
        return RET_OK

    def get_raw_kver(self):
        return os.uname()[2]

    def get_kver(self):
        s = self.get_raw_kver()
        s = s.replace('xen', '')
        s = s.replace('hugemem', '')
        s = s.replace('smp', '')
        s = s.replace('PAE', '')
	s = s.replace('.x86_64','')
	s = s.replace('.i686','')
        return s

    def workaround_python_cmp(self, s):
        """ python list cmp says a > 9, but rpm says z < 0, ie :
            python says 2.6.18-238.el5 > 2.6.18-238.11.1.el5
            which is wrong in the POV of the package manager.

            replace .[a-z]* by .00000000[a-z] to force the desired behaviour
        """
        return re.sub("\.[a-zA-Z]+", repl, s)

    def check_pkg(self, pkg, verbose=True):
        if not pkg["pkgname"] in self.installed_packages:
            if verbose:
                print pkg["pkgname"], "is not installed (%s:not applicable)"%pkg["rule"]
            return RET_OK

        name = pkg["pkgname"]

        if name.startswith("kernel"):
            kver = self.get_raw_kver()
            for i in ('xen', 'hugemem', 'smp', 'PAE'):
                if kver.endswith(i) and name != "kernel-"+i:
                    if verbose:
                        print name, "bypassed :", i, "kernel booted", "(%s:not applicable)"%pkg["rule"]
                    return RET_OK

        r = RET_OK
        max = "0"
        max_v = V(max)
        ok = []
        minver = self.workaround_python_cmp(pkg['minver'])
        target = V(minver)
        if 'firstver' in pkg and pkg['firstver'] != "":
            firstver = self.workaround_python_cmp(pkg['firstver'])
        else:
            firstver = "0"
        firstver_v = V(firstver)
        candidates = map(lambda x: [name]+list(x), self.installed_packages[name])

        for _name, vers, arch in candidates:
            _vers = self.workaround_python_cmp(vers)
            actual = V(_vers)
            if actual > max_v or max == "0":
                max = vers
                max_v = actual
            if target <= actual or firstver_v > actual:
                ok.append((_name, vers, arch))

        if max == "0":
            # not installed
            if verbose:
                print name, "is not installed (%s:not applicable)"%pkg["rule"]
            return RET_OK

        if name.startswith("kernel"):
            kver = self.get_kver()
            if len(ok) == 0:
                if verbose:
                    print >>sys.stderr, ', '.join(map(lambda x: x[0]+"-"+x[1]+"."+x[2], candidates)), 'installed and vulnerable. upgrade to', pkg["minver"], "(%s:need upgrade)"%pkg["rule"]
                return RET_ERR
            elif kver not in map(lambda x: x[1], ok):
                if verbose:
                    print >>sys.stderr, ', '.join(map(lambda x: x[0]+"-"+x[1]+"."+x[2], ok)), "installed and not vulnerable but vulnerable kernel", self.get_raw_kver(), "booted", "(%s:need reboot)"%pkg["rule"]
                raise LiveKernVulnerable()
            else:
                if verbose:
                    print "kernel", self.get_raw_kver(), "installed, booted and not vulnerable", "(%s:not vulnerable)"%pkg["rule"]
                return RET_OK

        if len(ok) > 0:
            if verbose:
                print "%s installed and not vulnerable (%s:not vulnerable)"%(', '.join(map(lambda x: x[0]+"-"+x[1]+"."+x[2], ok)), pkg["rule"])
            return RET_OK

        if verbose:
            print >>sys.stderr, 'package', name+"-"+vers, 'is vulnerable. upgrade to', pkg["minver"], "(%s:need upgrade)"%pkg["rule"]
        return RET_ERR

    def check(self):
        r = 0
        for pkg in self.packages:
            try:
                _r = self.check_pkg(pkg)
                r |= _r
            except LiveKernVulnerable:
                r |= RET_ERR
        return r

    def fix(self):
        r = 0
        for pkg in self.packages:
            r |= self.fix_pkg(pkg)
        if self.fix_all is not None and len(self.fix_list) > 0:
            self.fix_all()
        if self.need_pushpkg:
            self.pushpkg()
        return r

    def pushpkg(self):
        bin = '/opt/opensvc/bin/nodemgr'
        if which(bin) is None:
            return
        cmd = [bin, 'pushpkg']
        print ' '.join(cmd)
        p = Popen(cmd)
        p.communicate()

    def fixable(self):
        r = 0
        for pkg in self.packages:
            r |= self.fixable_pkg(pkg)
        return r

if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix [uri]"""%sys.argv[0]
    if len(sys.argv) not in (3, 4):
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        if len(sys.argv) == 4:
            o = CompVuln(sys.argv[1], sys.argv[3])
        else:
            o = CompVuln(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

0707010000bcee000081ed00000000000000000000000154fc7bfb00001e7d000000660000000300000000000000000000003800000000root/opt/opensvc/var/compliance/com.opensvc/sysvinit.py   from subprocess import *
import os
import sys
import glob
import re

class InitError(Exception):
    pass

class UnknownService(Exception):
    pass

class SetError(Exception):
    pass

class SeqError(Exception):
    pass

class DupError(Exception):
    pass

class SysVInit(object):
    def __init__(self):
        self.load()

    def __str__(self):
        s = ""
        for svc in self.services:
            s += "%-20s %s\n"%(svc, ' '.join(map(lambda x: '%-4s'%x,  str(self.services[svc]))))
        return s


    def get_svcname(self, s):
        _s = os.path.basename(s)
        _svcname = re.sub(r'^[SK][0-9]+', '', _s)
        _seq = re.sub(r'[KS](\d+).+', r'\1', _s)
        if _s[0] == 'S':
            _state = 'on'
        elif _s[0] == 'K':
            _state = 'off'
        else:
            raise InitError("unexepected service name: %s"%s)
        return _state, _seq, _svcname

    def load(self):
        self.services = {}
        self.levels = (0, 1, 2, 3, 4, 5, 6)
        default = "none"

        self.base_d = "/etc"
        self.init_d = self.base_d + "/init.d"
        if not os.path.exists(self.init_d):
            self.base_d = "/sbin"
            self.init_d = self.base_d + "/init.d"
        if not os.path.exists(self.init_d):
            raise InitError("init dir not found")

        for l in self.levels:
            for s in glob.glob("%s/rc%d.d/[SK]*"%(self.base_d, l)):
                state, seq, svc = self.get_svcname(s)
                if svc not in self.services:
                    self.services[svc] = {seq: [default, default, default, default, default, default, default]}
                if seq not in self.services[svc]:
                    self.services[svc][seq] = [default, default, default, default, default, default, default]
                self.services[svc][seq][l] = state

    def activate(self, service, levels, seq):
        for l in levels:
            self.activate_one(service, levels, seq)

    def activate_one(self, service, level, seq):
        if len(service) == 0:
            SetError("service is empty")

        start_l = "S%s%s"%(seq,service)
        svc_p = "../init.d/"+service

        os.chdir(self.base_d+"/rc%s.d"%level)

        g = glob.glob("[SK]*%s"%service)
        if len(g) > 0:
            cmd = ['rm', '-f'] + g
            print " ".join(cmd)
            p = Popen(cmd, stdout=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                raise SetError()

        cmd = ['ln', '-sf', svc_p, start_l]
        print " ".join(cmd)
        p = Popen(cmd, stdout=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            raise SetError()

    def deactivate_one(self, service, level, seq):
        if len(service) == 0:
            SetError("service is empty")
        stop_l = "K%s%s"%(seq,service)
        svc_p = "../init.d/"+service

        os.chdir(self.base_d+"/rc%s.d"%level)

        g = glob.glob("[SK]*%s"%service)
        if len(g) > 0:
            cmd = ['rm', '-f'] + g
            print " ".join(cmd)
            p = Popen(cmd, stdout=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                raise SetError()

        cmd = ['ln', '-sf', svc_p, stop_l]
        print " ".join(cmd)
        p = Popen(cmd, stdout=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            raise SetError()

    def delete_one(self, service, level):
        if len(service) == 0:
            SetError("service is empty")
        g = glob.glob(self.base_d+"/rc%s.d"%level+"/*"+service)
        if len(g) == 0:
            return
        cmd = ['rm', '-f'] + g
        print " ".join(cmd)
        p = Popen(cmd, stdout=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            raise SetError()

    def check_init(self, service):
        init_f = os.path.join(self.init_d, service)
        if os.path.exists(init_f):
            return True
        return False

    def set_state(self, service, level, state, seq):
        if service in self.services and seq in self.services[service]:
            curstates = self.services[service][seq]

            if state != "del" and len(curstates) == 1 and curstates[int(level)] == state or \
               state == "del" and len(curstates) == 1 and curstates[int(level)] == "none":
                return

        if state == "on":
            self.activate_one(service, level, seq)
        elif state == "off":
            self.deactivate_one(service, level, seq)
        elif state == "del":
            self.delete_one(service, level)
        else:
            raise SetError()

    def get_state(self, service, level, seq):
        if service not in self.services:
            raise UnknownService()

        # compute the number of different launcher for this service in the runlevel
        l = []
        for _seq in self.services[service]:
            if self.services[service][_seq][level] != "none":
                l.append(self.services[service][_seq][level])

        if seq is None:
            if len(l) == 0:
                return "none"
            raise SeqError()

        if len(l) > 1:
            raise DupError()

        try:
            curstates = self.services[service][seq]
            curstate = curstates[int(level)]
        except:
            curstate = "none"

        if len(l) == 1 and curstate == "none":
            raise SeqError()
        return curstate

    def check_state(self, service, levels, state, seq=None, verbose=False):
        r = 0
        if seq is not None and type(seq) == int:
            seq = "%02d"%seq

        if not self.check_init(service):
            if verbose:
                print >>sys.stderr, "service %s init script does not exist in %s"%(service, self.init_d)
            r |= 1

        if seq is None and state != "del":
            if verbose:
                print >>sys.stderr, "service %s sequence number must be set"%(service)
            return 1

        for level in levels:
            try:
                level = int(level)
            except:
                continue
            try:
                curstate = self.get_state(service, level, seq)
            except DupError:
                if verbose:
                    print >>sys.stderr, "service %s has multiple launchers at level %d"%(service, level)
                r |= 1
                continue
            except SeqError:
                if verbose:
                    print >>sys.stderr, "service %s sequence number error at level %d"%(service, level)
                r |= 1
                continue
            except UnknownService:
                curstate = "none"

            if (state != "del" and curstate != state) or \
               (state == "del" and curstate != "none"):
                if verbose:
                    print >>sys.stderr, "service", service, "at runlevel", level, "is in state", curstate, "! target state is", state
                r |= 1
            else:
                if verbose:
                    print "service", service, "at runlevel", level, "is in state", curstate
        return r
            
    def fix_state(self, service, levels, state, seq=None):
        if seq is not None and type(seq) == int:
            seq = "%02d"%seq

        if seq is None and state != "del":
            print >>sys.stderr, "service %s sequence number must be set"%(service)
            return 1

        for level in levels:
            try:
                self.set_state(service, level, state, seq)
            except SetError:
                print >>sys.stderr, "failed to set", service, "runlevels"
                return 1
        return 0

if __name__ == "__main__":
    o = SysVInit()
    print o
    print 'xfs@rc3 =', o.get_state('xfs', 3)
   0707010000bcef000081ed00000000000000000000000154fc7bfb00003c83000000660000000300000000000000000000003500000000root/opt/opensvc/var/compliance/com.opensvc/users.py  #!/usr/bin/env /opt/opensvc/bin/python
""" 
module use OSVC_COMP_USER_... vars
which define {'username':{'propname':'propval',... }, ...}

example: 
{"tibco":{"shell":"/bin/ksh","gecos":"agecos",},
 "tibco1":{"shell":"/bin/tcsh","gecos":"another gecos",},
}

supported dictionnary keys:
- uid
- gid
- gecos
- home
- shell

supported toggles:
- OSVC_COMP_USERS_INITIAL_PASSWD=true|false

"""

import os
import sys
import json
import pwd
import re
from utilities import which

try:
    import spwd
    cap_shadow = True
except:
    cap_shadow = False

from subprocess import Popen, list2cmdline, PIPE

sys.path.append(os.path.dirname(__file__))

from comp import *

blacklist = [
 "root",
 "bin",
 "daemon",
 "adm",
 "lp",
 "sync",
 "shutdown",
 "halt",
 "mail",
 "news",
 "uucp",
 "operator",
 "nobody",
 "nscd",
 "vcsa",
 "pcap",
 "mailnull",
 "smmsp",
 "sshd",
 "rpc",
 "avahi",
 "rpcuser",
 "nfsnobody",
 "haldaemon",
 "avahi-autoipd",
 "ntp"
]

class CompUser(object):
    def __init__(self, prefix='OSVC_COMP_USER_'):
        self.prefix = prefix.upper()
        self.pwt = {
            'shell': 'pw_shell',
            'home': 'pw_dir',
            'uid': 'pw_uid',
            'gid': 'pw_gid',
            'gecos': 'pw_gecos',
            'password': 'pw_passwd',
        }
        self.spwt = {
            'spassword': 'sp_pwd',
        }
        self.usermod_p = {
            'shell': '-s',
            'home': '-d',
            'uid': '-u',
            'gid': '-g',
            'gecos': '-c',
            'password': '-p',
            'spassword': '-p',
        }
        self.sysname, self.nodename, x, x, self.machine = os.uname()

        if "OSVC_COMP_USERS_INITIAL_PASSWD" in os.environ and \
           os.environ["OSVC_COMP_USERS_INITIAL_PASSWD"] == "true":
            self.initial_passwd = True
        else:
            self.initial_passwd = False

        if self.sysname not in ['SunOS', 'Linux', 'HP-UX', 'AIX', 'OSF1']:
            print >>sys.stderr, 'module not supported on', self.sysname
            raise NotApplicable()

        self.users = {}
        for k in [ key for key in os.environ if key.startswith(self.prefix)]:
            try:
                d = json.loads(os.environ[k])
                for user in d:
                    if user not in self.users:
                        self.users[user] = d[user]
                    else:
                        for key in self.usermod_p.keys():
                            if key in d[user] and key not in self.users[user]:
                                self.users[user][key] = d[user][key]
            except ValueError:
                print >>sys.stderr, 'user syntax error on var[', k, '] = ',os.environ[k]

        if len(self.users) == 0:
            raise NotApplicable()

        p = re.compile('%%ENV:\w+%%')
        for user, d in self.users.items():
            for k in d:
                if type(d[k]) not in [str, unicode]:
                    continue
                for m in p.findall(d[k]):
                    s = m.strip("%").replace('ENV:', '')
                    if s in os.environ:
                        v = os.environ[s]
                    elif 'OSVC_COMP_'+s in os.environ:
                        v = os.environ['OSVC_COMP_'+s]
                    else:
                        print >>sys.stderr, s, 'is not an env variable'
                        raise NotApplicable()
                    d[k] = d[k].replace(m, v)
                if k in ('uid', 'gid'):
                    d[k] = int(d[k])

        for user, d in self.users.items():
            if cap_shadow:
                if "password" in d and len(d["password"]) > 0 and \
                   ("spassword" not in d or len(d["spassword"]) == 0):
                    self.users[user]["spassword"] = self.users[user]["password"]
                    del self.users[user]["password"]
                if "spassword" not in d:
                    self.users[user]["spassword"] = "x"
            else:
                if "spassword" in d and len(d["spassword"]) > 0 and \
                   ("password" not in d or len(d["password"]) == 0):
                    self.users[user]["password"] = self.users[user]["spassword"]
                    del self.users[user]["spassword"]
                if "password" not in d:
                    self.users[user]["password"] = "x"

    def fixable(self):
        if not which('usermod'):
            print >>sys.stderr, "usermod program not found"
            return RET_ERR
        return RET_OK

    def grpconv(self):
        if not cap_shadow or not os.path.exists('/etc/gshadow'):
            return
        if not which('grpconv'):
            return
        with open('/etc/group', 'r') as f:
            buff = f.read()
        l = []
        for line in buff.split('\n'):
            u = line.split(':')[0]
            if u in l:
                print >>sys.stderr, "duplicate group %s in /etc/group. skip grpconv (grpconv bug workaround)"%u
                return
            l.append(u)
        p = Popen(['grpconv'])
        p.communicate()

    def pwconv(self):
        if not cap_shadow or not os.path.exists('/etc/shadow'):
            return
        if not which('pwconv'):
            return
        p = Popen(['pwconv'])
        p.communicate()

    def fix_item(self, user, item, target):
        if item in ["password", "spassword"]:
            if self.initial_passwd:
                print "skip", user, "password modification in initial_passwd mode"
                return RET_OK
            if target == "x":
                return RET_OK
        cmd = ['usermod', self.usermod_p[item], str(target)]
        if item == 'home':
            cmd.append('-m')
        cmd.append(user)
        print list2cmdline(cmd)
        p = Popen(cmd)
        out, err = p.communicate()
        r = p.returncode
        self.pwconv()
        self.grpconv()
        if r == 0:
            return RET_OK
        else:
            return RET_ERR

    def check_item(self, user, item, target, current, verbose=False):
        if type(current) == int and current < 0:
            current += 4294967296
        if target == current:
            if verbose:
                print 'user', user, item+':', current
            return RET_OK
        elif "passw" in item and target == "!!" and current == "":
            if verbose:
                print 'user', user, item+':', current
            return RET_OK
        else:
            if verbose:
                print >>sys.stderr, 'user', user, item+':', current, 'target:', target
            return RET_ERR

    def check_user_del(self, user, verbose=True):
        r = 0
        try:
            userinfo=pwd.getpwnam(user)
        except KeyError:
            if verbose:
                print 'user', user, 'does not exist, on target'
            return RET_OK
        if verbose:
            print >>sys.stderr, 'user', user, "exists, shouldn't"
        return RET_ERR

    def check_user(self, user, props, verbose=True):
        if user.startswith('-'):
            return self.check_user_del(user.lstrip('-'), verbose=verbose)
        r = 0
        try:
            userinfo=pwd.getpwnam(user)
        except KeyError:
            if self.try_create_user(props):
                if verbose:
                    print >>sys.stderr, 'user', user, 'does not exist'
                return RET_ERR
            else:
                if verbose:
                    print >>sys.stderr, 'user', user, 'does not exist and not enough info to create it'
                return RET_ERR

        for prop in self.pwt:
            if prop in props:
                if prop == "password":
                    if self.initial_passwd:
                        if verbose:
                            print "skip", user, "passwd checking in initial_passwd mode"
                        continue
                    if props[prop] == "x":
                        continue
                r |= self.check_item(user, prop, props[prop], getattr(userinfo, self.pwt[prop]), verbose=verbose)

        if 'check_home' not in props or props['check_home'] == "yes":
            r |= self.check_home_ownership(user, verbose=verbose)

        if not cap_shadow:
            return r

        try:
            usersinfo=spwd.getspnam(user)
        except KeyError:
            if "spassword" in props:
                if verbose:
                    print >>sys.stderr, user, "not declared in /etc/shadow"
                r |= RET_ERR
            usersinfo = None

        if usersinfo is not None:
            for prop in self.spwt:
                if prop in props:
                    if prop == "spassword":
                        if self.initial_passwd:
                            if verbose:
                                print "skip", user, "spasswd checking in initial_passwd mode"
                            continue
                        if props[prop] == "x":
                            continue
                    r |= self.check_item(user, prop, props[prop], getattr(usersinfo, self.spwt[prop]), verbose=verbose)

        return r

    def try_create_user(self, props):
        #
        # don't try to create user if passwd db is not 'files'
        # beware: 'files' db is the implicit default
        #
        if 'db' in props and props['db'] != 'files':
            return False
        return True

    def get_uid(self, user):
        import pwd
        try:
            info=pwd.getpwnam(user)
            uid = info[2]
        except:
            print >>sys.stderr, "user %s does not exist"%user
            raise ComplianceError()
        return uid

    def check_home_ownership(self, user, verbose=True):
        path = os.path.expanduser("~"+user)
        if not os.path.exists(path):
            if verbose:
                print >>sys.stderr, path, "homedir does not exist"
            return RET_ERR 
        tuid = self.get_uid(user)
        uid = os.stat(path).st_uid
        if uid != tuid:
            if verbose: print >>sys.stderr, path, 'uid should be %s but is %s'%(str(tuid), str(uid))
            return RET_ERR
        if verbose: print path, 'owner is', user
        return RET_OK

    def fix_home_ownership(self, user):
        if self.check_home_ownership(user, verbose=False) == RET_OK:
            return RET_OK
        uid = self.get_uid(user)
        path = os.path.expanduser("~"+user)
        if not os.path.exists(path):
            if os.path.exists("/etc/skel"):
                cmd = ['cp', '-R', '/etc/skel/', path]
                print list2cmdline(cmd)
                p = Popen(cmd)
                out, err = p.communicate()
                r = p.returncode
                if r != 0:
                    return RET_ERR

                cmd = ['chown', '-R', str(uid), path]
                print list2cmdline(cmd)
                p = Popen(cmd)
                out, err = p.communicate()
                r = p.returncode
                if r != 0:
                    return RET_ERR
            else:
                os.makedirs(path)
                os.chown(path, uid, -1)
        return RET_OK

    def unlock_user(self, user):
        if self.sysname != "SunOS":
            return
        cmd = ["uname", "-r"]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return
        if out.strip() == '5.8':
            unlock_opt = '-d'
        else:
            unlock_opt = '-u'
        cmd = ["passwd", unlock_opt, user]
        print list2cmdline(cmd)
        p = Popen(cmd)
        out, err = p.communicate()
        r = p.returncode
        if r == 0:
            return RET_OK
        else:
            return RET_ERR

    def create_user(self, user, props):
        cmd = ['useradd']
        for item in props:
            if item == "check_home":
                continue
            prop = str(props[item])
            if len(prop) == 0:
                continue
            if item.endswith("password") and self.sysname in ("AIX", "SunOS", "OSF1"):
                continue
            cmd = cmd + self.usermod_p[item].split() + [prop]
            if item == "home":
                cmd.append("-m")
        cmd += [user]
        print list2cmdline(cmd)
        p = Popen(cmd)
        out, err = p.communicate()
        r = p.returncode
        if r == 0:
            if self.unlock_user(user) == RET_ERR:
                return RET_ERR
            return RET_OK
        else:
            return RET_ERR

    def fix_user_del(self, user):
        if user in blacklist:
            print >>sys.stderr, "delete", user, "... cowardly refusing"
            return RET_ERR
        cmd = ['userdel', user]
        print list2cmdline(cmd)
        p = Popen(cmd)
        out, err = p.communicate()
        r = p.returncode
        if r == 0:
            return RET_OK
        else:
            return RET_ERR

    def fix_user(self, user, props):
        if user.startswith('-'):
            return self.fix_user_del(user.lstrip('-'))
        r = 0
        try:
            userinfo = pwd.getpwnam(user)
        except KeyError:
            if self.try_create_user(props):
                return self.create_user(user, props)
            else:
                print 'user', user, 'does not exist and not enough info to create it'
                return RET_OK

        for prop in self.pwt:
            if prop in props and \
               self.check_item(user, prop, props[prop], getattr(userinfo, self.pwt[prop])) != RET_OK:
                r |= self.fix_item(user, prop, props[prop])

        if 'check_home' not in props or props['check_home'] == "yes":
            r |= self.fix_home_ownership(user)

        if not cap_shadow:
            return r

        try:
            usersinfo = spwd.getspnam(user)
        except KeyError:
            if "spassword" in props:
                self.fix_item(user, "spassword", props["spassword"])
                usersinfo = spwd.getspnam(user)
            else:
                usersinfo = None

        if usersinfo is not None:
            for prop in self.spwt:
                if prop in props and \
                    self.check_item(user, prop, props[prop], getattr(usersinfo, self.spwt[prop])) != RET_OK:
                    r |= self.fix_item(user, prop, props[prop])
        return r

    def check(self):
        r = 0
        for user, props in self.users.items():
            r |= self.check_user(user, props)
        return r

    def fix(self):
        r = 0
        for user, props in self.users.items():
            if self.check_user(user, props, verbose=False) == RET_ERR:
                r |= self.fix_user(user, props)
        return r

if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = CompUser(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

 0707010000bcf0000081a400000000000000000000000154fc7bfb00000693000000660000000300000000000000000000003900000000root/opt/opensvc/var/compliance/com.opensvc/utilities.py  #
# Copyright (c) 2011 Christophe Varoqui <christophe.varoqui@opensvc.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os
import sys

class NoRule(Exception):
    pass

def is_exe(fpath):
    """Returns True if file path is executable, False otherwize
    does not follow symlink
    """
    return os.path.exists(fpath) and os.access(fpath, os.X_OK)

def which(program):
    """Returns True if program is in PATH and executable, False
    otherwize
    """
    fpath, fname = os.path.split(program)
    if fpath and is_exe(program):
        return program
    for path in os.environ["PATH"].split(os.pathsep):
        exe_file = os.path.join(path, program)
        if is_exe(exe_file):
            return exe_file
    return None

def get_rule(rule_var):
    """ Return the value if set in the environment.
        Print some useful message if not set.
    """
    if rule_var not in os.environ:
        print >>sys.stderr, rule_var, "is not present in this node's ruleset"
        raise NoRule()
    return os.environ[rule_var]

 0707010000bcf2000081ed00000000000000000000000154fc7bfb00001696000000660000000300000000000000000000003600000000root/opt/opensvc/var/compliance/com.opensvc/xinetd.py #!/usr/bin/env /opt/opensvc/bin/python
""" 
module use OSVC_COMP_USER_... vars
which define {"gssftp": {"disable": "no", "server_args": "-l -a -u 022", ...}, ...}

supported dictionnary keys:
- disable
- server_args
"""

import os
import sys
import json
import pwd
from subprocess import Popen, list2cmdline

sys.path.append(os.path.dirname(__file__))

from comp import *

class Xinetd(object):
    def __init__(self, prefix='OSVC_COMP_XINETD_'):
        self.prefix = prefix.upper()
        self.base = os.path.join(os.sep, "etc", "xinetd.d")
        if not os.path.exists(self.base):
            print >>sys.stderr, self.base, 'does not exist'
            raise NotApplicable()

        self.svcs = {}
        for k in [ key for key in os.environ if key.startswith(self.prefix)]:
            try:
                self.svcs.update(json.loads(os.environ[k]))
            except ValueError:
                print >>sys.stderr, 'xinetd service syntax error on var[', k, '] = ',os.environ[k]

        if len(self.svcs) == 0:
            raise NotApplicable()
        self.cf_d = {}
        self.known_props = (
            "flags",
            "socket_type",
            "wait",
            "user",
            "server",
            "server_args",
            "disable")

    def fixable(self):
        return RET_NA

    def get_svc(self, svc):
        if svc in self.cf_d:
            return self.cf_d[svc]

        p = os.path.join(self.base, svc)
        if not os.path.exists(p):
            self.cf_d[svc] = {}
            return self.cf_d[svc]

        if svc not in self.cf_d:
            self.cf_d[svc] = {}

        with open(p, 'r') as f:
            for line in f.read().split('\n'):
                if '=' not in line:
                    continue
                l = line.split('=')
                if len(l) != 2:
                    continue
                var = l[0].strip()
                val = l[1].strip()
                self.cf_d[svc][var] = val

        return self.cf_d[svc]

    def fix_item(self, svc, item, target):
        if item not in self.known_props:
            print >>sys.stderr, 'xinetd service', svc, item+': unknown property in compliance rule'
            return RET_ERR
        cf = self.get_svc(svc)

        if item in cf and cf[item] == target:
            return RET_OK

        p = os.path.join(self.base, svc)
        if not os.path.exists(p):
            print >>sys.stderr, p, "does not exist"
            return RET_ERR

        done = False
        with open(p, 'r') as f:
            buff = f.read().split('\n')
            for i, line in enumerate(buff):
                if '=' not in line:
                    continue
                l = line.split('=')
                if len(l) != 2:
                    continue
                var = l[0].strip()
                if var != item:
                    continue
                l[1] = target
                buff[i] = "= ".join(l)
                done = True

        if not done:
            with open(p, 'r') as f:
                buff = f.read().split('\n')
                for i, line in enumerate(buff):
                    if '=' not in line:
                        continue
                    l = line.split('=')
                    if len(l) != 2:
                        continue
                    buff.insert(i, item+" = "+target)
                    done = True
                    break

        if not done:
            print >>sys.stderr, "failed to set", item, "=", target, "in", p
            return RET_ERR

        with open(p, 'w') as f:
            f.write("\n".join(buff))

        print "set", item, "=", target, "in", p
        return RET_OK

    def check_item(self, svc, item, target, verbose=False):
        if item not in self.known_props:
            print >>sys.stderr, 'xinetd service', svc, item+': unknown property in compliance rule'
            return RET_ERR
        cf = self.get_svc(svc)
        if item in cf and target == cf[item]:
            if verbose:
                print 'xinetd service', svc, item+':', cf[item]
            return RET_OK
        elif item in cf:
            if verbose:
                print >>sys.stderr, 'xinetd service', svc, item+':', cf[item], 'target:', target
        else:
            if verbose:
                print >>sys.stderr, 'xinetd service', svc, item+': unset', 'target:', target
        return RET_ERR

    def check_svc(self, svc, props):
        r = 0
        for prop in props:
            r |= self.check_item(svc, prop, props[prop], verbose=True)
        return r

    def fix_svc(self, svc, props):
        r = 0
        for prop in props:
            r |= self.fix_item(svc, prop, props[prop])
        return r

    def check(self):
        r = 0
        for svc, props in self.svcs.items():
            r |= self.check_svc(svc, props)
        return r

    def fix(self):
        r = 0
        for svc, props in self.svcs.items():
            r |= self.fix_svc(svc, props)
        return r

if __name__ == "__main__":
    syntax = """syntax:
      %s PREFIX check|fixable|fix"""%sys.argv[0]
    if len(sys.argv) != 3:
        print >>sys.stderr, "wrong number of arguments"
        print >>sys.stderr, syntax
        sys.exit(RET_ERR)
    try:
        o = Xinetd(sys.argv[1])
        if sys.argv[2] == 'check':
            RET = o.check()
        elif sys.argv[2] == 'fix':
            RET = o.fix()
        elif sys.argv[2] == 'fixable':
            RET = o.fixable()
        else:
            print >>sys.stderr, "unsupported argument '%s'"%sys.argv[2]
            print >>sys.stderr, syntax
            RET = RET_ERR
    except NotApplicable:
        sys.exit(RET_NA)
    except:
        import traceback
        traceback.print_exc()
        sys.exit(RET_ERR)

    sys.exit(RET)

  07070100000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000b00000000TRAILER!!!                                                                                                                                                                                                                                                                                                                                                        