Search in sources :

Example 1 with UpdateSite

use of hudson.model.UpdateSite in project hudson-2.x by hudson.

the class InstallPluginCommand method run.

protected int run() throws Exception {
    Hudson h = Hudson.getInstance();
    h.checkPermission(Hudson.ADMINISTER);
    for (String source : sources) {
        // is this a file?
        FilePath f = new FilePath(channel, source);
        if (f.exists()) {
            stdout.println(Messages.InstallPluginCommand_InstallingPluginFromLocalFile(f));
            if (name == null)
                name = f.getBaseName();
            f.copyTo(getTargetFile());
            continue;
        }
        // is this an URL?
        try {
            URL u = new URL(source);
            stdout.println(Messages.InstallPluginCommand_InstallingPluginFromUrl(u));
            if (name == null) {
                name = u.getPath();
                name = name.substring(name.indexOf('/') + 1);
                name = name.substring(name.indexOf('\\') + 1);
                int idx = name.lastIndexOf('.');
                if (idx > 0)
                    name = name.substring(0, idx);
            }
            getTargetFile().copyFrom(u);
            continue;
        } catch (MalformedURLException e) {
        // not an URL
        }
        // is this a plugin the update center?
        UpdateSite.Plugin p = h.getUpdateCenter().getPlugin(source);
        if (p != null) {
            stdout.println(Messages.InstallPluginCommand_InstallingFromUpdateCenter(source));
            p.deploy().get();
            continue;
        }
        stdout.println(Messages.InstallPluginCommand_NotAValidSourceName(source));
        if (!source.contains(".") && !source.contains(":") && !source.contains("/") && !source.contains("\\")) {
            // looks like a short plugin name. Why did we fail to find it in the update center?
            if (h.getUpdateCenter().getSites().isEmpty()) {
                stdout.println(Messages.InstallPluginCommand_NoUpdateCenterDefined());
            } else {
                Set<String> candidates = new HashSet<String>();
                for (UpdateSite s : h.getUpdateCenter().getSites()) {
                    Data dt = s.getData();
                    if (dt == null) {
                        stdout.println(Messages.InstallPluginCommand_NoUpdateDataRetrieved(s.getUrl()));
                    } else {
                        candidates.addAll(dt.plugins.keySet());
                    }
                }
                stdout.println(Messages.InstallPluginCommand_DidYouMean(source, EditDistance.findNearest(source, candidates)));
            }
        }
        return 1;
    }
    if (restart)
        h.restart();
    // all success
    return 0;
}
Also used : FilePath(hudson.FilePath) MalformedURLException(java.net.MalformedURLException) Hudson(hudson.model.Hudson) Data(hudson.model.UpdateSite.Data) UpdateSite(hudson.model.UpdateSite) URL(java.net.URL) HashSet(java.util.HashSet)

Example 2 with UpdateSite

use of hudson.model.UpdateSite in project hudson-2.x by hudson.

the class PluginManager method doSiteConfigure.

/**
 * Bare-minimum configuration mechanism to change the update center.
 */
public HttpResponse doSiteConfigure(@QueryParameter String site) throws IOException {
    Hudson hudson = Hudson.getInstance();
    hudson.checkPermission(Hudson.ADMINISTER);
    UpdateCenter uc = hudson.getUpdateCenter();
    PersistedList<UpdateSite> sites = uc.getSites();
    for (UpdateSite s : sites) {
        if (s.getId().equals("default"))
            sites.remove(s);
    }
    sites.add(new UpdateSite("default", site));
    return HttpResponses.redirectToContextRoot();
}
Also used : UpdateCenter(hudson.model.UpdateCenter) Hudson(hudson.model.Hudson) UpdateSite(hudson.model.UpdateSite)

Example 3 with UpdateSite

use of hudson.model.UpdateSite in project plugin-compat-tester by jenkinsci.

the class PluginCompatTester method scanWAR.

/**
 * Scans through a WAR file, accumulating plugin information
 * @param war WAR to scan
 * @param pluginGroupIds Map pluginName to groupId if set in the manifest, MUTATED IN THE EXECUTION
 * @return Update center data
 * @throws IOException
 */
private UpdateSite.Data scanWAR(File war, Map<String, String> pluginGroupIds) throws IOException {
    JSONObject top = new JSONObject();
    top.put("id", DEFAULT_SOURCE_ID);
    JSONObject plugins = new JSONObject();
    JarFile jf = new JarFile(war);
    if (pluginGroupIds == null) {
        pluginGroupIds = new HashMap<String, String>();
    }
    try {
        Enumeration<JarEntry> entries = jf.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            String name = entry.getName();
            Matcher m = Pattern.compile(JENKINS_CORE_FILE_REGEX).matcher(name);
            if (m.matches()) {
                if (top.has("core")) {
                    throw new IOException(">1 jenkins-core.jar in " + war);
                }
                // http://foobar is used to workaround the check in https://github.com/jenkinsci/jenkins/commit/f8daafd0327081186c06555f225e84c420261b4c
                // We do not really care about the value
                top.put("core", new JSONObject().accumulate("name", "core").accumulate("version", m.group(1)).accumulate("url", "https://foobar"));
            }
            // TODO: should it also scan detached plugins info?
            m = Pattern.compile("WEB-INF/(?:optional-)?plugins/([^/.]+)[.][hj]pi").matcher(name);
            if (m.matches()) {
                JSONObject plugin = new JSONObject().accumulate("url", "");
                InputStream is = jf.getInputStream(entry);
                try {
                    JarInputStream jis = new JarInputStream(is);
                    try {
                        Manifest manifest = jis.getManifest();
                        String shortName = manifest.getMainAttributes().getValue("Short-Name");
                        if (shortName == null) {
                            shortName = manifest.getMainAttributes().getValue("Extension-Name");
                            if (shortName == null) {
                                shortName = m.group(1);
                            }
                        }
                        plugin.put("name", shortName);
                        pluginGroupIds.put(shortName, manifest.getMainAttributes().getValue("Group-Id"));
                        String version = manifest.getMainAttributes().getValue("Plugin-Version");
                        // Remove extra build information from the version number
                        final Matcher matcher = Pattern.compile("^(.+-SNAPSHOT)(.+)$").matcher(version);
                        if (matcher.matches()) {
                            version = matcher.group(1);
                        }
                        plugin.put("version", version);
                        plugin.put("url", "jar:" + war.toURI() + "!/" + name);
                        JSONArray dependenciesA = new JSONArray();
                        String dependencies = manifest.getMainAttributes().getValue("Plugin-Dependencies");
                        if (dependencies != null) {
                            // e.g. matrix-auth:1.0.2;resolution:=optional,credentials:1.8.3;resolution:=optional
                            for (String pair : dependencies.split(",")) {
                                boolean optional = pair.endsWith("resolution:=optional");
                                String[] nameVer = pair.replace(";resolution:=optional", "").split(":");
                                assert nameVer.length == 2;
                                dependenciesA.add(new JSONObject().accumulate("name", nameVer[0]).accumulate("version", nameVer[1]).accumulate("optional", String.valueOf(optional)));
                            }
                        }
                        plugin.accumulate("dependencies", dependenciesA);
                        plugins.put(shortName, plugin);
                    } finally {
                        jis.close();
                    }
                } finally {
                    is.close();
                }
            }
        }
    } finally {
        jf.close();
    }
    top.put("plugins", plugins);
    if (!top.has("core")) {
        throw new IOException("no jenkins-core.jar in " + war);
    }
    System.out.println("Scanned contents of " + war + ": " + top);
    return newUpdateSiteData(new UpdateSite(DEFAULT_SOURCE_ID, null), top);
}
Also used : Matcher(java.util.regex.Matcher) JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JSONArray(net.sf.json.JSONArray) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Manifest(java.util.jar.Manifest) JSONObject(net.sf.json.JSONObject) UpdateSite(hudson.model.UpdateSite)

Example 4 with UpdateSite

use of hudson.model.UpdateSite in project plugin-compat-tester by jenkinsci.

the class PluginCompatTester method extractUpdateCenterData.

/**
 * Extracts Update Site data from the update center.
 * @param groupIDs Target storage for Group IDs. The existing values won't be overridden
 * @return Update site Data
 */
private UpdateSite.Data extractUpdateCenterData(Map<String, String> groupIDs) {
    URL url = null;
    String jsonp = null;
    try {
        url = new URL(config.updateCenterUrl);
        jsonp = IOUtils.toString(url.openStream());
    } catch (IOException e) {
        throw new RuntimeException("Invalid update center url : " + config.updateCenterUrl, e);
    }
    String json = jsonp.substring(jsonp.indexOf('(') + 1, jsonp.lastIndexOf(')'));
    UpdateSite us = new UpdateSite(DEFAULT_SOURCE_ID, url.toExternalForm());
    JSONObject jsonObj = JSONObject.fromObject(json);
    UpdateSite.Data site = newUpdateSiteData(us, jsonObj);
    // UpdateSite.Plugin does not contain gav object, so we process the JSON object on our own here
    for (Map.Entry<String, JSONObject> e : (Set<Map.Entry<String, JSONObject>>) jsonObj.getJSONObject("plugins").entrySet()) {
        String gav = e.getValue().getString("gav");
        String groupId = gav.split(":")[0];
        groupIDs.putIfAbsent(e.getKey(), groupId);
    }
    return site;
}
Also used : SortedSet(java.util.SortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ScmFileSet(org.apache.maven.scm.ScmFileSet) TreeSet(java.util.TreeSet) JSONObject(net.sf.json.JSONObject) IOException(java.io.IOException) UpdateSite(hudson.model.UpdateSite) Map(java.util.Map) HashMap(java.util.HashMap) URL(java.net.URL)

Aggregations

UpdateSite (hudson.model.UpdateSite)4 Hudson (hudson.model.Hudson)2 IOException (java.io.IOException)2 URL (java.net.URL)2 JSONObject (net.sf.json.JSONObject)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 FilePath (hudson.FilePath)1 UpdateCenter (hudson.model.UpdateCenter)1 Data (hudson.model.UpdateSite.Data)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 SortedSet (java.util.SortedSet)1 TreeSet (java.util.TreeSet)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1