mirror of https://github.com/etcd-io/dbtester.git
agent: clean up 'Command' and flags
This commit is contained in:
parent
e37dff1554
commit
5e0d55e245
|
|
@ -0,0 +1,97 @@
|
|||
// Copyright 2017 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// 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.
|
||||
|
||||
package agent
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/coreos/dbtester/agent/agentpb"
|
||||
"github.com/coreos/pkg/capnslog"
|
||||
"github.com/spf13/cobra"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type flags struct {
|
||||
agentLog string
|
||||
databaseLog string
|
||||
systemMetricsLog string
|
||||
|
||||
javaExec string
|
||||
etcdExec string
|
||||
zetcdExec string
|
||||
cetcdExec string
|
||||
consulExec string
|
||||
|
||||
zkWorkDir string
|
||||
zkDataDir string
|
||||
zkConfig string
|
||||
etcdDataDir string
|
||||
consulDataDir string
|
||||
|
||||
grpcPort string
|
||||
}
|
||||
|
||||
var globalFlags flags
|
||||
|
||||
func init() {
|
||||
Command.PersistentFlags().StringVar(&globalFlags.agentLog, "agent-log", filepath.Join(homeDir(), "agent.log"), "agent log path.")
|
||||
Command.PersistentFlags().StringVar(&globalFlags.databaseLog, "database-log", filepath.Join(homeDir(), "database.log"), "Database log path.")
|
||||
Command.PersistentFlags().StringVar(&globalFlags.systemMetricsLog, "system-metrics-log", filepath.Join(homeDir(), "system-metrics.csv"), "System metrics log path.")
|
||||
|
||||
Command.PersistentFlags().StringVar(&globalFlags.javaExec, "java-exec", "/usr/bin/java", "Java executable binary path (needed for Zookeeper).")
|
||||
Command.PersistentFlags().StringVar(&globalFlags.etcdExec, "etcd-exec", filepath.Join(os.Getenv("GOPATH"), "bin/etcd"), "etcd executable binary path.")
|
||||
Command.PersistentFlags().StringVar(&globalFlags.zetcdExec, "zetcd-exec", filepath.Join(os.Getenv("GOPATH"), "bin/zetcd"), "zetcd executable binary path .")
|
||||
Command.PersistentFlags().StringVar(&globalFlags.cetcdExec, "cetcd-exec", filepath.Join(os.Getenv("GOPATH"), "bin/cetcd"), "cetcd executable binary path .")
|
||||
Command.PersistentFlags().StringVar(&globalFlags.consulExec, "consul-exec", filepath.Join(os.Getenv("GOPATH"), "bin/consul"), "Consul executable binary path.")
|
||||
|
||||
Command.PersistentFlags().StringVar(&globalFlags.zkWorkDir, "zk-work-dir", filepath.Join(homeDir(), "zookeeper"), "Zookeeper working directory.")
|
||||
Command.PersistentFlags().StringVar(&globalFlags.zkDataDir, "zk-data-dir", filepath.Join(homeDir(), "zookeeper/data.zk"), "Zookeeper data directory.")
|
||||
Command.PersistentFlags().StringVar(&globalFlags.zkConfig, "zk-config", filepath.Join(homeDir(), "zookeeper/zookeeper.config"), "Zookeeper configuration file path.")
|
||||
Command.PersistentFlags().StringVar(&globalFlags.etcdDataDir, "etcd-data-dir", filepath.Join(homeDir(), "etcd.data"), "etcd data directory.")
|
||||
Command.PersistentFlags().StringVar(&globalFlags.consulDataDir, "consul-data-dir", filepath.Join(homeDir(), "consul.data"), "Consul data directory.")
|
||||
|
||||
Command.PersistentFlags().StringVar(&globalFlags.grpcPort, "agent-port", ":3500", "Port to server agent gRPC server.")
|
||||
}
|
||||
|
||||
// Command implements 'agent' command.
|
||||
var Command = &cobra.Command{
|
||||
Use: "agent",
|
||||
Short: "Database 'agent' in remote servers (runs database, upload logs).",
|
||||
RunE: commandFunc,
|
||||
}
|
||||
|
||||
func commandFunc(cmd *cobra.Command, args []string) error {
|
||||
f, err := openToAppend(globalFlags.agentLog)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
capnslog.SetFormatter(capnslog.NewPrettyFormatter(f, false))
|
||||
|
||||
var (
|
||||
grpcServer = grpc.NewServer()
|
||||
sender = NewTransporterServer()
|
||||
)
|
||||
ln, err := net.Listen("tcp", globalFlags.grpcPort)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
agentpb.RegisterTransporterServer(grpcServer, sender)
|
||||
|
||||
plog.Infof("agent started with gRPC %s (log path %q)", globalFlags.grpcPort, globalFlags.agentLog)
|
||||
return grpcServer.Serve(ln)
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
// Copyright 2016 CoreOS, Inc.
|
||||
// Copyright 2017 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
|
|
|||
|
|
@ -19,19 +19,32 @@ import (
|
|||
"runtime"
|
||||
)
|
||||
|
||||
func openToAppend(fpath string) (*os.File, error) {
|
||||
f, err := os.OpenFile(fpath, os.O_RDWR|os.O_APPEND, 0777)
|
||||
func openToRead(fpath string) (*os.File, error) {
|
||||
f, err := os.OpenFile(fpath, os.O_RDONLY, 0444)
|
||||
if err != nil {
|
||||
f, err = os.Create(fpath)
|
||||
if err != nil {
|
||||
return f, err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func openToAppend(fpath string) (*os.File, error) {
|
||||
f, err := os.OpenFile(fpath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0777)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func openToOverwrite(fpath string) (*os.File, error) {
|
||||
f, err := os.OpenFile(fpath, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0777)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func toFile(txt, fpath string) error {
|
||||
f, err := os.OpenFile(fpath, os.O_RDWR|os.O_TRUNC, 0777)
|
||||
f, err := openToOverwrite(fpath)
|
||||
if err != nil {
|
||||
f, err = os.Create(fpath)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Reference in New Issue