def user_data(request): vm_ip = request.META.get('REMOTE_ADDR') vm = VM.get_by_ip( vm_ip ) user_data = vm.long_dict['user_data'] return base64.b64decode( user_data )
def get_command(remote_ip, **kw): """ @param_post{remote_ip,string} @param_post{kw,dict} keyword params @returns{Command} next command from the que to the asking VM """ vm = VM.get_by_ip(remote_ip) log.debug(0, "Get first command for %s" % vm.id) command = vm.command_set.filter(state=command_states['pending']).order_by('id') if len(command) == 0: return response('ctx_no_command') command = command[0] log.debug(0, "First command is %s" % command.id) command.state = command_states['executing'] command.save() d = command.dict() r = response('ok', d) if int(kw.get('version', 0)) < VERSION: f = file(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'actions.py'), 'r') r['actions_file'] = f.read() f.close() return r
def instance_id(request): vm_ip = request.META.get('REMOTE_ADDR') vm = VM.get_by_ip( vm_ip ) vm_id = 'i-' + str(vm.dict['vm_id']) return vm_id
def ami_id(request): vm_ip = request.META.get('REMOTE_ADDR') vm = VM.get_by_ip( vm_ip ) ami = 'ami-' + str(vm.dict['image_id']) return ami
def hello(vm_ip, **args): """ First function which must be called by VMs ctx module. It registers VM with status 'running ctx', also serves a special role when creating farms (tracking head, and worker nodes) @parameter{vm_ip,string} @parameter{args} """ vm = VM.get_by_ip(vm_ip) log.debug(vm.user_id, "Hello from vm %d ip: %s" % (vm.id, vm_ip)) vm.ctx_api_version = args.get('version', None) vm.state = vm_states['running ctx'] if vm.ssh_username and vm.ssh_key: Command.execute('add_ssh_key', vm.user_id, vm.id, user=vm.ssh_username, ssh_key=vm.ssh_key) if vm.is_head(): Command.register_head(vm) elif vm.is_farm(): Command.register_node(vm) try: vm.save(update_fields=['state', 'ctx_api_version']) except Exception, e: log.error( vm.user_id, "Cannot update database for vm %d: %s" % (vm.id, e.message)) return response('ctx_error', "Cannot update database: %s" % e.message)
def reservation_id(request): vm_ip = request.META.get('REMOTE_ADDR') vm = VM.get_by_ip( vm_ip ) reservation_id = vm.long_dict['reservation_id'] return 'r-' + str(reservation_id)
def hello(vm_ip, **args): """ First function which must be called by VMs ctx module. It registers VM with status 'running ctx', also serves a special role when creating farms (tracking head, and worker nodes) @parameter{vm_ip,string} @parameter{args} """ vm = VM.get_by_ip(vm_ip) log.debug(vm.user_id, "Hello from vm %d ip: %s" % (vm.id, vm_ip)) vm.ctx_api_version = args.get('version', None) vm.state = vm_states['running ctx'] if vm.ssh_username and vm.ssh_key: Command.execute('add_ssh_key', vm.user_id, vm.id, user=vm.ssh_username, ssh_key=vm.ssh_key) if vm.is_head(): Command.register_head(vm) elif vm.is_farm(): Command.register_node(vm) try: vm.save(update_fields=['state', 'ctx_api_version']) except Exception, e: log.error(vm.user_id, "Cannot update database for vm %d: %s" % (vm.id, e.message)) return response('ctx_error', "Cannot update database: %s" % e.message)
def ami_launch_index(request): vm_ip = request.META.get('REMOTE_ADDR') vm = VM.get_by_ip( vm_ip ) instance_id = vm.dict['vm_id'] reservation_id = vm.long_dict['reservation_id'] launch_index = reservation_id - instance_id + 1; return launch_index
def public_ipv4(request): vm_ip = request.META.get('REMOTE_ADDR') vm = VM.get_by_ip( vm_ip ) leases = vm.dict['leases'] ip = leases for ip in leases: if ip.get('public_ip'): ip = ip.get('public_ip') else: ip = "" return ip
def hello(remote_ip, **kw): """ REST stub for hello function @parameter{kw} @returns HTTP response """ vm = VM.get_by_ip(remote_ip) log.info(vm.user_id, "vm called hello") Command.hello(remote_ip) r = response('ok') if int(kw.get('version', 0)) < VERSION: f = file(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'actions.py'), 'r') r['actions_file'] = f.read() f.close() return r
def hello(remote_ip, **kw): """ REST stub for hello function @param_post{remote_ip,string} @param_post{kw} @returns HTTP response """ vm = VM.get_by_ip(remote_ip) log.info(vm.user_id, "vm called hello") Command.hello(remote_ip) r = response('ok') if int(kw.get('version', 0)) < VERSION: f = file(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'actions.py'), 'r') r['actions_file'] = f.read() f.close() return r
def public_keys(request, number=None, key_type=None): vm_ip = request.META.get('REMOTE_ADDR') vm = VM.get_by_ip( vm_ip ) if number == "0" and vm.long_dict.get('ssh_key'): if key_type == "openssh-key": # HARDCODED VALUE, only one key can be assigned return vm.long_dict.get('ssh_key') else: if key_type: pass # todo error else: return "openssh-key" else: if number or key_type: pass # todo error if vm.long_dict.get('ssh_key'): return '0=public-key'
def finish_command(remote_ip, command_id, status, returns=None, **kw): """ REST stub for finish_command @param_post{remote_ip,string} @param_post{command_id,string} hash string identyfing command @param_post{status,string} @param_post{returns,dict} dictionary containing VM returned values @param_post{kw,dict} keyword params """ vm = VM.get_by_ip(remote_ip) if returns: returns = json.dumps(returns) log.debug(0, "Select command %s %s" % (command_id, status)) try: command = vm.command_set.get(id=command_id) except Command.DoesNotExist: return log.debug(0, "Finish command %s" % command) if command is None: for c in Command.objects.all(): log.debug(0, 'Finish - Available cmds id:%s, state:%s, name:%s, vmid:%s' % (c.id, c.state, c.name, c.vm_id)) return log.debug(vm.user_id, "command state %s" % command.state) command.response = returns command.state = command_states[status] log.debug(vm.user_id, "Finish command %s" % command.id) command.save() r = response('ok') if int(kw.get('version', 0)) < VERSION: f = file(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'actions.py'), 'r') r['actions_file'] = f.read() f.close() return r
def instance_type(request): vm_ip = request.META.get('REMOTE_ADDR') vm = VM.get_by_ip( vm_ip ) template_name = str(vm.dict['template_name']) return template_name