Exemplo n.º 1
0
 public async Task SetFieldValueAsync <TFieldValue>(string entityId, Expression <Func <T, TFieldValue> > propertyExpression, TFieldValue value)
 {
     var redisKey      = PrimaryCacheKeyFormatter(entityId);
     var hashName      = GetPropertyName(propertyExpression);
     var serializedObj = JsonConvert.SerializeObject(value);
     await RedisDatabase.HashSetAsync(redisKey, hashName, serializedObj).ConfigureAwait(false);
 }
Exemplo n.º 2
0
        public async Task <HttpActionResult <DeploymentRequest, Deployment> > Post([FromBody] DeploymentRequest request) => await this.WithResponseContainer(
            request,
            async info =>
        {
            if (string.IsNullOrEmpty(request.Name))
            {
                throw new InternalHttpException("Deployment package name must be specified.", (int)HttpStatusCode.BadRequest, new { request });
            }

            if (string.IsNullOrEmpty(request.Version))
            {
                throw new InternalHttpException("Deployment package version must be specified.", (int)HttpStatusCode.BadRequest, new { request });
            }

            if (request.Agents == null || request.Agents.Count == 0)
            {
                throw new InternalHttpException("Agent list must be specified.", (int)HttpStatusCode.BadRequest, new { request });
            }

            IDeserializer deserializer = new DeserializerBuilder().Build();
            var yaml = deserializer.Deserialize <Dictionary <string, List <string> > >(new StringReader(request.Yaml));
            Dictionary <string, List <string> > dict = new Dictionary <string, List <string> >(yaml, StringComparer.InvariantCultureIgnoreCase)
                                                       .ToDictionary(kv => kv.Key, kv => kv.Value ?? new List <string>());
            var deployment = new Deployment
            {
                DeploymentPackage = new DeploymentPackage
                {
                    Name    = request.Name,
                    Version = request.Version
                },
                InstallCommands   = dict.ContainsKey(nameof(Deployment.InstallCommands)) ? dict[nameof(Deployment.InstallCommands)] : new List <string>(),
                StartCommands     = dict.ContainsKey(nameof(Deployment.StartCommands)) ? dict[nameof(Deployment.StartCommands)] : new List <string>(),
                StopCommands      = dict.ContainsKey(nameof(Deployment.StopCommands)) ? dict[nameof(Deployment.StopCommands)] : new List <string>(),
                UninstallCommands = dict.ContainsKey(nameof(Deployment.UninstallCommands)) ? dict[nameof(Deployment.UninstallCommands)] : new List <string>(),
                Timestamp         = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                Id = Guid.NewGuid().ToString()
            };

            if (deployment.InstallCommands.Count == 0 && deployment.StartCommands.Count == 0)
            {
                throw new InternalHttpException("Must specify at least one of install/start commands.", (int)HttpStatusCode.BadRequest, new { request });
            }

            if (deployment.StopCommands.Count == 0 && deployment.UninstallCommands.Count == 0)
            {
                throw new InternalHttpException("Must specify at least one of uninstall/stop commands.", (int)HttpStatusCode.BadRequest, new { request });
            }

            Dictionary <string, AgentInfo> agentsDict   = await RedisDatabase.HashGetAllAsync <AgentInfo>(CacheKeys.AgentInfo);
            Dictionary <string, Deployment> deployments = agentsDict.Values
                                                          .Where(i => i.Status == AgentStatus.Ready)
                                                          .Where(i => request.Agents.Contains(i.Id))
                                                          .Select(i => i.Id)
                                                          .ToDictionary(k => k, v => deployment);

            await RedisDatabase.HashSetAsync(CacheKeys.ActiveDeployments, deployments);
            return(deployment);
        });
Exemplo n.º 3
0
 public async Task SetDictionaryFieldValueAsync <TFieldValue>(string entityId, Expression <Func <T, object> > propertyExpression,
                                                              object dictionaryKey, TFieldValue value)
 {
     if (!FlattenDictionaries)
     {
         return;
     }
     var redisKey      = PrimaryCacheKeyFormatter(entityId);
     var propertyName  = GetPropertyName(propertyExpression);
     var serializedKey = JsonConvert.SerializeObject(dictionaryKey);
     var hashName      = DictionaryHashFieldNameFormatter(propertyName, serializedKey);
     var serializedObj = JsonConvert.SerializeObject(value);
     await RedisDatabase.HashSetAsync(redisKey, hashName, serializedObj).ConfigureAwait(false);
 }
Exemplo n.º 4
0
        public async Task SetAllAsync(T entity)
        {
            var hashEntries = new List <HashEntry>();
            var props       = typeof(T).GetProperties().ToList();

            foreach (var propertyInfo in props)
            {
                var hashFieldName  = propertyInfo.Name;
                var propInterfaces = propertyInfo.PropertyType.GetInterfaces();
                if (FlattenDictionaries && CanGetHashItemsFromCollection(propertyInfo, propInterfaces, hashFieldName, hashEntries, entity))
                {
                    continue;
                }
                var itemFieldValue = JsonConvert.SerializeObject(propertyInfo.GetValue(entity));
                var hashEntry      = new HashEntry(hashFieldName, itemFieldValue);
                hashEntries.Add(hashEntry);
            }
            await RedisDatabase.HashSetAsync(PrimaryCacheKeyFormatter(PrimaryEntityIdLocator(entity)), hashEntries.ToArray()).ConfigureAwait(false);
        }
Exemplo n.º 5
0
        public async Task <HttpActionResult <AgentInfo, DeploymentCommand> > Poll([FromBody] AgentInfo agentInfo) => await this.WithResponseContainer(
            agentInfo,
            async info =>
        {
            var command = new DeploymentCommand
            {
                Execute = true
            };

            string sourceIp = Request?.HttpContext?.Connection?.RemoteIpAddress?.ToString();
            long now        = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            var newInfo     = new AgentInfo
            {
                DeploymentPackage   = info.DeploymentPackage,
                ActiveDeploymentId  = info.ActiveDeploymentId,
                ReportedIpAddresses = info.ReportedIpAddresses,
                Status     = info.Status,
                IpAddress  = sourceIp,
                LastUpdate = now
            };
            await RedisDatabase.HashSetAsync(CacheKeys.AgentInfo, info.Id, newInfo);

            Deployment activeDeployment = await RedisDatabase.HashGetAsync <Deployment>(CacheKeys.ActiveDeployments, info.Id);
            command.Deployment          = activeDeployment;

            List <DeploymentResult> deploymentResults = await RedisDatabase.GetAsync <List <DeploymentResult> >(CacheKeys.DeploymentResults);
            List <string> successful = (deploymentResults ?? new List <DeploymentResult>())
                                       .Where(i => i != null)
                                       .Where(i => i.CommandResults.All(j => j.Success))
                                       .Select(i => i.AgentInfo.ActiveDeploymentId)
                                       .ToList();

            if (activeDeployment == null || successful.Contains(activeDeployment.Id) || agentInfo.ActiveDeploymentId == activeDeployment.Id)
            {
                command.Execute = false;
            }

            return(command);
        });