CentOS7 への Django アプリケーションデプロイを Gradle で半自動化

CentOS7 へ Python3 をインストールして Django REST framework  アプリケーションのサンプルの動作確認ができたので、Gradleを使ってデプロイ手順の半自動化を試みる。

1.やりたいこと

とっかかりとして、最低限、以下程度のことをやりたい。

  1. GitからHEADリビジョンを取得
  2. Zip圧縮してアーカイブを作成
  3. SSH経由でサーバーへアーカイブをコピー
  4. アーカイブを展開

2.構成

ここまでで、Visual Studio Code上に作成した構造は以下のような感じ。あと、ワークスペース(django_api_lesson) 直下に以下を作成

  • デプロイ用アーカイブ作成用のディレクトリ(buid)を作成
  • Gradle用のビルドスクリプト(build.gradle)を作成
  • SSH KEY ファイル(id_rsa) を配置。

django_gradle01

SSH KEYファイルの作成

Gradle から SSHでログインするために、SSH KEYファイルを、ログイン先のサーバーで作成する。

  1. [root@ganzin ~]# ssh-keygen -t rsa
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Created directory '/root/.ssh'.
  5. Enter passphrase (empty for no passphrase):
  6. Enter same passphrase again:
  7. Your identification has been saved in /root/.ssh/id_rsa.
  8. Your public key has been saved in /root/.ssh/id_rsa.pub.
  9. The key fingerprint is:
  10. 28:90:51:e4:6d:a7:ae:8b:36:95:67:f2:83:b5:bf:fc root@ganzin
  11. The key's randomart image is:
  12. +--[ RSA 2048]----+
  13. | .oo |
  14. | + . |
  15. | o . o . |
  16. | . . + |
  17. | ..o S |
  18. | +o+ |
  19. | . B.. |
  20. | o...+. |
  21. | ...o. o+oE |
  22. +-----------------+

ホームディレクトリの .ssh ディレクトリ配下に、id_rsa ファイルが生成されるので、FTPなどで、ローカルに取得する。

  1. [root@ganzin .ssh]# ls
  2. id_rsa id_rsa.pub

3.Gradle ビルドスクリプトの記述

Gradleインストール手順 に従いインストール。

上記、build.gradle に以下のようなスクリプトを記述。

  1. buildscript{
  2. repositories {
  3. jcenter()
  4. }
  5. dependencies {
  6. // SSHプラグインの設定
  7. // https://gradle-ssh-plugin.github.io/
  8. classpath 'org.hidetake:gradle-ssh-plugin:2.9.0'
  9. }
  10. }
  11.  
  12. plugins {
  13. // SSHプラグインの設定
  14. id 'org.hidetake.ssh' version '2.9.0'
  15. }
  16.  
  17. // SSHプラグインの設定
  18. apply plugin: 'org.hidetake.ssh'
  19.  
  20. // デフォルトで実行するタスク順に指定
  21. defaultTasks 'clean', 'export', 'zip', 'deploy'
  22.  
  23. // 各種設定
  24. final APP_NAME = 'firstapi' // アプリケーション名
  25. final REOMOTE_PATH = '/home/piroto/djangoapp' // デプロイ先ディレクトリ
  26. final BUID_DIR = 'build' // デプロイ用アーカイブ作成先ディレクトリ
  27. final EXPORT_DIR = 'repo' // GITリポジトリExport先ディレクトリ
  28.  
  29. // SSH接続情報
  30. // $ ssh-keygen -t rsa
  31. // C:\Users\pppiroto\.ssh\known_hosts
  32. remotes {
  33. web01 {
  34. host = '192.168.0.36'
  35. user = 'root'
  36. password = 'password'
  37. // SSH KEYファイルを指定(サーバーで、ssh-keygenで生成し、プロジェクトルートディレクトリに配置)
  38. identity = file('id_rsa')
  39. }
  40. }
  41.  
  42. // デプロイ用アーカイブ作成先ディレクトリのクリーン
  43. task clean {
  44. doLast {
  45. exec {
  46. executable 'cmd'
  47. args 'rmdir', '/S', '/Q', "${BUID_DIR}\\${EXPORT_DIR}"
  48. }
  49. exec {
  50. executable 'cmd'
  51. args 'del', "${BUID_DIR}\\${APP_NAME}.zip"
  52. }
  53. }
  54. }
  55.  
  56. // ローカルGitリポジトリからHEADリビジョンをExport
  57. // http://sakebook.hatenablog.com/entry/2014/09/03/084456
  58. // http://qiita.com/scalper/items/1905b47209989dda5648
  59. task export {
  60. doLast {
  61. exec {
  62. executable 'git'
  63. args 'checkout-index', '-a', '-f', "--prefix=${BUID_DIR}/${EXPORT_DIR}/"
  64. }
  65. }
  66. }
  67.  
  68. // GitリポジトリからExportしたプログラムをZipに圧縮
  69. task zip(type: Zip) {
  70. baseName = "${APP_NAME}"
  71. destinationDir = file("${BUID_DIR}")
  72. exclude "${APP_NAME}\\__pycache__"
  73. into("${APP_NAME}") {
  74. from ("${BUID_DIR}/${EXPORT_DIR}/${APP_NAME}") {
  75. include '**/*.*'
  76. }
  77. }
  78. }
  79.  
  80. // SSHを利用してデプロイ用アーカイブを送信後、展開
  81. task deploy {
  82. doLast {
  83. ssh.run {
  84. session(remotes.web01) {
  85. execute "rm -R -f ${REOMOTE_PATH}/${APP_NAME}"
  86. execute "rm -f ${REOMOTE_PATH}/${APP_NAME}.zip"
  87. put from:"${projectDir}\\${BUID_DIR}\\${APP_NAME}.zip", into:"${REOMOTE_PATH}/${APP_NAME}.zip"
  88. execute "unzip ${REOMOTE_PATH}/${APP_NAME}.zip -d ${REOMOTE_PATH}"
  89. }
  90. }
  91. }
  92. }

Gradle ssh プラグインで reject HostKey エラー 発生時の対処

4.実行

  1. PS C:\workspaces\vscode\django_api_lesson> gradle
  2. :clean
  3. Microsoft Windows [Version 10.0.14393]
  4. (c) 2016 Microsoft Corporation. All rights reserved.
  5.  
  6. Microsoft Windows [Version 10.0.14393]
  7. (c) 2016 Microsoft Corporation. All rights reserved.
  8.  
  9. C:\workspaces\vscode\django_api_lesson>Microsoft Windows [Version 10.0.14393]
  10. (c) 2016 Microsoft Corporation. All rights reserved.
  11.  
  12. C:\workspaces\vscode\django_api_lesson>:export
  13. :zip UP-TO-DATE
  14. :deploy
  15. web01#53|Archive: /home/piroto/djangoapp/firstapi.zip
  16. web01#53| creating: /home/piroto/djangoapp/firstapi/
  17. web01#53| inflating: /home/piroto/djangoapp/firstapi/db.sqlite3
  18. web01#53| creating: /home/piroto/djangoapp/firstapi/firstapi/
  19. web01#53| inflating: /home/piroto/djangoapp/firstapi/firstapi/settings.py
  20. web01#53| inflating: /home/piroto/djangoapp/firstapi/firstapi/urls.py
  21. web01#53| inflating: /home/piroto/djangoapp/firstapi/firstapi/wsgi.py
  22. web01#53| inflating: /home/piroto/djangoapp/firstapi/firstapi/__init__.py
  23. web01#53| inflating: /home/piroto/djangoapp/firstapi/manage.py
  24.  
  25. BUILD SUCCESSFUL
  26.  
  27. Total time: 4.73 secs

OK OK

Follow me!

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です