From 8cb9b263079391575fc00f652d3ad0edadab0aae Mon Sep 17 00:00:00 2001 From: Daniel Zabel Date: Wed, 20 Jul 2016 17:16:31 +0200 Subject: [PATCH 1/2] rake tasks - add robocop.yml and Rakefile - update Gemfile with chefdk 0.12.0 gems --- .rubocop.yml | 27 ++++++++++++++++++ Gemfile | 72 +++++++++++++++++++++++++++++++++++++----------- Rakefile | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+), 16 deletions(-) create mode 100644 .rubocop.yml create mode 100644 Rakefile diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..935e02e --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,27 @@ +AllCops: + TargetRubyVersion: 2.1 +Documentation: + Enabled: false +AlignParameters: + Enabled: false +CyclomaticComplexity: + Max: 10 +Encoding: + Enabled: false +HashSyntax: + Enabled: false +LineLength: + Enabled: false +MethodLength: + Max: 50 +Style/MethodName: + Enabled: false +Style/FileName: + Enabled: false +# otherwise knife-cookbook-doc style complains +Style/LeadingCommentSpace: + Enabled: false +SpaceBeforeFirstArg: + Enabled: false +Metrics/AbcSize: + Enabled: false diff --git a/Gemfile b/Gemfile index 6f4b1b4..82c547a 100644 --- a/Gemfile +++ b/Gemfile @@ -1,20 +1,60 @@ source 'https://rubygems.org' -gem 'berkshelf' +# use berkshelf/4.3.0 dependencies embedded in chefdk/0.12.0 +group :berkshelf do + gem 'berkshelf', '= 4.3.0' + gem 'addressable', '= 2.4.0' + gem 'berkshelf-api-client', '= 2.0.2' + gem 'buff-config', '= 1.0.1' + gem 'buff-extensions', '= 1.0.0' + gem 'buff-shell_out', '= 0.2.0' + gem 'celluloid', '= 0.16.0' + gem 'celluloid-io', '= 0.16.2' + gem 'cleanroom', '= 1.0.0' + gem 'faraday', '= 0.9.2' + gem 'httpclient', '= 2.7.1' + gem 'minitar', '= 0.5.4' + gem 'octokit', '= 4.3.0' + gem 'retryable', '= 2.0.3' + gem 'ridley', '= 4.5.0' + gem 'sawyer', '= 0.7.0' + gem 'solve', '= 2.0.2' + gem 'molinillo', '= 0.2.3' + gem 'dep-selector-libgecode', '= 1.1.0' + gem 'dep_selector', '= 1.0.3' + gem 'thor', '= 0.19.1' +end -# Uncomment these lines if you want to live on the Edge: -# -# group :development do -# gem "berkshelf", github: "berkshelf/berkshelf" -# gem "vagrant", github: "mitchellh/vagrant", tag: "v1.6.3" -# end -# -# group :plugins do -# gem "vagrant-berkshelf", github: "berkshelf/vagrant-berkshelf" -# gem "vagrant-omnibus", github: "schisamo/vagrant-omnibus" -# end +group :chef do + gem 'chef', '~> 12.8.1' + gem 'ohai', '~> 8.12.1' + gem 'chefspec', '~> 4.6.0' + gem 'foodcritic', '~> 6.0.1' + gem 'fauxhai', '~> 3.1.0' + gem 'serverspec', '~> 2.31.0' + gem 'specinfra', '~> 2.53.1' + gem 'inspec' + gem 'rack', '~> 1.5.5' +end -gem 'test-kitchen' -gem 'kitchen-vagrant' -gem 'foodcritic' -gem 'chefspec' +group :tools do + gem 'rake' + gem 'rake-notes' + gem 'rubocop' + gem 'rubocop-checkstyle_formatter' + gem 'rspec' + gem 'rspec_junit_formatter' + gem 'knife-cookbook-doc' + gem 'knife-supermarket' + gem 'emeril' +end + +group :kitchen do + gem 'test-kitchen' + gem 'kitchen-vagrant' + gem 'kitchen-docker' + gem 'kitchen-ec2' + gem 'kitchen-ssh' + gem 'kitchen-localhost' + gem 'kitchen-inspec' +end diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..56782f4 --- /dev/null +++ b/Rakefile @@ -0,0 +1,78 @@ +require 'rspec/core/rake_task' +require 'rubocop/rake_task' +require 'foodcritic' +require 'kitchen' +require 'emeril/rake_tasks' + +desc 'Cleans the workspace and the bundle dir !!!!, bundle install required afterwards.' +task :clean do + rm_rf %w(.bundle build berks-cookbooks Berksfile.lock .kitchen .vagrant .rakeTasks) +end + +desc 'Run all style checks' +task style: ['style:chef', 'style:ruby'] + +desc 'Run all tests' +task test: ['test:unit', 'test:kitchen'] + +desc 'Regenerate README.md' +task :doc do + `bundle exec knife cookbook doc .` +end + +desc 'Run all regular tasks' +task default: %w(style test) + +namespace :style do + desc 'Run Ruby style checks' + RuboCop::RakeTask.new(:ruby) do |task| + task.patterns = ['**/*.rb'] + # don't abort rake on failure + task.fail_on_error = false + end + + desc 'Run Chef style checks' + FoodCritic::Rake::LintTask.new(:chef) do |t| + t.options = { + tags: %w(~FC015 ~FC009 ~FC021 ~FC064), + fail_tags: ['correctness'] + } + end +end + +namespace :test do + desc 'Run Test Kitchen with Vagrant' + task :kitchen do + Kitchen.logger = Kitchen.default_file_logger + Kitchen::Config.new.instances.each do |instance| + instance.test(:always) + end + end + + desc 'Runs specs with chefspec.' + RSpec::Core::RakeTask.new(:unit, [:recipe, :output_file]) do |t, args| + args.with_defaults(recipe: '*', output_file: nil) + t.verbose = false + t.fail_on_error = false + t.rspec_opts = '--color --format progress --format RspecJunitFormatter --out build/chefspec-reports.xml' + t.ruby_opts = '-W0' # it supports ruby options too + t.pattern = "spec/#{args.recipe}_spec.rb" + end +end + +desc 'Releases Cookbook' +Emeril::RakeTasks.new do |t| + # turn on debug logging + t.config[:logger].level = :debug + + # set a category for this cookbook + t.config[:category] = 'Applications' + + # currently we don't have a supermarket + t.config[:publish_to_supermarket] = false +end + +desc 'share cookbook to supermarket' +task :share do + `knife supermarket share caddy` +end From 12b2d1b666dd7e8a042d541a2dfd063b317da19d Mon Sep 17 00:00:00 2001 From: Daniel Zabel Date: Wed, 20 Jul 2016 17:32:14 +0200 Subject: [PATCH 2/2] fix rubocop style issues --- Berksfile | 2 +- Thorfile | 4 ++-- Vagrantfile | 5 ++--- libraries/helpers.rb | 4 ++-- recipes/default.rb | 9 ++++++--- spec/recipes/default_spec.rb | 4 ++-- 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Berksfile b/Berksfile index 967b9a7..34fea21 100644 --- a/Berksfile +++ b/Berksfile @@ -1,3 +1,3 @@ -source "https://supermarket.chef.io" +source 'https://supermarket.chef.io' metadata diff --git a/Thorfile b/Thorfile index 7a0c0ff..19aaaf2 100644 --- a/Thorfile +++ b/Thorfile @@ -5,8 +5,8 @@ require 'bundler/setup' require 'berkshelf/thor' begin - require "kitchen/thor_tasks" + require 'kitchen/thor_tasks' Kitchen::ThorTasks.new rescue LoadError - puts ">>>>> Kitchen gem not loaded, omitting tasks" unless ENV["CI"] + puts '>>>>> Kitchen gem not loaded, omitting tasks' unless ENV['CI'] end diff --git a/Vagrantfile b/Vagrantfile index 914e68e..9068274 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -2,7 +2,7 @@ # vi: set ft=ruby : # Vagrantfile API/syntax version. Don't touch unless you know what you're doing! -VAGRANTFILE_API_VERSION = '2' +VAGRANTFILE_API_VERSION = '2'.freeze Vagrant.require_version '>= 1.5.0' @@ -18,7 +18,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # # $ vagrant plugin install vagrant-omnibus # - if Vagrant.has_plugin?("vagrant-omnibus") + if Vagrant.has_plugin?('vagrant-omnibus') config.omnibus.chef_version = 'latest' end @@ -27,7 +27,6 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # config.vm.box_url doesn't need to be specified. config.vm.box = 'chef/ubuntu-14.04' - # Assign this VM to a host-only network IP, allowing you to access it # via the IP. Host-only networks can talk to the host machine as well as # any other machines on the same network, but cannot be accessed (through this diff --git a/libraries/helpers.rb b/libraries/helpers.rb index 763e5d9..308a048 100644 --- a/libraries/helpers.rb +++ b/libraries/helpers.rb @@ -7,7 +7,7 @@ def initialize @document = '' end - def to_caddy(directive, content, level=0) + def to_caddy(directive, content, level = 0) if content.is_a? Hash output "#{directive} {", level content.each { |subdirective, subcontent| to_caddy(subdirective, subcontent, level + 1) } @@ -23,7 +23,7 @@ def to_caddy(directive, content, level=0) end end - def output(content, level=0) + def output(content, level = 0) @document << (' ' * level) + content + "\n" end diff --git a/recipes/default.rb b/recipes/default.rb index ee0af5b..4f89925 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -43,16 +43,19 @@ end template '/etc/Caddyfile' do - variables ({ 'hosts' => node['caddy']['hosts'], 'document' => CaddyDocument.new }) + variables( + :hosts => node['caddy']['hosts'], + :document => CaddyDocument.new + ) notifies :restart, 'service[caddy]' end -variables = ({ +variables = { :workdir => caddy_path, :command => '/usr/local/bin/caddy', :options => "#{caddy_letsencrypt_arguments} -pidfile /var/run/caddy.pid -log #{caddy_path}/caddy.log -conf /etc/Caddyfile", :ulimit => node['caddy']['ulimit'] -}) +} if %w(arch gentoo fedora suse).include?(node['platform_family']) is_systemd = true diff --git a/spec/recipes/default_spec.rb b/spec/recipes/default_spec.rb index da5da90..a1cc7a2 100644 --- a/spec/recipes/default_spec.rb +++ b/spec/recipes/default_spec.rb @@ -8,7 +8,7 @@ end context 'complex Caddyfile' do - let(:hosts_json) { JSON.parse(File.read("spec/fixtures/hosts.json")) } + let(:hosts_json) { JSON.parse(File.read('spec/fixtures/hosts.json')) } let(:chef_run) do ChefSpec::SoloRunner.new do |node| node.set['caddy']['hosts'] = hosts_json @@ -16,7 +16,7 @@ end it 'generates complex Caddyfile' do - caddy_file = File.read("spec/fixtures/Caddyfile").chomp + caddy_file = File.read('spec/fixtures/Caddyfile').chomp expect(chef_run).to render_file('/etc/Caddyfile').with_content(caddy_file) end end