Class: ImageFactoryConsole

Inherits:
Qmf2::ConsoleHandler
  • Object
show all
Defined in:
lib/imagefactory/console.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ImageFactoryConsole

Returns a new instance of ImageFactoryConsole.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/imagefactory/console.rb', line 31

def initialize(args={})
#    @retry_limit = args.include?(:retry_limit) ? args[:retry_limit] : 20
#    @delay = args.include?(:delay) ? args[:delay] : 15
  host = args.include?(:host) ? args[:host] : "localhost"
  port = args.include?(:port) ? args[:port] : 5672
#   url = "amqp://#{host}:#{port}" <- the amqp part here doesnt work yet
  url = "#{host}:#{port}"
  opts = {"reconnect"=>"true"}
  @connection = Cqpid::Connection.new(url, opts)
  @connection.open
  @session = Qmf2::ConsoleSession.new(@connection)
  @session.open
  @session.set_agent_filter("[and, [eq, _vendor, [quote, 'redhat.com']], [eq, _product, [quote, 'imagefactory']]]")

  if args.include?(:logger)
    @logger = args[:logger]
  else
    @logger = Logger.new(STDOUT)
    @logger.level = Logger::ERROR
    @logger.datetime_format = "%Y-%m-%d %H:%M:%S"
  end
  @handler = args.include?(:handler)? args[:handler]: BaseHandler.new(@logger)
  super(@session)
end

Instance Attribute Details

#handlerObject

Returns the value of attribute handler.



29
30
31
# File 'lib/imagefactory/console.rb', line 29

def handler
  @handler
end

#qObject

Returns the value of attribute q.



29
30
31
# File 'lib/imagefactory/console.rb', line 29

def q
  @q
end

Instance Method Details

#agent_added(agent) ⇒ Object

TODO: enhance both of these methods to handle multiple agents



179
180
181
182
# File 'lib/imagefactory/console.rb', line 179

def agent_added(agent)
  @logger.debug "GOT AN AGENT:  #{agent} at #{Time.now.utc}"
  @q = agent if agent.product == "imagefactory"
end

#agent_deleted(agent, reason) ⇒ Object



184
185
186
187
188
189
# File 'lib/imagefactory/console.rb', line 184

def agent_deleted(agent, reason)
  @logger.debug "AGENT GONE:  #{agent} at #{Time.now.utc}, because #{reason}"
  unless @q==nil
    @q = nil if (@q.product == agent.product && @q.name.eql?(agent.name))
  end
end

#agent_restarted(agent) ⇒ Object

TODO: handle agent restart events (this will be more useful when restarted agent can recover an in-process build



193
194
195
# File 'lib/imagefactory/console.rb', line 193

def agent_restarted(agent)
  @logger.debug "AGENT RESTARTED:  #{agent.product}"
end

#agent_schema_updated(agent) ⇒ Object

TODO: handle schema updates. This will be more useful when/if we make this a more generic console to talk to different agents.



199
200
201
# File 'lib/imagefactory/console.rb', line 199

def agent_schema_updated(agent)
  @logger.debug "AGENT SCHEMA UPDATED:  #{agent.product}"
end

#build(template, targets, image = '', build = '') ⇒ Object

Call this method to initiate a build, and get back an array of BuildAdaptor objects.

  • template => String

Description of what to build. Can be xml, uuid, or url

  • targets => Array

Represents the names of the clouds to target (ec2, mock, rackspace, etc)

  • image_id => String

The UUID of an image previously built

  • build_id => String

The UUID of a previous build of the image

  • Returns => an array of BuildAdaptor objects



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/imagefactory/console.rb', line 68

def build(template, targets, image='', build='')
  targets = [targets] unless targets.instance_of?(Array)
  # TODO: return error if there is a problem calling this method or getting
  # a factory instance
  begin
    response = factory.build_image(image, build, template, targets)
    build_adaptors(response)
  rescue Exception => e
    @logger.debug "Encountered error in build_image: #{e}"
    return e
  end
end

#build_image(descriptor, target) ⇒ Object

DEPRECATED: Please use build instead. Call this method to initiate a build, and get back an BuildAdaptor object.

  • descriptor => String

This can be either xml or a url pointing to the xml

  • target => String

Represents the target provider type to build for (ec2, mock, etc)

  • Returns => a BuildAdaptor object



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/imagefactory/console.rb', line 142

def build_image(descriptor, target)
  @logger.warn "[DEPRECATION] 'build_image' is deprecated.  Please use 'build' instead."
  # TODO: return error if there is a problem calling this method or getting
  # a factory instance
  begin
    response = factory.image(descriptor, target)
    build_adaptor(response)
  rescue Exception => e
    @logger.debug "Encountered error in build_image: #{e}"
    return e
  end
end

#event_raised(agent, data, timestamp, severity) ⇒ Object



203
204
205
206
# File 'lib/imagefactory/console.rb', line 203

def event_raised(agent, data, timestamp, severity)
  @logger.debug "GOT AN EVENT:  #{agent}, #{data} at #{timestamp}"
  @handler.handle(data)
end

#import_image(image, build, target_identifier, image_description, target, provider) ⇒ Object

Call this method to import an image array of BuildAdaptor objects.

  • image => string

The uuid of an existing image

  • build => string

the uuid of an existing build, or the empty string

  • target_identifier => string

the target specific image ID

  • image_desc => string

an xml string description of the image

  • target => string

name of the cloud to target

  • provider => string

the name of the cloud provider, often a region Returns => a map including UUIDs as strings for ‘target_image’, ‘build’, ‘image’, ‘provider_image’



122
123
124
125
126
127
128
129
130
131
# File 'lib/imagefactory/console.rb', line 122

def import_image(image, build, target_identifier, image_description, target, provider)
  # TODO: return error if there is a problem calling this method or getting
  # a factory instance
  begin
    factory.import_image(image, build, target_identifier, image_description, target, provider)
  rescue Exception => e
    @logger.debug "Encountered error in import_image: #{e}"
    return e
  end
end

#push(providers, credentials, image_id, build_id = '') ⇒ Object

Call this method to push an image to a provider, and get back an array of BuildAdaptor objects.

  • providers => Array

Represents the target providers to build for (ec2-us-east, mock1, etc)

  • credentials => String

XML block to be used for registration, upload, etc

  • image_id => String

The UUID of an image previously built

  • build_id => String

The UUID of a previous build of the image

  • Returns => an array of BuildAdaptor objects



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/imagefactory/console.rb', line 93

def push(providers, credentials, image_id, build_id='')
  providers = [providers] unless providers.instance_of?(Array)
  # TODO: return error if there is a problem calling this method or getting
  # a factory instance
  begin
    response = factory.push_image(image_id, build_id, providers, credentials)
    build_adaptors(response)
  rescue Exception => e
    @logger.debug "Encountered error in push_image: #{e}"
    return e
  end
end

#push_image(image_id, provider, credentials) ⇒ Object

DEPRECATED: Please use push instead. Call this method to push an image to a provider, and get back an BuildAdaptor object.

  • image_id => String (uuid)

  • provider => String

Represents the target provider to build for (ec2-us-east, mock, etc)

  • credentials => String

XML block to be used for registration, upload, etc

  • Returns => a BuildAdaptor object



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/imagefactory/console.rb', line 165

def push_image(image_id, provider, credentials)
  @logger.warn "[DEPRECATION] 'push_image' is deprecated.  Please use 'push' instead."
  # TODO: return error if there is a problem calling this method or getting
  # a factory instance
  begin
    response = factory.provider_image(image_id, provider, credentials)
    build_adaptor(response)
  rescue Exception => e
    @logger.debug "Encountered error in push_image: #{e}"
    return e
  end
end

#shutdownObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/imagefactory/console.rb', line 208

def shutdown
  @logger.debug "Closing connections.."
  if @session
    @session.close
  end
  @connection.close

  #TODO Replace this with some control flow to manage the cancelling of the console handler, atm
  #     this code swallows error message generated by closing connection, then cancelling console.
  begin
    self.cancel
  rescue QmfError
    # TODO We should be logging to a file rather than STDOUT, uncomment this code when logging is configured properly
    # Commented out to clean up user output
    #@logger.error "Issue Closing Console Handler"
  end
end