danarchy_deploy 0.1.3 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/Gemfile.lock +9 -20
  4. data/README.md +10 -9
  5. data/Rakefile +0 -4
  6. data/bin/danarchy_deploy +24 -10
  7. data/danarchy_deploy.gemspec +4 -3
  8. data/lib/danarchy_deploy.rb +186 -55
  9. data/lib/danarchy_deploy/applicator.rb +39 -0
  10. data/lib/danarchy_deploy/applicator/nginx.rb +86 -0
  11. data/lib/danarchy_deploy/applicator/phpfpm.rb +84 -0
  12. data/lib/danarchy_deploy/applicator/redmine.rb +40 -0
  13. data/lib/danarchy_deploy/applicator/ssl.rb +14 -0
  14. data/lib/danarchy_deploy/applicator/wordpress.rb +146 -0
  15. data/lib/danarchy_deploy/applicator/wordpress/wpcli.rb +67 -0
  16. data/lib/danarchy_deploy/applicator/wordpress/wpcli_install.sh +36 -0
  17. data/lib/danarchy_deploy/applicator/wordpress/wpconfig.rb +49 -0
  18. data/lib/danarchy_deploy/archiver.rb +9 -7
  19. data/lib/danarchy_deploy/archiver/svn.rb +17 -0
  20. data/lib/danarchy_deploy/hash_deep_merge.rb +9 -0
  21. data/lib/danarchy_deploy/helpers.rb +42 -10
  22. data/lib/danarchy_deploy/services.rb +12 -25
  23. data/lib/danarchy_deploy/services/init.rb +52 -0
  24. data/lib/danarchy_deploy/services/init/openrc.rb +72 -0
  25. data/lib/danarchy_deploy/services/init/systemd.rb +69 -0
  26. data/lib/danarchy_deploy/services/mongodb.rb +162 -0
  27. data/lib/danarchy_deploy/services/mysql.rb +58 -0
  28. data/lib/danarchy_deploy/services/mysql/new_server.rb +72 -0
  29. data/lib/danarchy_deploy/services/mysql/privileges.rb +35 -0
  30. data/lib/danarchy_deploy/system.rb +86 -0
  31. data/lib/danarchy_deploy/system/centos.rb +17 -0
  32. data/lib/danarchy_deploy/system/debian.rb +61 -0
  33. data/lib/danarchy_deploy/system/gentoo.rb +66 -0
  34. data/lib/danarchy_deploy/system/opensuse.rb +22 -0
  35. data/lib/danarchy_deploy/templater.rb +53 -13
  36. data/lib/danarchy_deploy/users.rb +29 -19
  37. data/lib/danarchy_deploy/version.rb +1 -1
  38. data/templates/deploy_template.json +3 -3
  39. metadata +34 -12
@@ -0,0 +1,39 @@
1
+ require_relative 'applicator/wordpress'
2
+ require_relative 'applicator/nginx'
3
+ require_relative 'applicator/phpfpm'
4
+ require_relative 'applicator/ssl'
5
+
6
+ module DanarchyDeploy
7
+ module Applicator
8
+ def self.new(os, user, options)
9
+ puts "\n" + self.name
10
+
11
+ user[:applications].each do |domain, app|
12
+ app[:domain] = domain.to_s
13
+ app[:username] = user[:username]
14
+ app[:path] = app[:path] ? app[:path] : user[:home] + '/' + app[:domain]
15
+
16
+ Dir.exist?(app[:path]) || FileUtils.mkdir_p(app[:path], mode: 0755)
17
+ FileUtils.chown_R(user[:username], user[:username], app[:path])
18
+
19
+ if app[:archives] && options[:first_run]
20
+ puts "\n > Deploying archives for #{domain}"
21
+ perms = { uid: user[:uid], gid: user[:gid] }
22
+ app[:archives].map{|a| a[:perms] = perms }
23
+ puts "\n |> Applying user's ownership to archives: #{perms}"
24
+ DanarchyDeploy::Archiver.new(app[:archives], options)
25
+ end
26
+
27
+ app = DanarchyDeploy::Applicator::PHPFPM.new(app, options) if app[:phpfpm]
28
+ app = DanarchyDeploy::Applicator::Nginx.new(app, options) if app[:nginx]
29
+ app = DanarchyDeploy::Applicator::WordPress.new(app, options) if app[:app] == 'wordpress'
30
+ app = DanarchyDeploy::Applicator::Redmine.new(app, options) if app[:app] == 'redmine'
31
+
32
+ app.delete_if { |k, v| [:username, :domain].include? k }
33
+ user[:applications][domain] = app
34
+ end
35
+
36
+ user
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,86 @@
1
+
2
+ module DanarchyDeploy
3
+ module Applicator
4
+ class Nginx
5
+ def self.new(app, options)
6
+ puts "\n" + self.name
7
+ puts "\n > Checking Nginx configuration for #{app[:username]}."
8
+
9
+ app = generate_paths(app, options)
10
+ if app[:nginx] == 'enabled'
11
+ enable_nginx(app, options)
12
+ elsif app[:nginx] == 'disabled'
13
+ disable_nginx(app, options)
14
+ end
15
+
16
+ app
17
+ end
18
+
19
+ def self.generate_paths(app, options)
20
+ nginx_config = Dir['/etc/nginx/**/nginx.conf'].last
21
+ sites_enabled = nil
22
+
23
+ if nginx_config.nil? && options[:pretend]
24
+ sites_enabled = '/etc/nginx/sites_enabled/'
25
+ puts " - Fake run: Testing deployment using #{sites_enabled}"
26
+ elsif nginx_config.nil? && !options[:pretend]
27
+ abort("\n ! ERROR: Could not establish nginx.conf location!")
28
+ else
29
+ nginx_config = File.readlines(nginx_config)
30
+ sites_enabled = nginx_config.grep(/include/).last.gsub(/\s+include |\*.conf;/, '').chomp.gsub(/\*;/, '')
31
+ end
32
+
33
+ app[:domaincfg] = app[:domaincfg] ?
34
+ app[:domaincfg] :
35
+ "/home/#{app[:username]}/nginx/sites-enabled/#{app[:domain]}.conf"
36
+ # app[:domaincfg] = "/home/#{app[:username]}/nginx/sites-enabled/#{app[:domain]}.conf" #sites_enabled + "#{app[:domain]}.conf"
37
+ app[:log_dir] = app[:log_dir] ?
38
+ app[:log_dir] :
39
+ "/home/#{app[:username]}/nginx/logs/#{app[:domain]}"
40
+ app
41
+ end
42
+
43
+ def self.enable_nginx(app, options)
44
+ if !options[:pretend]
45
+ puts "\n |+ Enabling Nginx for '#{app[:domain]}'."
46
+ FileUtils.mkdir_p([File.dirname(app[:domaincfg]), app[:log_dir]])
47
+ FileUtils.chown_R(app[:username], app[:username], "/home/#{app[:username]}/nginx")
48
+ DanarchyDeploy::Users.add_to_group({username: 'nginx', groups: [app[:username]]}, options)
49
+ end
50
+
51
+ source = options[:deploy_dir] + '/templates/applications/nginx/domain.conf.erb'
52
+ template = { target: app[:domaincfg],
53
+ source: source,
54
+ variables: { username: app[:username],
55
+ domain: app[:domain] },
56
+ dir_perms: { owner: app[:username],
57
+ group: app[:username],
58
+ mode: '0755' },
59
+ file_perms: { owner: app[:username],
60
+ group: app[:username],
61
+ mode: '0644' } }
62
+
63
+ # if app[:ssl]
64
+ # if app[:ssl][:type] == 'letsencrypt'
65
+ # DanarchyDeploy::Applicator::SSL::LetsEncrypt.new(template, options)
66
+ # end
67
+ # end
68
+
69
+ templates = [template]
70
+ DanarchyDeploy::Templater.new(templates, options)
71
+ end
72
+
73
+ def self.disable_nginx(app, options)
74
+ if options[:pretend]
75
+ puts " - Fake run: Remove #{app[:domaincfg]}"
76
+ else
77
+ puts "\n ! Disabling Nginx for '#{app[:domain]}'."
78
+ if File.exist?(app[:domaincfg])
79
+ File.delete(app[:domaincfg])
80
+ puts " |_ Removed: #{app[:domaincfg]}"
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,84 @@
1
+
2
+ module DanarchyDeploy
3
+ module Applicator
4
+ class PHPFPM
5
+ def self.new(app, options)
6
+ puts "\n" + self.name
7
+ puts "\n > Checking #{app[:username]}'s PHP-FPM config."
8
+
9
+ app = generate_paths(app, options)
10
+ if app[:phpfpm] == 'enabled'
11
+ enable_phpfpm(app, options)
12
+ elsif app[:phpfpm] == 'disabled'
13
+ disable_phpfpm(app, options)
14
+ end
15
+
16
+ app
17
+ end
18
+
19
+ def self.generate_paths(app, options)
20
+ phpfpm_config = Dir['/etc/**/php-fpm.conf'].last
21
+ sites_enabled = nil
22
+
23
+ if phpfpm_config.nil? && options[:pretend]
24
+ sites_enabled = '/etc/php/fpm-pretend/sites_enabled/'
25
+ puts " - Fake run: Testing deployment using #{sites_enabled}"
26
+ elsif phpfpm_config.nil? && !options[:pretend]
27
+ abort("\n ! ERROR: Could not establish php-fpm.conf location!")
28
+ else
29
+ puts " |+ Found php-fpm.conf at: #{phpfpm_config}."
30
+ phpfpm_config = File.readlines(phpfpm_config)
31
+ sites_enabled = phpfpm_config.grep(/^include/).last.gsub(/(^.*=|\*$|\*.conf)/, '').chomp
32
+ end
33
+
34
+ app[:phpcfg] = app[:phpcfg] ?
35
+ app[:phpcfg] :
36
+ "/home/#{app[:username]}/php-fpm/sites-enabled/#{app[:domain]}.conf"
37
+ # app[:phpcfg] = sites_enabled + "#{app[:domain].gsub('.','_')}.conf"
38
+ app
39
+ end
40
+
41
+ def self.enable_phpfpm(app, options)
42
+ tmpdir = "/home/#{app[:username]}/tmp"
43
+
44
+ if !options[:pretend]
45
+ puts "\n |+ Enabling PHP-FPM for '#{app[:username]}'."
46
+ FileUtils.mkdir_p(File.dirname(app[:phpcfg]))
47
+ FileUtils.mkdir_p(tmpdir, mode: 1750)
48
+ FileUtils.chown(app[:username], app[:username], tmpdir)
49
+ end
50
+
51
+ pool = app[:domain].gsub('.','_')
52
+ web_user = 'nginx' if app[:nginx]
53
+ web_user = 'apache' if app[:apache]
54
+ source = options[:deploy_dir] + '/templates/applications/php/phpfpm.conf.erb'
55
+ templates = [{ target: app[:phpcfg],
56
+ source: source,
57
+ variables: { pool: pool,
58
+ username: app[:username],
59
+ web_user: web_user,
60
+ tmp: tmpdir },
61
+ dir_perms: { owner: 'root',
62
+ group: 'root',
63
+ mode: '0755' },
64
+ file_perms: { owner: 'root',
65
+ group: 'root',
66
+ mode: '0644' } }]
67
+
68
+ DanarchyDeploy::Templater.new(templates, options)
69
+ end
70
+
71
+ def self.disable_phpfpm(app, options)
72
+ if options[:pretend]
73
+ puts " - Fake run: Remove #{app[:user_phpcfg]}"
74
+ else
75
+ puts "\n ! Disabling PHP-FPM for '#{app[:username]}'."
76
+ if File.exist?(app[:phpcfg])
77
+ File.delete(app[:phpcfg])
78
+ puts " |_ Removed: #{app[:phpcfg]}"
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,40 @@
1
+
2
+ module DanarchyDeploy
3
+ module Applicator
4
+ module Redmine
5
+ def self.new(app, options)
6
+ puts "\n" + self.name
7
+ puts " > Checking on Redmine installation at #{app[:path]}"
8
+
9
+ repo = 'https://svn.redmine.org/redmine/branches/' + app[:version]
10
+ end
11
+
12
+ private
13
+ def self.cmd_prefix(app)
14
+
15
+ end
16
+
17
+ def self.redmine_version(app)
18
+ version = []
19
+ version_rb = File.readlines(app[:path] + '/lib/redmine/version.rb')
20
+ version_rb.grep(/(MAJOR|MINOR|TINY)\s+=/).each do |v|
21
+ v = v.chomp.gsub!(/\s+/, '')
22
+ version.push(v.split(/=/).last)
23
+ end
24
+
25
+ version.join('.')
26
+ end
27
+
28
+ def self.database_yml(app)
29
+ dbs = app[:database]
30
+ dbs.each do |db, values|
31
+ values[:adapter] = values[:adapter] ? values[:adapter] : 'mysql2'
32
+ values[:encoding] = values[:encoding] ? values[:encoding] : 'utf8'
33
+ end
34
+
35
+ db_yml = app[:path] + '/config/database.yml'
36
+ File.write(db_yml, dbs.to_yaml)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+
2
+ module DanarchyDeploy
3
+ module Applicator
4
+ module SSL
5
+ class SelfSigned
6
+
7
+ end
8
+
9
+ class LetsEncrypt
10
+ # DateTime.now > DateTime.parse(cert.grep(/Not After/).first.split(' : ').last) - 7
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,146 @@
1
+ require 'net/http'
2
+ require_relative 'wordpress/wpcli'
3
+ require_relative 'wordpress/wpconfig'
4
+
5
+ module DanarchyDeploy
6
+ module Applicator
7
+ module WordPress
8
+ def self.new(app, options)
9
+ puts "\n" + self.name
10
+ puts " > Checking on WordPress installation at '#{app[:path]}'."
11
+
12
+ app[:prefix] = cmd_prefix(app)
13
+ wpcli = WordPress::WPCLI.new(app, options)
14
+ app = WordPress::WPConfig.new(app, options)
15
+
16
+ # if options[:first_run] &&
17
+ wpcli.install if wpcli.version[:stderr] =~
18
+ /Error: This does not seem to be a WordPress install/
19
+
20
+ siteurl = wpcli.siteurl
21
+ if siteurl =~ /http.*:\/\/(www.|)#{app[:domain]}/
22
+ puts " |+ Siteurl: #{siteurl} found in the WP database. Continuing with deployment."
23
+ elsif siteurl =~ /Error: The site you have requested is not installed/
24
+ puts " |! Domain: #{app[:domain]} not found in current database."
25
+
26
+ db_backup = app[:path] + '/' + app[:domain].gsub('.','_') + '.sql'
27
+ if File.exist?(db_backup) && options[:first_run]
28
+ puts " |+ Importing from local backup."
29
+ import = wpcli.import
30
+ else
31
+ puts " |- . No database content! Skipping deployment of #{app[:domain]}."
32
+ return app
33
+ end
34
+ else
35
+ puts " ! Domain: #{app[:domain]} does not match the database's current siteurl: #{siteurl}"
36
+ puts " |- Skipping #{app[:domain]} deployment"
37
+ return app
38
+ end
39
+
40
+ if app[:autoupdate]
41
+ wpcli.update
42
+ else
43
+ wpcli.check_update
44
+ end
45
+
46
+ app.delete(:prefix)
47
+ app
48
+ end
49
+ # if options[:pretend]
50
+ # puts "\tFake run: Skipping WordPress configuration for #{app[:domain]}"
51
+ # else
52
+ # verify_result = wp_ensure_installed(prefix, app, options)
53
+
54
+ # if verify_result[:stderr]
55
+ # puts " |+ Installing WordPress to: #{app[:path]}"
56
+ # install_result = wp_install(prefix, app, options)
57
+ # abort(' ! WordPress installation failed!') if install_result[:stderr]
58
+ # verify_result = wp_ensure_installed(prefix, app, options)
59
+ # abort(' ! WordPress verification failed!') if verify_result[:stderr]
60
+ # end
61
+
62
+ # puts " |+ WordPress #{verify_result[:stdout].chomp} found!" if verify_result[:stdout]
63
+ # app = verify_generate_wp_salts(app, options)
64
+ # DanarchyDeploy::Templater.new(app[:templates], options)
65
+ # end
66
+
67
+ # puts "\n > Verifying wp-config.php for '#{app[:path]}/wp-config.php'."
68
+ # wp_config_new(app, options)
69
+ # app
70
+ # end
71
+
72
+ private
73
+ def self.cmd_prefix(app)
74
+ "sudo -u #{app[:username]} bash -c "
75
+ # wp_root_mkdir(prefix, app, options)
76
+ # wp_cli_install(options)
77
+ # prefix
78
+ end
79
+
80
+ def self.wp_root_mkdir(prefix, app, options)
81
+ mkdir_cmd = prefix + "'test -d #{app[:path]} || mkdir -v #{app[:path]}'"
82
+ mkdir_result = DanarchyDeploy::Helpers.run_command(mkdir_cmd, options)
83
+
84
+ if mkdir_result[:stderr]
85
+ abort(" ! Failed to create directory: #{app[:path]}!")
86
+ elsif mkdir_result[:stdout]
87
+ puts " |+ Created directory: #{app[:path]}"
88
+ end
89
+ end
90
+
91
+ def self.wp_cli_install(options)
92
+ wpcli_install = 'bash ' + File.expand_path(
93
+ File.dirname(__FILE__) +
94
+ '/wordpress/wpcli_install.sh')
95
+ wpcli_result = DanarchyDeploy::Helpers.run_command(wpcli_install, options)
96
+
97
+ if wpcli_result[:stderr]
98
+ abort(' ! WP-CLI installation failed!')
99
+ else
100
+ puts ' |+ WP-CLI installed.'
101
+ end
102
+ end
103
+
104
+ def self.wp_ensure_installed(prefix, app, options)
105
+ installed = false
106
+ version = nil
107
+
108
+ if Dir.entries(app[:path]) == %w[. ..]
109
+ puts " |+ Installing WordPress to: #{app[:path]}"
110
+ wp_install(prefix, app, options)
111
+ # wp_config_new(app, options)
112
+ # return
113
+ end
114
+
115
+ version = wp_version(prefix, app, options)
116
+ # Error: This does not seem to be a WordPress install.
117
+ end
118
+
119
+ def self.wp_install(prefix, app, options)
120
+ cmd = prefix + "'wp core download --path=#{app[:path]}'"
121
+ DanarchyDeploy::Helpers.run_command(cmd, options)
122
+ end
123
+
124
+ def self.wp_update(prefix, app, options)
125
+ cmd = prefix + "'wp core update --path=#{app[:path]}'"
126
+ DanarchyDeploy::Helpers.run_command(cmd, options)
127
+ end
128
+
129
+ def self.wp_version(prefix, app, options)
130
+ cmd = prefix + "'wp core version --path=#{app[:path]}'"
131
+ DanarchyDeploy::Helpers.run_command(cmd, options)
132
+ end
133
+
134
+ def self.wp_verify_uptodate()
135
+ check_cmd = prefix + "'wp core check-update --path=#{app[:path]}'"
136
+ DanarchyDeploy::Helpers.run_command(check_cmd, options)
137
+ end
138
+
139
+ def self.wp_config_verify(prefix, app, options)
140
+ config_cmd = prefix + "wp config list DB_USER DB_PASSWORD DB_NAME DB_HOST table_prefix --path=#{app[:path]} --format=json"
141
+ wp_config_current = JSON.parse(`#{config_cmd}`)
142
+ p wp_config_current
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,67 @@
1
+ module DanarchyDeploy
2
+ module Applicator
3
+ module WordPress
4
+ class WPCLI
5
+ def initialize(app, options)
6
+ puts "\n > Initializing WordPress CLI"
7
+ @database = app[:database]
8
+ @prefix = app[:prefix]
9
+ @path = app[:path]
10
+ @user = app[:user]
11
+ @options = options
12
+ wpcli_install
13
+ end
14
+
15
+ def install
16
+ cmd = @prefix + "'wp core download --path=#{@path}'"
17
+ DanarchyDeploy::Helpers.run_command(cmd, @options)
18
+ end
19
+
20
+ def update
21
+ cmd = @prefix + "'wp core update --path=#{@path}'"
22
+ DanarchyDeploy::Helpers.run_command(cmd, @options)
23
+ end
24
+
25
+ def version
26
+ cmd = @prefix + "'wp core version --path=#{@path}'"
27
+ DanarchyDeploy::Helpers.run_command(cmd, @options)
28
+ end
29
+
30
+ def check_update
31
+ cmd = @prefix + "'wp core check-update --path=#{@path}'"
32
+ DanarchyDeploy::Helpers.run_command(cmd, @options)
33
+ end
34
+
35
+ def siteurl
36
+ cmd = @prefix + "'wp option get siteurl --path=#{@path}'"
37
+ siteurl = DanarchyDeploy::Helpers.run_command(cmd, @options)
38
+
39
+ if siteurl[:stdout]
40
+ return siteurl[:stdout].chomp
41
+ else
42
+ return siteurl[:stderr]
43
+ end
44
+ end
45
+
46
+ def import
47
+ cmd = @prefix + "'wp db import #{@database[:backup]} --path=#{@path}'"
48
+ DanarchyDeploy::Helpers.run_command(cmd, @options)
49
+ end
50
+
51
+ private
52
+
53
+ def wpcli_install
54
+ install_cmd = 'bash ' + __dir__ + '/wpcli_install.sh'
55
+ wpcli_result = DanarchyDeploy::Helpers.run_command(
56
+ install_cmd, @options)
57
+
58
+ if wpcli_result[:stderr]
59
+ abort(' ! WP-CLI installation failed!')
60
+ else
61
+ puts ' |+ WP-CLI installed.'
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end