capistranoのタスクの例

git cloneは git init、リモートリポジトリの各種設定(※)、git pullを一気に実行

set :application, 'finalge_sample_app'
set :repo_url, 'git@github.com:mumoshu/finagle_sample_app.git'

task :update do
	run_locally do
		application = fetch :application
			if test "[ -d #{application} ]"
				execute "cd #{application}; git pull"
			end
		else
			execute "git clone #{fetch :repo_url} #{application}"
		end
	end
end
task :archive => :update do
	run_locally do
		sbt_output = capture "cd #{fetch :application}; sbt pack-archive"

		sbt_output_without_escape_sequences = sbt_output.lines.map { |line| line.gsub(/\e\[\d{1,2}m/, '')}.join

		archive_relative_path = sbt_output_without_escape_sequences.match(/\[info\] Generating (?<archive_path>.+\.tar\.gz)\s*$/)[:archive_path]
		archive_name = archive_relative_path.match(/(?<archive_name>[^\/]+\.tar\.gz)$/)[:archive_name]
		archive_absolute_path = File.join(capture("cd #{fetch(:application)}; pwd").chomp, archive_relative_path)

		info archive_absolute_path
		info archive_name

		set :archive_absolute_path, archive_absolute_path
		set :archive_name, archive_name
	end
end
task :deploy => :archive do
	archive_path = fetch :archive_absolute_path
	archive_name = fetch :archive_name
	release_path = File.join(fetch(:deploy_to), fetch(:application))

	on roles(:web) do
		unless test "[ -d #{release_path} ]"
			execute "mkdir -p #{release_path}"
		end

		upload! archive_path, release_path

		execute "cd #{release_path}; tar -zxvf #{archive_name}"

		ここでアプリケーションの起動
	end
end

尋常じゃねー 差がありすぎて、追い付ける気が全くしねー
だいたいscalaなんて、全然やりこんでねーぞ。。。
capistranoの理解は若干進んだが、後退している気しかしねー

config/deploy.rb

ステージ間共通の設定を書く
– アプリケーション名
– レポジトリ名
– 利用するSCM
– タスク
– それぞれのタスクで実行するコマンド

※SCMとは?
Software Configuration Management(ソフトウェア構成管理)の略。 ソフトウェア開発プロジェクトの成果物を管理する方法論。

set :repo_url, 'git@github.com:mumoshu/finagle_sample_app'

fetch :repo_url

task

task: uptime do
	task somthing
end
task :uptime do
	run_locally do
		localmachine command
	end
	on server do
		server command
	end
end

task :uptime do
	run_locally do
		localmachine command
	end
	on roles(:web) do
		server command
	end
end

executeによるコマンド実行

task :uptime do
	run_locally do
		localmachine command
		execute "uptime"
	end
	on roles(:web) do
		server command
		execute "uptime"
	end
end

caputerによるコマンド実行結果の取得
output = caputer “uptime”
infoによるログ出力
output = caputer “uptime”
info output

config/deploy/${stagename}.rb

config/deploy/${stagename}.rb

-そのステージで作業対象サーバ
-そのステージだけで実行するタスク

ステージの意味がわからん。タスクのひとまとまりって解釈でOK?

作業対象サーバ例
-ホスト名、サーバーロール、ログインユーザ、SSH設定、その他、そのサーバに紐づく任意の設定

server 'localhost', user: 'vagrant', roles: %w{web}

localhostを対象に、webサーバーというロールを与え、vagrantでログインする

cap install後のconfigファイルを見てみよう

ディレクトリです

config/deploy/test.rb
-作業サーバの設定を行う

config/deploy.rb
-capistranoデフォルトタスクの消去
-タスク「ソースコードの取得」の定義
-タスク「ビルドとアーカイビング・パッケージング」の定義
-タスク「アプリケーションのビルドとインストール」「アプリケーションの起動と停止の定義」

うおーrubyの文法忘れた~
deploy.rb

framework_tasks = [:starting, :started, :updating, :updated, :publishing, :published, :finishing, :finished]

framework_tasks.each do |t|
	Rake::Task["deploy:#{t}"].clear
end

Rake::Task[:deploy].clear

capistranoを使おう

[vagrant@localhost capistrano]$ gem list
capistrano (3.11.0)

既に入ってますね。

cap installでインストールします。
[vagrant@localhost capistrano]$ cap install
mkdir -p config/deploy
create config/deploy.rb
create config/deploy/staging.rb
create config/deploy/production.rb
mkdir -p lib/capistrano/tasks
create Capfile
Capified

Capistranoとは

Ruby製の自動デプロイツール
デプロイ作業をコマンド数行で実行

Capistranoタスク自体は「コマンドを順番に実行する」だけ

覚えること
-Capistranoのモデル
-Capistranoのワークフロー
-Capistranoの設定ファイルの書き方

capistranoを理解する為に必要なこと
-Capistrano
-ライブラリ
-設定ファイル
-ホスト

Capistranoの要素
-capコマンド
-Capistranoのライブラリ
-デフォルトのデプロイタスク

ライブラリやデフォルトタスクを利用して設定ファイルを記述し、capコマンドで事項する。

ライブラリ
Capistranoはフレームワークでライブラリを利用できる。具体的には以下。
-Rubyライブラリ
-Capistrano拡張

設定ファイル
一度だけ使う設定はライブラリではなく、設定ファイルに記述する。
– config/deploy.rb
– config/deploy/任意のステージ名.rb

ホスト
デプロイにあたって、実行する対象マシンは「ローカルマシン」と「サーバー」の二つ

Capistranoのワークフロー
– capitstranoインストール
– 設定ファイルのひな形をつくる
– 設定ファイルのカスタマイズ
– capコマンドを実行

shellでselect

selectを使おう

#!/bin/bash

select color in red blue yellow green; do
case "$color" in
	red)
		echo "stop"
		;;
	blue)
		echo "go"
		;;
	yello)
		echo "caution"
		;;
	*)
		echo "wrong signal"
		break
esac
done

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
1) red
2) blue
3) yellow
4) green
#? 2
go
#? 4
wrong signal

#!/bin/bash

hello(){
  echo "hello ... $1"
}

hello yoshida

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
hello … yoshida

UNIX系(Linux,Mac)はシェルスクリプト、Windowsはバッチ処理
シェルスクリプトに処理を書いていき、実行する

数値比較

#!/bin/bash

read -p "Number? " n
if ((n > 10)); then
	echo "bigger than 10"
fi

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
Number? 12
bigger than 10

#!/bin/bash

for i in {1..5};
do
	echo $i
done

#!/bin/bash

for ((i=1; i<10; i++)); do echo $i done [/code] [code] #!/bin/bash for item in $(date); do echo $item done [/code] [vagrant@localhost shell]$ sed -i 's/\r//' hello [vagrant@localhost shell]$ ./hello 2018年 9月 30日 日曜日 12:11:36 JST [code] #!/bin/bash i=1 while read line; do echo $i "$line" ((i++)) done < colors.txt [/code] [vagrant@localhost shell]$ sed -i 's/\r//' hello [vagrant@localhost shell]$ ./hello 1 red 2 blue [code] #!/bin/bash read -p "Signal color? " color case "$color" in red) echo "stop" ;; blue) echo "go" ;; yello) echo "caution" ;; *) echo "wrong signal" esac [/code] [vagrant@localhost shell]$ sed -i 's/\r//' hello [vagrant@localhost shell]$ ./hello Signal color? red stop

特殊変数

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello yoshida
hello yoshida

#!/bin/bash

echo "hello $1"

$1, $2などを使う

#!/bin/bash

echo "hello $1"

echo $0
echo $#
echo $@

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello a aa aaa
hello a
./hello
3
a aa aaa

ユーザーからの入力を受け取る

#!/bin/bash

read -p "名前: " name
echo "hello ${name}"

[vagrant@localhost shell]$ ./hello
名前: 田中
hello 田中

なんじゃこりゃーー

#!/bin/bash

read -p "Pick 3 colors: " c1 c2 c3
echo $c1
echo $c2
echo $c3

[vagrant@localhost shell]$ ./hello
Pick 3 colors: red geen blue
red
geen
blue

配列

#!/bin/bash

colors=(red blue pink)
colors[1]=silver
colors+=(green orange)
echo ${colors[@]}

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
red silver pink green orange

数値計算

#!/bin/bash

echo $((5 + 2))

n=10
((n=n + 2))
echo $n

if文

read -p "Name? " name
if [ "$name" = "yoshida" ] 
then
	echo "welcome"
else
	echo "you are not allowed"
fi

[vagrant@localhost shell]$ ./hello
Name? yoshida
welcome

#!/bin/bash

read -p "Name? " name
if [ "$name" = "yoshida" ] 
then
	echo "welcome"
elif [ "$name" = "kobayashi" ] 
then
	echo "welcome, too"
else
	echo "you are not allowed"
fi

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
Name? kobayashi
welcome, too

[vagrant@localhost shell]$ ./hello
Name?
empty …

ファイルを比較

#!/bin/bash

if [[ -f $0 ]]; then
	echo "file exists ..."
fi

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
file exists …