install_linux.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2018 The etcd Authors
  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. // +build linux
  15. package main
  16. import (
  17. "fmt"
  18. "io/ioutil"
  19. "net/http"
  20. "os/exec"
  21. "path/filepath"
  22. "go.etcd.io/etcd/pkg/fileutil"
  23. )
  24. const downloadURL = `https://storage.googleapis.com/etcd/%s/etcd-%s-linux-amd64.tar.gz`
  25. func install(ver, dir string) (string, error) {
  26. ep := fmt.Sprintf(downloadURL, ver, ver)
  27. resp, err := http.Get(ep)
  28. if err != nil {
  29. return "", err
  30. }
  31. defer resp.Body.Close()
  32. d, err := ioutil.ReadAll(resp.Body)
  33. if err != nil {
  34. return "", err
  35. }
  36. tarPath := filepath.Join(dir, "etcd.tar.gz")
  37. if err = ioutil.WriteFile(tarPath, d, fileutil.PrivateFileMode); err != nil {
  38. return "", err
  39. }
  40. if err = exec.Command("bash", "-c", fmt.Sprintf("tar xzvf %s -C %s --strip-components=1", tarPath, dir)).Run(); err != nil {
  41. return "", err
  42. }
  43. return filepath.Join(dir, "etcd"), nil
  44. }