如何在 Droplet 创建后处于活动状态时收到通知
How to be notified when a droplet is active after its creation
我正在使用 DO API 和 Ansible 编写自动化脚本。我可以创建很多液滴,但如何知道创建的液滴是否已激活?
第一种(天真的)方法使用以下过程:
A. Create droplet with the Digital Ocean API
B. Call the API to get the created droplet informations
1. is active ?
yes :
no : go to B
在最好的世界里,创建 droplet 后,我会收到通知(就像 droplet 创建完成时执行的 webhook)。可能吗?
查看 API 文档 https://developers.digitalocean.com/documentation/v2/
您应该能够看到 Droplet 的状态(参见 Droplet 部分)。
使用你的逻辑你可以:
- 创建 Droplet 并将 ID 存储在变量中
- 睡 1 分钟
- 调用 ID 为 /v2/droplets/$DROPLET_ID 的 Droplet。
- 测试响应状态(指示 Droplet 实例状态的状态字符串。这可能是 "new"、"active"、"off" 或 "archive"。) .
- 如果 status == new 做点什么
更新
另一种方法是在创建液滴时对其进行修改。有了Digital ocean就可以通过User Data
,之前我用这个来自动配置服务器,这里是一个例子。
$user_data = <<<EOD
#!/bin/bash
apt-get update
apt-get -y install apache2
apt-get -y install php5
apt-get -y install php5-mysql
apt-get -y install unzip
service apache2 restart
cd /var/www/html
mkdir pack
cd pack
wget --user {$wgetUser} --password {$wgetPass} http://x.x.x.x/pack.tar.gz
tar -xvf pack.tar.gz
php update.php
EOD;
//Start of the droplet creation
$data = array(
"name"=>"AutoRes".$humanProv.strtoupper($lang),
"region"=>randomRegion(),
"size"=>"512mb",
"image"=>"ubuntu-14-04-x64",
"ssh_keys"=>$sshKey,
"backups"=>false,
"ipv6"=>false,
"user_data"=>$user_data,
"private_networking"=>null,
);
$chDroplet = curl_init('https://api.digitalocean.com/v2/droplets');
curl_setopt($chDroplet, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($chDroplet, CURLOPT_POSTFIELDS, json_encode($data) );
curl_setopt($chDroplet, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$apiKey,
'Content-Type: application/json',
'Content-Length: ' . strlen(json_encode($data)),
));
基本上,一旦 Droplet 处于活动状态,它将 运行 这些命令,然后从我的服务器下载 tar.gz 文件并执行它,您可能会创建 update.php 来调用您的服务器并因此更新 Droplet 在线。
对于第一种方法,DigitalOcean 的 API 也 returns Action items。这些可用于检查您执行的不同操作的状态。返回的 json 看起来像:
{
"action": {
"id": 36804636,
"status": "completed",
"type": "create",
"started_at": "2014-11-14T16:29:21Z",
"completed_at": "2014-11-14T16:30:06Z",
"resource_id": 3164444,
"resource_type": "droplet",
"region": "nyc3",
"region_slug": "nyc3"
}
}
以下是如何使用它们的简单示例:
import os, time
import requests
token = os.getenv('DO_TOKEN')
url = "https://api.digitalocean.com/v2/droplets"
payload = {'name': 'example.com', 'region': 'nyc3', 'size': '512mb', "image": "ubuntu-14-04-x64"}
headers = {'Authorization': 'Bearer {}'.format(token)}
r = requests.post(url, headers=headers, json=payload)
action_url = r.json()['links']['actions'][0]['href']
r = requests.get(action_url, headers=headers)
status = r.json()['action']['status']
while status != u'completed':
print('Waiting for Droplet...')
time.sleep(2)
r = requests.get(action_url, headers=headers)
status = r.json()['action']['status']
print('Droplet ready...')
我正在使用 DO API 和 Ansible 编写自动化脚本。我可以创建很多液滴,但如何知道创建的液滴是否已激活?
第一种(天真的)方法使用以下过程:
A. Create droplet with the Digital Ocean API
B. Call the API to get the created droplet informations
1. is active ?
yes :
no : go to B
在最好的世界里,创建 droplet 后,我会收到通知(就像 droplet 创建完成时执行的 webhook)。可能吗?
查看 API 文档 https://developers.digitalocean.com/documentation/v2/
您应该能够看到 Droplet 的状态(参见 Droplet 部分)。
使用你的逻辑你可以:
- 创建 Droplet 并将 ID 存储在变量中
- 睡 1 分钟
- 调用 ID 为 /v2/droplets/$DROPLET_ID 的 Droplet。
- 测试响应状态(指示 Droplet 实例状态的状态字符串。这可能是 "new"、"active"、"off" 或 "archive"。) .
- 如果 status == new 做点什么
更新
另一种方法是在创建液滴时对其进行修改。有了Digital ocean就可以通过User Data
,之前我用这个来自动配置服务器,这里是一个例子。
$user_data = <<<EOD
#!/bin/bash
apt-get update
apt-get -y install apache2
apt-get -y install php5
apt-get -y install php5-mysql
apt-get -y install unzip
service apache2 restart
cd /var/www/html
mkdir pack
cd pack
wget --user {$wgetUser} --password {$wgetPass} http://x.x.x.x/pack.tar.gz
tar -xvf pack.tar.gz
php update.php
EOD;
//Start of the droplet creation
$data = array(
"name"=>"AutoRes".$humanProv.strtoupper($lang),
"region"=>randomRegion(),
"size"=>"512mb",
"image"=>"ubuntu-14-04-x64",
"ssh_keys"=>$sshKey,
"backups"=>false,
"ipv6"=>false,
"user_data"=>$user_data,
"private_networking"=>null,
);
$chDroplet = curl_init('https://api.digitalocean.com/v2/droplets');
curl_setopt($chDroplet, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($chDroplet, CURLOPT_POSTFIELDS, json_encode($data) );
curl_setopt($chDroplet, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$apiKey,
'Content-Type: application/json',
'Content-Length: ' . strlen(json_encode($data)),
));
基本上,一旦 Droplet 处于活动状态,它将 运行 这些命令,然后从我的服务器下载 tar.gz 文件并执行它,您可能会创建 update.php 来调用您的服务器并因此更新 Droplet 在线。
对于第一种方法,DigitalOcean 的 API 也 returns Action items。这些可用于检查您执行的不同操作的状态。返回的 json 看起来像:
{
"action": {
"id": 36804636,
"status": "completed",
"type": "create",
"started_at": "2014-11-14T16:29:21Z",
"completed_at": "2014-11-14T16:30:06Z",
"resource_id": 3164444,
"resource_type": "droplet",
"region": "nyc3",
"region_slug": "nyc3"
}
}
以下是如何使用它们的简单示例:
import os, time
import requests
token = os.getenv('DO_TOKEN')
url = "https://api.digitalocean.com/v2/droplets"
payload = {'name': 'example.com', 'region': 'nyc3', 'size': '512mb', "image": "ubuntu-14-04-x64"}
headers = {'Authorization': 'Bearer {}'.format(token)}
r = requests.post(url, headers=headers, json=payload)
action_url = r.json()['links']['actions'][0]['href']
r = requests.get(action_url, headers=headers)
status = r.json()['action']['status']
while status != u'completed':
print('Waiting for Droplet...')
time.sleep(2)
r = requests.get(action_url, headers=headers)
status = r.json()['action']['status']
print('Droplet ready...')