Skip to content

Commit c0987a5

Browse files
committed
update, add kvm interface
1 parent 6f604f4 commit c0987a5

38 files changed

+1074
-129
lines changed

README.md

+25-4
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,28 @@
2828
start the log server in case of no permission to the directory /var/log
2929

3030
sudo nohup python ~/dev_virt/lib/Log/logging_server.py &
31+
## Xenserver API Architecture
32+
![highlevel](XenserverApi-Architecture.png)
3133

3234
## Some phrase:
3335
- PIF: physical interface, the eth on server
3436
- VIF: virtual interface, the eth on VM
3537
- SR: storage repository, the storage pool on server
3638

39+
# Create KVM VMS
40+
1. Auto schedual the target vm to a available server, this is only available on dev env
41+
42+
> lain3_node.py --role=rolename --cluster=<test|xyz|kvm>
43+
44+
2. manually given the target vm-name, host and vm-ip. Product env should use this command
45+
46+
> lain3_node.py --role=rolename --name=new_vm_name --host=hostip --ip=vm_ip
3747
# Create multiple VMs from xml config
3848
- `setup_vms.py --validate xmlFile` Validate the given xml file
3949
- `setup_vms.py --create xmlFile` Do the create up according to the xml file
4050

4151
Create VMs with xml just need to write a xml config following the xml-example. Before really creating VMs, please run
42-
*setup_system.py --validate* to check your xml.
52+
*setup_vms.py --validate* to check your xml.
4353

4454
```xml
4555
<servers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="">
@@ -64,8 +74,13 @@ Create VMs with xml just need to write a xml config following the xml-example. B
6474

6575
<!--here you can add another server -->
6676
<SERVER serverIp="192.168.1.15" user="root" passwd="xxxxxx">
77+
<!--VM element: vmname, template is required, others are options -->
6778
<VM vmname="createViaXml2" cpucores="1" cpumax="2" minMemory="1" memory="2" maxMemory="3" template="NewTemplate">
79+
<!--IP element: vifIndex and ip is required, device/network/bridge is options, if no device/network/bridge, will choose manage network as default -->
6880
<IP vifIndex="0" ip="192.168.1.241" device="eth1" />
81+
<IP vifIndex="1" ip="192.168.1.241" network="xenbr1" />
82+
<IP vifIndex="2" ip="192.168.1.242" bridge="xenbr1" />
83+
<!--DISK element: size is required, storage is options, is no storage, it will choose the storage which has largest free size -->
6984
<DISK size="2" />
7085
</VM>
7186
</SERVER>
@@ -107,10 +122,11 @@ Create VMs with xml just need to write a xml config following the xml-example. B
107122
**neither *--device* nor *--network*, the default manage network will be used**
108123
> create_vm.py -c "test2" -t "CentOS 7.2 template" --ip=192.168.1.100 --vif=0
109124
110-
##### 3). <b>**Create a new VM with given max cpu cores:**<b>
125+
##### 3). <b>**Create a new VM with given max cpu cores and current cpu cores:**<b>
111126
- `--cpu-max=MAX_CORES` Config the max VCPU cores.
127+
- `--cpu-cores=CPU_CORES` Config the number of startup VCPUs for the new created VM
112128

113-
> create_vm.py -c "test2" -t "CentOS 7.2 template" --cpu-max=2
129+
> create_vm.py -c "test2" -t "CentOS 7.2 template" --cpu-core=2 --cpu-max=4
114130
115131
   The max cpu cores can be configured when VM is power off only, and it affect the upper limit when set the cpu cores lively
116132

@@ -136,6 +152,11 @@ Create VMs with xml just need to write a xml config following the xml-example. B
136152
**if no *--storage*, will use the storage which has a largest free volume**
137153
> create_vm.py "test1"--add-disk=2
138154
155+
## 3. delete_vm.py
156+
To delete a vm, please use *delete_vm.py --vm=vm_to_delete*, this will ask user to input 'yes' or 'no' to confirm
157+
- `delete_vm.py --vm=vm_name [--host=ip --user=user --pwd=passwd]`
158+
159+
if `--force` is set, then the additional disk on vm will also be deleted, this feature is only support on kvm now.
139160

140161
## 4. config_vm.py
141162
#### The IP and memory configuration is same as that in create VM.
@@ -154,7 +175,7 @@ Create VMs with xml just need to write a xml config following the xml-example. B
154175
> config_vm.py "test1" --add-vif=1 --device=eth1 --ip=192.168.1.200 --netmask=255.255.255.0
155176
156177
##### 2). <b>**config a VM' cpu when it is running**<b>
157-
- `--cpu-cores=CPU_CORES` Config the VCPU cores lively
178+
- `--cpu-cores=CPU_CORES` Config the VCPU cores lively for a running VM or the number of startup VCPUs for a halted VM
158179

159180
> config_vm.py "test1" --cpu-core=4
160181

XenserverApi-Architecture.png

222 KB
Loading

app/cmdb/host_driver.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def respond_data(self):
7373
return json.loads(self.resp).get('data', {})
7474
elif isinstance(self.resp, dict):
7575
return self.resp.get('data', {})
76-
except ValueError, error:
76+
except ValueError as error:
7777
log.exception(error)
7878

7979
return {}

app/cmdb/hosts.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/usr/bin/env python
22
# -*- coding:UTF-8 -*-
33

4-
import requests
54
import json
6-
from lib.Log.log import log
5+
6+
import requests
7+
78
from app.cmdb.host_driver import HostDbDriver
89
from app.cmdb.settings import HOSTs_URL
10+
from lib.Log.log import log
911

1012

1113
class HostDriver(HostDbDriver):
@@ -91,7 +93,7 @@ def update(self, id=None, sn=None, hostname=None, data=None, json_data=None):
9193
if data:
9294
if not isinstance(data, dict):
9395
log.error("Data should be a dict.")
94-
return False
96+
return False
9597
elif json_data:
9698
if not isinstance(json_data, dict):
9799
log.error("Json data should be a dict")
@@ -111,12 +113,13 @@ def update(self, id=None, sn=None, hostname=None, data=None, json_data=None):
111113
if data:
112114
data['modified'] = str(modified)
113115
log.debug("Patch url:%s, data: %s", url, data.get('comment', data))
114-
self.resp = self.session.patch(url, data=data) # When dict value is None, pass in data will not set db null
116+
self.resp = self.session.patch(url, data=data) # When dict value is None, pass in data will not set db null
115117
elif json_data:
116118
json_data['modified'] = str(modified)
117119
json_data = json.dumps(json_data)
118120
log.debug("Patch url:%s, json data: %s", url, json_data)
119-
self.resp = self.session.patch(url, json=json_data) # when dict value is none, json value is null, set db null
121+
# when dict value is none, json value is null, pass in json=null will set db null
122+
self.resp = self.session.patch(url, json=json_data)
120123

121124
if self.resp.status_code == requests.codes.ok:
122125
log.info("Update to database successfully.")
@@ -163,7 +166,7 @@ def query(self, id=None, sn=None, hostname=None):
163166
log.debug(self.resp.content)
164167

165168
if not self.respond_data_count:
166-
log.warn("No records found with query data: %s.", data)
169+
log.debug("No records found with query data: %s.", data)
167170
return []
168171
else:
169172
return self.respond_data_list

etc/lain3Nodeconfig.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"master": {
3+
"cpu": 8,
4+
"memory": 16,
5+
"template": "k8s-template"
6+
},
7+
"ingress": {
8+
"cpu": 8,
9+
"memory": 16,
10+
"template": "k8s-template"
11+
},
12+
"node": {
13+
"cpu": 16,
14+
"memory": 48,
15+
"template": "k8s-template"
16+
},
17+
"etcd": {
18+
"cpu": 4,
19+
"memory": 8,
20+
"template": "k8s-template"
21+
},
22+
"jenkins": {
23+
"cpu": 8,
24+
"memory": 32,
25+
"template": "template",
26+
"disk_size": 100,
27+
"add_disk_num": 2
28+
},
29+
"normal": {
30+
"cpu": 4,
31+
"memory": 8,
32+
"template": "nfs-template"
33+
}
34+
}

initial_env.sh

100755100644
File mode changed.

lib/Log/log.py

100755100644
File mode changed.

lib/Log/log_test.py

100755100644
File mode changed.

lib/Log/logging.conf

100755100644
File mode changed.

lib/Log/logging_server.conf

100755100644
File mode changed.

lib/Log/logging_server.py

100755100644
File mode changed.

lib/Utils/constans.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
File Name: constans.py
5+
Author: longhui
6+
Created Time: 2019-04-24 11:07:34
7+
Description: lain3 node for k8s cluster default config
8+
"""
9+
template_dict = {
10+
11+
"master": {"cpu": 8, "memory": 16, "template": "k8s-template"},
12+
"ingress": {"cpu": 8, "memory": 16, "template": "k8s-template"},
13+
"node": {"cpu": 16, "memory": 32, "template": "k8s-template", "disk_size": 200, "add_disk_num": 1},
14+
"etcd": {"cpu": 4, "memory": 8, "template": "k8s-template", "disk_size": 100, "add_disk_num": 1},
15+
"jenkins": {"cpu": 8, "memory": 32, "template": "template", "disk_size": 100, "add_disk_num": 2},
16+
}
17+
18+
DISK_POOL = "kvm-disk-pool"
19+
NETFS_POOL_TYPE="netfs"
20+
DEFAULT_NETWORK = "libvirtmgr-net"
21+
MEMORY_OVERCOMMIT_FRACTION = 1.2
22+
Libvirtd_User = "admin"
23+
Libvirtd_Pass = "admin"

lib/Utils/network_utils.py

100755100644
+24-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Created Time: 2018-03-06 10:43:55
77
'''
88
from ipaddress import ip_address, ip_network, AddressValueError, NetmaskValueError
9+
910
from lib.Log.log import log
1011

1112

@@ -40,12 +41,16 @@ def _ipv4_address_check(cls, ipv4_address, prop_string):
4041
return True
4142

4243
@classmethod
43-
def is_valid_ipv4_parameter(cls, dest_ip, netmask, gateway=None):
44-
'''
45-
check the validation of the DCN related parameters.
44+
def is_valid_ipv4_parameter(cls, dest_ip, bridge_ip, netmask, gateway=None):
45+
"""
46+
check the dest ip belong to the same hosts generated by bridge ip and netmask.
4647
For the ipaddress APIs, please refer to https://docs.python.org/3/library/ipaddress.html
47-
original: isValidDcnIpParameters
48-
'''
48+
:param dest_ip: target ip
49+
:param bridge_ip: host ip
50+
:param netmask: netmask
51+
:param gateway:
52+
:return: true or false
53+
"""
4954

5055
# check the validation of the IP address
5156
try:
@@ -58,11 +63,15 @@ def is_valid_ipv4_parameter(cls, dest_ip, netmask, gateway=None):
5863

5964
# check the validation of the netmask
6065
try:
61-
ip_address_netmask = ip_network(unicode(dest_ip + "/" + netmask), strict=False)
66+
ip_address_netmask = ip_network(unicode(bridge_ip + "/" + netmask), strict=False)
6267
except (AddressValueError, NetmaskValueError, ValueError) as error:
6368
log.exception("Invalid IPv4 netmask %s: %s", netmask, str(error))
6469
return False
6570

71+
if ip_addr not in ip_address_netmask.hosts():
72+
log.error("The dest ip [%s] not belong to the target network: %s", dest_ip, ip_address_netmask)
73+
return False
74+
6675
if gateway is None:
6776
return True
6877

@@ -101,13 +110,15 @@ def is_IP_pingable(ip_address):
101110

102111

103112
if __name__ == "__main__":
104-
ip_addr_network = "92.0.2.0"
113+
ip_addr_network = "92.0.2.1"
105114
gateway = "92.0.2.1"
106115
dest_ip = "92.0.2.0"
107116
netmask = "255.255.255.0"
108-
print IpCheck.is_valid_ipv4_dcn_parameter(ip_addr_network, netmask, gateway)
109-
print IpCheck.is_valid_ipv4_dcn_parameter(dest_ip, netmask, gateway=None)
110-
#import pyping
111-
#pyping.ping(hostname, timeout, count, packet_size)
112-
print is_IP_pingable(gateway)
113-
print is_IP_pingable("localhost")
117+
print IpCheck.is_valid_ipv4_parameter(ip_addr_network, "92.0.2.3", netmask, gateway)
118+
print IpCheck.is_valid_ipv4_parameter(dest_ip, "92.0.3.3", netmask, gateway=None)
119+
# import pyping
120+
# pyping.ping(hostname, timeout, count, packet_size)
121+
ip_address_netmask = ip_network(unicode("192.168.1.3" + "/" + "24"), strict=False)
122+
print(ip_address_netmask, type(ip_address_netmask), str(ip_address_netmask))
123+
for ip in ip_address_netmask.hosts():
124+
print(ip, type(ip), str(ip))

0 commit comments

Comments
 (0)