Created
May 13, 2015 06:26
-
-
Save Damovisa/45b83066bcd5d651863b to your computer and use it in GitHub Desktop.
Javascript for the Octopus Environments page to enable/disable all machines in an environment
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
var OctoTool = { | |
GetEnvironmentMap: function(machineStatus) { | |
var machinesByEnv = angular.element($('.octo-group-body.machines')).scope().environments.map(function(el, i) { | |
var em = angular.element($('.octo-group-body.machines')).scope().machines[el.Id]; | |
return {EnvironmentId: el.Id, OnlineMachines: em ? em[machineStatus] : null} ; | |
}) | |
var values = {}; | |
for (var i=0; i<machinesByEnv.length; i++) { | |
values[machinesByEnv[i].EnvironmentId] = machinesByEnv[i].OnlineMachines; | |
} | |
return values; | |
}, | |
SetMachinesInEnvironment: function(envId, setDisabled) { | |
var envmachines = this.GetEnvironmentMap(setDisabled ? 'Online' : 'Disabled'); | |
if (envId && envmachines[envId]) { | |
var octoUrl = window.location.href.substring(0,window.location.href.indexOf('/app#')) + '/api/machines/'; | |
console.log('Ok, ' + (setDisabled ? 'disabling' : 'enabling') + ' : ' + envmachines[envId].map(function(e){return e.Id;}).join(", ")); | |
for (var i=0; i<envmachines[envId].length; i++) { | |
var machine = envmachines[envId][i]; | |
var url = octoUrl + machine.Id; | |
machine.IsDisabled = setDisabled; | |
$.ajax({ | |
url: url, | |
type: 'put', | |
data: JSON.stringify(machine), | |
dataType: 'json', | |
contentType: 'application/json', | |
success: function(data) { | |
console.log(data.Name + ': ' + data.StatusSummary); | |
} | |
}); | |
} | |
return 'Made requests. Refresh when complete.'; | |
} else { | |
return 'No suitable machines for this environment'; | |
} | |
}, | |
DisableMachinesInEnvironment: function(envId) { | |
return this.SetMachinesInEnvironment(envId, true); | |
}, | |
EnableMachinesInEnvironment: function(envId) { | |
return this.SetMachinesInEnvironment(envId, false); | |
} | |
}; | |
if (window.location.href.endsWith('/app#/environments')) { | |
$('.octo-group-header .tools a[octo-permission=EnvironmentEdit]').each(function(i, a) { | |
var envId = a.href.substring(a.href.lastIndexOf('/')+1); | |
var dis = $('<button onclick="console.log(OctoTool.DisableMachinesInEnvironment(\''+envId+'\'))" class="btn btn-link"></a>').text('Disable All'); | |
var en = $('<button onclick="console.log(OctoTool.EnableMachinesInEnvironment(\''+envId+'\'))" class="btn btn-link"></a>').text('Enable All'); | |
$(a).before(dis); | |
$(a).before(en); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: This is really brittle, and a fairly hacky way of solving the problem.
But as they say: if it's stupid and works, it ain't stupid :)