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



159
160
161
162
163
164
165
166
167
168
# File 'lib/candelabra/installer.rb', line 159

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



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

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)


221
222
223
# File 'lib/candelabra/installer.rb', line 221

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



245
246
247
# File 'lib/candelabra/installer.rb', line 245

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)


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

def input?
  test ?p, input_path
end

.input_pathObject



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

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

.install_pianobarObject Also known as: update_pianobar

Instal



143
144
145
146
147
# File 'lib/candelabra/installer.rb', line 143

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



205
206
207
# File 'lib/candelabra/installer.rb', line 205

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

.output?Boolean

Returns:

  • (Boolean)


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

def output?
  test ?p, output_path
end

.output_pathObject



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

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

.piano_pathObject



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

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)


179
180
181
# File 'lib/candelabra/installer.rb', line 179

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



192
193
194
# File 'lib/candelabra/installer.rb', line 192

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



109
110
111
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
# File 'lib/candelabra/installer.rb', line 109

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( 2 )
    puts ''
    puts "Select Auto station".center( CONSOLE_WIDTH + 20, ' ' )
    stations = Remote.stations
    stations.each { |s| puts s }
    
    begin
      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
# File 'lib/candelabra/installer.rb', line 91

def start_pianobar
  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