Merge pull request #12 from Azure/vnet-support

Support for Batch AAD authn.
This commit is contained in:
Christian 2018-02-01 21:28:46 +11:00 коммит произвёл GitHub
Родитель 2063f16a8f 37ebb009d4
Коммит b25ac3f79b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 141 добавлений и 64 удалений

Просмотреть файл

@ -3,25 +3,35 @@
<appSettings>
<add key="BatchAccount" value=""/>
<add key="BatchKey" value=""/>
<add key="BatchUrl" value=""/>
<add key="BatchUrl" value="https://{}.batch.azure.com"/>
<add key="AuthorityUri" value="https://login.microsoftonline.com/{}"/>
<add key="BatchResourceUri" value="https://batch.core.windows.net/"/>
<add key="ApplicationId" value=""/>
<add key="RedirectUri" value=""/>
<add key="RestrictToPublicIp" value="false"/>
<add key="MyPublicIp" value=""/>
<add key="PublicPortRange" value="20000:20099"/>
<add key="VRaySererPort" value="20207"/>
<!-- VNets/Subnets are mutually exclusive with Public Endpoints above -->
<!-- If VNet is set, public endpoints will not be created -->
<add key="SubnetResourceId" value="/subscriptions/{subscription}/resourcegroups/{resourceGroup}/providers/Microsoft.Network/virtualnetworks/{vnet}/subnets/{subnet}"/>
<add key="VirtualMachineCount" value="2"/>
<add key="UseLowPriority" value="false"/>
<add key="VirtualMachineSize" value="Standard_F16"/>
<add key="ImagePublisher" value="batch"/>
<add key="ImageOffer" value="rendering-windows2016"/>
<add key="ImageSku" value="rendering"/>
<add key="ImageVersion" value="latest"/>
<add key="NodeAgentSku" value="batch.node.windows amd64"/>
<add key="VRaySetupCommand" value="cmd.exe /c C:\Autodesk\MayaIO2017\vray\bin\vray.exe -server -portNumber=20207"/>
<add key="VRaySetupCommand" value="cmd.exe /c C:\Autodesk\MayaIO2017\vray\bin\vray.exe -server -portNumber={0}"/>
<add key="VRayDRConfigPath" value="Autodesk\3dsMax\2018 - 64bit\ENU\en-US\plugcfg"/>
<add key="VRayDRConfig" value="restart_slaves 0
list_in_scene 0
max_servers 0
@ -33,9 +43,9 @@ cache_limit 100.000000
"/>
<add key="VRayRTDRConfig" value="autostart_local_slave 0
"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>

Просмотреть файл

@ -9,6 +9,7 @@ using System.Threading.Tasks;
using Microsoft.Azure.Batch;
using Microsoft.Azure.Batch.Auth;
using Microsoft.Azure.Batch.Common;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
namespace VRayPoolManager
{
@ -18,11 +19,7 @@ namespace VRayPoolManager
static void Main(string[] args)
{
Client = BatchClient.Open(
new BatchSharedKeyCredentials(
ConfigurationManager.AppSettings["BatchUrl"],
ConfigurationManager.AppSettings["BatchAccount"],
ConfigurationManager.AppSettings["BatchKey"]));
Client = GetClient();
if (args.Length != 2)
{
@ -51,9 +48,38 @@ namespace VRayPoolManager
}
}
public static BatchClient GetClient()
{
if (string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["BatchAccount"]))
{
// AAD
Func<Task<string>> tokenProvider = () => GetAuthenticationTokenAsync();
return BatchClient.Open(new BatchTokenCredentials(ConfigurationManager.AppSettings["BatchUrl"], tokenProvider));
}
// Shared Key
return BatchClient.Open(
new BatchSharedKeyCredentials(
ConfigurationManager.AppSettings["BatchUrl"],
ConfigurationManager.AppSettings["BatchAccount"],
ConfigurationManager.AppSettings["BatchKey"]));
}
public static async Task<string> GetAuthenticationTokenAsync()
{
var authContext = new AuthenticationContext(ConfigurationManager.AppSettings["AuthorityUri"]);
// Acquire the authentication token from Azure AD.
var authResult = await authContext.AcquireTokenAsync(ConfigurationManager.AppSettings["BatchResourceUri"],
ConfigurationManager.AppSettings["ApplicationId"],
new Uri(ConfigurationManager.AppSettings["RedirectUri"]),
new PlatformParameters(PromptBehavior.Auto));
return authResult.AccessToken;
}
private static void CreatePool(string poolName)
{
var restrictToPublicIp = Boolean.Parse(ConfigurationManager.AppSettings["RestrictToPublicIp"]);
var vmSize = ConfigurationManager.AppSettings["VirtualMachineSize"];
var dedicatedVmCount = Int32.Parse(ConfigurationManager.AppSettings["VirtualMachineCount"]);
@ -79,28 +105,8 @@ namespace VRayPoolManager
dedicatedVmCount,
lowPriorityVmCount);
SetupPoolNetworking(pool);
NetworkSecurityGroupRule[] nsgRules = null;
if (restrictToPublicIp)
{
var publicIp = GetPublicIp();
nsgRules = new[]
{
new NetworkSecurityGroupRule(200, NetworkSecurityGroupRuleAccess.Allow, publicIp),
new NetworkSecurityGroupRule(201, NetworkSecurityGroupRuleAccess.Deny, "*"),
};
}
var portTuple = GetPublicPortRange();
var inboundNatPools = new List<InboundNatPool>
{
new InboundNatPool("VRay", InboundEndpointProtocol.Tcp, 20207, portTuple.Item1, portTuple.Item2, nsgRules)
};
pool.NetworkConfiguration = new NetworkConfiguration();
pool.NetworkConfiguration.EndpointConfiguration = new PoolEndpointConfiguration(inboundNatPools.AsReadOnly());
pool.InterComputeNodeCommunicationEnabled = true;
pool.ApplicationLicenses = new List<string> { "3dsmax", "vray" };
pool.Commit();
@ -113,7 +119,10 @@ namespace VRayPoolManager
var vmCount = Math.Max(lowPriorityVmCount, dedicatedVmCount);
var task = new CloudTask("setup-vray-dr", "dir");
task.MultiInstanceSettings = new MultiInstanceSettings(ConfigurationManager.AppSettings["VRaySetupCommand"], vmCount);
task.MultiInstanceSettings = new MultiInstanceSettings(
string.Format(ConfigurationManager.AppSettings["VRaySetupCommand"], ConfigurationManager.AppSettings["VRaySererPort"]),
vmCount);
task.Constraints = new TaskConstraints(maxTaskRetryCount: 3);
job.AddTask(task);
@ -151,6 +160,42 @@ namespace VRayPoolManager
Console.ReadLine();
}
private static void SetupPoolNetworking(CloudPool pool)
{
pool.NetworkConfiguration = new NetworkConfiguration();
pool.InterComputeNodeCommunicationEnabled = true;
if (string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["SubnetResourceId"]))
{
// If no VNet, setup inbound endpoints
var restrictToPublicIp = Boolean.Parse(ConfigurationManager.AppSettings["RestrictToPublicIp"]);
NetworkSecurityGroupRule[] nsgRules = null;
if (restrictToPublicIp)
{
var publicIp = GetPublicIp();
nsgRules = new[]
{
new NetworkSecurityGroupRule(200, NetworkSecurityGroupRuleAccess.Allow, publicIp),
new NetworkSecurityGroupRule(201, NetworkSecurityGroupRuleAccess.Deny, "*"),
};
}
var portTuple = GetPublicPortRange();
var inboundNatPools = new List<InboundNatPool>
{
new InboundNatPool("VRay", InboundEndpointProtocol.Tcp, 20207, portTuple.Item1, portTuple.Item2, nsgRules)
};
pool.NetworkConfiguration.EndpointConfiguration = new PoolEndpointConfiguration(inboundNatPools.AsReadOnly());
}
else
{
pool.NetworkConfiguration.SubnetId = ConfigurationManager.AppSettings["SubnetResourceId"];
}
}
private static void WriteVRayConfig(CloudPool pool)
{
var vrayConfig = Path.Combine(
@ -189,34 +234,26 @@ namespace VRayPoolManager
File.WriteAllText(vrayRtConfig, "");
foreach (var computeNode in pool.ListComputeNodes())
{
if (computeNode.EndpointConfiguration != null && computeNode.EndpointConfiguration.InboundEndpoints != null)
var computeNodeEntry = GetComputeNodeEntry(computeNode);
Console.WriteLine(" {0} {1}", computeNode.Id, computeNodeEntry);
if (vrayConfigContent.Contains(computeNodeEntry))
{
foreach (var endpoint in computeNode.EndpointConfiguration.InboundEndpoints)
{
if (endpoint.Name.StartsWith("VRay"))
{
Console.WriteLine(" {0} {1}:{2}", computeNode.Id, endpoint.PublicIPAddress, endpoint.FrontendPort);
var computeNodeEntry = string.Format("{0} 1 {1}\n", endpoint.PublicIPAddress, endpoint.FrontendPort);
if (vrayConfigContent.Contains(computeNodeEntry))
{
Console.WriteLine("Compute node {0} already exists in config file {1}", computeNodeEntry, vrayConfig);
}
else
{
File.AppendAllText(vrayConfig, computeNodeEntry);
}
if (vrayRtConfigContent.Contains(computeNodeEntry))
{
Console.WriteLine("Compute node {0} already exists in config file {1}", computeNodeEntry, vrayRtConfigContent);
}
else
{
File.AppendAllText(vrayRtConfig, computeNodeEntry);
}
}
}
Console.WriteLine("Compute node {0} already exists in config file {1}", computeNodeEntry, vrayConfig);
}
else
{
File.AppendAllText(vrayConfig, computeNodeEntry);
}
if (vrayRtConfigContent.Contains(computeNodeEntry))
{
Console.WriteLine("Compute node {0} already exists in config file {1}", computeNodeEntry, vrayRtConfigContent);
}
else
{
File.AppendAllText(vrayRtConfig, computeNodeEntry);
}
}
@ -227,6 +264,29 @@ namespace VRayPoolManager
Console.WriteLine("Updated VRayRT DR config file: " + vrayConfig);
}
private static string GetComputeNodeEntry(ComputeNode computeNode)
{
if (string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["SubnetResourceId"]))
{
// Get public endpoint
if (computeNode.EndpointConfiguration != null &&
computeNode.EndpointConfiguration.InboundEndpoints != null)
{
foreach (var endpoint in computeNode.EndpointConfiguration.InboundEndpoints)
{
if (endpoint.Name.StartsWith("VRay"))
{
Console.WriteLine(" {0} {1}:{2}", computeNode.Id, endpoint.PublicIPAddress,
endpoint.FrontendPort);
return string.Format("{0} 1 {1}\n", endpoint.PublicIPAddress, endpoint.FrontendPort);
}
}
}
}
return string.Format("{0} 1 {1}\n", computeNode.IPAddress, ConfigurationManager.AppSettings["VRaySererPort"]);
}
private static Tuple<int, int> GetPublicPortRange()
{
var portRangeSetting = ConfigurationManager.AppSettings["PublicPortRange"];
@ -241,7 +301,7 @@ namespace VRayPoolManager
var tokens = portRangeSetting.Split(':');
return Tuple.Create<int, int>(int.Parse(tokens[0]), int.Parse(tokens[1]));
}
catch (Exception e)
catch (Exception)
{
throw new Exception("Invalid port range specified: " + portRangeSetting + ", please specify a port range in the format 20000:20099");
}

Просмотреть файл

@ -35,6 +35,12 @@
<Reference Include="Microsoft.Azure.Batch, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Azure.Batch.8.0.1\lib\net452\Microsoft.Azure.Batch.dll</HintPath>
</Reference>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=3.19.1.3001, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.19.1\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
</Reference>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory.Platform, Version=3.19.1.3001, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.19.1\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Rest.ClientRuntime.2.3.10\lib\net452\Microsoft.Rest.ClientRuntime.dll</HintPath>
</Reference>

Просмотреть файл

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Azure.Batch" version="8.0.1" targetFramework="net452" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.19.1" targetFramework="net452" />
<package id="Microsoft.Rest.ClientRuntime" version="2.3.10" targetFramework="net452" />
<package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.10" targetFramework="net452" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net452" />