-
Notifications
You must be signed in to change notification settings - Fork 510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Storage] R2 Cloud Object Storage integration #1736
Merged
landscapepainter
merged 100 commits into
skypilot-org:master
from
landscapepainter:r2-integration
Mar 26, 2023
Merged
Changes from 46 commits
Commits
Show all changes
100 commits
Select commit
Hold shift + click to select a range
987b0bb
first r2 integration code added
fba15d0
minor fix
a3de58d
integrating R2 as part of TestStorageCredentials
4d655ae
session error fix
f4e4f87
session var fix
df6f527
fix test_public_bucket
aa2b244
temp change gs for test
f3dc326
debug print
d4f1be1
print handle check
dfb995e
print update
e90af35
debug print _add_store
e8ffe13
debug update
ef66f56
update debug
f1e589c
test_smoke_change
0417e63
test bulk deletion
ffcbbad
debug logger
f56fb31
debug
487525a
dbg
7e915ef
dbg
769a470
dbg
27b2953
dbg
ff0da6e
dbg
3848aaa
dbg
0313246
dbg
6bc4291
dbg
a7d4a51
dbg
ae03628
dbg
b345459
chage R2Store parent
164a9e2
dbg test_private_bucket
f875bff
fix_nonexistent_bucket test
053e33f
fix cli_ls_cmd
2cf01a3
fix tmp_awscli_bucket_r2
0c62cb0
fix2 tmp_awscli_bucket_r2 and nonexistent test
22ba2a7
quick fix
70d78ad
nonexistent test fix
87c98bd
add comment
e0f7eed
storage.py order change
5909122
add gs:// to test_smoke
7a8916f
pylint error fix
3d7407b
fix format
0014838
first review fix
570ec51
formatted
landscapepainter 5ce573d
resolve merge conflict
c83faae
commit before resolving merge confilct
75ffa92
'origin' to r2-integration resolve merge conflict
e3c77b2
adding instructions for R2 in docs
02d52e6
Merge remote-tracking branch 'origin' into r2-integration
landscapepainter 32556aa
gs test failure fix
landscapepainter 19ced23
undo installation doc.
landscapepainter 95f2959
Merge branch 'master' into r2-integration
landscapepainter 53ddf90
adding **kwargs adaptor/cloudflare.py
landscapepainter 5ac2bb8
undo **kwargs
landscapepainter 8c191fa
update kwargs and delete_r2_bucket
landscapepainter 3bab24c
update
landscapepainter bfeccb0
check if R2 test skip works
landscapepainter 1f690a1
testing for failure
landscapepainter e15e084
test list comp.
landscapepainter 95e6547
testing
landscapepainter 7f173fb
testing
landscapepainter 0a99bfa
testing
landscapepainter 2207d72
test delete_r2 change
landscapepainter b54b614
test skipif syntax
landscapepainter fc1a645
test
landscapepainter 03ec8c3
test
landscapepainter 91609a6
test
landscapepainter 2a135d8
test
landscapepainter 3e952b8
check if conditional skipping works
landscapepainter ebc63c4
check if conditionally skipping works
landscapepainter 99ab367
test
landscapepainter 3924cfd
test
landscapepainter bd82146
test
landscapepainter 8ae98b7
test
landscapepainter 6a1f346
test
landscapepainter 3071499
test
landscapepainter 8a8f54a
format
landscapepainter 779c200
reverting back to conditional skip
landscapepainter a918c37
revert back to conditional skip
landscapepainter 3f5ab45
debug
landscapepainter 90ab019
format
landscapepainter 0708cad
Merge branch 'r2-integration' of https://github.com/landscapepainter/…
landscapepainter 6802426
r2 multi-file support COPY
landscapepainter 1bdc992
Merge remote-tracking branch 'origin/r2-integration' into r2-integration
landscapepainter cfc7c2d
multifile COPY R2 Fix
landscapepainter 9146395
fix
landscapepainter 5277e59
fix
landscapepainter 12789e1
fix
landscapepainter fa7f4df
format fix
landscapepainter 6011108
add r2 storage mounting test
landscapepainter 060a54e
fix indent
landscapepainter 609ad95
fix bug
landscapepainter cab4df0
test fix and format
landscapepainter c7e0a2b
Merge branch 'master' into r2-integration
landscapepainter 9667d94
fix generic_cloud
landscapepainter 01cf583
update --cloudflare functionality
landscapepainter 6167ebb
format
landscapepainter a6a1249
fix conftest
landscapepainter 1b7475e
fixes
landscapepainter f640db8
comment on conftest.py
landscapepainter bc59e9d
minor fix
landscapepainter 28b3ea6
format
landscapepainter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
"""Cloudflare cloud adaptors""" | ||
|
||
# pylint: disable=import-outside-toplevel | ||
|
||
import functools | ||
import threading | ||
import os | ||
|
||
boto3 = None | ||
botocore = None | ||
_session_creation_lock = threading.RLock() | ||
ACCOUNT_ID_PATH = '~/.cloudflare/accountid' | ||
R2_PROFILE_NAME = 'r2' | ||
|
||
|
||
def import_package(func): | ||
|
||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs): | ||
global boto3, botocore | ||
if boto3 is None or botocore is None: | ||
try: | ||
import boto3 as _boto3 | ||
import botocore as _botocore | ||
boto3 = _boto3 | ||
botocore = _botocore | ||
except ImportError: | ||
raise ImportError('Fail to import dependencies for Cloudflare.' | ||
'Try pip install "skypilot[aws]"') from None | ||
return func(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
|
||
# lru_cache() is thread-safe and it will return the same session object | ||
# for different threads. | ||
# Reference: https://docs.python.org/3/library/functools.html#functools.lru_cache # pylint: disable=line-too-long | ||
@functools.lru_cache() | ||
@import_package | ||
def session(): | ||
"""Create an AWS session.""" | ||
# Creating the session object is not thread-safe for boto3, | ||
# so we add a reentrant lock to synchronize the session creation. | ||
# Reference: https://github.com/boto/boto3/issues/1592 | ||
# However, the session object itself is thread-safe, so we are | ||
# able to use lru_cache() to cache the session object. | ||
with _session_creation_lock: | ||
return boto3.session.Session(profile_name=R2_PROFILE_NAME) | ||
|
||
|
||
@functools.lru_cache() | ||
@import_package | ||
def resource(resource_name: str): | ||
"""Create an Cloudflare resource. | ||
|
||
Args: | ||
resource_name: Cloudflare resource name (e.g., 's3'). | ||
kwargs: Other options. | ||
""" | ||
# Need to use the resource retrieved from the per-thread session | ||
# to avoid thread-safety issues (Directly creating the client | ||
# with boto3.resource() is not thread-safe). | ||
# Reference: https://stackoverflow.com/a/59635814 | ||
|
||
session_ = session() | ||
cloudflare_credentials = session_.get_credentials().get_frozen_credentials() | ||
endpoint = create_endpoint() | ||
|
||
return session_.resource( | ||
resource_name, | ||
endpoint_url=endpoint, | ||
aws_access_key_id=cloudflare_credentials.access_key, | ||
aws_secret_access_key=cloudflare_credentials.secret_key, | ||
region_name='auto') | ||
|
||
|
||
@functools.lru_cache() | ||
def client(service_name: str, region): | ||
"""Create an CLOUDFLARE client of a certain service. | ||
|
||
Args: | ||
service_name: CLOUDFLARE service name (e.g., 's3'). | ||
kwargs: Other options. | ||
""" | ||
# Need to use the client retrieved from the per-thread session | ||
# to avoid thread-safety issues (Directly creating the client | ||
# with boto3.client() is not thread-safe). | ||
# Reference: https://stackoverflow.com/a/59635814 | ||
|
||
session_ = session() | ||
cloudflare_credentials = session_.get_credentials().get_frozen_credentials() | ||
endpoint = create_endpoint() | ||
|
||
return session_.client( | ||
service_name, | ||
endpoint_url=endpoint, | ||
aws_access_key_id=cloudflare_credentials.access_key, | ||
aws_secret_access_key=cloudflare_credentials.secret_key, | ||
region_name=region) | ||
|
||
|
||
@import_package | ||
def botocore_exceptions(): | ||
"""AWS botocore exception.""" | ||
from botocore import exceptions | ||
return exceptions | ||
|
||
|
||
def create_endpoint(): | ||
"""Reads accountid necessary to interact with R2""" | ||
|
||
accountid_path = os.path.expanduser(ACCOUNT_ID_PATH) | ||
with open(accountid_path, 'r') as f: | ||
lines = f.readlines() | ||
accountid = lines[0] | ||
|
||
accountid = accountid.strip() | ||
endpoint = 'https://' + accountid + '.r2.cloudflarestorage.com' | ||
|
||
return endpoint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: *a cloudflare