public static KeyPair CreateInstance(AWSModel.KeyPair awsKeyPair) { KeyPair keyPair = new KeyPair(); keyPair.KeyFingerprint = awsKeyPair.KeyFingerprint; keyPair.KeyMaterial = awsKeyPair.KeyMaterial; keyPair.KeyName = awsKeyPair.KeyName; return(keyPair); }
private async Task CreateAsync(CancellationToken? cancellationToken = null) { CancellationToken token = cancellationToken.HasValue ? cancellationToken.Value : new CancellationToken(); this.Logger.Log("Starting instance creation process"); Exception exception = null; try { await this.CreateSecurityGroupAsync(); token.ThrowIfCancellationRequested(); } catch (Exception e) { exception = e; } if (exception != null) { this.Logger.Log("Error creating security group: {0}. Performing rollback", exception.Message); await this.DeleteSecurityGroupAsync(); throw exception; } exception = null; try { await this.AuthorizeIngressAsync(new[] { new PortRangeDescription(22, 22, "tcp"), new PortRangeDescription(-1, -1, "icmp") }); token.ThrowIfCancellationRequested(); } catch (Exception e) { exception = e; } if (exception != null) { this.Logger.Log("Error authorising ingress: {0}. Performing rollback", exception.Message); await this.DeleteSecurityGroupAsync(); throw exception; } exception = null; try { this.privateKeyPair = await this.EnsureKeyPairCreatedAsync(); token.ThrowIfCancellationRequested(); } catch (Exception e) { exception = e; } if (exception != null) { this.Logger.Log("Error ensuring key pair created: {0}. Performing rollback", exception.Message); await this.DeleteSecurityGroupAsync(); throw exception; } exception = null; try { if (this.Specification.IsSpotInstance) await this.BidForInstanceAsync(token); else await this.CreateInstanceAsync(token); token.ThrowIfCancellationRequested(); this.PublicIp = (await this.DescribeInstanceAsync()).PublicIpAddress; } catch (Exception e) { exception = e; } if (exception != null) { this.Logger.Log("Error creating instance: {0}. Performing rollback", exception.Message); if (this.Specification.IsSpotInstance) await this.CancelBidRequestAsync(); await this.TerminateAsync(); await this.DeleteSecurityGroupAsync(); throw exception; } // We need to know this to create volumes if (this.Specification.AvailabilityZone == null) this.Specification.AvailabilityZone = (await this.DescribeInstanceAsync()).Placement.AvailabilityZone; this.Logger.Log("Instance has been created"); }
/// <summary> /// Sets the KeyPair property /// </summary> /// <param name="keyPair">KeyPair property</param> /// <returns>this instance</returns> public CreateKeyPairResult WithKeyPair(KeyPair keyPair) { this.keyPairField = keyPair; return this; }
private async Task<KeyPair> EnsureKeyPairCreatedAsync() { KeyPair keyPair = null; var existingKey = this.config.LoadKey(); // If we've got a key, check that it still exists on amazon if (existingKey != null) { this.Logger.Log("Saved key pair found. Fingerprint: {0}", existingKey.Value.Fingerprint); var response = await this.Client.DescribeKeyPairsAsync(new DescribeKeyPairsRequest() { Filters = new List<Filter>() { new Filter() { Name = "fingerprint", Values = new List<string>() { existingKey.Value.Fingerprint } }, }, }); var keyPairInfo = response.KeyPairs.FirstOrDefault(); if (keyPairInfo != null) { keyPair = new KeyPair() { KeyFingerprint = keyPairInfo.KeyFingerprint, KeyName = keyPairInfo.KeyName, KeyMaterial = existingKey.Value.Key, }; } } // If we don't have a keypair locally, or we do but it isn't on amazon if (keyPair == null) { this.Logger.Log("Creating key pair"); var response = await this.Client.CreateKeyPairAsync(new CreateKeyPairRequest() { KeyName = String.Format("Ec2Manager-{0}-{1}", Environment.MachineName, Guid.NewGuid().ToString()), }); keyPair = response.KeyPair; this.Logger.Log("Key pair created. Fingerprint: {0}", keyPair.KeyFingerprint); this.config.SaveKey(new KeyDescription(keyPair.KeyMaterial, keyPair.KeyFingerprint)); } return keyPair; }