Skip to content

Commit 2b8bf2c

Browse files
committed
gcp workspaces
1 parent 3fa3159 commit 2b8bf2c

File tree

6 files changed

+112
-8
lines changed

6 files changed

+112
-8
lines changed

Diff for: R/class_aws.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ store_delete_objects.tar_aws <- function(store, meta, batch_size, verbose) {
177177
store_aws_region(.x),
178178
store_aws_endpoint(.x),
179179
sep = "|"
180-
)
180+
) %||% NA_character_
181181
)
182+
meta <- meta[!is.na(meta$bucket_group), ]
182183
for (group in unique(meta$bucket_group)) {
183184
subset <- meta[meta$bucket_group == group,, drop = FALSE] # nolint
184185
example_path <- subset$path[[1L]]

Diff for: R/class_database_gcp.R

+60
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ database_gcp_class <- R6::R6Class(
6565
)
6666
invisible()
6767
},
68+
download_workspace = function(name, store, verbose = TRUE) {
69+
path <- path_workspace(store, name)
70+
key <- path_workspace(dirname(dirname(self$key)), name)
71+
gcp <- self$resources$gcp
72+
if (verbose) {
73+
tar_print(
74+
"Downloading gcp workspace file ",
75+
key,
76+
" to local file ",
77+
path
78+
)
79+
}
80+
dir_create(dirname(path))
81+
gcp_gcs_download(
82+
file = path,
83+
key = key,
84+
bucket = gcp$bucket,
85+
max_tries = gcp$max_tries %|||% 5L,
86+
verbose = gcp$verbose %|||% TRUE
87+
)
88+
invisible()
89+
},
6890
upload = function(verbose = TRUE) {
6991
if (verbose) {
7092
tar_print(
@@ -92,6 +114,22 @@ database_gcp_class <- R6::R6Class(
92114
)
93115
invisible()
94116
},
117+
upload_workspace = function(target, meta, reporter) {
118+
name <- target_get_name(target)
119+
path <- path_workspace(meta$store, name)
120+
key <- path_workspace(dirname(dirname(self$key)), name)
121+
gcp <- self$resources$gcp
122+
gcp_gcs_upload(
123+
file = path,
124+
key = key,
125+
bucket = gcp$bucket,
126+
predefined_acl = gcp$predefined_acl %|||% "private",
127+
max_tries = gcp$max_tries %|||% 5L,
128+
verbose = gcp$verbose %|||% TRUE
129+
)
130+
reporter$report_workspace_upload(target)
131+
invisible()
132+
},
95133
head = function() {
96134
gcp <- self$resources$gcp
97135
head <- gcp_gcs_head(
@@ -118,7 +156,29 @@ database_gcp_class <- R6::R6Class(
118156
max_tries = gcp$max_tries %|||% 5L,
119157
verbose = gcp$verbose %|||% TRUE
120158
)
159+
},
160+
delete_cloud_workspaces = function() {
161+
prefix <- dirname(path_workspace(dirname(dirname(self$key)), "x"))
162+
gcp <- self$resources$gcp
163+
keys <- names(
164+
gcp_gcs_list_md5s(
165+
prefix = prefix,
166+
bucket = gcp$bucket,
167+
verbose = FALSE,
168+
max_tries = gcp$max_tries %|||% 5L
169+
)
170+
)
171+
for (key in keys) {
172+
gcp_gcs_delete(
173+
key = key,
174+
bucket = gcp$bucket,
175+
verbose = FALSE,
176+
max_tries = gcp$max_tries %|||% 5L
177+
)
178+
}
179+
invisible()
121180
}
181+
122182
)
123183
)
124184
# nocov end

Diff for: R/class_gcp.R

+7-4
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,14 @@ store_delete_objects.tar_gcp <- function(store, meta, batch_size, verbose) {
132132
gcp <- store$resources$gcp
133133
meta$bucket_group <- map_chr(
134134
x = meta$path,
135-
f = ~paste(
136-
store_gcp_bucket(.x),
137-
sep = "|"
138-
)
135+
f = function(x) {
136+
paste(
137+
store_gcp_bucket(x),
138+
sep = "|"
139+
) %||% NA_character_
140+
}
139141
)
142+
meta <- meta[!is.na(meta$bucket_group), ]
140143
for (group in unique(meta$bucket_group)) {
141144
subset <- meta[meta$bucket_group == group,, drop = FALSE] # nolint
142145
for (index in seq_len(nrow(subset))) {

Diff for: targets.Rproj

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
Version: 1.0
2-
ProjectId: 509dc227-2bd1-454b-86f9-e6d90611f2e1
32

43
RestoreWorkspace: Default
54
SaveWorkspace: Default

Diff for: tests/aws/test-aws-workspaces.R

-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# Verify all `targets` buckets are deleted afterwards.
33
tar_test("aws workspaces are uploaded and downloaded", {
44
skip_if_no_aws()
5-
skip_if_not_installed("arrow")
65
s3 <- paws.storage::s3()
76
bucket_name <- random_bucket_name()
87
s3$create_bucket(Bucket = bucket_name)
@@ -16,7 +15,6 @@ tar_test("aws workspaces are uploaded and downloaded", {
1615
max_tries = 20
1716
)
1817
),
19-
format = "parquet",
2018
repository = "aws",
2119
repository_meta = "aws"
2220
)

Diff for: tests/gcp/test-gcp-workspaces.R

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Use sparingly to minimize gcp costs.
2+
# Verify all `targets` buckets are deleted afterwards.
3+
tar_test("gcp workspaces are uploaded and downloaded", {
4+
skip_if_no_gcp()
5+
skip_if_not_installed("arrow")
6+
bucket_name <- random_bucket_name()
7+
# needs to be a GCP project the tester auth has access to
8+
gcp_gcs_auth(max_tries = 5)
9+
project <- Sys.getenv("GCE_DEFAULT_PROJECT_ID")
10+
googleCloudStorageR::gcs_create_bucket(bucket_name, projectId = project)
11+
on.exit(gcp_gcs_delete_bucket(bucket_name))
12+
expr <- quote({
13+
tar_option_set(
14+
resources = tar_resources(
15+
gcp = tar_resources_gcp(
16+
bucket = !!bucket_name,
17+
prefix = "_targets",
18+
max_tries = 20
19+
)
20+
),
21+
repository = "gcp",
22+
repository_meta = "gcp"
23+
)
24+
list(
25+
tar_target(x, stop("this is an error"))
26+
)
27+
})
28+
expr <- tar_tidy_eval(expr, environment(), TRUE)
29+
eval(as.call(list(`tar_script`, expr, ask = FALSE)))
30+
path <- "_targets/workspaces/x"
31+
expect_false(gcp_gcs_exists(key = path, bucket = bucket_name))
32+
expect_error(tar_make(callr_function = NULL), class = "tar_condition_run")
33+
expect_true(gcp_gcs_exists(key = path, bucket = bucket_name))
34+
unlink(path)
35+
expect_false(file.exists(path))
36+
expect_error(tar_workspace(x), class = "tar_condition_validate")
37+
tar_workspace_download(x)
38+
expect_true(file.exists(path))
39+
expect_silent(tar_workspace(x))
40+
tar_destroy()
41+
expect_false(gcp_gcs_exists(key = path, bucket = bucket_name))
42+
expect_error(tar_workspace_download(x), class = "http_404")
43+
})

0 commit comments

Comments
 (0)