Skip to content

Commit

Permalink
Merge pull request #321 from microsoft/dev
Browse files Browse the repository at this point in the history
Merge dev to main
  • Loading branch information
markusheiliger authored Feb 10, 2022
2 parents 4d3fe83 + 9691153 commit d0b6e8b
Show file tree
Hide file tree
Showing 8 changed files with 455 additions and 297 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"namespace": "teamcloud",
"configurationSource": {
"file": "file"
}
"source": "file",
"file" : ""
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"namespace": "teamcloud",
"configurationSource": {
"yaml": "yaml"
}
"source": "yaml",
"yaml" : ""
}
28 changes: 28 additions & 0 deletions src/TeamCloud.Azure.Resources/Typed/AzureContainerGroupResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,34 @@ private async Task<IContainerGroup> GetGroupAsync()
.ConfigureAwait(false);
}

public async Task<string> GetStateAsync()
{
var group = await containerGroup
.ConfigureAwait(false);

return group.State;
}

public async Task StopAsync()
{
var group = await containerGroup
.ConfigureAwait(false);

await group
.StopAsync()
.ConfigureAwait(false);
}

public async Task RestartAsync()
{
var group = await containerGroup
.ConfigureAwait(false);

await group
.RestartAsync()
.ConfigureAwait(false);
}

public async Task<string> GetLogContentAsync(string containerName)
{
if (string.IsNullOrWhiteSpace(containerName))
Expand Down
2 changes: 2 additions & 0 deletions src/TeamCloud.Model/Data/ComponentTaskRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ public class ComponentTaskRunner
{
public string Id { get; set; }

public bool WebServer { get; set; }

public Dictionary<string, string> With { get; set; } = new Dictionary<string, string>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,58 +41,58 @@ public async Task<ComponentTask> Run(
if (context is null)
throw new ArgumentNullException(nameof(context));

var componentDeployment = context.GetInput<Input>().ComponentTask;
var componentTask = context.GetInput<Input>().ComponentTask;

try
{

if (AzureResourceIdentifier.TryParse(componentDeployment.ResourceId, out var resourceId)
if (AzureResourceIdentifier.TryParse(componentTask.ResourceId, out var resourceId)
&& await azureResourceService.ExistsResourceAsync(resourceId.ToString()).ConfigureAwait(false))
{
var session = await azureResourceService.AzureSessionService
.CreateSessionAsync(resourceId.SubscriptionId)
.ConfigureAwait(false);

var runner = await session.ContainerGroups
var group = await session.ContainerGroups
.GetByIdAsync(resourceId.ToString())
.ConfigureAwait(false);

var container = runner.Containers
.SingleOrDefault()
var runner = group.Containers
.SingleOrDefault(c => c.Key.Equals(componentTask.Id, StringComparison.OrdinalIgnoreCase))
.Value;

if (container?.InstanceView is null)
if (runner?.InstanceView is null)
{
componentDeployment.TaskState = TaskState.Initializing;
componentTask.TaskState = TaskState.Initializing;
}
else if (container.InstanceView.CurrentState is not null)
else if (runner.InstanceView.CurrentState is not null)
{
componentDeployment.TaskState = TaskState.Processing;
componentDeployment.ExitCode = container.InstanceView.CurrentState.ExitCode;
componentDeployment.Started = container.InstanceView.CurrentState.StartTime;
componentDeployment.Finished = container.InstanceView.CurrentState.FinishTime;
componentTask.TaskState = TaskState.Processing;
componentTask.ExitCode = runner.InstanceView.CurrentState.ExitCode;
componentTask.Started = runner.InstanceView.CurrentState.StartTime;
componentTask.Finished = runner.InstanceView.CurrentState.FinishTime;

if (componentDeployment.ExitCode.HasValue)
if (componentTask.ExitCode.HasValue)
{
componentDeployment.TaskState = componentDeployment.ExitCode == 0
componentTask.TaskState = componentTask.ExitCode == 0
? TaskState.Succeeded // ExitCode indicates successful provisioning
: TaskState.Failed; // ExitCode indicates failed provisioning
}
else if (container.InstanceView.CurrentState.State?.Equals("Terminated", StringComparison.OrdinalIgnoreCase) ?? false)
else if (runner.InstanceView.CurrentState.State?.Equals("Terminated", StringComparison.OrdinalIgnoreCase) ?? false)
{
// container instance was terminated without exit code
componentDeployment.TaskState = TaskState.Failed;
componentTask.TaskState = TaskState.Failed;
}

if (componentDeployment.TaskState == TaskState.Failed && !componentDeployment.ExitCode.HasValue)
if (componentTask.TaskState == TaskState.Failed && !componentTask.ExitCode.HasValue)
{
var output = new StringBuilder();

output.AppendLine($"Creating runner {runner.Id} ended in state {runner.State} !!! {Environment.NewLine}");
output.AppendLine(await runner.GetLogContentAsync(container.Name).ConfigureAwait(false));
output.AppendLine($"Creating runner {group.Id} ended in state {group.State} !!! {Environment.NewLine}");
output.AppendLine(await group.GetLogContentAsync(runner.Name).ConfigureAwait(false));

var project = await projectRepository
.GetAsync(componentDeployment.Organization, componentDeployment.ProjectId)
.GetAsync(componentTask.Organization, componentTask.ProjectId)
.ConfigureAwait(false);

if (AzureResourceIdentifier.TryParse(project?.StorageId, out var storageId))
Expand All @@ -106,13 +106,13 @@ public async Task<ComponentTask> Run(
var outputDirectory = ".output";

var fileClient = await storage
.CreateShareFileClientAsync(componentDeployment.ComponentId, $"{outputDirectory}/{componentDeployment.Id}")
.CreateShareFileClientAsync(componentTask.ComponentId, $"{outputDirectory}/{componentTask.Id}")
.ConfigureAwait(false);

if (!await fileClient.ExistsAsync().ConfigureAwait(false))
{
await storage
.EnsureDirectoryPathAsync(componentDeployment.ComponentId, outputDirectory)
.EnsureDirectoryPathAsync(componentTask.ComponentId, outputDirectory)
.ConfigureAwait(false);

var logBuffer = Encoding.Default.GetBytes(output.ToString());
Expand All @@ -130,8 +130,8 @@ await fileStream
}
}

componentDeployment = await componentTaskRepository
.SetAsync(componentDeployment)
componentTask = await componentTaskRepository
.SetAsync(componentTask)
.ConfigureAwait(false);
}
}
Expand All @@ -140,7 +140,7 @@ await fileStream
throw exc.AsSerializable();
}

return componentDeployment;
return componentTask;
}

internal struct Input
Expand Down
Loading

0 comments on commit d0b6e8b

Please sign in to comment.