動かざることバグの如し

近づきたいよ 君の理想に

シェルスクリプトからrbenv上のrubyを実行したい

環境

  • rbenv

通常rbenvは/etc/profile.dか~/.bashrcなどの読み込み時にロードされる。 が、シェルスクリプトからだとロードされないのでrbenvが無い環境扱いになってしまう。

ので、以下のように明示的にrbenvを読み込ませる必要がある。

#!/bin/bash

export PATH="/usr/local/rbenv/bin:$PATH"
eval "$(rbenv init -)"
rbenv shell 2.4.0
echo ruby -v

これで

ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]

とか表示されればおk

Linuxでパーミッション情報やシンボリックリンクを維持したままコピーしたい

特定のディレクトリをバックアップしたいとき、cp -r target destすればもちろんコピーできるが、とっても大切なデータの場合慎重になる。

というのもcpコマンドではパーミッションまで保持してくれないからである。

-aオプションをつけると属性を引き継いだままコピーできるが、うまくいかないケースも稀にある。

そんなときはcpではなくtarを使うと吉

tar cfp - target_dir | tar xfp - -C /tmp/

オプションの意味だが、

  • c createの意味 作成モード
  • f fileの意味 デフォルトだとテープとかいう謎なのでファイルモードに -p permissionの意味 今回のキモオプション パーミッションを保持する

  • x 解凍モード

  • f 同じくファイルモード
  • p 同じくパーミッションを保持
  • C 指定したディレクトリへ解凍

例えば以下のようなworkディレクトリを/root/workへまるごとバックアップしたい時がある

# ll work/
total 4
-rw-rw-r-- 1 nginx   nginx      0 Aug 30 14:36 aaa
drwxrwxr-x 2 vagrant vagrant 4096 Aug 30 14:36 bbb

# ll work/bbb/
total 0
-rw-rw-r-- 1 root root 0 Aug 30 14:36 cccc

tarで実行

tar cfp - work | tar xfp - -C /root

ちゃんとパーミッションが引き継がれている。

# ll /root/work/
total 4
-rw-rw-r-- 1 nginx   nginx      0 Aug 30 14:36 aaa
drwxrwxr-x 2 vagrant vagrant 4096 Aug 30 14:36 bbb

# ll /root/work/bbb/
total 0
-rw-rw-r-- 1 root root 0 Aug 30 14:36 cccc

参考リンク

Capistrano3のデフォルトのデプロイタスクをoverrideする方法

環境

やりたいこと

Capistranoにはデフォルトでdeploy: やgit:でたくさんのタスクが用意されていて、cap production deployでいろんなタスクが実行されるが、一部のタスクをオレオレにカスタマイズしたかった

方法

公式サイトにトピックがあった

deploy.rbの任意の場所で以下

Rake::Task["deploy:log_revision"].clear_actions

これでタスクの中が空になる

で、以下のように再定義してあげると定義したタスクが同じタイミングで実行される やったね

namespace :deploy do
  
  task :log_revision do |t|
    on roles(:all) do |s|
      info "yeahhhhhhhhhhhhhhhh"
    end
  end
end

SSL証明書エラーのテストに使える badssl.com が便利

SSL証明書絡みのエラーは厄介である。が、その状況を再現するのって結構難しかったりする。

そんなときはbadssl.comが便利

期限切れの証明書

https://expired.badssl.com/

このサーバーが expired.badssl.com であることを確認できませんでした。セキュリティ証明書の有効期限が 1,254 日前に切れています。原因として、設定が不適切であるか、悪意のあるユーザーが接続を妨害していることが考えられます。パソコンの時計は現在 2018年9月17日月曜日に設定されています。この時刻が正しくない場合は、システムの時計を修正した後このページを更新してください。

ホスト名が不一致

https://wrong.host.badssl.com/

このサーバーが wrong.host.badssl.com であることを確認できませんでした。このサーバーのセキュリティ証明書は *.badssl.com から発行されています。原因としては、不適切な設定や、悪意のあるユーザーによる接続妨害が考えられます。

オレオレ証明書

https://self-signed.badssl.com/

このサーバーが self-signed.badssl.com であることを確認できませんでした。このサーバーのセキュリティ証明書は、ご使用のパソコンのオペレーティング システムによって信頼されているものではありません。原因としては、不適切な設定や、悪意のあるユーザーによる接続妨害が考えられます。

他にも見慣れないエラーが沢山あって驚き

Let's EncryptのワイルドカードSSL証明書の更新に苦労した話

経緯

一応ドメインを持っていて、turai.work ドメインを持っているのだが、Let's EncryptワイルドカードSSL証明書で運用している。

期限も近づいてきたし、certbot-autoコマンドで更新するか〜と思って更新を実行したのだが

/root/certbot/certbot-auto renew --force-renewal

以下のようなエラーが

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/turai.work.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Could not choose appropriate plugin: The manual plugin is not working; there may be problems with your existing configuration.
The error was: PluginError('An authentication script must be provided with --manual-auth-hook when using the manual plugin non-interactively.',)
Attempting to renew cert (turai.work) from /etc/letsencrypt/renewal/turai.work.conf produced an unexpected error: The manual plugin is not working; there may be problems with your existing configuration.
The error was: PluginError('An authentication script must be provided with --manual-auth-hook when using the manual plugin non-interactively.',). Skipping.
All renewal attempts failed. The following certs could not be renewed:
  /etc/letsencrypt/live/turai.work/fullchain.pem (failure)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

All renewal attempts failed. The following certs could not be renewed:
  /etc/letsencrypt/live/turai.work/fullchain.pem (failure)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1 renew failure(s), 0 parse failure(s)

なんで?????ちなみに他にワイルドカードではないLet's EncryptのSSL証明書もあるのだが、そっちは上記のコマンドで正常に更新できた。

エラー原因

どうやらワイルドカードSSL証明書の場合はイレギュラーで、更新のたびにDNSの認証が必要らしい。メンドクセーーーー

が、更新しないとエラーになって悲しいことになるのですることに。てかDNS更新必要ならcronで定期実行できないじゃんと嘆いてたらcertbot-dns-route53なるプラグインを発見

certbot用のプラグインで、AWSのキーを予めセットしておけば更新時に自動で処理してくれる。素晴らしい

ってことで導入

certbot-dns-route53のインストール

通常 /opt/eff.org/certbot/venv/bin/pip にcertbotが使っているPythonの環境が入っているのでその中のpipを使ってインストール

/opt/eff.org/certbot/venv/bin/pip install certbot-dns-route53

インストールが完了したらプラグイン一覧で確認

./certbot/certbot-auto plugins

以下のような項目があればインストールに成功している

* dns-route53
Description: Obtain certificates using a DNS TXT record (if you are using AWS
Route53 for DNS).
Interfaces: IAuthenticator, IPlugin
Entry point: dns-route53 = certbot_dns_route53.dns_route53:Authenticator

AWSのキー取得

AWSのIAM管理画面へログインして新しいIAMを作成、Route53の権限を設定してアクセスキーとシークレットキーを取得

rootコマンドで実行するので/root/.aws/credentialsに以下を作成

[default]
aws_secret_access_key = *************
aws_access_key_id = ******

あとはdns-route53のプラグインを使用することを明示的に書いて更新実行

/root/certbot/certbot-auto renew --force-renewal --no-self-upgrade --server https://acme-v02.api.letsencrypt.org/directory --agree-tos --dns-route53
Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/turai.work/fullchain.pem (success)

って言われたら成功 やったね

あとの詳しいことは公式ドキュメント参照

参考リンク