Class: ChefDocker::DockerDelete

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/docker_delete.rb

Instance Method Summary collapse

Instance Method Details

#delete(container) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/chef/knife/docker_delete.rb', line 69

def delete(container)
  `docker stop #{container}`
  `docker rm #{container}`

  if config[:_purge]
    destroy_item(Chef::Node, container, "node")
    destroy_item(Chef::ApiClient, container, "client")
  end
end

#destroy_item(klass, name, type_name) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/chef/knife/docker_delete.rb', line 59

def destroy_item(klass, name, type_name)
  begin
    object = klass.load(name)
    object.destroy
    ui.warn("Deleted #{type_name} #{name}")
  rescue Net::HTTPServerException
    ui.warn("Could not find a #{type_name} named #{name} to delete!")
  end
end

#runObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chef/knife/docker_delete.rb', line 34

def run
  if @name_args.length == 0
    ui.error("Please provide at least one container id")
    show_usage
    exit 1
  end

  running_containers = []

  `docker ps`.split(/\n/).each do |c|
    if c =~ /^[\d\w]{12}\s/
      running_containers.push c.split[0]
    end
  end

  @name_args.each do |name|
    unless running_containers.include? name
      ui.warn("Container #{name} does not exist")
      next
    end

    delete name
  end
end