Skip to content

Commit 7c80648

Browse files
yangmvyangmv
authored andcommitted
cmdb新增自动判断并生成秘钥对
1 parent a2c9b51 commit 7c80648

File tree

12 files changed

+87
-13
lines changed

12 files changed

+87
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#### 一 安装依赖
1818
```
19+
yum -y install $(cat rpm_requirements.txt)
1920
pip3 install --upgrade pip
2021
pip3 install -r requirements.txt
2122
```
1.22 KB
Binary file not shown.

apps/assets/cores/server.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
from ops.settings import BASE_DIR
1010
from assets.models import server as models
1111
from apps.ws.cores.api import get_asset_info
12-
from libs.common import remoteUpfile_Exec,remoteUpfile_Exec_KEY,getKeyFile
12+
from libs.common import remoteUpfile_Exec,remoteUpfile_Exec_KEY,getKeyFile,exec_shell
1313
from ops.settings import PUBLIC_KEY
1414
import json
1515
import os
1616
import threading
17-
from django.core import exceptions
1817

1918
def rsyncHostData(data):
2019
'''更新获取到的资产信息入库CMDB'''
@@ -39,8 +38,44 @@ def __init__(self,hosts):
3938
self.hosts = hosts
4039
self.lock = threading.Lock()
4140
self.Error = {}
41+
self.rsa_dir = os.path.dirname(PUBLIC_KEY)
42+
43+
@property
44+
def check_rsa(self):
45+
'''检查CMDB Server端秘钥对配置'''
46+
obj = models.Configs.objects.filter(name='cmdb').first()
47+
if not obj:
48+
id_rsa_exists = os.path.exists('%s/id_rsa'%self.rsa_dir)
49+
# 如果本地有,则无需重新生成
50+
if not id_rsa_exists:
51+
check_rsa = self.initRsa()
52+
if not check_rsa:
53+
return False
54+
self.saveConf()
55+
return True
56+
else:
57+
return True
58+
59+
def initRsa(self):
60+
'''Server端生成秘钥对,并写入配置'''
61+
cmd = 'ssh-keygen -t rsa -P "" -f %s/id_rsa'%self.rsa_dir
62+
code,ret = exec_shell(cmd)
63+
if code == 0:
64+
return True
65+
else:
66+
return False
67+
68+
def saveConf(self):
69+
'''把读取或者生成的秘钥信息保存到SQL'''
70+
with open('%s/id_rsa'%self.rsa_dir,'r') as id_rsa,open('%s/id_rsa.pub'%self.rsa_dir,'r') as id_rsa_pub:
71+
models.Configs.objects.get_or_create(
72+
name = 'cmdb',
73+
id_rsa = id_rsa.read(),
74+
id_rsa_pub = id_rsa_pub.read()
75+
)
4276

4377
def start(self):
78+
'''批量下发server端公钥到client端'''
4479
threads = [threading.Thread(target=self.exec, args=(host,)) for host in self.hosts]
4580
for start_t in threads:
4681
start_t.start()
@@ -92,7 +127,7 @@ class getHostData_SSH():
92127
pass
93128

94129
class getHostData():
95-
'''通过AnsibleAPI 批量获取主机资产信息'''
130+
'''通过AnsibleAPI 批量获取主机资产信息(容器环境不支持)'''
96131
def __init__(self,hosts):
97132
self.host_list = hosts
98133
self.Error_host = []
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Generated by Django 2.1.3 on 2019-01-11 17:10
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('assets', '0005_dbserver_region'),
10+
]
11+
12+
operations = [
13+
migrations.CreateModel(
14+
name='Configs',
15+
fields=[
16+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
17+
('name', models.CharField(max_length=16, unique=True)),
18+
('id_rsa', models.TextField(blank=True, max_length=4096, null=True)),
19+
('id_rsa_pub', models.TextField(blank=True, max_length=4096, null=True)),
20+
],
21+
options={
22+
'verbose_name': 'CMDB配置表',
23+
'verbose_name_plural': 'CMDB配置表',
24+
},
25+
),
26+
]
435 Bytes
Binary file not shown.

apps/assets/models/server.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,13 @@ class RecorderLog(models.Model):
159159
data = models.TextField(null=True,blank=True)
160160
class Meta:
161161
verbose_name = '回放日志'
162-
verbose_name_plural = '回放日志'
162+
verbose_name_plural = '回放日志'
163+
164+
165+
class Configs(models.Model):
166+
name = models.CharField(unique=True, max_length=16)
167+
id_rsa = models.TextField(max_length=4096, blank=True, null=True) #私钥
168+
id_rsa_pub = models.TextField(max_length=4096, blank=True, null=True) #公钥
169+
class Meta:
170+
verbose_name = 'CMDB配置表'
171+
verbose_name_plural = 'CMDB配置表'
82 Bytes
Binary file not shown.

apps/assets/views/server.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,17 @@ def post(self, request, format=None):
235235
if request.data and type(request.data) == list:
236236
hosts = models.Server.objects.filter(id__in=request.data)
237237
obj = rsyncPublicKey(hosts)
238-
rsync = obj.start()
239-
if rsync:
240-
ret['msg'] = json.dumps(rsync,ensure_ascii=False)
238+
if obj.check_rsa:
239+
rsync = obj.start()
240+
if rsync:
241+
ret['msg'] = json.dumps(rsync,ensure_ascii=False)
242+
else:
243+
models.Server.objects.filter(id__in=request.data).update(public_key=True)
244+
ret['status'] = True
245+
ret['msg'] = 'Success'
246+
ret['data'] = request.data
241247
else:
242-
models.Server.objects.filter(id__in=request.data).update(public_key=True)
243-
ret['status'] = True
244-
ret['msg'] = 'Success'
245-
ret['data'] = request.data
248+
ret['msg'] = '秘钥对生成失败,请手工生成秘钥对'
246249
else:
247250
ret['msg'] = 'args is None, Please Check!'
248251
return Response(ret)
0 Bytes
Binary file not shown.

apps/ws/cores/api.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python
22
#encoding:utf-8
33

4-
54
def get_object(model, **kwargs):
65
"""
76
use this function for query

0 commit comments

Comments
 (0)