github.com/coreos/mantle@v0.13.0/platform/machine/aws/cluster.go (about)

     1  // Copyright 2016 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aws
    16  
    17  import (
    18  	"os"
    19  	"path/filepath"
    20  
    21  	"github.com/coreos/mantle/platform"
    22  	"github.com/coreos/mantle/platform/conf"
    23  )
    24  
    25  type cluster struct {
    26  	*platform.BaseCluster
    27  	flight *flight
    28  }
    29  
    30  func (ac *cluster) NewMachine(userdata *conf.UserData) (platform.Machine, error) {
    31  	conf, err := ac.RenderUserData(userdata, map[string]string{
    32  		"$public_ipv4":  "${COREOS_EC2_IPV4_PUBLIC}",
    33  		"$private_ipv4": "${COREOS_EC2_IPV4_LOCAL}",
    34  	})
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	var keyname string
    40  	if !ac.RuntimeConf().NoSSHKeyInMetadata {
    41  		keyname = ac.flight.Name()
    42  	}
    43  	instances, err := ac.flight.api.CreateInstances(ac.Name(), keyname, conf.String(), 1)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	mach := &machine{
    49  		cluster: ac,
    50  		mach:    instances[0],
    51  	}
    52  
    53  	mach.dir = filepath.Join(ac.RuntimeConf().OutputDir, mach.ID())
    54  	if err := os.Mkdir(mach.dir, 0777); err != nil {
    55  		mach.Destroy()
    56  		return nil, err
    57  	}
    58  
    59  	confPath := filepath.Join(mach.dir, "user-data")
    60  	if err := conf.WriteFile(confPath); err != nil {
    61  		mach.Destroy()
    62  		return nil, err
    63  	}
    64  
    65  	if mach.journal, err = platform.NewJournal(mach.dir); err != nil {
    66  		mach.Destroy()
    67  		return nil, err
    68  	}
    69  
    70  	if err := platform.StartMachine(mach, mach.journal); err != nil {
    71  		mach.Destroy()
    72  		return nil, err
    73  	}
    74  
    75  	ac.AddMach(mach)
    76  
    77  	return mach, nil
    78  }
    79  
    80  func (ac *cluster) Destroy() {
    81  	ac.BaseCluster.Destroy()
    82  	ac.flight.DelCluster(ac)
    83  }