from-scratch 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/Berksfile +1 -7
  3. data/Berksfile.lock +6 -0
  4. data/README.md +8 -3
  5. data/bin/scratchify +1 -1
  6. data/cookbooks/ruby_build/CHANGELOG.md +72 -0
  7. data/cookbooks/ruby_build/README.md +338 -0
  8. data/cookbooks/ruby_build/attributes/default.rb +67 -0
  9. data/cookbooks/ruby_build/libraries/ruby_build_recipe_helpers.rb +40 -0
  10. data/cookbooks/ruby_build/metadata.json +39 -0
  11. data/cookbooks/ruby_build/providers/ruby.rb +88 -0
  12. data/cookbooks/ruby_build/recipes/default.rb +69 -0
  13. data/cookbooks/ruby_build/resources/ruby.rb +33 -0
  14. data/cookbooks/ruby_rbenv/CHANGELOG.md +221 -0
  15. data/cookbooks/ruby_rbenv/README.md +1059 -0
  16. data/cookbooks/ruby_rbenv/attributes/default.rb +73 -0
  17. data/cookbooks/ruby_rbenv/libraries/chef_provider_package_rbenvrubygems.rb +94 -0
  18. data/cookbooks/ruby_rbenv/libraries/chef_rbenv_mixin.rb +49 -0
  19. data/cookbooks/ruby_rbenv/libraries/chef_rbenv_recipe_helpers.rb +118 -0
  20. data/cookbooks/ruby_rbenv/libraries/chef_rbenv_script_helpers.rb +79 -0
  21. data/cookbooks/ruby_rbenv/libraries/matchers.rb +25 -0
  22. data/cookbooks/ruby_rbenv/metadata.json +1 -0
  23. data/cookbooks/ruby_rbenv/providers/global.rb +53 -0
  24. data/cookbooks/ruby_rbenv/providers/plugin.rb +55 -0
  25. data/cookbooks/ruby_rbenv/providers/rehash.rb +45 -0
  26. data/cookbooks/ruby_rbenv/providers/ruby.rb +123 -0
  27. data/cookbooks/ruby_rbenv/providers/script.rb +81 -0
  28. data/cookbooks/ruby_rbenv/recipes/default.rb +23 -0
  29. data/cookbooks/ruby_rbenv/recipes/system.rb +52 -0
  30. data/cookbooks/ruby_rbenv/recipes/system_install.rb +45 -0
  31. data/cookbooks/ruby_rbenv/recipes/user.rb +76 -0
  32. data/cookbooks/ruby_rbenv/recipes/user_install.rb +48 -0
  33. data/cookbooks/ruby_rbenv/resources/gem.rb +44 -0
  34. data/cookbooks/ruby_rbenv/resources/global.rb +33 -0
  35. data/cookbooks/ruby_rbenv/resources/plugin.rb +15 -0
  36. data/cookbooks/ruby_rbenv/resources/rehash.rb +29 -0
  37. data/cookbooks/ruby_rbenv/resources/ruby.rb +43 -0
  38. data/cookbooks/ruby_rbenv/resources/script.rb +39 -0
  39. data/cookbooks/ruby_rbenv/templates/default/rbenv.sh.erb +15 -0
  40. data/cookbooks/scratchify/Berksfile +1 -7
  41. data/cookbooks/scratchify/Berksfile.lock +6 -0
  42. data/cookbooks/scratchify/README.md +8 -3
  43. data/cookbooks/scratchify/bin/scratchify +1 -1
  44. data/cookbooks/scratchify/from-scratch.gemspec +1 -0
  45. data/cookbooks/scratchify/lib/from-scratch.rb +51 -9
  46. data/cookbooks/scratchify/lib/from-scratch/version.rb +2 -2
  47. data/cookbooks/scratchify/metadata.json +2 -1
  48. data/cookbooks/scratchify/spec/from/scratch_spec.rb +62 -5
  49. data/cookbooks/scratchify/spec/spec_helper.rb +8 -1
  50. data/cookbooks/scratchify/templates/node.json.erb +28 -7
  51. data/cookbooks/scratchify/templates/user.json.erb +1 -1
  52. data/from-scratch.gemspec +1 -0
  53. data/lib/from-scratch.rb +51 -9
  54. data/lib/from-scratch/version.rb +2 -2
  55. data/metadata.rb +1 -0
  56. data/templates/node.json.erb +28 -7
  57. data/templates/user.json.erb +1 -1
  58. metadata +50 -3
@@ -1,15 +1,26 @@
1
1
  require 'securerandom'
2
2
  require 'erb'
3
3
  require 'fileutils'
4
+ require 'optparse'
5
+ require 'ostruct'
4
6
 
5
- module FromScratch
6
- def self.run!
7
- app_name, host = ARGV
8
- ssh_pub_key = `cat ~/.ssh/id_rsa.pub`.strip
9
- postgresql_admin_password = `echo -n '#{SecureRandom.base64(16)}''postgres' | openssl md5 | sed -e 's/.* /md5/'`.strip
10
- postgresql_user_password = SecureRandom.base64(16)
7
+ class FromScratch
8
+ DEFAULTS = { ruby_installer: 'rvm' }
11
9
 
12
- { node: ['nodes', host], user: ['data_bags/users', 'deploy'] }.each do |from, to|
10
+ attr_accessor :options
11
+
12
+ def initialize
13
+ @options = OpenStruct.new DEFAULTS
14
+ end
15
+
16
+ def run!
17
+ parse_options
18
+ get_host_and_app_name
19
+ generate_values
20
+
21
+ { node: ['nodes', @options.host],
22
+ user: ['data_bags/users', 'deploy']
23
+ }.each do |from, to|
13
24
  FileUtils.mkdir_p File.expand_path("../../tmp/#{to[0]}", __FILE__)
14
25
  File.open(File.expand_path("../../tmp/#{to.join('/')}.json", __FILE__), 'w') do |f|
15
26
  f.write ERB.new(File.open(File.expand_path("../../templates/#{from}.json.erb", __FILE__)).read).result(binding)
@@ -17,10 +28,41 @@ module FromScratch
17
28
  end
18
29
 
19
30
  Dir.chdir(File.expand_path('../..', __FILE__)) do
20
- system "knife solo bootstrap root@#{host} -c ./.chef/knife.rb"
21
- system "knife solo clean root@#{host} -c ./.chef/knife.rb"
31
+ system "knife solo bootstrap root@#{@options.host} -c ./.chef/knife.rb"
32
+ system "knife solo clean root@#{@options.host} -c ./.chef/knife.rb"
22
33
  end
23
34
 
24
35
  FileUtils.rm_rf [File.expand_path('../../tmp', __FILE__)]
25
36
  end
37
+
38
+ def get_host_and_app_name
39
+ @options.app_name, @options.host = ARGV.select { |x| !(x =~ /^\-/) }
40
+
41
+ unless @options.app_name && @options.host
42
+ raise ArgumentError, 'You should specify APP_NAME and HOST. Use --help for information.'
43
+ end
44
+ end
45
+
46
+ def parse_options
47
+ opt_parser = OptionParser.new do |args|
48
+ args.banner = "Usage: scratchify your_app_name your.host.com [options]"
49
+
50
+ args.on '--rbenv', 'Use RBENV instead of RVM' do
51
+ @options.ruby_installer = 'rbenv'
52
+ end
53
+
54
+ args.on '-h', '--help', 'Prints this help' do
55
+ puts args
56
+ exit
57
+ end
58
+ end
59
+
60
+ opt_parser.parse!
61
+ end
62
+
63
+ def generate_values
64
+ @options.ssh_pub_key = `cat ~/.ssh/id_rsa.pub`.strip
65
+ @options.postgresql_admin_password = `echo -n '#{SecureRandom.base64(16)}''postgres' | openssl md5 | sed -e 's/.* /md5/'`.strip
66
+ @options.postgresql_user_password = SecureRandom.base64(16)
67
+ end
26
68
  end
@@ -1,3 +1,3 @@
1
- module FromScratch
2
- VERSION = "0.4.0"
1
+ class FromScratch
2
+ VERSION = "0.6.0"
3
3
  end
@@ -13,7 +13,8 @@
13
13
  "user": ">= 0.0.0",
14
14
  "rvm": ">= 0.0.0",
15
15
  "postgresql": ">= 0.0.0",
16
- "nginx": ">= 0.0.0"
16
+ "nginx": ">= 0.0.0",
17
+ "ruby_rbenv": ">= 0.0.0"
17
18
  },
18
19
  "recommendations": {
19
20
  },
@@ -1,11 +1,68 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe From::Scratch do
4
- it 'has a version number' do
5
- expect(From::Scratch::VERSION).not_to be nil
3
+ describe FromScratch do
4
+ let(:instance) { described_class.new }
5
+ let(:app_name) { 'my_app' }
6
+ let(:host) { 'host.example.com' }
7
+
8
+ describe '::new' do
9
+ subject { instance }
10
+
11
+ its('options.to_h') { are_expected.to eq(described_class::DEFAULTS) }
12
+ end
13
+
14
+ describe '#get_host_and_app_name' do
15
+ before do
16
+ stub_const 'ARGV', argv
17
+ end
18
+
19
+ subject { instance.options }
20
+
21
+ context 'with app_name and host' do
22
+ before { instance.get_host_and_app_name }
23
+ let(:argv) { [app_name, host] }
24
+
25
+ its(:app_name) { is_expected.to eq(app_name) }
26
+ its(:host) { is_expected.to eq(host) }
27
+ end
28
+
29
+ context 'without one of app_name or host' do
30
+ let(:argv) { [app_name] }
31
+
32
+ it 'should raise' do
33
+ expect do
34
+ instance.get_host_and_app_name
35
+ end.to raise_error(ArgumentError)
36
+ end
37
+
38
+ context 'and with --option' do
39
+ let(:argv) { [app_name, '--option'] }
40
+
41
+ it 'should raise' do
42
+ expect do
43
+ instance.get_host_and_app_name
44
+ end.to raise_error(ArgumentError)
45
+ end
46
+ end
47
+ end
6
48
  end
7
49
 
8
- it 'does something useful' do
9
- expect(false).to eq(true)
50
+ describe '#parse_options' do
51
+ before do
52
+ stub_const 'ARGV', argv
53
+ instance.parse_options
54
+ end
55
+
56
+ subject { instance.options }
57
+
58
+ context 'with --rbenv' do
59
+ let(:argv) { [app_name, host, '--rbenv'] }
60
+
61
+ its(:ruby_installer) { is_expected.to eq('rbenv') }
62
+ end
63
+ end
64
+
65
+ it 'has a version number' do
66
+ expect(FromScratch::VERSION).not_to be nil
10
67
  end
11
68
  end
@@ -1,2 +1,9 @@
1
1
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
- require 'from/scratch'
2
+ require 'rspec/its'
3
+ require 'from-scratch'
4
+ require 'pry'
5
+
6
+ RSpec.configure do |c|
7
+ c.filter_run focus: true
8
+ c.run_all_when_everything_filtered = true
9
+ end
@@ -1,8 +1,13 @@
1
1
  {
2
2
  "run_list": [
3
3
  "recipe[user::data_bag]",
4
+ <% if @options.ruby_installer == 'rvm' %>
4
5
  "recipe[scratchify::pre_rvm]",
5
6
  "recipe[rvm::user]",
7
+ <% elsif @options.ruby_installer == 'rbenv' %>
8
+ "recipe[ruby_build]",
9
+ "recipe[ruby_rbenv::user]",
10
+ <% end %>
6
11
  "recipe[postgresql::server]",
7
12
  "recipe[postgresql::config_pgtune]",
8
13
  "recipe[postgresql::setup_users]",
@@ -14,6 +19,7 @@
14
19
 
15
20
  "users": ["deploy"],
16
21
 
22
+ <% if @options.ruby_installer == 'rvm' %>
17
23
  "rvm": {
18
24
  "user_default_ruby": "2.2.3",
19
25
  "user_install_rubies": true,
@@ -27,27 +33,42 @@
27
33
  ],
28
34
  "gpg": {}
29
35
  },
36
+ <% elsif @options.ruby_installer == 'rbenv' %>
37
+ "rbenv": {
38
+ "user_installs": [
39
+ { "user": "deploy",
40
+ "rubies": [{
41
+ "name": "2.2.3",
42
+ "environment": {
43
+ "RUBY_CONFIGURE_OPTS": "--disable-install-doc"
44
+ }
45
+ }],
46
+ "global": "2.2.3"
47
+ }
48
+ ]
49
+ },
50
+ <% end %>
30
51
 
31
52
  "postgresql": {
32
53
  "version": "9.4",
33
54
  "enable_pgdg_yum": true,
34
55
  "enable_pgdg_apt": true,
35
56
  "password": {
36
- "postgres": "<%= postgresql_admin_password %>"
57
+ "postgres": "<%= @options.postgresql_admin_password %>"
37
58
  },
38
59
  "users": [
39
60
  {
40
- "username": "<%= app_name %>",
61
+ "username": "<%= @options.app_name %>",
41
62
  "superuser": false,
42
63
  "createdb": false,
43
64
  "login": true,
44
- "password": "<%= postgresql_user_password %>"
65
+ "password": "<%= @options.postgresql_user_password %>"
45
66
  }
46
67
  ],
47
68
  "databases": [
48
69
  {
49
- "name": "<%= app_name %>",
50
- "owner": "<%= app_name %>"
70
+ "name": "<%= @options.app_name %>",
71
+ "owner": "<%= @options.app_name %>"
51
72
  }
52
73
  ],
53
74
  "pg_hba": [
@@ -72,7 +93,7 @@
72
93
  },
73
94
 
74
95
  "scratchify": {
75
- "app_name": "<%= app_name %>"
96
+ "app_name": "<%= @options.app_name %>"
76
97
  },
77
98
 
78
99
  "nginx": {
@@ -81,6 +102,6 @@
81
102
  },
82
103
 
83
104
  "automatic": {
84
- "ipaddress": "<%= host %>"
105
+ "ipaddress": "<%= @options.host %>"
85
106
  }
86
107
  }
@@ -2,5 +2,5 @@
2
2
  "id": "deploy",
3
3
  "name": "deploy",
4
4
  "groups": ["deploy"],
5
- "ssh_keys": ["<%= ssh_pub_key %>"]
5
+ "ssh_keys": ["<%= @options.ssh_pub_key %>"]
6
6
  }
data/from-scratch.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.10"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency 'rspec', "~> 3.3"
24
+ spec.add_development_dependency 'rspec-its'
24
25
  spec.add_development_dependency 'pry', "~> 0.10"
25
26
 
26
27
  spec.add_dependency 'knife-solo'
data/lib/from-scratch.rb CHANGED
@@ -1,15 +1,26 @@
1
1
  require 'securerandom'
2
2
  require 'erb'
3
3
  require 'fileutils'
4
+ require 'optparse'
5
+ require 'ostruct'
4
6
 
5
- module FromScratch
6
- def self.run!
7
- app_name, host = ARGV
8
- ssh_pub_key = `cat ~/.ssh/id_rsa.pub`.strip
9
- postgresql_admin_password = `echo -n '#{SecureRandom.base64(16)}''postgres' | openssl md5 | sed -e 's/.* /md5/'`.strip
10
- postgresql_user_password = SecureRandom.base64(16)
7
+ class FromScratch
8
+ DEFAULTS = { ruby_installer: 'rvm' }
11
9
 
12
- { node: ['nodes', host], user: ['data_bags/users', 'deploy'] }.each do |from, to|
10
+ attr_accessor :options
11
+
12
+ def initialize
13
+ @options = OpenStruct.new DEFAULTS
14
+ end
15
+
16
+ def run!
17
+ parse_options
18
+ get_host_and_app_name
19
+ generate_values
20
+
21
+ { node: ['nodes', @options.host],
22
+ user: ['data_bags/users', 'deploy']
23
+ }.each do |from, to|
13
24
  FileUtils.mkdir_p File.expand_path("../../tmp/#{to[0]}", __FILE__)
14
25
  File.open(File.expand_path("../../tmp/#{to.join('/')}.json", __FILE__), 'w') do |f|
15
26
  f.write ERB.new(File.open(File.expand_path("../../templates/#{from}.json.erb", __FILE__)).read).result(binding)
@@ -17,10 +28,41 @@ module FromScratch
17
28
  end
18
29
 
19
30
  Dir.chdir(File.expand_path('../..', __FILE__)) do
20
- system "knife solo bootstrap root@#{host} -c ./.chef/knife.rb"
21
- system "knife solo clean root@#{host} -c ./.chef/knife.rb"
31
+ system "knife solo bootstrap root@#{@options.host} -c ./.chef/knife.rb"
32
+ system "knife solo clean root@#{@options.host} -c ./.chef/knife.rb"
22
33
  end
23
34
 
24
35
  FileUtils.rm_rf [File.expand_path('../../tmp', __FILE__)]
25
36
  end
37
+
38
+ def get_host_and_app_name
39
+ @options.app_name, @options.host = ARGV.select { |x| !(x =~ /^\-/) }
40
+
41
+ unless @options.app_name && @options.host
42
+ raise ArgumentError, 'You should specify APP_NAME and HOST. Use --help for information.'
43
+ end
44
+ end
45
+
46
+ def parse_options
47
+ opt_parser = OptionParser.new do |args|
48
+ args.banner = "Usage: scratchify your_app_name your.host.com [options]"
49
+
50
+ args.on '--rbenv', 'Use RBENV instead of RVM' do
51
+ @options.ruby_installer = 'rbenv'
52
+ end
53
+
54
+ args.on '-h', '--help', 'Prints this help' do
55
+ puts args
56
+ exit
57
+ end
58
+ end
59
+
60
+ opt_parser.parse!
61
+ end
62
+
63
+ def generate_values
64
+ @options.ssh_pub_key = `cat ~/.ssh/id_rsa.pub`.strip
65
+ @options.postgresql_admin_password = `echo -n '#{SecureRandom.base64(16)}''postgres' | openssl md5 | sed -e 's/.* /md5/'`.strip
66
+ @options.postgresql_user_password = SecureRandom.base64(16)
67
+ end
26
68
  end
@@ -1,3 +1,3 @@
1
- module FromScratch
2
- VERSION = "0.5.0"
1
+ class FromScratch
2
+ VERSION = "0.6.0"
3
3
  end
data/metadata.rb CHANGED
@@ -17,3 +17,4 @@ depends 'user'
17
17
  depends 'rvm'
18
18
  depends 'postgresql'
19
19
  depends 'nginx'
20
+ depends 'ruby_rbenv'
@@ -1,8 +1,13 @@
1
1
  {
2
2
  "run_list": [
3
3
  "recipe[user::data_bag]",
4
+ <% if @options.ruby_installer == 'rvm' %>
4
5
  "recipe[scratchify::pre_rvm]",
5
6
  "recipe[rvm::user]",
7
+ <% elsif @options.ruby_installer == 'rbenv' %>
8
+ "recipe[ruby_build]",
9
+ "recipe[ruby_rbenv::user]",
10
+ <% end %>
6
11
  "recipe[postgresql::server]",
7
12
  "recipe[postgresql::config_pgtune]",
8
13
  "recipe[postgresql::setup_users]",
@@ -14,6 +19,7 @@
14
19
 
15
20
  "users": ["deploy"],
16
21
 
22
+ <% if @options.ruby_installer == 'rvm' %>
17
23
  "rvm": {
18
24
  "user_default_ruby": "2.2.3",
19
25
  "user_install_rubies": true,
@@ -27,27 +33,42 @@
27
33
  ],
28
34
  "gpg": {}
29
35
  },
36
+ <% elsif @options.ruby_installer == 'rbenv' %>
37
+ "rbenv": {
38
+ "user_installs": [
39
+ { "user": "deploy",
40
+ "rubies": [{
41
+ "name": "2.2.3",
42
+ "environment": {
43
+ "RUBY_CONFIGURE_OPTS": "--disable-install-doc"
44
+ }
45
+ }],
46
+ "global": "2.2.3"
47
+ }
48
+ ]
49
+ },
50
+ <% end %>
30
51
 
31
52
  "postgresql": {
32
53
  "version": "9.4",
33
54
  "enable_pgdg_yum": true,
34
55
  "enable_pgdg_apt": true,
35
56
  "password": {
36
- "postgres": "<%= postgresql_admin_password %>"
57
+ "postgres": "<%= @options.postgresql_admin_password %>"
37
58
  },
38
59
  "users": [
39
60
  {
40
- "username": "<%= app_name %>",
61
+ "username": "<%= @options.app_name %>",
41
62
  "superuser": false,
42
63
  "createdb": false,
43
64
  "login": true,
44
- "password": "<%= postgresql_user_password %>"
65
+ "password": "<%= @options.postgresql_user_password %>"
45
66
  }
46
67
  ],
47
68
  "databases": [
48
69
  {
49
- "name": "<%= app_name %>",
50
- "owner": "<%= app_name %>"
70
+ "name": "<%= @options.app_name %>",
71
+ "owner": "<%= @options.app_name %>"
51
72
  }
52
73
  ],
53
74
  "pg_hba": [
@@ -72,7 +93,7 @@
72
93
  },
73
94
 
74
95
  "scratchify": {
75
- "app_name": "<%= app_name %>"
96
+ "app_name": "<%= @options.app_name %>"
76
97
  },
77
98
 
78
99
  "nginx": {
@@ -81,6 +102,6 @@
81
102
  },
82
103
 
83
104
  "automatic": {
84
- "ipaddress": "<%= host %>"
105
+ "ipaddress": "<%= @options.host %>"
85
106
  }
86
107
  }