Example #1
0
class TestAdHocRunner(unittest.TestCase):
    def setUp(self):
        host_data = [
            {
                "hostname": "testserver",
                "ip": "192.168.244.168",
                "port": 22,
                "username": "******",
                "password": "******",
            },
        ]
        inventory = BaseInventory(host_data)
        self.runner = AdHocRunner(inventory)

    def test_run(self):
        tasks = [
            {
                "action": {
                    "module": "shell",
                    "args": "ls"
                },
                "name": "run_cmd"
            },
            {
                "action": {
                    "module": "shell",
                    "args": "whoami"
                },
                "name": "run_whoami"
            },
        ]
        ret = self.runner.run(tasks, "all")
        print(ret.results_summary)
        print(ret.results_raw)
Example #2
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
Example #3
0
def exec_script(request):
    assets_list = request.POST.getlist('assets')
    asset_groups_list = request.POST.getlist('asset_groups')
    script = request.POST.get('script')
    hosts, assets, asset_groups = get_hosts(assets_list, asset_groups_list)
    runner = AdHocRunner(hosts, forks=5)
    ret = runner.run(task_tuple=[('script', script)])
    print(ret)
def run_adhoc(task_name, tasks, inventory):
    """
    :param task_name
    :param tasks
    :param inventory
    """
    from ops.ansible.runner import AdHocRunner
    runner = AdHocRunner(inventory, options=const.TASK_OPTIONS)
    result = runner.run(tasks, 'all', task_name)
    return result.results_raw, result.results_summary
Example #5
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"])
Example #6
0
def exec_shell(request):
    assets_list = request.POST.getlist('assets')
    asset_groups_list = request.POST.getlist('asset_groups')
    shell = request.POST.get('shell')
    print(assets_list, asset_groups_list, shell)
    user = User.objects.get(username=request.user)
    hosts, assets, asset_groups = get_hosts(assets_list, asset_groups_list)

    task_tuple = (('shell', shell),)
    hoc = AdHocRunner(hosts=hosts)
    hoc.results_callback = CommandResultCallback()
    ret = hoc.run(task_tuple)
    bash_exec = AnsibleExecShellLog.objects.create(
        user=user,
        shell=shell,
        assets=assets,
        asset_groups=asset_groups,
        system_user='******',
        result=json.dumps(ret),
    )
    return ret
Example #7
0
class TestAdHocRunner(unittest.TestCase):
    def setUp(self):
        host_data = [
            {
                "hostname": "testserver",
                "ip": "192.168.244.168",
                "port": 22,
                "username": "******",
                "password": "******",
            },
        ]
        inventory = BaseInventory(host_data)
        self.runner = AdHocRunner(inventory)

    def test_run(self):
        tasks = [
            {"action": {"module": "shell", "args": "ls"}, "name": "run_cmd"},
            {"action": {"module": "shell", "args": "whoami"}, "name": "run_whoami"},
        ]
        ret = self.runner.run(tasks, "all")
        print(ret.results_summary)
        print(ret.results_raw)
Example #8
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)