Class: Ignition::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/ignition/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_shared_option(name, options = {}) ⇒ Object



23
24
25
26
# File 'lib/ignition/cli.rb', line 23

def add_shared_option(name, options = {})
  @shared_options = {} if @shared_options.nil?
  @shared_options[name] =  options
end

.is_thor_reserved_word?(word, type) ⇒ Boolean

Hackery. Take the run method away from Thor so that we can redefine it.

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/ignition/cli.rb', line 37

def is_thor_reserved_word?(word, type)
  return false if word == "run"
  super
end

.shared_options(*option_names) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/ignition/cli.rb', line 28

def shared_options(*option_names)
  option_names.each do |option_name|
    opt =  @shared_options[option_name]
    raise "Tried to access shared option '#{option_name}' but it was not previously defined" if opt.nil?
    option option_name, opt
  end
end

Instance Method Details

#infoObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ignition/cli.rb', line 128

def info
    puts "info bundle id #{options[:bundle_id]}"
    puts "load hostname:#{options[:hostname]}"
    puts "load port:#{options[:port]}"
    puts "load scale:#{options[:scale]}"

    uri = URI.parse("http://#{options[:hostname]}:#{options[:port]}/v1/ignition/bundles")
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Get.new(uri.request_uri)
    response = http.request(request)

    if response.code.to_i == 200
      bundles = JSON.parse(response.body)
      if options[:bundle_id] != "*"
        bundles.reject!{ |b| b.id != options[:bundle_id]}
      end
      BundleHelper.print_bundles(options[:hostname],bundles)
    else
      puts "ERROR - #{response.code}"
      puts response.body
    end
end

#load(bundle_path) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ignition/cli.rb', line 78

def load(bundle_path)
  if (!File.exists?(bundle_path))
    puts "ERROR: bundle file missing on path #{bundle_path}"
  else
    puts "load file:#{bundle_path}"
    puts "load hostname:#{options[:hostname]}"
    puts "load port:#{options[:port]}"

    raw_data = File.read(bundle_path)
    data = JSON.parse(raw_data)
    # puts data.class
    # pp data.to_hash
    params = {'data' => raw_data}

    pp params
    
    x = Net::HTTP.post_form(URI.parse("http://#{options[:hostname]}:#{options[:port]}/v1/ignition/bundles"), params)
    pp x

    puts x.body  
    puts x.code
  end
end

#logs(bundle_id) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/ignition/cli.rb', line 203

def logs(bundle_id)
    puts "info bundle id #{options[:bundle_id]}"
    puts "load hostname:#{options[:hostname]}"
    puts "load port:#{options[:port]}"

    uri = URI.parse("http://#{options[:hostname]}:#{options[:port]}/v1/ignition/bundle/#{bundle_id}/logs")
    http = Net::HTTP.new(uri.host, uri.port)
    req = Net::HTTP::Get.new(uri.path)
    response = http.request(req)

    if response.code.to_i == 200
      # puts "all ok"

      logs = JSON.parse(response.body)

      # pp logs
      logs.each do |log|
        puts log.strip
      end
    else
      puts "ERROR - #{response.code}"
      puts response.body
    end
end

#nodesObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ignition/cli.rb', line 55

def nodes
  # puts "Run Scan #{options[:port]}"
  # puts "TODO"

  uri = URI.parse("http://#{options[:hostname]}:#{options[:port]}/v1/ignition/nodes")
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)

  # puts response.body
  if response.code.to_i == 200
    nodes = JSON.parse(response.body)
    # BundleHelper.print_bundles(options[:hostname],bundles)
    # pp nodes
    tp(nodes.map{ |k,v| {:hostname => k, :last_seen => v, :secs_ago => (DateTime.now.to_time - DateTime.parse(v).to_time).to_i } })
  else
    puts "ERROR - #{response.code}"
    puts response.body
  end
end

#run(bundle_id) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ignition/cli.rb', line 107

def run(bundle_id)
    puts "run bundle id #{bundle_id}"
    puts "load hostname:#{options[:hostname]}"
    puts "load port:#{options[:port]}"
    puts "load scale:#{options[:scale]}"

    # Net::HTTP.post_form(URI.parse("http://#{options[:hostname]}:#{options[:port]}/v1/ignition/bundles"), params)
    uri = URI.parse("http://#{options[:hostname]}:#{options[:port]}/v1/ignition/bundle/#{bundle_id}/scale/#{options[:scale]}")
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Get.new(uri.request_uri)
    response = http.request(request)

    puts response.body  
    puts response.code
end

#stop(bundle_id) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ignition/cli.rb', line 156

def stop(bundle_id)
    puts "run bundle id #{bundle_id}"
    puts "load hostname:#{options[:hostname]}"
    puts "load port:#{options[:port]}"
    puts "load scale:#{options[:scale]}"

    # Net::HTTP.post_form(URI.parse("http://#{options[:hostname]}:#{options[:port]}/v1/ignition/bundles"), params)
    uri = URI.parse("http://#{options[:hostname]}:#{options[:port]}/v1/ignition/bundle/#{bundle_id}/scale/#{options[:scale]}")
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Get.new(uri.request_uri)
    response = http.request(request)

    puts response.body  
    puts response.code
end

#unload(bundle_id) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/ignition/cli.rb', line 177

def unload(bundle_id)
    puts "info bundle id #{options[:bundle_id]}"
    puts "load hostname:#{options[:hostname]}"
    puts "load port:#{options[:port]}"
    puts "load scale:#{options[:scale]}"

    uri = URI.parse("http://#{options[:hostname]}:#{options[:port]}/v1/ignition/bundle/#{bundle_id}")
    http = Net::HTTP.new(uri.host, uri.port)
    req = Net::HTTP::Delete.new(uri.path)
    response = http.request(req)
    puts "deleted #{response}"

    if response.code.to_i == 200
      puts "all ok"
    else
      puts "ERROR - #{response.code}"
      puts response.body
    end
end