Module: Candelabra::Installer

Extended by:
OSX, Ubuntu
Defined in:
lib/candelabra/installer.rb

Overview

This module handles the installing of pianobar Candelabra will support two different ways of installing First is via osx # => brew Second is ubuntu # => apt-get

It will also know if pianobar is installed. Because packagemanagers are a good thing Candelabra will allow the packagemanage handle all the hard work with keeping the version correct

For Example:

Candelabra::Installer.install 'pianobar'
  # => brew install pianobar
  # => sudo apt-get install pianobar

Constant Summary collapse

CONSOLE_WIDTH =
70

Class Method Summary collapse

Methods included from OSX

extended, included, osx?

Methods included from Ubuntu

extended, included, linux?

Class Method Details

.ask(question, visiable = true) ⇒ Object

Helper for asking a question

Params:

question => the question u want to ask maybe?
visiable => this  will determine if  the output  should be
  displayed or not

Returns result of the user’s input



162
163
164
165
166
167
168
169
170
171
# File 'lib/candelabra/installer.rb', line 162

def ask( question, visiable=true )
  begin
    `stty echo`
    print question
    `stty -echo` unless visiable
    gets.chomp
  ensure
    `stty echo`
  end
end

.backup_config_nameObject



76
77
78
# File 'lib/candelabra/installer.rb', line 76

def backup_config_name
  [config_path, @username, Dir.glob(config_path + '*').size.to_s].join('.')
end

.config_pathObject



232
233
234
# File 'lib/candelabra/installer.rb', line 232

def config_path
  "#{piano_path}/config"
end

.config_template(station_id = nil) ⇒ Object



80
81
82
83
84
# File 'lib/candelabra/installer.rb', line 80

def config_template( station_id = nil )
  exe_path = "#{File.dirname(__FILE__)}/../../bin/candelabra"
  station  = station_id
  get_template('config', binding)
end

.ctl?Boolean

Pianobar can talk to a remote file. This will determin if the file is setup.

Returns true when setup in the expected location

Returns:

  • (Boolean)


224
225
226
# File 'lib/candelabra/installer.rb', line 224

def ctl?
  test ?p, ctl_path
end

.ctl_pathObject

The path of the control file. This file must be a fifo file inorder for it to be the correct ctl file.

Returns the path to the control file



248
249
250
# File 'lib/candelabra/installer.rb', line 248

def ctl_path
  "#{piano_path}/ctl"
end

.get_template(name, this_binding = nil) ⇒ Object



86
87
88
89
# File 'lib/candelabra/installer.rb', line 86

def get_template( name, this_binding = nil )
  erb = ERB.new(File.read(File.dirname(__FILE__) + "/templates/#{name}.erb"))
  erb.result this_binding
end

.headerObject



35
36
37
# File 'lib/candelabra/installer.rb', line 35

def header
  puts get_template 'header'
end

.input?Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/candelabra/installer.rb', line 216

def input?
  test ?p, input_path
end

.input_pathObject



240
241
242
# File 'lib/candelabra/installer.rb', line 240

def input_path
  "#{piano_path}/input.fifo"
end

.install_pianobarObject Also known as: update_pianobar

Instal



146
147
148
149
150
# File 'lib/candelabra/installer.rb', line 146

def install_pianobar
  print "Installing Pianobar".ljust(CONSOLE_WIDTH, '.')
  install 'pianobar' unless pianobar?
  print pianobar? ? 'SUCCESS'.green : 'FAILED'.red.blink
end

.make_fifosObject



69
70
71
72
73
# File 'lib/candelabra/installer.rb', line 69

def make_fifos
  mkfifo( ctl_path )    unless ctl?
  mkfifo( output_path ) unless output?
  mkfifo( input_path )  unless input?
end

.mkfifo(path) ⇒ Object

Util method. Should be moved to the install module when it has been created.

Example:

Candelabra::Pianobar.make_logs
  # => doesn't really belong here

Returns nothing. but it makes the logs dir



208
209
210
# File 'lib/candelabra/installer.rb', line 208

def mkfifo( path )
  %x[mkfifo "#{path}"]
end

.output?Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/candelabra/installer.rb', line 212

def output?
  test ?p, output_path
end

.output_pathObject



236
237
238
# File 'lib/candelabra/installer.rb', line 236

def output_path
  "#{piano_path}/output.fifo"
end

.piano_pathObject



228
229
230
# File 'lib/candelabra/installer.rb', line 228

def piano_path
  "#{ENV['HOME']}/.config/pianobar"
end

.pianobar?Boolean

Checking to determine if pianobar is installed it is just looking for the executable.

Example:

Candelabra::Install.pianobar?
  # => true if pianobar is in the path

Returns true when pianobar is installed

Returns:

  • (Boolean)


182
183
184
# File 'lib/candelabra/installer.rb', line 182

def pianobar?
  !pianobar_path.empty?
end

.pianobar_pathObject

Gets the path to pianobar. If it’s installed on the system and in your path it will be located. This path is what is used to determine if pianobar needs to be installed

Example:

Candelabra::Installer.pianobar_path
  # => /usr/local/bin/pianobar

Returns a string



195
196
197
# File 'lib/candelabra/installer.rb', line 195

def pianobar_path
  %x[which pianobar]
end

.runObject

Executing the installing process.



25
26
27
28
29
30
31
32
33
# File 'lib/candelabra/installer.rb', line 25

def run
  Pianobar.stop_all # make sure all are off
  header
  what_is_installed?
  install_pianobar
  setup_config_file
  make_fifos
  setup_auto_play_station
end

.setup_auto_play_stationObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/candelabra/installer.rb', line 112

def setup_auto_play_station
  puts "Testing Configuration".center(CONSOLE_WIDTH + 20)
  start_pianobar


  if Pianobar.running?
    `echo '0' > #{Installer.input_path}` # forcing auto selection of the first station
    sleep( 5 )
    puts ''
    puts "Select Auto station".center( CONSOLE_WIDTH + 20, ' ' )
    
    begin
      stations = Remote.stations
      stations.each { |s| puts s }
      result = ask 'Select Station and press ENTER:'
      raise "You must enter the number of a valid station" unless result == result.to_i.to_s
      raise "That is not a valid station it must be a number between 0 and #{stations.size - 1}" unless (0..stations.size - 1).include? result.to_i
      puts "You selected: #{stations[result.to_i]}"
    rescue RuntimeError => e
      puts e.message.red
      puts "You Entered: #{result.red}"
      puts "Try again"
      retry
    end

    Remote.change_station result

    write_config_file( Remote.station_id )

    start_pianobar
  end
end

.setup_config_fileObject



55
56
57
58
59
# File 'lib/candelabra/installer.rb', line 55

def setup_config_file
  puts get_template 'setup_config_file'
  @username, @password  = ask( "Enter username:" ), ask( "Enter password:", false )
  write_config_file 
end

.start_pianobarObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/candelabra/installer.rb', line 91

def start_pianobar
  Remote.flush
  Remote.flush_input

  if Pianobar.running?
    print "Restarting Pianobar with Autostation".ljust(CONSOLE_WIDTH + 20, '.')
    Pianobar.restart
  else
    print "Starting Pianobar".ljust(CONSOLE_WIDTH - 5, '.')
    Pianobar.start
  end

  5.times do 
    sleep(1)
    putc '.'
  end

  print Pianobar.running? ? "SUCCESS".green : "FAILED".red.blink
  puts ""
end

.what_is_installed?Boolean

Display what is installed to the user

Return if all is ok to install

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/candelabra/installer.rb', line 42

def what_is_installed?
  which_os        = os.green
  package_manager = ( has_installer?  ? 'Yes'.green : 'No'.red.blink )
  exe_installed   = ( pianobar?       ? 'Yes'.green : 'No'.red.blink )
  ctl_file        = ( ctl?            ? 'Yes'.green : 'No'.red.blink )
  has_notifier    = ( notify?         ? 'Yes'.green : 'No'.red.blink )
  ok              = ( has_installer?  ? 'Yes'.green : 'No'.red.blink )

  puts get_template 'what_is_installed', binding

  has_installer?
end

.write_config_file(station = nil) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/candelabra/installer.rb', line 61

def write_config_file( station = nil )
  FileUtils.mkdir_p piano_path
  FileUtils.mv config_path, backup_config_name if File.exists? config_path
  File.open( config_path, 'w' ) do |f| 
    f.write config_template( station )
  end
end