Exemple #1
0
        private async Task Deploy(Data.Gamespace gamespace)
        {
            var tasks = new List <Task <Vm> >();

            var spec = JsonSerializer.Deserialize <ChallengeSpec>(gamespace.Challenge ?? "{}", jsonOptions);

            var templates = Mapper.Map <List <ConvergedTemplate> >(gamespace.Workspace.Templates);

            foreach (var template in templates)
            {
                // apply template macro substitutions
                foreach (string key in spec.Randoms.Keys)
                {
                    template.Guestinfo = template.Guestinfo?.Replace(key, spec.Randoms[key]);
                    template.Detail    = template.Detail?.Replace(key, spec.Randoms[key]);
                }

                // TODO: add template replicas

                tasks.Add(
                    _pod.Deploy(
                        template.ToVirtualTemplate(gamespace.GlobalId)
                        )
                    );
            }

            await Task.WhenAll(tasks.ToArray());
        }
Exemple #2
0
        public async Task <ActionResult <Vm> > Deploy(int id)
        {
            VmTemplate template = await _templateService.GetDeployableTemplate(id, null);

            Vm vm = await _pod.Deploy(template);

            // SendBroadcast(vm, "deploy");
            VmState state = new VmState
            {
                Id        = id.ToString(),
                Name      = vm.Name.Untagged(),
                IsRunning = vm.State == VmPowerState.Running
            };

            await _hub.Clients.Group(vm.Name.Tag()).VmEvent(new BroadcastEvent <VmState>(User, "VM.DEPLOY", state));

            return(Ok(vm));
        }
Exemple #3
0
        private async Task <GameState> Deploy(Data.Gamespace gamespace, GamespaceSpec spec)
        {
            var deployTasks = new List <Task <Vm> >();

            try
            {
                var templates = String.IsNullOrEmpty(spec.Templates)
                    ? Mapper.Map <List <ConvergedTemplate> >(gamespace.Workspace.Templates)
                    : JsonSerializer.Deserialize <List <ConvergedTemplate> >(spec.Templates);

                ApplyIso(templates, spec);

                ExpandTemplates(templates, spec);

                // await AddNetworkServer(templates, spec);

                foreach (var template in templates)
                {
                    var virtualTemplate = template.ToVirtualTemplate(gamespace.GlobalId);

                    if (spec.HostAffinity)
                    {
                        virtualTemplate.AutoStart = false;
                    }

                    deployTasks.Add(_pod.Deploy(virtualTemplate));
                }

                Task.WaitAll(deployTasks.ToArray());

                if (spec.HostAffinity)
                {
                    await _pod.SetAffinity(gamespace.GlobalId, deployTasks.Select(t => t.Result).ToArray(), true);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error deploying Engine mojo");

                throw ex;
            }

            return(await LoadState(gamespace, gamespace.WorkspaceId));
        }
Exemple #4
0
        private async Task <GameState> Deploy(Data.Gamespace gamespace)
        {
            var tasks = new List <Task <Vm> >();

            var templates = Mapper.Map <List <ConvergedTemplate> >(gamespace.Workspace.Templates);

            foreach (var template in templates)
            {
                tasks.Add(
                    _pod.Deploy(
                        template.ToVirtualTemplate(gamespace.GlobalId)
                        )
                    );
            }

            Task.WaitAll(tasks.ToArray());

            return(await LoadState(gamespace, gamespace.WorkspaceId));
        }
Exemple #5
0
        public async Task <ActionResult <Vm> > DeployVm(string id)
        {
            VmTemplate template = await _templateService
                                  .GetDeployableTemplate(id, null)
            ;

            string name = $"{template.Name}#{template.IsolationTag}";

            AuthorizeAny(
                () => Actor.IsAdmin,
                () => CanManageVm(name, Actor.Id).Result
                );

            Vm vm = await _pod.Deploy(template, Actor.IsBuilder);

            if (template.HostAffinity)
            {
                await _pod.SetAffinity(
                    template.IsolationTag,
                    new Vm[] { vm },
                    true
                    );

                vm.State = VmPowerState.Running;
            }

            // SendBroadcast(vm, "deploy");
            VmState state = new VmState
            {
                Id          = template.Id.ToString(),
                Name        = vm.Name.Untagged(),
                IsolationId = vm.Name.Tag(),
                IsRunning   = vm.State == VmPowerState.Running
            };

            await Hub.Clients
            .Group(state.IsolationId)
            .VmEvent(new BroadcastEvent <VmState>(User, "VM.DEPLOY", state))
            ;

            return(Ok(vm));
        }
        private async Task Deploy(TopoMojo.Api.Data.Gamespace gamespace, bool sudo = false)
        {
            var tasks = new List <Task <Vm> >();

            var spec = JsonSerializer.Deserialize <ChallengeSpec>(gamespace.Challenge ?? "{}", jsonOptions);

            var isoTargets = (spec.Challenge?.Iso?.Targets ?? "")
                             .ToLower()
                             .Split(' ', StringSplitOptions.RemoveEmptyEntries);

            var templates = Mapper.Map <List <ConvergedTemplate> >(
                gamespace.Workspace.Templates
                .Where(t => t.Variant == 0 || (t.Variant - 1) == gamespace.Variant)
                );

            foreach (var template in templates.ToList())
            {
                // normalize name for variant-specific templates
                if (template.Variant > 0 && template.Name.EndsWith($"_v{template.Variant}"))
                {
                    template.Name = template.Name.Substring(0, template.Name.LastIndexOf('_'));
                }

                // apply template macro substitutions
                foreach (var kvp in spec.Transforms)
                {
                    template.Guestinfo = template.Guestinfo?.Replace($"##{kvp.Key}##", kvp.Value);
                    template.Detail    = template.Detail?.Replace($"##{kvp.Key}##", kvp.Value);
                }

                // apply challenge iso
                if (string.IsNullOrEmpty(template.Iso) && isoTargets.Contains(template.Name.ToLower()))
                {
                    template.Iso = $"{spec.Challenge.Iso.File}";
                }

                // expand replicas
                int replicas = template.Replicas < 0
                    ? gamespace.PlayerCount
                    : Math.Min(template.Replicas, _options.ReplicaLimit)
                ;

                if (replicas > 1)
                {
                    for (int i = 1; i < replicas; i++)
                    {
                        var tt = template.Clone <ConvergedTemplate>();

                        tt.Name += $"_{i+1}";

                        templates.Add(tt);
                    }

                    template.Name += "_1";
                }
            }

            foreach (var template in templates)
            {
                tasks.Add(
                    _pod.Deploy(
                        template
                        .ToVirtualTemplate(gamespace.Id)
                        .SetHostAffinity(gamespace.Workspace.HostAffinity),
                        sudo
                        )
                    );
            }

            await Task.WhenAll(tasks.ToArray());

            if (gamespace.Workspace.HostAffinity)
            {
                var vms = tasks.Select(t => t.Result).ToArray();

                await _pod.SetAffinity(gamespace.Id, vms, true);

                foreach (var vm in vms)
                {
                    vm.State = VmPowerState.Running;
                }
            }

            if (gamespace.StartTime.Year <= 1)
            {
                gamespace.StartTime = DateTimeOffset.UtcNow;
                await _store.Update(gamespace);
            }
        }