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.

FederationClient.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit;
  17. import java.io.File;
  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.List;
  21. import org.kohsuke.args4j.CmdLineException;
  22. import org.kohsuke.args4j.CmdLineParser;
  23. import org.kohsuke.args4j.Option;
  24. import com.gitblit.manager.FederationManager;
  25. import com.gitblit.manager.GitblitManager;
  26. import com.gitblit.manager.IGitblit;
  27. import com.gitblit.manager.INotificationManager;
  28. import com.gitblit.manager.RepositoryManager;
  29. import com.gitblit.manager.RuntimeManager;
  30. import com.gitblit.manager.UserManager;
  31. import com.gitblit.models.FederationModel;
  32. import com.gitblit.models.Mailing;
  33. import com.gitblit.service.FederationPullService;
  34. import com.gitblit.utils.FederationUtils;
  35. import com.gitblit.utils.StringUtils;
  36. import com.gitblit.utils.XssFilter;
  37. import com.gitblit.utils.XssFilter.AllowXssFilter;
  38. /**
  39. * Command-line client to pull federated Gitblit repositories.
  40. *
  41. * @author James Moger
  42. *
  43. */
  44. public class FederationClient {
  45. public static void main(String[] args) {
  46. Params params = new Params();
  47. CmdLineParser parser = new CmdLineParser(params);
  48. try {
  49. parser.parseArgument(args);
  50. } catch (CmdLineException t) {
  51. usage(parser, t);
  52. }
  53. System.out.println("Gitblit Federation Client v" + Constants.getVersion() + " (" + Constants.getBuildDate() + ")");
  54. // command-line specified base folder
  55. File baseFolder = new File(System.getProperty("user.dir"));
  56. if (!StringUtils.isEmpty(params.baseFolder)) {
  57. baseFolder = new File(params.baseFolder);
  58. }
  59. File regFile = com.gitblit.utils.FileUtils.resolveParameter(Constants.baseFolder$, baseFolder, params.registrationsFile);
  60. FileSettings settings = new FileSettings(regFile.getAbsolutePath());
  61. List<FederationModel> registrations = new ArrayList<FederationModel>();
  62. if (StringUtils.isEmpty(params.url)) {
  63. registrations.addAll(FederationUtils.getFederationRegistrations(settings));
  64. } else {
  65. if (StringUtils.isEmpty(params.token)) {
  66. System.out.println("Must specify --token parameter!");
  67. System.exit(0);
  68. }
  69. FederationModel model = new FederationModel("Gitblit");
  70. model.url = params.url;
  71. model.token = params.token;
  72. model.mirror = params.mirror;
  73. model.bare = params.bare;
  74. model.folder = "";
  75. registrations.add(model);
  76. }
  77. if (registrations.size() == 0) {
  78. System.out.println("No Federation Registrations! Nothing to do.");
  79. System.exit(0);
  80. }
  81. // command-line specified repositories folder
  82. if (!StringUtils.isEmpty(params.repositoriesFolder)) {
  83. settings.overrideSetting(Keys.git.repositoriesFolder, new File(
  84. params.repositoriesFolder).getAbsolutePath());
  85. }
  86. // configure the Gitblit singleton for minimal, non-server operation
  87. XssFilter xssFilter = new AllowXssFilter();
  88. RuntimeManager runtime = new RuntimeManager(settings, xssFilter, baseFolder).start();
  89. NoopNotificationManager notifications = new NoopNotificationManager().start();
  90. UserManager users = new UserManager(runtime, null).start();
  91. RepositoryManager repositories = new RepositoryManager(runtime, null, users).start();
  92. FederationManager federation = new FederationManager(runtime, notifications, repositories).start();
  93. IGitblit gitblit = new GitblitManager(null, null, runtime, null, notifications, users, null, repositories, null, federation, null);
  94. FederationPullService puller = new FederationPullService(gitblit, federation.getFederationRegistrations()) {
  95. @Override
  96. public void reschedule(FederationModel registration) {
  97. // NOOP
  98. }
  99. };
  100. puller.run();
  101. System.out.println("Finished.");
  102. System.exit(0);
  103. }
  104. private static void usage(CmdLineParser parser, CmdLineException t) {
  105. System.out.println(Constants.getGitBlitVersion());
  106. System.out.println();
  107. if (t != null) {
  108. System.out.println(t.getMessage());
  109. System.out.println();
  110. }
  111. if (parser != null) {
  112. parser.printUsage(System.out);
  113. }
  114. System.exit(0);
  115. }
  116. /**
  117. * Parameters class for FederationClient.
  118. */
  119. private static class Params {
  120. @Option(name = "--registrations", usage = "Gitblit Federation Registrations File", metaVar = "FILE")
  121. public String registrationsFile = "${baseFolder}/federation.properties";
  122. @Option(name = "--url", usage = "URL of Gitblit instance to mirror from", metaVar = "URL")
  123. public String url;
  124. @Option(name = "--mirror", usage = "Mirror repositories")
  125. public boolean mirror;
  126. @Option(name = "--bare", usage = "Create bare repositories")
  127. public boolean bare;
  128. @Option(name = "--token", usage = "Federation Token", metaVar = "TOKEN")
  129. public String token;
  130. @Option(name = "--baseFolder", usage = "Base folder for received data", metaVar = "PATH")
  131. public String baseFolder;
  132. @Option(name = "--repositoriesFolder", usage = "Destination folder for cloned repositories", metaVar = "PATH")
  133. public String repositoriesFolder;
  134. }
  135. private static class NoopNotificationManager implements INotificationManager {
  136. @Override
  137. public NoopNotificationManager start() {
  138. return this;
  139. }
  140. @Override
  141. public NoopNotificationManager stop() {
  142. return this;
  143. }
  144. @Override
  145. public boolean isSendingMail() {
  146. return false;
  147. }
  148. @Override
  149. public void sendMailToAdministrators(String subject, String message) {
  150. }
  151. @Override
  152. public void sendMail(String subject, String message, Collection<String> toAddresses) {
  153. }
  154. @Override
  155. public void sendHtmlMail(String subject, String message, Collection<String> toAddresses) {
  156. }
  157. @Override
  158. public void send(Mailing mailing) {
  159. }
  160. }
  161. }