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 | |
jcnelson | 266113a | 2014-07-16 15:50:27 -0400 | [diff] [blame] | 51 | def sync_record(self, volume): |
| 52 | """ |
| 53 | Synchronize a Volume record with Syndicate. |
| 54 | """ |
| 55 | |
| 56 | logger.info( "Sync Volume = %s\n\n" % volume.name ) |
| 57 | |
| 58 | user_email = volume.owner_id.email |
| 59 | config = syndicatelib.get_config() |
| 60 | |
| 61 | volume_principal_id = syndicatelib.make_volume_principal_id( user_email, volume.name ) |
| 62 | |
| 63 | # get the observer secret |
| 64 | try: |
| 65 | observer_secret = config.SYNDICATE_OPENCLOUD_SECRET |
| 66 | except Exception, e: |
| 67 | traceback.print_exc() |
| 68 | logger.error("config is missing SYNDICATE_OPENCLOUD_SECRET") |
| 69 | raise e |
| 70 | |
| 71 | # volume owner must exist as a Syndicate user... |
| 72 | try: |
| 73 | rc, user = syndicatelib.ensure_principal_exists( volume_principal_id, observer_secret, is_admin=False, max_UGs=1100, max_RGs=1) |
| 74 | assert rc == True, "Failed to create or read volume principal '%s'" % volume_principal_id |
| 75 | except Exception, e: |
| 76 | traceback.print_exc() |
| 77 | logger.error("Failed to ensure principal '%s' exists" % volume_principal_id ) |
| 78 | raise e |
| 79 | |
| 80 | # volume must exist |
| 81 | |
| 82 | # create or update the Volume |
| 83 | try: |
| 84 | new_volume = syndicatelib.ensure_volume_exists( volume_principal_id, volume, user=user ) |
| 85 | except Exception, e: |
| 86 | traceback.print_exc() |
| 87 | logger.error("Failed to ensure volume '%s' exists" % volume.name ) |
| 88 | raise e |
| 89 | |
| 90 | # did we create the Volume? |
| 91 | if new_volume is not None: |
| 92 | # we're good |
| 93 | pass |
| 94 | |
| 95 | # otherwise, just update it |
| 96 | else: |
| 97 | try: |
| 98 | rc = syndicatelib.update_volume( volume ) |
| 99 | except Exception, e: |
| 100 | traceback.print_exc() |
| 101 | logger.error("Failed to update volume '%s', exception = %s" % (volume.name, e.message)) |
| 102 | raise e |
| 103 | |
| 104 | return True |
Sapan Bhatia | bdd1a93 | 2014-07-23 10:50:51 -0400 | [diff] [blame] | 105 | |
| 106 | def delete_record(self, volume): |
| 107 | try: |
| 108 | volume_name = volume.name |
| 109 | syndicatelib.ensure_volume_absent( volume_name ) |
| 110 | except Exception, e: |
| 111 | traceback.print_exc() |
| 112 | logger.exception("Failed to erase volume '%s'" % volume_name) |
| 113 | raise e |
| 114 | |
jcnelson | 266113a | 2014-07-16 15:50:27 -0400 | [diff] [blame] | 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | if __name__ == "__main__": |
| 120 | sv = SyncVolume() |
| 121 | |
| 122 | |
| 123 | # first, set all volumes to not-enacted so we can test |
| 124 | for v in Volume.objects.all(): |
| 125 | v.enacted = None |
| 126 | v.save() |
| 127 | |
| 128 | # NOTE: for resetting only |
| 129 | if len(sys.argv) > 1 and sys.argv[1] == "reset": |
| 130 | sys.exit(0) |
| 131 | |
| 132 | recs = sv.fetch_pending() |
| 133 | |
| 134 | for rec in recs: |
| 135 | rc = sv.sync_record( rec ) |
| 136 | if not rc: |
| 137 | print "\n\nFailed to sync %s\n\n" % (rec.name) |
| 138 | |