'Create a tar.gz file from the instance specified by the options.
The file will be written to options.tarball_uri.
It can be later turned into a GCE image by passing it as the --source-uri
to gcloud images create.'

def create_tarball(self):

    project = self.__project
    basename = os.path.basename(self.options.tarball_uri).replace('_', '-')
    first_dot = basename.find('.')
    if first_dot:
        basename = basename[0:first_dot]
    disk_name = '{name}-export'.format(name=basename)
    print 'Attaching    external    disk    "{disk}"    to    extract    image    tarball.'.format(disk=disk_name)
    check_run_quick('gcloud    {account}    compute    disks    create        {disk_name}    --project    {project}    --zone    {zone}    --type    pd-ssd'.format(account=self.__gcloud_account_arg, disk_name=disk_name, project=self.__project, zone=self.__zone), echo=False)
    check_run_quick('gcloud    {account}    compute    instances    attach-disk    {instance}    --disk={disk_name}    --device-name=export-disk    --project={project}    --zone={zone}'.format(account=self.__gcloud_account_arg, instance=self.__instance, disk_name=disk_name, project=self.__project, zone=self.__zone), echo=False)
    print 'Attaching    snapshot    "{name}-snapshot".'.format(name=self.__instance)
    check_run_quick('gcloud    {account}    compute    disks    create        {name}-snapshot    --source-snapshot    {name}-snapshot    --project    {project}    --zone    {zone}    --type    pd-ssd'.format(account=self.__gcloud_account_arg, name=self.__instance, project=self.__project, zone=self.__zone), echo=False)
    check_run_quick('gcloud    {account}    compute    instances    attach-disk    {instance}    --disk={instance}-snapshot    --device-name=snapshot-disk    --project={project}    --zone={zone}'.format(account=self.__gcloud_account_arg, instance=self.__instance, project=self.__project, zone=self.__zone), echo=False)
    try:
        self.__extract_image_tarball_helper()
    finally:
        print 'Detaching    and    deleting    external    disks.'
        for name in [disk_name, '{name}-snapshot'.format(name=self.__instance)]:
            run_quick('gcloud    {account}    compute    instances    detach-disk    -q    {instance}    --disk={name}    --project={project}    --zone={zone}'.format(account=self.__gcloud_account_arg, instance=self.__instance, name=name, project=self.__project, zone=self.__zone), echo=False)
            run_quick('gcloud    {account}    compute    disks    delete    -q    {name}    --project={project}    --zone={zone}'.format(account=self.__gcloud_account_arg, name=name, project=self.__project, zone=self.__zone), echo=False)
