提交 18833611 编写于 作者: I Ing. Pavel Janousek

[JENKINS-30521] reload-job CLI command moved from Core to CLI

上级 7fb4696a
/*
* The MIT License
*
* Copyright (c) 2015 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.cli;
import hudson.Extension;
import hudson.cli.handlers.ViewOptionHandler;
import hudson.model.AbstractItem;
import hudson.model.AbstractProject;
import hudson.model.Item;
import hudson.model.ViewGroup;
import hudson.model.View;
import jenkins.model.Jenkins;
import org.kohsuke.args4j.Argument;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author pjanouse
* @since TODO
*/
@Extension
public class ReloadJobCommand extends CLICommand {
@Argument(usage="Name of the job(s) to reload", required=true, multiValued=true)
private List<String> jobs;
private static final Logger LOGGER = Logger.getLogger(ReloadJobCommand.class.getName());
@Override
public String getShortDescription() {
return Messages.ReloadJobCommand_ShortDescription();
}
@Override
protected int run() throws Exception {
boolean errorOccurred = false;
final Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
stderr.println("The Jenkins instance has not been started, or was already shut down!");
return -1;
}
final HashSet<String> hs = new HashSet<String>();
hs.addAll(jobs);
for (String job_s: hs) {
AbstractItem job = null;
try {
// TODO: JENKINS-30786
Item item = jenkins.getItemByFullName(job_s);
if (item instanceof AbstractItem) {
job = (AbstractItem) item;
} else if (item != null) {
LOGGER.log(Level.WARNING, "Unsupported item type: {0}", item.getClass().getName());
}
if(job == null) {
// TODO: JENKINS-30785
AbstractProject project = AbstractProject.findNearest(job_s);
if(project == null) {
stderr.format("No such job \u2018%s\u2019 exists.\n", job_s);
} else {
stderr.format("No such job \u2018%s\u2019 exists. Perhaps you meant \u2018%s\u2019?", job_s, project.getFullName());
}
errorOccurred = true;
continue;
}
try {
job.checkPermission(AbstractItem.CONFIGURE);
} catch (Exception e) {
stderr.println(e.getMessage());
errorOccurred = true;
continue;
}
job.doReload();
} catch (Exception e) {
final String errorMsg = String.format("Unexpected exception occurred during reloading of job '%s': %s",
job == null ? "(null)" : job.getFullName(),
e.getMessage());
stderr.println(errorMsg);
LOGGER.warning(errorMsg);
errorOccurred = true;
//noinspection UnnecessaryContinue
continue;
}
}
return errorOccurred ? 1 : 0;
}
}
......@@ -680,7 +680,6 @@ public abstract class AbstractItem extends Actionable implements Item, HttpDelet
*
* @since 1.556
*/
@CLIMethod(name="reload-job")
@RequirePOST
public void doReload() throws IOException {
checkPermission(CONFIGURE);
......
......@@ -83,3 +83,5 @@ Cannot build {0} for unknown reasons.
DeleteNodeCommand.ShortDescription=Deletes node(s)
ReloadJobCommand.ShortDescription=Reload job(s)
......@@ -114,4 +114,7 @@ InstallPluginCommand.ShortDescription=Instala um plugin a partir de um arquivo,
# Deletes a node
CLI.delete-node.shortDescription=Remover o n\u00f3
# Deletes a job
DeleteJobCommand.ShortDescription=Remover uma job
DeleteJobCommand.ShortDescription=Remover uma job(s)
ReloadJobCommand.ShortDescription=\
Recarrega job(s) do disco.
......@@ -91,7 +91,6 @@ BallColor.Unstable=Unstable
Build.post_build_steps_failed=Post-build steps failed
CLI.clear-queue.shortDescription=Clears the build queue.
CLI.reload-job.shortDescription=Reloads this job from disk.
CLI.disable-job.shortDescription=Disables a job.
CLI.enable-job.shortDescription=Enables a job.
CLI.disconnect-node.shortDescription=Disconnects from a node.
......
......@@ -477,8 +477,6 @@ AbstractProject.WipeOutPermission.Description=Esta permiss\u00e3o deixa que seja
ChoiceParameterDefinition.MissingChoices=Exige escolhas.
# This permission allows users to run jobs as them on slaves.
Computer.BuildPermission.Description=Esta permiss\u00e3o deixa que usu\u00e1rios executem jobs em slaves.
# Reloads this job from disk.
CLI.reload-job.shortDescription=Recarrega este job do disco.
# No such slave "{0}" exists. Did you mean "{1}"?
Computer.NoSuchSlaveExists=N\u00e3o existe o slave {0}. Voc\u00ea quis dizer "{1}"?
# Aborted
......
/*
* The MIT License
*
* Copyright 2015 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.cli;
import hudson.FilePath;
import hudson.model.FreeStyleProject;
import hudson.model.Job;
import hudson.tasks.Shell;
import jenkins.model.Jenkins;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import java.io.File;
import static hudson.cli.CLICommandInvoker.Matcher.failedWith;
import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput;
import static hudson.cli.CLICommandInvoker.Matcher.succeededSilently;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
/**
* @author pjanouse
*/
public class ReloadJobCommandTest {
private CLICommandInvoker command;
@Rule public final JenkinsRule j = new JenkinsRule();
@Before public void setUp() {
command = new CLICommandInvoker(j, "reload-job");
}
@Test public void reloadJobShouldFailWithoutJobConfigurePermission() throws Exception {
FreeStyleProject project = j.createFreeStyleProject("aProject");
project.getBuildersList().add(new Shell("echo 1"));
assertThat(project.scheduleBuild2(0).get().getLog(), containsString("echo 1"));
changeProjectOnTheDisc(project, "echo 1", "echo 2");
final CLICommandInvoker.Result result = command
.authorizedTo(Job.READ, Jenkins.READ)
.invokeWithArgs("aProject");
assertThat(result, failedWith(1));
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("user is missing the Job/Configure permission"));
assertThat(project.scheduleBuild2(0).get().getLog(), containsString("echo 1"));
}
@Test public void reloadJobShouldFailWithoutJobReadPermission() throws Exception {
FreeStyleProject project = j.createFreeStyleProject("aProject");
project.getBuildersList().add(new Shell("echo 1"));
assertThat(project.scheduleBuild2(0).get().getLog(), containsString("echo 1"));
changeProjectOnTheDisc(project, "echo 1", "echo 2");
final CLICommandInvoker.Result result = command
.authorizedTo(Job.CONFIGURE, Jenkins.READ)
.invokeWithArgs("aProject");
assertThat(result, failedWith(1));
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("No such job \u2018aProject\u2019 exists."));
assertThat(project.scheduleBuild2(0).get().getLog(), containsString("echo 1"));
}
@Test public void reloadJobShouldSucceed() throws Exception {
FreeStyleProject project = j.createFreeStyleProject("aProject");
project.getBuildersList().add(new Shell("echo 1"));
assertThat(project.scheduleBuild2(0).get().getLog(), containsString("echo 1"));
changeProjectOnTheDisc(project, "echo 1", "echo 2");
final CLICommandInvoker.Result result = command
.authorizedTo(Job.READ, Job.CONFIGURE, Jenkins.READ)
.invokeWithArgs("aProject");
assertThat(result, succeededSilently());
assertThat(project.scheduleBuild2(0).get().getLog(), containsString("echo 2"));
}
@Test public void reloadJobShouldFailIfJobDoesNotExist() throws Exception {
final CLICommandInvoker.Result result = command
.authorizedTo(Job.READ, Job.CONFIGURE, Jenkins.READ)
.invokeWithArgs("never_created");
assertThat(result, failedWith(1));
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("No such job \u2018never_created\u2019 exists."));
}
@Test public void reloadJobShouldFailIfJobDoesNotExistButNearExists() throws Exception {
FreeStyleProject project = j.createFreeStyleProject("never_created");
final CLICommandInvoker.Result result = command
.authorizedTo(Job.READ, Job.CONFIGURE, Jenkins.READ)
.invokeWithArgs("never_created1");
assertThat(result, failedWith(1));
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("No such job \u2018never_created1\u2019 exists. Perhaps you meant \u2018never_created\u2019?"));
}
@Test public void reloadJobManyShouldSucceed() throws Exception {
FreeStyleProject project1 = j.createFreeStyleProject("aProject1");
project1.getBuildersList().add(new Shell("echo 1"));
FreeStyleProject project2 = j.createFreeStyleProject("aProject2");
project2.getBuildersList().add(new Shell("echo 1"));
FreeStyleProject project3 = j.createFreeStyleProject("aProject3");
project3.getBuildersList().add(new Shell("echo 1"));
assertThat(project1.scheduleBuild2(0).get().getLog(), containsString("echo 1"));
assertThat(project2.scheduleBuild2(0).get().getLog(), containsString("echo 1"));
assertThat(project3.scheduleBuild2(0).get().getLog(), containsString("echo 1"));
changeProjectOnTheDisc(project1, "echo 1", "echo 2");
changeProjectOnTheDisc(project2, "echo 1", "echo 2");
changeProjectOnTheDisc(project3, "echo 1", "echo 2");
final CLICommandInvoker.Result result = command
.authorizedTo(Job.READ, Job.CONFIGURE, Jenkins.READ)
.invokeWithArgs("aProject1", "aProject2", "aProject3");
assertThat(result, succeededSilently());
assertThat(project1.scheduleBuild2(0).get().getLog(), containsString("echo 2"));
assertThat(project2.scheduleBuild2(0).get().getLog(), containsString("echo 2"));
assertThat(project3.scheduleBuild2(0).get().getLog(), containsString("echo 2"));
}
@Test public void reloadJobManyShouldFailIfAJobDoesNotExist() throws Exception {
FreeStyleProject project1 = j.createFreeStyleProject("aProject1");
project1.getBuildersList().add(new Shell("echo 1"));
FreeStyleProject project2 = j.createFreeStyleProject("aProject2");
project2.getBuildersList().add(new Shell("echo 1"));
assertThat(project1.scheduleBuild2(0).get().getLog(), containsString("echo 1"));
assertThat(project2.scheduleBuild2(0).get().getLog(), containsString("echo 1"));
changeProjectOnTheDisc(project1, "echo 1", "echo 2");
changeProjectOnTheDisc(project2, "echo 1", "echo 2");
final CLICommandInvoker.Result result = command
.authorizedTo(Job.READ, Job.CONFIGURE, Jenkins.READ)
.invokeWithArgs("aProject1", "aProject2", "never_created");
assertThat(result, failedWith(1));
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("No such job \u2018never_created\u2019 exists."));
assertThat(project1.scheduleBuild2(0).get().getLog(), containsString("echo 2"));
assertThat(project2.scheduleBuild2(0).get().getLog(), containsString("echo 2"));
}
@Test public void reloadJobManyShouldSucceedEvenAJobIsSpecifiedTwice() throws Exception {
FreeStyleProject project1 = j.createFreeStyleProject("aProject1");
project1.getBuildersList().add(new Shell("echo 1"));
FreeStyleProject project2 = j.createFreeStyleProject("aProject2");
project2.getBuildersList().add(new Shell("echo 1"));
assertThat(project1.scheduleBuild2(0).get().getLog(), containsString("echo 1"));
assertThat(project2.scheduleBuild2(0).get().getLog(), containsString("echo 1"));
changeProjectOnTheDisc(project1, "echo 1", "echo 2");
changeProjectOnTheDisc(project2, "echo 1", "echo 2");
final CLICommandInvoker.Result result = command
.authorizedTo(Job.READ, Job.CONFIGURE, Jenkins.READ)
.invokeWithArgs("aProject1", "aProject2", "aProject1");
assertThat(result, succeededSilently());
assertThat(project1.scheduleBuild2(0).get().getLog(), containsString("echo 2"));
assertThat(project2.scheduleBuild2(0).get().getLog(), containsString("echo 2"));
}
/**
* Modify a project directly on the disc
*
* @param project modified project
* @param oldstr old configuration item - for rewrite
* @param newstr rew configuration item - after rewrite
* @throws Exception if an issue occurred
*/
private void changeProjectOnTheDisc(final FreeStyleProject project, final String oldstr,
final String newstr) throws Exception {
FilePath fp = new FilePath(new File(project.getRootDir()+"/config.xml"));
fp.write(fp.readToString().replace(oldstr, newstr), null);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册