-
Notifications
You must be signed in to change notification settings - Fork 902
/
titusRunJobStage.js
153 lines (126 loc) · 4.44 KB
/
titusRunJobStage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';
let angular = require('angular');
import {Subject} from 'rxjs/Subject';
module.exports = angular.module('spinnaker.core.pipeline.stage.titus.runJobStage', [
require('./runJobExecutionDetails.controller.js')
])
.config(function (pipelineConfigProvider) {
pipelineConfigProvider.registerStage({
provides: 'runJob',
useBaseProvider: true,
cloudProvider: 'titus',
providesFor: ['aws', 'titus'],
templateUrl: require('./runJobStage.html'),
executionDetailsUrl: require('./runJobExecutionDetails.html'),
validators: [
{type: 'requiredField', fieldName: 'cluster.imageId'},
{type: 'requiredField', fieldName: 'credentials'},
{type: 'requiredField', fieldName: 'cluster.region'},
{type: 'requiredField', fieldName: 'cluster.resources.cpu'},
{type: 'requiredField', fieldName: 'cluster.resources.memory'},
{type: 'requiredField', fieldName: 'cluster.resources.disk'},
{type: 'requiredField', fieldName: 'cluster.runtimeLimitSecs'}
]
});
}).controller('titusRunJobStageCtrl', function ($scope, accountService, $q) {
let stage = $scope.stage;
let vm = this;
if (!stage.cluster) {
stage.cluster = {};
}
$scope.stage.waitForCompletion = ($scope.stage.waitForCompletion === undefined ? true : $scope.stage.waitForCompletion);
this.loaded = false;
this.removedGroups = [];
this.groupsRemovedStream = new Subject();
this.accountChangedStream = new Subject();
this.regionChangedStream = new Subject();
this.accountChanged = () => {
this.accountChangedStream.next(null);
setRegistry();
this.updateRegions();
};
this.regionChanged = () => {
this.regionChangedStream.next(null);
};
this.updateRegions = () => {
if (stage.credentials) {
$scope.regions = $scope.backingData.credentialsKeyedByAccount[stage.credentials].regions;
if ($scope.regions.map(r => r.name).every(r => r !== stage.cluster.region)) {
this.regionChanged();
delete stage.cluster.region;
}
} else {
$scope.regions = null;
}
};
this.onChange = (changes) => {
stage.registry = changes.registry;
};
function setRegistry() {
if (stage.credentials) {
stage.registry = $scope.backingData.credentialsKeyedByAccount[stage.credentials].registry;
}
}
function updateImageId() {
if ($scope.stage.repository && $scope.stage.tag) {
$scope.stage.cluster.imageId = `${$scope.stage.repository}:${$scope.stage.tag}`;
}
else {
delete $scope.stage.cluster.imageId;
}
}
if ($scope.stage.cluster.imageId) {
const image = $scope.stage.cluster.imageId;
$scope.stage.organization = '';
const parts = image.split('/');
if (parts.length > 1) {
$scope.stage.organization = parts.shift();
}
const rest = parts.shift().split(':');
if ($scope.stage.organization) {
$scope.stage.repository = `${$scope.stage.organization}/${rest.shift()}`;
} else {
$scope.stage.repository = rest.shift();
}
$scope.stage.tag = rest.shift();
}
$scope.$watchGroup(['stage.repository', 'stage.tag'], updateImageId);
if (!stage.credentials && $scope.application.defaultCredentials.titus) {
stage.credentials = $scope.application.defaultCredentials.titus;
}
if (!stage.cluster.env) {
stage.cluster.env = {};
}
if (!stage.cluster.application) {
stage.cluster.application = $scope.application.name;
}
if (!stage.cloudProvider) {
stage.cloudProvider = 'titus';
}
if (!stage.cluster.securityGroups) {
stage.cluster.securityGroups = [];
}
if (!stage.cluster.capacity) {
stage.cluster.capacity = {
min: 1,
max: 1,
desired: 1
};
}
stage.cluster.runtimeLimitSecs = stage.cluster.runtimeLimitSecs || 3600;
stage.deferredInitialization = true;
$q.all({
credentialsKeyedByAccount: accountService.getCredentialsKeyedByAccount('titus'),
}).then((backingData) => {
backingData.credentials = Object.keys(backingData.credentialsKeyedByAccount);
$scope.backingData = backingData;
if (!stage.credentials) {
stage.credentials = backingData.credentials[0];
}
setRegistry();
return $q.all([]).then(() => {
vm.updateRegions();
this.loaded = true;
});
});
});