Add golang cookbook

Needs updating of ark and git cookbooks
This commit is contained in:
2020-08-23 11:01:05 +02:00
parent 217ab471ce
commit 0005f9ab7d
55 changed files with 670 additions and 716 deletions

View File

@@ -0,0 +1,98 @@
# CHANGELOG for golang
This file is used to list changes made in each version of golang.
## 4.1.0
- Create a custom resource to install Go from source or by downloading a package
## 4.0.0
- Update testing setup:
- Use Chef Workstation instead of Rubygems
- Includes dropping `foodcritic` in favor of current `cookstyle`
- Add more recent & current distro versions
- Add very basic unit tests
- Use GitHub Actions instead of Travis CI
- Refactor minitest to Inspec
- Refactor to use current style of Chef Custom Resource
- Update default version to Go v1.14.4
- Use `build_essential` when building from source
- Requires Chef >= 14.0.0
- Omit attempting to install `bzr` on CentOS 8, they do not have that package available
- Utilize default Chef order-of-operations instead of `notifies`
- Use `ark` resource to simplify download & installation of binary & source
- Clean up unnecessary env vars from source build
- Build from source per [official Go docs](https://golang.org/doc/install/source)
- Use native resources instead of shell commands when building from source
- Set `$PATH` in `golang.sh` so that the existing values are last, per general practice
- Tighten up `golang_package` so it does not perform actions at compile time, it uses `execute` in favor of `bash` resources, and in general is more Chef-y
- Update tests to validate that non-root users can install Go and Go packages
- Refactor attributes to add ability to install a version from source that differs from the packaged version (which is needed to build from source)
## 3.0.0
- rename to `chef-golang` so that metadata name matches repositoriy name
## 2.0.0
- change attribute namespace to `node['golang']`
## 1.7.2
- Better chef12,13 support
- Ruby 2.3
- Update gems, lint
## 1.7.1
- Create a temporary directory
## 1.7.0
- Golang 1.5
## 1.6.2
- Ability to install from source
## 1.6.0
- Removed support for Ruby 1.9.3
## 1.5.1
- Fix testing with Vagrant, Test-Kitchen
- Golang 1.4
## 1.5.0
- Golang 1.3
## 1.4.0
- Add build action to LWRP
- Update default go version to 1.2.2
- Add autodetection the platform architecture
- Change package location to `http://golang.org/dl/`
## 1.3.0
## 1.2.0
## 1.1.0
- Added package LWRP
- Configurable `gopath` & `gobin`
## 1.0.2
- Lets users easily specify another install dir
## 1.0.1
- Avoid extra unpacked copy of Go
## 1.0.0
- Initial release of golang

View File

@@ -0,0 +1,4 @@
# Contributing
Please refer to
[https://github.com/chef-cookbooks/community_cookbook_documentation/blob/master/CONTRIBUTING.MD](https://github.com/chef-cookbooks/community_cookbook_documentation/blob/master/CONTRIBUTING.MD)

View File

@@ -0,0 +1,77 @@
# Golang (Go) Cookbook
[![Cookbook Version](https://img.shields.io/cookbook/v/golang.svg?style=flat)](https://supermarket.chef.io/cookbooks/golang)
[![CI State](https://github.com/sous-chefs/golang/workflows/ci/badge.svg)](https://github.com/sous-chefs/golang/actions?query=workflow%3Aci)
[![OpenCollective](https://opencollective.com/sous-chefs/backers/badge.svg)](#backers)
[![OpenCollective](https://opencollective.com/sous-chefs/sponsors/badge.svg)](#sponsors)
[![License](https://img.shields.io/badge/License-Apache%202.0-green.svg)](https://opensource.org/licenses/Apache-2.0)
## Description
Chef cookbook for the [Go programming language](http://golang.org/).
## Requirements
### Platform
* Ubuntu >= 18.04
* Debian >= 8
* CentOS >= 7
## Usage
### golang::default
Include `golang` in your node's `run_list`:
```json
{
"name":"my_node",
"run_list": [
"recipe[golang]"
]
}
```
### golang::packages
To install Go packages using node attributes add the packages to the `['golang']['packages']` attribute:
```json
{
"name":"my_node",
"golang": {
"packages": [
"github.com/go-check/check"
]
},
"run_list": [
"recipe[golang]"
]
}
```
## Contributors
This project exists thanks to all the people who [contribute.](https://opencollective.com/sous-chefs/contributors.svg?width=890&button=false)
### Backers
Thank you to all our backers!
![https://opencollective.com/sous-chefs#backers](https://opencollective.com/sous-chefs/backers.svg?width=600&avatarHeight=40)
### Sponsors
Support this project by becoming a sponsor. Your logo will show up here with a link to your website.
![https://opencollective.com/sous-chefs/sponsor/0/website](https://opencollective.com/sous-chefs/sponsor/0/avatar.svg?avatarHeight=100)
![https://opencollective.com/sous-chefs/sponsor/1/website](https://opencollective.com/sous-chefs/sponsor/1/avatar.svg?avatarHeight=100)
![https://opencollective.com/sous-chefs/sponsor/2/website](https://opencollective.com/sous-chefs/sponsor/2/avatar.svg?avatarHeight=100)
![https://opencollective.com/sous-chefs/sponsor/3/website](https://opencollective.com/sous-chefs/sponsor/3/avatar.svg?avatarHeight=100)
![https://opencollective.com/sous-chefs/sponsor/4/website](https://opencollective.com/sous-chefs/sponsor/4/avatar.svg?avatarHeight=100)
![https://opencollective.com/sous-chefs/sponsor/5/website](https://opencollective.com/sous-chefs/sponsor/5/avatar.svg?avatarHeight=100)
![https://opencollective.com/sous-chefs/sponsor/6/website](https://opencollective.com/sous-chefs/sponsor/6/avatar.svg?avatarHeight=100)
![https://opencollective.com/sous-chefs/sponsor/7/website](https://opencollective.com/sous-chefs/sponsor/7/avatar.svg?avatarHeight=100)
![https://opencollective.com/sous-chefs/sponsor/8/website](https://opencollective.com/sous-chefs/sponsor/8/avatar.svg?avatarHeight=100)
![https://opencollective.com/sous-chefs/sponsor/9/website](https://opencollective.com/sous-chefs/sponsor/9/avatar.svg?avatarHeight=100)

View File

@@ -0,0 +1,15 @@
#
# Cookbook:: golang
# Attribute:: default
#
default['golang']['scm'] = true
default['golang']['scm_packages'] = value_for_platform(
'centos' => { '>= 8' => %w(git mercurial) },
'default' => %w(git mercurial bzr)
)
default['golang']['install_dir'] = '/usr/local'
default['golang']['gopath'] = '/opt/go'
default['golang']['gobin'] = '/opt/go/bin'
default['golang']['packages'] = []

View File

@@ -0,0 +1 @@
{"name":"golang","version":"4.1.0","description":"Installs go programming language","long_description":"","maintainer":"Sous Chefs","maintainer_email":"help@sous-chefs.org","license":"Apache-2.0","platforms":{"debian":">= 0.0.0","ubuntu":">= 0.0.0","redhat":">= 0.0.0","centos":">= 0.0.0","fedora":">= 0.0.0","amazon":">= 0.0.0","scientific":">= 0.0.0","oracle":">= 0.0.0"},"dependencies":{"ark":"~> 5.0"},"recommendations":{},"suggestions":{},"conflicting":{},"providing":{},"replacing":{},"attributes":{},"groupings":{},"recipes":{},"source_url":"https://github.com/sous-chefs/golang","issues_url":"https://github.com/sous-chefs/golang/issues","chef_version":[[">= 14.0"]],"ohai_version":[]}

View File

@@ -0,0 +1,20 @@
name 'golang'
maintainer 'Sous Chefs'
maintainer_email 'help@sous-chefs.org'
license 'Apache-2.0'
description 'Installs go programming language'
source_url 'https://github.com/sous-chefs/golang'
issues_url 'https://github.com/sous-chefs/golang/issues'
chef_version '>= 14.0'
version '4.1.0'
supports 'debian'
supports 'ubuntu'
supports 'redhat'
supports 'centos'
supports 'fedora'
supports 'amazon'
supports 'scientific'
supports 'oracle'
depends 'ark', '~> 5.0'

View File

@@ -0,0 +1,30 @@
#
# Cookbook:: golang
# Recipe:: default
#
# Copyright:: 2013, Alexander Rozhnov
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
golang 'Install go' do
from_source node['golang']['from_source']
version node['golang']['version'] if node['golang']['version'] # go version
source_version node['golang']['source_version'] if node['golang']['source_version'] # go version from source
owner node['golang']['owner'] if node['golang']['owner']
group node['golang']['group'] if node['golang']['group']
end
node['golang']['packages'].each do |package|
golang_package package
end

View File

@@ -0,0 +1,167 @@
#
# Cookbook:: golang
# Resource:: default
#
# Copyright:: 2020, Sous-chefs
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Install golang by compiling from source
property :from_source, [true, false], default: false
# Base directory for go
property :install_dir, String, default: '/usr/local'
property :gopath, String, default: '/opt/go'
property :gobin, String, default: '/opt/go/bin'
# Directory and file attributes
property :owner, String, default: 'root'
property :group, String, default: 'root'
property :directory_mode, String, default: '0755'
# version to install
# ark input binaries and stuff download
# e.g. https://dl.google.com/go/go1.14.4.linux-amd64.tar.gz
# https://dl.google.com/go/go1.14.4/linux-amd64.tar.gz
# https://golang.org/dl/go1.14.6.darwin-amd64.tar.gz
# https://golang.org/dl/go1.14.6.linux-amd64.tar.gz
property :url, String, default: 'https://golang.org/dl/goVERSION.OS-PLATFORM.tar.gz'
property :version, String, default: '1.14.4'
# ark input source
# e.g. https://dl.google.com/go/go1.14.4.src.tar.gz
property :source_url, String, default: 'https://golang.org/dl/goVERSION.src.tar.gz'
property :source_version, String, default: '1.14.4'
# installation script from the source repository
property :source_method, String, default: 'all.bash'
# install SCM packages
property :scm, [true, false], default: true
property :scm_packages, [String, Array], default: %w(git mercurial)
action_class do
def bin_url
return new_resource.url if property_is_set?(:url)
new_resource.url.sub(/PLATFORM/, platform).sub(/OS/, os).sub(/VERSION/, new_resource.version)
end
def source_url
return new_resource.source_url if property_is_set?(:source_url)
new_resource.source_url.sub(/VERSION/, new_resource.source_version)
end
def os
node['os']
end
def platform
node['kernel']['machine'] =~ /i.86/ ? '386' : 'amd64'
end
end
action :install do
directory new_resource.gopath do
recursive true
owner new_resource.owner
group new_resource.group
mode new_resource.directory_mode
end
directory new_resource.gobin do
recursive true
owner new_resource.owner
group new_resource.group
mode new_resource.directory_mode
end
directory new_resource.install_dir do
recursive true
mode new_resource.directory_mode
end
directory ::File.join(Chef::Config[:file_cache_path], 'go') do
recursive true
end
template '/etc/profile.d/golang.sh' do
source 'golang.sh.erb'
mode new_resource.directory_mode
variables gobin: new_resource.gobin,
gopath: new_resource.gobin,
install_dir: new_resource.install_dir
end
if new_resource.scm
apt_update do
only_if { platform_family? 'debian' }
end
package new_resource.scm_packages
end
# pgk to install go binaries, doc, base packages
# ark is not idempotent by itself. Check to see if the go binary is the correct version
url = bin_url
ark 'go' do
url url
version new_resource.version
not_if "/usr/local/go-#{new_resource.version}/bin/go version | grep #{new_resource.version}"
end
##### SOURCE BUILD
if new_resource.from_source
directory ::File.join(new_resource.install_dir, 'go', 'bin') do
recursive true
mode new_resource.directory_mode
end
build_essential
file "#{new_resource.install_dir}/go" do
manage_symlink_source true
action :delete
only_if do
# Create idempotency by not deleting symlink if it points to source build
::File.exist?("#{new_resource.install_dir}/go") &&
::File.ftype("#{new_resource.install_dir}/go") == 'link' &&
::File.readlink("#{new_resource.install_dir}/go") != "#{new_resource.install_dir}/go-source"
end
end
url = source_url
ark 'go' do # resource with the same name, that's an issue. Source install
url url
version 'source'
action :put
release_file '/tmp/ark_release_file'
not_if "test -x #{::File.join(new_resource.install_dir, 'go', 'bin', 'go')} && #{::File.join(new_resource.install_dir, 'go', 'bin', 'go')} version | grep #{new_resource.source_version}"
end
# see if build already done
# converge_by if not done
execute 'build-golang' do
cwd "#{new_resource.install_dir}/go/src"
command "./#{new_resource.source_method}"
environment({
# Use the package-installed Go as the bootstrap version b/c Go is built with Go
GOROOT_BOOTSTRAP: "#{new_resource.install_dir}/go-#{new_resource.version}",
GOROOT: "#{new_resource.install_dir}/go",
GOBIN: "#{new_resource.install_dir}/go/bin",
})
not_if "test -x #{::File.join(new_resource.install_dir, 'go', 'bin', 'go')} && #{::File.join(new_resource.install_dir, 'go', 'bin', 'go')} version | grep #{new_resource.source_version}"
end
end
end

View File

@@ -0,0 +1,52 @@
#
# Cookbook:: golang
# Resource:: default
#
default_action :install
gocache = '/tmp/go'
action :install do
execute "go get #{new_resource.name}" do
user node['golang']['owner']
group node['golang']['group']
environment({
PATH: "#{node['golang']['install_dir']}/go/bin:#{node['golang']['gobin']}:" \
'/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
GOPATH: node['golang']['gopath'],
GOBIN: node['golang']['gobin'],
GOCACHE: gocache,
})
end
end
action :update do
execute "go get -u #{new_resource.name}" do
user node['golang']['owner']
group node['golang']['group']
environment({
PATH: "#{node['golang']['install_dir']}/go/bin:#{node['golang']['gobin']}:" \
'/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
GOPATH: node['golang']['gopath'],
GOBIN: node['golang']['gobin'],
GOCACHE: gocache,
})
end
end
action :build do
execute "go build -work -x #{new_resource.name}" do
user node['golang']['owner']
group node['golang']['group']
cwd gocache
environment({
PATH: "#{node['golang']['install_dir']}/go/bin:#{node['golang']['gobin']}:" \
'/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
HOME: "/home/#{node['golang']['owner']}",
GOPATH: node['golang']['gopath'],
GOBIN: node['golang']['gobin'],
GOCACHE: gocache,
})
end
end

View File

@@ -0,0 +1,3 @@
export PATH="<%= ::File.join(@install_dir, 'go', 'bin') %>:<%= @gobin %>:$PATH"
export GOPATH=<%= @gopath %>
export GOBIN=<%= @gobin %>