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

Version auto discovery #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
---
go_tarball: "go1.7.1.linux-amd64.tar.gz"
go_tarball_checksum: "sha256:43ad621c9b014cde8db17393dc108378d37bc853aa351a6c74bf6432c1bbd182"
go_version_target: "go version go1.7.1 linux/amd64"
set_go_path: true
# Set download location to an overridable var if you want to use an internal mirror
#go_download_location: "https://storage.googleapis.com/golang/{{ go_tarball }}"

# go_version, go_tarball, and its checksum can be set by a deployer to
# force a specific version.
# if they are ALL not set, version will be autodiscovered.
# the version will be found thanks to github releases.
# the shasum will be fetched from "{{ go_download_location }}/{{ go_tarball }}.sha256"
#go_version: "1.7.1"
#go_tarball: "go{{ go_version }}.linux-amd64.tar.gz"
#go_tarball_checksum: "sha256:43ad621c9b014cde8db17393dc108378d37bc853aa351a6c74bf6432c1bbd182"
go_version_target: "go version go{{ go_version }} linux/amd64"
set_go_path: true
30 changes: 26 additions & 4 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
---
- name: Discover the latest version
shell:
curl https://api.github.com/repos/golang/go/git/refs/tags/ | egrep 'ref.*refs/tags/go([0-9.]+)",' | egrep -o "[0-9.]+" | sort | tail -n 1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just did something similar using curl and jq.

Example: curl -L https://api.github.com/repos/golang/go/tags | jq '[ .[].name ] | sort | reverse | .[0]' --raw-output

You could refactor that and use the Ansible module get_url and Jinja filters like from_json and so on therefor avoiding the use of the shell 😉

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would be more elegant indeed.
jq is absent on my machine, and I didn't want to introduce a dependency. It's not really more readable either.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The jq usage was just an example 😉 The same can be done with Jinja filters in Ansible.

register: latest_release
changed_when: False
when:
- not go_tarball is defined
- not go_tarball_checksum is defined
- not go_download_location is defined
Copy link

@ypid ypid Oct 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is bad style according to PEP8 and bad English. Consider changing to is not or is undefined.


- name: Setting facts for autodiscovered version
set_fact:
go_version: "{{ latest_release.stdout_lines[0] }}"
go_tarball: "go{{ latest_release.stdout_lines[0] }}.linux-amd64.tar.gz"
when: not latest_release | skipped

- name: Setting facts based on previous autodiscovered facts
set_fact:
go_tarball_checksum: "sha256:{{ lookup('url',go_download_buckets_url+go_tarball+'.sha256') }}"
go_download_location: "{{ go_download_buckets_url }}{{ go_tarball }}"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checksum and tarball are downloaded from the same origin which basically now only ensures that CRC checksums in TCP did not fail. Consider using OpenGPG to check authenticity of the downloaded tarball.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I'm ensuring everything went fine through the wire.

I don't see the difference between manually checking the SHA on the website and doing the checksum on your machine to see if it was properly downloaded.

Could you be more clear about this?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refer to the debops.hashicorp for how this should be done (using OpenPGP).

when: not latest_release | skipped

- name: Download the Go tarball
get_url:
url: "{{ go_download_location }}"
dest: /usr/local/src/{{ go_tarball }}
dest: "/usr/local/src/"
checksum: "{{ go_tarball_checksum }}"

- name: Register the current Go version (if any)
command: /usr/local/go/bin/go version
ignore_errors: yes
register: go_version
register: current_go_version
changed_when: false

- name: Remove old installation of Go
file:
path: /usr/local/go
state: absent
when: go_version|failed or go_version.stdout != go_version_target
when: current_go_version|failed or current_go_version.stdout != go_version_target

- name: Extract the Go tarball if Go is not yet installed or not the desired version
unarchive:
src: /usr/local/src/{{ go_tarball }}
dest: /usr/local
copy: no
when: go_version|failed or go_version.stdout != go_version_target
when: current_go_version|failed or current_go_version.stdout != go_version_target

- name: Add the Go bin directory to the PATH environment variable for all users
copy:
Expand Down
2 changes: 1 addition & 1 deletion vars/main.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
go_download_location: "https://storage.googleapis.com/golang/{{ go_tarball }}"
go_download_buckets_url: "https://storage.googleapis.com/golang/"