CentOS7 への Django アプリケーションデプロイを Gradle で半自動化
CentOS7 へ Python3 をインストールして Django REST framework アプリケーションのサンプルの動作確認ができたので、Gradleを使ってデプロイ手順の半自動化を試みる。
1.やりたいこと
とっかかりとして、最低限、以下程度のことをやりたい。
- GitからHEADリビジョンを取得
- Zip圧縮してアーカイブを作成
- SSH経由でサーバーへアーカイブをコピー
- アーカイブを展開
2.構成
ここまでで、Visual Studio Code上に作成した構造は以下のような感じ。あと、ワークスペース(django_api_lesson) 直下に以下を作成
- デプロイ用アーカイブ作成用のディレクトリ(buid)を作成
- Gradle用のビルドスクリプト(build.gradle)を作成
- SSH KEY ファイル(id_rsa) を配置。
SSH KEYファイルの作成
Gradle から SSHでログインするために、SSH KEYファイルを、ログイン先のサーバーで作成する。
[root@ganzin ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 28:90:51:e4:6d:a7:ae:8b:36:95:67:f2:83:b5:bf:fc root@ganzin The key's randomart image is: +--[ RSA 2048]----+ | .oo | | + . | | o . o . | | . . + | | ..o S | | +o+ | | . B.. | | o...+. | | ...o. o+oE | +-----------------+
ホームディレクトリの .ssh ディレクトリ配下に、id_rsa ファイルが生成されるので、FTPなどで、ローカルに取得する。
[root@ganzin .ssh]# ls id_rsa id_rsa.pub
3.Gradle ビルドスクリプトの記述
上記、build.gradle に以下のようなスクリプトを記述。
buildscript{
repositories {
jcenter()
}
dependencies {
// SSHプラグインの設定
// https://gradle-ssh-plugin.github.io/
classpath 'org.hidetake:gradle-ssh-plugin:2.9.0'
}
}
plugins {
// SSHプラグインの設定
id 'org.hidetake.ssh' version '2.9.0'
}
// SSHプラグインの設定
apply plugin: 'org.hidetake.ssh'
// デフォルトで実行するタスク順に指定
defaultTasks 'clean', 'export', 'zip', 'deploy'
// 各種設定
final APP_NAME = 'firstapi' // アプリケーション名
final REOMOTE_PATH = '/home/piroto/djangoapp' // デプロイ先ディレクトリ
final BUID_DIR = 'build' // デプロイ用アーカイブ作成先ディレクトリ
final EXPORT_DIR = 'repo' // GITリポジトリExport先ディレクトリ
// SSH接続情報
// $ ssh-keygen -t rsa
// C:\Users\pppiroto\.ssh\known_hosts
remotes {
web01 {
host = '192.168.0.36'
user = 'root'
password = 'password'
// SSH KEYファイルを指定(サーバーで、ssh-keygenで生成し、プロジェクトルートディレクトリに配置)
identity = file('id_rsa')
}
}
// デプロイ用アーカイブ作成先ディレクトリのクリーン
task clean {
doLast {
exec {
executable 'cmd'
args 'rmdir', '/S', '/Q', "${BUID_DIR}\\${EXPORT_DIR}"
}
exec {
executable 'cmd'
args 'del', "${BUID_DIR}\\${APP_NAME}.zip"
}
}
}
// ローカルGitリポジトリからHEADリビジョンをExport
// http://sakebook.hatenablog.com/entry/2014/09/03/084456
// http://qiita.com/scalper/items/1905b47209989dda5648
task export {
doLast {
exec {
executable 'git'
args 'checkout-index', '-a', '-f', "--prefix=${BUID_DIR}/${EXPORT_DIR}/"
}
}
}
// GitリポジトリからExportしたプログラムをZipに圧縮
task zip(type: Zip) {
baseName = "${APP_NAME}"
destinationDir = file("${BUID_DIR}")
exclude "${APP_NAME}\\__pycache__"
into("${APP_NAME}") {
from ("${BUID_DIR}/${EXPORT_DIR}/${APP_NAME}") {
include '**/*.*'
}
}
}
// SSHを利用してデプロイ用アーカイブを送信後、展開
task deploy {
doLast {
ssh.run {
session(remotes.web01) {
execute "rm -R -f ${REOMOTE_PATH}/${APP_NAME}"
execute "rm -f ${REOMOTE_PATH}/${APP_NAME}.zip"
put from:"${projectDir}\\${BUID_DIR}\\${APP_NAME}.zip", into:"${REOMOTE_PATH}/${APP_NAME}.zip"
execute "unzip ${REOMOTE_PATH}/${APP_NAME}.zip -d ${REOMOTE_PATH}"
}
}
}
}
Gradle ssh プラグインで reject HostKey エラー 発生時の対処
4.実行
PS C:\workspaces\vscode\django_api_lesson> gradle :clean Microsoft Windows [Version 10.0.14393] (c) 2016 Microsoft Corporation. All rights reserved. Microsoft Windows [Version 10.0.14393] (c) 2016 Microsoft Corporation. All rights reserved. C:\workspaces\vscode\django_api_lesson>Microsoft Windows [Version 10.0.14393] (c) 2016 Microsoft Corporation. All rights reserved. C:\workspaces\vscode\django_api_lesson>:export :zip UP-TO-DATE :deploy web01#53|Archive: /home/piroto/djangoapp/firstapi.zip web01#53| creating: /home/piroto/djangoapp/firstapi/ web01#53| inflating: /home/piroto/djangoapp/firstapi/db.sqlite3 web01#53| creating: /home/piroto/djangoapp/firstapi/firstapi/ web01#53| inflating: /home/piroto/djangoapp/firstapi/firstapi/settings.py web01#53| inflating: /home/piroto/djangoapp/firstapi/firstapi/urls.py web01#53| inflating: /home/piroto/djangoapp/firstapi/firstapi/wsgi.py web01#53| inflating: /home/piroto/djangoapp/firstapi/firstapi/__init__.py web01#53| inflating: /home/piroto/djangoapp/firstapi/manage.py BUILD SUCCESSFUL Total time: 4.73 secs
OK OK
