forked from posit-dev/rsconnect-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
1410 lines (1180 loc) · 53 KB
/
api.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
RStudio Connect API client and utility functions
"""
from os.path import abspath
import time
from typing import IO, Callable
import base64
import datetime
import hashlib
import hmac
import typing
import webbrowser
from _ssl import SSLError
from urllib import parse
from urllib.parse import urlparse
import re
from warnings import warn
from six import text_type
import gc
from . import validation
from .http_support import HTTPResponse, HTTPServer, append_to_path, CookieJar
from .log import logger, connect_logger, cls_logged, console_logger
from .models import AppModes
from .metadata import ServerStore, AppStore
from .exception import RSConnectException
from .bundle import _default_title, fake_module_file_from_directory
class AbstractRemoteServer:
def __init__(self, url: str, remote_name: str):
self.url = url
self.remote_name = remote_name
def handle_bad_response(self, response):
if isinstance(response, HTTPResponse):
if response.exception:
raise RSConnectException(
"Exception trying to connect to %s - %s" % (self.url, response.exception), cause=response.exception
)
# Sometimes an ISP will respond to an unknown server name by returning a friendly
# search page so trap that since we know we're expecting JSON from Connect. This
# also catches all error conditions which we will report as "not running Connect".
else:
if response.json_data and "error" in response.json_data and response.json_data["error"] is not None:
error = "%s reported an error (calling %s): %s" % (
self.remote_name,
response.full_uri,
response.json_data["error"],
)
raise RSConnectException(error)
if response.status < 200 or response.status > 299:
raise RSConnectException(
"Received an unexpected response from %s (calling %s): %s %s"
% (
self.remote_name,
response.full_uri,
response.status,
response.reason,
)
)
class ShinyappsServer(AbstractRemoteServer):
"""
A simple class to encapsulate the information needed to interact with an
instance of the shinyapps.io server.
"""
def __init__(self, url: str, account_name: str, token: str, secret: str):
super().__init__(url or "https://api.shinyapps.io", "shinyapps.io")
self.account_name = account_name
self.token = token
self.secret = secret
class RSConnectServer(AbstractRemoteServer):
"""
A simple class to encapsulate the information needed to interact with an
instance of the Connect server.
"""
def __init__(self, url, api_key, insecure=False, ca_data=None):
super().__init__(url, "RStudio Connect")
self.api_key = api_key
self.insecure = insecure
self.ca_data = ca_data
# This is specifically not None.
self.cookie_jar = CookieJar()
TargetableServer = typing.Union[ShinyappsServer, RSConnectServer]
class S3Server(AbstractRemoteServer):
def __init__(self, url: str):
super().__init__(url, "S3")
class RSConnectClient(HTTPServer):
def __init__(self, server: RSConnectServer, cookies=None, timeout=30):
if cookies is None:
cookies = server.cookie_jar
super().__init__(
append_to_path(server.url, "__api__"),
server.insecure,
server.ca_data,
cookies,
timeout,
)
self._server = server
if server.api_key:
self.key_authorization(server.api_key)
def _tweak_response(self, response):
return (
response.json_data
if response.status and response.status == 200 and response.json_data is not None
else response
)
def me(self):
return self.get("me")
def server_settings(self):
return self.get("server_settings")
def python_settings(self):
return self.get("v1/server_settings/python")
def app_search(self, filters):
return self.get("applications", query_params=filters)
def app_create(self, name):
return self.post("applications", body={"name": name})
def app_get(self, app_id):
return self.get("applications/%s" % app_id)
def app_upload(self, app_id, tarball):
return self.post("applications/%s/upload" % app_id, body=tarball)
def app_update(self, app_id, updates):
return self.post("applications/%s" % app_id, body=updates)
def app_add_environment_vars(self, app_guid, env_vars):
env_body = [dict(name=kv[0], value=kv[1]) for kv in env_vars]
return self.patch("v1/content/%s/environment" % app_guid, body=env_body)
def app_deploy(self, app_id, bundle_id=None):
return self.post("applications/%s/deploy" % app_id, body={"bundle": bundle_id})
def app_publish(self, app_id, access):
return self.post(
"applications/%s" % app_id,
body={"access_type": access, "id": app_id, "needs_config": False},
)
def app_config(self, app_id):
return self.get("applications/%s/config" % app_id)
def bundle_download(self, content_guid, bundle_id):
response = self.get("v1/content/%s/bundles/%s/download" % (content_guid, bundle_id), decode_response=False)
self._server.handle_bad_response(response)
return response
def content_search(self):
response = self.get("v1/content")
self._server.handle_bad_response(response)
return response
def content_get(self, content_guid):
response = self.get("v1/content/%s" % content_guid)
self._server.handle_bad_response(response)
return response
def content_build(self, content_guid, bundle_id=None):
response = self.post("v1/content/%s/build" % content_guid, body={"bundle_id": bundle_id})
self._server.handle_bad_response(response)
return response
def task_get(self, task_id, first_status=None):
params = None
if first_status is not None:
params = {"first_status": first_status}
response = self.get("tasks/%s" % task_id, query_params=params)
self._server.handle_bad_response(response)
return response
def deploy_git(self, app_name, repository, branch, subdirectory):
app = self.app_create(app_name)
self._server.handle_bad_response(app)
self.post(
"applications/%s/repo" % app["guid"],
body={
"repository": repository, "branch": branch , "subdirectory": subdirectory
}
)
task = self.post("applications/%s/deploy" % app["guid"], body=dict())
self._server.handle_bad_response(task)
return {
"task_id": task["id"],
"app_id": app["id"],
"app_guid": app["guid"],
"app_url": app["url"],
"title": app["title"],
}
def deploy(self, app_id, app_name, app_title, title_is_default, tarball, env_vars=None):
if app_id is None:
# create an app if id is not provided
app = self.app_create(app_name)
self._server.handle_bad_response(app)
app_id = app["id"]
# Force the title to update.
title_is_default = False
else:
# assume app exists. if it was deleted then Connect will
# raise an error
app = self.app_get(app_id)
self._server.handle_bad_response(app)
app_guid = app["guid"]
if env_vars:
result = self.app_add_environment_vars(app_guid, list(env_vars.items()))
self._server.handle_bad_response(result)
if app["title"] != app_title and not title_is_default:
self._server.handle_bad_response(self.app_update(app_id, {"title": app_title}))
app["title"] = app_title
app_bundle = self.app_upload(app_id, tarball)
self._server.handle_bad_response(app_bundle)
task = self.app_deploy(app_id, app_bundle["id"])
self._server.handle_bad_response(task)
return {
"task_id": task["id"],
"app_id": app_id,
"app_guid": app["guid"],
"app_url": app["url"],
"title": app["title"],
}
def download_bundle(self, content_guid, bundle_id):
results = self.bundle_download(content_guid, bundle_id)
self._server.handle_bad_response(results)
return results
def search_content(self):
results = self.content_search()
self._server.handle_bad_response(results)
return results
def get_content(self, content_guid):
results = self.content_get(content_guid)
self._server.handle_bad_response(results)
return results
def wait_for_task(
self, task_id, log_callback, abort_func=lambda: False, timeout=None, poll_wait=0.5, raise_on_error=True
):
last_status = None
ending = time.time() + timeout if timeout else 999999999999
if log_callback is None:
log_lines = []
log_callback = log_lines.append
else:
log_lines = None
sleep_duration = 0.5
time_slept = 0
while True:
if time.time() >= ending:
raise RSConnectException("Task timed out after %d seconds" % timeout)
elif abort_func():
raise RSConnectException("Task aborted.")
# we continue the loop so that we can re-check abort_func() in case there was an interrupt (^C),
# otherwise the user would have to wait a full poll_wait cycle before the program would exit.
if time_slept <= poll_wait:
time_slept += sleep_duration
time.sleep(sleep_duration)
continue
else:
time_slept = 0
task_status = self.task_get(task_id, last_status)
self._server.handle_bad_response(task_status)
last_status = self.output_task_log(task_status, last_status, log_callback)
if task_status["finished"]:
result = task_status.get("result")
if isinstance(result, dict):
data = result.get("data", "")
type = result.get("type", "")
if data or type:
log_callback("%s (%s)" % (data, type))
err = task_status.get("error")
if err:
log_callback("Error from Connect server: " + err)
exit_code = task_status["code"]
if exit_code != 0:
exit_status = "Task exited with status %d." % exit_code
if raise_on_error:
raise RSConnectException(exit_status)
else:
log_callback("Task failed. %s" % exit_status)
return log_lines, task_status
@staticmethod
def output_task_log(task_status, last_status, log_callback):
"""Pipe any new output through the log_callback.
Returns an updated last_status which should be passed into
the next call to output_task_log.
Raises RSConnectException on task failure.
"""
new_last_status = last_status
if task_status["last_status"] != last_status:
for line in task_status["status"]:
log_callback(line)
new_last_status = task_status["last_status"]
return new_last_status
# for backwards compatibility with rsconnect-jupyter
RSConnect = RSConnectClient
class RSConnectExecutor:
def __init__(
self,
name: str = None,
url: str = None,
api_key: str = None,
insecure: bool = False,
cacert: IO = None,
ca_data: str = None,
cookies=None,
account=None,
token: str = None,
secret: str = None,
timeout: int = 30,
logger=console_logger,
**kwargs
) -> None:
self.reset()
self._d = kwargs
self.setup_remote_server(
name=name,
url=url or kwargs.get("server"),
api_key=api_key,
insecure=insecure,
cacert=cacert,
ca_data=ca_data,
account_name=account,
token=token,
secret=secret,
)
self.setup_client(cookies, timeout)
self.logger = logger
@classmethod
def fromConnectServer(cls, connect_server, **kwargs):
return cls(
url=connect_server.url,
api_key=connect_server.api_key,
insecure=connect_server.insecure,
ca_data=connect_server.ca_data,
**kwargs,
)
def reset(self):
self._d = None
self.remote_server = None
self.client = None
self.logger = None
gc.collect()
return self
def drop_context(self):
self._d = None
gc.collect()
return self
def setup_remote_server(
self,
name: str = None,
url: str = None,
api_key: str = None,
insecure: bool = False,
cacert: IO = None,
ca_data: str = None,
account_name: str = None,
token: str = None,
secret: str = None,
):
validation.validate_connection_options(
url=url,
api_key=api_key,
insecure=insecure,
cacert=cacert,
account_name=account_name,
token=token,
secret=secret,
name=name,
)
if cacert and not ca_data:
ca_data = text_type(cacert.read())
server_data = ServerStore().resolve(name, url)
if server_data.from_store:
url = server_data.url
api_key = server_data.api_key
insecure = server_data.insecure
ca_data = server_data.ca_data
account_name = server_data.account_name
token = server_data.token
secret = server_data.secret
self.is_server_from_store = server_data.from_store
if api_key:
self.remote_server = RSConnectServer(url, api_key, insecure, ca_data)
elif token and secret:
self.remote_server = ShinyappsServer(url, account_name, token, secret)
else:
raise RSConnectException("Unable to infer Connect server type and setup server.")
def setup_client(self, cookies=None, timeout=30, **kwargs):
if isinstance(self.remote_server, RSConnectServer):
self.client = RSConnectClient(self.remote_server, cookies, timeout)
elif isinstance(self.remote_server, ShinyappsServer):
self.client = ShinyappsClient(self.remote_server, timeout)
else:
raise RSConnectException("Unable to infer Connect client.")
@property
def state(self):
return self._d
def get(self, key: str, *args, **kwargs):
return kwargs.get(key) or self.state.get(key)
def pipe(self, func, *args, **kwargs):
return func(*args, **kwargs)
@cls_logged("Validating server...")
def validate_server(
self,
name: str = None,
url: str = None,
api_key: str = None,
insecure: bool = False,
cacert: IO = None,
api_key_is_required: bool = False,
account_name: str = None,
token: str = None,
secret: str = None,
):
if (url and api_key) or isinstance(self.remote_server, RSConnectServer):
self.validate_connect_server(name, url, api_key, insecure, cacert, api_key_is_required)
elif (url and token and secret) or isinstance(self.remote_server, ShinyappsServer):
self.validate_shinyapps_server(url, account_name, token, secret)
else:
raise RSConnectException("Unable to validate server from information provided.")
return self
def validate_connect_server(
self,
name: str = None,
url: str = None,
api_key: str = None,
insecure: bool = False,
cacert: IO = None,
api_key_is_required: bool = False,
**kwargs
):
"""
Validate that the user gave us enough information to talk to shinyapps.io or a Connect server.
:param name: the nickname, if any, specified by the user.
:param url: the URL, if any, specified by the user.
:param api_key: the API key, if any, specified by the user.
:param insecure: a flag noting whether TLS host/validation should be skipped.
:param cacert: the file object of a CA certs file containing certificates to use.
:param api_key_is_required: a flag that notes whether the API key is required or may
be omitted.
:param token: The shinyapps.io authentication token.
:param secret: The shinyapps.io authentication secret.
"""
url = url or self.remote_server.url
api_key = api_key or self.remote_server.api_key
insecure = insecure or self.remote_server.insecure
api_key_is_required = api_key_is_required or self.get("api_key_is_required", **kwargs)
ca_data = None
if cacert:
ca_data = text_type(cacert.read())
api_key = api_key or self.remote_server.api_key
insecure = insecure or self.remote_server.insecure
if not ca_data:
ca_data = self.remote_server.ca_data
api_key_is_required = api_key_is_required or self.get("api_key_is_required", **kwargs)
if name and url:
raise RSConnectException("You must specify only one of -n/--name or -s/--server, not both")
if not name and not url:
raise RSConnectException("You must specify one of -n/--name or -s/--server.")
server_data = ServerStore().resolve(name, url)
connect_server = RSConnectServer(url, None, insecure, ca_data)
# If our info came from the command line, make sure the URL really works.
if not server_data.from_store:
self.server_settings
connect_server.api_key = api_key
if not connect_server.api_key:
if api_key_is_required:
raise RSConnectException('An API key must be specified for "%s".' % connect_server.url)
return self
# If our info came from the command line, make sure the key really works.
if not server_data.from_store:
_ = self.verify_api_key(connect_server)
self.remote_server = connect_server
self.client = RSConnectClient(self.remote_server)
return self
def validate_shinyapps_server(
self, url: str = None, account_name: str = None, token: str = None, secret: str = None, **kwargs
):
url = url or self.remote_server.url
account_name = account_name or self.remote_server.account_name
token = token or self.remote_server.token
secret = secret or self.remote_server.secret
server = ShinyappsServer(url, account_name, token, secret)
with ShinyappsClient(server) as client:
try:
result = client.get_current_user()
server.handle_bad_response(result)
except RSConnectException as exc:
raise RSConnectException("Failed to verify with shinyapps.io ({}).".format(exc))
@cls_logged("Making bundle ...")
def make_bundle(self, func: Callable, *args, **kwargs):
path = (
self.get("path", **kwargs)
or self.get("file", **kwargs)
or self.get("file_name", **kwargs)
or self.get("directory", **kwargs)
or self.get("file_or_directory", **kwargs)
)
app_id = self.get("app_id", **kwargs)
title = self.get("title", **kwargs)
app_store = self.get("app_store", *args, **kwargs)
if not app_store:
module_file = fake_module_file_from_directory(path)
self.state["app_store"] = app_store = AppStore(module_file)
d = self.state
d["title_is_default"] = not bool(title)
d["title"] = title or _default_title(path)
force_unique_name = app_id is None
d["deployment_name"] = self.make_deployment_name(d["title"], force_unique_name)
try:
bundle = func(*args, **kwargs)
except IOError as error:
msg = "Unable to include the file %s in the bundle: %s" % (
error.filename,
error.args[1],
)
raise RSConnectException(msg)
d["bundle"] = bundle
return self
def check_server_capabilities(self, capability_functions):
"""
Uses a sequence of functions that check for capabilities in a Connect server. The
server settings data is retrieved by the gather_server_details() function.
Each function provided must accept one dictionary argument which will be the server
settings data returned by the gather_server_details() function. That function must
return a boolean value. It must also contain a docstring which itself must contain
an ":error:" tag as the last thing in the docstring. If the function returns False,
an exception is raised with the function's ":error:" text as its message.
:param capability_functions: a sequence of functions that will be called.
:param details_source: the source for obtaining server details, gather_server_details(),
by default.
"""
if isinstance(self.remote_server, ShinyappsServer):
return self
details = self.server_details
for function in capability_functions:
if not function(details):
index = function.__doc__.find(":error:") if function.__doc__ else -1
if index >= 0:
message = function.__doc__[index + 7 :].strip()
else:
message = "The server does not satisfy the %s capability check." % function.__name__
raise RSConnectException(message)
return self
@cls_logged("Deploying bundle ...")
def deploy_bundle(
self,
app_id: int = None,
deployment_name: str = None,
title: str = None,
title_is_default: bool = False,
bundle: IO = None,
env_vars=None,
):
app_id = app_id or self.get("app_id")
deployment_name = deployment_name or self.get("deployment_name")
title = title or self.get("title")
title_is_default = title_is_default or self.get("title_is_default")
bundle = bundle or self.get("bundle")
env_vars = env_vars or self.get("env_vars")
if isinstance(self.remote_server, RSConnectServer):
result = self.client.deploy(
app_id,
deployment_name,
title,
title_is_default,
bundle,
env_vars,
)
self.remote_server.handle_bad_response(result)
self.state["deployed_info"] = result
return self
else:
contents = bundle.read()
bundle_size = len(contents)
bundle_hash = hashlib.md5(contents).hexdigest()
prepare_deploy_result = self.client.prepare_deploy(
app_id,
deployment_name,
bundle_size,
bundle_hash,
)
upload_url = prepare_deploy_result.presigned_url
parsed_upload_url = urlparse(upload_url)
with S3Client(
"{}://{}".format(parsed_upload_url.scheme, parsed_upload_url.netloc), timeout=120
) as s3_client:
upload_result = s3_client.upload(
"{}?{}".format(parsed_upload_url.path, parsed_upload_url.query),
prepare_deploy_result.presigned_checksum,
bundle_size,
contents,
)
S3Server(upload_url).handle_bad_response(upload_result)
self.client.do_deploy(prepare_deploy_result.bundle_id, prepare_deploy_result.app_id)
print("Application successfully deployed to {}".format(prepare_deploy_result.app_url))
webbrowser.open_new(prepare_deploy_result.app_url)
self.state["deployed_info"] = {
"app_url": prepare_deploy_result.app_url,
"app_id": prepare_deploy_result.app_id,
"app_guid": None,
"title": title,
}
return self
def emit_task_log(
self,
app_id: int = None,
task_id: int = None,
log_callback=connect_logger,
abort_func: Callable[[], bool] = lambda: False,
timeout: int = None,
poll_wait: float = 0.5,
raise_on_error: bool = True,
):
"""
Helper for spooling the deployment log for an app.
:param app_id: the ID of the app that was deployed.
:param task_id: the ID of the task that is tracking the deployment of the app..
:param log_callback: the callback to use to write the log to. If this is None
(the default) the lines from the deployment log will be returned as a sequence.
If a log callback is provided, then None will be returned for the log lines part
of the return tuple.
:param timeout: an optional timeout for the wait operation.
:param poll_wait: how long to wait between polls of the task api for status/logs
:param raise_on_error: whether to raise an exception when a task is failed, otherwise we
return the task_result so we can record the exit code.
"""
if isinstance(self.remote_server, RSConnectServer):
app_id = app_id or self.state["deployed_info"]["app_id"]
task_id = task_id or self.state["deployed_info"]["task_id"]
log_lines, _ = self.client.wait_for_task(
task_id, log_callback.info, abort_func, timeout, poll_wait, raise_on_error
)
self.remote_server.handle_bad_response(log_lines)
app_config = self.client.app_config(app_id)
self.remote_server.handle_bad_response(app_config)
app_dashboard_url = app_config.get("config_url")
log_callback.info("Deployment completed successfully.")
log_callback.info("\t Dashboard content URL: %s", app_dashboard_url)
log_callback.info("\t Direct content URL: %s", self.state["deployed_info"]["app_url"])
return self
@cls_logged("Saving deployed information...")
def save_deployed_info(self, *args, **kwargs):
app_store = self.get("app_store", *args, **kwargs)
path = (
self.get("path", **kwargs)
or self.get("file", **kwargs)
or self.get("file_name", **kwargs)
or self.get("directory", **kwargs)
or self.get("file_or_directory", **kwargs)
)
deployed_info = self.get("deployed_info", *args, **kwargs)
app_store.set(
self.remote_server.url,
abspath(path),
deployed_info["app_url"],
deployed_info["app_id"],
deployed_info["app_guid"],
deployed_info["title"],
self.state["app_mode"],
)
return self
@cls_logged("Validating app mode...")
def validate_app_mode(self, *args, **kwargs):
path = (
self.get("path", **kwargs)
or self.get("file", **kwargs)
or self.get("file_name", **kwargs)
or self.get("directory", **kwargs)
or self.get("file_or_directory", **kwargs)
)
app_store = self.get("app_store", *args, **kwargs)
if not app_store:
module_file = fake_module_file_from_directory(path)
self.state["app_store"] = app_store = AppStore(module_file)
new = self.get("new", **kwargs)
app_id = self.get("app_id", **kwargs)
app_mode = self.get("app_mode", **kwargs)
if new and app_id:
raise RSConnectException("Specify either a new deploy or an app ID but not both.")
existing_app_mode = None
if not new:
if app_id is None:
# Possible redeployment - check for saved metadata.
# Use the saved app information unless overridden by the user.
app_id, existing_app_mode = app_store.resolve(self.remote_server.url, app_id, app_mode)
logger.debug("Using app mode from app %s: %s" % (app_id, app_mode))
elif app_id is not None:
# Don't read app metadata if app-id is specified. Instead, we need
# to get this from the remote.
if isinstance(self.remote_server, RSConnectServer):
app = get_app_info(self.remote_server, app_id)
existing_app_mode = AppModes.get_by_ordinal(app.get("app_mode", 0), True)
elif isinstance(self.remote_server, ShinyappsServer):
app = get_shinyapp_info(self.remote_server, app_id)
existing_app_mode = AppModes.get_by_cloud_name(app.json_data["mode"])
else:
raise RSConnectException("Unable to infer Connect client.")
if existing_app_mode and app_mode != existing_app_mode:
msg = (
"Deploying with mode '%s',\n"
+ "but the existing deployment has mode '%s'.\n"
+ "Use the --new option to create a new deployment of the desired type."
) % (app_mode.desc(), existing_app_mode.desc())
raise RSConnectException(msg)
self.state["app_id"] = app_id
self.state["app_mode"] = app_mode
return self
@property
def server_settings(self):
try:
result = self.client.server_settings()
self.remote_server.handle_bad_response(result)
except SSLError as ssl_error:
raise RSConnectException("There is an SSL/TLS configuration problem: %s" % ssl_error)
return result
def verify_api_key(self, server=None):
"""
Verify that an API Key may be used to authenticate with the given RStudio Connect server.
If the API key verifies, we return the username of the associated user.
"""
if not server:
server = self.remote_server
if isinstance(server, ShinyappsServer):
raise RSConnectException("Shinnyapps server does not use an API key.")
with RSConnectClient(server) as client:
result = client.me()
if isinstance(result, HTTPResponse):
if result.json_data and "code" in result.json_data and result.json_data["code"] == 30:
raise RSConnectException("The specified API key is not valid.")
raise RSConnectException("Could not verify the API key: %s %s" % (result.status, result.reason))
return self
@property
def api_username(self):
result = self.client.me()
self.remote_server.handle_bad_response(result)
return result["username"]
@property
def python_info(self):
"""
Return information about versions of Python that are installed on the indicated
Connect server.
:return: the Python installation information from Connect.
"""
result = self.client.python_settings()
self.remote_server.handle_bad_response(result)
return result
@property
def server_details(self):
"""
Builds a dictionary containing the version of RStudio Connect that is running
and the versions of Python installed there.
:return: a three-entry dictionary. The key 'connect' will refer to the version
of Connect that was found. The key `python` will refer to a sequence of version
strings for all the versions of Python that are installed. The key `conda` will
refer to data about whether Connect is configured to support Conda environments.
"""
def _to_sort_key(text):
parts = [part.zfill(5) for part in text.split(".")]
return "".join(parts)
server_settings = self.server_settings
python_settings = self.python_info
python_versions = sorted([item["version"] for item in python_settings["installations"]], key=_to_sort_key)
conda_settings = {
"supported": python_settings["conda_enabled"] if "conda_enabled" in python_settings else False
}
return {
"connect": server_settings["version"],
"python": {
"api_enabled": python_settings["api_enabled"] if "api_enabled" in python_settings else False,
"versions": python_versions,
},
"conda": conda_settings,
}
def make_deployment_name(self, title, force_unique):
"""
Produce a name for a deployment based on its title. It is assumed that the
title is already defaulted and validated as appropriate (meaning the title
isn't None or empty).
We follow the same rules for doing this as the R rsconnect package does. See
the title.R code in https://github.com/rstudio/rsconnect/R with the exception
that we collapse repeating underscores and, if the name is too short, it is
padded to the left with underscores.
:param title: the title to start with.
:param force_unique: a flag noting whether the generated name must be forced to be
unique.
:return: a name for a deployment based on its title.
"""
_name_sub_pattern = re.compile(r"[^A-Za-z0-9_ -]+")
_repeating_sub_pattern = re.compile(r"_+")
# First, Generate a default name from the given title.
name = _name_sub_pattern.sub("", title.lower()).replace(" ", "_")
name = _repeating_sub_pattern.sub("_", name)[:64].rjust(3, "_")
# Now, make sure it's unique, if needed.
if force_unique:
name = find_unique_name(self.remote_server, name)
return name
def filter_out_server_info(**kwargs):
server_fields = {"connect_server", "name", "server", "api_key", "insecure", "cacert"}
new_kwargs = {k: v for k, v in kwargs.items() if k not in server_fields}
return new_kwargs
class S3Client(HTTPServer):
def upload(self, path, presigned_checksum, bundle_size, contents):
headers = {
"content-type": "application/x-tar",
"content-length": str(bundle_size),
"content-md5": presigned_checksum,
}
return self.put(path, headers=headers, body=contents, decode_response=False)
class PrepareDeployResult:
def __init__(self, app_id: int, app_url: str, bundle_id: int, presigned_url: str, presigned_checksum: str):
self.app_id = app_id
self.app_url = app_url
self.bundle_id = bundle_id
self.presigned_url = presigned_url
self.presigned_checksum = presigned_checksum
class ShinyappsClient(HTTPServer):
_TERMINAL_STATUSES = {"success", "failed", "error"}
def __init__(self, shinyapps_server: ShinyappsServer, timeout: int = 30):
self._token = shinyapps_server.token
self._key = base64.b64decode(shinyapps_server.secret)
self._server = shinyapps_server
super().__init__(shinyapps_server.url, timeout=timeout)
def _get_canonical_request(self, method, path, timestamp, content_hash):
return "\n".join([method, path, timestamp, content_hash])
def _get_canonical_request_signature(self, request):
result = hmac.new(self._key, request.encode(), hashlib.sha256).hexdigest()
return base64.b64encode(result.encode()).decode()
def get_extra_headers(self, url, method, body):
canonical_request_method = method.upper()
canonical_request_path = parse.urlparse(url).path
canonical_request_date = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT")
# get request checksum
md5 = hashlib.md5()
body = body or b""
body_bytes = body if isinstance(body, bytes) else body.encode()
md5.update(body_bytes)
canonical_request_checksum = md5.hexdigest()
canonical_request = self._get_canonical_request(
canonical_request_method, canonical_request_path, canonical_request_date, canonical_request_checksum
)
signature = self._get_canonical_request_signature(canonical_request)
return {
"X-Auth-Token": "{0}".format(self._token),
"X-Auth-Signature": "{0}; version=1".format(signature),
"Date": canonical_request_date,
"X-Content-Checksum": canonical_request_checksum,
}
def get_application(self, application_id):
return self.get("/v1/applications/{}".format(application_id))
def create_application(self, account_id, application_name):
application_data = {
"account": account_id,
"name": application_name,
"template": "shiny",
}
return self.post("/v1/applications/", body=application_data)
def get_accounts(self):
return self.get("/v1/accounts/")
def _get_applications_like_name_page(self, name: str, offset: int):
return self.get(
"/v1/applications?filter=name:like:{}&offset={}&count=100&use_advanced_filters=true".format(name, offset)