Class: Machinery::ListTask

Inherits:
Object show all
Defined in:
lib/list_task.rb

Overview

Copyright © 2013-2016 SUSE LLC

This program is free software; you can redistribute it and/or modify it under the terms of version 3 of the GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, contact SUSE LLC.

To contact SUSE about this file by physical or electronic mail, you may find current contact information at www.suse.com

Instance Method Summary collapse

Instance Method Details

#list(store, system_descriptions, 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/list_task.rb', line 19

def list(store, system_descriptions, options = {})
  if options[:html]
    list_html(store, options)
  else
    if system_descriptions.empty?
      descriptions = store.list
    else
      descriptions = system_descriptions.sort
    end
    has_incompatible_version = false

    descriptions.each do |name|
      begin
        system_description = Machinery::SystemDescription.load(name, store, skip_validation: true)
      rescue Machinery::Errors::SystemDescriptionIncompatible => e
        show_error("#{e}\n", options)
        next
      rescue Machinery::Errors::SystemDescriptionNotFound
        show_error(
          "#{name}: Couldn't find a system description with the name '#{name}'.", options
        )
        next
      rescue Machinery::Errors::SystemDescriptionValidationFailed
        show_error("#{name}: This description is broken. Use "\
          "`#{Machinery::Ui::Hint.program_name} validate "\
          "#{name}` to see the error message.", options)
        next
      rescue Machinery::Errors::SystemDescriptionError
        show_error("#{name}: This description is broken.", options)
        next
      end

      if options[:short]
        Machinery::Ui.puts name
      else
        scopes = []

        system_description.scopes.each do |scope|
          entry = Machinery::Ui.internal_scope_list_to_string(scope)
          if Machinery::SystemDescription::EXTRACTABLE_SCOPES.include?(scope)
            if system_description.scope_extracted?(scope)
              entry += " (extracted)"
            else
              entry += " (not extracted)"
            end
          end

          if options[:verbose]
            meta = system_description[scope].meta
            if meta
              time = Time.parse(meta.modified).getlocal
              date = time.strftime "%Y-%m-%d %H:%M:%S"
              hostname = meta.hostname
            else
              date = "unknown"
              hostname = "Unknown hostname"
            end
            entry += "\n      Host: [#{hostname}]"
            entry += "\n      Date: (#{date})"
          end

          scopes << entry
        end

        Machinery::Ui.puts " #{name}:\n   * " + scopes .join("\n   * ") + "\n"
      end
    end

    Machinery::Ui::Hint.print(:upgrade_system_description) if has_incompatible_version
  end
end

#list_html(store, options) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/list_task.rb', line 91

def list_html(store, options)
  begin
    Machinery::LocalSystem.validate_existence_of_command("xdg-open", "xdg-utils")

    url = "http://#{options[:ip]}:#{options[:port]}/"

    Machinery::Ui.use_pager = false
    Machinery::Ui.puts <<EOF
Trying to start a web server for serving the descriptions on #{url}.

The server can be closed with Ctrl+C.
EOF

    server = Machinery::Html.run_server(store, port: options[:port], ip: options[:ip]) do
      Machinery::LoggedCheetah.run("xdg-open", url)
    end

    server.join # Wait until the user cancelled the blocking webserver
  rescue Cheetah::ExecutionFailed => e
    raise Machinery::Errors::OpenInBrowserFailed.new(
      "Could not open system descriptions in the web browser.\n" \
        "Error: #{e}\n"
    )
  end
end