Skip to content

Commit

Permalink
Refactor lwrp into new style custom resources to address #13 (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivadim authored Nov 27, 2017
1 parent 3ed572d commit ac16dbd
Show file tree
Hide file tree
Showing 17 changed files with 401 additions and 469 deletions.
44 changes: 27 additions & 17 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ driver:

provisioner:
name: chef_zero
log_level: auto
client_rb:
add_formatter: doc

Expand All @@ -17,16 +18,28 @@ platforms:
box: debian/jessie64
provisioner:
require_chef_omnibus: 12.20.3

- name: debian9
driver_config:
box: debian/stretch64
provisioner:
require_chef_omnibus: 13.6.4

- name: ubuntu1604
driver_config:
box: ubuntu/xenial64
provisioner:
require_chef_omnibus: 13.0.118
require_chef_omnibus: 13.6.4

- name: ubuntu1710
driver_config:
box: ubuntu/artful64
provisioner:
require_chef_omnibus: 13.6.4

- name: centos6
- name: centos7
driver_config:
box: bento/centos-6.7
box: centos/7
provisioner:
require_chef_omnibus: 12.20.3

Expand All @@ -39,26 +52,20 @@ platforms:
# 3. In the 'suites' section of this file, list your box name(s) under 'includes' for the corresponding platform
# 4. Confirm your box shows up when running `vagrant box list` and `kitchen list` when in this cookbook directory

- name: osx109-desktop
driver_config:
box: osx109-desktop
provisioner:
require_chef_omnibus: 13.0.118

- name: osx1010
- name: osx1012
driver:
box: osx1010
box: osx1012
username: <%= ENV['BOX_USERNAME'] %>
provisioner:
require_chef_omnibus: 13.0.118
require_chef_omnibus: 13.6.4

- name: windows10
driver_config:
guest: windows
communicator: 'winrm'
box: win10x64-enterprise
provisioner:
require_chef_omnibus: 12.20.3
require_chef_omnibus: 13.6.4

- name: windows-2012-r2
driver_config:
Expand All @@ -82,12 +89,16 @@ suites:
vsts_pool: <%= ENV['VSTS_POOL'] %>
vsts_token: <%= ENV['VSTS_TOKEN'] %>

- name: debian-basic
- name: linux-basic
run_list:
- recipe[debian-basic::default]
- recipe[linux-basic::default]
includes:
- debian8
- debian9
- ubuntu1604
- ubuntu1710
- centos7

attributes:
vsts_agent_test:
username: <%= ENV['BOX_USERNAME'] %>
Expand All @@ -99,8 +110,7 @@ suites:
run_list:
- recipe[osx-basic::default]
includes:
- osx109-desktop
- osx1010
- osx1012

attributes:
vsts_agent_test:
Expand Down
17 changes: 0 additions & 17 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,2 @@
LineLength:
Max: 300

Style/AlignParameters:
Exclude:
- '**/metadata.rb'

Style/SpaceBeforeFirstArg:
Exclude:
- '**/metadata.rb'

Metrics/MethodLength:
Max: 30

Metrics/AbcSize:
Max: 30

EndOfLine:
Enabled: false
4 changes: 1 addition & 3 deletions Berksfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
source 'https://supermarket.chef.io'

cookbook 'ark', git: 'git://github.com/ivadim/ark.git'

metadata

group :integration do
cookbook 'debian-basic', path: './test/cookbooks/debian-basic'
cookbook 'linux-basic', path: './test/cookbooks/linux-basic'
cookbook 'windows-basic', path: './test/cookbooks/windows-basic'
cookbook 'osx-basic', path: './test/cookbooks/osx-basic'
end
File renamed without changes.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ Requirements
### Platforms
The following platforms are tested and supported:
- Debian 8 x64 (Jessie)
- Ubuntu 16.04
- Debian 9 x64 (Stretch)
- Ubuntu 16.04 (Xenial Xerus)
- Ubuntu 17.10 (Artful Aardvark)
- CentOS 7
- Windows 10
- Windows Server 2012 R2
- OS X 10.10.5
Expand All @@ -25,8 +28,8 @@ The following platforms are tested and supported:
Attributes
----------
* `node['vsts_agent']['binary']['version']` - set version of package to install
* `node['vsts_agent']['prerequisites']['osx']['install']` - control macos dependencies installation. Default true
* `node['vsts_agent']['prerequisites']['debian']['install']` - control debian dependencies installation. Default true
* `node['vsts_agent']['prerequisites']['redhat']['install']` - control dependencies installation for redhat based distros(redhat, centos etc.) . Default true
* `node['vsts_agent']['prerequisites']['debian']['install']` - control dependencies installation for debian based distros(debian, ubuntu etc.). Default true

Resource/Provider
-----------------
Expand Down Expand Up @@ -58,7 +61,7 @@ This resource installs and configures the vsts build and release agent

#### Examples
Install, configure, restart and remove an agent.
Check [windows](test/cookbooks/windows-basic/recipes/default.rb), [debian](test/cookbooks/debian-basic/recipes/default.rb) or [osx](test/cookbooks/osx-basic/recipes/default.rb) tests for more examples.
Check [windows](test/cookbooks/windows-basic/recipes/default.rb), [linux](test/cookbooks/linux-basic/recipes/default.rb) or [osx](test/cookbooks/osx-basic/recipes/default.rb) tests for more examples.

```ruby
include_recipe 'vsts_agent::default'
Expand Down
21 changes: 12 additions & 9 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
require 'rspec/core/rake_task'
require 'rubocop/rake_task'
require 'cookstyle'
require 'foodcritic'
require 'kitchen'

namespace :style do
desc 'Run Ruby style checks'
RuboCop::RakeTask.new(:ruby)
desc 'Run cookstyle checks'
RuboCop::RakeTask.new(:ruby) do |task|
task.options << '--display-cop-names'
end

desc 'Run Chef style checks'
desc 'Run foodcritic checks'
FoodCritic::Rake::LintTask.new(:chef)
end

desc 'Run all style checks'
task style: ['style:chef', 'style:ruby']

desc 'Run ChefSpec'
RSpec::Core::RakeTask.new(:spec)

namespace :kitchen do
desc 'Run Test Kitchen with Vagrant'
task :linux do
Expand All @@ -25,6 +22,12 @@ namespace :kitchen do
end
end

desc 'Run all style checks'
task style: ['style:chef', 'style:ruby']

desc 'Run ChefSpec'
RSpec::Core::RakeTask.new(:spec)

task :supermarket do
exec 'chef exec knife supermarket share vsts_agent Other -o .. -k supermarket.pem -u vsts_agent_cookbook'
end
Expand Down
23 changes: 9 additions & 14 deletions attributes/default.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
default['vsts_agent']['binary']['version'] = '2.116.1'
default['vsts_agent']['binary']['version'] = '2.125.1'

case node['platform_family']
when 'windows'
default['vsts_agent']['binary']['url'] = 'https://github.com/Microsoft/vsts-agent/releases/download/v%s/vsts-agent-win7-x64-%s.zip'
when 'rhel'
default['vsts_agent']['binary']['url'] = 'https://github.com/Microsoft/vsts-agent/releases/download/v%s/vsts-agent-rhel.7.2-x64-%s.tar.gz'
when 'debian'
default['vsts_agent']['binary']['url'] =
if platform?('ubuntu') && node['platform_version'].to_i >= 16
'https://github.com/Microsoft/vsts-agent/releases/download/v%s/vsts-agent-ubuntu.16.04-x64-%s.tar.gz'
else
'https://github.com/Microsoft/vsts-agent/releases/download/v%s/vsts-agent-ubuntu.14.04-x64-%s.tar.gz'
end
default['vsts_agent']['binary']['url'] = 'https://github.com/Microsoft/vsts-agent/releases/download/v%s/vsts-agent-win-x64-%s.zip'
when 'debian', 'rhel'
default['vsts_agent']['binary']['url'] = 'https://github.com/Microsoft/vsts-agent/releases/download/v%s/vsts-agent-linux-x64-%s.tar.gz'
when 'mac_os_x', 'mac_os_x_server'
default['vsts_agent']['binary']['url'] = 'https://github.com/Microsoft/vsts-agent/releases/download/v%s/vsts-agent-osx.10.11-x64-%s.tar.gz'
default['vsts_agent']['binary']['url'] = 'https://github.com/Microsoft/vsts-agent/releases/download/v%s/vsts-agent-osx-x64-%s.tar.gz'
end

default['vsts_agent']['prerequisites']['osx']['install'] = true
# applies for debian based distros: ubuntu, debian etc...
default['vsts_agent']['prerequisites']['debian']['install'] = true
default['vsts_agent']['prerequisites']['debian']['libicu52']['url'] = 'http://security.ubuntu.com/ubuntu/pool/main/i/icu/libicu52_52.1-8ubuntu0.2_amd64.deb'

# applies for redhat based distros: redhat, centos etc...
default['vsts_agent']['prerequisites']['redhat']['install'] = true
65 changes: 19 additions & 46 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ module Helpers
include Chef::DSL::PlatformIntrospection

require 'json'
require 'fileutils'

VARS_TO_SAVE = %w(install_dir user group).freeze

def archive_name(resource)
def archive_name(version)
name = 'vsts_agent'
name += '_' + resource.version if resource.version
name += '_' + version if version
name
end

def download_url(version, node)
url = node['vsts_agent']['binary']['url']
def download_url(version)
url = Chef.run_context.node['vsts_agent']['binary']['url']
url.gsub '%s', version
end

Expand Down Expand Up @@ -43,52 +42,26 @@ def agent_exists?(install_dir)
::File.exist?("#{install_dir}/.agent")
end

def valid_vsts_url?(url)
vsts_url_pattern = %r{https:\/\/.*\.visualstudio\.com}
vsts_url_pattern.match(url)
end

def save_vars(resource, node)
VARS_TO_SAVE.each { |var| node.normal['vsts_agent']['agents'][resource.agent_name][var] = resource.send(var) if resource.respond_to?(var.to_sym) }
node.save
end

def load_vars(resource, node)
VARS_TO_SAVE.each { |var| resource.send(var, node['vsts_agent']['agents'][resource.agent_name][var]) if resource.respond_to?(var.to_sym) }
def save_state(agent_name, hash = {})
::File.write(state_file(agent_name), hash.to_json)
end

def load_current_state(resource, node)
resource.exists = false
return unless agent_attribute?(resource.agent_name, node)
load_vars(resource, node)
return unless agent_exists?(resource.install_dir)
load_data_from_json(resource)
resource.runasservice(service_exist?(resource.install_dir))
resource.exists = true
def load_state(agent_name)
state_file = state_file(agent_name)
return unless ::File.exist?(state_file)
state = ::File.read(state_file(agent_name))
JSON.parse(state)
end

def load_data_from_json(resource)
f = ::File.read(::File.join(resource.install_dir, '.agent'), mode: 'r:bom|utf-8').strip
agent = JSON.parse(f)
resource.vsts_url(agent['serverUrl'])
resource.vsts_pool(agent['poolName'])
resource.work_folder(agent['workFolder'])
end

def agent_attribute?(agent_name, node)
if node['vsts_agent']['agents'].nil? ||
node['vsts_agent']['agents'][agent_name].nil? ||
node['vsts_agent']['agents'][agent_name]['install_dir'].nil? ||
node['vsts_agent']['agents'][agent_name]['install_dir'].empty?
false
else
true
end
def state_file(agent_name)
save_dir = "#{Chef::Config[:file_cache_path]}/vsts_agent"
::FileUtils.mkdir_p save_dir unless ::File.directory?(save_dir)
"#{save_dir}/#{agent_name}-state.json"
end

def remove_current_state(resource, node)
node.normal['vsts_agent']['agents'][resource.agent_name] = {}
node.save
def remove_current_state(agent_name)
state_file = state_file(agent_name)
::File.delete(state_file) if ::File.exist?(state_file)
end

def set_auth(args, resource)
Expand Down
Loading

0 comments on commit ac16dbd

Please sign in to comment.