1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
18 "git.curoverse.com/arvados.git/lib/cloud"
19 "git.curoverse.com/arvados.git/sdk/go/arvados"
20 "github.com/aws/aws-sdk-go/aws"
21 "github.com/aws/aws-sdk-go/aws/credentials"
22 "github.com/aws/aws-sdk-go/aws/session"
23 "github.com/aws/aws-sdk-go/service/ec2"
24 "github.com/sirupsen/logrus"
25 "golang.org/x/crypto/ssh"
28 // Driver is the ec2 implementation of the cloud.Driver interface.
29 var Driver = cloud.DriverFunc(newEC2InstanceSet)
31 type ec2InstanceSetConfig struct {
33 SecretAccessKey string
35 SecurityGroupIDs []string
41 type ec2Interface interface {
42 DescribeKeyPairs(input *ec2.DescribeKeyPairsInput) (*ec2.DescribeKeyPairsOutput, error)
43 ImportKeyPair(input *ec2.ImportKeyPairInput) (*ec2.ImportKeyPairOutput, error)
44 RunInstances(input *ec2.RunInstancesInput) (*ec2.Reservation, error)
45 DescribeInstances(input *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error)
46 CreateTags(input *ec2.CreateTagsInput) (*ec2.CreateTagsOutput, error)
47 TerminateInstances(input *ec2.TerminateInstancesInput) (*ec2.TerminateInstancesOutput, error)
50 type ec2InstanceSet struct {
51 ec2config ec2InstanceSetConfig
52 instanceSetID cloud.InstanceSetID
53 logger logrus.FieldLogger
56 keys map[string]string
59 func newEC2InstanceSet(config json.RawMessage, instanceSetID cloud.InstanceSetID, _ cloud.SharedResourceTags, logger logrus.FieldLogger) (prv cloud.InstanceSet, err error) {
60 instanceSet := &ec2InstanceSet{
61 instanceSetID: instanceSetID,
64 err = json.Unmarshal(config, &instanceSet.ec2config)
68 awsConfig := aws.NewConfig().
69 WithCredentials(credentials.NewStaticCredentials(
70 instanceSet.ec2config.AccessKeyID,
71 instanceSet.ec2config.SecretAccessKey,
73 WithRegion(instanceSet.ec2config.Region)
74 instanceSet.client = ec2.New(session.Must(session.NewSession(awsConfig)))
75 instanceSet.keys = make(map[string]string)
76 if instanceSet.ec2config.EBSVolumeType == "" {
77 instanceSet.ec2config.EBSVolumeType = "gp2"
79 return instanceSet, nil
82 func awsKeyFingerprint(pk ssh.PublicKey) (md5fp string, sha1fp string, err error) {
83 // AWS key fingerprints don't use the usual key fingerprint
84 // you get from ssh-keygen or ssh.FingerprintLegacyMD5()
85 // (you can get that from md5.Sum(pk.Marshal())
87 // AWS uses the md5 or sha1 of the PKIX DER encoding of the
88 // public key, so calculate those fingerprints here.
94 if err := ssh.Unmarshal(pk.Marshal(), &rsaPub); err != nil {
95 return "", "", fmt.Errorf("agent: Unmarshal failed to parse public key: %v", err)
97 rsaPk := rsa.PublicKey{
98 E: int(rsaPub.E.Int64()),
101 pkix, _ := x509.MarshalPKIXPublicKey(&rsaPk)
102 md5pkix := md5.Sum([]byte(pkix))
103 sha1pkix := sha1.Sum([]byte(pkix))
106 for i := 0; i < len(md5pkix); i += 1 {
107 md5fp += fmt.Sprintf(":%02x", md5pkix[i])
109 for i := 0; i < len(sha1pkix); i += 1 {
110 sha1fp += fmt.Sprintf(":%02x", sha1pkix[i])
112 return md5fp[1:], sha1fp[1:], nil
115 func (instanceSet *ec2InstanceSet) Create(
116 instanceType arvados.InstanceType,
117 imageID cloud.ImageID,
118 newTags cloud.InstanceTags,
119 initCommand cloud.InitCommand,
120 publicKey ssh.PublicKey) (cloud.Instance, error) {
122 md5keyFingerprint, sha1keyFingerprint, err := awsKeyFingerprint(publicKey)
124 return nil, fmt.Errorf("Could not make key fingerprint: %v", err)
126 instanceSet.keysMtx.Lock()
129 if keyname, ok = instanceSet.keys[md5keyFingerprint]; !ok {
130 keyout, err := instanceSet.client.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{
131 Filters: []*ec2.Filter{&ec2.Filter{
132 Name: aws.String("fingerprint"),
133 Values: []*string{&md5keyFingerprint, &sha1keyFingerprint},
137 return nil, fmt.Errorf("Could not search for keypair: %v", err)
140 if len(keyout.KeyPairs) > 0 {
141 keyname = *(keyout.KeyPairs[0].KeyName)
143 keyname = "arvados-dispatch-keypair-" + md5keyFingerprint
144 _, err := instanceSet.client.ImportKeyPair(&ec2.ImportKeyPairInput{
146 PublicKeyMaterial: ssh.MarshalAuthorizedKey(publicKey),
149 return nil, fmt.Errorf("Could not import keypair: %v", err)
152 instanceSet.keys[md5keyFingerprint] = keyname
154 instanceSet.keysMtx.Unlock()
156 ec2tags := []*ec2.Tag{}
157 for k, v := range newTags {
158 ec2tags = append(ec2tags, &ec2.Tag{
160 Value: aws.String(v),
164 rii := ec2.RunInstancesInput{
165 ImageId: aws.String(string(imageID)),
166 InstanceType: &instanceType.ProviderType,
167 MaxCount: aws.Int64(1),
168 MinCount: aws.Int64(1),
171 NetworkInterfaces: []*ec2.InstanceNetworkInterfaceSpecification{
172 &ec2.InstanceNetworkInterfaceSpecification{
173 AssociatePublicIpAddress: aws.Bool(false),
174 DeleteOnTermination: aws.Bool(true),
175 DeviceIndex: aws.Int64(0),
176 Groups: aws.StringSlice(instanceSet.ec2config.SecurityGroupIDs),
177 SubnetId: &instanceSet.ec2config.SubnetID,
179 DisableApiTermination: aws.Bool(false),
180 InstanceInitiatedShutdownBehavior: aws.String("terminate"),
181 TagSpecifications: []*ec2.TagSpecification{
182 &ec2.TagSpecification{
183 ResourceType: aws.String("instance"),
186 UserData: aws.String(base64.StdEncoding.EncodeToString([]byte("#!/bin/sh\n" + initCommand + "\n"))),
189 if instanceType.AddedScratch > 0 {
190 rii.BlockDeviceMappings = []*ec2.BlockDeviceMapping{&ec2.BlockDeviceMapping{
191 DeviceName: aws.String("/dev/xvdt"),
192 Ebs: &ec2.EbsBlockDevice{
193 DeleteOnTermination: aws.Bool(true),
194 VolumeSize: aws.Int64((int64(instanceType.AddedScratch) + (1<<30 - 1)) >> 30),
195 VolumeType: &instanceSet.ec2config.EBSVolumeType,
199 if instanceType.Preemptible {
200 rii.InstanceMarketOptions = &ec2.InstanceMarketOptionsRequest{
201 MarketType: aws.String("spot"),
202 SpotOptions: &ec2.SpotMarketOptions{
203 InstanceInterruptionBehavior: aws.String("terminate"),
204 MaxPrice: aws.String(fmt.Sprintf("%v", instanceType.Price)),
208 rsv, err := instanceSet.client.RunInstances(&rii)
215 provider: instanceSet,
216 instance: rsv.Instances[0],
220 func (instanceSet *ec2InstanceSet) Instances(tags cloud.InstanceTags) (instances []cloud.Instance, err error) {
221 var filters []*ec2.Filter
222 for k, v := range tags {
223 filters = append(filters, &ec2.Filter{
224 Name: aws.String("tag:" + k),
225 Values: []*string{aws.String(v)},
228 dii := &ec2.DescribeInstancesInput{Filters: filters}
230 dio, err := instanceSet.client.DescribeInstances(dii)
235 for _, rsv := range dio.Reservations {
236 for _, inst := range rsv.Instances {
237 if *inst.State.Name != "shutting-down" && *inst.State.Name != "terminated" {
238 instances = append(instances, &ec2Instance{instanceSet, inst})
242 if dio.NextToken == nil {
243 return instances, err
245 dii.NextToken = dio.NextToken
249 func (az *ec2InstanceSet) Stop() {
252 type ec2Instance struct {
253 provider *ec2InstanceSet
254 instance *ec2.Instance
257 func (inst *ec2Instance) ID() cloud.InstanceID {
258 return cloud.InstanceID(*inst.instance.InstanceId)
261 func (inst *ec2Instance) String() string {
262 return *inst.instance.InstanceId
265 func (inst *ec2Instance) ProviderType() string {
266 return *inst.instance.InstanceType
269 func (inst *ec2Instance) SetTags(newTags cloud.InstanceTags) error {
270 var ec2tags []*ec2.Tag
271 for k, v := range newTags {
272 ec2tags = append(ec2tags, &ec2.Tag{
274 Value: aws.String(v),
278 _, err := inst.provider.client.CreateTags(&ec2.CreateTagsInput{
279 Resources: []*string{inst.instance.InstanceId},
286 func (inst *ec2Instance) Tags() cloud.InstanceTags {
287 tags := make(map[string]string)
289 for _, t := range inst.instance.Tags {
290 tags[*t.Key] = *t.Value
296 func (inst *ec2Instance) Destroy() error {
297 _, err := inst.provider.client.TerminateInstances(&ec2.TerminateInstancesInput{
298 InstanceIds: []*string{inst.instance.InstanceId},
303 func (inst *ec2Instance) Address() string {
304 if inst.instance.PrivateIpAddress != nil {
305 return *inst.instance.PrivateIpAddress
311 func (inst *ec2Instance) RemoteUser() string {
312 return inst.provider.ec2config.AdminUsername
315 func (inst *ec2Instance) VerifyHostKey(ssh.PublicKey, *ssh.Client) error {
316 return cloud.ErrNotImplemented