Class: CKnife::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/cknife/command_line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f, s, o, opts) ⇒ CommandLine

Returns a new instance of CommandLine.



6
7
8
9
10
11
# File 'lib/cknife/command_line.rb', line 6

def initialize(f, s, o, opts)
  self.option_file = f
  self.option_file_contents = s
  self.thor_output = o
  self.options = opts
end

Instance Attribute Details

#option_fileObject

Returns the value of attribute option_file.



4
5
6
# File 'lib/cknife/command_line.rb', line 4

def option_file
  @option_file
end

#option_file_contentsObject

Returns the value of attribute option_file_contents.



4
5
6
# File 'lib/cknife/command_line.rb', line 4

def option_file_contents
  @option_file_contents
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/cknife/command_line.rb', line 4

def options
  @options
end

#thor_outputObject

Returns the value of attribute thor_output.



4
5
6
# File 'lib/cknife/command_line.rb', line 4

def thor_output
  @thor_output
end

Instance Method Details

#create_opt_file(connect_msg) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cknife/command_line.rb', line 37

def create_opt_file(connect_msg)
  if File.exists?(option_file)
    thor_output.say("A #{option_file} file is already present.")
    thor_output.say(connect_msg)
    return
  end

  write_option_file
  thor_output.say("Wrote #{option_file} to $CWD.")
  thor_output.say(connect_msg)
  thor_output.say("Remember to delete the #{option_file} file when you are finished.")
end

#dc(cmd) ⇒ Object

dump command?



14
15
16
# File 'lib/cknife/command_line.rb', line 14

def dc(cmd)
  puts "PGPASSFILE=#{pg_pass_file} #{cmd}"
end

#delete_opt_fileObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cknife/command_line.rb', line 22

def delete_opt_file
  if !File.exists?(option_file)
    thor_output.say("No #{option_file} file to delete.")
    return
  end

  s = File.read(option_file)
  if s == option_file_contents
    File.unlink(option_file)
    thor_output.say("Deleted #{option_file} file.")
  else
    thor_output.say("The #{option_file} file's contents do not match what this tool would have generated. Assuming you are trying to delete a #{option_file} file that this tool did not generate, please inspect the file to ensure it contains what you expect, and then delete it yourself.", :red)
  end
end

#execute(cmd, input = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cknife/command_line.rb', line 50

def execute(cmd, input = nil)
  return if !@session_ok
  dc(cmd) if options[:verbose]
  stdin, stdout, stderr, wait_thread = Open3.popen3(cmd)
  if input
    puts input if options[:verbose]
    stdin.write input
    stdin.close
  end
  output = stdout.read
  output += stderr.read
  $stdout.write output if options[:verbose]
  stdout.close
  stderr.close
  result = wait_thread.value.to_i

  if result != 0
    @session_ok = false
    msg = "An error occurred."
    msg += " If the --verbose flag is available for this command, you may try turning it on." if !options[:verbose]
    thor_output.say(msg, :red)
  end

  yield if block_given?
  output
end

#with_option_fileObject



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
105
106
107
108
109
110
111
112
113
# File 'lib/cknife/command_line.rb', line 77

def with_option_file
  if @session_live
    return yield
  end

  @session_live = true
  @session_ok = true

  existing_option_file = false
  if File.exists?(option_file)
    existing_option_file = true
    s = File.read(option_file)
    if s != option_file_contents
      thor_output.say("A #{option_file} file is present, but it does not match your database configuration. The contents of the #{option_file} file must exactly match what this tool would generate. Please reconcile the #{option_file} file with your configuration, and then try again. You can also delete the #{option_file} file since this tool generates one in order to do its job (and removes it after finishing).", :red)
      return
    end
  end

  write_option_file

  result = nil
  begin
    result = yield self # don't know what we're planning to do with the result here...
  ensure
    if !existing_option_file
      FileUtils.rm(option_file)
      if File.exists?(option_file)
        thor_output.say("Failed to remove #{option_file} file. Please remove it for your infrastructure's security.")
      end
    else
      thor_output.say("Left existing #{option_file} file on disk.", :yellow)
    end
  end

  @session_live = false
  result
end

#write_option_fileObject



18
19
20
# File 'lib/cknife/command_line.rb', line 18

def write_option_file
  File.open(option_file, "w", 0600) { |f| f.write option_file_contents }
end