summaryrefslogtreecommitdiffstats
path: root/jenkins/groovy_boot_script.txt
blob: 015484f6852f1ef313f73c153e0f84a1d85acdd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import hudson.slaves.OfflineCause.SimpleOfflineCause
import hudson.util.RemotingDiagnostics
import java.io.PrintWriter
import java.io.StringWriter
import java.lang.System

// returns full stack trace of a throwable 't' as a string
def getStackTrace(t) {
    def sw = new StringWriter()
    def pw = new PrintWriter(sw, true)
    t.printStackTrace(pw)
    pw.flush()
    sw.flush()
    return sw.toString()
}

// FIXME: why cannot we import jenkins.util.NonLocalizable ?
class OfflineMessage extends org.jvnet.localizer.Localizable {
  def message
  OfflineMessage() {
    super(null, null, [])
    def timestr = new Date().format("HH:mm dd/MM/yy z", TimeZone.getTimeZone("UTC"))
    this.message = "automated reboot at end of build at " + timestr
  }
  String toString() {
    this.message
  }
  String toString(java.util.Locale l) {
    toString()
  }
}

def computer = manager.build.getBuiltOn().toComputer()
def channel = computer.getChannel()
def cause = SimpleOfflineCause.create(new OfflineMessage())

def rebooted = 0

for (i in 1..5) {
  try {
    RemotingDiagnostics.executeGroovy( """

      if (Functions.isWindows()) {
        'shutdown /r /t 10 /c "Restarting after Jenkins build completed"'.execute()
      } else {
        "sudo -n /sbin/reboot".execute()
      }

    """, channel )
    rebooted = i
    break
  } catch (Exception e) {
    manager.addWarningBadge("could not reboot [attempt " + i + "]: " + getStackTrace(e))
    // disconnecting the computer, waiting for it to reconnect and reinitializing the
    // channel helps to recover from "Could not initialize class org.codehause.groovy.runtime.InvokerHelper",
    // which occurs on slaves sometimes (with Jenkins 1.466) for unknown reasons.
    computer.disconnect(cause).get()
    sleep(30000)
    channel = computer.getChannel()
    def attempts = 0
    while (channel == null && ++attempts < 5) {
      sleep(30000)
      channel = computer.getChannel()
    }
  }
}

if (rebooted == 0) {
  manager.addShortText("reboot failed")
} else if (rebooted > 1) {
  manager.addShortText("rebooted after " + rebooted + " attempts")
} else {
  manager.addInfoBadge("rebooted after build")
}

if (rebooted) {
  // make sure jobs cannot be scheduled until after the node reboots
  computer.setTemporarilyOffline(true, cause)
}