From e0d96b152df60381afebae8b93c94ceb27420d5b Mon Sep 17 00:00:00 2001 From: LianBo Date: Mon, 13 Feb 2023 19:49:22 +0800 Subject: [PATCH 1/4] Fix some issues with install.sh --- scripts/install.sh | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/scripts/install.sh b/scripts/install.sh index 0083c2e527..9ce2ffb17e 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -2,6 +2,8 @@ set -e +RED='\033[0;31m' +NC='\033[0m' # First - check OS. OS="$(uname)" @@ -11,7 +13,8 @@ if [[ "${OS}" == "Linux" ]]; then elif [[ "${OS}" == "Darwin" ]];then OS=darwin else - abort "Unsupport OS - ${OS}" + echo -e "Error: ${RED}Unsupport OS - ${OS}${NC}" + exit fi # Second - check the CPU arch @@ -21,10 +24,11 @@ if [[ $ARCH == x86_64 ]]; then ARCH=amd64 elif [[ $ARCH == i686 || $ARCH == i386 ]]; then ARCH=386 -elif [[ $ARCH == aarch64* || $ARCH == armv8* ]]; then +elif [[ $ARCH == aarch64* || $ARCH == armv8* || $ARCH == arm64* ]]; then ARCH=arm64 else - abort "Unsupport CPU - ${ARCH}" + echo -e "Error: ${RED}Unsupport CPU - ${ARCH}${NC}" + exit fi # Third - download the binaries @@ -44,7 +48,8 @@ echo "Create the directory - \"${DIR}\" successfully." echo "Downloading the release file - \"${ARTIFACT}\" ..." curl -sL ${ARTIFACT_URL} -o ${DIR}/${ARTIFACT} echo "Downloaded \"${ARTIFACT}\"" -tar -zxf ${DIR}/${ARTIFACT} -C easegress +tar -zxf ${DIR}/${ARTIFACT} -C "${DIR}" +rm ${DIR}/${ARTIFACT} echo "Extract the files successfully" # Fourth - configure the easegress @@ -82,3 +87,5 @@ if [[ "${OS}" == "linux" ]]; then sleep 2 systemctl status easegress fi + +echo "Installed successfully" From 0eb6141e9deda6879ba19e1ff3346455b55dd558 Mon Sep 17 00:00:00 2001 From: LianBo Date: Tue, 14 Feb 2023 13:48:50 +0800 Subject: [PATCH 2/4] remove the rm command --- scripts/install.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/install.sh b/scripts/install.sh index 9ce2ffb17e..5d38d9292f 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -49,7 +49,6 @@ echo "Downloading the release file - \"${ARTIFACT}\" ..." curl -sL ${ARTIFACT_URL} -o ${DIR}/${ARTIFACT} echo "Downloaded \"${ARTIFACT}\"" tar -zxf ${DIR}/${ARTIFACT} -C "${DIR}" -rm ${DIR}/${ARTIFACT} echo "Extract the files successfully" # Fourth - configure the easegress From 285b1d6689e6ca1f6be71ef69c9b7d2a67dbfb92 Mon Sep 17 00:00:00 2001 From: LianBo Date: Tue, 14 Feb 2023 18:04:21 +0800 Subject: [PATCH 3/4] Update scripts/install.sh Co-authored-by: Hao Chen --- scripts/install.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/install.sh b/scripts/install.sh index 5d38d9292f..dd75640c84 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -83,8 +83,9 @@ if [[ "${OS}" == "linux" ]]; then sudo systemctl start easegress #check the status - sleep 2 - systemctl status easegress + systemctl -q is-active easegress.service && \ + echo "Easegress service is running!" || \ + systemctl status easegress.service fi echo "Installed successfully" From db9de99458320687bf66fdb65a7cec02437e8bba Mon Sep 17 00:00:00 2001 From: LianBo Date: Mon, 13 Feb 2023 19:49:22 +0800 Subject: [PATCH 4/4] The option.Parse method should return failure or success without other logic --- cmd/server/main.go | 13 ++++++++----- pkg/cluster/cluster_test.go | 4 ++-- pkg/cluster/member_test.go | 2 +- pkg/cluster/test_util.go | 2 +- pkg/option/option.go | 39 +++++++++++++++++++------------------ scripts/install.sh | 19 ++++++------------ 6 files changed, 38 insertions(+), 41 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 57a215b5a0..8d62b6ceee 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -42,15 +42,18 @@ func main() { rand.Seed(time.Now().UnixNano()) opt := option.New() - msg, err := opt.Parse() - if err != nil { + if err := opt.Parse(); err != nil { common.Exit(1, err.Error()) } - if msg != "" { - common.Exit(0, msg) + + if opt.ShowVersion { + common.Exit(0, version.Short) + } + if opt.ShowHelp { + common.Exit(0, opt.FlagUsages()) } - err = env.InitServerDir(opt) + err := env.InitServerDir(opt) if err != nil { log.Printf("failed to init env: %v", err) os.Exit(1) diff --git a/pkg/cluster/cluster_test.go b/pkg/cluster/cluster_test.go index 16b139fc9a..2f4fa19634 100644 --- a/pkg/cluster/cluster_test.go +++ b/pkg/cluster/cluster_test.go @@ -95,7 +95,7 @@ func mockStaticClusterMembers(count int) ([]*option.Options, membersSlice, []*pb opt.LogDir = "log" opt.MemberDir = "member" opt.Debug = false - _, err = opt.Parse() // create directories + err = opt.Parse() // create directories if err != nil { panic(fmt.Errorf("parse option failed: %v", err)) } @@ -184,7 +184,7 @@ func createSecondaryNode(clusterName string, primaryListenPeerURLs []string) *cl opt.Cluster.PrimaryListenPeerURLs = primaryListenPeerURLs opt.APIAddr = fmt.Sprintf("localhost:%d", ports[0]) - _, err = opt.Parse() + err = opt.Parse() check(err) env.InitServerDir(opt) diff --git a/pkg/cluster/member_test.go b/pkg/cluster/member_test.go index 0dbcf297a8..dc883a999d 100644 --- a/pkg/cluster/member_test.go +++ b/pkg/cluster/member_test.go @@ -53,7 +53,7 @@ func mockTestOpt(ports []int) *option.Options { opt.MemberDir = "member" opt.Debug = false - if _, err := opt.Parse(); err != nil { + if err := opt.Parse(); err != nil { panic(fmt.Errorf("parse option failed: %v", err)) } diff --git a/pkg/cluster/test_util.go b/pkg/cluster/test_util.go index f3e6b16ed5..a72c5d1222 100644 --- a/pkg/cluster/test_util.go +++ b/pkg/cluster/test_util.go @@ -53,7 +53,7 @@ func CreateOptionsForTest(tempDir string) *option.Options { opt.LogDir = fmt.Sprintf("%s/log", tempDir) opt.MemberDir = fmt.Sprintf("%s/member", tempDir) - _, err = opt.Parse() + err = opt.Parse() check(err) env.InitServerDir(opt) diff --git a/pkg/option/option.go b/pkg/option/option.go index 562356009c..3f7941f477 100644 --- a/pkg/option/option.go +++ b/pkg/option/option.go @@ -32,7 +32,6 @@ import ( "github.com/megaease/easegress/pkg/common" "github.com/megaease/easegress/pkg/util/codectool" - "github.com/megaease/easegress/pkg/version" ) // ClusterOptions defines the cluster members. @@ -159,7 +158,7 @@ func New() *Options { opt.flags.IntVar(&opt.StatusUpdateMaxBatchSize, "status-update-max-batch-size", 20, "Number of object statuses to update at maximum in one transaction.") - opt.viper.BindPFlags(opt.flags) + _ = opt.viper.BindPFlags(opt.flags) return opt } @@ -181,27 +180,24 @@ func (opt *Options) renameLegacyClusterRoles() { fmtLogger := fmt.Printf // Importing logger here is an import cycle, so use fmt instead. if opt.ClusterRole == "writer" { opt.ClusterRole = "primary" - fmtLogger(warning, "writer", "primary") + _, _ = fmtLogger(warning, "writer", "primary") } if opt.ClusterRole == "reader" { opt.ClusterRole = "secondary" - fmtLogger(warning, "reader", "secondary") + _, _ = fmtLogger(warning, "reader", "secondary") } } -// Parse parses all arguments, returns normal message without error if --help/--version set. -func (opt *Options) Parse() (string, error) { +// Parse parses all arguments, when the user wants to display version information or view help, +// we do not execute subsequent logic and return directly. +func (opt *Options) Parse() error { err := opt.flags.Parse(os.Args[1:]) if err != nil { - return "", err - } - - if opt.ShowVersion { - return version.Short, nil + return err } - if opt.ShowHelp { - return opt.flags.FlagUsages(), nil + if opt.ShowVersion || opt.ShowHelp { + return nil } opt.viper.AutomaticEnv() @@ -213,7 +209,7 @@ func (opt *Options) Parse() (string, error) { opt.viper.SetConfigType("yaml") err := opt.viper.ReadInConfig() if err != nil { - return "", fmt.Errorf("read config file %s failed: %v", + return fmt.Errorf("read config file %s failed: %v", opt.ConfigFile, err) } } @@ -234,7 +230,7 @@ func (opt *Options) Parse() (string, error) { c.TagName = "yaml" }) if err != nil { - return "", fmt.Errorf("yaml file unmarshal failed, please make sure you provide valid yaml file, %v", err) + return fmt.Errorf("yaml file unmarshal failed, please make sure you provide valid yaml file, %v", err) } opt.renameLegacyClusterRoles() @@ -248,17 +244,17 @@ func (opt *Options) Parse() (string, error) { err = opt.validate() if err != nil { - return "", err + return err } err = opt.prepare() if err != nil { - return "", err + return err } buff, err := codectool.MarshalYAML(opt) if err != nil { - return "", fmt.Errorf("marshal config to yaml failed: %v", err) + return fmt.Errorf("marshal config to yaml failed: %v", err) } opt.yamlStr = string(buff) @@ -266,7 +262,12 @@ func (opt *Options) Parse() (string, error) { fmt.Printf("%s", opt.yamlStr) } - return "", nil + return nil +} + +// FlagUsages export original flag usages, see FlagSet.FlagUsages. +func (opt *Options) FlagUsages() string { + return opt.flags.FlagUsages() } // ParseURLs parses list of strings to url.URL objects. diff --git a/scripts/install.sh b/scripts/install.sh index dd75640c84..0083c2e527 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -2,8 +2,6 @@ set -e -RED='\033[0;31m' -NC='\033[0m' # First - check OS. OS="$(uname)" @@ -13,8 +11,7 @@ if [[ "${OS}" == "Linux" ]]; then elif [[ "${OS}" == "Darwin" ]];then OS=darwin else - echo -e "Error: ${RED}Unsupport OS - ${OS}${NC}" - exit + abort "Unsupport OS - ${OS}" fi # Second - check the CPU arch @@ -24,11 +21,10 @@ if [[ $ARCH == x86_64 ]]; then ARCH=amd64 elif [[ $ARCH == i686 || $ARCH == i386 ]]; then ARCH=386 -elif [[ $ARCH == aarch64* || $ARCH == armv8* || $ARCH == arm64* ]]; then +elif [[ $ARCH == aarch64* || $ARCH == armv8* ]]; then ARCH=arm64 else - echo -e "Error: ${RED}Unsupport CPU - ${ARCH}${NC}" - exit + abort "Unsupport CPU - ${ARCH}" fi # Third - download the binaries @@ -48,7 +44,7 @@ echo "Create the directory - \"${DIR}\" successfully." echo "Downloading the release file - \"${ARTIFACT}\" ..." curl -sL ${ARTIFACT_URL} -o ${DIR}/${ARTIFACT} echo "Downloaded \"${ARTIFACT}\"" -tar -zxf ${DIR}/${ARTIFACT} -C "${DIR}" +tar -zxf ${DIR}/${ARTIFACT} -C easegress echo "Extract the files successfully" # Fourth - configure the easegress @@ -83,9 +79,6 @@ if [[ "${OS}" == "linux" ]]; then sudo systemctl start easegress #check the status - systemctl -q is-active easegress.service && \ - echo "Easegress service is running!" || \ - systemctl status easegress.service + sleep 2 + systemctl status easegress fi - -echo "Installed successfully"