如何搭建一个运维后台,能让我们完成自动化管理。
我们的第一步如何做好一个简洁的资产管理系统。
我在这里就使用Django-admin来搭建这个系统。
首先创建一个项目,在我使用的1.7版本的Django
root@CD-FTP-VPN:/opt# django-admin startproject Ops_System 项目名字为Ops_System
root@CD-FTP-VPN:/opt# cd Ops_System
root@CD-FTP-VPN:/opt/Ops_System# tree #这里是目录树。
.
|-- manage.py
`-- Ops_System
|-- __init__.py
|-- settings.py
|-- urls.py
`-- wsgi.py
1 directory, 5 files
继续创建一个APP
root@CD-FTP-VPN:/opt/Ops_System# django-admin startapp Asset_management
root@CD-FTP-VPN:/opt/Ops_System# tree
.
|-- Asset_management
| |-- admin.py
| |-- __init__.py
| |-- migrations
| | `-- __init__.py
| |-- models.py
| |-- tests.py
| `-- views.py
|-- manage.py
`-- Ops_System
|-- __init__.py
|-- settings.py
|-- urls.py
`-- wsgi.py
这里要注意下settings.py是全局的配置,即项目下的所有全局配置都在这里,下面要说的urls.py也类似
配置项目下的setting文件
root@CD-FTP-VPN:/opt/Ops_System# vim Ops_System/settings.py
"""
Django settings for pay1pay project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) #变量,路径是指的我们项目的初始路径/opt/Ops_System
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '0f-rcb1@lb3%105!zcwt4dk*ykutswchc6if3p4rv&jt2-1^y4'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True #我们开启debug方便排错,加上运维系统使用的人也不多,完全不需要第3方类似Apache的HTTP服务的支持。
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Asset_management', #添加我们的项目
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.locale.LocaleMiddleware', #这个类让我们可以使得DJANGO的在前端展示的语言和系统同步。
)
ROOT_URLCONF = 'Ops_System.urls'
WSGI_APPLICATION = 'Ops_System.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = { #我在这里是使用mysql,Django默认的是sqlite3
'default': {
'ENGINE': 'django.db.backends.mysql',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'NAME':'Ops_System',
'USER':'ops',
'PASSWORD':'ops',
'HOST':'localhost',
'PORT':'3306',
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Shanghai' #修改时区,不然Django的时间和我们系统时间会不一致
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
启用urls.py
root@CD-FTP-VPN:/opt/Ops_System# vim Ops_System/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'Ops_System.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)), # Django默认已经启用了admin
)
APP下的models.py
root@CD-FTP-VPN:/opt/Ops_System# vim Asset_management/models.py
# -*- coding:utf-8 -*-
from django.db import models
# Create your models here.
class ServerList(models.Model):
hostname=models.CharField(max_length=50,verbose_name=u'主机名')
base=models.CharField(max_length=100,verbose_name=u'机房')
location=models.CharField(max_length=50,verbose_name=u'机架')
version=models.CharField(max_length=50,verbose_name=u'型号')
memory=models.CharField(max_length=15,verbose_name=u'内存')
disk=models.CharField(max_length=50,verbose_name=u'硬盘')
cpu=models.CharField(max_length=50,verbose_name=u'CPU')
disk_array=models.CharField(max_length=50,verbose_name=u'硬盘阵列')
power=models.CharField(max_length=100,verbose_name=u'电源')
extend_hardware=models.CharField(max_length=50,blank=True, null=True,verbose_name=u'额外添加')
wan_ip=models.CharField(max_length=80,verbose_name=u'外网IP')
Intranet_ip=models.CharField(max_length=80,verbose_name=u'内网IP')
os=models.CharField(max_length=50,verbose_name=u'系统版本')
usage=models.CharField(max_length=80,verbose_name=u'用途')
serial_number=models.CharField(max_length=100,verbose_name=u'序列号')
fixed_assets_encoding=models.CharField(max_length=100,verbose_name=u'固定资产编号')
ops_history=models.TextField(verbose_name=u'操作历史',blank=True, null=True)
comment=models.TextField(verbose_name=u'注释',blank=True, null=True)
def __unicode__(self):
return u'%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s' %(self.hostname,self.base,self.location,self.version,self.memory,self.disk,self.cpu,self.disk_array,self.power,self.extend_hardware,self.wan_ip,self.Intranet_ip,self.os,self.usage,self.serial_number,self.fixed_assets_encoding,self.ops_history,self.comment)
APP下的admin.py
root@CD-FTP-VPN:/opt/Ops_System# vim Asset_management/admin.py
from django.contrib import admin
# Register your models here.
from Asset_management.models import ServerList
class TitleList(admin.ModelAdmin):
list_display = ('hostname','base','location','version','memory','disk','cpu','disk_array','power','extend_hardware','wan_ip','Intranet_ip','os','usage','serial_number','fixed_assets_encoding','ops_history','comment')
search_fields = ('hostname','base','location','version','memory','disk','cpu','disk_array','power','extend_hardware','wan_ip','Intranet_ip','os','usage','serial_number','fixed_assets_encoding')
admin.site.register(ServerList,TitleList)
删除 migrations 目录
root@CD-FTP-VPN:/opt/Ops_System# rm Asset_management/migrations/ -rf
创建数据库,赋予权限
CREATE DATABASE `Ops_System` /*!40100 DEFAULT CHARACTER SET utf8 */;
GRANT ALL PRIVILEGES ON `Ops_System`.* TO 'ops'@'localhost' identified by 'ops';
同步数据库
root@CD-FTP-VPN:/opt/Ops_System# python manage.py syncdb
Operations to perform:
Synchronize unmigrated apps: Asset_management
Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Creating table Asset_management_serverlist
Installing custom SQL...
Installing indexes...
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying sessions.0001_initial... OK
You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'root'): admin 创建用户admin
Email address:
Password:
Password (again):
Superuser created successfully.
root@CD-FTP-VPN:/opt/Ops_System#
现在开始启动啦
先写个启动脚本
root@CD-FTP-VPN:/opt/Ops_System# vim start.sh
#!/bin/sh
export.UTF-8
python manage.py runserver 0.0.0.0:8080
root@CD-FTP-VPN:/opt/Ops_System# sh start.sh 启动我们的网站
打开浏览器输入http://192.168.1.1:8080/admin/ 访问我们新创建的后台
输入密码账号进入我们可以看见我们定义的ServerList
点击增加,我们试着加一条记录
点击保存,我们能返回这个列表
怎么样,这个UI不错的吧。到这里一个简单的资产管理后台就做好了。
怎么样 很简单吧。