goploy/model/ServerModel.go

322 lines
5.8 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
import (
2022-01-15 14:21:31 +08:00
"github.com/zhenorzz/goploy/utils"
2020-08-04 20:00:21 +08:00
2020-08-04 14:28:25 +08:00
sq "github.com/Masterminds/squirrel"
)
const serverTable = "`server`"
type Server struct {
2022-01-15 14:21:31 +08:00
ID int64 `json:"id"`
Name string `json:"name"`
IP string `json:"ip"`
Port int `json:"port"`
Owner string `json:"owner"`
Path string `json:"path"`
Password string `json:"password"`
JumpIP string `json:"jumpIP"`
JumpPort int `json:"jumpPort"`
JumpOwner string `json:"jumpOwner"`
JumpPath string `json:"jumpPath"`
JumpPassword string `json:"jumpPassword"`
NamespaceID int64 `json:"namespaceId"`
Description string `json:"description"`
2022-05-15 11:12:58 +08:00
OS string `json:"os"`
2022-01-15 14:21:31 +08:00
OSInfo string `json:"osInfo"`
State int8 `json:"state"`
InsertTime string `json:"insertTime"`
UpdateTime string `json:"updateTime"`
2020-08-04 14:28:25 +08:00
}
type Servers []Server
2022-04-09 14:52:19 +08:00
func (s Server) GetList() (Servers, error) {
2020-08-04 14:28:25 +08:00
rows, err := sq.
2022-01-15 14:21:31 +08:00
Select(
"id",
"namespace_id",
"name",
"ip",
"port",
"owner",
"path",
"password",
"jump_ip",
"jump_port",
"jump_owner",
"jump_path",
"jump_password",
"description",
2022-05-15 11:12:58 +08:00
"os",
2022-01-15 14:21:31 +08:00
"os_info",
"state",
"insert_time",
"update_time").
2020-08-04 14:28:25 +08:00
From(serverTable).
Where(sq.Eq{
2021-02-26 16:45:13 +08:00
"namespace_id": []int64{0, s.NamespaceID},
2020-08-04 14:28:25 +08:00
}).
OrderBy("id DESC").
RunWith(DB).
Query()
if err != nil {
return nil, err
}
servers := Servers{}
for rows.Next() {
var server Server
2021-02-07 15:43:02 +08:00
if err := rows.Scan(
&server.ID,
2021-02-26 16:45:13 +08:00
&server.NamespaceID,
2021-02-07 15:43:02 +08:00
&server.Name,
&server.IP,
&server.Port,
&server.Owner,
&server.Path,
&server.Password,
2022-01-15 14:21:31 +08:00
&server.JumpIP,
&server.JumpPort,
&server.JumpOwner,
&server.JumpPath,
&server.JumpPassword,
2021-02-07 15:43:02 +08:00
&server.Description,
2022-05-15 11:12:58 +08:00
&server.OS,
2021-09-18 16:35:50 +08:00
&server.OSInfo,
2021-05-25 16:12:08 +08:00
&server.State,
2021-02-07 15:43:02 +08:00
&server.InsertTime,
&server.UpdateTime); err != nil {
2020-08-04 14:28:25 +08:00
return nil, err
}
servers = append(servers, server)
}
return servers, nil
}
func (s Server) GetAll() (Servers, error) {
rows, err := sq.
2022-01-15 14:21:31 +08:00
Select(
"id",
"name",
"ip",
"owner",
"description",
"insert_time",
"update_time").
2020-08-04 14:28:25 +08:00
From(serverTable).
Where(sq.Eq{
2021-02-26 16:45:13 +08:00
"namespace_id": []int64{0, s.NamespaceID},
2020-08-04 14:28:25 +08:00
"state": Enable,
}).
OrderBy("id DESC").
RunWith(DB).
Query()
if err != nil {
return nil, err
}
servers := Servers{}
for rows.Next() {
var server Server
2022-01-15 14:21:31 +08:00
if err := rows.Scan(
&server.ID,
&server.Name,
&server.IP,
&server.Owner,
&server.Description,
&server.InsertTime,
&server.UpdateTime); err != nil {
2020-08-04 14:28:25 +08:00
return nil, err
}
servers = append(servers, server)
}
return servers, nil
}
func (s Server) GetData() (Server, error) {
var server Server
2022-03-16 10:18:08 +08:00
builder := sq.
2022-01-15 14:21:31 +08:00
Select(
"id",
"namespace_id",
"name",
2022-05-15 11:12:58 +08:00
"os",
2022-01-15 14:21:31 +08:00
"ip",
"port",
"owner",
"path",
"password",
"jump_ip",
"jump_port",
"jump_owner",
"jump_path",
"jump_password",
2022-04-29 14:59:18 +08:00
"state",
2022-01-15 14:21:31 +08:00
).
2022-03-16 10:18:08 +08:00
From(serverTable)
if s.ID > 0 {
builder = builder.Where(sq.Eq{"id": s.ID})
}
if s.Name != "" {
builder = builder.Where(sq.Eq{"name": s.Name})
}
if s.IP != "" {
builder = builder.Where(sq.Eq{"ip": s.IP})
}
err := builder.
2020-08-04 14:28:25 +08:00
OrderBy("id DESC").
RunWith(DB).
QueryRow().
2022-01-15 14:21:31 +08:00
Scan(&server.ID,
&server.NamespaceID,
&server.Name,
2022-05-15 11:12:58 +08:00
&server.OS,
2022-01-15 14:21:31 +08:00
&server.IP,
&server.Port,
&server.Owner,
&server.Path,
&server.Password,
&server.JumpIP,
&server.JumpPort,
&server.JumpOwner,
&server.JumpPath,
&server.JumpPassword,
2022-04-29 14:59:18 +08:00
&server.State,
2022-01-15 14:21:31 +08:00
)
2020-08-04 14:28:25 +08:00
if err != nil {
2021-11-20 15:51:19 +08:00
return server, err
2020-08-04 14:28:25 +08:00
}
return server, nil
}
2020-08-15 13:38:06 +08:00
// AddRow return LastInsertId
2020-08-04 14:28:25 +08:00
func (s Server) AddRow() (int64, error) {
result, err := sq.
Insert(serverTable).
2022-01-15 14:21:31 +08:00
Columns(
"namespace_id",
"name",
2022-05-15 11:12:58 +08:00
"os",
2022-01-15 14:21:31 +08:00
"ip",
"port",
"owner",
"password",
"path",
"jump_ip",
"jump_port",
"jump_owner",
"jump_password",
"jump_path",
"description",
"os_info").
Values(
s.NamespaceID,
s.Name,
2022-05-15 11:12:58 +08:00
s.OS,
2022-01-15 14:21:31 +08:00
s.IP,
s.Port,
s.Owner,
s.Password,
s.Path,
s.JumpIP,
s.JumpPort,
s.JumpOwner,
s.JumpPassword,
s.JumpPath,
s.Description,
s.OSInfo).
2020-08-04 14:28:25 +08:00
RunWith(DB).
Exec()
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
return id, err
}
2020-08-15 13:38:06 +08:00
// EditRow -
2020-08-04 14:28:25 +08:00
func (s Server) EditRow() error {
_, err := sq.
Update(serverTable).
SetMap(sq.Eq{
2022-01-15 14:21:31 +08:00
"namespace_id": s.NamespaceID,
"name": s.Name,
2022-05-15 11:12:58 +08:00
"os": s.OS,
2022-01-15 14:21:31 +08:00
"ip": s.IP,
"port": s.Port,
"owner": s.Owner,
"password": s.Password,
"path": s.Path,
"jump_ip": s.JumpIP,
"jump_port": s.JumpPort,
"jump_owner": s.JumpOwner,
"jump_password": s.JumpPassword,
"jump_path": s.JumpPath,
"description": s.Description,
"os_info": s.OSInfo,
2020-08-04 14:28:25 +08:00
}).
Where(sq.Eq{"id": s.ID}).
RunWith(DB).
Exec()
return err
}
2021-05-25 16:12:08 +08:00
// ToggleRow -
func (s Server) ToggleRow() error {
2020-08-04 14:28:25 +08:00
tx, err := DB.Begin()
if err != nil {
2022-04-10 15:26:46 +08:00
return err
2020-08-04 14:28:25 +08:00
}
_, err = sq.
Update(serverTable).
SetMap(sq.Eq{
2021-05-25 16:12:08 +08:00
"state": s.State,
2020-08-04 14:28:25 +08:00
}).
Where(sq.Eq{"id": s.ID}).
RunWith(tx).
Exec()
if err != nil {
tx.Rollback()
return err
}
2021-05-25 16:12:08 +08:00
if s.State == Disable {
_, err = sq.
Delete(projectServerTable).
Where(sq.Eq{"server_id": s.ID}).
RunWith(tx).
Exec()
if err != nil {
tx.Rollback()
return err
}
2020-08-04 14:28:25 +08:00
}
if err = tx.Commit(); err != nil {
2022-04-10 15:26:46 +08:00
return err
2020-08-04 14:28:25 +08:00
}
return nil
}
2022-01-15 14:21:31 +08:00
2022-04-29 14:59:18 +08:00
func (s Server) ToSSHConfig() utils.SSHConfig {
2022-01-15 14:21:31 +08:00
return utils.SSHConfig{
User: s.Owner,
Password: s.Password,
Path: s.Path,
Host: s.IP,
Port: s.Port,
JumpUser: s.JumpOwner,
JumpPassword: s.JumpPassword,
JumpPath: s.JumpPath,
JumpHost: s.JumpIP,
JumpPort: s.JumpPort,
}
}