public void TakeSnapshots(IEnumerable<Instance> instances, Ec2BootstrapConfig config) { foreach (var instance in instances) { foreach (var volumeId in instance.BlockDeviceMappings.Select(x => x.Ebs.VolumeId)) { var request = new CreateSnapshotRequest { Description = "Snapshot for ConDep integration testing", VolumeId = volumeId }; var response = _client.CreateSnapshot(request); config.Instances .Single(x => x.InstanceId == instance.InstanceId) .BaseSnapshots.Add(new Ec2Snapshot { SnapshotId = response.Snapshot.SnapshotId, VolumeId = response.Snapshot.VolumeId, VolumeSize = response.Snapshot.VolumeSize }); } } var snapshotIds = config.Instances.SelectMany(x => x.BaseSnapshots.Select(y => y.SnapshotId)).ToList(); _tagHandler.CreateNameTags(config.BootstrapId, snapshotIds); Logger.WithLogSection("Waiting for snapshots to finish", () => WaitForSnapshotsToFinish(snapshotIds)); }
private static void HaveAccessToServers(Ec2BootstrapConfig config) { Logger.WithLogSection("Checking access to servers", () => { foreach (var instance in config.Instances) { HaveAccessToServer(instance, 1, 5); } }); }
public string Boostrap(string vpcId, string imageId, int numberOfInstances, string pathToRsaPrivateKey) { var config = new Ec2BootstrapConfig(Guid.NewGuid().ToString()) { VpcId = vpcId, AwsProfileName = _awsProfileName }; Logger.Info("Creating security group..."); var securityGroupId = _securityGroupHandler.CreateSecurityGroup(vpcId, config.BootstrapId); var securityGroupTag = _tagHandler.CreateNameTags(config.BootstrapId, new[] {securityGroupId}); config.SecurityGroupId = securityGroupId; config.SecurityGroupTag = securityGroupTag; Logger.Info("Creating instances..."); var instanceIds = _instanceHandler.CreateInstances(config.BootstrapId, imageId, numberOfInstances, securityGroupId).ToList(); Logger.Info("Tagging instances..."); var instanceTag = _tagHandler.CreateNameTags(config.BootstrapId, instanceIds); Logger.WithLogSection("Waiting for instances to be ready", () => _instanceHandler.WaitForInstancesStatus(instanceIds, Ec2InstanceState.Running)); List<Tuple<string, string>> passwords = null; Logger.WithLogSection("Waiting for Windows password to be available", () => { passwords = _passwordHandler.WaitForPassword(instanceIds, pathToRsaPrivateKey); }); var instances = _instanceHandler.GetInstances(instanceIds).ToList(); foreach (var instance in instances) { config.Instances.Add(new Ec2Instance { InstanceId = instance.InstanceId, PublicDns = instance.PublicDnsName, PublicIp = instance.PublicIpAddress, Tag = instanceTag, UserName = "******", Password = passwords.Single(x => x.Item1 == instance.InstanceId).Item2 }); } HaveAccessToServers(config); //StopServers(config); //if (takeSnapshots) //{ // Logger.Info("Taking snapshots of disks to enable resets..."); // _snapshotHandler.TakeSnapshots(instances, config); //} //else //{ // Logger.Warn("Snapshots disabled. Note that reset will not work without snapshots."); //} //StartServers(config); Logger.Info("Storing configuration..."); var configHandler = new BootstrapConfigHandler(config.BootstrapId); configHandler.Write(config); return config.BootstrapId; }
private void StopServers(Ec2BootstrapConfig config) { Logger.WithLogSection("Stopping servers", () => { var instanceIds = config.Instances.Select(x => x.InstanceId).ToList(); var request = new StopInstancesRequest { InstanceIds = instanceIds }; _client.StopInstances(request); _instanceHandler.WaitForInstancesStatus(instanceIds, Ec2InstanceState.Stopped); }); }
private static void HaveAccessToServers(Ec2BootstrapConfig config) { Logger.WithLogSection("Checking access to servers", () => { foreach (var instance in config.Instances) { HaveAccessToServer(instance, 1, 5); } }); }
private Ec2BootstrapConfig BootstrapNew(Ec2BootstrapConfig config) { var amiLocator = new Ec2AmiLocator(_client); var imageValues = _options.Image; if (imageValues.HasImageId()) { _options.InstanceRequest.ImageId = imageValues.Id; } else if (imageValues.HasLatestImageDefined()) { switch (imageValues.LatestImage) { case AwsWindowsImage.Win2008: _options.InstanceRequest.ImageId = amiLocator.Find2008Core(); break; case AwsWindowsImage.Win2008R2: _options.InstanceRequest.ImageId = amiLocator.Find2008R2Core(); break; case AwsWindowsImage.Win2012: _options.InstanceRequest.ImageId = amiLocator.Find2012Core(); break; case AwsWindowsImage.Win2012R2: _options.InstanceRequest.ImageId = amiLocator.Find2012R2Core(); break; case AwsWindowsImage.Win2016: _options.InstanceRequest.ImageId = amiLocator.Find2016Core(); break; default: throw new Exception("Image " + imageValues.LatestImage + " currently not supported. Please specify image id as a string instead."); } } else if (imageValues.HasImageFilter()) { _options.InstanceRequest.ImageId = amiLocator.FindWithFilters(imageValues.Filters, imageValues.FilterByOwner); } else { _options.InstanceRequest.ImageId = amiLocator.Find2012R2Core(); } Logger.Info("Creating instances..."); var instanceIds = _instanceHandler.CreateInstances(_options).ToList(); Thread.Sleep(10000); //Logger.Info("Tagging instances. //var instanceTag = _tagHandler.CreateNameTags(config.BootstrapId, instanceIds); Logger.WithLogSection("Waiting for instances to be ready", () => _instanceHandler.WaitForInstancesStatus(instanceIds, Ec2InstanceState.Running)); List <Tuple <string, string> > passwords = null; if (!_conDepSettings.Config.DeploymentUser.IsDefined()) { Logger.WithLogSection("Waiting for Windows password to be available", () => { passwords = _passwordHandler.WaitForPassword(instanceIds, _options.PrivateKeyFileLocation); }); } _instanceHandler.TagInstances(instanceIds, _options.Tags); var instances = _instanceHandler.GetInstances(instanceIds).ToList(); foreach (var instance in instances) { config.Instances.Add(new Ec2Instance { InstanceId = instance.InstanceId, UserName = _conDepSettings.Config.DeploymentUser.IsDefined() ? _conDepSettings.Config.DeploymentUser.UserName : @".\Administrator", Password = _conDepSettings.Config.DeploymentUser.IsDefined() ? _conDepSettings.Config.DeploymentUser.Password : passwords.Single(x => x.Item1 == instance.InstanceId).Item2, ManagementAddress = GetManagementAddress(instance) }); } HaveAccessToServers(config); //StopServers(config); //if (takeSnapshots) //{ // Logger.Info("Taking snapshots of disks to enable resets..."); // _snapshotHandler.TakeSnapshots(instances, config); //} //else //{ // Logger.Warn("Snapshots disabled. Note that reset will not work without snapshots."); //} //StartServers(config); //Logger.Info("Storing configuration..."); //var configHandler = new BootstrapConfigHandler(config.BootstrapId); //configHandler.Write(config); //return config.BootstrapId; return(config); }
private Ec2BootstrapConfig GetConfigFromExisting(AwsBootstrapOptionsValues options, Ec2BootstrapConfig config) { Logger.Info("Allready bootstrapped. Getting server information."); var existingInstances = options.IdempotencyType == AwsEc2IdempotencyType.ClientToken ? _instanceHandler.GetInstances(_options.InstanceRequest.ClientToken).ToList() : _instanceHandler.GetInstances(_options.IdempotencyTags).ToList(); List <Tuple <string, string> > existingPasswords = null; if (!_conDepSettings.Config.DeploymentUser.IsDefined()) { existingPasswords = _passwordHandler.WaitForPassword(existingInstances.Select(x => x.InstanceId), _options.PrivateKeyFileLocation); } foreach (var instance in existingInstances.Where(x => x.State.Name == "Running")) { config.Instances.Add(new Ec2Instance { InstanceId = instance.InstanceId, UserName = _conDepSettings.Config.DeploymentUser.IsDefined() ? _conDepSettings.Config.DeploymentUser.UserName : "******", Password = _conDepSettings.Config.DeploymentUser.IsDefined() ? _conDepSettings.Config.DeploymentUser.Password : existingPasswords.Single(x => x.Item1 == instance.InstanceId).Item2, AwsInstance = instance, ManagementAddress = GetManagementAddress(instance) }); } return(config); }