blob: 360cb6fb55349302ed3892b85f1452faf106e089 [file] [log] [blame]
jcnelson266113a2014-07-16 15:50:27 -04001#!/usr/bin/python
2
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
15 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
16
17from django.db.models import F, Q
18from planetstack.config import Config
19from 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
50 def fetch_pending(self):
51 return VolumeAccessRight.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
52
53 def sync_record(self, vac):
54
55 syndicate_caps = "UNKNOWN" # for exception handling
56
57 # get arguments
58 config = syndicatelib.get_config()
59 user_email = vac.owner_id.email
60 volume_name = vac.volume.name
61 syndicate_caps = syndicatelib.opencloud_caps_to_syndicate_caps( vac.cap_read_data, vac.cap_write_data, vac.cap_host_data )
62
63 logger.info( "Sync VolumeAccessRight for (%s, %s)" % (user_email, volume_name) )
64
65 # validate config
66 try:
67 RG_port = config.SYNDICATE_RG_DEFAULT_PORT
68 observer_secret = config.SYNDICATE_OPENCLOUD_SECRET
69 except Exception, e:
70 traceback.print_exc()
71 logger.error("syndicatelib config is missing SYNDICATE_RG_DEFAULT_PORT, SYNDICATE_OPENCLOUD_SECRET")
72 raise e
73
74 # ensure the user exists and has credentials
75 try:
76 rc, user = syndicatelib.ensure_principal_exists( user_email, observer_secret, is_admin=False, max_UGs=1100, max_RGs=1 )
77 assert rc is True, "Failed to ensure principal %s exists (rc = %s,%s)" % (user_email, rc, user)
78 except Exception, e:
79 traceback.print_exc()
80 logger.error("Failed to ensure user '%s' exists" % user_email )
81 raise e
82
83 # make the access right for the user to create their own UGs, and provision an RG for this user that will listen on localhost.
84 # the user will have to supply their own RG closure.
85 try:
86 rc = syndicatelib.setup_volume_access( user_email, volume_name, syndicate_caps, RG_port, observer_secret )
87 assert rc is True, "Failed to setup volume access for %s in %s" % (user_email, volume_name)
88
89 except Exception, e:
90 traceback.print_exc()
91 logger.error("Faoed to ensure user %s can access Volume %s with rights %s" % (user_email, volume_name, syndicate_caps))
92 raise e
93
94 return True
95
96
97if __name__ == "__main__":
98
99 # first, set all VolumeAccessRights to not-enacted so we can test
100 for v in VolumeAccessRight.objects.all():
101 v.enacted = None
102 v.save()
103
104 # NOTE: for resetting only
105 if len(sys.argv) > 1 and sys.argv[1] == "reset":
106 sys.exit(0)
107
108
109 sv = SyncVolumeAccessRight()
110 recs = sv.fetch_pending()
111
112 for rec in recs:
113 sv.sync_record( rec )
114