Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
itminedu
kamaki
Commits
67469d65
Commit
67469d65
authored
Jan 04, 2013
by
Stavros Sachtouris
Browse files
Handle connection errors, allow special handling
Showcase: server_info catche/manage clienterror that was a connection error
parent
e13e308f
Changes
4
Hide whitespace changes
Inline
Side-by-side
kamaki/cli/commands/cyclades_cli.py
View file @
67469d65
...
...
@@ -37,7 +37,7 @@ from kamaki.cli.utils import print_dict, print_list, print_items, bold
from
kamaki.cli.errors
import
raiseCLIError
,
CLISyntaxError
from
kamaki.clients.cyclades
import
CycladesClient
,
ClientError
from
kamaki.cli.argument
import
FlagArgument
,
ValueArgument
,
KeyValueArgument
from
kamaki.cli.argument
import
ProgressBarArgument
from
kamaki.cli.argument
import
ProgressBarArgument
,
DateArgument
from
kamaki.cli.commands
import
_command_init
from
base64
import
b64encode
...
...
@@ -59,6 +59,12 @@ about_authentication = '\n User Authentication:\
\n
to check authentication: /astakos authenticate
\
\n
to set authentication token: /config set token <token>'
howto_token
=
[
'Make sure a valid token is provided:'
,
' to check if the token is valid: /astakos authenticate'
,
' to set a token: /config set [.server.]token <token>'
,
' to get current token: /config get [server.]token'
]
class
_init_cyclades
(
_command_init
):
def
main
(
self
,
service
=
'compute'
):
...
...
@@ -78,7 +84,7 @@ class server_list(_init_cyclades):
arguments
=
dict
(
detail
=
FlagArgument
(
'show detailed output'
,
'-l'
),
since
=
Valu
eArgument
(
since
=
Dat
eArgument
(
'show only items since date (
\'
d/m/Y H:M:S
\'
)'
,
'--since'
)
)
...
...
@@ -123,7 +129,13 @@ class server_list(_init_cyclades):
@
command
(
server_cmds
)
class
server_info
(
_init_cyclades
):
"""Get server details"""
"""Detailed information on a Virtual Machine
Contains:
- name, id, status, create/update dates
- network interfaces
- metadata (e.g. os, superuser) and diagnostics
- hardware flavor and os image ids
"""
@
classmethod
def
_print
(
self
,
server
):
...
...
@@ -147,7 +159,25 @@ class server_info(_init_cyclades):
try
:
server
=
self
.
client
.
get_server_details
(
int
(
server_id
))
except
ValueError
as
err
:
raiseCLIError
(
err
,
'Server id must be positive integer'
,
1
)
raiseCLIError
(
err
,
'Server id must be a positive integer'
,
1
)
except
ClientError
as
ce
:
if
ce
.
status
==
401
:
raiseCLIError
(
ce
,
'Authorization failed'
,
details
=
howto_token
)
elif
ce
.
status
==
404
:
raiseCLIError
(
ce
,
'Server with id %s not found'
%
server_id
)
elif
ce
.
status
==
111
:
raiseCLIError
(
err
,
'Connection to %s refused'
%
self
.
client
.
base_url
,
details
=
[
'Check if service is up or set compute.url'
,
' to get service url: /config get compute.url'
,
' to set service url: /config set compute.url <URL>'
])
elif
ce
.
status
==
110
:
raiseCLIError
(
err
,
'Connection to %s timed out'
%
self
.
client
.
base_url
,
details
=
[
'Check if service is up or set compute.url'
,
' to get service url: /config get compute.url'
,
' to set service url: /config set compute.url <URL>'
])
raiseCLIError
(
ce
)
except
Exception
as
err
:
raiseCLIError
(
err
)
self
.
_print
(
server
)
...
...
kamaki/clients/__init__.py
View file @
67469d65
...
...
@@ -215,7 +215,7 @@ class Client(object):
if
not
errstr
:
errstr
=
(
'%s'
%
type
(
err
))[
7
:
-
2
]
raise
ClientError
(
'%s
\n
'
%
errstr
,
status
=
getattr
(
err
,
'status'
,
0
))
status
=
getattr
(
err
,
'status'
,
0
)
or
getattr
(
err
,
'errno'
,
0
))
self
.
http_client
.
reset_headers
()
self
.
http_client
.
reset_params
()
...
...
kamaki/clients/connection/errors.py
View file @
67469d65
...
...
@@ -33,10 +33,18 @@
class
HTTPConnectionError
(
Exception
):
def
__init__
(
self
,
message
):
errno
=
None
def
__init__
(
self
,
message
,
errno
=
None
):
super
(
HTTPConnectionError
,
self
).
__init__
(
message
)
if
errno
:
self
.
errno
=
errno
class
HTTPResponseError
(
Exception
):
def
__init__
(
self
,
message
):
errno
=
None
def
__init__
(
self
,
message
,
errno
=
None
):
super
(
HTTPResponseError
,
self
).
__init__
(
message
)
if
errno
:
self
.
errno
=
errno
kamaki/clients/connection/kamakicon.py
View file @
67469d65
...
...
@@ -36,7 +36,7 @@ from objpool.http import get_http_connection
from
kamaki.clients.connection
import
HTTPConnection
,
HTTPResponse
from
kamaki.clients.connection.errors
import
HTTPConnectionError
from
kamaki.clients.connection.errors
import
HTTPResponseError
from
socket
import
gaierror
from
socket
import
gaierror
,
error
from
json
import
loads
...
...
@@ -175,13 +175,14 @@ class KamakiHTTPConnection(HTTPConnection):
url
=
str
(
self
.
path
),
headers
=
http_headers
,
body
=
data
)
except
IOError
as
ioe
:
raise
HTTPConnectionError
(
'Cannot connect to %s: %s'
%
(
self
.
url
,
ioe
.
strerror
),
errno
=
ioe
.
errno
)
except
Exception
as
err
:
from
traceback
import
format_stack
from
kamaki.clients
import
recvlog
recvlog
.
debug
(
'
\n
'
.
join
([
'%s'
%
type
(
err
)]
+
format_stack
()))
conn
.
close
()
if
isinstance
(
err
,
gaierror
):
raise
HTTPConnectionError
(
'Cannot connect to %s - %s'
%
(
self
.
url
,
err
))
raise
return
KamakiHTTPResponse
(
conn
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment