Class: AvstCloud::RackspaceConnection

Inherits:
CloudConnection show all
Defined in:
lib/avst-cloud/rackspace_connection.rb

Constant Summary

Constants inherited from CloudConnection

CloudConnection::UNIMPLEMENTED

Instance Attribute Summary collapse

Attributes inherited from CloudConnection

#connection, #provider, #provider_pass, #provider_user

Instance Method Summary collapse

Methods included from Logging

included, #logger, #logger=

Constructor Details

#initialize(provider_access_user, provider_access_pass, region = :lon) ⇒ RackspaceConnection

Returns a new instance of RackspaceConnection.



23
24
25
26
# File 'lib/avst-cloud/rackspace_connection.rb', line 23

def initialize(provider_access_user, provider_access_pass, region=:lon)
    super('rackspace',provider_access_user, provider_access_pass)
    @region = region
end

Instance Attribute Details

#regionObject

Returns the value of attribute region.



21
22
23
# File 'lib/avst-cloud/rackspace_connection.rb', line 21

def region
  @region
end

Instance Method Details

#create_server(server_name, image_id, flavor_id = '4') ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/avst-cloud/rackspace_connection.rb', line 36

def create_server(server_name, image_id, flavor_id='4')
    server_number, os="ubuntu14"
    
    logger.debug "Creating Rackspace server:"
    logger.debug "server_name      - #{server_name}"
    logger.debug "flavor_id        - #{flavor_id}"
    logger.debug "image_id         - #{image_id}"

    unless server_name and image_id
        raise "Please provide server_name, image_id and flavor_id"
    end

    # Check existing server
    existing_server = find_fog_server(server_name, false)
    if existing_server && existing_server.state != 'SHUTOFF'
        logger.debug "Server found in state: #{existing_server.state}"
        raise "Server with the same name found!"
    elsif existing_server && existing_server.state == 'SHUTOFF'
        logger.debug "Server found and is stopped, restarting it."
        existing_server.reboot 'HARD'
        result_server = AvstCloud::RackspaceServer.new(existing_server, server_name, nil, nil , nil)
        result_server.wait_for_state() {|serv| serv.ready?}
        logger.debug "[DONE]\n\n"
        logger.debug "The server was successfully re-started.\n\n"
    else
        # create server
        server = connect.servers.create :name => server_name,
                                        :flavor_id => flavor_id,
                                        :image_id => image_id
        begin
            result_server = AvstCloud::RackspaceServer.new(server, server_name, nil, nil , nil)
            # Check every 5 seconds to see if server is in the active state (ready?).
            # If the server has not been built in 5 minutes (600 seconds) an exception will be raised.
            result_server.wait_for_state() {|serv| serv.ready?}
            logger.debug "[DONE]\n\n"

            logger.debug "The server has been successfully created, to login onto the server:\n\n"
            logger.debug "\t ssh #{server.username}@#{server.public_ip_address}\n\n"

        rescue Fog::Errors::TimeoutError
            logger.debug "[TIMEOUT]\n\n"
            logger.debug "This server is currently #{server.progress}% into the build process and is taking longer to complete than expected."
            logger.debug "You can continute to monitor the build process through the web console at https://mycloud.rackspace.com/\n\n"
            raise "Timeout while creating Rackspace server #{server_name}"
        end
        logger.debug "The #{server.username} password is #{server.password}\n\n"
    end
    result_server.access_user = server.username
    result_server.access_password = server.password
    result_server.ip_address =  server.public_ip_address
    result_server
end

#find_fog_server(server_name, should_fail = true) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/avst-cloud/rackspace_connection.rb', line 115

def find_fog_server(server_name, should_fail=true)
    serv = connect.servers.find{|serv| serv.name == server_name}
    unless serv
        if should_fail
            logger.debug "Server not found for name: #{server_name}"
            raise "Server not found for name: #{server_name}"
        end
    end
    serv
end

#list_flavoursObject



98
99
100
101
102
# File 'lib/avst-cloud/rackspace_connection.rb', line 98

def list_flavours
    connect.flavors.each do |fl|
        logger.debug fl.inspect
    end
end

#list_imagesObject



104
105
106
107
108
# File 'lib/avst-cloud/rackspace_connection.rb', line 104

def list_images
    connect.images.each do |im|
        logger.debug im.inspect
    end
end

#list_known_serversObject

Returns list of servers from fog



111
112
113
# File 'lib/avst-cloud/rackspace_connection.rb', line 111

def list_known_servers
    connect.servers.all
end

#server(server_name, root_user, root_password, os = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/avst-cloud/rackspace_connection.rb', line 28

def server(server_name, root_user, root_password, os=nil)
    server = find_fog_server(server_name)
    if !root_user
        root_user = "root"
    end
    AvstCloud::RackspaceServer.new(server, server_name, server.public_ip_address, root_user, root_password)
end

#server_status(server_name) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/avst-cloud/rackspace_connection.rb', line 89

def server_status(server_name)
    server = find_fog_server(server_name, false)
    if server
        server.state
    else
        'not_created'
    end
end