Beispiel #1
0
def GetParentIdentifier(f):
    """Try to look at the parent identifier from a docker image.

  The identifier is expected to be in the 'top' file for our rule so we look at
  it first ('./top', 'top'). If it's not found, then we use the 'repositories'
  file and tries to parse it to get the first declared repository (so we can
  actually parse a file generated by 'docker save').

  Args:
    f: the input tar file.
  Returns:
    The identifier of the docker image, or None if no identifier was found.
  """
    # TODO(dmarting): Maybe we could drop the 'top' file all together?
    top = utils.GetTarFile(f, 'top')
    if top:
        return top.strip()
    repositories = utils.GetTarFile(f, 'repositories')
    if repositories:
        data = json.loads(repositories)
        for k1 in data:
            for k2 in data[k1]:
                # Returns the first found key
                return data[k1][k2].strip()
    return None
def main(unused_argv):
    base_json = '{}'
    manifest = utils.GetLatestManifestFromTar(FLAGS.base)
    if manifest:
        config_file = manifest['Config']
        base_json = utils.GetTarFile(FLAGS.base, config_file)
    data = json.loads(base_json)

    layers = []
    for layer in FLAGS.layer:
        layers.append(utils.ExtractValue(layer))

    labels = KeyValueToDict(FLAGS.labels)
    for label, value in labels.iteritems():
        if value.startswith('@'):
            with open(value[1:], 'r') as f:
                labels[label] = f.read()

    output = CreateImageConfig(
        data,
        ConfigOptions(layers=layers,
                      entrypoint=FLAGS.entrypoint,
                      cmd=FLAGS.command,
                      user=FLAGS.user,
                      labels=labels,
                      env=KeyValueToDict(FLAGS.env),
                      ports=FLAGS.ports,
                      volumes=FLAGS.volumes,
                      workdir=FLAGS.workdir))

    with open(FLAGS.output, 'w') as fp:
        json.dump(output, fp, sort_keys=True)
        fp.write('\n')
Beispiel #3
0
def main(unused_argv):
    parent = ''
    base_json = '{}'
    if FLAGS.base:
        parent = GetParentIdentifier(FLAGS.base)
        if parent:
            base_json = utils.GetTarFile(FLAGS.base, '%s/json' % parent)
    data = json.loads(base_json)

    name = FLAGS.name
    if name.startswith('@'):
        with open(name[1:], 'r') as f:
            name = f.read()

    labels = KeyValueToDict(FLAGS.labels)
    for label, value in labels.items():
        if value.startswith('@'):
            with open(value[1:], 'r') as f:
                labels[label] = f.read()

    output = RewriteMetadata(
        data,
        MetadataOptions(name=name,
                        parent=parent,
                        size=os.path.getsize(FLAGS.layer),
                        entrypoint=FLAGS.entrypoint,
                        cmd=FLAGS.command,
                        user=FLAGS.user,
                        labels=labels,
                        env=KeyValueToDict(FLAGS.env),
                        ports=FLAGS.ports,
                        volumes=FLAGS.volumes,
                        workdir=FLAGS.workdir))

    with open(FLAGS.output, 'w') as fp:
        json.dump(output, fp, sort_keys=True)
        fp.write('\n')