Skip to content

Commit

Permalink
fix: fix windows installer service and missing flags
Browse files Browse the repository at this point in the history
Signed-off-by: Michele Meloni <cleaversdev@gmail.com>
  • Loading branch information
mmeloni committed May 14, 2021
1 parent 545da7c commit 3fb881f
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 216 deletions.
56 changes: 4 additions & 52 deletions cmd/immudb/command/init_unix.go → cmd/immudb/command/init.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build linux darwin freebsd

/*
Copyright 2021 CodeNotary, Inc. All rights reserved.
Expand All @@ -25,56 +23,6 @@ import (
"github.com/spf13/viper"
)

func parseOptions() (options *server.Options, err error) {
dir := viper.GetString("dir")
port := viper.GetInt("port")
address := viper.GetString("address")
pidfile := viper.GetString("pidfile")
logfile := viper.GetString("logfile")
mtls := viper.GetBool("mtls")
auth := viper.GetBool("auth")
maxRecvMsgSize := viper.GetInt("max-recv-msg-size")
noHistograms := viper.GetBool("no-histograms")
detached := viper.GetBool("detached")
certificate := viper.GetString("certificate")
pkey := viper.GetString("pkey")
clientcas := viper.GetString("clientcas")
devMode := viper.GetBool("devmode")
adminPassword := viper.GetString("admin-password")
maintenance := viper.GetBool("maintenance")
signingKey := viper.GetString("signingKey")
synced := viper.GetBool("synced")
tokenExpTime := viper.GetInt("token-expiry-time")

storeOpts := server.DefaultStoreOptions().WithSynced(synced)

tlsConfig, err := setUpTLS(pkey, certificate, clientcas, mtls)
if err != nil {
return options, err
}

options = server.
DefaultOptions().
WithDir(dir).
WithPort(port).
WithAddress(address).
WithPidfile(pidfile).
WithLogfile(logfile).
WithTLS(tlsConfig).
WithAuth(auth).
WithMaxRecvMsgSize(maxRecvMsgSize).
WithNoHistograms(noHistograms).
WithDetached(detached).
WithDevMode(devMode).
WithAdminPassword(adminPassword).
WithMaintenance(maintenance).
WithSigningKey(signingKey).
WithStoreOptions(storeOpts).
WithTokenExpiryTime(tokenExpTime)

return options, nil
}

func (cl *Commandline) setupFlags(cmd *cobra.Command, options *server.Options) {
cmd.Flags().String("dir", options.Dir, "data folder")
cmd.Flags().IntP("port", "p", options.Port, "port number")
Expand All @@ -96,6 +44,8 @@ func (cl *Commandline) setupFlags(cmd *cobra.Command, options *server.Options) {
cmd.Flags().String("signingKey", options.SigningKey, "signature private key path. If a valid one is provided, it enables the cryptographic signature of the root. E.g. \"./../test/signer/ec3.key\"")
cmd.Flags().Bool("synced", false, "synced mode prevents data lost under unexpected crashes but affects performance")
cmd.Flags().Int("token-expiry-time", options.TokenExpiryTimeMin, "client authentication token expiration time. Minutes")
cmd.Flags().Bool("web-server", options.WebServer, "enable or disable web/console server")
cmd.Flags().Int("web-server-port", options.WebServerPort, "web/console server port")
}

func setupDefaults(options *server.Options) {
Expand All @@ -117,4 +67,6 @@ func setupDefaults(options *server.Options) {
viper.SetDefault("maintenance", options.GetMaintenance())
viper.SetDefault("synced", false)
viper.SetDefault("token-expiry-time", options.TokenExpiryTimeMin)
viper.SetDefault("web-server", options.WebServer)
viper.SetDefault("web-server-port", options.WebServerPort)
}
148 changes: 0 additions & 148 deletions cmd/immudb/command/init_windows.go

This file was deleted.

80 changes: 80 additions & 0 deletions cmd/immudb/command/parse_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2021 CodeNotary, 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.
*/

package immudb

import (
"github.com/codenotary/immudb/pkg/server"
"github.com/spf13/viper"
)

func parseOptions() (options *server.Options, err error) {
dir := viper.GetString("dir")
port := viper.GetInt("port")
address := viper.GetString("address")

pidfile := viper.GetString("pidfile")
logfile := viper.GetString("logfile")

mtls := viper.GetBool("mtls")
auth := viper.GetBool("auth")
maxRecvMsgSize := viper.GetInt("max-recv-msg-size")
noHistograms := viper.GetBool("no-histograms")
detached := viper.GetBool("detached")
certificate := viper.GetString("certificate")
pkey := viper.GetString("pkey")
clientcas := viper.GetString("clientcas")

devMode := viper.GetBool("devmode")
adminPassword := viper.GetString("admin-password")
maintenance := viper.GetBool("maintenance")
signingKey := viper.GetString("signingKey")
synced := viper.GetBool("synced")
tokenExpTime := viper.GetInt("token-expiry-time")

webServer := viper.GetBool("web-server")
webServerPort := viper.GetInt("web-server-port")

storeOpts := server.DefaultStoreOptions().WithSynced(synced)

tlsConfig, err := setUpTLS(pkey, certificate, clientcas, mtls)
if err != nil {
return options, err
}

options = server.
DefaultOptions().
WithDir(dir).
WithPort(port).
WithAddress(address).
WithPidfile(pidfile).
WithLogfile(logfile).
WithTLS(tlsConfig).
WithAuth(auth).
WithMaxRecvMsgSize(maxRecvMsgSize).
WithNoHistograms(noHistograms).
WithDetached(detached).
WithDevMode(devMode).
WithAdminPassword(adminPassword).
WithMaintenance(maintenance).
WithSigningKey(signingKey).
WithStoreOptions(storeOpts).
WithTokenExpiryTime(tokenExpTime).
WithWebServer(webServer).
WithWebServerPort(webServerPort)

return options, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.

package config

var ConfigImmudb = []byte(`dir = "%programdata%\\Immudb"
var ConfigImmudb = []byte(`dir = "%programdata%\\Immudb\\data"
network = "tcp"
address = "0.0.0.0"
port = 3322
Expand Down
23 changes: 10 additions & 13 deletions cmd/sservice/sservice_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,9 @@ func (ss *sservice) UninstallSetup(serviceName string) (err error) {
if err = ss.RemoveProgramFiles(serviceName); err != nil {
return err
}
// remove ProgramData folder only if it is empty
var cepd string
if cepd, err = helper.ResolvePath(ss.v.GetString("dir"), false); err != nil {
return err
}
// get immudb folder
cepd := strings.Replace(ss.v.GetString("dir"), "data", "", 1)
// remove immudb folder folder only if it is empty
if _, err := os.Stat(cepd); !os.IsNotExist(err) {
f1, err := ss.os.Open(cepd)
if err != nil {
Expand All @@ -147,9 +145,7 @@ func (ss *sservice) EraseData(serviceName string) (err error) {
return err
}
var path string
if path, err = helper.ResolvePath(filepath.FromSlash(ss.v.GetString("dir")), false); err != nil {
return err
}
path = filepath.FromSlash(ss.v.GetString("dir"))
if err := ss.osRemoveAll(path); err != nil {
return err
}
Expand All @@ -169,7 +165,8 @@ func (ss *sservice) GetDefaultConfigPath(serviceName string) (dataDir string, er
return "", err
}
dataDir = strings.Replace(dataDir, "%programdata%", pd, -1)
return filepath.Join(strings.Title(dataDir), "config", serviceName+".toml"), err
configDir := strings.Replace(dataDir, "data", "", 1)
return filepath.Join(strings.Title(configDir), "config", serviceName+".toml"), err
}

func (ss *sservice) ReadConfig(serviceName string) (err error) {
Expand Down Expand Up @@ -229,16 +226,16 @@ func (ss *sservice) CopyExecInOsDefault(serviceName string) (path string, err er
return path, err
}

// RemoveProgramFiles remove all program files
// RemoveProgramFiles remove only config folder
func (ss *sservice) RemoveProgramFiles(serviceName string) (err error) {
var path string
if err = ss.ReadConfig(serviceName); err != nil {
return err
}
if path, err = helper.ResolvePath(filepath.Join(filepath.FromSlash(ss.v.GetString("dir")), "config"), false); err != nil {
configPath, err := ss.GetDefaultConfigPath(serviceName)
if err != nil {
return err
}
return ss.osRemoveAll(path)
return ss.osRemoveAll(filepath.Dir(configPath))
}

func (ss sservice) UninstallExecutables(serviceName string) (err error) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/sservice/sservice_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
daem "github.com/takama/daemon"
)

func TestSservice_NewDaemon(t *testing.T) {
ss := NewSService()
func TestSserviceWin_NewDaemon(t *testing.T) {
ss := NewSService(&Option{})
d, err := ss.NewDaemon("test", "", "")
assert.Nil(t, err)
dc, _ := daem.New("test", "", "")
Expand Down

0 comments on commit 3fb881f

Please sign in to comment.