capistrano-exfel 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,127 @@
1
+ namespace :app_home do
2
+ desc 'Create on server the necessary placeholders for storing the Application'
3
+ task :create_all do
4
+ invoke 'app_home:create_deploy_folder'
5
+ invoke 'app_home:create_shared_folder'
6
+ invoke 'app_home:create_revisions_file'
7
+ end
8
+
9
+ desc 'Create application deploy folders on server and give it the correct permissions'
10
+ task :create_deploy_folder do
11
+ on roles(:app) do
12
+ sudo_cmd = "echo #{fetch(:password)} | sudo -S"
13
+
14
+ debug '#' * 50
15
+
16
+ debug "mkdir -p #{fetch(:deploy_to)}"
17
+ execute "#{sudo_cmd} mkdir -p #{fetch(:deploy_to)}"
18
+
19
+ debug "chgrp #{fetch(:app_group_owner)} #{fetch(:deploy_to)}"
20
+ execute "#{sudo_cmd} chgrp #{fetch(:app_group_owner)} #{fetch(:deploy_to)}"
21
+
22
+ debug "chmod g+ws #{fetch(:deploy_to)}"
23
+ execute "#{sudo_cmd} chmod g+ws #{fetch(:deploy_to)}"
24
+
25
+ debug '#' * 50
26
+ end
27
+ end
28
+
29
+ desc 'Create shared folder on server DEPLOY folder and give it the correct permissions'
30
+ task :create_shared_folder do
31
+ on roles(:app) do
32
+ sudo_cmd = "echo #{fetch(:password)} | sudo -S"
33
+
34
+ debug '#' * 50
35
+
36
+ debug "mkdir -p #{fetch(:shared_path)}"
37
+ execute "#{sudo_cmd} mkdir -p #{fetch(:shared_path)}"
38
+
39
+ debug "chmod g+ws #{fetch(:shared_path)}"
40
+ execute "#{sudo_cmd} chmod g+ws #{fetch(:shared_path)}"
41
+
42
+ set :shared_config_path, "#{fetch(:shared_path)}/config"
43
+
44
+ debug "mkdir -p #{fetch(:shared_config_path)}"
45
+ execute "#{sudo_cmd} mkdir -p #{fetch(:shared_config_path)}"
46
+
47
+ debug "chmod g+ws #{fetch(:shared_config_path)}"
48
+ execute "#{sudo_cmd} chmod g+ws #{fetch(:shared_config_path)}"
49
+
50
+ debug '#' * 50
51
+ end
52
+ end
53
+
54
+ desc 'create revisions.log file on server DEPLOY folder and give it the correct permissions'
55
+ task :create_revisions_file do
56
+ on roles(:app) do
57
+ debug '#' * 50
58
+
59
+ set :revisions_log_file_path, "#{fetch(:deploy_to)}/revisions.log"
60
+
61
+ debug "touch #{fetch(:revisions_log_file_path)}"
62
+ execute :touch, fetch(:revisions_log_file_path)
63
+
64
+ debug "chmod g+w #{fetch(:revisions_log_file_path)}"
65
+ execute "chmod g+w #{fetch(:revisions_log_file_path)}"
66
+
67
+ debug '#' * 50
68
+ end
69
+ end
70
+
71
+ desc 'Correct shared folder permissions'
72
+ task :correct_shared_permissions do
73
+ on roles(:app), in: :sequence, wait: 5 do
74
+ sudo_cmd = "echo #{fetch(:password)} | sudo -S"
75
+
76
+ debug '#' * 50
77
+
78
+ # Needs access to the folder due to the first write and log rotation
79
+ debug "chown -R nobody.#{fetch(:app_group_owner)} #{fetch(:shared_path)}/log"
80
+ execute "#{sudo_cmd} chown -R nobody.#{fetch(:app_group_owner)} #{fetch(:shared_path)}/log"
81
+
82
+ # # Only files should be changed
83
+ # debug "chown nobody.#{fetch(:app_group_owner)} #{fetch(:shared_path)}/config/*"
84
+ # execute "#{sudo_cmd} chown nobody.#{fetch(:app_group_owner)} #{fetch(:shared_path)}/config/*"
85
+ #
86
+ # debug "chmod 440 #{fetch(:shared_path)}/config/*"
87
+ # execute "#{sudo_cmd} chmod 440 #{fetch(:shared_path)}/config/*"
88
+
89
+ # Needs write permissions
90
+ debug "chown -R nobody.#{fetch(:app_group_owner)} #{fetch(:shared_path)}/tmp/"
91
+ execute "#{sudo_cmd} chown -R nobody.#{fetch(:app_group_owner)} #{fetch(:shared_path)}/tmp/"
92
+
93
+ debug '#' * 50
94
+ end
95
+ end
96
+
97
+ task :reload_server_cache do
98
+ on roles(:app), in: :sequence, wait: 5 do
99
+ debug '#' * 100
100
+ debug "curl https://in.xfel.eu/#{fetch(:app_name_uri)} -v"
101
+ execute :curl, "https://in.xfel.eu/#{fetch(:app_name_uri)} -v"
102
+ debug 'Application visited successfully...'
103
+ debug '#' * 100
104
+ end
105
+ end
106
+
107
+ task :deploy_success_msg do
108
+ on roles(:app), in: :sequence, wait: 5 do
109
+ info '#' * 100
110
+ info '#' * 10 + ' => Application Successfully deployed...'
111
+ info '#' * 100
112
+ info '#' * 10 + " => visit: https://in.xfel.eu/#{fetch(:app_name_uri)}"
113
+ info '#' * 100
114
+ end
115
+ end
116
+
117
+ ###
118
+ # This task doesn't look to be working:
119
+ # desc 'Restart application'
120
+ ###
121
+ task :restart do
122
+ on roles(:app), in: :sequence, wait: 5 do
123
+ info '#' * 10 + ' Touching restart.txt...'
124
+ execute :touch, release_path.join('tmp/restart.txt')
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,224 @@
1
+ ####################################################################################################
2
+ #
3
+ # Capistrano Important Tasks
4
+ #
5
+ #####################################################################################################
6
+ #
7
+ # See all available tasks
8
+ # cap -T
9
+
10
+ # To deploy a new server with capistrano execute:
11
+ # cap [development|test|production] application:deploy_first_time
12
+
13
+ # To deploy the application, including assets compilation:
14
+ # cap [development|test|production] application:deploy
15
+
16
+ # To restart the application, including reloading server cache:
17
+ # cap [development|test|production] application:restart
18
+
19
+ ####################################################################################################
20
+ #
21
+ # Capistrano Internal Tasks
22
+ #
23
+ #####################################################################################################
24
+ #
25
+ # To execute db seed (can only be executed once... to add default roles and users):
26
+ # cap [development|test|production] db:seed
27
+
28
+ # To restart Apache:
29
+ # cap [development|test|production] deploy:restart_apache
30
+
31
+ # To execute rake commands:
32
+ # cap [development|test|production] util:runrake task=secret
33
+
34
+ # XFEL application specific tasks
35
+ namespace :application do
36
+ # Task 'application:deploy' deploys a new version of the application in the specified server
37
+ desc 'Re-deploy existent Application in the specified server'
38
+ task :deploy do
39
+ on roles(:app, :web) do
40
+ info '#' * 100
41
+ info '#' * 10 + ' => Start subsequents times deploy...'
42
+ info '#' * 100
43
+
44
+ # This is advisable to kill users cookies after the upgrade.
45
+ # The consequence is that users will be logged out automatically from the Application after the upgrade.
46
+ # This is important to avoid errors with old validity_token in forms
47
+ invoke 'secrets:update_app_secret'
48
+
49
+ invoke :deploy
50
+ invoke 'application:restart'
51
+ end
52
+ end
53
+
54
+ # Task 'application:deploy_first_time' deploys an application for the first time in the specified server.
55
+ # This task besides deploying the application also make all the necessary configurations
56
+ desc 'Deploy Application for the first time in the specified server'
57
+ task :deploy_first_time do
58
+ on roles(:app, :web) do
59
+ info '#' * 100
60
+ info '#' * 10 + ' => Start first time deploy...'
61
+ info '#' * 100
62
+
63
+ invoke 'app_home:create_all'
64
+ invoke 'haproxy:configure_and_start' # This should go to Puppet
65
+ invoke 'database:configure_mysql'
66
+ invoke 'secrets:configure'
67
+ invoke 'apache:configure_and_start'
68
+ invoke 'apache:check_write_permissions'
69
+ invoke :deploy
70
+ invoke 'app_home:correct_shared_permissions'
71
+ invoke 'application:restart'
72
+ end
73
+ end
74
+
75
+ desc 'Restarts the application, including reloading server cache'
76
+ task :restart do
77
+ # invoke 'app_home:restart'
78
+ invoke 'apache:restart'
79
+ invoke 'app_home:reload_server_cache'
80
+ invoke 'app_home:deploy_success_msg'
81
+ end
82
+
83
+ desc 'Restarts the application, including reloading server cache'
84
+ task :reconfigure_apache do
85
+ invoke 'apache:configure'
86
+ invoke 'application:restart'
87
+ end
88
+
89
+ desc 'Show variables values without deploying'
90
+ task :show_variables do
91
+ on roles(:app, :web) do
92
+ info '#' * 100
93
+ info "username => #{fetch(:username)}"
94
+ info 'password => **********'
95
+ info "rails_env => #{fetch(:rails_env)}"
96
+ info "app_name => #{fetch(:app_name)}"
97
+ info "app_domain => #{fetch(:app_domain)}"
98
+ info "default_app_uri => #{fetch(:default_app_uri)}"
99
+ info "app_name_uri => #{fetch(:app_name_uri)}"
100
+ info "app_full_url => #{fetch(:app_full_url)}"
101
+ info "deploy_to => #{fetch(:deploy_to)}"
102
+ info "shared_path => #{fetch(:shared_path)}"
103
+ info "repo_url => #{fetch(:repo_url)}"
104
+ info "branch => #{fetch(:branch)}"
105
+ info "scm => #{fetch(:scm)}"
106
+ info "format => #{fetch(:format)}"
107
+ info "log_level => #{fetch(:log_level)}"
108
+ info "pty => #{fetch(:pty)}"
109
+ info "linked_files => #{fetch(:linked_files)}"
110
+ info "linked_dirs => #{fetch(:linked_dirs)}"
111
+ info "keep_releases => #{fetch(:keep_releases)}"
112
+ info "use_sudo => #{fetch(:use_sudo)}"
113
+ info "app_group_owner => #{fetch(:app_group_owner)}"
114
+ info "apache_document_root => #{fetch(:apache_document_root)}"
115
+ info "apache_deploy_symbolic_link => #{fetch(:apache_deploy_symbolic_link)}"
116
+ info "tmp_dir => #{fetch(:tmp_dir)}"
117
+ info '#' * 100
118
+ end
119
+ end
120
+ end
121
+
122
+ namespace :load do
123
+ task :defaults do
124
+ # Set username and password
125
+ ask :username, proc { `whoami`.chomp }.call
126
+ set :password, -> { ask('password', nil, echo: false) }
127
+
128
+ # Application Name
129
+ set :app_name, -> { ask('Please specify the application name (i.e. my_app)', 'my_app') }
130
+
131
+ # Build default application URI
132
+ set :default_app_uri, -> do
133
+ case fetch(:rails_env).to_s
134
+ when 'development'
135
+ "dev_#{fetch(:app_name)}"
136
+ when 'test'
137
+ "test_#{fetch(:app_name)}"
138
+ else
139
+ "#{fetch(:app_name)}"
140
+ end
141
+ end
142
+
143
+ # Set application related information
144
+ set :app_domain, -> do
145
+ ask('Please specify the application domain (i.e. https://in.xfel.eu/)', 'https://in.xfel.eu/')
146
+ end
147
+
148
+ set :app_name_uri, -> do
149
+ ask("Please specify the application URI (i.e. #{fetch(:default_app_uri)})", fetch(:default_app_uri))
150
+ end
151
+
152
+ set :app_full_url, -> { "#{fetch(:app_domain)}#{fetch(:app_name_uri)}" }
153
+
154
+ # Default deploy_to directory value is /var/www/
155
+ set :deploy_to, -> { File.join('/data', fetch(:app_name_uri)) }
156
+
157
+ # Shared folder inside deployment directory
158
+ set :shared_path, -> { File.join(fetch(:deploy_to), 'shared') }
159
+
160
+ # Set git repository information
161
+ set :repo_url, -> { '' }
162
+
163
+ # Default branch is :master
164
+ ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call
165
+
166
+ # Default value for :scm is :git
167
+ set :scm, -> { :git }
168
+
169
+ # Default value for :format is :pretty
170
+ set :format, -> { :pretty }
171
+
172
+ # Default value for :log_level is :debug
173
+ set :log_level, -> { :info }
174
+
175
+ # Default value for :pty is false
176
+ set :pty, -> { true }
177
+
178
+ # Default value for :linked_files is []
179
+ set :linked_files, -> { %w(config/database.yml config/secrets.yml) }
180
+
181
+ # Default value for linked_dirs is []
182
+ set :linked_dirs, -> { %w(bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system) }
183
+
184
+ # Default value for keep_releases is 5
185
+ set :keep_releases, -> { 5 }
186
+
187
+ # Sudo related information
188
+ set :use_sudo, -> { true }
189
+ set :app_group_owner, -> { 'exfl_itdm' }
190
+
191
+ # Capistrano::Rails
192
+ #
193
+ # Defaults to 'assets' this should match config.assets.prefix in your rails config/application.rb
194
+ # set :assets_prefix, 'prepackaged-assets'
195
+ #
196
+ # set :assets_roles, [:web, :app] # Defaults to [:web]
197
+ #
198
+ # If you need to touch public/images, public/javascripts and public/stylesheets on each deploy:
199
+ # set :normalize_asset_timestamps, %{public/images public/javascripts public/stylesheets}
200
+
201
+ # Bundle
202
+ # set :bundle_flags, '--quiet' # '--deployment --quiet' is the default
203
+
204
+ # RVM related information
205
+ set :rvm_type, -> { :system }
206
+ # set :rvm_ruby_version, '2.1.2'
207
+ set :rvm_roles, [:app, :web]
208
+ # set :rvm_custom_path, '~/.myveryownrvm' # only needed if not detected
209
+
210
+ # Apache related information
211
+ set :apache_document_root, -> { '/var/www/html/' }
212
+ set :apache_deploy_symbolic_link, -> { "#{fetch(:apache_document_root)}#{fetch(:app_name_uri)}" }
213
+
214
+ # set :tmp_dir, '/home/dh_user_name/tmp'
215
+ set :tmp_dir, -> { File.join('/tmp', fetch(:username)) }
216
+
217
+ # Set umask for remote commands
218
+ SSHKit.config.umask = '0002'
219
+
220
+ # Map commands
221
+ SSHKit.config.command_map[:rake] = 'bundle exec rake'
222
+ SSHKit.config.command_map[:rails] = 'bundle exec rails'
223
+ end
224
+ end
@@ -0,0 +1,106 @@
1
+ namespace :database do
2
+ desc 'Seed default data (roles and common users) to the database'
3
+ task :seed do
4
+ on roles(:app), in: :sequence, wait: 5 do
5
+ execute_rake_command('db:seed')
6
+ end
7
+ end
8
+
9
+ desc 'Create MySQL specific database.yml in the shared path'
10
+ task :configure_mysql do
11
+ on roles(:app) do
12
+ set :database_original_file_name, 'database_mysql.yml'
13
+ invoke 'database:configure_database_file'
14
+ end
15
+ end
16
+
17
+ desc 'Create PostgreSQL specific database.yml in the shared path'
18
+ task :configure_postgresql do
19
+ on roles(:app) do
20
+ set :database_original_file_name, 'database_postgresql.yml'
21
+ invoke 'database:configure_database_file'
22
+ end
23
+ end
24
+
25
+ desc 'Create SQLite specific database.yml in the shared path'
26
+ task :configure_sqlite do
27
+ on roles(:app) do
28
+ set :database_original_file_name, 'database_sqlite.yml'
29
+ invoke 'database:configure_database_file'
30
+ end
31
+ end
32
+
33
+ # desc 'Configure database.yml in the shared path'
34
+ task :configure_database_file do
35
+ on roles(:app) do
36
+ set :database_original_file_path, "config/recipes/config/#{fetch(:database_original_file_name)}"
37
+ set :database_file_path, "#{fetch(:shared_path)}/config/database.yml"
38
+
39
+ invoke 'database:set_permissions_pre_update'
40
+ invoke 'database:set_database_file'
41
+ invoke 'database:set_permissions_post_update'
42
+ end
43
+ end
44
+
45
+ # desc 'Set (create or replace) database.yml in the shared path'
46
+ task :set_database_file do
47
+ on roles(:app) do
48
+ debug '#' * 50
49
+ debug 'Create and configure database.yml file'
50
+
51
+ default_host = '127.0.0.1'
52
+ default_database = "#{fetch(:app_name)}_dev"
53
+ default_username = "#{fetch(:app_name)}_dev"
54
+ default_password = ''
55
+
56
+ set :database_host, ask('Database host:', default_host)
57
+ set :database_name, ask('Database Name:', default_database)
58
+ set :database_username, ask('Database Username:', default_username)
59
+ set :database_password, ask('Database Password:', default_password)
60
+
61
+ upload! StringIO.new(File.read("#{fetch(:database_original_file_path)}")), "#{fetch(:database_file_path)}"
62
+
63
+ execute "sed -i 's/<<database_name>>/#{fetch(:database_name)}/g' #{fetch(:database_file_path)}"
64
+ execute "sed -i 's/<<database_username>>/#{fetch(:database_username)}/g' #{fetch(:database_file_path)}"
65
+ execute "sed -i 's/<<database_password>>/#{fetch(:database_password)}/g' #{fetch(:database_file_path)}"
66
+ execute "sed -i 's/<<database_host>>/#{fetch(:database_host)}/g' #{fetch(:database_file_path)}"
67
+
68
+ debug '#' * 50
69
+ end
70
+ end
71
+
72
+ # desc 'Correct database.yml file permissions before change the file'
73
+ task :set_permissions_pre_update do
74
+ on roles(:app) do
75
+ sudo_cmd = "echo #{fetch(:password)} | sudo -S"
76
+
77
+ debug '#' * 50
78
+
79
+ chmod_command = "chmod -f 777 #{fetch(:database_file_path)} || true"
80
+ debug chmod_command
81
+ execute "#{sudo_cmd} #{chmod_command}"
82
+
83
+ debug '#' * 50
84
+ end
85
+ end
86
+
87
+ # desc 'Correct database.yml file permissions after change the file'
88
+ task :set_permissions_post_update do
89
+ on roles(:app) do
90
+ sudo_cmd = "echo #{fetch(:password)} | sudo -S"
91
+
92
+ debug '#' * 50
93
+
94
+ # Update database.yml user and group owners
95
+ chown_command = "chown nobody.#{fetch(:app_group_owner)} #{fetch(:database_file_path)}"
96
+ debug chown_command
97
+ execute "#{sudo_cmd} #{chown_command}"
98
+
99
+ chmod_command = "chmod 440 #{fetch(:database_file_path)}"
100
+ debug chmod_command
101
+ execute "#{sudo_cmd} #{chmod_command}"
102
+
103
+ debug '#' * 50
104
+ end
105
+ end
106
+ end