Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #2319 from oleg-nenashev/JENKINS-34674
[JENKINS-34674,JENKINS-34675] - Handling of the default update site ID
  • Loading branch information
oleg-nenashev committed May 10, 2016
2 parents b9bb52f + 25f6c2c commit 0b3dbfc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
36 changes: 28 additions & 8 deletions core/src/main/java/hudson/model/UpdateCenter.java
Expand Up @@ -144,9 +144,10 @@ public class UpdateCenter extends AbstractModelObject implements Saveable, OnMas

/**
* {@linkplain UpdateSite#getId() ID} of the default update site.
* @since 1.483
* @since 1.483 - public property
* @since TODO - configurable via system property
*/
public static final String ID_DEFAULT = "default";
public static final String ID_DEFAULT = System.getProperty(UpdateCenter.class.getName()+".defaultUpdateSiteId", "default");

@Restricted(NoExternalUse.class)
public static final String ID_UPLOAD = "_upload";
Expand Down Expand Up @@ -290,7 +291,7 @@ public InstallationJob getJob(Plugin plugin) {
/**
* Get the current connection status.
* <p>
* Supports a "siteId" request parameter, defaulting to "default" for the default
* Supports a "siteId" request parameter, defaulting to {@link #ID_DEFAULT} for the default
* update site.
*
* @return The current connection status.
Expand All @@ -302,6 +303,9 @@ public HttpResponse doConnectionStatus(StaplerRequest request) {
String siteId = request.getParameter("siteId");
if (siteId == null) {
siteId = ID_DEFAULT;
} else if (siteId.equals("default")) {
// If the request explicitly requires the default ID, ship it
siteId = ID_DEFAULT;
}
ConnectionCheckJob checkJob = getConnectionCheckJob(siteId);
if (checkJob == null) {
Expand Down Expand Up @@ -334,7 +338,8 @@ public HttpResponse doConnectionStatus(StaplerRequest request) {
}
return HttpResponses.okJSON(checkJob.connectionStates);
} else {
return HttpResponses.errorJSON(String.format("Unknown site '%s'.", siteId));
return HttpResponses.errorJSON(String.format("Cannot check connection status of the update site with ID='%s'"
+ ". This update center cannot be resolved", siteId));
}
} catch (Exception e) {
return HttpResponses.errorJSON(String.format("ERROR: %s", e.getMessage()));
Expand Down Expand Up @@ -463,7 +468,10 @@ public List<UpdateSite> getSiteList() {

/**
* Alias for {@link #getById}.
* @param id ID of the update site to be retrieved
* @return Discovered {@link UpdateSite}. {@code null} if it cannot be found
*/
@CheckForNull
public UpdateSite getSite(String id) {
return getById(id);
}
Expand All @@ -488,7 +496,10 @@ public String getLastUpdatedString() {
/**
* Gets {@link UpdateSite} by its ID.
* Used to bind them to URL.
* @param id ID of the update site to be retrieved
* @return Discovered {@link UpdateSite}. {@code null} if it cannot be found
*/
@CheckForNull
public UpdateSite getById(String id) {
for (UpdateSite s : sites) {
if (s.getId().equals(id)) {
Expand All @@ -502,8 +513,9 @@ public UpdateSite getById(String id) {
* Gets the {@link UpdateSite} from which we receive updates for <tt>jenkins.war</tt>.
*
* @return
* null if no such update center is provided.
* {@code null} if no such update center is provided.
*/
@CheckForNull
public UpdateSite getCoreSource() {
for (UpdateSite s : sites) {
Data data = s.getData();
Expand All @@ -527,6 +539,7 @@ public String getDefaultBaseUrl() {

/**
* Gets the plugin with the given name from the first {@link UpdateSite} to contain it.
* @return Discovered {@link Plugin}. {@code null} if it cannot be found
*/
public @CheckForNull Plugin getPlugin(String artifactId) {
for (UpdateSite s : sites) {
Expand Down Expand Up @@ -2042,12 +2055,19 @@ public static void init(Jenkins h) throws IOException {

@Restricted(NoExternalUse.class)
public static void updateDefaultSite() {
final UpdateSite site = Jenkins.getInstance().getUpdateCenter().getSite(UpdateCenter.ID_DEFAULT);
if (site == null) {
LOGGER.log(Level.SEVERE, "Upgrading Jenkins. Cannot retrieve the default Update Site ''{0}''. "
+ "Plugin installation may fail.", UpdateCenter.ID_DEFAULT);
return;
}
try {
// Need to do the following because the plugin manager will attempt to access
// $JENKINS_HOME/updates/default.json. Needs to be up to date.
Jenkins.getInstance().getUpdateCenter().getSite(UpdateCenter.ID_DEFAULT).updateDirectlyNow(true);
// $JENKINS_HOME/updates/$ID_DEFAULT.json. Needs to be up to date.
site.updateDirectlyNow(true);
} catch (Exception e) {
LOGGER.log(WARNING, "Upgrading Jenkins. Failed to update default UpdateSite. Plugin upgrades may fail.", e);
LOGGER.log(WARNING, "Upgrading Jenkins. Failed to update the default Update Site '" + UpdateCenter.ID_DEFAULT +
"'. Plugin upgrades may fail.", e);
}
}

Expand Down
Expand Up @@ -67,7 +67,7 @@ public void doConnectionStatus_unknown_site() throws IOException, SAXException {
JSONObject response = jenkinsRule.getJSON("updateCenter/connectionStatus?siteId=blahblah").getJSONObject();

Assert.assertEquals("error", response.getString("status"));
Assert.assertEquals("Unknown site 'blahblah'.", response.getString("message"));
Assert.assertEquals("Cannot check connection status of the update site with ID='blahblah'. This update center cannot be resolved", response.getString("message"));
}

private UpdateSite updateSite = new UpdateSite(UpdateCenter.ID_DEFAULT, "http://xyz") {
Expand Down
10 changes: 8 additions & 2 deletions war/src/main/js/pluginSetupWizardGui.js
Expand Up @@ -889,9 +889,15 @@ var createPluginSetupWizard = function(appendTarget) {
translations = localizations;

// check for connectivity
jenkins.testConnectivity(handleGenericError(function(isConnected) {
//TODO: make the Update Center ID configurable
var siteId = 'default';
jenkins.testConnectivity(siteId, handleGenericError(function(isConnected, isFatal, errorMessage) {
if(!isConnected) {
setPanel(offlinePanel);
if (isFatal) { // We cannot continue, show error
setPanel(errorPanel, { errorMessage: 'Default update site connectivity check failed with fatal error: ' + errorMessage + '. If you see this issue for the custom Jenkins WAR bundle, consider setting the correct value of the hudson.model.UpdateCenter.defaultUpdateSiteId system property (requires Jenkins restart). Otherwise please create a bug in Jenkins JIRA.' });
} else { // The update center is offline, no problem
setPanel(offlinePanel);
}
return;
}

Expand Down
12 changes: 8 additions & 4 deletions war/src/main/js/util/jenkins.js
Expand Up @@ -192,18 +192,22 @@ exports.loadTranslations = function(bundleName, handler, onError) {
/**
* Runs a connectivity test, calls handler with a boolean whether there is sufficient connectivity to the internet
*/
exports.testConnectivity = function(handler) {
exports.testConnectivity = function(siteId, handler) {
// check the connectivity api
var testConnectivity = function() {
exports.get('/updateCenter/connectionStatus?siteId=default', function(response) {
exports.get('/updateCenter/connectionStatus?siteId=' + siteId, function(response) {
if(response.status !== 'ok') {
handler(false, true, response.message);
}

var uncheckedStatuses = ['PRECHECK', 'CHECKING', 'UNCHECKED'];
if(uncheckedStatuses.indexOf(response.data.updatesite) >= 0 || uncheckedStatuses.indexOf(response.data.internet) >= 0) {
setTimeout(testConnectivity, 100);
}
else {
if(response.status !== 'ok' || response.data.updatesite !== 'OK' || response.data.internet !== 'OK') {
// no connectivity
handler(false);
// no connectivity, but not fatal
handler(false, false);
}
else {
handler(true);
Expand Down

0 comments on commit 0b3dbfc

Please sign in to comment.