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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.List;
  20. import com.beust.jcommander.JCommander;
  21. import com.beust.jcommander.Parameter;
  22. import com.beust.jcommander.ParameterException;
  23. import com.beust.jcommander.Parameters;
  24. import com.gitblit.models.FederationModel;
  25. import com.gitblit.utils.FederationUtils;
  26. import com.gitblit.utils.StringUtils;
  27. /**
  28. * Command-line client to pull federated Gitblit repositories.
  29. *
  30. * @author James Moger
  31. *
  32. */
  33. public class FederationClient {
  34. public static void main(String[] args) {
  35. Params params = new Params();
  36. JCommander jc = new JCommander(params);
  37. try {
  38. jc.parse(args);
  39. } catch (ParameterException t) {
  40. usage(jc, t);
  41. }
  42. IStoredSettings settings = new FileSettings(params.registrationsFile);
  43. List<FederationModel> registrations = new ArrayList<FederationModel>();
  44. if (StringUtils.isEmpty(params.url)) {
  45. registrations.addAll(FederationUtils.getFederationRegistrations(settings));
  46. } else {
  47. if (StringUtils.isEmpty(params.token)) {
  48. System.out.println("Must specify --token parameter!");
  49. System.exit(0);
  50. }
  51. FederationModel model = new FederationModel("Gitblit");
  52. model.url = params.url;
  53. model.token = params.token;
  54. model.mirror = params.mirror;
  55. model.bare = params.bare;
  56. model.frequency = params.frequency;
  57. model.folder = "";
  58. registrations.add(model);
  59. }
  60. if (registrations.size() == 0) {
  61. System.out.println("No Federation Registrations! Nothing to do.");
  62. System.exit(0);
  63. }
  64. System.out.println("Gitblit Federation Client v" + Constants.VERSION + " (" + Constants.VERSION_DATE + ")");
  65. // command-line specified repositories folder
  66. if (!StringUtils.isEmpty(params.repositoriesFolder)) {
  67. settings.overrideSetting(Keys.git.repositoriesFolder, new File(
  68. params.repositoriesFolder).getAbsolutePath());
  69. }
  70. // configure the Gitblit singleton for minimal, non-server operation
  71. GitBlit.self().configureContext(settings, false);
  72. FederationPullExecutor executor = new FederationPullExecutor(registrations, params.isDaemon);
  73. executor.run();
  74. if (!params.isDaemon) {
  75. System.out.println("Finished.");
  76. System.exit(0);
  77. }
  78. }
  79. private static void usage(JCommander jc, ParameterException t) {
  80. System.out.println(Constants.getGitBlitVersion());
  81. System.out.println();
  82. if (t != null) {
  83. System.out.println(t.getMessage());
  84. System.out.println();
  85. }
  86. if (jc != null) {
  87. jc.usage();
  88. }
  89. System.exit(0);
  90. }
  91. /**
  92. * JCommander Parameters class for FederationClient.
  93. */
  94. @Parameters(separators = " ")
  95. private static class Params {
  96. @Parameter(names = { "--registrations" }, description = "Gitblit Federation Registrations File", required = false)
  97. public String registrationsFile = "federation.properties";
  98. @Parameter(names = { "--daemon" }, description = "Runs in daemon mode to schedule and pull repositories", required = false)
  99. public boolean isDaemon;
  100. @Parameter(names = { "--url" }, description = "URL of Gitblit instance to mirror from", required = false)
  101. public String url;
  102. @Parameter(names = { "--mirror" }, description = "Mirror repositories", required = false)
  103. public boolean mirror;
  104. @Parameter(names = { "--bare" }, description = "Create bare repositories", required = false)
  105. public boolean bare;
  106. @Parameter(names = { "--token" }, description = "Federation Token", required = false)
  107. public String token;
  108. @Parameter(names = { "--frequency" }, description = "Period to wait between pull attempts (requires --daemon)", required = false)
  109. public String frequency = "60 mins";
  110. @Parameter(names = { "--repositoriesFolder" }, description = "Destination folder for cloned repositories", required = false)
  111. public String repositoriesFolder;
  112. }
  113. }