2015年1月31日土曜日

groovyとshipyardでengineを作成する

groovyとshipyardでengineを作成する groovyとshipyardでengineを作成するには、以下のサンプルコードのようにX-Service-Keyにサービスキーを設定して/api/enginesにPOSTします。

サンプルコード
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.3.6')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.auth.*
import org.apache.http.entity.*
import groovy.json.*

def host = "192.168.1.212" // replace this
def port = 8080
def serviceKey = "your-service-key"
def id = "test_engine"

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {

  def method = new HttpPost(
    "http://${host}:${port}/api/engines"
  )
  method.setHeader("X-Service-Key", serviceKey)
  def json = new JsonBuilder()
  json (
    "ssl_cert": "",
    "ssl_key": "",
    "ca_cert": "",
    engine:[
      "id":id,
      "cpus": 1.0,
      "memory": 1024.0,
      "addr":"http://docker-host:4243",
      "labels":["default"]
    ]
  )
  method.setHeader("Content-Type", "application/json; charset=utf-8")
  method.setEntity(new StringEntity(json.toString(), "UTF-8"))
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()
  println response.getEntity().getContent().text
}

関連情報

0 件のコメント:

コメントを投稿