Code Monkey home page Code Monkey logo

ops_sdk's Introduction

安装

python3.6 安装

python链接

在 CentOS 7 中安装 Python 依赖
$ yum -y groupinstall development
$ yum -y install zlib-devel
$ yum install -y python3-devel openssl-devel libxslt-devel libxml2-devel libcurl-devel
在 Debian 中,我们需要安装 gcc、make 和 zlib 压缩/解压缩库
$ aptitude -y install gcc make zlib1g-dev
运行下面的命令来安装 Python 3.6:
$ wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz
$ xz -d  Python-3.6.3.tar.xz
$ tar xvf Python-3.6.3.tar
$ cd Python-3.6.3/
$ ./configure
$ make && make install

# 查看安装
$ python3 -V
pip3安装
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ python3 get-pip.py

# 查看安装
$ pip3 -V
SDK 安装
$ pip3 install -U git+https://github.com/ss1917/ops_sdk.git

结构

.
├── README.md    项目readme
└── opssdk
    ├── logs     日志模块
    ├── install  安装模块
    ├── get_info 配置获取
    └── operate  运维操作
        ├── check           系统参数检查和获取
        ├── mysql           mysql 操作
        ├── mail            发送邮件
        └── centralization  集中化管理工具 salt

logs

from opssdk.logs import Log
### 日志路径
log_path = '/log/yunwei/{0}.log'.format(os.path.basename(__file__))
### 添加日志标识
log_ins = Log('yunwei', log_path)
### 写日志 ('debug', 'info', 'warning', 'error', 'critical')
log_ins.write_log('info', 'ceshi')

operate

  • exec_shell 执行shell命令
from opssdk.operate import exec_shell
recode,stdout = exec_shell('ls')
recode 为0 则代表成功stdout 内容 为列表格式 半月逗号分隔
recode 非0 则代表失败stdout 内容 字符串格式
  • exclusiveLock 脚本锁,防止脚本重复执行
from opssdk.operate import exclusiveLock
exclusiveLock(脚本名称)
  • MyCrypt 加密解密模块
from opssdk.operate import MyCrypt
mc = MyCrypt()                  # 实例化
mc.my_encrypt('ceshi')          # 对字符串ceshi进行加密
mc.my_decrypt('')               # 对密文进行解密
  • now_time 获取当前时间 '%Y-%m-%d-%H-%M-%S'格式
from opssdk.operate import now_time
print(now_time())
  • is_ip 判断是否是IP ,True代表是,False代表不是
from opssdk.operate import is_ip
print(is_ip('192.168.1.11'))

check

系统参数检查和获取

  • check_disk 检查目录磁盘剩余空间是否大于10G
from opssdk.operate.check import check_disk
print(check_disk('/data1'))
  • check_sys_version 检查系统版本
from opssdk.operate.check import check_sys_version
print(check_sys_version())
  • get_ip_address 根据网卡获取ip地址
from opssdk.operate.check import get_ip_address
print(get_ip_address('lo'))

get_info

解析配置文件

  • json_to_dict 根据json文件的路径 把内容转化成字典格式
from opssdk.get_info import json_to_dict
print(json_to_dict('/tmp/conf.json'))
  • IniToDict 根据ini文件的路径、节点 把内容转化成字典格式
from opssdk.get_info import IniToDict
itd = IniToDict('/tmp/conf.ini','config') # 实例化
print(itd.get_option())
print(itd.get_option('v1'))

mysql 操作

from opssdk.operate.mysql import MysqlBase
mysql_dict = {"host": "172.16.0.223", "port": 3306, "user": "root", "passwd": "ljXrcyn7", "db": "zhi"}
mb = MysqlBase(**mysql_dict)
### 查询 返回查询值
mb.query(sql)
### 增删改 返回影响行
mb.change(sql)

mail

发送邮件

from opssdk.operate.mail import Mail
mailto_list = "[email protected], [email protected], [email protected]"
sm = Mail()
"""
:param to_list:  收件人以半角逗号分隔 必填
:param header:   发件名,必填
:param sub:      标题 必填。
:param content:  发件内容 必填。
:param subtype:  发件格式 默认plain,可选 html格式
:param att:      附件 只支持单附件,选填
:return:         True or False
"""
sm.send_mail(mailto_list, '运维', "标题", "内容")
sm.send_mail(mailto_list, '运维', "标题", "内容", 'plain', '/tmp/cof.ini')

salt api 操作

from  opssdk.operate.centralization import SaltApi
my_salt = SaltApi(url='https://127.0.0.1:8001/', username="saltapi", password="shenshuo")
### 主机  执行方法   命令
req = my_salt.run('*', 'cmd.run_all', 'w')
status, stdout, stderr = req[0], req[1], req[2]
print(status, stdout, stderr)

ops_sdk's People

Contributors

ss1917 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.