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
+21 -10
View File
@@ -252,10 +252,15 @@ func fnCreate(c *cli.Context) error {
}
}
srcArchiveName := c.String("src")
deployArchiveName := c.String("code")
if len(deployArchiveName) == 0 {
deployArchiveName = c.String("deploy")
srcArchiveName := c.StringSlice("src")
var deployArchiveName []string
codeFlag := false
code := c.String("code")
if len(code) == 0 {
deployArchiveName = c.StringSlice("deploy")
} else {
deployArchiveName = append(deployArchiveName, c.String("code"))
codeFlag = true
}
// fatal when both src & deploy archive are empty
if len(srcArchiveName) == 0 && len(deployArchiveName) == 0 {
@@ -265,7 +270,7 @@ func fnCreate(c *cli.Context) error {
buildcmd := c.String("buildcmd")
// create new package in the same namespace as the function.
pkgMetadata = createPackage(client, fnNamespace, envName, envNamespace, srcArchiveName, deployArchiveName, buildcmd, specFile)
pkgMetadata = createPackage(client, fnNamespace, envName, envNamespace, srcArchiveName, deployArchiveName, buildcmd, specFile, codeFlag)
fmt.Printf("package '%v' created\n", pkgMetadata.Name)
}
@@ -466,11 +471,17 @@ func fnUpdate(c *cli.Context) error {
envNamespace = ""
}
deployArchiveName := c.String("code")
if len(deployArchiveName) == 0 {
deployArchiveName = c.String("deploy")
var deployArchiveName []string
codeFlag := false
code := c.String("code")
if len(code) == 0 {
deployArchiveName = c.StringSlice("deploy")
} else {
deployArchiveName = append(deployArchiveName, c.String("code"))
codeFlag = true
}
srcArchiveName := c.String("src")
srcArchiveName := c.StringSlice("src")
pkgName := c.String("pkg")
entrypoint := c.String("entrypoint")
buildcmd := c.String("buildcmd")
@@ -563,7 +574,7 @@ func fnUpdate(c *cli.Context) error {
log.Fatal("Package is used by multiple functions, use --force to force update")
}
pkgMetadata, err = updatePackage(client, pkg, pkg.Spec.Environment.Name, pkg.Spec.Environment.Namespace, srcArchiveName, deployArchiveName, buildcmd, false)
pkgMetadata, err = updatePackage(client, pkg, envName, envNamespace, srcArchiveName, deployArchiveName, buildcmd, false, codeFlag)
util.CheckErr(err, fmt.Sprintf("update package '%v'", pkgName))
fmt.Printf("package '%v' updated\n", pkgMetadata.GetName())
+4 -4
View File
@@ -90,8 +90,8 @@ func newCliApp() *cli.App {
fnNameFlag := cli.StringFlag{Name: "name", Usage: "function name"}
fnEnvNameFlag := cli.StringFlag{Name: "env", Usage: "environment name for function"}
fnCodeFlag := cli.StringFlag{Name: "code", Usage: "local path or URL for source code"}
fnDeployArchiveFlag := cli.StringFlag{Name: "deployarchive, deploy", Usage: "local path or URL for deployment archive"}
fnSrcArchiveFlag := cli.StringFlag{Name: "sourcearchive, src", Usage: "local path or URL for source archive"}
fnDeployArchiveFlag := cli.StringSliceFlag{Name: "deployarchive, deploy", Usage: "local path or URL for deployment archive"}
fnSrcArchiveFlag := cli.StringSliceFlag{Name: "sourcearchive, src", Usage: "local path or URL for source archive"}
fnPkgNameFlag := cli.StringFlag{Name: "pkgname, pkg", Usage: "Name of the existing package (--deploy and --src and --env will be ignored), should be in the same namespace as the function"}
fnPodFlag := cli.StringFlag{Name: "pod", Usage: "function pod name, optional (use latest if unspecified)"}
fnFollowFlag := cli.BoolFlag{Name: "follow, f", Usage: "specify if the logs should be streamed"}
@@ -234,8 +234,8 @@ func newCliApp() *cli.App {
pkgNameFlag := cli.StringFlag{Name: "name", Usage: "Package name"}
pkgForceFlag := cli.BoolFlag{Name: "force, f", Usage: "Force update a package even if it is used by one or more functions"}
pkgEnvironmentFlag := cli.StringFlag{Name: "env", Usage: "Environment name"}
pkgSrcArchiveFlag := cli.StringFlag{Name: "sourcearchive, src", Usage: "Local path or URL for source archive"}
pkgDeployArchiveFlag := cli.StringFlag{Name: "deployarchive, deploy", Usage: "Local path or URL for binary archive"}
pkgSrcArchiveFlag := cli.StringSliceFlag{Name: "sourcearchive, src", Usage: "Local path or URL for source archive"}
pkgDeployArchiveFlag := cli.StringSliceFlag{Name: "deployarchive, deploy", Usage: "Local path or URL for binary archive"}
pkgBuildCmdFlag := cli.StringFlag{Name: "buildcmd", Usage: "Build command for builder to run with"}
pkgOutputFlag := cli.StringFlag{Name: "output, o", Usage: "Output filename to save archive content"}
pkgOrphanFlag := cli.BoolFlag{Name: "orphan", Usage: "orphan packages that are not referenced by any function"}
+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
}
+3
View File
@@ -969,6 +969,9 @@ func localArchiveFromSpec(specDir string, aus *ArchiveUploadSpec) (*fission.Arch
// XXX if there are lots of globs it's probably more efficient
// to do a filepath.Walk and call path.Match on each path...
files := make([]string, 0)
if len(aus.IncludeGlobs) == 1 && archiver.Zip.Match(aus.IncludeGlobs[0]) {
files = append(files, aus.IncludeGlobs[0])
}
for _, relativeGlob := range aus.IncludeGlobs {
absGlob := rootDir + "/" + relativeGlob
f, err := filepath.Glob(absGlob)