Ejemplo n.º 1
0
    /**
     * Retrieve the data from a JsonData and create a GameData with the info
     */
    private static GameData GetGameDataFromJsonData(JsonData jsonData)
    {
        PersistenceType type = PersistenceType.gameData;
        GameData        gd   = null;

        if (jsonData[type.ToString()].Count == 1)
        {
            gd = new GameData();
            gd.currentLevel = Convert.ToInt32(jsonData[type.ToString()]["currentLevel"].ToString());
        }
        return(gd);
    }
Ejemplo n.º 2
0
        private async Task <IAppServiceCertificate> GetExistingCertificateAsync(PersistenceType persistenceType)
        {
            if (persistenceType != PersistenceType.Site)
            {
                logger.LogTrace("Skipping certificate retrieval of a certificate of type {0}, which can't be persisted in Azure.", persistenceType);
                return(null);
            }

            var certificates = await client.WebApps.Manager
                               .AppServiceCertificates
                               .ListByResourceGroupAsync(azureOptions.ResourceGroupName);

            logger.LogInformation("Trying to find existing Azure certificate with key {0}.", persistenceType);

            foreach (var certificate in certificates)
            {
                var tags = certificate.Tags;
                if (!tags.ContainsKey(TagName) || tags[TagName] != persistenceType.ToString())
                {
                    continue;
                }

                return(certificate);
            }

            logger.LogInformation("Could not find existing Azure certificate.");

            return(null);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Create a request for a new Snapshot job
 /// </summary>
 /// <param name="targetStoreName">The name of the store that will be created to hold the snapshot</param>
 /// <param name="persistenceType">The type of persistence to use for the snapshot store</param>
 /// <param name="label">A user-friendly label for the job</param>
 /// <returns>A new <see cref="JobRequestObject"/></returns>
 public static JobRequestObject CreateSnapshotJob(string targetStoreName, PersistenceType persistenceType,
                                                  string label = null)
 {
     return(new JobRequestObject("CreateSnapshot",
                                 new Dictionary <string, string>
     {
         { "TargetStoreName", targetStoreName },
         { "PersistenceType", persistenceType.ToString() },
     },
                                 label));
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Create a request for a new Snapshot job
 /// </summary>
 /// <param name="targetStoreName">The name of the store that will be create to hold the snapshot</param>
 /// <param name="persistenceType">The type of persitence to use for the store created by the snapshot</param>
 /// <param name="commitPointId">The ID of the commit point to copy data from</param>
 /// <param name="label">A user-friendly label for the job</param>
 /// <returns>A new <see cref="JobRequestObject"/>.</returns>
 public static JobRequestObject CreateSnapshotJob(string targetStoreName, PersistenceType persistenceType,
                                                  ulong commitPointId, string label = null)
 {
     return(new JobRequestObject("CreateSnapshot",
                                 new Dictionary <string, string>
     {
         { "TargetStoreName", targetStoreName },
         { "PersistenceType", persistenceType.ToString() },
         { "CommitId", commitPointId.ToString(CultureInfo.InvariantCulture) }
     },
                                 label));
 }
Ejemplo n.º 5
0
    /**
     * Retrieve the data from a JsonData and create an Item list with the info
     */
    private static List <Item> GetItemFromJsonData(JsonData jsonData)
    {
        PersistenceType type  = PersistenceType.itens;
        Item            item  = null;
        List <Item>     itens = new List <Item>();

        for (int i = 0; i < jsonData[type.ToString()].Count; i++)
        {
            item             = new Item();
            item.id          = Convert.ToInt32(jsonData[type.ToString()][i]["id"].ToString());
            item.name        = jsonData[type.ToString()][i]["name"].ToString();
            item.description = jsonData[type.ToString()][i]["description"].ToString();
            item.amount      = Convert.ToInt32(jsonData[type.ToString()][i]["amount"].ToString());
            item.spritePos   = Convert.ToInt32(jsonData[type.ToString()][i]["spritePos"].ToString());
            int x = Convert.ToInt32(jsonData[type.ToString()][i]["coords"][0].ToString());
            int y = Convert.ToInt32(jsonData[type.ToString()][i]["coords"][1].ToString());
            item.coords = new int[] { x, y };
            itens.Add(item);
        }
        return(itens);
    }
Ejemplo n.º 6
0
 public override string ToString()
 {
     return(dataTag + " " + persistenceType.ToString());
 }
Ejemplo n.º 7
0
        public async Task PersistAsync(PersistenceType persistenceType, byte[] bytes)
        {
            if (bytes.Length == 0)
            {
                logger.LogWarning("Tried to persist empty certificate.");
                return;
            }

            if (persistenceType != PersistenceType.Site)
            {
                logger.LogTrace("Skipping certificate persistence because a certificate of type {0} can't be persisted in Azure.", persistenceType);
                return;
            }

            var domains = letsEncryptOptions.Domains.ToArray();

            logger.LogInformation("Creating new Azure certificate for key {0} and domains {1}.", persistenceType, domains);

            var apps = await client.WebApps.ListByResourceGroupAsync(azureOptions.ResourceGroupName);

            string regionName = null;

            var relevantApps = new HashSet <(IWebApp App, IDeploymentSlot Slot)>();

            foreach (var app in apps)
            {
                logger.LogTrace("Checking hostnames of app {0} ({1}) against domains {2}.", app.Name, app.HostNames, domains);

                if (azureOptions.Slot == null)
                {
                    if (!app.HostNames.Any(domains.Contains))
                    {
                        continue;
                    }

                    relevantApps.Add((app, null));
                }
                else
                {
                    var slots = app.DeploymentSlots
                                .List()
                                .Where(x => x
                                       .HostNames
                                       .Any(domains.Contains));
                    if (!slots.Any())
                    {
                        continue;
                    }

                    foreach (var slot in slots)
                    {
                        relevantApps.Add((app, slot));
                    }
                }

                regionName = app.RegionName;
            }

            if (regionName == null)
            {
                throw new InvalidOperationException("Could not find an app that has a hostname created for domains " + domains + ".");
            }

            var azureCertificate = await GetExistingCertificateAsync(persistenceType);

            if (azureCertificate != null)
            {
                logger.LogInformation("Updating existing Azure certificate for key {0}.", persistenceType);
                await client.WebApps.Manager
                .AppServiceCertificates
                .Inner
                .UpdateAsync(
                    azureOptions.ResourceGroupName,
                    azureCertificate.Name,
                    new CertificatePatchResource()
                {
                    Password  = nameof(FluffySpoon),
                    HostNames = domains,
                    PfxBlob   = bytes
                });

                azureCertificate = await GetExistingCertificateAsync(persistenceType);
            }
            else
            {
                logger.LogInformation("Found region name to use: {0}.", regionName);

                var certificateName = TagName + "_" + Guid.NewGuid();
                azureCertificate = await client.WebApps.Manager
                                   .AppServiceCertificates
                                   .Define(certificateName)
                                   .WithRegion(regionName)
                                   .WithExistingResourceGroup(azureOptions.ResourceGroupName)
                                   .WithPfxByteArray(bytes)
                                   .WithPfxPassword(nameof(FluffySpoon))
                                   .CreateAsync();

                var tags = new Dictionary <string, string>();
                foreach (var tag in azureCertificate.Tags)
                {
                    tags.Add(tag.Key, tag.Value);
                }

                tags.Add(TagName, persistenceType.ToString());

                logger.LogInformation("Updating tags: {0}.", tags);

                await client.WebApps.Manager
                .AppServiceCertificates
                .Inner
                .UpdateAsync(
                    azureOptions.ResourceGroupName,
                    azureCertificate.Name,
                    new CertificatePatchResource()
                {
                    Tags = tags
                });
            }

            foreach (var appTuple in relevantApps)
            {
                string[] domainsToUpgrade;
                if (azureOptions.Slot == null)
                {
                    logger.LogInformation("Updating host name bindings for app {0}", appTuple.App.Name);
                    domainsToUpgrade = appTuple
                                       .App
                                       .HostNames
                                       .Where(domains.Contains)
                                       .ToArray();
                }
                else
                {
                    logger.LogInformation("Updating host name bindings for app {0}/{1}", appTuple.App.Name, appTuple.Slot.Name);
                    domainsToUpgrade = appTuple
                                       .Slot
                                       .HostNames
                                       .Where(domains.Contains)
                                       .ToArray();
                }

                foreach (var domain in domainsToUpgrade)
                {
                    logger.LogDebug("Updating host name bindings for domain {0}", domain);

                    if (azureOptions.Slot == null)
                    {
                        await client.WebApps.Inner.CreateOrUpdateHostNameBindingWithHttpMessagesAsync(
                            azureOptions.ResourceGroupName,
                            appTuple.App.Name,
                            domain,
                            new HostNameBindingInner(
                                azureResourceType : AzureResourceType.Website,
                                hostNameType : HostNameType.Verified,
                                customHostNameDnsRecordType : CustomHostNameDnsRecordType.CName,
                                sslState : SslState.SniEnabled,
                                thumbprint : azureCertificate.Thumbprint));
                    }
                    else
                    {
                        await client.WebApps.Inner.CreateOrUpdateHostNameBindingSlotWithHttpMessagesAsync(
                            azureOptions.ResourceGroupName,
                            appTuple.App.Name,
                            domain,
                            new HostNameBindingInner(
                                azureResourceType : AzureResourceType.Website,
                                hostNameType : HostNameType.Verified,
                                customHostNameDnsRecordType : CustomHostNameDnsRecordType.CName,
                                sslState : SslState.SniEnabled,
                                thumbprint : azureCertificate.Thumbprint),
                            appTuple.Slot.Name);
                    }
                }
            }
        }
Ejemplo n.º 8
0
 private string GetCertificatePath(PersistenceType persistenceType)
 {
     return(relativeFilePath + "_" + persistenceType.ToString());
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Create a request for a new Snapshot job
 /// </summary>
 /// <param name="targetStoreName">The name of the store that will be create to hold the snapshot</param>
 /// <param name="persistenceType">The type of persitence to use for the store created by the snapshot</param>
 /// <param name="commitPointId">The ID of the commit point to copy data from</param>
 /// <param name="label">A user-friendly label for the job</param>
 /// <returns>A new <see cref="JobRequestObject"/>.</returns>
 public static JobRequestObject CreateSnapshotJob(string targetStoreName, PersistenceType persistenceType,
                                                  ulong commitPointId, string label = null)
 {
     return new JobRequestObject("CreateSnapshot",
                                 new Dictionary<string, string>
                                     {
                                         {"TargetStoreName", targetStoreName},
                                         {"PersistenceType", persistenceType.ToString()},
                                         {"CommitId", commitPointId.ToString(CultureInfo.InvariantCulture)}
                                     },
                                 label);
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Create a request for a new Snapshot job
 /// </summary>
 /// <param name="targetStoreName">The name of the store that will be created to hold the snapshot</param>
 /// <param name="persistenceType">The type of persistence to use for the snapshot store</param>
 /// <param name="label">A user-friendly label for the job</param>
 /// <returns>A new <see cref="JobRequestObject"/></returns>
 public static JobRequestObject CreateSnapshotJob(string targetStoreName, PersistenceType persistenceType,
                                                  string label = null)
 {
     return new JobRequestObject("CreateSnapshot",
                                 new Dictionary<string, string>
                                     {
                                         {"TargetStoreName", targetStoreName},
                                         {"PersistenceType", persistenceType.ToString()},
                                     },
                                 label);
 }