遠程部署python程序

接着上一篇管理python程序的db schema, 本篇介紹遠程部署到指定環境(prod or staging).
使用的技術是Capistrano.python

環境準備

Clone Template

使用下面的命名得到模版,裏面有準備好的各個config以及基本bin包。mysql

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:git

➜  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定製成你須要的參數就好。github

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定製成你須要的參數就好。web

Deploy

你能夠在本地使用下面的command,將code部署到production環境。sql

cap production deploy

在上述命令運行成功以後,登陸到prouction env上check下目錄結構,以下所示:segmentfault

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環境。api

更新config/database.yml

只要將username/password/database改爲特定值就好,筆者的配置以下:app

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

輸出結果以下:less

== 20170405024951 CreatePipelineTable: migrating ==============================
-- create_table(:d_pipeline)
   -> 0.0355s
== 20170405024951 CreatePipelineTable: migrated (0.0356s) =====================
相關文章
相關標籤/搜索