github.com/coreos/mantle@v0.13.0/platform/machine/gcloud/cluster.go (about) 1 // Copyright 2015 CoreOS, Inc. 2 // Copyright 2015 The Go Authors. 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 gcloud 17 18 import ( 19 "os" 20 "path/filepath" 21 22 "golang.org/x/crypto/ssh/agent" 23 24 "github.com/coreos/mantle/platform" 25 "github.com/coreos/mantle/platform/api/gcloud" 26 "github.com/coreos/mantle/platform/conf" 27 ) 28 29 type cluster struct { 30 *platform.BaseCluster 31 flight *flight 32 } 33 34 // Calling in parallel is ok 35 func (gc *cluster) NewMachine(userdata *conf.UserData) (platform.Machine, error) { 36 conf, err := gc.RenderUserData(userdata, map[string]string{ 37 "$public_ipv4": "${COREOS_GCE_IP_EXTERNAL_0}", 38 "$private_ipv4": "${COREOS_GCE_IP_LOCAL_0}", 39 }) 40 if err != nil { 41 return nil, err 42 } 43 44 var keys []*agent.Key 45 if !gc.RuntimeConf().NoSSHKeyInMetadata { 46 keys, err = gc.Keys() 47 if err != nil { 48 return nil, err 49 } 50 } 51 52 instance, err := gc.flight.api.CreateInstance(conf.String(), keys) 53 if err != nil { 54 return nil, err 55 } 56 57 intip, extip := gcloud.InstanceIPs(instance) 58 59 gm := &machine{ 60 gc: gc, 61 name: instance.Name, 62 intIP: intip, 63 extIP: extip, 64 } 65 66 gm.dir = filepath.Join(gc.RuntimeConf().OutputDir, gm.ID()) 67 if err := os.Mkdir(gm.dir, 0777); err != nil { 68 gm.Destroy() 69 return nil, err 70 } 71 72 confPath := filepath.Join(gm.dir, "user-data") 73 if err := conf.WriteFile(confPath); err != nil { 74 gm.Destroy() 75 return nil, err 76 } 77 78 if gm.journal, err = platform.NewJournal(gm.dir); err != nil { 79 gm.Destroy() 80 return nil, err 81 } 82 83 if err := platform.StartMachine(gm, gm.journal); err != nil { 84 gm.Destroy() 85 return nil, err 86 } 87 88 gc.AddMach(gm) 89 90 return gm, nil 91 } 92 93 func (gc *cluster) Destroy() { 94 gc.BaseCluster.Destroy() 95 gc.flight.DelCluster(gc) 96 }