-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get nickname from seahub database #663
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -379,3 +379,124 @@ load_ccnet_database_config (SeafileSession *session) | |
g_free (engine); | ||
return ret; | ||
} | ||
|
||
static char * | ||
parse_seahub_db_config () | ||
{ | ||
char buf[1024]; | ||
GError *error = NULL; | ||
int retcode = 0; | ||
char *child_stdout = NULL; | ||
char *child_stderr = NULL; | ||
|
||
char *binary_path = g_find_program_in_path ("parse_seahub_db.py"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 怎样保证能找到这个脚本呢?需要把脚本放在 bin 目录下面的话,需要记录一个任务告诉马宇航处理一下。 |
||
|
||
snprintf (buf, | ||
sizeof(buf), | ||
"python3 %s", | ||
binary_path); | ||
g_spawn_command_line_sync (buf, | ||
&child_stdout, | ||
&child_stderr, | ||
&retcode, | ||
&error); | ||
|
||
if (error != NULL) { | ||
seaf_warning ("Failed to run python parse_seahub_db.py: %s\n", error->message); | ||
g_free (binary_path); | ||
g_clear_error (&error); | ||
return NULL; | ||
} | ||
g_spawn_check_exit_status (retcode, &error); | ||
if (error != NULL) { | ||
seaf_warning ("Failed to run python parse_seahub_db.py: %s\n", error->message); | ||
g_free (binary_path); | ||
g_clear_error (&error); | ||
return NULL; | ||
} | ||
|
||
g_free (binary_path); | ||
return g_strdup(child_stdout); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个应该不用复制吧,因为本来就是新分配的。 |
||
} | ||
|
||
int | ||
load_seahub_database_config (SeafileSession *session) | ||
{ | ||
int ret = 0; | ||
json_t *object = NULL; | ||
json_error_t err; | ||
const char *engine = NULL, *name = NULL, *user = NULL, *password = NULL, *host = NULL, *charset = NULL; | ||
int port; | ||
char *json_str = NULL; | ||
|
||
json_str = parse_seahub_db_config (); | ||
if (!json_str){ | ||
seaf_warning ("Failed to parse seahub database config.\n"); | ||
ret = -1; | ||
goto out; | ||
} | ||
|
||
object = json_loadb (json_str, strlen(json_str), 0, &err); | ||
if (!object) { | ||
seaf_warning ("Failed to load seahub db json: %s: %s\n", json_str, err.text); | ||
ret = -1; | ||
goto out; | ||
} | ||
|
||
engine = json_object_get_string_member (object, "ENGINE"); | ||
name = json_object_get_string_member (object, "NAME"); | ||
user = json_object_get_string_member (object, "USER"); | ||
password = json_object_get_string_member (object, "PASSWORD"); | ||
host = json_object_get_string_member (object, "HOST"); | ||
charset = json_object_get_string_member (object, "CHARSET"); | ||
port = json_object_get_int_member (object, "PORT"); | ||
if (port <= 0) { | ||
port = MYSQL_DEFAULT_PORT; | ||
} | ||
|
||
if (!engine || strstr (engine, "sqlite") != NULL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sqlite 是不能支持多进程同时打开的吧,而且11版本之后 sqlite 已经不支持了。所以这里如果检测到 sqlite 就不用做 username 的转换了。 |
||
goto out; | ||
} | ||
#ifdef HAVE_MYSQL | ||
else if (strstr (engine, "mysql") != NULL) { | ||
seaf_message("Use database Mysql\n"); | ||
if (!host) { | ||
seaf_warning ("Seahub DB host not set in config.\n"); | ||
ret = -1; | ||
goto out; | ||
} | ||
if (!user) { | ||
seaf_warning ("Seahub DB user not set in config.\n"); | ||
ret = -1; | ||
goto out; | ||
} | ||
if (!password) { | ||
seaf_warning ("Seahub DB password not set in config.\n"); | ||
ret = -1; | ||
goto out; | ||
} | ||
if (!name) { | ||
seaf_warning ("Seahub DB name not set in config.\n"); | ||
ret = -1; | ||
goto out; | ||
} | ||
|
||
session->seahub_db = seaf_db_new_mysql (host, port, user, password, name, NULL, FALSE, FALSE, NULL, charset, DEFAULT_MAX_CONNECTIONS); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 需要检查必要的参数是否已经设置了。 |
||
if (!session->seahub_db) { | ||
seaf_warning ("Failed to open seahub database.\n"); | ||
ret = -1; | ||
goto out; | ||
} | ||
} | ||
#endif | ||
else { | ||
seaf_warning ("Unknown database type: %s.\n", engine); | ||
ret = -1; | ||
} | ||
|
||
out: | ||
if (object) | ||
json_decref (object); | ||
g_free (json_str); | ||
return ret; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -317,6 +317,7 @@ AC_CONFIG_FILES( | |
controller/Makefile | ||
tools/Makefile | ||
doc/Makefile | ||
scripts/Makefile | ||
) | ||
|
||
AC_OUTPUT |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,15 +5,18 @@ import ( | |
"crypto/tls" | ||
"crypto/x509" | ||
"database/sql" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
"os/signal" | ||
"path/filepath" | ||
"runtime/debug" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
|
||
|
@@ -41,7 +44,7 @@ var pidFilePath string | |
var logFp *os.File | ||
|
||
var dbType string | ||
var seafileDB, ccnetDB *sql.DB | ||
var seafileDB, ccnetDB, seahubDB *sql.DB | ||
|
||
// when SQLite is used, user and group db are separated. | ||
var userDB, groupDB *sql.DB | ||
|
@@ -269,6 +272,69 @@ func loadSeafileDB() { | |
dbType = dbEngine | ||
} | ||
|
||
func loadSeahubDB() { | ||
scriptPath, err := exec.LookPath("parse_seahub_db.py") | ||
if err != nil { | ||
log.Warnf("Failed to find script of parse_seahub_db.py: %v", err) | ||
return | ||
} | ||
cmd := exec.Command("python3", scriptPath) | ||
dbData, err := cmd.CombinedOutput() | ||
if err != nil { | ||
log.Warnf("Failed to run python parse_seahub_db.py: %v", err) | ||
return | ||
} | ||
|
||
dbConfig := make(map[string]string) | ||
|
||
err = json.Unmarshal(dbData, &dbConfig) | ||
if err != nil { | ||
log.Warnf("Failed to decode seahub database json file: %v", err) | ||
return | ||
} | ||
|
||
dbEngine := dbConfig["ENGINE"] | ||
dbName := dbConfig["NAME"] | ||
user := dbConfig["USER"] | ||
password := dbConfig["PASSWORD"] | ||
host := dbConfig["HOST"] | ||
portStr := dbConfig["PORT"] | ||
|
||
if strings.Index(dbEngine, "mysql") >= 0 { | ||
port, err := strconv.ParseInt(portStr, 10, 64) | ||
if err != nil || port <= 0 { | ||
port = 3306 | ||
} | ||
if dbName == "" { | ||
log.Warnf("Seahub DB name not set in config") | ||
return | ||
} | ||
if user == "" { | ||
log.Warnf("Seahub DB user not set in config") | ||
return | ||
} | ||
if password == "" { | ||
log.Warnf("Seahub DB password not set in config") | ||
return | ||
} | ||
if host == "" { | ||
log.Warnf("Seahub DB host not set in config") | ||
return | ||
} | ||
|
||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=%t", user, password, host, port, dbName, false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里也需要类似 C 部分改一下。 另外,怎样保证在 go fileserver 运行之前 seaf-server 已经解析好了 seahub_db.json 呢?而且把数据库密码这些敏感信息写入到 tmp 下面也有安全问题的,应该改为 seaf-server 和 go fileserver 都直接把 python 脚本的输出读取到内存里面,不需要临时文件。 |
||
|
||
seahubDB, err = sql.Open("mysql", dsn) | ||
if err != nil { | ||
log.Warnf("Failed to open database: %v", err) | ||
} | ||
} else if strings.Index(dbEngine, "sqlite") >= 0 { | ||
return | ||
} else { | ||
log.Warnf("Unsupported database %s.", dbEngine) | ||
} | ||
} | ||
|
||
func writePidFile(pid_file_path string) error { | ||
file, err := os.OpenFile(pid_file_path, os.O_CREATE|os.O_WRONLY, 0664) | ||
if err != nil { | ||
|
@@ -367,6 +433,8 @@ func main() { | |
fp.Close() | ||
} | ||
|
||
loadSeahubDB() | ||
|
||
repomgr.Init(seafileDB) | ||
|
||
fsmgr.Init(centralDir, dataDir, option.FsCacheLimit) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个字符串要释放一下吧。