Project

General

Profile

Download (3.06 KB) Statistics
| Branch: | Tag: | Revision:

foreman-docker / app / helpers / containers_helper.rb @ 04b6e05e

1
module ContainersHelper
2
  def managed?(container, resource)
3
    uuids_in_resource(resource).include? container.identity
4
  end
5

    
6
  def uuids_in_resource(resource)
7
    @uuids_in_resource ||= {}
8
    @uuids_in_resource[resource.id] ||= Container.where(:compute_resource_id => resource.id)
9
                                        .pluck(:uuid)
10
  end
11

    
12
  def link_to_container(container, resource)
13
    link_to_if_authorized container.name[1..-1].titleize,
14
                          container_link_hash(container, resource)
15
  end
16

    
17
  def link_to_taxonomies(taxonomies)
18
    taxonomies.map { |taxonomy| link_to(taxonomy) }.join(" ")
19
  end
20

    
21
  def container_link_hash(container, resource)
22
    if managed?(container, resource)
23
      hash_for_container_path(:id => Container.find_by_uuid(container.identity).id)
24
    else
25
      hash_for_compute_resource_vm_path(:compute_resource_id => resource,
26
                                        :id                  => container.identity)
27
    end
28
  end
29

    
30
  def container_title_actions(container)
31
    @compute_resource = container.compute_resource
32
    title_actions(
33
      button_group(
34
        link_to(_('Commit'), '#commit-modal',
35
                :'data-toggle' => 'modal',
36
                :class => 'btn btn-default')
37
      ),
38
      button_group(container_power_action(container.in_fog)),
39
      button_group(
40
        display_delete_if_authorized(
41
          hash_for_container_path(:id => container.id)
42
                                  .merge(:auth_object => container,
43
                                         :auth_action => 'destroy',
44
                                         :authorizer  => authorizer),
45
          :confirm => _("Delete %s?") % container.name,
46
          :class => 'btn btn-default')
47
      )
48
    )
49
  end
50

    
51
  def container_power_action(vm, authorizer = nil)
52
    if managed?(vm, @compute_resource)
53
      id = Container.find_by_uuid(vm.identity).id
54
      opts = hash_for_power_container_path(:id => id)
55
             .merge(:auth_object => @compute_resource,
56
                    :permission => 'power_compute_resources_vms',
57
                    :authorizer => authorizer)
58
    else
59
      opts = hash_for_power_compute_resource_vm_path(:compute_resource_id => @compute_resource,
60
                                                     :id => vm.identity)
61
             .merge(:auth_object => @compute_resource, :permission => 'power_compute_resources_vms',
62
                    :authorizer => authorizer)
63
    end
64
    html = if vm.ready?
65
             { :confirm => power_on_off_message(vm), :class => "btn btn-danger" }
66
           else
67
             { :class => "btn btn-info" }
68
           end
69

    
70
    display_link_if_authorized "Power #{action_string(vm)}", opts, html.merge(:method => :put)
71
  end
72

    
73
  def power_on_off_message(vm)
74
    _("Are you sure you want to power %{act} %{vm}?") % { :act => action_string(vm).downcase.strip,
75
                                                          :vm => vm }
76
  end
77

    
78
  def processes(container)
79
    ForemanDocker::Docker.get_container(container).top
80
  end
81

    
82
  def logs(container, opts = {})
83
    ForemanDocker::Docker.get_container(container).logs(opts)
84
  end
85
end