Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Paste
P1876
test-arcanist-local-configuration.py
Active
Public
Actions
Authored by
avivey
on Oct 26 2015, 5:47 PM.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Award Token
Flag For Later
Tags
None
Subscribers
None
#! /usr/bin/env python
import
subprocess
import
json
import
sys
import
re
PHAB_HTTP_URI
=
"https://secure.phabricator.com/api/"
PHAB_SSH_HOST
=
"dweller@secure.phabricator.com"
PHAB_SSH_PORT
=
22
class
ExecutionException
(
Exception
):
def
__init__
(
self
,
command
,
exit_code
):
self
.
command
=
command
self
.
exit_code
=
exit_code
def
execute
(
*
cmd
,
**
kwargs
):
''' Execute command.
returns stdout if execution exited with 0
raise ExecutionException otherwise. '''
if
cmd
and
isinstance
(
cmd
[
0
],
(
list
,
tuple
)):
cmd
=
cmd
[
0
]
# print(str(cmd))
stdin
=
kwargs
.
get
(
'stdin'
)
p
=
subprocess
.
Popen
(
cmd
,
stdout
=
subprocess
.
PIPE
,
stdin
=
subprocess
.
PIPE
)
stdout
,
_
=
p
.
communicate
(
stdin
)
c
=
p
.
wait
()
if
c
!=
0
:
raise
ExecutionException
(
' '
.
join
(
cmd
),
c
)
return
stdout
.
strip
()
def
error
(
message
):
print
print
' '
+
str
(
message
)
print
sys
.
exit
(
2
)
try
:
output
=
execute
(
'arc'
,
'get-config'
,
'phabricator.uri'
,
'default'
)
match
=
re
.
search
(
'Current Value: "(.*)"'
,
output
)
if
match
is
None
:
error
(
'arc is not configured with a `phabricator.uri`'
)
api
=
match
.
group
(
1
)
if
api
!=
PHAB_HTTP_URI
:
error
(
'arc configuration mismatch: '
'arc is configured to use `
%s
`, but expected `
%s
`'
%
(
api
,
PHAB_HTTP_URI
))
except
OSError
:
error
(
'Could not execute `arc`. Check your path. Maybe start a new shell'
)
try
:
output
=
execute
(
'arc'
,
'call-conduit'
,
'user.whoami'
,
stdin
=
'{}'
)
except
:
error
(
'arcanist conduit token is not configured!'
)
output
=
json
.
loads
(
output
)
if
output
[
'errorMessage'
]
is
not
None
:
error
(
output
[
'errorMessage'
])
api_user
=
output
[
'response'
]
try
:
output
=
execute
(
'ssh'
,
'-p '
+
str
(
PHAB_SSH_PORT
),
PHAB_SSH_HOST
,
'conduit user.whoami'
,
stdin
=
'{}'
)
except
ExecutionException
:
error
(
'Failed to connect using SSH. Check your keys'
)
output
=
json
.
loads
(
output
)
if
output
[
'error_info'
]
is
not
None
:
error
(
output
[
'error_info'
])
ssh_user
=
output
[
'result'
]
api_user_name
=
api_user
[
'userName'
]
ssh_user_name
=
ssh_user
[
'userName'
]
if
api_user
!=
ssh_user
:
if
api_user_name
==
ssh_user_name
:
error
(
'Identity mismatch: '
'API and SSH access provide the same username, but the account'
'details are different.'
)
else
:
error
(
'Identity mismatch: '
'API token belongs to user `
%s
`, '
'but SSH key matches user `
%s
`'
%
(
api_user_name
,
ssh_user_name
))
print
'Successfuly configured to use arc as user `
%s
`'
%
api_user_name
Event Timeline
avivey
edited the content of this paste.
(Show Details)
Oct 26 2015, 5:47 PM
2015-10-26 17:47:36 (UTC+0)
avivey
changed the title of this paste from untitled to
test-arcanist-local-configuration.py
.
avivey
updated the paste's language from
autodetect
to
autodetect
.
avivey
edited the content of this paste.
(Show Details)
Oct 26 2015, 6:13 PM
2015-10-26 18:13:13 (UTC+0)
avivey
edited the content of this paste.
(Show Details)
Oct 26 2015, 6:59 PM
2015-10-26 18:59:09 (UTC+0)
avivey
edited the content of this paste.
(Show Details)
Oct 27 2015, 10:21 PM
2015-10-27 22:21:17 (UTC+0)
avivey
mentioned this in
Community Resources
.
Nov 5 2015, 6:29 PM
2015-11-05 18:29:36 (UTC+0)
Log In to Comment