Created
April 30, 2018 16:44
-
-
Save op-ct/2b740ca99c816ca35d158c2d5f7561a4 to your computer and use it in GitHub Desktop.
rsync Puppet module changes to Beaker Vagrant hosts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'json' | |
| require 'yaml' | |
| require 'rake/file_utils' | |
| module Simp; end | |
| module Simp::BeakerSuites; end | |
| module Simp::BeakerSuites::Helpers | |
| include FileUtils | |
| @beaker_suites_rpath = 'spec/acceptance/suites' | |
| def initialize( module_dir ) | |
| @module_dir = module_dir | |
| end | |
| def module_metadata | |
| @module_metadata ||= ( | |
| md = JSON.parse File.read(File.join(@module_dir,'metadata.json')) | |
| md['module_name'] = md['name'].split('-').last | |
| md['forge_org_name'] = md['name'].split('-').first | |
| md | |
| ) | |
| end | |
| def beaker_suite_dirs | |
| @beaker_suite_dirs ||= ( | |
| entries = File.expand_path(File.join(@module_dir,@beaker_suite_dir,'*','nodesets')) | |
| Dir[entries].map{|e| File.dirname e} | |
| ) | |
| end | |
| def beaker_suites | |
| @beaker_suites ||= beaker_suite_dirs(@module_dir).map{|e| File.basename(File.dirname(e))}.flatten | |
| end | |
| def nodesets( beaker_suite_dir ) | |
| glob = File.expand_path('nodesets/*.yml', beaker_suite_dir) | |
| Dir[glob].map{|e| File.basename(File.dirname(e))}.flatten | |
| end | |
| end | |
| class VagrantRsyncer | |
| include Simp::BeakerSuites::Helpers | |
| def initialize( module_dir, beaker_suite, beaker_nodeset, beaker_hosts ) | |
| @module_dir = module_dir | |
| @suite = beaker_suite | |
| @nodeset = beaker_nodeset | |
| @hosts = beaker_hosts | |
| end | |
| def vagrant_rsync | |
| cwd = Dir.pwd | |
| rsync_cmd = 'rsync -av --no-links --exclude=.git --exclude=.\*.sw\? --exclude=.vagrant --exclude=spec' | |
| vagrant_dir = File.expand_path("#{@nodeset}.yml", '.vagrant/beaker_vagrant_files') | |
| File.open(File.join(vagrant_dir,'pluginsync.pp'),'w') do |f| | |
| f.puts <<-PLUGINSYNC_MANIFEST.gsub!(/^\s{8}/,'') | |
| file { $::settings::libdir: | |
| ensure => directory, | |
| source => 'puppet:///plugins', | |
| recurse => true, | |
| purge => true, | |
| backup => false, | |
| noop => false | |
| } | |
| PLUGINSYNC_MANIFEST | |
| end | |
| Dir.chdir @module_dir | |
| sh "#{rsync_cmd} spec/fixtures/modules #{vagrant_dir}/" | |
| sh "#{rsync_cmd} ./ #{vagrant_dir}/modules/#{module_metadata['module_name']}/" | |
| Dir.chdir vagrant_dir | |
| suts = (@hosts == 'ALL') ? [nil] : @hosts.split(',') | |
| suts.each do |sut| | |
| sh "vagrant rsync #{sut}" | |
| sh "vagrant ssh #{sut} -- sudo rsync -av /vagrant/modules/ /etc/puppetlabs/code/environments/production/modules/" | |
| sh 'vagrant ssh -- sudo /opt/puppetlabs/bin/puppet apply /vagrant/pluginsync.pp' | |
| end | |
| Dir.chdir cwd | |
| end | |
| end | |
| namespace :vagrant do | |
| desc 'rsync module changes to beaker vagrant hosts' | |
| task :rsync do |t,args| | |
| args.with_defaults( | |
| :beaker_suite => 'default', | |
| :beaker_nodeset => 'default', | |
| :hosts => 'ALL' | |
| ) | |
| v = VagrantRsyncer.new( | |
| Rake.application.original_dir, | |
| args[:beaker_suite], | |
| args[:beaker_nodeset], | |
| args[:hosts] | |
| ) | |
| v.vagrant_rsync | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment