stack_and_upgrade_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. var stringUtils = require('utils/string_utils');
  20. App.MainAdminStackAndUpgradeController = Em.Controller.extend(App.LocalStorage, {
  21. name: 'mainAdminStackAndUpgradeController',
  22. /**
  23. * @type {Object|null}
  24. */
  25. serviceToInstall: null,
  26. /**
  27. * @type {object}
  28. * @default null
  29. */
  30. upgradeData: null,
  31. /**
  32. * @type {number}
  33. * @default null
  34. */
  35. upgradeId: null,
  36. /**
  37. * @type {string}
  38. * @default null
  39. */
  40. upgradeVersion: null,
  41. /**
  42. * version that currently applied to server
  43. * @type {Object|null}
  44. */
  45. currentVersion: null,
  46. /**
  47. * versions to which cluster could be upgraded
  48. * @type {Array}
  49. */
  50. targetVersions: [],
  51. /**
  52. * restore data from localStorage
  53. */
  54. init: function () {
  55. ['upgradeId', 'upgradeVersion'].forEach(function (property) {
  56. if (this.getDBProperty(property)) {
  57. this.set(property, this.getDBProperty(property));
  58. }
  59. }, this);
  60. },
  61. /**
  62. * @type {Array}
  63. */
  64. services: function() {
  65. return App.StackService.find().map(function(s) {
  66. s.set('isInstalled', App.Service.find().someProperty('serviceName', s.get('serviceName')));
  67. return s;
  68. });
  69. }.property('App.router.clusterController.isLoaded'),
  70. /**
  71. * launch Add Service wizard
  72. * @param event
  73. */
  74. goToAddService: function (event) {
  75. this.set('serviceToInstall', event.context);
  76. App.get('router').transitionTo('main.serviceAdd');
  77. },
  78. /**
  79. * call to fetch cluster stack versions
  80. * @return {$.ajax}
  81. */
  82. loadVersionsInfo: function () {
  83. return App.ajax.send({
  84. name: 'admin.stack_versions.all',
  85. sender: this,
  86. data: {},
  87. success: 'loadVersionsInfoSuccessCallback'
  88. });
  89. },
  90. /**
  91. * parse stack versions and
  92. * set <code>currentVersion</code>
  93. * set <code>targetVersions</code>
  94. * @param data
  95. */
  96. loadVersionsInfoSuccessCallback: function (data) {
  97. var versions = this.parseVersionsData(data);
  98. var current = versions.findProperty('state', 'CURRENT');
  99. var targetVersions = versions.without(current).filter(function (version) {
  100. //Only higher versions that have already been installed to all the hosts are shown
  101. return (version.state === 'INSTALLED' &&
  102. stringUtils.compareVersions(version.repository_version, current.repository_version) === 1);
  103. });
  104. this.set('currentVersion', current);
  105. this.set('targetVersions', targetVersions);
  106. },
  107. /**
  108. * parse ClusterStackVersions data to form common structure
  109. * @param {object} data
  110. * @return {Array}
  111. */
  112. parseVersionsData: function (data) {
  113. return data.items.map(function (item) {
  114. item.ClusterStackVersions.repository_name = item.repository_versions[0].RepositoryVersions.display_name;
  115. item.ClusterStackVersions.repository_id = item.repository_versions[0].RepositoryVersions.id;
  116. item.ClusterStackVersions.repository_version = item.repository_versions[0].RepositoryVersions.repository_version;
  117. return item.ClusterStackVersions;
  118. });
  119. },
  120. /**
  121. * load upgrade tasks by upgrade id
  122. * @return {$.Deferred}
  123. * @param {boolean} onlyState
  124. */
  125. loadUpgradeData: function (onlyState) {
  126. var upgradeId = this.get('upgradeId');
  127. var deferred = $.Deferred();
  128. if (Em.isNone(upgradeId)) {
  129. deferred.resolve();
  130. console.log('Upgrade in INIT state');
  131. } else {
  132. App.ajax.send({
  133. name: (onlyState) ? 'admin.upgrade.state' : 'admin.upgrade.data',
  134. sender: this,
  135. data: {
  136. id: upgradeId
  137. },
  138. success: 'loadUpgradeDataSuccessCallback'
  139. }).then(deferred.resolve);
  140. }
  141. return deferred.promise();
  142. },
  143. /**
  144. * parse and push upgrade tasks to controller
  145. * @param data
  146. */
  147. loadUpgradeDataSuccessCallback: function (data) {
  148. App.set('upgradeState', data.Upgrade.request_status);
  149. if (data.upgrade_groups) {
  150. this.updateUpgradeData(data);
  151. }
  152. },
  153. /**
  154. * update data of Upgrade
  155. * @param {object} newData
  156. */
  157. updateUpgradeData: function (newData) {
  158. var oldData = this.get('upgradeData'),
  159. groupsMap = {},
  160. itemsMap = {},
  161. tasksMap = {};
  162. if (Em.isNone(oldData)) {
  163. this.initUpgradeData(newData);
  164. } else {
  165. //create entities maps
  166. newData.upgrade_groups.forEach(function (newGroup) {
  167. groupsMap[newGroup.UpgradeGroup.group_id] = newGroup.UpgradeGroup;
  168. newGroup.upgrade_items.forEach(function (item) {
  169. itemsMap[item.UpgradeItem.stage_id] = item.UpgradeItem;
  170. item.tasks.forEach(function (task) {
  171. tasksMap[task.Tasks.id] = task.Tasks;
  172. });
  173. })
  174. });
  175. //update existed entities with new data
  176. oldData.upgradeGroups.forEach(function (oldGroup) {
  177. oldGroup.set('status', groupsMap[oldGroup.get('group_id')].status);
  178. oldGroup.set('progress_percent', groupsMap[oldGroup.get('group_id')].progress_percent);
  179. oldGroup.upgradeItems.forEach(function (item) {
  180. item.set('status', itemsMap[item.get('stage_id')].status);
  181. item.set('progress_percent', itemsMap[item.get('stage_id')].progress_percent);
  182. item.tasks.forEach(function (task) {
  183. task.set('status', tasksMap[task.get('id')].status);
  184. });
  185. })
  186. });
  187. }
  188. },
  189. /**
  190. * change structure of Upgrade
  191. * In order to maintain nested views in template object should have direct link to its properties, for example
  192. * item.UpgradeItem.<properties> -> item.<properties>
  193. * @param {object} newData
  194. */
  195. initUpgradeData: function (newData) {
  196. var upgradeGroups = [];
  197. //wrap all entities into App.upgradeEntity
  198. newData.upgrade_groups.forEach(function (newGroup) {
  199. var oldGroup = App.upgradeEntity.create(newGroup.UpgradeGroup);
  200. var upgradeItems = [];
  201. newGroup.upgrade_items.forEach(function (item) {
  202. var oldItem = App.upgradeEntity.create(item.UpgradeItem);
  203. var tasks = [];
  204. item.tasks.forEach(function (task) {
  205. tasks.pushObject(App.upgradeEntity.create(task.Tasks));
  206. });
  207. oldItem.set('tasks', tasks);
  208. upgradeItems.pushObject(oldItem);
  209. });
  210. oldGroup.set('upgradeItems', upgradeItems);
  211. upgradeGroups.pushObject(oldGroup);
  212. });
  213. this.set('upgradeData', Em.Object.create({
  214. upgradeGroups: upgradeGroups,
  215. Upgrade: newData.Upgrade
  216. }));
  217. },
  218. /**
  219. * make call to start downgrade process
  220. */
  221. downgrade: function () {
  222. //TODO start downgrade
  223. },
  224. /**
  225. * make call to start upgrade process and show popup with current progress
  226. * @param {object} version
  227. */
  228. upgrade: function (version) {
  229. App.ajax.send({
  230. name: 'admin.upgrade.start',
  231. sender: this,
  232. data: {
  233. version: version.value
  234. },
  235. success: 'upgradeSuccessCallback'
  236. });
  237. this.set('upgradeVersion', version.label);
  238. this.setDBProperty('upgradeVersion', version.label);
  239. },
  240. /**
  241. * success callback of <code>upgrade()</code>
  242. * @param {object} data
  243. */
  244. upgradeSuccessCallback: function (data) {
  245. this.set('upgradeId', data.resources[0].Upgrade.request_id);
  246. this.setDBProperty('upgradeId', data.resources[0].Upgrade.request_id);
  247. App.clusterStatus.setClusterStatus({
  248. clusterName: App.get('clusterName'),
  249. clusterState: 'DEFAULT',
  250. localdb: App.db.data
  251. });
  252. this.openUpgradeDialog();
  253. },
  254. /**
  255. * send request for pre upgrade check
  256. * @param version
  257. */
  258. runPreUpgradeCheck: function(version) {
  259. App.ajax.send({
  260. name: "admin.rolling_upgrade.pre_upgrade_check",
  261. sender: this,
  262. data: version,
  263. success: "runPreUpgradeCheckSuccess"
  264. })
  265. },
  266. /**
  267. * success callback of <code>runPreUpgradeCheckSuccess()</code>
  268. * if there are some fails - it shows popup else run upgrade
  269. * @param data {object}
  270. * @param opt {object}
  271. * @param params {object}
  272. * @returns {App.ModalPopup|void}
  273. */
  274. runPreUpgradeCheckSuccess: function(data, opt, params) {
  275. if (data.UpgradeChecks.someProperty('status',"FAIL")) {
  276. return App.ModalPopup.show({
  277. header: Em.I18n.t('admin.stackUpgrade.preupgradeCheck.header').format(params.label),
  278. primary: Em.I18n.t('common.dismiss'),
  279. secondary: false,
  280. bodyClass: Em.View.extend({
  281. templateName: require('templates/main/admin/stack_upgrade/pre_upgrade_check_dialog'),
  282. checks: function() {
  283. return data.UpgradeChecks.filterProperty('status',"FAIL");
  284. }.property()
  285. })
  286. })
  287. } else {
  288. this.upgrade(params);
  289. }
  290. },
  291. /**
  292. * make call to resume upgrade process and show popup with current progress
  293. */
  294. resumeUpgrade: function () {
  295. //TODO resume upgrade
  296. },
  297. /**
  298. * make call to stop upgrade process
  299. */
  300. stopUpgrade: function () {
  301. //TODO stop upgrade
  302. },
  303. /**
  304. * make call to finish upgrade process
  305. */
  306. finalize: function () {
  307. //TODO execute finalize
  308. this.finish();
  309. },
  310. /**
  311. * finish upgrade wizard
  312. * clean auxiliary data
  313. */
  314. finish: function () {
  315. this.set('upgradeId', null);
  316. this.setDBProperty('upgradeId', undefined);
  317. App.set('upgradeState', 'INIT');
  318. this.set('upgradeVersion', null);
  319. this.setDBProperty('upgradeVersion', undefined);
  320. App.clusterStatus.setClusterStatus({
  321. clusterName: App.get('clusterName'),
  322. clusterState: 'DEFAULT',
  323. localdb: App.db.data
  324. });
  325. },
  326. /**
  327. * show dialog with tasks of upgrade
  328. * @return {App.ModalPopup}
  329. */
  330. openUpgradeDialog: function () {
  331. App.router.transitionTo('admin.stackUpgrade');
  332. }
  333. });
  334. /**
  335. * @type {Ember.Object}
  336. * @class
  337. */
  338. App.upgradeEntity = Em.Object.extend({
  339. /**
  340. * @type {boolean}
  341. */
  342. errorLogOpened: false,
  343. /**
  344. * @type {boolean}
  345. */
  346. outputLogOpened: false,
  347. /**
  348. * @type {boolean}
  349. */
  350. isExpanded: false,
  351. /**
  352. * @type {boolean}
  353. */
  354. isRunning: function () {
  355. return ['IN_PROGRESS'].contains(this.get('status'));
  356. }.property('status'),
  357. /**
  358. * width style of progress bar
  359. * @type {string}
  360. */
  361. progressWidth: function () {
  362. return "width:" + Math.floor(this.get('progress')) + '%;';
  363. }.property('progress'),
  364. /**
  365. * @type {number}
  366. */
  367. progress: function () {
  368. return Math.floor(this.get('progress_percent'));
  369. }.property('progress_percent'),
  370. /**
  371. * indicate whether entity has active link
  372. * @type {boolean}
  373. */
  374. isActive: function () {
  375. return this.get('status') !== 'PENDING';
  376. }.property('status')
  377. });