forked from go-squads/saga-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxc.go
137 lines (121 loc) · 3.17 KB
/
lxc.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
log "github.com/sirupsen/logrus"
)
type lxc struct {
ID string `db:"id" json:"id"`
LxdID string `db:"lxd_id" json:"lxd_id"`
Name string `db:"name" json:"name"`
Type string `db:"type" json:"type"`
Alias string `db:"alias" json:"alias"`
Protocol string `db:"protocol" json:"protocol"`
Server string `db:"server" json:"server"`
Address string `db:"address" json:"address"`
Status string `db:"status" json:"status"`
Description string `db:"description" json:"description"`
WeightScore string `json:"weight"`
WeightValue float64 `json:"weightValue"`
}
func (l *lxc) getLxc(db PostgresQL) error {
rows, err := db.Queryx("SELECT id, lxd_id, name, type, alias, address, description, status FROM lxc WHERE id=$1 LIMIT 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 *lxc) checkNeedUpdate(curLxc lxc) bool {
if curLxc.Status == "started" {
if l.Status == "starting" {
return false
} else {
return true
}
} else if curLxc.Status == "stopped" {
if l.Status == "stopping" {
return false
} else {
return true
}
}
return true
}
func (l *lxc) updateStatusByID(db PostgresQL) error {
curLxc := lxc{ID: l.ID}
if err := curLxc.getLxc(db); err != nil {
return err
}
if l.checkNeedUpdate(curLxc) {
log.Info("Lxc status update needed")
_, err := db.Exec("UPDATE lxc SET status = $2 WHERE id = $1", l.ID, l.Status)
if err != nil {
return err
}
}
return nil
}
func (l *lxc) insertLxc(db PostgresQL) error {
_, err := db.NamedExec("INSERT INTO lxc (id, lxd_id, name, type, alias, protocol, server, address, description, status) VALUES (:id, :lxd_id, :name, :type, :alias, :protocol, :server, :address, :description, :status)", l)
if err != nil {
return err
}
return nil
}
func (l *lxc) deleteLxc(db PostgresQL) error {
_, err := db.Queryx("DELETE FROM lxc WHERE id = $1", l.ID)
if err != nil {
return err
}
return nil
}
func (l *lxc) getLxcListByLxdID(db PostgresQL, lxdID string) ([]lxc, error) {
rows, err := db.Queryx("SELECT id, lxd_id, name, type, alias, protocol, server, address, description, status FROM lxc WHERE lxd_id=$1", lxdID)
if err != nil {
return nil, err
}
defer rows.Close()
lxcList := []lxc{}
for rows.Next() {
l := lxc{}
err = rows.StructScan(&l)
if err != nil {
return nil, err
}
lxcList = append(lxcList, l)
}
return lxcList, nil
}
func (l *lxc) checkIfLxcExist(db PostgresQL) bool {
rows, err := db.Queryx("SELECT id, lxd_id, name, type, alias, protocol, server, address, description, status FROM lxc WHERE name=$1", l.Name)
if err != nil {
return false
}
defer rows.Close()
if rows.Next() {
return true
}
return false
}
func (l *lxc) getLxcNameByID(db PostgresQL) string {
rows, err := db.Queryx("SELECT name FROM lxc WHERE id=$1", l.ID)
if err != nil {
log.Error(err.Error())
return ""
}
defer rows.Close()
lxcData := lxc{}
if rows.Next() {
if err = rows.StructScan(&lxcData); err != nil {
log.Error(err.Error())
return ""
}
}
log.Infof("lxc name: %s", lxcData.Name)
return lxcData.Name
}