-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThorfile
More file actions
144 lines (117 loc) · 3.19 KB
/
Thorfile
File metadata and controls
144 lines (117 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
141
142
143
144
require './environment'
require 'uri'
module Commands
def ssh_command
"ssh #{ENV['PI_USER']}@#{ENV['PI_HOSTNAME']}"
end
def copy_command(origin, destination)
"sudo cp #{origin} #{destination}"
end
def remotely(*args)
"#{ssh_command} '#{args.join(';')}'"
end
def console_command
'bundle exec irb -r ./environment.rb'
end
def restart_command
'sudo restart jukebox'
end
def upload_command
"rsync -avzhe ssh --delete-after --exclude '.git' --exclude 'vendor/bundle' --exclude '.bundle' . #{ENV['PI_USER']}@#{ENV['PI_HOSTNAME']}:/home/pi/jukebox"
end
end
class Pi < Thor
include Commands
include Thor::Actions
HOSTNAME='raspberrypi.local'
USER='pi'
desc 'ssh', 'SSH into the raspberry pi'
def ssh
run ssh_command
end
desc 'reboot', 'Reboots the raspberry pi'
def reboot
run remotely("sudo reboot")
end
desc 'upload', 'Uploads project files into raspberry pi'
def upload
run upload_command
end
desc 'deploy', 'Deploys the project into raspberry pi'
def deploy
run 'bundle package'
run upload_command
run remotely('cd jukebox', 'bundle install --local --deployment', restart_command)
end
desc 'restart', 'Restart the jukebox process'
def restart
run remotely(restart_command)
end
class Network < Pi
desc 'wifi', 'Set raspberry pi on wifi network mode'
def wifi
run remotely(copy_command('/etc/network/interfaces.wifi', '/etc/network/interfaces'))
invoke :reboot
end
desc 'adhoc', 'Set raspberry pi on ad-hoc network mode'
def adhoc
run remotely(copy_command('/etc/network/interfaces.adhoc', '/etc/network/interfaces'))
invoke :reboot
end
end
class Audio < Pi
desc 'devices', 'Lists the audio devices available to the raspberry pi'
def devices
run(remotely('cd jukebox', 'thor local:audio:devices'))
end
end
end
class Local < Thor
include Commands
include Thor::Actions
desc 'console', 'Spawns a console'
def console
exec console_command
end
class Audio < Local
desc 'devices', 'Lists the audio devices available to the local machine'
def devices
require 'audio-playback'
say Hirb::Helpers::AutoTable.render (AudioPlayback::Device::Output.all.map { |d| { id: d.id, name: d.name }} )
end
end
end
class Library < Thor
desc 'sync', 'Synchronizes local song library with the manifest'
def sync
::Library.synchronize
end
desc 'list', 'Lists all songs in the library'
def list
say Hirb::Helpers::AutoTable.render(::Library.list)
end
end
class Jukebox < Thor
include Commands
include Thor::Actions
desc 'logs', 'Tails the jukebox process logs'
def logs
run remotely('sudo tail -f -n 200 /var/log/upstart/jukebox.log')
end
desc 'song', 'Plays song on running jukebox server in raspberry pi'
def play_song(id)
put jukebox_url("/play/song/#{id}")
end
desc 'stop', 'Plays song on running jukebox server in raspberry pi'
def stop(id)
put jukebox_url("/stop")
end
no_commands do
def jukebox_url(path = '/')
URI::HTTP.build(host: ENV['PI_HOSTNAME'], port: ENV['CONTROL_PORT'], path: path).to_s
end
def put(url)
RestClient.put(url, {})
end
end
end