液滴创建后活跃时要通知如何通知 我正在使用DO API和Ansible来处理自动化脚本。我可以创建很多液滴,但是如何知道创建的液滴是否活跃? 第一种(幼稚的)方法使用以下

问题描述 投票:0回答:2
在最好的世界中,在液滴创建之后,我将收到通知(就像液滴创建完成后执行的Webhook一样)。有可能吗?

看API文档

https://developers.digitalocean.com/documentation/v2/


您应该能够看到液滴的状态(请参阅液滴部分)。

使用您的逻辑:

digital-ocean
2个回答
4
投票
创建液滴并将ID存储在变量

中 sleep 1分钟 用ID/v2/droplets/quloplet_id.cl.call。 测试响应状态(指示液滴实例状态的状态字符串。这可能是“新”,“活动”,“ off”或“存档”。)。

如果状态==新做某事

update
  1. 另一个方法是在创建的液滴时修改液滴。使用Digital Ocean,您可以通过
  2. User Data
  3. $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)), ));
  4. 滴一旦液滴处于活动状态,它将运行这些命令,然后从我的服务器下载tar.gz文件并执行它,您可能会创建update.php来调用服务器,因此将液滴在线更新。
  5. 对于第一种方法,Digitalocean的API也返回
  6. 动作项目
。这些可用于检查您采取的不同动作的状态。返回的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...')

# after you define the droplet you want to create,
# this will let you know exactly when the droplet is ready.
# and return its IP address and Active status

...
droplet.create()
    i = 0
    while True:
        actions = droplet.get_actions()
        for action in actions:
            action.load()
            status = action.status
            print(status)        
            if status == "completed":
                print("Droplet created")
                droplets = manager.get_all_droplets(tag_name=[droplet_name"])
                for droplet in droplets:
                    if droplet.name == droplet_name:   
                        while True:
                            if droplet.status == "active":
                                print("Droplet is active")
                                return f"droplet created {droplet_name=} {droplet.ip_address=} {droplet.status=}"
                            else:
                                time.sleep(5)
                                print("Droplet is under creation, waiting for it to be active...")                 
        time.sleep(5)


1
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.