|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Script to query and update each Site with WLCG scope tags to migrate them |
| 5 | + * to a new heirachical structure. |
| 6 | + * |
| 7 | + * Desired Behaviour: |
| 8 | + * Sites get every possible combination of their existing WLCG VO and tierN |
| 9 | + * scope tags in the new heirachical structure. |
| 10 | + * - a site with alice and tier1 should get alice.tier1 |
| 11 | + * - a site with alice, atlas and tier2 should get alice.tier2 and atlas.tier2 |
| 12 | + * - a site with atlas and tier2 should get atlas.tier2 |
| 13 | + * - a site with alice, tier1 and tier2 should get alice.tier1 and alice.tier2 |
| 14 | + * - a site with alice, atlas, tier1 and tier2 should get |
| 15 | + * - alice.tier1 |
| 16 | + * - alice.tier2 |
| 17 | + * - atlas.tier1 |
| 18 | + * - atlas.tier2 |
| 19 | + * |
| 20 | + * This script is intended to be one time use, i.e. once it has run to |
| 21 | + * completion - I don't anticpate needing the script again. |
| 22 | + * However, effort has been taken to ensure the script can be safely |
| 23 | + * run muliptle times - i.e. should a update randomly fail, the whole |
| 24 | + * script can be re-run. |
| 25 | + * |
| 26 | + * Usage: php resources/wlcg-scope-tag-migration/WLCGScopeTagAddRunner.php |
| 27 | + */ |
| 28 | + |
| 29 | +require_once dirname(__FILE__) . "/../../lib/Doctrine/bootstrap.php"; |
| 30 | +require dirname(__FILE__) . '/../../lib/Doctrine/bootstrap_doctrine.php'; |
| 31 | +require_once dirname(__FILE__) . '/../../lib/Gocdb_Services/Factory.php'; |
| 32 | + |
| 33 | +// This will supply the WLCG VO and tierN scope tags. |
| 34 | +require_once dirname(__FILE__) . "/WLCGScopeTagList.php"; |
| 35 | + |
| 36 | + |
| 37 | +$sectionBreak = "=========================================================\n"; |
| 38 | +$em = $entityManager; |
| 39 | + |
| 40 | +echo "Querying for all sites\n"; |
| 41 | +$siteDql = "SELECT s FROM Site s"; |
| 42 | +$allSitesList = $entityManager->createQuery($siteDql)->getResult(); |
| 43 | + |
| 44 | +echo "Starting update of Sites: " . date('D, d M Y H:i:s') . "\n"; |
| 45 | +echo $sectionBreak; |
| 46 | + |
| 47 | +foreach ($allSitesList as $site) { |
| 48 | + echo $site->getShortName() . ":\n"; |
| 49 | + |
| 50 | + $siteScopes = $site->getScopes()->toArray(); |
| 51 | + |
| 52 | + // Sites can support multiple CERN VOs - at any and multiple tiers. |
| 53 | + // Check each combination in turn. |
| 54 | + foreach ($wlcgScopesList as $wlcgScope) { |
| 55 | + foreach ($tierScopesList as $tierScope) { |
| 56 | + if ( |
| 57 | + (in_array($wlcgScope, $siteScopes)) && |
| 58 | + (in_array($tierScope, $siteScopes)) |
| 59 | + ) { |
| 60 | + $newScopeName = $wlcgScope . "." . $tierScope; |
| 61 | + |
| 62 | + // check if new scope has been already applied |
| 63 | + // (from a previous run). |
| 64 | + if (in_array($newScopeName, $siteScopes)) { |
| 65 | + echo "Skipping, site already has " . $newScopeName . "\n"; |
| 66 | + continue; |
| 67 | + } else { |
| 68 | + // apply new scope tag in a transaction. |
| 69 | + $em->getConnection()->beginTransaction(); |
| 70 | + try { |
| 71 | + // need the object not the name to add to a site. |
| 72 | + $site->addScope($allScopeDict[$newScopeName]); |
| 73 | + $em->merge($site); |
| 74 | + $em->flush(); |
| 75 | + $em->getConnection()->commit(); |
| 76 | + echo "Added " . $newScopeName . "\n"; |
| 77 | + } catch (\Exception $ex) { |
| 78 | + $em->getConnection()->rollback(); |
| 79 | + $em->close(); |
| 80 | + throw $ex; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +$em->flush(); |
| 89 | +echo "Completed ok: " . date('D, d M Y H:i:s') . "\n"; |
0 commit comments