Skip to content

Instantly share code, notes, and snippets.

@bjorkstromm
Created September 27, 2016 21:41
Show Gist options
  • Save bjorkstromm/ba5a66f59e324eb26662a007a15b642a to your computer and use it in GitHub Desktop.
Save bjorkstromm/ba5a66f59e324eb26662a007a15b642a to your computer and use it in GitHub Desktop.
Service Fabric Application Management (Managed)
private static async Task RemoveApplicationAsync(
FabricClient client,
Uri applicationName, //<.. Read from AppParameters
string applicationTypeName, //<-- Should be read from ApplicationManifest
string applicationTypeVersion //<-- Should be read from ApplicationManifest
)
{
await client.ApplicationManager.DeleteApplicationAsync(applicationName);
foreach(var node in await client.QueryManager.GetNodeListAsync())
{
foreach(var replica in await client.QueryManager.GetDeployedReplicaListAsync(node.NodeName, applicationName))
{
var partitionSelector = PartitionSelector.PartitionIdOf(applicationName, replica.Partitionid);
var replicaSelector = ReplicaSelector.ReplicaIdOf(partitionSelector, 0L);
await client.FaultManager.RemoveReplicaAsync(replicaSelector, CompletionMode.Verify, true);
}
}
await client.ApplicationManager.UnprovisionApplicationAsync(applicationTypeName, applicationTypeVersion);
}
private static async Task PublishApplicationAsync(
FabricClient client,
string path,
Uri applicationName, //<.. Read from AppParameters
Dictionary<string, string> applicationParameters, //<-- Read from AppParameters
string applicationTypeName, //<-- Should be read from ApplicationManifest
string applicationTypeVersion //<-- Should be read from ApplicationManifest
)
{
if(!Directory.Exists(path))
{
throw new ArgumentException("path does not exist", nameof(path));
}
var app = await client.QueryManager.GetApplicationListAsync(applicationName);
if (app.Any())
{
// Check also typename and version
throw new ArgumentException("Application already exists");
}
var appType = await client.QueryManager.GetApplicationTypeListAsync(applicationTypeName);
if(appType.Any(a => a.ApplicationTypeVersion == applicationTypeVersion))
{
throw new ArgumentException("ApplicationType already exists");
}
var imageStoreConnectionString = await GetImageStoreConnectionStringAsync(client);
client.ApplicationManager.CopyApplicationPackage(imageStoreConnectionString, path, applicationTypeName);
await client.ApplicationManager.ProvisionApplicationAsync(applicationTypeName);
client.ApplicationManager.RemoveApplicationPackage(imageStoreConnectionString, applicationTypeName);
var applicationDescription = new ApplicationDescription
{
ApplicationName = applicationName,
ApplicationTypeName = applicationTypeName,
ApplicationTypeVersion = applicationTypeVersion
};
foreach(var param in applicationParameters)
{
applicationDescription.ApplicationParameters.Add(param.Key, param.Value);
}
await client.ApplicationManager.CreateApplicationAsync(applicationDescription);
}
private static async Task<string> GetImageStoreConnectionStringAsync(FabricClient client)
{
var manifest = await client.ClusterManager.GetClusterManifestAsync();
var document = new XmlDocument();
document.LoadXml(manifest);
var namespaces = new XmlNamespaceManager(document.NameTable);
namespaces.AddNamespace("sf", "http://schemas.microsoft.com/2011/01/fabric");
return document.SelectSingleNode(@"//sf:Section[@Name='Management']/sf:Parameter[@Name='ImageStoreConnectionString']/@Value", namespaces).Value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment