error = 'ERROR: you entered an invalid number of servers' if num_servers == '': print error sys.exit(1) num_servers = int(num_servers) if (num_servers < 1) or (num_servers > 50): print error, '[ either too few (0) or too many >50 ]' sys.exit(1) print 'building {0} servers...'.format(num_servers) create_servers = [] for n in range (1, num_servers+1): create_servers.append(prefix+str(n)) # get all OS's in a list, filter list for Cent* matches, sort filtered list, and grab latest version os_imgs = helper.act_loop(cs.images.list) cent_os_imgs = [img for img in helper.act_loop(cs.images.list) if "Cent" in img.name] cent_os_imgs.sort(key=lambda x: x.name, reverse=True) latest_cent_os_img = cent_os_imgs[0] # search flavors, get 512* instance via match sv_512 = [flavor for flavor in helper.act_loop(cs.flavors.list) if "512" in flavor.name][0] # queue a list of servers to build out queued_servers = [] data = {} for host in create_servers: data = { 'name': host, 'os_img_id': latest_cent_os_img.id, 'flavor_id': sv_512.id, 'files': files, 'completed': 'no'
num_servers = helper.strip_non_numbers(sys.argv[1]) error = 'ERROR: you entered an invalid number of servers' if num_servers == '': print error sys.exit(1) num_servers = int(num_servers) if (num_servers < 1) or (num_servers > 50): print error, '[ either too few (0) or too many >50 ]' sys.exit(1) create_servers = [] for n in range (1, num_servers+1): create_servers.append(prefix+str(n)) # get most recent cloud server, take an clone image of it server = helper.act_loop(cs.servers.list)[0] print 'targeting server:', server.name print 'Creating clone image of target server...' image_label = '{0}-image'.format(server.name) os_img = helper.act_loop(server.create_image, image_label) while True: os_img = helper.act_loop(cs.images.get, os_img) if os_img.status == 'ACTIVE': break print 'waiting for image build, status: {0}, progress: {1}%'.format(os_img.status, os_img.progress) time.sleep(seconds_before_retrying) print 'SUCCESS: cloned images, proceeding to build servers..' # get the right server for our image, upgrade any legacy images to 512 server img_ram_req = os_img.minRam
num_servers = helper.strip_non_numbers(sys.argv[1]) error = 'ERROR: you entered an invalid number of servers' if num_servers == '': print error sys.exit(1) num_servers = int(num_servers) if (num_servers < 1) or (num_servers > 50): print error, '[ either too few (0) or too many >50 ]' sys.exit(1) create_servers = [] for n in range(1, num_servers + 1): create_servers.append(prefix + str(n)) # get most recent cloud server, take an clone image of it server = helper.act_loop(cs.servers.list)[0] print 'targeting server:', server.name print 'Creating clone image of target server...' image_label = '{0}-image'.format(server.name) os_img = helper.act_loop(server.create_image, image_label) while True: os_img = helper.act_loop(cs.images.get, os_img) if os_img.status == 'ACTIVE': break print 'waiting for image build, status: {0}, progress: {1}%'.format( os_img.status, os_img.progress) time.sleep(seconds_before_retrying) print 'SUCCESS: cloned images, proceeding to build servers..' # get the right server for our image, upgrade any legacy images to 512 server
sys.exit(1) else: local_dir_raw = sys.argv[1] # test for folder try: os.chdir(local_dir_raw) except OSError: print 'ERROR: the resource:', local_dir_raw, 'is not a valid directory' sys.exit(1) local_dir_cwd = os.getcwd() local_dir_abs = os.path.abspath(local_dir_raw) local_dir_relpath = os.path.relpath(local_dir_cwd) local_dir_name = os.path.basename(local_dir_abs) # check for container, create one if it does not exist in CF cont = helper.act_loop(cf.get_container, local_dir_name) # get a list of local files, perserve nested structure in filename local_files = {} for dir, subdir, files in os.walk(local_dir_relpath): dir = str.split(dir, '/') dir[0] = '' if dir != []: fol = '/'.join(dir) for file in files: name = '{0}/{1}'.format(fol, file) path = '{0}{1}'.format(local_dir_cwd, name) name = name[1:] local_files[name] = path """ sys.exit(0)
# this script creates a CDN enabled container with a specified name import sys import os import re import time import pyrax import common as helper # preliminary creds_file = os.path.expanduser("~/.rackspace_cloud_credentials") pyrax.set_credential_file(creds_file) cf = pyrax.cloudfiles # take one parameter: the name of the CF container to create if len(sys.argv) < 2: print 'syntax: {0} <container_name>'.format(sys.argv[0]) sys.exit(1) else: site_cont_name = sys.argv[1] # try to use the container if it exists, otherwise create one using provided name and then use it cont = helper.act_loop(cf.get_container, site_cont_name) # make container public, cdn enable container cont.make_public(ttl=1200) # print results print 'created public CDN container: {0}'.format(site_cont_name)
cdns = pyrax.cloud_dns # takes a single parameter: container name to create if len(sys.argv) < 2: print 'syntax: {0} <container_name>'.format(sys.argv[0]) sys.exit(1) else: valid_fqdn = helper.validate_fqdn(sys.argv[1]) if valid_fqdn: site_cont_name = sys.argv[1] else: print 'ERROR: the specified name contains invalid characters (see: RFC 1123 sec. 2.1)' sys.exit(1) # check for container cont = helper.act_loop(cf.get_container, site_cont_name) # make container public, cdn enable container helper.act_loop(cont.make_public, ttl=1200) # create an index page for our container index_page_content = "Welcome to Ryan's test site on the rack cloud CDN!" obj = helper.act_loop(cf.store_object, site_cont_name, "index.htm", index_page_content) # enable it to serve an index file, helper.act_loop(cont.set_web_index_page, 'index.htm') print 'SUCCESS: uploaded web page to new container..' # create a CNAME record pointing to the CDN URL of the container uri = helper.act_loop(str, cont.cdn_uri)
error = 'ERROR: you entered an invalid number of servers' if num_servers == '': print error sys.exit(1) num_servers = int(num_servers) if (num_servers < 1) or (num_servers > 50): print error, '[ either too few (0) or too many >50 ]' sys.exit(1) print 'building {0} servers...'.format(num_servers) create_servers = [] for n in range(1, num_servers + 1): create_servers.append(prefix + str(n)) # get all OS's in a list, filter list for Cent* matches, sort filtered list, and grab latest version os_imgs = helper.act_loop(cs.images.list) cent_os_imgs = [img for img in os_imgs if "Cent" in img.name] cent_os_imgs.sort(key=lambda x: x.name, reverse=True) latest_cent_os_img = cent_os_imgs[0] # search flavors, get 512* instance via match sv_512 = [ flavor for flavor in helper.act_loop(cs.flavors.list) if "512" in flavor.name ][0] # queue a list of servers to build out queued_servers = [] data = {} for host in create_servers: data = { 'name': host,
db_instance_name = sys.argv[1] db_count = helper.strip_non_numbers(sys.argv[2]) # ensure that a valid amount of databases are provided or fail if db_count == '': print 'ERROR: you entered an invalid number of databases, please try again with a proper amount' sys.exit(1) db_count = int(db_count) if (db_count < 1) or (db_count > 50): print 'ERROR: you provided either too few (<1) or too many (>50)' sys.exit(1) # search through flavors and get object for the "1GB" one inst_512 = [flavor for flavor in helper.act_loop(cdb.list_flavors) if "1GB" in flavor.name][0] # create instance with our flavor db_instance = helper.act_loop(cdb.create, db_instance_name, flavor=inst_512.name) while True: db_instance = helper.act_loop(cdb.get, db_instance) if db_instance.status == 'ACTIVE': print 'instance: {0} has been created!'.format(db_instance.name) break print 'instance: {0} is still in BUILD status, checking server every {1} seconds until completion..'.format(db_instance.name,seconds_before_retrying) time.sleep(seconds_before_retrying) # queue up a list of db's to build out create_dbs = [] for n in range (1, db_count+1): create_dbs.append(prefix+str(n))
db_instance_name = sys.argv[1] db_count = helper.strip_non_numbers(sys.argv[2]) # ensure that a valid amount of databases are provided or fail if db_count == '': print 'ERROR: you entered an invalid number of databases, please try again with a proper amount' sys.exit(1) db_count = int(db_count) if (db_count < 1) or (db_count > 50): print 'ERROR: you provided either too few (<1) or too many (>50)' sys.exit(1) # search through flavors and get object for the "1GB" one inst_512 = [ flavor for flavor in helper.act_loop(cdb.list_flavors) if "1GB" in flavor.name ][0] # create instance with our flavor db_instance = helper.act_loop(cdb.create, db_instance_name, flavor=inst_512.name) while True: db_instance = helper.act_loop(cdb.get, db_instance) if db_instance.status == 'ACTIVE': print 'instance: {0} has been created!'.format(db_instance.name) break print 'instance: {0} is still in BUILD status, checking server every {1} seconds until completion..'.format( db_instance.name, seconds_before_retrying) time.sleep(seconds_before_retrying)
error = 'ERROR: you entered an invalid number of servers' if num_servers == '': print error sys.exit(1) num_servers = int(num_servers) if (num_servers < 1) or (num_servers > 50): print error, '[ either too few (0) or too many >50 ]' sys.exit(1) print 'building {0} servers...'.format(num_servers) create_servers = [] for n in range(1, num_servers + 1): create_servers.append(prefix + str(n)) # get all OS's in a list, filter list for Cent* matches, sort filtered list, and grab latest version os_imgs = helper.act_loop(cs.images.list) cent_os_imgs = [ img for img in helper.act_loop(cs.images.list) if "Cent" in img.name ] cent_os_imgs.sort(key=lambda x: x.name, reverse=True) latest_cent_os_img = cent_os_imgs[0] # search flavors, get 512* instance via match sv_512 = [ flavor for flavor in helper.act_loop(cs.flavors.list) if "512" in flavor.name ][0] # queue a list of servers to build out queued_servers = [] data = {} for host in create_servers: data = {
valid_host = sys.argv[2] print "validated host:", valid_host, "successfully" else: print "ERROR: invalid hostname" sys.exit(1) # use helper function to ensure that the specified ipv4 address is valid if valid_ipv4: valid_ipv4 = sys.argv[3] print "validated IPv4:", valid_ipv4, "successfully" else: print "ERROR: invalid ipv4 address" sys.exit(1) # retrieve a list of all domains on this cloud account, limit result to a specified domain, retrieve domain object get_domain = [dom for dom in helper.act_loop(cdns.list) if valid_fqdn == dom.name] if len(get_domain) == 1: domain = get_domain[0] else: print "ERROR:", valid_fqdn, "does not exist on your account" sys.exit(1) # format the subdomain record using the specified hostname subdomain = "{0}.{1}".format(valid_host, domain.name) # create a record using dict records = [{"type": "A", "name": subdomain, "data": valid_ipv4, "ttl": 300}] print "adding A record for:", subdomain helper.act_loop(cdns.add_records, domain, records) print "created dns record: {0} -> {1} on domain: {2}".format(valid_host, valid_ipv4, domain.name)
print 'validated host:', valid_host, 'successfully' else: print 'ERROR: invalid hostname' sys.exit(1) # use helper function to ensure that the specified ipv4 address is valid if valid_ipv4: valid_ipv4 = sys.argv[3] print 'validated IPv4:', valid_ipv4, 'successfully' else: print 'ERROR: invalid ipv4 address' sys.exit(1) # retrieve a list of all domains on this cloud account, limit result to a specified domain, retrieve domain object get_domain = [ dom for dom in helper.act_loop(cdns.list) if valid_fqdn == dom.name ] if len(get_domain) == 1: domain = get_domain[0] else: print 'ERROR:', valid_fqdn, 'does not exist on your account' sys.exit(1) # format the subdomain record using the specified hostname subdomain = '{0}.{1}'.format(valid_host, domain.name) # create a record using dict records = [{"type": "A", "name": subdomain, "data": valid_ipv4, "ttl": 300}] print 'adding A record for:', subdomain helper.act_loop(cdns.add_records, domain, records) print 'created dns record: {0} -> {1} on domain: {2}'.format(
cdns = pyrax.cloud_dns # takes a single parameter: container name to create if len(sys.argv) < 2: print 'syntax: {0} <container_name>'.format(sys.argv[0]) sys.exit(1) else: valid_fqdn = helper.validate_fqdn(sys.argv[1]) if valid_fqdn: site_cont_name= sys.argv[1] else: print 'ERROR: the specified name contains invalid characters (see: RFC 1123 sec. 2.1)' sys.exit(1) # check for container cont = helper.act_loop(cf.get_container, site_cont_name) # make container public, cdn enable container helper.act_loop(cont.make_public, ttl=1200) # create an index page for our container index_page_content = "Welcome to Ryan's test site on the rack cloud CDN!" obj = helper.act_loop(cf.store_object, site_cont_name, "index.htm", index_page_content) # enable it to serve an index file, helper.act_loop(cont.set_web_index_page, 'index.htm') print 'SUCCESS: uploaded web page to new container..' # create a CNAME record pointing to the CDN URL of the container uri = helper.act_loop(str, cont.cdn_uri) uri_host = uri.split('http://') # parse the hostname from the URI
error = 'ERROR: you entered an invalid number of servers' if num_servers == '': print error sys.exit(1) num_servers = int(num_servers) if (num_servers < 1) or (num_servers > 50): print error, '[ either too few (0) or too many >50 ]' sys.exit(1) print 'building {0} servers...'.format(num_servers) create_servers = [] for n in range (1, num_servers+1): create_servers.append(prefix+str(n)) # get all OS's in a list, filter list for Cent* matches, sort filtered list, and grab latest version os_imgs = helper.act_loop(cs.images.list) cent_os_imgs = [img for img in helper.act_loop(cs.images.list) if "Cent" in img.name] cent_os_imgs.sort(key=lambda x: x.name, reverse=True) latest_cent_os_img = cent_os_imgs[0] # search flavsies, get 512* instance via match sv_512 = [flavor for flavor in helper.act_loop(cs.flavors.list) if "512" in flavor.name][0] # grab the SSH keys from the host so we can inject them into the server via files kwarg with open ("/root/.ssh/authorized_keys", "r") as root_ssh_auth_keys: ssh_auth_file=root_ssh_auth_keys.read().replace('\n', '') files = {"/root/.ssh/authorized_keys": ssh_auth_file} # queue a list of servers to build out queued_servers = [] data = {}
error = 'ERROR: you entered an invalid number of servers' if num_servers == '': print error sys.exit(1) num_servers = int(num_servers) if (num_servers < 1) or (num_servers > 50): print error, '[ either too few (0) or too many >50 ]' sys.exit(1) print 'building {0} servers...'.format(num_servers) create_servers = [] for n in range (1, num_servers+1): create_servers.append(prefix+str(n)) # get all OS's in a list, filter list for Cent* matches, sort filtered list, and grab latest version os_imgs = helper.act_loop(cs.images.list) cent_os_imgs = [img for img in os_imgs if "Cent" in img.name] cent_os_imgs.sort(key=lambda x: x.name, reverse=True) latest_cent_os_img = cent_os_imgs[0] # search flavors, get 512* instance via match sv_512 = [flavor for flavor in helper.act_loop(cs.flavors.list) if "512" in flavor.name][0] # queue a list of servers to build out queued_servers = [] data = {} for host in create_servers: data = { 'name': host, 'os_img_id': latest_cent_os_img.id, 'flavor_id': sv_512.id, 'files': None,