-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasks.py
180 lines (129 loc) · 4.5 KB
/
tasks.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import getpass
import os
import platform
from invoke import run, task
import ssh_client
if 'USERNAME' in os.environ:
username = os.environ['USERNAME']
else:
username = 'alex'
if 'HOSTNAME' in os.environ:
hostname = os.environ['HOSTNAME']
else:
hostname = '10.0.1.60'
plugin_name = 'etvnet'
bundle_name = 'Etvnet.bundle'
archive = bundle_name + '.zip'
def dest_root(os):
if os == 'Darwin':
return '~'
else:
return '/var/lib/plexmediaserver'
def get_password(username, hostname):
return getpass.getpass('Password for %s@%s: ' % (username, hostname))
def execute_remote_command(command, hostname, username, password):
client = None
try:
client = ssh_client.SshClient(host=hostname, port=22, username=username, password=password)
ret = client.execute(command, sudo=True)
print " ".join(ret["out"]), " E ".join(ret["err"]), ret["retval"]
finally:
if client:
client.close()
plex_home = dest_root(platform.system()) + "/Library/Application\ Support/Plex\ Media\ Server"
plugins_dir = plex_home + '/Plug-ins'
plugin_dir = plugins_dir + '/' + bundle_name
unix_plex_home = dest_root('Unix') + "/Library/Application\ Support/Plex\ Media\ Server"
unix_plugins_dir = unix_plex_home + '/Plug-ins'
unix_plugin_dir = unix_plugins_dir + '/' + bundle_name
@task
def test(script):
run("python " + script)
@task
def reset():
run("rm -rf " + plex_home + "/Plug-in\ Support/Caches/com.plexapp.plugins." + plugin_name)
run("rm -rf " + plex_home + "/Plug-in\ Support/Data/com.plexapp.plugins." + plugin_name)
run("rm -rf " + plex_home + "/Plug-in\ Support/Preferences/com.plexapp.plugins." + plugin_name + ".xml")
# run("rm -rf " + plugin_dir)
print("Plugin was reset.")
@task
def copy(plugin_dir):
run("mkdir -p " + plugin_dir + "/Contents/Code")
run("cp -R Contents/* " + plugin_dir + "/Contents")
print("Files were copied.")
@task
def reload():
import urllib2
url = "http://127.0.0.1:32400/:/plugins/com.plexapp.system/restart"
urllib2.urlopen(url).read()
print("Server was restarted.")
#run("tail -f ~/Library/Logs/PMS\ Plugin\ Logs/com.plexapp.plugins." + plugin_name + ".log")
@task
def deploy():
copy(plugin_dir)
reset()
reload()
@task
def pip():
import pip
installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
for i in installed_packages])
print(installed_packages_list)
@task
def reset_remote(password):
command = """
sudo -S rm -rf {plex_home}/Plug-in\ Support/Caches/com.plexapp.plugins.{plugin_name}
sudo -S rm -rf {plex_home}/Plug-in\ Support/Data/com.plexapp.plugins.{plugin_name}
sudo -S rm -rf {plex_home}/Plug-in\ Support/Preferences/com.plexapp.plugins.{plugin_name}.xml
sudo -S rm -rf {plex_home}/Plug-ins\{bundle_name}/Contents/Code
echo "Plugin was reset.".
""".format(plex_home=unix_plex_home, plugin_dir=unix_plugin_dir, bundle_name=bundle_name, plugin_name=plugin_name)
execute_remote_command(command, hostname, username, password)
@task
def zip(archive):
run("cd build && zip -r " + archive + " .")
@task
def scp(archive):
run("scp build/" + archive + " " + username + "@" + hostname + ":" + archive)
@task
def unzip_remote(password):
command = "sudo -S unzip -o " + bundle_name + ".zip -d " + unix_plugins_dir
execute_remote_command(command, hostname, username, password)
@task
def restart_remote(password):
command = """
sudo -S service plexmediaserver restart
echo "Server was restarted."
"""
execute_remote_command(command, hostname, username, password)
@task
def chown_remote(password):
command = "sudo -S chown -R plex " + unix_plugin_dir
execute_remote_command(command, hostname, username, password)
@task
def ls_remote(password):
execute_remote_command('ls', hostname, username, password)
@task
def clean():
run("rm -rf build")
@task
def build():
clean()
run("mkdir -p build/" + bundle_name)
copy('build/' + bundle_name)
zip(archive)
@task
def rdeploy():
password = get_password(hostname, username)
build()
scp(archive)
reset_remote(password)
unzip_remote(password)
restart_remote(password)
chown_remote(password)
@task
def plex_uninstall():
run("rm -rf ~/Library/Application Support/Plex Media Server/")
run("rm -rf ~/Library/Caches/PlexMediaServer/")
run("rm -rf ~/Library/Preferences/com.plexapp.plexmediaserver.plist")