当前位置: 首页>>代码示例>>Python>>正文


Python Utils.runshellcommand方法代码示例

本文整理汇总了Python中utils.Utils.runshellcommand方法的典型用法代码示例。如果您正苦于以下问题:Python Utils.runshellcommand方法的具体用法?Python Utils.runshellcommand怎么用?Python Utils.runshellcommand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在utils.Utils的用法示例。


在下文中一共展示了Utils.runshellcommand方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: customizeImage

# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import runshellcommand [as 别名]
def customizeImage(config, mount_path):
    build_scripts_path = os.path.dirname(os.path.abspath(__file__))
    image_name = config['image_type']
    if 'additionalfiles' in config:
        for filetuples in config['additionalfiles']:
            for src, dest in filetuples.items():
                if (os.path.isdir(build_scripts_path + '/' +
                                  image_name + '/' + src)):
                    shutil.copytree(build_scripts_path + '/' +
                                    image_name + '/' + src,
                                    mount_path + dest, True)
                else:
                    shutil.copyfile(build_scripts_path + '/' +
                                    image_name + '/' + src,
                                    mount_path + dest)
    if 'postinstallscripts' in config:
        if not os.path.exists(mount_path + "/tempscripts"):
            os.mkdir(mount_path + "/tempscripts")
        for script in config['postinstallscripts']:
            shutil.copy(build_scripts_path + '/' +
                        image_name + '/' + script,
                        mount_path + "/tempscripts")
        for script in os.listdir(mount_path + "/tempscripts"):
            print("     ...running script {}".format(script))
            Utils.runshellcommand(
                "chroot {} /bin/bash -c '/tempscripts/{}'".format(mount_path, script))
        shutil.rmtree(mount_path + "/tempscripts", ignore_errors=True)
    if 'expirepassword' in config and config['expirepassword']:
        # Do not run 'chroot -R' from outside. It will not find nscd socket.
        Utils.runshellcommand("chroot {} /bin/bash -c 'chage -d 0 root'".format(mount_path))
开发者ID:frapposelli,项目名称:photon,代码行数:32,代码来源:imagegenerator.py

示例2: prepLoopDevice

# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import runshellcommand [as 别名]
def prepLoopDevice(loop_device_path, mount_path):
    Utils.runshellcommand(
            "mount -t ext4 {} {}".format(loop_device_path, mount_path))
    Utils.runshellcommand("mount -o bind /proc {}".format(mount_path + "/proc"))
    Utils.runshellcommand("mount -o bind /dev {}".format(mount_path + "/dev"))
    Utils.runshellcommand("mount -o bind /dev/pts {}".format(mount_path + "/dev/pts"))
    Utils.runshellcommand("mount -o bind /sys {}".format(mount_path + "/sys"))        
开发者ID:frapposelli,项目名称:photon,代码行数:9,代码来源:imagegenerator.py

示例3: installAdditionalRpms

# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import runshellcommand [as 别名]
def installAdditionalRpms(mount_path, additional_rpms_path):
    os.mkdir(mount_path + "/additional_rpms")
    Utils.copyallfiles(additional_rpms_path,
                       mount_path + "/additional_rpms")
    Utils.runshellcommand(
        "chroot {} /bin/bash -c 'rpm -i /additional_rpms/*'".format(mount_path))
    shutil.rmtree(mount_path + "/additional_rpms", ignore_errors=True)
    shutil.rmtree(additional_rpms_path, ignore_errors=True)
开发者ID:frapposelli,项目名称:photon,代码行数:10,代码来源:imagegenerator.py

示例4: generateUuid

# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import runshellcommand [as 别名]
def generateUuid(loop_device_path):
    partuuidval = (Utils.runshellcommand(
        "blkid -s PARTUUID -o value {}".format(loop_device_path))).rstrip('\n')
    uuidval = (Utils.runshellcommand(
        "blkid -s UUID -o value {}".format(loop_device_path))).rstrip('\n')
    if partuuidval == '':
        sgdiskout = Utils.runshellcommand(
            "sgdisk -i 2 {} ".format(disk_device))
        partuuidval = (re.findall(r'Partition unique GUID.*',
                                  sgdiskout))[0].split(':')[1].strip(' ').lower()

    if partuuidval == '':
        raise RuntimeError("Cannot generate partuuid")

    return (uuidval, partuuidval)
开发者ID:frapposelli,项目名称:photon,代码行数:17,代码来源:imagegenerator.py

示例5: generateImage

# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import runshellcommand [as 别名]
def generateImage(raw_image_path, additional_rpms_path, tools_bin_path, src_root, config):
    working_directory = os.path.dirname(raw_image_path)
    mount_path = os.path.splitext(raw_image_path)[0]
    build_scripts_path = os.path.dirname(os.path.abspath(__file__))

    if os.path.exists(mount_path) and os.path.isdir(mount_path):
        shutil.rmtree(mount_path)
    os.mkdir(mount_path)
    disk_device = (Utils.runshellcommand(
        "losetup --show -f {}".format(raw_image_path))).rstrip('\n')
    disk_partitions = Utils.runshellcommand("kpartx -as {}".format(disk_device))
    device_name = disk_device.split('/')[2]
    if not device_name:
        raise Exception("Could not create loop device and partition")

    loop_device_path = "/dev/mapper/{}p2".format(device_name)

    print(loop_device_path)

    try:
        (uuidval, partuuidval) = generateUuid(loop_device_path)
        # Prep the loop device
        prepLoopDevice(loop_device_path, mount_path)
        # Clear the root password if not set explicitly from the config file
        if config['passwordtext'] == 'PASSWORD':
            Utils.replaceinfile(mount_path + "/etc/shadow",
                                'root:.*?:', 'root:*:')
        # Clear machine-id so it gets regenerated on boot
        open(mount_path + "/etc/machine-id", "w").close()
        # Write fstab
        writefstabandgrub(mount_path, uuidval, partuuidval)
        if additional_rpms_path and os.path.exists(additional_rpms_path):
            installAdditionalRpms(mount_path, additional_rpms_path)
        # Perform additional steps defined in installer config
        customizeImage(config, mount_path)
    except Exception as e:
        print(e)
    finally:
        cleanupMountPoints(mount_path)
        Utils.runshellcommand("kpartx -d {}".format(disk_device))
        Utils.runshellcommand("losetup -d {}".format(disk_device))

        shutil.rmtree(mount_path)
        createOutputArtifact(raw_image_path, config, src_root, tools_bin_path)
开发者ID:frapposelli,项目名称:photon,代码行数:46,代码来源:imagegenerator.py

示例6: create_ova_image

# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import runshellcommand [as 别名]
def create_ova_image(raw_image_name, tools_path, build_scripts_path, config):
    output_path = os.path.dirname(os.path.realpath(raw_image_name))
    utils = Utils()
    # Remove older artifacts
    files = os.listdir(output_path)
    for file in files:
        if file.endswith(".vmdk"):
            os.remove(os.path.join(output_path, file))

    vmx_path = output_path + '/photon-ova.vmx'
    utils.replaceandsaveasnewfile(build_scripts_path + '/vmx-template',
                                  vmx_path, 'VMDK_IMAGE',
                                  output_path + '/photon-ova.vmdk')
    vixdiskutil_path = tools_path + 'vixdiskutil'
    vmdk_path = output_path + '/photon-ova.vmdk'
    ovf_path = output_path + '/photon-ova.ovf'
    mf_path = output_path + '/photon-ova.mf'
    ovfinfo_path = build_scripts_path + '/ovfinfo.txt'
    vmdk_capacity = (int(config['size']['root']) +
                     int(config['size']['swap'])) * 1024
    utils.runshellcommand(
        "{} -convert {} -cap {} {}".format(vixdiskutil_path,
                                           raw_image_name,
                                           vmdk_capacity,
                                           vmdk_path))
    utils.runshellcommand(
        "{} -wmeta toolsVersion 2147483647 {}".format(vixdiskutil_path, vmdk_path))

    utils.runshellcommand("ovftool {} {}".format(vmx_path, ovf_path))
    utils.replaceinfile(ovf_path, 'otherGuest', 'other3xLinux64Guest')

    #Add product info
    if os.path.exists(ovfinfo_path):
        with open(ovfinfo_path) as f:
            lines = f.readlines()
            for line in fileinput.input(ovf_path, inplace=True):
                if line.strip() == '</VirtualHardwareSection>':
                    for ovfinfoline in lines:
                        print(ovfinfoline)
                else:
                    print(line)

    if os.path.exists(mf_path):
        os.remove(mf_path)

    cwd = os.getcwd()
    os.chdir(output_path)
    out = utils.runshellcommand("openssl sha1 photon-ova-disk1.vmdk photon-ova.ovf")
    with open(mf_path, "w") as source:
        source.write(out)
    rawsplit = os.path.splitext(raw_image_name)
    ova_name = rawsplit[0] + '.ova'

    ovatar = tarfile.open(ova_name, "w", format=tarfile.USTAR_FORMAT)
    for name in ["photon-ova.ovf", "photon-ova.mf", "photon-ova-disk1.vmdk"]:
        ovatar.add(name, arcname=os.path.basename(name))
    ovatar.close()
    os.remove(vmx_path)
    os.remove(mf_path)

    if 'additionalhwversion' in config:
        for addlversion in config['additionalhwversion']:
            new_ovf_path = output_path + "/photon-ova-hw{}.ovf".format(addlversion)
            mf_path = output_path + "/photon-ova-hw{}.mf".format(addlversion)
            utils.replaceandsaveasnewfile(
                ovf_path, new_ovf_path, "vmx-.*<", "vmx-{}<".format(addlversion))
            out = utils.runshellcommand("openssl sha1 photon-ova-disk1.vmdk "
                                        "photon-ova-hw{}.ovf".format(addlversion))
            with open(mf_path, "w") as source:
                source.write(out)
            temp_name_list = os.path.basename(ova_name).split('-')
            temp_name_list = temp_name_list[:2] + ["hw{}".format(addlversion)] + temp_name_list[2:]
            new_ova_name = '-'.join(temp_name_list)
            new_ova_path = output_path + '/' + new_ova_name
            ovatar = tarfile.open(new_ova_path, "w", format=tarfile.USTAR_FORMAT)
            for name in [new_ovf_path, mf_path, "photon-ova-disk1.vmdk"]:
                ovatar.add(name, arcname=os.path.basename(name))
            ovatar.close()

            os.remove(new_ovf_path)
            os.remove(mf_path)
    os.chdir(cwd)
    os.remove(ovf_path)
    os.remove(vmdk_path)
    files = os.listdir(output_path)
    for file in files:
        if file.endswith(".vmdk"):
            os.remove(os.path.join(output_path, file))
开发者ID:TiejunChina,项目名称:photon,代码行数:90,代码来源:customize_cloud_image.py

示例7: Utils

# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import runshellcommand [as 别名]
    parser.add_argument("-r", "--raw-image-path", dest="raw_image_path")
    parser.add_argument("-c", "--vmdk-config-path", dest="vmdk_config_path")
    parser.add_argument("-w", "--working-directory", dest="working_directory")
    parser.add_argument("-m", "--mount-path", dest="mount_path")
    parser.add_argument("-a", "--additional-rpms-path", dest="additional_rpms_path")
    parser.add_argument("-i", "--image-name", dest="image_name")
    parser.add_argument("-t", "--tools-bin-path", dest="tools_bin_path")
    parser.add_argument("-b", "--build-scripts-path", dest="build_scripts_path")
    parser.add_argument("-s", "--src-root", dest="src_root")

    options = parser.parse_args()
    utils = Utils()
    config = utils.jsonread(options.vmdk_config_path)
    print(options)

    disk_device = (utils.runshellcommand(
        "losetup --show -f {}".format(options.raw_image_path))).rstrip('\n')
    disk_partitions = utils.runshellcommand("kpartx -as {}".format(disk_device))
    device_name = disk_device.split('/')[2]

    if not os.path.exists(options.mount_path):
        os.mkdir(options.mount_path)
    loop_device_path = "/dev/mapper/{}p2".format(device_name)

    try:
        print("Generating PARTUUID for the loop device ...")
        partuuidval = (utils.runshellcommand(
            "blkid -s PARTUUID -o value {}".format(loop_device_path))).rstrip('\n')
        uuidval = (utils.runshellcommand(
            "blkid -s UUID -o value {}".format(loop_device_path))).rstrip('\n')
        if partuuidval == '':
            sgdiskout = utils.runshellcommand(
开发者ID:TiejunChina,项目名称:photon,代码行数:34,代码来源:customize_cloud_image.py

示例8: createOutputArtifact

# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import runshellcommand [as 别名]
def createOutputArtifact(raw_image_path, config, src_root, tools_bin_path):
    photon_release_ver = os.environ['PHOTON_RELEASE_VER']
    photon_build_num = os.environ['PHOTON_BUILD_NUM']
    new_name = ""
    img_path = os.path.dirname(os.path.realpath(raw_image_path))
    # Rename gce image to disk.raw
    if config['image_type'] == "gce":
        new_name = img_path + '/disk.raw'

    else:
        new_name = (img_path + '/photon-' + config['image_type'] +
                    '-' + photon_release_ver + '-' +
                    photon_build_num + '.raw')

    shutil.move(raw_image_path, new_name)
    raw_image = new_name

    if config['artifacttype'] == 'tgz':
        print("Generating the tar.gz artifact ...")
        outputfile = (img_path + '/photon-' + config['image_type'] +
                      '-' + photon_release_ver + '-' +
                      photon_build_num + '.tar.gz')
        generateCompressedFile(raw_image, outputfile, "w:gz")
    elif config['artifacttype'] == 'xz':
        print("Generating the xz artifact ...")
        outputfile = (img_path + '/photon-' + config['image_type'] +
                      '-' + photon_release_ver + '-' +
                      photon_build_num + '.xz')
        generateCompressedFile(raw_image, outputfile, "w:xz")
    elif 'vhd' in config['artifacttype']:
        relrawpath = os.path.relpath(raw_image, src_root)
        vhdname = (os.path.dirname(relrawpath) + '/photon-' +
                   config['image_type'] + '-' + photon_release_ver + '-' +
                   photon_build_num + '.vhd')
        print("Converting raw disk to vhd ...")
        info_output = Utils.runshellcommand(
            "docker run -v {}:/mnt:rw anishs/qemu-img info -f raw --output json {}"
            .format(src_root, '/mnt/' + relrawpath))
        mbsize = 1024 * 1024
        mbroundedsize = ((int(json.loads(info_output)["virtual-size"])/mbsize + 1) * mbsize)
        Utils.runshellcommand(
            "docker run -v {}:/mnt:rw anishs/qemu-img resize -f raw {} {}"
            .format(src_root, '/mnt/' + relrawpath, mbroundedsize))
        Utils.runshellcommand(
            "docker run -v {}:/mnt:rw anishs/qemu-img convert {} -O "
            "vpc -o subformat=fixed,force_size {}"
            .format(src_root, '/mnt/' + relrawpath, '/mnt/' + vhdname))
        if config['artifacttype'] == 'vhd.gz':
            outputfile = (img_path + '/photon-' + config['image_type'] +
                          '-' + photon_release_ver + '-' +
                          photon_build_num + '.vhd.tar.gz')
            generateCompressedFile(vhdname, outputfile, "w:gz")
    elif config['artifacttype'] == 'ova':
        ovagenerator.create_ova_image(raw_image, tools_bin_path, config)
    elif config['artifacttype'] == 'raw':
        pass
    else:
        raise ValueError("Unknown output format")

    if not config['keeprawdisk']:
        os.remove(raw_image)
开发者ID:frapposelli,项目名称:photon,代码行数:63,代码来源:imagegenerator.py

示例9: cleanupMountPoints

# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import runshellcommand [as 别名]
def cleanupMountPoints(mount_path):
    Utils.runshellcommand("umount -l {}".format(mount_path + "/sys"))
    Utils.runshellcommand("umount -l {}".format(mount_path + "/dev/pts"))
    Utils.runshellcommand("umount -l {}".format(mount_path + "/dev"))
    Utils.runshellcommand("umount -l {}".format(mount_path + "/proc"))

    Utils.runshellcommand("sync")
    Utils.runshellcommand("umount -l {}".format(mount_path))
开发者ID:frapposelli,项目名称:photon,代码行数:10,代码来源:imagegenerator.py

示例10: Utils

# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import runshellcommand [as 别名]
    parser.add_option("-r", "--raw-image-path",  dest="raw_image_path")
    parser.add_option("-c", "--vmdk-config-path", dest="vmdk_config_path")
    parser.add_option("-w",  "--working-directory",  dest="working_directory")
    parser.add_option("-m",  "--mount-path",  dest="mount_path")
    parser.add_option("-a",  "--additional-rpms-path",  dest="additional_rpms_path")
    parser.add_option("-i",  "--image-name",  dest="image_name")
    parser.add_option("-t",  "--tools-bin-path",  dest="tools_bin_path")
    parser.add_option("-b",  "--build-scripts-path",  dest="build_scripts_path")

    (options,  args) = parser.parse_args()
    utils = Utils()
    config = utils.jsonread(options.vmdk_config_path)
    print options

    disk_device = (utils.runshellcommand("losetup --show -f {}".format(options.raw_image_path))).rstrip('\n')
    disk_partitions = utils.runshellcommand("kpartx -as {}".format(disk_device))
    device_name = disk_device.split('/')[2]

    if not os.path.exists(options.mount_path):
        os.mkdir(options.mount_path)
    loop_device_path =  "/dev/mapper/{}p2".format(device_name)

    try:
        print "Generating PARTUUID for the loop device ..."
        partuuidval = (utils.runshellcommand("blkid -s PARTUUID -o value {}".format(loop_device_path))).rstrip('\n')
        uuidval = (utils.runshellcommand("blkid -s UUID -o value {}".format(loop_device_path))).rstrip('\n')
        if (partuuidval == ''):
            sgdiskout = utils.runshellcommand("sgdisk -i 2 {} ".format(disk_device))
            partuuidval = (re.findall(r'Partition unique GUID.*', sgdiskout))[0].split(':')[1].strip(' ').lower()
开发者ID:megacoder,项目名称:photon,代码行数:31,代码来源:customize_cloud_image.py

示例11: create_ova_image

# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import runshellcommand [as 别名]
def create_ova_image(raw_image_name, tools_path, build_scripts_path, config):
    output_path = os.path.dirname(os.path.realpath(raw_image_name))
    utils = Utils()
    # Remove older artifacts
    files = os.listdir(output_path)    
    for file in files:
        if file.endswith(".vmdk"):
            os.remove(os.path.join(output_path, file))

    vmx_path = output_path + '/photon-ova.vmx'
    utils.replaceandsaveasnewfile(build_scripts_path + '/vmx-template', vmx_path, 'VMDK_IMAGE', output_path + '/photon-ova.vmdk')
    vixdiskutil_path = tools_path + 'vixdiskutil'
    vmdk_path = output_path + '/photon-ova.vmdk'
    ovf_path = output_path + '/photon-ova.ovf'
    mf_path = output_path + '/photon-ova.mf'
    utils.runshellcommand("{} -convert {} -cap 16000 {}".format(vixdiskutil_path, raw_image_name, vmdk_path))
    utils.runshellcommand("{} -wmeta toolsVersion 2147483647 {}".format(vixdiskutil_path, vmdk_path))

    utils.runshellcommand("ovftool {} {}".format(vmx_path, ovf_path))
    utils.replaceinfile(ovf_path, 'otherGuest', 'other3xLinux64Guest')

    #Add product info
    for line in fileinput.input(ovf_path, inplace=True):
        if line.strip() == '</VirtualSystem>':
            print ' \t<ProductSection> \n \t\t<Info>Information about the installed software</Info> \n \t\t<Product>Photon</Product> \n \t\t<Vendor>VMware Inc.</Vendor> \n \t\t<Version>1.0</Version> \n \t\t<FullVersion>1.0</FullVersion> \n \t</ProductSection> '
        print line,

    if os.path.exists(mf_path):
        os.remove(mf_path)

    cwd = os.getcwd()
    os.chdir(output_path)
    out = utils.runshellcommand("openssl sha1 photon-ova-disk1.vmdk photon-ova.ovf")
    with open(mf_path, "w") as source:
        source.write(out)
    rawsplit = os.path.splitext(raw_image_name)
    ova_name = rawsplit[0] + '.ova'

    ovatar = tarfile.open(ova_name, "w", format = tarfile.USTAR_FORMAT)
    for name in ["photon-ova.ovf", "photon-ova.mf", "photon-ova-disk1.vmdk"]:
        ovatar.add(name, arcname=os.path.basename(name))
    ovatar.close()
    os.remove(vmx_path)    
    os.remove(mf_path)

    if 'additionalhwversion' in config:
        for addlversion in config['additionalhwversion']:
            new_ovf_path = output_path + "/photon-ova-hw{}.ovf".format(addlversion)
            mf_path = output_path + "/photon-ova-hw{}.mf".format(addlversion)
            utils.replaceandsaveasnewfile(ovf_path, new_ovf_path, "vmx-.*<", "vmx-{}<".format(addlversion)) 
            out = utils.runshellcommand("openssl sha1 photon-ova-disk1.vmdk photon-ova-hw{}.ovf".format(addlversion))
            with open(mf_path, "w") as source:
                source.write(out)
            temp_name_list = os.path.basename(ova_name).split('-')
            temp_name_list = temp_name_list[:2] + ["hw{}".format(addlversion)] + temp_name_list[2:]
            new_ova_name = '-'.join(temp_name_list)
            new_ova_path = output_path + '/' + new_ova_name
            ovatar = tarfile.open(new_ova_path, "w", format = tarfile.USTAR_FORMAT)
            for name in [new_ovf_path, mf_path, "photon-ova-disk1.vmdk"]:
                ovatar.add(name, arcname=os.path.basename(name))
            ovatar.close()

            os.remove(new_ovf_path)
            os.remove(mf_path)
    os.chdir(cwd)    
    os.remove(ovf_path)
    os.remove(vmdk_path)
    files = os.listdir(output_path)    
    for file in files:
        if file.endswith(".vmdk"):
            os.remove(os.path.join(output_path, file))    
开发者ID:scrothers,项目名称:photon,代码行数:73,代码来源:customize_cloud_image.py


注:本文中的utils.Utils.runshellcommand方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。