Ejemplo n.º 1
0
    def setUp(self):
        inventory = BaseInventory(host_data)
        runner = AdHocRunner(inventory)

        tasks = [
            {
                "action": {
                    "module": "shell",
                    "args": "ip addr"
                },
                "name": "ip_addr"
            },
            {
                "action": {
                    "module": "shell",
                    "args": "systemctl status keepalived"
                },
                "name": "keepalived_ip"
            },
        ]
        ret = runner.run(tasks, "all")
        keepIpList = []
        for x, y in ret.results_raw["ok"].items():
            if re.search('Sending gratuitous ARP',
                         y['keepalived_ip']['stdout'].split('\n')[-1]):
                keepIpList.append([
                    y['keepalived_ip']['stdout'].split('\n')[-1] +
                    "<br/>++<br/>",
                    y['ip_addr']['stdout'].split('2: eth0')[-1].replace(
                        "\n", "<br/>")
                ])

        return keepIpList
Ejemplo n.º 2
0
    def setUp(self):
        host_list = [{
            "hostname": "testserver1",
            "ip": "102.1.1.1",
            "port": 22,
            "username": "******",
            "password": "******",
            "private_key": "/tmp/private_key",
            "become": {
                "method": "sudo",
                "user": "******",
                "pass": None,
            },
            "groups": ["group1", "group2"],
            "vars": {"sexy": "yes"},
        }, {
            "hostname": "testserver2",
            "ip": "8.8.8.8",
            "port": 2222,
            "username": "******",
            "password": "******",
            "private_key": "/tmp/private_key",
            "become": {
                "method": "su",
                "user": "******",
                "pass": "******",
            },
            "groups": ["group3", "group4"],
            "vars": {"love": "yes"},
        }]

        self.inventory = BaseInventory(host_list=host_list)
Ejemplo n.º 3
0
 def setUp(self):
     host_data = [
         {
             "hostname": "testserver",
             "ip": "192.168.244.168",
             "port": 22,
             "username": "******",
             "password": "******",
         },
     ]
     inventory = BaseInventory(host_data)
     self.runner = CommandRunner(inventory)
Ejemplo n.º 4
0
class TestJMSInventory(unittest.TestCase):
    def setUp(self):
        host_list = [{
            "hostname": "testserver1",
            "ip": "192.168.4.192",
            "port": 22,
            "username": "******",
            "password": "******",
            "private_key": "/tmp/private_key",
            "become": {
                "method": "sudo",
                "user": "******",
                "pass": None,
            },
            "groups": ["group1", "group2"],
            "vars": {
                "sexy": "yes"
            },
        }, {
            "hostname": "testserver2",
            "ip": "192.168.4.193",
            "port": 22,
            "username": "******",
            "password": "******",
            "private_key": "/tmp/private_key",
            "become": {
                "method": "su",
                "user": "******",
                "pass": "******",
            },
            "groups": ["group3", "group4"],
            "vars": {
                "love": "yes"
            },
        }]

        self.inventory = BaseInventory(host_list=host_list)

    def test_hosts(self):
        print("#" * 10 + "Hosts" + "#" * 10)
        for host in self.inventory.hosts:
            print(host)

    def test_groups(self):
        print("#" * 10 + "Groups" + "#" * 10)
        for group in self.inventory.groups:
            print(group)

    def test_group_all(self):
        print("#" * 10 + "all group hosts" + "#" * 10)
        group = self.inventory.get_group('all')
        print(group.hosts)
Ejemplo n.º 5
0
    def setUp(self):
        host_list = [{
            "hostname": "testserver1",
            "ip": "192.168.4.192",
            "port": 22,
            "username": "******",
            "password": "******",
            "private_key": "/tmp/private_key",
            "become": {
                "method": "sudo",
                "user": "******",
                "pass": None,
            },
            "groups": ["group1", "group2"],
            "vars": {
                "sexy": "yes"
            },
        }, {
            "hostname": "testserver2",
            "ip": "192.168.4.193",
            "port": 22,
            "username": "******",
            "password": "******",
            "private_key": "/tmp/private_key",
            "become": {
                "method": "su",
                "user": "******",
                "pass": "******",
            },
            "groups": ["group3", "group4"],
            "vars": {
                "love": "yes"
            },
        }]

        self.inventory = BaseInventory(host_list=host_list)
Ejemplo n.º 6
0
class TestJMSInventory(unittest.TestCase):
    def setUp(self):
        host_list = [{
            "hostname": "testserver1",
            "ip": "102.1.1.1",
            "port": 22,
            "username": "******",
            "password": "******",
            "private_key": "/tmp/private_key",
            "become": {
                "method": "sudo",
                "user": "******",
                "pass": None,
            },
            "groups": ["group1", "group2"],
            "vars": {"sexy": "yes"},
        }, {
            "hostname": "testserver2",
            "ip": "8.8.8.8",
            "port": 2222,
            "username": "******",
            "password": "******",
            "private_key": "/tmp/private_key",
            "become": {
                "method": "su",
                "user": "******",
                "pass": "******",
            },
            "groups": ["group3", "group4"],
            "vars": {"love": "yes"},
        }]

        self.inventory = BaseInventory(host_list=host_list)

    def test_hosts(self):
        print("#"*10 + "Hosts" + "#"*10)
        for host in self.inventory.hosts:
            print(host)

    def test_groups(self):
        print("#" * 10 + "Groups" + "#" * 10)
        for group in self.inventory.groups:
            print(group)

    def test_group_all(self):
        print("#" * 10 + "all group hosts" + "#" * 10)
        group = self.inventory.get_group('all')
        print(group.hosts)
Ejemplo n.º 7
0
    def get(self, request, format=None):
        inventory = BaseInventory(host_data)
        runner = AdHocRunner(inventory)

        tasks = [
            {
                "action": {
                    "module": "shell",
                    "args": "ls " + vhost_dir
                },
                "name": "lsvhost"
            },
        ]
        ret = runner.run(tasks, "all")

        return Response(
            ret.results_raw["ok"]["nginx_1"]["lsvhost"]["stdout_lines"])
Ejemplo n.º 8
0
 def setUp(self):
     host_data = [
         {
             "hostname": "testserver",
             "ip": "192.168.1.152",
             "port": 22,
             "username": "******",
             "password": "******",
         },
         {
             "hostname": "testserver2",
             "ip": "192.168.2.220",
             "port": 22,
             "username": "******",
             "password": "******",
         },
     ]
     inventory = BaseInventory(host_data)
     self.runner = CommandRunner(inventory)
Ejemplo n.º 9
0
    def setUp(self):
        host_data = [
            {
                "hostname": "testserver",
                "ip": "10.104.9.133",
                "port": 22,
                "username": "******",
                "password": "******",
            },
        ]

        options = Options(listtags=False,
                          listtasks=False,
                          listhosts=False,
                          syntax=False,
                          timeout=60,
                          connection='ssh',
                          module_path='',
                          forks=10,
                          remote_user='******',
                          private_key_file=None,
                          ssh_common_args="",
                          ssh_extra_args="",
                          sftp_extra_args="",
                          scp_extra_args="",
                          become=None,
                          become_method=None,
                          become_user=None,
                          verbosity=None,
                          extra_vars=[],
                          check=False,
                          playbook_path='/tmp/ping.yml',
                          passwords=None,
                          diff=False,
                          gathering='implicit',
                          remote_tmp='/tmp/.ansible')
        inventory = BaseInventory(host_data)
        self.runner = PlayBookRunner(inventory, options)
Ejemplo n.º 10
0
    def post(self, request, format=None):
        dict_a = {}
        if request:
            post_a = request.data["domain"]

        inventory = BaseInventory(host_data)
        runner = AdHocRunner(inventory)

        tasks = [
            {"action": {"module": "copy", "args": \
            {"src": "/storage/" + example + "/", "dest": vhost_dir + request.data["domain"]}}, \
            "name": "copy" },

            {"action": {"module": "stat", \
            "args": {"path": vhost_dir + request.data["domain"] + "/lobby." + request.data["domain"] + ".conf"}}, \
            "name": "stat_lobby" },

            {"action": {"module": "shell", \
            "args": "for name in `ls " + vhost_dir + request.data["domain"] + "/*" + example + ".conf`;do mv $name ${name%." + example + ".conf}." + request.data["domain"] + ".conf;done"}, \
            "name": "rename_conf" },

            {"action": {"module": "shell", \
            "args": "sed -i 's#" + example + "#" + request.data["domain"] + "#g'  " + vhost_dir + request.data["domain"] + "/*"}, \
            "name": "sed_domain" },

            # game.example.com.conf
            {"action": {"module": "file", \
            "args": {"path": www_dir + "web-desktop" + request.data["domain"], "state": "directory"}}, \
            "name": "file_web-desktop" },

            {"action": {"module": "replace", \
            "args": {"path": vhost_dir + request.data["domain"] + "/game." + request.data["domain"] + ".conf", \
            "regexp": old_www_dir + "web-desktophuihuang", \
            "replace": www_dir + "web-desktop" + request.data["domain"]}}, \
            "name": "create_game" },

            {"action": {"module": "replace", \
            "args": {"path": vhost_dir + request.data["domain"] + "/game." + request.data["domain"] + ".conf", \
            "regexp": old_www_dir + "web-mobile-huihuang", \
            "replace": www_dir + "web-mobile-" + request.data["domain"]}}, \
            "name": "create_game_web-desktop-mobile" },

            # g.example.com.conf
            {"action": {"module": "file", \
            "args": {"path": www_dir + "web-mobile-" + request.data["domain"], "state": "directory"}}, \
            "name": "file_g" },

            {"action": {"module": "replace", \
            "args": {"path": vhost_dir + request.data["domain"] + "/g." + request.data["domain"] + ".conf", \
            "regexp": old_www_dir + "web-mobile-huihuang", "replace": www_dir + "web-mobile-" + request.data["domain"]}}, \
            "name": "create_g" },

            #m.example.com.conf
            {"action": {"module": "file", \
            "args": {"path": www_dir + request.data["domain"] + "/h5", "state": "directory"}}, \
            "name": "file_h5" },

            {"action": {"module": "replace", \
            "args": {"path": vhost_dir + request.data["domain"] + "/m." + request.data["domain"] + ".conf", \
            "regexp": old_www_dir + example + "/h5", "replace": www_dir + request.data["domain"] + "/h5"}}, \
            "name": "create_m" },

            #www.example.com.conf
            {"action": {"module": "file", \
            "args": {"path": www_dir + request.data["domain"] + "/pc", "state": "directory"}}, \
            "name": "file_pc" },

            {"action": {"module": "replace", \
            "args": {"path": vhost_dir + request.data["domain"] + "/www." + request.data["domain"] + ".conf", \
            "regexp": old_www_dir + example + "/pc/", "replace": www_dir + request.data["domain"] + "/pc"}}, \
            "name": "create_pc" },
            # 在nginx.conf 主配文件后面加上 include的目录
            {"action": {"module": "replace", \
            "args": {"path": master_nginx_conf, \
            "regexp": "#headinclude", \
            "replace": "#headinclude\r\n  include vhost/" + request.data["domain"] + "/*.conf;"}}, \
            "name": "replace_nginx" },

            # 最后要一个nginx reload 的动作
            {"action": {"module": "command", \
            "args": nginx_reload }, \
            "name": "nginx_reload" },

            ]

        ret = runner.run(tasks, "all")
        exechost = ret.results_raw["ok"]["nginx_1"]
        okdomain = {
            request.data["domain"]: {
                "stat_lobby": {
                    "conf_file":
                    exechost["stat_lobby"]["invocation"]["module_args"]
                    ["path"],
                },
                "stat_game": {
                    "root_dir":
                    exechost["create_game"]["invocation"]["module_args"]
                    ["replace"],
                    "conf_file":
                    exechost["create_game"]["invocation"]["module_args"]
                    ["path"],
                },
                "stat_g": {
                    "root_dir":
                    exechost["create_g"]["invocation"]["module_args"]
                    ["replace"],
                    "conf_file":
                    exechost["create_g"]["invocation"]["module_args"]["path"],
                },
                "stat_m": {
                    "root_dir":
                    exechost["create_m"]["invocation"]["module_args"]
                    ["replace"],
                    "conf_file":
                    exechost["create_m"]["invocation"]["module_args"]["path"],
                },
                "stat_pc": {
                    "root_dir":
                    exechost["create_pc"]["invocation"]["module_args"]
                    ["replace"],
                    "conf_file":
                    exechost["create_pc"]["invocation"]["module_args"]["path"],
                },
            },
        }
        return Response(okdomain)
Ejemplo n.º 11
0
import unittest
import sys

sys.path.insert(0, "../..")

from ops.ansible.runner import CopyAdHocRunner, CopyRunner
from ops.ansible.inventory import BaseInventory

host_data = [
    {
        "hostname": "testserver",
        "ip": "192.168.1.152",
        "port": 22,
        "username": "******",
        "password": "******",
    },
    {
        "hostname": "testserver2",
        "ip": "192.168.2.220",
        "port": 22,
        "username": "******",
        "password": "******",
    },
]
inventory = BaseInventory(host_data)
runner = CopyRunner(inventory)

cmd = "src=/tmp/test2.py dest=/tmp/"
res = runner.mycopy(cmd, 'all')
print(res.results_command)