Class: Octosh::CLI

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

Defined Under Namespace

Classes: MODE

Class Method Summary collapse

Class Method Details

.exec_script(hosts, script, user, options) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/octosh/cli.rb', line 149

def self.exec_script(hosts, script, user, options)
  workers = []
  
  hosts.each do |host|
    prompt_for_password(options[:password_prompt], options[:uniform_password])
    exec_user,hostname = ""
    if host.include? '@'
      # USer defined with host, override provided user
      exec_user,hostname = host.split('@')
    else
      exec_user = user
      hostname = host
    end
    worker = Octosh::Worker.new(hostname, exec_user, @password, options)
    workers << worker
  end
  
  Parallel.each(workers, :in_threads => workers.length) do |worker|
    worker.exec_script(script) do |output|
      puts "#{worker.host} -- #{output}"
    end
  end
  
end

.inline_bash(hosts, bash, user, options) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/octosh/cli.rb', line 123

def self.inline_bash(hosts, bash, user, options)
  workers = []
  
  hosts.each do |host|
    prompt_for_password(options[:password_prompt], options[:uniform_password])
    exec_user,hostname = ""
    if host.include? '@'
      # USer defined with host, override provided user
      exec_user,hostname = host.split('@')
    else
      exec_user = user
      hostname = host
    end
    
    worker = Octosh::Worker.new(hostname, exec_user, @password, options)
    workers << worker
  end
  
  Parallel.each(workers, :in_threads => workers.length) do |worker|
    #puts "#{worker.host} -- #{}"
    worker.exec(bash) do |output|
      puts "#{worker.host} -- #{output}"
    end
  end
end

.prompt_for_password(password_prompt, uniform_password, host = "current host") ⇒ Object



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

def self.prompt_for_password(password_prompt, uniform_password, host="current host")
  if password_prompt
    # Password authentication
    if uniform_password and @password.nil?
      # One password for all hosts
      @password = Octosh::Helper.password_prompt("Password: ")
    elsif not uniform_password
      # One password for each host
      @password = Octosh::Helper.password_prompt("Password for #{host}: ")
    end
  end
end

.startObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
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
105
106
107
108
# File 'lib/octosh/cli.rb', line 19

def self.start
  options = {}
  
  # Default configuration
  options[:default_user] = "root"
  options[:password_prompt] = true
  options[:uniform_password] = false
  options[:forward_agent] = false
  options[:interactive] = false

  optparse = OptionParser.new do|opts|
    opts.banner = "Usage: octosh [options] [octo config file]"
    
    opts.on('-c', '--config FILE', 'Octo config file') do |file|
      options[:config] = file
    end
    
    opts.on('-i', '--interactive', 'Interactive shell prompt') do
      options[:interactive] = true
    end
      
    opts.on('-b', '--bash COMMAND', 'Explicitly define a command(s) to run on all hosts (Requires --hosts switch)') do |bash|
      options[:bash] = bash
    end
    
    opts.on('-s', '--script SCRIPT', 'Path to script to run on all hosts (Requires --hosts switch)') do |script|
      options[:script] = script
    end
    
    opts.on('-r', '--hosts USER@HOST,USER@HOST', Array, 'Lists of hosts to use when using inline execution (with -b or -s switches)') do |list|
      options[:hosts] = list
    end
    
    opts.on('-u', '--user USER', 'User to use when a user isn\'t defined in the --hosts list (ie. just IP address)') do |user|
      options[:default_user] = user
    end
    
    opts.on('-p', '--uniform-password', 'Uniform password') do
      options[:uniform_password] = true
    end
    
    opts.on('--forward-agent', 'Forward Agent') do
      options[:forward_agent] = true
    end
    
    opts.on_tail('-h', '--help', 'Display this screen' ) do
      puts opts
      exit
    end
  end.parse!
  
  if not ARGV.empty? and not options[:config]
    puts "Using config file"
    options[:config] = ARGV[0]
    options[:mode] = Octosh::CLI::MODE::CONFIG
  elsif ARGV.empty? and options[:config]
    puts "Using config file"
    options[:mode] = Octosh::CLI::MODE::CONFIG
  elsif not ARGV.empty? and options[:config]
    puts "Two config files specified (#{options[:config]} and #{ARGV[0]}), using explicit config file (#{options[:config]})"
    options[:mode] = Octosh::CLI::MODE::CONFIG
  elsif options[:interactive]
    puts "Interactive mode"
    options[:mode] = Octosh::CLI::MODE::INTERACTIVE
    shell = Octosh::Shell.new(options[:hosts], options)
    shell.start
  elsif (options[:bash] or options[:script]) and options[:hosts]
    puts "Using inline execution"
    options[:mode] = Octosh::CLI::MODE::INLINE
    
    if options[:bash] and options[:script]
      "Error -- Cannot specify both an inline command to run (-b) and a script file (-s)"
      exit
    elsif options[:bash]
      puts "Inline bash"
      self.inline_bash(options[:hosts], options[:bash], options[:default_user], options)
    elsif options[:script]
      puts "Call script on each server"
      self.exec_script(options[:hosts], options[:script], options[:default_user], options)
    else
      "Error -- Something broke"
      exit
    end
      
  else
    puts "Error -- Must either provide an Octo config file or arguments for inline execution (--hosts along with -b or -s)"
    exit
  end

end