Skip to content

Commit

Permalink
add redis config
Browse files Browse the repository at this point in the history
celery redis config update
update scripts update
  • Loading branch information
guohongze committed Feb 9, 2018
1 parent 471117f commit 9ba73cc
Show file tree
Hide file tree
Showing 15 changed files with 288 additions and 23 deletions.
6 changes: 6 additions & 0 deletions adminset.conf
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ collection = sys_info
[webssh]
domain = adminset.cn

[redis]
redis_host = 127.0.0.1
redis_port = 6379
redis_password = root
redis_db = 0


16 changes: 14 additions & 2 deletions adminset/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

import ConfigParser
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'adminset.settings')

app = Celery('adminset')

# redis connect code
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
config = ConfigParser.ConfigParser()
config.read(os.path.join(BASE_DIR, 'adminset.conf'))
redis_host = config.get('redis', "redis_host")
redis_port = config.get("redis", "redis_port")
redis_db = config.get('redis', "redis_db")
redis_password = config.get('redis', "redis_password")
if redis_password:
app.conf.broker_url = 'redis://:{0}@{1}:{2}/{3}'.format(redis_password, redis_host, redis_port, redis_db)
else:
app.conf.broker_url = 'redis://{0}:{1}/{2}'.format(redis_host, redis_port, redis_db)

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
Expand Down
2 changes: 2 additions & 0 deletions adminset/ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
USER = 'cn=admin,dc=gccmx,dc=cn'
PASSWORD = 'yourpass'
BASE_DN = 'dc=gccmx,dc=cn'


class LDAPTool:

def __init__(self,ldap_host=None,base_dn=None,user=None,password=None):
Expand Down
12 changes: 9 additions & 3 deletions adminset/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

config = ConfigParser.ConfigParser()
config.read(os.path.join(BASE_DIR, 'adminset.conf'))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/


# SECURITY WARNING: keep the secret key used in production secret!

SECRET_KEY = 'n@s)3&f$tu#-^^%k-dj__th2)7m!m*(ag!fs=6ezyzb7l%@i@9'
CELERY_BROKER_URL = 'redis://localhost/0'
# if redis_password:
# CELERY_BROKER_URL = 'redis://:{0}@{1}:{2}/{3}'.format(redis_password, redis_host, redis_port, redis_db)
# else:
# CELERY_BROKER_URL = 'redis://{0}:{1}/{2}'.format(redis_host, redis_port, redis_db)
CELERY_ACCEPT_CONTENT = ['json']
CELERY_RESULT_BACKEND = 'django-db'
CELERY_TASK_SERIALIZER = 'json'
Expand Down Expand Up @@ -97,8 +104,7 @@
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }
config = ConfigParser.ConfigParser()
config.read(os.path.join(BASE_DIR, 'adminset.conf'))


DATABASES = {}
if config.get('db', 'engine') == 'mysql':
Expand Down
22 changes: 22 additions & 0 deletions config/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def index(request):
mongodb_pwd = config.get('mongodb', 'mongodb_pwd')
mongodb_collection = config.get('mongodb', 'collection')
webssh_domain = config.get('webssh', 'domain')
redis_host = config.get('redis', 'redis_host')
redis_port = config.get('redis', 'redis_port')
redis_password = config.get('redis', 'redis_password')
redis_db = config.get('redis', 'redis_db')
return render(request, 'config/index.html', locals())


Expand Down Expand Up @@ -73,6 +77,11 @@ def config_save(request):
mongodb_collection = request.POST.get('mongodb_collection')
# webssh domain
webssh_domain = request.POST.get('webssh_domain')
# redis infon
redis_host = request.POST.get('redis_host')
redis_port = request.POST.get('redis_port')
redis_password = request.POST.get('redis_password')
redis_db = request.POST.get('redis_db')

config = ConfigParser.RawConfigParser()
dirs = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand Down Expand Up @@ -102,6 +111,11 @@ def config_save(request):
config.set('mongodb', 'collection', mongodb_collection)
config.add_section('webssh')
config.set('webssh', 'domain', webssh_domain)
config.add_section('redis')
config.set('redis', 'redis_host', redis_host)
config.set('redis', 'redis_port', redis_port)
config.set('redis', 'redis_password', redis_password)
config.set('redis', 'redis_db', redis_db)
tips = u"保存成功!"
display_control = ""
with open(dirs+'/adminset.conf', 'wb') as cfgfile:
Expand All @@ -127,6 +141,10 @@ def config_save(request):
mongodb_pwd = config.get('mongodb', 'mongodb_pwd')
mongodb_collection = config.get('mongodb', 'collection')
webssh_domain = config.get('webssh', 'domain')
redis_host = config.get('redis', 'redis_host')
redis_port = config.get('redis', 'redis_port')
redis_password = config.get('redis', 'redis_password')
redis_db = config.get('redis', 'redis_db')
else:
display_control = "none"
return render(request, 'config/index.html', locals())
Expand All @@ -151,6 +169,10 @@ def get_dir(args):
mongodb_pwd = config.get('mongodb', 'mongodb_pwd')
mongodb_collection = config.get('mongodb', 'collection')
webssh_domain = config.get('webssh', 'domain')
redis_host = config.get('redis', 'redis_host')
redis_port = config.get('redis', 'redis_port')
redis_password = config.get('redis', 'redis_password')
redis_db = config.get('redis', 'redis_db')
# 根据传入参数返回变量以获取配置,返回变量名与参数名相同
if args:
return vars()[args]
Expand Down
4 changes: 2 additions & 2 deletions delivery/delivery.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ def delivery_edit(request, project_id):
def delivery_deploy(request, project_id):
server_list = []
project = Delivery.objects.get(job_name_id=project_id)
project.bar_data = 1
project.bar_data = 10
job_name = project.job_name.name
source_address = project.job_name.source_address
app_path = project.job_name.appPath
project.status = True
project.deploy_num += 1
project.save()
sleep(5)
sleep(3)
os.system("mkdir -p /var/opt/adminset/workspace/{0}/logs".format(job_name))
if app_path == "/":
return HttpResponse("app deploy destination cannot /")
Expand Down
38 changes: 28 additions & 10 deletions delivery/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
import shutil
from time import sleep

# class GetRedis(object):
# host = get_dir("redis_host")
# port = get_dir("redis_port")
# db = get_dir("redis_db")
# password = get_dir("redis_password")
#
# def connect(self):
# conn = redis.StrictRedis(host=self.host, port=self.port,
# password=self.password, db=self.db)
# return conn


@shared_task
def deploy(job_name, server_list, app_path, source_address, project_id):
Expand All @@ -18,22 +29,29 @@ def deploy(job_name, server_list, app_path, source_address, project_id):
if app_path.endswith("/"):
app_path += "/"
# clean build code
p1.bar_data = 2
p1.bar_data = 20
p1.save()
sleep(5)
print p1.bar_data
sleep(3)
cmd = ""
if p1.build_clean:
try:
shutil.rmtree("{0}code/".format(job_workspace))
except:
print "dir is not exists"
if os.path.exists("{0}code/.git".format(job_workspace)):
print "git pull"
cmd = "cd {0}code/ && git pull".format(job_workspace)
else:
print "git clone"
cmd = "git clone {0} {1}code/".format(source_address, job_workspace)
p1.bar_data = 3
if p1.job_name.SOURCE_TYPE == "git":
if os.path.exists("{0}code/.git".format(job_workspace)):
cmd = "cd {0}code/ && git pull".format(job_workspace)
else:
cmd = "git clone {0} {1}code/".format(source_address, job_workspace)
if p1.job_name.SOURCE_TYPE == "svn":
if os.path.exists("{0}code/.svn".format(job_workspace)):
cmd = "cd {0}code/ && svn update".format(job_workspace)
else:
cmd = "svn checkout {0} {1}code/".format(source_address, job_workspace)
if p1.job_name.SOURCE_TYPE == "file":
os.remove("{0}code/*".format(job_workspace))
cmd = "cd {1}code/ && wget {0} ".format(source_address, job_workspace)
p1.bar_data = 30
p1.save()
sleep(5)
p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
Expand Down
161 changes: 161 additions & 0 deletions docs/Manual.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#主机名
adminset程序依赖主机名,所以被控机器、客户机,都需要设置主机名,主机名唯一,并且可以被解析
请在 /etc/hosts 或是DNS中加入主机名的解析。
#install
安装需要使用yum源请保证可用,或使用本地yum源。
如果ubuntu客户端需要ansible等管理功能,需要开启root登录(配置脚本会自动开始,如不需要请手工关闭)

服务器安装:
1、下载代码
git clone https://github.com/guohongze/adminset.git
2、执行安装脚本
adminset/install/server/server_install.sh
安装过程需要输入管理员数据库等交互信息,如果安装中断再次执行server_install.sh即可.
安装过程中会生成rsa密钥,位于/root/.ssh 目录下,如果已经存在,忽略即可。
3、交互信息
1、如果系统开启了selinux会提示:Do you want to disabled selinux?[yes/no]
选择yes。(默认yes)
2、YUM源选择提示do you want to use an internet yum repository?[yes/no]
没有本地的yum源请选择yes,如果有本地的YUM源(包括epel源)请选择no。(默认值yes)
3、数据库选择提示:do you want to create a new mysql database?[yes/no]
本地没有数据库选择yes自动下载安装mariadb数据库,如已经存在mysql或mariadb数据库选择no,然后填写相关信息主机、端口、用户名、密码。(默认值yes)
4、mongodb选择提示:do you want to create a new Mongodb?[YES/no]
本地没有mongodb选择yes自动下载安装mongodb数据库,如已经存在mongodb数据库选择no,然后填写相关信息主机、端口、用户名、密码。(默认值yes)
5、创建超管用户提示,please create your adminset' super admin: 输入超管用户名、邮件、密码。
客户端安装
说明:为保证注册IP是管理IP(后续会被ansible等调用),客户端的IP抓取目前使用主机名解析,否则报错。 如:主机名为cn-bj-web01 请在/etc/hosts中加入相应的解析 192.168.x.x cn-bj-web01,这样再执行adminset_agent.py 可以保证正常运行。 centos7不进行解析也可获取主机IP.
step1:安装依赖
拷贝adminset/install/client/client_install.sh 到客户机上并执行:
sh client_install.sh

step2:执行agent
拷贝adminset/install/client/adminset_agent.py 到客户机上并执行:
python adminset_agent.py
后台运行请参考:
nohup adminset_aent.py &
agent默认每1800秒上传一次资产和硬件信息,可以在adminset_agent.py中自定义
访问:
http://your_server_ip
使用自己在安装过程中创建的super admin用户名密码

#程序目录
安装脚本会将文件安装在/var/opt/adminset
main为程序代码
config 配置
pid pid文件
logs 日志
data 常用数据

#站点导航用法
在站点管理中输入常用的运维工具系统后会自动出现在站点导航界面。

#cmdb用法
install/client/adminset_agent.py 开户后会自动上报主机相关信息到CMDB
获取主机信息
http://your_server_ip/cmdb/get/host/?token=your_token&name=host_name
获取所有主机:
http://your_server_ip/cmdb/get/host/?token=your_token&name=all
获取组信息:
http://your_server_ip/cmdb/get/group/?token=your_token&name=group_name
获取所有组:
http://your_server_ip/cmdb/get/group/?token=your_token&name=all

#启用webssh
需要设置域名解析,默认域名为adminset.cn(可以在配置管理页面进行变更)
需要将这个域名做泛解析指向adminset所在的服务器,在本地或公网DNS都行,如果没有可以设置HOSTS解析,但HOSTS不支持泛解析。
这样做是为了解决webssh启动时区分不同session进行认证而设置。

指向完成后点击资产管理中的webssh按钮会触发域名格式如下:
{{ host.hostname }}.adminset.cn:2222/ssh/host/{{ host.ip }}
如主机名为cmdb IP为 192.168.47.130
http://cmdb.adminset.cn:2222/ssh/host/192.168.47.130
通过此URL进入webssh访问界面,第一次进入时会询问用户名密码,请填写系统对应的用户和密码即可。


# 定时任务用法
首先新建interval 或crontab
新建任务填写名字
选择间隔或crontab
在Keyword arguments(任务指令):处的写法是json格式:
执行命令<br>
{"host":"c1", name:"service tomcat restart"}
执行脚本<br>
{"host":"c1", name:"reboot.sh"}
拉到最下边Task (registered)
setup.tasks.command是直接向目标机器发送命令
setup.tasks.scripts是在目标机器上执行一个你已经上传到服务器中的脚本,默认路径/var/opt/adminset/data/scripts
注意:已经运行任务以后,再去修改任务不会立即生效,需要重启beat组件,在任务编排的后台管理中可以重启。
这是由于celery的BUG导致,会在社区发布稳定版本以后修复。

#ansible用法
1、自动设置证书认证
通过adminset_agent自动上报的服务器,可以自动设置免密登入(认书认证)
前提是已经在客户端做了hosts解析,并且密码与在服务器的系统配置>密钥设置>ssh password
相同,也就是说如果自动分发密钥必须在系统配置中提前输入密码并保存,系统默认带的密码是root。
注意:系统只有在第一次上报信息时会调用ssh密钥分发.如果以后想使用自动密钥分发需要在资产管理中
删除服务信息,然后再自动上报即可。

2、手工设置认证书认证。
配置免密钥登陆客机(ansible和shell管理客户机需要此配置)
在服务器上执行
ssh-keygen
ssh-copy-id -i /root/.ssh/id_rsa.pub {客户机IP}
输入客户机密码后认证成功可以ssh免密登入

CMDB自动上报主机以后,在ansible页面执行 同步到ansible将主机信息写入ansible的hosts文件
然后将playbook 或是role脚本上传到/var/opt/adminset/data/playbook 或/var/opt/adminset/data/roles

#shell用法
依赖免密登入(与ansible同)
CMDB自动上报主机以后,shell界面可以直接调用主机。
然后将常用脚本上传到/var/opt/adminset/data/scripts 中shell脚本栏将会自动发现脚本。

#监控平台用法
当adminset_agent.py自动上报信息到,监控会自动发现并配置,无需干预.
当监控页面打开时,前端JS每10秒会异步抓取监控数据
agent默认每60秒上传一次监控数据,可以在adminset_agent.py中自定义

#权限管理
1、新建权限如:
名字:资产管理
URL:/cmdb/
2、新建角色:
名字:资产管理员
可选择权限:资产管理
3、新建用户
在角色一栏选择:资产管理员


#组件启动管理
service adminset {start|stop|restart} # gunicorn管理程序
service nginx {start|stop|restart} # web server
service redis {start|stop|restart} # 缓存和任务列表
service mariadb {start|stop|restart} # 数据库,账号资产等信息
service celery {start|stop|restart} # 异步任务主程序
service beat {start|stop|restart} # 任务调用
service mongod {start|stop|restart} # 监控数据库
service webssh {start|stop|restart} # web终端功能

#升级与更新
强烈建设在升级或更新adminset之前先备份数据库,并在测试环境验证通过,因为adminset在快速的发展过程中,每版本功能与结构变化较大。
1)小版本更新:
如v0.3.6更新到v0.3.7只需下载相应版本的代码到本地然后执行:
adminset/install/update.sh
2)大版本更新:
如v0.3.0更新到v0.4.0
adminset/install/server_install.sh
如果原在数据库本机上未设置密码则在do you want to install new mysql/mongodb时选择yes可覆盖安装,这时会要求再创建一个超管,但不能和以前的超管同名,因为之前已经有数据了。
如果已设置密码或是独立服务器可以选择no,或者在安装完成后手动修改/var/opt/adminset/main/adminset.conf文件中的相应信息。
3)二次开发
rsync.sh脚本只做增量,rsync参数不带--delete选项,不会在生产环境删除代码中已删除的条目,不更新组件配置文件,不会生成新的ORM数据库条目。
update.sh脚本带--delete选项,同步代码,重新发布各组件的配置文件,并重新生成ORM数据文件(makemigrations migrate)。
4)0.5版本结构变动较大,更新请使用server_install.sh 然后在设置数据库时使用现有数据库。

# 安全
强烈建议不要将程序启动在有公网可以直接访问的设备上,如果需要请使用VPN。
建议生产环境中使用https配置服务器<br>
建议adminset放在网管区中,并且开启防火墙。
django的settings中开启了DEBUG,在生产中需要关闭并指定自己的域名。

# 开发者交流
QQ群:427794947
11 changes: 9 additions & 2 deletions install/server/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ rsync --progress -ra --delete --exclude '.git' $cur_dir/ $adminset_dir
#scp $adminset_dir/install/server/ansible/ansible.cfg /etc/ansible/ansible.cfg
cd $adminset_dir
pip install -r requirements.txt
python manage.py makemigrations
python manage.py migrate

if [ $1 ]
then
python manage.py makemigrations $1
python manage.py migrate
else
python manage.py makemigrations
python manage.py migrate
fi
echo "####update celery####"
mkdir -p $config_dir/celery
scp $adminset_dir/install/server/celery/beat.conf $config_dir/celery/beat.conf
Expand Down
Binary file added static/dist/img/avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/dist/img/avatar04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/dist/img/avatar2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/dist/img/avatar3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/dist/img/avatar5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 9ba73cc

Please sign in to comment.