Class: Powify::Server

Inherits:
Object
  • Object
show all
Extended by:
Powify
Defined in:
lib/powify/server.rb

Constant Summary collapse

AVAILABLE_METHODS =
%w(install reinstall update uninstall remove start stop restart host unhost status config list logs)

Constants included from Powify

POWPATH, VERSION

Class Method Summary collapse

Methods included from Powify

config, current_path, extension

Class Method Details

._extract_forward_info(port) ⇒ Object



122
123
124
125
126
# File 'lib/powify/server.rb', line 122

def _extract_forward_info(port)
  res_types = { 'p' => :pid, 'c' => :command }
  result = %x{lsof -iTCP:#{port} -Fpc}
  Hash[ result.split("\n").map { |line| [ res_types[line[0]], line[1..-1] ] } ]
end

.configObject

Print the current POW server configuration



114
115
116
117
118
119
120
# File 'lib/powify/server.rb', line 114

def config
  $stdout.puts "The current configuration of the pow server is:\n\n"
  result = %x{curl localhost/config.json --silent --header host:pow}
  json = JSON.parse(result)
  json.each_pair {|k,v| $stdout.puts "  #{k}: #{v}"}
  $stdout.puts "\n"
end

.hostObject

Add POW domains to the hosts file

Original Author: Christopher Lindblom (github.com/lindblom) Original Context: github.com/lindblom/powder/commit/8b2f2609e91ddbc72f53c7fbb6daee92a82e21c0

This method was taken from Christopher Lindlom pull request to powder, a similar gem for managing pow applications. I DID NOT write this code (although I tested it), so don’t give me any credit!



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/powify/server.rb', line 62

def host
  hosts_file_path = '/etc/hosts'
  hosts_file = File.read(hosts_file_path)
  return $stdout.puts 'Pow is already in the hosts file. Please run `powify server unhost`' if hosts_file =~ /(#powify)/ || File.exists?("#{hosts_file_path}.powify.bak")

  # break our hosts file into lines
  hosts_file = hosts_file.split("\n")
  pow_domains = Dir["#{POWPATH}/*"].collect { |a| "127.0.0.1\t#{File.basename(a)}.#{extension}\t#powify" }

  # find the loop back and insert our domains after
  first_loopback_index = hosts_file.index{ |i| i =~ /^(127.0.0.1).+/ }
  hosts_file = hosts_file.insert(first_loopback_index + 1, pow_domains)

  %x{sudo cp #{hosts_file_path} #{hosts_file_path}.powify.bak}
  File.open(hosts_file_path, 'w+') { |f| f.puts hosts_file.join("\n") }

  %x{dscacheutil -flushcache}
  $stdout.puts "All Pow apps were added to the hosts file."
  $stdout.puts "The old host file is saved at #{hosts_file_path}.powify.bak."
end

.installObject Also known as: reinstall, update

Install the POW server



18
19
20
21
22
# File 'lib/powify/server.rb', line 18

def install
  $stdout.puts "Installing/Re-installing/Updating pow server..."
  %x{curl get.pow.cx | sh}
  $stdout.puts "Done!"
end

.listObject

List all active POW applications currently on the server



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/powify/server.rb', line 128

def list
  $stdout.puts "The following POW applications are available:\n\n"
  Dir["#{POWPATH}/*"].each do |a|
    if File.symlink?(a)
      $stdout.puts "  #{File.basename(a)} -> #{File.readlink(a)}"
    else
      port = File.open(a) { |f| f.readline.to_i }
      info = _extract_forward_info(port)
      $stdout.puts "  #{File.basename(a)} -> forwarding to :#{port} for" +
                   " #{info.fetch(:command, 'Unknown')}[#{info.fetch(:pid, '???')}]"
    end
  end
  $stdout.puts "\nRun `powify open [APP_NAME]` to browse an app"
end

.logsObject

Tail the server logs



144
145
146
# File 'lib/powify/server.rb', line 144

def logs
  system "tail -f ~/Library/Logs/Pow/access.log"
end

.restartObject

Restart the POW server



51
52
53
# File 'lib/powify/server.rb', line 51

def restart
  stop && start
end

.run(args = []) ⇒ Object



11
12
13
14
15
# File 'lib/powify/server.rb', line 11

def run(args = [])
  method = args[0].to_s.downcase
  raise "The command `#{args.first}` does not exist for `powify server`!" unless Powify::Server::AVAILABLE_METHODS.include?(method)
  self.send(method)
end

.startObject

Start the POW server (command taken from 37 Signals installation script)



35
36
37
38
39
40
# File 'lib/powify/server.rb', line 35

def start
  $stdout.puts "Starting the pow server..."
  %x{sudo launchctl load -Fw /Library/LaunchDaemons/cx.pow.firewall.plist 2>/dev/null}
  %x{launchctl load -Fw "$HOME/Library/LaunchAgents/cx.pow.powd.plist" 2>/dev/null}
  $stdout.puts "Done!"
end

.statusObject

Print the current POW server status



105
106
107
108
109
110
111
# File 'lib/powify/server.rb', line 105

def status
  $stdout.puts "The current status of the pow server is:\n\n"
  result = %x{curl localhost/status.json --silent --header host:pow}
  json = JSON.parse(result)
  json.each_pair { |k,v| $stdout.puts "  #{k}: #{v}" }
  $stdout.puts "\n"
end

.stopObject

Stop the POW server (command taken from 37 Signals installation script)



43
44
45
46
47
48
# File 'lib/powify/server.rb', line 43

def stop
  $stdout.puts "Stopping the pow server..."
  %x{launchctl unload "$HOME/Library/LaunchAgents/cx.pow.powd.plist" 2>/dev/null}
  %x{sudo launchctl unload "/Library/LaunchDaemons/cx.pow.firewall.plist" 2>/dev/null}
  $stdout.puts "Done!"
end

.unhostObject

Remove POW domains from the hosts file

Original Author: Christopher Lindblom (github.com/lindblom) Original Context: github.com/lindblom/powder/commit/8b2f2609e91ddbc72f53c7fbb6daee92a82e21c0

This method was taken from Christopher Lindlom pull request to powder, a similar gem for managing pow applications. I DID NOT write this code (although I tested it), so don’t give me any credit!



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/powify/server.rb', line 90

def unhost
  hosts_file_path = '/etc/hosts'
  hosts_file = File.read(hosts_file_path)
  return $stdout.puts 'Pow is not in the host file, and there is no backup file. Please run `powify server host`' unless hosts_file =~ /.+(#powify)/ || File.exists?("#{hosts_file_path}.powify.bak")

  hosts_file = hosts_file.split("\n").delete_if { |row| row =~ /.+(#powify)/ } # remove any existing records

  File.open(hosts_file_path, 'w+') { |f| f.puts hosts_file.join("\n") }
  %x{sudo rm #{hosts_file_path}.powify.bak}

  %x{dscacheutil -flushcache}
  $stdout.puts "All Pow apps were removed from the hosts file."
end

.uninstallObject Also known as: remove

Uninstall the POW server



27
28
29
30
31
# File 'lib/powify/server.rb', line 27

def uninstall
  $stdout.puts "Uninstalling/Removing pow server..."
  %x{curl get.pow.cx/uninstall.sh | sh}
  $stdout.puts "Done!"
end