src, docker: Change pkg import path of DMA
[barometer.git] / src / dma / cmd / infofetch / daemon.go
1 /*
2  * Copyright 2018 NEC Corporation
3  *
4  *   Licensed under the Apache License, Version 2.0 (the "License");
5  *   you may not use this file except in compliance with the License.
6  *   You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *   Unless required by applicable law or agreed to in writing, software
11  *   distributed under the License is distributed on an "AS IS" BASIS,
12  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *   See the License for the specific language governing permissions and
14  *   limitations under the License.
15  */
16
17 package main
18
19 import (
20         "context"
21         "github.com/BurntSushi/toml"
22         "github.com/go-redis/redis"
23         libvirt "github.com/libvirt/libvirt-go"
24         "github.com/opnfv/barometer/src/dma/pkg/common"
25         "log"
26         "sync"
27 )
28
29 var infoPool common.RedisPool
30
31 // Config is ...
32 type Config struct {
33         Common    CommonConfig
34         InfoFetch InfoFetchConfig
35 }
36
37 // CommonConfig is ...
38 type CommonConfig struct {
39         RedisHost     string `toml:"redis_host"`
40         RedisPort     string `toml:"redis_port"`
41         RedisPassword string `toml:"redis_password"`
42         RedisDB       int    `toml:"redis_db"`
43 }
44
45 // InfoFetchConfig is ...
46 type InfoFetchConfig struct {
47         OSUsername          string `toml:"os_username"`
48         OSUserDomainName    string `toml:"os_user_domain_name"`
49         OSProjectDomainName string `toml:"os_project_domain_name"`
50         OSProjectName       string `toml:"os_project_name"`
51         OSPassword          string `toml:"os_password"`
52         OSAuthURL           string `toml:"os_auth_url"`
53 }
54
55 func main() {
56
57         var config Config
58         _, err := toml.DecodeFile("/etc/barometer-dma/config.toml", &config)
59         if err != nil {
60                 log.Println("Read error of config file")
61         }
62
63         var waitgroup sync.WaitGroup
64         libvirt.EventRegisterDefaultImpl()
65
66         redisClient := redis.NewClient(&redis.Options{
67                 Addr:     config.Common.RedisHost + ":" + config.Common.RedisPort,
68                 Password: config.Common.RedisPassword,
69                 DB:       config.Common.RedisDB,
70         })
71         infoPool = common.RedisPool{Client: redisClient}
72         // Initialize redis db...
73         infoPool.DelAll()
74
75         conn, err := libvirt.NewConnect("qemu:///system")
76         if err != nil {
77                 log.Fatalln("libvirt connect error")
78         }
79         defer conn.Close()
80
81         vmIfInfoChan := make(chan string)
82         {
83                 ctx := context.Background()
84                 waitgroup.Add(1)
85                 go func() {
86                         RunNeutronInfoFetch(ctx, &config, vmIfInfoChan)
87                         waitgroup.Done()
88                 }()
89         }
90
91         //Get active VM info
92         GetActiveDomain(conn, vmIfInfoChan)
93         {
94                 ctx := context.Background()
95                 waitgroup.Add(1)
96                 go func() {
97                         RunVirshEventLoop(ctx, conn, vmIfInfoChan)
98                         waitgroup.Done()
99                 }()
100         }
101
102         waitgroup.Wait()
103 }