Skip to content

Commit 8bd8e23

Browse files
committed
系统安装
1 parent 0b0217e commit 8bd8e23

File tree

9 files changed

+271
-0
lines changed

9 files changed

+271
-0
lines changed

installos/__init__.py

Whitespace-only changes.

installos/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

installos/cobbler_api.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import cobbler
2+
import xmlrpclib
3+
class CobblerAPI(object):
4+
def __init__(self,url,user,password):
5+
self.cobbler_user= user
6+
self.cobbler_pass = password
7+
self.cobbler_url = url
8+
9+
def add_system(self,hostname,ip_add,mac_add,profile):
10+
'''
11+
Add Cobbler System Infomation
12+
'''
13+
ret = {
14+
"result": True,
15+
"comment": [],
16+
}
17+
18+
remote = xmlrpclib.Server(self.cobbler_url)
19+
token = remote.login(self.cobbler_user,self.cobbler_pass)
20+
system_id = remote.new_system(token)
21+
remote.modify_system(system_id,"name",hostname,token)
22+
remote.modify_system(system_id,"hostname",hostname,token)
23+
remote.modify_system(system_id,'modify_interface', {
24+
"macaddress-eth0" : mac_add,
25+
"ipaddress-eth0" : ip_add,
26+
"dnsname-eth0" : hostname,
27+
}, token)
28+
remote.modify_system(system_id,"profile",profile,token)
29+
remote.save_system(system_id, token)
30+
try:
31+
remote.sync(token)
32+
except Exception as e:
33+
ret['result'] = False
34+
ret['comment'].append(str(e))
35+
return ret

installos/form.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: utf-8 -*-
2+
from django import forms
3+
from installos.models import SystemInstall
4+
5+
class SystemInstallForm(forms.ModelForm):
6+
class Meta:
7+
model = SystemInstall
8+
exclude = ('install_date',)
9+
widgets = {
10+
'ip': forms.TextInput(attrs={'class': 'form-control'}),
11+
'hostname': forms.TextInput(attrs={'class': 'form-control'}),
12+
'macaddress': forms.TextInput(attrs={'class': 'form-control'}),
13+
'system_version': forms.TextInput(attrs={'class': 'form-control'}),
14+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
]
11+
12+
operations = [
13+
migrations.CreateModel(
14+
name='InstallRecord',
15+
fields=[
16+
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
17+
('install_date', models.CharField(max_length=20, verbose_name='\u5b89\u88c5\u65f6\u95f4')),
18+
('ip', models.CharField(max_length=20, verbose_name='\u5b89\u88c5IP')),
19+
('system_version', models.CharField(max_length=20, verbose_name=b'\xe5\xae\x89\xe8\xa3\x85\xe6\x93\x8d\xe4\xbd\x9c\xe7\xb3\xbb\xe7\xbb\x9f\xe7\x89\x88\xe6\x9c\xac')),
20+
],
21+
options={
22+
'verbose_name': '\u88c5\u673a\u8bb0\u5f55\u7ba1\u7406',
23+
},
24+
),
25+
migrations.CreateModel(
26+
name='SystemInstall',
27+
fields=[
28+
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
29+
('ip', models.CharField(max_length=20, verbose_name='\u5f85\u88c5\u673aIP')),
30+
('hostname', models.CharField(max_length=30, verbose_name='\u4e3b\u673a\u540d')),
31+
('macaddress', models.CharField(max_length=50, verbose_name='MAC\u5730\u5740')),
32+
('system_version', models.CharField(max_length=20, verbose_name='\u64cd\u4f5c\u7cfb\u7edf')),
33+
('install_date', models.DateTimeField(auto_now_add=True, verbose_name='\u5b89\u88c5\u65f6\u95f4')),
34+
],
35+
options={
36+
'verbose_name': '\u88c5\u673a\u5217\u8868',
37+
'verbose_name_plural': '\u88c5\u673a\u5217\u8868\u7ba1\u7406',
38+
},
39+
),
40+
]

installos/migrations/__init__.py

Whitespace-only changes.

installos/models.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- coding: utf-8 -*-
2+
from django.db import models
3+
4+
class SystemInstall(models.Model):
5+
"""
6+
System Install Manage
7+
"""
8+
ip = models.CharField(max_length=20, verbose_name=u'待装机IP')
9+
hostname = models.CharField(max_length=30, verbose_name=u'主机名')
10+
macaddress = models.CharField(max_length=50, verbose_name=u'MAC地址')
11+
system_version = models.CharField(max_length=20, verbose_name=u'操作系统')
12+
install_date = models.DateTimeField(auto_now_add=True, verbose_name=u'安装时间')
13+
14+
def __unicode__(self):
15+
return u'%s -- %s' %(self.ip, self.install_date)
16+
17+
class Meta:
18+
verbose_name = u'装机列表'
19+
verbose_name_plural = u'装机列表管理'
20+
21+
class InstallRecord(models.Model):
22+
"""
23+
Server Install Recored
24+
"""
25+
install_date = models.CharField(max_length=20, verbose_name=u'安装时间')
26+
ip = models.CharField(max_length=20, verbose_name=u'安装IP')
27+
system_version = models.CharField(max_length=20, verbose_name = '安装操作系统版本')
28+
29+
def __unicode__(self):
30+
return u'%s - %s' %(self.ip, self.system_version)
31+
32+
class Meta:
33+
verbose_name = u'装机记录'
34+
verbose_name = u'装机记录管理'

installos/tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

installos/views.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# -*- coding: utf-8 -*-
2+
from django.http import HttpResponseRedirect
3+
from django.template import RequestContext
4+
from django.shortcuts import render_to_response, get_object_or_404
5+
from django.core.urlresolvers import reverse
6+
from django.contrib.auth.decorators import login_required
7+
from django.core.paginator import Paginator
8+
from installos.models import *
9+
from asset.models import HostList
10+
from installos.form import SystemInstallForm
11+
from installos.cobbler_api import CobblerAPI
12+
from ycm.models import *
13+
from ycm.mysql import db_operate
14+
from Ycm import settings
15+
16+
def system_install_managed(request,id=None):
17+
"""
18+
Management host to be installed
19+
"""
20+
user = request.user
21+
if id:
22+
system_install = get_object_or_404(SystemInstall, pk=id)
23+
action = 'edit'
24+
page_name = '编辑安装信息'
25+
else:
26+
system_install = SystemInstall()
27+
action = 'add'
28+
page_name = '添加安装信息'
29+
if request.method == 'POST':
30+
operate = request.POST.get('operate')
31+
form = SystemInstallForm(request.POST,instance=system_install)
32+
if form.is_valid():
33+
if action == 'add':
34+
form.save()
35+
return HttpResponseRedirect(reverse('install_list'))
36+
if operate:
37+
if operate == 'update':
38+
form.save()
39+
db = db_operate()
40+
sql = 'select ip from installed_systeminstall where id = %s' % (id)
41+
ret = db.mysql_command(settings.YCM_MYSQL,sql)
42+
Message.objects.create(type='idc', action='install', action_ip=ret, content='主机信息已更新(macadd、system_version),准备装机')
43+
return HttpResponseRedirect(reverse('install_list'))
44+
else:
45+
pass
46+
else:
47+
form = SystemInstallForm(instance=system_install)
48+
49+
return render_to_response('install_manage.html',
50+
{"form": form,
51+
"page_name": page_name,
52+
"action": action,
53+
},context_instance=RequestContext(request))
54+
55+
def system_install_list(request):
56+
"""
57+
List all waiting for the host operating system is installed
58+
"""
59+
user = request.user
60+
61+
#获取待装机的信息,从数据库中查询是否存在,未存在的插入到列表
62+
result = HostList.objects.filter(status='待装机')
63+
install_list = []
64+
for i in range(len(result)):
65+
ip = str(result[i]).split()[0]
66+
hostname = str(result[i]).split()[2]
67+
ret = SystemInstall.objects.filter(ip=ip)
68+
if ret:
69+
message = ip + ' already in database'
70+
else:
71+
data = {'ip': ip, 'hostname': hostname}
72+
install_list.append(data)
73+
74+
#列表数据插入数据库
75+
for i in range(len(install_list)):
76+
p = SystemInstall(ip=install_list[i]['ip'],hostname=install_list[i]['hostname'])
77+
p.save()
78+
79+
all_system_list = SystemInstall.objects.all()
80+
paginator = Paginator(all_system_list,10)
81+
82+
try:
83+
page = int(request.GET.get('page','1'))
84+
except ValueError:
85+
page = 1
86+
87+
try:
88+
all_system_list = paginator.page(page)
89+
except :
90+
all_system_list = paginator.page(paginator.num_pages)
91+
92+
return render_to_response('install_list.html', {'all_system_list': all_system_list, 'page': page, 'paginator':paginator},context_instance=RequestContext(request))
93+
94+
95+
def system_install(request):
96+
"""
97+
安装系统
98+
"""
99+
100+
cobbler = CobblerAPI(url=settings.Cobbler_API['url'],username=settings.Cobbler_API['user'],password=settings.Cobbler_API['password'])
101+
if request.method == 'GET':
102+
ip = request.GET.get('ip')
103+
hostname = request.GET.get('host')
104+
mac_add = request.GET.get('mac')
105+
profile = request.GET.get('ver')
106+
ret = cobbler.add_system(hostname=hostname,ip_add=ip,mac_add=mac_add,profile=profile)
107+
if ret['result']:
108+
data = SystemInstall.objects.filter(ip=ip)
109+
install_date = str(data[0]).split('--')[1].strip()
110+
InstallRecord.objects.create(ip=ip,system_version=profile,install_date=install_date)
111+
HostList.objects.filter(ip=ip).update(status='已使用') #主机信息加入cobbler system,主机列表的状态变更为已使用状态,不再是待装机状态!
112+
SystemInstall.objects.filter(ip=ip).delete() #安装后,装机列表此IP信息删除,转让到安装记录里供审计
113+
Message.objects.create(type='idc', action='install', action_ip=ip, content='主机信息添加至cobbler,进入安装模式')
114+
return HttpResponseRedirect(reverse('install_list'),{},context_instance=RequestContext(request))
115+
116+
117+
def system_install_record(request):
118+
"""
119+
List all operating system installation records
120+
"""
121+
122+
user = request.user
123+
124+
record = InstallRecord.objects.all()
125+
paginator = Paginator(record,10)
126+
127+
try:
128+
page = int(request.GET.get('page','1'))
129+
except ValueError:
130+
page = 1
131+
132+
try:
133+
record = paginator.page(page)
134+
except :
135+
record = paginator.page(paginator.num_pages)
136+
137+
return render_to_response('install_record_list.html',
138+
{'record': record,
139+
'page': page,
140+
'paginator':paginator},
141+
context_instance=RequestContext(request)
142+
)

0 commit comments

Comments
 (0)