2017-01-16 20:59:50 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
|
* that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer.
|
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer in the documentation and/or other
|
|
|
|
|
* materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using XenAdmin.Actions;
|
2020-09-25 15:39:22 +02:00
|
|
|
|
using XenAdmin.Commands;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAdmin.Controls;
|
|
|
|
|
using XenAdmin.Dialogs;
|
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Wizards.ImportWizard
|
|
|
|
|
{
|
|
|
|
|
public partial class StoragePickerPage : XenTabPage
|
|
|
|
|
{
|
|
|
|
|
#region Private fields
|
|
|
|
|
private volatile Task m_importTask;
|
|
|
|
|
private bool m_alreadyFoundVM;
|
|
|
|
|
private ActionProgressDialog m_actionDialog;
|
|
|
|
|
private string m_buttonNextText = Messages.WIZARD_BUTTON_NEXT;
|
|
|
|
|
private bool m_buttonPreviousEnabled = true;
|
|
|
|
|
private bool m_buttonNextEnabled;
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public event Action ImportVmCompleted;
|
|
|
|
|
|
|
|
|
|
public StoragePickerPage()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Base class (XenTabPage) overrides
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the page's title (headline)
|
|
|
|
|
/// </summary>
|
2019-06-25 13:52:32 +02:00
|
|
|
|
public override string PageTitle => Messages.IMPORT_SELECT_STORAGE_PAGE_TITLE;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the page's label in the (left hand side) wizard progress panel
|
|
|
|
|
/// </summary>
|
2019-06-25 13:52:32 +02:00
|
|
|
|
public override string Text => Messages.IMPORT_SELECT_STORAGE_PAGE_TEXT;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the value by which the help files section for this page is identified
|
|
|
|
|
/// </summary>
|
2019-06-25 13:52:32 +02:00
|
|
|
|
public override string HelpID => "StoragePicker";
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
protected override bool ImplementsIsDirty()
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-09 01:31:46 +01:00
|
|
|
|
protected override void PageLoadedCore(PageLoadedDirection direction)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
m_buttonNextText = (direction == PageLoadedDirection.Forward) ? Messages.IMPORT_VM_IMPORT : Messages.WIZARD_BUTTON_NEXT;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-09 00:55:50 +01:00
|
|
|
|
protected override void PageLeaveCore(PageLoadedDirection direction, ref bool cancel)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (direction == PageLoadedDirection.Forward && IsDirty)
|
|
|
|
|
{
|
|
|
|
|
cancel = true;
|
|
|
|
|
IsDirty = false;
|
|
|
|
|
SetButtonNextEnabled(false);
|
|
|
|
|
m_buttonPreviousEnabled = false;
|
|
|
|
|
OnPageUpdated();
|
2021-11-05 22:46:07 +01:00
|
|
|
|
ImportXvaAction = new ImportVmAction(TargetHost == null ? TargetConnection : TargetHost.Connection,
|
|
|
|
|
TargetHost, FilePath, SR,
|
2020-09-25 15:39:22 +02:00
|
|
|
|
VMOperationCommand.WarningDialogHAInvalidConfig, VMOperationCommand.StartDiagnosisForm);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
ImportXvaAction.Completed += m_importXvaAction_Completed;
|
|
|
|
|
|
|
|
|
|
m_actionDialog = new ActionProgressDialog(ImportXvaAction, ProgressBarStyle.Blocks) {ShowCancel = true};
|
|
|
|
|
// Now we want to start a scanning thread to search for the task ref on the vms
|
|
|
|
|
ThreadPool.QueueUserWorkItem(ScanTask, null);
|
|
|
|
|
m_alreadyFoundVM = false;
|
|
|
|
|
m_actionDialog.Show(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string NextText(bool isLastPage)
|
|
|
|
|
{
|
|
|
|
|
return m_buttonNextText;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void PopulatePage()
|
2020-03-30 00:49:06 +02:00
|
|
|
|
{
|
|
|
|
|
SetButtonNextEnabled(false);
|
2021-11-05 22:46:07 +01:00
|
|
|
|
m_srPicker.PopulateAsync(SrPicker.SRPickerType.VM, TargetConnection, TargetHost, null, null);
|
2020-03-30 00:49:06 +02:00
|
|
|
|
IsDirty = true;
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
public override bool EnablePrevious()
|
|
|
|
|
{
|
|
|
|
|
return m_buttonPreviousEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool EnableNext()
|
|
|
|
|
{
|
|
|
|
|
return m_buttonNextEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Accessors
|
|
|
|
|
|
2021-11-05 22:46:07 +01:00
|
|
|
|
internal IXenConnection TargetConnection { get; set; }
|
|
|
|
|
|
|
|
|
|
internal Host TargetHost { get; set; }
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public ImportVmAction ImportXvaAction { get; private set; }
|
|
|
|
|
|
|
|
|
|
public string FilePath { private get; set; }
|
|
|
|
|
|
2019-06-25 13:52:32 +02:00
|
|
|
|
public SR SR => m_srPicker.SR;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
public VM ImportedVm { get; private set; }
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Private methods
|
|
|
|
|
|
|
|
|
|
private void SetButtonNextEnabled(bool enabled)
|
|
|
|
|
{
|
|
|
|
|
m_buttonNextEnabled = enabled;
|
|
|
|
|
OnPageUpdated();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool ImportInProgress()
|
|
|
|
|
{
|
|
|
|
|
// Check if the upload action has started
|
|
|
|
|
if (ImportXvaAction != null)
|
|
|
|
|
{
|
|
|
|
|
// if the VM has been uploaded we can continue
|
|
|
|
|
if (m_alreadyFoundVM)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// if it has failed or been cancelled we let them try again
|
|
|
|
|
if (ImportXvaAction.IsCompleted)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ScanTask(object obj)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOffEventThread();
|
|
|
|
|
|
|
|
|
|
if (ImportXvaAction == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
while (ImportXvaAction.RelatedTask == null)
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
|
2021-11-05 22:46:07 +01:00
|
|
|
|
while ((m_importTask = TargetConnection.Resolve(ImportXvaAction.RelatedTask)) == null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
|
|
|
|
|
// We register a XenObjectsUpdated event handler where we check that the import task has the object creation phase marked as "complete";
|
|
|
|
|
// Once the object creation is complete, we look for the vm; When we found the vm we unregister this event handler;
|
2021-11-05 22:46:07 +01:00
|
|
|
|
TargetConnection.XenObjectsUpdated += targetConnection_XenObjectsUpdated;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2015-04-16 15:17:02 +02:00
|
|
|
|
Program.Invoke(this, CheckTask);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckTask()
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
|
|
|
|
// We need to wait for the task to make the object creation phase as complete
|
|
|
|
|
|
2014-12-11 16:05:50 +01:00
|
|
|
|
if (m_importTask == null || m_importTask.other_config == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (!m_importTask.other_config.ContainsKey("object_creation"))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (m_importTask.other_config["object_creation"] != "complete")
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Once this is done, look for the vm
|
|
|
|
|
GetVm();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GetVm()
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
|
|
|
|
if (m_alreadyFoundVM)
|
|
|
|
|
{
|
|
|
|
|
// Should never get here (as we unregister XenObjectsUpdated event handler after we find the vm) but just in case,
|
|
|
|
|
// unregister XenObjectsUpdated event handler
|
2021-11-05 22:46:07 +01:00
|
|
|
|
TargetConnection.XenObjectsUpdated -= targetConnection_XenObjectsUpdated;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 22:46:07 +01:00
|
|
|
|
foreach (VM vm in TargetConnection.Cache.VMs)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (!vm.other_config.ContainsKey(ImportVmAction.IMPORT_TASK))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
string importTaskRef = vm.other_config[ImportVmAction.IMPORT_TASK];
|
|
|
|
|
|
|
|
|
|
if (m_importTask.opaque_ref == importTaskRef)
|
|
|
|
|
{
|
|
|
|
|
m_alreadyFoundVM = true;
|
|
|
|
|
ImportedVm = vm;
|
|
|
|
|
|
|
|
|
|
// We found the VM, unregister XenObjectsUpdated event handler
|
2021-11-05 22:46:07 +01:00
|
|
|
|
TargetConnection.XenObjectsUpdated -= targetConnection_XenObjectsUpdated;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
// And close the dialog, flick to next page.
|
|
|
|
|
m_actionDialog.Close();
|
|
|
|
|
if (ImportVmCompleted != null)
|
|
|
|
|
ImportVmCompleted();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Event handlers
|
|
|
|
|
|
|
|
|
|
private void targetConnection_XenObjectsUpdated(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
CheckTask();
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-05 15:28:21 +02:00
|
|
|
|
private void m_importXvaAction_Completed(ActionBase sender)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2015-04-16 15:17:02 +02:00
|
|
|
|
Program.Invoke(this, () =>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
|
|
|
|
if (!(ImportXvaAction.Succeeded) || ImportXvaAction.Cancelled)
|
|
|
|
|
{
|
|
|
|
|
// task failed or has been cancelled, unregister XenObjectsUpdated event handler
|
2021-11-05 22:46:07 +01:00
|
|
|
|
TargetConnection.XenObjectsUpdated -= targetConnection_XenObjectsUpdated;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
// Give the user a chance to correct any errors
|
|
|
|
|
m_actionDialog = null;
|
|
|
|
|
m_buttonPreviousEnabled = true;
|
|
|
|
|
OnPageUpdated();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 00:49:06 +02:00
|
|
|
|
private void m_srPicker_SelectedIndexChanged(object sender, EventArgs e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (ImportInProgress())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
SetButtonNextEnabled(m_srPicker.SR != null);
|
|
|
|
|
IsDirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|