from-scratch 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/Berksfile +1 -1
  3. data/Berksfile.lock +7 -4
  4. data/cookbooks/openssl/CHANGELOG.md +43 -2
  5. data/cookbooks/openssl/README.md +149 -55
  6. data/cookbooks/openssl/libraries/helpers.rb +60 -0
  7. data/cookbooks/openssl/libraries/matchers.rb +13 -0
  8. data/cookbooks/openssl/libraries/random_password.rb +82 -0
  9. data/cookbooks/openssl/libraries/secure_password.rb +3 -2
  10. data/cookbooks/openssl/metadata.json +1 -31
  11. data/cookbooks/openssl/providers/dhparam.rb +33 -0
  12. data/cookbooks/openssl/providers/rsa_key.rb +39 -0
  13. data/cookbooks/openssl/providers/x509.rb +44 -34
  14. data/cookbooks/openssl/recipes/upgrade.rb +13 -11
  15. data/cookbooks/openssl/resources/dhparam.rb +10 -0
  16. data/cookbooks/openssl/resources/rsa_key.rb +10 -0
  17. data/cookbooks/openssl/resources/x509.rb +11 -11
  18. data/cookbooks/postgresql/.gitignore +18 -0
  19. data/cookbooks/postgresql/.kitchen.yml +175 -0
  20. data/cookbooks/postgresql/.rspec +3 -0
  21. data/cookbooks/postgresql/CHANGELOG.md +1 -1
  22. data/cookbooks/postgresql/CONTRIBUTING.md +6 -0
  23. data/cookbooks/postgresql/Cheffile +6 -0
  24. data/cookbooks/postgresql/Gemfile +12 -0
  25. data/cookbooks/postgresql/LICENSE +201 -0
  26. data/cookbooks/postgresql/Rakefile +22 -0
  27. data/cookbooks/postgresql/TESTING.md +22 -0
  28. data/cookbooks/postgresql/attributes/default.rb +72 -1
  29. data/cookbooks/postgresql/metadata.json +7 -12
  30. data/cookbooks/postgresql/providers/database.rb +73 -0
  31. data/cookbooks/postgresql/providers/user.rb +82 -0
  32. data/cookbooks/postgresql/recipes/setup_databases.rb +36 -0
  33. data/cookbooks/postgresql/recipes/setup_users.rb +17 -0
  34. data/cookbooks/postgresql/resources/database.rb +20 -0
  35. data/cookbooks/postgresql/resources/user.rb +20 -0
  36. data/cookbooks/postgresql/test/unit/debian_server_spec.rb +72 -0
  37. data/cookbooks/postgresql/test/unit/default_spec.rb +37 -0
  38. data/cookbooks/postgresql/test/unit/server_spec.rb +41 -0
  39. data/cookbooks/postgresql/test/unit/spec_helper.rb +20 -0
  40. data/cookbooks/scratchify/Berksfile +1 -1
  41. data/cookbooks/scratchify/Berksfile.lock +5 -4
  42. data/cookbooks/scratchify/lib/from-scratch.rb +1 -0
  43. data/cookbooks/scratchify/lib/from-scratch/version.rb +1 -1
  44. data/cookbooks/scratchify/templates/node.json.erb +17 -0
  45. data/from-scratch.gemspec +2 -2
  46. data/lib/from-scratch.rb +7 -3
  47. data/lib/from-scratch/version.rb +1 -1
  48. data/templates/node.json.erb +33 -0
  49. metadata +33 -7
@@ -0,0 +1,82 @@
1
+ #
2
+ # Cookbook Name:: postgresql
3
+ # Provider:: user
4
+ #
5
+
6
+ # Support whyrun
7
+ def whyrun_supported?
8
+ true
9
+ end
10
+
11
+ action :create do
12
+ unless @current_resource.exists
13
+ converge_by "Create PostgreSQL User #{new_resource.name}" do
14
+ execute "create postgresql user #{new_resource.name}" do # ~FC009
15
+ user "postgres"
16
+ command %(psql -c "CREATE ROLE #{role_sql}")
17
+ sensitive true
18
+ end
19
+
20
+ new_resource.updated_by_last_action(true)
21
+ end
22
+ end
23
+ end
24
+
25
+ action :update do
26
+ if @current_resource.exists
27
+ converge_by "Update PostgreSQL User #{new_resource.name}" do
28
+ execute "update postgresql user #{new_resource.name}" do
29
+ user "postgres"
30
+ command %(psql -c "ALTER ROLE #{role_sql}")
31
+ sensitive true
32
+ end
33
+
34
+ new_resource.updated_by_last_action(true)
35
+ end
36
+ end
37
+ end
38
+
39
+ action :drop do
40
+ if @current_resource.exists
41
+ converge_by "Drop PostgreSQL User #{new_resource.name}" do
42
+ execute "drop postgresql user #{new_resource.name}" do
43
+ user "postgres"
44
+ command %(psql -c 'DROP ROLE IF EXISTS \\\"#{new_resource.name}\\\"')
45
+ sensitive true
46
+ end
47
+
48
+ new_resource.updated_by_last_action(true)
49
+ end
50
+ end
51
+ end
52
+
53
+ def load_current_resource
54
+ @current_resource = Chef::Resource::PostgresqlUser.new(new_resource.name)
55
+ @current_resource.name(new_resource.name)
56
+
57
+ @current_resource.exists = user_exists?
58
+ end
59
+
60
+ def user_exists?
61
+ exists = %(psql -c "SELECT rolname FROM pg_roles WHERE rolname='#{new_resource.name}'" | grep '#{new_resource.name}') # rubocop:disable LineLength
62
+
63
+ cmd = Mixlib::ShellOut.new(exists, user: "postgres")
64
+ cmd.run_command
65
+ cmd.exitstatus.zero?
66
+ end
67
+
68
+ def role_sql # rubocop:disable AbcSize, MethodLength
69
+ sql = %(\\\"#{new_resource.name}\\\" )
70
+
71
+ %w[superuser createdb createrole inherit replication login].each do |perm|
72
+ sql << "#{"NO" unless new_resource.send(perm)}#{perm.upcase} "
73
+ end
74
+
75
+ sql << if new_resource.encrypted_password
76
+ "ENCRYPTED PASSWORD '#{new_resource.encrypted_password}'"
77
+ elsif new_resource.password
78
+ "PASSWORD '#{new_resource.password}'"
79
+ else
80
+ ""
81
+ end
82
+ end
@@ -0,0 +1,36 @@
1
+ #
2
+ # Cookbook Name:: postgresql
3
+ # Recipe:: setup_databases
4
+ #
5
+
6
+ databases = node["postgresql"]["databases"]
7
+
8
+ # setup databases
9
+ databases.each do |db|
10
+ db_action = (db["action"] || "create").to_sym
11
+ db_extensions = Array(db["extensions"])
12
+ db_languages = Array(db["languages"])
13
+
14
+ postgresql_database db["name"] do
15
+ owner db["owner"]
16
+ encoding db["encoding"]
17
+ template db["template"]
18
+ locale db["locale"]
19
+ action db_action
20
+ end
21
+
22
+ # check for extensions/languages to install from `databases` attribute key
23
+ next unless db_action == :create
24
+
25
+ db_extensions.each do |extension|
26
+ postgresql_extension extension do
27
+ database db["name"]
28
+ end
29
+ end
30
+
31
+ db_languages.each do |language|
32
+ postgresql_language language do
33
+ database db["name"]
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ #
2
+ # Cookbook Name:: postgresql
3
+ # Recipe:: setup_users
4
+ #
5
+
6
+ # setup users
7
+
8
+ node["postgresql"]["users"].each do |user|
9
+ postgresql_user user["username"] do
10
+ superuser user["superuser"]
11
+ createdb user["createdb"]
12
+ login user["login"]
13
+ password user["password"]
14
+ encrypted_password user["encrypted_password"]
15
+ action Array(user["action"] || "create").map(&:to_sym)
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ #
2
+ # Cookbook Name:: postgresql
3
+ # Resource:: database
4
+ #
5
+
6
+ actions :create, :drop
7
+
8
+ default_action :create
9
+
10
+ attribute :name, kind_of: String, name_attribute: true
11
+ attribute :user, kind_of: String, default: "postgres"
12
+ attribute :username, kind_of: String
13
+ attribute :host, kind_of: String
14
+ attribute :port, kind_of: Integer
15
+ attribute :encoding, kind_of: String, default: "UTF-8"
16
+ attribute :locale, kind_of: String, default: "en_US.UTF-8"
17
+ attribute :template, kind_of: String, default: "template0"
18
+ attribute :owner, kind_of: String
19
+
20
+ attr_accessor :exists
@@ -0,0 +1,20 @@
1
+ #
2
+ # Cookbook Name:: postgresql
3
+ # Resource:: user
4
+ #
5
+
6
+ actions :create, :update, :drop
7
+
8
+ default_action :create
9
+
10
+ attribute :name, kind_of: String, name_attribute: true
11
+ attribute :superuser, kind_of: [TrueClass, FalseClass], default: false
12
+ attribute :createdb, kind_of: [TrueClass, FalseClass], default: false
13
+ attribute :createrole, kind_of: [TrueClass, FalseClass], default: false
14
+ attribute :inherit, kind_of: [TrueClass, FalseClass], default: true
15
+ attribute :replication, kind_of: [TrueClass, FalseClass], default: false
16
+ attribute :login, kind_of: [TrueClass, FalseClass], default: true
17
+ attribute :password, kind_of: String
18
+ attribute :encrypted_password, kind_of: String
19
+
20
+ attr_accessor :exists
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'debian::postgresql::server' do
4
+ let(:chef_application) do
5
+ double('Chef::Application',fatal!:false);
6
+ end
7
+ let(:chef_run) do
8
+ runner = ChefSpec::SoloRunner.new(
9
+ platform: 'debian', version: '7.4'
10
+ ) do |node|
11
+ node.automatic['memory']['total'] = '2048kB'
12
+ node.automatic['ipaddress'] = '1.1.1.1'
13
+ node.set['postgresql']['version'] = '9.1'
14
+ node.set['postgresql']['password']['postgres'] = 'password'
15
+ end
16
+ runner.converge('postgresql::server')
17
+ end
18
+ before do
19
+ stub_const('Chef::Application',chef_application)
20
+ allow(File).to receive(:directory?).and_call_original
21
+ allow(File).to receive(:directory?).with('/etc/postgresql/9.1/main').and_return(false)
22
+ stub_command("ls /var/lib/postgresql/9.1/main/recovery.conf").and_return(false)
23
+ end
24
+
25
+ it 'Install postgresql 9.1' do
26
+ expect(chef_run).to install_package('postgresql-9.1')
27
+ end
28
+
29
+ it 'Install postgresql 9.1 client' do
30
+ expect(chef_run).to install_package('postgresql-client-9.1')
31
+ end
32
+
33
+ it 'Install postgresql 9.1 dev files' do
34
+ expect(chef_run).to install_package('libpq-dev')
35
+ end
36
+
37
+ it 'Enable and start service postgresql' do
38
+ expect(chef_run).to enable_service('postgresql')
39
+ expect(chef_run).to start_service('postgresql')
40
+ end
41
+
42
+ it 'Create configuration files' do
43
+ expect(chef_run).to create_template('/etc/postgresql/9.1/main/postgresql.conf')
44
+ expect(chef_run).to create_template('/etc/postgresql/9.1/main/pg_hba.conf')
45
+ end
46
+
47
+ it 'Assign Postgres Password' do
48
+ expect(chef_run).to run_bash('assign-postgres-password')
49
+ end
50
+
51
+ context 'when running as a standby host' do
52
+ it 'does not assign the Postgres password' do
53
+ stub_command("ls /var/lib/postgresql/9.1/main/recovery.conf").and_return(false)
54
+ expect(chef_run).to_not run_bash('assign_postgres_password')
55
+ end
56
+ end
57
+
58
+ it 'Launch Cluster Creation' do
59
+ expect(chef_run).to run_execute('Set locale and Create cluster')
60
+ end
61
+
62
+ context 'Directory /etc/postgresql/9.1/main exist' do
63
+ before do
64
+ allow(File).to receive(:directory?).and_call_original
65
+ allow(File).to receive(:directory?).with('/etc/postgresql/9.1/main').and_return(true)
66
+ end
67
+
68
+ it 'Don\'t launch Cluster Creation' do
69
+ expect(chef_run).to_not run_execute('Set locale and Create cluster')
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'postgresql::default' do
4
+ platforms = {
5
+ 'ubuntu' => {
6
+ 'versions' => ['10.04', '12.04', '14.04']
7
+ },
8
+ 'centos' => {
9
+ 'versions' => ['6.4', '7.0']
10
+ },
11
+ 'redhat' => {
12
+ 'versions' => ['6.5', '7.0']
13
+ },
14
+ 'debian' => {
15
+ 'versions' => ['7.6']
16
+ }
17
+ }
18
+
19
+ platforms.each do |platform, config|
20
+ config['versions'].each do |version|
21
+ context "on #{platform} #{version}" do
22
+ let(:chef_run) {
23
+ ChefSpec::SoloRunner.new(
24
+ :platform => platform.to_s,
25
+ :version => version.to_s
26
+ ) do |node|
27
+ node.set['postgresql']['password']['postgres'] = 'ilikewaffles'
28
+ end.converge(described_recipe)
29
+ }
30
+
31
+ it 'runs no tests' do
32
+ expect(chef_run)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'postgresql::server' do
4
+ platforms = {
5
+ 'ubuntu' => {
6
+ 'versions' => ['10.04', '12.04', '14.04']
7
+ },
8
+ 'centos' => {
9
+ 'versions' => ['6.4', '7.0']
10
+ },
11
+ 'redhat' => {
12
+ 'versions' => ['6.5', '7.0']
13
+ },
14
+ 'debian' => {
15
+ 'versions' => ['7.6']
16
+ }
17
+ }
18
+
19
+ platforms.each do |platform, config|
20
+ config['versions'].each do |version|
21
+ context "on #{platform} #{version}" do
22
+ let(:chef_run) {
23
+ ChefSpec::SoloRunner.new(
24
+ :platform => platform.to_s,
25
+ :version => version.to_s
26
+ ) do |node|
27
+ node.set['postgresql']['password']['postgres'] = 'ilikewaffles'
28
+ end.converge(described_recipe)
29
+ }
30
+
31
+ before do
32
+ stub_command(/ls \/.*\/recovery.conf/).and_return(false)
33
+ end
34
+
35
+ it 'runs no tests' do
36
+ expect(chef_run)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,20 @@
1
+ COOKBOOK_RESOLVERS = {
2
+ 'batali' => ['Batali', 'batali/chefspec'],
3
+ 'berkshelf' => ['Berksfile', 'chefspec/berkshelf'],
4
+ 'librarian' => ['Cheffile', 'chefspec/librarian']
5
+ }
6
+
7
+ require 'chefspec'
8
+
9
+ if ENV['COOKBOOK_RESOLVER']
10
+ require COOKBOOK_RESOLVERS[ENV['COOKBOOK_RESOLVER']]
11
+ else
12
+ resolver_lib = COOKBOOK_RESOLVERS.values.detect do |r_file, _r_lib|
13
+ File.exist?(File.join(File.dirname(__FILE__), '..', '..', r_file))
14
+ end
15
+ fail 'Failed to locate valid cookbook resolver files!' unless resolver_lib
16
+ puts "Resolving cookbooks from #{resolver_lib.first}"
17
+ require resolver_lib.last
18
+ end
19
+
20
+ at_exit { ChefSpec::Coverage.report! }
@@ -7,7 +7,7 @@ metadata
7
7
 
8
8
  cookbook 'user'
9
9
  cookbook 'rvm', github: 'fnichol/chef-rvm'
10
- cookbook 'postgresql'
10
+ cookbook 'postgresql', path: '../postgresql'
11
11
 
12
12
  # cookbook 'dpkg_packages', git: "https://gitlab.acid.cl/acidlabs/chef-dpkg-packages.git"
13
13
  # cookbook 'nginx', git: "https://gitlab.acid.cl/acidlabs/chef-nginx.git"
@@ -1,5 +1,6 @@
1
1
  DEPENDENCIES
2
2
  postgresql
3
+ path: ../postgresql
3
4
  rvm
4
5
  git: git://github.com/fnichol/chef-rvm.git
5
6
  revision: 08ec265f277e112a5a2e4b201bd32ddfe1bb968c
@@ -14,12 +15,12 @@ GRAPH
14
15
  chef-sugar (3.1.1)
15
16
  chef_gem (0.1.0)
16
17
  java (1.35.0)
17
- openssl (4.0.0)
18
- chef-sugar (>= 0.0.0)
19
- postgresql (3.4.20)
18
+ openssl (4.4.0)
19
+ chef-sugar (>= 3.1.1)
20
+ postgresql (3.4.21)
20
21
  apt (>= 1.9.0)
21
22
  build-essential (>= 0.0.0)
22
- openssl (~> 4.0.0)
23
+ openssl (~> 4.0)
23
24
  rvm (0.10.1)
24
25
  chef_gem (>= 0.0.0)
25
26
  java (>= 0.0.0)
@@ -7,6 +7,7 @@ module FromScratch
7
7
  app_name, host = ARGV
8
8
  ssh_pub_key = `cat ~/.ssh/id_rsa.pub`.strip
9
9
  postgresql_admin_password = `echo -n '#{SecureRandom.hex(64)}''postgres' | openssl md5 | sed -e 's/.* /md5/'`.strip
10
+ postgresql_user_password = SecureRandom.hex(16)
10
11
 
11
12
  { node: ['nodes', host], user: ['data_bags/users', 'deploy'] }.each do |from, to|
12
13
  FileUtils.mkdir_p File.expand_path("../../tmp/#{to[0]}", __FILE__)
@@ -1,3 +1,3 @@
1
1
  module FromScratch
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -3,6 +3,8 @@
3
3
  "recipe[rvm::system]",
4
4
  "recipe[postgresql::server]",
5
5
  "recipe[postgresql::config_pgtune]",
6
+ "recipe[postgresql::setup_users]",
7
+ "recipe[postgresql::setup_databases]",
6
8
  "recipe[user::data_bag]",
7
9
  "recipe[scratchify]"
8
10
  ],
@@ -23,6 +25,21 @@
23
25
  "password": {
24
26
  "postgres": "<%= postgresql_admin_password %>"
25
27
  },
28
+ "users": [
29
+ {
30
+ "username": "<%= app_name %>",
31
+ "superuser": false,
32
+ "createdb": false,
33
+ "login": true,
34
+ "password": "<%= postgresql_user_password %>"
35
+ }
36
+ ],
37
+ "databases": [
38
+ {
39
+ "name": "<%= app_name %>",
40
+ "owner": "<%= app_name %>"
41
+ }
42
+ ],
26
43
  "config_pgtune": {
27
44
  "db_type": "web"
28
45
  }
data/from-scratch.gemspec CHANGED
@@ -20,8 +20,8 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.10"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
- spec.add_development_dependency 'rspec', "~> 3.3.0"
24
- spec.add_development_dependency 'pry'
23
+ spec.add_development_dependency 'rspec', "~> 3.3"
24
+ spec.add_development_dependency 'pry', "~> 0.10"
25
25
 
26
26
  spec.add_dependency 'knife-solo'
27
27
  spec.add_dependency 'knife-solo_data_bag'