Exemplo n.º 1
0
def test_authenticate_success():
    cs = cloudservers.CloudServers("username", "apikey")
    auth_response = httplib2.Response({
        'status':
        204,
        'x-server-management-url':
        'https://servers.api.rackspacecloud.com/v1.0/443470',
        'x-auth-token':
        '1b751d74-de0c-46ae-84f0-915744b582d1',
    })
    mock_request = mock.Mock(return_value=(auth_response, None))

    @mock.patch.object(httplib2.Http, "request", mock_request)
    def test_auth_call():
        cs.client.authenticate()
        mock_request.assert_called_with(cs.client.AUTH_URL,
                                        'GET',
                                        headers={
                                            'X-Auth-User': '******',
                                            'X-Auth-Key': 'apikey',
                                            'User-Agent': cs.client.USER_AGENT
                                        })
        assert_equal(cs.client.management_url,
                     auth_response['x-server-management-url'])
        assert_equal(cs.client.auth_token, auth_response['x-auth-token'])

    test_auth_call()
Exemplo n.º 2
0
def test_auth_manual():
    cs = cloudservers.CloudServers("username", "password")

    @mock.patch.object(cs.client, 'authenticate')
    def test_auth_call(m):
        cs.authenticate()
        m.assert_called()

    test_auth_call()
Exemplo n.º 3
0
def test_authenticate_failure():
    cs = cloudservers.CloudServers("username", "apikey")
    auth_response = httplib2.Response({'status': 401})
    mock_request = mock.Mock(return_value=(auth_response, None))

    @mock.patch.object(httplib2.Http, "request", mock_request)
    def test_auth_call():
        assert_raises(cloudservers.Unauthorized, cs.client.authenticate)

    test_auth_call()
Exemplo n.º 4
0
def test_auth_automatic():
    client = cloudservers.CloudServers("username", "apikey").client
    client.management_url = ''
    mock_request = mock.Mock(return_value=(None, None))

    @mock.patch.object(client, 'request', mock_request)
    @mock.patch.object(client, 'authenticate')
    def test_auth_call(m):
        client.get('/')
        m.assert_called()
        mock_request.assert_called()

    test_auth_call()
Exemplo n.º 5
0
def shutdown_and_destroy_workers():
    """ Use Fabric to shut down celery on workers then destroy them """

    cloudservers = cloudservers.CloudServers(settings.CLOUDSERVERS_USERNAME,
                                             settings.CLOUDSERVERS_API_KEY)
    name = settings.WORKER_PREFIX
    servers = cloudservers.servers.list()
    """ Use Fabric to shut down celery daemon on the workers """
    for s in servers:
        if name in s.name:
            print "Shutting down celery workers on %s - %s" % (s.name,
                                                               s.private_ip)
            subprocess.call("/usr/local/bin/fab \
                            -f /opt/codebase/auto-scale/fabfile.py \
                            -H " + s.private_ip + " stop_celery_worker",
                            shell=True)

    # Sleep for 600 seconds
    # Allows time for all celery workers to finish processing their tasks
    sleep(600)
    """ Delete the servers from Rackspace Cloud """
    i = 0
    for s in servers:
        if name in s.name:
            print "DELETING server: %-20s" % (s.name)
            s.delete()
            i += 1
    """ Restart networking to clear iptables policies """
    if i > 0:
        print "Restarting networking to clear iptables policies"
        subprocess.call(["/etc/init.d/networking", "restart"])
    else:
        print "No workers found or deleted. Not restarting networking."
    """ Delete the temporary file """
    try:
        os.remove(settings.AUTOSCALE_TMP_FILE)
    except:
        print "File %s does not exist." % (settings.AUTOSCALE_TMP_FILE)

    return True
Exemplo n.º 6
0
def deploy_worker(thread_num):
    cs = cloudservers.CloudServers(settings.CLOUDSERVERS_USERNAME,
                                   settings.CLOUDSERVERS_API_KEY)
    s = _create_server(cs, settings.CLOUDSERVERS_IMAGE_TEMPLATE, 256)

    _wait_for_server(cs, s, with_url_ping=False)

    print '%d: %s: Server IP is %s (private: %s)' % (thread_num,
                                                     s.id,
                                                     s.public_ip,
                                                     s.private_ip)

    # small delay to allow the server to fully boot up
    sleep(60)

    # set fabric's env.host_string to the IP address of the new server
    env.host_string = s.private_ip

    # add iptables rules on master server so workers can connect to it
    _set_up_iptables_locally(cs, s)
    # rsync the code base to the new worker node
    _rsync_codebase_to_worker(cs, s)

    # reboot the newly created worker node
    print '%d: Rebooting: %s (id: %s)' % (thread_num, s.private_ip, s.id)
    _reboot_server(cs, s)
    sleep(90)

    # start the celery daemon on the new worker node
    print '%d: Starting celery worker: %s (id: %s)' % (thread_num,
                                                       s.private_ip,
                                                       s.id)
    env.host_string = s.private_ip
    _start_celery_worker()

    sleep(30)
Exemplo n.º 7
0
import cloudservers
import ConfigParser

# get our config from the config file
config = ConfigParser.ConfigParser()
config.read("./cf.ini")

USERNAME = config.get("auth", "username")
AUTH_URL = config.get("auth", "url")
API_KEY = config.get("auth", "key")
# Server ID to resize
SERVER = '10003688'
FLAVOR = '2'

# create connection object
cnx = cloudservers.CloudServers(USERNAME, API_KEY, auth_url=AUTH_URL)

# get a list of servers on the account
servers = cnx.servers.list()
for server in servers:
    print server

# get a list of available flavors
flavors = cnx.flavors.list()
for flavor in flavors:
    print flavor

# get a list of server IDs
for server in servers:
    print server.id