From 3bbe9291a2fcee8378b6575fa33abe787b9d1c98 Mon Sep 17 00:00:00 2001 From: Larry Price Date: Sun, 28 Dec 2014 19:11:35 -0500 Subject: [PATCH] openshift deployment implementation --- .gitignore | 2 ++ Rakefile | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ config.ru | 2 -- 3 files changed, 94 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index f8a6236bb06..23554178aaa 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ .sass-cache .gist-cache .pygments-cache +.ruby-version +.ruby-gemset _deploy public sass.old diff --git a/Rakefile b/Rakefile index 099f5644774..9c2cbc45b81 100644 --- a/Rakefile +++ b/Rakefile @@ -270,6 +270,98 @@ multitask :push do end end +desc "Deploy website to OpenShift" +task :openshift do + puts "## Deploying branch to OpenShift" + puts "## Pulling any updates from OpenShift" + cd "#{deploy_dir}" do + Bundler.with_clean_env { + system "git pull origin master" + system "bundle install" + } + end + if Dir.exists? "#{public_dir}" + (Dir["#{deploy_dir}/public/*"]).each { |f| rm_rf(f) } + Rake::Task[:copydot].invoke(public_dir, deploy_dir) + puts "\n## Copying #{public_dir} to #{deploy_dir}/public" + cp_r "#{public_dir}/.", "#{deploy_dir}/public" + end + cd "#{deploy_dir}" do + system "git add -A" + message = "Site updated at #{Time.now.utc}" + puts "\n## Committing: #{message}" + system "git commit -m \"#{message}\"" + puts "\n## Pushing generated #{deploy_dir} website" + Bundler.with_clean_env { system "git push -f origin master" } + puts "\n## OpenShift deploy complete" + end +end + +desc "Set up _deploy folder and deploy branch for Openshift deployment" +task :setup_openshift, [:repo, :force_ssl] do |t, args| + if args.repo + repo_url = args.repo + else + puts "Enter the read/write url for your repository" + puts "(For example, 'ssh://546bcc30e0b8cd7bdc000025@example-username.rhcloud.com/~/git/example.git/')" + repo_url = get_stdin("Repository url: ") + end + + url = /ssh:\/\/\w+@(?\w+-\w+\.rhcloud\.com)/.match(repo_url)[:url] + jekyll_config = IO.read('_config.yml') + jekyll_config.sub!(/^url:.*$/, "url: #{url}") + File.open('_config.yml', 'w') do |f| + f.write jekyll_config + end + rm_rf deploy_dir + mkdir_p "#{deploy_dir}/public" + + cd "#{deploy_dir}" do + system "git init" + system "git remote add origin #{repo_url}" + system "git pull origin master" + end + + cp "config.ru", "#{deploy_dir}/" + + cd "#{deploy_dir}" do + system 'echo "My Octopress Page is coming soon …" > public/index.html' + + # Create Gemfile + File.open("Gemfile", "w") do |f| + f.puts "source 'https://rubygems.org'" + f.puts + f.puts "gem 'sinatra'" + unless args.force_ssl.nil? + f.puts "gem 'rack-ssl'" + end + end + + unless args.force_ssl.nil? + puts "\n## Updating Static Server to force SSL in production" + file = File.readlines("config.ru") + index = file.index { |line| line =~ /class SinatraStaticServer/ } + file.insert(index+1, " configure :production do") + file.insert(index+2, " use Rack::SSL") + file.insert(index+3, " end\n\n") + file.insert(index, "require 'rack/ssl'\n\n") + File.open("config.ru", "w") do |f| + f.puts file + end + end + + system "git add ." + system "git commit -m \"Octopress init\"" + rakefile = IO.read(__FILE__) + rakefile.sub!(/deploy_branch(\s*)=(\s*)(["'])[\w-]*["']/, "deploy_branch\\1=\\2\\3master\\3") + rakefile.sub!(/deploy_default(\s*)=(\s*)(["'])[\w-]*["']/, "deploy_default\\1=\\2\\3openshift\\3") + File.open(__FILE__, 'w') do |f| + f.write rakefile + end + end + puts "\n---\n## Now you can deploy to #{repo_url} with `rake deploy` or `rake openshift` ##" +end + desc "Update configurations to support publishing to root or sub directory" task :set_root_dir, :dir do |t, args| puts ">>> !! Please provide a directory, eg. rake config_dir[publishing/subdirectory]" unless args.dir diff --git a/config.ru b/config.ru index 8e3dc08a771..c1591ab55ff 100644 --- a/config.ru +++ b/config.ru @@ -5,7 +5,6 @@ require 'sinatra/base' $root = ::File.dirname(__FILE__) class SinatraStaticServer < Sinatra::Base - get(/.+/) do send_sinatra_file(request.path) {404} end @@ -19,7 +18,6 @@ class SinatraStaticServer < Sinatra::Base file_path = File.join(file_path, 'index.html') unless file_path =~ /\.[a-z]+$/i File.exist?(file_path) ? send_file(file_path) : missing_file_block.call end - end run SinatraStaticServer