forked from PelicanPlatform/pelican
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_sync.go
More file actions
222 lines (200 loc) · 6.19 KB
/
object_sync.go
File metadata and controls
222 lines (200 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/***************************************************************
*
* Copyright (C) 2025, Pelican Project, Morgridge Institute for Research
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************/
package main
import (
"net/url"
"os"
"strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/pelicanplatform/pelican/client"
"github.com/pelicanplatform/pelican/config"
"github.com/pelicanplatform/pelican/error_codes"
"github.com/pelicanplatform/pelican/param"
)
var (
syncCmd = &cobra.Command{
Use: "sync {source ...} {destination}",
Short: "Sync a directory to or from a Pelican federation",
Run: syncMain,
PreRun: func(cmd *cobra.Command, args []string) {
commaFlagsListToViperSlice(cmd, map[string]string{"cache": param.Client_PreferredCaches.GetName()})
},
}
)
func init() {
flagSet := syncCmd.Flags()
flagSet.StringP("cache", "c", "", `A comma-separated list of preferred caches to try for the transfer, where a "+" in the list indicates
the client should fallback to discovered caches if all preferred caches fail.`)
flagSet.StringP("token", "t", "", "Token file to use for transfer")
objectCmd.AddCommand(syncCmd)
}
func getLastScheme(scheme string) string {
idx := strings.LastIndex(scheme, "+")
if idx == -1 {
return scheme
}
return scheme[idx+1:]
}
// Returns true if the input is a url-like object that
// pelican can consume.
//
// Schemes we understand are "osdf", "pelican",
// "foo+osdf", or "foo+pelican" where "foo" is some arbitrary
// prefix not containing a "/"
func isPelicanUrl(input string) bool {
prefix, _, found := strings.Cut(input, "://")
if !found {
return false
}
if strings.Contains(prefix, "/") {
return false
}
scheme := getLastScheme(prefix)
if scheme != "pelican" && scheme != "osdf" {
return false
}
if _, err := url.Parse(input); err != nil {
return false
}
return true
}
func syncMain(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
err := config.InitClient()
if err != nil {
log.Errorln(err)
if client.IsRetryable(err) {
log.Errorln("Errors are retryable")
os.Exit(11)
} else {
os.Exit(1)
}
}
tokenLocation, _ := cmd.Flags().GetString("token")
pb := newProgressBar()
defer pb.shutdown()
// Check if the program was executed from a terminal
// https://rosettacode.org/wiki/Check_output_device_is_a_terminal#Go
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode()&os.ModeCharDevice) != 0 && param.Logging_LogLocation.GetString() == "" && !param.Logging_DisableProgressBars.GetBool() {
pb.launchDisplay(ctx)
}
if len(args) < 2 {
log.Errorln("No source or destination to sync")
err = cmd.Help()
if err != nil {
log.Errorln("Failed to print out help:", err)
}
os.Exit(1)
}
sources := args[:len(args)-1]
dest := args[len(args)-1]
doDownload := false
if isPelicanUrl(dest) {
for _, src := range sources {
if isPelicanUrl(src) {
log.Errorf("URL (%s) cannot be a source when synchronizing to a federation URL", src)
os.Exit(1)
}
}
log.Debugln("Synchronizing to a Pelican data federation")
} else {
if !isPelicanUrl(sources[0]) {
log.Errorln("Either the first or last argument must be a pelican:// or osdf://-style URL specifying a remote destination")
os.Exit(1)
}
for _, src := range sources {
if !isPelicanUrl(src) {
log.Errorln("When synchronizing to a local directory, all sources must be pelican URLs:", src)
os.Exit(1)
}
}
log.Debugln("Synchronizing from a Pelican data federation")
doDownload = true
}
log.Debugln("Sources:", sources)
log.Debugln("Destination:", dest)
// Get any configured preferred caches, to be passed along to the client
// as options.
caches, err := getPreferredCaches()
if err != nil {
log.Errorln("Failed to get preferred caches:", err)
os.Exit(1)
}
if doDownload && len(sources) > 1 {
if destStat, err := os.Stat(dest); err != nil {
log.Errorln("Destination does not exist")
os.Exit(1)
} else if !destStat.IsDir() {
log.Errorln("Destination is not a directory")
os.Exit(1)
}
}
lastSrc := ""
if doDownload {
for _, src := range sources {
if _, err = client.DoGet(ctx, src, dest, true,
client.WithCallback(pb.callback), client.WithTokenLocation(tokenLocation),
client.WithCaches(caches...), client.WithSynchronize(client.SyncSize)); err != nil {
lastSrc = src
break
}
}
} else {
for _, src := range sources {
if srcStat, err := os.Stat(src); err != nil {
log.Errorln("Source: " + src + " does not exist")
os.Exit(1)
} else if !srcStat.IsDir() && string(dest[len(dest)-1]) == `/` {
log.Warningln("Destination: " + dest + " ends with '/', but the source is a file. If the destination does not exist, it will be treated as an object, not a collection.")
}
if _, err = client.DoPut(ctx, src, dest, true,
client.WithCallback(pb.callback), client.WithTokenLocation(tokenLocation),
client.WithCaches(caches...), client.WithSynchronize(client.SyncSize)); err != nil {
lastSrc = src
break
}
}
}
// Exit with failure
if err != nil {
if handleCredentialPasswordError(err) {
os.Exit(1)
}
// Print the list of errors
errMsg := err.Error()
var pe error_codes.PelicanError
var te *client.TransferErrors
if errors.As(err, &te) {
errMsg = te.UserError()
}
if errors.Is(err, &pe) {
errMsg = pe.Error()
log.Errorln("Failure getting " + lastSrc + ": " + errMsg)
os.Exit(pe.ExitCode())
} else { // For now, keeping this else here to catch any errors that are not classified PelicanErrors
log.Errorln("Failure getting " + lastSrc + ": " + errMsg)
if client.ShouldRetry(err) {
log.Errorln("Errors are retryable")
os.Exit(11)
}
os.Exit(1)
}
}
}