Skip to content

Instantly share code, notes, and snippets.

@gtudan gtudan/restart.coffee Secret
Created May 4, 2018

Embed
What would you like to do?
Hubot Restart-Skript
# Description:
# Startet einen Dienst nach Ankündigung im Channel neu
#
# Dependencies:
# "cron": "1.0.9"
# "moment": "2.22.1"
#
# Configuration:
# RUNDECK_TOKEN - ein Auth-Token für Rundeck
# RUNDECK_URL - die Rundeck Base URL
# RUNDECK_URL - eas Rundeck Projekt
#
# Commands:
# hubot restart <service> auf <environment> - Starte einen Countdown für den Neustart des Dienstes
# hubot restart <service> auf <environment> in <n>min - Starte einen n-minütigen Countdown für den Neustart des Dienstes
# hubot Veto - abort a pending Restart
#
# Author:
# gtudan
cronJob = require('cron').CronJob
moment = require('moment')
jobs = [];
module.exports = (robot) ->
robot.respond /restart (Wildfly|Keycloak) auf (E|QS|Test|Prod)( in (\d+)min)?/i, (res) ->
service = res.match[1]
environment = res.match[2]
time = res.match[4]
restartService(res, service, environment, time)
robot.respond /Veto.*/i, (res) ->
job = jobs.pop()
if (job)
job.stop();
res.reply "Neustart abgebrochen!"
robot.messageRoom job.user, "#{res.envelope.user.name} hat ein Veto zu deinem Neustart eingelegt."
restartService = (res, service, environment, timeInMinutes) ->
if environment in ['Test', 'Prod']
res.reply "Es tut mir Leid, #{user}, aber das kann ich nicht tun."
else
user = res.envelope.user.name
res.reply "Okay, los geht's"
job = new Job(service, environment, user, timeInMinutes)
jobs.push job
job.start(robot)
robot.messageRoom 'entwicklung', "@here #{user} möchte einen #{service}-Neustart auf #{environment} in #{timeInMinutes} Minuten! Veto?"
class Job
constructor: (@service, @env, @user, @timeInMinutes) ->
start: (robot) ->
at = moment().add(@timeInMinutes, 'minutes').toDate();
@cronjob = new cronJob(at, => @restartService robot)
@cronjob.start()
stop: ->
@cronjob.stop()
restartService: (robot) ->
jobs.pop()
robot.messageRoom 'entwicklung', "@here #{@service} auf #{@env} startet neu!"
new Rundeck(robot).restart(@service, @env)
class Rundeck
constructor: (@robot) ->
@logger = @robot.logger
@authToken = process.env.RUNDECK_TOKEN
@project = process.env.RUNDECK_PROJECT
@baseUrl = "#{process.env.RUNDECK_URL}/api/14"
@headers =
"Accept": "application/json"
"Content-Type": "application/json"
"X-Rundeck-Auth-Token": "#{@authToken}"
restart: (service, env) ->
command =
"project": "#{@project}"
"nodeThreadcount": 4
"exec": "sudo systemctl restart #{service.toLowerCase()}"
"filter": "tags: #{service.toLowerCase()}, environment: #{env}"
robot.http("#{@baseUrl}/project/#{@project}/run/command")
.headers(@headers)
.post(JSON.stringify(command)) (err, res, body) =>
if err?
@logger.error JSON.stringify(err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.