Skip to content

Add tab completion script for zsh and fix bash completion for ABS services #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ If you are running on macOS and use Homebrew's `bash-completion` formula, you ca
ln -s $(floaty completion --shell bash) /usr/local/etc/bash_completion.d/floaty
```

There is also tab completion for zsh:

```zsh
source $(floaty completion --shell zsh)
```

## vmpooler API

This cli tool uses the [vmpooler API](https://github.com/puppetlabs/vmpooler/blob/master/API.md).
Expand Down
2 changes: 1 addition & 1 deletion extras/completions/floaty.bash
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ _vmfloaty()

COMPREPLY=( $(compgen -W "${_vmfloaty_avail_templates}" -- "${cur}") )
elif [[ $hostname_subcommands =~ (^| )$prev($| ) ]] ; then
_vmfloaty_active_hostnames=$(floaty list --active 2>/dev/null | grep '^-' | cut -d' ' -f2)
_vmfloaty_active_hostnames=$(floaty list --active --hostnameonly 2>/dev/null)
COMPREPLY=( $(compgen -W "${_vmfloaty_active_hostnames}" -- "${cur}") )
else
COMPREPLY=( $(compgen -W "${subcommands}" -- "${cur}") )
Expand Down
37 changes: 37 additions & 0 deletions extras/completions/floaty.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
_floaty()
{
local line subcommands template_subcommands hostname_subcommands

subcommands="delete get help list modify query revert snapshot ssh status summary token"

template_subcommands=("get" "ssh")
hostname_subcommands=("delete" "modify" "query" "revert" "snapshot")

_arguments -C \
"1: :(${subcommands})" \
"*::arg:->args"

if ((template_subcommands[(Ie)$line[1]])); then
_floaty_template_sub
elif ((hostname_subcommands[(Ie)$line[1]])); then
_floaty_hostname_sub
fi
}

_floaty_template_sub()
{
if [[ -z "$_vmfloaty_avail_templates" ]] ; then
_vmfloaty_avail_templates=$(floaty list 2>/dev/null)
fi

_arguments "1: :(${_vmfloaty_avail_templates})"
}

_floaty_hostname_sub()
{
_vmfloaty_active_hostnames=$(floaty list --active --hostnameonly 2>/dev/null)

_arguments "1: :(${_vmfloaty_active_hostnames})"
}

compdef _floaty floaty
5 changes: 5 additions & 0 deletions lib/vmfloaty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def run # rubocop:disable Metrics/AbcSize
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--active', 'Prints information about active vms for a given token'
c.option '--json', 'Prints information as JSON'
c.option '--hostnameonly', 'When listing active vms, prints only hostnames, one per line'
c.option '--token STRING', String, 'Token for pooler service'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--user STRING', String, 'User to authenticate with'
Expand Down Expand Up @@ -115,6 +116,10 @@ def run # rubocop:disable Metrics/AbcSize
else
if options.json
puts Utils.get_host_data(verbose, service, running_vms).to_json
elsif options.hostnameonly
Utils.get_host_data(verbose, service, running_vms).each do |hostname, host_data|
Utils.print_fqdn_for_host(service, hostname, host_data)
end
else
puts "Your VMs on #{host}:"
Utils.pretty_print_hosts(verbose, service, running_vms)
Expand Down
42 changes: 34 additions & 8 deletions lib/vmfloaty/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,28 @@ def self.generate_os_hash(os_args)
os_types
end

def self.print_fqdn_for_host(service, hostname, host_data)
case service.type
when 'ABS'
abs_hostnames = []

host_data['allocated_resources'].each do |vm_name, _i|
abs_hostnames << vm_name['hostname']
end

puts abs_hostnames.join("\n")
when 'Pooler'
puts "#{hostname}.#{host_data['domain']}"
when 'NonstandardPooler'
puts host_data['fqdn']
else
raise "Invalid service type #{service.type}"
end
end

def self.pretty_print_hosts(verbose, service, hostnames = [], print_to_stderr = false, indent = 0)
output_target = print_to_stderr ? $stderr : $stdout

fetched_data = self.get_host_data(verbose, service, hostnames)
fetched_data.each do |hostname, host_data|
case service.type
Expand All @@ -93,25 +114,30 @@ def self.pretty_print_hosts(verbose, service, hostnames = [], print_to_stderr =
#
# Create a vmpooler service to query each hostname there so as to get the metadata too

vmpooler_service = service.clone
vmpooler_service.silent = true
vmpooler_service.maybe_use_vmpooler
puts "- [JobID:#{host_data['request']['job']['id']}] <#{host_data['state']}>"
host_data['allocated_resources'].each do |vm_name, _i|
self.pretty_print_hosts(verbose, vmpooler_service, vm_name['hostname'].split('.')[0], print_to_stderr, indent+2)
output_target.puts "- [JobID:#{host_data['request']['job']['id']}] <#{host_data['state']}>"
host_data['allocated_resources'].each do |allocated_resources, _i|
if allocated_resources['engine'] == "vmpooler"
vmpooler_service = service.clone
vmpooler_service.silent = true
vmpooler_service.maybe_use_vmpooler
self.pretty_print_hosts(verbose, vmpooler_service, allocated_resources['hostname'].split('.')[0], print_to_stderr, indent+2)
else
#TODO we could add more specific metadata for the other services, nspooler and aws
output_target.puts " - #{allocated_resources['hostname']} (#{allocated_resources['type']})"
end
end
when 'Pooler'
tag_pairs = []
tag_pairs = host_data['tags'].map { |key, value| "#{key}: #{value}" } unless host_data['tags'].nil?
duration = "#{host_data['running']}/#{host_data['lifetime']} hours"
metadata = [host_data['template'], duration, *tag_pairs]
puts "- #{hostname}.#{host_data['domain']} (#{metadata.join(', ')})".gsub(/^/, ' ' * indent)
output_target.puts "- #{hostname}.#{host_data['domain']} (#{metadata.join(', ')})".gsub(/^/, ' ' * indent)
when 'NonstandardPooler'
line = "- #{host_data['fqdn']} (#{host_data['os_triple']}"
line += ", #{host_data['hours_left_on_reservation']}h remaining"
line += ", reason: #{host_data['reserved_for_reason']}" unless host_data['reserved_for_reason'].empty?
line += ')'
puts line
output_target.puts line
else
raise "Invalid service type #{service.type}"
end
Expand Down
Loading