]> git.proxmox.com Git - pve-manager.git/blob - www/manager/lxc/Snapshot.js
7b2e33ae5060bd575df1be2c6b56ecdeb1a92688
[pve-manager.git] / www / manager / lxc / Snapshot.js
1 Ext.define('PVE.window.LxcSnapshot', {
2 extend: 'Ext.window.Window',
3
4 resizable: false,
5
6 take_snapshot: function(snapname, descr, vmstate) {
7 var me = this;
8 var params = { snapname: snapname };
9 if (descr) {
10 params.description = descr;
11 }
12
13 PVE.Utils.API2Request({
14 params: params,
15 url: '/nodes/' + me.nodename + '/lxc/' + me.vmid + "/snapshot",
16 waitMsgTarget: me,
17 method: 'POST',
18 failure: function(response, opts) {
19 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
20 },
21 success: function(response, options) {
22 var upid = response.result.data;
23 var win = Ext.create('PVE.window.TaskProgress', { upid: upid });
24 win.show();
25 me.close();
26 }
27 });
28 },
29
30 update_snapshot: function(snapname, descr) {
31 var me = this;
32 PVE.Utils.API2Request({
33 params: { description: descr },
34 url: '/nodes/' + me.nodename + '/lxc/' + me.vmid + "/snapshot/" +
35 snapname + '/config',
36 waitMsgTarget: me,
37 method: 'PUT',
38 failure: function(response, opts) {
39 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
40 },
41 success: function(response, options) {
42 me.close();
43 }
44 });
45 },
46
47 initComponent : function() {
48 var me = this;
49
50 if (!me.nodename) {
51 throw "no node name specified";
52 }
53
54 if (!me.vmid) {
55 throw "no VM ID specified";
56 }
57
58 var summarystore = Ext.create('Ext.data.Store', {
59 model: 'KeyValue',
60 sorters: [
61 {
62 property : 'key',
63 direction: 'ASC'
64 }
65 ]
66 });
67
68 var items = [
69 {
70 xtype: me.snapname ? 'displayfield' : 'textfield',
71 name: 'snapname',
72 value: me.snapname,
73 fieldLabel: gettext('Name'),
74 vtype: 'ConfigId',
75 allowBlank: false
76 }
77 ];
78
79 if (me.snapname) {
80 items.push({
81 xtype: 'displayfield',
82 name: 'snaptime',
83 fieldLabel: gettext('Timestamp')
84 });
85 }
86
87 items.push({
88 xtype: 'textareafield',
89 grow: true,
90 name: 'description',
91 fieldLabel: gettext('Description')
92 });
93
94 if (me.snapname) {
95 items.push({
96 title: gettext('Settings'),
97 xtype: 'grid',
98 height: 200,
99 store: summarystore,
100 columns: [
101 {header: gettext('Key'), width: 150, dataIndex: 'key'},
102 {header: gettext('Value'), flex: 1, dataIndex: 'value'}
103 ]
104 });
105 }
106
107 me.formPanel = Ext.create('Ext.form.Panel', {
108 bodyPadding: 10,
109 border: false,
110 fieldDefaults: {
111 labelWidth: 100,
112 anchor: '100%'
113 },
114 items: items
115 });
116
117 var form = me.formPanel.getForm();
118
119 var submitBtn;
120
121 if (me.snapname) {
122 me.title = gettext('Edit') + ': ' + gettext('Snapshot');
123 submitBtn = Ext.create('Ext.Button', {
124 text: gettext('Update'),
125 handler: function() {
126 if (form.isValid()) {
127 var values = form.getValues();
128 me.update_snapshot(me.snapname, values.description);
129 }
130 }
131 });
132 } else {
133 me.title ="VM " + me.vmid + ': ' + gettext('Take Snapshot');
134 submitBtn = Ext.create('Ext.Button', {
135 text: gettext('Take Snapshot'),
136 handler: function() {
137 if (form.isValid()) {
138 var values = form.getValues();
139 me.take_snapshot(values.snapname, values.description);
140 }
141 }
142 });
143 }
144
145 Ext.apply(me, {
146 modal: true,
147 width: 450,
148 border: false,
149 layout: 'fit',
150 buttons: [ submitBtn ],
151 items: [ me.formPanel ]
152 });
153
154 if (me.snapname) {
155 Ext.apply(me, {
156 width: 620,
157 height: 400
158 });
159 }
160
161 me.callParent();
162
163 if (!me.snapname) {
164 return;
165 }
166
167 // else load data
168 PVE.Utils.API2Request({
169 url: '/nodes/' + me.nodename + '/lxc/' + me.vmid + "/snapshot/" +
170 me.snapname + '/config',
171 waitMsgTarget: me,
172 method: 'GET',
173 failure: function(response, opts) {
174 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
175 me.close();
176 },
177 success: function(response, options) {
178 var data = response.result.data;
179 var kvarray = [];
180 Ext.Object.each(data, function(key, value) {
181 if (key === 'description' || key === 'snaptime') {
182 return;
183 }
184 kvarray.push({ key: key, value: value });
185 });
186
187 summarystore.suspendEvents();
188 summarystore.add(kvarray);
189 summarystore.sort();
190 summarystore.resumeEvents();
191 summarystore.fireEvent('datachanged', summarystore);
192
193 form.findField('snaptime').setValue(new Date(data.snaptime));
194 form.findField('description').setValue(data.description);
195 }
196 });
197 }
198 });