cap-strap 0.0.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of cap-strap might be problematic. Click here for more details.

data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # CapStrap
2
+
3
+ CapStrap is a Capistrano Extension that bootstraps a machine for future capistrano tasks.
4
+ It installs System-wide Rvm and specified rubies and gems. It will create a user for future capistrano
5
+ deploy tasks.
6
+
7
+ ## Notes
8
+
9
+ Currently only Ubuntu 10.04 and 10.10 have been tested.
10
+
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'cap-strap', :git => 'git@github.com:substantial/cap-strap.git'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ ## Usage
23
+
24
+ You will be prompted for the name of the future deploy_user, location of authorized keys and
25
+ the deploy_key. Deploy key isn't required, and you may skip.
26
+
27
+ In your deploy.rb, you may override the following variables:
28
+ Note: Paths are relative to the root
29
+
30
+ * `set :deploy_user, "<name of user for future deployes>"`
31
+ * `set :authorized_keys_file, "<location of authorized keys>"`
32
+ * `set :deploy_key_file, "<location of deploy-key>"`
33
+ * `set :known_hosts, "<string of known hosts">` - Default is github.com
34
+ * `set :default_ruby, "1.9.3-p125"` - Default is 1.9.3-p125
35
+ * `set :rubies, [
36
+ "ree-1.8.7-2012.02",
37
+ {
38
+ :version => "1.9.3-p125-perf",
39
+ :patch => "falcon,debug --force-autoconf -j 3"
40
+ }
41
+ ]`
42
+ * `set :gemset, "<default gemset>"` - Default is global
43
+ * `set :global_gems, ["bundler", "other-gem"]` - Default is ["bundler"]
44
+
45
+ Note: Run from project root
46
+
47
+ $ cap <stage> bootstrap -s user=<root_username>
48
+
49
+ ## Testing
50
+
51
+ We're https://github.com/technicalpickles/capistrano-spec for testing the capistrano
52
+ configuration.
53
+
54
+ ###TODO: Use Vagrant to test cap-strap.
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ task :default => :spec
5
+
6
+ Rspec::Core::RakeTask.new do |t|
7
+ t.pattern = "./spec/*_spec.rb"
8
+ end
data/cap-strap.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cap-strap/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cap-strap"
7
+ s.version = "0.0.2"
8
+ s.authors = ["Shaun Dern"]
9
+ s.email = ["shaun@substantial.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Bootstrap a new machine for future Capistrano tasks.}
12
+ s.description = %q{Bootstrap a a machine. Add a deploy user, install RVM, desired rubies and gems.}
13
+
14
+ s.rubyforge_project = "cap-strap"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "capistrano", '>= 2.0.0'
22
+ s.add_development_dependency "capistrano-spec"
23
+ s.add_development_dependency "rspec"
24
+
25
+ s.add_runtime_dependency "capistrano", ">= 2.0.0"
26
+ end
data/lib/cap-strap.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'capistrano'
2
+
3
+ require 'cap-strap/helpers'
4
+ require "cap-strap/recipes/rvm"
5
+ require "cap-strap/recipes/bootstrap"
6
+
7
+ if instance = Capistrano::Configuration.instance
8
+ Capistrano::RVM.load_into(instance)
9
+ Capistrano::Bootstrap.load_into(instance)
10
+
11
+ instance.load do
12
+ ssh_options[:forward_agent] = true
13
+ end
14
+ end
@@ -0,0 +1,78 @@
1
+ def _cset(name, *args, &block)
2
+ unless exists?(name)
3
+ set(name, *args, &block)
4
+ end
5
+ end
6
+
7
+ def add_user_to_group(user, group)
8
+ sudo "usermod -a -G #{group} #{user}"
9
+ end
10
+
11
+ def install_packages(packages)
12
+ sudo "apt-get update"
13
+ packages.each do |pkg|
14
+ sudo "apt-get -y install #{pkg}"
15
+ end
16
+ end
17
+
18
+ def create_user(user)
19
+ unless user_exists?(user)
20
+ sudo "useradd -s /bin/bash -d /home/#{user} -m #{user}"
21
+ end
22
+ end
23
+
24
+ def add_group_to_sudoers(group)
25
+ unless group_has_sudo?(group)
26
+ puts "********* Adding #{group} to sudoers"
27
+ sudo "cp /etc/sudoers /etc/sudoers.bak"
28
+ sudo "chmod a+w /etc/sudoers.bak"
29
+ sudo "echo %rvm ALL=NOPASSWD:ALL >> /etc/sudoers.bak"
30
+ sudo "chmod a-w /etc/sudoers.bak"
31
+ sudo "mv /etc/sudoers.bak /etc/sudoers"
32
+ end
33
+ end
34
+
35
+ def group_has_sudo?(group)
36
+ sudoer_test = "sudo bash -c \"if grep -Fxq '%#{group} ALL=NOPASSWD:ALL' /etc/sudoers; then echo 'true'; else echo 'false'; fi\""
37
+ return capture(sudoer_test).include?("true")
38
+ end
39
+
40
+ def user_exists?(user)
41
+ user_test = "if id #{user} > /dev/null 2>&1; then echo 'true'; else echo 'false'; fi"
42
+ return capture(user_test).include?("true")
43
+ end
44
+
45
+ def default_known_hosts
46
+ <<-TEXT
47
+ github.com,207.97.227.239 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==
48
+ TEXT
49
+ end
50
+
51
+ def install_ruby(ruby, patch = nil)
52
+ command = "rvm install #{ruby}"
53
+ command << " --patch #{patch}" if patch
54
+ sudo rvm_wrapper(command)
55
+ end
56
+
57
+ def install_global_gem(ruby, gem)
58
+ sudo rvm_wrapper("rvm use #{ruby}@global --create && gem install #{gem} --no-rdoc --no-ri")
59
+ end
60
+
61
+ def rvm_wrapper(command)
62
+ "bash -c '. /etc/profile.d/rvm.sh && #{command}'"
63
+ end
64
+
65
+ def gemrc
66
+ <<TEXT
67
+ ---
68
+ :update_sources: true
69
+ :sources:
70
+ - http://gems.rubyforge.org/
71
+ - http://gems.github.com
72
+ :benchmark: false
73
+ :bulk_threshold: 1000
74
+ :backtrace: false
75
+ :verbose: true
76
+ gem: --no-ri --no-rdoc
77
+ TEXT
78
+ end
@@ -0,0 +1,89 @@
1
+ require 'cap-strap/helpers'
2
+ require 'cap-strap/recipes/rvm'
3
+ require 'capistrano/cli'
4
+
5
+ module Capistrano
6
+ module Bootstrap
7
+ def self.load_into(configuration)
8
+ configuration.load do
9
+ before 'rvm:install_system_wide_rvm', 'bootstrap:install_rvm_dependencies'
10
+
11
+ rvm_packages = %w( build-essential openssl libreadline6 libreadline6-dev
12
+ curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev
13
+ libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf
14
+ libc6-dev ncurses-dev automake libtool bison subversion )
15
+
16
+ _cset(:deploy_user) { Capistrano::CLI.ui.ask("deploy user: ") }
17
+ _cset(:user) { Capistrano::CLI.ui.ask("bootstrap root user: ") }
18
+ _cset(:authorized_keys_file) { Capistrano::CLI.ui.ask("Location of authorized keys relative to root to upload: ") }
19
+ _cset(:deploy_key_file) { Capistrano::CLI.ui.ask("Location of deploy key for upload, press enter to skip: ") }
20
+ _cset(:known_hosts) { default_known_hosts }
21
+
22
+ namespace :bootstrap do
23
+ desc "bootstraps a fresh box."
24
+ task :default do
25
+ rvm.default
26
+ bootstrap.create_deploy_user
27
+ bootstrap.upload_deploy_authorized_keys
28
+ bootstrap.add_known_hosts
29
+ bootstrap.add_rvm_to_sudoers
30
+ bootstrap.upload_deploy_key
31
+ end
32
+
33
+ task :create_deploy_user do
34
+ create_user(deploy_user)
35
+ add_user_to_group(deploy_user, "rvm")
36
+ add_user_to_group(deploy_user, "admin")
37
+ end
38
+
39
+ task :upload_deploy_authorized_keys do
40
+ begin
41
+ authorized_keys_path = File.join(File.expand_path(Dir.pwd), authorized_keys_file)
42
+ puts "************Looking for Authorized Keys at: #{authorized_keys_path}"
43
+ authorized_keys = File.read(authorized_keys_file)
44
+ put(authorized_keys, "authorized_keys", :mode => "0600")
45
+ sudo "mkdir -p /home/#{deploy_user}/.ssh"
46
+ sudo "chmod 0700 /home/#{deploy_user}/.ssh"
47
+ sudo "mv ~/authorized_keys /home/#{deploy_user}/.ssh/"
48
+ sudo "chown -R #{deploy_user}:rvm /home/#{deploy_user}/.ssh"
49
+ rescue Exception => e
50
+ puts e
51
+ end
52
+ end
53
+
54
+ task :add_known_hosts do
55
+ put(known_hosts, "known_hosts", :mode => "0644")
56
+ sudo "mv ~/known_hosts /home/#{deploy_user}/.ssh/known_hosts"
57
+ sudo "chown -R deploy:rvm /home/#{deploy_user}/.ssh"
58
+ end
59
+
60
+ task :add_rvm_to_sudoers do
61
+ add_group_to_sudoers("rvm")
62
+ end
63
+
64
+ task :install_rvm_dependencies do
65
+ install_packages(rvm_packages)
66
+ end
67
+
68
+ desc "Uploads the id_rsa for the deploy user. Put the key under config/deploy-key and run."
69
+ task :upload_deploy_key do
70
+ if deploy_key_file.empty?
71
+ puts "No deploy key specified, skipping"
72
+ else
73
+ begin
74
+ deploy_key_path= File.join(File.expand_path(Dir.pwd), deploy_key_file)
75
+ puts "************Looking for Deploy key at: #{deploy_key_path}"
76
+ id_rsa = File.read(deploy_key_path)
77
+ put(id_rsa, "id_rsa", :mode => "0600")
78
+ sudo "mv ~/id_rsa /home/#{deploy_user}/.ssh/"
79
+ sudo "chown -R #{deploy_user}:rvm /home/#{deploy_user}/.ssh"
80
+ rescue Exception => e
81
+ puts e
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,70 @@
1
+ require 'cap-strap/helpers'
2
+
3
+ module Capistrano
4
+ module RVM
5
+ def self.load_into(configuration)
6
+ configuration.load do
7
+
8
+ _cset :default_ruby , "1.9.3-p125"
9
+ _cset :gemset, "global"
10
+ _cset :rubies, []
11
+ _cset(:user) { Capistrano::CLI.ui.ask("bootstrap root user: ") }
12
+ _cset :global_gems, ["bundler"]
13
+
14
+ default_run_options[:shell] = '/bin/bash'
15
+ default_run_options[:pty] = true
16
+
17
+ namespace :rvm do
18
+ task :default do
19
+ rvm.install_system_wide_rvm
20
+ rvm.add_gemrc
21
+ rvm.add_user_to_rvm_group
22
+ rvm.install_rubies
23
+ rvm.set_default_ruby
24
+ rvm.create_default_gemset
25
+ end
26
+
27
+ task :install_system_wide_rvm do
28
+ command = "curl -L get.rvm.io | "
29
+ command << "sudo bash -s stable"
30
+ run command
31
+ end
32
+
33
+ task :install_rubies do
34
+ rubies << default_ruby
35
+ rubies.each do |rubie|
36
+ if rubie.is_a?(Hash)
37
+ ruby = rubie.fetch(:version)
38
+ ruby_patch = rubie.fetch(:patch)
39
+ else
40
+ ruby = rubie
41
+ ruby_patch = nil
42
+ end
43
+ install_ruby(ruby, ruby_patch)
44
+ global_gems.each do |gem|
45
+ install_global_gem(ruby, gem)
46
+ end
47
+ end
48
+ end
49
+
50
+ task :create_default_gemset do
51
+ sudo rvm_wrapper("rvm use #{default_ruby}@#{gemset} --create")
52
+ end
53
+
54
+ task :set_default_ruby do
55
+ sudo rvm_wrapper("rvm use #{default_ruby} --default")
56
+ end
57
+
58
+ task :add_user_to_rvm_group do
59
+ add_user_to_group(user, "rvm")
60
+ end
61
+
62
+ task :add_gemrc do
63
+ put gemrc, "gemrc"
64
+ sudo "mv ~/gemrc /etc/"
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Capistrano::Bootstrap do
4
+ let(:ui) { double("ui", :ask => 'prompt') }
5
+ before do
6
+ @configuration = Capistrano::Configuration.new
7
+ @configuration.extend(Capistrano::Spec::ConfigurationExtension)
8
+ Capistrano::Bootstrap.load_into(@configuration)
9
+ Capistrano::CLI.stub(:ui).and_return { ui }
10
+ end
11
+
12
+ describe "default variables" do
13
+ it "prompts for a bootstrap user" do
14
+ @configuration.fetch(:user).should == 'prompt'
15
+ end
16
+
17
+ it "prompts for a deploy user" do
18
+ @configuration.fetch(:deploy_user).should == 'prompt'
19
+ end
20
+
21
+ it 'prompts for authorized keys if no path set' do
22
+ @configuration.fetch(:authorized_keys_file).should == 'prompt'
23
+ end
24
+
25
+ it "prompts for deploy key location if not set" do
26
+ @configuration.fetch(:deploy_key_file).should == 'prompt'
27
+ end
28
+ end
29
+
30
+ describe "variables set" do
31
+ before do
32
+ @configuration.set(:user, "foo")
33
+ @configuration.set(:deploy_user, "bar")
34
+ @configuration.set(:known_hosts, "baz")
35
+ end
36
+
37
+ it "doesn't prompted for a bootstrap user" do
38
+ @configuration.fetch(:user).should == "foo"
39
+ end
40
+
41
+ it "doesn't prompted for a deploy user" do
42
+ @configuration.fetch(:deploy_user).should == "bar"
43
+ end
44
+
45
+ it "replaces known_hosts with ones that are set" do
46
+ @configuration.fetch(:known_hosts).should == "baz"
47
+ end
48
+ end
49
+ end
data/spec/rvm_spec.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Capistrano::RVM do
4
+ let(:ui) { double("ui", :ask => 'prompt') }
5
+ before do
6
+ @configuration = Capistrano::Configuration.new
7
+ @configuration.extend(Capistrano::Spec::ConfigurationExtension)
8
+ Capistrano::RVM.load_into(@configuration)
9
+ Capistrano::CLI.stub(:ui).and_return { ui }
10
+ end
11
+
12
+ describe "default variables" do
13
+ it "has a prompts for a bootstrap user" do
14
+ @configuration.fetch(:user).should == 'prompt'
15
+ end
16
+
17
+ it "has a default ruby" do
18
+ @configuration.fetch(:default_ruby).should be
19
+ end
20
+
21
+ it "has a default gemset" do
22
+ @configuration.fetch(:gemset).should be
23
+ end
24
+
25
+ it "defaults to empty ruby set" do
26
+ @configuration.fetch(:rubies).length.should be 0
27
+ end
28
+
29
+ it "has bundler in it's global gems" do
30
+ @configuration.fetch(:global_gems).include?("bundler").should == true
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,8 @@
1
+ require 'capistrano'
2
+ require 'cap-strap'
3
+
4
+ require 'capistrano-spec'
5
+
6
+ Rspec.configure do |config|
7
+ config.include Capistrano::Spec::Matchers
8
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cap-strap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Shaun Dern
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: capistrano
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: capistrano-spec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: capistrano
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 2.0.0
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 2.0.0
78
+ description: Bootstrap a a machine. Add a deploy user, install RVM, desired rubies
79
+ and gems.
80
+ email:
81
+ - shaun@substantial.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - README.md
89
+ - Rakefile
90
+ - cap-strap.gemspec
91
+ - lib/cap-strap.rb
92
+ - lib/cap-strap/helpers.rb
93
+ - lib/cap-strap/recipes/bootstrap.rb
94
+ - lib/cap-strap/recipes/rvm.rb
95
+ - spec/bootstrap_spec.rb
96
+ - spec/rvm_spec.rb
97
+ - spec/spec_helper.rb
98
+ homepage: ''
99
+ licenses: []
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project: cap-strap
118
+ rubygems_version: 1.8.22
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Bootstrap a new machine for future Capistrano tasks.
122
+ test_files:
123
+ - spec/bootstrap_spec.rb
124
+ - spec/rvm_spec.rb
125
+ - spec/spec_helper.rb