Skip to content

Commit

Permalink
Add validation of package at install time, and allow installing from URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Anagrius authored and anagrius committed Sep 24, 2020
1 parent 6d5d5cc commit 2e0616d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 18 deletions.
26 changes: 17 additions & 9 deletions api/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ func (p *Packages) Validate(repoOrViewName string, absDiretoryPath string) (*Val
}

// InstallArchive installs a local package (zip file).
func (p *Packages) InstallArchive(repoOrViewName string, pathToZip string) error {
func (p *Packages) InstallArchive(repoOrViewName string, pathToZip string) (*ValidationResponse, error) {

fileReader, openErr := os.Open(pathToZip)

if openErr != nil {
return openErr
return nil, openErr
}
defer fileReader.Close()

Expand All @@ -85,14 +85,22 @@ func (p *Packages) InstallArchive(repoOrViewName string, pathToZip string) error
response, httpErr := p.client.HTTPRequestContext(context.Background(), "POST", urlPath, fileReader, "application/zip")

if httpErr != nil {
return httpErr
return nil, httpErr
}

if response.StatusCode >= 400 {
return fmt.Errorf("Bad response. %s", response.Status)
return nil, fmt.Errorf("Bad response. %s", response.Status)
}

return nil
var report ValidationResponse
decoder := json.NewDecoder(response.Body)
decodeErr := decoder.Decode(&report)

if decodeErr != nil {
return nil, decodeErr
}

return &report, nil
}

type (
Expand Down Expand Up @@ -134,24 +142,24 @@ func (p *Packages) CreateArchive(packageDirPath string, targetFileName string) e
}

// InstallFromDirectory installs a package from a directory containing the package files.
func (p *Packages) InstallFromDirectory(packageDirPath string, targetRepoOrView string) error {
func (p *Packages) InstallFromDirectory(packageDirPath string, targetRepoOrView string) (*ValidationResponse, error) {
zipFilePath, err := createTempZipFromFolder(packageDirPath)

if err != nil {
return err
return nil, err
}

zipFile, err := os.Open(zipFilePath)

if err != nil {
return err
return nil, err
}

defer zipFile.Close()
defer os.Remove(zipFile.Name())

if err != nil {
return err
return nil, err
}

return p.InstallArchive(targetRepoOrView, zipFilePath)
Expand Down
76 changes: 70 additions & 6 deletions cmd/humioctl/packages_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ package main

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/humio/cli/api"
"github.com/humio/cli/prompt"

"github.com/spf13/cobra"
Expand All @@ -35,25 +39,45 @@ func installPackageCmd() *cobra.Command {

out.Info(fmt.Sprintf("Installing Package from: %s", path))

// Get the HTTP client
client := NewApiClient(cmd)
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
downloadedFile, err := getURLPackage(path)

if err != nil {
out.Error(fmt.Sprintf("Failed to download: %s %s", path, err))
os.Exit(1)
}

// defer os.Remove(downloadedFile.Name())

path = downloadedFile.Name()
println(path)
}

var createErr error
isDir, err := isDirectory(path)
println(path)

if err != nil {
out.Error(fmt.Sprintf("Errors installing archive: %s", err))
os.Exit(1)
}

// Get the HTTP client
client := NewApiClient(cmd)

var validationResult *api.ValidationResponse
var createErr error

if isDir {
createErr = client.Packages().InstallFromDirectory(path, repoOrView)
validationResult, createErr = client.Packages().InstallFromDirectory(path, repoOrView)
} else {
createErr = client.Packages().InstallArchive(repoOrView, path)
validationResult, createErr = client.Packages().InstallArchive(repoOrView, path)
}

if createErr != nil {
out.Error(fmt.Sprintf("Errors installing archive: %s", createErr))
os.Exit(1)
} else if !validationResult.IsValid() {
printValidation(out, validationResult)
os.Exit(1)
}
},
}
Expand All @@ -68,3 +92,43 @@ func isDirectory(path string) (bool, error) {
}
return fileInfo.IsDir(), err
}

func getURLPackage(url string) (*os.File, error) {
// Github URLs should have the .got suffix removed
if strings.HasPrefix(url, "https://github.com") || strings.HasPrefix(url, "http://github.com") {
url = strings.TrimSuffix(url, ".git")
}

zipBallURL := url + "/zipball/master/"
response, err := http.Get(zipBallURL)

if err != nil {
return nil, err
}

if response.StatusCode >= 400 {
return nil, fmt.Errorf("error downloading file %s: %s", zipBallURL, response.Status)
}

defer response.Body.Close()
zipContent, err := ioutil.ReadAll(response.Body)

if err != nil {
return nil, err
}

tempDir := os.TempDir()
zipFile, err := ioutil.TempFile(tempDir, "humio-package.*.zip")

if err != nil {
return nil, err
}

_, err = zipFile.Write(zipContent)

if err != nil {
return nil, err
}

return zipFile, nil
}
12 changes: 9 additions & 3 deletions cmd/humioctl/packages_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"os"
"path/filepath"

"github.com/humio/cli/api"

"github.com/humio/cli/prompt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -59,13 +61,17 @@ func validatePackageCmd() *cobra.Command {
if validationResult.IsValid() {
out.Info("Package is valid")
} else {
out.Error("Package is not valid")
out.Error(out.List(validationResult.InstallationErrors))
out.Error(out.List(validationResult.ParseErrors))
printValidation(out, validationResult)
os.Exit(1)
}
},
}

return &cmd
}

func printValidation(out *prompt.Prompt, validationResult *api.ValidationResponse) {
out.Error("Package is not valid")
out.Error(out.List(validationResult.InstallationErrors))
out.Error(out.List(validationResult.ParseErrors))
}

0 comments on commit 2e0616d

Please sign in to comment.