Search in sources :

Example 1 with FederationModel

use of com.gitblit.models.FederationModel in project gitblit by gitblit.

the class FederationUtils method getFederationRegistrations.

/**
	 * Returns the list of federated gitblit instances that this instance will
	 * try to pull.
	 *
	 * @return list of registered gitblit instances
	 */
public static List<FederationModel> getFederationRegistrations(IStoredSettings settings) {
    List<FederationModel> federationRegistrations = new ArrayList<FederationModel>();
    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 origin Gitblit instance
            federatedModels.get(server).url = settings.getString(key, "");
        } else if (setting.equals("token")) {
            // token for the origin 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("bare")) {
            // whether pulled repositories should be bare
            federatedModels.get(server).bare = settings.getBoolean(key, true);
        } else if (setting.equals("mirror")) {
            // are the repositories to be true mirrors of the origin
            federatedModels.get(server).mirror = 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);
    }
    return federationRegistrations;
}
Also used : FederationModel(com.gitblit.models.FederationModel) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 2 with FederationModel

use of com.gitblit.models.FederationModel in project gitblit by gitblit.

the class FederationPullService method run.

/**
	 * Run method for this pull service.
	 */
@Override
public void run() {
    for (FederationModel registration : registrations) {
        FederationPullStatus was = registration.getLowestStatus();
        try {
            Date now = new Date(System.currentTimeMillis());
            pull(registration);
            sendStatusAcknowledgment(registration);
            registration.lastPull = now;
            FederationPullStatus is = registration.getLowestStatus();
            if (is.ordinal() < was.ordinal()) {
                // the status for this registration has downgraded
                logger.warn("Federation pull status of {0} is now {1}", registration.name, is.name());
                if (registration.notifyOnError) {
                    String message = "Federation pull of " + registration.name + " @ " + registration.url + " is now at " + is.name();
                    gitblit.sendMailToAdministrators("Pull Status of " + registration.name + " is " + is.name(), message);
                }
            }
        } catch (Throwable t) {
            logger.error(MessageFormat.format("Failed to pull from federated gitblit ({0} @ {1})", registration.name, registration.url), t);
        } finally {
            reschedule(registration);
        }
    }
}
Also used : FederationModel(com.gitblit.models.FederationModel) FederationPullStatus(com.gitblit.Constants.FederationPullStatus) Date(java.util.Date)

Example 3 with FederationModel

use of com.gitblit.models.FederationModel in project gitblit by gitblit.

the class ServicesManager method configureFederation.

protected void configureFederation() {
    boolean validPassphrase = true;
    String passphrase = settings.getString(Keys.federation.passphrase, "");
    if (StringUtils.isEmpty(passphrase)) {
        logger.info("Federation passphrase is blank! This server can not be PULLED from.");
        validPassphrase = false;
    }
    if (validPassphrase) {
        // standard tokens
        for (FederationToken tokenType : FederationToken.values()) {
            logger.info(MessageFormat.format("Federation {0} token = {1}", tokenType.name(), gitblit.getFederationToken(tokenType)));
        }
        // federation set tokens
        for (String set : settings.getStrings(Keys.federation.sets)) {
            logger.info(MessageFormat.format("Federation Set {0} token = {1}", set, gitblit.getFederationToken(set)));
        }
    }
    // Schedule or run the federation executor
    List<FederationModel> registrations = gitblit.getFederationRegistrations();
    if (registrations.size() > 0) {
        FederationPuller executor = new FederationPuller(registrations);
        scheduledExecutor.schedule(executor, 1, TimeUnit.MINUTES);
    }
}
Also used : FederationModel(com.gitblit.models.FederationModel) FederationToken(com.gitblit.Constants.FederationToken)

Example 4 with FederationModel

use of com.gitblit.models.FederationModel in project gitblit by gitblit.

the class FederationTests method getRegistration.

private FederationModel getRegistration() {
    FederationModel model = new FederationModel("localhost");
    model.url = this.url;
    model.token = this.token;
    return model;
}
Also used : FederationModel(com.gitblit.models.FederationModel)

Example 5 with FederationModel

use of com.gitblit.models.FederationModel in project gitblit by gitblit.

the class FederationClient method main.

public static void main(String[] args) {
    Params params = new Params();
    CmdLineParser parser = new CmdLineParser(params);
    try {
        parser.parseArgument(args);
    } catch (CmdLineException t) {
        usage(parser, t);
    }
    System.out.println("Gitblit Federation Client v" + Constants.getVersion() + " (" + Constants.getBuildDate() + ")");
    // command-line specified base folder
    File baseFolder = new File(System.getProperty("user.dir"));
    if (!StringUtils.isEmpty(params.baseFolder)) {
        baseFolder = new File(params.baseFolder);
    }
    File regFile = com.gitblit.utils.FileUtils.resolveParameter(Constants.baseFolder$, baseFolder, params.registrationsFile);
    FileSettings settings = new FileSettings(regFile.getAbsolutePath());
    List<FederationModel> registrations = new ArrayList<FederationModel>();
    if (StringUtils.isEmpty(params.url)) {
        registrations.addAll(FederationUtils.getFederationRegistrations(settings));
    } else {
        if (StringUtils.isEmpty(params.token)) {
            System.out.println("Must specify --token parameter!");
            System.exit(0);
        }
        FederationModel model = new FederationModel("Gitblit");
        model.url = params.url;
        model.token = params.token;
        model.mirror = params.mirror;
        model.bare = params.bare;
        model.folder = "";
        registrations.add(model);
    }
    if (registrations.size() == 0) {
        System.out.println("No Federation Registrations!  Nothing to do.");
        System.exit(0);
    }
    // command-line specified repositories folder
    if (!StringUtils.isEmpty(params.repositoriesFolder)) {
        settings.overrideSetting(Keys.git.repositoriesFolder, new File(params.repositoriesFolder).getAbsolutePath());
    }
    // configure the Gitblit singleton for minimal, non-server operation
    XssFilter xssFilter = new AllowXssFilter();
    RuntimeManager runtime = new RuntimeManager(settings, xssFilter, baseFolder).start();
    NoopNotificationManager notifications = new NoopNotificationManager().start();
    UserManager users = new UserManager(runtime, null).start();
    RepositoryManager repositories = new RepositoryManager(runtime, null, users).start();
    FederationManager federation = new FederationManager(runtime, notifications, repositories).start();
    IGitblit gitblit = new GitblitManager(null, null, runtime, null, notifications, users, null, repositories, null, federation, null);
    FederationPullService puller = new FederationPullService(gitblit, federation.getFederationRegistrations()) {

        @Override
        public void reschedule(FederationModel registration) {
        // NOOP
        }
    };
    puller.run();
    System.out.println("Finished.");
    System.exit(0);
}
Also used : FederationModel(com.gitblit.models.FederationModel) CmdLineParser(org.kohsuke.args4j.CmdLineParser) GitblitManager(com.gitblit.manager.GitblitManager) FederationManager(com.gitblit.manager.FederationManager) ArrayList(java.util.ArrayList) RuntimeManager(com.gitblit.manager.RuntimeManager) IGitblit(com.gitblit.manager.IGitblit) FederationPullService(com.gitblit.service.FederationPullService) UserManager(com.gitblit.manager.UserManager) AllowXssFilter(com.gitblit.utils.XssFilter.AllowXssFilter) XssFilter(com.gitblit.utils.XssFilter) AllowXssFilter(com.gitblit.utils.XssFilter.AllowXssFilter) RepositoryManager(com.gitblit.manager.RepositoryManager) File(java.io.File) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

FederationModel (com.gitblit.models.FederationModel)6 ArrayList (java.util.ArrayList)3 File (java.io.File)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 FederationPullStatus (com.gitblit.Constants.FederationPullStatus)1 FederationRequest (com.gitblit.Constants.FederationRequest)1 FederationToken (com.gitblit.Constants.FederationToken)1 FederationManager (com.gitblit.manager.FederationManager)1 GitblitManager (com.gitblit.manager.GitblitManager)1 IGitblit (com.gitblit.manager.IGitblit)1 RepositoryManager (com.gitblit.manager.RepositoryManager)1 RuntimeManager (com.gitblit.manager.RuntimeManager)1 UserManager (com.gitblit.manager.UserManager)1 FederationProposal (com.gitblit.models.FederationProposal)1 TeamModel (com.gitblit.models.TeamModel)1 UserModel (com.gitblit.models.UserModel)1 FederationPullService (com.gitblit.service.FederationPullService)1 XssFilter (com.gitblit.utils.XssFilter)1 AllowXssFilter (com.gitblit.utils.XssFilter.AllowXssFilter)1