Example #1
0
def main():

    pyrax.set_credential_file("./CredsFile")

    cs = pyrax.cloudservers
    servers = cs.servers.list()
    image_list = cs.images.list()

    srv_dict = {}


    print "Select a server from which an image will be created."
    for pos, srv in enumerate(servers):
        print "%s: %s" % (pos, srv.name)
        srv_dict[str(pos)] = srv.id
    selection = None
    while selection not in srv_dict:
        if selection is not None:
            print " -- Invalid choice"
        selection = raw_input("Enter the number for your choice: ")

    server_id = srv_dict[selection]
    print
    img_name = raw_input("Enter a name for the image: ")

    images = {}
    images[img_name] = cs.servers.create_image(server_id, img_name)
    
    completed_images = []
    while len(completed_images) < 1:    
        for img in cs.images.list():
            if str(img_name) in img.name and img.status == 'ACTIVE':
                completed_images.append(images[img_name])
        print "Waiting for image to be saved. Trying again in 30 seconds."
        time.sleep(30)

    print "\nImage is ready. Creating server."

    servers = {}
    servers[img_name] = cs.servers.create(img_name, images[img_name], 2)
    print "Building server: %s" % img_name
    
    print 
    
    completed_details = []
    while len(completed_details) < 1:
        for img_name in servers:
            servers[img_name].get()
            if len(servers[img_name].networks) > 0:
                completed_details.append(servers[img_name])        
        print "Waiting for details to be ready. Trying again in 30 seconds."
        time.sleep(30)
        
    for img_name in servers:
        servers[img_name].get()
        print "Name: %s" % servers[img_name].name
        print "ID: %s" % servers[img_name].id
        print "Status: %s" % servers[img_name].status
        print "Admin Password: %s" % servers[img_name].adminPass
        print "Networks: %s \n" % servers[img_name].networks
Example #2
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument ('-c', '--container', help='Name of container to be created.', required=True)
    parser.add_argument ('-f', '--fqdn', help='FQDN of record to add', required=True)
    parser.add_argument ('-i', '--ip', help='IP Address of FQDN record')
    parser.add_argument ('-e', '--email', help='IP Address of FQDN record', required=True)
    
    args = parser.parse_args()

    pyrax.set_credential_file(".PathtoCreds")
    cf = pyrax.cloudfiles
    dns = pyrax.cloud_dns

    cont = cf.create_container(args.container)
    print "Creating container named: %s" % (cont.name)
    cf.make_container_public(args.container, ttl=900)
    print "CDN Enabled."
    
    content = "Hello World"
    obj = cf.store_object(cont.name, "index.html", content, content_type="text/html")
    print "Stored Object: %s" % (obj)
    print "CDN URI: %s" % (cont.cdn_uri)
    
    dom = dns.create(name=args.fqdn, emailAddress=args.email)
            
    recs = [{
            "type": "CNAME",
            "name": "test." + str(args.fqdn),
            "data": cont.cdn_uri,
            "ttl": 6000,
            }]
                    
    print dom.add_records(recs)
Example #3
0
def main():
    creds_file = os.path.expanduser("~/.rackspace_cloud_credentials")
    pyrax.set_credential_file(creds_file)
    servers = create_chal10_servers()
    lb = create_chal10_lb(servers)
    lb_vip = lb.virtual_ips[0].address
    dns = create_dns(lb_vip)
Example #4
0
def main():

	pyrax.set_setting("identity_type", "rackspace")
	creds_file = os.path.expanduser('~/.rackspace_cloud_credentials')
	pyrax.set_credential_file(creds_file)
	available_regions = pyrax.regions

	print "Regions Available:", " ".join(list(available_regions))
	region = ""
	while region not in pyrax.regions:
		region = raw_input("Select region: ")
	cs = pyrax.connect_to_cloudservers(region)
	servers = cs.servers.list()
	srv_dict = {}

	print "Select a server from which an image will be created."
	for pos, srv in enumerate(servers):
	    print "%s: %s" % (pos, srv.name)
	    srv_dict[str(pos)] = srv.id
	selection = None
	while selection not in srv_dict:
	    if selection is not None:
	        print " -- Invalid choice"
	    selection = raw_input("Enter the number for your choice: ")

	server_id = srv_dict[selection]
	print
	nm = raw_input("Enter a name for the image: ")

	img_id = cs.servers.create_image(server_id, nm)

	print "Image '%s' is being created. Its ID is: %s" % (nm, img_id)
def main():

    # Parse the command line arguments
    parser = argparse.ArgumentParser(description="Add a dns A record",
                                     prog='cdns_add_dns_record.py')
    parser.add_argument('--fqdn', help='fully qualified domain name',
                        required=True)
    parser.add_argument('--ip', help='ip address', required=True)
    args = parser.parse_args()

    # Authenticate using a credentials file: "~/.rackspace_cloud_credentials"
    cred_file = "%s/.rackspace_cloud_credentials" % (os.environ['HOME'])
    print "Setting authentication file to %s" % (cred_file)
    pyrax.set_credential_file(cred_file)

    # Instantiate a clouddns object
    print "Instantiating cloud_dns object"
    cdnsobj = pyrax.cloud_dns

    # Add the domain record
    domains = cdnsobj.list()
    for domain in domains:
        if args.fqdn.endswith(domain.name):
            print "Found a matching domain: " + domain.name + \
                  " for fqdn: " + args.fqdn
            recs = [{'type': 'A', 'name': args.fqdn, 'data': args.ip,
                     'ttl': 6000}]
            print "Adding record: \n\t" + args.fqdn + "   IN  A  " + args.ip
            cdnsobj.add_records(domain, recs)
Example #6
0
    def __init__(self, quiet=False, credfile=None):
        self.quiet = quiet

        # Handle authentication
        if credfile is None:
            # No credentials passed
            try:
                if pyrax.identity.authenticated is False:
                    raise ValueError(
                        "ERROR: FLUFFY_BASE(): PyRAX is not authenticated and no authentication tokens passed."
                    )
            except AttributeError:
                raise ValueError("ERROR: FLUFFY_BASE(): PyRAX is not initialized and no authentication tokens passed.")

        else:
            # If we were passed tokens, just reauth.
            # TODO: Come up with some solution to see if reauth is required
            # (I don't think pyrax.identity gives us enough hooks right now.)

            if credfile is None:
                raise ValueError("ERROR: FLUFFY_BASE(): Either credfile is required")

            norm_conf_path = os.path.expanduser(os.path.normpath(credfile))
            pyrax.settings.read_config(norm_conf_path)  # Not sure if hack, emailed support
            pyrax.set_credential_file(norm_conf_path)
Example #7
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--region', metavar='region', default='ORD', choices=['ORD','DFW','LON'], help="Cloud region (default: ORD)")
    parser.add_argument('--flavor',metavar='flavor', default=2, choices=range(2,9), help="Flavor ID (default: 2)")
    parser.add_argument('uuid', metavar='uuid', help="UUID of server that you would like cloned")
    args = parser.parse_args()

    pyrax.set_credential_file(os.path.expanduser("~/.rackspace_cloud_credentials"),region=args.region)
    cs = pyrax.cloudservers

    print "Creating image of server %s..." %  args.uuid
    server = cs.servers.get(args.uuid)
    image_id = server.create_image("Image of " + server.name)
    image = cs.images.get(image_id)
    progress_monitor(image)
    print "Building new server from image. This may take a few minutes..."

    name = "Clone of %s" % server.name
    server = cs.servers.create(name, image_id, args.flavor)
    progress_monitor(server)

    print "\n====> Server Details\n"
    print "ID: %s" % server.id
    print "Name: %s" % name
    print "Admin Password: %s" % server.adminPass
    print "Networks:"
    for key, ips in server.networks.iteritems():
        for ip in ips:
            print "%s%s: %s" % (" "*4, key, ip)

    return
Example #8
0
def push_rackspace(fname, cred_file="rs.cred"):
    creds_file = os.path.expanduser(cred_file)
    pyrax.set_credential_file(cred_file)
    cf = pyrax.cloudfiles
    with open(fname, "rb") as f:
        fdata = f.read()
    obj = cf.store_object("cyclus", fname, fdata)
Example #9
0
def main():

	pyrax.set_setting("identity_type", "rackspace")
	creds_file = os.path.expanduser('~/.rackspace_cloud_credentials')
	pyrax.set_credential_file(creds_file)

	available_regions = pyrax.regions

	print "Regions Available:", " ".join(list(available_regions))
	region = ""
	while region not in available_regions:
		region = raw_input("Select Region: ")

	cs = pyrax.connect_to_cloudservers(region)
	available_images = cs.images.list()
	available_flavors = cs.flavors.list()

	image = make_choice(images, "Select an image: ")
	flavor = make_choice(flavors, "Select a flavor: ")

	base_name = raw_input('Base server name': )
	server_count = int(raw_input('Number of servers to build: '))

	print "Building {} servers with base name '{}', flavor '{}', and image " 
		"'{}'.".format(server_count, base_name, flavor.name, image.name)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('base', help='The base hostname to use, 3x512MB'
                        ' servers will be built using this base hostname')
    parser.add_argument('--dc', required=False, help='The region to '
                        'build the servers in', choices=['DFW', 'ORD', 'LON'],
                        default=pyrax.default_region)
    parser.add_argument('--image', required=False, help='The image ID to build'
                        ' the servers with',
                        default='5cebb13a-f783-4f8c-8058-c4182c724ccd')
    parser.add_argument('--count', required=False, help='Number of servers to '
                        'build. Default 2', default=2, type=int)

    args = parser.parse_args()
    credentials_file = os.path.expanduser('~/.rackspace_cloud_credentials')
    pyrax.set_credential_file(credentials_file, region=args.dc)
    cs = pyrax.cloudservers
    clb = pyrax.cloud_loadbalancers

    print 'Building servers in: %s' % args.dc

    servers = {}
    for i in xrange(0, args.count):
        host = '%s%d' % (args.base, i)
        print 'Creating server: %s' % host
        servers[host] = cs.servers.create(host, args.image, 2)
        print '%s: %s' % (host, servers[host].id)
    statuses = ['ACTIVE', 'ERROR', 'UNKNOWN']
    while filter(lambda server: server.status not in statuses,
                 servers.values()):
        print 'Sleeping 30 seconds before checking for server readiness...'
        time.sleep(30)
        for host in servers:
            if servers[host].status in statuses:
                continue
            servers[host].get()

    nodes = []
    for host, server in server_details.iteritems():
        nodes.append(clb.Node(address=server.networks['private'][0], port=80,
                              condition='ENABLED'))

    vip = clb.VirtualIP(type='PUBLIC')

    lb = clb.create('%s-lb' % args.base, port=80, protocol='HTTP', nodes=nodes,
                    virtual_ips=[vip])

    public_addresses = [vip.address for vip in lb.virtual_ips]

    t = prettytable.PrettyTable(['ID', 'Host', 'Status', 'IP',
                                 'Admin Password'])
    for host, server in servers.iteritems():
        t.add_row([server.id, host, server.status,
                   ', '.join(server.networks['public']), server.adminPass])
    print 'Servers and loadbalancer online and ready...'
    print t
    t = prettytable.PrettyTable(['ID', 'Name', 'IP Address'])
    t.add_row([lb.id, lb.name, ', '.join(public_addresses)])
    print
    print t
def check_usage(instance_id, threshold, region):
    pyrax.set_credential_file(
        os.path.expanduser("~/.rackspace_cloud_credentials"))
    cdb = pyrax.connect_to_cloud_databases(region=region)

    matched_instance = None
    for instance in cdb.list():
        if instance.id == instance_id:
            matched_instance = instance
    if not matched_instance:
        print 'status error Unable to find instance', instance_id
        sys.exit(1)

    # Force usage lookup
    matched_instance.get()
    database_size = matched_instance.volume['size']
    database_usage = matched_instance.volume['used']
    percentage_used = database_usage / database_size

    if percentage_used >= threshold:
        print 'status error usage over threshold'
    else:
        print 'status ok usage within threshold'

    print "metric database_GB_container_size float", database_size
    print "metric database_GB_used float", database_usage
    print "metric percentage_used float", percentage_used
def main():

	credz = os.path.expanduser("~/.rackspace_cloud_credentials")

	try:
		pyrax.set_credential_file(credz)
	except exceptions.AuthenticationFailed:
		print "\n\nAuth failed, possibly wrong info in .rackspace_cloud_credentials"
	if pyrax.identity.authenticated:
		print '-'*15, '\n', "Auth successful as %s" % pyrax.identity.username, '\n', '-'*15
	else:
		print "Authentication failed."
		sys.exit(1)

        cf = pyrax.cloudfiles
        dns = pyrax.cloud_dns
        indexfile = "index.html"
        index_data = "<html>\n<body>\n<h1>Welcome to the internet</h1>\n"

        cont = cf.create_container("index")
        cont.make_public(ttl=900)
        uppit = cont.store_object(indexfile, index_data)
        print "Uploaded %s" % uppit, '\n', '-'*15
        cont.set_web_index_page(indexfile)
        dom_n = "superc00lwebsite.com"
        dom_e = "*****@*****.**"
        domain = dns.create(name=dom_n, emailAddress=dom_e)
        name = "cdn." + dom_n
        recadd = {"type": "CNAME", "name": name, "data": cont.cdn_uri, "ttl":900}
        fin = domain.add_records(recadd)
        print fin
def main():

	credz = os.path.expanduser("~/.rackspace_cloud_credentials")

	try:
		pyrax.set_credential_file(credz)
	except exceptions.AuthenticationFailed:
		print "\n\nAuth failed, possibly wrong info in .rackspace_cloud_credentials"
	if pyrax.identity.authenticated:
		print '-'*15, '\n', "Auth successful as %s" % pyrax.identity.username, '\n', '-'*15
	else:
		print "Authentication failed."
		sys.exit(1)

        cf = pyrax.cloudfiles
        ctnr = cf.list_containers()
        c_ttl = 900
        print "Current Containers:\n"
        for c in ctnr:
            print c
        print '-'*15, '\n', "Please type a container name then press ENTER to CDN enable it\n", '-'*15, '\n'
        ctnrn = raw_input("Container:")

        container = cf.create_container(ctnrn)
        container.make_public(ttl=c_ttl)
        cont = cf.get_container(container)

        print '\n', '-'*15, '\n'
        print "cdn_enabled", cont.cdn_enabled
        print "cdn_ttl", cont.cdn_ttl
        print "cdn_uri", cont.cdn_uri
        print "cdn_ssl_uri", cont.cdn_ssl_uri
        print "cdn_streaming_uri", cont.cdn_streaming_uri
        print '\n', '-'*15
Example #14
0
def provision():
    # Set the credentials for pyrax
    pyrax.set_setting("identity_type", "rackspace")
    pyrax.set_credential_file(".pyrax.cfg")

    # Provision the server with Rackspace
    server = pyrax.cloudservers.servers.create(
        "warehouse-repository",
        IMAGE_ID,
        FLAVOR_ID,
    )

    # Store the password
    fabric.state.env.password = server.adminPass

    # Wait until the server is built
    server = pyrax.utils.wait_for_build(server)

    fabric.state.env.host_string = "root@{}".format(
        filter(lambda x: "." in x, server.networks["public"])[0],
    )

    # Bootstrap our environment
    _bootstrap_environment()

    # Write out our config
    with open(".rackspace.json", "w") as rs:
        json.dump(
            {
                "host_string": fabric.state.env.host_string,
                "password": fabric.state.env.password,
            },
            rs,
        )
Example #15
0
def main():
    pyrax.set_credential_file(os.path.expanduser('~/.rackspace_cloud_credentials'))
    cs = pyrax.cloudservers
    # Ubuntu 12.04 Image
    image_id = '5cebb13a-f783-4f8c-8058-c4182c724ccd'
    # 512M Server
    flavor_id = 2
    # Server name prefix
    prefix = 'web'
    password_dict = {}

    def print_server_details(server):
        print "\nid: %s" % server.id
        print "name: %s" % server.name
        print "adminPass: %s" % password_dict.get(server.id)
        print "networks: %s" % server.networks
        return

    def build_check_callback(server):
        if not server:
            print 'ERROR: One of your servers does not appear to be building properly'
        elif server.status == "ACTIVE":
            print_server_details(server)
        else:
            print "ERROR: Server %s (%s) is in ERROR" % (server.id, server.name)
            print_server_details(server)
        return

    for i in xrange(0,3):
        server = cs.servers.create('%s%s' % (prefix,str(i+ 1)), image_id, flavor_id)
        password_dict[server.id] = server.adminPass
        pyrax.utils.wait_until(server,'status',['ACTIVE','ERROR'],build_check_callback, interval=15, attempts=30)
    print "Your Ubuntu 12.04 512M 3-pack is now building! Server details provided upon build completion. This may take a minute or so..."

    return
Example #16
0
def main():

    # Log the startup
    jarlog.logit('INFO', "Starting jarflyd")

    # Get the config
    confobj = config.GetConfig()

    # Set up the identity type
    pyrax.set_setting("identity_type", "rackspace")

    # Set up the credentials file
    cred_file = confobj.get("global", "credentials_file")
    jarlog.logit('INFO', "Authenticating using cred file: %s" % cred_file)
    pyrax.set_credential_file(cred_file)

    # Set up the default region
    globalRegion = confobj.get("global", "region")
    jarlog.logit('INFO', "Setting global region to: %s" % globalRegion)
    pyrax.connect_to_services(region=globalRegion)

    # Start reading in the jar sections
    sections = confobj.sections()
    for section in sections:
        if section.startswith("jar-"):
            processJar(confobj, section)
def api_initialization():
    pyrax.set_setting("identity_type", "rackspace")
    try:
        progname, username, api_key = argv
        pyrax.set_credentials(username, api_key)
    except ValueError:
        if os.path.isfile(os.path.expanduser("~/.rackspace_cloud_credentials")):
            creds_file = os.path.expanduser("~/.rackspace_cloud_credentials")
            try:
                pyrax.set_credential_file(creds_file)
            except pexc.AuthenticationFailed:
                print "The credentials located in ~/.rackspace_cloud_credentials are not valid. Please provide the correct Username and API Key below.\n"
                cred_prompt()
        else:
            cred_prompt()
    except pexc.AuthenticationFailed:
        if os.path.isfile(os.path.expanduser("~/.rackspace_cloud_credentials")):
            print "The provided credentials are not valid; reverting to the ~/.rackspace_cloud_credentials file."
            creds_file = os.path.expanduser("~/.rackspace_cloud_credentials")
            try:
                pyrax.set_credential_file(creds_file)
            except pexc.AuthenticationFailed:
                print "The credentials located in ~/.rackspace_cloud_credentials are not valid. Please provide the correct Username and API Key below.\n"
                cred_prompt()
        else:
            print "The provided credentials are not valid; please enter them below.\n"
            cred_prompt()
def load(context, path, callback):
    key = (
            context.config.RACKSPACE_PYRAX_REGION, 
            context.config.get('RACKSPACE_PYRAX_IDENTITY_TYPE','rackspace'),
            context.config.RACKSPACE_PYRAX_CFG,
            context.config.RACKSPACE_PYRAX_PUBLIC,
            context.config.RACKSPACE_LOADER_CONTAINER
        )

    if key not in CONNECTIONS:
        if(context.config.RACKSPACE_PYRAX_REGION):
            pyrax.set_default_region(context.config.RACKSPACE_PYRAX_REGION)
        pyrax.set_setting('identity_type', context.config.get('RACKSPACE_PYRAX_IDENTITY_TYPE','rackspace'))
        pyrax.set_credential_file(expanduser(context.config.RACKSPACE_PYRAX_CFG))
        cf = pyrax.connect_to_cloudfiles(public=context.config.RACKSPACE_PYRAX_PUBLIC)
        CONNECTIONS[key] = cf.get_container(context.config.RACKSPACE_LOADER_CONTAINER)

    cont = CONNECTIONS[key]
    file_abspath = normalize_path(context, path)
    logger.debug("[LOADER] getting from %s/%s" % (context.config.RACKSPACE_LOADER_CONTAINER, file_abspath))
    try:
        obj = cont.get_object(file_abspath)
        if obj:
            logger.debug("[LOADER] Found object at %s/%s" % (context.config.RACKSPACE_LOADER_CONTAINER, file_abspath))
        else:
            logger.warning("[LOADER] Unable to find object %s/%s" % (context.config.RACKSPACE_LOADER_CONTAINER, file_abspath ))
    except:
        callback(None)
    else:
        callback(obj.get())
Example #19
0
    def __init__(self, parsed_url):
        duplicity.backend.Backend.__init__(self, parsed_url)

        try:
            import pyrax
        except ImportError as e:
            raise BackendException("""\
Hubic backend requires the pyrax library available from Rackspace.
Exception: %s""" % str(e))

        # Inform Pyrax that we're talking to Hubic
        pyrax.set_setting("identity_type", "duplicity.backends.pyrax_identity.hubic.HubicIdentity")

        CREDENTIALS_FILE = os.path.expanduser("~/.hubic_credentials")
        if os.path.exists(CREDENTIALS_FILE):
            try:
                pyrax.set_credential_file(CREDENTIALS_FILE)
            except Exception as e:
                log.FatalError("Connection failed, please check your credentials: %s %s"
                               % (e.__class__.__name__, util.uexc(e)),
                               log.ErrorCode.connection_failed)

        else:
            raise BackendException("No ~/.hubic_credentials file found.")

        container = parsed_url.path.lstrip('/')

        self.client_exc = pyrax.exceptions.ClientException
        self.nso_exc = pyrax.exceptions.NoSuchObject
        self.container = pyrax.cloudfiles.create_container(container)
Example #20
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('--region', default="ORD", choices=['ORD','DFW','LON'], help="Cloud region (default: ORD)")
    parser.add_argument('--flavor', type=int, default=1, choices=range(1,7), help="Flavor ID (default: 1)")
    parser.add_argument('--volume', default=1, help="Size of disk in GB (default:1, Max:150)")
    parser.add_argument('user', help="Database User")
    parser.add_argument('password', help="Database Password")
    parser.add_argument('database', metavar='database-name', help="Database Name")
    parser.add_argument('instance', metavar='instance-name', help="Instance name")
    args = parser.parse_args()

    pyrax.set_credential_file(os.path.expanduser("~/.rackspace_cloud_credentials"),region=args.region)
    cdb = pyrax.cloud_databases

    db_dict = [{"name": args.database}]
    user_dict = [
                    {
                        "name": args.user,
                        "password": args.password,
                        "databases": db_dict
                    }
                ]
    print "Building database instance. This may take a few minutes..."
    instance = cdb.create(args.instance, flavor=args.flavor, volume=args.volume, users=user_dict, databases=db_dict)
    instance = pyrax.utils.wait_until(instance,'status',['ACTIVE','ERROR'], interval=15)
    if instance.status == 'ACTIVE':
        print "Instance built successfully."
    else:
        raise SystemExit("ERROR: Instance build was unsuccessful")

    return
Example #21
0
def master_up(key_name, credential_file="~/.rackspace_cloud_credentials"):
    '''
    Create a salt-master on Rackspace

    Alternatively create the master using nova
    '''

    # Authenticate with Rackspace, use credential file
    pyrax.set_setting("identity_type", "rackspace")
    pyrax.set_credential_file(os.path.expanduser(credential_file))

    # Shorthand
    cs = pyrax.cloudservers

    # Building Ubuntu 12.04 boxes with 512 MB RAM
    iter_flavors = ifilter(lambda flavor: flavor.ram == 512, cs.flavors.list())
    flavor_512 = iter_flavors.next()
    iter_os = ifilter(lambda img: "Ubuntu 12.04" in img.name, cs.images.list())
    ubu_image = iter_os.next()

    master = cs.servers.create("master.ipython.org", ubu_image.id, flavor_512,
                               key_name=key_name)

    master = pyrax.utils.wait_for_build(master, verbose=True)

    env.hosts = [master.accessIPv4]

    print("Master IP: {}".format(master.accessIPv4))
    return master.accessIPv4
Example #22
0
def auth(credential_location="~/.rackspace_cloud_credentials"):
    """
    Loads the pyrax credentials from ~/.rackspace_cloud_credentials
    :param credential_location: The location containing the credential ini
    """
    credentials = os.path.expanduser(credential_location)
    pyrax.set_credential_file(credentials)
Example #23
0
def cloud_connect():
    creds_file = os.path.expanduser("~/.rackspace_cloud_credentials")
    try:
        pyrax.set_setting("identity_type", "rackspace")
        pyrax.set_credential_file(creds_file)
    except exc.AuthenticationFailed:
        print "Problem with credential file ~/.rackspace_cloud_credentials"
Example #24
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--region', metavar='region',  default='ORD', choices=['ORD','DFW','LON'], help="Cloud region (default: ORD)")
    parser.add_argument('--ttl', metavar='ttl', default=259200, type=int, help="CDN container TTL in seconds (default: 259200)")
    parser.add_argument('--enable-log', action='store_true', help="Enable CDN log retention")
    parser.add_argument('container', metavar='container', help="Cloud Files container name. Container will be created if it does not exist")
    
    args = parser.parse_args()

    pyrax.set_credential_file(os.path.expanduser("~/.rackspace_cloud_credentials"),region=args.region)
    cf = pyrax.cloudfiles 

    if args.container not in cf.list_containers():
        print "Container does not exist and will be created."

    # if container already exists, create_container will still return existing container.
    cont = cf.create_container(args.container)

    cf.make_container_public(args.container, ttl=args.ttl)
    cf.set_cdn_log_retention(cont, args.enable_log)
    cont = cf.get_container(args.container)

    print "Container CDN Enabled."
    print "name:", cont.name
    print "ttl:", cont.cdn_ttl
    print "uri:", cont.cdn_uri
    print "ssl uri:", cont.cdn_ssl_uri
    print "streaming uri:", cont.cdn_streaming_uri
    print "ios uri:", cont.cdn_ios_uri
    print "log retention:", cont.cdn_log_retention

    return
Example #25
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument ('-n', '--name', help='Name of database to be created', required=True)
    parser.add_argument ('-u', '--username', help='Username of database to be created', required=True)
    parser.add_argument ('-p', '--password', help='Password of database to be created', required=True)


    args = parser.parse_args()

    pyrax.set_credential_file(".PathToCreds")
    cdb = pyrax.cloud_databases
 
    # Creates the instance
    instance_name = "Default Instance"
    inst = cdb.create(instance_name, flavor="1GB Instance", volume=2)
    print "Instance: %s" % (inst)

    completed_instance = []
    while len(completed_instance) < 1:    
        for instance in cdb.list():
            if str(instance_name) in instance.name and instance.status == 'ACTIVE':
                completed_instance.append(instance_name)
        print "Waiting for instance to be ready. Trying again in 30 seconds."
        time.sleep(30)
    
    print "Instance is ready. Creating database."
    
    # Creates the database   
    db = inst.create_database(args.name)
    print "DB: %s" % (db)    

    # Creates the user    
    user = inst.create_user(name=args.username, password=args.password, database_names=[db])
    print "User: %s" % (user)
Example #26
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument("instancename")
    parser.add_argument("databasename")
    parser.add_argument("username")
    parser.add_argument("--flavor", type=int, default=1)
    parser.add_argument("--volumesize", default=1)
    parser.add_argument("--password")
    args = parser.parse_args()

    if args.password is None:
        args.password = pyrax.utils.random_name(length=10, ascii_only=True)

    creds_file = os.path.expanduser("~/.rackspace_cloud_credentials")
    pyrax.set_credential_file(creds_file)
    cdb = pyrax.cloud_databases

    db_dicts = [{"name": args.databasename}]
    body = [{"name": args.username, "password": args.password, "databases": db_dicts}]

    instance = cdb.create(args.instancename, flavor=args.flavor, volume=args.volumesize, users=body, databases=db_dicts)

    print "Name:", instance.name
    print "ID:", instance.id
    print "Username:"******"Password:"******"Status:", instance.status
    print "Flavor:", instance.flavor.name
Example #27
0
def setup():
    username = os.environ.get('OS_USERNAME')
    api_key = os.environ.get('OS_PASSWORD')
    credentials = os.environ.get('RAX_CREDENTIALS') or os.environ.get('RAX_CREDS_FILE')
    region = os.environ.get('OS_REGION_NAME')

    if credentials is None:
        credentails = os.path.expanduser('~/.rackspace_cloud_credentials')

    try:
        pyrax.set_setting('identity_type', 'rackspace')

        if api_key and username:
            pyrax.set_credentials(username, api_key=api_key)
        elif credentials:
            credentials = os.path.expanduser(credentials)
            pyrax.set_credential_file(credentials)
        else:
            sys.stderr.write('No value in environment variable %s and/or no '
                             'credentials file at %s\n'
                             % (e.message, default_creds_file))
            sys.exit(1)
    except Exception, e:
        sys.stderr.write("%s: %s\n" % (e, e.message))
        sys.exit(1)
Example #28
0
def main():
          
    parser = argparse.ArgumentParser()
    parser.add_argument('-f', '--flavor', default=2, help='Flavor for new cloud servers')
    parser.add_argument('-i', '--image', default='e4dbdba7-b2a4-4ee5-8e8f-4595b6d694ce', help='Flavor for new cloud servers')
    parser.add_argument('-b', '--basename', default='New', help='Base name for the servers. For example \'Web\' would produce servers of the names Web1, Web2, etc')
    parser.add_argument('-q', '--quantity', default=3, help='Quantity of servers you want to spin up. The default is 3.')

    args = parser.parse_args()
    
    pyrax.set_credential_file(os.path.join(os.path.expanduser("~"), "rackspace_cloud_credentials"))
    
    cs = pyrax.cloudservers
    
    n = 0
    new_servers = []
    while n <= args.quantity - 1 :
        print "Spinning up server " + args.basename + str(n + 1)
        new_servers.append(cs.servers.create(args.basename + str(n + 1), args.image, args.flavor))
        p = 0
        while p < 100:
            p = cs.servers.get(new_servers[n].id).progress
            time.sleep(10)
            print "Progress: " + str(p) + "%"
        n += 1
        
    print "All server builds complete:"
    print ""
    for srv in new_servers:
        print "Name: " + srv.name
        print "ID: ", srv.id
        print "Admin password: "******""
Example #29
0
def servers_api(creds_file, region = None):
    """Sets up a pyrax instance for Cloud Servers in the given region."""
    pyrax.set_setting("identity_type", "rackspace")
    pyrax.set_credential_file(os.path.expanduser(creds_file), region = region)
    cs = pyrax.cloudservers

    return cs
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('container', help='The destination CloudFiles '
                        'container to enable CDN support on')
    parser.add_argument('--dc', required=False, help='The region to '
                        'build the servers in', choices=['DFW', 'ORD', 'LON'],
                        default=pyrax.default_region)

    args = parser.parse_args()

    credentials_file = os.path.expanduser('~/.rackspace_cloud_credentials')
    pyrax.set_credential_file(credentials_file, region=args.dc)
    cf = pyrax.cloudfiles

    print 'Working in %s' % args.dc

    try:
        container = cf.get_container(args.container)
        print 'Container %s exists, using...' % args.container
    except pyrax.exceptions.NoSuchContainer as e:
        print 'Container %s does not exist, creating...' % args.container
        container = cf.create_container(args.container)

    container.make_public(ttl=900)

    print '%s CDN created and enabled:' % args.container
    print 'CDN URI: %s' % container.cdn_uri
    print 'CDN SSL URI: %s' % container.cdn_ssl_uri
    print 'CDN Streaming URI: %s' % container.cdn_streaming_uri
Example #31
0
def main():

    # Set the credentials file up
    cred_file = "%s/.rackspace_cloud_credentials" % (os.environ['HOME'])
    print "Setting authentication file to %s" % (cred_file)
    pyrax.set_credential_file(cred_file)

    # Check for existing
    csobj = pyrax.cloudservers

    # Get the vyatta image
    images = csobj.list_images()
    for image in images:
        if image.name == "Vyatta Network OS 6.5R2":
            vimage = image
            print "Found image: " + vimage.name

    # Get the needed flavor
    flavors = csobj.list_flavors()
    for flavor in flavors:
        if flavor.name == "1GB Standard Instance":
            vflavor = flavor
            print "Found flavor: " + vflavor.name

    # Create your keypair if it doens't already exist
    addkey = 1
    keypairs = csobj.keypairs.list()
    for keypair in keypairs:
        if keypair.name == "my_key":
            print "keypair " + keypair.name + " exists. Skipping creation"
            addkey = 0
    if addkey == 1:
        with open(os.path.expanduser("~/.ssh/id_rsa.pub")) as keyfile:
            csobj.keypairs.create("my_key", keyfile.read())

    # Create your networks
    cnobj = pyrax.cloud_networks
    dmznet = create_network(cnobj, "dmz", "192.168.33.0/24")
    appnet = create_network(cnobj, "app", "192.168.34.0/24")
    datanet = create_network(cnobj, "data", "192.168.35.0/24")
    nics_list = [{'net-id': '00000000-0000-0000-0000-000000000000'},
                 {'net-id': '11111111-1111-1111-1111-111111111111'},
                 {'net-id': dmznet.id},
                 {'net-id': appnet.id},
                 {'net-id': datanet.id}]


    # Create your server
    addserver = 1
    servers = csobj.servers.list()
    for server in servers:
        if server.name == "vyatta.linuxrackers.com":
            print "Server " + server.name + " exists. Skipping creation"
            serverafter = server
            addserver = 0

    if addserver == 1:
        server = csobj.servers.create("vyatta.linuxrackers.com",
                                      vimage.id, vflavor.id, nics=nics_list,
                                      key_name="my_key")
        serverafter = pyrax.utils.wait_until(server, "status",
                                            ["ACTIVE", "ERROR"])



    # print the network & pass info
    print "Server Networks: "
    print serverafter.networks
    print "Server Password "
    print serverafter.adminPass
Example #32
0
def main():
    """Run the main application."""

    # Setup argument parsing
    parser = argparse.ArgumentParser(description='Basic pyrax CLI utilities',
                                     epilog='Licensed "Apache 2.0"')

    parser.add_argument('-c',
                        '--credentialsfile',
                        help='<Required> The path to the pyrax.cfg file',
                        default='~/.pyrax.cfg')

    parser.add_argument(
        '-r',
        '--cloudregion',
        help='<Required> The region to execute commands against',
        required=True)

    parser.add_argument(
        '-s',
        '--serveruuid',
        help='<Required> The server uuid to execute commands against',
        required=True)

    parser.add_argument('-i',
                        '--imagename',
                        help='<Required> The name to use for the saved image',
                        required=True)

    # Parse arguments
    args = parser.parse_args()

    # Set the identity type for Rackspace Cloud
    pyrax.set_setting('identity_type', 'rackspace')

    # Set the credentials
    pyrax.set_credential_file(args.credentialsfile)

    # Create the cloudservers object
    cs = pyrax.connect_to_cloudservers(region=args.cloudregion)

    # Get the list of current images with the same name
    current_images = [
        image for image in cs.images.list()
        if hasattr(image, "server") and image.name == args.imagename
    ]

    # Request the image creation (it returns the id, not an object)
    saved_image_id = cs.servers.create_image(args.serveruuid, args.imagename)

    # Get the image object
    saved_image = cs.images.get(saved_image_id)

    # Wait until the image creation returns a result
    saved_image = pyrax.utils.wait_until(saved_image,
                                         "status", ["ACTIVE", "ERROR"],
                                         attempts=0)

    # Delete all previous images of the same name if the resulting image
    # status is 'ACTIVE'
    if saved_image.status == "ACTIVE":
        for image in current_images:
            image.delete()
Example #33
0
import redis

REDIS_HOST = os.environ.get('REDIS_HOST', 'localhost')
REDIS_DB = int(os.environ.get('REDIS_DB', 5))

parser = argparse.ArgumentParser()
parser.add_argument('which', nargs='?', default=None)
args = parser.parse_args()

if args.which is None:
    print "Finding latest backup..."

    import pyrax
    pyrax.set_setting('identity_type', 'rackspace')
    pyrax.set_credential_file(os.path.expanduser('~/.raxcreds'), region='SYD')
    container = pyrax.cloudfiles.get_container('backups')

    backups = container.get_objects()
    backups.sort(key=lambda x: x.name)
    print "Downloading {}...".format(backups[-1].name)
    container.download_object(backups[-1].name, '.')
    backup = backups[-1].name
else:
    backup = args.which

r = redis.StrictRedis(host=REDIS_HOST, db=REDIS_DB)

for entry in json.loads(gzip.GzipFile(backup, 'r').read())['backup']:
    key = entry.pop('id')
    p = r.pipeline()
Example #34
0
    try:
        creds_file = os.environ['RAX_CREDS_FILE']
    except KeyError, e:
        # But if that fails, use the default location of ~/.rackspace_cloud_credentials
        if os.path.isfile(default_creds_file):
            creds_file = default_creds_file
        else:
            sys.stderr.write('No value in environment variable %s and/or no '
                             'credentials file at %s\n' %
                             (e.message, default_creds_file))
            sys.exit(1)

    pyrax.set_setting('identity_type', 'rackspace')

    try:
        pyrax.set_credential_file(os.path.expanduser(creds_file))
    except Exception, e:
        sys.stderr.write("%s: %s\n" % (e, e.message))
        sys.exit(1)

    regions = []
    for region in os.getenv('RAX_REGION', 'all').split(','):
        region = region.strip().upper()
        if region == 'ALL':
            regions = pyrax.regions
            break
        elif region not in pyrax.regions:
            sys.stderr.write('Unsupported region %s' % region)
            sys.exit(1)
        elif region not in regions:
            regions.append(region)
Example #35
0
# basedir contains the following:
#    DNS zones in basedir/var/named
#        With the assumption that zone files are named domain.tld.db
#    Destination for processed DNS zones in basedir/var/processed
#    Rackspace Cloud credentials in basedir/cloudcred
#    A log file is created at basedir/import-(current UTC timestamp).log
# IMPORTANT: Trailing slash is necessary
basedir = "/home/exampleuser/zonefiles/"

# domainemail is the contact email address with '.' for '@' and a trailing '.', used to generate the SOA records.
domainemail = 'it.example.com.'

# Set the Cloud identity credentials.
pyrax.set_setting("identity_type", "rackspace")
pyrax.set_credential_file(basedir + "cloudcred")
cdns = pyrax.cloud_dns

# Generate a string from the current UTC timestamp.
utcstamp = str(int(time.time()))

# Create the log file and set the logging configuration.
logging.basicConfig(filename=basedir + 'import-' + utcstamp + '.log',
                    level=logging.INFO)

# Build the path to the bind files. Trailing slash required.
bindpath = basedir + "var/named/"

# Build the path to move the processed zone files to.
procpath = basedir + "var/processed/"
Example #36
0
import dns.exception
from tld import get_tld

# Configure some basic logging
logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)

# Determine the current directory
current_directory = os.path.abspath(os.path.dirname(__file__))

# Ensure that the environment variable PYRAX_CREDS is set and it contains the
# path to your pyrax credentials file.
pyrax.set_setting("identity_type", "rackspace")
try:
    pyrax.set_credential_file("{0}/.pyrax".format(current_directory))
    rax_dns = pyrax.cloud_dns
    rax_clb = pyrax.cloud_loadbalancers
except KeyError:
    logger.error(" + Missing pyrax credentials file (export PYRAX_CREDS)")
    sys.exit(1)

# Determine the IP addresses for Rackspace's public nameservers. We will query
# these servers later to determine when our challenge record is published.
dns_servers = ["ns1.rackspace.com", "ns2.rackspace.com"]
resolver = dns.resolver.Resolver()
rackspace_dns_servers = [
    item.address for server in dns_servers for item in resolver.query(server)
]

Example #37
0
def setup():
    default_creds_file = os.path.expanduser('~/.rackspace_cloud_credentials')

    # pyrax does not honor the environment variable CLOUD_VERIFY_SSL=False, so let's help pyrax
    if 'CLOUD_VERIFY_SSL' in os.environ:
        pyrax.set_setting(
            'verify_ssl', os.environ['CLOUD_VERIFY_SSL']
            in [1, 'true', 'True'])

    env = get_config(p, 'rax', 'environment', 'RAX_ENV', None)
    if env:
        pyrax.set_environment(env)

    keyring_username = pyrax.get_setting('keyring_username')

    # Attempt to grab credentials from environment first
    creds_file = get_config(p, 'rax', 'creds_file', 'RAX_CREDS_FILE', None)
    if creds_file is not None:
        creds_file = os.path.expanduser(creds_file)
    else:
        # But if that fails, use the default location of
        # ~/.rackspace_cloud_credentials
        if os.path.isfile(default_creds_file):
            creds_file = default_creds_file
        elif not keyring_username:
            sys.stderr.write('No value in environment variable %s and/or no '
                             'credentials file at %s\n' %
                             ('RAX_CREDS_FILE', default_creds_file))
            sys.exit(1)

    identity_type = pyrax.get_setting('identity_type')
    pyrax.set_setting('identity_type', identity_type or 'rackspace')

    region = pyrax.get_setting('region')

    try:
        if keyring_username:
            pyrax.keyring_auth(keyring_username, region=region)
        else:
            pyrax.set_credential_file(creds_file, region=region)
    except Exception as e:
        sys.stderr.write("%s: %s\n" % (e, e.message))
        sys.exit(1)

    regions = []
    if region:
        regions.append(region)
    else:
        region_list = get_config(p,
                                 'rax',
                                 'regions',
                                 'RAX_REGION',
                                 'all',
                                 islist=True)
        for region in region_list:
            region = region.strip().upper()
            if region == 'ALL':
                regions = pyrax.regions
                break
            elif region not in pyrax.regions:
                sys.stderr.write('Unsupported region %s' % region)
                sys.exit(1)
            elif region not in regions:
                regions.append(region)

    return regions
Example #38
0
def main():
    """
    Challenge 8
    -- Write a script that will create a static webpage served out of
       Cloud Files. The script must create a new container, CDN enable it,
       enable it to serve an index file, create an index file object,
       upload the object to the container, and create a CNAME record
       pointing to the CDN URL of the container.
    """
    # Parse script parameters
    p = argparse.ArgumentParser(description=("Create a Cloud Files static "
                                             "web site from a local "
                                             "folder/directory"))
    p.add_argument("directory", action="store", type=str,
                   metavar="[source directory]",
                   help="Local directory to upload content from")
    p.add_argument("container", action="store", type=str,
                   metavar="[container name]",
                   help="Container name where content should be uploaded")
    p.add_argument("fqdn", action="store", type=str, metavar="[cname]",
                   help="CNAME for CDN alias record")
    p.add_argument("-r", "--region", action="store", required=False,
                   metavar="[region]", type=str,
                   help=("Region where container should be created"
                         " (defaults to 'ORD'"),
                   choices=["ORD", "DFW", "LON", "IAD", "HKG", "SYD"],
                   default="ORD")
    p.add_argument("-t", "--cdn-ttl", action="store", required=False,
                   metavar="[cdn ttl]", type=int,
                   help=("CDN TTL for the container (default %d seconds)" %
                             (MIN_TTL)),
                   default=MIN_TTL)
    p.add_argument("-i", "--index", action="store", required=False,
                   metavar="[static web index]", type=str,
                   help="Static web index file (default 'index.html')",
                   default="index.html")
    p.add_argument("-l", "--cname-ttl", action="store", required=False,
                   type=int, metavar="[cname TTL]",
                   help=(("CNAME record TTL (in seconds) for the CDN "
                          "URI (default %d seconds)") % (DEFAULT_TTL)),
                   default=DEFAULT_TTL)
    p.add_argument("-f", "--force", action="store_true",
                   required=False,
                   help="Permit upload to an existing container")

    # Parse arguments (validate user input)
    args = p.parse_args()

    # Determine if the upload directory exists
    if not os.path.isdir(args.directory):
        print ("ERROR: Specified directory (%s) does not exist, please check "
               "the path and try again)" % (args.directory))
        sys.exit(1)

    # Determine if CDN TTL is at least the minimum value permitted
    if args.cdn_ttl < MIN_TTL:
        print "ERROR: Minimum CDN TTL permitted is %ds" % (MIN_TTL)
        sys.exit(2)

    # Same again for the CNAME record
    if args.cname_ttl < DEFAULT_TTL:
        print "ERROR: Minimum CNAME TTL permitted is %ds" % (DEFAULT_TTL)
        sys.exit(3)

    # Determine if the FQDN is correctly formated (at least three segments
    # separated by '.' are required).
    #    NOTE: This can be improved since we're not checking whether or not
    #          the zone in question is a valid TLD or if the string only has
    #          valid (alphanumeric) characters
    segments = args.fqdn.split('.')
    if len(segments) < 3:
        print ("ERROR: FQDN string is incorrectly formatted, please check "
               "and try again")
        print ("Base zone/domain in the format 'example.com' will not be "
               "accepted")
        sys.exit(4)
    # All is apparently well, define the zone/domain using the FQDN string
    else:
        zone_name = '.'.join(segments[-(len(segments)-1):])

    # Define the authentication credentials file location and request that
    # pyrax makes use of it. If not found, let the client/user know about it.

    # Use a credential file in the following format:
    # [rackspace_cloud]
    # username = myusername
    # api_key = 01234567890abcdef
    # region = LON

    try:
        creds_file = os.path.expanduser(CONFIG_FILE)
        pyrax.set_setting("identity_type", IDENTITY_TYPE)
        pyrax.set_credential_file(creds_file, args.region)
    except e.AuthenticationFailed:
        print ("ERROR: Authentication failed. Please check and confirm "
               "that the API username, key, and region are in place "
               "and correct.")
        sys.exit(5)
    except e.FileNotFound:
        print "ERROR: Credentials file '%s' not found" % (creds_file)
        sys.exit(6)

    # Use a shorter Cloud Files and Cloud DNS class reference strings
    # This simplifies invocation later on (less typing)
    cf = pyrax.cloudfiles
    dns = pyrax.cloud_dns

    # Grab zone list
    domains = zone_list(dns)

    # No zones found, inform that one needs to be created and exit
    if len(domains) == 0:
        print "ERROR: You have no domains/zones at this time"
        print "Please create one first then try again"
        sys.exit(7)

    # Attempt to locate the zone extracted from FQDN string
    try:
        zone = [i for i in domains if zone_name in i.name][0]
    except:
        print "ERROR: Zone '%s' not found" % (zone_name)
        print "Please check/create and try again"
        sys.exit(8)

    # Determine if container already exists, otherwise create it
    try:
        print "INFO: Checking if container already exists..."
        cont = cf.get_container(args.container)
    except e.NoSuchContainer:
        cont = None

    # Container not found, create it
    if cont is None:
        try:
            print ("INFO: Container '%s' not found, creating with TTL set "
                   "to %d..." % (args.container, args.cdn_ttl))
            cont = cf.create_container(args.container)
            cont.make_public(ttl=args.cdn_ttl)
        except e.CDNFailed:
            print "ERROR: Could not CDN enable the container"
            sys.exit(9)
        except:
            print "ERROR: Could not create container", args.container
            sys.exit(10)

    # Otherwise inform the user/client that the directory exists and
    # determine if we can proceed (is the force flag set)
    else:
        print ("INFO: Container '%s' found with TTL set to %d"
               % (cont.name, cont.cdn_ttl))
        if args.force:
            print "INFO: Proceeding as force flag is set"
        else:
            print "INFO: Force flag not set, exiting..."
            sys.exit(11)

    # Set the static web index file (metadata/header)
    print "INFO: Setting static web index file to", args.index
    meta = {"X-Container-Meta-Web-Index": args.index}
    cf.set_container_metadata(cont, meta)

    # Check and confirm if the index file exists, otherwise create a
    # placeholder and inform/warn the client of the action
    index_file = args.directory + "/" + args.index
    if not os.path.isfile(index_file):
        print ("INFO: Index file '%s' not found, creating a placeholder"
               % (index_file))
        f = open(index_file,'w')
        f.write("Index page placeholder\n")
        f.close()

    # Start the upload
    print "INFO: Beginning directory/folder upload"
    (upload_key, total_bytes) = cf.upload_folder(args.directory, cont)

    # Inform the user of the total upload size
    print ("INFO: Total upload size: %d bytes (%.2f MB)"
           % (total_bytes, total_bytes / 1024.0 / 1024))

    # Prepare progress toolbar
    sys.stdout.write("[%s]" % (" " * TOOLBAR_WIDTH))
    sys.stdout.flush()
    # Return to start of line, after '['
    sys.stdout.write("\b" * (TOOLBAR_WIDTH + 1))

    # Print the upload progress (1 second interval)
    uploaded = 0
    while uploaded < total_bytes:
        uploaded = cf.get_uploaded(upload_key)
        progress = int(ceil((uploaded * 100.0) / total_bytes / 2))
        sys.stdout.write("%s" % ("=" * progress))
        sys.stdout.flush()
        sys.stdout.write("\b" * (progress)) # Reset progress bar
        sleep(1)
    print

    # Upload completed, print object count and CDN URIs
    objs = cf.get_container_object_names(cont)
    print "INFO: Number of objects uploaded: %d" % (len(objs))

    # Attempt to create the new CNAME record
    cname_rec = {"type": "CNAME",
            "name": args.fqdn,
            "data": cont.cdn_uri.replace("http://", ""),
            "ttl": args.cname_ttl}

    try:
        rec = zone.add_record(cname_rec)
        print "INFO: DNS record successfully added"
        print ("-- Record details\n\tName: %s\n\tType: %s\n\tIP address: "
               "%s\n\tTTL: %s") % (rec[0].name, rec[0].type, rec[0].data,
               rec[0].ttl)
        print "INFO: All requests completed successfully"
    except e.DomainRecordAdditionFailed as err:
        print "ERROR: Record addition request failed\nReason:", err
        sys.exit(12)
Example #39
0
def main():
    """
    Build a number of Next-Gen Cloud Servers following a similar naming
    convention. (ie. web1, web2, web3) and returns the IP and login
    credentials for each server
    """
    # Variable to determine if build errors were encountered
    ERRORS = False

    # Compile a list of available RAM sizes for use in argument parsing
    # later on. The choices permitted will be made up of this list.
    #    NOTE: Should revisit to make more dynamic for if and when
    #          the list is updated
    FLAVOR_LIST = [512, 1024, 2048, 4096, 8192, 15360, 30720]

    # Define the script parameters (all are optional for the time being)
    parser = argparse.ArgumentParser(description=("Cloud Server provisioning "
                                                  "application"))
    parser.add_argument("-p",
                        "--prefix",
                        action="store",
                        required=False,
                        metavar="SERVER_NAME_PREFIX",
                        type=str,
                        help=("Server name prefix (defaults to 'node-' +"
                              " a random 8 charachter string"
                              "e.g. node-54jg84d9, node-57fhd49h, ...)"),
                        default="node-")
    parser.add_argument("-r",
                        "--region",
                        action="store",
                        required=False,
                        metavar="REGION",
                        type=str,
                        help=("Region where servers should be built (defaults"
                              " to 'LON'"),
                        choices=["ORD", "DFW", "LON", "SYD", "IAD"],
                        default="LON")
    parser.add_argument("-i",
                        "--image",
                        action="store",
                        required=True,
                        metavar="SERVER_IMAGE_ID",
                        type=str,
                        help=("Image ID to be used in server build"
                              " There is no default, ID must be supplied"))
    parser.add_argument("-s",
                        "--size",
                        action="store",
                        required=False,
                        metavar="SERVER_RAM_SIZE",
                        type=int,
                        help=("Server RAM size in megabytes (defaults to "
                              "'512')"),
                        choices=FLAVOR_LIST,
                        default=512)
    parser.add_argument("-m",
                        "--meta",
                        action="store",
                        required=False,
                        metavar="METADATA_DICTIONARY",
                        type=json.loads,
                        help=("Metadata to be used in the build request(s) - "
                              '(must be in the format: {"key": "value"'
                              ', "key": "value", ...}) Maximum of 5 '
                              "key/value pairs, default: %s" % (METADATA)),
                        default=METADATA)
    parser.add_argument("-c",
                        "--count",
                        action="store",
                        required=False,
                        metavar="SERVER_COUNT",
                        type=int,
                        help=("Number of servers to build (defaults to "
                              "'%d')" % (SERVER_COUNT)),
                        choices=range(1, 51),
                        default=SERVER_COUNT)
    parser.add_argument("-l",
                        "--logpath",
                        action="store",
                        required=False,
                        metavar="LOG_DIRECTORY",
                        type=str,
                        help=("The directory to create log files in"),
                        default=LOGPATH)
    parser.add_argument("-v",
                        "--verbose",
                        action="store_true",
                        required=False,
                        help=("Turn on debug verbosity"),
                        default=False)

    # Parse arguments (validate user input)
    args = parser.parse_args()

    # Configure log formatting
    logFormatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    rootLogger = logging.getLogger()
    # Check what level we should log with
    if args.verbose:
        rootLogger.setLevel(logging.DEBUG)
    else:
        rootLogger.setLevel(logging.WARNING)
    # Configure logging to console
    consoleHandler = logging.StreamHandler()
    consoleHandler.setFormatter(logFormatter)
    rootLogger.addHandler(consoleHandler)
    # Configure logging to file
    try:
        fileHandler = logging.FileHandler("{0}/{1}.log".format(
            args.logpath, os.path.basename(__file__)))
        fileHandler.setFormatter(logFormatter)
        rootLogger.addHandler(fileHandler)
    except IOError:
        rootLogger.critical("Unable to write to log file directory '%s'" %
                            (args.logpath))
        exit(1)

    # Define the authentication credentials file location and request that
    # pyrax makes use of it. If not found, let the client/user know about it.

    # Use a credential file in the following format:
    # [rackspace_cloud]
    # username = myusername
    # api_key = 01234567890abcdef
    # region = LON

    try:
        creds_file = os.path.expanduser(CONFIG_FILE)
        pyrax.set_credential_file(creds_file, args.region)
    except e.AuthenticationFailed:
        rootLogger.critical(
            "Authentication failed. Please check and confirm"
            "that the API username, key, and region are in place "
            "and correct.")
        exit(2)
    except e.FileNotFound:
        rootLogger.critical("Credentials file '%s' not found" % (creds_file))
        exit(3)

    # Use a shorter Cloud Servers class reference string
    # This simplifies invocation later on (less typing)
    cs = pyrax.cloudservers

    # Locate the image to build from (confirm it exists)
    try:
        image = [i for i in cs.images.list() if args.image in i.id][0]
    except:
        rootLogger.critical("Image ID provided was not found. Please check "
                            "and try again")
        exit(4)

    # Grab the flavor ID from the RAM amount selected by the user.
    # The server create request requires the ID rather than RAM amount.
    flavor = [f for f in cs.flavors.list() if args.size == f.ram][0]

    rootLogger.warning(
        "Cloud Server build request initiated. Consider using --verbose. Please wait ..."
    )

    # Print the image ID and name selected, as well as server count
    rootLogger.info("Image details, ID: '%s' Name: '%s'" %
                    (image.id, image.name))
    rootLogger.info("Server build details, Size: '%d' MB Count: '%d'" %
                    (args.size, args.count))

    # Server list definition to be used in tracking build status/comletion
    servers = []

    # Iterate through the server count specified, sending the build request
    # for each one in turn (concurrent builds)
    count = 1
    while count <= args.count:
        # Issue the server creation request
        srv = cs.servers.create(args.prefix +
                                pyrax.utils.random_name(8, ascii_only=True),
                                image.id,
                                flavor.id,
                                meta=args.meta)
        # Add server ID from the create request to the tracking list
        servers.append(srv)
        count += 1

    # Check on the status of the server builds. Completed or error/unknown
    # states are removed from the list until nothing remains.
    while servers:
        # Track the element position for easier/efficient removal
        count = 0
        for server in servers:
            # Get the updated server details
            server.get()
            # Should it meet the necessary criteria, provide extended info
            # and remove from the list
            if server.status in ["ACTIVE", "ERROR", "UNKNOWN"]:
                rootLogger.info(
                    "Server details: Name: '%s' Status: '%s' Admin password: '******'"
                    % (server.name, server.status, server.adminPass))
                rootLogger.info(
                    "Networks: Public #1: '%s' Public #2: '%s' Private: %s" %
                    (server.networks["public"][0],
                     server.networks["public"][1],
                     server.networks["private"][0]))
                if server.status not in ["ACTIVE"]:
                    ERRORS = True
                    rootLogger.warning(
                        "Something went wrong with the build request")
                del servers[count]
            count += 1
        # Reasonable wait period between checks
        sleep(15)

    # All done
    exit_msg = "Build requests completed"
    if ERRORS:
        rootLogger.warning("'%s' - with errors (see above for details)" %
                           (exit_msg))
        exit(5)
    else:
        rootLogger.warning("%s" % (exit_msg))
        exit(0)
Example #40
0
def setup():
    pyrax.set_setting("identity_type", "rackspace")
    pyrax.set_setting('region', 'ORD')
    pyrax.set_credential_file('rs.cred')
Example #41
0
#!/usr/bin/env python
"""challenge4.py: Response to Challenge 4 of Rackspace API Challenge"""
"""redirect stderr to eliminate status messages"""

__author__ = "*****@*****.**"

import pyrax
import sys
import json
import argparse
import os

pyrax.set_credential_file("/home/cdw/.rackspace_cloud_credentials")
dns = pyrax.cloud_dns
sys.stdout.flush()
sys.stderr.flush()

parser = argparse.ArgumentParser(
    description='Challenge 4: Create a DNS A record')
parser.add_argument('fqdn',
                    help='Fully qualified domain name of A record to create')
parser.add_argument('ip', help='IP address to point the A record at')
args = parser.parse_args()

# we need to figure out where our fqdn goes
fqdn_list = args.fqdn.split('.')
fqdn_tld = []
domain_id = []

# we loop through the domains and split them into reverse order lists
# then we compare the current tld to the one specified in the arguments
Example #42
0
def main():

    # Set default metadata key to search for
    METAKEY = "MyGroup0_clb"
    # Set default metadata value to search for
    METAVALUE = "clb0"
    # Set default location of pyrax configuration file
    CREDFILE = "~/.rackspace_cloud_credentials"
    # Set the default location of log files
    LOGPATH = "/var/log/"

    # A short description
    parser = argparse.ArgumentParser(
        description=("Automatically update load balancer nodes"))
    # Set argument for the region
    parser.add_argument("-r",
                        "--region",
                        action="store",
                        required=False,
                        metavar="REGION",
                        type=str,
                        help=("Region where servers should be built (defaults"
                              " to 'LON'"),
                        choices=["ORD", "DFW", "LON"
                                 "IAD", "SYD"],
                        default="LON")
    # Set argument for the metadata key
    parser.add_argument("-mk",
                        "--metakey",
                        action="store",
                        required=False,
                        metavar="META_KEY",
                        type=str,
                        help=("Matadata key that desired node has"),
                        default=METAKEY)
    # Set argument for the metadata value
    parser.add_argument("-mv",
                        "--metavalue",
                        action="store",
                        required=False,
                        metavar="META_VALUE",
                        type=str,
                        help=("Metadata value that desired node has"),
                        default=METAVALUE)
    # Set argument for the cloud load balancer ID
    parser.add_argument("-i",
                        "--clbid",
                        action="store",
                        required=True,
                        metavar="CLB_ID",
                        type=int,
                        help=("Cloud Load Balancer ID"))
    # Set argument for the pyrax credentials file loacation
    parser.add_argument("-c",
                        "--credfile",
                        action="store",
                        required=False,
                        metavar="CREDENTIALS_FILE",
                        type=str,
                        help=("The location of your pyrax configuration file"),
                        default=CREDFILE)
    # Set argument for the log file directory
    parser.add_argument("-p",
                        "--logpath",
                        action="store",
                        required=False,
                        metavar="LOG_DIRECTORY",
                        type=str,
                        help=("The directory to create log files in"),
                        default=LOGPATH)
    # Set argument for verbosity
    parser.add_argument("-v",
                        "--verbose",
                        action="store_true",
                        required=False,
                        help=("Turn on debug verbosity"),
                        default=False)

    # Parse the arguments
    args = parser.parse_args()

    # Configure logging
    logFormatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    rootLogger = logging.getLogger()
    # Check what level we should log with
    if args.verbose:
        rootLogger.setLevel(logging.DEBUG)
    else:
        rootLogger.setLevel(logging.WARNING)
    # Configure logging to console
    consoleHandler = logging.StreamHandler()
    consoleHandler.setFormatter(logFormatter)
    rootLogger.addHandler(consoleHandler)
    # Configure logging to file
    try:
        fileHandler = logging.FileHandler("{0}/{1}.log".format(
            args.logpath, os.path.basename(__file__)))
        fileHandler.setFormatter(logFormatter)
        rootLogger.addHandler(fileHandler)
    except IOError:
        rootLogger.critical("Unable to write to log file directory '%s'" %
                            (logpath))
        exit(1)

    # Define the authentication credentials file location and request that
    # pyrax makes use of it. If not found, let the client/user know about it.

    # Use a credential file in the following format:
    # [rackspace_cloud]
    # username = myusername
    # api_key = 01234567890abcdef
    # region = LON

    pyrax.set_setting("identity_type", "rackspace")

    # Test that the pyrax configuration file provided exists
    try:
        creds_file = os.path.expanduser(args.credfile)
        pyrax.set_credential_file(creds_file, args.region)
    # Exit if authentication fails
    except e.AuthenticationFailed:
        rootLogger.critical(
            "Authentication failed. Please check and confirm"
            "that the API username, key, and region are in place"
            "and correct.")
        exit(1)
    # Exit if file does not exist
    except e.FileNotFound:
        rootLogger.critical("Credentials file '%s' not found" % (creds_file))
        rootLogger.info(
            "%s", """Use a credential file in the following format:\n
                                 [rackspace_cloud]
                                 username = myuseername
                                 api_key = 01sdf444g3ffgskskeoek0349
                                 """)
        exit(2)

    # Shorten some pyrax calls
    cs = pyrax.cloudservers
    clb = pyrax.cloud_loadbalancers

    # Check that we have some servers in this region
    if not cs.servers.list():
        rootLogger.critical("No servers found in region '%s'" % (args.region))
        exit(3)
    # Check that we have the load balancer in this region
    if not clb.find(id=args.clbid):
        rootLogger.critical("No matching load balancer found in region '%s'" %
                            (args.region))
        exit(4)

    # Find our load balancer
    myclb = clb.find(id=args.clbid)
    # Let the user know we found the load balancer
    rootLogger.info('Found Load Balancer, id: %i status: %s' %
                    (myclb.id, myclb.status))

    # Create some empty lists
    clbips = []
    csips = []

    # Make a list of nodes currently in out load balancer.
    for node in myclb.nodes:
        if node.condition == 'ENABLED' or 'DISABLED':
            clbips.append(node.address)

    # Let the user know what nodes we found
    rootLogger.info('Current load balancer nodes: %s' % (clbips))

    # Make a list of servers that match out metadata
    for server in cs.servers.list():
        # filter out only ACTIVE ones
        if server.status == 'ACTIVE':
            # Check for servers that match both your meta key and value
            if args.metakey in server.metadata and server.metadata[
                    args.metakey] == args.metavalue:
                # Grab the private ip address of matching server
                csips.append(server.networks['private'][0])

    # Let the user know what servers match our metadata
    rootLogger.info('Cloud servers matching metadata: %s' % (csips))

    if set(clbips) == set(csips):
        rootLogger.info("No update required")
        exit(0)
    else:
        pass

    # Make a list of new ip's to add into load balancer
    newips = [x for x in csips if x not in clbips]

    # Make a list of old ip's to remove from load balancer
    oldips = [x for x in clbips if x not in csips]

    # Check out verbosity
    if args.verbose:
        v = True
    else:
        v = False

    # Set our load balancing port
    myport = myclb.port

    # If we have new ip's then add them
    if newips:
        # Let the user know what IP's we are adding
        rootLogger.warning('Nodes to add to load balancer %i: %s' %
                           (myclb.id, newips))
        for ip in newips:
            new_node = clb.Node(address=ip, port=myport, condition="ENABLED")
            myclb.add_nodes([new_node])
            # Wait for the load balancer to update
            pyrax.utils.wait_until(myclb,
                                   "status",
                                   "ACTIVE",
                                   interval=1,
                                   attempts=30,
                                   verbose=v)

    # If we have old ip'd then remove them
    if oldips:
        # Let the user know what ips' we are removing
        rootLogger.warning('Nodes to remove from load balancer %i: %s' %
                           (myclb.id, oldips))
        for node in myclb.nodes:
            if node.address in oldips:
                node.delete()
                pyrax.utils.wait_until(myclb,
                                       "status",
                                       "ACTIVE",
                                       interval=1,
                                       attempts=30,
                                       verbose=v)

    # Let the user know we are done
    rootLogger.info("Update complete")
    exit(0)
Example #43
0
def main():
    """
    Challenge 5
    -- Write a script that creates a Cloud Database instance. This instance
       should contain at least one database, and the database should have at
       least one user that can connect to it.
    """
    # Parse script parameters
    p = argparse.ArgumentParser(description=("Create an Cloud DB instance "
                                             "along with a DB and management"
                                             " user"))
    p.add_argument("instance",
                   action="store",
                   type=str,
                   metavar="[instance name]",
                   help="Preferred Cloud DB instance name")
    p.add_argument("-m",
                   "--memory",
                   action="store",
                   required=False,
                   type=int,
                   metavar="[size]",
                   help="Preferred memory size of instance (default 512MB)",
                   choices=FLAVOUR_LIST,
                   default=512)
    p.add_argument("-v",
                   "--volume",
                   action="store",
                   required=False,
                   type=int,
                   metavar="[volume size]",
                   help="Preferred DB volume size in GB (default 1GB)",
                   default=1)
    p.add_argument("-d",
                   "--db",
                   action="store",
                   required=False,
                   type=str,
                   metavar="[db name]",
                   help="Preferred DB name (default 'mydb')",
                   default="mydb")
    p.add_argument("-u",
                   "--username",
                   action="store",
                   required=False,
                   type=str,
                   metavar="[db user]",
                   help=("Preferred DB management user (default "
                         "'dbuser'"),
                   default="dbuser")
    p.add_argument("-p",
                   "--password",
                   action="store",
                   required=False,
                   type=str,
                   metavar="[db password]",
                   help=("Preferred DB user password (default is a random "
                         "string"),
                   default=(pyrax.utils.random_ascii(length=10)))
    p.add_argument("-o",
                   "--host",
                   action="store",
                   required=False,
                   type=str,
                   metavar="[host]",
                   help=("Host IP address/wildcard for user access (default "
                         "is '%')"),
                   default="%")
    p.add_argument("-r",
                   "--region",
                   action="store",
                   required=False,
                   metavar="[region]",
                   type=str,
                   help=("Region where container should be created "
                         "(defaults to 'ORD'"),
                   choices=["ORD", "DFW", "LON", "IAD", "HKG", "SYD"],
                   default="ORD")

    # Parse arguments (validate user input)
    args = p.parse_args()

    # Determine if volume size is in acceptable range
    if args.volume < 1 or args.volume > 150:
        print("ERROR: Permitted volume size is between 1 and %d GB" %
              (MAX_VOL_SIZE))
        sys.exit(1)

    # Define the authentication credentials file location and request that
    # pyrax makes use of it. If not found, let the client/user know about it.

    # Use a credential file in the following format:
    # [rackspace_cloud]
    # username = myusername
    # api_key = 01234567890abcdef
    # region = LON

    try:
        creds_file = os.path.expanduser(CONFIG_FILE)
        pyrax.set_setting("identity_type", IDENTITY_TYPE)
        pyrax.set_credential_file(creds_file, args.region)
    except e.AuthenticationFailed:
        print(
            "ERROR: Authentication failed. Please check and confirm "
            "that the API username, key, and region are in place and "
            "correct.")
        sys.exit(2)
    except e.FileNotFound:
        print "ERROR: Credentials file '%s' not found" % (creds_file)
        sys.exit(3)

    # Use a shorter Cloud Databases class reference string
    # This simplifies invocation later on (less typing)
    cdb = pyrax.cloud_databases

    # Determine which flavor was selected and grab the full details
    try:
        flavor = [i for i in cdb.list_flavors() if args.memory == i.ram][0]
    except:
        print(
            "ERROR: Flavor name provided has not matched any entries. "
            "Please check and try again.")
        sys.exit(4)

    # Attempt to create the instance
    try:
        print "INFO: Creating instance '%s'" % (args.instance)
        instance = cdb.create(args.instance, flavor=flavor, volume=args.volume)
    except:
        print "ERROR: Instance creation failed"
        sys.exit(5)

    # Keep checking status until it's something other than 'BUILD'
    while instance.status in ["BUILD"]:
        # Push something to the screen to assure us things are moving along
        sys.stdout.write(".")
        sys.stdout.flush()
        # Reasonable wait time between status checks
        sleep(30)
        instance.get()
    print

    # Inform the client/user of the outcome
    if instance.status not in ["ACTIVE"]:
        print("ERROR: Instance build failed\nStatus: %s" % instance.status)
        sys.exit(6)
    else:
        print "INFO: Instance successfully created"

    try:
        # Add the new DB to the instance
        instance.create_database(args.db)

        # Same for the user
        instance.create_user(args.username,
                             args.password,
                             database_names=args.db,
                             host=args.host)
    except e.BadRequest as err:
        print "ERROR: DB and user creation failed\nReason:", err
        sys.exit(7)

    # We're all done
    print(
        "-- Details:\n\tDB Host: %s\n\tDB Name: %s\n\tDB User: %s\n\t"
        "DB Password: %s" %
        (instance.hostname, args.db, args.username, args.password))
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('base',
                        help='The base hostname to use, 3x512MB'
                        ' servers will be built using this base hostname')
    parser.add_argument('--dc',
                        required=False,
                        help='The region to '
                        'build the servers in',
                        choices=['DFW', 'ORD', 'LON'],
                        default=pyrax.default_region)
    parser.add_argument('--image',
                        required=False,
                        help='The image ID to build'
                        ' the servers with',
                        default='5cebb13a-f783-4f8c-8058-c4182c724ccd')
    parser.add_argument('--count',
                        required=False,
                        help='Number of servers to '
                        'build. Default 2',
                        default=2,
                        type=int)

    args = parser.parse_args()
    credentials_file = os.path.expanduser('~/.rackspace_cloud_credentials')
    pyrax.set_credential_file(credentials_file, region=args.dc)
    cs = pyrax.cloudservers
    clb = pyrax.cloud_loadbalancers

    print 'Building servers in: %s' % args.dc

    servers = {}
    for i in xrange(0, args.count):
        host = '%s%d' % (args.base, i)
        print 'Creating server: %s' % host
        servers[host] = cs.servers.create(host, args.image, 2)
        print '%s: %s' % (host, servers[host].id)
    statuses = ['ACTIVE', 'ERROR', 'UNKNOWN']
    while filter(lambda server: server.status not in statuses,
                 servers.values()):
        print 'Sleeping 30 seconds before checking for server readiness...'
        time.sleep(30)
        for host in servers:
            if servers[host].status in statuses:
                continue
            servers[host].get()

    nodes = []
    for host, server in server_details.iteritems():
        nodes.append(
            clb.Node(address=server.networks['private'][0],
                     port=80,
                     condition='ENABLED'))

    vip = clb.VirtualIP(type='PUBLIC')

    lb = clb.create('%s-lb' % args.base,
                    port=80,
                    protocol='HTTP',
                    nodes=nodes,
                    virtual_ips=[vip])

    public_addresses = [vip.address for vip in lb.virtual_ips]

    t = prettytable.PrettyTable(
        ['ID', 'Host', 'Status', 'IP', 'Admin Password'])
    for host, server in servers.iteritems():
        t.add_row([
            server.id, host, server.status,
            ', '.join(server.networks['public']), server.adminPass
        ])
    print 'Servers and loadbalancer online and ready...'
    print t
    t = prettytable.PrettyTable(['ID', 'Name', 'IP Address'])
    t.add_row([lb.id, lb.name, ', '.join(public_addresses)])
    print
    print t
Example #45
0
def main():
    """
    Challenge 3
    -- Write a script that accepts a directory as an argument as well as a
       container name. The script should upload the contents of the specified
       directory to the container (or create it if it doesn't exist). The
       script should handle errors appropriately. (Check for invalid paths,
       etc.)
    """
    # Parse script parameters
    p = argparse.ArgumentParser(description=("Push local objects "
                                             "to Cloud Files"))
    p.add_argument("directory",
                   action="store",
                   type=str,
                   metavar="[local dir]",
                   help="Local directory to upload content from")
    p.add_argument("container",
                   action="store",
                   type=str,
                   metavar="[container name]",
                   help=("Container name where content should "
                         "be uploaded"))
    p.add_argument("-r",
                   "--region",
                   action="store",
                   required=False,
                   metavar="[region]",
                   type=str,
                   help=("Region where container should be created"
                         " (defaults to 'ORD'"),
                   choices=["ORD", "DFW", "LON", "IAD", "HKG", "SYD"],
                   default="ORD")
    p.add_argument("-f",
                   "--force",
                   action="store_true",
                   required=False,
                   help=("Permit/force the upload to an "
                         "existing container"))

    # Parse arguments (validate user input)
    args = p.parse_args()

    # Determine if the upload directory exists
    if not os.path.isdir(args.directory):
        print(
            "ERROR: Specified directory (%s) does not exist, please check "
            "the path and try again)" % (args.directory))
        sys.exit(1)

    # Define the authentication credentials file location and request that
    # pyrax makes use of it. If not found, let the client/user know about it.

    # Use a credential file in the following format:
    # [rackspace_cloud]
    # username = myusername
    # api_key = 01234567890abcdef
    # region = LON

    try:
        creds_file = os.path.expanduser(CONFIG_FILE)
        pyrax.set_setting("identity_type", IDENTITY_TYPE)
        pyrax.set_credential_file(creds_file, args.region)
    except e.AuthenticationFailed:
        print(
            "ERROR: Authentication failed. Please check and confirm "
            "that the API username, key, and region are in place "
            "and correct.")
        sys.exit(2)
    except e.FileNotFound:
        print "ERROR: Credentials file '%s' not found" % (creds_file)
        sys.exit(3)

    # Use a shorter Cloud Files class reference string
    # This simplifies invocation later on (less typing)
    cf = pyrax.cloudfiles

    # Determine if container already exists, otherwise create it
    try:
        print "Checking if container already exists..."
        cont = cf.get_container(args.container)
    except:
        cont = None

    # Container not found, create it
    if cont is None:
        try:
            print("Container '%s' not found, creating..." % (args.container))
            cont = cf.create_container(args.container)
        except:
            print "ERROR: Could not create container", args.container
            sys.exit(4)
    # Otherwise inform the user/client that the directory exists and
    # determine if we can proceed (is the overwrite flag set)
    else:
        print "Container '%s' found" % (cont.name)
        if args.force:
            print "Proceeding as upload has been forced"
        else:
            print "Force flag not set, exiting..."
            sys.exit(5)

    # Start the upload
    print "Beginning directory/folder upload"
    (upload_key, total_bytes) = cf.upload_folder(args.directory, cont)

    # Inform the user of the total upload size
    print("Total upload size: %d bytes (%.2f MB)" %
          (total_bytes, total_bytes / 1024.0 / 1024))

    # Prepare progress toolbar
    sys.stdout.write("[%s]" % (" " * TOOLBAR_WIDTH))
    sys.stdout.flush()
    # Return to start of line, after '['
    sys.stdout.write("\b" * (TOOLBAR_WIDTH + 1))

    # Print the upload progress (1 second interval)
    uploaded = 0
    while uploaded < total_bytes:
        uploaded = cf.get_uploaded(upload_key)
        progress = int(ceil((uploaded * 100.0) / total_bytes / 2))
        sys.stdout.write("%s" % ("=" * progress))
        sys.stdout.flush()
        sys.stdout.write("\b" * (progress))  # Reset progress bar
        sleep(1)
    print

    # Upload completed, confirm/print object count
    objs = cf.get_container_object_names(cont)
    print "Number of objects uploaded: %d\nUpload complete" % (len(objs))
Example #46
0
 def __init__(self, filename, region, container_name, use_public):
     pyrax.set_setting('identity_type', 'rackspace')
     pyrax.set_credential_file(filename)
     self.cf = pyrax.connect_to_cloudfiles(region=region, public=use_public)
     self.cnt = self.cf.create_container(container_name)
Example #47
0
                    help='List details about the specific host (IP address)')

args = parser.parse_args()

# setup the auth
try:
    creds_file = os.environ['RAX_CREDS_FILE']
    region = os.environ['RAX_REGION']
except KeyError, e:
    sys.stderr.write('Unable to load %s\n' % e.message)
    sys.exit(1)

pyrax.set_setting('identity_type', 'rackspace')

try:
    pyrax.set_credential_file(os.path.expanduser(creds_file), region=region)
except Exception, e:
    sys.stderr.write("%s: %s\n" % (e, e.message))
    sys.exit(1)

# Execute the right stuff
if not args.host:
    groups = {}

    # Cycle on servers
    for server in pyrax.cloudservers.list():
        # Define group (or set to empty string)
        try:
            group = server.metadata['group']
        except KeyError:
            group = 'undefined'
Example #48
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('base', help='The base hostname to use')
    parser.add_argument('--domain',
                        help='The domain to create the DNS record '
                        'in',
                        required=True)
    parser.add_argument('--sshkey',
                        help='The SSH key to upload be placed at'
                        '/root/.ssh/authorized_keys',
                        required=True)
    parser.add_argument('--dc',
                        required=False,
                        help='The region to '
                        'build the servers in',
                        choices=['DFW', 'ORD', 'LON'],
                        default=pyrax.default_region)
    parser.add_argument('--image',
                        required=False,
                        help='The image ID to build'
                        ' the servers with',
                        default='5cebb13a-f783-4f8c-8058-c4182c724ccd')
    parser.add_argument('--flavor',
                        required=False,
                        help='The flavor ID to'
                        ' build the servers with',
                        default=2,
                        type=int)
    parser.add_argument('--count',
                        required=False,
                        help='Number of servers to '
                        'build. Default 2',
                        default=2,
                        type=int)
    parser.add_argument('--error-page',
                        required=False,
                        help='Path to an html'
                        ' file to be used as the custom error page on the'
                        ' laod balancer')
    parser.add_argument('--container',
                        required=False,
                        help='Cloud files '
                        'container to backup the custom error page to. '
                        'Defaults to [BASE].[DOMAIN]')

    args = parser.parse_args()
    credentials_file = os.path.expanduser('~/.rackspace_cloud_credentials')
    pyrax.set_credential_file(credentials_file, region=args.dc)
    cs = pyrax.cloudservers
    clb = pyrax.cloud_loadbalancers
    dns = pyrax.cloud_dns
    cf = pyrax.cloudfiles

    parts = args.domain.split('.')
    if len(parts) < 2:
        raise SystemExit('Not a proper domain: %s' % args.domain)

    print 'Building servers in: %s' % args.dc

    try:
        with open(os.path.expanduser(args.sshkey)) as f:
            sshkey = f.read()
        if 'PRIVATE KEY' in sshkey:
            raise SystemExit('Please provide a public key, key provided was'
                             ' a private key')
    except IOError:
        raise SystemExit('SSH Key does not exist: %s' % args.sshkey)
    servers = {}
    for i in xrange(0, args.count):
        host = '%s%d' % (args.base, i)
        print 'Creating server: %s' % host
        servers[host] = cs.servers.create(
            host,
            args.image,
            args.flavor,
            files={'/root/.ssh/authorized_keys': sshkey})
        print '%s: %s' % (host, servers[host].id)
    statuses = ['ACTIVE', 'ERROR', 'UNKNOWN']
    progress = []
    print 'Sleeping 30 seconds before checking server build progress...'
    print '\n' * (abs(args.count) - 1)
    while filter(lambda server: server.status not in statuses,
                 servers.values()):
        time.sleep(30)
        progress[:] = []
        for host in servers:
            if servers[host].status in statuses:
                progress.append('%s: %s%%' % (host, servers[host].progress))
                continue
            servers[host].get()
            progress.append('%s: %s%%' % (host, servers[host].progress))
        progress.sort()
        sys.stdout.write('\r%s%s' % ('\x1b[A' *
                                     (len(progress) - 1), '\n'.join(progress)))
        sys.stdout.flush()

    active = (len(
        filter(lambda server: server.status == 'ACTIVE', servers.values())))
    if not active:
        raise SystemExit('\nNo servers built successfully')
    elif active != args.count:
        print '\nNot all servers successfully built'

    print '\n\nBuilding load balancer'
    nodes = []
    for host, server in servers.iteritems():
        if server.status != 'ACTIVE':
            print 'Not adding %s to LB as it did not build' % host
            continue
        nodes.append(
            clb.Node(address=server.networks['private'][0],
                     port=80,
                     condition='ENABLED'))

    vip = clb.VirtualIP(type='PUBLIC')

    lb = clb.create('%s-lb' % args.base,
                    port=80,
                    protocol='HTTP',
                    nodes=nodes,
                    virtual_ips=[vip])
    print '%s: %s' % (lb.name, lb.id)

    lb = pyrax.utils.wait_until(lb,
                                'status', ['ACTIVE'],
                                interval=30,
                                verbose=True)
    if not lb:
        raise SystemExit('Load balancer failed to build')

    public_addresses = [vip.address for vip in lb.virtual_ips]

    print 'Adding HTTP health check...'
    lb.add_health_monitor(type='HTTP',
                          delay=1,
                          timeout=3,
                          attemptsBeforeDeactivation=2,
                          path='/',
                          statusRegex="^[23][0-9][0-9]$",
                          bodyRegex='</html>',
                          hostHeader='%s.%s' % (args.base, args.domain))

    lb = pyrax.utils.wait_until(lb,
                                'status', ['ACTIVE'],
                                attempts=20,
                                verbose=True)

    default_error_page = """<html>
<head>
    <title>ErRAWR!</title>
</head>
<body>
    <h1>Oops, ErRAWR!</h1>
</body>
</html>"""
    if args.error_page:
        try:
            with open(args.error_page) as f:
                error_content = f.read()
        except IOError:
            error_content = default_error_page
    else:
        error_content = default_error_page
    print 'Adding custom error page...'
    lb.set_error_page(error_content)

    domains = dns.list()
    try:
        dom = filter(lambda x: x.name == args.domain, domains)[0]
    except IndexError:
        raise SystemExit('No matching domains: %s' % args.domain)

    records = []
    for public_address in public_addresses:
        rec_type = 'AAAA' if ':' in public_address else 'A'
        records.append({
            'ttl': 300,
            'name': '%s.%s' % (args.base, args.domain),
            'type': rec_type,
            'data': public_address
        })

    domain = dom.add_records(records)
    print('Record(s) added: %s -> %s' %
          ('%s.%s' % (args.base, args.domain), ', '.join(public_addresses)))

    if args.container:
        container = args.container
    else:
        container = '%s.%s' % (args.base, args.domain)

    print('Backing up custom error page (%s) to container %s' %
          ('%s-error.html' % ('%s.%s' % (args.base, args.domain)), container))
    try:
        cont = cf.get_container(container)
    except pyrax.exceptions.NoSuchContainer as e:
        print 'Container %s does not exist, creating...' % container
        cont = cf.create_container(container)

    cont.store_object('%s-error.html' % ('%s.%s' % (args.base, args.domain)),
                      error_content,
                      content_type='text/html')

    t = prettytable.PrettyTable(
        ['ID', 'Host', 'Status', 'IP', 'Admin Password'])
    for host, server in servers.iteritems():
        t.add_row([
            server.id, host, server.status,
            ', '.join(server.networks['public']), server.adminPass
        ])
    print 'Servers and loadbalancer online and ready...'
    print t
    t = prettytable.PrettyTable(['ID', 'Host', 'Name', 'IP Address'])
    t.add_row([
        lb.id,
        '%s.%s' % (args.base, args.domain), lb.name,
        ', '.join(public_addresses)
    ])
    print
    print t
Example #49
0
def main():

    # Define the authentication credentials file location and request that
    # pyrax makes use of it. If not found, let the client/user know about it.

    # Use a credential file in the following format:
    # [rackspace_cloud]
    # username = myusername
    # api_key = 01234567890abcdef
    # region = LON

    # Set identity type as rackspace
    pyrax.set_setting("identity_type", "rackspace")

    # Test that the pyrax configuration file provided exists
    try:
        creds_file = os.path.expanduser(args.credfile)
        pyrax.set_credential_file(creds_file, args.region)
    # Exit if authentication fails
    except pex.AuthenticationFailed:
        rootLogger.critical("Authentication failed")
        rootLogger.info(
            "%s", """Please check and confirm that the API username,
                                     key, and region are in place and correct."""
        )
        exit(2)
    # Exit if file does not exist
    except pex.FileNotFound:
        rootLogger.critical("Credentials file '%s' not found" % (creds_file))
        rootLogger.info(
            "%s", """Use a credential file in the following format:\n
                                 [rackspace_cloud]
                                 username = myuseername
                                 api_key = 01sdf444g3ffgskskeoek0349""")
        exit(3)

    # Shorten the call to cloud databases
    cdb = pyrax.cloud_databases

    # Try to find the instance matching UUID provided
    try:
        mycdb = cdb.find(id=args.instance)
    except pex.NotFound:
        rootLogger.critical("No instances found matching '%s'" %
                            (args.instance))
        exit(4)

    # Create a new backup
    if args.description:
        description = args.description
    else:
        description = "Created on " + datetime.datetime.now().strftime(
            "%Y-%b-%d-%H:%M")

    try:
        rootLogger.info("Creating backup of '%s'." % (mycdb.name))
        new_backup = mycdb.create_backup(
            args.backup + '-' + datetime.datetime.now().strftime("%y%m%d%H%M"),
            description=description)
    except pex.ClientException:
        type, value, traceback = sys.exc_info()
        rootLogger.critical(value.message)
        exit(5)

    # Put our current backups in a list
    backups = []
    for backup in mycdb.list_backups():
        if backup.name.startswith(args.backup) and backup.name[10:].isnumeric:
            backups.append(backup)

    # Sort out backups by date (this is probably not a very reliable way)
    backups.sort(key=lambda backup: backup.created, reverse=True)

    # Print our current backups
    rootLogger.info("Current backups of '%s' below:" % (mycdb.name))
    for backup in backups:
        rootLogger.info("Name: '%s' Created on '%s'" %
                        (backup.name, backup.created))

    # Check if backups need to be deleted
    if len(backups) > args.number and args.number is not 0:
        # Warn of backups being deleted
        rootLogger.warning("There are '%i' backups. Need to delete '%i'." %
                           (len(backups), len(backups) - args.number))
        # Delete oldest backups if current backups > target backups
        for backup in backups[args.number:]:
            try:
                rootLogger.warning("The below backups will be deleted.")
                rootLogger.warning("Name: '%s' Created on '%s'." %
                                   (backup.name, backup.created))
                backup.delete()
            except pex.ClientException:
                type, value, traceback = sys.exc_info()
                rootLogger.critical(value.message)
                exit(6)

            # Wait until the instance is active
            pyrax.utils.wait_for_build(mycdb,
                                       "status",
                                       "ACTIVE",
                                       interval=1,
                                       attempts=30)

    exit(0)
Example #50
0
'''
Write a script that uses Cloud DNS to create a new A record
when passed a FQDN and IP address as arguments. Worth 1 Point
'''

import pyrax
import os

pyrax.set_credential_file(
    os.path.join(os.path.expanduser('~'), '.rackspace_cloud_credentials'))

domain = pyrax.cloud_dns.create(name='my-awesome-example.com',
                                emailAddress='*****@*****.**')
domain.add_record([{
    'type': 'A',
    'name': 'example.com',
    'data': '200.168.1.1',
    'ttl': 6000
}])
Example #51
0
def setup():
    default_creds_file = os.path.expanduser('~/.rackspace_cloud_credentials')

    env = get_config(p, 'rax', 'environment', 'RAX_ENV', None)
    if env:
        pyrax.set_environment(env)

    keyring_username = pyrax.get_setting('keyring_username')

    # Attempt to grab credentials from environment first
    creds_file = get_config(p, 'rax', 'creds_file', 'RAX_CREDS_FILE', None)
    if creds_file is not None:
        creds_file = os.path.expanduser(creds_file)
    else:
        # But if that fails, use the default location of
        # ~/.rackspace_cloud_credentials
        if os.path.isfile(default_creds_file):
            creds_file = default_creds_file
        elif not keyring_username:
            sys.exit('No value in environment variable %s and/or no '
                     'credentials file at %s' %
                     ('RAX_CREDS_FILE', default_creds_file))

    identity_type = pyrax.get_setting('identity_type')
    pyrax.set_setting('identity_type', identity_type or 'rackspace')

    region = pyrax.get_setting('region')

    try:
        if keyring_username:
            pyrax.keyring_auth(keyring_username, region=region)
        else:
            pyrax.set_credential_file(creds_file, region=region)
    except Exception as e:
        sys.exit("%s: %s" % (e, e.message))

    regions = []
    if region:
        regions.append(region)
    else:
        try:
            # Ansible 2.3+
            region_list = get_config(p,
                                     'rax',
                                     'regions',
                                     'RAX_REGION',
                                     'all',
                                     value_type='list')
        except TypeError:
            # Ansible 2.2.x and below
            # pylint: disable=unexpected-keyword-arg
            region_list = get_config(p,
                                     'rax',
                                     'regions',
                                     'RAX_REGION',
                                     'all',
                                     islist=True)

        for region in region_list:
            region = region.strip().upper()
            if region == 'ALL':
                regions = pyrax.regions
                break
            elif region not in pyrax.regions:
                sys.exit('Unsupported region %s' % region)
            elif region not in regions:
                regions.append(region)

    return regions
Example #52
0
def main():

    # Parse the command line arguments
    parser = argparse.ArgumentParser(
        description="Create chef server for cloud edge env.",
        prog='create_chef_server.py')
    #parser.add_argument(
    #    '--image', help='image id or name Ubuntu 12.10 \
    #    (Quantal Quetzal)', default='Ubuntu 12.10 (Quantal Quetzal)')
    parser.add_argument('--flavor',
                        help='flavor id or name (default: 512MB Standard \
        Instance)',
                        default='512MB Standard Instance')
    parser.add_argument('--region',
                        help='Region (default: DFW)',
                        default="DFW")
    parser.add_argument('--fqdn',
                        help='fully qualified domain name',
                        required=True)
    parser.add_argument('--public_keyfile',
                        help='ssh public key file',
                        required=True)
    args = parser.parse_args()

    # Authenticate using a credentials file: "~/.rackspace_cloud_credentials"
    cred_file = "%s/.rackspace_cloud_credentials" % (os.environ['HOME'])
    print "Setting authentication file to %s" % (cred_file)
    pyrax.set_credential_file(cred_file)

    # Instantiate a cloudservers object
    print "Instantiating cloudservers object"
    csobj = pyrax.cloudservers

    # Instantiate a clouddns object
    print "Instantiating cloud_dns object"
    cdnsobj = pyrax.cloud_dns

    # Get the flavor id
    flavor_id = get_flavor_from_id_or_name(csobj, args.flavor)

    # Get the image id
    image_id = get_image_from_id_or_name(csobj, args.image)

    # Lets check the domain existance for the fqdn before going on
    check_dns(cdnsobj, args.fqdn)

    # Check public key file
    cs_files = check_pub_key_file(args.public_keyfile)

    # Add the install script to the cs_files dict
    cs_files = create_install_script(cs_files)

    # Create the servers by server_count
    orig_server = create_server(csobj, args.fqdn, flavor_id, image_id,
                                cs_files)

    # Call function to wait on it to build
    updated_server = wait_and_update(csobj, orig_server)

    # Create a DNS entry for the server
    create_dns(cdnsobj, args.fqdn, updated_server.accessIPv4)

    # Run the command to install chef
    subprocess.call([
        "ssh", "root@" + updated_server.accessIPv4, "bash",
        "/root/install_chef.sh"
    ])

    # Print server information and exit
    print_info(orig_server.adminPass, updated_server)
Example #53
0
def cloudfiles_func(settings, filename, results):
    """
    Uploads files to Rackspace Cloud Files.
    """

    name = threading.currentThread().getName()
    logger = logging.getLogger(__name__ + "." + name)

    creds_file = settings['credential_file']
    pyrax.set_credential_file(creds_file)
    pyrax.set_setting('use_servicenet', settings['use_snet'])
    region = settings['region']
    container_name = settings['container_name']
    nest_by_timestamp = settings.get('nest_by_timestamp', False)
    obj_ttl = settings.get('set_ttl', None)

    try:
        cf = pyrax.connect_to_cloudfiles(region=region)
        container = cf.get_container(container_name)
    except:
        logger.error(
            "Unable to connect to cloudfiles. Transfer for {0} aborted, failing gracefully."
            .format(filename))
        results.append(name)
        return

    if os.path.getsize(filename) >= 5368709120:
        logger.error(
            "{0} is too large. Files over 5GB are not currently supported.".
            format(filename))
        results.append(name)
        return

    obj_name = os.path.basename(filename)

    # Create new obj_name for nested directory
    if nest_by_timestamp:
        t = os.path.getmtime(filename)
        d = dt.fromtimestamp(t)
        obj_name = "{year}/{month}/{day}/{filename}".format(
            year=d.strftime("%Y"),
            month=d.strftime("%m"),
            day=d.strftime("%d"),
            filename=obj_name)

    chksum = pyrax.utils.get_checksum(filename)
    for i in range(MAX_RETRIES):
        try:
            start = time.time()
            #Used for testing the retry
            #raise pyrax.exceptions.UploadFailed()
            obj = container.upload_file(filename,
                                        obj_name=obj_name,
                                        etag=chksum,
                                        ttl=obj_ttl)
            end = time.time()
            logger.debug("%s transferred to %s in %.2f secs." %
                         (filename, container_name, (end - start)))
            break
        except pyrax.exceptions.UploadFailed:
            logger.warning("Upload to container:%s in %s failed, retry %d" %
                           (container_name, region, i + 1))
            time.sleep(2)
    else:
        logger.error("Upload to container:%s in %s failed!" %
                     (container_name, region))
        results.append(name)
Example #54
0
def main():
    """
    Challenge 6
    -- Write a script that creates a CDN-enabled container in Cloud Files
    """
    # Parse script parameters
    p = argparse.ArgumentParser(description=("Push local objects "
                                             "to Cloud Files"))
    p.add_argument("directory",
                   action="store",
                   type=str,
                   metavar="[local dir]",
                   help="Local directory to upload content from")
    p.add_argument("container",
                   action="store",
                   type=str,
                   metavar="[container name]",
                   help="Container name where content should be uploaded")
    p.add_argument("-r",
                   "--region",
                   action="store",
                   required=False,
                   metavar="[region]",
                   type=str,
                   help=("Region where container should be created"
                         " (defaults to 'ORD'"),
                   choices=["ORD", "DFW", "LON", "IAD", "HKG", "SYD"],
                   default="ORD")
    p.add_argument("-t",
                   "--ttl",
                   action="store",
                   required=False,
                   type=int,
                   help=("CDN TTL for the container (default '%d seconds')" %
                         (MIN_TTL)),
                   default=MIN_TTL)
    p.add_argument("-f",
                   "--force",
                   action="store_true",
                   required=False,
                   help=("Permit upload to an "
                         "existing container"))

    args = p.parse_args()

    # Determine if the upload directory exists
    if not os.path.isdir(args.directory):
        print(
            "ERROR: Specified directory (%s) does not exist, please check "
            "the path and try again)" % (args.directory))
        sys.exit(1)

    # Determine if TTL is at least the minimum value permitted
    if args.ttl < MIN_TTL:
        print "ERROR: Minimum TTL permitted is %ds" % (MIN_TTL)
        sys.exit(2)

    # Define the authentication credentials file location and request that
    # pyrax makes use of it. If not found, let the client/user know about it.

    # Use a credential file in the following format:
    # [rackspace_cloud]
    # username = myusername
    # api_key = 01234567890abcdef
    # region = LON

    try:
        creds_file = os.path.expanduser(CONFIG_FILE)
        pyrax.set_setting("identity_type", IDENTITY_TYPE)
        pyrax.set_credential_file(creds_file, args.region)
    except e.AuthenticationFailed:
        print(
            "ERROR: Authentication failed. Please check and confirm "
            "that the API username, key, and region are in place "
            "and correct.")
        sys.exit(3)
    except e.FileNotFound:
        print "ERROR: Credentials file '%s' not found" % (creds_file)
        sys.exit(4)

    # Use a shorter Cloud Files class reference string
    # This simplifies invocation later on (less typing)
    cf = pyrax.cloudfiles

    # Determine if container already exists, otherwise create it
    try:
        print "INFO: Checking if container already exists..."
        cont = cf.get_container(args.container)
    except:
        cont = None

    # Container not found, create it and CDN enable
    if cont is None:
        try:
            print(
                "INFO: Container '%s' not found, creating with TTL set "
                "to %d..." % (args.container, args.ttl))
            cont = cf.create_container(args.container)
            cont.make_public(ttl=args.ttl)
        except:
            print "ERROR: Could not create container", args.container
            sys.exit(5)
    # Otherwise inform the user/client that the directory exists and
    # determine if we can proceed (is the overwrite flag set)
    else:
        print("INFO: Container '%s' found with TTL set to %d" %
              (cont.name, cont.cdn_ttl))
        if args.force:
            print "INFO: Proceeding as force flag is set"
        else:
            print "INFO: Force flag not set, exiting..."
            sys.exit(6)

    # Start the upload
    print "INFO: Beginning directory/folder upload"
    (upload_key, total_bytes) = cf.upload_folder(args.directory, cont)

    # Inform the user of the total upload size
    print("INFO: Total upload size: %d bytes (%.2f MB)" %
          (total_bytes, total_bytes / 1024.0 / 1024))

    # Prepare progress toolbar
    sys.stdout.write("[%s]" % (" " * TOOLBAR_WIDTH))
    sys.stdout.flush()
    # Return to start of line, after '['
    sys.stdout.write("\b" * (TOOLBAR_WIDTH + 1))

    # Print the upload progress (1 second interval)
    uploaded = 0
    while uploaded < total_bytes:
        uploaded = cf.get_uploaded(upload_key)
        progress = int(ceil((uploaded * 100.0) / total_bytes / 2))
        sys.stdout.write("%s" % ("=" * progress))
        sys.stdout.flush()
        sys.stdout.write("\b" * (progress))  # Reset progress bar
        sleep(1)
    print

    # Upload completed, print object count and CDN URIs
    objs = cf.get_container_object_names(cont)
    print "INFO: Number of objects uploaded: %d" % (len(objs))
    print(
        "\nCDN links:\n\tHTTP: %s\n\tHTTPS: %s\n\tStreaming: %s\n\tiOS "
        "streaming: %s\n" % (cont.cdn_uri, cont.cdn_ssl_uri,
                             cont.cdn_streaming_uri, cont.cdn_ios_uri))
    print "INFO: Upload completed successfully"
Example #55
0
        elif not keyring_username:
            sys.stderr.write('No value in environment variable %s and/or no '
                             'credentials file at %s\n'
                             % (e.message, default_creds_file))
            sys.exit(1)

    identity_type = pyrax.get_setting('identity_type')
    pyrax.set_setting('identity_type', identity_type or 'rackspace')

    region = pyrax.get_setting('region')

    try:
        if keyring_username:
            pyrax.keyring_auth(keyring_username, region=region)
        else:
            pyrax.set_credential_file(creds_file, region=region)
    except Exception, e:
        sys.stderr.write("%s: %s\n" % (e, e.message))
        sys.exit(1)

    regions = []
    if region:
        regions.append(region)
    else:
        for region in os.getenv('RAX_REGION', 'all').split(','):
            region = region.strip().upper()
            if region == 'ALL':
                regions = pyrax.regions
                break
            elif region not in pyrax.regions:
                sys.stderr.write('Unsupported region %s' % region)
Example #56
0
                    metavar="DBPASS")
parser.add_argument("-t",
                    dest="interval",
                    type=int,
                    default=5,
                    help="wait INTERVAL seconds between status checks",
                    metavar="INTERVAL")
parser.add_argument("-d",
                    dest="dc",
                    default="DFW",
                    help="use data center DC",
                    metavar="DC")

args = parser.parse_args()

pyrax.set_credential_file(os.path.expanduser("~/.rackspace_cloud_credentials"))

cs = pyrax.connect_to_cloud_databases(region=args.dc)

# check for valid flavor
flavors = cs.list_flavors()
flavor = None
for f in flavors:
    if f.id == args.flavor:
        flavor = f
if flavor is None:
    print "Flavor %s not found.  Please specify an existing flavor ID:" % (
        args.flavor)
    for f in flavors:
        print "ID: %s  Name: %s" % (f.id, f.name)
    exit(1)
Example #57
0
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from __future__ import print_function

import os
import pyrax

pyrax.set_setting("identity_type", "rackspace")
creds_file = os.path.expanduser("~/.rackspace_cloud_credentials")
pyrax.set_credential_file(creds_file)
clb = pyrax.cloud_loadbalancers

# You can specify a type, address, IP version, and ID
# None are required; defaults are:
#    type: PUBLIC
#    address: None
#    ip_version: IPV4
#    id: None

vip = clb.VirtualIP()
print("Virtual IP (using defaults):", vip
)
vip = clb.VirtualIP(type="SERVICENET", address="1.2.3.4", ipVersion="IPV4", id=999)
print("Virtual IP (using supplied values):", vip)
'''
Challenge 1: Write a script that builds three 512 MB Cloud Servers that
following a similar naming convention. (ie., web1, web2, web3) and
returns the IP and login credentials for each server. Use any image
you want. Worth 1 point
'''

import pyrax
from os.path import expanduser, join

pyrax.set_credential_file(join(expanduser("~"),
                               ".rackspace_cloud_credentials"))
cs = pyrax.cloudservers
i = 1
servers = []
while i < 4:
    servers.append(
        cs.servers.create('server' + str(i),
                          'da1f0392-8c64-468f-a839-a9e56caebf07', 2))
    i += 1
print 'building servers... please wait'
for server in servers:
    new_srv = pyrax.utils.wait_until(server,
                                     "status", ["ACTIVE", "ERROR"],
                                     attempts=0)
    print 'The server %s has the user root with password %s and you can connect on IP %s.' % (
        server.name, server.adminPass, new_srv.networks['public'][1])
Example #59
0
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
#import time
#import requests
import pyrax
#import pyrax.exceptions as exc

########################################################
# This script uses a config file for credential storage:
# [rackspace_cloud]
# username = username
# api_key = LOLnopeLOL
########################################################

#cf_ord = pyrax.connect_to_cloudfiles("ORD")
#cf.list_containers()
#cf.get_container("DoubleTake")
#cont.get_objects()
pyrax.set_setting("identity_type", "rackspace")
CREDS_FILE = os.path.expanduser("~/.rax_creds")
pyrax.set_credential_file(CREDS_FILE)
CF = pyrax.cloudfiles
CONT_NAME = 'DoubleTake'
ONAME = 'doubletake_8.1.1.808.0-Linux.zip'
CF.set_temp_url_key()
CONT = CF.get_container("DoubleTake")
OBJ = CONT.get_object('doubletake_8.1.1.808.0-Linux.zip')
TEMP_URL = OBJ.get_temp_url(seconds=6000)
print(TEMP_URL)
parser.add_argument(
    '-k',
    '--keywords',
    help='Only return containers with these strings; comma-separated.')

## Parse arguments
args = parser.parse_args()

## Convert exclude & keyword to lists
excludes = args.exclude.split(',') if args.exclude else []
keywords = args.keywords.split(',') if args.keywords else []

## Set up the Rackspace Connection:
if os.path.isfile(credentialFileName):
    pyrax.set_setting("identity_type", "rackspace")
    pyrax.set_credential_file(credentialFileName)
else:
    print "Invalid Credential file."
    sys.exit(1)

## Instantiate Cloud Files access:
cf = pyrax.cloudfiles

######################################
## Main

## Get a list of container names
containerNames = cf.list_containers()

if args.keywords:
    filteredList = []