Skip to content

Powershell commandlet to enable diagnostics for V2 VM #662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Common/Commands.Common/Commands.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
<Compile Include="AzurePSCmdlet.cs" />
<Compile Include="CmdletExtensions.cs" />
<Compile Include="ConfigurationConstants.cs" />
<Compile Include="DiagnosticsHelper.cs" />
<Compile Include="ErrorHelper.cs" />
<Compile Include="IdnHelper.cs" />
<Compile Include="ManagementOperationContext.cs" />
Expand Down
127 changes: 127 additions & 0 deletions src/Common/Commands.Common/DiagnosticsHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// 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://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 Microsoft.WindowsAzure.Commands.Common.Properties;
using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using Newtonsoft.Json;

namespace Microsoft.WindowsAzure.Commands.Utilities.Common
{
public static class DiagnosticsHelper
{
private static string XmlNamespace = "http://schemas.microsoft.com/ServiceHosting/2010/10/DiagnosticsConfiguration";
private static string EncodedXmlCfg = "xmlCfg";
private static string StorageAccount = "storageAccount";
private static string Path = "path";
private static string ExpandResourceDirectory = "expandResourceDirectory";
private static string LocalResourceDirectory = "localResourceDirectory";
private static string StorageAccountNameTag = "storageAccountName";
private static string StorageAccountKeyTag = "storageAccountKey";
private static string StorageAccountEndPointTag = "storageAccountEndPoint";

public static string GetJsonSerializedPublicDiagnosticsConfigurationFromFile(string configurationPath,
string storageAccountName)
{
return
JsonConvert.SerializeObject(
DiagnosticsHelper.GetPublicDiagnosticsConfigurationFromFile(configurationPath, storageAccountName));
}

public static Hashtable GetPublicDiagnosticsConfigurationFromFile(string configurationPath, string storageAccountName)
{
using (StreamReader reader = new StreamReader(configurationPath))
{
return GetPublicDiagnosticsConfiguration(reader.ReadToEnd(), storageAccountName);
}
}

public static Hashtable GetPublicDiagnosticsConfiguration(string config, string storageAccountName)
{
// find the <WadCfg> element and extract it
int wadCfgBeginIndex = config.IndexOf("<WadCfg>");
if (wadCfgBeginIndex == -1)
{
throw new ArgumentException(Resources.IaasDiagnosticsBadConfigNoWadCfg);
}

int wadCfgEndIndex = config.IndexOf("</WadCfg>");
if (wadCfgEndIndex == -1)
{
throw new ArgumentException(Resources.IaasDiagnosticsBadConfigNoEndWadCfg);
}

if (wadCfgEndIndex <= wadCfgBeginIndex)
{
throw new ArgumentException(Resources.IaasDiagnosticsBadConfigNoMatchingWadCfg);
}

string encodedConfiguration = Convert.ToBase64String(
Encoding.UTF8.GetBytes(
config.Substring(
wadCfgBeginIndex, wadCfgEndIndex + "</WadCfg>".Length - wadCfgBeginIndex).ToCharArray()));

// Now extract the local resource directory element
XmlDocument doc = new XmlDocument();
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("ns", XmlNamespace);
doc.LoadXml(config);
var node = doc.SelectSingleNode("//ns:LocalResourceDirectory", ns);
string localDirectory = (node != null && node.Attributes != null) ? node.Attributes[Path].Value : null;
string localDirectoryExpand = (node != null && node.Attributes != null)
? node.Attributes["expandEnvironment"].Value
: null;
if (localDirectoryExpand == "0")
{
localDirectoryExpand = "false";
}
if (localDirectoryExpand == "1")
{
localDirectoryExpand = "true";
}

var hashTable = new Hashtable();
hashTable.Add(EncodedXmlCfg, encodedConfiguration);
hashTable.Add(StorageAccount, storageAccountName);
if (!string.IsNullOrEmpty(localDirectory))
{
var localDirectoryHashTable = new Hashtable();
localDirectoryHashTable.Add(Path, localDirectory);
localDirectoryHashTable.Add(ExpandResourceDirectory, localDirectoryExpand);
hashTable.Add(LocalResourceDirectory, localDirectoryHashTable);
}

return hashTable;
}

public static string GetJsonSerializedPrivateDiagnosticsConfiguration(string storageAccountName,
string storageKey, string endpoint)
{
return JsonConvert.SerializeObject(GetPrivateDiagnosticsConfiguration( storageAccountName, storageKey, endpoint));
}

public static Hashtable GetPrivateDiagnosticsConfiguration(string storageAccountName, string storageKey, string endpoint)
{
var hashTable = new Hashtable();
hashTable.Add(StorageAccountNameTag, storageAccountName);
hashTable.Add(StorageAccountKeyTag, storageKey);
hashTable.Add(StorageAccountEndPointTag, endpoint);
return hashTable;
}
}
}
27 changes: 27 additions & 0 deletions src/Common/Commands.Common/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/Common/Commands.Common/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1443,4 +1443,13 @@ The file needs to be a PowerShell script (.ps1 or .psm1).</value>
<value>Cannot delete '{0}': {1}</value>
<comment>{0} is the path of a file, {1} is an error message</comment>
</data>
<data name="IaasDiagnosticsBadConfigNoEndWadCfg" xml:space="preserve">
<value>Cannot find the WadCfg end element in the config.</value>
</data>
<data name="IaasDiagnosticsBadConfigNoMatchingWadCfg" xml:space="preserve">
<value>WadCfg start element in the config is not matching the end element.</value>
</data>
<data name="IaasDiagnosticsBadConfigNoWadCfg" xml:space="preserve">
<value>Cannot find the WadCfg element in the config.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@
<HintPath>..\..\..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.WindowsAzure.Management.Storage">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this change adds a new DLL to the output please update WIX file

<HintPath>..\..\..\packages\Microsoft.WindowsAzure.Management.Storage.5.1.1\lib\net40\Microsoft.WindowsAzure.Management.Storage.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression.FileSystem" />
Expand Down Expand Up @@ -161,6 +164,9 @@
<Compile Include="Extension\CustomScript\RemoveAzureVMCustomScriptExtensionCommand.cs" />
<Compile Include="Extension\CustomScript\SetAzureVMCustomScriptExtensionCommand.cs" />
<Compile Include="Extension\CustomScript\VirtualMachineCustomScriptExtensionContext.cs" />
<Compile Include="Extension\Diagnostics\GetAzureVMDiagnosticsExtension.cs" />
<Compile Include="Extension\Diagnostics\RemoveAzureVMDiagnosticsExtension.cs" />
<Compile Include="Extension\Diagnostics\SetAzureVMDiagnosticsExtension.cs" />
<Compile Include="Extension\DSC\DscExtensionCmdletCommonBase.cs" />
<Compile Include="Extension\DSC\PublishAzureVMDscConfigurationCommand.cs" />
<Compile Include="Extension\DSC\RemoveAzureVMDscExtensionCommand.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static class ProfileNouns
public const string VirtualMachineExtension = "AzureVMExtension";
public const string VirtualMachineCustomScriptExtension = "AzureVMCustomScriptExtension";
public const string VirtualMachineAccessExtension = "AzureVMAccessExtension";
public const string VirtualMachineDiagnosticsExtension = "AzureVMDiagnosticsExtension";
public const string VirtualMachineExtensionImage = "AzureVMExtensionImage";
public const string VirtualMachineExtensionImageVersion = "AzureVMExtensionImageVersion";
public const string VirtualMachineExtensionImageType = "AzureVMExtensionImageType";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// 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://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.Linq;
using System.Management.Automation;
using Microsoft.Azure.Commands.Compute.Common;
using Microsoft.Azure.Commands.Compute.Models;
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;

namespace Microsoft.Azure.Commands.Compute
{
[Cmdlet(
VerbsCommon.Get,
ProfileNouns.VirtualMachineDiagnosticsExtension),
OutputType(
typeof(PSVirtualMachineExtension))]
public class GetAzureVMDiagnosticsExtensionCommand : VirtualMachineExtensionBaseCmdlet
{
[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The resource group name.")]
[ValidateNotNullOrEmpty]
public string ResourceGroupName { get; set; }

[Alias("ResourceName")]
[Parameter(
Mandatory = true,
Position = 1,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The virtual machine name.")]
[ValidateNotNullOrEmpty]
public string VMName { get; set; }

[Alias("ExtensionName")]
[Parameter(
Mandatory = true,
Position = 2,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Extension Name.")]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

[Parameter(
Position = 3,
ValueFromPipelineByPropertyName = true,
HelpMessage = "To show the status.")]
[ValidateNotNullOrEmpty]
public SwitchParameter Status { get; set; }

public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();

ExecuteClientAction(() =>
{
VirtualMachineExtensionGetResponse virtualMachineExtensionGetResponse = null;
if (Status.IsPresent)
{
virtualMachineExtensionGetResponse =
this.VirtualMachineExtensionClient.GetWithInstanceView(this.ResourceGroupName,
this.VMName, this.Name);
}
else
{
virtualMachineExtensionGetResponse = this.VirtualMachineExtensionClient.Get(this.ResourceGroupName,
this.VMName, this.Name);
}

var returnedExtension = virtualMachineExtensionGetResponse.ToPSVirtualMachineExtension(this.ResourceGroupName);
WriteObject(returnedExtension);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// 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://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 System.Management.Automation;
using Microsoft.Azure.Commands.Compute.Common;
using Microsoft.Azure.Management.Compute;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;

namespace Microsoft.Azure.Commands.Compute
{
[Cmdlet(
VerbsCommon.Remove,
ProfileNouns.VirtualMachineDiagnosticsExtension)]
public class RemoveAzureVMDiagnosticsExtensionCommand : VirtualMachineExtensionBaseCmdlet
{
[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The resource group name.")]
[ValidateNotNullOrEmpty]
public string ResourceGroupName { get; set; }

[Alias("ResourceName")]
[Parameter(
Mandatory = true,
Position = 1,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The virtual machine name.")]
[ValidateNotNullOrEmpty]
public string VMName { get; set; }

[Alias("ExtensionName")]
[Parameter(
Mandatory = true,
Position = 2,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Extension Name.")]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();

ExecuteClientAction(() =>
{
var op = this.VirtualMachineExtensionClient.Delete(this.ResourceGroupName, this.VMName,
this.Name);
WriteObject(op);
});
}
}
}
Loading