This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
layers.js
131 lines (117 loc) · 4.68 KB
/
layers.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
/*
* This file is part of Cockpit.
*
* Copyright (C) 2016 Red Hat, Inc.
*
* Cockpit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Cockpit is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
*/
/* globals cockpit */
(function() {
"use strict";
function v1CompatibilityLabel(layer, lower) {
var cmd, last;
if (layer.v1Compatibility.container_config) {
cmd = layer.v1Compatibility.container_config.Cmd;
if (cmd) {
last = cmd[cmd.length - 1];
if (last.indexOf("#(nop)") === 0)
return last.substring(6).trim();
else if (cmd.length == 1 && cmd[0].indexOf("/bin/sh -c #(nop)") === 0)
return cmd[0].substring(17).trim();
else
return cmd.join(" ");
}
}
return layer.v1Compatibility.id;
}
angular.module('registryUI.images')
.factory('prepareLayer', [
'gettextCatalog',
function(gettextCatalog) {
return function prepareLayer(layer, index, layers) {
var result;
/* DockerImageManifest */
if (layer.v1Compatibility) {
result = {
id: layer.v1Compatibility.id,
size: layer.v1Compatibility.Size || 0,
label: v1CompatibilityLabel(layer, layers[index + 1])
};
/* DockerImageLayers */
} else if (layer.name && layer.size) {
result = {
id: layer.name,
size: layer.size || 0,
label: layer.name,
};
/* Unsupported layer type */
} else {
result = {
size: 0,
id: index,
label: gettextCatalog.getString("Unknown layer"),
};
}
/* Some hints for coloring the display */
if (result.label.indexOf("RUN ") === 0)
result.hint = "run";
else if (result.label.indexOf("ADD ") === 0 || result.size > 8192)
result.hint = "add";
else
result.hint = "other";
return result;
};
}
])
.directive('registryImageLayers', [
'imageLayers',
'prepareLayer',
'gettextCatalog',
function(imageLayers, prepareLayer, gettextCatalog) {
return {
restrict: 'E',
scope: {
image: '=',
data: '=?layers',
},
templateUrl: 'registry-image-widgets/views/image-layers.html',
link: function($scope, element, attributes) {
$scope.formatSize = function(bytes) {
var n;
if (!bytes) {
return "";
} else if (bytes > 1024 && typeof cockpit != "undefined") {
return cockpit.format_bytes(bytes);
} else if (bytes > 1024 * 1024) {
n = (bytes / (1024 * 1024)).toFixed(1);
return gettextCatalog.getPlural(n, $scope, "{0} MB", "{0} MB").replace("{0}", n);
} else {
return gettextCatalog.getPlural(n, $scope, "{0} byte", "{0} bytes").replace("{0}", bytes);
}
};
$scope.$watch('data', function(layers) {
if (layers && layers.length)
layers = layers.map(prepareLayer).reverse();
$scope.layers = layers;
});
$scope.$watch('image', function(image) {
/* Only digest layers if image is set, or null */
if (!angular.isUndefined(image))
$scope.data = imageLayers(image);
});
}
};
}
]);
}());