Capistrano: 設定 shared link 分享目錄

紅寶鐵軌客
Join to follow...
Follow/Unfollow Writer: 紅寶鐵軌客
By following, you’ll receive notifications when this author publishes new articles.
Don't wait! Sign up to follow this writer.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.
寫程式中、折磨中、享受中 ......
662   0  
·
2017/11/17
·
3 mins read


前天,很高興的寫的廣告控制的後台,就快樂的上線了,寫完,當然也有好好的試過,上線後,用戶也用得很高興,就這樣,我又繼續寫其他的案子,但是今天一早,用戶就告訴我說,上傳的照片看不到了,我直覺的就是,好煩啊,一定是你自己沒上傳好,但是用戶最大,還是乖乖的上線看看,咦,真的很奇怪,有照片檔的名稱與連結,但是,照片檔不見了,哈,我的錯,我忘了設定 Capistrano 的 share 連接目錄了。

當時在寫程式時,新增了一個用戶目錄給用戶上傳檔案用,寫完測試沒問題,但是用戶上傳檔案後,如果我又用 Capistrano 更新程式,bundle exec cap production deploy,舊的檔案就會被移到備份,而新的程式目錄則會完全來自新開發上傳的檔案,這一點問題都沒有,但是用戶的資料一定會不見,實務上使用,用戶的資料,不是在資料庫中的,就必須要存在另一個獨立不會被更新的目錄中,這我都知道,但是,寫的時候,想說等一下再設定,就忘了,而這種問題,驗證程式也找不到問題的,所以,就特寫此文,以茲紀念,我想沒人會跟我一樣無腦啦。

在 Capistrano 中,有一個很方便的設定連接目錄方法,在 deploy.rb 中,只要加入這一行,就會自動幫我們設好連結目錄,很簡單,只要將要連結的目錄寫進以下這行就好。

set :linked_dirs, %w{public/uploads tmp/pids log public/flags}

在這個設定中,就指定了:public/uploads,tmp/pids, log,public/flags 四個連結目錄,連結的目錄基本上,如果不變動,沒特別指定,好像也沒人要改變它,就會是在 deploy 目錄下叫做 shared,這個 Capistrano 設定真的很簡單,就把要連結的目錄加上去就好了,以新增一個 public/flags 的連接目錄來說:

  1. 就把它加到 set :linked_dirs 上的大括弧中去,如上面。
  2. 在 server 的 deploy 目錄下,新增一個 public/flags 的目錄,就是:mkdir public/flags

這樣就好了,真簡單。

那到底 capistrano 做了什麼事呢?在執行更新:bundle exec cap production deploy 後,我們可以在 log 中看到以下:

00:08 deploy:symlink:linked_dirs
     ....
      02 rm -rf /home/deploy/apps/new_app/releases/20171117062426/public/flags
    ✔ 02 deploy@192.168.100.200 0.138s
      03 ln -s /home/deploy/apps/new_app/shared/public/flags /home/deploy/apps/new_app/releases/201711…

所以謎底揭曉, Capistrano 會先把上傳新增到 server 的 public/flags 目錄刪掉,再建立一個連結到 shared/public/flags,很標準的一個做法,在實務上,我們還要記得把 public/flags 加到 .gitignore 中,沒加也沒差啦,但是只是傳上去,也是被殺掉,浪費時間頻寬。

就這樣,特寫此文,紀念我的笨錯誤。 


WriterShelf™ is a unique multiple pen name blogging and forum platform. Protect relationships and your privacy. Take your writing in new directions. ** Join WriterShelf**
WriterShelf™ is an open writing platform. The views, information and opinions in this article are those of the author.


Article info

Categories:
Tags:
Total: 707 words


Share this article:
About the Author

很久以前就是個「寫程式的」,其實,什麼程式都不熟⋯⋯
就,這會一點點,那會一點點⋯⋯




Join the discussion now!
Don't wait! Sign up to join the discussion.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.