Module: NexClient::Commands::Helpers

Included in:
Addons, Apps, CubeInstances, CubeTemplates, Domains, ExecTasks, Organizations, Racks, SslCertificates, Users
Defined in:
lib/nex_client/commands/helpers.rb

Constant Summary collapse

LOG_COLORS =
[:cyan,:green,:red,:yellow,:magenta]
VARS_TITLE =
"Environment Variables".colorize(:blue)
VARS_HEADERS =
['key','value'].map(&:upcase)

Instance Method Summary collapse

Instance Method Details

#display_events(events) ⇒ Object

Display a list of events



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/nex_client/commands/helpers.rb', line 56

def display_events(events)
  # Content
  events.sort_by { |e| e.id.to_i }.each do |e|
    username = e&.username || 'system'
    session_id = e&.session_id || '-'
    puts [
      e.created_at,
      e.event.ljust(12,' '),
      username.ljust(15,' '),
      session_id.ljust(6,' '),
      e.level.ljust(6,' '),
      e.message
    ].join(" | ")
  end
  puts "\n"
end

#display_logs(logs) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nex_client/commands/helpers.rb', line 24

def display_logs(logs)
  color_index = 0
  logs.each do |container_id,log_lines|
    color_index = (color_index + 1) % LOG_COLORS.size
    puts "\n"
    puts "Node: #{container_id}".colorize(LOG_COLORS[color_index])
    puts "-"*50
    puts log_lines.join("\n")
  end
  puts "\n"
end

#display_raw_vars(list) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/nex_client/commands/helpers.rb', line 170

def display_raw_vars(list)
  table = [list].flatten.compact.map do |e|
    e.sort_by { |k, v| k }.map { |k,v| "#{k}=#{v}" }
  end.flatten
  puts table
  puts "\n"
end

#display_record_errors(record) ⇒ Object



10
11
12
13
14
# File 'lib/nex_client/commands/helpers.rb', line 10

def display_record_errors(record)
  record.errors.messages.each do |k,v|
    v.each { |m| puts "#{k} #{m}" }
  end
end

#display_vars(list, truncate = true) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/nex_client/commands/helpers.rb', line 183

def display_vars(list,truncate = true)
  table = Terminal::Table.new title: VARS_TITLE, headings: VARS_HEADERS do |t|
    [list].flatten.compact.each do |e|
      e.sort_by { |k, v| k }.each do |k,v|
        val = truncate ? v.to_s.truncate(100) : v
        t.add_row([k,val])
      end
    end
  end
  puts table
  puts "\n"
end

#display_yaml_vars(vars) ⇒ Object



178
179
180
181
# File 'lib/nex_client/commands/helpers.rb', line 178

def display_yaml_vars(vars)
  puts vars.to_yaml
  puts "\n"
end

#error(msg) ⇒ Object



20
21
22
# File 'lib/nex_client/commands/helpers.rb', line 20

def error(msg)
  puts msg.colorize(:red)
end

#events(args, opts) ⇒ Object

Retrieve resource events



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/nex_client/commands/helpers.rb', line 37

def events(args,opts)
  filters = {}
  filters[:'source.name'] = args.first
  filters[:event] = opts.type if opts.type
  tail_size = (opts.tail || 50).to_i

  events = NexClient::Event.where(filters).order(id: :desc)
  list = events.to_a
  while list.count < tail_size
    events = events.pages.next
    break unless events
    list |= events.to_a
  end
  list = list.first(tail_size)

  self.display_events(list.to_a)
end

#format_cmd(cmd, values = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/nex_client/commands/helpers.rb', line 97

def format_cmd(cmd,values = {})
  formatted = cmd

  # Replace declared values
  values.each do |k,v|
    formatted = formatted.gsub("{{#{k}}}",v)
  end

  # Remove remaining template values
  formatted.gsub(/{{[^{}]+}}/,'')
end

#hash_from_file(file) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/nex_client/commands/helpers.rb', line 127

def hash_from_file(file)
  if file =~ /(\.yml|\.yaml)$/
    YAML.load_file(file)
  else
    JSON.parse(File.read(file))
  end
end

#perform_ssh_cmd(ssh_cmd_template, exec_cmd = nil) ⇒ Object

Perform an SSH command based on a template Fetch current user key and username



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/nex_client/commands/helpers.rb', line 111

def perform_ssh_cmd(ssh_cmd_template, exec_cmd = nil)
  unless ssh_cmd_template
    error('Error! Cannot perform SSH. Command Template was empty. Are you sure the app is running ?')
    return false
  end

  with_cert_identity do |username,pv_key_path|
    # Format command
    ssh_cmd = format_cmd(ssh_cmd_template, username: username, certfile: pv_key_path)
    ssh_cmd += " #{exec_cmd}" if exec_cmd

    # Run command
    system(ssh_cmd)
  end
end

#success(msg) ⇒ Object



16
17
18
# File 'lib/nex_client/commands/helpers.rb', line 16

def success(msg)
  puts msg.colorize(:green)
end

#vars_from_file(file, prefix = nil, &block) ⇒ Object

Parse plain or yaml file



136
137
138
139
140
141
142
143
# File 'lib/nex_client/commands/helpers.rb', line 136

def vars_from_file(file,prefix = nil,&block)
  # YAML file
  if file =~ /(\.yml|\.yaml)$/
    vars_from_yaml_file(file,prefix,&block)
  else
    vars_from_plain_file(file,prefix,&block)
  end
end

#vars_from_plain_file(file, prefix = nil, &block) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/nex_client/commands/helpers.rb', line 159

def vars_from_plain_file(file,prefix = nil,&block)
  f = File.read(file)
  f.split("\n").each do |e|
    line = e.strip
    next if line.empty? || line.start_with?('#')
    key,val = e.split('=',2)
    key = prefix.present? ? "#{prefix}_#{key}" : key
    yield(key,val)
  end
end

#vars_from_yaml_file(file, prefix = nil, &block) ⇒ Object

Accept a path to a file or a hash



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/nex_client/commands/helpers.rb', line 146

def vars_from_yaml_file(file,prefix = nil,&block)
  vars = file.is_a?(Hash) ? file : YAML.load_file(file)

  vars.each do |k,v|
    key = prefix.present? ? "#{prefix}_#{k}" : k
    if v.is_a?(Hash)
      self.vars_from_yaml_file(v,key,&block)
    else
      yield(key,v)
    end
  end
end

#with_cert_identityObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/nex_client/commands/helpers.rb', line 73

def with_cert_identity
  # Fetch user
  me = NexClient::Me.find.first
  if me.api_only
    error('Error! Cannot SSH with an api-only user')
    return false
  end

  # Get SSH details
  username = me.handle.downcase
  pv_key = me.private_key

  # Create SSH Key
  pv_key_file = Tempfile.new('nex.sshkey')
  pv_key_file.write(pv_key)
  pv_key_file.close

  begin
    yield(username,pv_key_file.path)
  ensure
    pv_key_file.unlink # delete tmp file
  end
end