Module: OmfRc::ResourceProxy::User

Includes:
OmfRc::ResourceProxyDSL
Defined in:
lib/nitos_testbed_rc/user.rb

Instance Method Summary collapse

Instance Method Details

#process_event(res, event_type, app_id, msg) ⇒ Object

This method processes an event coming from the application instance, which was started by this Resource Proxy (RP). It is a callback, which is usually called by the ExecApp class in OMF

Parameters:

  • res (AbstractResource)

    this RP

  • event_type (String)

    the type of event from the app instance (STARTED, DONE.OK, DONE.ERROR, STDOUT, STDERR)

  • app_id (String)

    the id of the app instance

  • msg (String)

    the message carried by the event



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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/nitos_testbed_rc/user.rb', line 155

def process_event(res, event_type, app_id, msg)
    logger.info "App Event from '#{app_id}' - #{event_type}: '#{msg}'"
    if event_type == 'EXIT'
      if msg == 0 #only when user creation succeeds, create a new public key and save it to /home/username/.ssh/
                  #then inform with the appropriate msg, and give the pub key
        key = OpenSSL::PKey::RSA.new(2048)

        pub_key = key.public_key

        path = "/home/#{res.property.username}/.ssh/"
        unless File.directory?(path)#create the directory if it doesn't exist (it will never exist)
          FileUtils.mkdir_p(path)
        end

        File.write("#{path}/id_rsa.pub", pub_key.to_pem)
        File.write("#{path}/id_rsa", key.to_pem)

        sleep 1
        res.inform(:status, {
          status_type: 'APP_EVENT',
          event: event_type.to_s.upcase,
          app: app_id,
          exit_code: msg,
          msg: msg,
          uid: res.uid, # do we really need this? Should be identical to 'src'
          pub_key: pub_key.to_pem
        }, :ALL)
      else #if msg!=0 then the application failed to complete
        path = "/home/#{res.property.username}/.ssh/"
        if File.exists?("#{path}/id_rsa.pub") && File.exists?("#{path}/id_rsa")#if keys exist just read the pub_key for the inform
          file = File.open("#{path}/id_rsa.pub", "rb")
          pub_key = file.read
          file.close
        else #if keys do not exist create them and then inform
          key = OpenSSL::PKey::RSA.new(2048)

          pub_key = key.public_key

          path = "/home/#{res.property.username}/.ssh/"
          unless File.directory?(path)#create the directory if it doesn't exist (it will never exist)
            FileUtils.mkdir_p(path)
          end

          pub_key = pub_key.to_pem

          File.write("#{path}/id_rsa.pub", pub_key)
          File.write("#{path}/id_rsa", key.to_pem)
        end
        sleep 1
        res.inform(:status, {
          status_type: 'APP_EVENT',
          event: event_type.to_s.upcase,
          app: app_id,
          exit_code: msg,
          msg: msg,
          uid: res.uid, # do we really need this? Should be identical to 'src'
          pub_key: pub_key
        }, :ALL)
      end
    else
      # puts "(((((((((((((( error: #{msg} ))))))))))))))))))"
      # res.inform(:status, {
      #   status_type: 'APP_EVENT',
      #   event: event_type.to_s.upcase,
      #   app: app_id,
      #   msg: msg,
      #   uid: res.uid
      # }, :ALL)
    end
end