summaryrefslogtreecommitdiffstats
path: root/applications/luci-app-dockerman/htdocs/luci-static/resources/view/dockerman/networks.js
blob: b8638edfedcf16c82efb188a6578c70b4e947ed7 (plain)
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
'use strict';
'require form';
'require fs';
'require ui';
'require dockerman.common as dm2';

/*
Copyright 2026
Docker manager JS for Luci by Paul Donald <newtwen+github@gmail.com> 
Based on Docker Lua by lisaac <https://github.com/lisaac/luci-app-dockerman>
LICENSE: GPLv2.0
*/


return dm2.dv.extend({
	load() {
		return Promise.all([
			dm2.network_list(),
			dm2.container_list({query: {all: true}}),
		]);
	},

	render([networks, containers]) {
		if (networks?.code !== 200) {
			return E('div', {}, [ networks?.body?.message ]);
		}

		let network_list = this.getNetworksTable(networks.body, containers.body);
		// let container_list = containers.body;
		const view = this; // Capture the view context


		let pollPending = null;
		let netSec = null;

		const refresh = () => {
			if (pollPending) return pollPending;
			pollPending = view.load().then(([networks2, containers2]) => {
				network_list = view.getNetworksTable(networks2.body, containers2.body);
				// container_list = containers2.body;
				m.data = new m.data.constructor({network: network_list, prune: {}});

				if (netSec) {
					netSec.footer = [
						`${_('Total')} ${network_list.length}`,
					];
				}

				return m.render();
			}).catch((err) => { console.warn(err) }).finally(() => { pollPending = null });
			return pollPending;
		};


		let s, o;
		const m = new form.JSONMap({network: network_list, prune: {}},
			_('Docker - Networks'),
			_('This page displays all docker networks that have been created on the connected docker host.'));
		m.submit = false;
		m.reset = false;

		s = m.section(form.TableSection, 'prune', _('Networks overview'), null);
		s.addremove = false;
		s.anonymous = true;

		const prune = s.option(form.Button, '_prune', null);
		prune.inputtitle = `${dm2.ActionTypes['prune'].i18n} ${dm2.ActionTypes['prune'].e}`;
		prune.inputstyle = 'negative';
		prune.onclick = L.bind(function(section_id, ev) {

			return this.super('handleXHRTransfer', [{
				q_params: {  },
				commandCPath: '/networks/prune',
				commandDPath: '/networks/prune',
				commandTitle: dm2.ActionTypes['prune'].i18n,
				onUpdate: (msg) => {
					try {
						if(msg.error)
							ui.addTimeLimitedNotification(dm2.ActionTypes['prune'].i18n, msg.error, 7000, 'error');

						const output = JSON.stringify(msg, null, 2) + '\n';
						view.insertOutput(output);
					} catch {

					}
				},
				noFileUpload: true,
			}]);

			// return view.executeDockerAction(
			// 	dm2.network_prune,
			// 	{ query: { filters: '' } },
			// 	dm2.ActionTypes['prune'].i18n,
			// 	{
			// 		showOutput: true,
			// 		successMessage: _('started/completed'),
			// 		onSuccess: () => {
			// 			setTimeout(() => window.location.href = `${this.dockerman_url}/networks`, 1000);
			// 		}
			// 	}
			// );
		}, this);

		netSec = m.section(form.TableSection, 'network');
		netSec.anonymous = true;
		netSec.nodescriptions = true;
		netSec.addremove = true;
		netSec.sortable = true;
		netSec.filterrow = true;
		netSec.addbtntitle = `${dm2.ActionTypes['create'].i18n} ${dm2.ActionTypes['create'].e}`;
		netSec.footer = [
			`${_('Total')} ${network_list.length}`,
		];

		netSec.handleAdd = function(section_id, ev) {
			window.location.href = `${view.dockerman_url}/network_new`;
		};

		netSec.handleRemove = function(section_id, force, ev) {
			const network = network_list.find(net => net['.name'] === section_id);
			if (!network?.Id) return false;

			return view.executeDockerAction(
				dm2.network_remove,
				{ id: network.Id },
				dm2.ActionTypes['remove'].i18n,
				{
					showOutput: true,
					onSuccess: () => {
						return refresh();
					}
				}
			);
		};

		netSec.handleInspect = function(section_id, ev) {
			const network = network_list.find(net => net['.name'] === section_id);
			if (!network?.Id) return false;

			return view.executeDockerAction(
				dm2.network_inspect,
				{ id: network.Id },
				dm2.ActionTypes['inspect'].i18n,
				{ showOutput: true, showSuccess: false }
			);
		};

		netSec.renderRowActions = function (section_id) {
			const network = network_list.find(net => net['.name'] === section_id);
			const btns = [
				E('button', {
					'class': 'cbi-button view',
					'title': dm2.ActionTypes['inspect'].i18n,
					'click': ui.createHandlerFn(this, this.handleInspect, section_id),
				}, [dm2.ActionTypes['inspect'].e]),

				E('div', {
					'style': 'width: 20px',
					// Some safety margin for mis-clicks
				}, [' ']),

				E('button', {
					'class': 'cbi-button cbi-button-negative remove',
					'title': dm2.ActionTypes['remove'].i18n,
					'click': ui.createHandlerFn(this, this.handleRemove, section_id, false),
					'disabled': network?._disable_delete,
				}, dm2.ActionTypes['remove'].e),
				E('button', {
					'class': 'cbi-button cbi-button-negative important remove',
					'title': dm2.ActionTypes['force_remove'].i18n,
					'click': ui.createHandlerFn(this, this.handleRemove, section_id, true),
					'disabled': network?._disable_delete,
				}, dm2.ActionTypes['force_remove'].e),
			];
			return E('td', { 'class': 'td middle cbi-section-actions' }, E('div', btns));
		};

		o = netSec.option(form.DummyValue, '_shortId', _('ID'));

		o = netSec.option(form.DummyValue, 'Name', _('Name'));

		o = netSec.option(form.DummyValue, 'Labels', _('Labels') + '  🏷️');
		o.cfgvalue = view.objectCfgValueTT;

		o = netSec.option(form.DummyValue, '_container', _('Containers'));

		o = netSec.option(form.DummyValue, 'Driver', _('Driver'));

		o = netSec.option(form.DummyValue, '_interface', _('Parent Interface'));

		o = netSec.option(form.DummyValue, '_subnet', _('Subnet'));

		o = netSec.option(form.DummyValue, '_gateway', _('Gateway'));

		this.insertOutputFrame(s, m);

		return m.render();
	},

	handleSave: null,
	handleSaveApply: null,
	handleReset: null,

	getNetworksTable(networks, containers) {
		const data = [];

		for (const [ , net] of (networks || []).entries()) {
			const n = net.Name;
			const _shortId = (net.Id || '').substring(0, 12);
			const shortLink = E('a', {
				'href': `${view.dockerman_url}/network/${net.Id}`,
				'style': 'font-family: monospace;',
				'title': _('Click to view this network'),
			}, [_shortId]);

			// Just push plain data objects without UCI metadata
			const configs = Array.isArray(net?.IPAM?.Config) ? net.IPAM.Config : [];
			data.push({
				...net,
				_gateway: configs.map(o => o.Gateway).filter(o => o).join(', ') || '',
				_subnet: configs.map(o => o.Subnet).filter(o => o).join(', ') || '',
				_disable_delete: ( n === 'bridge' || n === 'none' || n === 'host' ) ? true : null,
				_shortId: shortLink,
				_container: this.parseContainerLinksForNetwork(net, containers),
				_interface: (net.Driver === 'bridge')
					? net.Options?.['com.docker.network.bridge.name'] || ''
					: (net.Driver === 'macvlan')
						? net?.Options?.parent
						: '',
			});
		}

		return data;
	},

});