Class: Anamo::Node::Thor

Inherits:
Thor
  • Object
show all
Defined in:
lib/anamo/node/thor.rb

Instance Method Summary collapse

Instance Method Details

#configureObject



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/anamo/node/thor.rb', line 55

def configure

  configuration = {}

  say "The Anamo configuration setup tool will walk you through the process\nof defining your configuration for:\n\n", [:green, :bold]
  say "    #{config_file_path}\n\n", :green

  configuration['api_key'] = ''
  until configuration['api_key'].strip.length > 0 do
    configuration['api_key'] = ask "What is your Anamo API key?\n", :bold
  end

  configuration['modules'] = {}

  if ['y','yes'].include? ask("Would you like to configure send frequencies? (y/n)\n", :bold).downcase
    freq = ask "How often should filesystem data be sent to server? (in seconds -- default: 240 seconds)\n", :bold
    if freq.strip.length > 0
      configuration['modules']['fstree'] = {
        'frequency' => freq.to_i
      }
    end
    freq = ask "How often should package data be sent to server? (in seconds -- default: 60 seconds)\n", :bold
    if freq.strip.length > 0
      configuration['modules']['pkgver'] = {
        'frequency' => freq.to_i
      }
    end
    freq = ask "How often should port data be sent to server? (in seconds -- default: 60 seconds)\n", :bold
    if freq.strip.length > 0
      configuration['modules']['ports'] = {
        'frequency' => freq.to_i
      }
    end
  end

  say "\nYour configuration file:\n\n", [:green, :bold]

  configuration_yaml = YAML.dump configuration

  say configuration_yaml

  if ['yes', 'y'].include? ask("\nWould you like to save this file now? (\"y\" to save)", :bold).downcase
    File.open(config_file_path, 'w') { |file| file.write configuration_yaml }
    File.chmod(0600, config_file_path)
    say "Configurated saved at: #{config_file_path}", [:cyan, :bold]
  else
    say 'Configuration not saved.', [:red, :bold]
  end

end

#execObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/anamo/node/thor.rb', line 16

def exec

  data = inspect false
  response = ::Anamo::Api.new.post_node MultiJson.dump data

  response_data = MultiJson.load response.body
  if response_data.respond_to?(:has_key?) and response_data.has_key?('client_key')
    File.open(key_file, 'w') { |file| file.write response_data['client_key'] }
    File.chmod(0600, key_file)
  end

end

#inspect(output = true) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/anamo/node/thor.rb', line 33

def inspect output = true

  ret = {}

  config = YAML.load_file "/etc/anamo.conf.yml"

  ret['api_key'] = config['api_key']
  ret['beacon_modules'] = config['modules']
  ret['node'] = {}
  ret['node']['hostname'] = Socket.gethostname
  ret['node']['ips'] = Socket.ip_address_list.map(){ |entry| entry.ip_address }.uniq

  puts ret if output

  ret

end