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")
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
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 )
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"))
35 logger.warning("No OPENCLOUD_PYTHONPATH set; assuming your PYTHONPATH works")
37 # syndicatelib will be in stes/..
38 parentdir = os.path.join(os.path.dirname(__file__),"..")
39 sys.path.insert(0,parentdir)
44 class SyncVolume(SyncStep):
48 def __init__(self, **args):
49 SyncStep.__init__(self, **args)
51 def sync_record(self, volume):
53 Synchronize a Volume record with Syndicate.
56 logger.info( "Sync Volume = %s\n\n" % volume.name )
58 user_email = volume.owner_id.email
59 config = syndicatelib.get_config()
61 volume_principal_id = syndicatelib.make_volume_principal_id( user_email, volume.name )
63 # get the observer secret
65 observer_secret = config.SYNDICATE_OPENCLOUD_SECRET
68 logger.error("config is missing SYNDICATE_OPENCLOUD_SECRET")
71 # volume owner must exist as a Syndicate user...
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
77 logger.error("Failed to ensure principal '%s' exists" % volume_principal_id )
82 # create or update the Volume
84 new_volume = syndicatelib.ensure_volume_exists( volume_principal_id, volume, user=user )
87 logger.error("Failed to ensure volume '%s' exists" % volume.name )
90 # did we create the Volume?
91 if new_volume is not None:
95 # otherwise, just update it
98 rc = syndicatelib.update_volume( volume )
100 traceback.print_exc()
101 logger.error("Failed to update volume '%s', exception = %s" % (volume.name, e.message))
106 def delete_record(self, volume):
108 volume_name = volume.name
109 syndicatelib.ensure_volume_absent( volume_name )
111 traceback.print_exc()
112 logger.exception("Failed to erase volume '%s'" % volume_name)
119 if __name__ == "__main__":
123 # first, set all volumes to not-enacted so we can test
124 for v in Volume.objects.all():
128 # NOTE: for resetting only
129 if len(sys.argv) > 1 and sys.argv[1] == "reset":
132 recs = sv.fetch_pending()
135 rc = sv.sync_record( rec )
137 print "\n\nFailed to sync %s\n\n" % (rec.name)