Api 数字海洋-当水滴在其创建后处于活动状态时,如何获得通知

Api 数字海洋-当水滴在其创建后处于活动状态时,如何获得通知,api,digital-ocean,Api,Digital Ocean,我正在使用DoAPI和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 在最好的情况下,在创建水滴之后,我会

我正在使用DoAPI和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
在最好的情况下,在创建水滴之后,我会收到通知(就像水滴创建完成时执行的webhoock)。可能吗

非常感谢查看API文档

您应该能够看到液滴的状态(请参见“液滴”部分)

使用您的逻辑,您可以:

  • 创建液滴并将id存储在变量中
  • 睡1分钟
  • 调用id为/v2/droplets/$droplet\u id的液滴
  • 测试响应状态(指示液滴实例状态的状态字符串。可能是“新建”、“活动”、“关闭”或“存档”)
  • 如果status==new,则执行某些操作
  • 更新

    另一种方法是在创建液滴时对其进行修改。使用Digital ocean,您可以传递
    用户数据
    ,之前我使用它自动配置服务器,下面是一个示例

    $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)),
        ));
    
    $user_data=“512mb”,
    “image”=>“ubuntu-14-04-x64”,
    “ssh_密钥”=>$sshKey,
    “备份”=>错误,
    “ipv6”=>错误,
    “用户数据”=>$user\u数据,
    “专用_网络”=>空,
    );
    $chDroplet=curl\u 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,数组(
    “授权:持有人”。$apiKey,
    '内容类型:application/json',
    “内容长度:”.strlen(json_encode($data)),
    ));
    

    基本上,一旦液滴处于活动状态,它将运行这些命令,然后从我的服务器下载tar.gz文件并执行它,您可以创建update.php来调用您的服务器,从而更新它,确保液滴处于在线状态。

    对于第一种方法,DigitalOcean的API也会返回。这些可用于检查您采取的不同操作的状态。返回的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...')
    

    谢谢你的回复。您描述了第一种方法,但我正在寻找“基于事件”的解决方案,如“webhoock”。@Anthony我现在更了解您需要什么。我已经更新了我的答案,太完美了!非常感谢这个解决方案。有了这个参数,我可以运行简单的wget来调用我的“webhook”。我不知道用户数据可以用来插入code@Anthony很高兴我能帮忙。用户数据是惊人的,它真的打开了数字海洋,正如我所说的,为了我的目的,我可以部署50台服务器,所有服务器都是自动配置的,只需点击一个按钮,感觉距离我在页面上的第一次回显还有很长的路要走。很高兴认识我的朋友。