github.com/coreos/mantle@v0.13.0/platform/machine/aws/flight.go (about) 1 // Copyright 2016 CoreOS, Inc. 2 // Copyright 2018 Red Hat 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package aws 17 18 import ( 19 ctplatform "github.com/coreos/container-linux-config-transpiler/config/platform" 20 "github.com/coreos/pkg/capnslog" 21 22 "github.com/coreos/mantle/platform" 23 "github.com/coreos/mantle/platform/api/aws" 24 ) 25 26 const ( 27 Platform platform.Name = "aws" 28 ) 29 30 var ( 31 plog = capnslog.NewPackageLogger("github.com/coreos/mantle", "platform/machine/aws") 32 ) 33 34 type flight struct { 35 *platform.BaseFlight 36 api *aws.API 37 keyAdded bool 38 } 39 40 // NewFlight creates an instance of a Flight suitable for spawning 41 // instances on Amazon Web Services' Elastic Compute platform. 42 // 43 // NewFlight will consume the environment variables $AWS_REGION, 44 // $AWS_ACCESS_KEY_ID, and $AWS_SECRET_ACCESS_KEY to determine the region to 45 // spawn instances in and the credentials to use to authenticate. 46 func NewFlight(opts *aws.Options) (platform.Flight, error) { 47 api, err := aws.New(opts) 48 if err != nil { 49 return nil, err 50 } 51 52 bf, err := platform.NewBaseFlight(opts.Options, Platform, ctplatform.EC2) 53 if err != nil { 54 return nil, err 55 } 56 57 af := &flight{ 58 BaseFlight: bf, 59 api: api, 60 } 61 62 keys, err := af.Keys() 63 if err != nil { 64 af.Destroy() 65 return nil, err 66 } 67 if err := api.AddKey(af.Name(), keys[0].String()); err != nil { 68 af.Destroy() 69 return nil, err 70 } 71 af.keyAdded = true 72 73 return af, nil 74 } 75 76 // NewCluster creates an instance of a Cluster suitable for spawning 77 // instances on Amazon Web Services' Elastic Compute platform. 78 func (af *flight) NewCluster(rconf *platform.RuntimeConfig) (platform.Cluster, error) { 79 bc, err := platform.NewBaseCluster(af.BaseFlight, rconf) 80 if err != nil { 81 return nil, err 82 } 83 84 ac := &cluster{ 85 BaseCluster: bc, 86 flight: af, 87 } 88 89 af.AddCluster(ac) 90 91 return ac, nil 92 } 93 94 func (af *flight) Destroy() { 95 if af.keyAdded { 96 if err := af.api.DeleteKey(af.Name()); err != nil { 97 plog.Errorf("Error deleting key %v: %v", af.Name(), err) 98 } 99 } 100 101 af.BaseFlight.Destroy() 102 }