goploy/internal/model/project_server.go

212 lines
5.7 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"
2022-11-23 10:30:02 +08:00
"github.com/zhenorzz/goploy/internal/pkg"
2022-01-15 14:21:31 +08:00
)
2020-08-04 14:28:25 +08:00
const projectServerTable = "`project_server`"
type ProjectServer struct {
2023-08-04 14:24:07 +08:00
ID int64 `json:"id"`
ProjectID int64 `json:"projectId"`
ServerID int64 `json:"serverId"`
Project Project `json:"project,omitempty"`
Server Server `json:"server,omitempty"`
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,
2022-05-15 11:12:58 +08:00
server.os,
2022-05-15 19:33:23 +08:00
server.ip,
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,
2023-08-04 14:24:07 +08:00
&projectServer.Server.Name,
&projectServer.Server.OS,
&projectServer.Server.IP,
&projectServer.Server.Port,
&projectServer.Server.Owner,
&projectServer.Server.Password,
&projectServer.Server.Path,
&projectServer.Server.JumpIP,
&projectServer.Server.JumpPort,
&projectServer.Server.JumpOwner,
&projectServer.Server.JumpPassword,
&projectServer.Server.JumpPath,
&projectServer.Server.Description,
&projectServer.InsertTime,
&projectServer.UpdateTime); err != nil {
return nil, err
}
projectServers = append(projectServers, projectServer)
}
return projectServers, nil
}
func (ps ProjectServer) GetBindProjectListByServerID() (ProjectServers, error) {
rows, err := sq.
Select(`
project_server.id,
project_id,
server_id,
project.name,
project.branch,
project.environment,
project_server.insert_time,
project_server.update_time`).
From(projectServerTable).
LeftJoin(projectTable + " ON project_server.project_id = project.id").
Where(sq.Eq{"server_id": ps.ServerID}).
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.Project.Name,
&projectServer.Project.Branch,
&projectServer.Project.Environment,
2020-08-04 14:28:25 +08:00
&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
2023-08-04 14:24:07 +08:00
func (ps ProjectServer) DeleteInID(id []int64) error {
_, err := sq.
Delete(projectServerTable).
Where(sq.Eq{"id": id}).
RunWith(DB).
Exec()
return err
}
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-11-23 10:30:02 +08:00
func (ps ProjectServer) ToSSHConfig() pkg.SSHConfig {
return pkg.SSHConfig{
2023-08-04 14:24:07 +08:00
User: ps.Server.Owner,
Password: ps.Server.Password,
Path: ps.Server.Path,
Host: ps.Server.IP,
Port: ps.Server.Port,
JumpUser: ps.Server.JumpOwner,
JumpPassword: ps.Server.JumpPassword,
JumpPath: ps.Server.JumpPath,
JumpHost: ps.Server.JumpIP,
JumpPort: ps.Server.JumpPort,
2022-01-15 14:21:31 +08:00
}
}
2022-05-02 12:01:57 +08:00
func (ps ProjectServer) ToSSHOption() string {
proxyCommand := ""
2023-08-04 14:24:07 +08:00
if ps.Server.JumpIP != "" {
if ps.Server.JumpPath != "" {
if ps.Server.JumpPassword != "" {
proxyCommand = fmt.Sprintf("-o ProxyCommand='sshpass -p %s -P assphrase ssh -o StrictHostKeyChecking=no -W %%h:%%p -i %s -p %d %s@%s' ", ps.Server.JumpPassword, ps.Server.JumpPath, ps.Server.JumpPort, ps.Server.JumpOwner, ps.Server.JumpIP)
2022-05-02 12:01:57 +08:00
} else {
2023-08-04 14:24:07 +08:00
proxyCommand = fmt.Sprintf("-o ProxyCommand='ssh -o StrictHostKeyChecking=no -W %%h:%%p -i %s -p %d %s@%s' ", ps.Server.JumpPath, ps.Server.JumpPort, ps.Server.JumpOwner, ps.Server.JumpIP)
2022-05-02 12:01:57 +08:00
}
} else {
2023-08-04 14:24:07 +08:00
proxyCommand = fmt.Sprintf("-o ProxyCommand='sshpass -p %s ssh -o StrictHostKeyChecking=no -W %%h:%%p -p %d %s@%s' ", ps.Server.JumpPassword, ps.Server.JumpPort, ps.Server.JumpOwner, ps.Server.JumpIP)
2022-05-02 12:01:57 +08:00
}
}
2023-08-04 14:24:07 +08:00
if ps.Server.Path != "" {
if ps.Server.Password != "" {
return fmt.Sprintf("sshpass -p %s -P assphrase ssh -o StrictHostKeyChecking=no %s -p %d -i %s", ps.Server.Password, proxyCommand, ps.Server.Port, ps.Server.Path)
2022-05-02 12:01:57 +08:00
} else {
2023-08-04 14:24:07 +08:00
return fmt.Sprintf("ssh -o StrictHostKeyChecking=no %s -p %d -i %s", proxyCommand, ps.Server.Port, ps.Server.Path)
2022-05-02 12:01:57 +08:00
}
} else {
2023-08-04 14:24:07 +08:00
return fmt.Sprintf("sshpass -p %s ssh -o StrictHostKeyChecking=no %s -p %d", ps.Server.Password, proxyCommand, ps.Server.Port)
2022-05-02 12:01:57 +08:00
}
}
2022-11-23 10:30:02 +08:00
func (ps ProjectServer) ReplaceVars(script string) string {
2023-08-04 18:31:15 +08:00
return ps.Server.ReplaceVars(script)
2022-11-23 10:30:02 +08:00
}