blob: e68b6af0f7676dc1ce0d6df0bb7ce2c5ce87c894 [file] [log] [blame]
S.Çağlar Onura95895d2015-02-09 13:34:11 -05001#!/usr/bin/env python
jcnelson266113a2014-07-16 15:50:27 -04002
3import os
4import sys
5import base64
6import traceback
7
8if __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
Scott Baker76a840e2015-02-11 21:38:09 -080015 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")
jcnelson266113a2014-07-16 15:50:27 -040016
17from django.db.models import F, Q
Scott Baker76a840e2015-02-11 21:38:09 -080018from xos.config import Config
jcnelson266113a2014-07-16 15:50:27 -040019from observer.syncstep import SyncStep
20from core.models import Service
21
22import logging
23from logging import Logger
24logging.basicConfig( format='[%(levelname)s] [%(module)s:%(lineno)d] %(message)s' )
25logger = logging.getLogger()
26logger.setLevel( logging.INFO )
27
28# point to planetstack
29if __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
35from syndicate_storage.models import VolumeAccessRight
36
37# syndicatelib will be in stes/..
38parentdir = os.path.join(os.path.dirname(__file__),"..")
39sys.path.insert(0,parentdir)
40
41import syndicatelib
42
43class SyncVolumeAccessRight(SyncStep):
44 provides=[VolumeAccessRight]
45 requested_interval=0
46
47 def __init__(self, **args):
48 SyncStep.__init__(self, **args)
49
jcnelson266113a2014-07-16 15:50:27 -040050 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 Bhatia44be95e2014-07-23 10:46:56 -040092
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
jcnelson266113a2014-07-16 15:50:27 -040097
98
99if __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