3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

capistranoのrole一覧をsqliteを使って取得する

Last updated at Posted at 2012-08-31

capistrano + chef-soloでのデプロイが最近よく見かける定番です。しかし、roleの一覧が deploy.rb の中にハードコーディングされてしまうのをなんとかしたいなと思っていました。

なぜならば、PHPやPythonを使ったプロジェクトでcapistranoを使ったときに、その情報を使って処理したいこと(管理用Webサイトなど)があるからです。そこで sqlite3でrole一覧を取得するコードを作ってみました。

deploy.rb
role :server, 'server1, 'server2'

のような deploy.rb に対して

deploy.rb
role(:server) do
  hosts = Array.new
  db = SQLite3::Database.new("serverlist.db")
  db.execute("SELECT name FROM serverlist WHERE role = 'server'") do |host|
    hosts.push(host[0])
  end
  db.close()
  hosts
end

のように書くことで serverlist テーブルに格納された role='server'である一覧を使うことができます。ちなみに serverlist テーブルの定義は以下です。

CREATE TABLE serverlist (
  name varchar(10),
  env  varchar(10),
  role varchar(10)
);

deploy.rb が長くなってしまうのが難点ですが。実は capistranoもrubyも初心者レベルですのでもっとよい方法があれば教えていただければありがたいですw

3
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?