def set_current_server_networks(self):
		cs_dfw = pyrax.connect_to_cloudservers(region="ORD")
		cs_ord = pyrax.connect_to_cloudservers(region="DFW")
		dfw_servers = cs_dfw.servers.list()
		ord_servers = cs_ord.servers.list()
		self.servers = dfw_servers + ord_servers
		for server in self.servers:
			if server.name == self.server_name:
				self.network = server.networks	
Exemplo n.º 2
0
def get_servers(location = 'all'):
  region_list = {}
  if location == 'all':
    for temp_reg in pyrax.regions:
      for server in pyrax.connect_to_cloudservers(region=temp_reg).servers.list():
        region_list[server.name] = [ server.name, server.id, server.status, temp_reg ]
  else:
    for server in pyrax.connect_to_cloudservers(region=location).servers.list():
      region_list[server.name] = [ server.name, server.id, server.status, location ]
  return region_list
Exemplo n.º 3
0
def build_server(build_queue):
    # create list of server information to keep each build contained to it's own thread
    build_details = []
    try:
        print "Getting job from queue..."
        server_name = build_queue.get()
        if ssh_auth == "y":
            print "Connecting to cloud to build server %s" % server_name
            server = pyrax.connect_to_cloudservers(region=region).servers.create(server_name, server_id, server_flv_id, ssh_auth)
            print "%s has started building" % server_name
            #pass
        else:
            server = pyrax.connect_to_cloudservers(region=region).servers.create(server_name, server_id, server_flv_id)
            #pass

        build_details.append(breakpoint)
        build_details.append("Name: " + server.name)
        build_details.append("ID: " + server.id)
        build_details.append("Region: " + region)
        build_details.append("Admin Password: "******"status", ["ACTIVE", "ERROR"], interval=5, callback=None, attempts=0, verbose=False, verbose_atts="progress")
                pyrax.utils.wait_until(server, 'status', ('ACTIVE', 'ERROR'), interval=5, attempts=wait_timeout/5)
                build_details.append("Network information:")
                for i in server.networks.get(u'public'):
                    build_details.append("Public IP: " + i)
                #build_details.append("Public IP: " + server.networks.get(u'public')[0])
                build_details.append("Private IP: " + server.networks.get(u'private')[0])
                
            except:
                print "Pyrax wait for build unsuccessful. Please reference your cloud panel for server IP's"

        print breakpoint
        server_info.write(breakpoint)
        for info in build_details:
            print info
            server_info.write(info + "\n")
        print breakpoint
        server_info.write(breakpoint)

        #server_info.write(build_details)
        #print build_details

        build_queue.task_done()
        print "%s has finished, ending thread" % server_name
        

    except nexc.BadRequest as e:
        print "Bad image/flavor combination. Please try again. (wah wah wah)\n"
        exit()
Exemplo n.º 4
0
def listservers():
    '''Print out servers in account'''
    auth()
    fields = ['Name', 'DC', 'Status', 'Date Created', 'Server ID']
    servdata = []
    dfwservers = pyrax.connect_to_cloudservers(region='DFW')
    ordservers = pyrax.connect_to_cloudservers(region='ORD')
    for server in dfwservers.servers.list():
        servdata.append((server.name, 'DFW', server.status, server.created,
                server.id))
    for server in ordservers.servers.list():
        servdata.append((server.name, 'ORD', server.status, server.created,
                server.id))
    printhortable(fields, servdata)
Exemplo n.º 5
0
def master_up(key_name, credential_file="~/.rackspace_cloud_credentials",
              image=default_layout["master"]["image"],
              flavor=default_layout["master"]["flavor"],
              name=default_layout["master"]["hostname"],
              region="IAD"):
    '''
    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.connect_to_cloudservers(region=region)

    master = cs.servers.create(name, image, flavor, key_name=key_name)

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

    env.hosts = [master.accessIPv4]

    print(master.networks)

    print("Master IP: {}".format(master.accessIPv4))

    return master.accessIPv4
Exemplo n.º 6
0
def minions_up(key_name, credential_file="~/.rackspace_cloud_credentials",
               layout=default_layout, region="IAD"):
    '''
    Creates a specific build of machines and bootstraps salt.

    The credential file can be set via keyword credential_file, and defaults
    to ~/.rackspace_cloud_credentials
    '''

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

    # Shorthand
    cs = pyrax.connect_to_cloudservers(region=region)

    minions = []

    for minion in layout["minions"]:
        minions.append(cs.servers.create(minion["hostname"],
                                         minion["image"],
                                         minion["flavor"],
                                         key_name=key_name))

    # Make sure all the minions are done before we move on to fabric runs
    minions = [pyrax.utils.wait_for_build(minion, verbose=True)
               for minion in minions]

    env.hosts = [str(minion.accessIPv4) for minion in minions]
    print(env.hosts)
Exemplo n.º 7
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)
Exemplo n.º 8
0
 def test_connect_to_cloudservers(self):
     pyrax.cloudservers = None
     sav = pyrax.connect_to_cloudservers
     pyrax.connect_to_cloudservers = self.orig_connect_to_cloudservers
     pyrax.cloudservers = pyrax.connect_to_cloudservers()
     self.assertIsNotNone(pyrax.cloudservers)
     pyrax.connect_to_cloudservers = sav
Exemplo n.º 9
0
def check_region_pings(pool, region):
    """Check pings for all servers in a given region"""
    cs = pyrax.connect_to_cloudservers(region=region)
    log.debug("Checking pings for %s", region)
    servers = cs.servers.findall()
    log.debug("%3i servers in Region %s", len(servers), region)
    list(pool.map(check_ping, servers))
Exemplo n.º 10
0
def server_info(id, dc):
    cs = pyrax.connect_to_cloudservers(region=dc)
    server = cs.servers.get(id)
    print "Server Name: ", server.name
    print "Server ID: ", server.id
    print "Public IP: ", server.accessIPv4
    print "Private IP :", server.addresses['private'][0]['addr']
Exemplo n.º 11
0
    def __init__(self, task_kwargs=None, json_config_file=None):
        json_config_file = json_config_file or './deploy_settings.json'
        self.cloudservers = None
        self.settings = AttributeDict({})
        self.fabric_env_servers = []
        self.created_servers = []

        task_kwargs = task_kwargs or {}

        settings = self.read_settings_file(json_config_file)

        for key in self.SETTINGS_KEYS:
            try:
                self.settings[key] = task_kwargs[key]
            except KeyError:
                try:
                    self.settings[key] = environ[key]
                except KeyError:
                    try:
                        self.settings[key] = settings[key]
                    except KeyError:
                        pass

        self.settings.server_count = int(self.settings.server_count)
        self.settings.setdefault('ssh_user', 'root')
        self.settings.setdefault('git_branch', 'master')
        self.ensure_settings('rackspace_username', 'rackspace_apikey')

        pyrax.set_setting('identity_type', 'rackspace')
        pyrax.set_credentials(
            self.settings.rackspace_username,
            self.settings.rackspace_apikey)
        self.cloudservers = pyrax.connect_to_cloudservers()
        self.loadbalancers = pyrax.connect_to_cloud_loadbalancers()
Exemplo n.º 12
0
 def test_connect_to_cloudservers(self):
     pyrax.cloudservers = None
     sav = pyrax.connect_to_cloudservers
     pyrax.connect_to_cloudservers = self.orig_connect_to_cloudservers
     pyrax.cloudservers = pyrax.connect_to_cloudservers()
     self.assertIsNotNone(pyrax.cloudservers)
     pyrax.connect_to_cloudservers = sav
Exemplo n.º 13
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)
Exemplo n.º 14
0
def server_selection():
    #Displays a list of all available images, allows the user to select one, and performs input validation.
    imgs = pyrax.connect_to_cloudservers(region=region).images.list()
    count = 1
    for img in imgs:
        print count, ")", img.name
        count += 1
    total_count = len(imgs)
    while True:
        server = raw_input("\nSelect the server that you want to create by entering a number between 1 and %s.%s" % (total_count, prompt))
        try:
            server = int(server)
            if server < 1 or server > total_count:
                print "Selection out of range."
            else:
                break
        except ValueError:
            print "Not a valid selection."
    count = 1
    for img in imgs:
        if count != server:
            count += 1
        else:
            server_type = img.name
            server_id = img.id
            break
    return (server_type, server_id)
Exemplo n.º 15
0
def flv_selection():
    #Displays a list of all available flavors, allows the user to select one, and performs input validation.
    flvs = pyrax.connect_to_cloudservers(region=region).list_flavors()
    count = 1
    for flv in flvs:
        print count, ")", flv.name
        count += 1
    total_count = len(flvs)
    while True:
        server_flv = raw_input("\nSelect the server that you want to create by entering a number between 1 and %s.%s" % (total_count, prompt))
        try:
            server_flv = int(server_flv)
            if server_flv < 1 or server_flv > total_count:
                print "Selection out of range."
            else:
                break
        except ValueError:
            print "Not a valid selection."
    count = 1
    for flv in flvs:
        if count != server_flv:
            count += 1
        else:
            server_flv_type = flv.name
            server_flv_id = flv.id
            break
    return (server_flv_type, server_flv_id)
Exemplo n.º 16
0
def connect_to_rackspace(region, access_key_id, secret_access_key):
    """ returns a connection object to Rackspace  """
    pyrax.set_setting('identity_type', 'rackspace')
    pyrax.set_default_region(region)
    pyrax.set_credentials(access_key_id, secret_access_key)
    nova = pyrax.connect_to_cloudservers(region=region)
    return nova
Exemplo n.º 17
0
def sync_vagrant_rackspace_environment():
    pyrax_login()

    # iterate over all of the cloud servers to identify those that are
    # already running.
    # https://github.com/rackspace/pyrax/blob/master/docs/cloud_servers.md
    cs = pyrax.connect_to_cloudservers(region="ORD")
    for server in cs.servers.list():

        # if the cloud server is already running and managed by
        # vagrant (in env.hosts), then let's make sure the id file is
        # set correctly
        server_configured = False
        filename = vagrant_id_filename(server.name, "rackspace")
        if server.name in env.hosts:
            if os.path.exists(filename):
                with open(filename, 'r') as stream:
                    if server.id == stream.read():
                        server_configured = True
        
        # if the server is not configured on the local machine, just
        # need to create the file with the server.id in it
        if server.name in env.hosts and not server_configured:
            if not os.path.exists(os.path.dirname(filename)):
                local("mkdir -p %s" % os.path.dirname(filename))
            with open(filename, 'w') as stream:
                stream.write(server.id)
Exemplo n.º 18
0
def delete_rax(args):
    """Function for deleting CloudServers"""
    for region in pyrax.identity.services.compute.regions:
        cs = pyrax.connect_to_cloudservers(region=region)
        servers = cs.servers.list(search_opts=dict(name='^%s' % args.match_re))
        for server in servers:
            prompt_and_delete(server, 'Delete matching %s? [y/n]: ' % server,
                              args.assumeyes)
Exemplo n.º 19
0
def list_servers(dc):
    cs = pyrax.connect_to_cloudservers(region=dc)
    for server in cs.servers.list():
        print "Server Name: ", server.name
        print "Server Status: ", server.status
        print "Server ID: ", server.id
        print "Public IP: ", server.accessIPv4
        print "Private IP: ", server.addresses['private'][0]['addr']
Exemplo n.º 20
0
 def _connect_to_rackspace(self):
     """ returns a connection object to Rackspace  """
     pyrax.set_setting('identity_type', 'rackspace')
     pyrax.set_default_region(self.state.region)
     pyrax.set_credentials(self.config.access_key_id,
                           self.config.secret_access_key)
     nova = pyrax.connect_to_cloudservers(region=self.state.region)
     return nova
Exemplo n.º 21
0
 def _connect_to_rackspace(self):
     """ returns a connection object to Rackspace  """
     pyrax.set_setting('identity_type', 'rackspace')
     pyrax.set_default_region(self.state.region)
     pyrax.set_credentials(self.config.access_key_id,
                           self.config.secret_access_key)
     nova = pyrax.connect_to_cloudservers(region=self.state.region)
     return nova
Exemplo n.º 22
0
def raxCreateServer(dc, imgIDToUse, flvrIDToUse, svrBaseName, numServers):
    raxCldSvr = pyrax.connect_to_cloudservers(region=dc)
    serverImgs = raxCldSvr.images.list()
    svrsCreated = {}  # Dictionary to hold info on the servers that get created
    completed = []  # Array to hold the servers that complete creation
    if raxArgs.interactive:
        for img in sorted(serverImgs, key=lambda serverImgs: serverImgs.name):
            print img.name, " || ID:", img.id
        imgIDToUse = raw_input('ID of image to use: ')
        imgNameToUse = [img.name for img in serverImgs if img.id == imgIDToUse][0]
        #print str(imgToUse)
        serverFlvrs = raxCldSvr.flavors.list()
        for flvr in serverFlvrs:
            print "Name: " + flvr.name + " || ID:" + flvr.id
        flvrIDToUse = raw_input('ID of flavor to use: ')
        print 'Using ' + bcolors.OKBLUE + imgNameToUse + bcolors.ENDC
        try:
            numServers = int(raxArgs.numServers)
        except (ValueError, TypeError):
            numServers = 3
        if (raxArgs.svrBaseName is None):
            svrBaseName = raw_input('What is the server base name to use: ')
        else:
            svrBaseName = raxArgs.svrBaseName
    print 'Creating a new server from image' \
        ' ' + bcolors.OKBLUE + imgIDToUse + bcolors.ENDC + ' in ' + \
        bcolors.WARNING + dc + bcolors.ENDC + '.'
    print 'Creating ' + str(numServers) + ' servers.'
    print 'Server name will begin with ' + svrBaseName

    for i in xrange(0, numServers):
        svrName = '%s%s' % (svrBaseName, i)
        svrsCreated[svrName] = raxCldSvr.servers.create(svrName, imgIDToUse,
                                                        flvrIDToUse)
        print "Created server: %s" % svrName
    sys.stdout.write("Waiting for builds to complete")
    sys.stdout.flush()
    while len(completed) < numServers:
        sys.stdout.write('.')
        sys.stdout.flush()
        time.sleep(30)
        for name, server in svrsCreated.iteritems():
            if name in completed:
                continue
            server.get()
            if server.status in ['ACTIVE', 'ERROR', 'UNKNOWN']:
                sys.stdout.write("\n")
                print '======================================='
                print 'Name: %s' % server.name
                if (server.status == 'ERROR'):
                    print 'Status: %s %s %s' % bcolors.FAIL, server.status, \
                        bcolors.ENDC
                else:
                    print 'Status: %s' % server.status
                print 'ID: %s' % server.id
                print 'Networks: %s' % server.networks
                print 'Password: %s' % server.adminPass
                completed.append(name)
Exemplo n.º 23
0
def connect_to_rackspace(region,
                         access_key_id,
                         secret_access_key):
    """ returns a connection object to Rackspace  """
    pyrax.set_setting('identity_type', 'rackspace')
    pyrax.set_default_region(region)
    pyrax.set_credentials(access_key_id, secret_access_key)
    nova = pyrax.connect_to_cloudservers(region=region)
    return nova
Exemplo n.º 24
0
def home(request):
    pyrax.set_credentials(request.user.rax_username, request.user.rax_api_key)

    cs_dfw = pyrax.connect_to_cloudservers(region="DFW")
    cs_ord = pyrax.connect_to_cloudservers(region="ORD")

    dfw_servers = cs_dfw.servers.list()
    ord_servers = cs_ord.servers.list()
    all_servers = dfw_servers + ord_servers

    html = "<html><body>"

    for server in all_servers:
        html += "Server: %s <br/>" % server.name

    html += "</body></html>"

    return HttpResponse(html)
Exemplo n.º 25
0
def connect_to_rackspace():
    """ returns a connection object to Rackspace  """
    import pyrax

    pyrax.set_setting('identity_type', env.os_auth_system)
    pyrax.set_default_region(env.os_region_name)
    pyrax.set_credentials(env.os_username, env.os_password)
    nova = pyrax.connect_to_cloudservers(region=env.os_region_name)
    return nova
Exemplo n.º 26
0
def main():
    """Parse arguments, take snap, and trigger deletion of oldest autosnap."""
    parser = argparse.ArgumentParser(description='auto-rotate image snapshots')
    parser.add_argument('user',
                        metavar='username',
                        type=str,
                        help='Username for account')
    parser.add_argument('key',
                        metavar='apikey',
                        type=str,
                        help='API key for account')
    parser.add_argument('server',
                        metavar='server',
                        type=str,
                        help='UUID of server to autosnap')
    parser.add_argument('retention',
                        metavar='retention',
                        type=int,
                        help='Minimum number of autosnaps to retain')
    parser.add_argument('region',
                        metavar='region',
                        type=str,
                        help='Region of Server (DFW, ORD, LON)',
                        choices=['DFW', 'ORD', 'LON'])

    args = parser.parse_args()

    pyrax.set_setting("identity_type", "rackspace")
    pyrax.set_credentials(args.user, args.key)

    #Set up a cloud object based on region specified by user
    if args.region == "LON":
        cloud = pyrax.cloudservers
    else:
        cloud = pyrax.connect_to_cloudservers(region=args.region)

    server = cloud.servers.get(args.server)
    images = cloud.images.list()

    snap_list = []

    snap_name = server.name + "-autosnap-" + args.server

    #Search for previous autosnaps of server and append to a list
    for i in images:
        search_name = server.name + "-autosnap-" + server.id
        if i.name == search_name:
            snap_list.append(i)

    #Delete oldest snapshot(s) if retention < num images
    while args.retention < len(snap_list):
        snap_list = delete_old(snap_list, cloud)

    #Create snapshot if retention >= number of previous autosnaps
    if args.retention >= len(snap_list):
        server.create_image(snap_name)
Exemplo n.º 27
0
 def __init__(self, *args, **kwargs):
     super(CloudBlockStorageVolume, self).__init__(*args, **kwargs)
     try:
         region = self.manager.api.region_name
         self._nova_volumes = pyrax.connect_to_cloudservers(region).volumes
     except AttributeError:
         # This will happen in unit testing, where the full pyrax
         # namespace is not exposed. In that situation, there is
         # no need for the reference anyway
         pass
Exemplo n.º 28
0
 def __init__(self, *args, **kwargs):
     super(CloudBlockStorageVolume, self).__init__(*args, **kwargs)
     try:
         region = self.manager.api.region_name
         self._nova_volumes = pyrax.connect_to_cloudservers(region).volumes
     except AttributeError:
         # This will happen in unit testing, where the full pyrax
         # namespace is not exposed. In that situation, there is
         # no need for the reference anyway
         pass
Exemplo n.º 29
0
def delete_rax_keypair(args):
    """Function for deleting Rackspace Key pairs"""
    print("--- Cleaning Key Pairs matching '%s'" % args.match_re)
    for region in pyrax.identity.services.compute.regions:
        cs = pyrax.connect_to_cloudservers(region=region)
        for keypair in cs.keypairs.list():
            if re.search(args.match_re, keypair.name):
                prompt_and_delete(keypair,
                                  'Delete matching %s? [y/n]: ' % keypair,
                                  args.assumeyes)
Exemplo n.º 30
0
def delete_rax(args):
    """Function for deleting CloudServers"""
    print("--- Cleaning CloudServers matching '%s'" % args.match_re)
    search_opts = dict(name='^%s' % args.match_re)
    for region in pyrax.identity.services.compute.regions:
        cs = pyrax.connect_to_cloudservers(region=region)
        servers = rax_list_iterator(cs.servers, search_opts=search_opts)
        for server in servers:
            prompt_and_delete(server, 'Delete matching %s? [y/n]: ' % server,
                              args.assumeyes)
Exemplo n.º 31
0
def delete_rax_keypair(args):
    """Function for deleting Rackspace Key pairs"""
    print ("--- Cleaning Key Pairs matching '%s'" % args.match_re)
    for region in pyrax.identity.services.compute.regions:
        cs = pyrax.connect_to_cloudservers(region=region)
        for keypair in cs.keypairs.list():
            if re.search(args.match_re, keypair.name):
                prompt_and_delete(keypair,
                                  'Delete matching %s? [y/n]: ' % keypair,
                                  args.assumeyes)
Exemplo n.º 32
0
def auth():
  username = str(request.form['username'])
  apikey   = str(request.form['apikey'])  

  try: 
    pyrax.settings.set('identity_type', 'rackspace')
    pyrax.set_credentials(username, apikey)
  except exc.AuthenticationFailed:
    return redirect('/login')

  cloudservers_ord = pyrax.connect_to_cloudservers(region="ORD")
  cloudservers_dfw = pyrax.connect_to_cloudservers(region="DFW")
  cloudservers_iad = pyrax.connect_to_cloudservers(region="IAD")

  cloudservers = cloudservers_ord.list() + cloudservers_dfw.list() + cloudservers_iad.list()
  return render_template('cloud_servers.html',
    title = 'Cloud Servers',
    username = username,
    servers = cloudservers)
Exemplo n.º 33
0
def _list(regions):
    groups = collections.defaultdict(list)
    hostvars = collections.defaultdict(dict)
    images = {}

    # Go through all the regions looking for servers
    for region in regions:
        # Connect to the region
        cs = pyrax.connect_to_cloudservers(region=region)
        for server in cs.servers.list():
            # Create a group on region
            groups[region].append(server.name)

            # Check if group metadata key in servers' metadata
            group = server.metadata.get('group')
            if group:
                groups[group].append(server.name)

            for extra_group in server.metadata.get('groups', '').split(','):
                if extra_group:
                    groups[extra_group].append(server.name)

            # Add host metadata
            for key, value in to_dict(server).items():
                hostvars[server.name][key] = value

            hostvars[server.name]['rax_region'] = region

            for key, value in server.metadata.iteritems():
                prefix = os.getenv('RAX_META_PREFIX', 'meta')
                groups['%s_%s_%s' % (prefix, key, value)].append(server.name)

            groups['instance-%s' % server.id].append(server.name)
            groups['flavor-%s' % server.flavor['id']].append(server.name)
            try:
                imagegroup = 'image-%s' % images[server.image['id']]
                groups[imagegroup].append(server.name)
                groups['image-%s' % server.image['id']].append(server.name)
            except KeyError:
                try:
                    image = cs.images.get(server.image['id'])
                except cs.exceptions.NotFound:
                    groups['image-%s' % server.image['id']].append(server.name)
                else:
                    images[image.id] = image.human_id
                    groups['image-%s' % image.human_id].append(server.name)
                    groups['image-%s' % server.image['id']].append(server.name)

            # And finally, add an IP address
            hostvars[server.name]['ansible_ssh_host'] = server.accessIPv4

    if hostvars:
        groups['_meta'] = {'hostvars': hostvars}
    print(json.dumps(groups, sort_keys=True, indent=4))
Exemplo n.º 34
0
def _get_driver(driver_type, region='ORD'):
    """
    Returns the appropriate diver for the specified rackspace product.

    Available options include::
        lb: Cloud Load Balancers
        db: Cloud Databases
        dns: Cloud DNS
        bs: Cloud Block Storage
        mon: Cloud Monitoring
        net: Cloud Networks
        cf: Cloud Files
        cs: Cloud Servers

    :param driver_type: A str or unicode object for the appropriate type of
    driver above.
    :param region: A str or unicode object specify which region the driver
    should be initialized for.
    :return: A driver object initialized to the specified region
    :raise TypeError:
    :raise KeyError: If no valid drivers are found
    """
    _auth()
    if not isinstance(driver_type, six.string_types):
        raise TypeError("driver_type must be str or unicode object")
    if not isinstance(region, six.string_types):
        raise TypeError("region must be str or unicode object")
    region = region.upper()

    if driver_type == "lb":
        return pyrax.connect_to_cloud_loadbalancers(region)

    if driver_type == "db":
        return pyrax.connect_to_cloud_databases(region)

    if driver_type == "dns":
        return pyrax.connect_to_cloud_dns()

    if driver_type == "bs":
        return pyrax.connect_to_cloud_blockstorage(region)

    if driver_type == "mon":
        return pyrax.connect_to_cloud_monitoring(region)

    if driver_type == "net":
        return pyrax.connect_to_cloud_networks(region)

    if driver_type == 'cf':
        return pyrax.connect_to_cloudfiles(region)

    if driver_type == 'cs':
        return pyrax.connect_to_cloudservers(region)

    raise KeyError(u"No Driver found by: {}".format(driver_type))
Exemplo n.º 35
0
def _get_driver(driver_type, region='ORD'):
    """
    Returns the appropriate diver for the specified rackspace product.

    Available options include::
        lb: Cloud Load Balancers
        db: Cloud Databases
        dns: Cloud DNS
        bs: Cloud Block Storage
        mon: Cloud Monitoring
        net: Cloud Networks
        cf: Cloud Files
        cs: Cloud Servers

    :param driver_type: A str or unicode object for the appropriate type of
    driver above.
    :param region: A str or unicode object specify which region the driver
    should be initialized for.
    :return: A driver object initialized to the specified region
    :raise TypeError:
    :raise KeyError: If no valid drivers are found
    """
    _auth()
    if not isinstance(driver_type, six.string_types):
        raise TypeError("driver_type must be str or unicode object")
    if not isinstance(region, six.string_types):
        raise TypeError("region must be str or unicode object")
    region = region.upper()

    if driver_type == "lb":
        return pyrax.connect_to_cloud_loadbalancers(region)

    if driver_type == "db":
        return pyrax.connect_to_cloud_databases(region)

    if driver_type == "dns":
        return pyrax.connect_to_cloud_dns()

    if driver_type == "bs":
        return pyrax.connect_to_cloud_blockstorage(region)

    if driver_type == "mon":
        return pyrax.connect_to_cloud_monitoring(region)

    if driver_type == "net":
        return pyrax.connect_to_cloud_networks(region)

    if driver_type == 'cf':
        return pyrax.connect_to_cloudfiles(region)

    if driver_type == 'cs':
        return pyrax.connect_to_cloudservers(region)

    raise KeyError(u"No Driver found by: {}".format(driver_type))
Exemplo n.º 36
0
 def test_set_http_debug(self):
     pyrax.cloudservers = None
     sav = pyrax.connect_to_cloudservers
     pyrax.connect_to_cloudservers = self.orig_connect_to_cloudservers
     pyrax.cloudservers = pyrax.connect_to_cloudservers()
     pyrax.cloudservers.http_log_debug = False
     pyrax.set_http_debug(True)
     self.assertTrue(pyrax.cloudservers.http_log_debug)
     pyrax.set_http_debug(False)
     self.assertFalse(pyrax.cloudservers.http_log_debug)
     pyrax.connect_to_cloudservers = sav
Exemplo n.º 37
0
def _list(regions):
    groups = collections.defaultdict(list)
    hostvars = collections.defaultdict(dict)
    images = {}

    # Go through all the regions looking for servers
    for region in regions:
        # Connect to the region
        cs = pyrax.connect_to_cloudservers(region=region)
        for server in cs.servers.list():
            # Create a group on region
            groups[region].append(server.name)

            # Check if group metadata key in servers' metadata
            group = server.metadata.get('group')
            if group:
                groups[group].append(server.name)

            for extra_group in server.metadata.get('groups', '').split(','):
                if extra_group:
                    groups[extra_group].append(server.name)

            # Add host metadata
            for key, value in to_dict(server).items():
                hostvars[server.name][key] = value

            hostvars[server.name]['rax_region'] = region

            for key, value in server.metadata.iteritems():
                prefix = os.getenv('RAX_META_PREFIX', 'meta')
                groups['%s_%s_%s' % (prefix, key, value)].append(server.name)

            groups['instance-%s' % server.id].append(server.name)
            groups['flavor-%s' % server.flavor['id']].append(server.name)
            try:
                imagegroup = 'image-%s' % images[server.image['id']]
                groups[imagegroup].append(server.name)
                groups['image-%s' % server.image['id']].append(server.name)
            except KeyError:
                try:
                    image = cs.images.get(server.image['id'])
                except cs.exceptions.NotFound:
                    groups['image-%s' % server.image['id']].append(server.name)
                else:
                    images[image.id] = image.human_id
                    groups['image-%s' % image.human_id].append(server.name)
                    groups['image-%s' % server.image['id']].append(server.name)

            # And finally, add an IP address
            hostvars[server.name]['ansible_ssh_host'] = server.accessIPv4

    if hostvars:
        groups['_meta'] = {'hostvars': hostvars}
    print(json.dumps(groups, sort_keys=True, indent=4))
Exemplo n.º 38
0
 def test_set_http_debug(self):
     pyrax.cloudservers = None
     sav = pyrax.connect_to_cloudservers
     pyrax.connect_to_cloudservers = self.orig_connect_to_cloudservers
     pyrax.cloudservers = pyrax.connect_to_cloudservers()
     pyrax.cloudservers.http_log_debug = False
     pyrax.set_http_debug(True)
     self.assertTrue(pyrax.cloudservers.http_log_debug)
     pyrax.set_http_debug(False)
     self.assertFalse(pyrax.cloudservers.http_log_debug)
     pyrax.connect_to_cloudservers = sav
Exemplo n.º 39
0
def delete_rax(args):
    """Function for deleting CloudServers"""
    print ("--- Cleaning CloudServers matching '%s'" % args.match_re)
    search_opts = dict(name='^%s' % args.match_re)
    for region in pyrax.identity.services.compute.regions:
        cs = pyrax.connect_to_cloudservers(region=region)
        servers = rax_list_iterator(cs.servers, search_opts=search_opts)
        for server in servers:
            prompt_and_delete(server,
                              'Delete matching %s? [y/n]: ' % server,
                              args.assumeyes)
Exemplo n.º 40
0
def create_server(name, img_id, flv_id, dc):
    cs = pyrax.connect_to_cloudservers(region=dc)
    new_server = cs.servers.create(name, img_id, flv_id)
    print "-"*30, "\nName:", new_server.name, "\nID:", new_server.id, \
    "\nStatus:", new_server.status, "\nAdmin Password:"******"IPv6:", new_server.networks["public"][1], \
        "\nIPv4:", new_server.networks["public"][0], \
        "\nPrivate IP", new_server.networks["private"][0]
Exemplo n.º 41
0
 def _create_client(self, clt_class, url, public=True, special=False):
     """
     Creates a client instance for the service.
     """
     if self.service == "compute" and not special:
         # Novaclient requires different parameters.
         client = pyrax.connect_to_cloudservers(region=self.region,
                 context=self.identity, verify_ssl=self.verify_ssl)
         client.identity = self.identity
     else:
         client = clt_class(self.identity, region_name=self.region,
                 management_url=url, verify_ssl=self.verify_ssl)
     return client
Exemplo n.º 42
0
 def _create_client(self, clt_class, url, public=True, special=False):
     """
     Creates a client instance for the service.
     """
     if self.service == "compute" and not special:
         # Novaclient requires different parameters.
         client = pyrax.connect_to_cloudservers(region=self.region,
                 context=self.identity, verify_ssl=self.verify_ssl)
         client.identity = self.identity
     else:
         client = clt_class(self.identity, region_name=self.region,
                 management_url=url, verify_ssl=self.verify_ssl)
     return client
Exemplo n.º 43
0
 def __init__(self, *args, **kwargs):
     super(CloudBlockStorageVolume, self).__init__(*args, **kwargs)
     try:
         region = self.manager.api.region_name
         self._nova_volumes = pyrax.connect_to_cloudservers(region).volumes
     except AttributeError:
         # This will happen in unit testing, where the full pyrax
         # namespace is not exposed. In that situation, there is
         # no need for the reference anyway
         pass
     self._snapshot_manager = BaseManager(self.manager.api,
             resource_class=CloudBlockStorageSnapshot,
             response_key="snapshot", uri_base="snapshots")
Exemplo n.º 44
0
def _create_rackspace_connection(creds, region):
    """
    Connect to a Rackspace region with the given credentials.

    :param creds: a dict containing a Rackspace username and api_key
    :region: the name of a Rackspace region, such as "ord"
    :rtype: a connected :class:`~novaclient.v1_1.client.Client`
    :raises EnvironmentError: if no credentials are provided
    """
    if not creds:
        raise EnvironmentError('No credentials provided')
    pyrax.set_setting('identity_type', creds['identity_type'])
    pyrax.set_credentials(creds['username'], creds['api_key'])
    return pyrax.connect_to_cloudservers(region.upper())
Exemplo n.º 45
0
def host(regions, hostname):
    hostvars = {}

    for region in regions:
        # Connect to the region
        cs = pyrax.connect_to_cloudservers(region=region)
        for server in cs.servers.list():
            if server.name == hostname:
                for key, value in to_dict(server).items():
                    hostvars[key] = value

                # And finally, add an IP address
                hostvars['ansible_ssh_host'] = server.accessIPv4
    print(json.dumps(hostvars, sort_keys=True, indent=4))
Exemplo n.º 46
0
def _list(regions):
    groups = collections.defaultdict(list)
    hostvars = collections.defaultdict(dict)

    # Go through all the regions looking for servers
    for region in regions:
        # Connect to the region
        cs = pyrax.connect_to_cloudservers(region=region)
        for server in cs.servers.list():
            # Create a group on region
            groups[region].append(server.name)

            # Check if group metadata key in servers' metadata
            try:
                group = server.metadata['group']
            except KeyError:
                pass
            else:
                # Create group if not exist and add the server
                groups[group].append(server.name)

            ansible_groups = server.metadata.get('ansible_groups', '')
            for group in ansible_groups.split(','):
                if group != '':
                    groups[group].append(server.name)

            # Add host metadata
            keys = [key for key in vars(server) if key not in ('manager', '_info')]
            for key in keys:
                # Extract value
                value = getattr(server, key)

                # Generate sanitized key
                key = 'rax_' + (re.sub("[^A-Za-z0-9\-]", "_", key)
                                  .lower()
                                  .lstrip('_'))
                hostvars[server.name][key] = value

            # And finally, add an IP address
            hostvars[server.name]['ansible_ssh_host'] = server.accessIPv4

    if hostvars:
        groups['_meta'] = {'hostvars': hostvars}
    print(json.dumps(groups, sort_keys=True, indent=4))
Exemplo n.º 47
0
    def _get_client(self):
        username = self.config['username']
        api_key = self.config['api_key']

        # Needs to be extracted to per-action
        region = self.config['region'].upper()

        pyrax.set_setting('identity_type', 'rackspace')
        pyrax.set_default_region(region)
        pyrax.set_credentials(username, api_key)

        debug = self.config.get('debug', False)
        if debug:
            pyrax.set_http_debug(True)

        pyrax.cloudservers = pyrax.connect_to_cloudservers(region=region)
        pyrax.cloud_loadbalancers = pyrax.connect_to_cloud_loadbalancers(region=region)
        pyrax.cloud_dns = pyrax.connect_to_cloud_dns(region=region)

        return pyrax
Exemplo n.º 48
0
 def _create_client(self, clt_class, url, public=True, special=False):
     """
     Creates a client instance for the service.
     """
     verify_ssl = pyrax.get_setting("verify_ssl")
     if not special and self.service == "object_store":
         # Swiftclient requires different parameters.
         client = pyrax.connect_to_cloudfiles(region=self.region,
                                              public=public,
                                              context=self.identity)
         client.identity = self.identity
     elif not special and self.service == "compute":
         # Novaclient also requires special handling.
         client = pyrax.connect_to_cloudservers(region=self.region,
                                                context=self.identity)
         client.identity = self.identity
     else:
         client = clt_class(self.identity,
                            region_name=self.region,
                            management_url=url,
                            verify_ssl=verify_ssl)
     return client
Exemplo n.º 49
0
def main():
    auth()

    #Creating cloudserver client
    cs = pyrax.connect_to_cloudservers(region="ORD")
    server_uuid = 'FILL_IN_YOUR_SERVER_UUID_HERE'
    image_name = "ROLLING_IMAGE_NAME"

    print "Looking for old images named %s" % image_name
    old_images = cs.images.findall(name=image_name)

    print "Finding server from uuid %s" % server_uuid
    server = cs.servers.get(server=server_uuid)

    print "Creating new image of %s named %s" % (server.name, image_name)
    image_uuid = create_image(server, image_name=image_name)

    print "New image created: %s" % image_uuid

    for image in old_images:
        print "Deleting Old Image: %s --  %s" % (image.name, image.id)
        cs.images.delete(image.id)
Exemplo n.º 50
0
def host(regions, hostname):
    hostvars = {}

    for region in regions:
        # Connect to the region
        cs = pyrax.connect_to_cloudservers(region=region)
        for server in cs.servers.list():
            if server.name == hostname:
                keys = [key for key in vars(server) if key not in ('manager', '_info')]
                for key in keys:
                    # Extract value
                    value = getattr(server, key)

                    # Generate sanitized key
                    key = 'rax_' + (re.sub("[^A-Za-z0-9\-]", "_", key)
                                      .lower()
                                      .lstrip("_"))
                    hostvars[key] = value

                # And finally, add an IP address
                hostvars['ansible_ssh_host'] = server.accessIPv4
    print(json.dumps(hostvars, sort_keys=True, indent=4))
Exemplo n.º 51
0
 def __init__(self, *args, **kwargs):
     super(CloudBlockStorageVolume, self).__init__(*args, **kwargs)
     region = self.manager.api.region_name
     context = self.manager.api.identity
     cs = pyrax.connect_to_cloudservers(region=region, context=context)
     self._nova_volumes = cs.volumes
Exemplo n.º 52
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()
Exemplo n.º 53
0
def launch_node(node_num=7):
    region = "iad"
    domain = "tmpnb.org"
    key_name = "team"
    
    pyrax.set_setting("identity_type", "rackspace")
    pyrax.set_credentials(os.environ["OS_USERNAME"], os.environ["OS_PASSWORD"])
    
    cs = pyrax.connect_to_cloudservers(region=region.upper())
    
    # My least favorite bug in pyrax - silent errors
    if(cs is None):
        raise Exception("Unable to connect to given region '{}'".format(region))
    
    # Get our base images
    images = cs.list_base_images()
    ubs = [image for image in images if "Ubuntu 14.04" in image.name]
    user_image = [image for image in ubs if "OnMetal" in image.name][0]
    proxy_image = [image for image in ubs if "PVHVM" in image.name][0]
    
    user_server_name, proxy_server_name = name_new_nodes(region.lower(),
                                                         node_num=node_num,
                                                         domain=domain)
    
    # Launch the servers
    proxy_server = cs.servers.create(proxy_server_name, image=proxy_image.id, flavor='performance2-15', key_name=key_name)
    user_server = cs.servers.create(user_server_name, image=user_image.id, flavor='onmetal-compute1', key_name=key_name)
    
    # Wait on them
    print("Waiting on Proxy server")
    proxy_server = pyrax.utils.wait_for_build(proxy_server, verbose=True)
    print("Waiting on Notebook User server")
    user_server = pyrax.utils.wait_for_build(user_server, verbose=True)
    
    # Making this in case we want some JSON
    node_layout = {
        'notebook_server': {
            'private': user_server.networks['private'][0],
            'public': user_server.networks['public'][0]
        },
        'proxy_server': {
            'public': proxy_server.networks['public'][0]
        }
    }
    
    inventory = '''[notebook]
{user_server_name} ansible_ssh_user=root ansible_ssh_host={notebook_server_public} configproxy_auth_token={token}

[proxy]
{proxy_server_name} ansible_ssh_user=root ansible_ssh_host={proxy_server_public} notebook_host={notebook_server_private}
'''.format(notebook_server_public=user_server.accessIPv4,
           notebook_server_private=user_server.networks['private'][0],
           proxy_server_public=proxy_server.accessIPv4,
           token=binascii.hexlify(os.urandom(24)),
           user_server_name=user_server_name,
           proxy_server_name=proxy_server_name,
    )
    
    print(inventory)
    
    pyrax.set_credentials(os.environ["OS_DNS_USERNAME"], os.environ["OS_DNS_PASSWORD"])
    
    dns = pyrax.cloud_dns.find(name=domain)
    dns.add_record({'type': 'A',
                    'name': proxy_server_name,
                    'ttl': 60*5,
                    'data': proxy_server.accessIPv4})
Exemplo n.º 54
0
def _list_into_cache(regions):
    groups = collections.defaultdict(list)
    hostvars = collections.defaultdict(dict)
    images = {}
    cbs_attachments = collections.defaultdict(dict)

    prefix = get_config(p, 'rax', 'meta_prefix', 'RAX_META_PREFIX', 'meta')

    try:
        # Ansible 2.3+
        networks = get_config(p,
                              'rax',
                              'access_network',
                              'RAX_ACCESS_NETWORK',
                              'public',
                              value_type='list')
    except TypeError:
        # Ansible 2.2.x and below
        # pylint: disable=unexpected-keyword-arg
        networks = get_config(p,
                              'rax',
                              'access_network',
                              'RAX_ACCESS_NETWORK',
                              'public',
                              islist=True)
    try:
        try:
            # Ansible 2.3+
            ip_versions = map(
                int,
                get_config(p,
                           'rax',
                           'access_ip_version',
                           'RAX_ACCESS_IP_VERSION',
                           4,
                           value_type='list'))
        except TypeError:
            # Ansible 2.2.x and below
            # pylint: disable=unexpected-keyword-arg
            ip_versions = map(
                int,
                get_config(p,
                           'rax',
                           'access_ip_version',
                           'RAX_ACCESS_IP_VERSION',
                           4,
                           islist=True))
    except:
        ip_versions = [4]
    else:
        ip_versions = [v for v in ip_versions if v in [4, 6]]
        if not ip_versions:
            ip_versions = [4]

    # Go through all the regions looking for servers
    for region in regions:
        # Connect to the region
        cs = pyrax.connect_to_cloudservers(region=region)
        if cs is None:
            warnings.warn(
                'Connecting to Rackspace region "%s" has caused Pyrax to '
                'return None. Is this a valid region?' % region,
                RuntimeWarning)
            continue
        for server in cs.servers.list():
            # Create a group on region
            groups[region].append(server.name)

            # Check if group metadata key in servers' metadata
            group = server.metadata.get('group')
            if group:
                groups[group].append(server.name)

            for extra_group in server.metadata.get('groups', '').split(','):
                if extra_group:
                    groups[extra_group].append(server.name)

            # Add host metadata
            for key, value in to_dict(server).items():
                hostvars[server.name][key] = value

            hostvars[server.name]['rax_region'] = region

            for key, value in iteritems(server.metadata):
                groups['%s_%s_%s' % (prefix, key, value)].append(server.name)

            groups['instance-%s' % server.id].append(server.name)
            groups['flavor-%s' % server.flavor['id']].append(server.name)

            # Handle boot from volume
            if not server.image:
                if not cbs_attachments[region]:
                    cbs = pyrax.connect_to_cloud_blockstorage(region)
                    for vol in cbs.list():
                        if boolean(vol.bootable, strict=False):
                            for attachment in vol.attachments:
                                metadata = vol.volume_image_metadata
                                server_id = attachment['server_id']
                                cbs_attachments[region][server_id] = {
                                    'id': metadata['image_id'],
                                    'name': slugify(metadata['image_name'])
                                }
                image = cbs_attachments[region].get(server.id)
                if image:
                    server.image = {'id': image['id']}
                    hostvars[server.name]['rax_image'] = server.image
                    hostvars[server.name]['rax_boot_source'] = 'volume'
                    images[image['id']] = image['name']
            else:
                hostvars[server.name]['rax_boot_source'] = 'local'

            try:
                imagegroup = 'image-%s' % images[server.image['id']]
                groups[imagegroup].append(server.name)
                groups['image-%s' % server.image['id']].append(server.name)
            except KeyError:
                try:
                    image = cs.images.get(server.image['id'])
                except cs.exceptions.NotFound:
                    groups['image-%s' % server.image['id']].append(server.name)
                else:
                    images[image.id] = image.human_id
                    groups['image-%s' % image.human_id].append(server.name)
                    groups['image-%s' % server.image['id']].append(server.name)

            # And finally, add an IP address
            ansible_ssh_host = None
            # use accessIPv[46] instead of looping address for 'public'
            for network_name in networks:
                if ansible_ssh_host:
                    break
                if network_name == 'public':
                    for version_name in ip_versions:
                        if ansible_ssh_host:
                            break
                        if version_name == 6 and server.accessIPv6:
                            ansible_ssh_host = server.accessIPv6
                        elif server.accessIPv4:
                            ansible_ssh_host = server.accessIPv4
                if not ansible_ssh_host:
                    addresses = server.addresses.get(network_name, [])
                    for address in addresses:
                        for version_name in ip_versions:
                            if ansible_ssh_host:
                                break
                            if address.get('version') == version_name:
                                ansible_ssh_host = address.get('addr')
                                break
            if ansible_ssh_host:
                hostvars[server.name]['ansible_ssh_host'] = ansible_ssh_host

    if hostvars:
        groups['_meta'] = {'hostvars': hostvars}

    with open(get_cache_file_path(regions), 'w') as cache_file:
        json.dump(groups, cache_file)
Exemplo n.º 55
0
                      help='The autoscale group config ID')
  parser.add_argument('--region', required=False, default='SYD',
                      help='The region to authenticate to')

  args = vars(parser.parse_args())

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

  # Using credentials file
  try:
    pyrax.set_credential_file(os.path.expanduser("~/.cloud_credentials"))
  except Exception, e:
    sys.stderr.write("Failed to authenticate: %s" % str(e))

  au = pyrax.connect_to_autoscale(region=args['region'])
  cs = pyrax.connect_to_cloudservers(region=args['region'])

  as_group = au.get(args['as_group']);

  snet_ips = []

  for s_id in as_group.get_state()['active']:
    try
      server = cs.servers.get(s_id)
      snet_ips.append(server.networks['private'][0])
    except:
      pass

  generate_lsync_config(snet_ips)

if __name__ == '__main__':
Exemplo n.º 56
0
def main():
    argument_spec = rax_argument_spec()
    argument_spec.update(
        dict(
            auto_increment=dict(default=True, type='bool'),
            boot_from_volume=dict(default=False, type='bool'),
            boot_volume=dict(type='str'),
            boot_volume_size=dict(type='int', default=100),
            boot_volume_terminate=dict(type='bool', default=False),
            config_drive=dict(default=False, type='bool'),
            count=dict(default=1, type='int'),
            count_offset=dict(default=1, type='int'),
            disk_config=dict(choices=['auto', 'manual']),
            exact_count=dict(default=False, type='bool'),
            extra_client_args=dict(type='dict', default={}),
            extra_create_args=dict(type='dict', default={}),
            files=dict(type='dict', default={}),
            flavor=dict(),
            group=dict(),
            image=dict(),
            instance_ids=dict(type='list'),
            key_name=dict(aliases=['keypair']),
            meta=dict(type='dict', default={}),
            name=dict(),
            networks=dict(type='list', default=['public', 'private']),
            service=dict(),
            state=dict(default='present', choices=['present', 'absent']),
            user_data=dict(no_log=True),
            wait=dict(default=False, type='bool'),
            wait_timeout=dict(default=300),
        ))

    module = AnsibleModule(
        argument_spec=argument_spec,
        required_together=rax_required_together(),
    )

    if not HAS_PYRAX:
        module.fail_json(msg='pyrax is required for this module')

    service = module.params.get('service')

    if service is not None:
        module.fail_json(msg='The "service" attribute has been deprecated, '
                         'please remove "service: cloudservers" from your '
                         'playbook pertaining to the "rax" module')

    auto_increment = module.params.get('auto_increment')
    boot_from_volume = module.params.get('boot_from_volume')
    boot_volume = module.params.get('boot_volume')
    boot_volume_size = module.params.get('boot_volume_size')
    boot_volume_terminate = module.params.get('boot_volume_terminate')
    config_drive = module.params.get('config_drive')
    count = module.params.get('count')
    count_offset = module.params.get('count_offset')
    disk_config = module.params.get('disk_config')
    if disk_config:
        disk_config = disk_config.upper()
    exact_count = module.params.get('exact_count', False)
    extra_client_args = module.params.get('extra_client_args')
    extra_create_args = module.params.get('extra_create_args')
    files = module.params.get('files')
    flavor = module.params.get('flavor')
    group = module.params.get('group')
    image = module.params.get('image')
    instance_ids = module.params.get('instance_ids')
    key_name = module.params.get('key_name')
    meta = module.params.get('meta')
    name = module.params.get('name')
    networks = module.params.get('networks')
    state = module.params.get('state')
    user_data = module.params.get('user_data')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))

    setup_rax_module(module, pyrax)

    if extra_client_args:
        pyrax.cloudservers = pyrax.connect_to_cloudservers(
            region=pyrax.cloudservers.client.region_name, **extra_client_args)
        client = pyrax.cloudservers.client
        if 'bypass_url' in extra_client_args:
            client.management_url = extra_client_args['bypass_url']

    if pyrax.cloudservers is None:
        module.fail_json(msg='Failed to instantiate client. This '
                         'typically indicates an invalid region or an '
                         'incorrectly capitalized region name.')

    cloudservers(module,
                 state=state,
                 name=name,
                 flavor=flavor,
                 image=image,
                 meta=meta,
                 key_name=key_name,
                 files=files,
                 wait=wait,
                 wait_timeout=wait_timeout,
                 disk_config=disk_config,
                 count=count,
                 group=group,
                 instance_ids=instance_ids,
                 exact_count=exact_count,
                 networks=networks,
                 count_offset=count_offset,
                 auto_increment=auto_increment,
                 extra_create_args=extra_create_args,
                 user_data=user_data,
                 config_drive=config_drive,
                 boot_from_volume=boot_from_volume,
                 boot_volume=boot_volume,
                 boot_volume_size=boot_volume_size,
                 boot_volume_terminate=boot_volume_terminate)