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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. /**
  37. * Command-line client to pull federated Gitblit repositories.
  38. *
  39. * @author James Moger
  40. *
  41. */
  42. public class FederationClient {
  43. public static void main(String[] args) {
  44. Params params = new Params();
  45. CmdLineParser parser = new CmdLineParser(params);
  46. try {
  47. parser.parseArgument(args);
  48. } catch (CmdLineException t) {
  49. usage(parser, t);
  50. }
  51. System.out.println("Gitblit Federation Client v" + Constants.getVersion() + " (" + Constants.getBuildDate() + ")");
  52. // command-line specified base folder
  53. File baseFolder = new File(System.getProperty("user.dir"));
  54. if (!StringUtils.isEmpty(params.baseFolder)) {
  55. baseFolder = new File(params.baseFolder);
  56. }
  57. File regFile = com.gitblit.utils.FileUtils.resolveParameter(Constants.baseFolder$, baseFolder, params.registrationsFile);
  58. FileSettings settings = new FileSettings(regFile.getAbsolutePath());
  59. List<FederationModel> registrations = new ArrayList<FederationModel>();
  60. if (StringUtils.isEmpty(params.url)) {
  61. registrations.addAll(FederationUtils.getFederationRegistrations(settings));
  62. } else {
  63. if (StringUtils.isEmpty(params.token)) {
  64. System.out.println("Must specify --token parameter!");
  65. System.exit(0);
  66. }
  67. FederationModel model = new FederationModel("Gitblit");
  68. model.url = params.url;
  69. model.token = params.token;
  70. model.mirror = params.mirror;
  71. model.bare = params.bare;
  72. model.folder = "";
  73. registrations.add(model);
  74. }
  75. if (registrations.size() == 0) {
  76. System.out.println("No Federation Registrations! Nothing to do.");
  77. System.exit(0);
  78. }
  79. // command-line specified repositories folder
  80. if (!StringUtils.isEmpty(params.repositoriesFolder)) {
  81. settings.overrideSetting(Keys.git.repositoriesFolder, new File(
  82. params.repositoriesFolder).getAbsolutePath());
  83. }
  84. // configure the Gitblit singleton for minimal, non-server operation
  85. RuntimeManager runtime = new RuntimeManager(settings, baseFolder).start();
  86. NoopNotificationManager notifications = new NoopNotificationManager().start();
  87. UserManager users = new UserManager(runtime).start();
  88. RepositoryManager repositories = new RepositoryManager(runtime, users).start();
  89. FederationManager federation = new FederationManager(runtime, notifications, repositories).start();
  90. IGitblit gitblit = new GitblitManager(runtime, notifications, users, null, repositories, null, federation);
  91. FederationPullService puller = new FederationPullService(gitblit, federation.getFederationRegistrations()) {
  92. @Override
  93. public void reschedule(FederationModel registration) {
  94. // NOOP
  95. }
  96. };
  97. puller.run();
  98. System.out.println("Finished.");
  99. System.exit(0);
  100. }
  101. private static void usage(CmdLineParser parser, CmdLineException t) {
  102. System.out.println(Constants.getGitBlitVersion());
  103. System.out.println();
  104. if (t != null) {
  105. System.out.println(t.getMessage());
  106. System.out.println();
  107. }
  108. if (parser != null) {
  109. parser.printUsage(System.out);
  110. }
  111. System.exit(0);
  112. }
  113. /**
  114. * Parameters class for FederationClient.
  115. */
  116. private static class Params {
  117. @Option(name = "--registrations", usage = "Gitblit Federation Registrations File", metaVar = "FILE")
  118. public String registrationsFile = "${baseFolder}/federation.properties";
  119. @Option(name = "--url", usage = "URL of Gitblit instance to mirror from", metaVar = "URL")
  120. public String url;
  121. @Option(name = "--mirror", usage = "Mirror repositories")
  122. public boolean mirror;
  123. @Option(name = "--bare", usage = "Create bare repositories")
  124. public boolean bare;
  125. @Option(name = "--token", usage = "Federation Token", metaVar = "TOKEN")
  126. public String token;
  127. @Option(name = "--baseFolder", usage = "Base folder for received data", metaVar = "PATH")
  128. public String baseFolder;
  129. @Option(name = "--repositoriesFolder", usage = "Destination folder for cloned repositories", metaVar = "PATH")
  130. public String repositoriesFolder;
  131. }
  132. private static class NoopNotificationManager implements INotificationManager {
  133. @Override
  134. public NoopNotificationManager start() {
  135. return this;
  136. }
  137. @Override
  138. public NoopNotificationManager stop() {
  139. return this;
  140. }
  141. @Override
  142. public void sendMailToAdministrators(String subject, String message) {
  143. }
  144. @Override
  145. public void sendMail(String subject, String message, Collection<String> toAddresses) {
  146. }
  147. @Override
  148. public void sendHtmlMail(String subject, String message, Collection<String> toAddresses) {
  149. }
  150. @Override
  151. public void send(Mailing mailing) {
  152. }
  153. }
  154. }