2020-08-04 14:28:25 +08:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Pagination struct
|
|
|
|
type Pagination struct {
|
|
|
|
Page uint64 `json:"page"`
|
|
|
|
Rows uint64 `json:"rows"`
|
|
|
|
Total uint64 `json:"total"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// state type
|
|
|
|
const (
|
|
|
|
Fail = iota
|
|
|
|
Success
|
|
|
|
)
|
|
|
|
|
|
|
|
// state type
|
|
|
|
const (
|
|
|
|
Disable = iota
|
|
|
|
Enable
|
|
|
|
)
|
|
|
|
|
2020-09-25 20:05:25 +08:00
|
|
|
// review state type
|
|
|
|
const (
|
|
|
|
PENDING = iota
|
|
|
|
APPROVE
|
|
|
|
DENY
|
|
|
|
)
|
|
|
|
|
2020-08-04 14:28:25 +08:00
|
|
|
// DB init when the program start
|
|
|
|
var DB *sql.DB
|
|
|
|
|
2020-08-15 13:38:06 +08:00
|
|
|
// Init DB
|
2020-08-04 14:28:25 +08:00
|
|
|
func Init() {
|
|
|
|
dbType := os.Getenv("DB_TYPE")
|
|
|
|
dbConn := os.Getenv("DB_CONN")
|
|
|
|
var err error
|
|
|
|
DB, err = sql.Open(dbType, dbConn)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PaginationFrom param return pagination struct
|
|
|
|
func PaginationFrom(param url.Values) (Pagination, error) {
|
|
|
|
page, err := strconv.ParseUint(param.Get("page"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return Pagination{}, errors.New("invalid page")
|
|
|
|
}
|
|
|
|
rows, err := strconv.ParseUint(param.Get("rows"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return Pagination{}, errors.New("invalid rows")
|
|
|
|
}
|
|
|
|
pagination := Pagination{Page: page, Rows: rows}
|
|
|
|
return pagination, nil
|
|
|
|
}
|
|
|
|
|
2021-02-26 16:45:13 +08:00
|
|
|
const ddl string = "CREATE DATABASE IF NOT EXISTS `goploy`; CREATE TABLE IF NOT EXISTS `goploy`.`log` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `type` tinyint(3) UNSIGNED NOT NULL DEFAULT 1 COMMENT 'log type', `ip` int(10) UNSIGNED NOT NULL DEFAULT 0, `desc` varchar(30) NOT NULL DEFAULT '' COMMENT 'description', `user_id` int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '', `create_time` int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '', PRIMARY KEY USING BTREE (`id`), INDEX `idx_create_time` USING BTREE(`create_time`) ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci; CREATE TABLE IF NOT EXISTS `goploy`.`project` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `namespace_id` int(10) UNSIGNED NOT NULL DEFAULT 0, `name` varchar(255) NOT NULL DEFAULT '' COMMENT 'project name', `url` varchar(255) NOT NULL DEFAULT '' COMMENT 'repository url', `path` varchar(255) NOT NULL DEFAULT '' COMMENT 'project deploy path', `symlink_path` varchar(255) NOT NULL DEFAULT '' COMMENT '(ln -sfn symlink_path/uuid project_path)', `environment` tinyint(4) UNSIGNED NOT NULL DEFAULT 0 COMMENT '1.production 2.pre-release 3.test 4.development', `branch` varchar(255) NOT NULL DEFAULT 'master' COMMENT 'repository branch', `review` tinyint(4) UNSIGNED NOT NULL DEFAULT '0' COMMENT '0.disable 1.enable', `review_url` varchar(1000) NOT NULL DEFAULT '' COMMENT 'review notification link', `after_pull_script_mode` varchar(20) NOT NULL DEFAULT '' COMMENT 'sh|php|py|...', `after_pull_script` text NOT NULL COMMENT '', `after_deploy_script_mode` varchar(20) NOT NULL DEFAULT '' COMMENT 'sh|php|py|...', `after_deploy_script` text NOT NULL COMMENT '', `rsync_option` varchar(255) NOT NULL DEFAULT '' COMMENT 'rsync options', `auto_deploy` tinyint(4) UNSIGNED NOT NULL DEFAULT 1 COMMENT '0.disable 1.webhook', `state` tinyint(4) UNSIGNED NOT NULL DEFAULT 1 COMMENT '0.disable 1.enable', `deploy_state` tinyint(4) UNSIGNED NOT NULL DEFAULT 0 COMMENT '0.not deploy 1.deploying 2.success 3.fail', `publisher_id` int(10) UNSIGNED NOT NULL DEFAULT 0, `publisher_name` varchar(255) NOT NULL DEFAULT '', `last_publish_token` char(36) CHARACTER SET utf8mb4 NOT NULL DEFAULT '', `notify_type` tinyint(4) UNSIGNED NOT NULL DEFAULT 0 COMMENT '1.weixin 2.ding talk 3.feishu 255.custom', `notify_target` varchar(255) NOT NULL DEFAULT '' COMMENT '', `insert_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY USING BTREE (`id`) ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci; CREATE TABLE IF NOT EXISTS `goploy`.`project_file` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `project_id` int(10) UNSIGNED NOT NULL, `filename` varchar(255) NOT NULL DEFAULT '', `insert_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY USING BTREE (`id`), KEY `idx_project_id` USING BTREE (`project_id`) ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci; CREATE TABLE IF NOT EXISTS `goploy`.`project_server` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `project_id` int(10) UNSIGNED NOT NULL DEFAULT 0, `server_id` int(10) UNSIGNED NOT NULL DEFAULT 0, `insert_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY USING BTREE (`id`), UNIQUE INDEX `uk_project_server` USING BTREE (`project_id`, `server_id`) ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci; CREATE TABLE IF NOT EXISTS `goploy`.`project_user` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `project_id` int(10) UNSIGNED NOT NULL DEFAULT 0, `user_id` int(10) UNSIGNED NOT NULL DEFAULT 0, `insert_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY USING BTREE (`id`), UNIQUE INDEX `uk_project_user` USING BTREE (`projec
|
2020-09-16 15:23:28 +08:00
|
|
|
const dml string = "REPLACE INTO `goploy`.`user`(`id`, `account`, `password`, `name`, `contact`, `state`, `super_manager`) VALUES (1, 'admin', '$2a$10$89ZJ2xeJj35GOw11Qiucr.phaEZP4.kBX6aKTs7oWFp1xcGBBgijm', '超管', '', 1, 1); REPLACE INTO `goploy`.`namespace`(`id`, `name`) VALUES (1, 'goploy'); REPLACE INTO `goploy`.`namespace_user`(`id`, `namespace_id`, `user_id`, `role`) VALUES (1, 1, 1, 'admin');"
|
2020-08-04 14:28:25 +08:00
|
|
|
|
2020-08-04 20:00:21 +08:00
|
|
|
// ImportSQL -
|
2020-08-04 14:28:25 +08:00
|
|
|
func ImportSQL(db *sql.DB) error {
|
|
|
|
for _, query := range strings.Split(ddl, ";") {
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err := db.Exec(query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, query := range strings.Split(dml, ";") {
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err := db.Exec(query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|