Skip to content

Commit

Permalink
增加插件描述
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce.wu committed Nov 13, 2024
1 parent bccfc38 commit b55808a
Show file tree
Hide file tree
Showing 23 changed files with 267 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<version>${revision}${changelist}</version>
<packaging>hpi</packaging>
<name>Jenkins ConfigOps Plugin</name>
<url>https://github.com/jenkinsci/${project.artifactId}-plugin</url>
<url>https://github.com/dumasd/${project.artifactId}</url>
<licenses>
<license>
<name>MIT License</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ public class LiquibaseRawCmdStep extends Step implements Serializable {
* Database ID
*/
private final String databaseId;
/**
* 在哪个路径下执行
*/
private String cwd;
/**
* Liquibase命令
*/
Expand All @@ -70,6 +66,14 @@ public class LiquibaseRawCmdStep extends Step implements Serializable {
* Liquibase命令参数
*/
private final String args;
/**
* changeLogFile文件
*/
private String changeLogFile;
/**
* 在哪个路径下执行
*/
private String cwd;

@DataBoundConstructor
public LiquibaseRawCmdStep(
Expand All @@ -81,6 +85,16 @@ public LiquibaseRawCmdStep(
this.args = args;
}

@DataBoundSetter
public void setToolUrl(String toolUrl) {
this.toolUrl = toolUrl;
}

@DataBoundSetter
public void setChangeLogFile(String changeLogFile) {
this.changeLogFile = changeLogFile;
}

@DataBoundSetter
public void setCwd(String cwd) {
this.cwd = cwd;
Expand Down Expand Up @@ -113,14 +127,20 @@ protected Map<String, Object> run() throws Exception {
cwdPath = workspace.child(step.getCwd());
}

String changeLogFile = null;
if (StringUtils.isNotBlank(step.getChangeLogFile())) {
FilePath changeLogFilePath = workspace.child(step.getChangeLogFile());
changeLogFile = changeLogFilePath.getRemote();
}

Logger logger = new Logger("LiquibaseCommand", taskListener);

logger.log(
"cwd:%s, command:%s, args:%s",
Util.fixNull(step.getCwd()), step.getCommand(), Util.fixNull(step.getArgs()));

DatabaseRunLiquibaseResp resp = cwdPath.act(new RemoteExecutionCallable(
step.getToolUrl(), step.getDatabaseId(), step.getCommand(), step.getArgs()));
step.getToolUrl(), step.getDatabaseId(), step.getCommand(), step.getArgs(), changeLogFile));

logger.log("Liquibase run command return code: %s", resp.getRetcode());
if (StringUtils.isNotBlank(resp.getStderr())) {
Expand All @@ -142,12 +162,15 @@ public static class RemoteExecutionCallable extends MasterToSlaveFileCallable<Da
private final String databaseId;
private final String command;
private final String args;
private final String changeLogFile;

public RemoteExecutionCallable(String toolUrl, String databaseId, String command, String args) {
public RemoteExecutionCallable(
String toolUrl, String databaseId, String command, String args, String changeLogFile) {
this.toolUrl = toolUrl;
this.databaseId = databaseId;
this.command = command;
this.args = args;
this.changeLogFile = changeLogFile;
}

@Override
Expand All @@ -162,6 +185,7 @@ public DatabaseRunLiquibaseResp invoke(File f, VirtualChannel channel)
.cwd(f.getAbsolutePath())
.command(command)
.args(args)
.changeLogFile(changeLogFile)
.build();
return client.databaseRunLiquibase(req);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
public class DatabaseRunLiquibaseReq implements Serializable {
private static final long serialVersionUID = -2239214531976180856L;

private String changeLogFile;

private String dbId;

private String cwd;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
package io.jenkins.plugins.configops.nacos;

import static hudson.model.ChoiceParameterDefinition.CHOICES_DELIMITER;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.remoting.VirtualChannel;
import hudson.util.FormValidation;
import io.jenkins.plugins.configops.model.dto.NacosConfigDTO;
import io.jenkins.plugins.configops.model.req.NacosGetChangeSetReq;
import io.jenkins.plugins.configops.model.resp.NacosGetChangeSetResp;
import io.jenkins.plugins.configops.utils.ConfigOpsClient;
import io.jenkins.plugins.configops.utils.Constants;
import io.jenkins.plugins.configops.utils.Logger;
import io.jenkins.plugins.configops.utils.Utils;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import jenkins.MasterToSlaveFileCallable;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.steps.Step;
Expand All @@ -33,6 +44,9 @@
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.verb.POST;

@Setter
@Getter
Expand Down Expand Up @@ -78,6 +92,24 @@ public void setVars(HashMap<String, String> vars) {
this.vars = vars;
}

@DataBoundSetter
public void setVars(String vars) {
String strippedChoices = StringUtils.trim(vars);
HashMap<String, String> result = new LinkedHashMap<>();
if (StringUtils.isNotBlank(strippedChoices)) {
List<String> choices = Arrays.stream(strippedChoices.split(CHOICES_DELIMITER))
.map(StringUtils::trim)
.filter(StringUtils::isNotBlank)
.distinct()
.collect(Collectors.toList());
for (String choice : choices) {
String[] kv = choice.split(":");
result.put(kv[0], kv[1]);
}
}
this.vars = result;
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new StepExecutionImpl(context, this);
Expand Down Expand Up @@ -173,5 +205,69 @@ public String getDisplayName() {
public String getFunctionName() {
return "nacosChangeSetGet";
}

@POST
public FormValidation doCheckNacosId(@QueryParameter("nacosId") String nacosId) {
if (Utils.isNullOrEmpty(nacosId)) {
return FormValidation.error("NacosId is required");
}
return FormValidation.ok();
}

@POST
public FormValidation doCheckChangeLogFile(@QueryParameter("changeLogFile") String changeLogFile) {
if (Utils.isNullOrEmpty(changeLogFile)) {
return FormValidation.error("ChangeLogFile is required");
}
return FormValidation.ok();
}

@POST
public FormValidation doCheckVars(@QueryParameter("vars") String vars) {
if (!areValidVars(vars)) {
return FormValidation.error("Vars is invalid");
}
return FormValidation.ok();
}

@Override
public Step newInstance(@Nullable StaplerRequest req, @NonNull JSONObject formData) throws FormException {
String nacosId = formData.getString("nacosId");
String changeLogFile = formData.getString("changeLogFile");
Object contexts = formData.getOrDefault("contexts", null);
Object count = formData.getOrDefault("count", null);
Object vars = formData.getOrDefault("vars", null);
NacosChangeSetGetStep step = new NacosChangeSetGetStep(nacosId, null, changeLogFile);
if (Objects.nonNull(contexts) && StringUtils.isNotBlank(contexts.toString())) {
step.setContexts(contexts.toString());
}

if (Objects.nonNull(count) && StringUtils.isNotBlank(count.toString())) {
step.setCount(Integer.parseInt(count.toString()));
}

if (Objects.nonNull(vars)) {
step.setVars(vars.toString());
}
return step;
}
}

public static boolean areValidVars(String value) {
String strippedChoices = StringUtils.trim(value);
if (StringUtils.isBlank(strippedChoices)) {
return true;
}
String[] choices = strippedChoices.split(CHOICES_DELIMITER);
if (ArrayUtils.isEmpty(choices)) {
return true;
}
for (String choice : choices) {
String[] pair = choice.split(":");
if (pair.length != 2) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.bind.JavaScriptMethod;
Expand All @@ -40,6 +41,7 @@ public class NacosConfigAlterParameterDefinition extends ParameterDefinition {
private static final long serialVersionUID = -6182842383814244217L;

private List<NacosConfigDTO> items;
private boolean selectAll = true;

@DataBoundConstructor
public NacosConfigAlterParameterDefinition(String name, @NonNull List<NacosConfigDTO> items) {
Expand All @@ -53,6 +55,11 @@ public NacosConfigAlterParameterDefinition(String name, @NonNull List<NacosConfi
});
}

@DataBoundSetter
public void setSelectAll(boolean selectAll) {
this.selectAll = selectAll;
}

public int getItemSelectSize() {
return Math.min(10, items.size());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="${%databaseId}" field="databaseId">
<f:textbox/>
</f:entry>
<f:entry title="${%command}" field="command">
<f:textbox/>
</f:entry>
<f:entry title="${%changeLogFile}" field="changeLogFile">
<f:textbox/>
</f:entry>
<f:entry title="${%args}" field="args">
<f:textbox/>
</f:entry>
<f:entry title="${%cwd}" field="cwd">
<f:textbox/>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
databaseId=DatabaseId
changeLogFile=ChangeLogFile
command=Command
args=Args
cwd=Cwd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
The Liquibase command args.<a href="https://docs.liquibase.com/commands/command-list.html">https://docs.liquibase.com/commands/command-list.html</a>
.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The specified Liquibase change log, can be a file or folder.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
The Liquibase command.<a href="https://docs.liquibase.com/commands/home.html">https://docs.liquibase.com/commands/home.html</a>
.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The Liquibase command is executed in the specified directory.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
Database ID configured in the <a href="https://github.com/dumasd/config-ops/blob/main/config.yaml.sample">config-ops
configuration file</a> .
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<p>Directly execute the Liquibase command to update the database.</p>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div>
<p>Apply the nacos change set.</p>

Config parameters please refer to:
<a href="https://github.com/dumasd/jenkins-config-ops-plugin/blob/main/src/main/java/io/jenkins/plugins/configops/nacos/NacosChangeSetApplyStep.java">NacosChangeSetApplyStep.java</a>

</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="${%nacosId}" field="nacosId">
<f:textbox/>
</f:entry>
<f:entry title="${%changeLogFile}" field="changeLogFile">
<f:textbox/>
</f:entry>
<f:entry title="${%contexts}" field="contexts">
<f:textbox/>
</f:entry>
<f:entry title="${%count}" field="count">
<f:textbox/>
</f:entry>
<f:entry title="${%vars}" field="vars">
<f:textarea/>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
nacosId=NacosId
changeLogFile=ChangeLogFile
contexts=Contexts
count=Count
vars=Vars
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The specified change log, can be a file or folder.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Only execute the specified contexts of changesets. If not filled, all changesets will be executed by default.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Only execute the specified count of changesets. If not filled, all changesets will be executed by default.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
Nacos ID configured in the <a href="https://github.com/dumasd/config-ops/blob/main/config.yaml.sample">config-ops
configuration file</a> .
</div>
Loading

0 comments on commit b55808a

Please sign in to comment.