updates.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package updates
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. "time"
  9. )
  10. //Download updates from given URL, return real time progress of stage (int), progress (int) and status text (string)
  11. func DownloadUpdatesFromURL(binaryURL string, webpackURL string, progressUpdateFunction func(int, float64, string)) error {
  12. //Create the update download folder
  13. os.RemoveAll("./updates")
  14. os.MkdirAll("./updates", 0755)
  15. //Get the total size of download expected
  16. binarySize, webpackSize, err := GetUpdateSizes(binaryURL, webpackURL)
  17. if err != nil {
  18. return errors.New("Unable to access given URL")
  19. }
  20. //Generate the download position
  21. binaryDownloadTarget := "./updates/" + filepath.Base(binaryURL)
  22. webpackDownloadTarget := "./updates/" + filepath.Base(webpackURL)
  23. //Check if the webpack is .tar.gz
  24. if filepath.Ext(webpackDownloadTarget) != ".gz" {
  25. //This is not a gzip file
  26. return errors.New("Webpack in invalid compression format")
  27. }
  28. //Start the download of binary
  29. //0 = false, 1 = true, 2 = error
  30. binaryDownloadComplete := 0
  31. webpackDownloadComplete := 0
  32. errorMessage := ""
  33. go func() {
  34. for binaryDownloadComplete == 0 {
  35. binaryFileSize := getFileSize(binaryDownloadTarget)
  36. progress := (float64(binaryFileSize) / float64(binarySize) * 100)
  37. progressUpdateFunction(0, progress, "Downloading binary")
  38. time.Sleep(100 * time.Millisecond)
  39. }
  40. if binaryDownloadComplete == 1 {
  41. progressUpdateFunction(0, 100, "Binary Download Completed")
  42. } else {
  43. progressUpdateFunction(0, 100, "Error: "+errorMessage)
  44. //Remove the update folder
  45. os.RemoveAll("./updates/")
  46. }
  47. }()
  48. err = downloadFile(binaryURL, binaryDownloadTarget)
  49. if err != nil {
  50. errorMessage = err.Error()
  51. binaryDownloadComplete = 2
  52. return err
  53. }
  54. binaryDownloadComplete = 1
  55. //Downlaod webpack
  56. go func() {
  57. for webpackDownloadComplete == 0 {
  58. webpackFileSize := getFileSize(webpackDownloadTarget)
  59. progress := (float64(webpackFileSize) / float64(webpackSize) * 100)
  60. progressUpdateFunction(1, progress, "Downloading webpack")
  61. time.Sleep(100 * time.Millisecond)
  62. }
  63. if webpackDownloadComplete == 1 {
  64. progressUpdateFunction(1, 100, "Webpack Download Completed")
  65. } else {
  66. progressUpdateFunction(1, 100, "Error: "+errorMessage)
  67. //Remove the update folder
  68. os.RemoveAll("./updates/")
  69. }
  70. }()
  71. err = downloadFile(webpackURL, webpackDownloadTarget)
  72. if err != nil {
  73. errorMessage = err.Error()
  74. webpackDownloadComplete = 2
  75. return err
  76. }
  77. webpackDownloadComplete = 1
  78. //Download completed. Try unzip webpack
  79. gzipstrean, err := os.Open(webpackDownloadTarget)
  80. if err != nil {
  81. return err
  82. }
  83. progressUpdateFunction(2, 0, "Extracting webpack")
  84. err = extractTarGz(gzipstrean, "./updates/", progressUpdateFunction)
  85. if err != nil {
  86. return err
  87. }
  88. gzipstrean.Close()
  89. //Remove the webpack compressed file
  90. os.Remove(webpackDownloadTarget)
  91. progressUpdateFunction(3, 100, "Updates Downloaded")
  92. return nil
  93. }
  94. //Get the update sizes, return binary size, webpack size and error if any
  95. func GetUpdateSizes(binaryURL string, webpackURL string) (int, int, error) {
  96. bps, err := getDownloadFileSize(binaryURL)
  97. if err != nil {
  98. return -1, -1, err
  99. }
  100. wps, err := getDownloadFileSize(webpackURL)
  101. if err != nil {
  102. return -1, -1, err
  103. }
  104. return bps, wps, nil
  105. }
  106. func GetLauncherVersion() (string, error) {
  107. //Check if there is a launcher listening to port 25576
  108. client := http.Client{
  109. Timeout: 3 * time.Second,
  110. }
  111. resp, err := client.Get("http://127.0.0.1:25576/chk")
  112. if err != nil {
  113. return "", errors.New("No launcher found. Unable to restart")
  114. }
  115. content, err := ioutil.ReadAll(resp.Body)
  116. if err != nil {
  117. return "", errors.New("Read launcher response failed")
  118. }
  119. return string(content), nil
  120. }
  121. func CheckLauncherPortResponsive() bool {
  122. client := http.Client{
  123. Timeout: 3 * time.Second,
  124. }
  125. _, err := client.Get("http://127.0.0.1:25576/chk")
  126. if err != nil {
  127. return false
  128. }
  129. return true
  130. }