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 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 @Override
218 protected boolean requiresRepository() {
219 return false;
220 }
221
222 protected void run() throws Exception {
223 AppServer server = new AppServer(port);
224 URI baseURI = server.getURI();
225 ServletContextHandler app = server.addContext("/");
226
227 final LargeFileRepository repository;
228 switch (storeType) {
229 case FS:
230 Path dir = Paths.get(directory);
231 FileLfsRepository fsRepo = new FileLfsRepository(
232 getStoreUrl(baseURI), dir);
233 FileLfsServlet content = new FileLfsServlet(fsRepo, 30000);
234 app.addServlet(new ServletHolder(content), STORE_PATH);
235 repository = fsRepo;
236 break;
237
238 case S3:
239 readAWSKeys();
240 checkOptions();
241 S3Config config = new S3Config(region, bucket,
242 storageClass.toString(), accessKey, secretKey,
243 expirationSeconds, disableSslVerify);
244 repository = new S3Repository(config);
245 break;
246 default:
247 throw new IllegalArgumentException(MessageFormat
248 .format(CLIText.get().lfsUnknownStoreType, storeType));
249 }
250
251 LfsProtocolServlet protocol = new LfsProtocolServlet() {
252
253 private static final long serialVersionUID = 1L;
254
255 @Override
256 protected LargeFileRepository getLargeFileRepository(
257 LfsRequest request, String path) {
258 return repository;
259 }
260 };
261 app.addServlet(new ServletHolder(protocol), PROTOCOL_PATH);
262
263 server.start();
264
265 outw.println(MessageFormat.format(CLIText.get().lfsProtocolUrl,
266 getProtocolUrl(baseURI)));
267 if (storeType == StoreType.FS) {
268 outw.println(MessageFormat.format(CLIText.get().lfsStoreDirectory,
269 directory));
270 outw.println(MessageFormat.format(CLIText.get().lfsStoreUrl,
271 getStoreUrl(baseURI)));
272 }
273 }
274
275 private void checkOptions() {
276 if (bucket == null || bucket.length() == 0) {
277 throw die(MessageFormat.format(CLIText.get().s3InvalidBucket,
278 bucket));
279 }
280 }
281
282 private void readAWSKeys() throws IOException, ConfigInvalidException {
283 String credentialsPath = System.getProperty("user.home")
284 + "/.aws/credentials";
285 FileBasedConfig c = new FileBasedConfig(new File(credentialsPath),
286 FS.DETECTED);
287 c.load();
288 accessKey = c.getString("default", null, "accessKey");
289 secretKey = c.getString("default", null, "secretKey");
290 if (accessKey == null || accessKey.isEmpty()) {
291 throw die(MessageFormat.format(CLIText.get().lfsNoAccessKey,
292 credentialsPath));
293 }
294 if (secretKey == null || secretKey.isEmpty()) {
295 throw die(MessageFormat.format(CLIText.get().lfsNoSecretKey,
296 credentialsPath));
297 }
298 }
299
300 private String getStoreUrl(URI baseURI) {
301 if (storeUrl == null) {
302 if (storeType == StoreType.FS) {
303 storeUrl = baseURI + "/" + OBJECTS;
304 } else {
305 die("Local store not running and no --store-url specified");
306 }
307 }
308 return storeUrl;
309 }
310
311 private String getProtocolUrl(URI baseURI) {
312 if (protocolUrl == null) {
313 protocolUrl = baseURI + PROTOCOL_PATH;
314 }
315 return protocolUrl;
316 }
317 }