Skip to content

Commit f2597a9

Browse files
committed
Use fakeDbDriver when no cmdb/database support
1 parent f901c02 commit f2597a9

File tree

3 files changed

+125
-61
lines changed

3 files changed

+125
-61
lines changed

README.md

+79-61
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# ReadME
22

3-
4-
## Env Setting Up:
3+
## Env Setting Up
54

65
1). create a virtual env with python2.7
76

@@ -20,6 +19,8 @@
2019

2120
export DB_HOST=127.0.0.1:8000
2221

22+
If you don't have a cmdb now, just let DB_HOST not set. It will do nothing with database.
23+
2324
5). setup the platform env: Xen for xenserver or KVM for KVM platform, the default is Xen
2425

2526
export PLATFORM=KVM
@@ -28,10 +29,13 @@
2829
start the log server in case of no permission to the directory /var/log
2930

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

34-
## Some phrase:
37+
## Some phrase
38+
3539
- PIF: physical interface, the eth on server
3640
- VIF: virtual interface, the eth on VM
3741
- SR: storage repository, the storage pool on server
@@ -55,17 +59,18 @@
5559
}
5660
```
5761

58-
## 1. Auto schedual the target vm to a available server
62+
## 1. Auto schedual the target vm to a available server
5963

6064
> schedule_node.py --role=rolename --cluster=<dev|test|prod>
61-
65+
6266
> Note: user can define the vm-name rule at function `generate_vmname_key(role, cluster)` in file `lib/Utils/schedule.py`
6367
64-
## 2. manually given the target vm-name, host and vm-ip.
68+
## 2. manually given the target vm-name, host and vm-ip
6569

6670
> schedule_node.py --role=rolename --name=new_vm_name --host=hostip --ip=vm_ip
6771
6872
# Create multiple VMs from xml config
73+
6974
- `setup_vms.py --validate xmlFile` Validate the given xml file
7075
- `setup_vms.py --create xmlFile` Do the create up according to the xml file
7176

@@ -108,32 +113,33 @@ Create VMs with xml just need to write a xml config following the xml-example. B
108113
</servers>
109114
```
110115

111-
112-
# Scripts:
116+
# Scripts
113117

114118
## 1. Sync information from exist server to database
115119

116-
- `sync_server_info.py` will sync both server and VM information to DB, and create a record in DB if not exist
117-
- `sync_server_info.py --update` Just update information to DB
120+
- `sync_server_info.py` will sync both server and VM information to DB, and create a record in DB if not exist
121+
- `sync_server_info.py --update` Just update information to DB
118122

119123
## 2. create_vm.py
120124

121-
- `--list-vm` List all the vms in server.
122-
- `--list-templ` List all the templates in the server.
123-
- `--list-network` List the bridge/switch network in the host
124-
- `--list-SR` List the storage repository infor in the host
125+
- `--list-vm` List all the vms in server.
126+
- `--list-templ` List all the templates in the server.
127+
- `--list-network` List the bridge/switch network in the host
128+
- `--list-SR` List the storage repository infor in the host
125129

126-
##### 2.1). <b>**Create a new VM with a template:**<b>
127-
- `-c VM_NAME, --create=VM_NAME` Create a new VM with a template.
128-
- `-t TEMPLATE, --templ=TEMPLATE` Template used to create a new VM.
130+
##### 2.1). <b>**Create a new VM with a template:**<b>
131+
132+
- `-c VM_NAME, --create=VM_NAME` Create a new VM with a template.
133+
- `-t TEMPLATE, --templ=TEMPLATE` Template used to create a new VM.
129134

130135
> create_vm.py -c "test_vm" -t "CentOS 7.2 Template"
131136
132-
##### 2.2). <b>**Create a new VM with a given IP: if a IP specified**<b>
133-
- `--vif=VIF_INDEX` The index of virtual interface on which configure will be
134-
- `--device=DEVICE` The target physic NIC name with an associated network VIF attach(ed) to
135-
- `--network=NETWORK` The target bridge/switch network which VIF connect(ed) to
136-
- `--ip=VIF_IP` The ip assigned to the virtual interface
137+
##### 2.2). <b>**Create a new VM with a given IP: if a IP specified**<b>
138+
139+
- `--vif=VIF_INDEX` The index of virtual interface on which configure will be
140+
- `--device=DEVICE` The target physic NIC name with an associated network VIF attach(ed) to
141+
- `--network=NETWORK` The target bridge/switch network which VIF connect(ed) to
142+
- `--ip=VIF_IP` The ip assigned to the virtual interface
137143

138144
**a VIF index(*--vif*) is needed and a PIF name(*--device*) or a bridge name(*--network*) is needed**
139145
> create_vm.py -c "test2" -t "CentOS 7.2 template" --ip=192.168.1.100 --vif=0 --device=eth0
@@ -143,98 +149,110 @@ Create VMs with xml just need to write a xml config following the xml-example. B
143149
**neither *--device* nor *--network*, the default manage network will be used**
144150
> create_vm.py -c "test2" -t "CentOS 7.2 template" --ip=192.168.1.100 --vif=0
145151
146-
##### 2.3). <b>**Create a new VM with given max cpu cores and current cpu cores:**<b>
147-
- `--cpu-max=MAX_CORES` Config the max VCPU cores.
148-
- `--cpu-cores=CPU_CORES` Config the number of startup VCPUs for the new created VM
152+
##### 2.3). <b>**Create a new VM with given max cpu cores and current cpu cores:**<b>
153+
154+
- `--cpu-max=MAX_CORES` Config the max VCPU cores.
155+
- `--cpu-cores=CPU_CORES` Config the number of startup VCPUs for the new created VM
149156

150157
> create_vm.py -c "test2" -t "CentOS 7.2 template" --cpu-core=2 --cpu-max=4
151158
152159
   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
153160

154-
##### 2.4). <b>**Create a new VM with memory size:**<b>
155-
- `--memory=MEMORY_SIZE` Config the target memory size in GB.
156-
- `--min-mem=MIN_MEMORY` Config the min static memory size in GB.
157-
- `--max-mem=MAX_MEMORY` Config the max static memory size in GB.
161+
##### 2.4). <b>**Create a new VM with memory size:**<b>
162+
163+
- `--memory=MEMORY_SIZE` Config the target memory size in GB.
164+
- `--min-mem=MIN_MEMORY` Config the min static memory size in GB.
165+
- `--max-mem=MAX_MEMORY` Config the max static memory size in GB.
158166

159167
<b>There are static memory and dynamic memory. The static memory can be set only when VM is power off, and dynamic memory
160168
can be set when VM is running or stop. Memory limits must satisfy: *min_static_memory <= min_dynamic_memory <= max_dynamic_memory <= max_static_memory*. The max_static_memory will affect the upper limit when set the dynamic memory lively when VM is running.<b>
161169

162-
163-
- `--memory will set the min dynamic memory and max dynamic memory to the target one, when VM power on, it runs at that memory size`
164-
- `--min-mem will set the min static memory `
165-
- `--max-mem will set the max static memory `
170+
- `--memory will set the min dynamic memory and max dynamic memory to the target one, when VM power on, it runs at that memory size`
171+
- `--min-mem will set the min static memory`
172+
- `--max-mem will set the max static memory`
166173
> create_vm.py -c "test2" -t "CentOS 7.2 template" --memory=2 --max-mem=4
167174
168175
##### 2.5).Add new disk to VM, the storage_name is choosed from *--list-SR*
169-
- `--add-disk=DISK_SIZE` The disk size(GB) add to the VM
170-
- `--storage=STORAGE_NAME` The storage location where the virtual disk put
176+
177+
- `--add-disk=DISK_SIZE` The disk size(GB) add to the VM
178+
- `--storage=STORAGE_NAME` The storage location where the virtual disk put
171179
> create_vm.py "test1"--add-disk=2 --storage=data2
172180
173181
**if no *--storage*, will use the storage which has a largest free volume**
174182
> create_vm.py "test1"--add-disk=2
175183
176184
## 3. delete_vm.py
185+
177186
To delete a vm, please use *delete_vm.py --vm=vm_to_delete*, this will ask user to input 'yes' or 'no' to confirm
178-
- `delete_vm.py --vm=vm_name [--host=ip --user=user --pwd=passwd]`
187+
188+
- `delete_vm.py --vm=vm_name [--host=ip --user=user --pwd=passwd]`
179189

180190
if `--force` is set, then the additional disk on vm will also be deleted, this feature is only support on kvm now.
181191

182192
## 4. config_vm.py
183-
#### The IP and memory configuration is same as that in create VM.
184-
- `--list-vif` List the virtual interface device in guest VM
185-
- `--list-pif` List the interface device in the host
186-
- `--list-network` List the bridge/switch network in the host
187-
- `--list-SR` List the storage repository information in the host
193+
194+
#### The IP and memory configuration is same as that in create VM
195+
196+
- `--list-vif` List the virtual interface device in guest VM
197+
- `--list-pif` List the interface device in the host
198+
- `--list-network` List the bridge/switch network in the host
199+
- `--list-SR` List the storage repository information in the host
188200

189201
##### 4.1). <b>**Config a VM's interface, add a VIF, delete a VIF, config a VIF(will delete old one if exist, otherwise create it newly), and the *--ip*, *--device*, *--network* is same as that when create vm**<b>
190-
- `--add-vif=ADD_INDEX` Add a virtual interface device to guest VM
191-
- `--del-vif=DEL_INDEX` Delete a virtual interface device from guest VM
192-
- `--vif=VIF_INDEX` Configure on a virtual interface device
202+
203+
- `--add-vif=ADD_INDEX` Add a virtual interface device to guest VM
204+
- `--del-vif=DEL_INDEX` Delete a virtual interface device from guest VM
205+
- `--vif=VIF_INDEX` Configure on a virtual interface device
193206

194207
> config_vm.py "test1" --vif=0 --ip=192.168.1.200 --device=eth0
195208
196209
> config_vm.py "test1" --add-vif=1 --device=eth1 --ip=192.168.1.200 --netmask=255.255.255.0
197210
198211
##### 4.2). <b>**config a VM' cpu when it is running**<b>
199-
- `--cpu-cores=CPU_CORES` Config the VCPU cores lively for a running VM or the number of startup VCPUs for a halted VM
212+
213+
- `--cpu-cores=CPU_CORES` Config the VCPU cores lively for a running VM or the number of startup VCPUs for a halted VM
200214

201215
> config_vm.py "test1" --cpu-core=4
202216

203217
##### 4.3). <b>**config a VM' memory when it is running**<b>
204-
- `--memory=MEMORY_SIZE` Config the target memory size in GB.
218+
219+
- `--memory=MEMORY_SIZE` Config the target memory size in GB.
205220

206221
> config_vm.py "test1" --memory=1
207222
208223
##### 4.4).Add new disk to VM, the storage_name is choosed from *--list-SR*
209-
- `--add-disk=DISK_SIZE` The disk size(GB) add to the VM
210-
- `--storage=STORAGE_NAME` The storage location where the virtual disk put
224+
225+
- `--add-disk=DISK_SIZE` The disk size(GB) add to the VM
226+
- `--storage=STORAGE_NAME` The storage location where the virtual disk put
211227
> config_vm.py "test1"--add-disk=2 --storage=data2
212228
213229
**if no *--storage*, will use the storage which has a largest free volume**
214230
> config_vm.py "test1"--add-disk=2
215231
216-
217232
## 5. power_on.py, power_off.py
218-
- power_on.py vm1 vm2 \[--host=ip --user=user --pwd=passwd\]
219-
- power_off.py vm1 vm2 \[--host=ip --user=user --pwd=passwd\]
233+
234+
- power_on.py vm1 vm2 \[--host=ip --user=user --pwd=passwd\]
235+
- power_off.py vm1 vm2 \[--host=ip --user=user --pwd=passwd\]
220236

221237
## 6. dump_vm.py, ist the memory, CPU, Disk, and system information of VM
222-
- `--list` List the cpu and memory information
223-
- `--list-disk` List the virtual disk size
224-
- `--list-vifs` List all the VIFs information
238+
239+
- `--list` List the cpu and memory information
240+
- `--list-disk` List the virtual disk size
241+
- `--list-vifs` List all the VIFs information
225242

226243
**if no options, will list all basic informations, include, cpu, memory, os, disk, interface mac and IP**
227244

228245
**Note: a problem is that the VIF index given to xenserver, but it is not always the index of eth in VM guest, it depend on the create sequence of virtual interface.**
229246

230247
## 7. dump_host.py, list the memory, CPU, Disk, and system information of server
231-
- `--list-sr` List all the storage information
232-
- `--list-pif` List all the interface information
233-
- `--list-bond` List all the bond information
234-
- `--list-vm` List all the vms
235-
- `--list-templ` List all the templates in the server.
236-
- `--list-network` List the network in the host (in Xenserver, it is same as bridge)
237-
- `--list-bridge` List the bridge/switch names in the host
248+
249+
- `--list-sr` List all the storage information
250+
- `--list-pif` List all the interface information
251+
- `--list-bond` List all the bond information
252+
- `--list-vm` List all the vms
253+
- `--list-templ` List all the templates in the server.
254+
- `--list-network` List the network in the host (in Xenserver, it is same as bridge)
255+
- `--list-bridge` List the bridge/switch names in the host
238256

239257
> dump_host.py \[--host=ip --user=user --pwd=passwd\]
240258

app/cmdb/fake_db_driver.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
File Name: fake_db_driver.py
5+
Author: longhui
6+
Created Time: 2021-6-26 18:24:47
7+
"""
8+
9+
10+
from lib.Db.db_driver import DatabaseDriver
11+
12+
13+
class FakeDBDriver(DatabaseDriver):
14+
def __init__(self, user="admin", passwd="admin"):
15+
pass
16+
17+
def __nonzero__(self):
18+
"""
19+
rewrite this so that the instance support value test
20+
:return:
21+
"""
22+
return True
23+
24+
def close(self):
25+
return True
26+
27+
def create(self, **kwargs):
28+
return True
29+
30+
def delete(self, id=None):
31+
return True
32+
33+
def update(self, id=None, data=None):
34+
return True
35+
36+
def query(self, id=None):
37+
return True

lib/Db/db_factory.py

+9
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,26 @@
77
Descriptions: The factory class for Database crud
88
'''
99

10+
import os
1011
from app.cmdb.hosts import HostDriver, VirtualHostDriver
12+
from app.cmdb.fake_db_driver import FakeDBDriver
13+
1114

1215
class DbFactory(object):
1316

1417
@classmethod
1518
def get_db_driver(cls, table_class):
1619
"""
1720
return the relative database driver class
21+
If DB_HOST enviroment is not configure, return fake db driver that will do nothing
1822
:param table_class: the table class define in app folder
1923
:return:
2024
"""
25+
26+
DB_HOST = os.getenv("DB_HOST", None)
27+
if DB_HOST is None:
28+
return FakeDBDriver()
29+
2130
if table_class == "Host": # Physical server
2231
return HostDriver()
2332
elif table_class == "VirtHost": # Virtual machine

0 commit comments

Comments
 (0)