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 package org.eclipse.jgit.pgm.debug;
45
46 import java.io.File;
47 import java.io.IOException;
48 import java.net.InetAddress;
49 import java.net.URI;
50 import java.net.URISyntaxException;
51 import java.net.UnknownHostException;
52 import java.nio.file.Path;
53 import java.nio.file.Paths;
54 import java.text.MessageFormat;
55
56 import org.eclipse.jetty.server.Connector;
57 import org.eclipse.jetty.server.HttpConfiguration;
58 import org.eclipse.jetty.server.HttpConnectionFactory;
59 import org.eclipse.jetty.server.Server;
60 import org.eclipse.jetty.server.ServerConnector;
61 import org.eclipse.jetty.server.handler.ContextHandlerCollection;
62 import org.eclipse.jetty.servlet.ServletContextHandler;
63 import org.eclipse.jetty.servlet.ServletHolder;
64 import org.eclipse.jgit.errors.ConfigInvalidException;
65 import org.eclipse.jgit.lfs.server.LargeFileRepository;
66 import org.eclipse.jgit.lfs.server.LfsProtocolServlet;
67 import org.eclipse.jgit.lfs.server.fs.FileLfsRepository;
68 import org.eclipse.jgit.lfs.server.fs.FileLfsServlet;
69 import org.eclipse.jgit.lfs.server.s3.S3Config;
70 import org.eclipse.jgit.lfs.server.s3.S3Repository;
71 import org.eclipse.jgit.pgm.Command;
72 import org.eclipse.jgit.pgm.TextBuiltin;
73 import org.eclipse.jgit.pgm.internal.CLIText;
74 import org.eclipse.jgit.storage.file.FileBasedConfig;
75 import org.eclipse.jgit.util.FS;
76 import org.kohsuke.args4j.Argument;
77 import org.kohsuke.args4j.Option;
78
79 @Command(common = true, usage = "usage_runLfsStore")
80 class LfsStore extends TextBuiltin {
81
82
83
84
85 static class AppServer {
86
87 private final Server server;
88
89 private final ServerConnector connector;
90
91 private final ContextHandlerCollection contexts;
92
93 private URI uri;
94
95 AppServer(int port) {
96 server = new Server();
97
98 HttpConfiguration http_config = new HttpConfiguration();
99 http_config.setOutputBufferSize(32768);
100
101 connector = new ServerConnector(server,
102 new HttpConnectionFactory(http_config));
103 connector.setPort(port);
104 try {
105 String host = InetAddress.getByName("localhost")
106 .getHostAddress();
107 connector.setHost(host);
108 if (host.contains(":") && !host.startsWith("["))
109 host = "[" + host + "]";
110 uri = new URI("http://" + host + ":" + port); //$NON-NLS-1$ //$NON-NLS-2$
111 } catch (UnknownHostException e) {
112 throw new RuntimeException("Cannot find localhost", e);
113 } catch (URISyntaxException e) {
114 throw new RuntimeException("Unexpected URI error on " + uri, e);
115 }
116
117 contexts = new ContextHandlerCollection();
118 server.setHandler(contexts);
119 server.setConnectors(new Connector[] { connector });
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133 ServletContextHandler addContext(String path) {
134 assertNotRunning();
135 if ("".equals(path))
136 path = "/";
137
138 ServletContextHandler ctx = new ServletContextHandler();
139 ctx.setContextPath(path);
140 contexts.addHandler(ctx);
141
142 return ctx;
143 }
144
145 void start() throws Exception {
146 server.start();
147 }
148
149 void stop() throws Exception {
150 server.stop();
151 }
152
153 URI getURI() {
154 return uri;
155 }
156
157 private void assertNotRunning() {
158 if (server.isRunning()) {
159 throw new IllegalStateException("server is running");
160 }
161 }
162 }
163
164 private static enum StoreType {
165 FS, S3;
166 }
167
168 private static enum StorageClass {
169 REDUCED_REDUNDANCY, STANDARD
170 }
171
172 private static final String OBJECTS = "objects/";
173
174 private static final String STORE_PATH = "/" + OBJECTS + "*";
175
176 private static final String PROTOCOL_PATH = "/lfs/objects/batch";
177
178 @Option(name = "--port", aliases = {"-p" },
179 metaVar = "metaVar_port", usage = "usage_LFSPort")
180 int port;
181
182 @Option(name = "--store", metaVar = "metaVar_lfsStorage", usage = "usage_LFSRunStore")
183 StoreType storeType;
184
185 @Option(name = "--store-url", aliases = {"-u" }, metaVar = "metaVar_url",
186 usage = "usage_LFSStoreUrl")
187 String storeUrl;
188
189 @Option(name = "--region", aliases = {"-r" },
190 metaVar = "metaVar_s3Region", usage = "usage_S3Region")
191 String region;
192
193 @Option(name = "--bucket", aliases = {"-b" },
194 metaVar = "metaVar_s3Bucket", usage = "usage_S3Bucket")
195 String bucket;
196
197 @Option(name = "--storage-class", aliases = {"-c" },
198 metaVar = "metaVar_s3StorageClass", usage = "usage_S3StorageClass")
199 StorageClass storageClass = StorageClass.REDUCED_REDUNDANCY;
200
201 @Option(name = "--expire", aliases = {"-e" },
202 metaVar = "metaVar_seconds", usage = "usage_S3Expiration")
203 int expirationSeconds = 600;
204
205 @Option(name = "--no-ssl-verify", usage = "usage_S3NoSslVerify")
206 boolean disableSslVerify = false;
207
208 @Argument(required = false, metaVar = "metaVar_directory", usage = "usage_LFSDirectory")
209 String directory;
210
211 String protocolUrl;
212
213 String accessKey;
214
215 String secretKey;
216
217
218 @Override
219 protected boolean requiresRepository() {
220 return false;
221 }
222
223
224 @Override
225 protected void run() throws Exception {
226 AppServer server = new AppServer(port);
227 URI baseURI = server.getURI();
228 ServletContextHandler app = server.addContext("/");
229
230 final LargeFileRepository repository;
231 switch (storeType) {
232 case FS:
233 Path dir = Paths.get(directory);
234 FileLfsRepository fsRepo = new FileLfsRepository(
235 getStoreUrl(baseURI), dir);
236 FileLfsServlet content = new FileLfsServlet(fsRepo, 30000);
237 app.addServlet(new ServletHolder(content), STORE_PATH);
238 repository = fsRepo;
239 break;
240
241 case S3:
242 readAWSKeys();
243 checkOptions();
244 S3Config config = new S3Config(region, bucket,
245 storageClass.toString(), accessKey, secretKey,
246 expirationSeconds, disableSslVerify);
247 repository = new S3Repository(config);
248 break;
249 default:
250 throw new IllegalArgumentException(MessageFormat
251 .format(CLIText.get().lfsUnknownStoreType, storeType));
252 }
253
254 LfsProtocolServlet protocol = new LfsProtocolServlet() {
255
256 private static final long serialVersionUID = 1L;
257
258 @Override
259 protected LargeFileRepository getLargeFileRepository(
260 LfsRequest request, String path, String auth) {
261 return repository;
262 }
263 };
264 app.addServlet(new ServletHolder(protocol), PROTOCOL_PATH);
265
266 server.start();
267
268 outw.println(MessageFormat.format(CLIText.get().lfsProtocolUrl,
269 getProtocolUrl(baseURI)));
270 if (storeType == StoreType.FS) {
271 outw.println(MessageFormat.format(CLIText.get().lfsStoreDirectory,
272 directory));
273 outw.println(MessageFormat.format(CLIText.get().lfsStoreUrl,
274 getStoreUrl(baseURI)));
275 }
276 }
277
278 private void checkOptions() {
279 if (bucket == null || bucket.length() == 0) {
280 throw die(MessageFormat.format(CLIText.get().s3InvalidBucket,
281 bucket));
282 }
283 }
284
285 private void readAWSKeys() throws IOException, ConfigInvalidException {
286 String credentialsPath = System.getProperty("user.home")
287 + "/.aws/credentials";
288 FileBasedConfig c = new FileBasedConfig(new File(credentialsPath),
289 FS.DETECTED);
290 c.load();
291 accessKey = c.getString("default", null, "accessKey");
292 secretKey = c.getString("default", null, "secretKey");
293 if (accessKey == null || accessKey.isEmpty()) {
294 throw die(MessageFormat.format(CLIText.get().lfsNoAccessKey,
295 credentialsPath));
296 }
297 if (secretKey == null || secretKey.isEmpty()) {
298 throw die(MessageFormat.format(CLIText.get().lfsNoSecretKey,
299 credentialsPath));
300 }
301 }
302
303 private String getStoreUrl(URI baseURI) {
304 if (storeUrl == null) {
305 if (storeType == StoreType.FS) {
306 storeUrl = baseURI + "/" + OBJECTS;
307 } else {
308 die("Local store not running and no --store-url specified");
309 }
310 }
311 return storeUrl;
312 }
313
314 private String getProtocolUrl(URI baseURI) {
315 if (protocolUrl == null) {
316 protocolUrl = baseURI + PROTOCOL_PATH;
317 }
318 return protocolUrl;
319 }
320 }