-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuildLogsController.js
More file actions
195 lines (184 loc) · 5.61 KB
/
buildLogsController.js
File metadata and controls
195 lines (184 loc) · 5.61 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict';
require('app').controller('BuildLogsController', BuildLogsController);
function BuildLogsController(
$scope,
$rootScope,
$timeout,
errs,
keypather,
launchDebugContainer,
loading,
updateInstanceWithNewBuild,
primus,
promisify,
fetchRepoDockerfile,
streamingLog
) {
var BLC = this;
BLC.showDebug = false;
BLC.headerContent = [];
function handleUpdate () {
var status = BLC.instance.status();
BLC.showErrorPanel = false;
if (status === 'buildFailed' || status === 'neverStarted') {
var buildError = BLC.instance.attrs.contextVersion.build.error || {};
BLC.buildStatus = 'failed';
BLC.failReason = buildError.message || 'failed';
BLC.showDebug = true;
BLC.buildLogsRunning = false;
if (status === 'neverStarted') {
BLC.showErrorPanel = true;
}
var repoAndBranchName = BLC.instance.getRepoAndBranchName().split('/');
var repoName = repoAndBranchName[0];
var branchName = repoAndBranchName[1];
var dockerfilePath = keypather.get(BLC, 'instance.mirroredDockerfile.attrs.path') + keypather.get(BLC, 'instance.mirroredDockerfile.attrs.name');
if (!!dockerfilePath) {
fetchRepoDockerfile(repoName, branchName, dockerfilePath)
.then(function (dockerfile) {
BLC.showNoDockerfileError = (BLC.instance.hasDockerfileMirroring() && dockerfile.message === 'Not Found');
});
}
} else if (status === 'building') {
BLC.buildStatus = 'running';
BLC.buildLogsRunning = true;
BLC.showDebug = false;
} else {
BLC.buildStatus = 'success';
BLC.buildLogsRunning = false;
}
}
var failCount = 0;
var streamDelay = 500;
function closeStream(stream) {
if (stream && stream.end) {
stream.end();
}
if (BLC.instance) {
BLC.instance.off('update', handleUpdate);
}
BLC.buildLogsRunning = false;
}
var unWatchDockerContainer = angular.noop;
function setupStream(oldStream) {
if (oldStream) {
closeStream(oldStream);
}
BLC.streamFailure = false;
var stream = null;
if (BLC.instance) {
BLC.buildStatus = 'starting';
BLC.buildLogs = [];
BLC.buildLogTiming = {};
unWatchDockerContainer();
unWatchDockerContainer = $scope.$watch(
'BLC.instance.attrs.contextVersion.build.dockerContainer',
function (cvContainerId, oldCvContainerId) {
if (cvContainerId && cvContainerId !== oldCvContainerId) {
// When the build changes
setupStream(stream);
} else if (cvContainerId) {
// This happens the first time
stream = primus.createBuildStream(BLC.instance.build);
// first time running
BLC.instance.off('update', handleUpdate);
BLC.instance.on('update', handleUpdate);
connectListenersToStream(stream);
handleUpdate();
}
}
);
} else if (BLC.debugContainer) {
stream = primus.createBuildStreamFromContextVersionId(BLC.debugContainer.attrs.contextVersion);
connectListenersToStream(stream);
}
}
function connectListenersToStream(stream) {
var streamingBuildLogs = null;
$scope.$on('$destroy', function () {
streamingBuildLogs.destroy();
closeStream(stream);
});
stream.on('data', function (data) {
if (data.type === 'log' && BLC.buildLogs.length === 0) {
BLC.headerContent.push(data.content);
}
stream.hasData = true;
});
stream.on('end', function () {
if (!stream.hasData) {
failCount += 1;
if (failCount > 10) {
BLC.headerContent = [ ];
BLC.streamFailure = true;
BLC.buildLogsRunning = false;
} else {
streamDelay = Math.floor(streamDelay * 1.3);
$timeout(function () {
setupStream(stream);
}, streamDelay);
}
} else {
BLC.buildLogsRunning = false;
}
$scope.$applyAsync();
});
stream.on('disconnection', function () {
setupStream();
$scope.$applyAsync();
});
streamingBuildLogs = streamingLog(stream);
BLC.buildLogs = streamingBuildLogs.logs;
BLC.buildLogTiming = streamingBuildLogs.times;
BLC.getRawLogs = streamingBuildLogs.getRawLogs;
}
BLC.getBuildLogs = function () {
if (BLC.instance) {
return BLC.buildLogs;
}
if (BLC.debugContainer) {
var newBuildLogs = [];
for (var i=0; i<BLC.buildLogs.length; i++) {
var command = BLC.buildLogs[i];
newBuildLogs.push(command);
if (command.imageId === BLC.debugContainer.attrs.layerId) {
return newBuildLogs;
}
}
return newBuildLogs;
}
};
setupStream();
this.generatingDebug = false;
this.actions = {
rebuildWithoutCache: function () {
loading('buildLogsController', true);
promisify(BLC.instance.build, 'deepCopy')()
.then(function (build) {
return updateInstanceWithNewBuild(
BLC.instance,
build,
true
);
})
.catch(errs.handler)
.finally(function () {
loading('buildLogsController', false);
});
},
launchDebugContainer: function (event, command) {
if (BLC.debugContainer) {
return;
}
$rootScope.$emit('close-popovers');
if (BLC.generatingDebug) {
return;
}
BLC.generatingDebug = true;
launchDebugContainer(BLC.instance.id(), BLC.instance.attrs.contextVersion._id, command.imageId, command.rawCommand)
.then(function () {
BLC.generatingDebug = false;
});
}
};
}