(maint) Adding a provider method tag_vm_user

This method should be called only once, when the VM is moved to a running state
which is when the data for the user (if using tokens) is available. In vmpooler
base provider it is a noop method, but the various providers can implement it
to tag or label a running VM with the name of the user who checked it out
This commit is contained in:
Samuel Beaulieu 2021-12-09 11:13:03 -06:00
parent 0777bd36d4
commit daea25b1d1
No known key found for this signature in database
GPG key ID: 12030F74136D0F34
3 changed files with 41 additions and 0 deletions

View file

@ -294,6 +294,12 @@ module Vmpooler
move_vm_queue(pool, vm, 'running', 'completed', redis, 'is listed as running, but has no checkouttime data. Removing from running')
end
# tag VM if not tagged yet, this ensures the method is only called once
unless redis.hget("vmpooler__vm__#{vm}", 'user_tagged')
success = provider.tag_vm_user(pool, vm)
redis.hset("vmpooler__vm__#{vm}", 'user_tagged', 'true') if success
end
throw :stop_checking if provider.vm_ready?(pool, vm)
throw :stop_checking if provider.get_vm(pool, vm)

View file

@ -212,6 +212,22 @@ module Vmpooler
raise("#{self.class.name} does not implement vm_ready?")
end
# tag_vm_user This method is called once we know who is using the VM (it is running). This method enables seeing
# who is using what in the provider pools.
# This method should be implemented in the providers, if it is not implemented, this base method will be called
# and should be a noop. The implementation should check if the vm has a user (as per redis) and add a new tag
# with the information.
# inputs
# [String] pool_name : Name of the pool
# [String] vm_name : Name of the VM to check if ready
# returns
# [Boolean] : true if successful, false if an error occurred and it should retry
def tag_vm_user(_pool_name, _vm_name)
# noop by design. If the provider does not implement this method, this base method is called (because inherited)
# and should basically do nothing.
true
end
# inputs
# [String] pool_name : Name of the pool
# [String] vm_name : Name of the VM to check if it exists

View file

@ -683,6 +683,25 @@ EOT
expect(redis.sismember("vmpooler__completed__#{pool}", vm)).to be(true)
end
end
it 'should try to tag if it has not been done' do
redis_connection_pool.with do |redis|
expect(provider).to receive(:vm_ready?).and_return(true)
redis.hset("vmpooler__active__#{pool}", vm,(Time.now - timeout*60*60).to_s)
expect(provider).to receive(:tag_vm_user)
subject._check_running_vm(vm, pool, 0, provider)
end
end
it 'should not try to tag if it has been done' do
redis_connection_pool.with do |redis|
expect(provider).to receive(:vm_ready?).and_return(true)
redis.hset("vmpooler__active__#{pool}", vm,(Time.now - timeout*60*60).to_s)
redis.hset("vmpooler__vm__#{vm}", 'user_tagged', 'true')
expect(provider).not_to receive(:tag_vm_user)
subject._check_running_vm(vm, pool, 0, provider)
end
end
end
context 'with a locked vm mutex' do