Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing latest version for linux #1

Merged
merged 2 commits into from
Sep 1, 2020

Conversation

elreydetoda
Copy link
Contributor

Hey @diodonfrost, I just wanted to submit this code (probably need to adapt it to whatever format you want), because I noticed a flaw with the sorting mechanism on Linux. We are currently on 2.2.10 release of vagrant, and you script was grabbing 2.2.9. So, it is a bit hacky, but I think it is giving the best results possible. I tried to solve this a more "ansible" way, but I couldn't figure out a solution to handle it properly.

here is a quick example of why/how it doesn't work

asciicast

the set command comes from this (my blog): https://blog.elreydetoda.site/cool-shell-tricks/#bashscriptingmodifiedscripthardening and helps prevent thing from being undefined and more explanation in my blog post.

this assigns everything from stdin to an array value, and I got it from here: https://github.com/koalaman/shellcheck/wiki/SC2207
IFS=" " read -r -a arrayzz

then the next line reads in that previously assigned array and sorts the values in the array with the latest value being at the top. So, then at the end you print out the first value in the array. which should be the latest release. got it from here, and modified it a bit to suit my sorting needs. Better explained here

IFS=$'\n' sorted=($( sort -t '.' -k1,1nr -k2,2nr -k3,3nr <<<"${arrayzz[*]}" ))

then you just print out that version so it gets put in the register

@diodonfrost diodonfrost self-assigned this Aug 30, 2020
@diodonfrost
Copy link
Owner

Thanks for the contribution.
It does need quite some work. If you can't find the time to follow-up, I will do it for you later.
I think it's better to use a custom python filter_plugins to sort complex version.

It can be done like this:

./filter_plugins/sort_versions.py

"""Sort complex versions"""

from distutils.version import LooseVersion


def filter_sort_versions(value):
    """
        Ansible entrypoint function
    """
    return sorted(value, key=LooseVersion)


class FilterModule(object):
    """
        Sort complex versions like 0.10.2, 0.1.1, 0.10.12
    """
    filter_sort = {
        'sort_versions': filter_sort_versions,
    }

    def filters(self):
        """
            Return the sorted values
        """
        return self.filter_sort

Modify tasks to sort vagrant versions with plugins sort_versions:

./tasks/setup-Linux.yml

- name: Linux | Find all versions of Vagrant
  uri:
    url: https://releases.hashicorp.com/vagrant/index.json
    return_content: yes
  register: vagrant_index
  when: vagrant_version == 'latest'

- name: Linux | Finds the latest version of Vagrant when latest var is define
  set_fact:
    vagrant_version_to_install: "{{ (vagrant_index.content | from_json).versions | list | sort_versions | last }}"
  when: vagrant_version == 'latest'

@elreydetoda
Copy link
Contributor Author

Sounds good, I will try and get that sent over before end of business tomorrow.

@elreydetoda
Copy link
Contributor Author

looks to be working properly, after the Travis CI step succeeds it should be good to go.

@diodonfrost
Copy link
Owner

Hi @elreydetoda , your code has been merged into the master branch.

Thanks for your help!

@diodonfrost diodonfrost merged commit 0aeb09b into diodonfrost:master Sep 1, 2020
@elreydetoda
Copy link
Contributor Author

No problem @diodonfrost , thank you for the work on the role in the first place and for working with me 😁

I plan to use it in one of my personal projects, so again thank you 🙏🎉

@elreydetoda elreydetoda deleted the patch-1 branch September 2, 2020 01:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants