sprinkle-tool / sprinkle

Sprinkle is a software provisioning tool you can use to build remote servers with. eg. to install a Rails, or Sinatra stack on a brand new slice directly after its been created
https://github.com/sprinkle-tool/sprinkle
MIT License
1.15k stars 138 forks source link

Is it possible to send local variables to a package? #77

Closed barnaclebarnes closed 11 years ago

barnaclebarnes commented 11 years ago

I have a package nginx_site that I want to make fairly generic so that I can install different sites for different roles. What I wan to do is something like:

# install.rb
policy :websites, :roles => :web_server do
  requires :nginx_site, :locals => {:site_name => 'somesite.com'}
  requires :nginx_site, :locals => {:site_name => 'someothersite.com'}
end

policy :api_server, :roles => :api_server do
  requires :nginx_site, :locals => {:site_name => 'api.somesite.com'}
end
# nginx-site.rb
package :nginx_site do
  runner "mkdir -p /var/www/#{site_name}"
  # Other site setup stuff...
end

Is there an easy way to pass variables to packages or some other way of accomplishing what I want?

ebeigarts commented 11 years ago

Not possible, see #7

But I do use this workaround:

Dir[File.dirname(__FILE__) + '/../nginx/sites-available/*.conf'].each do |path|
  name = File.basename(path, ".conf")

  package :"www_#{name}" do
    user = name.gsub(/[^a-z]/, "-")

    transfer path, "/etc/nginx/sites-available/#{name}"

    runner "mkdir -p /var/www/"
    runner "useradd -m -g www-data --home /var/www/#{user} -s /bin/bash www-#{user}; true"
    runner "mkdir -p /var/www/#{user}"
    runner "chmod 755 /var/www/#{user}"
    runner "chown www-#{user}:www-data /var/www/#{user}"
    runner "chown root:root /etc/nginx/sites-available/#{name}"
    runner "ln -nfs /etc/nginx/sites-available/#{name} /etc/nginx/sites-enabled/#{name}"
    runner "/etc/init.d/nginx restart"

    verify do
      has_directory "/var/www/#{user}"
      matches_local path, "/etc/nginx/sites-enabled/#{name}"
    end
  end
end

and then in policies:

requires :www_example
requires :"www_domain.com"
...
joshgoebel commented 11 years ago

Please try package arguments with sprinkle 0.5.0.rc1:

policy :websites, :roles => :web_server do
  requires :nginx_site, :locals => {:site_name => 'somesite.com'}
  requires :blah, 1, 2, 3
end

package :nginx_site do
  runner "mkdir -p /var/www/#{opts[:locals][:site_name]}"
end

package :blah do
  runner "count #{args.split(" ")}" # calls "count 1 2 3"
end

Right now the entire hash you pass is available as args but in the future some keys sprinkle might want to use internally could reserved, such as :version, etc.

joshgoebel commented 11 years ago

Or maybe try opts, since I'm using extract_options... so an array of arguments would drop into args and any hashes you pass should drop into opts.

joshgoebel commented 11 years ago

https://github.com/sprinkle-tool/sprinkle/wiki/Passing-arguments-to-packages