8 if __name__ == "__main__":
10 if os.getenv("OPENCLOUD_PYTHONPATH"):
11 sys.path.append( os.getenv("OPENCLOUD_PYTHONPATH") )
13 print >> sys.stderr, "No OPENCLOUD_PYTHONPATH variable set. Assuming that OpenCloud is in PYTHONPATH"
15 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
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
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 )
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"))
33 logger.warning("No OPENCLOUD_PYTHONPATH set; assuming your PYTHONPATH works")
35 from syndicate_storage.models import VolumeAccessRight
37 # syndicatelib will be in stes/..
38 parentdir = os.path.join(os.path.dirname(__file__),"..")
39 sys.path.insert(0,parentdir)
43 class SyncVolumeAccessRight(SyncStep):
44 provides=[VolumeAccessRight]
47 def __init__(self, **args):
48 SyncStep.__init__(self, **args)
50 def sync_record(self, vac):
52 syndicate_caps = "UNKNOWN" # for exception handling
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 )
60 logger.info( "Sync VolumeAccessRight for (%s, %s)" % (user_email, volume_name) )
64 RG_port = config.SYNDICATE_RG_DEFAULT_PORT
65 observer_secret = config.SYNDICATE_OPENCLOUD_SECRET
68 logger.error("syndicatelib config is missing SYNDICATE_RG_DEFAULT_PORT, SYNDICATE_OPENCLOUD_SECRET")
71 # ensure the user exists and has credentials
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)
77 logger.error("Failed to ensure user '%s' exists" % user_email )
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.
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)
88 logger.error("Faoed to ensure user %s can access Volume %s with rights %s" % (user_email, volume_name, syndicate_caps))
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):
99 if __name__ == "__main__":
101 # first, set all VolumeAccessRights to not-enacted so we can test
102 for v in VolumeAccessRight.objects.all():
106 # NOTE: for resetting only
107 if len(sys.argv) > 1 and sys.argv[1] == "reset":
111 sv = SyncVolumeAccessRight()
112 recs = sv.fetch_pending()
115 sv.sync_record( rec )