summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClark Boylan <clark.boylan@gmail.com>2014-01-28 16:51:09 -0800
committerClark Boylan <clark.boylan@gmail.com>2014-01-29 18:06:10 -0800
commitdf9ee54284b3214ade72dadea7361655933a7c1f (patch)
tree490a78c76197eb36fe6e78f8e9da13ddb9c66545
parent7e2ca62e1c078061ffd620e46cb095f31c31420c (diff)
Get master hostname from master computer object.0.0.3
Jenkins.getDisplayName() is useless and returns "Jenkins". Get the name of the master via the Jenkins master computer object. Fall back on doing a DNS name lookup otherwise. Change-Id: Icee4e22e9ca989fd1faa6d27f4dad3be76da6385
Notes
Notes (review): Verified+2: Jenkins Approved+1: Clark Boylan <cboylan@sapwetik.org> Code-Review+2: Clark Boylan <cboylan@sapwetik.org> Code-Review+1: Khai Do <zaro0508@gmail.com> Submitted-by: Jenkins Submitted-at: Wed, 05 Feb 2014 23:28:24 +0000 Reviewed-on: https://review.openstack.org/69763 Project: openstack-infra/zmq-event-publisher Branch: refs/heads/master
-rw-r--r--src/main/java/org/jenkinsci/plugins/ZMQEventPublisher/Phase.java10
-rw-r--r--src/main/java/org/jenkinsci/plugins/ZMQEventPublisher/RunListenerImpl.java27
2 files changed, 30 insertions, 7 deletions
diff --git a/src/main/java/org/jenkinsci/plugins/ZMQEventPublisher/Phase.java b/src/main/java/org/jenkinsci/plugins/ZMQEventPublisher/Phase.java
index cfce93f..e198f0f 100644
--- a/src/main/java/org/jenkinsci/plugins/ZMQEventPublisher/Phase.java
+++ b/src/main/java/org/jenkinsci/plugins/ZMQEventPublisher/Phase.java
@@ -38,7 +38,7 @@ public enum Phase {
38 STARTED, COMPLETED, FINISHED; 38 STARTED, COMPLETED, FINISHED;
39 39
40 @SuppressWarnings({ "unchecked", "rawtypes" }) 40 @SuppressWarnings({ "unchecked", "rawtypes" })
41 public String handlePhase(Run run, String status, TaskListener listener) { 41 public String handlePhase(Run run, String status, String masterName, TaskListener listener) {
42 Jenkins jenkins = Jenkins.getInstance(); 42 Jenkins jenkins = Jenkins.getInstance();
43 HudsonNotificationProperty property = (HudsonNotificationProperty) 43 HudsonNotificationProperty property = (HudsonNotificationProperty)
44 run.getParent().getProperty(HudsonNotificationProperty.class); 44 run.getParent().getProperty(HudsonNotificationProperty.class);
@@ -47,7 +47,7 @@ public enum Phase {
47 jenkins.getDescriptor(HudsonNotificationProperty.class); 47 jenkins.getDescriptor(HudsonNotificationProperty.class);
48 if ((property != null && property.isEnabled()) || 48 if ((property != null && property.isEnabled()) ||
49 (globalProperty != null && globalProperty.isGloballyEnabled())) { 49 (globalProperty != null && globalProperty.isGloballyEnabled())) {
50 return buildMessage(run.getParent(), run, status); 50 return buildMessage(run.getParent(), run, status, masterName);
51 } 51 }
52 return null; 52 return null;
53 } 53 }
@@ -55,7 +55,7 @@ public enum Phase {
55 private Gson gson = new GsonBuilder().setFieldNamingPolicy( 55 private Gson gson = new GsonBuilder().setFieldNamingPolicy(
56 FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create(); 56 FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
57 57
58 private String buildMessage(Job job, Run run, String status) { 58 private String buildMessage(Job job, Run run, String status, String masterName) {
59 JobState jobState = new JobState(); 59 JobState jobState = new JobState();
60 jobState.setName(job.getName()); 60 jobState.setName(job.getName());
61 jobState.setUrl(job.getUrl()); 61 jobState.setUrl(job.getUrl());
@@ -70,13 +70,15 @@ public enum Phase {
70 if (rootUrl != null) { 70 if (rootUrl != null) {
71 buildState.setFullUrl(rootUrl + run.getUrl()); 71 buildState.setFullUrl(rootUrl + run.getUrl());
72 } 72 }
73 if (masterName != null) {
74 buildState.setHostName(masterName);
75 }
73 76
74 Executor executor = run.getExecutor(); 77 Executor executor = run.getExecutor();
75 if (executor != null) { 78 if (executor != null) {
76 Computer computer = executor.getOwner(); 79 Computer computer = executor.getOwner();
77 if (computer != null) { 80 if (computer != null) {
78 buildState.setNodeName(computer.getName()); 81 buildState.setNodeName(computer.getName());
79 buildState.setHostName(jenkins.getDisplayName());
80 } 82 }
81 } 83 }
82 84
diff --git a/src/main/java/org/jenkinsci/plugins/ZMQEventPublisher/RunListenerImpl.java b/src/main/java/org/jenkinsci/plugins/ZMQEventPublisher/RunListenerImpl.java
index 5ac462f..e630cc5 100644
--- a/src/main/java/org/jenkinsci/plugins/ZMQEventPublisher/RunListenerImpl.java
+++ b/src/main/java/org/jenkinsci/plugins/ZMQEventPublisher/RunListenerImpl.java
@@ -18,12 +18,16 @@
18package org.jenkinsci.plugins.ZMQEventPublisher; 18package org.jenkinsci.plugins.ZMQEventPublisher;
19 19
20import hudson.Extension; 20import hudson.Extension;
21import hudson.model.Computer;
21import hudson.model.Result; 22import hudson.model.Result;
22import hudson.model.Run; 23import hudson.model.Run;
23import hudson.model.TaskListener; 24import hudson.model.TaskListener;
24import hudson.model.listeners.RunListener; 25import hudson.model.listeners.RunListener;
25import hudson.util.DaemonThreadFactory; 26import hudson.util.DaemonThreadFactory;
26 27
28import jenkins.model.Jenkins;
29
30import java.net.InetAddress;
27import java.util.concurrent.LinkedBlockingQueue; 31import java.util.concurrent.LinkedBlockingQueue;
28import java.util.logging.Level; 32import java.util.logging.Level;
29import java.util.logging.Logger; 33import java.util.logging.Logger;
@@ -43,18 +47,35 @@ public class RunListenerImpl extends RunListener<Run> {
43 new DaemonThreadFactory(); 47 new DaemonThreadFactory();
44 private ZMQRunnable ZMQRunner; 48 private ZMQRunnable ZMQRunner;
45 private Thread thread; 49 private Thread thread;
50 private String masterName;
46 51
47 public RunListenerImpl() { 52 public RunListenerImpl() {
48 super(Run.class); 53 super(Run.class);
49 ZMQRunner = new ZMQRunnable(queue); 54 ZMQRunner = new ZMQRunnable(queue);
50 thread = threadFactory.newThread(ZMQRunner); 55 thread = threadFactory.newThread(ZMQRunner);
51 thread.start(); 56 thread.start();
57
58 Computer master = null;
59 String hostname = null;
60 // query Jenkins for master's name
61 try {
62 master = Jenkins.getInstance().getComputer("");
63 if (master != null) {
64 hostname = master.getHostName();
65 }
66 else {
67 // master node may not be enabled so get masterName from system
68 hostname = java.net.InetAddress.getLocalHost().getHostName();
69 }
70 } catch (Exception e) {
71 }
72 masterName = hostname;
52 } 73 }
53 74
54 @Override 75 @Override
55 public void onCompleted(Run build, TaskListener listener) { 76 public void onCompleted(Run build, TaskListener listener) {
56 String event = "onCompleted"; 77 String event = "onCompleted";
57 String json = Phase.COMPLETED.handlePhase(build, getStatus(build), listener); 78 String json = Phase.COMPLETED.handlePhase(build, getStatus(build), masterName, listener);
58 sendEvent(event, json); 79 sendEvent(event, json);
59 } 80 }
60 81
@@ -70,14 +91,14 @@ public class RunListenerImpl extends RunListener<Run> {
70 @Override 91 @Override
71 public void onFinalized(Run build) { 92 public void onFinalized(Run build) {
72 String event = "onFinalized"; 93 String event = "onFinalized";
73 String json = Phase.FINISHED.handlePhase(build, getStatus(build), TaskListener.NULL); 94 String json = Phase.FINISHED.handlePhase(build, getStatus(build), masterName, TaskListener.NULL);
74 sendEvent(event, json); 95 sendEvent(event, json);
75 } 96 }
76 97
77 @Override 98 @Override
78 public void onStarted(Run build, TaskListener listener) { 99 public void onStarted(Run build, TaskListener listener) {
79 String event = "onStarted"; 100 String event = "onStarted";
80 String json = Phase.STARTED.handlePhase(build, getStatus(build), listener); 101 String json = Phase.STARTED.handlePhase(build, getStatus(build), masterName, listener);
81 sendEvent(event, json); 102 sendEvent(event, json);
82 } 103 }
83 104