1 Application Technology
back-end
- Python3.7
- Django2.x
- Celery4.x
- Saltstack
- Restful
- Mysql
- Redis
Front end
- jquery
- bootstrap
- bootstrap-table
- bootstrap-fileinput
2 Implementation process
2.1 CMDB Asset Management
2.1.1 Asset Input
- Fill in the form on the front page.
- Submit data to Django background.
- Write to Mysql database through Django Orm operation
data_info = {'id':1,'ip':'192.168.56.1','hostname':'test'} OpServerM.objects.get_or_create(**data_info)
2.1.2 Asset deletion
- Similar to above
2.1.3 Asset Modification
- Editing front-end using bootstrapTable
- Submit Modified Data to Django Background through ajax
- Then update the database through the Orm operation
update_data = {'id':1,'ip':'192.168.56.101','hostname':'test01'} server = OpServerM.objects.filter(pk=update_data['id']) server.update(**update_data)
2.1.4 Batch Import Assets
- The front-end uses bootstrap-fileinput to upload data to Django background
- Django Background Writes Mysql Database with bulk_create Batch Operation
response = HttpResponse() response['Content-Type'] = "text/javascript" hostsFile = request.FILES.getlist('uploadhosts') for i in hostsFile: filename = os.path.join(MEDIA_ROOT,i.name) file = open(filename, 'wb') for chrunk in i.chunks(): file.write(chrunk) file.close()
- Batch Import Database bulk_create
def imp_hosts(filename,sheet_name,model_name): fileds = model_name._meta.get_fields(include_parents=False) flist = [] for filed in fileds: flist.append(str(filed).split('.')[-1]) wb = load_workbook(filename) ##Read the excel table sheet name sheet_name data ws = wb[sheet_name] List=[] for row in list(ws.rows)[1:]: cols = [] for col in row: cols.append(col.value) tab_dic = dict(map(lambda x, y: [x, y], flist, cols)) List.append(model_name(**tab_dic)) model_name.objects.bulk_create(List)
2.2 Server Management--Saltstack
2.2.1 Defines various service state.sls modules
- EG: File Synchronization
log-monitor: file.managed: - name: /data/shell/log-monitor.sh - source: salt://init/centos/files/log-monitor.sh - template: jinja
- For more tasks, please refer to the official website.
2.2.2 Configuration of Salt API
Reference material
2.2.3 Salt Restful API
2.2.4 Execution of orders
salt '192.168.56.101' cmd.run 'free -m'
2.2.5 System Initialization, Basic Service Installation, Application Installation, User Management
- Enter assets through CMDB
- Django Background Configuration Timing Task Reads CMDB Data
- Distribution of choreographed saltstack tasks to target hosts using state.sls module
Reference material
Saltstack task orchestration stats.sls
2.3 Document Distribution
2.3.1 File Upload
- The front end uploads to Django static service directory media using bootstrap-fileinput
vim {django_root}/project/settings.py MEDIA_URL='/media/' MEDIA_ROOT='media'
- Call the cp.get_url module of saltstack api and distribute it to the target server
salt '192.168.56.101' cp.get_url http://{django-server:port}/media/upload.file /tmp/upload.file
2.3.2 File Download
- Call the saltstack api and use the cp.push module to pull the target file to the Salt Master end
salt '192.168.56.101' cp.push /tmp/download.file
- Then use cp.get_file to push Salt Master-side files to Django's static service media directory
salt 'django-server' cp.get_file salt://192.168.56.101/tmp/download.file /{DJANGO_ROOT}/media/download.file
- Finally, download the files in Django's media directory from the front end
wget http://{django-server:port}/media/download.file
2.4 Task Management--Celery
2.4.1 Writing Tasks
- Timely detection of CMDB hosts, basic services, applications, users
Reference material
Django 2 integrates celery 4 to perform asynchronous and timing tasks
2.4.2 Start the Celery Work Process
celery -A celery_tasks worker -l info
2.4.3 View Registration Tasks
2.4.4 Add task
- Task management using Flower api
Celery Management and Monitoring Tool-Flower