You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LfsStore.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (C) 2015, Sasa Zivkov <sasa.zivkov@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.pgm.debug;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.net.InetAddress;
  47. import java.net.URI;
  48. import java.net.URISyntaxException;
  49. import java.net.UnknownHostException;
  50. import java.nio.file.Path;
  51. import java.nio.file.Paths;
  52. import java.text.MessageFormat;
  53. import org.eclipse.jetty.server.Connector;
  54. import org.eclipse.jetty.server.HttpConfiguration;
  55. import org.eclipse.jetty.server.HttpConnectionFactory;
  56. import org.eclipse.jetty.server.Server;
  57. import org.eclipse.jetty.server.ServerConnector;
  58. import org.eclipse.jetty.server.handler.ContextHandlerCollection;
  59. import org.eclipse.jetty.servlet.ServletContextHandler;
  60. import org.eclipse.jetty.servlet.ServletHolder;
  61. import org.eclipse.jgit.errors.ConfigInvalidException;
  62. import org.eclipse.jgit.lfs.server.LargeFileRepository;
  63. import org.eclipse.jgit.lfs.server.LfsProtocolServlet;
  64. import org.eclipse.jgit.lfs.server.fs.FileLfsRepository;
  65. import org.eclipse.jgit.lfs.server.fs.FileLfsServlet;
  66. import org.eclipse.jgit.lfs.server.s3.S3Config;
  67. import org.eclipse.jgit.lfs.server.s3.S3Repository;
  68. import org.eclipse.jgit.pgm.Command;
  69. import org.eclipse.jgit.pgm.TextBuiltin;
  70. import org.eclipse.jgit.pgm.internal.CLIText;
  71. import org.eclipse.jgit.storage.file.FileBasedConfig;
  72. import org.eclipse.jgit.util.FS;
  73. import org.kohsuke.args4j.Argument;
  74. import org.kohsuke.args4j.Option;
  75. @Command(common = true, usage = "usage_runLfsStore")
  76. class LfsStore extends TextBuiltin {
  77. /**
  78. * Tiny web application server for testing
  79. */
  80. class AppServer {
  81. private final Server server;
  82. private final ServerConnector connector;
  83. private final ContextHandlerCollection contexts;
  84. private URI uri;
  85. AppServer(int port) {
  86. server = new Server();
  87. HttpConfiguration http_config = new HttpConfiguration();
  88. http_config.setOutputBufferSize(32768);
  89. connector = new ServerConnector(server,
  90. new HttpConnectionFactory(http_config));
  91. connector.setPort(port);
  92. try {
  93. String host = InetAddress.getByName("localhost") //$NON-NLS-1$
  94. .getHostAddress();
  95. connector.setHost(host);
  96. if (host.contains(":") && !host.startsWith("[")) //$NON-NLS-1$ //$NON-NLS-2$
  97. host = "[" + host + "]"; //$NON-NLS-1$//$NON-NLS-2$
  98. uri = new URI("http://" + host + ":" + port); //$NON-NLS-1$ //$NON-NLS-2$
  99. } catch (UnknownHostException e) {
  100. throw new RuntimeException("Cannot find localhost", e); //$NON-NLS-1$
  101. } catch (URISyntaxException e) {
  102. throw new RuntimeException("Unexpected URI error on " + uri, e); //$NON-NLS-1$
  103. }
  104. contexts = new ContextHandlerCollection();
  105. server.setHandler(contexts);
  106. server.setConnectors(new Connector[] { connector });
  107. }
  108. /**
  109. * Create a new servlet context within the server.
  110. * <p>
  111. * This method should be invoked before the server is started, once for
  112. * each context the caller wants to register.
  113. *
  114. * @param path
  115. * path of the context; use "/" for the root context if
  116. * binding to the root is desired.
  117. * @return the context to add servlets into.
  118. */
  119. ServletContextHandler addContext(String path) {
  120. assertNotRunning();
  121. if ("".equals(path)) //$NON-NLS-1$
  122. path = "/"; //$NON-NLS-1$
  123. ServletContextHandler ctx = new ServletContextHandler();
  124. ctx.setContextPath(path);
  125. contexts.addHandler(ctx);
  126. return ctx;
  127. }
  128. void start() throws Exception {
  129. server.start();
  130. }
  131. void stop() throws Exception {
  132. server.stop();
  133. }
  134. URI getURI() {
  135. return uri;
  136. }
  137. private void assertNotRunning() {
  138. if (server.isRunning()) {
  139. throw new IllegalStateException("server is running"); //$NON-NLS-1$
  140. }
  141. }
  142. }
  143. private static enum StoreType {
  144. FS, S3;
  145. }
  146. private static enum StorageClass {
  147. REDUCED_REDUNDANCY, STANDARD
  148. }
  149. private static final String OBJECTS = "objects/"; //$NON-NLS-1$
  150. private static final String STORE_PATH = "/" + OBJECTS + "*"; //$NON-NLS-1$//$NON-NLS-2$
  151. private static final String PROTOCOL_PATH = "/lfs/objects/batch"; //$NON-NLS-1$
  152. @Option(name = "--port", aliases = {"-p" },
  153. metaVar = "metaVar_port", usage = "usage_LFSPort")
  154. int port;
  155. @Option(name = "--store", metaVar = "metaVar_lfsStorage", usage = "usage_LFSRunStore")
  156. StoreType storeType;
  157. @Option(name = "--store-url", aliases = {"-u" }, metaVar = "metaVar_url",
  158. usage = "usage_LFSStoreUrl")
  159. String storeUrl;
  160. @Option(name = "--region", aliases = {"-r" },
  161. metaVar = "metaVar_s3Region", usage = "usage_S3Region")
  162. String region; // $NON-NLS-1$
  163. @Option(name = "--bucket", aliases = {"-b" },
  164. metaVar = "metaVar_s3Bucket", usage = "usage_S3Bucket")
  165. String bucket; // $NON-NLS-1$
  166. @Option(name = "--storage-class", aliases = {"-c" },
  167. metaVar = "metaVar_s3StorageClass", usage = "usage_S3StorageClass")
  168. StorageClass storageClass = StorageClass.REDUCED_REDUNDANCY;
  169. @Option(name = "--expire", aliases = {"-e" },
  170. metaVar = "metaVar_seconds", usage = "usage_S3Expiration")
  171. int expirationSeconds = 600;
  172. @Option(name = "--no-ssl-verify", usage = "usage_S3NoSslVerify")
  173. boolean disableSslVerify = false;
  174. @Argument(required = false, metaVar = "metaVar_directory", usage = "usage_LFSDirectory")
  175. String directory;
  176. String protocolUrl;
  177. String accessKey;
  178. String secretKey;
  179. @Override
  180. protected boolean requiresRepository() {
  181. return false;
  182. }
  183. protected void run() throws Exception {
  184. AppServer server = new AppServer(port);
  185. URI baseURI = server.getURI();
  186. ServletContextHandler app = server.addContext("/"); //$NON-NLS-1$
  187. final LargeFileRepository repository;
  188. switch (storeType) {
  189. case FS:
  190. Path dir = Paths.get(directory);
  191. FileLfsRepository fsRepo = new FileLfsRepository(
  192. getStoreUrl(baseURI), dir);
  193. FileLfsServlet content = new FileLfsServlet(fsRepo, 30000);
  194. app.addServlet(new ServletHolder(content), STORE_PATH);
  195. repository = fsRepo;
  196. break;
  197. case S3:
  198. readAWSKeys();
  199. checkOptions();
  200. S3Config config = new S3Config(region, bucket,
  201. storageClass.toString(), accessKey, secretKey,
  202. expirationSeconds, disableSslVerify);
  203. repository = new S3Repository(config);
  204. break;
  205. default:
  206. throw new IllegalArgumentException(MessageFormat
  207. .format(CLIText.get().lfsUnknownStoreType, storeType));
  208. }
  209. LfsProtocolServlet protocol = new LfsProtocolServlet() {
  210. private static final long serialVersionUID = 1L;
  211. @Override
  212. protected LargeFileRepository getLargeFileRepository(
  213. LfsRequest request, String path) {
  214. return repository;
  215. }
  216. };
  217. app.addServlet(new ServletHolder(protocol), PROTOCOL_PATH);
  218. server.start();
  219. outw.println(MessageFormat.format(CLIText.get().lfsProtocolUrl,
  220. getProtocolUrl(baseURI)));
  221. if (storeType == StoreType.FS) {
  222. outw.println(MessageFormat.format(CLIText.get().lfsStoreDirectory,
  223. directory));
  224. outw.println(MessageFormat.format(CLIText.get().lfsStoreUrl,
  225. getStoreUrl(baseURI)));
  226. }
  227. }
  228. private void checkOptions() {
  229. if (bucket == null || bucket.length() == 0) {
  230. throw die(MessageFormat.format(CLIText.get().s3InvalidBucket,
  231. bucket));
  232. }
  233. }
  234. private void readAWSKeys() throws IOException, ConfigInvalidException {
  235. String credentialsPath = System.getProperty("user.home") //$NON-NLS-1$
  236. + "/.aws/credentials"; //$NON-NLS-1$
  237. FileBasedConfig c = new FileBasedConfig(new File(credentialsPath),
  238. FS.DETECTED);
  239. c.load();
  240. accessKey = c.getString("default", null, "accessKey"); //$NON-NLS-1$//$NON-NLS-2$
  241. secretKey = c.getString("default", null, "secretKey"); //$NON-NLS-1$ //$NON-NLS-2$
  242. if (accessKey == null || accessKey.isEmpty()) {
  243. throw die(MessageFormat.format(CLIText.get().lfsNoAccessKey,
  244. credentialsPath));
  245. }
  246. if (secretKey == null || secretKey.isEmpty()) {
  247. throw die(MessageFormat.format(CLIText.get().lfsNoSecretKey,
  248. credentialsPath));
  249. }
  250. }
  251. private String getStoreUrl(URI baseURI) {
  252. if (storeUrl == null) {
  253. if (storeType == StoreType.FS) {
  254. storeUrl = baseURI + "/" + OBJECTS; //$NON-NLS-1$
  255. } else {
  256. die("Local store not running and no --store-url specified"); //$NON-NLS-1$
  257. }
  258. }
  259. return storeUrl;
  260. }
  261. private String getProtocolUrl(URI baseURI) {
  262. if (protocolUrl == null) {
  263. protocolUrl = baseURI + PROTOCOL_PATH;
  264. }
  265. return protocolUrl;
  266. }
  267. }