Commit 96ac6b32 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Implement deregister

parent 574a3ba4
......@@ -8,7 +8,6 @@ import (
"log"
"net/http"
"os"
"regexp"
"time"
"gopkg.in/alecthomas/kingpin.v2"
......@@ -73,7 +72,10 @@ var (
).Version(
"0.4.0",
)
configFile = app.Arg("config", "Config file to file to use.").Required().String()
cmdStart = app.Command("start", "Starts the client in its normal operation mode.").Default()
cmdDeregister = app.Command("deregister", "Before disabling the client: Use deregister to inform the backend that the client ceases operation.")
configFile = app.Flag("config", "Config file to file to use.").Short('c').Required().String()
scriptDebugging = app.Flag("debug-scripts", "Display debugging info concerning executed scripts").Bool()
backendDebugging = app.Flag("debug-backend", "Display debugging info concerning the backend").Bool()
debugAll = app.Flag("debug", "Display all debugging info").Bool()
......@@ -173,35 +175,31 @@ func getConfig(configFile string) (c config, err error) {
if c.Password == "" {
log.Fatalf("[Conf] No 'password' in config")
}
if c.FetchIntervalString == "" {
c.FetchInterval = defaultFetchInterval
log.Printf("[Conf] Using default fetch_interval of %v", c.FetchInterval)
} else {
c.FetchInterval, err = time.ParseDuration(c.FetchIntervalString)
if err != nil {
log.Printf("[Conf] Error parsing fetch interval: %s", err)
err = nil
var parseError error
c.FetchInterval, parseError = time.ParseDuration(c.FetchIntervalString)
if parseError != nil {
log.Printf("[Conf] Error parsing 'fetch_interval': %s", parseError)
c.FetchInterval = defaultFetchInterval
log.Printf("[Conf] Using default 'fetch_interval' of %v", c.FetchInterval)
}
c.ReconnectTimeout, parseError = time.ParseDuration(c.ReconnectTimeoutString)
if parseError != nil {
log.Printf("[Conf] Error parsing 'reconnect_timeout': %s", parseError)
c.ReconnectTimeout = defaultReconnectTimeout
log.Printf("[Conf] Using default 'reconnect_timeout' of %v", c.ReconnectTimeout)
}
// fetch the remote configuration
err = c.syncConfig()
if err != nil {
log.Printf("[Conf] Error fetching remote config: %s", err)
return
log.Printf("[Conf] Using default fetch_interval of %v", c.FetchInterval)
}
}
// strip the group authority from entitlement names
nameExtractor := regexp.MustCompile("^(.*?)#")
for entName, entServices := range c.EntitlementToServices {
match := nameExtractor.FindStringSubmatch(entName)
if len(match) == 2 {
delete(c.EntitlementToServices, entName)
c.EntitlementToServices[match[1]] = entServices
if c.ReconnectTimeoutString == "" {
c.ReconnectTimeout = defaultReconnectTimeout
log.Printf("[Conf] Using default reconnect_timeout of %v", c.ReconnectTimeout)
} else {
c.ReconnectTimeout, err = time.ParseDuration(c.ReconnectTimeoutString)
if err != nil {
log.Printf("[Conf] Error parsing reconnect timeout: %s", err)
err = nil
c.ReconnectTimeout = defaultReconnectTimeout
log.Printf("[Conf] Using default reconnect_timeout of %v", c.ReconnectTimeout)
}
}
......@@ -215,32 +213,37 @@ func getConfig(configFile string) (c config, err error) {
return
}
func main() {
var err error
// get arguments
kingpin.MustParse(app.Parse(os.Args[1:]))
func (c *config) deregister() {
var (
err error
req *http.Request
resp *http.Response
)
log.Printf("Deregistering this client at the backend")
if *debugAll {
*scriptDebugging = true
*backendDebugging = true
}
if *scriptDebugging {
log.Printf("[Debug] script debugging enabled")
}
if *backendDebugging {
log.Printf("[Debug] backend debugging enabled")
}
if *sequentialExecution {
log.Printf("[Debug] Executing tasks sequentially")
req, err = http.NewRequest(
"PUT",
"https://"+c.Host+"/backend/clientapi/deregister",
nil,
)
if err != nil {
log.Fatalf("Unable to deregister: %s", err)
}
req.SetBasicAuth(c.Username, c.Password)
// read the config file
c, err := getConfig(*configFile)
resp, err = client.Do(req)
if err != nil {
log.Fatalf("[Exit] No valid config. Exiting")
log.Fatalf("Unable to deregister: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Cannot read response body")
}
log.Printf("Response: %s", body)
}
func (c *config) start() {
if len(c.EntitlementToServices) == 0 && len(c.GroupToServices) == 0 {
log.Printf("[P] Not starting pubsub because the are no services to subscribe to")
return
......@@ -263,3 +266,44 @@ func main() {
forever := make(chan bool)
<-forever
}
func main() {
var err error
// get arguments
cmd := kingpin.MustParse(app.Parse(os.Args[1:]))
if *debugAll {
*scriptDebugging = true
*backendDebugging = true
}
if *scriptDebugging {
log.Printf("[Debug] script debugging enabled")
}
if *backendDebugging {
log.Printf("[Debug] backend debugging enabled")
}
if *sequentialExecution {
log.Printf("[Debug] Executing tasks sequentially")
}
// read the config file
c, err := getConfig(*configFile)
if err != nil {
log.Fatalf("[Exit] No valid config. Exiting")
}
switch cmd {
case "start":
// fetch the remote configuration
err = c.syncConfig()
if err != nil {
log.Printf("[Conf] Error fetching remote config: %s", err)
return
}
c.start()
case "deregister":
c.deregister()
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment