/
inst_status.go
160 lines (152 loc) · 4.29 KB
/
inst_status.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Copyright <holder> All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package grpcs
import (
"context"
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"github.com/IBM/cloudland/web/clui/model"
"github.com/IBM/cloudland/web/sca/dbs"
"github.com/jinzhu/gorm"
)
func init() {
Add("inst_status", InstanceStatus)
}
type SecurityData struct {
Secgroup int64
RemoteIp string `json:"remote_ip"`
RemoteGroup string `json:"remote_group"`
Direction string `json:"direction"`
IpVersion string `json:"ip_version"`
Protocol string `json:"protocol"`
PortMin int32 `json:"port_min"`
PortMax int32 `json:"port_max"`
}
func InstanceStatus(ctx context.Context, job *model.Job, args []string) (status string, err error) {
//|:-COMMAND-:| launch_vm.sh '3' '5 running 7 running 9 shut_off'
db := dbs.DB()
argn := len(args)
if argn < 2 {
err = fmt.Errorf("Wrong params")
log.Println("Invalid args", err)
return
}
hyperID, err := strconv.Atoi(args[1])
if err != nil {
log.Println("Invalid hypervisor ID", err)
return
}
hyper := &model.Hyper{Hostid: int32(hyperID)}
err = db.Where(hyper).Take(hyper).Error
if err != nil {
log.Println("Failed to query hyper", err)
return
}
statusList := strings.Split(args[2], " ")
for i := 0; i < len(statusList); i += 2 {
instID, err := strconv.Atoi(statusList[i])
if err != nil {
log.Println("Invalid instance ID", err)
continue
}
status := statusList[i+1]
instance := &model.Instance{Model: model.Model{ID: int64(instID)}}
err = db.Unscoped().Take(instance).Error
if err != nil {
log.Println("Invalid instance ID", err)
if gorm.IsRecordNotFoundError(err) {
instance.Hostname = "unknown"
instance.Status = status
instance.Hyper = int32(hyperID)
err = db.Create(instance).Error
if err != nil {
log.Println("Failed to create unknown instance", err)
}
}
continue
}
if instance.Status == "migrating" && instance.Hyper == int32(hyperID) {
continue
}
if instance.Status != status {
err = db.Unscoped().Model(instance).Update(map[string]interface{}{
"status": status,
}).Error
if err != nil {
log.Println("Failed to update status", err)
}
}
if instance.Hyper != int32(hyperID) {
instance.Hyper = int32(hyperID)
err = db.Unscoped().Model(instance).Update(map[string]interface{}{
"hyper": int32(hyperID),
}).Error
if err != nil {
log.Println("Failed to hypervisor", err)
}
err = db.Unscoped().Model(&model.Interface{}).Where("instance = ?", instance.ID).Update(map[string]interface{}{
"hyper": int32(hyperID),
"zone_id": hyper.ZoneID,
}).Error
if err != nil {
log.Println("Failed to update interface", err)
continue
}
err = ApplySecgroups(ctx, instance)
if err != nil {
log.Println("Failed to apply security groups", err)
continue
}
}
}
return
}
func ApplySecgroups(ctx context.Context, instance *model.Instance) (err error) {
db := dbs.DB()
var ifaces []*model.Interface
if err = db.Set("gorm:auto_preload", true).Where("instance = ?", instance.ID).Find(&ifaces).Error; err != nil {
log.Println("Interfaces query failed", err)
return
}
for _, iface := range ifaces {
var secRules []*model.SecurityRule
secRules, err = model.GetSecurityRules(iface.Secgroups)
if err != nil {
log.Println("Failed to get security rules", err)
continue
}
securityData := []*SecurityData{}
for _, rule := range secRules {
sgr := &SecurityData{
Secgroup: rule.Secgroup,
RemoteIp: rule.RemoteIp,
RemoteGroup: rule.RemoteGroup,
Direction: rule.Direction,
IpVersion: rule.IpVersion,
Protocol: rule.Protocol,
PortMin: rule.PortMin,
PortMax: rule.PortMax,
}
securityData = append(securityData, sgr)
}
var jsonData []byte
jsonData, err = json.Marshal(securityData)
if err != nil {
log.Println("Failed to marshal security json data, %v", err)
continue
}
control := fmt.Sprintf("inter=%d", instance.Hyper)
command := fmt.Sprintf("/opt/cloudland/scripts/backend/attach_nic.sh '%d' '%d' '%s' '%s' <<EOF\n%s\nEOF", instance.ID, iface.Address.Subnet.Vlan, iface.Address.Address, iface.MacAddr, jsonData)
err = HyperExecute(ctx, control, command)
if err != nil {
log.Println("Launch vm command execution failed", err)
continue
}
}
return
}