diff --git a/Capfile b/Capfile index 9cfa1807..20d02533 100644 --- a/Capfile +++ b/Capfile @@ -22,10 +22,15 @@ require "capistrano/bundler" require "capistrano/rails" require "capistrano/rails/assets" require "capistrano/rails/migrations" -# require "capistrano/passenger" +require "capistrano/scm/git" +install_plugin Capistrano::SCM::Git + +require 'capistrano/puma' +install_plugin Capistrano::Puma, load_hooks: false # Default puma tasks +install_plugin Capistrano::Puma::Monit # if you need the monit tasks +install_plugin Capistrano::Puma::Jungle # if you need the jungle tasks # Load custom tasks from `lib/capistrano/tasks` if you have any defined Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r } Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r } Dir.glob('lib/capistrano/**/*.rb').each { |r| import r } - diff --git a/Gemfile b/Gemfile index 1a15a61a..143907b6 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -ruby '2.3.1' + git_source(:github) do |repo_name| repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") "https://github.com/#{repo_name}.git" @@ -44,7 +44,7 @@ gem 'mini_magick' gem 'jquery-fileupload-rails', '~> 0.4.7' #Report and Printing gems -gem 'cups' +#gem 'cups' #remove for cloud installation gem 'prawn' gem 'prawn-table' gem 'prawn-qrcode' @@ -114,6 +114,7 @@ group :development do gem 'capistrano-bundler' gem 'capistrano-rails' gem 'capistrano-rbenv', github: "capistrano/rbenv" + gem 'capistrano3-puma' end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index c0dd2cf8..db4f94aa 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -62,7 +62,7 @@ GEM builder (3.2.3) byebug (11.0.1) cancancan (1.17.0) - capistrano (3.10.1) + capistrano (3.11.2) airbrussh (>= 1.0.0) i18n rake (>= 10.0.0) @@ -72,6 +72,10 @@ GEM capistrano-rails (1.4.0) capistrano (~> 3.1) capistrano-bundler (~> 1.1) + capistrano3-puma (3.1.1) + capistrano (~> 3.7) + capistrano-bundler + puma (~> 3.4) carrierwave (1.3.1) activemodel (>= 4.0.0) activesupport (>= 4.0.0) @@ -89,7 +93,6 @@ GEM concurrent-ruby (1.1.5) connection_pool (2.2.2) crass (1.0.5) - cups (0.1.10) database_cleaner (1.7.0) diff-lcs (1.3) erubi (1.9.0) @@ -99,8 +102,8 @@ GEM factory_girl_rails (4.9.0) factory_girl (~> 4.9.0) railties (>= 3.0.0) - faker (2.5.0) - i18n (~> 1.6.0) + faker (2.6.0) + i18n (>= 1.6, < 1.8) ffi (1.11.1) filterrific (5.2.1) font-awesome-rails (4.7.0.5) @@ -140,7 +143,7 @@ GEM listen (3.0.8) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) - loofah (2.3.0) + loofah (2.3.1) crass (~> 1.0.2) nokogiri (>= 1.5.9) mail (2.7.1) @@ -223,23 +226,23 @@ GEM chunky_png (~> 1.0) rqrcode_core (~> 0.1.0) rqrcode_core (0.1.0) - rspec-core (3.8.2) - rspec-support (~> 3.8.0) - rspec-expectations (3.8.5) + rspec-core (3.9.0) + rspec-support (~> 3.9.0) + rspec-expectations (3.9.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.8.0) - rspec-mocks (3.8.2) + rspec-support (~> 3.9.0) + rspec-mocks (3.9.0) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.9.0) rspec-rails (3.9.0) actionpack (>= 3.0) activesupport (>= 3.0) railties (>= 3.0) - rspec-core (~> 3.8.0) - rspec-expectations (~> 3.8.0) - rspec-mocks (~> 3.8.0) - rspec-support (~> 3.8.0) - rspec-support (3.8.3) + rspec-core (~> 3.9.0) + rspec-expectations (~> 3.9.0) + rspec-mocks (~> 3.9.0) + rspec-support (~> 3.9.0) + rspec-support (3.9.0) ruby-ole (1.2.12.2) rubyzip (1.0.0) sass (3.7.4) @@ -265,10 +268,9 @@ GEM simple_form (5.0.1) actionpack (>= 5.0) activemodel (>= 5.0) - spreadsheet (1.2.4) + spreadsheet (1.2.5) ruby-ole (>= 1.0) - spring (2.0.2) - activesupport (>= 4.2) + spring (2.1.0) spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) spring (>= 1.2, < 3.0) @@ -322,10 +324,10 @@ DEPENDENCIES capistrano-bundler capistrano-rails capistrano-rbenv! + capistrano3-puma carrierwave (~> 1.0) chartkick coffee-rails (~> 4.2) - cups database_cleaner factory_girl_rails (~> 4.0) faker @@ -370,8 +372,5 @@ DEPENDENCIES web-console (>= 3.3.0) whenever -RUBY VERSION - ruby 2.3.1p112 - BUNDLED WITH 2.0.2 diff --git a/app/views/settings/menus/_form.html.erb b/app/views/settings/menus/_form.html.erb index 15842333..770a18b3 100755 --- a/app/views/settings/menus/_form.html.erb +++ b/app/views/settings/menus/_form.html.erb @@ -7,8 +7,8 @@
<%= f.input :name,:input_html=>{:class=>"col-md-9"} %> - <%= f.input :is_active,:input_html=>{:class=>"col-md-9"} %> - <%= f.input :is_ordering,:input_html=>{:class=>"col-md-9"} %> + <%= f.input :is_active,:input_html=>{:class=>"col-md-9"}, as: :boolean %> + <%= f.input :is_ordering,:input_html=>{:class=>"col-md-9"}, as: :boolean %>
@@ -50,7 +50,7 @@ <% end %>
- +
@@ -58,7 +58,7 @@
<% end %> - +
@@ -81,7 +81,7 @@ 2) <%= t("views.right_panel.button.back") %> - <%= t("views.right_panel.detail.back_txt") %> <%= t("views.right_panel.detail.menu_txt") %>

-
+ - diff --git a/config/deploy.rb b/config/deploy.rb index 498e8284..05bba77d 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -1,20 +1,106 @@ -lock "3.10.1" +# config valid only for current version of Capistrano +lock '3.11.2' -set :application, "zsai" -set :repo_url, 'git@bitbucket.org:code2lab/sxrestaurant.git' +set :application, 'SmartsalesSX' set :deploy_user, 'deploy' +set :deploy_to, '/home/deploy/apps/SmartsalesSX' -set :rbenv_type, :global +# setup repo details +#set :scm, :git +set :repo_url, 'git@gitlab.com:code2lab/SXRestaurant.git' + +# setup rbenv. set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec" - set :rbenv_map_bins, %w{rake gem bundle ruby rails} +# how many old releases do we want to keep, not much set :keep_releases, 5 -set :linked_files, %w{config/database.yml config/secrets.yml config/puma.rb config/sidekiq.yml config/shops.json config/cable.yml} +# files we want symlinking to specific entries in shared +set :linked_files, %w{config/database.yml config/secrets.yml} -set :linked_dirs, %w{bin log tmp/puma tmp/pids tmp/cache tmp/sockets vendor/bundle public/system pids sockets} +# dirs we want symlinking to shared +set :linked_dirs, %w{log tmp/pids tmp/puma tmp/cache tmp/sockets vendor/bundle public/system} +# what specs should be run before deployment is allowed to +# continue, see lib/capistrano/tasks/run_tests.cap set :tests, [] set :pty, true + +set :puma_jungle_conf, '/etc/puma.conf' +set :puma_run_path, '/usr/local/bin/run-puma' +set :puma_bind, %w(tcp://0.0.0.0:9393) + +#set :enable_ssl, true + +# which config files should be copied by deploy:setup_config +# see documentation in lib/capistrano/tasks/setup_config.cap +# for details of operations +set(:config_files, %w( + database.yml + log_rotation + monit.conf + sidekiq_init.sh + sidekiq.yml +)) + +# which config files should be made executable after copying +# by deploy:setup_config +set(:executable_config_files, %w( + sidekiq_init.sh +)) + +# files which need to be symlinked to other parts of the +# filesystem. For example nginx virtualhosts, log rotation +# init scripts etc. The full_app_name variable isn't +# available at this point so we use a custom template {{}} +# tag and then add it at run time. +set(:symlinks, [ + { + source: "sidekiq_init.sh", + link: "/etc/init.d/sidekiq_{{full_app_name}}" + }, + { + source: "log_rotation", + link: "/etc/logrotate.d/{{full_app_name}}" + }, + { + source: "monit", + link: "/etc/monit/conf.d/{{full_app_name}}.conf" + } +]) + +# this: +# http://www.capistranorb.com/documentation/getting-started/flow/ +# is worth reading for a quick overview of what tasks are called +# and when for `cap stage deploy` + +namespace :deploy do + # make sure we're deploying what we think we're deploying + before :deploy, "deploy:check_revision" + # only allow a deploy with passing tests to deployed + before :deploy, "deploy:run_tests" + # compile assets locally then rsync + after 'deploy:symlink:shared', 'deploy:compile_assets_locally' + Rake::Task["deploy:assets:precompile"].clear_actions + + after :finishing, 'deploy:cleanup' + + #nginx will be install in the Load Balancer - No need to deploy there + # remove the default nginx configuration as it will tend + # to conflict with our configs. + #before 'deploy:setup_config', 'nginx:remove_default_vhost' + + # reload nginx to it will pick up any modified vhosts from + # setup_config + #after 'deploy:setup_config', 'nginx:reload' + + # Restart monit so it will pick up any monit configurations + # we've added + #after 'deploy:setup_config', 'monit:restart' + + # As of Capistrano 3.1, the `deploy:restart` task is not called + # automatically. + after 'finishing', 'puma:jungle:restart' +end diff --git a/config/deploy/production.rb b/config/deploy/production.rb index 23a6daf7..bcca8f23 100644 --- a/config/deploy/production.rb +++ b/config/deploy/production.rb @@ -9,13 +9,12 @@ # set :rails_env, :production set :stage, :production -set :server_name, "svr.sxrestaurant.host" +set :server_name, "a.c2l.shop" +set :branch, "r-1902001-01" set :full_app_name, "#{fetch(:application)}_#{fetch(:stage)}" - -server '52.77.219.228', user: 'deploy', roles: %w{web app db}, primary: true +server '167.71.194.57', user: 'deploy', roles: %w{web app db}, primary: true set :deploy_to, "/home/#{fetch(:deploy_user)}/apps/#{fetch(:full_app_name)}" -set :rbenv_ruby, '2.4.1' +set :rbenv_ruby, '2.6.5' set :rails_env, :production - diff --git a/config/initializers/license.rb b/config/initializers/license.rb index e329f313..31510a0f 100644 --- a/config/initializers/license.rb +++ b/config/initializers/license.rb @@ -1,6 +1,8 @@ -if File.exist?("config/license.yml") +if File.exist?("config/license.yml") config = YAML.load_file(Rails.root.join("config/license.yml")) config.fetch(Rails.env, {}).each do |key, value| ENV[key.upcase] = value.to_s end -end \ No newline at end of file +else + ENV["SERVER_MODE"] = "cloud" +end diff --git a/docker-compose.yml b/docker-compose.yml index d1a961f7..ab61383d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,31 +1,33 @@ version: '3' services: - code2lab: - build: - context: . - dockerfile: ./Dockerfile - links: - - redis - volumes: - - .:/sxrestaurant - env_file: - - .code2lab.env - ports: - - '8082:62158' - environment: - - REDIS_URL=redis://redis:6379/0 - sidekiq: - build: . - command: bundle exec sidekiq -C config/sidekiq.yml - links: - - redis - volumes: - - .:/sxrestaurant - environment: - - REDIS_URL=redis://redis:6379/0 - redis: - image: redis - ports: - - '6380:6379' - volumes: - - ../data/redis:/data \ No newline at end of file + code2lab: + build: + context: . + dockerfile: ./Dockerfile + links: + - redis + volumes: + - .:/sxrestaurant + - "/etc/timezone:/etc/timezone:ro" + - "/etc/localtime:/etc/localtime:ro" + env_file: + - .code2lab.env + ports: + - '62160:62158' + environment: + - REDIS_URL=redis://172.17.0.1:6380/0 + sidekiq: + build: . + command: bundle exec sidekiq -C config/sidekiq.yml + links: + - redis + volumes: + - .:/sxrestaurant + environment: + - REDIS_URL=redis://172.17.0.1:6380/0 + redis: + image: redis + ports: + - '6380:6379' + volumes: + - ../data/redis:/data diff --git a/lib/capistrano/substitute_strings.rb b/lib/capistrano/substitute_strings.rb new file mode 100644 index 00000000..d4b24432 --- /dev/null +++ b/lib/capistrano/substitute_strings.rb @@ -0,0 +1,12 @@ +# we often want to refer to variables which +# are defined in subsequent stage files. This +# let's us use the {{var}} to represent fetch(:var) +# in strings which are only evaluated at runtime. + +def sub_strings(input_string) + output_string = input_string + input_string.scan(/{{(\w*)}}/).each do |var| + output_string.gsub!("{{#{var[0]}}}", fetch(var[0].to_sym)) + end + output_string +end diff --git a/lib/capistrano/tasks/check_revision.cap b/lib/capistrano/tasks/check_revision.cap new file mode 100644 index 00000000..72b4aa12 --- /dev/null +++ b/lib/capistrano/tasks/check_revision.cap @@ -0,0 +1,14 @@ +namespace :deploy do + desc "checks whether the currently checkout out revision matches the + remote one we're trying to deploy from" + task :check_revision do + branch = fetch(:branch) + unless `git rev-parse HEAD` == `git rev-parse #{branch}` + puts "WARNING: HEAD is not the same as #{branch}" + puts "Run `git push` to sync changes or make sure you've" + puts "checked out the branch: #{branch} as you can only deploy" + puts "if you've got the target branch checked out" + exit + end + end +end diff --git a/lib/capistrano/tasks/compile_assets_locally.cap b/lib/capistrano/tasks/compile_assets_locally.cap new file mode 100644 index 00000000..e6264e81 --- /dev/null +++ b/lib/capistrano/tasks/compile_assets_locally.cap @@ -0,0 +1,17 @@ +namespace :deploy do + desc "compiles assets locally then rsyncs" + task :compile_assets_locally do + run_locally do + execute "RAILS_ENV=#{fetch(:rails_env)} bundle exec rake assets:precompile" + end + on roles(:app) do |role| + run_locally do + execute"rsync -av ./public/assets/ #{role.user}@#{role.hostname}:#{release_path}/public/assets/;" + end + sudo "chmod -R 755 #{release_path}/public/assets/" + end + run_locally do + execute "rm -rf ./public/assets" + end + end +end diff --git a/lib/capistrano/tasks/logs.cap b/lib/capistrano/tasks/logs.cap new file mode 100644 index 00000000..7bbfac2e --- /dev/null +++ b/lib/capistrano/tasks/logs.cap @@ -0,0 +1,14 @@ +namespace :logs do + task :tail, :file do |t, args| + if args[:file] + on roles(:app) do + execute "tail -f #{shared_path}/log/#{args[:file]}.log" + end + else + puts "please specify a logfile e.g: 'rake logs:tail[logfile]" + puts "will tail 'shared_path/log/logfile.log'" + puts "remember if you use zsh you'll need to format it as:" + puts "rake 'logs:tail[logfile]' (single quotes)" + end + end +end diff --git a/lib/capistrano/tasks/monit.cap b/lib/capistrano/tasks/monit.cap new file mode 100644 index 00000000..5dbac6a2 --- /dev/null +++ b/lib/capistrano/tasks/monit.cap @@ -0,0 +1,10 @@ +namespace :monit do + %w(start stop restart).each do |task_name| + desc "#{task_name} Monit" + task task_name do + on roles(:app), in: :sequence, wait: 5 do + sudo "service monit #{task_name}" + end + end + end +end diff --git a/lib/capistrano/tasks/nginx.cap b/lib/capistrano/tasks/nginx.cap new file mode 100644 index 00000000..dfadd5c6 --- /dev/null +++ b/lib/capistrano/tasks/nginx.cap @@ -0,0 +1,22 @@ +namespace :nginx do + %w(start stop restart reload).each do |task_name| + desc "#{task } Nginx" + task task_name do + on roles(:app), in: :sequence, wait: 5 do + sudo "/etc/init.d/nginx #{task_name}" + end + end + end + + desc "Remove default Nginx Virtual Host" + task "remove_default_vhost" do + on roles(:app) do + if test("[ -f /etc/nginx/sites-enabled/default ]") + sudo "rm /etc/nginx/sites-enabled/default" + puts "removed default Nginx Virtualhost" + else + puts "No default Nginx Virtualhost to remove" + end + end + end +end diff --git a/lib/capistrano/tasks/restart.cap b/lib/capistrano/tasks/restart.cap new file mode 100644 index 00000000..ff8c028f --- /dev/null +++ b/lib/capistrano/tasks/restart.cap @@ -0,0 +1,10 @@ +namespace :deploy do + desc 'Commands for unicorn application' + %w(start stop force-stop restart upgrade reopen-logs).each do |command| + task command.to_sym do + on roles(:app), in: :sequence, wait: 5 do + sudo "/etc/init.d/unicorn_#{fetch(:full_app_name)} #{command}" + end + end + end +end diff --git a/lib/capistrano/tasks/run_tests.cap b/lib/capistrano/tasks/run_tests.cap new file mode 100644 index 00000000..32f26f9c --- /dev/null +++ b/lib/capistrano/tasks/run_tests.cap @@ -0,0 +1,18 @@ +namespace :deploy do + desc "Runs test before deploying, can't deploy unless they pass" + task :run_tests do + test_log = "log/capistrano.test.log" + tests = fetch(:tests) + tests.each do |test| + puts "--> Running tests: '#{test}', please wait ..." + unless system "bundle exec rspec #{test} > #{test_log} 2>&1" + puts "--> Tests: '#{test}' failed. Results in: #{test_log} and below:" + system "cat #{test_log}" + exit; + end + puts "--> '#{test}' passed" + end + puts "--> All tests passed" + system "rm #{test_log}" + end +end diff --git a/lib/capistrano/tasks/setup_config.cap b/lib/capistrano/tasks/setup_config.cap new file mode 100644 index 00000000..81ce88d9 --- /dev/null +++ b/lib/capistrano/tasks/setup_config.cap @@ -0,0 +1,33 @@ +namespace :deploy do + task :setup_config do + on roles(:app) do + # make the config dir + execute :mkdir, "-p #{shared_path}/config" + full_app_name = fetch(:full_app_name) + + # config files to be uploaded to shared/config, see the + # definition of smart_template for details of operation. + # Essentially looks for #{filename}.erb in deploy/#{full_app_name}/ + # and if it isn't there, falls back to deploy/#{shared}. Generally + # everything should be in deploy/shared with params which differ + # set in the stage files + config_files = fetch(:config_files) + config_files.each do |file| + smart_template file + end + + # which of the above files should be marked as executable + executable_files = fetch(:executable_config_files) + executable_files.each do |file| + execute :chmod, "+x #{shared_path}/config/#{file}" + end + + # symlink stuff which should be... symlinked + symlinks = fetch(:symlinks) + + symlinks.each do |symlink| + sudo "ln -nfs #{shared_path}/config/#{symlink[:source]} #{sub_strings(symlink[:link])}" + end + end + end +end diff --git a/lib/capistrano/template.rb b/lib/capistrano/template.rb new file mode 100644 index 00000000..07bd0be3 --- /dev/null +++ b/lib/capistrano/template.rb @@ -0,0 +1,32 @@ +# will first try and copy the file: +# config/deploy/#{full_app_name}/#{from}.erb +# to: +# shared/config/to +# if the original source path doesn exist then it will +# search in: +# config/deploy/shared/#{from}.erb +# this allows files which are common to all enviros to +# come from a single source while allowing specific +# ones to be over-ridden +# if the target file name is the same as the source then +# the second parameter can be left out +def smart_template(from, to=nil) + to ||= from + full_to_path = "#{shared_path}/config/#{to}" + if from_erb_path = template_file(from) + from_erb = StringIO.new(ERB.new(File.read(from_erb_path)).result(binding)) + upload! from_erb, full_to_path + info "copying: #{from_erb} to: #{full_to_path}" + else + error "error #{from} not found" + end +end + +def template_file(name) + if File.exist?((file = "config/deploy/#{fetch(:full_app_name)}/#{name}.erb")) + return file + elsif File.exist?((file = "config/deploy/shared/#{name}.erb")) + return file + end + return nil +end