CLI: Reduce memory usage for plugin installation (#19639)

* grafana-cli: use tmp file when downloading plugin install file
This commit is contained in:
Olivier Lemasle
2019-11-06 13:42:58 +01:00
committed by Arve Knudsen
parent 46a4118461
commit b4712ec4b9
6 changed files with 115 additions and 65 deletions

View File

@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
@ -107,12 +108,24 @@ func InstallPlugin(pluginName, version string, c utils.CommandLine) error {
logger.Infof("into: %v\n", pluginFolder)
logger.Info("\n")
content, err := c.ApiClient().DownloadFile(pluginName, pluginFolder, downloadURL, checksum)
// Create temp file for downloading zip file
tmpFile, err := ioutil.TempFile("", "*.zip")
if err != nil {
return errutil.Wrap("Failed to create temporary file", err)
}
defer os.Remove(tmpFile.Name())
err = c.ApiClient().DownloadFile(pluginName, tmpFile, downloadURL, checksum)
if err != nil {
tmpFile.Close()
return errutil.Wrap("Failed to download plugin archive", err)
}
err = tmpFile.Close()
if err != nil {
return errutil.Wrap("Failed to close tmp file", err)
}
err = extractFiles(content, pluginName, pluginFolder, isInternal)
err = extractFiles(tmpFile.Name(), pluginName, pluginFolder, isInternal)
if err != nil {
return errutil.Wrap("Failed to extract plugin archive", err)
}
@ -197,8 +210,10 @@ func RemoveGitBuildFromName(pluginName, filename string) string {
var permissionsDeniedMessage = "Could not create %s. Permission denied. Make sure you have write access to plugindir"
func extractFiles(body []byte, pluginName string, filePath string, allowSymlinks bool) error {
r, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
func extractFiles(archiveFile string, pluginName string, filePath string, allowSymlinks bool) error {
logger.Debugf("Extracting archive %v to %v...\n", archiveFile, filePath)
r, err := zip.OpenReader(archiveFile)
if err != nil {
return err
}