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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/*
* Copyright (C) 2015, Sasa Zivkov <sasa.zivkov@sap.com> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.pgm.debug;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.MessageFormat;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lfs.server.LargeFileRepository;
import org.eclipse.jgit.lfs.server.LfsProtocolServlet;
import org.eclipse.jgit.lfs.server.fs.FileLfsRepository;
import org.eclipse.jgit.lfs.server.fs.FileLfsServlet;
import org.eclipse.jgit.lfs.server.s3.S3Config;
import org.eclipse.jgit.lfs.server.s3.S3Repository;
import org.eclipse.jgit.pgm.Command;
import org.eclipse.jgit.pgm.TextBuiltin;
import org.eclipse.jgit.pgm.internal.CLIText;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
@Command(common = true, usage = "usage_runLfsStore")
class LfsStore extends TextBuiltin {
/**
* Tiny web application server for testing
*/
static class AppServer {
private final Server server;
private final ServerConnector connector;
private final ContextHandlerCollection contexts;
private URI uri;
AppServer(int port) {
server = new Server();
HttpConfiguration http_config = new HttpConfiguration();
http_config.setOutputBufferSize(32768);
connector = new ServerConnector(server,
new HttpConnectionFactory(http_config));
connector.setPort(port);
try {
String host = InetAddress.getByName("localhost") //$NON-NLS-1$
.getHostAddress();
connector.setHost(host);
if (host.contains(":") && !host.startsWith("[")) //$NON-NLS-1$ //$NON-NLS-2$
host = "[" + host + "]"; //$NON-NLS-1$//$NON-NLS-2$
uri = new URI("http://" + host + ":" + port); //$NON-NLS-1$ //$NON-NLS-2$
} catch (UnknownHostException e) {
throw new RuntimeException("Cannot find localhost", e); //$NON-NLS-1$
} catch (URISyntaxException e) {
throw new RuntimeException("Unexpected URI error on " + uri, e); //$NON-NLS-1$
}
contexts = new ContextHandlerCollection();
server.setHandler(contexts);
server.setConnectors(new Connector[] { connector });
}
/**
* Create a new servlet context within the server.
* <p>
* This method should be invoked before the server is started, once for
* each context the caller wants to register.
*
* @param path
* path of the context; use "/" for the root context if
* binding to the root is desired.
* @return the context to add servlets into.
*/
ServletContextHandler addContext(String path) {
assertNotRunning();
if ("".equals(path)) //$NON-NLS-1$
path = "/"; //$NON-NLS-1$
ServletContextHandler ctx = new ServletContextHandler();
ctx.setContextPath(path);
contexts.addHandler(ctx);
return ctx;
}
void start() throws Exception {
server.start();
}
void stop() throws Exception {
server.stop();
}
URI getURI() {
return uri;
}
private void assertNotRunning() {
if (server.isRunning()) {
throw new IllegalStateException("server is running"); //$NON-NLS-1$
}
}
}
private enum StoreType {
FS, S3;
}
private enum StorageClass {
REDUCED_REDUNDANCY, STANDARD
}
private static final String OBJECTS = "objects/"; //$NON-NLS-1$
private static final String STORE_PATH = "/" + OBJECTS + "*"; //$NON-NLS-1$//$NON-NLS-2$
private static final String PROTOCOL_PATH = "/lfs/objects/batch"; //$NON-NLS-1$
@Option(name = "--port", aliases = {"-p" },
metaVar = "metaVar_port", usage = "usage_LFSPort")
int port;
@Option(name = "--store", metaVar = "metaVar_lfsStorage", usage = "usage_LFSRunStore")
StoreType storeType;
@Option(name = "--store-url", aliases = {"-u" }, metaVar = "metaVar_url",
usage = "usage_LFSStoreUrl")
String storeUrl;
@Option(name = "--region", aliases = {"-r" },
metaVar = "metaVar_s3Region", usage = "usage_S3Region")
String region; // $NON-NLS-1$
@Option(name = "--bucket", aliases = {"-b" },
metaVar = "metaVar_s3Bucket", usage = "usage_S3Bucket")
String bucket; // $NON-NLS-1$
@Option(name = "--storage-class", aliases = {"-c" },
metaVar = "metaVar_s3StorageClass", usage = "usage_S3StorageClass")
StorageClass storageClass = StorageClass.REDUCED_REDUNDANCY;
@Option(name = "--expire", aliases = {"-e" },
metaVar = "metaVar_seconds", usage = "usage_S3Expiration")
int expirationSeconds = 600;
@Option(name = "--no-ssl-verify", usage = "usage_S3NoSslVerify")
boolean disableSslVerify = false;
@Argument(required = false, metaVar = "metaVar_directory", usage = "usage_LFSDirectory")
String directory;
String protocolUrl;
String accessKey;
String secretKey;
@Override
protected boolean requiresRepository() {
return false;
}
@Override
protected void run() throws Exception {
AppServer server = new AppServer(port);
URI baseURI = server.getURI();
ServletContextHandler app = server.addContext("/"); //$NON-NLS-1$
final LargeFileRepository repository;
switch (storeType) {
case FS:
Path dir = Paths.get(directory);
FileLfsRepository fsRepo = new FileLfsRepository(
getStoreUrl(baseURI), dir);
FileLfsServlet content = new FileLfsServlet(fsRepo, 30000);
app.addServlet(new ServletHolder(content), STORE_PATH);
repository = fsRepo;
break;
case S3:
readAWSKeys();
checkOptions();
S3Config config = new S3Config(region, bucket,
storageClass.toString(), accessKey, secretKey,
expirationSeconds, disableSslVerify);
repository = new S3Repository(config);
break;
default:
throw new IllegalArgumentException(MessageFormat
.format(CLIText.get().lfsUnknownStoreType, storeType));
}
LfsProtocolServlet protocol = new LfsProtocolServlet() {
private static final long serialVersionUID = 1L;
@Override
protected LargeFileRepository getLargeFileRepository(
LfsRequest request, String path, String auth) {
return repository;
}
};
app.addServlet(new ServletHolder(protocol), PROTOCOL_PATH);
server.start();
outw.println(MessageFormat.format(CLIText.get().lfsProtocolUrl,
getProtocolUrl(baseURI)));
if (storeType == StoreType.FS) {
outw.println(MessageFormat.format(CLIText.get().lfsStoreDirectory,
directory));
outw.println(MessageFormat.format(CLIText.get().lfsStoreUrl,
getStoreUrl(baseURI)));
}
}
private void checkOptions() {
if (bucket == null || bucket.length() == 0) {
throw die(MessageFormat.format(CLIText.get().s3InvalidBucket,
bucket));
}
}
private void readAWSKeys() throws IOException, ConfigInvalidException {
String credentialsPath = System.getProperty("user.home") //$NON-NLS-1$
+ "/.aws/credentials"; //$NON-NLS-1$
FileBasedConfig c = new FileBasedConfig(new File(credentialsPath),
FS.DETECTED);
c.load();
accessKey = c.getString("default", null, "accessKey"); //$NON-NLS-1$//$NON-NLS-2$
secretKey = c.getString("default", null, "secretKey"); //$NON-NLS-1$ //$NON-NLS-2$
if (accessKey == null || accessKey.isEmpty()) {
throw die(MessageFormat.format(CLIText.get().lfsNoAccessKey,
credentialsPath));
}
if (secretKey == null || secretKey.isEmpty()) {
throw die(MessageFormat.format(CLIText.get().lfsNoSecretKey,
credentialsPath));
}
}
private String getStoreUrl(URI baseURI) {
if (storeUrl == null) {
if (storeType == StoreType.FS) {
storeUrl = baseURI + "/" + OBJECTS; //$NON-NLS-1$
} else {
die("Local store not running and no --store-url specified"); //$NON-NLS-1$
}
}
return storeUrl;
}
private String getProtocolUrl(URI baseURI) {
if (protocolUrl == null) {
protocolUrl = baseURI + PROTOCOL_PATH;
}
return protocolUrl;
}
}
|