S.Çağlar Onur | 3e92b4d | 2015-02-09 13:34:11 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
jcnelson | 266113a | 2014-07-16 15:50:27 -0400 | [diff] [blame] | 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | import base64 |
| 6 | import traceback |
| 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 | from django.db.models import F, Q |
| 18 | from planetstack.config import Config |
| 19 | from observer.syncstep import SyncStep |
| 20 | from core.models import Service |
| 21 | |
| 22 | import logging |
| 23 | from logging import Logger |
| 24 | logging.basicConfig( format='[%(levelname)s] [%(module)s:%(lineno)d] %(message)s' ) |
| 25 | logger = logging.getLogger() |
| 26 | logger.setLevel( logging.INFO ) |
| 27 | |
| 28 | # point to planetstack |
| 29 | if __name__ != "__main__": |
| 30 | if os.getenv("OPENCLOUD_PYTHONPATH") is not None: |
| 31 | sys.path.insert(0, os.getenv("OPENCLOUD_PYTHONPATH")) |
| 32 | else: |
| 33 | logger.warning("No OPENCLOUD_PYTHONPATH set; assuming your PYTHONPATH works") |
| 34 | |
| 35 | from syndicate_storage.models import VolumeAccessRight |
| 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 | class SyncVolumeAccessRight(SyncStep): |
| 44 | provides=[VolumeAccessRight] |
| 45 | requested_interval=0 |
| 46 | |
| 47 | def __init__(self, **args): |
| 48 | SyncStep.__init__(self, **args) |
| 49 | |
jcnelson | 266113a | 2014-07-16 15:50:27 -0400 | [diff] [blame] | 50 | def sync_record(self, vac): |
| 51 | |
| 52 | syndicate_caps = "UNKNOWN" # for exception handling |
| 53 | |
| 54 | # get arguments |
| 55 | config = syndicatelib.get_config() |
| 56 | user_email = vac.owner_id.email |
| 57 | volume_name = vac.volume.name |
| 58 | syndicate_caps = syndicatelib.opencloud_caps_to_syndicate_caps( vac.cap_read_data, vac.cap_write_data, vac.cap_host_data ) |
| 59 | |
| 60 | logger.info( "Sync VolumeAccessRight for (%s, %s)" % (user_email, volume_name) ) |
| 61 | |
| 62 | # validate config |
| 63 | try: |
| 64 | RG_port = config.SYNDICATE_RG_DEFAULT_PORT |
| 65 | observer_secret = config.SYNDICATE_OPENCLOUD_SECRET |
| 66 | except Exception, e: |
| 67 | traceback.print_exc() |
| 68 | logger.error("syndicatelib config is missing SYNDICATE_RG_DEFAULT_PORT, SYNDICATE_OPENCLOUD_SECRET") |
| 69 | raise e |
| 70 | |
| 71 | # ensure the user exists and has credentials |
| 72 | try: |
| 73 | rc, user = syndicatelib.ensure_principal_exists( user_email, observer_secret, is_admin=False, max_UGs=1100, max_RGs=1 ) |
| 74 | assert rc is True, "Failed to ensure principal %s exists (rc = %s,%s)" % (user_email, rc, user) |
| 75 | except Exception, e: |
| 76 | traceback.print_exc() |
| 77 | logger.error("Failed to ensure user '%s' exists" % user_email ) |
| 78 | raise e |
| 79 | |
| 80 | # make the access right for the user to create their own UGs, and provision an RG for this user that will listen on localhost. |
| 81 | # the user will have to supply their own RG closure. |
| 82 | try: |
| 83 | rc = syndicatelib.setup_volume_access( user_email, volume_name, syndicate_caps, RG_port, observer_secret ) |
| 84 | assert rc is True, "Failed to setup volume access for %s in %s" % (user_email, volume_name) |
| 85 | |
| 86 | except Exception, e: |
| 87 | traceback.print_exc() |
| 88 | logger.error("Faoed to ensure user %s can access Volume %s with rights %s" % (user_email, volume_name, syndicate_caps)) |
| 89 | raise e |
| 90 | |
| 91 | return True |
Sapan Bhatia | 44be95e | 2014-07-23 10:46:56 -0400 | [diff] [blame] | 92 | |
| 93 | # Jude: this will simply go on to purge the object from |
| 94 | # OpenCloud. The previous 'deleter' version was a no-op also. |
| 95 | def delete_record(self, obj): |
| 96 | pass |
jcnelson | 266113a | 2014-07-16 15:50:27 -0400 | [diff] [blame] | 97 | |
| 98 | |
| 99 | if __name__ == "__main__": |
| 100 | |
| 101 | # first, set all VolumeAccessRights to not-enacted so we can test |
| 102 | for v in VolumeAccessRight.objects.all(): |
| 103 | v.enacted = None |
| 104 | v.save() |
| 105 | |
| 106 | # NOTE: for resetting only |
| 107 | if len(sys.argv) > 1 and sys.argv[1] == "reset": |
| 108 | sys.exit(0) |
| 109 | |
| 110 | |
| 111 | sv = SyncVolumeAccessRight() |
| 112 | recs = sv.fetch_pending() |
| 113 | |
| 114 | for rec in recs: |
| 115 | sv.sync_record( rec ) |
| 116 | |