-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
99 lines (91 loc) · 2.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"os"
"log"
"fmt"
"database/sql"
"github.com/domeos/dockerConnector/connector"
"github.com/codegangsta/cli"
_ "github.com/go-sql-driver/mysql"
)
func main() {
logFile, err := os.OpenFile("dockerConnector.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
if err == nil {
log.SetOutput(logFile)
} else {
fmt.Println("[Error] Set Log File: ", err.Error())
}
app := cli.NewApp()
app.Name = "dockerConnector"
app.Usage = "Provide convenient access to docker container on any hosts via SSH"
app.Version = "1.0"
app.Author = "dockerConnector"
app.Email = "[email protected]"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "ssh-addr",
Usage: "the address that SSH listens",
Value: ":2222",
},
cli.StringFlag{
Name: "mysql-user",
Usage: "MySQL user",
Value: "root",
},
cli.StringFlag{
Name: "mysql-password",
Usage: "MySQL password",
Value: "root",
},
cli.StringFlag{
Name: "mysql-ip",
Usage: "MySQL ip",
Value: "127.0.0.1",
},
cli.StringFlag{
Name: "mysql-port",
Usage: "MySQL port",
Value: "3306",
},
cli.StringFlag{
Name: "mysql-database",
Usage: "MySQL database",
Value: "dockerConnector",
},
}
app.Action = run
if err := app.Run(os.Args); err != nil {
log.Fatal("[Fatal] Start to Run: ", err.Error())
}
}
func run(c *cli.Context) {
errs := make(chan error)
db, err := initDB(c)
if err != nil {
log.Fatal("[Fatal] Connect to MySQL: ", err.Error())
}
con, err := connector.New(c.String("ssh-addr"), c.String("server"), db, errs)
if err != nil {
log.Fatal("[Fatal] Create New Connector: ", err.Error())
}
con.Start()
for i := 0; i < 2; i++ {
err := <-errs
if err != nil {
log.Fatal("[Fatal]", err)
}
}
}
func initDB(c *cli.Context) (*sql.DB, error) {
user := c.String("mysql-user")
password := c.String("mysql-password")
ip := c.String("mysql-ip")
port := c.String("mysql-port")
database := c.String("mysql-database")
sqlString := user + ":" + password + "@tcp(" + ip + ":" + port + ")/" + database
db, err := sql.Open("mysql", sqlString)
if err != nil {
return nil, err
}
return db, nil
}