forked from go-squads/saga-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxc_services.go
120 lines (104 loc) · 3.2 KB
/
lxc_services.go
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
log "github.com/sirupsen/logrus"
)
type lxcService struct {
ID string `db:"id" json:"id"`
Service string `db:"service" json:"service"`
LxcID string `db:"lxc_id" json:"lxc_id"`
LxcPort string `db:"lxc_port" json:"lxc_port"`
LxdID string `db:"lxd_id" json:"lxd_id"`
LxdPort string `db:"lxd_port" json:"lxd_port"`
LxcName string `db:"lxc_name" json:"lxc_name"`
Status string `db:"status" json:"status"`
LxdName string `db:"lxd_name" json:"lxd_name"`
LxdAddress string `db:"lxd_address" json:"lxd_address"`
}
func (l *lxcService) insertLxcService(db PostgresQL) error {
_, err := db.NamedExec(`INSERT INTO lxc_services (id, service, lxc_id, lxc_port, lxd_id, lxd_port, lxc_name, status, lxd_name, lxd_address) VALUES (:id, :service, :lxc_id, :lxc_port, :lxd_id, :lxd_port, :lxc_name, 'creating', :lxd_name, :lxd_address)`, l)
if err != nil {
return err
}
return nil
}
func (l *lxcService) checkIfLxcServiceExist(db PostgresQL) bool {
rows, err := db.Queryx("SELECT id, service, lxc_id, lxc_port, lxd_id, lxd_port FROM lxc_services WHERE lxc_port=$1 AND lxd_port=$2", l.LxcPort, l.LxdPort)
if err != nil {
return false
}
defer rows.Close()
if rows.Next() {
return true
}
return false
}
func getLxcServicesListByLxdName(db PostgresQL, lxdName string) ([]lxcService, error) {
rows, err := db.Queryx("SELECT id, service, lxc_id, lxc_port, lxd_id, lxd_port, lxc_name, status, lxd_address FROM lxc_services WHERE lxd_name=$1", lxdName)
if err != nil {
return nil, err
}
defer rows.Close()
var lxcServiceList []lxcService
for rows.Next() {
l := lxcService{}
if err = rows.StructScan(&l); err != nil {
return nil, err
}
lxcServiceList = append(lxcServiceList, l)
}
return lxcServiceList, nil
}
func (l *lxcService) getLxcService(db PostgresQL) error {
rows, err := db.Queryx("SELECT id, service, lxc_id, lxc_port, lxd_id, lxd_port, lxc_name, status, lxd_address FROM lxc_services WHERE id=$1", l.ID)
if err != nil {
return err
}
defer rows.Close()
if rows.Next() {
err = rows.StructScan(&l)
if err != nil {
return err
}
}
return nil
}
func (l *lxcService) checkNeedUpdateLxcService(curLxc lxcService) bool {
if curLxc.Status == "created" {
if l.Status == "creating" {
return false
} else {
return true
}
}
return true
}
func (l *lxcService) updateStatusByID(db PostgresQL) error {
curLxc := lxcService{ID: l.ID}
if err := curLxc.getLxcService(db); err != nil {
return err
}
if l.checkNeedUpdateLxcService(curLxc) {
log.Info("Lxc status update needed")
_, err := db.Exec("UPDATE lxc_services SET status = $2 WHERE id = $1", l.ID, l.Status)
if err != nil {
return err
}
}
return nil
}
func (l *lxcService) getLxcServicesListByLxcID(db PostgresQL) ([]lxcService, error) {
rows, err := db.Queryx("SELECT id, service, lxc_id, lxc_port, lxd_id, lxd_port, lxc_name, status, lxd_name, lxd_address FROM lxc_services WHERE lxc_id=$1", l.LxcID)
if err != nil {
return nil, err
}
defer rows.Close()
var lxcServiceList []lxcService
for rows.Next() {
l := lxcService{}
if err = rows.StructScan(&l); err != nil {
return nil, err
}
lxcServiceList = append(lxcServiceList, l)
}
return lxcServiceList, nil
}