Last active
May 8, 2020 18:09
-
-
Save donpdonp/64d8ec7922441e743808 to your computer and use it in GitHub Desktop.
gluon vmctl
This file contains 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
(function() { | |
//setup | |
setup() | |
// descriptor | |
return {name:"vmctl"} | |
}) | |
// cache | |
var applist | |
var github_key | |
function setup() { | |
bot.say(bot.admin_channel, 'vmctl: setup bot owner '+bot.owner) | |
db.get('vmctl:githubkey', function(githubkey){ | |
if(githubkey) { | |
github_key = githubkey | |
bot.say(bot.admin_channel, 'vmctl: github key loaded.') | |
} else { | |
bot.say(bot.admin_channel, 'vmctl: warning: githubkey empty.') | |
} | |
var applist_key = 'vmctl:applist:'+bot.host_id | |
db.get(applist_key, function(json){ | |
if(json) { | |
applist = JSON.parse(json) | |
bot.say(bot.admin_channel, 'vmctl: app lists '+applist_key+' loaded') | |
Object.keys(applist).forEach(function(username){ | |
if(username == bot.owner) { | |
bot.say(bot.admin_channel, 'loading user '+username+' '+applist[username].length+' apps') | |
applist[username].forEach(function(url, idx){ | |
var report = scriptload(url) | |
bot.say(bot.admin_channel, '#'+(idx+1)+'/'+applist[username].length+' '+report) | |
}) | |
} else { | |
bot.say(bot.admin_channel, 'no multiload support. skipping user '+username) | |
} | |
}) | |
} else { | |
bot.say(bot.admin_channel, 'app list '+applist_key+' empty.') | |
applist = {} | |
} | |
}) | |
}) | |
} | |
function scriptload(url){ | |
var gist = gist_expand(url) | |
var fullurl, report | |
if(gist) { | |
fullurl = gist['raw_url'] | |
report = "gist "+gist.filename+" "+gist.size+" bytes "+gist.updated_at | |
if(gist.filename == "vmctl.js"){ | |
fullurl = "" | |
report = "(skip self-load of vmctl.js)" | |
} | |
} else { | |
fullurl = url | |
report = url | |
} | |
var err = vm.add(fullurl) | |
if(err) { | |
report += ' vmctl start err: '+err | |
} | |
return report | |
} | |
function go(msg) { | |
if(msg.method == "irc.privmsg") { | |
var parts = msg.params.message.split(' ') | |
var cmd = parts[0] | |
if(cmd == "vm" && msg.params.nick == bot.owner) { | |
if(parts[1] == 'add') { do_vm_add(msg, parts)} | |
if(parts[1] == 'list') { do_vm_list(msg, parts)} | |
if(parts[1] == 'del') { do_vm_del(msg, parts)} | |
if(parts[1] == 'reload') { do_vm_reload(msg, parts)} | |
} | |
} | |
} | |
function do_vm_add(msg, parts) { | |
var url = parts[2] | |
var gist = gist_expand(url) | |
var fullurl = gist ? gist['raw_url'] : url | |
var report = gist ? "gist "+gist.filename+" "+gist.size+" bytes "+gist.updated_at : url | |
var err = vm.add(fullurl) | |
if(err) { | |
bot.say(msg.params.channel, err) | |
} else { | |
bot.say(msg.params.channel, "Added "+report) | |
add_to_applist(msg.params.nick, url) | |
} | |
} | |
function add_to_applist(nick, url) { | |
if(!applist[nick]) { | |
applist[nick] = [] | |
} | |
var idx = applist[nick].indexOf(url) | |
if( idx == -1 ) { | |
applist[nick].push(url) | |
var applist_key = 'vmctl:applist:'+bot.host_id | |
var app_report = Object.keys(applist).map(function(username){return username+" "+applist[username].length+" apps"}) | |
bot.say(bot.admin_channel, 'saving applist '+app_report.join(',')) | |
db.set(applist_key, JSON.stringify(applist)) | |
} | |
} | |
function remove_from_applist(nick, url){ | |
if(!applist[nick]) { | |
applist[nick] = [] | |
} | |
var idx = applist[nick].indexOf(url) | |
if( idx > -1 ) { | |
applist[nick].splice(idx, 1) | |
var applist_key = 'vmctl:applist:'+bot.host_id | |
db.set(applist_key, JSON.stringify(applist), function(result){ | |
bot.say(bot.admin_channel, "vmctl: "+url+" removed from applist") | |
}) | |
} else { | |
bot.say(bot.admin_channel, "vm.del remove_from_applist url not found "+url) | |
} | |
} | |
function do_vm_list(msg, parts) { | |
vm.list(function(vms){ | |
var ownergroups = vms.reduce(function(m, owna){ | |
var parts=owna.split('/') | |
if(!m[parts[0]]) { m[parts[0]] = [] } | |
m[parts[0]].push(parts[1]) | |
return m}, {}) | |
var owner = msg.params.nick | |
if(Object.keys(ownergroups).indexOf(owner) > -1) { | |
bot.say(msg.params.channel, "["+owner+"] "+ownergroups[owner].sort().join()) | |
} else { | |
bot.say(msg.params.channel, msg.params.nick+": no vms loaded for you.") | |
} | |
}) | |
} | |
function do_vm_del(msg, parts) { | |
var name = parts[2] | |
vm.list(function(vms){ | |
var winners = vms.filter(function(vm_name) { | |
var parts=vm_name.split('/') | |
return parts[0] == msg.params.nick | |
}) | |
var url = gist_shrink(vm.del(name)) | |
if(url) { | |
bot.say(msg.params.channel, "vm.del remove "+bot.owner+"/"+name+" url: "+url) | |
remove_from_applist(msg.params.nick, url) | |
} else { | |
bot.say(msg.params.channel, "vm.del failed for "+bot.owner+"/"+name) | |
} | |
}) | |
} | |
function do_vm_reload(msg, parts) { | |
var name = parts[2] | |
bot.say(msg.params.channel, "reloading "+name) | |
//var err = vm.reload(name) | |
var url = vm.del(name) | |
bot.say(msg.params.channel, "vm.del returned "+url) | |
var gist_url = gist_shrink(url) | |
bot.say(msg.params.channel, "vm add "+gist_url) | |
var gist = gist_expand(gist_url) | |
var url = gist ? gist['raw_url'] : gist_url | |
var report = gist ? "gist "+gist.filename+" "+gist.size+" bytes "+gist.updated_at : gist_url | |
bot.say(msg.params.channel, "Addding "+report) | |
var err = vm.add(url) | |
if (err) { | |
bot.say(msg.params.channel, err) | |
} | |
} | |
function gist_expand(url) { | |
var gist_parts = url.match(/\/\/gist.github.com\/.*\/(.*)$/) | |
if(gist_parts) { | |
var id = gist_parts[1] | |
var params = { url: 'https://api.github.com/gists/'+id } | |
if(github_key) { | |
params['headers'] = {"Authorization" : 'token '+github_key} | |
} | |
var gist_json = http.get(params).body | |
try { | |
var gist = JSON.parse(gist_json) | |
if(gist['message']) { | |
bot.say(bot.admin_channel, gist['message']) | |
} else { | |
var filename = Object.keys(gist['files'])[0] | |
var filegist = gist['files'][filename] | |
filegist.updated_at = gist.updated_at | |
return filegist | |
} | |
} catch(e) { | |
var snippet = gist_json ? JSON.stringify(gist_json).substr(0,20) : "-empty data-" | |
bot.say(bot.admin_channel, "vmctl gist_expand "+params.url+" failed "+e+" "+ snippet) | |
} | |
} | |
} | |
function gist_shrink(url) { | |
var gist_parts = url.match(/\/\/gist.githubusercontent.com\/(.*\/.*)\/raw/) | |
if(gist_parts) { | |
var gist_url = "https://gist.github.com/" | |
url = gist_url + gist_parts[1] | |
} | |
return url | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment