runtimeinfo.go 2.1 KB

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