Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / export.rb @ d76bdbc0

1
module HammerCLICsv
2
  class CsvCommand
3
    class ExportCommand < HammerCLI::Apipie::Command
4
      include ::HammerCLICsv::Utils::Config
5

    
6
      command_name 'export'
7
      desc         'export into directory'
8

    
9
      def self.supported?
10
        true
11
      end
12

    
13
      option %w(-v --verbose), :flag, _('be verbose')
14
      option %w(--threads), 'THREAD_COUNT', _('Number of threads to hammer with'),
15
             :default => 1, :hidden => true
16
      option '--dir', 'DIRECTORY', _('directory to export to')
17
      option %w(--organization), 'ORGANIZATION', _('Only process organization matching this name')
18

    
19
      RESOURCES = %w(
20
        settings organizations locations puppet_environments operating_systems
21
        domains architectures partition_tables lifecycle_environments host_collections
22
        provisioning_templates
23
        subscriptions activation_keys hosts content_hosts reports roles users
24
      )
25
      SUPPORTED_RESOURCES = %w(
26
        settings
27
      )
28
      RESOURCES.each do |resource|
29
        dashed = resource.sub('_', '-')
30
        option "--#{dashed}", 'FILE', "csv file for #{dashed}",
31
               :hidden => !SUPPORTED_RESOURCES.include?(resource)
32
      end
33

    
34
      def execute
35
        @api = api_connection
36
        skipped_resources = (RESOURCES - SUPPORTED_RESOURCES)
37

    
38
        (RESOURCES - skipped_resources).each do |resource|
39
          hammer_resource(resource)
40
        end
41

    
42
        HammerCLI::EX_OK
43
      end
44

    
45
      def hammer(context = nil)
46
        context ||= {
47
          :interactive => false,
48
          :username => @username,
49
          :password => @password
50
        }
51

    
52
        HammerCLI::MainCommand.new('', context)
53
      end
54

    
55
      def hammer_resource(resource)
56
        return if !self.send("option_#{resource}") && !option_dir
57
        options_file = self.send("option_#{resource}") || "#{option_dir}/#{resource.sub('_', '-')}.csv"
58
        args = []
59
        args += %W( --server #{@server} ) if @server
60
        args += %W( csv #{resource.sub('_', '-')} --export --file #{options_file} )
61
        args << '-v' if option_verbose?
62
        args += %W( --organization #{option_organization} ) if option_organization
63
        args += %W( --threads #{option_threads} ) if option_threads
64
        puts "Exporting '#{args.join(' ')}'" if option_verbose?
65
        hammer.run(args)
66
      end
67

    
68
      def check_server_status(server, username, password)
69
        url = "#{server}/api/status"
70
        uri = URI(url)
71
        nethttp = Net::HTTP.new(uri.host, uri.port)
72
        nethttp.use_ssl = uri.scheme == 'https'
73
        nethttp.verify_mode = OpenSSL::SSL::VERIFY_NONE
74
        server_status = nethttp.start do |http|
75
          request = Net::HTTP::Get.new uri.request_uri
76
          request.basic_auth(username, password)
77
          response = http.request(request)
78
          JSON.parse(response.body)
79
        end
80

    
81
        server_status
82
      end
83
    end
84
  end
85
end