github.com/coreos/mantle@v0.13.0/platform/machine/esx/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 esx 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 } 31 32 func (ec *cluster) vmname() string { 33 b := make([]byte, 5) 34 rand.Read(b) 35 return fmt.Sprintf("%s-%x", ec.Name(), b) 36 } 37 38 func (ec *cluster) NewMachine(userdata *conf.UserData) (platform.Machine, error) { 39 conf, err := ec.RenderUserData(userdata, map[string]string{ 40 "$public_ipv4": "${COREOS_ESX_IPV4_PUBLIC_0}", 41 "$private_ipv4": "${COREOS_ESX_IPV4_PRIVATE_0}", 42 }) 43 if err != nil { 44 return nil, err 45 } 46 47 conf.AddSystemdUnit("coreos-metadata.service", `[Unit] 48 Description=VMware metadata agent 49 50 [Service] 51 Type=oneshot 52 Environment=OUTPUT=/run/metadata/coreos 53 ExecStart=/usr/bin/mkdir --parent /run/metadata 54 ExecStart=/usr/bin/bash -c 'echo "COREOS_ESX_IPV4_PRIVATE_0=$(ip addr show ens192 | grep -Po "inet \K[\d.]+")\nCOREOS_ESX_IPV4_PUBLIC_0=$(ip addr show ens192 | grep -Po "inet \K[\d.]+")" > ${OUTPUT}'`, false) 55 56 instance, err := ec.flight.api.CreateDevice(ec.vmname(), conf) 57 if err != nil { 58 return nil, err 59 } 60 61 mach := &machine{ 62 cluster: ec, 63 mach: instance, 64 } 65 66 mach.dir = filepath.Join(ec.RuntimeConf().OutputDir, mach.ID()) 67 if err := os.Mkdir(mach.dir, 0777); err != nil { 68 mach.Destroy() 69 return nil, err 70 } 71 72 confPath := filepath.Join(mach.dir, "user-data") 73 if err := conf.WriteFile(confPath); err != nil { 74 mach.Destroy() 75 return nil, err 76 } 77 78 if mach.journal, err = platform.NewJournal(mach.dir); err != nil { 79 mach.Destroy() 80 return nil, err 81 } 82 83 if err := platform.StartMachine(mach, mach.journal); err != nil { 84 mach.Destroy() 85 return nil, err 86 } 87 88 ec.AddMach(mach) 89 90 return mach, nil 91 } 92 93 func (ec *cluster) Destroy() { 94 ec.BaseCluster.Destroy() 95 ec.flight.DelCluster(ec) 96 }