Beispiel #1
0
        public static async Task <bool> WaitForRunToBeAppliedAsync(
            ImplementationEntity implementationEntity,
            CasterApiClient casterApiClient,
            int loopIntervalSeconds,
            int maxWaitMinutes,
            CancellationToken ct)
        {
            if (implementationEntity.RunId == null)
            {
                return(false);
            }
            var endTime = DateTime.UtcNow.AddMinutes(maxWaitMinutes);
            var status  = "Applying";

            while (status == "Applying" && DateTime.UtcNow < endTime)
            {
                var casterRun = await casterApiClient.GetRunAsync((Guid)implementationEntity.RunId);

                status = casterRun.Status;
                // if not there yet, pause before the next check
                if (status == "Applying")
                {
                    Thread.Sleep(TimeSpan.FromSeconds(loopIntervalSeconds));
                }
            }
            if (status == "Applied")
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
 public void Add(ImplementationEntity implementationEntity)
 {
     if (implementationEntity == null)
     {
         throw new ArgumentNullException(nameof(implementationEntity));
     }
     _implementationQueue.Add(implementationEntity);
 }
Beispiel #3
0
        public static async Task <bool> DeleteCasterWorkspaceAsync(ImplementationEntity implementationEntity,
                                                                   CasterApiClient casterApiClient, TokenResponse tokenResponse, CancellationToken ct)
        {
            try
            {
                await casterApiClient.DeleteWorkspaceAsync((Guid)implementationEntity.WorkspaceId, ct);

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Beispiel #4
0
        public static async Task <bool> ApplyRunAsync(
            ImplementationEntity implementationEntity,
            CasterApiClient casterApiClient,
            CancellationToken ct)
        {
            var initialInternalStatus = implementationEntity.InternalStatus;

            // if status is Planned or Applying
            try
            {
                await casterApiClient.ApplyRunAsync((Guid)implementationEntity.RunId, ct);

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
        private async Task <ImplementationEntity> CreateImplementationEntityAsync(Guid definitionId, CancellationToken ct)
        {
            _logger.LogInformation($"For Definition {definitionId}, Create Implementation.");
            var userId               = _user.GetId();
            var username             = _user.Claims.First(c => c.Type.ToLower() == "name").Value;
            var implementationEntity = new ImplementationEntity()
            {
                CreatedBy      = userId,
                UserId         = userId,
                Username       = username,
                DefinitionId   = definitionId,
                Status         = ImplementationStatus.Creating,
                InternalStatus = InternalImplementationStatus.LaunchQueued
            };

            _context.Implementations.Add(implementationEntity);
            await _context.SaveChangesAsync(ct);

            _logger.LogInformation($"Implementation {implementationEntity.Id} created for Definition {definitionId}.");

            return(implementationEntity);
        }
Beispiel #6
0
        public static async Task <string> GetCasterVarsFileContentAsync(ImplementationEntity implementationEntity, S3PlayerApiClient playerApiClient, CancellationToken ct)
        {
            try
            {
                var varsFileContent = "";
                var exercise        = (await playerApiClient.GetExerciseAsync((Guid)implementationEntity.ExerciseId, ct)) as S3.Player.Api.Models.Exercise;
                varsFileContent = $"exercise_id = \"{exercise.Id}\"\r\nuser_id = \"{implementationEntity.UserId}\"\r\nusername = \"{implementationEntity.Username}\"\r\n";
                var teams = (await playerApiClient.GetExerciseTeamsAsync((Guid)exercise.Id, ct)) as IEnumerable <Team>;

                foreach (var team in teams)
                {
                    var cleanTeamName = Regex.Replace(team.Name.ToLower().Replace(" ", "_"), "[@&'(\\s)<>#]", "", RegexOptions.None);
                    varsFileContent += $"{cleanTeamName} = \"{team.Id}\"\r\n";
                }

                return(varsFileContent);
            }
            catch (Exception ex)
            {
                return("");
            }
        }
Beispiel #7
0
        public static async Task <Guid?> CreateRunAsync(
            ImplementationEntity implementationEntity,
            CasterApiClient casterApiClient,
            bool isDestroy,
            CancellationToken ct)
        {
            var runCommand = new CreateRunCommand()
            {
                WorkspaceId = implementationEntity.WorkspaceId,
                IsDestroy   = isDestroy
            };

            try
            {
                var casterRun = await casterApiClient.CreateRunAsync(runCommand, ct);

                return(casterRun.Id);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Beispiel #8
0
        public static async Task <Guid?> CreatePlayerExerciseAsync(S3PlayerApiClient playerApiClient, ImplementationEntity implementationEntity, Guid parentExerciseId, CancellationToken ct)
        {
            try
            {
                var exercise = (await playerApiClient.CloneExerciseAsync(parentExerciseId, ct)) as S3.Player.Api.Models.Exercise;
                exercise.Name = $"{exercise.Name.Replace("Clone of ", "")} - {implementationEntity.Username}";
                await playerApiClient.UpdateExerciseAsync((Guid)exercise.Id, exercise, ct);

                // add user to first non-admin team
                var roles = await playerApiClient.GetRolesAsync(ct) as IEnumerable <Role>;

                var teams = (await playerApiClient.GetExerciseTeamsAsync((Guid)exercise.Id, ct)) as IEnumerable <Team>;
                foreach (var team in teams)
                {
                    if (team.Permissions.Where(p => p.Key == "ExerciseAdmin").Any())
                    {
                        continue;
                    }

                    if (team.RoleId.HasValue)
                    {
                        var role = roles.Where(r => r.Id == team.RoleId).FirstOrDefault();

                        if (role != null && role.Permissions.Where(p => p.Key == "ExerciseAdmin").Any())
                        {
                            continue;
                        }
                    }

                    await playerApiClient.AddUserToTeamAsync(team.Id.Value, implementationEntity.UserId, ct);
                }
                return(exercise.Id);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Beispiel #9
0
        public static async Task <Session> CreateSteamfitterSessionAsync(SteamfitterApiClient steamfitterApiClient, ImplementationEntity implementationEntity, Guid scenarioId, CancellationToken ct)
        {
            try
            {
                var session = await steamfitterApiClient.CreateSessionFromScenarioAsync(scenarioId, ct);

                session.Name       = $"{session.Name.Replace("From Scenario ", "")} - {implementationEntity.Username}";
                session.ExerciseId = implementationEntity.ExerciseId;
                session            = await steamfitterApiClient.UpdateSessionAsync((Guid)session.Id, session, ct);

                return(session);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Beispiel #10
0
        public static async Task <Guid?> CreateCasterWorkspaceAsync(CasterApiClient casterApiClient, ImplementationEntity implementationEntity, Guid directoryId, string varsFileContent, CancellationToken ct)
        {
            try
            {
                // remove special characters from the user name, use lower case and replace spaces with underscores
                var userName = Regex.Replace(implementationEntity.Username.ToLower().Replace(" ", "_"), "[@&'(\\s)<>#]", "", RegexOptions.None);
                // create the new workspace
                var workspaceCommand = new CreateWorkspaceCommand()
                {
                    Name        = $"{userName}-{implementationEntity.UserId.ToString()}",
                    DirectoryId = directoryId,
                    DynamicHost = true
                };
                var workspaceId = (await casterApiClient.CreateWorkspaceAsync(workspaceCommand, ct)).Id;
                // create the workspace variable file
                var createFileCommand = new CreateFileCommand()
                {
                    Name        = $"{workspaceCommand.Name}.auto.tfvars",
                    DirectoryId = directoryId,
                    WorkspaceId = workspaceId,
                    Content     = varsFileContent
                };
                await casterApiClient.CreateFileAsync(createFileCommand, ct);

                return(workspaceId);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }