Improve archive package user experience (#927)

This commit is contained in:
Vishal
2018-11-29 23:00:38 +08:00
committed by Ta-Ching Chen
parent 7cedf8d580
commit 19b52938c3
10 changed files with 235 additions and 110 deletions
+53 -17
View File
@@ -30,10 +30,12 @@ import (
"path/filepath"
"strings"
"text/tabwriter"
"time"
"github.com/dchest/uniuri"
"github.com/fission/fission/fission/util"
storageSvcClient "github.com/fission/fission/storagesvc/client"
"github.com/mholt/archiver"
"github.com/satori/go.uuid"
"github.com/urfave/cli"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -82,15 +84,15 @@ func pkgCreate(c *cli.Context) error {
log.Fatal("Need --env argument.")
}
envNamespace := c.String("envNamespace")
srcArchiveName := c.String("src")
deployArchiveName := c.String("deploy")
srcArchive := c.StringSlice("src")
deployArchive := c.StringSlice("deploy")
buildcmd := c.String("buildcmd")
if len(srcArchiveName) == 0 && len(deployArchiveName) == 0 {
if len(srcArchive) == 0 && len(deployArchive) == 0 {
log.Fatal("Need --src to specify source archive, or use --deploy to specify deployment archive.")
}
meta := createPackage(client, pkgNamespace, envName, envNamespace, srcArchiveName, deployArchiveName, buildcmd, "")
meta := createPackage(client, pkgNamespace, envName, envNamespace, srcArchive, deployArchive, buildcmd, "", false)
fmt.Printf("Package '%v' created\n", meta.GetName())
return nil
@@ -108,15 +110,15 @@ func pkgUpdate(c *cli.Context) error {
force := c.Bool("f")
envName := c.String("env")
envNamespace := c.String("envNamespace")
srcArchiveName := c.String("src")
deployArchiveName := c.String("deploy")
srcArchive := c.StringSlice("src")
deployArchive := c.StringSlice("deploy")
buildcmd := c.String("buildcmd")
if len(srcArchiveName) > 0 && len(deployArchiveName) > 0 {
if len(srcArchive) > 0 && len(deployArchive) > 0 {
log.Fatal("Need either of --src or --deploy and not both arguments.")
}
if len(srcArchiveName) == 0 && len(deployArchiveName) == 0 &&
if len(srcArchive) == 0 && len(deployArchive) == 0 &&
len(envName) == 0 && len(buildcmd) == 0 {
log.Fatal("Need --env or --src or --deploy or --buildcmd argument.")
}
@@ -146,7 +148,7 @@ func pkgUpdate(c *cli.Context) error {
}
newPkgMeta, err := updatePackage(client, pkg,
envName, envNamespace, srcArchiveName, deployArchiveName, buildcmd, false)
envName, envNamespace, srcArchive, deployArchive, buildcmd, false, false)
if err != nil {
util.CheckErr(err, "update package")
}
@@ -163,8 +165,8 @@ func pkgUpdate(c *cli.Context) error {
return nil
}
func updatePackage(client *client.Client, pkg *crd.Package, envName, envNamespace,
srcArchiveName, deployArchiveName, buildcmd string, forceRebuild bool) (*metav1.ObjectMeta, error) {
func updatePackage(client *client.Client, pkg *crd.Package, envName, envNamespace string,
srcArchive []string, deployArchive []string, buildcmd string, forceRebuild bool, codeFlag bool) (*metav1.ObjectMeta, error) {
var srcArchiveMetadata, deployArchiveMetadata *fission.Archive
needToBuild := false
@@ -184,13 +186,20 @@ func updatePackage(client *client.Client, pkg *crd.Package, envName, envNamespac
needToBuild = true
}
if len(srcArchiveName) > 0 {
if len(srcArchive) > 0 {
srcArchiveName := archiveParser(srcArchive, envName)
srcArchiveMetadata = createArchive(client, srcArchiveName, "")
pkg.Spec.Source = *srcArchiveMetadata
needToBuild = true
}
if len(deployArchiveName) > 0 {
if len(deployArchive) > 0 {
var deployArchiveName string
if codeFlag {
deployArchiveName = deployArchive[0]
} else {
deployArchiveName = archiveParser(deployArchive, envName)
}
deployArchiveMetadata = createArchive(client, deployArchiveName, "")
pkg.Spec.Deployment = *deployArchiveMetadata
// Users may update the env, envNS and deploy archive at the same time,
@@ -439,7 +448,7 @@ func pkgRebuild(c *cli.Context) error {
pkg.Metadata.Name, fission.BuildStatusFailed))
}
_, err = updatePackage(client, pkg, "", "", "", "", "", true)
_, err = updatePackage(client, pkg, "", "", nil, nil, "", true, false)
util.CheckErr(err, "update package")
fmt.Printf("Retrying build for pkg %v. Use \"fission pkg info --name %v\" to view status.\n", pkg.Metadata.Name, pkg.Metadata.Name)
@@ -530,7 +539,7 @@ func createArchive(client *client.Client, fileName string, specFile string) *fis
return &archive
}
func createPackage(client *client.Client, pkgNamespace, envName, envNamespace, srcArchiveName, deployArchiveName, buildcmd string, specFile string) *metav1.ObjectMeta {
func createPackage(client *client.Client, pkgNamespace string, envName string, envNamespace string, srcArchive []string, deployArchive []string, buildcmd string, specFile string, codeFlag bool) *metav1.ObjectMeta {
pkgSpec := fission.PackageSpec{
Environment: fission.EnvironmentReference{
Namespace: envNamespace,
@@ -540,14 +549,21 @@ func createPackage(client *client.Client, pkgNamespace, envName, envNamespace, s
var pkgStatus fission.BuildStatus = fission.BuildStatusSucceeded
var pkgName string
if len(deployArchiveName) > 0 {
if len(deployArchive) > 0 {
if len(specFile) > 0 { // we should do this in all cases, i think
pkgStatus = fission.BuildStatusNone
}
var deployArchiveName string
if codeFlag {
deployArchiveName = deployArchive[0]
} else {
deployArchiveName = archiveParser(deployArchive, envName)
}
pkgSpec.Deployment = *createArchive(client, deployArchiveName, specFile)
pkgName = util.KubifyName(fmt.Sprintf("%v-%v", path.Base(deployArchiveName), uniuri.NewLen(4)))
}
if len(srcArchiveName) > 0 {
if len(srcArchive) > 0 {
srcArchiveName := archiveParser(srcArchive, envName)
pkgSpec.Source = *createArchive(client, srcArchiveName, specFile)
// set pending status to package
pkgStatus = fission.BuildStatusPending
@@ -652,3 +668,23 @@ func downloadURL(fileUrl string) (io.ReadCloser, error) {
}
return resp.Body, nil
}
func archiveParser(archiveInput []string, envName string) string {
var archiveName = ""
if (len(archiveInput) == 1 && archiver.Zip.Match(archiveInput[0])) ||
(len(archiveInput) == 1 && (strings.HasPrefix(archiveInput[0], "http://") || strings.HasPrefix(archiveInput[0], "https://"))) {
return archiveInput[0]
}
tmpDir, err := fission.GetTempDir()
if err != nil {
util.CheckErr(err, "create archive file")
}
archiveName, err = fission.MakeArchive(filepath.Join(tmpDir, fmt.Sprintf("%v-%v", envName, time.Now().Unix())), archiveInput...)
if err != nil {
util.CheckErr(err, "create archive file")
}
return archiveName
}