From 8f2256d2990d62bedba885cb3609b242559bc38d Mon Sep 17 00:00:00 2001
From: caoyuan <cao.yuan@99cloud.net>
Date: Tue, 16 Jan 2018 12:58:17 +0800
Subject: [PATCH] Optimise the destroy role by script module

Ansible provide script module to run shell script, The local script at
path will be transferred to the remote node and then executed, so no
need to copy script to remote node and use shell moulde to run it.
this patch optimise it.

Change-Id: If774502b66652f25593cda137cc8a5baefbd9695
---
 .../destroy/tasks/cleanup_containers.yml      |  2 +-
 ansible/roles/destroy/tasks/cleanup_host.yml  | 23 ++++++--------
 .../roles/destroy/tasks/cleanup_images.yml    |  2 +-
 ansible/roles/destroy/tasks/copy_tools.yml    | 31 -------------------
 ansible/roles/destroy/tasks/main.yml          |  2 --
 .../destroy/tasks/validate_docker_execute.yml |  2 +-
 6 files changed, 12 insertions(+), 50 deletions(-)
 delete mode 100644 ansible/roles/destroy/tasks/copy_tools.yml

diff --git a/ansible/roles/destroy/tasks/cleanup_containers.yml b/ansible/roles/destroy/tasks/cleanup_containers.yml
index 4ccdd9cdd8..ad7b8af275 100644
--- a/ansible/roles/destroy/tasks/cleanup_containers.yml
+++ b/ansible/roles/destroy/tasks/cleanup_containers.yml
@@ -1,3 +1,3 @@
 ---
 - name: Destroying all Kolla containers and volumes
-  command: /tmp/kolla-cleanup/tools/cleanup-containers
+  script: ../tools/cleanup-containers
diff --git a/ansible/roles/destroy/tasks/cleanup_host.yml b/ansible/roles/destroy/tasks/cleanup_host.yml
index 1ee9958051..92248e7a1d 100644
--- a/ansible/roles/destroy/tasks/cleanup_host.yml
+++ b/ansible/roles/destroy/tasks/cleanup_host.yml
@@ -1,17 +1,12 @@
 ---
 - name: Destroying Kolla host configuration
   become: true
-  command: >
-    env enable_haproxy={{ enable_haproxy }}
-        enable_swift={{ enable_swift }}
-        glance_file_datadir_volume={{ glance_file_datadir_volume }}
-        kolla_internal_vip_address={{ kolla_internal_vip_address }}
-        kolla_external_vip_address={{ kolla_external_vip_address }}
-        kolla_dev_repos_directory={{ kolla_dev_repos_directory }}
-        destroy_include_dev={{ destroy_include_dev }}
-        /tmp/kolla-cleanup/tools/cleanup-host
-
-- name: Destroying kolla-cleanup folder
-  file:
-    path: /tmp/kolla-cleanup
-    state: absent
+  script:  ../tools/cleanup-host
+  environment:
+    enable_haproxy: "{{ enable_haproxy }}"
+    enable_swift: "{{ enable_swift }}"
+    glance_file_datadir_volume: "{{ glance_file_datadir_volume }}"
+    kolla_internal_vip_address: "{{ kolla_internal_vip_address }}"
+    kolla_external_vip_address: "{{ kolla_external_vip_address }}"
+    kolla_dev_repos_directory: "{{ kolla_dev_repos_directory }}"
+    destroy_include_dev: "{{ destroy_include_dev }}"
diff --git a/ansible/roles/destroy/tasks/cleanup_images.yml b/ansible/roles/destroy/tasks/cleanup_images.yml
index ea8f8715c9..8484b7e9c9 100644
--- a/ansible/roles/destroy/tasks/cleanup_images.yml
+++ b/ansible/roles/destroy/tasks/cleanup_images.yml
@@ -1,5 +1,5 @@
 ---
 - name: Removing Kolla images
-  command: /tmp/kolla-cleanup/tools/cleanup-images --all
+  script: ../tools/cleanup-images --all
   when:
     - destroy_include_images | bool
diff --git a/ansible/roles/destroy/tasks/copy_tools.yml b/ansible/roles/destroy/tasks/copy_tools.yml
deleted file mode 100644
index 6a990e197c..0000000000
--- a/ansible/roles/destroy/tasks/copy_tools.yml
+++ /dev/null
@@ -1,31 +0,0 @@
----
-- name: Creating /kolla-cleanup/tools directory on node
-  file:
-    state: directory
-    path: /tmp/kolla-cleanup/tools
-
-- name: Copying validate-docker-execute.sh file
-  copy:
-    src: ../tools/validate-docker-execute.sh
-    dest: /tmp/kolla-cleanup/tools
-    mode: 0755
-
-- name: Copying cleanup-containers file
-  copy:
-    src: ../tools/cleanup-containers
-    dest: /tmp/kolla-cleanup/tools
-    mode: 0755
-
-- name: Copying cleanup-host file
-  copy:
-    src: ../tools/cleanup-host
-    dest: /tmp/kolla-cleanup/tools
-    mode: 0755
-
-- name: Copying cleanup-images file
-  copy:
-    src: ../tools/cleanup-images
-    dest: /tmp/kolla-cleanup/tools
-    mode: 0755
-  when:
-    - destroy_include_images | bool
diff --git a/ansible/roles/destroy/tasks/main.yml b/ansible/roles/destroy/tasks/main.yml
index 0ef20695d8..33c0113649 100644
--- a/ansible/roles/destroy/tasks/main.yml
+++ b/ansible/roles/destroy/tasks/main.yml
@@ -1,6 +1,4 @@
 ---
-- include: copy_tools.yml
-
 - include: validate_docker_execute.yml
 
 - include: cleanup_containers.yml
diff --git a/ansible/roles/destroy/tasks/validate_docker_execute.yml b/ansible/roles/destroy/tasks/validate_docker_execute.yml
index 2839cefe82..0e85832c65 100644
--- a/ansible/roles/destroy/tasks/validate_docker_execute.yml
+++ b/ansible/roles/destroy/tasks/validate_docker_execute.yml
@@ -1,3 +1,3 @@
 ---
 - name: Ensure the docker service is running
-  command: /tmp/kolla-cleanup/tools/validate-docker-execute.sh
+  script: ../tools/validate-docker-execute.sh