| | |
| | | import com.gitblit.Constants.FederationToken;
|
| | | import com.gitblit.models.FederationModel;
|
| | | import com.gitblit.models.FederationProposal;
|
| | | import com.gitblit.models.FederationSet;
|
| | | import com.gitblit.models.RepositoryModel;
|
| | | import com.gitblit.models.UserModel;
|
| | | import com.gitblit.utils.ByteFormat;
|
| | | import com.gitblit.utils.FederationUtils;
|
| | | import com.gitblit.utils.JGitUtils;
|
| | | import com.gitblit.utils.JsonUtils;
|
| | | import com.gitblit.utils.StringUtils;
|
| | | import com.google.gson.Gson;
|
| | | import com.google.gson.GsonBuilder;
|
| | |
|
| | | /**
|
| | | * GitBlit is the servlet context listener singleton that acts as the core for
|
| | |
| | | public void setUserService(IUserService userService) {
|
| | | logger.info("Setting up user service " + userService.toString());
|
| | | this.userService = userService;
|
| | | this.userService.setup(settings);
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | model.origin = config.getString("remote", "origin", "url");
|
| | | }
|
| | | r.close();
|
| | | if (getBoolean(Keys.web.showRepositorySizes, true)) {
|
| | | ByteFormat byteFormat = new ByteFormat();
|
| | | model.size = byteFormat.format(calculateSize(model)); |
| | | }
|
| | | return model;
|
| | | }
|
| | |
|
| | |
| | | // Schedule the federation executor
|
| | | List<FederationModel> registrations = getFederationRegistrations();
|
| | | if (registrations.size() > 0) {
|
| | | scheduledExecutor.schedule(new FederationPullExecutor(registrations), 1,
|
| | | TimeUnit.MINUTES);
|
| | | FederationPullExecutor executor = new FederationPullExecutor(registrations, true);
|
| | | scheduledExecutor.schedule(executor, 1, TimeUnit.MINUTES);
|
| | | }
|
| | | }
|
| | |
|
| | |
| | | */
|
| | | public List<FederationModel> getFederationRegistrations() {
|
| | | if (federationRegistrations.isEmpty()) {
|
| | | List<String> keys = settings.getAllKeys(Keys.federation._ROOT);
|
| | | keys.remove(Keys.federation.name);
|
| | | keys.remove(Keys.federation.passphrase);
|
| | | keys.remove(Keys.federation.allowProposals);
|
| | | keys.remove(Keys.federation.proposalsFolder);
|
| | | keys.remove(Keys.federation.defaultFrequency);
|
| | | keys.remove(Keys.federation.sets);
|
| | | Collections.sort(keys);
|
| | | Map<String, FederationModel> federatedModels = new HashMap<String, FederationModel>();
|
| | | for (String key : keys) {
|
| | | String value = key.substring(Keys.federation._ROOT.length() + 1);
|
| | | List<String> values = StringUtils.getStringsFromValue(value, "\\.");
|
| | | String server = values.get(0);
|
| | | if (!federatedModels.containsKey(server)) {
|
| | | federatedModels.put(server, new FederationModel(server));
|
| | | }
|
| | | String setting = values.get(1);
|
| | | if (setting.equals("url")) {
|
| | | // url of the remote Gitblit instance
|
| | | federatedModels.get(server).url = settings.getString(key, "");
|
| | | } else if (setting.equals("token")) {
|
| | | // token for the remote Gitblit instance
|
| | | federatedModels.get(server).token = settings.getString(key, "");
|
| | | } else if (setting.equals("frequency")) {
|
| | | // frequency of the pull operation
|
| | | federatedModels.get(server).frequency = settings.getString(key, "");
|
| | | } else if (setting.equals("folder")) {
|
| | | // destination folder of the pull operation
|
| | | federatedModels.get(server).folder = settings.getString(key, "");
|
| | | } else if (setting.equals("freeze")) {
|
| | | // set the repository to read-only after pull
|
| | | federatedModels.get(server).freeze = settings.getBoolean(key, true);
|
| | | } else if (setting.equals("mergeAccounts")) {
|
| | | // merge remote accounts into local accounts
|
| | | federatedModels.get(server).mergeAccounts = settings.getBoolean(key, false);
|
| | | } else if (setting.equals("sendStatus")) {
|
| | | // send a status acknowledgment to source Gitblit instance
|
| | | // at end of git pull
|
| | | federatedModels.get(server).sendStatus = settings.getBoolean(key, false);
|
| | | } else if (setting.equals("notifyOnError")) {
|
| | | // notify administrators on federation pull failures
|
| | | federatedModels.get(server).notifyOnError = settings.getBoolean(key, false);
|
| | | } else if (setting.equals("exclude")) {
|
| | | // excluded repositories
|
| | | federatedModels.get(server).exclusions = settings.getStrings(key);
|
| | | } else if (setting.equals("include")) {
|
| | | // included repositories
|
| | | federatedModels.get(server).inclusions = settings.getStrings(key);
|
| | | }
|
| | | }
|
| | |
|
| | | // verify that registrations have a url and a token
|
| | | for (FederationModel model : federatedModels.values()) {
|
| | | if (StringUtils.isEmpty(model.url)) {
|
| | | logger.warn(MessageFormat.format(
|
| | | "Dropping federation registration {0}. Missing url.", model.name));
|
| | | continue;
|
| | | }
|
| | | if (StringUtils.isEmpty(model.token)) {
|
| | | logger.warn(MessageFormat.format(
|
| | | "Dropping federation registration {0}. Missing token.", model.name));
|
| | | continue;
|
| | | }
|
| | | // set default frequency if unspecified
|
| | | if (StringUtils.isEmpty(model.frequency)) {
|
| | | model.frequency = settings.getString(Keys.federation.defaultFrequency,
|
| | | "60 mins");
|
| | | }
|
| | | federationRegistrations.add(model);
|
| | | }
|
| | | federationRegistrations.addAll(FederationUtils.getFederationRegistrations(settings));
|
| | | }
|
| | | return federationRegistrations;
|
| | | }
|
| | |
| | | }
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | /**
|
| | | * Returns the list of federation sets.
|
| | | * |
| | | * @return list of federation sets
|
| | | */
|
| | | public List<FederationSet> getFederationSets(String gitblitUrl) {
|
| | | List<FederationSet> list = new ArrayList<FederationSet>();
|
| | | // generate standard tokens
|
| | | for (FederationToken type : FederationToken.values()) {
|
| | | FederationSet fset = new FederationSet(type.toString(), type, getFederationToken(type));
|
| | | fset.repositories = getRepositories(gitblitUrl, fset.token);
|
| | | list.add(fset);
|
| | | }
|
| | | // generate tokens for federation sets
|
| | | for (String set : settings.getStrings(Keys.federation.sets)) {
|
| | | FederationSet fset = new FederationSet(set, FederationToken.REPOSITORIES,
|
| | | getFederationToken(set));
|
| | | fset.repositories = getRepositories(gitblitUrl, fset.token);
|
| | | list.add(fset);
|
| | | }
|
| | | return list;
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | * @param proposal
|
| | | * the proposal
|
| | | * @param gitblitUrl
|
| | | * the url of your gitblit instance
|
| | | * the url of your gitblit instance to send an email to
|
| | | * administrators
|
| | | * @return true if the proposal was submitted
|
| | | */
|
| | | public boolean submitFederationProposal(FederationProposal proposal, String gitblitUrl) {
|
| | | // convert proposal to json
|
| | | Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
| | | String json = gson.toJson(proposal);
|
| | | String json = JsonUtils.toJsonString(proposal);
|
| | |
|
| | | try {
|
| | | // make the proposals folder
|
| | |
| | | && file.getName().toLowerCase().endsWith(Constants.PROPOSAL_EXT);
|
| | | }
|
| | | });
|
| | | Gson gson = new Gson();
|
| | | for (File file : files) {
|
| | | String json = com.gitblit.utils.FileUtils.readContent(file, null);
|
| | | FederationProposal proposal = gson.fromJson(json, FederationProposal.class);
|
| | | FederationProposal proposal = JsonUtils.fromJsonString(json,
|
| | | FederationProposal.class);
|
| | | list.add(proposal);
|
| | | }
|
| | | }
|
| | | return list;
|
| | | }
|
| | |
|
| | | /**
|
| | | * Get repositories for the specified token.
|
| | | * |
| | | * @param gitblitUrl
|
| | | * the base url of this gitblit instance
|
| | | * @param token
|
| | | * the federation token
|
| | | * @return a map of <cloneurl, RepositoryModel>
|
| | | */
|
| | | public Map<String, RepositoryModel> getRepositories(String gitblitUrl, String token) {
|
| | | Map<String, String> federationSets = new HashMap<String, String>();
|
| | | for (String set : getStrings(Keys.federation.sets)) {
|
| | | federationSets.put(getFederationToken(set), set);
|
| | | }
|
| | |
|
| | | // Determine the Gitblit clone url
|
| | | StringBuilder sb = new StringBuilder();
|
| | | sb.append(gitblitUrl);
|
| | | sb.append(Constants.GIT_PATH);
|
| | | sb.append("{0}");
|
| | | String cloneUrl = sb.toString();
|
| | |
|
| | | // Retrieve all available repositories
|
| | | UserModel user = new UserModel(Constants.FEDERATION_USER);
|
| | | user.canAdmin = true;
|
| | | List<RepositoryModel> list = getRepositoryModels(user);
|
| | |
|
| | | // create the [cloneurl, repositoryModel] map
|
| | | Map<String, RepositoryModel> repositories = new HashMap<String, RepositoryModel>();
|
| | | for (RepositoryModel model : list) {
|
| | | // by default, setup the url for THIS repository
|
| | | String url = MessageFormat.format(cloneUrl, model.name);
|
| | | switch (model.federationStrategy) {
|
| | | case EXCLUDE:
|
| | | // skip this repository
|
| | | continue;
|
| | | case FEDERATE_ORIGIN:
|
| | | // federate the origin, if it is defined
|
| | | if (!StringUtils.isEmpty(model.origin)) {
|
| | | url = model.origin;
|
| | | }
|
| | | break;
|
| | | }
|
| | |
|
| | | if (federationSets.containsKey(token)) {
|
| | | // include repositories only for federation set
|
| | | String set = federationSets.get(token);
|
| | | if (model.federationSets.contains(set)) {
|
| | | repositories.put(url, model);
|
| | | }
|
| | | } else {
|
| | | // standard federation token for ALL
|
| | | repositories.put(url, model);
|
| | | }
|
| | | }
|
| | | return repositories;
|
| | | }
|
| | |
|
| | | /**
|
| | | * Creates a proposal from the token.
|
| | | * |
| | | * @param gitblitUrl
|
| | | * the url of this Gitblit instance
|
| | | * @param token
|
| | | * @return a potential proposal
|
| | | */
|
| | | public FederationProposal createFederationProposal(String gitblitUrl, String token) {
|
| | | FederationToken tokenType = FederationToken.REPOSITORIES;
|
| | | for (FederationToken type : FederationToken.values()) {
|
| | | if (token.equals(getFederationToken(type))) {
|
| | | tokenType = type;
|
| | | break;
|
| | | }
|
| | | }
|
| | | Map<String, RepositoryModel> repositories = getRepositories(gitblitUrl, token);
|
| | | FederationProposal proposal = new FederationProposal(gitblitUrl, tokenType, token,
|
| | | repositories);
|
| | | return proposal;
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | *
|
| | | * @param settings
|
| | | */
|
| | | public void configureContext(IStoredSettings settings) {
|
| | | public void configureContext(IStoredSettings settings, boolean startFederation) {
|
| | | logger.info("Reading configuration from " + settings.toString());
|
| | | this.settings = settings;
|
| | | repositoriesFolder = new File(settings.getString(Keys.git.repositoriesFolder, "git"));
|
| | |
| | | loginService = new FileUserService(realmFile);
|
| | | }
|
| | | setUserService(loginService);
|
| | | configureFederation();
|
| | | mailExecutor = new MailExecutor(settings);
|
| | | if (mailExecutor.isReady()) {
|
| | | scheduledExecutor.scheduleAtFixedRate(mailExecutor, 1, 2, TimeUnit.MINUTES);
|
| | | } else {
|
| | | logger.warn("Mail server is not properly configured. Mail services disabled.");
|
| | | }
|
| | | if (startFederation) {
|
| | | configureFederation();
|
| | | }
|
| | | }
|
| | |
|
| | |
| | | if (settings == null) {
|
| | | // Gitblit WAR is running in a servlet container
|
| | | WebXmlSettings webxmlSettings = new WebXmlSettings(contextEvent.getServletContext());
|
| | | configureContext(webxmlSettings);
|
| | | configureContext(webxmlSettings, true);
|
| | | }
|
| | | }
|
| | |
|