2016-02-01 12:59:10 +01:00
|
|
|
/*
|
2016-02-23 05:42:22 +01:00
|
|
|
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2016-02-01 12:59:10 +01:00
|
|
|
*/
|
2016-02-26 11:54:14 +01:00
|
|
|
|
2016-02-01 12:59:10 +01:00
|
|
|
package dao
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2016-03-29 07:33:49 +02:00
|
|
|
"github.com/vmware/harbor/utils/log"
|
|
|
|
|
2016-02-01 12:59:10 +01:00
|
|
|
"os"
|
|
|
|
"strings"
|
2016-05-24 03:41:34 +02:00
|
|
|
"sync"
|
2016-02-01 12:59:10 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
2016-02-26 11:13:13 +01:00
|
|
|
_ "github.com/go-sql-driver/mysql" //register mysql driver
|
2016-02-01 12:59:10 +01:00
|
|
|
)
|
|
|
|
|
2016-02-26 11:13:13 +01:00
|
|
|
// NonExistUserID : if a user does not exist, the ID of the user will be 0.
|
2016-02-26 04:26:54 +01:00
|
|
|
const NonExistUserID = 0
|
2016-02-01 12:59:10 +01:00
|
|
|
|
|
|
|
func isIllegalLength(s string, min int, max int) bool {
|
|
|
|
if min == -1 {
|
|
|
|
return (len(s) > max)
|
|
|
|
}
|
|
|
|
if max == -1 {
|
|
|
|
return (len(s) <= min)
|
|
|
|
}
|
|
|
|
return (len(s) < min || len(s) > max)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isContainIllegalChar(s string, illegalChar []string) bool {
|
|
|
|
for _, c := range illegalChar {
|
|
|
|
if strings.Index(s, c) >= 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:13:13 +01:00
|
|
|
// GenerateRandomString generates a random string
|
2016-02-01 12:59:10 +01:00
|
|
|
func GenerateRandomString() (string, error) {
|
|
|
|
o := orm.NewOrm()
|
2016-02-23 05:42:22 +01:00
|
|
|
var uuid string
|
|
|
|
err := o.Raw(`select uuid() as uuid`).QueryRow(&uuid)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-02-23 05:42:22 +01:00
|
|
|
return uuid, nil
|
2016-02-01 12:59:10 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:13:13 +01:00
|
|
|
//InitDB initializes the database
|
2016-02-23 13:49:59 +01:00
|
|
|
func InitDB() {
|
2016-05-19 10:09:44 +02:00
|
|
|
// orm.Debug = true
|
2016-02-01 12:59:10 +01:00
|
|
|
orm.RegisterDriver("mysql", orm.DRMySQL)
|
|
|
|
addr := os.Getenv("MYSQL_HOST")
|
2016-04-01 12:54:21 +02:00
|
|
|
port := os.Getenv("MYSQL_PORT")
|
2016-02-01 12:59:10 +01:00
|
|
|
username := os.Getenv("MYSQL_USR")
|
2016-04-01 12:54:21 +02:00
|
|
|
password := os.Getenv("MYSQL_PWD")
|
2016-02-01 12:59:10 +01:00
|
|
|
|
2016-04-01 12:54:21 +02:00
|
|
|
log.Debugf("db url: %s:%s, db user: %s", addr, port, username)
|
2016-02-26 04:26:54 +01:00
|
|
|
dbStr := username + ":" + password + "@tcp(" + addr + ":" + port + ")/registry"
|
2016-02-01 12:59:10 +01:00
|
|
|
ch := make(chan int, 1)
|
|
|
|
go func() {
|
|
|
|
var err error
|
|
|
|
var c net.Conn
|
|
|
|
for {
|
2016-04-20 08:44:19 +02:00
|
|
|
c, err = net.DialTimeout("tcp", addr+":"+port, 20*time.Second)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err == nil {
|
|
|
|
c.Close()
|
|
|
|
ch <- 1
|
|
|
|
} else {
|
2016-04-19 10:00:23 +02:00
|
|
|
log.Errorf("failed to connect to db, retry after 2 seconds :%v", err)
|
2016-02-01 12:59:10 +01:00
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-ch:
|
2016-02-23 05:42:22 +01:00
|
|
|
case <-time.After(60 * time.Second):
|
|
|
|
panic("Failed to connect to DB after 60 seconds")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-02-26 04:26:54 +01:00
|
|
|
err := orm.RegisterDataBase("default", "mysql", dbStr)
|
2016-02-23 13:49:59 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-05-20 10:36:10 +02:00
|
|
|
|
|
|
|
var globalOrm orm.Ormer
|
2016-05-24 03:41:34 +02:00
|
|
|
var once sync.Once
|
2016-05-20 10:36:10 +02:00
|
|
|
|
2016-05-20 10:54:24 +02:00
|
|
|
// GetOrmer :set ormer singleton
|
2016-05-20 10:36:10 +02:00
|
|
|
func GetOrmer() orm.Ormer {
|
2016-05-24 03:41:34 +02:00
|
|
|
once.Do(func() {
|
2016-05-20 10:36:10 +02:00
|
|
|
globalOrm = orm.NewOrm()
|
2016-05-24 03:41:34 +02:00
|
|
|
})
|
2016-05-20 10:36:10 +02:00
|
|
|
return globalOrm
|
|
|
|
}
|