summaryrefslogtreecommitdiff
path: root/cmd/dryad/dryad.go
blob: 1e79d870a7c5c06c7e9e3c709f328441f92ae8b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 *  Copyright (c) 2017-2018 Samsung Electronics Co., Ltd 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 main

import (
	"flag"
	"log"
	"net"
	"net/rpc"
	"os"
	"os/signal"

	"git.tizen.org/tools/boruta/dryad"
	"git.tizen.org/tools/boruta/dryad/conf"
	dryad_rpc "git.tizen.org/tools/boruta/rpc/dryad"
	superviser_rpc "git.tizen.org/tools/boruta/rpc/superviser"
)

var (
	confPath      string
	configuration *conf.General
)

func init() {
	configuration = conf.NewConf()

	flag.StringVar(&confPath, "conf", "/etc/boruta/dryad.conf", "path to the configuration file")
}

func exitOnErr(ctx string, err error) {
	if err != nil {
		log.Fatal(ctx, err)
	}
}

func generateConfFile() {
	f, err := os.Create(confPath)
	exitOnErr("can't create configuration file:", err)
	defer f.Close()
	exitOnErr("can't generate new configuration:", configuration.Marshal(f))
}

func readConfFile() {
	f, err := os.Open(confPath)
	exitOnErr("can't open configuration file:", err)
	defer f.Close()
	exitOnErr("can't parse configuration:", configuration.Unmarshal(f))
}

func main() {
	flag.Parse()

	// Read configuration.
	_, err := os.Stat(confPath)
	if err != nil {
		if os.IsNotExist(err) {
			generateConfFile()
			log.Fatal("configuration file generated. Please edit it first")
		}
		log.Fatal("can't access file:", err)
	}
	readConfFile()

	rusalka := dryad.NewRusalka(configuration.User.Name, configuration.User.Groups)

	l, err := net.Listen("tcp", configuration.Address)
	exitOnErr("can't listen on port:", err)
	defer l.Close()

	srv := rpc.NewServer()
	err = dryad_rpc.RegisterDryadService(srv, rusalka)
	exitOnErr("can't start RPC service:", err)

	go srv.Accept(l)

	boruta, err := superviser_rpc.DialSuperviserClient(configuration.BorutaAddress)
	exitOnErr("failed to initialize connection to boruta:", err)
	defer boruta.Close()

	err = boruta.Register(configuration.Caps)
	exitOnErr("failed to register to boruta:", err)

	// Wait for interrupt.
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	<-c
}