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, password_prompt = true, uniform_password = false) ⇒ Object



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

def self.exec_script(hosts, script, user, password_prompt=true, uniform_password=false)
  workers = []
  
  hosts.each do |host|
    prompt_for_password(password_prompt, 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)
    workers << worker
  end
  
  Parallel.each(workers, :in_threads => workers.length) do |worker|
    puts "#{worker.host} -- #{worker.exec_script(script)}"
  end
  
end

.inline_bash(hosts, bash, user, password_prompt = true, uniform_password = false) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/octosh/cli.rb', line 105

def self.inline_bash(hosts, bash, user, password_prompt=true, uniform_password=false)
  workers = []
  
  hosts.each do |host|
    prompt_for_password(password_prompt, 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)
    workers << worker
  end
  
  Parallel.each(workers, :in_threads => workers.length) do |worker|
    puts "#{worker.host} -- #{worker.exec(bash)}"
  end
end

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



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/octosh/cli.rb', line 92

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



18
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
# File 'lib/octosh/cli.rb', line 18

def self.start
  options = OpenStruct.new

  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('-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
    
    options.default_user = "root"
    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
    
    options.password_prompt = true
    options.uniform_password = false
    opts.on('-p', '--uniform-password', 'Uniform password') do
      options.uniform_password = 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.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.password_prompt, options.uniform_password)
    elsif options.script
      puts "Call script on each server"
      self.exec_script(options.hosts, options.script, options.default_user, options.password_prompt, options.uniform_password)
    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