goploy/model/ProjectServerModel.go

170 lines
5.0 KiB
Go
Raw Normal View History

2022-04-09 21:53:37 +08:00
// Copyright 2022 The Goploy Authors. All rights reserved.
// Use of this source code is governed by a GPLv3-style
// license that can be found in the LICENSE file.
2020-08-04 14:28:25 +08:00
package model
2022-01-15 14:21:31 +08:00
import (
2022-05-02 12:01:57 +08:00
"fmt"
2022-01-15 14:21:31 +08:00
sq "github.com/Masterminds/squirrel"
"github.com/zhenorzz/goploy/utils"
)
2020-08-04 14:28:25 +08:00
const projectServerTable = "`project_server`"
type ProjectServer struct {
2022-01-15 14:21:31 +08:00
ID int64 `json:"id"`
ProjectID int64 `json:"projectId"`
ServerID int64 `json:"serverId"`
ServerName string `json:"serverName"`
2022-05-15 11:12:58 +08:00
ServerOS string `json:"serverOS"`
2022-01-15 14:21:31 +08:00
ServerIP string `json:"serverIP"`
ServerPort int `json:"serverPort"`
ServerOwner string `json:"serverOwner"`
ServerPassword string `json:"serverPassword"`
ServerPath string `json:"serverPath"`
ServerJumpIP string `json:"serverJumpIP"`
ServerJumpPort int `json:"serverJumpPort"`
ServerJumpOwner string `json:"serverJumpOwner"`
ServerJumpPassword string `json:"serverJumpPassword"`
ServerJumpPath string `json:"serverJumpPath"`
ServerDescription string `json:"serverDescription"`
InsertTime string `json:"insertTime"`
UpdateTime string `json:"updateTime"`
2020-08-04 14:28:25 +08:00
}
type ProjectServers []ProjectServer
func (ps ProjectServer) GetBindServerListByProjectID() (ProjectServers, error) {
rows, err := sq.
2021-02-07 15:43:02 +08:00
Select(`
project_server.id,
project_id,
server_id,
server.name,
server.ip,
2022-05-15 11:12:58 +08:00
server.os,
2021-02-07 15:43:02 +08:00
server.port,
server.owner,
server.password,
server.path,
2022-01-15 14:21:31 +08:00
server.jump_ip,
server.jump_port,
server.jump_owner,
server.jump_password,
server.jump_path,
2021-02-07 15:43:02 +08:00
server.description,
project_server.insert_time,
project_server.update_time`).
2020-08-04 14:28:25 +08:00
From(projectServerTable).
LeftJoin(serverTable + " ON project_server.server_id = server.id").
Where(sq.Eq{"project_id": ps.ProjectID}).
RunWith(DB).
Query()
if err != nil {
return nil, err
}
projectServers := ProjectServers{}
for rows.Next() {
var projectServer ProjectServer
if err := rows.Scan(
&projectServer.ID,
&projectServer.ProjectID,
&projectServer.ServerID,
&projectServer.ServerName,
2022-05-15 11:12:58 +08:00
&projectServer.ServerOS,
2020-08-04 14:28:25 +08:00
&projectServer.ServerIP,
&projectServer.ServerPort,
&projectServer.ServerOwner,
2021-02-07 15:43:02 +08:00
&projectServer.ServerPassword,
&projectServer.ServerPath,
2022-01-15 14:21:31 +08:00
&projectServer.ServerJumpIP,
&projectServer.ServerJumpPort,
&projectServer.ServerJumpOwner,
&projectServer.ServerJumpPassword,
&projectServer.ServerJumpPath,
2020-08-04 14:28:25 +08:00
&projectServer.ServerDescription,
&projectServer.InsertTime,
&projectServer.UpdateTime); err != nil {
return nil, err
}
projectServers = append(projectServers, projectServer)
}
return projectServers, nil
}
func (ps ProjectServers) AddMany() error {
if len(ps) == 0 {
return nil
}
builder := sq.
Replace(projectServerTable).
Columns("project_id", "server_id")
for _, row := range ps {
builder = builder.Values(row.ProjectID, row.ServerID)
}
_, err := builder.RunWith(DB).Exec()
return err
}
func (ps ProjectServer) DeleteRow() error {
_, err := sq.
Delete(projectServerTable).
Where(sq.Eq{"id": ps.ID}).
RunWith(DB).
Exec()
return err
}
2022-01-15 14:21:31 +08:00
2022-03-24 15:31:08 +08:00
func (ps ProjectServer) DeleteByProjectID() error {
_, err := sq.
Delete(projectServerTable).
Where(sq.Eq{"project_id": ps.ProjectID}).
RunWith(DB).
Exec()
return err
}
2022-05-02 12:01:57 +08:00
func (ps ProjectServer) ToSSHConfig() utils.SSHConfig {
2022-01-15 14:21:31 +08:00
return utils.SSHConfig{
User: ps.ServerOwner,
Password: ps.ServerPassword,
Path: ps.ServerPath,
Host: ps.ServerIP,
Port: ps.ServerPort,
JumpUser: ps.ServerJumpOwner,
JumpPassword: ps.ServerJumpPassword,
JumpPath: ps.ServerJumpPath,
JumpHost: ps.ServerJumpIP,
JumpPort: ps.ServerJumpPort,
}
}
2022-05-02 12:01:57 +08:00
func (ps ProjectServer) ToSSHOption() string {
proxyCommand := ""
if ps.ServerJumpIP != "" {
if ps.ServerJumpPath != "" {
if ps.ServerJumpPassword != "" {
proxyCommand = fmt.Sprintf("-o ProxyCommand='sshpass -p %s -P assphrase ssh -o StrictHostKeyChecking=no -W %%h:%%p -i %s -p %d %s@%s' ", ps.ServerPassword, ps.ServerJumpPath, ps.ServerJumpPort, ps.ServerJumpOwner, ps.ServerJumpIP)
} else {
proxyCommand = fmt.Sprintf("-o ProxyCommand='ssh -o StrictHostKeyChecking=no -W %%h:%%p -i %s -p %d %s@%s' ", ps.ServerJumpPath, ps.ServerJumpPort, ps.ServerJumpOwner, ps.ServerJumpIP)
}
} else {
proxyCommand = fmt.Sprintf("-o ProxyCommand='sshpass -p %s ssh -o StrictHostKeyChecking=no -W %%h:%%p -p %d %s@%s' ", ps.ServerPassword, ps.ServerJumpPort, ps.ServerJumpOwner, ps.ServerJumpIP)
}
}
if ps.ServerPath != "" {
if ps.ServerPassword != "" {
return fmt.Sprintf("sshpass -p %s -P assphrase ssh -o StrictHostKeyChecking=no %s -p %d -i %s", ps.ServerPassword, proxyCommand, ps.ServerPort, ps.ServerPath)
} else {
return fmt.Sprintf("ssh -o StrictHostKeyChecking=no %s -p %d -i %s", proxyCommand, ps.ServerPort, ps.ServerPath)
}
} else {
return fmt.Sprintf("sshpass -p %s ssh -o StrictHostKeyChecking=no %s -p %d", ps.ServerPassword, proxyCommand, ps.ServerPort)
}
}