0

我正在尝试使用 Google Cloud 计算 VM 执行关闭脚本。

我在运行时看到此输出gcloud compute connect-to-serial-port startup-test-v

Apr  8 22:01:25 startup-test-v shutdown-script: INFO Starting shutdown scripts.
Apr  8 22:01:25 startup-test-v shutdown-script: INFO Found shutdown-script in metadata.
Apr  8 22:01:26 startup-test-v shutdown-script: INFO shutdown-script: No change requested; skipping update for [startup-test-v].
Apr  8 22:01:27 startup-test-v shutdown-script: INFO shutdown-script: Return code 0.
Apr  8 22:01:27 startup-test-v shutdown-script: INFO Finished running shutdown scripts.

我从命令行创建抢占式实例并在 GUI 中将其关闭。

gcloud compute instances create $INSTANCE_NAME \
    --zone=$ZONE \
    --image-family=$IMAGE_FAMILY \
    --image-project=deeplearning-platform-release \
    --maintenance-policy=TERMINATE \
    --machine-type=$INSTANCE_TYPE \
    --boot-disk-size=50GB \
    --metadata="install-nvidia-driver=True" \
    --preemptible \
    --scopes="storage-rw,cloud-platform" \
    --metadata-from-file="shutdown-script=gce/shutdown_test.sh"

shutdown_test.sh很简单:

#!/bin/bash
echo "+++ Shutdown test +++"
exit 0

启动脚本按预期工作。我也尝试将--metadata-from-file标志交换--metadata-from-file shutdown-script=gce/shutdown_test.sh为,没有任何变化。

想法?似乎 GCE 正在寻找关闭脚本,但没有执行它。

4

2 回答 2

1

事实证明,图像可以覆盖 CLI 定义的关闭脚本元数据

在我的例子中,pytorch-latest-gpuimage 将shutdown-script元数据更改为指向它自己的关闭脚本。它在第一次启动时执行此操作。

如果您编辑该脚本 - 定义于/opt/deeplearning/bin/shutdown_script.sh- 您可以获得您喜欢的任何关闭行为。否则,您可以编辑元数据以指向您的脚本。您的关闭脚本将出现在串行输出日志中。

Apr  9 23:26:46 new-test-d shutdown-script: INFO Starting shutdown scripts.
Apr  9 23:26:47 new-test-d shutdown-script: INFO Found shutdown-script in metadata.
Apr  9 23:26:47 new-test-d shutdown-script: INFO shutdown-script: ++++++++++++++ Shutdown test +++++++++++++++++

shutdown-script您可以通过省略cloud-platformin不授予实例添加元数据的权限来防止图像脚本发生更改--scope。或者您可以shutdown-script在启动后在 GUI 中进行编辑。您也可以通过启动脚本重新编辑它。

于 2020-04-09T23:53:52.923 回答
1

关闭脚本既不向串行控制台也不向当前的 SSH 终端控制台提供任何输出,因为它以 root 用户身份运行,并且stdout既不路由到/dev/tty*/dev/ttyS*.

为确保关闭脚本确实有效,您可以在 VM 元数据键中输入shutdown-script如下内容:

#!/bin/bash 
ofile=/var/tmp/shutdown.txt 
echo "+++ Running shutdown script +++"
echo "id = $(id)" > $ofile 
echo "script_file path = $(realpath $0)" >> $ofile 
echo "script_file rights, user, group = $(stat -c "%A %U %G" $0)" >> $ofile

接下来连接到串行控制台和 SSH 以准备观察关闭脚本提供的任何输出的缺失。

然后停止并启动 VM。

最后,您将看到关闭脚本实际上已以 root 用户身份运行并留下了一个日志文件:

$ ls -l /var/tmp/shutdown.txt 
 rw-r--r-- 1    root    root    165     Apr  9 18:40    shutdown.txt 
$ cat /var/tmp/shutdown.txt  
 id = uid=0(root) gid=0(root) groups=0(root) 
 script_file path = /tmp/metadata-scripts196132089/shutdown-script 
 script_file rights, user, group = -rwxr-xr-x root root 

关闭脚本适用于抢占式实例与普通实例相同。不同之处在于前者的关机时间较短(30秒)。

于 2020-04-09T19:28:37.453 回答