接著上一篇管理python程式的db schema, 本篇介紹遠端部署到指定環境(prod or staging).
使用的技術是Capistrano.
環境準備
Clone Template
使用下面的命名獲得模版,裡面有準備好的各個config以及基本bin包。
git clone https://github.com/flying-bird/python-db-schema
Install Package
cd python-db-schema
bundle install
Change Config
Update config/deploy/production.rb
default config in python-db-schema/config/deploy/production.rb:
➜ python-db-schema git:(master) less config/deploy/production.rb
# Simple Role Syntax
# ==================
# Supports bulk-adding hosts to roles, the primary server in each group
# is considered to be the first unless any hosts have the primary
# property set. Don`t declare `role :all`, it`s a meta role.
role :app, %w{your_name@prod_env_ip_or_host}
role :web, %w{your_name@prod_env_ip_or_host}
role :db, %w{your_name@prod_env_ip_or_host}
將上面的config的your_name和prod_env_ip_or_host定製成你需要的引數就好。
Update config/deploy.rb
➜ python-db-schema git:(master) less config/deploy.rb
set :application, `python-db-schema`
set :repo_url, `https://github.com/flying-bird/python-db-schema`
set :branch, "master"
set :user, "your_account"
set :deploy_via, :copy
set :linked_dirs, %w{log}
set :deploy_to, `/tmp/your_deploy_path`
將上面的config的your_account,your_deploy_path和repo_url定製成你需要的引數就好。
Deploy
你可以在本地使用下面的command,將code部署到production環境。
cap production deploy
在上述命令執行成功之後,登入到prouction env上check下目錄結構,如下所示:
your_account@production_host: ls /tmp/python-db-schema
current git-ssh.sh releases repo revisions.log shared
your_account@production_host: ls /tmp/python-db-schema/current
Gemfile Gemfile.lock README.md REVISION Rakefile bin config log src
Migrate DB Schema
將code部署到production之後,apply db schema到production環境。
更新config/database.yml
只要將username/password/database改成特定值就好,筆者的配置如下:
staging:
adapter: mysql2
encoding: utf8
pool: 20
username: mysql
password: 123456
socket: /var/lib/mysql/mysql.sock
host: 192.168.10.111
port: 3306
database: dashboard_test
production:
adapter: mysql2
encoding: utf8
pool: 20
username: mysql
password: 123456
socket: /var/lib/mysql/mysql.sock
host: 192.168.10.222
port: 3306
database: dashboard_production
Apply Schema in Production Env
rake db:migrate RAILS_ENV=production
輸出結果如下:
== 20170405024951 CreatePipelineTable: migrating ==============================
-- create_table(:d_pipeline)
-> 0.0355s
== 20170405024951 CreatePipelineTable: migrated (0.0356s) =====================