Example #1
0
        public async Task ShutDownSystem(bool stopJumpBox)
        {
            var autoScalingHelper = new AutoScalingHelper(GlobalVariables.Enviroment, GlobalVariables.Region, GlobalVariables.Color);
            var lstGroup          = await autoScalingHelper.GetEnvironmentAutoScalingGroupList();

            if (!stopJumpBox)
            {
                var jumpBox = lstGroup.Find(o => o.Name.IndexOf("Jump") >= 0);
                if (jumpBox != null)
                {
                    lstGroup.Remove(jumpBox);
                }
            }
            foreach (var group in lstGroup)
            {
                await autoScalingHelper.StopScalingGroup(group.AutoScalingGroupName);
            }

            ElasticBeanstalkServices service = new ElasticBeanstalkServices();

            service.DisableScheduleActions(true);

            //helper.ShutdownSystem();
            RDSHelper rdsHelper = new RDSHelper(GlobalVariables.Enviroment, GlobalVariables.Region, GlobalVariables.Color);
            var       response  = await rdsHelper.GetRDSInstance();

            await rdsHelper.StopRdsInstance(response.DBInstanceIdentifier);
        }
Example #2
0
        public async Task StartSystem(
            List <AutoScalingGroupSettings> lstSettings, bool isRdsMultyAz)
        {
            try
            {
                ElasticBeanstalkServices service = new ElasticBeanstalkServices();
                service.DisableScheduleActions(false);
                //Start RDS
                RDSHelper rdsHelper = new RDSHelper(GlobalVariables.Enviroment, GlobalVariables.Region, GlobalVariables.Color);
                try
                {
                    var response = await rdsHelper.GetRDSInstance();

                    await rdsHelper.StartRdsInstance(response.DBInstanceIdentifier, isRdsMultyAz);
                }
                catch (Exception ex)
                {
                    if (ex.GetType().Name == "InvalidDBInstanceStateException")
                    {
                        LogServices.WriteLog(
                            $"RDS instance is not stopped, system starting will continue. Detailed message: {ex.Message}",
                            LogType.Warn, GlobalVariables.Enviroment.ToString());
                    }
                }
                //Start applications by changing the auto scaling group
                var autoScalingHelper = new AutoScalingHelper(GlobalVariables.Enviroment, GlobalVariables.Region, GlobalVariables.Color);
                var lstGroup          = await autoScalingHelper.GetEnvironmentAutoScalingGroupList();

                var jumpBox = lstGroup.Find(o => o.Name.IndexOf("Jump") >= 0);
                if (jumpBox.MaxSize == 0)
                {
                    var settings = new AutoScalingGroupSettings()
                    {
                        MaxSize         = 1,
                        MinSize         = 1,
                        DesiredCapacity = 1
                    };
                    await autoScalingHelper.ChangeScalingGroup(jumpBox.AutoScalingGroupName, settings);
                }
                foreach (var group in lstGroup)
                {
                    var settings = lstSettings.Find(o => group.Name.IndexOf(o.Environment.ToString()) == 0 &&
                                                    group.Name.IndexOf(o.Application.ToString()) > 0);
                    if (settings != null)
                    {
                        await autoScalingHelper.ChangeScalingGroup(group.AutoScalingGroupName, settings);
                    }
                }
            }
            catch (Exception ex)
            {
                LogServices.WriteLog(ex.Message + " Stack trace: " + ex.StackTrace, LogType.Error, GlobalVariables.Enviroment.ToString());
            }
        }
Example #3
0
        public async Task ClearTargetGroupAttachments()
        {
            var loadBalancerHelper = new ALBHelper(
                GlobalVariables.Enviroment, GlobalVariables.Region, GlobalVariables.Color);
            var scalingGroupHelper = new AutoScalingHelper(
                GlobalVariables.Enviroment, GlobalVariables.Region, GlobalVariables.Color);
            var autoScalingGroups = await scalingGroupHelper.GetEnvironmentAutoScalingGroupList(true);

            foreach (var autoScalingGroup in autoScalingGroups)
            {
                await scalingGroupHelper.ClearAutoScalingGroupAttachedTargetGroups(autoScalingGroup);
            }
        }
Example #4
0
        public async Task GenerateExternalLoadBalancers()
        {
            ALBHelper loadBalancerHelper = new ALBHelper(
                GlobalVariables.Enviroment, GlobalVariables.Region, GlobalVariables.Color);
            var scalingGroupHelper = new AutoScalingHelper(
                GlobalVariables.Enviroment, GlobalVariables.Region, GlobalVariables.Color);
            var apps = Enum.GetNames(typeof(ApplicationServer)).ToList();

            apps.Remove("JumpBox");
            apps.Remove("Worker");
            var colors  = Enum.GetNames(typeof(Color)).ToList();
            var sGroups = await scalingGroupHelper.GetEnvironmentAutoScalingGroupList(true);

            sGroups.Remove(sGroups.Find(o => o.Name.IndexOf("Jump") >= 0));
            var ec2Helper = new EC2Helper(GlobalVariables.Enviroment, GlobalVariables.Region, GlobalVariables.Color);
            var vpc       = await ec2Helper.GetVPC();

            var subnets = await ec2Helper.GetSubnetList();

            var securirtyGroups = await ec2Helper.GetSecurityGroupList();

            foreach (var appName in apps)
            {
                var tGroups = new List <SA_TargetGroup>();
                var newSGs  = new List <string>();
                var oldSGs  = securirtyGroups.FindAll(o =>
                                                      o.DisplayName.Contains(appName) &&
                                                      o.DisplayName.Contains(GlobalVariables.Enviroment) &&
                                                      o.Description == "Load Balancer Security Group").Select(o => o.GroupId).ToList();
                //foreach (string oldSG in oldSGs)
                //{
                //    await ec2Helper.CopySecurityGroup(oldSG,
                //        $"{GlobalVariables.Enviroment}-{GlobalVariables.Color}-{appName}-External-ALB",
                //        "External Application Load Balancer Security Group");
                //}
                //return;
                var loadBalancerArn = await loadBalancerHelper.CreateLoadBalancer(
                    $"{GlobalVariables.Enviroment.ToString()}-{appName}-ALB",
                    securirtyGroups.FindAll(o =>
                                            o.DisplayName.Contains(appName) &&
                                            o.DisplayName.Contains(GlobalVariables.Enviroment.ToString()) &&
                                            o.Description == "Load Balancer Security Group").Select(o => o.GroupId).ToList(),
                    subnets.FindAll(o => o.DisplayName.Contains("Public")).Select(o => o.SubnetId).ToList());

                foreach (var color in colors)
                {
                    var targetGroup = sGroups.Find(o => o.Name.Contains($"{GlobalVariables.Enviroment}-{color}-{appName}"));
                    if (targetGroup == null)
                    {
                        LogServices.WriteLog(
                            $"{GlobalVariables.Enviroment}-{color}-{appName} target group doesn't exist",
                            LogType.Warn, "SwitchDeploymentService");
                        continue;
                    }
                    var tGroup = await loadBalancerHelper.CreateTargetGroup(
                        $"{GlobalVariables.Enviroment.ToString()}-{color}-{appName}-TG",
                        vpc.VpcId, "HTTP", 80);

                    await scalingGroupHelper.AttachLoadBalancerTargetGroups(
                        sGroups.Find(o => o.Name.Contains($"{GlobalVariables.Enviroment}-{color}-{appName}")),
                        new List <string>() { tGroup.TargetGroupArn });

                    tGroups.Add(tGroup);
                }
                var greenTargetGroup = tGroups.Find(o => o.TargetGroupName.Contains(Color.green.ToString()));
                if (greenTargetGroup != null)
                {
                    await loadBalancerHelper.CreatListener(loadBalancerArn, greenTargetGroup.TargetGroupArn,
                                                           "HTTP", PRODUCTION_HTTP_PORT);

                    await loadBalancerHelper.CreatListener(loadBalancerArn, greenTargetGroup.TargetGroupArn,
                                                           "HTTPS", PRODUCTION_HTTPS_PORT,
                                                           "arn:aws:acm:us-east-2:125237747044:certificate/abb607ce-4090-45cb-bf29-923e08106664");
                }
                var blueTargetGroup = tGroups.Find(o => o.TargetGroupName.Contains(Color.blue.ToString()));
                if (blueTargetGroup != null)
                {
                    await loadBalancerHelper.CreatListener(loadBalancerArn, blueTargetGroup.TargetGroupArn,
                                                           "HTTP", PRE_PRODUCTION_HTTP_PORT);

                    await loadBalancerHelper.CreatListener(loadBalancerArn, blueTargetGroup.TargetGroupArn,
                                                           "HTTPS", PRE_PRODUCTION_HTTPS_PORT,
                                                           "arn:aws:acm:us-east-2:125237747044:certificate/abb607ce-4090-45cb-bf29-923e08106664");
                }
            }
        }