Project

General

Profile

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

foreman_docker / app / helpers / containers_helper.rb @ c80f3b10

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', :'data-toggle' => 'modal')
35
      ),
36
      button_group(container_power_action(container.in_fog)),
37
      button_group(
38
        display_delete_if_authorized(
39
          hash_for_container_path(:id => container.id)
40
                                  .merge(:auth_object => container,
41
                                         :auth_action => 'destroy',
42
                                         :authorizer  => authorizer),
43
          :confirm     => _("Delete %s?") % container.name)
44
      )
45
    )
46
  end
47

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

    
67
    display_link_if_authorized "Power #{action_string(vm)}", opts, html.merge(:method => :put)
68
  end
69

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

    
75
  def auto_complete_docker_search(name, val, options = {})
76
    addClass options, 'form-control'
77
    text_field_tag(name, val, options)
78
  end
79

    
80
  def processes(container)
81
    ForemanDocker::Docker.get_container(container).top
82
  end
83

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