LoginSignup
1
0

More than 1 year has passed since last update.

Linode の StackScripts のデバッグ方法

Posted at

はじめに

StackScripts は Linode で初回構成(プロビジョニング)時に実行できるスクリプトです。
とても便利な機能ですが、標準では StackScripts の記述を間違えたときの原因が追えません。
今回は StackScripts の実行ログを取得し、原因解析の時間を短縮する方法をご紹介します。

StackScripts の解説はこちら。

StackScripts のデバッグ方法

ベーシックな方法ですが、StackScripts で実行されたログをそのままファイルに書き出します。

こちらのように StackScripts に一文加えるだけです。

exec 1> >(tee -a "/var/log/linode_stackscripts.log") 2>&1

以下はサンプルの StackScripts です。
仮想マシン作成時にユーザを追加し、SSH の設定を変更する処理などを記載しています。

#!/bin/bash
#<UDF name="username" label="SSH Username" default="">
#<UDF name="password" label="SSH Password" default="">
#<UDF name="publickey" label="SSH Public Key" default="">
#<UDF name="disable_root" label="Disable root login for SSH" oneOf="Yes,No" default="Yes">
source <ssinclude StackScriptID=1>

# enable logging
exec 1> >(tee -a "/var/log/linode_stackscripts.log") 2>&1

if [ "$USERNAME" ] && [ "PASSWORD" ]; then
    user_add_sudo "$USERNAME" "$PASSWORD"
    
    if use_add_pubkey "$USERNAME" "$PUBLICKEY" && [ "$DISABLE_ROOT" == "Yes" ]; then
        ssh_disable_root
    fi
fi

こちらの StackScripts を実行すると、/var/log/linode_stackscripts.log にログが書き出されます。

$ cat /var/log/linode_stackscripts.log
Adding user `user1' ...
Adding new group `user1' (1000) ...
Adding new user `user1' (1000) with group `user1' ...
Creating home directory `/home/user1' ...
Copying files from `/etc/skel' ...
/root/StackScript: line 14: use_add_pubkey: command not found

こちらを見てみると、/root/StackScript: line 14: use_add_pubkey: command not found との記載があります。
StackScripts の14行目において、本来 user_add_pubkey という関数名であるところが use_add_pubkey となっていたためエラーが発生したとわかります。
他のエラーもログに出力されるので、解析に便利ですね。

まとめ

今回は StackScripts のデバッグ方法についてご紹介しました。
StackScripts に一文追加するだけで利用できるので、StackScripts を作られる際はぜひ追加することをおすすめします。

関連記事

アカマイ・テクノロジーズ合同会社の Qiita では Linode 関連など開発者向けの記事を記載しています。

1
0
0

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
1
0