動かざることバグの如し

近づきたいよ 君の理想に

Capistrano3でdefault_envとかwithinが効かないとき

環境

症状

Capistranoは非常に便利なデプロイツールで、Railsをデプロイするなら必須だと思う。いろいろ機能があって、その中でもdefault_envでexecuteするときの環境変数を設定できたり、withinでそのディレクトリの中で実行とかできる。。はずなのだが何回やってもうまくいかない

原因

書き方が悪い

どういうことか

withinの場合。例えばrelease_path以下内で実行したいとき、以下は有効

task :touch do
    on roles(:all) do
      within release_path do
        execute :touch, "hogehoge.txt"
      end
    end
  end

下はうまくいかない例

# not work!!!
task :touch do
    on roles(:all) do
      within release_path do
        execute "touch hogehoge.txt"
      end
    end
  end

違いがおわかりだろうか、executeのあとにシンボルを渡さないとwithinは効かない

だからexecuteを実行する際は、

execute :実行したいコマンド名 "コマンドの引数"

にしなくてはいけない

default_envにしてもそうで、

set :default_env, {
  "PATH" => "/opt/php/php-7.1.3/bin:$PATH"
}

とかで環境変数をセットできるがexecuteをシンボルで渡さないと反映されない。

割とハマってのでメモ てか駄目なら駄目でエラーとかになってくれ。。。

参考リンク

iPhoneで見ているサイトをURLとタイトルで一発で共有ツイートさせる方法

やりたいこと

iOSSafariで見ているサイトを一発でツイートできるようになる。しかもURLとタイトル、更には選択範囲の引用も可能

以下の動画がデモ

昔はブックマークレットでゴリゴリやってたけど、流石にモバイルでそれはつらい、、ということで代案探してたらショートカット駆使すればいいことを知った。

用意するもの

  • iPhone
  • ショートカットアプリ
    • もともとWorkflowって名前のサードパーティアプリだったけどAppleが買収して「ショートカット」という名前で公式リリースするようになった

手順

まずSafari特有の共有メニューにする必要がある。最初は右上の「二」みたいなトグルアイコンをクリック

すると個別設定画面に行くので、「共有シートに表示」をON、下の「受け入れの種類」をタップしてSafariのみにする。最終的に以下のようになっていればOK

さっきの画面に戻って、下の検索バーから「テキスト」と入力して「テキスト」を選択

するとフロー一覧にテキストが追加される。

そしたらテキストをタップして「マジックステッキのようなアイコン」をタップ

すると「マジック変数の選択」になるので、上の「ショートカットの入力」をタップ

タップした「ショートカットの入力」がテキストの中に入るので、もう一度タップ。すると今度はSafariから受け取ったどのデータを出力するか選択するのでが出るので「ページのURL」を選択

他にもタイトルとか、お好みで選択範囲とか繰り返して追加していくと、以下のようになるはず

f:id:thr3a:20190313012330p:plain

最後にツイートをしたいので、さっきの検索バーに「ツイッター」と入力して「ツイート」を選択

フロー一覧にツイートが追加されれば無事完了 右上の「完了」をタップ

あとは共有したいページで共有→ショートカットで作ったショートカットを選択すれば一発で共有ツイートが生成される。

まとめ

  • ショートカットマジ便利

rbenvでインストールした全バージョンのrubyに一括gem installするスクリプト

何がしたいか

VS codeを使うようになったんだけど、Rubyの保管をするために拡張機能でsolargraphをインストールした

これは非常に便利で、まぁruby書くなら必須拡張機能だよね、ってことでおすすめなんだけど、1点欠点があって、rubyのライブラリに依存している点

インストールしないと

Solargraph gem not found

と怒られる。対処は簡単で、

gem install solargraph

してVS codeを再起動すればいいんだけど、rbenvでrubyのバージョンを複数インストールしているとツラい。なぜならバージョンごとによって当然gemのsolargraphも管理されているわけで、全バージョンにインストールしないと毎回VS codeに怒られる。てかsolargraphが使えない。

対処法

自動で一括インストールするシェルスクリプト書いた。

eval "$(rbenv init -)"
for v in `rbenv versions --bare`
do
rbenv shell $v
ruby -v
gem install solargraph
done

これで各バージョンに対してgem install solargraphが実行される。ビルドが多いので、結構時間かかるかも(

LinuxでL2TP/IPsecなVPNサーバーを構築する

自宅で動いているTinker Board(ARM)を使ってVPNサーバーを構築してみた。

PPTPVPNサーバーを構築するのは割と楽なんだが、セキュリティ上の理由から、Macではその認証経由では接続できない。ということで、L2TP/IPsecしかなく、YAMAHAのお高いルーターを買うべきか悩んでいたところだったのだが、SoftEtherというアプリケーションだと無料で構築できるらしい。

インストール

以下からダウンロード

http://www.softether-download.com/ja.aspx?product=softether

するとリンク一覧が出るので、一番最新のやつのリンクをコピってwget&make

# tar xzvf softether-vpnserver-v4.22-9634-beta-2016.11.27-linux-arm_eabi-32bit.tar.gz
# cd vpnserver
# make

途中ライセンス確認されるので1を入力してエンター

The preparation of SoftEther VPN Server is completed ! と表示されればビルド成功

/usr/localへ移動

cd ../
mv vpnserver /usr/local

実行権限付与

cd /usr/loca/vpnserver
chmod +x vpncmd
chmod +x vpnserver

起動

サービスと登録周りは自分でやらなくてはいけない

今回はsystemdを使ってみる。/etc/systemd/system/vpnserver.serviceに以下

[Unit]
Description=SoftEther VPN Server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/vpnserver/vpnserver start
ExecStop=/usr/local/vpnserver/vpnserver stop
RestartSec=3s

[Install]
WantedBy=multi-user.target

でリロード

systemctl daemon-reload

起動

systemctl start vpnserver

うまく起動していればsystemctl status vpnserver のときにstatusが activeになってるはず

マシン起動時に、自動起動させたい場合は

systemctl enable vpnserver

しておく

せっかくなので旧式のinit.dバージョンも載せておく 動作確認済み

/etc/init.d/vpnserverに以下

DAEMON=/usr/local/vpnserver/vpnserver
LOCK=/var/lock/vpnserver
test -x $DAEMON || exit 0
case "$1" in
start)
$DAEMON start
touch $LOCK
;;
stop)
$DAEMON stop
rm $LOCK
;;
restart)
$DAEMON stop
sleep 3
$DAEMON start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0

で実行権限付与

# chmod +x /etc/init.d/vpnserver

自動起動するようにする

# update-rc.d vpnserver defaults

起動

# /etc/init.d/vpnserver start

初期設定

以下で起動する

# /usr/local/vpnserver/vpncmd
# /usr/local/vpnserver/vpncmd                                                    
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.27 Build 9667   (English)
Compiled 2018/05/26 09:09:16 by yagi at pc33
Copyright (c) SoftEther VPN Project. All Rights Reserved.

By using vpncmd program, the following can be achieved. 

1. Management of VPN Server or VPN Bridge 
2. Management of VPN Client
3. Use of VPN Tools (certificate creation and Network Traffic Speed Test Tool)
# 設定をしたいので1を選択
Select 1, 2 or 3: 1

Specify the host name or IP address of the computer that the destination VPN Server or VPN Bridge is operating on. 
By specifying according to the format 'host name:port number', you can also specify the port number. 
(When the port number is unspecified, 443 is used.)
If nothing is input and the Enter key is pressed, the connection will be made to the port number 8888 of localhost (this computer).
# localhostと入力
Hostname of IP Address of Destination: localhost

If connecting to the server by Virtual Hub Admin Mode, please input the Virtual Hub name. 
If connecting by server admin mode, please press Enter without inputting anything.
# 空白のままエンター
Specify Virtual Hub Name: 
Connection has been established with VPN Server "localhost" (port 443).

You have administrator privileges for the entire VPN Server.

VPN Server>

ユーザーの作成

まずはユーザーの作成から、とその前に設定対象のハブを選択。すでにDEFAULTというハブが作られているのでそれを使う

VPN Server>Hub DEFAULT 
Hub command - Select Virtual Hub to Manage
The Virtual Hub "DEFAULT" has been selected.
The command completed successfully.

VPN Server/DEFAULT>
VPN Server/DEFAULT>UserCreate
UserCreate command - Create User 
User Name: ユーザ名

# 空欄のままエンター
Assigned Group Name:
# 空欄のままエンター
User Full Name:
# 空欄のままエンター
User Description:

The command completed successfully.

パスワードを設定

VPN Server/DEFAULT>UserPasswordSet
UserPasswordSet command - Set Password Authentication for User Auth Type and Set Password
User Name: さっきのユーザ名

Please enter the password. To cancel press the Ctrl+D key.

Password: *******************
Confirm input: *******************


The command completed successfully.

IPsec / L2TP での通信を有効化

IPsecEnableで設定に入れる。

VPN Server/DEFAULT>IPsecEnable
IPsecEnable command - Enable or Disable IPsec VPN Server Function
Enable L2TP over IPsec Server Function (yes / no): yes

Enable Raw L2TP Server Function (yes / no): no

Enable EtherIP / L2TPv3 over IPsec Server Function (yes / no): no

Pre Shared Key for IPsec (Recommended: 9 letters at maximum): ********

Default Virtual HUB in a case of omitting the HUB on the Username: DEFAULT

The command completed successfully.

VPNで接続するにあたってSecureNatEnable、NatEnable、DHCPEnableも有効しておく。

VPN Server/DEFAULT>SecureNatEnable
SecureNatEnable command - Enable the Virtual NAT and DHCP Server Function (SecureNat Function)
The command completed successfully.
VPN Server/DEFAULT>NatEnable      
NatEnable command - Enable Virtual NAT Function of SecureNAT Function
The command completed successfully.

VPN Server/DEFAULT>DHCPEnable
DhcpEnable command - Enable Virtual DHCP Server Function of SecureNAT Function
The command completed successfully.

いざ接続

MacからVPN接続したいときは以下の公式サイトが役立つ

Mac OS X からの接続方法 - SoftEther VPN プロジェクト

やったね、これでブラクラ貼っても逮捕されないね!(?

ポート開放

ルーターでポートの設定を行ってる場合は以下の2つを開放する必要がある。 UDPなので注意