def __init__(self, config): self.config = config cloud_name = self.config.get('global', 'default_cloud') self.auth_token = self.config.get_cloud(cloud_name, 'token') cacerts_path = self.config.get('global', 'ca_certs') https.patch_with_certs(cacerts_path) auth_url = self.config.get_cloud(cloud_name, 'url') auth = AstakosClient(auth_url, self.auth_token) self.endpoints = dict( astakos=auth_url, cyclades=auth.get_endpoint_url(CycladesComputeClient.service_type), network=auth.get_endpoint_url(CycladesNetworkClient.service_type), plankton=auth.get_endpoint_url(ImageClient.service_type) ) self.user_id = auth.user_info['id']
def ADD_VM(url, token, name, project): # create a new vm astakos = AstakosClient(url, token, name) service_type = CycladesComputeClient.service_type endpoint = astakos.get_endpoint_url(service_type) compute = CycladesComputeClient(endpoint, token) vm_name = name flavor = '260' image = 'eca2f4ef-b428-4096-a47d-29ddf5ed68d9' # server = compute.create_server(name=vm_name, flavor_id=flavor, image_id=image) server = compute.create_server(name=vm_name, key_name='cluster key', flavor_id=flavor, image_id=image, project_id=project) print(server['status']) with open(vm_name + '.info', "w") as f: for s in server: f.write(s) active = compute.wait_server_until(server['id'], 'ACTIVE') if active != 'ACTIVE': print('Waiting for server to build...')
def REMOVE_VM(url, token, server): astakos = AstakosClient(url, token) service_type = CycladesComputeClient.service_type endpoint = astakos.get_endpoint_url(service_type) compute = CycladesComputeClient(endpoint, token) # find server id s = compute.list_servers(name=server) for i in s: if i['name'] == server: id = i['id'] print(id) compute.delete_server(id)
def _get_pithos_client(self, auth_url, token, container): try: astakos = AstakosClient(auth_url, token) except ClientError: logger.error("Failed to authenticate user token") raise try: PITHOS_URL = astakos.get_endpoint_url( AgkyraPithosClient.service_type) except ClientError: logger.error("Failed to get endpoints for Pithos") raise try: account = astakos.user_info['id'] return AgkyraPithosClient(PITHOS_URL, token, account, container) except ClientError: logger.error("Failed to initialize Pithos client") raise
def list_pithos_files(self): """ Method for listing pithos+ files available to the user """ auth_url = self.opts['auth_url'] token = self.opts['token'] try: auth = AstakosClient(auth_url, token) auth.authenticate() except ClientError: msg = ' Authentication error: Invalid Token' logging.error(msg) exit(error_fatal) pithos_endpoint = auth.get_endpoint_url('object-store') pithos_container = self.opts.get('pithos_container','pithos') user_id = auth.user_info['id'] pithos_client = PithosClient(pithos_endpoint,self.opts['token'], user_id, pithos_container) objects = pithos_client.list_objects() for object in objects: is_dir = 'application/directory' in object.get('content_type', object.get('content-type', '')) if not is_dir: print u"{:>12s} \"pithos:/{:s}/{:s}\"".format(bytes_to_shorthand(object['bytes']), pithos_container,object['name'])
def list_pithos_files(self): """ Method for listing pithos+ files available to the user """ auth_url = self.opts['auth_url'] token = self.opts['token'] try: auth = AstakosClient(auth_url, token) auth.authenticate() except ClientError: msg = 'Authentication error: Invalid Token' logging.error(msg) exit(error_fatal) pithos_endpoint = auth.get_endpoint_url('object-store') pithos_container = self.opts.get('pithos_container','pithos') user_id = auth.user_info['id'] pithos_client = PithosClient(pithos_endpoint,self.opts['token'], user_id, pithos_container) objects = pithos_client.list_objects() for object in objects: is_dir = 'application/directory' in object.get('content_type', object.get('content-type', '')) is_dir = 'application/folder' in object.get('content_type', object.get('content-type', '')) if not is_dir: print u"{:>12s} \"pithos:/{:s}/{:s}\"".format(bytes_to_shorthand(object['bytes']), pithos_container,object['name'])
except ImportError: pass import webob.exc # endpoints are offered auth-free, so no need for an admin token ADMIN_TOKEN = '' auth = AstakosClient(AUTH_URL, ADMIN_TOKEN) endpoints = {'identity': AUTH_URL} client_classes = {'identity': AstakosClient} for cls in (CycladesComputeClient, CycladesNetworkClient, CycladesBlockStorageClient): service_type = cls.service_type endpoints[service_type] = auth.get_endpoint_url(service_type) client_classes[service_type] = cls def handle_exceptions(f): """Run a method, raise Synnefo errors as snf-occi exceptions""" def wrapper(*args, **kwargs): try: return f(*args, **kwargs) except ClientError as ce: print 'ClientError:', ce, ce.status, ce.details exc = { 400: webob.exc.HTTPBadRequest, 401: webob.exc.HTTPUnauthorized, 403: webob.exc.HTTPForbidden, 404: webob.exc.HTTPNotFound,
https.patch_ignore_ssl() authURL = "https://accounts.okeanos.grnet.gr/identity/v2.0" X_AUTH_TOKEN_NAME = 'X_AUTH_TOKEN' token = ENV[X_AUTH_TOKEN_NAME] projectId = '464eb0e7-b556-4fc7-8afb-d590feebaad8' serverId = '660580' cycladesServiceType = CycladesClient.service_type blockStorageServiceType = CycladesBlockStorageClient.service_type ac = AstakosClient(authURL, token) cycladesURL = ac.get_endpoint_url(cycladesServiceType) cc = CycladesClient(cycladesURL, token) blockStorageURL = ac.get_endpoint_url(blockStorageServiceType) bsc = CycladesBlockStorageClient(blockStorageURL, token) onc = OkeanosNativeClient(token, authURL) print "cycladesURL = %s" % cycladesURL print "blockStorageURL = %s" % blockStorageURL print "ac = %s" % ac print "cc = %s" % cc print "bsc = %s" % bsc print "onc = %s" % onc # servers = cc.list_servers()
def get_network_client(self): astakos = AstakosClient(self.endpoints['astakos'], self.auth_token) network_url = astakos.get_endpoint_url(CycladesNetworkClient.service_type) network_client = CycladesNetworkClient(network_url, self.auth_token) return network_client
class Clients(object): """Our kamaki clients""" auth_url = None token = None # Astakos astakos = None retry = CONNECTION_RETRY_LIMIT # Compute compute = None compute_url = None # Cyclades cyclades = None # Network network = None network_url = None # Pithos pithos = None pithos_url = None # Image image = None image_url = None def _kamaki_ssl(self, ignore_ssl=None): """Patch kamaki to use the correct CA certificates Read kamaki's config file and decide if we are going to use CA certificates and patch kamaki clients accordingly. """ config = kamaki_config.Config() if ignore_ssl is None: ignore_ssl = config.get("global", "ignore_ssl").lower() == "on" ca_file = config.get("global", "ca_certs") if ignore_ssl: # Skip SSL verification https.patch_ignore_ssl() else: # Use ca_certs path found in kamakirc https.patch_with_certs(ca_file) def initialize_clients(self, ignore_ssl=False): """Initialize all the Kamaki Clients""" # Path kamaki for SSL verification self._kamaki_ssl(ignore_ssl=ignore_ssl) # Initialize kamaki Clients self.astakos = AstakosClient(self.auth_url, self.token) self.astakos.CONNECTION_RETRY_LIMIT = self.retry self.compute_url = self.astakos.get_endpoint_url( ComputeClient.service_type) self.compute = ComputeClient(self.compute_url, self.token) self.compute.CONNECTION_RETRY_LIMIT = self.retry self.cyclades_url = self.astakos.get_endpoint_url( CycladesClient.service_type) self.cyclades = CycladesClient(self.cyclades_url, self.token) self.cyclades.CONNECTION_RETRY_LIMIT = self.retry self.block_storage_url = self.astakos.get_endpoint_url( BlockStorageClient.service_type) self.block_storage = BlockStorageClient(self.block_storage_url, self.token) self.block_storage.CONNECTION_RETRY_LIMIT = self.retry self.network_url = self.astakos.get_endpoint_url( CycladesNetworkClient.service_type) self.network = CycladesNetworkClient(self.network_url, self.token) self.network.CONNECTION_RETRY_LIMIT = self.retry self.pithos_url = self.astakos.get_endpoint_url( PithosClient.service_type) self.pithos = PithosClient(self.pithos_url, self.token) self.pithos.CONNECTION_RETRY_LIMIT = self.retry self.image_url = self.astakos.get_endpoint_url( ImageClient.service_type) self.image = ImageClient(self.image_url, self.token) self.image.CONNECTION_RETRY_LIMIT = self.retry
class Clients(object): """Our kamaki clients""" auth_url = None token = None # Astakos astakos = None retry = CONNECTION_RETRY_LIMIT # Compute compute = None compute_url = None # Cyclades cyclades = None # Network network = None network_url = None # Pithos pithos = None pithos_url = None # Image image = None image_url = None def _kamaki_ssl(self, ignore_ssl=None): """Patch kamaki to use the correct CA certificates Read kamaki's config file and decide if we are going to use CA certificates and patch kamaki clients accordingly. """ config = kamaki_config.Config() if ignore_ssl is None: ignore_ssl = config.get("global", "ignore_ssl").lower() == "on" ca_file = config.get("global", "ca_certs") if ignore_ssl: # Skip SSL verification https.patch_ignore_ssl() else: # Use ca_certs path found in kamakirc https.patch_with_certs(ca_file) def initialize_clients(self, ignore_ssl=False): """Initialize all the Kamaki Clients""" # Path kamaki for SSL verification self._kamaki_ssl(ignore_ssl=ignore_ssl) # Initialize kamaki Clients self.astakos = AstakosClient(self.auth_url, self.token) self.astakos.CONNECTION_RETRY_LIMIT = self.retry self.compute_url = self.astakos.get_endpoint_url( ComputeClient.service_type) self.compute = ComputeClient(self.compute_url, self.token) self.compute.CONNECTION_RETRY_LIMIT = self.retry self.cyclades_url = self.astakos.get_endpoint_url( CycladesClient.service_type) self.cyclades = CycladesClient(self.cyclades_url, self.token) self.cyclades.CONNECTION_RETRY_LIMIT = self.retry self.block_storage_url = self.astakos.get_endpoint_url( CycladesBlockStorageClient.service_type) self.block_storage = CycladesBlockStorageClient( self.block_storage_url, self.token) self.block_storage.CONNECTION_RETRY_LIMIT = self.retry self.network_url = self.astakos.get_endpoint_url( CycladesNetworkClient.service_type) self.network = CycladesNetworkClient(self.network_url, self.token) self.network.CONNECTION_RETRY_LIMIT = self.retry self.pithos_url = self.astakos.get_endpoint_url( PithosClient.service_type) self.pithos = PithosClient(self.pithos_url, self.token) self.pithos.CONNECTION_RETRY_LIMIT = self.retry self.image_url = self.astakos.get_endpoint_url( ImageClient.service_type) self.image = ImageClient(self.image_url, self.token) self.image.CONNECTION_RETRY_LIMIT = self.retry
def get_image_client(self): astakos = AstakosClient(self.endpoints['astakos'], self.auth_token) image_url = astakos.get_endpoint_url(ImageClient.service_type) plankton = ImageClient(image_url, self.auth_token) return plankton
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # # The views and conclusions contained in the software and # documentation are those of the authors and should not be # interpreted as representing official policies, either expressed # or implied, of GRNET S.A. from kamaki.clients.astakos import AstakosClient from kamaki.clients.image import ImageClient AUTHENTICATION_URL = "https://astakos.example.com/identity/v2.0" TOKEN = "User-Token" astakos = AstakosClient(AUTHENTICATION_URL, TOKEN) user = astakos.authenticate() uuid = user["access"]["user"]["id"] service_type = ImageClient.service_type endpoint = astakos.get_endpoint_url(service_type) image = ImageClient(endpoint, TOKEN) # Get all images owned/registered by me images = filter(lambda img: img["owner"] == uuid, image.list_public()) print "My images:\n" for i in images: print "\t{name} ({location})".format(name=i["name"], location=i["location"])
# POSSIBILITY OF SUCH DAMAGE. # # The views and conclusions contained in the software and # documentation are those of the authors and should not be # interpreted as representing official policies, either expressed # or implied, of GRNET S.A. from kamaki.clients.astakos import AstakosClient from kamaki.clients.pithos import PithosClient AUTHENTICATION_URL = "https://astakos.example.com/identity/v2.0" TOKEN = "User-Token" astakos = AstakosClient(AUTHENTICATION_URL, TOKEN) service_type = PithosClient.service_type endpoint = astakos.get_endpoint_url(service_type) pithos = PithosClient(endpoint, TOKEN) user = astakos.authenticate() uuid = user["access"]["user"]["id"] pithos.account = uuid # Get the project containers we care for containers = filter( lambda c: c["name"] in ("pithos", "images"), pithos.list_containers()) # Construct dict of the form {CONTAINER_NAME: PROJECT_ID, ...} projects = dict([( c["name"], c["x_container_policy"]["project"]) for c in containers])
def get_compute_client(self): astakos = AstakosClient(self.endpoints['astakos'], self.auth_token) cyclades_url = astakos.get_endpoint_url(CycladesComputeClient.service_type) compute_client = CycladesComputeClient(cyclades_url, self.auth_token) return compute_client