harbor/dao/base.go

100 lines
2.3 KiB
Go
Raw Normal View History

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"
"time"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql" //register mysql driver
2016-02-01 12:59:10 +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
}
// 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
}
//InitDB initializes the database
2016-02-23 13:49:59 +01:00
func InitDB() {
2016-02-01 12:59:10 +01:00
orm.RegisterDriver("mysql", orm.DRMySQL)
addr := os.Getenv("MYSQL_HOST")
port := os.Getenv("MYSQL_PORT")
2016-02-01 12:59:10 +01:00
username := os.Getenv("MYSQL_USR")
password := os.Getenv("MYSQL_PWD")
2016-02-01 12:59:10 +01: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 {
c, err = net.Dial("tcp", addr+":"+port)
if err == nil {
c.Close()
ch <- 1
} else {
2016-03-29 07:33:49 +02:00
log.Info("failed to connect to db, retry after 2 seconds...")
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
}