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