runtimeinfo.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. WebuiDirectory string
  23. ThemesDirectory string
  24. }
  25. var Runtime = &RuntimeInfo{
  26. OS: runtime.GOOS,
  27. Arch: runtime.GOARCH,
  28. InContainer: isInContainer(),
  29. OSReleasePrettyName: getOsReleasePrettyName(),
  30. User: os.Getenv("USER"),
  31. Uid: os.Getenv("UID"),
  32. SshFoundKey: searchForSshKey(),
  33. SshFoundConfig: searchForSshConfig(),
  34. }
  35. func fileExists(path string) bool {
  36. if _, err := os.Stat(path); err == nil {
  37. return true
  38. }
  39. return false
  40. }
  41. func searchForSshKey() string {
  42. if fileExists("/config/ssh/id_rsa") {
  43. return "/config/ssh/id_rsa"
  44. }
  45. return searchForHomeFile(".ssh/id_rsa")
  46. }
  47. func searchForSshConfig() string {
  48. if fileExists("/config/ssh/config") {
  49. return "/config/ssh/config"
  50. }
  51. return searchForHomeFile(".ssh/config")
  52. }
  53. func searchForHomeFile(file string) string {
  54. path, _ := filepath.Abs(path.Join(os.Getenv("HOME"), file))
  55. if _, err := os.Stat(path); err == nil {
  56. return path
  57. }
  58. return "not found at " + path
  59. }
  60. func isInContainer() bool {
  61. if _, err := os.Stat("/.dockerenv"); errors.Is(err, os.ErrNotExist) {
  62. return false
  63. }
  64. return true
  65. }
  66. func getOsReleasePrettyName() string {
  67. handle, err := os.Open("/etc/os-release")
  68. if err != nil {
  69. return ""
  70. }
  71. scanner := bufio.NewScanner(handle)
  72. scanner.Split(bufio.ScanLines)
  73. for scanner.Scan() {
  74. line := scanner.Text()
  75. if strings.Contains(line, "PRETTY_NAME") {
  76. return line
  77. }
  78. }
  79. handle.Close()
  80. return "notfound"
  81. }