Created
November 15, 2015 11:48
-
-
Save unosk/bdc709ca3dcec465683b to your computer and use it in GitHub Desktop.
This file contains 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' | |
set :itamae_rsync_src, './ops/itamae' | |
set :itamae_rsync_dst, '/tmp/itamae' | |
set :itamae_rsync_options, %w(-avzr --delete --exclude='.vagrant') | |
namespace :itamae do | |
task :install do | |
on roles(:all) do | |
execute <<-SCRIPT | |
which itamae > /dev/null 2>&1 && exit 0 | |
echo 'deb https://dl.bintray.com/itamae/itamae trusty contrib' | sudo tee /etc/apt/sources.list.d/itamae.list | |
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv D401AB61 | |
sudo apt-get update | |
sudo apt-get install itamae | |
SCRIPT | |
end | |
end | |
task :rsync do | |
on roles(:all) do |server| | |
run_locally do | |
src = "#{fetch(:itamae_rsync_src)}/" | |
dst = "#{server.hostname}:#{fetch(:itamae_rsync_dst)}" | |
execute :rsync, fetch(:itamae_rsync_options), "--rsh='#{build_ssh_command(server)}'", src, dst | |
end | |
end | |
end | |
task :attrs do | |
on roles(:all) do |server| | |
attrs = fetch(:itamae_attributes).to_json | |
upload! StringIO.new(attrs), File.join(fetch(:itamae_rsync_dst), './node.json') | |
end | |
end | |
task :setup do | |
invoke('itamae:install') | |
invoke('itamae:rsync') | |
invoke('itamae:attrs') | |
end | |
desc 'Itamae plan(dry-run)' | |
task plan: :setup do | |
on roles(:all) do | |
run_itamae(dry_run: true) | |
end | |
end | |
desc 'Itamae apply' | |
task apply: :setup do | |
on roles(:all) do | |
run_itamae(dry_run: false) | |
end | |
end | |
def run_itamae(dry_run: true) | |
entrypoint = File.join(fetch(:itamae_rsync_dst), './bootstrap.rb') | |
node_json = File.join(fetch(:itamae_rsync_dst), './node.json') | |
with_verbosity Logger::DEBUG do | |
sudo :itamae, :local, entrypoint, '-j', node_json, '--no-color', dry_run ? '--dry_run' : nil | |
end | |
end | |
def build_ssh_command(server) | |
ssh_options = fetch(:ssh_options) || {} | |
ssh_options.merge!(server.ssh_options || {}) | |
ssh_options[:user] ||= server.user | |
ssh_options[:port] ||= server.port | |
ssh_options[:keys] ||= server.keys | |
command = 'ssh' | |
command << " -l #{ssh_options[:user]}" if ssh_options[:user] | |
command << " -p #{ssh_options[:port]}" if ssh_options[:port] | |
ssh_options[:keys].each do |key| | |
command << " -i #{key}" | |
end | |
command | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment