Project

General

Profile

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

foreman-docker / app / helpers / containers_helper.rb @ e6eb1c48

1
module ContainersHelper
2
  def managed_icon(container, resource)
3
    icon_text(managed?(container, resource) ? 'check' : 'unchecked')
4
  end
5

    
6
  def managed?(container, resource)
7
    uuids_in_resource(resource).include? container.identity
8
  end
9

    
10
  def uuids_in_resource(resource)
11
    @uuids_in_resource ||= {}
12
    @uuids_in_resource[resource.id] ||= Container.where(:compute_resource_id => resource.id)
13
                                        .pluck(:uuid)
14
  end
15

    
16
  def link_to_container(container, resource)
17
    link_to_if_authorized container.name[1..-1].titleize,
18
                          container_link_hash(container, resource)
19
  end
20

    
21
  def link_to_taxonomies(taxonomies)
22
    taxonomies.map do |taxonomy|
23
      link_to(taxonomy)
24
    end.join(' ')
25
  end
26

    
27
  def container_link_hash(container, resource)
28
    if managed?(container, resource)
29
      hash_for_container_path(:id => Container.find_by_uuid(container.identity).id)
30
    else
31
      hash_for_compute_resource_vm_path(:compute_resource_id => resource,
32
                                        :id                  => container.identity)
33
    end
34
  end
35

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

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

    
73
    display_link_if_authorized "Power #{action_string(vm)}", opts, html.merge(:method => :put)
74
  end
75

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

    
81
  def auto_complete_docker_search(name, val, options = {})
82
    addClass options, 'form-control'
83
    text_field_tag(name, val, options)
84
  end
85

    
86
  def hub_url(image)
87
    if image['is_official']
88
      "https://registry.hub.docker.com/_/#{image['name']}"
89
    else
90
      "https://registry.hub.docker.com/u/#{image['name']}"
91
    end
92
  end
93

    
94
  # Compatibility fixes - to be removed once 1.7 compatibility is no longer required
95
  if SETTINGS[:version].to_s.to_f <= 1.7
96
    def trunc_with_tooltip(text, length = 32)
97
      trunc(text, length)
98
    end
99
  end
100
end