From dec26c32ebfc6988135ef7acee8941860e001d9f Mon Sep 17 00:00:00 2001 From: nkottary Date: Thu, 21 Jan 2016 11:37:45 +0530 Subject: [PATCH] Added Google cloud storage plugin --- engine/conf/tornado.conf.tpl | 1 + engine/src/juliabox/cloud/compute.py | 5 +- .../juliabox/plugins/bucket_gs/__init__.py | 3 + .../src/juliabox/plugins/bucket_gs/impl_gs.py | 71 +++++++++++++++++++ 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 engine/src/juliabox/plugins/bucket_gs/__init__.py create mode 100644 engine/src/juliabox/plugins/bucket_gs/impl_gs.py diff --git a/engine/conf/tornado.conf.tpl b/engine/conf/tornado.conf.tpl index 93be1cb..0cc801e 100644 --- a/engine/conf/tornado.conf.tpl +++ b/engine/conf/tornado.conf.tpl @@ -134,6 +134,7 @@ Welcome to JuliaBox. We hope you will like it and also share with your friends. "juliabox.plugins.sendmail_ses", "juliabox.plugins.api_admin", "juliabox.plugins.user_admin", + "juliabox.plugins.bucket_gs", "" ], diff --git a/engine/src/juliabox/cloud/compute.py b/engine/src/juliabox/cloud/compute.py index fa73740..2322373 100644 --- a/engine/src/juliabox/cloud/compute.py +++ b/engine/src/juliabox/cloud/compute.py @@ -9,7 +9,7 @@ class JBPluginCloud(LoggerMixin): """ Interfaces with cloud service providers or provides similar services locally. - - `JBPluginCloud.JBP_BUCKETSTORE`, `JBPluginCloud.JBP_BUCKETSTORE_S3`: + - `JBPluginCloud.JBP_BUCKETSTORE`, `JBPluginCloud.JBP_BUCKETSTORE_S3`, `JBPluginCloud.JBP_BUCKETSTORE_GS`: Provides storage for blobs of data in named buckets. Similar to Amazon S3 or OpenStack Swift. - `push(bucket, local_file, metadata=None)` - `pull(bucket, local_file, metadata_only=False)` @@ -52,6 +52,7 @@ class JBPluginCloud(LoggerMixin): JBP_BUCKETSTORE = "cloud.bucketstore" JBP_BUCKETSTORE_S3 = "cloud.bucketstore.s3" + JBP_BUCKETSTORE_GS = "cloud.bucketstore.gs" JBP_DNS = "cloud.dns" JBP_DNS_ROUTE53 = "cloud.dns.route53" @@ -171,4 +172,4 @@ def deregister_instance_dns(): plugin = JBPluginCloud.jbox_get_plugin(JBPluginCloud.JBP_DNS) if plugin is None: return - plugin.delete_cname(Compute.get_alias_hostname()) \ No newline at end of file + plugin.delete_cname(Compute.get_alias_hostname()) diff --git a/engine/src/juliabox/plugins/bucket_gs/__init__.py b/engine/src/juliabox/plugins/bucket_gs/__init__.py new file mode 100644 index 0000000..45238af --- /dev/null +++ b/engine/src/juliabox/plugins/bucket_gs/__init__.py @@ -0,0 +1,3 @@ +__author__ = 'Nishanth' + +from impl_gs import JBoxGS diff --git a/engine/src/juliabox/plugins/bucket_gs/impl_gs.py b/engine/src/juliabox/plugins/bucket_gs/impl_gs.py new file mode 100644 index 0000000..91fef18 --- /dev/null +++ b/engine/src/juliabox/plugins/bucket_gs/impl_gs.py @@ -0,0 +1,71 @@ +__author__ = 'Nishanth' + +import os +import boto +from boto.gs.key import Key +import gcs_oauth2_boto_plugin +from juliabox.cloud import JBPluginCloud + +class JBoxGS(JBPluginCloud): + provides = [JBPluginCloud.JBP_BUCKETSTORE, JBPluginCloud.JBP_BUCKETSTORE_GS] + CONN = None + BUCKETS = dict() + + @staticmethod + def connect(): + if JBoxGS.CONN is None: + JBoxGS.CONN = boto.connect_gs() + return JBoxGS.CONN + + @staticmethod + def connect_bucket(bucket): + if bucket not in JBoxGS.BUCKETS: + JBoxGS.BUCKETS[bucket] = JBoxGS.connect().get_bucket(bucket) + return JBoxGS.BUCKETS[bucket] + + @staticmethod + def push(bucket, local_file, metadata=None): + key_name = os.path.basename(local_file) + k = Key(JBoxGS.connect_bucket(bucket)) + k.key = key_name + if metadata is not None: + for meta_name, meta_value in metadata.iteritems(): + k.set_metadata(meta_name, meta_value) + k.set_contents_from_filename(local_file) + return k + + @staticmethod + def pull(bucket, local_file, metadata_only=False): + key_name = os.path.basename(local_file) + k = JBoxGS.connect_bucket(bucket).get_key(key_name) + if (k is not None) and (not metadata_only): + k.get_contents_to_filename(local_file) + return k + + @staticmethod + def delete(bucket, local_file): + key_name = os.path.basename(local_file) + k = JBoxGS.connect_bucket(bucket).delete_key(key_name) + return k + + @staticmethod + def copy(from_file, to_file, from_bucket, to_bucket=None): + if to_bucket is None: + to_bucket = from_bucket + + from_key_name = os.path.basename(from_file) + to_key_name = os.path.basename(to_file) + + k = JBoxGS.connect_bucket(from_bucket).get_key(from_key_name) + if k is None: + return None + k_new = k.copy(to_bucket, to_key_name) + return k_new + + @staticmethod + def move(from_file, to_file, from_bucket, to_bucket=None): + k_new = JBoxGS.copy(from_file, to_file, from_bucket, to_bucket) + if k_new is None: + return None + JBoxGS.delete(from_bucket, from_file) + return k_new