install -f - support stdin && add example

This commit is contained in:
louisehong
2020-08-04 15:52:50 +08:00
parent c4e98d2389
commit 816b702221
3 changed files with 46 additions and 9 deletions
+39 -5
View File
@@ -15,27 +15,52 @@
package cmd
import (
"bytes"
"github.com/fanux/sealos/install"
"github.com/spf13/cobra"
"github.com/wonderivan/logger"
"io/ioutil"
"os"
)
var AppURL string
var (
AppURL string
installExample string = `
# Apply the configuration in values.yaml to a kubernetes Cluster.
sealos install --pkg-url /root/dashboard.tar -f values.yaml
# Apply the yaml passed into stdin to a kubenertes Cluster.
cat values.yaml | sealos install --pkg-url /root/dashboard.tar -f -
# Set the WorkDir for your Package
sealos install --pkg-url /root/dashboard.tar -w /data
`
)
// installCmd represents the install command
var installCmd = &cobra.Command{
Use: "install",
Short: "install kubernetes apps, like dashboard prometheus ..",
Long: `sealos install --pkg-url /root/dashboard.tar --workdir /data \
Long: `sealos install --pkg-url /root/dashboard.tar --workdir /data \
-f /root/values.yaml -c /root/config`,
Example: installExample,
Run: func(cmd *cobra.Command, args []string) {
// 处理stdin
if install.Values == "-" {
err := ReadStdin()
if err != nil {
logger.Error(err)
os.Exit(-1)
}
}
install.AppInstall(AppURL)
},
PreRun: func(cmd *cobra.Command, args []string) {
if install.ExitInstallCase(AppURL) {
if install.ExitInstallCase(AppURL) {
cmd.Help()
os.Exit(install.ErrorExitOSCase)
}
},
}
var name string
@@ -45,6 +70,15 @@ func init() {
installCmd.Flags().StringVar(&AppURL, "pkg-url", "", "http://store.lameleg.com/prometheus.tar.gz download offline plugins package url, or file localtion ex. /root/prometheus.tar.gz")
installCmd.Flags().StringVarP(&install.Workdir, "workdir", "w", "/root/", "workdir for install package home ex. sealos install --pkg-url dashboard.tar --workdir /data")
installCmd.Flags().StringVarP(&install.PackageConfig, "pkg-config","c", "", `packageConfig for install package config ex. sealos install --pkg-url dashboard.tar -c config`)
installCmd.Flags().StringVarP(&install.Values, "values","f", "", "values for install package values.yaml , you know what you did .ex. sealos install --pkg-url dashboard.tar -f values.yaml")
installCmd.Flags().StringVarP(&install.PackageConfig, "pkg-config", "c", "", `packageConfig for install package config ex. sealos install --pkg-url dashboard.tar -c config`)
installCmd.Flags().StringVarP(&install.Values, "values", "f", "", "values for install package values.yaml , you know what you did .ex. sealos install --pkg-url dashboard.tar -f values.yaml")
}
func ReadStdin() error {
var b bytes.Buffer
_, err := b.ReadFrom(os.Stdin)
if err != nil {
return err
}
return ioutil.WriteFile(install.Workdir+"values.yaml", b.Bytes(), 0660)
}
+4 -1
View File
@@ -183,7 +183,10 @@ func (r *RunOnEveryNodes) Run(config SealConfig, url, pkgName, workdir string) {
nodes := append(config.Masters, config.Nodes...)
// values.yaml 存在, 则将 values.yaml复制到各个节点。
if Values != "" {
if Values == "-" {
// 处理 stdin
SendPackage(workdir+"values.yaml", nodes, workspace, nil, nil)
} else if Values != "" {
SendPackage(Values, nodes, workspace, nil, nil)
}
SendPackage(url, nodes, workspace, nil, nil)
+3 -3
View File
@@ -56,9 +56,9 @@ func ExitInitCase() bool {
}
func ExitInstallCase(pkgUrl string) bool {
// values.yaml 使用了-f 但是文件不存在.
if Values != "" && !FileExist(Values) {
logger.Error("your values File is not exist, Please check your Values.yaml is exist")
// values.yaml 使用了-f 但是文件不存在. 并且不使用 stdin
if Values != "-" && !FileExist(Values) && Values !="" {
logger.Error("your values File is not exist and you have no stdin input, Please check your Values.yaml is exist")
return true
}
// PackageConfig 使用了-c 但是文件不存在