Last active
December 21, 2015 07:28
-
-
Save jezhalford/f2402afb84892b16acc2 to your computer and use it in GitHub Desktop.
Thing to turn crontabs into puppet manifests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>crontab2puppet - Convert a crontab to a puppet manifest.</title> | |
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet"> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> | |
</head> | |
<body style="padding: 20px"> | |
<h1>crontab2puppet</h1> | |
<p style="padding: 20px 0"> | |
Fill in the boxes below to convert a raw crontab to a snippet of puppet manifest. Comments are preserved. | |
</p> | |
<div class="row clearfix"> | |
<div class="span3"> | |
<label for="user">Username to run cronjobs as</label> | |
<input type="text" id="user" class="triggers"> | |
</div> | |
<div class="span6"> | |
<label for="name">Name prefix for each cronjob (puppet requires that each job have a name)</label> | |
<input type="text" id="name" class="triggers"> | |
</div> | |
</div> | |
<p> | |
<label for="crontab">Paste in your crontab</label> | |
<textarea rows="10" style="width: 75%" id="crontab" class="triggers"></textarea> | |
</p> | |
<label for="result">Result (click to select all)</label> | |
<pre style="width: 75%; min-height: 200px; max-height: 500px; overflow: auto;" id="result"> | |
</pre> | |
<div class="alert alert-block" id="warn" style="width: 75%; display: none;"> | |
<strong>WARNING:</strong> Some lines of your input could not be parsed. | |
</div> | |
<script> | |
$(document).ready(function() { | |
/** | |
* Select the text in the result PRE | |
*/ | |
function selectResult() { | |
if (document.selection) { | |
var range = document.body.createTextRange(); | |
range.moveToElementText($('#result')[0]); | |
range.select(); | |
} else if (window.getSelection) { | |
var range = document.createRange(); | |
range.selectNode($('#result')[0]); | |
window.getSelection().addRange(range); | |
} | |
} | |
/** | |
* Formats a given cron value as appropriate for puppet | |
*/ | |
function formatValue(value) { | |
if(value.indexOf(',') > -1) { | |
return '[' + value + ']'; | |
} | |
return "'" + value + "'"; | |
} | |
/** | |
* Outputs a cron job in puppet manifest format | |
*/ | |
function formatter(min, hour, dayOfMonth, month, dayOfWeek, command, name, user) { | |
var cronspec = []; | |
if(min != '*') { | |
cronspec.push(["\tminute => ", formatValue(min)].join('')); | |
} | |
if(hour != '*') { | |
cronspec.push(["\thour => ", formatValue(hour)].join('')); | |
} | |
if(dayOfMonth != '*') { | |
cronspec.push(["\tmonthday => ", formatValue(dayOfMonth)].join('')); | |
} | |
if(month != '*') { | |
cronspec.push(["\tmonth => ", formatValue(month)].join('')); | |
} | |
if(dayOfWeek != '*') { | |
cronspec.push(["\tweekday => ", formatValue(dayOfWeek)].join('')); | |
} | |
cronspec.push(["\tcommand => '", command, "'"].join('')); | |
return "cron { '" + name + "':\n\tensure => 'present',\n\tuser => '" + user + "',\n" + cronspec.join(',\n') + "\n}\n"; | |
}; | |
/** | |
* Parse the input | |
*/ | |
function parseCrontab(crontab, user, namePrefix) { | |
$('#warn').css({'display' : 'none'}); | |
$('p', $('#warn')).remove(); | |
var jobscount = 1; | |
var lines = crontab.split(/\n/); | |
var out = []; | |
$(lines).each(function(i) { | |
if(this.trim().charAt(0) == '#' || this.trim().length == 0) { | |
out.push(this); | |
} | |
else { | |
var cronData = this.trim().split(/[ ]+/); | |
if(cronData.length < 6) { | |
out.push("# WARNING: Couldn't parse the following from line " + (i+1) + " of your input: "); | |
out.push('#\t' + this); | |
$('#warn').css({'display' : 'block'}).append("<p>Couldn't parse the following from line " + (i+1) + " of your input<br>" + this + "</p>"); | |
} | |
else { | |
out.push( | |
formatter( | |
cronData.shift(), | |
cronData.shift(), | |
cronData.shift(), | |
cronData.shift(), | |
cronData.shift(), | |
cronData.join(' '), | |
namePrefix + '-' + jobscount, | |
user | |
) | |
); | |
jobscount++; | |
} | |
} | |
}); | |
return out.join('\n'); | |
}; | |
/** | |
* Init... | |
*/ | |
var user = $('#user'); | |
var namePrefix = $('#name'); | |
var crontab = $('#crontab'); | |
$('.triggers').on('change keyup', function(){ | |
$('#result').text(parseCrontab(crontab.val(), user.val(), namePrefix.val())); | |
}); | |
$('#result').on('click', selectResult); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment