Skip to content

Commit

Permalink
Merge pull request YoLoveLife#39 from YoLoveLife/new-api
Browse files Browse the repository at this point in the history
remove old api&put the new api in
  • Loading branch information
YoLoveLife authored Nov 10, 2017
2 parents b5e985a + 235e5f3 commit 85ee5ec
Show file tree
Hide file tree
Showing 74 changed files with 925 additions and 90,709 deletions.
2 changes: 2 additions & 0 deletions .idea/devEops.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,010 changes: 535 additions & 475 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/deveops/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ def ddr():
import time
host = Host.objects.all()[0]
host.info = str(time.time())
host.save()
host.save()
2 changes: 1 addition & 1 deletion inventory/__init__.py → apps/execute/ansible/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
# Time 06 14:48
# Time 17-11-10
# Author Yo
# Email [email protected]
45 changes: 45 additions & 0 deletions apps/execute/ansible/inventory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
# Time 17-11-6
# Author Yo
# Email [email protected]
from ansible.inventory import Host,Group,Inventory
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from utils.aes import decrypt
class YoHost(Host):
def __init__(self,host):
self.host = host
self.name = host.service_ip
self.port = host.sshport
super(YoHost,self).__init__(self.name,self.port)
self.set_all_variable()

def set_all_variable(self):
self.set_variable('ansible_host', self.name)
self.set_variable('ansible_port', self.port)
self.set_variable('ansible_user', self.host.normal_user)
# self.set_variable('ansible_ssh_pass', self.host.sshpasswd) #密码登陆
self.set_variable("ansible_become", True)
self.set_variable("ansible_become_method", 'sudo')
self.set_variable("ansible_become_user", 'root')
self.set_variable("ansible_become_pass", decrypt(self.host.sshpasswd))

class YoInventory(Inventory):
def __init__(self,host_list):
if host_list is None:
host_list = []
else:
self.host_list = host_list
self.loader = DataLoader()
self.variable_manager = VariableManager()
super(YoInventory,self).__init__(self.loader,self.variable_manager,self.host_list)

def parse_inventory(self,host_list):
all = Group('all')
for host in host_list:
h = YoHost(host)
all.add_host(h)
self.groups = dict(all=all)


181 changes: 181 additions & 0 deletions apps/execute/ansible/runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
# Time 17-11-7
# Author Yo
# Email [email protected]
import time
from collections import namedtuple

import ansible.constants as C
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.parsing.dataloader import DataLoader
from ansible.playbook.play import Play
from ansible.utils.vars import load_extra_vars
from ansible.utils.vars import load_options_vars
from ansible.vars import VariableManager
from apps.execute.ansible.inventory import YoInventory
import os,glob
from operation.models import Script
FILENAME = r"/tmp/%s%s"

class YoRunner(object):
Options = namedtuple("Options", [
'connection', 'module_path', 'private_key_file', "remote_user",
'timeout', 'forks', 'become', 'become_method', 'become_user',
'check', 'extra_vars',
]
)

def __init__(self,
hosts=C.DEFAULT_HOST_LIST,
forks=C.DEFAULT_FORKS, # 5
timeout=C.DEFAULT_TIMEOUT, # SSH timeout = 10s
remote_user=C.DEFAULT_REMOTE_USER, # root
module_path=None, # dirs of custome modules
connection_type="smart",
become=None,
become_method=None,
become_user=None,
check=False,
passwords=None,
extra_vars=None,
private_key_file=None,
gather_facts='no'):
self.pattern = ''
self.variable_manager = VariableManager()
self.loader = DataLoader()
self.gather_facts = gather_facts
self.options = self.Options(
connection=connection_type,
timeout=timeout,
module_path=module_path,
forks=forks,
become=become,
become_method=become_method,
become_user=become_user,
check=check,
remote_user=remote_user,
extra_vars=extra_vars or [],
private_key_file=private_key_file,
)
# self.variable_manager.extra_vars = load_extra_vars(self.loader,
# options=self.options)
self.variable_manager.extra_vars = extra_vars
self.variable_manager.options_vars = load_options_vars(self.options)
self.passwords = passwords or {}
self.inventory = YoInventory(hosts)
self.variable_manager.set_inventory(self.inventory)
self.tasks = []
self.play_source = None
self.play = None
self.runner = None
self.timestamp = str(time.time())
self.filename = FILENAME%(self.timestamp,'')
self.have_script = 0

def set_callback(self,callback):
self.results_callback=callback

@staticmethod
def check_module_args(module_name, module_args=''):
if module_name in C.MODULE_REQUIRE_ARGS and not module_args:
err = "No argument passed to '%s' module." % module_name
print(err)
return False
return True

def task_add(self,task_tuple):
for task in task_tuple:
if not self.check_module_args(task.module,task.args):
return
if task.module == u'script':
self.have_script = 1
script = Script.objects.filter(id=task.args)
if os.path.exists(self.filename):
os.remove(self.filename)
script_name = FILENAME % (self.timestamp, '-' + str(script.get().id))
output = open(script_name, 'w')
output.writelines(script.get().formatScript())
output.close()

self.tasks.append(
dict(action=dict(
module=task.module,
args=script_name,
))
)

def run(self, task_tuple,):# pattern='all'):
"""
:param task_tuple: (('shell', 'ls'), ('ping', ''))
:param pattern:
:param timestamp:
:return:
"""
self.task_add(task_tuple)

self.play_source = dict(
name=self.timestamp,
hosts='all',
gather_facts=self.gather_facts,
tasks=self.tasks
)

self.play = Play().load(
self.play_source,
variable_manager=self.variable_manager,
loader=self.loader,
)

self.runner = TaskQueueManager(
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.passwords,
stdout_callback=self.results_callback,
)

# if not self.inventory.list_hosts("all"):
# raise AnsibleError("Inventory is empty.")
#
# if not self.inventory.list_hosts(self.pattern):
# raise AnsibleError(
# "pattern: %s dose not match any hosts." % self.pattern)

try:
self.runner.run(self.play)
finally:
if self.runner:
self.runner.cleanup()
if self.loader:
self.loader.cleanup_all_tmp_files()
if self.have_script:
self.cleanup_script()

def cleanup_script(self):
# if os.path.exists(self.filename):
# os.remove(self.filename)
# return self.filename
for name in glob.glob(self.filename+'*'):
if os.path.exists(name):
print(name)
os.remove(name)
# def clean_result(self):
# """
# :return: {
# "success": ['hostname',],
# "failed": [('hostname', 'msg'), {}],
# }
# """
# result = {'success': [], 'failed': []}
# for host in self.results_callback.result_q['contacted']:
# result['success'].append(host)
#
# for host, msgs in self.results_callback.result_q['dark'].items():
# msg = '\n'.join(['{} {}: {}'.format(
# msg.get('module_stdout', ''),
# msg.get('invocation', {}).get('module_name'),
# msg.get('msg', '')) for msg in msgs])
# result['failed'].append((host, msg))
# return result
29 changes: 18 additions & 11 deletions apps/execute/api.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# -*- coding:utf-8 -*-
import models,serializers
from application.models import DB
from execute.callback import ResultCallback
# from execute.service.catch.db import DBAnsibleService
from manager.models import Host
from operation.models import PlayBook
from rest_framework import generics
from rest_framework.permissions import IsAuthenticated
from execute.service.catch.basic import BasicAnsibleService
from execute.service.catch.db import DBAnsibleService
from operation.models import PlayBook
from manager.models import Host
from application.models import DBDetail,DB
from service.catch.basic import BasicAnsibleService

import serializers
from execute.ansible.runner import YoRunner

class UpdateHostAPI(generics.ListAPIView):
serializer_class = serializers.UpdateHostSerializer
permission_classes = [IsAuthenticated]
Expand All @@ -16,10 +18,15 @@ def get_queryset(self):
return Host.objects.filter(id=self.kwargs['pk'])

def get(self, request, *args, **kwargs):
host = Host.objects.get(id=self.kwargs['pk'])
playbook = PlayBook.objects.get(id = 1)
bas = BasicAnsibleService(hostlist=[host])
bas.run(tasklist=playbook.tasks.all().order_by('-sort'))
# host = Host.objects.get(id=self.kwargs['pk'])
# playbook = PlayBook.objects.get(id = 1)
# bas = BasicAnsibleService(hostlist=[host])
# bas.run(tasklist=playbook.tasks.all().order_by('-sort'))
hosts = Host.objects.all()
runner = YoRunner(hosts=hosts,extra_vars={'ddr':'ls','zzc':'hostname'})
runner.set_callback(ResultCallback())
playbook = PlayBook.objects.all()[0]
ret = runner.run(playbook.tasks.all())
return super(UpdateHostAPI,self).get(request,*args,**kwargs)

class CatchDBStatusAPI(generics.ListAPIView):
Expand Down
74 changes: 35 additions & 39 deletions apps/execute/service/__init__.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
# -*- coding:utf-8 -*-
from yosible.tasks.tasks import Task
from yosible.run.ansiblerun import Ansible
from yosible.run.playbook import Playbook
from yosible.tasks.tasks import Tasks
from operation.models import Script
__metaclass__ = type
class AnsibleService():
def __init__(self,filename):
self.ansible = Ansible(filename)
self.playbook = Playbook(pbname='null',pbfacts='no')
self.tasks = Tasks()
self.push_tasks()
self.push_playbook()

def run(self,tasklist,maker):
for task in tasklist :
t = Task(module = task.module,args=task.args)
#如果为脚本 则单独将脚本文件写出来
if task.module == u'script':
script = Script.objects.get(id=int(str(task.args)))
t.args = maker.script_maker(script_id=str(script.id),script=script.formatScript())
self.tasks.push_task(t)
result = self.ansible.run_playbook()
maker.inventory_clear()

return result

def push_makername(self):
return self.maker.filename

def push_tasks(self):
self.playbook.push_tasks(self.tasks)
return

def push_playbook(self):
self.ansible.set_playbook(self.playbook)

def push_callback(self,callback):
self.ansible.set_callback(callback)
# from operation.models import Script
# __metaclass__ = type
# class AnsibleService():
# def __init__(self,filename):
# self.ansible = Ansible(filename)
# self.playbook = Playbook(pbname='null',pbfacts='no')
# self.tasks = Tasks()
# self.push_tasks()
# self.push_playbook()
#
# def run(self,tasklist,maker):
# for task in tasklist :
# t = Task(module = task.module,args=task.args)
# #如果为脚本 则单独将脚本文件写出来
# if task.module == u'script':
# script = Script.objects.get(id=int(str(task.args)))
# t.args = maker.script_maker(script_id=str(script.id),script=script.formatScript())
# self.tasks.push_task(t)
# result = self.ansible.run_playbook()
# maker.inventory_clear()
#
# return result
#
# def push_makername(self):
# return self.maker.filename
#
# def push_tasks(self):
# self.playbook.push_tasks(self.tasks)
# return
#
# def push_playbook(self):
# self.ansible.set_playbook(self.playbook)
#
# def push_callback(self,callback):
# self.ansible.set_callback(callback)
Loading

0 comments on commit 85ee5ec

Please sign in to comment.