]> git.proxmox.com Git - pve-manager.git/blob - www/manager6/window/Restore.js
ui: eslint: enforce "no-extra-parens" rule
[pve-manager.git] / www / manager6 / window / Restore.js
1 Ext.define('PVE.window.Restore', {
2 extend: 'Ext.window.Window', // fixme: Proxmox.window.Edit?
3
4 resizable: false,
5
6 initComponent: function() {
7 var me = this;
8
9 if (!me.nodename) {
10 throw "no node name specified";
11 }
12
13 if (!me.volid) {
14 throw "no volume ID specified";
15 }
16
17 if (!me.vmtype) {
18 throw "no vmtype specified";
19 }
20
21 var storagesel = Ext.create('PVE.form.StorageSelector', {
22 nodename: me.nodename,
23 name: 'storage',
24 value: '',
25 fieldLabel: gettext('Storage'),
26 storageContent: me.vmtype === 'lxc' ? 'rootdir' : 'images',
27 // when restoring a container without specifying a storage, the backend defaults
28 // to 'local', which is unintuitive and 'rootdir' might not even be allowed on it
29 allowBlank: me.vmtype !== 'lxc',
30 emptyText: me.vmtype === 'lxc' ? '' : gettext('From backup configuration'),
31 autoSelect: me.vmtype === 'lxc',
32 });
33
34 var IDfield;
35 if (me.vmid) {
36 IDfield = Ext.create('Ext.form.field.Display', {
37 name: 'vmid',
38 value: me.vmid,
39 fieldLabel: me.vmtype === 'lxc' ? 'CT' : 'VM',
40 });
41 } else {
42 IDfield = Ext.create('PVE.form.GuestIDSelector', {
43 name: 'vmid',
44 guestType: me.vmtype,
45 loadNextFreeID: true,
46 validateExists: false,
47 });
48 }
49
50 var items = [
51 {
52 xtype: 'displayfield',
53 value: me.volidText || me.volid,
54 fieldLabel: gettext('Source'),
55 },
56 storagesel,
57 IDfield,
58 {
59 xtype: 'pveBandwidthField',
60 name: 'bwlimit',
61 backendUnit: 'KiB',
62 fieldLabel: gettext('Read Limit'),
63 emptyText: gettext('Defaults to target storage restore limit'),
64 autoEl: {
65 tag: 'div',
66 'data-qtip': gettext("Use '0' to disable all bandwidth limits."),
67 },
68 },
69 {
70 xtype: 'fieldcontainer',
71 layout: 'hbox',
72 items: [{
73 xtype: 'proxmoxcheckbox',
74 name: 'unique',
75 fieldLabel: gettext('Unique'),
76 hidden: !!me.vmid,
77 flex: 1,
78 autoEl: {
79 tag: 'div',
80 'data-qtip': gettext('Autogenerate unique properties, e.g., MAC addresses'),
81 },
82 checked: false,
83 },
84 {
85 xtype: 'proxmoxcheckbox',
86 name: 'start',
87 flex: 1,
88 fieldLabel: gettext('Start after restore'),
89 labelWidth: 105,
90 checked: false,
91 }],
92 },
93 ];
94
95 if (me.vmtype === 'lxc') {
96 items.push({
97 xtype: 'proxmoxcheckbox',
98 name: 'unprivileged',
99 value: true,
100 fieldLabel: gettext('Unprivileged container'),
101 });
102 }
103
104 me.formPanel = Ext.create('Ext.form.Panel', {
105 bodyPadding: 10,
106 border: false,
107 fieldDefaults: {
108 labelWidth: 100,
109 anchor: '100%',
110 },
111 items: items,
112 });
113
114 var form = me.formPanel.getForm();
115
116 var doRestore = function(url, params) {
117 Proxmox.Utils.API2Request({
118 url: url,
119 params: params,
120 method: 'POST',
121 waitMsgTarget: me,
122 failure: function(response, opts) {
123 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
124 },
125 success: function(response, options) {
126 var upid = response.result.data;
127
128 var win = Ext.create('Proxmox.window.TaskViewer', {
129 upid: upid,
130 });
131 win.show();
132 me.close();
133 },
134 });
135 };
136
137 var submitBtn = Ext.create('Ext.Button', {
138 text: gettext('Restore'),
139 handler: function() {
140 var values = form.getValues();
141
142 var params = {
143 vmid: me.vmid || values.vmid,
144 force: me.vmid ? 1 : 0,
145 };
146 if (values.unique) { params.unique = 1; }
147 if (values.start) { params.start = 1; }
148 if (values.storage) { params.storage = values.storage; }
149
150 if (values.bwlimit !== undefined) {
151 params.bwlimit = values.bwlimit;
152 }
153
154 var url;
155 var msg;
156 if (me.vmtype === 'lxc') {
157 url = '/nodes/' + me.nodename + '/lxc';
158 params.ostemplate = me.volid;
159 params.restore = 1;
160 if (values.unprivileged) { params.unprivileged = 1; }
161 msg = Proxmox.Utils.format_task_description('vzrestore', params.vmid);
162 } else if (me.vmtype === 'qemu') {
163 url = '/nodes/' + me.nodename + '/qemu';
164 params.archive = me.volid;
165 msg = Proxmox.Utils.format_task_description('qmrestore', params.vmid);
166 } else {
167 throw 'unknown VM type';
168 }
169
170 if (me.vmid) {
171 msg += '. ' + gettext('This will permanently erase current VM data.');
172 Ext.Msg.confirm(gettext('Confirm'), msg, function(btn) {
173 if (btn !== 'yes') {
174 return;
175 }
176 doRestore(url, params);
177 });
178 } else {
179 doRestore(url, params);
180 }
181 },
182 });
183
184 form.on('validitychange', function(f, valid) {
185 submitBtn.setDisabled(!valid);
186 });
187
188 var title = gettext('Restore') + ": " + (
189 me.vmtype === 'lxc' ? 'CT' : 'VM');
190
191 if (me.vmid) {
192 title += " " + me.vmid;
193 }
194
195 Ext.apply(me, {
196 title: title,
197 width: 500,
198 modal: true,
199 layout: 'auto',
200 border: false,
201 items: [me.formPanel],
202 buttons: [submitBtn],
203 });
204
205 me.callParent();
206 },
207 });