Class: Hotdog::Commands::Untag

Inherits:
BaseCommand show all
Defined in:
lib/hotdog/commands/untag.rb

Constant Summary

Constants inherited from BaseCommand

BaseCommand::MASK_DATABASE, BaseCommand::MASK_QUERY, BaseCommand::PERSISTENT_DB

Instance Attribute Summary

Attributes inherited from BaseCommand

#application, #logger, #options

Instance Method Summary collapse

Methods inherited from BaseCommand

#execute, #fixed_string?, #initialize, #parse_options, #reload

Constructor Details

This class inherits a constructor from Hotdog::Commands::BaseCommand

Instance Method Details

#define_options(optparse, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/hotdog/commands/untag.rb', line 8

def define_options(optparse, options={})
  default_option(options, :tag_source, "user")
  default_option(options, :tags, [])
  optparse.on("--source SOURCE") do |v|
    options[:tag_source] = v
  end
  optparse.on("--tag TAG") do |v|
    options[:tags] << v
  end
end

#run(args = [], options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hotdog/commands/untag.rb', line 19

def run(args=[], options={})
  args.each do |host_name|
    host_name = host_name.sub(/\Ahost:/, "")

    if options[:tags].empty?
      # delete all user tags
      code, detach_tags = dog.detach_tags(host_name, source=options[:tag_source])
      if code.to_i / 100 != 2
        raise("dog.detach_tags(#{host_name.inspect}, source=#{options[:tag_source].inspect}) returns [#{code.inspect}, #{detach_tags.inspect}]")
      end
    else
      code, host_tags = dog.host_tags(host_name, source=options[:tag_source])
      if code.to_i / 100 != 2
        raise("dog.host_tags(#{host_name.inspect}, source=#{options[:tag_source].inspect}) returns [#{code.inspect}, #{host_tags.inspect}]")
      end
      old_tags = host_tags["tags"]
      new_tags = old_tags - options[:tags]
      if old_tags == new_tags
        # nop
      else
        code, update_tags = dog.update_tags(host_name, new_tags, source=options[:tag_source])
        if code.to_i / 100 != 2
          raise("dog.update_tags(#{host_name.inspect}, #{new_tags.inspect}, source=#{options[:tag_source].inspect}) returns [#{code.inspect}, #{update_tags.inspect}]")
        end
      end
    end
  end

  # Remove persistent.db to schedule update on next invocation
  if @db
    close_db(@db)
  end
  FileUtils.rm_f(File.join(options[:confdir], PERSISTENT_DB))
end