Skip to content

Commit 7b01b68

Browse files
authored
Merge pull request #27 from Basavaraju-G/master
libvirtcli code changes to work with ocp-ci
2 parents 8aa08b1 + 38af9a2 commit 7b01b68

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed

boskos/janitor/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/Basavaraju-G/janitor
2+
3+
go 1.17
4+
5+
require github.com/libvirt/libvirt-go v7.4.0+incompatible

boskos/janitor/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/libvirt/libvirt-go v7.4.0+incompatible h1:crnSLkwPqCdXtg6jib/FxBG/hweAc/3Wxth1AehCXL4=
2+
github.com/libvirt/libvirt-go v7.4.0+incompatible/go.mod h1:34zsnB4iGeOv7Byj6qotuW8Ya4v4Tr43ttjz/F0wjLE=

boskos/janitor/libvirt.go

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"flag"
6+
"log"
7+
"strings"
8+
"github.com/libvirt/libvirt-go"
9+
)
10+
11+
func main() {
12+
var (
13+
arch, leasedResource string
14+
REMOTE_LIBVIRT_URI string
15+
REMOTE_LIBVIRT_HOSTNAME, REMOTE_LIBVIRT_HOSTNAME_1, REMOTE_LIBVIRT_HOSTNAME_2 string
16+
)
17+
// check lease name
18+
flag.StringVar(&leasedResource, "leasedResource", "", "enter leased resource name")
19+
flag.Parse()
20+
if len(leasedResource) == 0 {
21+
fmt.Println("usage: -leasedResource is empty")
22+
return
23+
}
24+
25+
LIBVIRT_HOSTS := make(map[string]string)
26+
if strings.Contains(leasedResource, "ppc64le"){
27+
arch = "ppc64le"
28+
} else {
29+
arch = "s390x"
30+
}
31+
32+
// Setting Hostnames
33+
if arch == "ppc64le" {
34+
REMOTE_LIBVIRT_HOSTNAME = "C155F2U33"
35+
REMOTE_LIBVIRT_HOSTNAME_1 = "C155F2U31"
36+
REMOTE_LIBVIRT_HOSTNAME_2 = "C155F2U35"
37+
LIBVIRT_HOSTS["libvirt-ppc64le-0-0"] = REMOTE_LIBVIRT_HOSTNAME
38+
LIBVIRT_HOSTS["libvirt-ppc64le-0-1"] = REMOTE_LIBVIRT_HOSTNAME
39+
LIBVIRT_HOSTS["libvirt-ppc64le-0-2"] = REMOTE_LIBVIRT_HOSTNAME
40+
LIBVIRT_HOSTS["libvirt-ppc64le-0-3"] = REMOTE_LIBVIRT_HOSTNAME
41+
LIBVIRT_HOSTS["libvirt-ppc64le-0-4"] = REMOTE_LIBVIRT_HOSTNAME
42+
LIBVIRT_HOSTS["libvirt-ppc64le-1-0"] = REMOTE_LIBVIRT_HOSTNAME_1
43+
LIBVIRT_HOSTS["libvirt-ppc64le-1-1"] = REMOTE_LIBVIRT_HOSTNAME_1
44+
LIBVIRT_HOSTS["libvirt-ppc64le-1-2"] = REMOTE_LIBVIRT_HOSTNAME_1
45+
LIBVIRT_HOSTS["libvirt-ppc64le-1-3"] = REMOTE_LIBVIRT_HOSTNAME_1
46+
LIBVIRT_HOSTS["libvirt-ppc64le-1-4"] = REMOTE_LIBVIRT_HOSTNAME_1
47+
LIBVIRT_HOSTS["libvirt-ppc64le-2-0"] = REMOTE_LIBVIRT_HOSTNAME_2
48+
LIBVIRT_HOSTS["libvirt-ppc64le-2-1"] = REMOTE_LIBVIRT_HOSTNAME_2
49+
LIBVIRT_HOSTS["libvirt-ppc64le-2-2"] = REMOTE_LIBVIRT_HOSTNAME_2
50+
LIBVIRT_HOSTS["libvirt-ppc64le-2-3"] = REMOTE_LIBVIRT_HOSTNAME_2
51+
LIBVIRT_HOSTS["libvirt-ppc64le-2-4"] = REMOTE_LIBVIRT_HOSTNAME_2
52+
53+
} else{
54+
REMOTE_LIBVIRT_HOSTNAME := "lnxocp01"
55+
REMOTE_LIBVIRT_HOSTNAME_1 := "lnxocp02"
56+
REMOTE_LIBVIRT_HOSTNAME_2 := "lnxocp06"
57+
LIBVIRT_HOSTS["libvirt-s390x-0-0"] = REMOTE_LIBVIRT_HOSTNAME
58+
LIBVIRT_HOSTS["libvirt-s390x-0-1"] = REMOTE_LIBVIRT_HOSTNAME
59+
LIBVIRT_HOSTS["libvirt-s390x-0-2"] = REMOTE_LIBVIRT_HOSTNAME
60+
LIBVIRT_HOSTS["libvirt-s390x-0-3"] = REMOTE_LIBVIRT_HOSTNAME
61+
LIBVIRT_HOSTS["libvirt-s390x-0-4"] = REMOTE_LIBVIRT_HOSTNAME
62+
LIBVIRT_HOSTS["libvirt-s390x-1-0"] = REMOTE_LIBVIRT_HOSTNAME_1
63+
LIBVIRT_HOSTS["libvirt-s390x-1-1"] = REMOTE_LIBVIRT_HOSTNAME_1
64+
LIBVIRT_HOSTS["libvirt-s390x-1-2"] = REMOTE_LIBVIRT_HOSTNAME_1
65+
LIBVIRT_HOSTS["libvirt-s390x-1-3"] = REMOTE_LIBVIRT_HOSTNAME_1
66+
LIBVIRT_HOSTS["libvirt-s390x-1-4"] = REMOTE_LIBVIRT_HOSTNAME_1
67+
LIBVIRT_HOSTS["libvirt-s390x-2-0"] = REMOTE_LIBVIRT_HOSTNAME_2
68+
LIBVIRT_HOSTS["libvirt-s390x-2-1"] = REMOTE_LIBVIRT_HOSTNAME_2
69+
LIBVIRT_HOSTS["libvirt-s390x-2-2"] = REMOTE_LIBVIRT_HOSTNAME_2
70+
LIBVIRT_HOSTS["libvirt-s390x-2-3"] = REMOTE_LIBVIRT_HOSTNAME_2
71+
LIBVIRT_HOSTS["libvirt-s390x-2-4"] = REMOTE_LIBVIRT_HOSTNAME_2
72+
73+
}
74+
75+
// get cluster libvirt uri or default it the first host
76+
remoteLibvirt := LIBVIRT_HOSTS[leasedResource]
77+
if len(remoteLibvirt) > 0 {
78+
REMOTE_LIBVIRT_URI = fmt.Sprintf("qemu+tcp://%s/system", remoteLibvirt)
79+
}else
80+
{
81+
REMOTE_LIBVIRT_URI = fmt.Sprintf("qemu+tcp://%s/system", REMOTE_LIBVIRT_HOSTNAME)
82+
}
83+
fmt.Println("Remote Host Name: ", REMOTE_LIBVIRT_URI)
84+
// default to first host
85+
defer func() {
86+
if err := recover(); err != nil {
87+
log.Println("Failed to connect to remote libvirt:", REMOTE_LIBVIRT_URI, err)
88+
}
89+
}()
90+
conn, err := libvirt.NewConnect(REMOTE_LIBVIRT_URI)
91+
if err != nil {
92+
fmt.Println("Failed to connect to remote libvirt: ", err)
93+
}
94+
defer conn.Close()
95+
96+
domains, err := conn.ListAllDomains(libvirt.CONNECT_LIST_DOMAINS_ACTIVE)
97+
if err != nil {
98+
fmt.Println("Failed to List Active Domains: ",err)
99+
}
100+
for _, domain := range domains {
101+
vmName, err := domain.GetName()
102+
if err == nil && strings.HasPrefix(vmName, leasedResource) {
103+
if ok,_ := domain.IsActive(); ok {
104+
fmt.Println("Domain is active, Destroying it.. :", vmName)
105+
_ = domain.Destroy()
106+
}
107+
fmt.Println("Started Undefineing vm: ", vmName)
108+
err = domain.Undefine()
109+
if err != nil{
110+
fmt.Println("Faield delete vm: %s", vmName)
111+
}
112+
}
113+
}
114+
115+
116+
storagePools, err := conn.ListAllStoragePools(libvirt.CONNECT_LIST_STORAGE_POOLS_ACTIVE)
117+
if err != nil {
118+
fmt.Println("Failed to List storage pools:",err)
119+
}
120+
for _, pool := range storagePools {
121+
defer pool.Free()
122+
poolName, err := pool.GetName()
123+
if err == nil && strings.Contains(poolName, leasedResource) {
124+
125+
pool, err := conn.LookupStoragePoolByName(poolName)
126+
if err != nil {
127+
fmt.Println("Failed get storage pool:", poolName, err)
128+
}
129+
130+
// delete all vols that return true from filter.
131+
vols, err := pool.ListAllStorageVolumes(0)
132+
if err != nil {
133+
fmt.Println("Failed to list volumes in: ", poolName, err)
134+
}
135+
136+
for _, vol := range vols {
137+
defer vol.Free()
138+
vName, err := vol.GetName()
139+
if err != nil {
140+
fmt.Println("failed get volume names in: ", poolName, err)
141+
}
142+
if err := vol.Delete(0); err != nil {
143+
fmt.Println("Failed delete volume %q from %q", vName, poolName)
144+
} else{
145+
fmt.Printf("delete volume %v from pool %v\n", vName, poolName)
146+
}
147+
}
148+
149+
if ok,_ := pool.IsActive(); ok {
150+
fmt.Println("Pool is active, Destroying it.. :", poolName)
151+
_ = pool.Destroy()
152+
}
153+
fmt.Println("Start Deleting storage pool: ", poolName)
154+
err = pool.Delete(0)
155+
if err != nil{
156+
fmt.Println("Failed delete storage pool:", poolName, err)
157+
}else{
158+
fmt.Println("deleted storage pool ", poolName)
159+
}
160+
fmt.Println("Undefine storage pool: ", poolName)
161+
err = pool.Undefine()
162+
if err != nil {
163+
fmt.Println("Failed to Undefine storage Pool:", poolName, err)
164+
}
165+
}
166+
}
167+
168+
networks, err := conn.ListAllNetworks(libvirt.CONNECT_LIST_NETWORKS_ACTIVE)
169+
if err != nil {
170+
fmt.Println("Failed to List All Networks: ",err)
171+
}
172+
for _, network := range networks {
173+
networkName, err := network.GetName()
174+
if err == nil && strings.Contains(networkName, leasedResource) {
175+
if ok,_ := network.IsActive(); ok {
176+
fmt.Println("network is active, Destroying it.. :", networkName)
177+
_ = network.Destroy()
178+
}
179+
fmt.Println("Undefine network: ", networkName)
180+
err = network.Undefine()
181+
if err != nil {
182+
fmt.Println("Failed to Undefine network: ", networkName, err)
183+
}
184+
}
185+
}
186+
187+
return
188+
}

0 commit comments

Comments
 (0)