[libvirt] [libvirt-jenkins-ci PATCH 2/5] ansible: Introduce the 'manage' tool

Andrea Bolognani posted 5 patches 7 years, 7 months ago
[libvirt] [libvirt-jenkins-ci PATCH 2/5] ansible: Introduce the 'manage' tool
Posted by Andrea Bolognani 7 years, 7 months ago
This script replaces the existing Makefile, and will be extended
to provide more functionality in future commits.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
---
 ansible/Makefile |  9 ---------
 ansible/manage   | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 9 deletions(-)
 delete mode 100644 ansible/Makefile
 create mode 100755 ansible/manage

diff --git a/ansible/Makefile b/ansible/Makefile
deleted file mode 100644
index 6af7ae3..0000000
--- a/ansible/Makefile
+++ /dev/null
@@ -1,9 +0,0 @@
-all:
-
-site:
-	@ansible-playbook site.yml
-
-clean:
-	@rm -f *.retry log
-
-.PHONY: all site clean
diff --git a/ansible/manage b/ansible/manage
new file mode 100755
index 0000000..46bec6c
--- /dev/null
+++ b/ansible/manage
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+PROGRAM_NAME="$0"
+
+# -------------------
+#  Utility functions
+# -------------------
+
+# die MESSAGE
+#
+# Abort the program after displaying $MESSAGE on standard error.
+die() {
+    echo "$1" >&2
+    exit 1
+}
+
+# ----------------------
+#  User-visible actions
+# ----------------------
+
+do_help() {
+    echo "\
+Usage: $PROGRAM_NAME ACTION [OPTIONS]
+
+Actions:
+  list             List known guests
+  prepare GUEST    Prepare or update GUEST. Can be run as many times as needed
+  update GUEST     Alias for prepare
+  help             Display this help"
+}
+
+do_list() {
+    INVENTORY=$(grep "^inventory\\s*=" ansible.cfg 2>/dev/null | sed "s/^.*=\\s*//g")
+    INVENTORY=${INVENTORY:-/etc/ansible/hosts}
+
+    grep -v '^\[' "$INVENTORY" | sort -u
+}
+
+do_prepare() {
+    GUEST="$1"
+
+    test "$GUEST" || {
+        die "Usage: $PROGRAM_NAME prepare GUEST"
+    }
+    do_list | grep -q "$GUEST" || {
+        die "$PROGRAM_NAME: $GUEST: Unknown guest"
+    }
+
+    ansible-playbook -l "$GUEST" site.yml
+}
+
+case "$1" in
+    list)           do_list ;;
+    prepare|update) do_prepare "$2" ;;
+    *help)          do_help ;;
+    *)              die "Usage: $PROGRAM_NAME ACTION [OPTIONS]" ;;
+esac
-- 
2.13.6

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [libvirt-jenkins-ci PATCH 2/5] ansible: Introduce the 'manage' tool
Posted by Pavel Hrdina 7 years, 6 months ago
On Mon, Oct 16, 2017 at 06:02:05PM +0200, Andrea Bolognani wrote:
> This script replaces the existing Makefile, and will be extended
> to provide more functionality in future commits.
> 
> Signed-off-by: Andrea Bolognani <abologna@redhat.com>
> ---
>  ansible/Makefile |  9 ---------
>  ansible/manage   | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 57 insertions(+), 9 deletions(-)
>  delete mode 100644 ansible/Makefile
>  create mode 100755 ansible/manage
> 
> diff --git a/ansible/Makefile b/ansible/Makefile
> deleted file mode 100644
> index 6af7ae3..0000000
> --- a/ansible/Makefile
> +++ /dev/null
> @@ -1,9 +0,0 @@
> -all:
> -
> -site:
> -	@ansible-playbook site.yml
> -
> -clean:
> -	@rm -f *.retry log
> -
> -.PHONY: all site clean
> diff --git a/ansible/manage b/ansible/manage
> new file mode 100755
> index 0000000..46bec6c
> --- /dev/null
> +++ b/ansible/manage
> @@ -0,0 +1,57 @@
> +#!/bin/sh
> +
> +PROGRAM_NAME="$0"
> +
> +# -------------------
> +#  Utility functions
> +# -------------------
> +
> +# die MESSAGE
> +#
> +# Abort the program after displaying $MESSAGE on standard error.
> +die() {
> +    echo "$1" >&2
> +    exit 1
> +}
> +
> +# ----------------------
> +#  User-visible actions
> +# ----------------------
> +
> +do_help() {
> +    echo "\
> +Usage: $PROGRAM_NAME ACTION [OPTIONS]
> +
> +Actions:
> +  list             List known guests
> +  prepare GUEST    Prepare or update GUEST. Can be run as many times as needed
> +  update GUEST     Alias for prepare
> +  help             Display this help"
> +}
> +
> +do_list() {
> +    INVENTORY=$(grep "^inventory\\s*=" ansible.cfg 2>/dev/null | sed "s/^.*=\\s*//g")
> +    INVENTORY=${INVENTORY:-/etc/ansible/hosts}

I don't think that there is a need to include system-wide inventory
since we have our own inventory and we don't use the system-wide
inventory at all.

> +
> +    grep -v '^\[' "$INVENTORY" | sort -u
> +}
> +
> +do_prepare() {
> +    GUEST="$1"
> +
> +    test "$GUEST" || {
> +        die "Usage: $PROGRAM_NAME prepare GUEST"
> +    }
> +    do_list | grep -q "$GUEST" || {
> +        die "$PROGRAM_NAME: $GUEST: Unknown guest"
> +    }
> +
> +    ansible-playbook -l "$GUEST" site.yml
> +}
> +
> +case "$1" in
> +    list)           do_list ;;
> +    prepare|update) do_prepare "$2" ;;
> +    *help)          do_help ;;
> +    *)              die "Usage: $PROGRAM_NAME ACTION [OPTIONS]" ;;

How about grouping *help) and *) together?  I was pretending to be a
basic user and I've run this script without any option at all and this
was the only output.  So it would be nice to print the help itself
or mention that there is some "--help" option.

> +esac

Pavel
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [libvirt-jenkins-ci PATCH 2/5] ansible: Introduce the 'manage' tool
Posted by Andrea Bolognani 7 years, 6 months ago
On Tue, 2017-10-17 at 15:21 +0200, Pavel Hrdina wrote:
> > +do_list() {
> > +    INVENTORY=$(grep "^inventory\\s*=" ansible.cfg 2>/dev/null | sed "s/^.*=\\s*//g")
> > +    INVENTORY=${INVENTORY:-/etc/ansible/hosts}
> 
> I don't think that there is a need to include system-wide inventory
> since we have our own inventory and we don't use the system-wide
> inventory at all.

Good point. I turned the absence of the local inventory file into
an error, and...

> > +    grep -v '^\[' "$INVENTORY" | sort -u

... improved the regular expression so that empty lines and comments
will be ignored in addition to group names.

> > +case "$1" in
> > +    list)           do_list ;;
> > +    prepare|update) do_prepare "$2" ;;
> > +    *help)          do_help ;;
> > +    *)              die "Usage: $PROGRAM_NAME ACTION [OPTIONS]" ;;
> 
> How about grouping *help) and *) together?  I was pretending to be a
> basic user and I've run this script without any option at all and this
> was the only output.  So it would be nice to print the help itself
> or mention that there is some "--help" option.

I went one step further and dropped the command-specific error
message in favor of displaying the help text. One less string, and
it's actually more informative :)

-- 
Andrea Bolognani / Red Hat / Virtualization

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list