package org.jenkinsci.plugins.mesos.listener;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.Computer;
import hudson.model.Executor;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.model.Run;
import hudson.model.listeners.RunListener;

import java.io.IOException;
import java.util.logging.Logger;

import org.jenkinsci.plugins.mesos.MesosSlave;

@SuppressWarnings("rawtypes")
@Extension
public class MesosRunListener extends RunListener<Run> {

  private static final Logger LOGGER = Logger.getLogger(MesosRunListener.class.getName());
  
  public MesosRunListener() {
    
  }
  
  /**
   * @param targetType
   */
  @SuppressWarnings("unchecked")
  public MesosRunListener(Class targetType) {
    super(targetType);    
  }
  
  /**
   * Prints the actual Hostname where Mesos slave is provisioned in console output.
   * This would help us debug/take action if build fails in that slave.
   */
  @Override
  public void onStarted(Run r, TaskListener listener) {
    if (r instanceof AbstractBuild) {
      Node node = getCurrentNode();
      if (node instanceof MesosSlave) {
        try {
          Computer computer = node.toComputer();
          if (computer != null) {
            String hostname = computer.getHostName();
            listener.getLogger().println("Mesos slave(hostname): " + hostname);
          } else {
            listener.getLogger().println("Mesos slave(computer is null)");
          }
        } catch (IOException e) {
          LOGGER.warning("IOException while trying to get hostname: " + e);
          e.printStackTrace();
        } catch (InterruptedException e) {
          LOGGER.warning("InterruptedException while trying to get hostname: " + e);
        }
      }
    }
  }

  /**
   * Returns the current {@link Node} on which we are building.
   */
  private final Node getCurrentNode() {
    Executor executor = Executor.currentExecutor();
    if (executor != null) {
      Computer owner = executor.getOwner();
      if (owner != null) {
        return owner.getNode();
      }
    }
    return null;
  }

}

@@ -36,24 +36,47 @@
 .listener;%0A%0A
+import hudson.EnvVars;%0A
 import hudso
@@ -614,16 +614,116 @@
 e());%0A  
+private static final String HOSTNAME = %22HOSTNAME%22;%0A  private static final String DOCKER = %22DOCKER%22;%0A
 %0A  publi
@@ -1269,16 +1269,50 @@
   try %7B%0A
+          String hostname = null;%0A
         
@@ -1366,28 +1366,117 @@
     
-if (computer
+MesosSlave mesosSlave = (MesosSlave) node;%0A          if (mesosSlave.getSlaveInfo().getContainerInfo()
  != null
 ) %7B%0A
@@ -1463,36 +1463,39 @@
 erInfo() != null
-) %7B%0A
+ &&%0A   
             Stri
@@ -1490,31 +1490,161 @@
         
-String hostname
+   DOCKER.equals(mesosSlave.getSlaveInfo().getContainerInfo().getType())) %7B%0A            if (computer != null) %7B%0A              EnvVars environment
  = compu
@@ -1654,16 +1654,19 @@
 .get
-HostName
+Environment
 ();%0A
@@ -1681,73 +1681,166 @@
     
-listener.getLogger().println(%22Mesos slave(hostname): %22 + hostn
+  hostname = environment.get(HOSTNAME);%0A            %7D%0A          %7D else %7B%0A              if(computer != null) %7B%0A                hostname = computer.getHostN
 ame
+(
 );%0A 
@@ -1844,35 +1844,42 @@
 ;%0A          
-%7D else %7B%0A  
+    %7D%0A          %7D%0A
           li
@@ -1922,40 +1922,33 @@
 ave(
-computer is null)%22);%0A          %7D
+hostname): %22 + hostname);
 %0A   

Fix for logging mesos slave hostname