github.com/coreos/mantle@v0.13.0/platform/machine/azure/cluster.go (about) 1 // Copyright 2018 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 azure 16 17 import ( 18 "fmt" 19 "math/rand" 20 "os" 21 "path/filepath" 22 23 "github.com/coreos/mantle/platform" 24 "github.com/coreos/mantle/platform/conf" 25 ) 26 27 type cluster struct { 28 *platform.BaseCluster 29 flight *flight 30 sshKey string 31 ResourceGroup string 32 StorageAccount string 33 } 34 35 func (ac *cluster) vmname() string { 36 b := make([]byte, 5) 37 rand.Read(b) 38 return fmt.Sprintf("%s-%x", ac.Name()[0:13], b) 39 } 40 41 func (ac *cluster) NewMachine(userdata *conf.UserData) (platform.Machine, error) { 42 conf, err := ac.RenderUserData(userdata, map[string]string{ 43 "$private_ipv4": "${COREOS_AZURE_IPV4_DYNAMIC}", 44 }) 45 if err != nil { 46 return nil, err 47 } 48 49 instance, err := ac.flight.api.CreateInstance(ac.vmname(), conf.String(), ac.sshKey, ac.ResourceGroup, ac.StorageAccount) 50 if err != nil { 51 return nil, err 52 } 53 54 mach := &machine{ 55 cluster: ac, 56 mach: instance, 57 } 58 59 mach.dir = filepath.Join(ac.RuntimeConf().OutputDir, mach.ID()) 60 if err := os.Mkdir(mach.dir, 0777); err != nil { 61 mach.Destroy() 62 return nil, err 63 } 64 65 confPath := filepath.Join(mach.dir, "user-data") 66 if err := conf.WriteFile(confPath); err != nil { 67 mach.Destroy() 68 return nil, err 69 } 70 71 if mach.journal, err = platform.NewJournal(mach.dir); err != nil { 72 mach.Destroy() 73 return nil, err 74 } 75 76 if err := platform.StartMachine(mach, mach.journal); err != nil { 77 mach.Destroy() 78 return nil, err 79 } 80 81 ac.AddMach(mach) 82 83 return mach, nil 84 } 85 86 func (ac *cluster) Destroy() { 87 ac.BaseCluster.Destroy() 88 if e := ac.flight.api.TerminateResourceGroup(ac.ResourceGroup); e != nil { 89 plog.Errorf("Deleting resource group %v: %v", ac.ResourceGroup, e) 90 } 91 ac.flight.DelCluster(ac) 92 }