示例#1
0
def main():
    args = docopt.docopt(__doc__)
    device_id = args["--device-id"]
    resource_path = args["--resource-path"]
    if (args["observe"]):
        observe_async(device_id, resource_path)
    if (args["execute"]):
        execute(device_id, resource_path)
    if (args["read"]):
        read(device_id, resource_path)
    if (args["write"]):
        value = args["--new-resource-value"]
        write(device_id, resource_path, value)
    if (args["filter"]):
        if args["--edge-devices"]:
            filter_edge_devices(args["--connected"])
        elif args["--host-edge"]:
            filter_edge_hosted_devices(args["--host-edge"],
                                       args["--connected"])
    if (args["convert-dev-cert"]):
        converter = CBORConverter(args["--development-certificate"],
                                  args["--update-resource"], args["--cbor"])
        converter.convert_to_cbor()
    if (args["add-custom-cert"]):
        CBORUtils.add_custom_certificate(args["--cbor"], args["--custom-cert"])
    if (args["print-cbor"]):
        CBORUtils.print_cbor(args["--cbor"])
示例#2
0
def describe(cloud_name, machine_id, print_list=True):
    provider, xxx, region, default_ssh_key, cloud_keys = cloud.read(
        cloud_name, True)

    svr, name, size, state, location, private_ip, \
    public_ip, key_name, vcpus, volumes \
      = get_describe_data(provider, machine_id, region, cloud_keys)

    if state == '' or state == None:
        return (None)

    headers = ['Name', 'Size', 'State', 'Location', 'PublicIp', 'Id']
    keys = ['name', 'size', 'state', 'location', 'public_ip', 'id']

    jsonList = []
    dict = {}
    dict["name"] = name
    dict["id"] = machine_id
    dict["size"] = size
    dict["state"] = state
    dict["location"] = location
    dict["private_ip"] = private_ip
    dict["public_ip"] = public_ip
    dict["key_name"] = key_name
    dict["vcpus"] = str(vcpus)
    dict["volumes"] = volumes
    jsonList.append(dict)

    if print_list:
        util.print_list(headers, keys, jsonList)
        return

    return (dict)
示例#3
0
def waitfor(cloud_name, machine_id, new_state, interval=5, max_tries=12):
    util.message(
        "waitfor '" + str(new_state) + "' up to " + str(interval * max_tries) +
        "s", "info")

    provider, xxx, region, default_ssh_key, cloud_keys = cloud.read(
        cloud_name, True)

    kount = 0
    while kount < max_tries:
        svr, name, size, state, location, private_ip, \
        public_ip, key_name, vcpus, volumes \
          = get_describe_data(provider, machine_id, region, cloud_keys)

        if (state == new_state) or (state == "active"):
            util.message("  " + new_state, "info")
            return (new_state)

        if state == "error":
            util.message(state, "error")
            return ("error")

        util.message("  " + state, "info")
        kount = kount + 1
        time.sleep(interval)

    util.message("max tries exceeded", "error")
    return ("error")
示例#4
0
def get_image(driver, cloud_name, platform='amd'):
    util.message("getting default image", "info")

    provider, xxx, region, default_ssh_key, cloud_keys = cloud.read(
        cloud_name, True)

    sql = "SELECT image_id, image_type FROM images \n" + \
          " WHERE provider = %s AND region = %s AND platform = %s AND is_default = 1"
    data = cloud.exec_sql(sql, [provider, region, platform])
    if data == None or data == []:
        util.message("Image not known for " + str(cloud_name) + \
          ", " + str(region) + ", " + str(platform) + ")", "error")
        return (None, None)

    image_id = str(data[0])
    image_type = str(data[1])

    if provider == 'aws':
        images = driver.list_images(ex_image_ids=image_id.split())
    else:
        images = driver.list_images()

    for i in images:
        if i.id == image_id:
            util.message("image_id - " + image_type + " : " + image_id, "info")
            return (i, image_type)

    util.message("Cannot Locate image '" + str(image_id) + "'", "error")
    return (None, None)
示例#5
0
def launch(cloud_name, size, name=None, key=None, location=None, security_group=None, \
           network=None, data_gb=None, wait_for=True):

    provider, xxx, region, default_ssh_key, cloud_keys = cloud.read(
        cloud_name, True)

    if key == None:
        key = default_ssh_key

    if name == None:
        machine_name = "?"
    else:
        machine_name = name

    util.message("launching - " + str(cloud_name) + ", " + \
      str(size) + ", " + str(key))
    try:
        driver = cloud.get_cloud_driver(cloud_name)
        if driver == None:
            util.message("cloud driver not found", "error")
            return (None)

        size = get_size(provider, size)

        util.message("validating size - " + str(size), "info")
        sizes = driver.list_sizes()
        for s in sizes:
            if s.name == size:
                sz = s
                break
        else:
            util.message("Invalid Size - " + str(size), "error")
            return (None)

        im, typ = get_image(driver, cloud_name)
        if im == None:
            return (None)

        util.message("creating machine on " + str(provider), "info")
        if provider == 'aws':
            nd = driver.create_node (name=machine_name, size=sz, image=im, ex_keyname=key, \
              ex_security_groups=["default"])
        else:
            nd = driver.create_node (name=machine_name, size=sz, image=im, ex_keyname=key, \
              ex_config_drive=True, ex_security_groups=driver.ex_list_security_groups(), \
              networks=driver.ex_list_networks())

    except Exception as e:
        util.message(str(e), "error")
        return (None)

    machine_id = nd.id
    util.message("machine_id - " + str(machine_id), "info")

    if wait_for == True:
        waitfor(cloud_name, machine_id, "running")

    return (machine_id)
示例#6
0
def rule_create(cloud_name, group_id, port, cidr="0.0.0.0/0"):
  provider, xxx, region, default_ssh_key, cloud_keys = cloud.read(cloud_name, True)

  if provider == "aws":
    rule_id = rule_create_aws(region, cloud_keys, group_id, port, cidr)
  else:
    rule_id = rule_create_openstack(region, cloud_keys, group_id, port, cidr)

  return(rule_id)
示例#7
0
def group_list(cloud_name, group_name=None, data_only=False):
  provider, xxx, region, default_ssh_key, cloud_keys = cloud.read(cloud_name, True)

  if provider == "aws":
    gl = group_list_aws(region, cloud_keys, group_name)
  else:
    gl = group_list_openstack(region, cloud_keys, group_name)

  if data_only == True:
    return(gl)

  headers = ['ID', 'Cidr', 'Port', 'Name']
  keys = ['id', 'cidr', 'port', 'name']

  util.print_list(headers, keys, gl)

  return
示例#8
0
def group_create(cloud_name, service, port=None, cidr="0.0.0.0/0"):
  provider, xxx, region, default_ssh_key, cloud_keys = cloud.read(cloud_name, True)

  group_name = get_unique_name(service, group_list(cloud_name, data_only=True))

  if provider == "aws":
    group_id = group_create_aws(region, cloud_keys, group_name)
  else:
    group_id = group_create_openstack(region, cloud_keys, group_name)

  if group_id == None:
    return

  ##util.message("group_id = " + str(group_id))

  if port == None:
    port = get_service_port(service)
    util.message("port = "  + str(port), "info")

  rule_create(cloud_name, group_id, port, cidr="0.0.0.0/0")

  return(group_id)