public async Task LoadExistingApps() { await _lock.WaitAsync(); try { var jsonBody = await GetContainerGroups(); JArray valueArray = jsonBody.value; var publicPorts = new HashSet <uint>(); foreach (dynamic value in valueArray) { string containerGroupName = value.name; string appName = ACIUtils.GetAppNameFromContainerGroupName(containerGroupName); var properties = value.properties; var ipAddress = properties.ipAddress; string ip = ipAddress.ip; if (ip == null) { continue; } var containerGroup = new ContainerGroup(containerGroupName, _config.Region, ip); foreach (var port in ipAddress.ports) { uint portNo = port.port; publicPorts.Add(portNo); } var containers = properties.containers; foreach (var container in containers) { string containerName = container.name; var containerProperties = container.properties; string containerImage = containerProperties.image; var currentContainer = new Container(containerName, containerImage); var containerPorts = containerProperties.ports; foreach (var containerPort in containerPorts) { uint portNo = containerPort.port; var cPort = new ContainerPort(portNo, publicPorts.Contains(portNo)); currentContainer.AddPort(cPort); } containerGroup.AddContainer(currentContainer); } ContainerGroupCollection containerGroupCollection; if (!_containerGroupCollections.TryGetValue(appName, out containerGroupCollection)) { containerGroupCollection = new ContainerGroupCollection(appName); } containerGroupCollection.AddContainerGroup(containerGroup); _containerGroupCollections[appName] = containerGroupCollection; publicPorts.Clear(); } } finally { _lock.Release(); } }
public async Task CreateApp(string appName, dynamic appDefinition) { await _lock.WaitAsync(); try { if (_containerGroupCollections.ContainsKey(appName)) { throw new Exception($"{appName} exists already"); } var containerGroupDefinition = appDefinition.containerGroup; var containerDefinitions = containerGroupDefinition.containers; var containerGroupName = ACIUtils.GetContainerGroupNameForAppName(appName); var containerGroup = new ContainerGroup(containerGroupName, _config.Region, ""); foreach (var containerDefinition in containerDefinitions) { string containerName = containerDefinition.name; string image = containerDefinition.image; var container = new Container(containerName, image); var containerPortsDefinition = containerDefinition.ports; foreach (var containerPortDefinition in containerPortsDefinition) { uint port = containerPortDefinition.port; string typeString = containerPortDefinition.type; var isPublic = typeString.Equals("public", StringComparison.OrdinalIgnoreCase); var containerPort = new ContainerPort(port, isPublic); container.AddPort(containerPort); } containerGroup.AddContainer(container); } containerGroup.IpAddress = await CreateContainerGroup(containerGroup); var containerGroupCollection = new ContainerGroupCollection(appName); containerGroupCollection.AddContainerGroup(containerGroup); _containerGroupCollections[appName] = containerGroupCollection; await Task.Delay(ContainerGroupCreationMillisecondsDelay); } finally { _lock.Release(); } }
public static void AssignAmmoBoxes(IEnumerable<ProjectileGun> guns, IEnumerable<AmmoBox> boxes) { //var gunCombos = GetPossibleGroupings(guns); var gunCombos = GetPossibleGroupings(guns); var gunAmmoGroupings = GetAmmoGroupings(gunCombos, boxes); // Choose the best set - some combination of most guns used with the most even spread of firings GunAmmoGroup[] best = GetBestAmmoAssignment(gunAmmoGroupings); #region REPORTS //var report_Combos = gunCombos. // Select(n => n.Select(o => new // { // Caliber = o.Caliber, // Guns = string.Join(", ", o.Guns.Select(p => p.Token)), // }).ToArray()). // ToArray(); //var report_Groupings = gunAmmoGroupings. // Select(n => n.Select(o => new // { // Ammo = o.Ammo == null ? "null" : o.Ammo.Length.ToString(), // Guns = o.Guns == null || o.Guns.Guns == null || o.Guns.Guns.Length == 0 ? "null" : string.Join(", ", o.Guns.Guns.Select(p => p.Token)), // }). // ToArray() // ).ToArray(); //var report_Best = best. // Select(o => new // { // Ammo = o.Ammo == null ? "null" : o.Ammo.Length.ToString(), // Guns = o.Guns == null || o.Guns.Guns == null || o.Guns.Guns.Length == 0 ? "null" : string.Join(", ", o.Guns.Guns.Select(p => p.Token)), // }). // ToArray(); #endregion // Lock the ammo boxes and guns into the calibers, distribute ammo boxes to guns foreach (GunAmmoGroup group in best) { IContainer container; if (group.Ammo.Length == 1) { container = group.Ammo[0]; } else { // More than one. Make a group to manage them (no need to expose this group to the caller, it's just a convenience // that manages pulling ammo from multiple boxes) ContainerGroup combined = new ContainerGroup(); combined.Ownership = ContainerGroup.ContainerOwnershipType.QuantitiesCanChange; foreach (AmmoBox ammo in group.Ammo) { combined.AddContainer(ammo); } container = combined; } container.RemovalMultiple = group.Guns.DemandPerGun; foreach (ProjectileGun gun in group.Guns.Guns) { gun.Caliber = group.Guns.Caliber; gun.Ammo = container; } } }