github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/vsphere/vsphere_provider.go (about) 1 package vsphere 2 3 import ( 4 "net/url" 5 "path/filepath" 6 "strings" 7 "os" 8 9 "github.com/sirupsen/logrus" 10 "github.com/emc-advanced-dev/pkg/errors" 11 "github.com/solo-io/unik/pkg/config" 12 "github.com/solo-io/unik/pkg/providers/common" 13 "github.com/solo-io/unik/pkg/providers/vsphere/vsphereclient" 14 "github.com/solo-io/unik/pkg/state" 15 "time" 16 ) 17 18 func VsphereStateFile() string { 19 return filepath.Join(config.Internal.UnikHome, "vsphere/state.json") 20 } 21 22 var VsphereImagesDirectory = "unik/vsphere/images/" 23 var VsphereVolumesDirectory = "unik/vsphere/volumes/" 24 25 const VsphereUnikInstanceListener = "VsphereUnikInstanceListener" 26 const instanceListenerPrefix = "unik_vsphere" 27 28 type VsphereProvider struct { 29 config config.Vsphere 30 state state.State 31 u *url.URL 32 instanceListenerIp string 33 } 34 35 func NewVsphereProvier(config config.Vsphere) (*VsphereProvider, error) { 36 rawUrl := "https://" + config.VsphereUser + ":" + config.VspherePassword + "@" + strings.TrimSuffix(strings.TrimPrefix(strings.TrimPrefix(config.VsphereURL, "http://"), "https://"), "/sdk") + "/sdk" 37 u, err := url.Parse(rawUrl) 38 if err != nil { 39 return nil, errors.New("parsing vsphere url", err) 40 } 41 42 p := &VsphereProvider{ 43 config: config, 44 state: state.NewBasicState(VsphereStateFile()), 45 u: u, 46 } 47 48 p.getClient().Mkdir("unik") 49 p.getClient().Mkdir("unik/vsphere") 50 p.getClient().Mkdir("unik/vsphere/images") 51 p.getClient().Mkdir("unik/vsphere/volumes") 52 53 if err := p.deployInstanceListener(); err != nil { 54 return nil, errors.New("deploying vsphere instance listener", err) 55 } 56 57 instanceListenerIp, err := common.GetInstanceListenerIp(instanceListenerPrefix, timeout) 58 if err != nil { 59 return nil, errors.New("failed to retrieve instance listener ip. is unik instance listener running?", err) 60 } 61 62 p.instanceListenerIp = instanceListenerIp 63 64 tmpDir := filepath.Join(os.Getenv("HOME"), ".unik", "tmp") 65 os.Setenv("TMPDIR", tmpDir) 66 logrus.Infof("Creating directory %s", tmpDir) 67 os.MkdirAll(tmpDir, 0755) 68 69 // begin update instances cycle 70 go func() { 71 for { 72 if err := p.syncState(); err != nil { 73 logrus.Error("error updating vsphere state:", err) 74 } 75 time.Sleep(time.Second) 76 } 77 }() 78 79 return p, nil 80 } 81 82 func (p *VsphereProvider) WithState(state state.State) *VsphereProvider { 83 p.state = state 84 return p 85 } 86 87 func (p *VsphereProvider) getClient() *vsphereclient.VsphereClient { 88 return vsphereclient.NewVsphereClient(p.u, p.config.Datastore, p.config.Datacenter) 89 } 90 91 //just for consistency 92 func getInstanceDatastoreDir(instanceName string) string { 93 return instanceName 94 } 95 96 func getImageDatastoreDir(imageName string) string { 97 return filepath.Join(VsphereImagesDirectory, imageName+"/") 98 } 99 100 func getImageDatastorePath(imageName string) string { 101 return filepath.Join(getImageDatastoreDir(imageName), "boot.vmdk") 102 } 103 104 func getVolumeDatastoreDir(volumeName string) string { 105 return filepath.Join(VsphereVolumesDirectory, volumeName+"/") 106 } 107 108 func getVolumeDatastorePath(volumeName string) string { 109 return filepath.Join(getVolumeDatastoreDir(volumeName), "data.vmdk") 110 }