Skip to content

Commit

Permalink
Merge pull request #6 from ph4r5h4d/postgresql-support
Browse files Browse the repository at this point in the history
Postgresql support
  • Loading branch information
ph4r5h4d authored Feb 16, 2020
2 parents 61fd904 + b132214 commit 73e7578
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 4 deletions.
12 changes: 12 additions & 0 deletions PostgreSQLChecker/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package PostgreSQLChecker

import (
"fmt"
)

func (pq PostgresSQLConnection) BuildConnectionString() string {
dsl := fmt.Sprintf("host=%s port=%d user=%s password=%s sslmode=%s dbname=%s ",
pq.Host, pq.Port, pq.Username, pq.Password, pq.SSLMode, pq.DatabaseName)

return dsl
}
52 changes: 52 additions & 0 deletions PostgreSQLChecker/postgresqlChecker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package PostgreSQLChecker

import (
"database/sql"
"errors"
_ "github.com/lib/pq"
"wait4it/model"
)

func (pq *PostgresSQLConnection) BuildContext(cx model.CheckContext) {
pq.Port = cx.Port
pq.Host = cx.Host
pq.Username = cx.Username
pq.Password = cx.Password
pq.DatabaseName = cx.DatabaseName
if len(cx.DBConf.SSLMode) < 1 {
pq.SSLMode = "disable"
} else {
pq.SSLMode = cx.DBConf.SSLMode
}
}

func (pq *PostgresSQLConnection) Validate() (bool, error) {
if len(pq.Host) == 0 || len(pq.Username) == 0 {
return false, errors.New("host or username can't be empty")
}

if pq.Port < 0 || pq.Port > 65535 {
return false, errors.New("invalid port range for mysql")
}

return true, nil
}

func (pq *PostgresSQLConnection) Check() (bool, bool, error) {
dsl := pq.BuildConnectionString()

db, err := sql.Open("postgres", dsl)

// if there is an error opening the connection, handle it
if err != nil {
return false, true, err
}

err = db.Ping()
if err != nil {
return false, false, nil
}
_ = db.Close()

return true, true, nil
}
10 changes: 10 additions & 0 deletions PostgreSQLChecker/structs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package PostgreSQLChecker

type PostgresSQLConnection struct {
Host string
Port int
Username string
Password string
DatabaseName string
SSLMode string
}
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ A simple go application to test whether a port is ready to accept a connection,
MySQL server is ready or not.
It also supports **timeout** so it can wait for a particular time and then fail.

## Supported Modules
* TCP port
* MySQL
* PostgresSQL

## Install
You can download the latest release, or you can build it yourself.
To build just run `go build -o wait4it`
Expand All @@ -27,6 +32,10 @@ Check a MySQL instance
```bash
./wait4it -h=127.0.0.1 -p=3306 -t=60 -u=root -P=secret -n=app
```
Check a PostgresSQL instance
```bash
./wait4it -type=postgres -h=127.0.0.1 -p=5432 -t=60 -u=postgres -P=secret -ssl=disable
```

### Docker
You can run this `wait4it` inside a docker container, and it's possible to run this container as init container inside
Expand All @@ -47,6 +56,11 @@ Check a MySQL instance
docker run ph4r5h4d/wait4it -h=127.0.0.1 -p=3306 -t=60 -u=root -P=secret -n=app
```

Check a PostgresSQL instance
```bash
docker run ph4r5h4d/wait4it -type=postgres -h=127.0.0.1 -p=5432 -t=60 -u=postgres -P=secret -ssl=disable
```

## Notes
#### Exit codes
* 0: connection established successfully
Expand Down
8 changes: 5 additions & 3 deletions cmd/check-module-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package cmd

import (
"wait4it/MySQLChecker"
"wait4it/PostgreSQLChecker"
"wait4it/TcpChecker"
)

var cm = map[string]interface{}{
"tcp": &TcpChecker.Tcp{},
"mysql": &MySQLChecker.MySQLConnection{},
}
"tcp": &TcpChecker.Tcp{},
"mysql": &MySQLChecker.MySQLConnection{},
"postgres": &PostgreSQLChecker.PostgresSQLConnection{},
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module wait4it

go 1.13

require github.com/go-sql-driver/mysql v1.5.0
require (
github.com/go-sql-driver/mysql v1.5.0
github.com/lib/pq v1.3.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
4 changes: 4 additions & 0 deletions inputParser/input-parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func GetInput() model.CheckContext {
username := flag.String("u", "", "Username of the service")
password := flag.String("P", "", "Password of the service")
databaseName := flag.String("n", "", "Name of the database")
sslMode := flag.String("ssl", "disable", "Enable or Disable ssl mode (for some database or services)")
flag.Parse()

c := model.CheckContext{
Expand All @@ -25,6 +26,9 @@ func GetInput() model.CheckContext {
Username: *username,
Password: *password,
DatabaseName: *databaseName,
DBConf: model.DatabaseSpecificConf{
SSLMode: *sslMode,
},
}
return c
}
5 changes: 5 additions & 0 deletions model/check-context-struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ type CheckContext struct {
Username string
Password string
DatabaseName string
DBConf DatabaseSpecificConf
}

type ConfigurationContext struct {
CheckType string
Timeout int
}

type DatabaseSpecificConf struct {
SSLMode string
}

0 comments on commit 73e7578

Please sign in to comment.