Restore.js 2.79 KB
Newer Older
Dietmar Maurer's avatar
Dietmar Maurer committed
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
Ext.define('PVE.window.Restore', {
    extend: 'Ext.window.Window', // fixme: PVE.window.Edit?

    resizable: false,

    initComponent : function() {
	var me = this;

	if (!me.nodename) {
	    throw "no node name specified";
	}

	if (!me.volid) {
	    throw "no volume ID specified";
	}

	if (!me.vmtype) {
	    throw "no vmtype specified";
	}

	var storagesel = Ext.create('PVE.form.StorageSelector', {
	    nodename: me.nodename,
	    name: 'storage',
	    value: '',
	    fieldLabel: 'Storage',
	    storageContent: (me.vmtype === 'openvz') ? 'rootdir' : 'images',
	    allowBlank: true
	});

	me.formPanel = Ext.create('Ext.form.Panel', {
	    bodyPadding: 10,
	    border: false,
	    fieldDefaults: {
		labelWidth: 60,
		anchor: '100%'
	    },
	    items: [
		{
		    xtype: 'displayfield',
		    value: me.volidText || me.volid,
		    fieldLabel: 'Source'
		},
		storagesel,
		{
45
		    xtype: me.vmid ? 'displayfield' : 'pveVMIDSelector',
Dietmar Maurer's avatar
Dietmar Maurer committed
46
		    name: 'vmid',
47 48
		    fieldLabel: 'VM ID',
		    value: me.vmid || PVE.data.ResourceStore.findNextVMID(),
Dietmar Maurer's avatar
Dietmar Maurer committed
49 50 51 52 53 54 55 56 57 58 59
		    validateExists: false
		}
	    ]
	});

	var form = me.formPanel.getForm();

	form.on('validitychange', function(f, valid) {
	    submitBtn.setDisabled(!valid);
	});

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	var doRestore = function(url, params) {
	    PVE.Utils.API2Request({
		url: url,
		params: params,
		method: 'POST',
		failure: function (response, opts) {
		    Ext.Msg.alert('Error', response.htmlStatus);
		},
		success: function(response, options) {
		    var upid = response.result.data;
		    
		    var win = Ext.create('PVE.window.TaskViewer', { 
			upid: upid
		    });
		    win.show();
		    me.close();
		}
	    });
	};

Dietmar Maurer's avatar
Dietmar Maurer committed
80 81 82 83 84 85 86 87
	var submitBtn = Ext.create('Ext.Button', {
	    text: 'Restore',
	    handler: function(){
		var storage = storagesel.getValue();
		var values = form.getValues();

		var params = {
		    storage: storage,
88 89
		    vmid: me.vmid ? me.vmid : values.vmid,
		    force: me.vmid ? 1 : 0
Dietmar Maurer's avatar
Dietmar Maurer committed
90 91 92 93 94
		};

		if (me.vmtype === 'openvz') {
		    url = '/nodes/' + me.nodename + '/openvz';
		    params.ostemplate = me.volid;
95
		    params.restore = 1;
Dietmar Maurer's avatar
Dietmar Maurer committed
96 97 98 99 100 101
		} else if (me.vmtype === 'qemu') {
		    url = '/nodes/' + me.nodename + '/qemu';
		    params.archive = me.volid;
		} else {
		    throw 'unknown VM type';
		}
102 103 104 105 106 107 108 109 110 111 112 113 114

		if (me.vmid) {
		    var msg = 'Are you sure you want to restore this VM"? ' +
			'This will permanently erase current VM data.';
		    Ext.Msg.confirm('Confirmation', msg, function(btn) {
			if (btn !== 'yes') {
			    return;
			}
			doRestore(url, params);
		    });
		} else {
		    doRestore(url, params);
		}
Dietmar Maurer's avatar
Dietmar Maurer committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	    }
	});

	var title = (me.vmtype === 'openvz') ? "Restore CT" : "Restore VM";

	Ext.apply(me, {
	    title: title,
	    width: 450,
	    modal: true,
	    layout: 'auto',
	    border: false,
	    items: [ me.formPanel ],
	    buttons: [ submitBtn ],
	});

	me.callParent();
    }
});