Paul Martin
2016-04-16 eecaad8b8e2c447429c31a01d49260ddd6b4ee03
commit | author | age
241f57 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.Collection;
21 import java.util.List;
22
23 import org.kohsuke.args4j.CmdLineException;
24 import org.kohsuke.args4j.CmdLineParser;
25 import org.kohsuke.args4j.Option;
26
27 import com.gitblit.manager.FederationManager;
28 import com.gitblit.manager.GitblitManager;
29 import com.gitblit.manager.IGitblit;
30 import com.gitblit.manager.INotificationManager;
31 import com.gitblit.manager.RepositoryManager;
32 import com.gitblit.manager.RuntimeManager;
33 import com.gitblit.manager.UserManager;
34 import com.gitblit.models.FederationModel;
35 import com.gitblit.models.Mailing;
36 import com.gitblit.service.FederationPullService;
37 import com.gitblit.utils.FederationUtils;
38 import com.gitblit.utils.StringUtils;
f7174e 39 import com.gitblit.utils.XssFilter;
JM 40 import com.gitblit.utils.XssFilter.AllowXssFilter;
241f57 41
JM 42 /**
43  * Command-line client to pull federated Gitblit repositories.
44  *
45  * @author James Moger
46  *
47  */
48 public class FederationClient {
49
50     public static void main(String[] args) {
51         Params params = new Params();
52         CmdLineParser parser = new CmdLineParser(params);
53         try {
54             parser.parseArgument(args);
55         } catch (CmdLineException t) {
56             usage(parser, t);
57         }
58
59         System.out.println("Gitblit Federation Client v" + Constants.getVersion() + " (" + Constants.getBuildDate() + ")");
60
61         // command-line specified base folder
62         File baseFolder = new File(System.getProperty("user.dir"));
63         if (!StringUtils.isEmpty(params.baseFolder)) {
64             baseFolder = new File(params.baseFolder);
65         }
66
67         File regFile = com.gitblit.utils.FileUtils.resolveParameter(Constants.baseFolder$, baseFolder, params.registrationsFile);
68         FileSettings settings = new FileSettings(regFile.getAbsolutePath());
69         List<FederationModel> registrations = new ArrayList<FederationModel>();
70         if (StringUtils.isEmpty(params.url)) {
71             registrations.addAll(FederationUtils.getFederationRegistrations(settings));
72         } else {
73             if (StringUtils.isEmpty(params.token)) {
74                 System.out.println("Must specify --token parameter!");
75                 System.exit(0);
76             }
77             FederationModel model = new FederationModel("Gitblit");
78             model.url = params.url;
79             model.token = params.token;
80             model.mirror = params.mirror;
81             model.bare = params.bare;
82             model.folder = "";
83             registrations.add(model);
84         }
85         if (registrations.size() == 0) {
86             System.out.println("No Federation Registrations!  Nothing to do.");
87             System.exit(0);
88         }
89
90         // command-line specified repositories folder
91         if (!StringUtils.isEmpty(params.repositoriesFolder)) {
92             settings.overrideSetting(Keys.git.repositoriesFolder, new File(
93                     params.repositoriesFolder).getAbsolutePath());
94         }
95
96         // configure the Gitblit singleton for minimal, non-server operation
f7174e 97         XssFilter xssFilter = new AllowXssFilter();
JM 98         RuntimeManager runtime = new RuntimeManager(settings, xssFilter, baseFolder).start();
241f57 99         NoopNotificationManager notifications = new NoopNotificationManager().start();
JM 100         UserManager users = new UserManager(runtime, null).start();
eecaad 101         RepositoryManager repositories = new RepositoryManager(runtime, null, users, null).start();
241f57 102         FederationManager federation = new FederationManager(runtime, notifications, repositories).start();
bd0e83 103         IGitblit gitblit = new GitblitManager(null, null, runtime, null, notifications, users, null, repositories, null, federation, null);
241f57 104
JM 105         FederationPullService puller = new FederationPullService(gitblit, federation.getFederationRegistrations()) {
106             @Override
107             public void reschedule(FederationModel registration) {
108                 // NOOP
109             }
110         };
111         puller.run();
112
113         System.out.println("Finished.");
114         System.exit(0);
115     }
116
117     private static void usage(CmdLineParser parser, CmdLineException t) {
118         System.out.println(Constants.getGitBlitVersion());
119         System.out.println();
120         if (t != null) {
121             System.out.println(t.getMessage());
122             System.out.println();
123         }
124
125         if (parser != null) {
126             parser.printUsage(System.out);
127         }
128         System.exit(0);
129     }
130
131     /**
132      * Parameters class for FederationClient.
133      */
134     private static class Params {
135
136         @Option(name = "--registrations", usage = "Gitblit Federation Registrations File", metaVar = "FILE")
137         public String registrationsFile = "${baseFolder}/federation.properties";
138
139         @Option(name = "--url", usage = "URL of Gitblit instance to mirror from", metaVar = "URL")
140         public String url;
141
142         @Option(name = "--mirror", usage = "Mirror repositories")
143         public boolean mirror;
144
145         @Option(name = "--bare", usage = "Create bare repositories")
146         public boolean bare;
147
148         @Option(name = "--token", usage = "Federation Token", metaVar = "TOKEN")
149         public String token;
150
151         @Option(name = "--baseFolder", usage = "Base folder for received data", metaVar = "PATH")
152         public String baseFolder;
153
154         @Option(name = "--repositoriesFolder", usage = "Destination folder for cloned repositories", metaVar = "PATH")
155         public String repositoriesFolder;
156
157     }
158
159     private static class NoopNotificationManager implements INotificationManager {
160
161         @Override
162         public NoopNotificationManager start() {
163             return this;
164         }
165
166         @Override
167         public NoopNotificationManager stop() {
168             return this;
169         }
170
171         @Override
172         public boolean isSendingMail() {
173             return false;
174         }
175
176         @Override
177         public void sendMailToAdministrators(String subject, String message) {
178         }
179
180         @Override
181         public void sendMail(String subject, String message, Collection<String> toAddresses) {
182         }
183
184         @Override
185         public void sendHtmlMail(String subject, String message, Collection<String> toAddresses) {
186         }
187
188         @Override
189         public void send(Mailing mailing) {
190         }
191     }
192 }