Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails Deployment in AWS

Avatar for mdesanti mdesanti
November 04, 2016

Rails Deployment in AWS

Avatar for mdesanti

mdesanti

November 04, 2016

Other Decks in Programming

Transcript

  1. ARQUITECTURA V1.0 EC2 ▸ Escalamiento sencillo ▸ Mantenimiento del SO

    a cargo del usuario ▸ Monitoreo provisto por AWS (cloudwatch)
  2. ARQUITECTURA V1.0 DEPLOY MANUAL ▸ ssh al server ▸ git

    pull ▸ assets:precompile ▸ Matar procesos del server y sidekiq ▸ Correr migraciones ▸ Levantar sidekiq y server
  3. ARQUITECTURA V1.0 CAPISTRANO group :development do gem 'capistrano', '~> 3.6'

    gem 'capistrano-rails', '~> 1.2’ gem 'capistrano-sidekiq' end bundle install && bundle exec cap install
  4. ARQUITECTURA V1.0 CAPFILE # Load DSL and set up stages

    require "capistrano/setup" # Include default deployment tasks require “capistrano/deploy" # Include tasks from other gems included in your Gemfile # For documentation on these, see for example: # # https://github.com/capistrano/rvm # https://github.com/capistrano/rbenv # https://github.com/capistrano/chruby # https://github.com/capistrano/bundler # https://github.com/capistrano/rails # https://github.com/capistrano/passenger # # require 'capistrano/rvm' # require 'capistrano/rbenv' # require 'capistrano/chruby' # require 'capistrano/bundler' # require 'capistrano/rails/assets' # require 'capistrano/rails/migrations' # require 'capistrano/passenger' # Load custom tasks from `lib/capistrano/tasks` Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
  5. ARQUITECTURA V1.0 DEPLOY.RB # config valid only for current version

    of Capistrano lock '3.6.1' set :application, 'my_app_name' set :repo_url, '[email protected]:me/my_repo.git' # Default branch is :master # ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp # Default deploy_to directory is /var/www/my_app_name # set :deploy_to, '/var/www/my_app_name' # Default value for :linked_files is [] # append :linked_files, 'config/database.yml', 'config/secrets.yml' # Default value for linked_dirs is [] # append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'public/system' # Default value for default_env is {} # set :default_env, { path: "/opt/ruby/bin:$PATH" } # Default value for keep_releases is 5 # set :keep_releases, 5 set :sidekiq_options_per_process, ["--queue high", "--queue default --queue low"]
  6. ARQUITECTURA V1.0 PRODUCTION.RB # server-based syntax # ====================== # Defines

    a single server with a list of roles and multiple properties. # You can define all roles on a single server, or split them: # server 'example.com', user: 'deploy', roles: %w{app db web}, my_property: :my_value # server 'example.com', user: 'deploy', roles: %w{app web}, other_property: :other_value # server 'db.example.com', user: 'deploy', roles: %w{db} # role-based syntax # ================== # Defines a role with one or multiple servers. The primary server in each # group is considered to be the first unless any hosts have the primary # property set. Specify the username and a domain or IP for the server. # Don't use `:all`, it's a meta role. # role :app, %w{[email protected]}, my_property: :my_value # role :web, %w{[email protected] [email protected]}, other_property: :other_value # role :db, %w{[email protected]}
  7. ARQUITECTURA V1.0 EXTENSIONES ÚTILES ▸ capistrano3-puma ▸ capistrano-rails-console ▸ capistrano-rbenv

    ▸ capistrano-postgresql: configura postgres en el server ▸ capistrano3-postgres: manejo de bakcups
  8. ARQUITECTURA V1.0 CONTRAS • Configuración del Sistema Operativo • Single

    point of failure • Manejo de backups de base de datos manual • Almacenamiento en el server
  9. ARQUITECTURA V2.0 VENTAJAS ▸ Bajo mantenimiento de DB ▸ Backups

    automáticos ▸ Restauración de DB a cualquier punto del tiempo en el pasado
  10. ARQUITECTURA V2.0 ALMACENAMIENTO: CONFIGURACIÓN gem ‘carrierwave’ rails generate uploader Avatar

    gem “fog-aws" CarrierWave.configure do |config| config.fog_provider = 'fog/aws' # required config.fog_credentials = { provider: 'AWS', # required aws_access_key_id: 'xxx', # required aws_secret_access_key: 'yyy', # required } config.fog_directory = 'name_of_directory' # required config.fog_attributes = { 'Cache-Control' => "max-age=#{365.day.to_i}" } # optional, defaults to {} end class AvatarUploader < CarrierWave::Uploader::Base storage :fog end
  11. ARQUITECTURA V2.0 UPLOAD V2 CLIENTE SERVER S3 1 2 3

    1.Cliente pide una pressigned url para subir la imagen 2.Server responde con una URL válida por un lapso de tiempo corto 3.Cliente sube la imagen a S3 directamente
  12. ARQUITECTURA V2.0 PRESSIGNED URL #Uploading an object using a pre-signed

    URL for SDK for Ruby - Version 2. require 'aws-sdk-resources' require 'net/http' s3 = Aws::S3::Resource.new(region:'us-west-2') obj = s3.bucket('BucketName').object('KeyName') # Replace BucketName with the name of your bucket. # Replace KeyName with the name of the object you are creating or replacing. url = URI.parse(obj.presigned_url(:put)) return url