make database config easier and update script for pushing releases

This commit is contained in:
or-else
2018-04-17 18:28:43 -07:00
parent 24787dbe30
commit 80c0cb44f9
7 changed files with 106 additions and 24 deletions

View File

@ -3,6 +3,85 @@
# Cross-compiling script using https://github.com/mitchellh/gox
# I use this to compile the Linux version of the server on my Mac.
~/go/bin/gox -osarch="linux/amd64" -ldflags "-X main.buildstamp=rdb.`date -u '+%Y%m%dT%H:%M:%SZ'`" -tags rethinkdb ./server
~/go/bin/gox -osarch="linux/amd64" -tags rethinkdb ./tinode-db
~/go/bin/gox -osarch="linux/amd64" ./keygen
# Supported OSs: darwin windows linux
goplat=( darwin windows linux )
# Supported CPU architectures: amd64
goarc=( amd64 )
# Supported database tags
dbtags=( mysql rethinkdb )
pushd ${GOHOME}chat > /dev/null
# Get last git tag as release version. Tag looks like 'v.1.2.3', so strip 'v'.
version=`git tag | tail -1`
version=${version#?}
for plat in "${goplat[@]}"
do
for arc in "${goarc[@]}"
do
# Keygen is database-independent
# Remove previous build
rm -f $GOPATH/bin/keygen
# Build
~/go/bin/gox -osarch="${plat}/${arc}" -output $GOPATH/bin/keygen ./keygen > /dev/null
for dbtag in "${dbtags[@]}"
do
echo "Building ${dbtag}-${plat}/${arc}..."
# Remove previous builds
rm -f $GOPATH/bin/tinode
rm -f $GOPATH/bin/init-db
# Build tinode server and database initializer for RethinkDb and MySQL.
~/go/bin/gox -osarch="${plat}/${arc}" \
-ldflags "-X main.buildstamp=`date -u '+%Y%m%dT%H:%M:%SZ'` -X main.builtfordb=${dbtag}" \
-tags ${dbtag} -output $GOPATH/bin/tinode ./server > /dev/null
~/go/bin/gox -osarch="${plat}/${arc}" -tags ${dbtag} -output $GOPATH/bin/init-db ./tinode-db > /dev/null
# Tar on Mac is inflexible about directories. Let's just copy release files to
# one directory.
rm -fR ./releases/tmp
mkdir -p ./releases/tmp/static
# Copy templates and database initialization files
cp ./server/tinode.conf ./releases/tmp
cp -R ./server/templ ./releases/tmp
cp -R ./server/static/img ./releases/tmp/static
cp -R ./server/static/audio ./releases/tmp/static
cp -R ./server/static/css ./releases/tmp/static
cp ./server/static/index.html ./releases/tmp/static
cp ./server/static/tinode.js ./releases/tmp/static
cp ./server/static/drafty.js ./releases/tmp/static
cp ./tinode-db/data.json ./releases/tmp
cp ./tinode-db/*.jpg ./releases/tmp
cp ./tinode-db/credentials.sh ./releases/tmp
# Build archive. All platforms but Windows use tar for archiving. Windows uses zip.
if [ "$plat" = "windows" ]; then
# Copy binaries
cp $GOPATH/bin/tinode.exe ./releases/tmp
cp $GOPATH/bin/init-db.exe ./releases/tmp
cp $GOPATH/bin/keygen.exe ./releases/tmp
# Remove possibly existing archive.
rm -f ./releases/tinode-${dbtag}."${version}.${plat}-${arc}".zip
# Generate a new one
pushd ./releases/tmp > /dev/null
zip -q -r ../tinode-${dbtag}."${version}.${plat}-${arc}".zip ./*
popd > /dev/null
else
# Copy binaries
cp $GOPATH/bin/tinode ./releases/tmp
cp $GOPATH/bin/init-db ./releases/tmp
cp $GOPATH/bin/keygen ./releases/tmp
# Remove possibly existing archive.
rm -f ./releases/tinode-${dbtag}."${version}.${plat}-${arc}".tar.gz
# Generate a new one
tar -C ${GOHOME}chat/releases/tmp -zcf ./releases/tinode-${dbtag}."${version}.${plat}-${arc}".tar.gz .
fi
done
done
done
popd > /dev/null

View File

@ -14,11 +14,6 @@ while IFS='' read -r line || [[ -n $line ]] ; do
done < /config.template
# Check if user requested to reset database.
if [[ "$1" = "-r" || "$1" = "--reset_db" ]]; then
rm -f /botdata/.tn-cookie
fi
# Initialize the database if it has not been initialized yet
if [ ! -f /botdata/.tn-cookie ]; then
# Run the generator. Save stdout to to a file to extract Tino's password for possible later use.

View File

@ -88,7 +88,10 @@ const (
// Build timestamp defined by the compiler.
// To define buildstamp as a timestamp of when the server was built add a flag to compiler command line:
// -ldflags "-X main.buildstamp=`date -u '+%Y%m%dT%H:%M:%SZ'`"
var buildstamp = "buildstamp-undefined"
var buildstamp = "undefined"
// Database adapter that this binary was built for. Defined at compile time.
var builtfordb = "undefined"
// CredValidator holds additional config params for a credential validator.
type credValidator struct {
@ -168,10 +171,11 @@ type configType struct {
}
func main() {
log.Printf("Server 'v%s:%s'; pid %d; started with %d process(es)", currentVersion, buildstamp, os.Getpid(),
runtime.GOMAXPROCS(runtime.NumCPU()))
log.Printf("Server 'v%s:%s:%s'; pid %d; started with %d process(es)", currentVersion,
buildstamp, builtfordb, os.Getpid(), runtime.GOMAXPROCS(runtime.NumCPU()))
var configfile = flag.String("config", "./tinode.conf", "Path to config file.")
var useAdapter = flag.String("store_use_adapter", builtfordb, "Override default database adapter")
// Path to static content.
var staticPath = flag.String("static_data", "", "Path to /static data for the server.")
var listenOn = flag.String("listen", "", "Override address and port to listen on for HTTP(S) clients.")
@ -197,7 +201,7 @@ func main() {
// Cluster won't be started here yet.
workerId := clusterInit(config.Cluster, clusterSelf)
var err = store.Open(workerId, string(config.Store))
var err = store.Open(workerId, *useAdapter, string(config.Store))
if err != nil {
log.Fatal("Failed to connect to DB:", err)
}

View File

@ -390,7 +390,7 @@ func (s *Session) hello(msg *ClientComMessage) {
s.queueOut(ErrVersionNotSupported(msg.Hi.Id, "", msg.timestamp))
return
}
params = map[string]interface{}{"ver": currentVersion, "build": buildstamp}
params = map[string]interface{}{"ver": currentVersion, "build": builtfordb + ":" + buildstamp}
} else if msg.Hi.Version == "" || parseVersion(msg.Hi.Version) == s.ver {
// Save changed device ID or Lang.

View File

@ -26,15 +26,18 @@ type configType struct {
Adapters map[string]json.RawMessage `json:"adapters"`
}
func openAdapter(workerId int, jsonconf string) error {
func openAdapter(workerId int, useAdapter, jsonconf string) error {
var config configType
if err := json.Unmarshal([]byte(jsonconf), &config); err != nil {
return errors.New("store: failed to parse config: " + err.Error() + "(" + jsonconf + ")")
}
adp = dbAdapters[config.UseAdapter]
if useAdapter == "" {
useAdapter = config.UseAdapter
}
adp = dbAdapters[useAdapter]
if adp == nil {
return errors.New("store: attept to Open an unknown adapter '" + config.UseAdapter + "'")
return errors.New("store: attept to Open an unknown adapter '" + useAdapter + "'")
}
if adp.IsOpen() {
@ -52,7 +55,7 @@ func openAdapter(workerId int, jsonconf string) error {
var adapter_config string
if config.Adapters != nil {
adapter_config = string(config.Adapters[config.UseAdapter])
adapter_config = string(config.Adapters[useAdapter])
}
return adp.Open(adapter_config)
@ -61,8 +64,8 @@ func openAdapter(workerId int, jsonconf string) error {
// Open initializes the persistence system. Adapter holds a connection pool for a database instance.
// name - name of the adapter rquested in the config file
// jsonconf - configuration string
func Open(workerId int, jsonconf string) error {
if err := openAdapter(workerId, jsonconf); err != nil {
func Open(workerId int, useAdapter, jsonconf string) error {
if err := openAdapter(workerId, useAdapter, jsonconf); err != nil {
return err
}
if err := adp.CheckDbVersion(); err != nil {
@ -92,9 +95,9 @@ func IsOpen() bool {
// InitDb creates a new database instance. If 'reset' is true it will first attempt to drop
// existing database. If jsconf is nil it will assume that the connection is already open.
// If it's non-nil, it will use the config string to open the DB connection first.
func InitDb(jsonconf string, reset bool) error {
func InitDb(useAdapter, jsonconf string, reset bool) error {
if !IsOpen() {
if err := openAdapter(1, jsonconf); err != nil {
if err := openAdapter(1, useAdapter, jsonconf); err != nil {
return err
}
}

View File

@ -15,14 +15,14 @@ import (
"github.com/tinode/chat/server/store/types"
)
func genDb(reset bool, dbsource string, data *Data) {
func genDb(reset bool, useAdapter, dbSource string, data *Data) {
var err error
defer store.Close()
log.Println("Initializing DB...")
err = store.InitDb(dbsource, reset)
err = store.InitDb(useAdapter, dbSource, reset)
if err != nil {
if strings.Contains(err.Error(), " already exists") {
log.Println("DB already exists, NOT reinitializing")

View File

@ -148,6 +148,7 @@ func getPassword(n int) string {
func main() {
var reset = flag.Bool("reset", false, "first delete the database if one exists")
var datafile = flag.String("data", "", "name of file with sample data")
var useAdapter = flag.String("store_use_adapter", "", "override default database adapter")
var conffile = flag.String("config", "./tinode.conf", "config of the database connection")
flag.Parse()
@ -173,5 +174,5 @@ func main() {
log.Fatal("Failed to parse config file:", err)
}
genDb(*reset, string(config.StoreConfig), &data)
genDb(*reset, *useAdapter, string(config.StoreConfig), &data)
}