-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...eManager/Compute/Commands.Compute/Extension/Diagnostics/GetAzureVMDiagnosticsExtension.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...nager/Compute/Commands.Compute/Extension/Diagnostics/RemoveAzureVMDiagnosticsExtension.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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