Skip to content

Commit 7ba1c44

Browse files
committed
Add --labelmatch option for jclouds-templates
1 parent 42dc83b commit 7ba1c44

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

CLI.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ The following CLI commands are provided by the jclouds-plugin:
2424
<a name="jclouds-templates"></a>
2525
### jclouds-templates
2626
SYNTAX:
27-
```java -jar jenkins-cli.jar jclouds-templates ```
27+
```
28+
java -jar jenkins-cli.jar jclouds-templates [-l (--labelmatch) EXPR]
29+
List all JClouds templates.
30+
-l (--labelmatch) EXPR : List templates that satisfy the specified label
31+
expression.
32+
```
2833

2934
With this command you can list all configured JClouds templates on the controller.
3035
A typical output is shown here:
@@ -58,6 +63,7 @@ google bh-stretch-64
5863
DigitalOcean dub2204 dub2204
5964
DigitalOcean dub2004jnlp dub2004jnlp
6065
```
66+
By specifying a [`label expression`](https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#node-allocate-node), you can find out, which templates would satisfy that expression. Of course the label expression usually should be quoted, so that your shell does not try to interpret it.
6167
<a name="jclouds-provision"></a>
6268
### jclouds-provision
6369
SYNTAX:

src/main/java/jenkins/plugins/jclouds/cli/JCloudsTemplatesCommand.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
package jenkins.plugins.jclouds.cli;
1717

1818
import hudson.Extension;
19+
import hudson.Util;
1920
import hudson.cli.CLICommand;
21+
import hudson.model.Label;
2022
import hudson.security.Permission;
2123
import java.io.IOException;
2224
import jenkins.plugins.jclouds.compute.JCloudsCloud;
2325
import jenkins.plugins.jclouds.compute.JCloudsSlaveTemplate;
26+
import org.kohsuke.args4j.Option;
2427

2528
/**
2629
* Provisions an agent.
@@ -38,22 +41,34 @@ public String getShortDescription() {
3841
return Messages.TemplatesCommand_shortDescription();
3942
}
4043

44+
@Option(
45+
required = false,
46+
metaVar = "EXPR",
47+
name = "-l",
48+
aliases = "--labelmatch",
49+
usage = "List templates that satisfy the specified label expression.")
50+
private String labelExpr;
51+
4152
@Override
4253
protected int run() throws IOException {
4354
int maxProfileLen = 0;
4455
int maxTemplateLen = 0;
4556
int maxLabelLen = 0;
57+
labelExpr = Util.fixEmptyAndTrim(labelExpr);
58+
Label label = null == labelExpr ? null : Label.parseExpression(labelExpr);
4659
for (final JCloudsCloud c : CliHelper.getAllJCloudClouds()) {
4760
if (c.hasPermission(Permission.READ)) {
4861
if (c.profile.length() > maxProfileLen) {
4962
maxProfileLen = c.profile.length();
5063
}
5164
for (final JCloudsSlaveTemplate t : c.getTemplates()) {
52-
if (t.name.length() > maxTemplateLen) {
53-
maxTemplateLen = t.name.length();
54-
}
55-
if (t.labelString.length() > maxLabelLen) {
56-
maxLabelLen = t.labelString.length();
65+
if (label == null || label.matches(t.getLabelSet())) {
66+
if (t.name.length() > maxTemplateLen) {
67+
maxTemplateLen = t.name.length();
68+
}
69+
if (t.labelString.length() > maxLabelLen) {
70+
maxLabelLen = t.labelString.length();
71+
}
5772
}
5873
}
5974
}
@@ -76,7 +91,9 @@ protected int run() throws IOException {
7691
final String indent = String.format("%-" + (maxProfileLen + maxTemplateLen + maxLabelLen + 4) + "s", "\n");
7792
for (final JCloudsCloud c : CliHelper.getAllJCloudClouds()) {
7893
for (final JCloudsSlaveTemplate t : c.getTemplates()) {
79-
stdout.printf(fmt, c.profile, t.name, t.labelString, t.description.replaceAll("\n", indent));
94+
if (label == null || label.matches(t.getLabelSet())) {
95+
stdout.printf(fmt, c.profile, t.name, t.labelString, t.description.replaceAll("\n", indent));
96+
}
8097
}
8198
}
8299
}

0 commit comments

Comments
 (0)