

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 自訂引導操作
<a name="pre_post_install"></a>

AWS ParallelCluster 建立叢集時， 可以在主要引導操作之前 （安裝前） 或之後 （安裝後） 執行任意程式碼。在大多數情況下，此程式碼會儲存在 Amazon Simple Storage Service (Amazon S3) 中，並透過 HTTPS 連線存取。程式碼會以根目錄執行，並且可以使用叢集作業系統支援的任何指令碼語言。程式碼通常使用 *Bash* 或 *Python*。

在啟動任何叢集部署引導操作之前，會呼叫預先安裝動作，例如設定 NAT、Amazon Elastic Block Store (Amazon EBS) 或排程器。有些預先安裝動作包括修改儲存體、新增額外的使用者，以及新增套件。

安裝後動作會在叢集引導程序完成後呼叫。安裝後動作是執行個體視為完全設定並完成之前要執行的最後一個動作。部分安裝後動作包括變更排程器設定、修改儲存體和修改套件。

您可以在組態期間指定指令碼，藉此將引數傳遞給指令碼。為此，您可以將它們傳遞為預先安裝或安裝後動作的雙引號。

如果預先安裝或安裝後動作失敗，執行個體引導也會失敗。成功會以零 (0) 的結束代碼發出訊號。任何其他結束碼表示執行個體引導失敗。

您可以區分執行頭和運算節點。取得 `/etc/parallelcluster/cfnconfig` 檔案，並分別評估頭部和運算節點的值為 "`MasterServer`" 和 "`ComputeFleet`" `cfn_node_type`的環境變數。

```
#!/bin/bash

. "/etc/parallelcluster/cfnconfig"

case "${cfn_node_type}" in
    MasterServer)
        echo "I am the head node" >> /tmp/head.txt
    ;;
    ComputeFleet)
        echo "I am a compute node" >> /tmp/compute.txt
    ;;
    *)
    ;;
esac
```

## Configuration
<a name="pre_post_install-configuration"></a>

以下組態設定用來定義安裝前和安裝後動作和引數。

```
# URL to a preinstall script. This is run before any of the boot_as_* scripts are run
# (no default)
pre_install = https://<bucket-name>.s3.amazonaws.com/my-pre-install-script.sh
# Arguments to be passed to preinstall script
# (no default)
pre_install_args = argument-1 argument-2
# URL to a postinstall script. This is run after any of the boot_as_* scripts are run
# (no default)
post_install = https://<bucket-name>.s3.amazonaws.com/my-post-install-script.sh
# Arguments to be passed to postinstall script
# (no default)
post_install_args = argument-3 argument-4
```

## 引數
<a name="arguments"></a>

前兩個引數 (`$0` 和 `$1`) 保留給指令碼名稱和 url。

```
$0 => the script name
$1 => s3 url
$n => args set by pre/post_install_args
```

## 範例
<a name="example"></a>

以下步驟建立簡單的安裝後指令碼，在叢集安裝 R 套件。

1. 建立指令碼。

   ```
   #!/bin/bash
   
   echo "post-install script has $# arguments"
   for arg in "$@"
   do
       echo "arg: ${arg}"
   done
   
   yum -y install "${@:2}"
   ```

1. 將具有正確許可的指令碼上傳至 Amazon S3。如果公有讀取許可不適合您，請使用 [`s3_read_resource`](cluster-definition.md#s3-read-resource)或 [`s3_read_write_resource`](cluster-definition.md#s3-read-write-resource) 參數來授予存取權。如需詳細資訊，請參閱[使用 Amazon S3](s3_resources.md)。

   ```
   $ aws s3 cp --acl public-read /path/to/myscript.sh s3://bucket-name/myscript.sh
   ```
**重要**  
如果在 Windows 上編輯指令碼，則必須在指令碼上傳到 Amazon S3 之前，將行尾從 CRLF 變更為 LF。

1. 更新 AWS ParallelCluster 組態以包含新的安裝後動作。

   ```
   [cluster default]
   ...
   post_install = https://bucket-name.s3.amazonaws.com/myscript.sh
   post_install_args = 'R curl wget'
   ```

   如果儲存貯體沒有公有讀取許可，請使用 `s3`做為 URL 通訊協定。

   ```
   [cluster default]
   ...
   post_install = s3://bucket-name/myscript.sh
   post_install_args = 'R curl wget'
   ```

1. 啟動叢集。

   ```
   $ pcluster create mycluster
   ```

1. 驗證輸出。

   ```
   $ less /var/log/cfn-init.log
   2019-04-11 10:43:54,588 [DEBUG] Command runpostinstall output: post-install script has 4 arguments
   arg: s3://bucket-name/test.sh
   arg: R
   arg: curl
   arg: wget
   Loaded plugins: dkms-build-requires, priorities, update-motd, upgrade-helper
   Package R-3.4.1-1.52.amzn1.x86_64 already installed and latest version
   Package curl-7.61.1-7.91.amzn1.x86_64 already installed and latest version
   Package wget-1.18-4.29.amzn1.x86_64 already installed and latest version
   Nothing to do
   ```