2016-07-13 21:10:10 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-07-25 14:24:22 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2016-07-13 21:10:10 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2016-07-25 14:24:22 +00:00
|
|
|
"strings"
|
2016-07-25 18:02:30 +00:00
|
|
|
|
2016-09-09 21:48:39 +00:00
|
|
|
"github.com/kubernetes-incubator/ocid/utils"
|
2016-07-13 21:10:10 +00:00
|
|
|
)
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
const (
|
|
|
|
// According to http://man7.org/linux/man-pages/man5/resolv.conf.5.html:
|
|
|
|
// "The search list is currently limited to six domains with a total of 256 characters."
|
|
|
|
maxDNSSearches = 6
|
|
|
|
)
|
|
|
|
|
|
|
|
func int64Ptr(i int64) *int64 {
|
|
|
|
return &i
|
|
|
|
}
|
|
|
|
|
|
|
|
func int32Ptr(i int32) *int32 {
|
|
|
|
return &i
|
|
|
|
}
|
|
|
|
|
|
|
|
func sPtr(s string) *string {
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
2016-07-13 21:10:10 +00:00
|
|
|
func getGPRCVersion() (string, error) {
|
|
|
|
_, file, _, ok := runtime.Caller(0)
|
|
|
|
if !ok {
|
2016-09-19 20:09:11 +00:00
|
|
|
return "", errors.New("failed to recover the caller information")
|
2016-07-13 21:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ocidRoot := filepath.Dir(filepath.Dir(file))
|
|
|
|
p := filepath.Join(ocidRoot, "Godeps/Godeps.json")
|
|
|
|
|
|
|
|
grepCmd := fmt.Sprintf(`grep -r "\"google.golang.org/grpc\"" %s -A 1 | grep "\"Rev\"" | cut -d: -f2 | tr -d ' "\n'`, p)
|
|
|
|
|
2016-07-29 22:35:10 +00:00
|
|
|
out, err := utils.ExecCmd("bash", "-c", grepCmd)
|
2016-07-13 21:10:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-07-19 18:53:57 +00:00
|
|
|
return out, nil
|
2016-07-13 21:10:10 +00:00
|
|
|
}
|
2016-07-25 14:24:22 +00:00
|
|
|
|
|
|
|
func copyFile(src, dest string) error {
|
|
|
|
in, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
|
|
|
|
out, err := os.Create(dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
_, err = io.Copy(out, in)
|
|
|
|
return err
|
2016-07-25 14:24:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeFile(path string) error {
|
|
|
|
if _, err := os.Stat(path); err == nil {
|
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseDNSOptions(servers, searches []string, path string) error {
|
|
|
|
nServers := len(servers)
|
|
|
|
nSearches := len(searches)
|
|
|
|
if nServers == 0 && nSearches == 0 {
|
|
|
|
return copyFile("/etc/resolv.conf", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nSearches > maxDNSSearches {
|
|
|
|
return fmt.Errorf("DNSOption.Searches has more than 6 domains")
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if nSearches > 0 {
|
|
|
|
data := fmt.Sprintf("search %s\n", strings.Join(searches, " "))
|
|
|
|
_, err = f.Write([]byte(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if nServers > 0 {
|
|
|
|
data := fmt.Sprintf("nameserver %s\n", strings.Join(servers, "\nnameserver "))
|
|
|
|
_, err = f.Write([]byte(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|