Class: EC2QueryClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/econe/EC2QueryClient.rb

Constant Summary collapse

API_VERSION =
'2008-12-01'

Instance Method Summary collapse

Constructor Details

#initialize(secret = nil, endpoint = nil) ⇒ Client

Returns a new instance of Client.



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
# File 'lib/econe/EC2QueryClient.rb', line 37

def initialize(secret=nil, endpoint=nil)
    # Autentication
    ec2auth=nil
    
    if secret
        ec2auth = secret.split(':')
    elsif ENV["ECONE_ACCESS_KEY"] and ENV["ECONE_SECRET_KEY"]
        ec2auth = [ENV["ECONE_ACCESS_KEY"], ENV["ECONE_SECRET_KEY"]]
    else
        ec2auth=CloudClient::get_one_auth
    end
   
    if !ec2auth
        raise "No authorization data present"
    end
    
    @access_key_id     = ec2auth[0]
    @access_key_secret = Digest::SHA1.hexdigest(ec2auth[1])
    
    # Server location
    
    if !endpoint
        if $ec2url
            endpoint = $ec2url
        else
            endpoint = "http://localhost:4567"
        end
    end
    
    @uri = URI.parse(endpoint)
 
    @ec2_connection = AWS::EC2::Base.new(
        :access_key_id     => @access_key_id,
        :secret_access_key => @access_key_secret,
        :server            => @uri.host,
        :port              => @uri.port,
        :use_ssl           => @uri.scheme == 'https')
end

Instance Method Details

#describe_imagesObject

:image_id –> ALL :owner_id –> mine (ALWAYS) :executable_by –> Always Public (NO ACLS)



208
209
210
211
212
213
214
215
216
217
# File 'lib/econe/EC2QueryClient.rb', line 208

def describe_images()
    begin
        response = @ec2_connection.describe_images
    rescue Exception => e
        error = CloudClient::Error.new(e.message)
        return error
    end
    
    return response
end

#describe_instancesObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/econe/EC2QueryClient.rb', line 81

def describe_instances()
    begin
        response = @ec2_connection.describe_instances
    rescue Exception => e
        error = CloudClient::Error.new(e.message)
        return error
    end
    
    return response
end

#register_image(image_id) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/econe/EC2QueryClient.rb', line 190

def register_image(image_id)
    begin
       response = @ec2_connection.register_image(
                    :image_location => image_id
                  )
    rescue Exception => e
        error = CloudClient::Error.new(e.message)
        return error
    end
    
    return response
end

#run_instances(ami_id, type) ⇒ Object

:image_id :instance_type



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/econe/EC2QueryClient.rb', line 96

def run_instances(ami_id, type)
    begin
        response = @ec2_connection.run_instances(
                        :image_id      => ami_id,
                        :min_count     => 1,
                        :max_count     => 1,
                        :instance_type => type
                   )            
    rescue Exception => e
        error = CloudClient::Error.new(e.message)
        return error
    end
    
    return response
end

#terminate_instances(instance_id) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/econe/EC2QueryClient.rb', line 116

def terminate_instances(instance_id)
    begin
        response = @ec2_connection.terminate_instances(
            :instance_id   => instance_id
         )
    rescue Exception => e
        error = CloudClient::Error.new(e.message)
        return error
    end
    
    return response
end

#upload_image(file_name, curb = true) ⇒ Object

Returns true if HTTP code is 200,



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/econe/EC2QueryClient.rb', line 133

def upload_image(file_name, curb=true)
    params = { "Action"           => "UploadImage",
               "SignatureVersion" => "2",
               "SignatureMethod"  => 'HmacSHA1',
               "AWSAccessKeyId"   => @access_key_id,
               "Version"          => API_VERSION,
               "Timestamp"        => Time.now.getutc.iso8601 }

    str = AWS.canonical_string(params, @uri.host)
    sig = AWS.encode(@access_key_secret, str, false)
   
    post_fields = Array.new;

    if curb and CURL_LOADED
        params.each { |k,v|
            post_fields << Curl::PostField.content(k,v)
        }

        post_fields << Curl::PostField.content("Signature",sig)
        post_fields << Curl::PostField.file("file",file_name)

        connection = Curl::Easy.new(@uri.to_s)
        connection.multipart_form_post = true

        connection.http_post(*post_fields)

        if connection.response_code == 200
            return AWS::Response.parse(:xml => connection.body_str)
        else
            return CloudClient::Error.new(connection.body_str)
        end
    else
        params["Signature"]=sig

        file=File.open(file_name)
        params["file"]=UploadIO.new(file,
            'application/octet-stream', file_name)

        req = Net::HTTP::Post::Multipart.new('/', params)
        res = CloudClient.http_start(@uri) do |http|
            http.request(req)
        end

        file.close

        if res.code == '200'
            return AWS::Response.parse(:xml => res.body)
        else
            return CloudClient::Error.new(res.body)
        end
    end
end