version: '2.0'

purr.create_vm_for_contestant:
    description: Create VM for the contestant.

    input:
        - contestant

    vars:
        username: odie

    output:
        address: <% $.address %>
        username: <% $.username %>
        password: <% $.password %>

    tasks:
        create_instance:
            action: purr.create_vm
            publish:
                address: <% task(create_instance).result.instance.ip_address %>
            input:
                tags: "Name=<% $.contestant %>"
            on-success:
                - gen_pass
        gen_pass:
            action: core.local
            input:
                cmd: "openssl rand -base64 32"
            publish:
                password: <% task(gen_pass).result.stdout %>
            on-success:
                - add_user
        add_user:
            action: purr.add_ssh_user
            input:
                hosts: <% $.address %>
                new_username: <% $.username %>
                new_password: <% $.password %>
                

version: '2.0'

purr.create_vm_for_contestant:
    description: Create VM for the contestant.

    input:
        - contestant

    vars:
        username: odie
        instance_id: null

    output:
        address: <% $.address %>
        username: <% $.username %>
        password: <% $.password %>

    tasks:
        create_instance:
            action: purr.create_vm
            publish:
                address: <% task(create_instance).result.instance.ip_address %>
                instance_id: <% task(create_instance).result.instance.id %>
            input:
                tags: "Name=<% $.contestant %>"
            on-success:
                - gen_pass
        gen_pass:
            action: core.local
            input:
                cmd: "openssl rand -base64 32"
            publish:
                password: <% task(gen_pass).result.stdout %>
            on-success:
                - add_user
            on-error:
                - destroy_vm
        add_user:
            action: purr.add_ssh_user
            input:
                hosts: <% $.address %>
                new_username: <% $.username %>
                new_password: <% $.password %>
            on-success:
                - set_tracker
            on-error:
                - destroy_vm
        set_tracker:
            action: st2.kv.set
            input:
                key: <% $.contestant + '.vm.id' %>
                value: <% $.instance_id %>
            on-error:
                - destroy_vm



        destroy_vm:
            action: purr.destroy_vm
            input:
                instance_id: <% $.instance_id %>

Add step to destroy VM on failure
Add step to destroy VM on failure
