runtimeinfo.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package installationinfo
  2. import (
  3. "bufio"
  4. "errors"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. )
  11. type runtimeInfo struct {
  12. OS string
  13. OSReleasePrettyName string
  14. Arch string
  15. InContainer bool
  16. LastBrowserUserAgent string
  17. User string
  18. Uid string
  19. SshFoundKey string
  20. SshFoundConfig string
  21. AvailableVersion string
  22. }
  23. var Runtime = &runtimeInfo{
  24. OS: runtime.GOOS,
  25. Arch: runtime.GOARCH,
  26. InContainer: isInContainer(),
  27. OSReleasePrettyName: getOsReleasePrettyName(),
  28. User: os.Getenv("USER"),
  29. Uid: os.Getenv("UID"),
  30. SshFoundKey: searchForSshKey(),
  31. SshFoundConfig: searchForSshConfig(),
  32. }
  33. func fileExists(path string) bool {
  34. if _, err := os.Stat(path); err == nil {
  35. return true
  36. }
  37. return false
  38. }
  39. func searchForSshKey() string {
  40. if fileExists("/config/ssh/id_rsa") {
  41. return "/config/ssh/id_rsa"
  42. }
  43. return searchForHomeFile(".ssh/id_rsa")
  44. }
  45. func searchForSshConfig() string {
  46. if fileExists("/config/ssh/config") {
  47. return "/config/ssh/config"
  48. }
  49. return searchForHomeFile(".ssh/config")
  50. }
  51. func searchForHomeFile(file string) string {
  52. path, _ := filepath.Abs(path.Join(os.Getenv("HOME"), file))
  53. if _, err := os.Stat(path); err == nil {
  54. return path
  55. }
  56. return "not found at " + path
  57. }
  58. func isInContainer() bool {
  59. if _, err := os.Stat("/.dockerenv"); errors.Is(err, os.ErrNotExist) {
  60. return false
  61. }
  62. return true
  63. }
  64. func getOsReleasePrettyName() string {
  65. handle, err := os.Open("/etc/os-release")
  66. if err != nil {
  67. return ""
  68. }
  69. scanner := bufio.NewScanner(handle)
  70. scanner.Split(bufio.ScanLines)
  71. for scanner.Scan() {
  72. line := scanner.Text()
  73. if strings.Contains(line, "PRETTY_NAME") {
  74. return line
  75. }
  76. }
  77. handle.Close()
  78. return "notfound"
  79. }