From 7155d1acb792091013c92b628c943a83dff6d46e Mon Sep 17 00:00:00 2001 From: Xinwei Xiong <3293172751NSS@gmail.com> Date: Sun, 21 Jan 2024 10:49:08 +0800 Subject: [PATCH] feat: Enhance Script Details and Add MacOS Compatibility with Documentation Updates (#1794) * feat: fix a portion of get path * feat: optimize mac deployment scripts --- .devcontainer/devcontainer.json | 10 ------- assets/colors.md | 11 ++++++++ pkg/common/config/parse.go | 22 +++++++++++---- pkg/util/flag/flag.go | 40 +++++++++++++++++++++++++++ pkg/util/genutil/genutil.go | 27 ++++++++++++++++++ pkg/util/genutil/genutil_test.go | 26 +++++++++++++++++ scripts/docker-start-all.sh | 2 -- scripts/install/openim-msgtransfer.sh | 19 +++++++++---- scripts/install/openim-rpc.sh | 3 +- scripts/start-all.sh | 2 ++ 10 files changed, 138 insertions(+), 24 deletions(-) create mode 100644 assets/colors.md create mode 100644 pkg/util/flag/flag.go create mode 100644 pkg/util/genutil/genutil.go create mode 100644 pkg/util/genutil/genutil_test.go diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index d8b13d3a4..dc003ca20 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -6,16 +6,6 @@ "build": { "dockerfile": "Dockerfile" }, // Replace with uncommented line below to build your own local copy of the image // "dockerFile": "../docker/Dockerfile-dev", - "containerEnv": { - // Uncomment to overwrite devcontainer .kube/config and .minikube certs with the localhost versions - // each time the devcontainer starts, if the respective .kube-localhost/config and .minikube-localhost - // folders respectively are bind mounted to the devcontainer. - // "SYNC_LOCALHOST_KUBECONFIG": "true" - - // Uncomment to disable docker-in-docker and automatically proxy default /var/run/docker.sock to - // the localhost bind-mount /var/run/docker-host.sock. - // "BIND_LOCALHOST_DOCKER": "true" - }, "remoteEnv": { "GO111MODULE": "on", "GOPROXY": "https://goproxy.cn", diff --git a/assets/colors.md b/assets/colors.md new file mode 100644 index 000000000..cf8777719 --- /dev/null +++ b/assets/colors.md @@ -0,0 +1,11 @@ +# Official Colors + +The openim logo has an official blue color. When reproducing the logo, please use the official color, when possible. + +## Pantone + +When possible, the Pantone color is preferred for print material. The official Pantone color is *285C*. + +## RGB + +When used digitally, the official RGB color code is *#326CE5*. diff --git a/pkg/common/config/parse.go b/pkg/common/config/parse.go index 1410a5a4a..64719d6a1 100644 --- a/pkg/common/config/parse.go +++ b/pkg/common/config/parse.go @@ -24,6 +24,7 @@ import ( "gopkg.in/yaml.v3" "github.com/openimsdk/open-im-server/v3/pkg/msgprocessor" + "github.com/openimsdk/open-im-server/v3/pkg/util/genutil" ) //go:embed version @@ -37,19 +38,30 @@ const ( // return absolude path join ../config/, this is k8s container config path. func GetDefaultConfigPath() string { - b, err := filepath.Abs(os.Args[0]) + executablePath, err := os.Executable() if err != nil { - fmt.Println("filepath.Abs error,err=", err) + fmt.Println("GetDefaultConfigPath error:", err.Error()) return "" } - return filepath.Join(filepath.Dir(b), "../config/") + + configPath, err := genutil.OutDir(filepath.Join(filepath.Dir(executablePath), "../config/")) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err) + os.Exit(1) + } + return configPath } // getProjectRoot returns the absolute path of the project root directory. func GetProjectRoot() string { - b, _ := filepath.Abs(os.Args[0]) + executablePath, _ := os.Executable() - return filepath.Join(filepath.Dir(b), "../../../../..") + projectRoot, err := genutil.OutDir(filepath.Join(filepath.Dir(executablePath), "../../../../..")) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err) + os.Exit(1) + } + return projectRoot } func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options { diff --git a/pkg/util/flag/flag.go b/pkg/util/flag/flag.go new file mode 100644 index 000000000..7bbacf444 --- /dev/null +++ b/pkg/util/flag/flag.go @@ -0,0 +1,40 @@ +package flag + +import ( + goFlag "flag" + "log" + "strings" + + "github.com/spf13/pflag" +) + +// WordSepNormalizeFunc changes all flags that contain "_" separators. +func WordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName { + if strings.Contains(name, "_") { + return pflag.NormalizedName(strings.ReplaceAll(name, "_", "-")) + } + return pflag.NormalizedName(name) +} + +// WarnWordSepNormalizeFunc changes and warns for flags that contain "_" separators. +func WarnWordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName { + if strings.Contains(name, "_") { + normalizedName := strings.ReplaceAll(name, "_", "-") + log.Printf("WARNING: flag %s has been deprecated and will be removed in a future version. Use %s instead.", name, normalizedName) + return pflag.NormalizedName(normalizedName) + } + return pflag.NormalizedName(name) +} + +// InitFlags normalizes, parses, then logs the command line flags. +func InitFlags() { + pflag.CommandLine.SetNormalizeFunc(WordSepNormalizeFunc) + pflag.CommandLine.AddGoFlagSet(goFlag.CommandLine) +} + +// PrintFlags logs the flags in the flagset. +func PrintFlags(flags *pflag.FlagSet) { + flags.VisitAll(func(flag *pflag.Flag) { + log.Printf("FLAG: --%s=%q", flag.Name, flag.Value) + }) +} diff --git a/pkg/util/genutil/genutil.go b/pkg/util/genutil/genutil.go new file mode 100644 index 000000000..36da89bbb --- /dev/null +++ b/pkg/util/genutil/genutil.go @@ -0,0 +1,27 @@ +package genutil + +import ( + "fmt" + "os" + "path/filepath" +) + +// OutDir creates the absolute path name from path and checks path exists. +// Returns absolute path including trailing '/' or error if path does not exist. +func OutDir(path string) (string, error) { + outDir, err := filepath.Abs(path) + if err != nil { + return "", err + } + + stat, err := os.Stat(outDir) + if err != nil { + return "", err + } + + if !stat.IsDir() { + return "", fmt.Errorf("output directory %s is not a directory", outDir) + } + outDir += "/" + return outDir, nil +} diff --git a/pkg/util/genutil/genutil_test.go b/pkg/util/genutil/genutil_test.go new file mode 100644 index 000000000..0ee85df27 --- /dev/null +++ b/pkg/util/genutil/genutil_test.go @@ -0,0 +1,26 @@ +package genutil + +import ( + "testing" +) + +func TestValidDir(t *testing.T) { + _, err := OutDir("./") + if err != nil { + t.Fatal(err) + } +} + +func TestInvalidDir(t *testing.T) { + _, err := OutDir("./nondir") + if err == nil { + t.Fatal("expected an error") + } +} + +func TestNotDir(t *testing.T) { + _, err := OutDir("./genutils_test.go") + if err == nil { + t.Fatal("expected an error") + } +} diff --git a/scripts/docker-start-all.sh b/scripts/docker-start-all.sh index 2616b7bd1..162655553 100755 --- a/scripts/docker-start-all.sh +++ b/scripts/docker-start-all.sh @@ -28,8 +28,6 @@ openim::log::info "\n# Use Docker to start all openim service" trap 'openim::util::onCtrlC' INT -"${OPENIM_ROOT}"/scripts/init-config.sh --skip - "${OPENIM_ROOT}"/scripts/start-all.sh sleep 5 diff --git a/scripts/install/openim-msgtransfer.sh b/scripts/install/openim-msgtransfer.sh index 18bbb3c02..f6039637c 100755 --- a/scripts/install/openim-msgtransfer.sh +++ b/scripts/install/openim-msgtransfer.sh @@ -12,6 +12,8 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# Use: +# ./scripts/install/openim-msgtransfer.sh openim::msgtransfer::start # Common utilities, variables and checks for all build scripts. set -o errexit @@ -64,15 +66,20 @@ function openim::msgtransfer::check() { PIDS=$(pgrep -f "${OPENIM_OUTPUT_HOSTBIN}/openim-msgtransfer") NUM_PROCESSES=$(echo "$PIDS" | wc -l) - # NUM_PROCESSES=$(($NUM_PROCESSES - 1)) if [ "$NUM_PROCESSES" -eq "$OPENIM_MSGGATEWAY_NUM" ]; then - openim::log::info "Found $OPENIM_MSGGATEWAY_NUM processes named $OPENIM_OUTPUT_HOSTBIN" - for PID in $PIDS; do - ps -p $PID -o pid,cmd - done + openim::log::info "Found $OPENIM_MSGGATEWAY_NUM processes named $OPENIM_OUTPUT_HOSTBIN" + for PID in $PIDS; do + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + ps -p $PID -o pid,cmd + elif [[ "$OSTYPE" == "darwin"* ]]; then + ps -p $PID -o pid,comm + else + openim::log::error "Unsupported OS type: $OSTYPE" + fi + done else - openim::log::error_exit "Expected $OPENIM_MSGGATEWAY_NUM openim msgtransfer processes, but found $NUM_PROCESSES msgtransfer processes." + openim::log::error_exit "Expected $OPENIM_MSGGATEWAY_NUM openim msgtransfer processes, but found $NUM_PROCESSES msgtransfer processes." fi } diff --git a/scripts/install/openim-rpc.sh b/scripts/install/openim-rpc.sh index b66004191..db1526d6a 100755 --- a/scripts/install/openim-rpc.sh +++ b/scripts/install/openim-rpc.sh @@ -123,12 +123,13 @@ function openim::rpc::start() { for ((i = 0; i < ${#OPENIM_RPC_SERVICE_LISTARIES[*]}; i++)); do # openim::util::stop_services_with_name ${OPENIM_RPC_SERVICE_LISTARIES openim::util::stop_services_on_ports ${OPENIM_RPC_PORT_LISTARIES[$i]} + openim::log::info "OpenIM ${OPENIM_RPC_SERVICE_LISTARIES[$i]} config path: ${OPENIM_RPC_CONFIG}" # Get the service and Prometheus ports. OPENIM_RPC_SERVICE_PORTS=( $(openim::util::list-to-string ${OPENIM_RPC_PORT_LISTARIES[$i]}) ) read -a OPENIM_RPC_SERVICE_PORTS_ARRAY <<< ${OPENIM_RPC_SERVICE_PORTS} - + OPENIM_RPC_PROM_PORTS=( $(openim::util::list-to-string ${OPENIM_RPC_PROM_PORT_LISTARIES[$i]}) ) read -a OPENIM_RPC_PROM_PORTS_ARRAY <<< ${OPENIM_RPC_PROM_PORTS} diff --git a/scripts/start-all.sh b/scripts/start-all.sh index 64b931f50..b1f9b865b 100755 --- a/scripts/start-all.sh +++ b/scripts/start-all.sh @@ -32,6 +32,8 @@ if [[ $? -ne 0 ]]; then fi set -o errexit +"${OPENIM_ROOT}"/scripts/init-config.sh --skip + echo "You need to start the following scripts in order: ${OPENIM_SERVER_SCRIPTARIES[@]}" openim::log::install_errexit