Skip to content

Commit

Permalink
Merge pull request #23 from csdhome/RunCloudAutomation
Browse files Browse the repository at this point in the history
Added the ability to run cloud automation scripts by a sysprep command in the registry and added the ability to run them after a reboot if one is detected.
  • Loading branch information
csdhome committed Aug 6, 2014
2 parents 4c9e65c + ec4d1ac commit fed545f
Show file tree
Hide file tree
Showing 18 changed files with 410 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
COMPANY = "Rackspace Cloud"
DESCRIPTION = "C#.NET Agent for Windows Virtual Machines"
CLR_VERSION = 'v3.5'
RELEASE_BUILD_NUMBER = "1.3.0.0"
RELEASE_BUILD_NUMBER = "1.3.0.1"

#Paths
SLN_FILE = File.join(ABSOLUTE_PATH,'src','WindowsConfigurationAgent.sln')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
[assembly: AssemblyCompany("Rackspace Cloud")]
[assembly: AssemblyProduct("Rackspace Cloud Server Agent")]
[assembly: AssemblyCopyright("Copyright (c) 2009 2010 2011, Rackspace Cloud. All Rights Reserved")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyVersion("1.3.0.1")]
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
[assembly: AssemblyCompany("Rackspace Cloud")]
[assembly: AssemblyProduct("Rackspace Cloud Server Agent")]
[assembly: AssemblyCopyright("Copyright (c) 2009 2010 2011, Rackspace Cloud. All Rights Reserved")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyVersion("1.3.0.1")]
10 changes: 9 additions & 1 deletion src/Rackspace.Cloud.Server.Agent.Service/ServerClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
using Rackspace.Cloud.Server.Common.Restart;
using StructureMap;
using StructureMap.Configuration.DSL;
using Rackspace.Cloud.Server.Common.Commands;
using Rackspace.Cloud.Server.Common.Commands;
using Rackspace.Cloud.Server.Agent.Actions;

namespace Rackspace.Cloud.Server.Agent.Service {
public class ServerClass {
Expand All @@ -50,6 +51,7 @@ public class ServerClass {
StructureMapConfiguration.BuildInstancesOf<ITimer>().TheDefaultIs(Registry.Object(_timer));
IoC.Register();

RunCloudAutomation();
CheckAgentUpdater();

_timer.Enabled = true;
Expand Down Expand Up @@ -95,5 +97,11 @@ private void CheckAgentUpdater()
_logger.Log(string.Format("Error checking the min version of the updater and updating: {0}", ex));
}
}

private void RunCloudAutomation()
{
var cloudAutomationActions = ObjectFactory.GetInstance<ICloudAutomationActions>();
cloudAutomationActions.RunPostRebootCloudAutomationScripts();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using NUnit.Framework;
using Rackspace.Cloud.Server.Agent.Actions;
using StructureMap;

namespace Rackspace.Cloud.Server.Agent.Specs
{
[TestFixture]
public class CloudAutomationSubActionsSpec
{
[SetUp]
public void Setup()
{
Utility.ConfigureStructureMap();
}

[Test]
public void should_detect_sysprep_key_and_remove_it_and_detect_it_is_gone()
{
var cloudAutomationActions = ObjectFactory.GetInstance<ICloudAutomationSubActions>();
RegistryKey rk = Registry.LocalMachine.CreateSubKey(Constants.RackspaceRegKey);
rk.SetValue(Constants.CloudAutomationSysPrepRegKey, "True");

Assert.IsTrue(cloudAutomationActions.IsSysPrepSignalPresent());

Assert.IsTrue(cloudAutomationActions.RemoveSysPrepSignal());

Assert.IsFalse(cloudAutomationActions.IsSysPrepSignalPresent());
}

[Test]
public void should_create_then_detect_kmsactivate_key_and_remove_it_and_detect_it_is_gone()
{
var cloudAutomationActions = ObjectFactory.GetInstance<ICloudAutomationSubActions>();

Assert.IsTrue(cloudAutomationActions.WriteKMSActivateSignal());

Assert.IsTrue(cloudAutomationActions.IsKMSActivateSignalPresent());

Assert.IsTrue(cloudAutomationActions.RemoveKMSActivateSignal());

Assert.IsFalse(cloudAutomationActions.IsKMSActivateSignalPresent());
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
[assembly: AssemblyCompany("Rackspace Cloud")]
[assembly: AssemblyProduct("Rackspace Cloud Server Agent")]
[assembly: AssemblyCopyright("Copyright (c) 2009 2010 2011, Rackspace Cloud. All Rights Reserved")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyVersion("1.3.0.1")]
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<Compile Include="AgentUpdateMessageHandlerSpec.cs" />
<Compile Include="AgentUpdateSpec.cs" />
<Compile Include="ChecksumValidatorSpec.cs" />
<Compile Include="CloudAutomationSubActionsSpec.cs" />
<Compile Include="CommandFactorySpec.cs" />
<Compile Include="DownloaderSpec.cs" />
<Compile Include="EncryptionSpec.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
[assembly: AssemblyCompany("Rackspace Cloud")]
[assembly: AssemblyProduct("Rackspace Cloud Server Agent")]
[assembly: AssemblyCopyright("Copyright (c) 2009 2010 2011, Rackspace Cloud. All Rights Reserved")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyVersion("1.3.0.1")]
110 changes: 110 additions & 0 deletions src/Rackspace.Cloud.Server.Agent/Actions/CloudAutomationActions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2011 OpenStack LLC.
// All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

using System;
using Rackspace.Cloud.Server.Common.Restart;
using Rackspace.Cloud.Server.Common.Logging;

namespace Rackspace.Cloud.Server.Agent.Actions
{
public interface ICloudAutomationActions
{
void RunKMSActivateCloudAutomationScripts();
void RunPostRebootCloudAutomationScripts();
}

public class CloudAutomationActions : ICloudAutomationActions
{
private readonly ILogger _logger;
private readonly ICloudAutomationSubActions _cloudAutomationSubActions;

public CloudAutomationActions(ILogger logger, ICloudAutomationSubActions cloudAutomationSubActions)
{
_logger = logger;
_cloudAutomationSubActions = cloudAutomationSubActions;
}

#region ICloudAutomationActions Members

public void RunPostRebootCloudAutomationScripts()
{
try
{
_logger.Log("Checking for KMSActivate cloud automation registry entry");
if (_cloudAutomationSubActions.IsKMSActivateSignalPresent())
{
_logger.Log("KMSActivate registry entry found, removing registry entry");
if (_cloudAutomationSubActions.RemoveKMSActivateSignal())
{
_logger.Log("Successfully removed KMSActivate registry entry, running cloud automation scripts");
_cloudAutomationSubActions.RunCloudAutomationScripts();
}
else
{
_logger.Log("Could not delete the KMSActivate registry entry for cloud automation, not running cloud automation.");
}
}
else
{
_logger.Log("KMSActivate cloud automation registry entry not detected.");
}
}
catch (Exception ex)
{
_logger.Log(string.Format("Error running cloud automation: {0}", ex));
}
}

public void RunKMSActivateCloudAutomationScripts()
{
try
{
_logger.Log("Checking for SysPrep cloud automation registry entry");
if (_cloudAutomationSubActions.IsSysPrepSignalPresent())
{
_logger.Log("SysPrep registry entry found, removing registry entry");
if (_cloudAutomationSubActions.RemoveSysPrepSignal())
{
_logger.Log("Successfully removed SysPrep registry entry, checking to see if a reboot is pending");
if (RestartManager.RestartNeeded)
{
_logger.Log("Reboot is pending, writing KMSActivate registry entry for post reboot cloud automation script execution");
_cloudAutomationSubActions.WriteKMSActivateSignal();
}
else
{
_logger.Log("Reboot is not pending, running cloud automation scripts");
_cloudAutomationSubActions.RunCloudAutomationScripts();
}
}
else
{
_logger.Log("Could not remove the SysPrep registry key, not writing the KMSActivate registry key for cloud automation.");
}
}
else
{
_logger.Log("SysPrep cloud automation registry entry not detected");
}
}
catch (Exception ex)
{
_logger.Log(string.Format("An error occurred while writing the KMSActivate registry key for cloud automation: {0}", ex));
}
}

#endregion
}
}
Loading

0 comments on commit fed545f

Please sign in to comment.