/usr/share/maas/web/static/js/angular/factories/general.js is in maas-region-api 2.4.0~beta2-6865-gec43e47e6-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | /* Copyright 2015-2018 Canonical Ltd. This software is licensed under the
* GNU Affero General Public License version 3 (see the file LICENSE).
*
* MAAS General Manager
*
* Manager for general information from the region. The general handler on the
* region side does not push information to the client. This manager uses
* polling to grab this data periodically from the region.
*
* This manage provides different pieces of data and is structure differently
* than extending the Manager service. It still provides the Manager service
* interface allowing the ManagerHelperService to load this manager.
*/
angular.module('MAAS').factory(
'GeneralManager',
['$q', '$timeout', 'RegionConnection', 'ErrorService',
function($q, $timeout, RegionConnection, ErrorService) {
// Constructor
function GeneralManager() {
// Holds the available endpoints and its data.
this._data = {
machine_actions: {
method: "general.machine_actions",
data: [],
requested: false,
loaded: false,
polling: [],
nextPromise: null
},
device_actions: {
method: "general.device_actions",
data: [],
request: false,
loaded: false,
polling: [],
nextPromise: null
},
region_controller_actions: {
method: "general.region_controller_actions",
data: [],
requested: false,
loaded: false,
polling: [],
nextPromise: null
},
rack_controller_actions: {
method: "general.rack_controller_actions",
data: [],
requested: false,
loaded: false,
polling: [],
nextPromise: null
},
region_and_rack_controller_actions: {
method: "general.region_and_rack_controller_actions",
data: [],
requested: false,
loaded: false,
polling: [],
nextPromise: null
},
architectures: {
method: "general.architectures",
data: [],
requested: false,
loaded: false,
polling: [],
nextPromise: null
},
known_architectures: {
method: "general.known_architectures",
data: [],
requested: false,
loaded: false,
polling: [],
nextPromise: null
},
pockets_to_disable: {
method: "general.pockets_to_disable",
data: [],
requested: false,
loaded: false,
polling: [],
nextPromise: null
},
components_to_disable: {
method: "general.components_to_disable",
data: [],
requested: false,
loaded: false,
polling: [],
nextPromise: null
},
hwe_kernels: {
method: "general.hwe_kernels",
data: [],
requested: false,
loaded: false,
polling: [],
nextPromise: null
},
min_hwe_kernels: {
method: "general.min_hwe_kernels",
data: [],
requested: false,
loaded: false,
polling: [],
nextPromise: null
},
default_min_hwe_kernel: {
method: "general.default_min_hwe_kernel",
data: { text: '' },
requested: false,
loaded: false,
polling: [],
nextPromise: null,
replaceData: function(oldData, newData) {
oldData.text = newData;
}
},
osinfo: {
method: "general.osinfo",
data: {},
requested: false,
loaded: false,
polling: [],
nextPromise: null,
isEmpty: function(data) {
var osystems = data.osystems;
return (angular.isUndefined(osystems) ||
osystems.length === 0);
},
replaceData: function(oldData, newData) {
angular.copy(newData, oldData);
}
},
bond_options: {
method: "general.bond_options",
data: {},
requested: false,
loaded: false,
polling: [],
nextPromise: null,
replaceData: function(oldData, newData) {
angular.copy(newData, oldData);
}
},
version: {
method: "general.version",
data: { text: null },
requested: false,
loaded: false,
polling: [],
nextPromise: null,
replaceData: function(oldData, newData) {
oldData.text = newData;
}
},
power_types: {
method: "general.power_types",
data: [],
requested: false,
loaded: false,
polling: [],
nextPromise: null,
replaceData: function(oldData, newData) {
// Add new power types.
var i, j, newPowerType, oldPowerType;
for(i = 0; i < newData.length; i++) {
newPowerType = newData[i];
var newItem = true;
for(j = 0; j < oldData.length; j++) {
oldPowerType = oldData[j];
if(newPowerType.name === oldPowerType.name) {
newItem = false;
break;
}
}
// Item was previously not in the list so it is
// inserted into the array.
if(newItem) {
oldData.push(newPowerType);
}
}
// Remove any power types that are not included in
// the newData.
for(i = oldData.length - 1; i >= 0; i--) {
oldPowerType = oldData[i];
var found = false;
for(j = 0; j < newData.length; j++) {
newPowerType = newData[j];
if(newPowerType.name === oldPowerType.name) {
found = true;
break;
}
}
// Item was previously not in the list so it is
// inserted into the array.
if(!found) {
oldData.splice(i, 1);
}
}
}
},
release_options: {
method: "general.release_options",
data: {},
requested: false,
loaded: false,
polling: [],
nextPromise: null,
replaceData: function(oldData, newData) {
angular.copy(newData, oldData);
}
}
};
// Amount of time in milliseconds the manager should wait to poll
// for new data.
this._pollTimeout = 10000;
// Amount of time in milliseconds the manager should wait to poll
// for new data when an error occurs.
this._pollErrorTimeout = 3000;
// Amount of time in milliseconds the manager should wait to poll
// for new data when the retrieved data is empty.
this._pollEmptyTimeout = 3000;
// Set to true when the items list should reload upon re-connection
// to the region.
this._autoReload = false;
// Use the same notify type as a default manager.
this._type = 'notify';
// The scopes that have loaded this manager.
this._scopes = [];
}
GeneralManager.prototype._getInternalData = function(name) {
var data = this._data[name];
if(angular.isUndefined(data)) {
throw new Error("Unknown data: " + name);
}
return data;
};
// Return loaded data.
GeneralManager.prototype.getData = function(name) {
var d = this._getInternalData(name);
d.requested = true;
return d.data;
};
// Return true when all data has been loaded.
GeneralManager.prototype.isLoaded = function() {
var loaded = true;
angular.forEach(this._data, function(data) {
if(!data.loaded) {
loaded = false;
}
});
return loaded;
};
// Return true when data has been loaded.
GeneralManager.prototype.isDataLoaded = function(name) {
return this._getInternalData(name).loaded;
};
// Returns true when the manager is currently polling.
GeneralManager.prototype.isPolling = function() {
var polling = false;
angular.forEach(this._data, function(data) {
if(data.polling.length > 0) {
polling = true;
}
});
return polling;
};
// Returns true when the manager is currently polling for that data.
GeneralManager.prototype.isDataPolling = function(name) {
return this._getInternalData(name).polling;
};
// Starts the manager polling for data.
GeneralManager.prototype.startPolling = function(scope, name) {
var data = this._getInternalData(name);
var idx = data.polling.indexOf(scope);
if(idx === -1) {
data.polling.push(scope);
if(data.polling.length === 1) {
// Polling needs to be started. This is the first scope
// that is requesting for polling to be performed.
this._poll(data);
}
}
};
// Stops the manager polling for data.
GeneralManager.prototype.stopPolling = function(scope, name) {
var data = this._getInternalData(name);
var idx = data.polling.indexOf(scope);
if(idx >= 0) {
data.polling.splice(idx, 1);
}
if(data.polling.length === 0 &&
angular.isObject(data.nextPromise)) {
$timeout.cancel(data.nextPromise);
data.nextPromise = null;
}
};
// Load the data from the region.
GeneralManager.prototype._loadData = function(data, raiseError) {
var replaceData = data.replaceData;
raiseError = raiseError || false;
// Set default replaceData function if data doesn't provide its
// own function.
if(angular.isUndefined(replaceData)) {
replaceData = function(oldData, newData) {
oldData.length = 0;
oldData.push.apply(oldData, newData);
};
}
return RegionConnection.callMethod(data.method).then(
function(newData) {
replaceData(data.data, newData);
data.loaded = true;
return data.data;
}, function(error) {
if(raiseError) {
ErrorService.raiseError(error);
}
return error;
});
};
GeneralManager.prototype._pollAgain = function(data, timeout) {
var self = this;
data.nextPromise = $timeout(function() {
self._poll(data);
}, timeout);
};
// Polls for the data from the region.
GeneralManager.prototype._poll = function(data) {
var self = this;
var isEmpty = data.isEmpty;
// Set default isEmpty function if data doesn't provide its
// own function.
if(angular.isUndefined(isEmpty)) {
isEmpty = function(data) {
return data.length === 0;
};
}
// Can only poll if connected.
if(!RegionConnection.isConnected()) {
this._pollAgain(data, this._pollErrorTimeout);
return;
}
return this._loadData(data, false).then(function(newData) {
var pollTimeout = self._pollTimeout;
if(isEmpty(data.data)) {
pollTimeout = self._pollEmptyTimeout;
}
self._pollAgain(data, pollTimeout);
return newData;
}, function(error) {
// Don't raise the error, just log it and try again.
console.log(error);
self._pollAgain(data, self._pollErrorTimeout);
});
};
// Loads all the items. This implemented so the ManagerHelperService
// can work on this manager just like all the rest. Optionally pass a
// list of specific items to load. Useful when reloading data.
GeneralManager.prototype.loadItems = function(items) {
var self = this;
var defer = $q.defer();
var waitingCount = 0;
if(angular.isArray(items)) {
waitingCount = items.length;
}else{
angular.forEach(this._data, function(data) {
if(data.requested) {
waitingCount++;
}
});
}
var done = function() {
waitingCount -= 1;
if(waitingCount === 0) {
defer.resolve();
}
};
angular.forEach(this._data, function(data, name) {
if((angular.isArray(items) && items.indexOf(name) !== -1) ||
data.requested === true) {
self._loadData(data, true).then(function() {
done();
});
}
});
return defer.promise;
};
// Enables auto reloading of the item list on connection to region.
GeneralManager.prototype.enableAutoReload = function() {
if(!this._autoReload) {
this._autoReload = true;
var self = this;
this._reloadFunc = function() {
self.loadItems();
};
RegionConnection.registerHandler("open", this._reloadFunc);
}
};
// Disable auto reloading of the item list on connection to region.
GeneralManager.prototype.disableAutoReload = function() {
if(this._autoReload) {
RegionConnection.unregisterHandler("open", this._reloadFunc);
this._reloadFunc = null;
this._autoReload = false;
}
};
return new GeneralManager();
}]);
|