forked from YoLoveLife/DevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request YoLoveLife#39 from YoLoveLife/new-api
remove old api&put the new api in
- Loading branch information
Showing
74 changed files
with
925 additions
and
90,709 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,4 @@ def ddr(): | |
import time | ||
host = Host.objects.all()[0] | ||
host.info = str(time.time()) | ||
host.save() | ||
host.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.