Commit 81b850de by 李杨杨

1.tx2升级脚本更新为全速版本;2.增加TX2配置和IP修改程序Purple_Gourd_for_TX2

1 parent 30d336d6
#!/usr/bin/env python3
import json
import shutil
from ansible.module_utils.common.collections import ImmutableDict
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.plugins.callback import CallbackBase
from ansible import context
import ansible.constants as C
class ResultCallback(CallbackBase):
"""A sample callback plugin used for performing an action as results come in
If you want to collect all results into a single object for processing at
the end of the execution, look into utilizing the ``json`` callback plugin
or writing your own custom callback plugin
"""
def format_print(self, backStatus, result):
host = result._host
print_color='yellow'
if backStatus == 'SUCESS' and result._result['changed'] == True:
status = 'CHANGED'
print_color = 'yellow'
elif backStatus == 'UNREACHABLE!' or backStatus == 'FAIL!':
status = backStatus
print_color = 'red'
else:
status = backStatus
print_color = 'green'
if status == 'UNREACHABLE!':
message = result._result['msg']
else:
message = '\n'.join(result._result['stdout_lines'])
if print_color == 'red':
print("\033[0;31m%s | %s >>\n%s\n\n\033[0m" % (host, status, message))
elif print_color == 'yellow':
print("\033[0;33m%s | %s >>\n%s\n\n\033[0m" % (host, status, message))
elif print_color == 'green':
print("\033[0;32m%s | %s >>\n%s\n\n\033[0m" % (host, status, message))
def v2_runner_on_ok(self, result, **kwargs):
"""Print a json representation of the result
This method could store the result in an instance attribute for retrieval later
"""
host = result._host
# print(json.dumps({host.name: result._result['stdout_lines']}, indent=4))
# print('SUCCESS: ' + host.name + ' ===> ' + json.dumps(result._result['stdout_lines']))
self.format_print('SUCESS', result)
def v2_runner_on_failed(self, result, **kwargs):
host = result._host
# print('FAIL: ' + host.name + ' ===> ' + result._result['stdout_lines'])
self.format_print('FAIL!', result)
def v2_runner_on_unreachable(self, result):
host = result._host
# print(json.dumps({host.name: result._result}, indent=4))
self.format_print('UNREACHABLE!', result)
#class ResultCallback(CallbackBase):
# """
# 重写callbackBase类的部分方法
# """
# def __init__(self, *args, **kwargs):
# super().__init__(*args, **kwargs)
# self.host_ok = {}
# self.host_unreachable = {}
# self.host_failed = {}
# self.task_ok = {}
# def v2_runner_on_unreachable(self, result):
# self.host_unreachable[result._host.get_name()] = result
#
# def v2_runner_on_ok(self, result, **kwargs):
# self.host_ok[result._host.get_name()] = result
#
# def v2_runner_on_failed(self, result, **kwargs):
# self.host_failed[result._host.get_name()] = result
class MyAnsiable():
def __init__(self,
connection='local', # 连接方式 local 本地方式,smart ssh方式
remote_user=None, # 远程用户
ack_pass=None, # 提示输入密码
sudo=None, sudo_user=None, ask_sudo_pass=None,
module_path=None, # 模块路径,可以指定一个自定义模块的路径
forks=10,
become=True, # 是否提权
become_method='sudo', # 提权方式 默认 sudo 可以是 su
become_user='root', # 提权后,要成为的用户,并非登录用户
check=False, diff=False,
listhosts=None, listtasks=None,listtags=None,
verbosity=3,
syntax=None,
start_at_task=None,
inventory=None):
# 函数文档注释
"""
初始化函数,定义的默认的选项值,
在初始化的时候可以传参,以便覆盖默认选项的值
"""
context.CLIARGS = ImmutableDict(
connection=connection,
remote_user=remote_user,
ack_pass=ack_pass,
sudo=sudo,
sudo_user=sudo_user,
ask_sudo_pass=ask_sudo_pass,
module_path=module_path,
become=become,
become_method=become_method,
become_user=become_user,
verbosity=verbosity,
listhosts=listhosts,
listtasks=listtasks,
listtags=listtags,
syntax=syntax,
start_at_task=start_at_task,
)
# 三元表达式,假如没有传递 inventory, 就使用 "localhost,"
# 确定 inventory 文件
self.inventory = inventory if inventory else "localhost,"
# 实例化数据解析器
self.loader = DataLoader()
# 实例化 资产配置对象
self.inv_obj = InventoryManager(loader=self.loader, sources=self.inventory)
# 设置密码,可以为空字典,但必须有此参数
self.passwords = {}
# 实例化回调插件对象
self.results_callback = ResultCallback()
# 变量管理器
self.variable_manager = VariableManager(self.loader, self.inv_obj)
def run(self, hosts='localhost', gether_facts="no", module="ping", args=''):
play_source = dict(
name = "Ad-hoc",
hosts = hosts,
gather_facts = gether_facts,
tasks = [
# 这里每个 task 就是这个列表中的一个元素,格式是嵌套的字典
# 也可以作为参数传递过来,这里就简单化了。
{"action":{"module": module, "args": args}},
])
play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader)
tqm = None
try:
tqm = TaskQueueManager(
inventory=self.inv_obj ,
variable_manager=self.variable_manager,
loader=self.loader,
passwords=self.passwords,
stdout_callback=self.results_callback)
result = tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup()
shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
def playbook(self,playbooks):
from ansible.executor.playbook_executor import PlaybookExecutor
playbook = PlaybookExecutor(playbooks=playbooks, # 注意这里是一个列表
inventory=self.inv_obj,
variable_manager=self.variable_manager,
loader=self.loader,
passwords=self.passwords)
# 使用回调函数
playbook._tqm._stdout_callback = self.results_callback
result = playbook.run()
def get_result(self):
result_raw = {'success':{},'failed':{},'unreachable':{}}
# print(self.results_callback.host_ok)
for host,result in self.results_callback.host_ok.items():
result_raw['success'][host] = result._result["stdout_lines"]
for host,result in self.results_callback.host_failed.items():
result_raw['failed'][host] = result._result
for host,result in self.results_callback.host_unreachable.items():
result_raw['unreachable'][host] = result._result
# 最终打印结果,并且使用 JSON 继续格式化
print(json.dumps(result_raw, indent=4))
if __name__ == '__main__':
ansible2 = MyAnsiable(inventory='tmp_hosts', connection='smart')
ansible2.run(hosts= "192.168.8.8", module="script", args='test_reboot.sh')
# ansible2.run(hosts= "192.168.8.100", module="raw", args='ls')
#ansible2.
# Purple_Gourd_for_TX2
## ----用于TX2配置和IP修改
1. 包含的功能有修改主节点配置/IP、修改子节点配置/IP,根据需要开启其中一种或多种
2. 更改为读取配置文件的执行方式,填写简要信息运行程序即可
3. 自动判断和安装ansible工具,直接调用Ansible API,根据配置动态生成主机列表
4. 摒弃开机脚本修改IP的方式,直接修改interfaces文件
5. 摒弃拷贝初始配置文件到各节点之后替换IP的配置方式,无需拷贝,直接修改各节点现有配置
6. 多进程方式提高配置速度
## 使用方式
```shell
# 根据modify.ini内提示联系实际场景,填写配置文件
chmod +x main.py
./main.py
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
class Inventory():
def __init__(self, hosts_path, group, hosts_list, user, passwd):
# ansible的hosts文件路径
self._hostsfile = hosts_path
self._data = self._getInventoryInfo(group, hosts_list, user, passwd)
if not self._genHostsFile():
print("生成失败临时hosts失败!")
sys.exit()
def _getInventoryInfo(self, group, hosts_list, user, passwd):
tempdata = {"Groupname": group, "Items": []}
for host in hosts_list:
tempdata['Items'].append(
{
"ansible_ssh_host": host,
"ansible_ssh_user": user,
"ansible_ssh_pass": passwd,
"ansible_sudo_pass": passwd
}
)
return tempdata
def _genHostsFile(self):
"""
生成hosts文件
:return: 生成成功返回True
"""
try:
with open(self._hostsfile, "a") as f:
groupname = self._data.get("Groupname")
f.write("["+groupname+"]\n")
for server in self._data.get("Items"):
ansible_ssh_host = server.get("ansible_ssh_host")
ansible_ssh_user = server.get("ansible_ssh_user")
ansible_ssh_pass = server.get("ansible_ssh_pass")
ansible_sudo_pass = server.get("ansible_sudo_pass")
info = "{0} ansible_ssh_user={1} ansible_ssh_pass={2} ansible_sudo_pass={3}".\
format(ansible_ssh_host, ansible_ssh_user, ansible_ssh_pass, ansible_sudo_pass)
line = info + "\n"
f.write(line)
except Exception as err:
print(err)
return False
return True
if __name__ == "__main__":
hosts_list=['192.168.8.100', '192.168.8.101', '192.168.8.102', '192.168.8.103', '192.168.8.105']
Inventory('tmp_hosts', 'tx2', hosts_list, 'ubuntu', 'ubuntu')
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2020/9/16 18:28
# @Author : Young Lee
# @Email : young_lee2017@163.com
import configparser
import chardet
import re
import logging
import sys
import os
import socket
import fcntl
import struct
from multiprocessing import Process
from create_hosts import Inventory
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logging.disable(logging.INFO)
class Configure_FX():
""" 配置繁星的类 """
def __init__(self):
self.config_file = 'modify.ini'
self.logger = logging.getLogger(__name__)
self.self_ip = self.get_self_ip('eth0')
# self.self_ip = '192.168.8.104'
self.reset_config()
# 获取所在主节点IP
def get_self_ip(self, ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15].encode()))[20:24])
# 初始化配置参数
def reset_config(self):
self.options = {}
self.master_info = {}
self.slave_info = {}
configfile = configparser.ConfigParser()
with open(self.config_file, 'rb') as f:
str = f.read()
coding = chardet.detect(str)['encoding']
configfile.read(self.config_file, encoding=coding)
for item in configfile.options('options'):
self.options[item] = configfile.get('options', item)
for item in configfile.options('master_info'):
self.master_info[item] = configfile.get('master_info', item)
for item in configfile.options('slave_info'):
self.slave_info[item] = configfile.get('slave_info', item)
self.logger.info(self.options)
self.logger.info(self.master_info)
self.logger.info(self.slave_info)
# 检测是否安装ansible/ansible版本是否为2.8
def check_ansible_version(self):
try:
import ansible
ansible_version = re.search('\d+.\d+', ansible.__version__).group()
if ansible_version != '2.8':
print("\033[0;36m>>>>>>>>>>即将开始安装ansible2.8.6<<<<<<<<<<\n\033[0m")
time.sleep(3)
result = os.system('tar zxf ansible_offline.tar.gz;chmod +x ansible_offline/install.sh;./ansible_offline/install.sh')
if result == 0:
print("\033[0;36m>>>>>>>>>>完成安装ansible2.8.6!<<<<<<<<<<\n\033[0m")
except:
print("\033[0;36m>>>>>>>>>>即将开始安装ansible2.8.6<<<<<<<<<<\n\033[0m")
time.sleep(3)
result = os.system('pwd;tar zxf ansible_offline.tar.gz;chmod +x ansible_offline/install.sh;./ansible_offline/install.sh')
if result == 0:
print("\033[0;36m>>>>>>>>>>完成安装ansible2.8.6!<<<<<<<<<<\n\033[0m")
finally:
from Ansible_API import MyAnsiable
global MyAnsiable
# 对操作过滤,程序的入口
def filter_operation(self):
if self.options['modify_ip_slave'] == '1' or self.options['update_config_slave'] == '1':
self.check_ansible_version()
self.make_slave_info()
if self.options['update_config_slave'] == '1':
self.make_master_info('slave_use')
self.configure_slave()
if self.options['modify_ip_slave'] == '1':
self.modify_slave_ip()
if self.options['modify_ip_master'] == '1' or self.options['update_config_master'] == '1':
self.make_master_info()
if self.options['update_config_master'] == '1':
self.configure_master()
if self.options['modify_ip_master'] == '1':
self.modify_master_ip()
# 将配置的IP转成列表
def get_each_ip(self, infos):
ip_list = []
ip_infos = re.findall('\d+.\d+.\d+.\d+-?\d*', infos)
for info in ip_infos:
if re.search('-', info):
ip_block = re.search('(\d+.\d+.\d+.)(\d+)-(\d+)', info)
if ip_block:
ip_pre = ip_block.group(1)
ip_min = ip_block.group(2)
ip_max = ip_block.group(3)
if ip_min > ip_max:
print('\033[0;31m起始IP大于结束IP(%s),请检查modify.ini!\033[0m' % info)
sys.exit()
ip_list = ip_list + ["%s%d" % (ip_pre, num) for num in range(int(ip_min), int(ip_max)+1)]
else:
print('\033[0;31m子节点IP配置存在空值(%s),请检查modify.ini!\033[0m' % info)
sys.exit()
else:
dest_ip = re.search('\d+.\d+.\d+.\d+', info).group()
if dest_ip:
ip_list.append(info)
return ip_list
# 删除旧hosts文件
def check_hosts_file(self):
self.hosts_file = 'tmp_hosts'
if os.path.exists(self.hosts_file):
os.remove(self.hosts_file)
# 再封装一层调用ansible
def use_ansible_api(self, ansible_worker, ip, module, command):
ansible_worker.run(hosts=ip, module=module, args=command)
# 整理子节点信息
def make_slave_info(self):
self.check_hosts_file()
if self.slave_info['slave_ip']:
# 生成子节点IP列表,跳过主节点IP
self.slave_ips = self.get_each_ip(self.slave_info['slave_ip'])
self_ip_index = self.slave_ips.index(self.self_ip)
del self.slave_ips[self_ip_index]
self.logger.info(self.slave_ips)
# 生成新子节点IP列表,跳过主节点新IP
if self.options['modify_ip_slave'] == '1':
if self.slave_info['slave_whether_reboot'] == '1':
self.slave_whether_reboot = 'reboot'
else:
self.whether_reboot = 'noreboot'
self.slave_new_mask = self.slave_info['slave_new_mask']
if self.slave_info['slave_new_ip'] and self.slave_new_mask:
self.slave_ips_new = self.get_each_ip(self.slave_info['slave_new_ip'])
if len(self.slave_ips) != len(self.slave_ips_new) - 1:
print('\033[0;31m新旧IP数目不一致,请检查modify.ini!\033[0m')
sys.exit()
del self.slave_ips_new[self_ip_index]
self.logger.info(self.slave_ips_new)
Inventory(self.hosts_file, 'old', self.slave_ips, 'ubuntu', 'ubuntu')
Inventory(self.hosts_file, 'tx2', self.slave_ips_new, 'ubuntu', 'ubuntu')
else:
print("\033[0;31m缺少子节点更改后的IP或掩码,请检查modify.ini!\033[0m")
sys.exit()
else:
Inventory(self.hosts_file, 'tx2', self.slave_ips, 'ubuntu', 'ubuntu')
else:
print('\033[0;31m没有填写子节点IP信息,请检查modify.ini!\033[0m')
sys.exit()
# 整理主节点信息
def make_master_info(self, type='master_use'):
if self.master_info['auto_get'] == '1' and self.options['modify_ip_master'] == '0':
self.master_int_ip = self.self_ip
# 获取子节点需要的主节点内网IP后不再进行其他操作
if type == 'slave_use':
return
self.master_ext_ip = self.get_self_ip('eth1')
else:
re_int = re.search('\d+.\d+.\d+.\d+', self.master_info['master_int_ip'])
if self.master_info['master_int_ip'] and re_int:
self.master_int_ip = re_int.group()
else:
print('\033[0;31mmaster_int_ip(%s)无效,请检查modify.ini!\033[0m' % self.master_info['master_int_ip'])
sys.exit()
# 获取子节点需要的主节点内网IP后不再进行其他操作
if type == 'slave_use':
return
re_ext = re.search('\d+.\d+.\d+.\d+', self.master_info['master_ext_ip'])
if self.master_info['master_ext_ip'] and re_ext:
self.master_ext_ip = re_ext.group()
else:
print('\033[0;31mmaster_ext_ip(%s)无效,请检查modify.ini!\033[0m' % self.master_info['master_ext_ip'])
sys.exit()
# 修改主节点IP时获取如下信息
if self.options['modify_ip_master'] == '1':
if self.master_info['master_whether_reboot'] == '1':
self.master_whether_reboot = 'reboot'
else:
self.master_whether_reboot = 'noreboot'
self.master_ext_mask = self.master_info['master_ext_mask']
self.master_int_mask = self.master_info['master_int_mask']
self.master_gateway = self.master_info['master_gateway']
# 更新主节点配置时获取如下信息
if self.options['update_config_master'] == '1':
self.plat_ip = self.master_info['plat_ip']
# 修改子节点IP
def modify_slave_ip(self):
print("\033[0;36m>>>>>>>>>>开始修改子节点IP<<<<<<<<<<\n\033[0m")
ansible_worker = MyAnsiable(inventory='tmp_hosts', connection='smart')
# 进程池Pool不能在类中使用,此处调用Process模拟进程池
process_list = []
for i in range(len(self.slave_ips)):
command = 'modify.sh %s %s %s' % (self.slave_ips_new[i], self.slave_new_mask, self.slave_whether_reboot)
p = Process(target=self.use_ansible_api,
args=(ansible_worker, self.slave_ips[i], 'script', command,))
process_list.append(p)
p.start()
while len(process_list) > 9:
for process in process_list:
if not process.is_alive():
process_list.remove(process)
for p in process_list:
p.join()
print("\033[0;36m>>>>>>>>>>完成修改子节点IP!<<<<<<<<<<\n\033[0m")
# 修改主节点IP
def modify_master_ip(self):
print("\033[0;36m>>>>>>>>>>开始修改主节点IP!<<<<<<<<<<\n\033[0m")
os.system('chmod +x modify.sh;./modify.sh %s %s %s' % (self.master_int_ip, self.master_int_mask, 'noreboot'))
os.system('chmod +x modify.sh;./modify.sh %s %s %s %s' % (self.master_ext_ip, self.master_ext_mask,
self.master_gateway, self.master_whether_reboot))
print("\033[0;36m>>>>>>>>>>完成修改主节点IP!<<<<<<<<<<\n\033[0m")
# 修改子节点配置,思路同modify_slave_ip
def configure_slave(self):
print("\033[0;36m>>>>>>>>>>开始修改子节点配置<<<<<<<<<<\n\033[0m")
ansible_worker = MyAnsiable(inventory='tmp_hosts', connection='smart')
if self.options['modify_ip_slave'] == '1':
process_list = []
for i in range(len(self.slave_ips)):
command = 'update_fx_config.sh %s %s' % (self.slave_ips_new[i], self.master_int_ip)
p = Process(target=self.use_ansible_api,
args=(ansible_worker, self.slave_ips[i], 'script', command,))
process_list.append(p)
p.start()
while len(process_list) > 9:
for process in process_list:
if not process.is_alive():
process_list.remove(process)
for p in process_list:
p.join()
else:
slave_group = 'tx2'
self.use_ansible_api(ansible_worker, slave_group, 'script', 'update_fx_config.sh %s %s' %
('{{inventory_hostname}}', self.master_int_ip))
print("\033[0;36m>>>>>>>>>>完成修改子节点配置!<<<<<<<<<<\n\033[0m")
# 修改主节点配置
def configure_master(self):
print("\033[0;36m>>>>>>>>>>开始修改主节点配置<<<<<<<<<<\n\033[0m")
os.system('chmod +x update_fx_config.sh && ./update_fx_config.sh %s %s %s' %
(self.master_ext_ip, self.master_int_ip, self.plat_ip))
print("\033[0;36m>>>>>>>>>>完成修改主节点配置!<<<<<<<<<<\n\033[0m")
# def thread_pool_callback(self, worker):
# self.logger.info("called thread pool executor callback function")
# worker_exception = worker.exception()
# if worker_exception:
# self.logger.exception("Worker return exception: {}".format(worker_exception))
if __name__ == "__main__":
rootPath = os.path.dirname(__file__)
os.chdir(rootPath)
configure_fx = Configure_FX()
# configure_fx.get_each_ip(configure_fx.slave_info['slave_ip'])
configure_fx.filter_operation()
# 选择要进行的操作
[options]
;是否修改主节点IP 0-否 1-是
modify_ip_master = 0
;是否更新主节点配置 0-否 1-是
update_config_master = 0
;是否修改子节点IP 0-否 1-是
modify_ip_slave = 0
;是否更新子节点配置 0-否 1-是
update_config_slave = 0
######################################################
# 主节点信息 更新主节点/子节点配置时填写 填写最新的信息
[master_info]
;平台IP 更新主节点配置时填写
;平台和主节点内网IP互通时推荐填写平台内网IP
;例:plat_ip = 192.168.8.239
plat_ip =
;是否自动获取主节点信息 0-否 1-是
;不需要更改主节点IP仅更新配置时打开此功能才生效
;打开此项 之后的主节点信息不需要填写
;自动获取eth0的IP作为内网IP eth1的IP作为外网IP
auto_get = 1
;修改IP后是否重启系统 0-否 1-是
master_whether_reboot = 0
;主节点外网IP 修改主节点IP/更新主节点配置时填写
master_ext_ip =
;主节点内网IP 修改主节点IP/更新主节点配置/更新子节点配置时填写
master_int_ip =
;主节点外网掩码、内网掩码 修改主节点IP时填写
master_ext_mask = 255.255.255.0
master_int_mask = 255.255.255.0
;主节点外网路由 修改主节点IP时填写
master_gateway =
######################################################
# 子节点信息 操作子节点时填写
[slave_info]
;子节点IP范围 多个用;隔开 程序可以自动跳过当前主节点
;例:slave_ip = 192.168.8.2-21
slave_ip =
;修改IP后是否重启系统 0-否 1-是
slave_whether_reboot = 1
# 修改子节点IP时填写salve_new_IP newNetmask
;子节点新IP范围 多个用;隔开
#例:slave_new_ip = 192.168.88.2-21
slave_new_ip =
;掩码
slave_new_mask = 255.255.255.0
#!/bin/bash
if [[ $# == 3 ]]
then
type=inner
ip=$1
netmask=$2
whether_reboot=$3
elif [[ $# == 4 ]]
then
type=out
ip=$1
netmask=$2
gateway=$3
whether_reboot=$4
else
echo Modify IP ERROR!
exit 0
fi
if [[ $type == inner ]]
then
net=`egrep -o '^auto eth0$' /etc/network/interfaces`
if [[ $net ]]
then
sed -i /"^auto eth0$"/,/"^address.*$"/s/"address.*$"/"address $ip"/ /etc/network/interfaces
sed -i /"^auto eth0$"/,/"^netmask.*$"/s/"^netmask.*$"/"netmask $netmask"/ /etc/network/interfaces
sed -i /"^auto eth0$"/,/"^auto eth1"/s/"^gateway"/"#gateway"/ /etc/network/interfaces
fi
elif [[ $type == out ]]
then
net=`egrep -o '^auto eth1$' /etc/network/interfaces`
if [[ $net ]]
then
sed -i /"^auto eth1$"/,/"^address.*$"/s/"address.*$"/"address $ip"/ /etc/network/interfaces
sed -i /"^auto eth1$"/,/"^netmask.*$"/s/"^netmask.*$"/"netmask $netmask"/ /etc/network/interfaces
sed -i /"^auto eth1$"/,/"^gateway.*$"/s/"^gateway.*$"/"gateway $gateway"/ /etc/network/interfaces
fi
fi
if [[ $whether_reboot = reboot ]]
then
reboot_time=`date -d "1 minutes" +%H:%M`
echo "IP($ip)修改成功,系统将会在$reboot_time重启"
shutdown -r $reboot_time
else
echo "IP($ip)修改成功, 请在合适的时间重启系统(reboot -f)"
fi
#!/bin/bash
if [[ $# == 2 ]]
then
type=slave
slave_int_ip=$1
master_int_ip=$2
elif [[ $# == 3 ]]
then
type=master
master_ext_ip=$1
master_int_ip=$2
plat_ip=$3
else
echo Update Config ERROR!
exit 0
fi
master_int_short=`egrep -o [0-9]+.[0-9]+$ <<< $master_int_ip`
update_slave(){
slave_type='slave-'$master_int_short
if [[ `cat /proc/net/dev|egrep -o eth1` && `grep ^if /root/UserApp/Daemon/start.sh|grep eth1` ]]
then
#sed -i '/.\/OpsDaemon/a \\n=====' /root/UserApp/Daemon/start.sh
#sed -i '/^else/a \\n=====' /root/UserApp/Daemon/start.sh
#sed -i '/=====/,/=====/s/^/#/' /root/UserApp/Daemon/start.sh
#sed -i '/^fi/,$s/^/#/' /root/UserApp/Daemon/start.sh
mv /root/UserApp/Daemon/start.sh /root/UserApp/Daemon/start.sh.bk
echo -e '#!/bin/bash\n\ncd /root/UserApp/Daemon\nchmod +x OpsDaemon.sh\n./OpsDaemon.sh &\n\ncd /root/UserApp/Daemon\nchmod +x AnalysisDaemon.sh\n./AnalysisDaemon.sh &' > /root/UserApp/Daemon/start.sh
chmod +x /root/UserApp/Daemon/start.sh
cp /root/UserApp/vioncfg/Ops/ServerConfig_slave.xml /root/UserApp/vioncfg/Ops/ServerConfig.xml
pkill -9 mediaSvr
pkill -9 MediaServer
pkill -9 dataExport
pkill -9 DataExport
pkill -9 OpeateServer
pkill -9 Nginx
/root/UserApp/Daemon/AnalysisDaemon.sh
fi
sed -i "1s/[0-9]\+.[0-9]\+.[0-9]\+.[0-9]\+/$slave_int_ip/" /root/UserApp/vioncfg/VAServer_System.xml
sed -i "2,\$s/<IP>[0-9]\+.[0-9]\+.[0-9]\+.[0-9]\+/<IP>$master_int_ip/" /root/UserApp/vioncfg/VAServer_System.xml
sed -i "s/>2</>10</2" /root/UserApp/vioncfg/VAServer_System.xml
sed -i "1,/>8851</s/[0-9]\+.[0-9]\+.[0-9]\+.[0-9]\+/$master_int_ip/" /root/UserApp/vioncfg/Ops/ServerConfig.xml
sed -i "s/deviceType1/$slave_type/" /root/UserApp/vioncfg/Ops/ServerConfig.xml
sed -i "/<NTP/,/<NTP/s/[0-9]\+.[0-9]\+.[0-9]\+.[0-9]\+<\/NTP/$master_int_ip<\/NTP/" /root/UserApp/vioncfg/Ops/ServerConfig.xml
pkill -9 VAServer
pkill -9 OpeateServer
}
update_master(){
master_type='master-'$master_int_short
sed -i "1,/>8851</s/[0-9]\+.[0-9]\+.[0-9]\+.[0-9]\+/$plat_ip/" /root/UserApp/vioncfg/Ops/ServerConfig.xml
sed -i "/<DNS/,/<\/DNS/s/IP>[0-9]\+.[0-9]\+.[0-9]\+.[0-9]\+/IP>$master_ext_ip/1" /root/UserApp/vioncfg/Ops/ServerConfig.xml
sed -i "/<DNS/,/<\/DNS/s/IP>[0-9]\+.[0-9]\+.[0-9]\+.[0-9]\+/IP>$master_int_ip/2" /root/UserApp/vioncfg/Ops/ServerConfig.xml
sed -i "/<NTP/,\$s/[0-9]\+.[0-9]\+.[0-9]\+.[0-9]\+<\/NTP/$plat_ip<\/NTP/" /root/UserApp/vioncfg/Ops/ServerConfig.xml
sed -i "s/deviceType1/$master_type/" /root/UserApp/vioncfg/Ops/ServerConfig.xml
sed -i "/<ip>/,/<port>/s/[0-9]\+.[0-9]\+.[0-9]\+.[0-9]\+/$master_int_ip/" /root/UserApp/vioncfg/DataExportService/DataExportConfig.xml
sed -i "s/<configServIp>[0-9]\+.[0-9]\+.[0-9]\+.[0-9]\+/<configServIp>$plat_ip/" /root/UserApp/vioncfg/DataExportService/DataExportConfig.xml
sed -i "s/<LocalIp>[0-9]\+.[0-9]\+.[0-9]\+.[0-9]\+/<LocalIp>$master_ext_ip/" /root/UserApp/vioncfg/MediaServer/mediaServer.xml
sed -i "s/<ServerIp>[0-9]\+.[0-9]\+.[0-9]\+.[0-9]\+/<ServerIp>$plat_ip/" /root/UserApp/vioncfg/MediaServer/mediaServer.xml
if [[ -f /etc/haproxy/haproxy.cfg_ ]]
then
sed -i "/videoupload/,/server/s/ [0-9]\+.[0-9]\+.[0-9]\+.[0-9]\+/ $plat_ip/" /etc/haproxy/haproxy.cfg
service haproxy restart
fi
pkill -9 OperateServer
pkill -9 DataExport
pkill -9 MediaServer
}
if [[ $type == slave ]]
then
update_slave
echo "子节点($slave_int_ip)配置更新完成!"
elif [[ $type == master ]]
then
update_master
echo "主节点($master_int_ip)配置更新完成!"
fi
# awesome script
跟运维相关的好脚本,分享一下吧。
......@@ -50,6 +52,14 @@ echo "05 2 * * * root elasticsearch_clean.py" >> /etc/crontab
诊断脚本,详细说明见diagnose_tx1目录中的README文档
## tx2_update_faster
## tx2_update_full_speed.sh
tx2分析程序升级脚本全速版,可以跑满主节点千兆带宽
```
chmod +x tx2_update_full_speed.sh
./tx2_update_full_speed.sh [IP组] [升级包名称]
```
## Purple_Gourd_for_TX2
tx2分析程序升级加速版,具体说明在tx2_update目录
\ No newline at end of file
用于更改TX2主节点的配置/IP、子节点的配置/IP,根据modify.ini内提示配置场景信息后执行main.py即可,具体说明见Purple_Gourd_for_TX2下的README
\ No newline at end of file
# tx2_update_faster
此工具基于ansible批量操作工具,利用FTP在传输升级包时去中心化,采用仿P2P方式提高整体传输速度,理论上升级包越大、节点越多速度提高越大。
请将此包放在/home/ubuntu下解压进入解压后目录,使用方法:
```shell
1. 升级包传至当前目录;
2. chmod +x tx2_update_faster.sh
./tx2_update_faster.sh IP组 升级包名称
例:
./tx2_update_faster.sh tx2 VAServer_LINUX-ARM-TX2_r78250-20200804-liuh.tar.gz
```
\ No newline at end of file
#!/bin/bash
flag=$1
host=$2
exist=`dpkg -l|grep vsftpd`
if [[ ! $exist && $flag == 0 ]]
then
if [[ -f /etc/vsftpd.conf ]];then
rm /etc/vsftpd.conf
fi
dpkg -i vsftpd_3.0.3-9build1_arm64.deb
systemctl disable vsftpd.service
echo 0
elif [[ $exist && $flag == 0 && `pgrep vsftpd` ]]
then
echo true
elif [[ ! $exist && $flag == 1 ]]
then
wget --ftp-user=ubuntu --ftp-password=ubuntu ftp://$host/tx2_update/vsftpd_3.0.3-9build1_arm64.deb
dpkg -i vsftpd_3.0.3-9build1_arm64.deb
rm vsftpd_3.0.3-9build1_arm64.deb
systemctl disable vsftpd.service
fi
sed -i 's/^#write_enable=YES$/write_enable=YES/' /etc/vsftpd.conf
systemctl restart vsftpd.service
#!/bin/bash
ori_dir=$(cd $(dirname $0);pwd)
cd $ori_dir
list_name=$1
file=$2
echo -e "\033[41;33mClean up residual of ansible tools, syslog and VAServer!\033[0m"
ansible $list_name -b -m raw -a "if [ -d /home/ubuntu/.ansible/tmp ];then rm -r /home/ubuntu/.ansible/tmp;fi;if [ -d /root/.ansible/tmp ];then rm -r /root/.ansible/tmp;fi;size=\$(du -sm /var/log|awk '{print \$1}');if [ \$size -gt 1000 ];then echo clean syslog;cat /dev/null > /var/log/syslog;cat /dev/null > /var/log/syslog.1;fi;cd /root/UserApp/services/analysis_service;if [ -d VAServer ];then rm -r VAServer;fi;pkill -9 VAServer"
echo -e "\033[41;33mCopy the analyzer to the child node!\033[0m"
chmod +x check_vsftp.sh
stop_flag=`./check_vsftp.sh 0`
ansible $list_name --list-hosts|egrep -o [0-9]+.[0-9]+.[0-9]+.[0-9]+ > hosts_a
cat /dev/null > hosts_b
if [[ ! -s hosts_a ]]
then
echo Please check /etc/ansible/hosts!
exit 0
fi
chmod 666 $file
first_host=`head -n 1 hosts_a`
self_host=`ip addr|grep inet|awk '{print $2}'|egrep -o [0-9]+.[0-9]+.[0-9]+.[0-9]+|grep ${first_host%.*}'\.'`
echo $self_host >> hosts_b
start=`date +%s`
while [[ -s hosts_a ]]
do
for host in `cat hosts_b`
do
if [[ -s hosts_a ]]
then
now_first=`head -n 1 hosts_a`
sed -i '1d' hosts_a
flag=0
#echo $now_first >> hosts_b
{
result=`ansible $now_first -b -m raw -a "\
mkdir -p /home/ubuntu/tx2_update;\
cd /home/ubuntu/tx2_update;\
wget --ftp-user=ubuntu --ftp-password=ubuntu ftp://$self_host/tx2_update/check_vsftp.sh;\
chmod +x check_vsftp.sh;./check_vsftp.sh 1 $self_host;rm check_vsftp.sh;\
wget --ftp-user=ubuntu --ftp-password=ubuntu ftp://$host/tx2_update/$file;\
chmod 666 $file"`
# echo "$result"
if [[ $result =~ 'CHANGED' ]]
then
printf "%-35s : %-8s\n" "$host --> $now_first" "SUCCESS!"
echo $now_first >> hosts_b
else
printf "%-35s : %-8s\n" "$host --> $now_first" "FAIL!"
fi
} &
fi
done
wait
done
end=`date +%s`
let use=end-start
echo Time-consuming: $use's'
rm hosts_a hosts_b
echo -e "\033[41;33mDelete junk files and restart the analyzer!\033[0m"
ansible $list_name -b -m raw -a "cd /home/ubuntu/tx2_update;tar -zxf $file -C /root/UserApp/services/analysis_service/;rm $file;systemctl stop vsftpd;rm -r /home/ubuntu/tx2_update;cat /root/UserApp/services/analysis_service/VAServer/last_version"
if [[ -z $stop_flag ]];
then
systemctl stop vsftpd
fi
#!/bin/bash
ori_dir=$(cd $(dirname $0);pwd)
cd $ori_dir
if [[ -p fifo_tmp ]]
then
rm fifo_tmp
fi
list_name=$1
file=$2
thread_num=11
mkfifo fifo_tmp
exec 9<>fifo_tmp
for i in `seq 1 $thread_num`
do
echo -ne "\n">&9
done
echo -e "\033[41;33mClean up residual of ansible tools, syslog and VAServer!\033[0m"
ansible $list_name -b -m raw -a "if [ -d /home/ubuntu/.ansible/tmp ];then rm -r /home/ubuntu/.ansible/tmp;fi;if [ -d /root/.ansible/tmp ];then rm -r /root/.ansible/tmp;fi;size=\$(du -sm /var/log|awk '{print \$1}');if [ \$size -gt 1000 ];then echo clean syslog;cat /dev/null > /var/log/syslog;cat /dev/null > /var/log/syslog.1;fi;cd /root/UserApp/services/analysis_service;if [ -d VAServer ];then rm -r VAServer;fi;pkill -9 VAServer;cat /dev/null"
echo -e "\033[41;33mCopy the analyzer to the child node!\033[0m"
hosts=`ansible $list_name --list-hosts|egrep -o [0-9]+.[0-9]+.[0-9]+.[0-9]+`
if [[ ! $hosts ]]
then
echo Please check /etc/ansible/hosts!
exit 0
fi
chmod 666 $file
time1=`date +%s`
IFS=$' '
while read host
do
read -u 9
{
sshpass -p ubuntu scp -o StrictHostKeyChecking=no $file ubuntu@$host:/home/ubuntu
printf "%-20s : %-8s\n-----------------\n\n" "==> $host" "DONE!"
echo -ne "\n">&9
} &
done <<< $hosts
wait
time2=`date +%s`
let cov=$time2-$time1
exec 9<&-;exec 9>&-
rm fifo_tmp
time2=`date +%s`
let use=time2-time1
echo Time-consuming: $use's'
echo -e "\033[41;33mUnzip the analyzer and clean the cache!\033[0m"
ansible $list_name -b -m raw -a "cd /home/ubuntu/;tar -zxf $file -C /root/UserApp/services/analysis_service/;rm $file;cat /root/UserApp/services/analysis_service/VAServer/last_version"
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!