Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / containers.rb @ f92e695a

1 c4dbc7a3 Tom McKay
module HammerCLICsv
2
  class CsvCommand
3
    class ContainersCommand < BaseCommand
4
      command_name 'containers'
5
      desc         'import or export containers'
6
7
      REGISTRY = 'Registry'
8
      REPOSITORY = 'Repository:Tag'
9
      COMPUTERESOURCE = 'Compute Resource'
10
      ATTACH = 'Attach I/O'
11
      ENTRYPOINT = 'Entry Point'
12
      COMMAND = 'Command'
13
14 c06f1783 Tom McKay
      def export(csv)
15
        csv << [NAME, REGISTRY, REPOSITORY, COMPUTERESOURCE, ATTACH, ENTRYPOINT, COMMAND]
16 6498291e Tom McKay
        @api.resource(:containers).call(:index, {
17
            'per_page' => 999999,
18
            'search' => option_search
19
        })['results'].each do |container|
20 c06f1783 Tom McKay
          csv << [container['name'],
21
                  container['registry_name'],
22
                  "#{container['repository_name']}:#{container['tag']}",
23
                  container['compute_resource_name'],
24
                  export_attach_types(container),
25
                  container['entrypoint'],
26
                  container['command']]
27 c4dbc7a3 Tom McKay
        end
28
      end
29
30
      def import
31
        @existing = {}
32
33
        thread_import do |line|
34
          create_containers_from_csv(line)
35
        end
36
      end
37
38
      def create_containers_from_csv(line)
39
        # TODO: containers cannot be updated (no api)
40 02387fb5 Tom McKay
        # count(line[COUNT]).times do |number|
41 c4dbc7a3 Tom McKay
        #   name = namify(line[NAME], number)
42
        #   params =  { 'id' => foreman_container(:name => name),
43
        #               'container' => {
44
        #                 'name' => name,
45
        #                 'command' => line[COMMAND]
46
        #               }
47
        #             }
48
        #   print "Updating container '#{name}'..." if option_verbose?
49
        #   @api.resource(:containers).call(:update, params)
50
        # end
51
        # print "done\n" if option_verbose?
52
      end
53
54
      private
55
56
      def export_attach_types(container)
57
        types = []
58
        types << 'tty' if container['tty']
59
        types << 'stdin' if container['attach_stdin']
60
        types << 'stdout' if container['attach_stdout']
61
        types << 'stderr' if container['attach_stderr']
62
        types.join(',')
63
      end
64
    end
65
  end
66
end