Merge pull request #498 from cheng--zhang/CP-12158-new

CP-12624: Upload a CallHome report to CIS on demand
This commit is contained in:
Mihaela Stoica 2015-06-11 15:17:03 +03:00
commit 9ffaab370c
5 changed files with 101 additions and 18 deletions

View File

@ -453,6 +453,7 @@ namespace XenAPI
public const string UPLOAD_LOCK = "CallHome.UploadLock";
public const string LAST_SUCCESSFUL_UPLOAD = "CallHome.LastSuccessfulUpload";
public const string LAST_FAILED_UPLOAD = "CallHome.LastFailedUpload";
public const string NEW_UPLOAD_REQUEST = "CallHome.NewUploadRequest";
public CallHomeSettings(CallHomeStatus status, int intervalInDays, DayOfWeek dayOfWeek, int timeOfDay, int retryInterval)
{

View File

@ -78,10 +78,22 @@ namespace XenServerHealthCheck
}
private static int SleepForLockConfirm = 10 * 1000; // 10 seconds
public static bool Request(IXenConnection connection, Session _session)
private static bool getLock(IXenConnection connection, Session session)
{
Dictionary<string, string> config = new Dictionary<string, string>();
string newUploadLock = System.Configuration.ConfigurationManager.AppSettings["UUID"];
newUploadLock += "|" + DateTime.UtcNow.ToString();
config[CallHomeSettings.UPLOAD_LOCK] = newUploadLock;
Pool.set_gui_config(session, connection.Cache.Pools[0].opaque_ref, config);
System.Threading.Thread.Sleep(SleepForLockConfirm);
config = Pool.get_gui_config(session, connection.Cache.Pools[0].opaque_ref);
return config[CallHomeSettings.UPLOAD_LOCK] == newUploadLock;
}
public static bool Request(IXenConnection connection, Session session)
{
bool needRetry = false;
Dictionary<string, string> config = Pool.get_gui_config(_session, connection.Cache.Pools[0].opaque_ref);
Dictionary<string, string> config = Pool.get_gui_config(session, connection.Cache.Pools[0].opaque_ref);
if (BoolKey(config, CallHomeSettings.STATUS) == false)
{
log.InfoFormat("Will not report for XenServer {0} that was not Enroll", connection.Hostname);
@ -172,14 +184,41 @@ namespace XenServerHealthCheck
}
}
string newUploadLock = System.Configuration.ConfigurationManager.AppSettings["UUID"];
newUploadLock += "|" + currentTime.ToString();
config[CallHomeSettings.UPLOAD_LOCK] = newUploadLock;
Pool.set_gui_config(_session, connection.Cache.Pools[0].opaque_ref, config);
System.Threading.Thread.Sleep(SleepForLockConfirm);
config = Pool.get_gui_config(_session, connection.Cache.Pools[0].opaque_ref);
return getLock(connection, session);
}
return config[CallHomeSettings.UPLOAD_LOCK] == newUploadLock;
private static int DemandTimeOutMinutes = 30;
public static bool OnDemandRequest(IXenConnection connection, Session session)
{
Dictionary<string, string> config = Pool.get_gui_config(session, connection.Cache.Pools[0].opaque_ref);
if (BoolKey(config, CallHomeSettings.STATUS) == false)
{
log.InfoFormat("Will not report on demand for XenServer {0} that was not Enroll", connection.Hostname);
return false;
}
//Check if there already some service doing the uploading already
if (CanLock(Get(config, CallHomeSettings.UPLOAD_LOCK)) == false)
{
log.InfoFormat("Will not report for XenServer {0} that already locked", connection.Hostname);
return false;
}
if (config.ContainsKey(CallHomeSettings.NEW_UPLOAD_REQUEST))
{
DateTime newUploadRequestDueTime = DateTime.Parse(Get(config, CallHomeSettings.NEW_UPLOAD_REQUEST)).AddMinutes(DemandTimeOutMinutes);
if (DateTime.Compare(newUploadRequestDueTime, DateTime.UtcNow) >= 0)
{
return getLock(connection, session);
}
else
{
log.InfoFormat("Will not report on demand for XenServer {0} since the demand due", connection.Hostname);
return false;
}
}
else
return false;
}
}
}

View File

@ -98,24 +98,24 @@ namespace XenServerHealthCheck
foreach (IXenConnection connection in Connections)
{
log.InfoFormat("Check server {0} with user {1}", connection.Hostname, connection.Username);
Session _session = new Session(connection.Hostname, 80);
_session.APIVersion = API_Version.LATEST;
Session session = new Session(connection.Hostname, 80);
session.APIVersion = API_Version.LATEST;
try
{
_session.login_with_password(connection.Username, connection.Password);
connection.LoadCache(_session);
if (RequestUploadTask.Request(connection, _session))
session.login_with_password(connection.Username, connection.Password);
connection.LoadCache(session);
if (RequestUploadTask.Request(connection, session) || RequestUploadTask.OnDemandRequest(connection, session))
{
//Create thread to do log uploading
log.InfoFormat("Will upload report for XenServer {0}", connection.Hostname);
}
_session.logout();
_session = null;
session.logout();
session = null;
}
catch (Exception exn)
{
if (_session != null)
_session.logout();
if (session != null)
session.logout();
log.Error(exn, exn);
}
}

View File

@ -7,6 +7,7 @@ namespace XenServerHealthCheckTests
{
RequestUploadTaskTests requestUploadTaskTests = new RequestUploadTaskTests();
requestUploadTaskTests.checkUploadLock();
requestUploadTaskTests.checkDemandLock();
}
}
}

View File

@ -127,5 +127,47 @@ namespace XenServerHealthCheckTests
catch (Exception)
{}
}
public void checkDemandLock()
{
DatabaseManager.CreateNewConnection(dbName);
IXenConnection connection = DatabaseManager.ConnectionFor(dbName);
Session _session = DatabaseManager.ConnectionFor(dbName).Session;
DatabaseManager.ConnectionFor(dbName).LoadCache(_session);
try
{
Dictionary<string, string> config = cleanStack();
connection.LoadCache(_session);
//1 Uploading is inprocess by current service, demand will be ignore
config = cleanStack();
config[CallHomeSettings.UPLOAD_LOCK] = UUID + "|" + DateTime.UtcNow.ToString();
config[CallHomeSettings.NEW_UPLOAD_REQUEST] = DateTime.UtcNow.ToString();
Pool.set_gui_config(_session, connection.Cache.Pools[0].opaque_ref, config);
Assert.IsFalse(RequestUploadTask.OnDemandRequest(connection, _session));
//2 Uploading is inprocess by other service, demand will be ignore
config = cleanStack();
config[CallHomeSettings.UPLOAD_LOCK] = "test2-test2" + "|" + DateTime.UtcNow.ToString();
config[CallHomeSettings.NEW_UPLOAD_REQUEST] = DateTime.UtcNow.ToString();
Pool.set_gui_config(_session, connection.Cache.Pools[0].opaque_ref, config);
Assert.IsFalse(RequestUploadTask.OnDemandRequest(connection, _session));
//3 Uploading is not due and demand due, demand will be ignore
config = cleanStack();
config[CallHomeSettings.UPLOAD_LOCK] = "test2-test2" + "|" + DateTime.UtcNow.Subtract(TimeSpan.FromDays(14)).ToString();
config[CallHomeSettings.NEW_UPLOAD_REQUEST] = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(31)).ToString();
Pool.set_gui_config(_session, connection.Cache.Pools[0].opaque_ref, config);
Assert.IsFalse(RequestUploadTask.OnDemandRequest(connection, _session));
//4 Uploading is due and demand not due, lock will be set
config = cleanStack();
config[CallHomeSettings.UPLOAD_LOCK] = "test2-test2" + "|" + DateTime.UtcNow.Subtract(TimeSpan.FromDays(14)).ToString();
config[CallHomeSettings.NEW_UPLOAD_REQUEST] = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(28)).ToString();
Pool.set_gui_config(_session, connection.Cache.Pools[0].opaque_ref, config);
Assert.IsTrue(RequestUploadTask.OnDemandRequest(connection, _session));
}
catch (Exception)
{ }
}
}
}