jcnelson | 266113a | 2014-07-16 15:50:27 -0400 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | import traceback |
| 6 | import base64 |
| 7 | |
| 8 | if __name__ == "__main__": |
| 9 | # for testing |
| 10 | if os.getenv("OPENCLOUD_PYTHONPATH"): |
| 11 | sys.path.append( os.getenv("OPENCLOUD_PYTHONPATH") ) |
| 12 | else: |
| 13 | print >> sys.stderr, "No OPENCLOUD_PYTHONPATH variable set. Assuming that OpenCloud is in PYTHONPATH" |
| 14 | |
| 15 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings") |
| 16 | |
| 17 | |
| 18 | from django.db.models import F, Q |
| 19 | from planetstack.config import Config |
| 20 | from observer.syncstep import SyncStep |
| 21 | from core.models import Service |
| 22 | from syndicate_storage.models import Volume |
| 23 | |
| 24 | import logging |
| 25 | from logging import Logger |
| 26 | logging.basicConfig( format='[%(levelname)s] [%(module)s:%(lineno)d] %(message)s' ) |
| 27 | logger = logging.getLogger() |
| 28 | logger.setLevel( logging.INFO ) |
| 29 | |
| 30 | # point to planetstack |
| 31 | if __name__ != "__main__": |
| 32 | if os.getenv("OPENCLOUD_PYTHONPATH") is not None: |
| 33 | sys.path.insert(0, os.getenv("OPENCLOUD_PYTHONPATH")) |
| 34 | else: |
| 35 | logger.warning("No OPENCLOUD_PYTHONPATH set; assuming your PYTHONPATH works") |
| 36 | |
| 37 | # syndicatelib will be in stes/.. |
| 38 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 39 | sys.path.insert(0,parentdir) |
| 40 | |
| 41 | import syndicatelib |
| 42 | |
| 43 | |
| 44 | class SyncVolume(SyncStep): |
| 45 | provides=[Volume] |
| 46 | requested_interval=0 |
| 47 | |
| 48 | def __init__(self, **args): |
| 49 | SyncStep.__init__(self, **args) |
| 50 | |
| 51 | def fetch_pending(self): |
| 52 | try: |
| 53 | ret = Volume.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 54 | return ret |
| 55 | except Exception, e: |
| 56 | traceback.print_exc() |
| 57 | return None |
| 58 | |
| 59 | def sync_record(self, volume): |
| 60 | """ |
| 61 | Synchronize a Volume record with Syndicate. |
| 62 | """ |
| 63 | |
| 64 | logger.info( "Sync Volume = %s\n\n" % volume.name ) |
| 65 | |
| 66 | user_email = volume.owner_id.email |
| 67 | config = syndicatelib.get_config() |
| 68 | |
| 69 | volume_principal_id = syndicatelib.make_volume_principal_id( user_email, volume.name ) |
| 70 | |
| 71 | # get the observer secret |
| 72 | try: |
| 73 | observer_secret = config.SYNDICATE_OPENCLOUD_SECRET |
| 74 | except Exception, e: |
| 75 | traceback.print_exc() |
| 76 | logger.error("config is missing SYNDICATE_OPENCLOUD_SECRET") |
| 77 | raise e |
| 78 | |
| 79 | # volume owner must exist as a Syndicate user... |
| 80 | try: |
| 81 | rc, user = syndicatelib.ensure_principal_exists( volume_principal_id, observer_secret, is_admin=False, max_UGs=1100, max_RGs=1) |
| 82 | assert rc == True, "Failed to create or read volume principal '%s'" % volume_principal_id |
| 83 | except Exception, e: |
| 84 | traceback.print_exc() |
| 85 | logger.error("Failed to ensure principal '%s' exists" % volume_principal_id ) |
| 86 | raise e |
| 87 | |
| 88 | # volume must exist |
| 89 | |
| 90 | # create or update the Volume |
| 91 | try: |
| 92 | new_volume = syndicatelib.ensure_volume_exists( volume_principal_id, volume, user=user ) |
| 93 | except Exception, e: |
| 94 | traceback.print_exc() |
| 95 | logger.error("Failed to ensure volume '%s' exists" % volume.name ) |
| 96 | raise e |
| 97 | |
| 98 | # did we create the Volume? |
| 99 | if new_volume is not None: |
| 100 | # we're good |
| 101 | pass |
| 102 | |
| 103 | # otherwise, just update it |
| 104 | else: |
| 105 | try: |
| 106 | rc = syndicatelib.update_volume( volume ) |
| 107 | except Exception, e: |
| 108 | traceback.print_exc() |
| 109 | logger.error("Failed to update volume '%s', exception = %s" % (volume.name, e.message)) |
| 110 | raise e |
| 111 | |
| 112 | return True |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | if __name__ == "__main__": |
| 118 | sv = SyncVolume() |
| 119 | |
| 120 | |
| 121 | # first, set all volumes to not-enacted so we can test |
| 122 | for v in Volume.objects.all(): |
| 123 | v.enacted = None |
| 124 | v.save() |
| 125 | |
| 126 | # NOTE: for resetting only |
| 127 | if len(sys.argv) > 1 and sys.argv[1] == "reset": |
| 128 | sys.exit(0) |
| 129 | |
| 130 | recs = sv.fetch_pending() |
| 131 | |
| 132 | for rec in recs: |
| 133 | rc = sv.sync_record( rec ) |
| 134 | if not rc: |
| 135 | print "\n\nFailed to sync %s\n\n" % (rec.name) |
| 136 | |