View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
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  
44  package org.eclipse.jgit.pgm;
45  
46  import java.io.File;
47  import java.net.InetSocketAddress;
48  import java.net.URISyntaxException;
49  import java.text.MessageFormat;
50  import java.util.ArrayList;
51  import java.util.List;
52  import java.util.concurrent.Executors;
53  
54  import org.eclipse.jgit.internal.ketch.KetchLeader;
55  import org.eclipse.jgit.internal.ketch.KetchLeaderCache;
56  import org.eclipse.jgit.internal.ketch.KetchPreReceive;
57  import org.eclipse.jgit.internal.ketch.KetchSystem;
58  import org.eclipse.jgit.internal.ketch.KetchText;
59  import org.eclipse.jgit.lib.Repository;
60  import org.eclipse.jgit.pgm.internal.CLIText;
61  import org.eclipse.jgit.storage.file.FileBasedConfig;
62  import org.eclipse.jgit.storage.file.WindowCacheConfig;
63  import org.eclipse.jgit.storage.pack.PackConfig;
64  import org.eclipse.jgit.transport.DaemonClient;
65  import org.eclipse.jgit.transport.DaemonService;
66  import org.eclipse.jgit.transport.ReceivePack;
67  import org.eclipse.jgit.transport.resolver.FileResolver;
68  import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
69  import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
70  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
71  import org.eclipse.jgit.util.FS;
72  import org.kohsuke.args4j.Argument;
73  import org.kohsuke.args4j.Option;
74  
75  @Command(common = true, usage = "usage_exportRepositoriesOverGit")
76  class Daemon extends TextBuiltin {
77  	@Option(name = "--config-file", metaVar = "metaVar_configFile", usage = "usage_configFile")
78  	File configFile;
79  
80  	@Option(name = "--port", metaVar = "metaVar_port", usage = "usage_portNumberToListenOn")
81  	int port = org.eclipse.jgit.transport.Daemon.DEFAULT_PORT;
82  
83  	@Option(name = "--listen", metaVar = "metaVar_hostName", usage = "usage_hostnameOrIpToListenOn")
84  	String host;
85  
86  	@Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
87  	int timeout = -1;
88  
89  	@Option(name = "--enable", metaVar = "metaVar_service", usage = "usage_enableTheServiceInAllRepositories")
90  	List<String> enable = new ArrayList<>();
91  
92  	@Option(name = "--disable", metaVar = "metaVar_service", usage = "usage_disableTheServiceInAllRepositories")
93  	List<String> disable = new ArrayList<>();
94  
95  	@Option(name = "--allow-override", metaVar = "metaVar_service", usage = "usage_configureTheServiceInDaemonServicename")
96  	List<String> canOverride = new ArrayList<>();
97  
98  	@Option(name = "--forbid-override", metaVar = "metaVar_service", usage = "usage_configureTheServiceInDaemonServicename")
99  	List<String> forbidOverride = new ArrayList<>();
100 
101 	@Option(name = "--export-all", usage = "usage_exportWithoutGitDaemonExportOk")
102 	boolean exportAll;
103 
104 	@Option(name = "--ketch", metaVar = "metaVar_ketchServerType", usage = "usage_ketchServerType")
105 	KetchServerType ketchServerType;
106 
107 	enum KetchServerType {
108 		LEADER;
109 	}
110 
111 	@Argument(required = true, metaVar = "metaVar_directory", usage = "usage_directoriesToExport")
112 	List<File> directory = new ArrayList<>();
113 
114 	/** {@inheritDoc} */
115 	@Override
116 	protected boolean requiresRepository() {
117 		return false;
118 	}
119 
120 	/** {@inheritDoc} */
121 	@Override
122 	protected void run() throws Exception {
123 		PackConfig packConfig = new PackConfig();
124 
125 		if (configFile != null) {
126 			if (!configFile.exists()) {
127 				throw die(MessageFormat.format(
128 						CLIText.get().configFileNotFound, //
129 						configFile.getAbsolutePath()));
130 			}
131 
132 			FileBasedConfig cfg = new FileBasedConfig(configFile, FS.DETECTED);
133 			cfg.load();
134 			new WindowCacheConfig().fromConfig(cfg).install();
135 			packConfig.fromConfig(cfg);
136 		}
137 
138 		int threads = packConfig.getThreads();
139 		if (threads <= 0)
140 			threads = Runtime.getRuntime().availableProcessors();
141 		if (1 < threads)
142 			packConfig.setExecutor(Executors.newFixedThreadPool(threads));
143 
144 		final FileResolver<DaemonClient> resolver = new FileResolver<>();
145 		for (File f : directory) {
146 			outw.println(MessageFormat.format(CLIText.get().exporting, f.getAbsolutePath()));
147 			resolver.exportDirectory(f);
148 		}
149 		resolver.setExportAll(exportAll);
150 
151 		final org.eclipse.jgit.transport.Daemon d;
152 		d = new org.eclipse.jgit.transport.Daemon(
153 				host != null ? new InetSocketAddress(host, port)
154 						: new InetSocketAddress(port));
155 		d.setPackConfig(packConfig);
156 		d.setRepositoryResolver(resolver);
157 		if (0 <= timeout)
158 			d.setTimeout(timeout);
159 
160 		for (String n : enable)
161 			service(d, n).setEnabled(true);
162 		for (String n : disable)
163 			service(d, n).setEnabled(false);
164 
165 		for (String n : canOverride)
166 			service(d, n).setOverridable(true);
167 		for (String n : forbidOverride)
168 			service(d, n).setOverridable(false);
169 		if (ketchServerType == KetchServerType.LEADER) {
170 			startKetchLeader(d);
171 		}
172 		d.start();
173 		outw.println(MessageFormat.format(CLIText.get().listeningOn, d.getAddress()));
174 	}
175 
176 	private static DaemonService service(
177 			final org.eclipse.jgit.transport.Daemon d,
178 			final String n) {
179 		final DaemonService svc = d.getService(n);
180 		if (svc == null)
181 			throw die(MessageFormat.format(CLIText.get().serviceNotSupported, n));
182 		return svc;
183 	}
184 
185 	private void startKetchLeader(org.eclipse.jgit.transport.Daemon daemon) {
186 		KetchSystem system = new KetchSystem();
187 		final KetchLeaderCache leaders = new KetchLeaderCache(system);
188 		final ReceivePackFactory<DaemonClient> factory;
189 
190 		factory = daemon.getReceivePackFactory();
191 		daemon.setReceivePackFactory(new ReceivePackFactory<DaemonClient>() {
192 			@Override
193 			public ReceivePack create(DaemonClient req, Repository repo)
194 					throws ServiceNotEnabledException,
195 					ServiceNotAuthorizedException {
196 				ReceivePack rp = factory.create(req, repo);
197 				KetchLeader leader;
198 				try {
199 					leader = leaders.get(repo);
200 				} catch (URISyntaxException err) {
201 					throw new ServiceNotEnabledException(
202 							KetchText.get().invalidFollowerUri, err);
203 				}
204 				rp.setPreReceiveHook(new KetchPreReceive(leader));
205 				return rp;
206 			}
207 		});
208 	}
209 }