C++调用C#管理Azure虚拟机

来源:互联网 发布:有什么好看的网络电影 编辑:程序博客网 时间:2024/06/08 17:03

    为了使用自己写的C++程序管理Azure虚拟机,查了好几天资料,开始的思路是使用cmd命令调用powershell来管理,写了几天脚本后发现cmd下只能运行一些简单的powershell命令,一套脚本在powershell下运行没有问题,但是在cmd下使用powershell ./script.p1 就会报一大堆错,跟微软的工程师沟通后了解到没有支持C++的SDK,只能使用python、.net等语言进行管理,结合我自身的情况我在python和c#中进行选择,考虑后决定使用C++调用C#来进行管理,在官方文档中找到了一套流程,是使用properties文件进行认证加载,但是在运行过程中会报错,结合微软工程师给我的文档,我将我所需求的功能总结出来,全部正常运行后,做成以下内容:

一、设置身份验证:
1、登录到powershell,记下有关用户和订阅的信息:
Login-AzureRmAccount -Environment "AzureChinaCloud"
2、在powershell中创建服务主体,会返回ApplicationId,注意记录下来:
# Create the service principal (use a strong password) //自己写DisplayName和Password的值
$sp = New-AzureRmADServicePrincipal -DisplayName "AzureDotNetTest" -Password "password"
# Give it the permissions it needs...
New-AzureRmRoleAssignment -ServicePrincipalName $sp.ApplicationId -RoleDefinitionName Contributor
# Display the Application ID, because we'll need it later.
$sp | Select DisplayName, ApplicationId
3、记录所需数据:
subscription:使用运行 Login-AzureRmAccount 后返回的 SubscriptionId 值。
client:使用服务主体输出中的 ApplicationId 值。
key:使用运行 New-AzureRmADServicePrincipal(不带引号)时分配的 -Password 参数。
tenant:使用运行 Login-AzureRmAccount 后返回的 TenantId 值。

二、创建新项目
1、创建新的控制台应用程序项目。 为此,请在 Visual Studio 中依次单击“文件”、“新建”、“项目...”。在 Visual C# 模板下选择“控制台应用(.NET Core)”,为项目命名,并单击“确定”。
创建新的控制台应用后,依次单击“工具”、“NuGet 包管理器”、“包管理器控制台”打开包管理器控制台。 在控制台中,执行以下三个命令来获取所需的包,第一个就足够我本次对虚拟机的管理了:
# Azure Management Libraries for .NET (Fluent)
Install-Package Microsoft.Azure.Management.Fluent
# Azure Store client libraries
Install-Package WindowsAzure.Storage
# SQL Database client libraries
Install-Package System.Data.SqlClient
2、编辑应用程序的 Program.cs 文件。如果要在新创建的程序中使用,必须加入所需引用,程序如下(我新建立的,将以下程序做成dll由c++调用):

using Microsoft.Azure.Management.Compute.Fluent.Models;using Microsoft.Azure.Management.Fluent;using Microsoft.Azure.Management.ResourceManager.Fluent;using Microsoft.Azure.Management.ResourceManager.Fluent.Core;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CSharpUtil{    public class AzureUtil    {        public static string subscriptionId = "6fdc7495";        public static string clientId = "de579e8f";        public static string clientSecret = "Passw0rd";        public static string tenantId = "9fc092f5";        static string groupName = "YangGangRS";        static string Location = "ChinaEast";        static string AvSetName = "YangGangAS";        static string publicIpDnsLabel = "YangGangPublicIP";        static string networkName = "YangGangVN";        static string WindowsUserName = "YGAdmin";        static string WindowsPassword = "Passw0rd1234";        static string subNetName = "YangGangSubnet";        static string imageName = "YangGangCSVM-Image";//设置各个参数的函数        public static bool InitializeAzureData(string gName = "YangGangRS", string location = "ChinaEast", string avSetName = "YangGangAS", string pipDnsLabel = "YangGangPublicIP"            , string networkname = "YangGangVN", string username = "YGAdmin", string passw0rd = "Passw0rd1234", string subnetName = "YangGangSubnet", string iName = "YangGangCSVM-Image")        {            try            {                groupName = gName;                Location = location;                AvSetName = avSetName;                publicIpDnsLabel = pipDnsLabel;                networkName = networkname;                WindowsUserName = username;                WindowsPassword = passw0rd;                subNetName = subnetName;                imageName = iName;            }            catch (Exception e)            {                return false;            }            return true;        }//创建新资源组和第一台虚拟机        public static bool CreateNewReourceAndNetworkAndVM(string vmName, string ipAddress)        {            try            {                string nicName = vmName + "-Nic";                var credential = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureChinaCloud);                var azure = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(credential).WithDefaultSubscription();                Console.WriteLine("Creating resource group...");                var resourceGroup = azure.ResourceGroups.Define(groupName).WithRegion(Location).Create();                Console.WriteLine("Creating availability set...");                var availabilitySet = azure.AvailabilitySets.Define(AvSetName).WithRegion(Location).WithExistingResourceGroup(groupName)                    .WithSku(AvailabilitySetSkuTypes.Managed).Create();                Console.WriteLine("Creating public IP address...");                var publicIPAddress = azure.PublicIPAddresses.Define(publicIpDnsLabel).WithRegion(Location).WithExistingResourceGroup(groupName).WithStaticIP().Create();                Console.WriteLine("Creating virtual network...");                var network = azure.Networks.Define(networkName).WithRegion(Location).WithExistingResourceGroup(groupName)                    .WithAddressSpace("10.1.0.0/16").WithSubnet(subNetName, "10.1.1.0/24").Create();                Console.WriteLine("Creating  network interface ...");                var networkInterFace = azure.NetworkInterfaces.Define(nicName).WithRegion(Location).WithExistingResourceGroup(groupName).WithExistingPrimaryNetwork(network)                    .WithSubnet(subNetName)                    .WithPrimaryPrivateIPAddressStatic(ipAddress)                    .WithExistingPrimaryPublicIPAddress(publicIPAddress).Create();                Console.WriteLine("Creating VM...");                var windowsVM = azure.VirtualMachines.Define(vmName).WithRegion(Location).WithExistingResourceGroup(groupName)                    .WithExistingPrimaryNetworkInterface(networkInterFace).WithLatestWindowsImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter-zhcn")                    .WithAdminUsername(WindowsUserName).WithAdminPassword(WindowsPassword).WithComputerName(vmName).WithExistingAvailabilitySet(availabilitySet)                    .WithSize(VirtualMachineSizeTypes.StandardD2).Create();                Console.WriteLine("Press enter to continue...");            }            catch (Exception e)            {                Console.WriteLine("over");                return false;            }            Console.WriteLine("CreateNewReourceAndNetworkAndVM success");            return true;        }//创建自定义镜像的虚拟机        public static bool CreateNextSelfImageVM(string vmName, string ipAddress)        {            try            {                Console.WriteLine("start");                string nicName = vmName + "-Nic";                var credential = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureChinaCloud);                var azure = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(credential).WithDefaultSubscription();                Console.WriteLine("Creating  network interface ...");                var network = azure.Networks.GetByResourceGroup(groupName, networkName);                var networkInterFace = azure.NetworkInterfaces.Define(nicName).WithRegion(Location).WithExistingResourceGroup(groupName).WithExistingPrimaryNetwork(network)                    .WithSubnet(subNetName)                    .WithPrimaryPrivateIPAddressStatic(ipAddress)                    .Create();                var myImageName = azure.VirtualMachineCustomImages.GetByResourceGroup(groupName, imageName).Id;                Console.WriteLine("Creating VM...");                var availabilitySet = azure.AvailabilitySets.GetByResourceGroup(groupName, AvSetName);                var windowsVM = azure.VirtualMachines.Define(vmName).WithRegion(Location).WithExistingResourceGroup(groupName)                    .WithExistingPrimaryNetworkInterface(networkInterFace).WithWindowsCustomImage(myImageName)                    .WithAdminUsername(WindowsUserName).WithAdminPassword(WindowsPassword).WithComputerName(vmName).WithExistingAvailabilitySet(availabilitySet)                    .WithSize(VirtualMachineSizeTypes.StandardD2V2).Create();            }            catch (Exception e)            {                Console.WriteLine("over");                return false;            }            Console.WriteLine("CreateNextSelfImageVM success");            return true;        }//创建官方镜像虚拟机        public static bool CreateNextWindowsVM(string vmName, string ipAddress)        {            try            {                string nicName = vmName + "-Nic";                var credential = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureChinaCloud);                var azure = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(credential).WithDefaultSubscription();                Console.WriteLine("Creating  network interface ...");                var network = azure.Networks.GetByResourceGroup(groupName, networkName);                var networkInterFace = azure.NetworkInterfaces.Define(nicName).WithRegion(Location).WithExistingResourceGroup(groupName).WithExistingPrimaryNetwork(network)                    .WithSubnet(subNetName)                    .WithPrimaryPrivateIPAddressStatic(ipAddress)                    .Create();                Console.WriteLine("Creating VM...");                var availabilitySet = azure.AvailabilitySets.GetByResourceGroup(groupName, AvSetName);                var windowsVM = azure.VirtualMachines.Define(vmName).WithRegion(Location).WithExistingResourceGroup(groupName)                    .WithExistingPrimaryNetworkInterface(networkInterFace).WithLatestWindowsImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter-zhcn")                    .WithAdminUsername(WindowsUserName).WithAdminPassword(WindowsPassword).WithComputerName(vmName).WithExistingAvailabilitySet(availabilitySet)                    .WithSize(VirtualMachineSizeTypes.StandardD2).Create();            }            catch (Exception e)            {                Console.WriteLine("over");                return false;            }            Console.WriteLine("CreateNextWindowsVM success");            return true;        }//停止一台虚拟机并解除分配        public static bool StopAzureVM(string vmName)        {            try            {                var credential = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureChinaCloud);                var azure = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(credential).WithDefaultSubscription();                var vm = azure.VirtualMachines.GetByResourceGroup(groupName, vmName);                Console.WriteLine("Stoping vm...");                vm.Deallocate();            }            catch (Exception e)            {                Console.WriteLine("over");                return false;            }            Console.WriteLine("StopAzureVM success");            return true;        }//开启一台虚拟机        public static bool StartAzureVM(string vmName)        {            try            {                var credential = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureChinaCloud);                var azure = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(credential).WithDefaultSubscription();                var vm = azure.VirtualMachines.GetByResourceGroup(groupName, vmName);                Console.WriteLine("Starting vm...");                vm.Start();            }            catch (Exception e)            {                Console.WriteLine("over");                return false;            }            Console.WriteLine("StartAzureVM success");            return true;        }//改变虚拟机大小        public static bool ChangeVMSize(string vmName)        {            try            {                var credential = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureChinaCloud);                var azure = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(credential).WithDefaultSubscription();                var vm = azure.VirtualMachines.GetByResourceGroup(groupName, vmName);                Console.WriteLine("Starting change vm size...");                vm.Update().WithSize(VirtualMachineSizeTypes.StandardD2).Apply();            }            catch (Exception e)            {                Console.WriteLine("over");                return false;            }            Console.WriteLine("ChangeVMSize success");            return true;        }        public static void Main(string[] args)        {            //CreateNewReourceAndNetworkAndVM(vmName, ipAddress);            string vmName = "YangGangCSVM1";            string ipAddress = "10.1.1.11";            CreateNextSelfImageVM(vmName, ipAddress);        }    }}
    上方的几个命令基本可以满足我的虚拟机管理需求,还有更多的功能可以点出来,针对虚拟机,镜像和其他资源的操作。

    在C++程序中,我使用以下方法使用这些函数,这里只举了一个例子,其他类似:

#using "..\\apps\\x64\\Debug\\CSharpUtil.dll"using namespace CSharpUtil;using namespace System;using namespace std;DLL_API bool CreateNextSelfImageVM(const wchar_t *wcvmName,const wchar_t *wcipAddress){AzureUtil ^azureUtil=gcnew AzureUtil;String ^wstrvmName=gcnew String(wcvmName);String ^wstripAddress=gcnew String(wcipAddress);return azureUtil->CreateNextSelfImageVM(wstrvmName,wstripAddress);}
    所有函数均以bool值返回,如果创建失败会catch到出错,返回false。没有事物的概念,可能网卡创建成功,但是虚拟机创建失败,这些我都是进行手动管理的,在利用redis管理虚拟机信息后,只要保证数据不出错且网络不中断,基本上不会出错。

原创粉丝点击