vitess.io/vitess@v0.16.2/go/vt/env/env.go (about) 1 /* 2 Copyright 2019 The Vitess 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 17 package env 18 19 import ( 20 "errors" 21 "fmt" 22 "os" 23 "os/exec" 24 "path" 25 "path/filepath" 26 "strings" 27 ) 28 29 const ( 30 // DefaultVtDataRoot is the default value for VTROOT environment variable 31 DefaultVtDataRoot = "/vt" 32 // DefaultVtRoot is only required for hooks 33 DefaultVtRoot = "/usr/local/vitess" 34 ) 35 36 // VtRoot returns $VTROOT or tries to guess its value if it's not set. 37 // This is the root for the 'vt' distribution, which contains bin/vttablet 38 // for instance. 39 func VtRoot() (root string, err error) { 40 if root = os.Getenv("VTROOT"); root != "" { 41 return root, nil 42 } 43 command, err := filepath.Abs(os.Args[0]) 44 if err != nil { 45 return 46 } 47 dir := path.Dir(command) 48 49 if strings.HasSuffix(dir, "/bin") { 50 return path.Dir(dir), nil 51 } 52 return DefaultVtRoot, nil 53 } 54 55 // VtDataRoot returns $VTDATAROOT or the default if $VTDATAROOT is not 56 // set. VtDataRoot does not check if the directory exists and is 57 // writable. 58 func VtDataRoot() string { 59 if dataRoot := os.Getenv("VTDATAROOT"); dataRoot != "" { 60 return dataRoot 61 } 62 63 return DefaultVtDataRoot 64 } 65 66 // VtMysqlRoot returns the root for the mysql distribution, 67 // which contains bin/mysql CLI for instance. 68 // If it is not set, look for mysqld in the path. 69 func VtMysqlRoot() (string, error) { 70 // if the environment variable is set, use that 71 if root := os.Getenv("VT_MYSQL_ROOT"); root != "" { 72 return root, nil 73 } 74 75 // otherwise let's look for mysqld in the PATH. 76 // ensure that /usr/sbin is included, as it might not be by default 77 // This is the default location for mysqld from packages. 78 newPath := fmt.Sprintf("/usr/sbin:%s", os.Getenv("PATH")) 79 os.Setenv("PATH", newPath) 80 path, err := exec.LookPath("mysqld") 81 if err != nil { 82 return "", errors.New("VT_MYSQL_ROOT is not set and no mysqld could be found in your PATH") 83 } 84 path = filepath.Dir(filepath.Dir(path)) // strip mysqld, and the sbin 85 return path, nil 86 } 87 88 // VtMysqlBaseDir returns the Mysql base directory, which 89 // contains the fill_help_tables.sql script for instance 90 func VtMysqlBaseDir() (string, error) { 91 // if the environment variable is set, use that 92 if root := os.Getenv("VT_MYSQL_BASEDIR"); root != "" { 93 return root, nil 94 } 95 96 // otherwise let's use VtMysqlRoot 97 root, err := VtMysqlRoot() 98 if err != nil { 99 return "", errors.New("VT_MYSQL_BASEDIR is not set. Please set $VT_MYSQL_BASEDIR") 100 } 101 return root, nil 102 }