本文整理匯總了Python中libcloud.compute.types.Provider.GCE屬性的典型用法代碼示例。如果您正苦於以下問題:Python Provider.GCE屬性的具體用法?Python Provider.GCE怎麽用?Python Provider.GCE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類libcloud.compute.types.Provider
的用法示例。
在下文中一共展示了Provider.GCE屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: delete_networks
# 需要導入模塊: from libcloud.compute.types import Provider [as 別名]
# 或者: from libcloud.compute.types.Provider import GCE [as 別名]
def delete_networks(self, driver, inf, timeout=120):
"""
Delete created GCE networks
"""
for gce_net in driver.ex_list_networks():
net_prefix = "im-%s-" % inf.id
if gce_net.name.startswith(net_prefix):
self.log_info("Deleting net %s." % gce_net.name)
cont = 0
deleted = False
while not deleted and cont < timeout:
cont += 5
try:
gce_net.destroy()
deleted = True
except Exception as ex:
self.log_warn("Error removing net: " + str(ex))
if not deleted:
time.sleep(5)
return True
示例2: parse_cli_args
# 需要導入模塊: from libcloud.compute.types import Provider [as 別名]
# 或者: from libcloud.compute.types.Provider import GCE [as 別名]
def parse_cli_args(self):
''' Command line argument processing '''
parser = argparse.ArgumentParser(
description='Produce an Ansible Inventory file based on GCE')
parser.add_argument('--list', action='store_true', default=True,
help='List instances (default: True)')
parser.add_argument('--host', action='store',
help='Get all information about an instance')
parser.add_argument('--instance-tags', action='store',
help='Only include instances with this tags, separated by comma')
parser.add_argument('--pretty', action='store_true', default=False,
help='Pretty format (default: False)')
parser.add_argument(
'--refresh-cache', action='store_true', default=False,
help='Force refresh of cache by making API requests (default: False - use cache files)')
self.args = parser.parse_args()
示例3: parse_cli_args
# 需要導入模塊: from libcloud.compute.types import Provider [as 別名]
# 或者: from libcloud.compute.types.Provider import GCE [as 別名]
def parse_cli_args(self):
''' Command line argument processing '''
parser = argparse.ArgumentParser(
description='Produce an Ansible Inventory file based on GCE')
parser.add_argument('--list', action='store_true', default=True,
help='List instances (default: True)')
parser.add_argument('--host', action='store',
help='Get all information about an instance')
parser.add_argument('--tagged', action='store',
help='Only include instances with this tag')
parser.add_argument('--pretty', action='store_true', default=False,
help='Pretty format (default: False)')
self.args = parser.parse_args()
tag_env = os.environ.get('GCE_TAGGED_INSTANCES')
if not self.args.tagged and tag_env:
self.args.tagged = tag_env
示例4: __init__
# 需要導入模塊: from libcloud.compute.types import Provider [as 別名]
# 或者: from libcloud.compute.types.Provider import GCE [as 別名]
def __init__(self, user_id, key, project):
"""Initialize Compute Engine Manager
:param user_id: Email address (Service Accounts) or Client ID
:type user_id: ``str``
:param key: Key file path (Service Accounts) or Client Secret
:type key: ``str``
:param project: GCE project name
:type project: ``str``
"""
self.logger = logging.getLogger(self.__class__.__name__)
self.user_id = user_id
self.key = key
self.project = project
self.gce = None
self.nodes = []
示例5: get_driver
# 需要導入模塊: from libcloud.compute.types import Provider [as 別名]
# 或者: from libcloud.compute.types.Provider import GCE [as 別名]
def get_driver(self, auth_data, datacenter=None):
"""
Get the compute driver from the auth data
Arguments:
- auth(Authentication): parsed authentication tokens.
- datacenter(str): datacenter to connect.
Returns: a :py:class:`libcloud.compute.base.NodeDriver` or None in case of error
"""
auths = auth_data.getAuthInfo(self.type)
if not auths:
raise Exception("No auth data has been specified to GCE.")
else:
auth = auths[0]
if self.driver and self.auth.compare(auth_data, self.type) and self.datacenter == datacenter:
return self.driver
else:
self.auth = auth_data
self.datacenter = datacenter
if 'username' in auth and 'password' in auth and 'project' in auth:
cls = libcloud_get_driver(Provider.GCE)
# Patch to solve some client problems with \\n
auth['password'] = auth['password'].replace('\\n', '\n')
lines = len(auth['password'].replace(" ", "").split())
if lines < 2:
raise Exception("The certificate provided to the GCE plugin has an incorrect format."
" Check that it has more than one line.")
driver = cls(auth['username'], auth['password'], project=auth['project'], datacenter=datacenter)
self.driver = driver
return driver
else:
self.log_error("No correct auth data has been specified to GCE: username, password and project")
self.log_debug(auth)
raise Exception(
"No correct auth data has been specified to GCE: username, password and project")
示例6: get_dns_driver
# 需要導入模塊: from libcloud.compute.types import Provider [as 別名]
# 或者: from libcloud.compute.types.Provider import GCE [as 別名]
def get_dns_driver(self, auth_data):
"""
Get the DNS driver from the auth data
Arguments:
- auth(Authentication): parsed authentication tokens.
Returns: a :py:class:`libcloud.dns.base.DNSDriver` or None in case of error
"""
auths = auth_data.getAuthInfo(self.type)
if not auths:
raise Exception("No auth data has been specified to GCE.")
else:
auth = auths[0]
if self.dns_driver and self.auth.compare(auth_data, self.type):
return self.dns_driver
else:
self.auth = auth_data
if 'username' in auth and 'password' in auth and 'project' in auth:
cls = get_dns_driver(DNSProvider.GOOGLE)
# Patch to solve some client problems with \\n
auth['password'] = auth['password'].replace('\\n', '\n')
lines = len(auth['password'].replace(" ", "").split())
if lines < 2:
raise Exception("The certificate provided to the GCE plugin has an incorrect format."
" Check that it has more than one line.")
driver = cls(auth['username'], auth['password'], project=auth['project'])
self.dns_driver = driver
return driver
else:
self.log_error("No correct auth data has been specified to GCE: username, password and project")
self.log_debug(auth)
raise Exception(
"No correct auth data has been specified to GCE: username, password and project")
示例7: delete_routes
# 需要導入模塊: from libcloud.compute.types import Provider [as 別名]
# 或者: from libcloud.compute.types.Provider import GCE [as 別名]
def delete_routes(self, driver, inf):
"""
Delete created GCE routes
"""
for gce_route in driver.ex_list_routes():
name_prefix = "im-%s-" % inf.id
if gce_route.name.startswith(name_prefix):
self.log_info("Deleting route %s." % gce_route.name)
gce_route.destroy()
return True
示例8: parse_cli_args
# 需要導入模塊: from libcloud.compute.types import Provider [as 別名]
# 或者: from libcloud.compute.types.Provider import GCE [as 別名]
def parse_cli_args(self):
''' Command line argument processing '''
parser = argparse.ArgumentParser(
description='Produce an Ansible Inventory file based on GCE')
parser.add_argument('--list', action='store_true', default=True,
help='List instances (default: True)')
parser.add_argument('--host', action='store',
help='Get all information about an instance')
parser.add_argument('--pretty', action='store_true', default=False,
help='Pretty format (default: False)')
self.args = parser.parse_args()
示例9: parse_cli_args
# 需要導入模塊: from libcloud.compute.types import Provider [as 別名]
# 或者: from libcloud.compute.types.Provider import GCE [as 別名]
def parse_cli_args(self):
''' Command line argument processing '''
parser = argparse.ArgumentParser(
description='Produce an Ansible Inventory file based on GCE')
parser.add_argument('--list', action='store_true', default=True,
help='List instances (default: True)')
parser.add_argument('--host', action='store',
help='Get all information about an instance')
parser.add_argument('--pretty', action='store_true', default=False,
help='Pretty format (default: False)')
parser.add_argument(
'--refresh-cache', action='store_true', default=False,
help='Force refresh of cache by making API requests (default: False - use cache files)')
self.args = parser.parse_args()
示例10: main
# 需要導入模塊: from libcloud.compute.types import Provider [as 別名]
# 或者: from libcloud.compute.types.Provider import GCE [as 別名]
def main():
key_values = ['project', 'firewall_db', 'keyfile']
if(len(sys.argv) != 2):
sys.exit(1)
# parse config.json
config_file = sys.argv[1]
config_content = json.load(open(config_file))
projects = []
for value in config_content:
temp = []
for key in key_values:
temp.append(value[key])
projects.append(temp)
for project in projects:
project_name = project[0]
db_filename = project[1]
key_file = project[2]
key_content = json.load(open(key_file))
ComputeEngine = get_driver(Provider.GCE)
driver = ComputeEngine(
key_content['client_email'],
key_file,
project=project_name)
gcp_firewall_rules = driver.ex_list_firewalls()
firewall_list = convert_to_list(gcp_firewall_rules, db_filename)
with open(db_filename, 'w') as f:
json.dump(firewall_list, f, indent=2)
sys.exit(0)
示例11: _readClusterSettings
# 需要導入模塊: from libcloud.compute.types import Provider [as 別名]
# 或者: from libcloud.compute.types.Provider import GCE [as 別名]
def _readClusterSettings(self):
"""
Read the cluster settings from the instance, which should be the leader.
See https://cloud.google.com/compute/docs/storing-retrieving-metadata for details about
reading the metadata.
"""
metadata_server = "http://metadata/computeMetadata/v1/instance/"
metadata_flavor = {'Metadata-Flavor': 'Google'}
zone = requests.get(metadata_server + 'zone', headers = metadata_flavor).text
self._zone = zone.split('/')[-1]
project_metadata_server = "http://metadata/computeMetadata/v1/project/"
self._projectId = requests.get(project_metadata_server + 'project-id', headers = metadata_flavor).text
# From a GCE instance, these values can be blank. Only the projectId is needed
self._googleJson = ''
self._clientEmail = ''
self._tags = requests.get(metadata_server + 'description', headers = metadata_flavor).text
tags = json.loads(self._tags)
self.clusterName = tags['clusterName']
self._gceDriver = self._getDriver()
self._instanceGroup = self._gceDriver.ex_get_instancegroup(self.clusterName, zone=self._zone)
leader = self.getLeader()
self._leaderPrivateIP = leader.privateIP
# generate a public key for the leader, which is used to talk to workers
self._masterPublicKey = self._setSSH()
# The location of the Google credentials file on instances.
self._credentialsPath = GoogleJobStore.nodeServiceAccountJson
self._keyName = 'core' # key name leader users to communicate with works
self._botoPath = self.NODE_BOTO_PATH # boto credentials (used if reading an AWS bucket)
示例12: retryPredicate
# 需要導入模塊: from libcloud.compute.types import Provider [as 別名]
# 或者: from libcloud.compute.types.Provider import GCE [as 別名]
def retryPredicate(e):
""" Not used by GCE """
return False
示例13: _getDriver
# 需要導入模塊: from libcloud.compute.types import Provider [as 別名]
# 或者: from libcloud.compute.types.Provider import GCE [as 別名]
def _getDriver(self):
""" Connect to GCE """
driverCls = get_driver(Provider.GCE)
return driverCls(self._clientEmail,
self._googleJson,
project=self._projectId,
datacenter=self._zone)
示例14: connect
# 需要導入模塊: from libcloud.compute.types import Provider [as 別名]
# 或者: from libcloud.compute.types.Provider import GCE [as 別名]
def connect(self, **kwargs):
"""Connect to Google Compute Engine.
"""
try:
self.gce = get_driver(Provider.GCE)(
self.user_id,
self.key,
project=self.project,
**kwargs)
except Exception as exc:
raise ComputeEngineManagerException("Unable to connect to Google Compute Engine: %s" % (str(exc),))
示例15: create_networks
# 需要導入模塊: from libcloud.compute.types import Provider [as 別名]
# 或者: from libcloud.compute.types.Provider import GCE [as 別名]
def create_networks(self, driver, radl, inf):
"""
Create GCE networks
"""
try:
i = 0
while radl.systems[0].getValue("net_interface." + str(i) + ".connection"):
net_name = radl.systems[0].getValue("net_interface." + str(i) + ".connection")
i += 1
network = radl.get_network_by_id(net_name)
if network.getValue('create') == 'yes' and not network.isPublic():
gce_net_name = "im-%s-%s" % (inf.id, net_name)
# First check if the net already exists
net = None
try:
net = driver.ex_get_network(gce_net_name)
except Exception:
self.log_debug("Net %s does not exist." % gce_net_name)
if net:
self.log_debug("Net %s already exist. Do not create it." % gce_net_name)
else:
net_cidr = network.getValue('cidr')
if net_cidr:
mode = "legacy"
used_cidrs = [gce_net.cidr for gce_net in driver.ex_list_networks()]
net_cidr = self.get_free_cidr(net_cidr, used_cidrs, inf)
else:
mode = "auto"
self.log_info("Create net %s with cidr %s." % (gce_net_name, net_cidr))
driver.ex_create_network(gce_net_name, net_cidr, "Net created by the IM", mode=mode)
if net_cidr:
network.setValue('cidr', net_cidr)
# Set also the cidr in the inf RADL
inf.radl.get_network_by_id(network.id).setValue('cidr', net_cidr)
network.setValue('provider_id', gce_net_name)
except Exception as ext:
self.log_exception("Error creating networks.")
try:
self.delete_networks(driver, inf)
except Exception:
self.log_exception("Error deleting networks.")
raise Exception("Error creating networks: %s" % ext)
return True