def handle_json_from_file(json_arg):
    """Attempts to read JSON file by the file url.

    :param json_arg: May be a file name containing the JSON.
    :returns: A list or dictionary parsed from JSON.
    """

    try:
        with open(json_arg, 'r') as f:
            json_arg = f.read().strip()
            json_arg = json.loads(json_arg)
    except IOError as e:
        err = _("Cannot get JSON from file '%(file)s'. "
                "Error: %(err)s") % {
                    'err': e,
                    'file': json_arg
                }
        raise exc.InvalidAttribute(err)
    except ValueError as e:
        err = (_("For JSON: '%(string)s', error: '%(err)s'") % {
            'err': e,
            'string': json_arg
        })
        raise exc.InvalidAttribute(err)

    return json_arg
示例#2
0
 def create(self, **kwargs):
     new = {}
     for (key, value) in kwargs.items():
         if key in CREATION_ATTRIBUTES:
             new[key] = value
         else:
             raise exceptions.InvalidAttribute(
                 "Key must be in %s" % ",".join(CREATION_ATTRIBUTES))
     return self._create(self._path(), new)
def do_pod_list(cs, args):
    """Print a list of registered pods."""
    bay = cs.bays.get(args.bay)
    if bay.status not in ['CREATE_COMPLETE', 'UPDATE_COMPLETE']:
        raise exceptions.InvalidAttribute(
            'Bay status for %s is: %s. We can not list pods in there until'
            ' the status is CREATE_COMPLETE or UPDATE_COMPLETE.' %
            (bay.uuid, bay.status))
    pods = cs.pods.list(args.bay)
    columns = ('uuid', 'name')
    utils.print_list(pods, columns,
                     {'versions': magnum_utils.print_list_field('versions')})
def do_rc_list(cs, args):
    """Print a list of registered replication controllers."""
    bay = cs.bays.get(args.bay)
    if bay.status not in ['CREATE_COMPLETE', 'UPDATE_COMPLETE']:
        raise exceptions.InvalidAttribute(
            'Bay status for %s is: %s. We cannot list '
            'replication controllers in bay until the bay status '
            'is CREATE_COMPLETE or UPDATE_COMPLETE.' % (args.bay, bay.status))

    rcs = cs.rcs.list(args.bay)
    columns = ('uuid', 'name', 'bay_uuid')
    utils.print_list(rcs, columns,
                     {'versions': magnum_utils.print_list_field('versions')})
示例#5
0
    def take_action(self, parsed_args):
        self.log.debug("take_action(%s)", parsed_args)

        mag_client = self.app.client_manager.container_infra
        args = {
            'cluster_template_id': parsed_args.cluster_template,
            'create_timeout': parsed_args.timeout,
            'discovery_url': parsed_args.discovery_url,
            'keypair': parsed_args.keypair,
            'master_count': parsed_args.master_count,
            'name': parsed_args.name,
            'node_count': parsed_args.node_count,
        }

        if len(parsed_args.floating_ip_enabled) > 1:
            raise exceptions.InvalidAttribute('--floating-ip-enabled and '
                                              '--floating-ip-disabled are '
                                              'mutually exclusive and '
                                              'should be specified only once.')
        elif len(parsed_args.floating_ip_enabled) == 1:
            args['floating_ip_enabled'] = parsed_args.floating_ip_enabled[0]

        if parsed_args.labels is not None:
            args['labels'] = magnum_utils.handle_labels(parsed_args.labels)

        if parsed_args.docker_volume_size is not None:
            args['docker_volume_size'] = parsed_args.docker_volume_size

        if parsed_args.master_flavor is not None:
            args['master_flavor_id'] = parsed_args.master_flavor

        if parsed_args.flavor is not None:
            args['flavor_id'] = parsed_args.flavor

        if parsed_args.fixed_network is not None:
            args["fixed_network"] = parsed_args.fixed_network

        if parsed_args.fixed_subnet is not None:
            args["fixed_subnet"] = parsed_args.fixed_subnet

        if parsed_args.merge_labels:
            # We are only sending this if it's True. This
            # way we avoid breaking older APIs.
            args["merge_labels"] = parsed_args.merge_labels

        cluster = mag_client.clusters.create(**args)
        print("Request to create cluster %s accepted" % cluster.uuid)
示例#6
0
def do_container_create(cs, args):
    """Create a container."""
    bay = cs.bays.get(args.bay)
    if bay.status not in ['CREATE_COMPLETE',
                          'UPDATE_IN_PROGRESS', 'UPDATE_COMPLETE']:
        raise exceptions.InvalidAttribute(
            'Bay status for %s is: %s. We cannot create a %s'
            ' unless the status is CREATE_COMPLETE, UPDATE_IN_PROGRESS'
            ' or UPDATE_COMPLETE.' % (bay.uuid, bay.status, "container"))
        return
    opts = {}
    opts['name'] = args.name
    opts['image'] = args.image
    opts['bay_uuid'] = bay.uuid
    opts['command'] = args.command
    opts['memory'] = args.memory
    _show_container(cs.containers.create(**opts))
def do_pod_create(cs, args):
    """Create a pod."""
    bay = cs.bays.get(args.bay)
    if bay.status not in ['CREATE_COMPLETE', 'UPDATE_COMPLETE']:
        raise exceptions.InvalidAttribute(
            'Bay status for %s is: %s. We cannot create a %s'
            ' until the status is CREATE_COMPLETE or UPDATE_COMPLETE.' %
            (bay.uuid, bay.status, "pod"))
    opts = {}
    opts['manifest_url'] = args.manifest_url
    opts['bay_uuid'] = bay.uuid

    if args.manifest is not None and os.path.isfile(args.manifest):
        with open(args.manifest, 'r') as f:
            opts['manifest'] = f.read()

    node = cs.pods.create(**opts)
    _show_pod(node)
    pass
def do_rc_create(cs, args):
    """Create a replication controller."""
    bay = cs.bays.get(args.bay)
    if bay.status not in ['CREATE_COMPLETE', 'UPDATE_COMPLETE']:
        raise exceptions.InvalidAttribute(
            'Bay status for %s is: %s. We cannot create a '
            'replication controller in bay until the status '
            'is CREATE_COMPLETE or UPDATE_COMPLETE.' % (args.bay, bay.status))

    opts = {}
    opts['manifest_url'] = args.manifest_url
    opts['bay_uuid'] = bay.uuid

    if args.manifest is not None and os.path.isfile(args.manifest):
        with open(args.manifest, 'r') as f:
            opts['manifest'] = f.read()

    rc = cs.rcs.create(**opts)
    _show_rc(rc)