From 67193dc90f691872495c25c90c2141586334ba43 Mon Sep 17 00:00:00 2001 From: Tim Petter Date: Tue, 23 Jun 2015 12:33:27 +0200 Subject: [PATCH] Add posibility to install go from source --- .kitchen.yml | 10 ++++++++++ README.md | 30 ++++++++++++++++++++++++++++++ attributes/default.rb | 8 ++++++++ recipes/default.rb | 39 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 86 insertions(+), 1 deletion(-) diff --git a/.kitchen.yml b/.kitchen.yml index b2341a5..22b1ca3 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -19,3 +19,13 @@ suites: go: owner: 'vagrant' group: 'vagrant' +- name: src + run_list: + - recipe[golang_test::default] + - recipe[minitest-handler] + attributes: + go: + from_source: true + arch: 'amd64' + owner: 'vagrant' + group: 'vagrant' diff --git a/README.md b/README.md index 9ea2b16..b491e55 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,36 @@ To install Go packages using node attributes, include `golang::packages` in your The mode of $GOPATH 0755 + + ['go']['from_source'] + Boolean + Install go from source + false + + + ['go']['os'] + String + Build go for which operating system + linux + + + ['go']['arch'] + String + Build go for which architecture + arm + + + ['go']['arm'] + String + Build go for which arm version + 6 + + + ['go']['source_method'] + String + Choose which install script should be used + all.bash + ## Testing diff --git a/attributes/default.rb b/attributes/default.rb index 4033edb..6d2d04d 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -1,6 +1,14 @@ default['go']['version'] = '1.4' default['go']['platform'] = node['kernel']['machine'] =~ /i.86/ ? '386' : 'amd64' default['go']['filename'] = "go#{node['go']['version']}.#{node['os']}-#{node['go']['platform']}.tar.gz" +default['go']['from_source'] = false +if node['go']['from_source'] + default['go']['filename'] = "go#{node['go']['version']}.src.tar.gz" + default['go']['os'] = 'linux' + default['go']['arch'] = 'arm' + default['go']['arm'] = '6' + default['go']['source_method'] = "all.bash" +end default['go']['url'] = "http://golang.org/dl/#{node['go']['filename']}" default['go']['install_dir'] = '/usr/local' default['go']['gopath'] = '/opt/go' diff --git a/recipes/default.rb b/recipes/default.rb index fb632b5..bbf67a6 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -22,16 +22,53 @@ code <<-EOH rm -rf go rm -rf #{node['go']['install_dir']}/go - tar -C #{node['go']['install_dir']} -xzf #{node["go"]["filename"]} + tar -C #{node['go']['install_dir']} -xzf #{node['go']['filename']} EOH + not_if { node['go']['from_source'] } action :nothing end +bash "build-golang" do + cwd Chef::Config[:file_cache_path] + code <<-EOH + rm -rf go + rm -rf #{node['go']['install_dir']}/go + tar -C #{node['go']['install_dir']} -xzf #{node['go']['filename']} + cd #{node['go']['install_dir']}/go/src + mkdir -p $GOBIN + ./#{node['go']['source_method']} + EOH + environment ({ + 'GOROOT' => "#{node['go']['install_dir']}/go", + 'GOBIN' => '$GOROOT/bin', + 'GOOS' => node['go']['os'], + 'GOARCH' => node['go']['arch'], + 'GOARM' => node['go']['arm'] + }) + only_if { node['go']['from_source'] } + action :nothing +end + +if node['go']['from_source'] + case node["platform"] + when 'debian', 'ubuntu' + packages = %w(build-essential) + when 'redhat', 'centos', 'fedora' + packages = %w(gcc glibc-devel) + end + packages.each do |dev_package| + package dev_package do + action :install + end + end +end + remote_file File.join(Chef::Config[:file_cache_path], node['go']['filename']) do source node['go']['url'] owner 'root' mode 0644 notifies :run, 'bash[install-golang]', :immediately + notifies :run, 'bash[build-golang]', :immediately not_if "#{node['go']['install_dir']}/go/bin/go version | grep \"go#{node['go']['version']} \"" end