Skip to content

Instantly share code, notes, and snippets.

@krislindgren
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krislindgren/fc519aa03d350f42e9e6 to your computer and use it in GitHub Desktop.
Save krislindgren/fc519aa03d350f42e9e6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import os
import hashlib
from contextlib import closing
import argparse
import requests
from keystoneclient.v2_0.client import Client
parser = argparse.ArgumentParser(description='Glance image downloader.')
parser.add_argument('image_id', help="The id of the image to download.")
parser.add_argument('--sha', default='foo', help="The expected checksum of the downloaded image.")
parser.add_argument('--token', default=None, help="The keystone token to use. If none provided, one will be requestsed.")
args = parser.parse_args()
image_id = args.image_id
image_url = "https://<glanceserver>:9292/v1/images/%s" % image_id
expected_sha = args.sha
image_location = '/root/imgdownload/%s.image'
user = os.getenv("OS_USERNAME")
password = os.getenv("OS_PASSWORD")
tenant = os.getenv("OS_TENANT_NAME")
auth_url = os.getenv("OS_AUTH_URL")
token = args.token
if token is None:
print 'Initialized.\nGetting token'
keystone_client = Client(username=user,
password=password,
tenant_name=tenant,
auth_url=auth_url)
auth_token = keystone_client.auth_ref['token']['id']
print 'Got token %s' % auth_token
else:
print 'Using provided token: %s' % token
auth_token = token
headers = {'X-Auth-Token': auth_token,
'Content-Type': 'application/json',
'Accept': 'application/json' }
file_loc = image_location % image_id
print 'File will be written to %s' % file_loc
print "Beginning download: %s" % image_url
print "File won't begin streaming until after a Response code is outputted."
with open(file_loc, 'wb') as f:
r = requests.get(image_url, verify=True, stream=True, headers=headers)
print 'Response code: %s' % r.status_code
with closing(r) as resp:
#s = hashlib.new("md5")
b = 0
for data in resp.iter_content(chunk_size=65535):
f.write(data)
f.flush()
#s.update(data)
b += len(data)
print("Read %s bytes" % b)
# if expected_sha and s.hexdigest() != expected_sha:
# print("Hash difference %s" % (s.hexdigest()))
# sys.exit(1)
#!/bin/bash
#constant
dlfolder="/root/imgdownload/"
authtoken="8c3300f036164760bce2fbfc9fdc8953"
mkdir -p $dlfolder
if [ $# -eq 0 ]
then
echo "You must list all the image uuid's that you want to download."
echo "Also this script assumes that you have bash environment varriables set IE keystonerc_admin to let you use the nova command directly"
echo "Usage: ./multi-img-download-newclient.sh <first_image_uuid> <second_image_uuid> <third_image_uuid>"
fi
function downloadimage() {
local image=$1
( python /root/client.py $image --token $authtoken) &
}
for i in $*
do
downloadimage $i
done
#!/bin/bash
#constants:
dlfolder="/root/imgdownload/"
mkdir -p $dlfolder
if [ $# -eq 0 ]
then
echo "You must list all the image uuid's that you want to download."
echo "Also this script assumes that you have bash environment varriables set IE keystonerc_admin to let you use the glance command directly"
echo "Usage: ./multi-img-download.sh <first_image_uuid> <second_image_uuid> <third_image_uuid>"
fi
function downloadimage() {
local image=$1
( glance image-download $image > $dlfolder$image.qcow2 ) &
}
for i in $*
do
downloadimage $i
done
#!/bin/bash
#constants:
flavor="4" #nova flavor-list
network="2d5fe344-4e98-4ccc-8c91-b8064d17c64c" #nova network-list
availabilityzone="glbt1-dev" #nova availability-zone-list
host="g1dlgenlab06.dev.glbt1.gdg" #nova hypervisor-list
vmbase_name="multimgtst"
if [ $# -eq 0 ]
then
echo "You must list all the image uuid's that you want to boot."
echo "You should also edit this script to set the constants to match your evironment"
echo "Also this script assumes that you have bash environment varriables set IE keystonerc_admin to let you use the nova command directly"
echo "Usage: ./multiboot.sh <first_image_uuid> <second_image_uuid> <third_image_uuid>"
fi
function bootvm() {
local vmname=$2
local image=$1
nova boot --flavor $flavor --image $image --nic net-id=$network --availability_zone $availabilityzone:$host $vmname
}
iterator=1
for i in $*
do
bootvm $i $vmbase_name$iterator
(( iterator += 1 ))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment