587f867aa6feba078e9bb98b785cd1953c85acd7
[project/luci.git] / modules / luci-mod-system / htdocs / luci-static / resources / view / system / startup.js
1 'use strict';
2 'require rpc';
3 'require fs';
4 'require ui';
5
6 return L.view.extend({
7 callInitList: rpc.declare({
8 object: 'luci',
9 method: 'getInitList',
10 expect: { '': {} }
11 }),
12
13 callInitAction: rpc.declare({
14 object: 'luci',
15 method: 'setInitAction',
16 params: [ 'name', 'action' ],
17 expect: { result: false }
18 }),
19
20 load: function() {
21 return Promise.all([
22 L.resolveDefault(fs.read('/etc/rc.local'), ''),
23 this.callInitList()
24 ]);
25 },
26
27 handleAction: function(name, action, ev) {
28 return this.callInitAction(name, action).then(function(success) {
29 if (success != true)
30 throw _('Command failed');
31
32 return true;
33 }).catch(function(e) {
34 ui.addNotification(null, E('p', _('Failed to execute "/etc/init.d/%s %s" action: %s').format(name, action, e)));
35 });
36 },
37
38 handleEnableDisable: function(name, isEnabled, ev) {
39 return this.handleAction(name, isEnabled ? 'disable' : 'enable', ev).then(L.bind(function(name, isEnabled, btn) {
40 btn.parentNode.replaceChild(this.renderEnableDisable({
41 name: name,
42 enabled: isEnabled
43 }), btn);
44 }, this, name, !isEnabled, ev.currentTarget));
45 },
46
47 handleRcLocalSave: function(ev) {
48 var value = (document.querySelector('textarea').value || '').trim().replace(/\r\n/g, '\n') + '\n';
49
50 return fs.write('/etc/rc.local', value).then(function() {
51 document.querySelector('textarea').value = value;
52 ui.addNotification(null, E('p', _('Contents have been saved.')), 'info');
53 }).catch(function(e) {
54 ui.addNotification(null, E('p', _('Unable to save contents: %s').format(e.message)));
55 });
56 },
57
58 renderEnableDisable: function(init) {
59 return E('button', {
60 class: 'btn cbi-button-%s'.format(init.enabled ? 'positive' : 'negative'),
61 click: ui.createHandlerFn(this, 'handleEnableDisable', init.name, init.enabled)
62 }, init.enabled ? _('Enabled') : _('Disabled'));
63 },
64
65 render: function(data) {
66 var rcLocal = data[0],
67 initList = data[1],
68 rows = [], list = [];
69
70 var table = E('div', { 'class': 'table' }, [
71 E('div', { 'class': 'tr table-titles' }, [
72 E('div', { 'class': 'th' }, _('Start priority')),
73 E('div', { 'class': 'th' }, _('Initscript')),
74 E('div', { 'class': 'th nowrap cbi-section-actions' })
75 ])
76 ]);
77
78 for (var init in initList)
79 if (initList[init].index < 100)
80 list.push(Object.assign({ name: init }, initList[init]));
81
82 list.sort(function(a, b) {
83 if (a.index != b.index)
84 return a.index - b.index
85
86 return a.name > b.name;
87 });
88
89 for (var i = 0; i < list.length; i++) {
90 rows.push([
91 '%02d'.format(list[i].index),
92 list[i].name,
93 E('div', [
94 this.renderEnableDisable(list[i]),
95 E('button', { 'class': 'btn cbi-button-action', 'click': ui.createHandlerFn(this, 'handleAction', list[i].name, 'start') }, _('Start')),
96 E('button', { 'class': 'btn cbi-button-action', 'click': ui.createHandlerFn(this, 'handleAction', list[i].name, 'restart') }, _('Restart')),
97 E('button', { 'class': 'btn cbi-button-action', 'click': ui.createHandlerFn(this, 'handleAction', list[i].name, 'stop') }, _('Stop'))
98 ])
99 ]);
100 }
101
102 cbi_update_table(table, rows);
103
104 var view = E('div', {}, [
105 E('h2', _('Startup')),
106 E('div', {}, [
107 E('div', { 'data-tab': 'init', 'data-tab-title': _('Initscripts') }, [
108 E('p', {}, _('You can enable or disable installed init scripts here. Changes will applied after a device reboot.<br /><strong>Warning: If you disable essential init scripts like "network", your device might become inaccessible!</strong>')),
109 table
110 ]),
111 E('div', { 'data-tab': 'rc', 'data-tab-title': _('Local Startup') }, [
112 E('p', {}, _('This is the content of /etc/rc.local. Insert your own commands here (in front of \'exit 0\') to execute them at the end of the boot process.')),
113 E('p', {}, E('textarea', { 'style': 'width:100%', 'rows': 20 }, [ (rcLocal != null ? rcLocal : '') ])),
114 E('div', { 'class': 'cbi-page-actions' }, [
115 E('button', {
116 'class': 'btn cbi-button-save',
117 'click': ui.createHandlerFn(this, 'handleRcLocalSave')
118 }, _('Save'))
119 ])
120 ])
121 ])
122 ]);
123
124 ui.tabs.initTabGroup(view.lastElementChild.childNodes);
125
126 return view;
127 },
128
129 handleSaveApply: null,
130 handleSave: null,
131 handleReset: null
132 });