Example #1
0
def bundle_userdata_files(fileobjs,
                          tar_fname=None,
                          compress=True,
                          use_cloudinit=True):
    script_type = starts_with_mappings['#!']
    ignored_type = starts_with_mappings['#ignored']
    for i, fobj in enumerate(fileobjs):
        ftype = _get_type_from_fp(fobj)
        if ftype == ignored_type:
            fileobjs[i] = utils.string_to_file("#!/bin/false\n" + fobj.read(),
                                               fobj.name)
            continue
        elif ftype != script_type:
            use_cloudinit = True
    if use_cloudinit:
        fileobjs += [
            utils.string_to_file('#cloud-config\ndisable_root: 0',
                                 'starcluster_cloud_config.txt')
        ]
        return mp_userdata_from_files(fileobjs, compress=compress)
    else:
        fileobjs += [
            utils.string_to_file(ENABLE_ROOT_LOGIN_SCRIPT,
                                 'starcluster_enable_root_login.sh')
        ]
        return userdata_script_from_files(fileobjs, tar_fname=tar_fname)
Example #2
0
def bundle_userdata_files(fileobjs, tar_fname=None, compress=True,
                          use_cloudinit=True):
    script_type = starts_with_mappings['#!']
    ignored_type = starts_with_mappings['#ignored']
    for i, fobj in enumerate(fileobjs):
        ftype = _get_type_from_fp(fobj)
        if ftype == ignored_type:
            fileobjs[i] = utils.string_to_file("#!/bin/false\n" + fobj.read(),
                                               fobj.name)
            continue
        elif ftype != script_type:
            use_cloudinit = True
    if use_cloudinit:
        fileobjs += [utils.string_to_file('#cloud-config\ndisable_root: 0',
                                          'starcluster_cloud_config.txt')]
        return mp_userdata_from_files(fileobjs, compress=compress)
    else:
        fileobjs += [utils.string_to_file(ENABLE_ROOT_LOGIN_SCRIPT,
                                          'starcluster_enable_root_login.sh')]
        return userdata_script_from_files(fileobjs, tar_fname=tar_fname)
def _test_append_userdata(compress=True, use_cloudinit=True):
    ud = _get_sample_userdata(compress=compress, use_cloudinit=use_cloudinit)
    unbundled = userdata.unbundle_userdata(ud, decompress=compress)
    new_script = '#!/bin/bash\ndate'
    new_fname = 'newfile.sh'
    assert new_fname not in unbundled
    unbundled[new_fname] = new_script
    new_fobj = utils.string_to_file(new_script, new_fname)
    new_ud = userdata.append_to_userdata(ud, [new_fobj], decompress=compress)
    new_unbundled = userdata.unbundle_userdata(new_ud, decompress=compress)
    assert new_unbundled == unbundled
Example #4
0
def _test_append_userdata(compress=True, use_cloudinit=True):
    ud = _get_sample_userdata(compress=compress, use_cloudinit=use_cloudinit)
    unbundled = userdata.unbundle_userdata(ud, decompress=compress)
    new_script = '#!/bin/bash\ndate'
    new_fname = 'newfile.sh'
    assert new_fname not in unbundled
    unbundled[new_fname] = new_script
    new_fobj = utils.string_to_file(new_script, new_fname)
    new_ud = userdata.append_to_userdata(ud, [new_fobj], decompress=compress)
    new_unbundled = userdata.unbundle_userdata(new_ud, decompress=compress)
    assert new_unbundled == unbundled
Example #5
0
            if f.name in filenames:
                continue
            contents = StringIO.StringIO(orig_tf.extractfile(f).read())
            new_tf.addfile(f, contents)
        new_tf.close()
        tarstr.seek(0)
        new_tf = tarfile.TarFile(fileobj=tarstr, mode='r')
        return userdata_script_from_files([], tar_file=new_tf)
    else:
        mpmime = get_mp_from_userdata(userdata_string, decompress=decompress)
        msgs = []
        for msg in mpmime.get_payload():
            if msg.get_filename() in filenames:
                continue
            msgs.append(msg)
        mpmime.set_payload(msgs)
        return mp_userdata_from_files([], multipart_mime=mpmime,
                                      compress=decompress)


if __name__ == '__main__':
    files = utils.strings_to_files(['#!/bin/bash\nhostname',
                                    '#!/bin/bash\ndate'],
                                   fname_prefix='sc_userdata_file')
    files += utils.string_to_file('#ignored\nblahblahblah', 'sc_metadata')
    script = bundle_userdata_files(files, use_cloudinit=False)
    f = open('/tmp/tester.sh', 'w')
    f.write(script)
    f.close()
    os.chmod('/tmp/tester.sh', 0750)
Example #6
0
                continue
            contents = StringIO.StringIO(orig_tf.extractfile(f).read())
            new_tf.addfile(f, contents)
        new_tf.close()
        tarstr.seek(0)
        new_tf = tarfile.TarFile(fileobj=tarstr, mode='r')
        return userdata_script_from_files([], tar_file=new_tf)
    else:
        mpmime = get_mp_from_userdata(userdata_string, decompress=decompress)
        msgs = []
        for msg in mpmime.get_payload():
            if msg.get_filename() in filenames:
                continue
            msgs.append(msg)
        mpmime.set_payload(msgs)
        return mp_userdata_from_files([],
                                      multipart_mime=mpmime,
                                      compress=decompress)


if __name__ == '__main__':
    files = utils.strings_to_files(
        ['#!/bin/bash\nhostname', '#!/bin/bash\ndate'],
        fname_prefix='sc_userdata_file')
    files += utils.string_to_file('#ignored\nblahblahblah', 'sc_metadata')
    script = bundle_userdata_files(files, use_cloudinit=False)
    f = open('/tmp/tester.sh', 'w')
    f.write(script)
    f.close()
    os.chmod('/tmp/tester.sh', 0750)