远程部署python程序

接着上一篇管理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) =====================
    原文作者:here_bg
    原文地址: https://segmentfault.com/a/1190000008962965
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞