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