Skip to content

Commit

Permalink
Modify the default port for goconserver rest api
Browse files Browse the repository at this point in the history
This commit modify the default port for rest api interface,
use 12429 by default.

This commit also includes the fix for the tty window size.
  • Loading branch information
chenglch committed Nov 15, 2017
1 parent a3897ab commit 0588fc3
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 22 deletions.
4 changes: 3 additions & 1 deletion benchmark/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, count):
if 'consoleserver_host' in os.environ:
self.host = os.environ["consoleserver_host"]
else:
self.host = 'http://localhost:8089'
self.host = 'http://localhost:12429'

@elaspe_run(desc="post")
def test_post(self):
Expand Down Expand Up @@ -153,6 +153,8 @@ def _test_get(i):
api.test_bulk_post()
elif method == 'bulk_delete':
api.test_bulk_delete()
elif method == 'list':
api.test_list()
else:
print("Unsupport sub command %s." % method)
else:
Expand Down
4 changes: 2 additions & 2 deletions common/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func InitServerConfig(confFile string) (*ServerConfig, error) {
serverConfig.Global.Worker = 1
serverConfig.Global.LogLevel = "info"
serverConfig.Global.StorageType = "file"
serverConfig.API.Port = "8089"
serverConfig.API.Port = "12429"
serverConfig.API.HttpTimeout = 10
serverConfig.Console.Port = "12430"
serverConfig.Console.DataDir = "/var/lib/goconserver/"
Expand Down Expand Up @@ -93,7 +93,7 @@ type ClientConfig struct {
func NewClientConfig() (*ClientConfig, error) {
var err error
clientConfig = new(ClientConfig)
clientConfig.HTTPUrl = "http://127.0.0.1:8089"
clientConfig.HTTPUrl = "http://127.0.0.1:12429"
clientConfig.ServerHost = "127.0.0.1"
clientConfig.ConsolePort = "12430"
clientConfig.ConsoleTimeout = 30
Expand Down
8 changes: 8 additions & 0 deletions common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (
HOST_NOT_EXIST
INVALID_TYPE
ETCD_NOT_INIT
SET_DEADLINE_ERROR
SEND_KEEPALIVE_ERROR

// error message
STRING_NOT_EXIST = "Not exist"
Expand All @@ -39,6 +41,9 @@ const (
STRING_NOT_TERMINAL = "Not terminal"
STRING_INVALID_TYPE = "Invalid type"
STRING_ETCD_NOT_INIT = "Etcd is not initialized"
//ssh
STRING_SET_DEADLINE_ERROR = "failed to set deadline"
STRING_SEND_KEEPALIVE_ERROR = "failed to send keep alive"
)

var (
Expand All @@ -59,6 +64,9 @@ var (

ErrNotTerminal = NewErr(NOT_TERMINAL, STRING_NOT_TERMINAL)
ErrInvalidType = NewErr(INVALID_TYPE, STRING_INVALID_TYPE)
// ssh
ErrSetDeadline = NewErr(SET_DEADLINE_ERROR, STRING_SET_DEADLINE_ERROR)
ErrSendKeepalive = NewErr(SEND_KEEPALIVE_ERROR, STRING_SEND_KEEPALIVE_ERROR)
// etcd
ErrETCDNotInit = NewErr(ETCD_NOT_INIT, STRING_ETCD_NOT_INIT)
)
Expand Down
55 changes: 50 additions & 5 deletions console/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ConsoleClient struct {
host, port string
origState *terminal.State
escape int // client exit signal
cr int
exit chan bool
retry bool
inputTask *common.Task
Expand Down Expand Up @@ -69,11 +70,16 @@ func (c *ConsoleClient) output(args ...interface{}) {
c.exit <- true
return
}
n, err = os.Stdout.Write(b)
if err != nil {
fmt.Printf("\rCould not send message, error: %s\r\n", err.Error())
c.exit <- true
return
b = c.transCr(b, n)
n = len(b)
for n > 0 {
tmp, err := os.Stdout.Write(b)
if err != nil {
fmt.Printf("\rCould not send message, error: %s\r\n", err.Error())
c.exit <- true
return
}
n -= tmp
}
}

Expand Down Expand Up @@ -127,6 +133,45 @@ func (c *ConsoleClient) checkEscape(b []byte, n int) (bool, int) {
return false, pos
}

func (c *ConsoleClient) transCr(b []byte, n int) []byte {
temp := make([]byte, 4096)
j := 0
for i := 0; i < n; i++ {
ch := b[i]
if c.cr == 0 {
if ch == ' ' {
c.cr = 1
} else {
temp[j] = ch
j++
}
} else if c.cr == 1 {
if ch == '\r' {
c.cr = 2
} else {
temp[j], temp[j+1] = ' ', ch
j += 2
c.cr = 0
}
} else if c.cr == 2 {
if ch == '\n' {
temp[j], temp[j+1], temp[j+2] = ' ', '\r', ch
j += 3
} else {
temp[j] = ch // ignore " \r"
j++
}
c.cr = 0
}
}
if c.cr == 1 {
c.cr = 0
temp[j] = ' '
j++
}
return temp[0:j]
}

func (c *ConsoleClient) tryConnect(conn net.Conn, name string) (int, error) {
m := make(map[string]string)
m["name"] = name
Expand Down
7 changes: 3 additions & 4 deletions console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ func (c *Console) writeClient(conn net.Conn) {
err = c.logger(logFile, []byte(welcome))
if err != nil {
plog.WarningNode(c.node.StorageNode.Name, fmt.Sprintf("Failed to log message to %s. Error:%s", logFile, err.Error()))
return
}
for {
if bufChan, ok = c.bufConn[conn]; !ok {
Expand Down Expand Up @@ -163,7 +162,6 @@ func (c *Console) readTarget() {
err = c.logger(logFile, []byte(msg))
if err != nil {
plog.WarningNode(c.node.StorageNode.Name, fmt.Sprintf("Failed to log message to %s. Error:%s", logFile, err.Error()))
return
}
for {
select {
Expand All @@ -185,15 +183,16 @@ func (c *Console) readTarget() {
err = c.logger(logFile, b[:n])
if err != nil {
plog.WarningNode(c.node.StorageNode.Name, fmt.Sprintf("Failed to log message to %s. Error:%s", logFile, err.Error()))
return
}
}
}
}

func (c *Console) writeClientChan(buf []byte) {
b := make([]byte, len(buf))
copy(b, buf)
for _, v := range c.bufConn {
v <- buf
v <- b
}
}

Expand Down
2 changes: 1 addition & 1 deletion etc/goconserver/client.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export CONGO_URL="http://127.0.0.1:8089"
export CONGO_URL="http://127.0.0.1:12429"
export CONGO_SERVER_HOST="127.0.0.1"
export CONGO_PORT="12430"
export CONGO_CONSOLE_TIMEOUT=3
Expand Down
4 changes: 2 additions & 2 deletions etc/goconserver/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ global:
# ssl_cert_file: /etc/goconserver/cert/server-cert.pem
# ssl_ca_cert_file: /etc/goconserver/cert/ca.pem
logfile: "/var/log/goconserver/server.log" # the log file for goconserver daemon.
log_level: "info" # debug, info, warn, error, fatal, panic
log_level: info # debug, info, warn, error, fatal, panic
worker: 4 # the max cpu cores for user workload
storage_type: file # file or etcd, etcd is just experimental option.

api:
port: "8089" # http(s) port
port: "12429" # http(s) port
http_timeout: 5 # in second

console:
Expand Down
2 changes: 1 addition & 1 deletion etc/systemd/goconserver.service
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Description=goconserver
[Service]
LimitNOFILE=65535
ExecStart=/usr/local/bin/goconserver
ExecStart=/usr/bin/goconserver
ExecStop=/bin/kill -TERM $MAINPID
ExecReload=/bin/kill -HUP $MAINPID
killMode=process
Expand Down
47 changes: 42 additions & 5 deletions plugins/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/chenglch/goconserver/common"
"golang.org/x/crypto/ssh"
"net"
"os"
)

Expand Down Expand Up @@ -41,6 +42,7 @@ type SSHConsole struct {
host string
client *ssh.Client
session *ssh.Session
exit chan bool
}

func NewSSHConsole(node string, params map[string]string) (ConsolePlugin, error) {
Expand Down Expand Up @@ -73,9 +75,11 @@ func NewSSHConsole(node string, params map[string]string) (ConsolePlugin, error)

var sshInst *SSHConsole
if privateKey != "" {
sshInst = &SSHConsole{host: fmt.Sprintf("%s:%s", host, port), user: user, privateKeyFile: privateKey, node: node}
sshInst = &SSHConsole{host: fmt.Sprintf("%s:%s", host, port), user: user,
privateKeyFile: privateKey, node: node, exit: make(chan bool, 0)}
} else if password != "" {
sshInst = &SSHConsole{host: fmt.Sprintf("%s:%s", host, port), user: user, password: password, node: node}
sshInst = &SSHConsole{host: fmt.Sprintf("%s:%s", host, port), user: user,
password: password, node: node, exit: make(chan bool, 0)}
} else {
return nil, common.NewErr(common.INVALID_PARAMETER, fmt.Sprintf("%s: Please specify the parameter password or private_key", node))
}
Expand Down Expand Up @@ -105,22 +109,54 @@ func (s *SSHConsole) appendPasswordAuthMethod(autoMethods *[]ssh.AuthMethod) {
}
}

func (s *SSHConsole) keepSSHAlive(cl *ssh.Client, conn net.Conn) error {
const keepAliveInterval = time.Minute
t := time.NewTicker(keepAliveInterval)
defer t.Stop()
for {
plog.DebugNode(s.node, "Keep alive goruntine for ssh connection started")
deadline := time.Now().Add(keepAliveInterval).Add(15 * time.Second)
err := conn.SetDeadline(deadline)
if err != nil {
plog.ErrorNode(s.node, "Failed to set deadline for ssh connection")
return common.ErrSetDeadline
}
select {
case <-t.C:
_, _, err = cl.SendRequest("[email protected]", true, nil)
if err != nil {
plog.ErrorNode(s.node, "Faild to send keepalive request")
return common.ErrSendKeepalive
}
case <-s.exit:
plog.Debug("Exit keepalive goroutine")
return nil
}
}
}

func (s *SSHConsole) connectToHost() error {
var err error
timeout := 5 * time.Second
autoMethods := make([]ssh.AuthMethod, 0)
s.appendPrivateKeyAuthMethod(&autoMethods)
s.appendPasswordAuthMethod(&autoMethods)
sshConfig := &ssh.ClientConfig{
User: s.user,
Auth: autoMethods,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: 5 * time.Second,
Timeout: timeout,
}
s.client, err = ssh.Dial("tcp", s.host, sshConfig)
conn, err := net.DialTimeout("tcp", s.host, timeout)
if err != nil {
return err
}

c, chans, reqs, err := ssh.NewClientConn(conn, s.host, sshConfig)
if err != nil {
return err
}
s.client = ssh.NewClient(c, chans, reqs)
go s.keepSSHAlive(s.client, conn)
s.session, err = s.client.NewSession()
if err != nil {
s.client.Close()
Expand Down Expand Up @@ -180,6 +216,7 @@ func (s *SSHConsole) Start() (*BaseSession, error) {

func (s *SSHConsole) Close() error {
if s.client != nil {
s.exit <- true
return s.client.Close()
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -x

BASEPATH=$(cd `dirname $0`; pwd)

cp $BASEPATH/goconserver $BASEPATH/congo /usr/local/bin/
cp $BASEPATH/goconserver $BASEPATH/congo /usr/bin/
mkdir -p /etc/goconserver /var/log/goconserver/nodes /var/lib/goconserver
chmod 700 /etc/goconserver /var/log/goconserver/nodes /var/lib/goconserver

Expand Down

0 comments on commit 0588fc3

Please sign in to comment.