JOVVHIJXYJN3RNRXZHPMHYHMIA3KRCL4UCJ5RM6S6ZOZY66LKKHQC
LSZLZHJYGXZTCNH4JUXU7W23MW5PBVM4OBMWRRVNEDROMIBUVQNAC
SMCOU72FKTPFNCDXFJAILVUWFE4DY33CJJE4436H5POKENFFDFFAC
ZWCTAZGLJZQNTYWTC2XQUKMILJF6JGDL5IND6QNYWK4FIGMLRFXAC
Q5WS4WKQLOKTAFV34LFBHQQ6WMWNEMYLEDUUJNPMCPXRTRNOKGRQC
6KJXJB7NZ5XJ3WGUWKFNT6LMBNYJN3IPHZ7B7H654D5B6LPOL5QAC
CQTN62OHT4DY35E2MJEG7GFTVNEE5KRDMV6ASBQLBHN7BUDK7WHAC
JARRBLZDQ2JZWY7IUVPTOT7WJMBPMLFLF2MGLVGOYROAAISYGLSAC
SGNXIOI4SO56F4YCGVMGZZO3AYXGPDL4FL75RDXAK5Y22IYWPPAQC
QT4FO2HPUPOS72Y5UTILN5AOV7S47AGG7V32CT24NA7TOVP76NAQC
3PNG7NIBQQURUUPRVQXYL342OT7JUUYOMY2JJNP6YDX7SYJDZMYAC
D5ZR3ZY63MRLQAIFYFRCZGDAIEQKHLR4MG46GVMZEVHKLXQIC5SAC
PP4LPADGGHINHEWAGRLKABDCXM7TK5Z5RWIHG7FVJYAALMHRY5JQC
67NFGL7DFEKIS7D7HC72XJ5CET6SE6AOE5F37XXFTBU6GZAZ64HQC
D5QIOJGPKQJIYBUCSC3MFJ3TXLPNZ2XMI37GXMFRVRFWWR2VMTFAC
SMTMFEKEB3YQU3X7NUA5FZ7KEW2BVTLNV2SF3SJFZ5XLU57QGYFAC
PZL3SZM3U3BYJX2RGYXC6NMBG7WQHFWHSYDYXZ7Q5VZA3EDYVPIQC
sub nixMachines {
my ($c) = @_;
my $result = "# GENERATED BY HYDRA\n";
foreach my $machine ($c->model("DB::BuildMachines")->all) {
if($machine->enabled) {
$result = $result . $machine->username . '@'. $machine->hostname . ' ';
foreach my $system ($machine->buildmachinesystemtypes) {
$result = $result . $system->system .',';
}
chop $result;
$result = $result . ' '. $machine->ssh_key . ' ' . $machine->maxconcurrent . ' '. $machine->speedfactor . ' ' . $machine->options . "\n";
}
}
return $result;
}
sub saveNixMachines {
my ($c) = @_;
die("File not writable: /etc/nix.machines") if ! -w "/etc/nix.machines" ;
open (NIXMACHINES, '>/etc/nix.machines') or die("Could not write to /etc/nix.machines");
print NIXMACHINES nixMachines($c);
close (NIXMACHINES);
}
}
sub machine : Chained('admin') PathPart('machine') CaptureArgs(1) {
my ($self, $c, $machineName) = @_;
requireAdmin($c);
my $machine = $c->model('DB::BuildMachines')->find($machineName)
or notFound($c, "Machine $machineName doesn't exist.");
$c->stash->{machine} = $machine;
sub machine_edit : Chained('machine') PathPart('edit') Args(0) {
my ($self, $c) = @_;
$c->stash->{template} = 'machine.tt';
$c->stash->{systemtypes} = [$c->model('DB::SystemTypes')->search({}, {order_by => "system"})];
$c->stash->{edit} = 1;
}
sub machine_edit_submit : Chained('machine') PathPart('submit') Args(0) {
my ($self, $c) = @_;
requirePost($c);
txn_do($c->model('DB')->schema, sub {
if (($c->request->params->{submit} || "") eq "delete") {
$c->stash->{machine}->delete;
} else {
updateMachine($c, $c->stash->{machine});
}
});
saveNixMachines($c);
$c->res->redirect("/admin/machines");
}
sub updateMachine {
my ($c, $machine) = @_;
my $hostname = trim $c->request->params->{"hostname"};
my $username = trim $c->request->params->{"username"};
my $maxconcurrent = trim $c->request->params->{"maxconcurrent"};
my $speedfactor = trim $c->request->params->{"speedfactor"};
my $ssh_key = trim $c->request->params->{"ssh_key"};
my $options = trim $c->request->params->{"options"};
my $systems = $c->request->params->{"systems"} ;
error($c, "Invalid or empty username.") if $username eq "";
error($c, "Max concurrent builds should be an integer > 0.") if $maxconcurrent eq "" || ! $maxconcurrent =~ m/[0-9]+/;
error($c, "Speed factor should be an integer > 0.") if $speedfactor eq "" || ! $speedfactor =~ m/[0-9]+/;
error($c, "Invalid or empty SSH key.") if $ssh_key eq "";
$machine->update(
{ username => $username
, maxconcurrent => $maxconcurrent
, speedfactor => $speedfactor
, ssh_key => $ssh_key
, options => $options
});
$machine->buildmachinesystemtypes->delete_all;
if(ref($systems) eq 'ARRAY') {
for my $s (@$systems) {
$machine->buildmachinesystemtypes->create({ system => $s}) ;
}
} else {
$machine->buildmachinesystemtypes->create({ system => $systems}) ;
}
}
sub create_machine : Chained('admin') PathPart('create-machine') Args(0) {
my ($self, $c) = @_;
requireAdmin($c);
$c->stash->{template} = 'machine.tt';
$c->stash->{systemtypes} = [$c->model('DB::SystemTypes')->search({}, {order_by => "system"})];
$c->stash->{edit} = 1;
$c->stash->{create} = 1;
}
sub create_machine_submit : Chained('admin') PathPart('create-machine/submit') Args(0) {
my ($self, $c) = @_;
requireAdmin($c);
my $hostname = trim $c->request->params->{"hostname"};
error($c, "Invalid or empty hostname.") if $hostname eq "";
txn_do($c->model('DB')->schema, sub {
my $machine = $c->model('DB::BuildMachines')->create(
{ hostname => $hostname });
updateMachine($c, $machine);
});
saveNixMachines($c);
$c->res->redirect("/admin/machines");
}
sub machine_enable : Chained('machine') PathPart('enable') Args(0) {
my ($self, $c) = @_;
$c->stash->{machine}->update({ enabled => 1});
saveNixMachines($c);
$c->res->redirect("/admin/machines");
}
sub machine_disable : Chained('machine') PathPart('disable') Args(0) {
my ($self, $c) = @_;
$c->stash->{machine}->update({ enabled => 0});
saveNixMachines($c);
$c->res->redirect("/admin/machines");
}
[% PROCESS common.tt %]
<form class="form-horizontal" method="post"
action="[% IF create %][% c.uri_for('/admin/create-machine/submit') %][% ELSE %][% c.uri_for('/admin/machine' machine.hostname 'submit') %][% END %]">
<fieldset>
[% IF create %]
<div class="control-group">
<label class="control-label">Host name</label>
<div class="controls">
<input type="text" class="span3" name="hostname" value="[% machine.hostname %]"></input>
</div>
</div>
[% END %]
<div class="control-group">
<label class="control-label">User name</label>
<div class="controls">
<input type="text" class="span3" name="username" value="[% machine.username %]"></input>
</div>
</div>
<div class="control-group">
<label class="control-label">SSH key location</label>
<div class="controls">
<input type="text" class="span3" name="ssh_key" value="[% machine.ssh_key %]"></input>
</div>
</div>
<div class="control-group">
<label class="control-label">Options</label>
<div class="controls">
<input type="text" class="span3" name="options" value="[% machine.options %]"></input>
</div>
</div>
<div class="control-group">
<label class="control-label">Max concurrent builds</label>
<div class="controls">
<input type="text" class="span3" name="maxconcurrent" value="[% machine.maxconcurrent %]"></input>
</div>
</div>
<div class="control-group">
<label class="control-label">Speed factor</label>
<div class="controls">
<input type="text" class="span3" name="speedfactor" value="[% machine.speedfactor %]"></input>
</div>
</div>
<div class="control-group">
<label class="control-label">Systems</label>
<div class="controls">
<select class="span3" multiple="1" name="systems">
[% FOREACH s IN systemtypes;
checked = false;
FOREACH ms IN machine.buildmachinesystemtypes;
checked = ms.system == s.system;
BREAK IF checked;
END %]
<option value="[% s.system %]" [% IF checked %]selected="1"[% END %]>[% s.system %]</option>
[% END %]
</select>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">
<i class="icon-ok icon-white"></i>
[%IF create %]Create[% ELSE %]Apply changes[% END %]
</button>
[% IF !create %]
<button id="delete-machine" type="submit" class="btn btn-danger" name="submit" value="delete">
<i class="icon-trash icon-white"></i>
Delete this machine
</button>
<script type="text/javascript">
$("#delete-machine").click(function() {
return confirm("Are you sure you want to delete this machine?");
});
</script>
[% END %]
</div>
</fieldset>
[% END %]
[% WRAPPER layout.tt title=(create ? "New machine" : "Machine $machine.hostname") %]