def _connect_to_openstack_sdk(*args, **kwargs): """ Connect to OpenStack SDK client """ # Atmosphere was configured on 'v2' naming. # This will update the value to the current naming, 'project_name' from openstack import profile from openstack import utils identity_version = kwargs.get('identity_api_version', 2) if identity_version == 2: return None utils.enable_logging( True, stream=sys.stdout) # TODO: stream this to _not_ stdout user_profile = profile.Profile() user_profile.set_region(profile.Profile.ALL, kwargs.get('region_name')) if 'project_name' not in kwargs and 'tenant_name' in kwargs: kwargs['project_name'] = kwargs.pop('tenant_name') user_profile.set_version('identity', 'v%s' % identity_version) user_profile.set_interface('identity', 'admin') user_agent = "rtwo/%s" % (rtwo_version(), ) stack_sdk = openstack_sdk.Connection(user_agent=user_agent, profile=user_profile, **kwargs) return stack_sdk
def from_config(cloud_name=None, cloud_config=None, options=None): """Create a Connection using openstack.config :param str cloud_name: Use the `cloud_name` configuration details when creating the Connection instance. :param cloud_config: An instance of `openstack.config.loader.OpenStackConfig` as returned from openstack.config. If no `config` is provided, `openstack.config.OpenStackConfig` will be called, and the provided `cloud_name` will be used in determining which cloud's configuration details will be used in creation of the `Connection` instance. :param options: A namespace object; allows direct passing in of options to be added to the cloud config. This does not have to be an instance of argparse.Namespace, despite the naming of the the `openstack.config.loader.OpenStackConfig.get_one_cloud` argument to which it is passed. :rtype: :class:`~openstack.connection.Connection` """ if cloud_config is None: occ = openstack.config.OpenStackConfig() cloud_config = occ.get_one_cloud(cloud=cloud_name, argparse=options) if cloud_config.debug: utils.enable_logging(True, stream=sys.stdout) return Connection(config=cloud_config)
def _connect_to_openstack_sdk(*args, **kwargs): """ Connect to OpenStack SDK client """ # Atmosphere was configured on 'v2' naming. # This will update the value to the current naming, 'project_name' from openstack import profile from openstack import utils identity_version = kwargs.get('identity_api_version', 2) if identity_version == 2: return None utils.enable_logging(True, stream=sys.stdout) # TODO: stream this to _not_ stdout user_profile = profile.Profile() user_profile.set_region(profile.Profile.ALL, kwargs.get('region_name')) if 'project_name' not in kwargs and 'tenant_name' in kwargs: kwargs['project_name'] = kwargs.pop('tenant_name') user_profile.set_version('identity', 'v%s' % identity_version) user_profile.set_interface('identity', 'admin') user_agent = "rtwo/%s" % (rtwo_version(),) stack_sdk = openstack_sdk.Connection( user_agent=user_agent, profile=user_profile, **kwargs ) return stack_sdk
def _console_tests(self, fake_logging, level, debug, stream): the_logger = mock.Mock() fake_logging.getLogger.return_value = the_logger utils.enable_logging(debug=debug, stream=stream) self.assertEqual(the_logger.addHandler.call_count, 2) the_logger.setLevel.assert_called_with(level)
def test_none(self): utils.enable_logging(debug=True) self.fake_get_logger.assert_has_calls([]) self.openstack_logger.setLevel.assert_called_with(logging.DEBUG) self.assertEqual(self.openstack_logger.addHandler.call_count, 1) self.assertIsInstance( self.openstack_logger.addHandler.call_args_list[0][0][0], logging.StreamHandler)
def _file_tests(self, fake_logging, level, debug): the_logger = mock.Mock() fake_logging.getLogger.return_value = the_logger fake_path = "fake/path.log" utils.enable_logging(debug=debug, path=fake_path) fake_logging.FileHandler.assert_called_with(fake_path) self.assertEqual(the_logger.addHandler.call_count, 2) the_logger.setLevel.assert_called_with(level)
def from_config(opts): """Create a connection from a configuration. Create a :class:`~openstack.connection.Connection` from a configuration similar to a os-client-config CloudConfig. :param opts: An options class like the :class:`~argparse.Namespace` class. :rtype: :class:`~openstack.connection.Connection` """ # TODO(thowe): I proposed that service name defaults to None in OCC defaults = {} prof = profile.Profile() services = [service.service_type for service in prof.get_services()] for service in services: defaults[service + '_service_name'] = None # TODO(thowe): default is 2 which turns into v2 which doesn't work # this stuff needs to be fixed where we keep version and path separated. defaults['network_api_version'] = 'v2.0' # Get the cloud_config occ = os_client_config.OpenStackConfig(override_defaults=defaults) cloud_config = occ.get_one_cloud(opts.cloud, argparse=opts) if cloud_config.debug: utils.enable_logging(True, stream=sys.stdout) # TODO(mordred) we need to add service_type setting to openstacksdk. # Some clouds have type overridden as well as name. prof = profile.Profile() services = [service.service_type for service in prof.get_services()] for service in cloud_config.get_services(): if service in services: version = cloud_config.get_api_version(service) if version: version = str(version) if not version.startswith("v"): version = "v" + version prof.set_version(service, version) prof.set_name(service, cloud_config.get_service_name(service)) prof.set_interface( service, cloud_config.get_interface(service)) prof.set_region(service, cloud_config.get_region_name(service)) # Auth auth = cloud_config.config['auth'] # TODO(thowe) We should be using auth_type auth['auth_plugin'] = cloud_config.config['auth_type'] if 'cacert' in cloud_config.config: auth['verify'] = cloud_config.config['cacert'] if 'insecure' in cloud_config.config: auth['verify'] = not bool(cloud_config.config['insecure']) return Connection(profile=prof, **auth)
def __init__(self, debug=False): utils.enable_logging(debug=debug, stream=sys.stdout) try: self.conn = connection.Connection(auth_url=self.auth_url, user_domain_id=self.userDomainId, project_id=self.projectId, username=self.username, password=self.password, verify=False) except _exceptions.InvalidRequest as e: raise _exceptions.InvalidRequest(message='init connect error')
def _file_tests(self, level, debug): file_handler = mock.Mock() self.useFixture( fixtures.MonkeyPatch('logging.FileHandler', file_handler)) fake_path = "fake/path.log" utils.enable_logging(debug=debug, path=fake_path) file_handler.assert_called_with(fake_path) self.assertEqual(self.openstack_logger.addHandler.call_count, 1) self.openstack_logger.setLevel.assert_called_with(level)
def from_config(opts): """Create a connection from a configuration. Create a :class:`~openstack.connection.Connection` from a configuration similar to a os-client-config CloudConfig. :param opts: An options class like the :class:`~argparse.Namespace` class. :rtype: :class:`~openstack.connection.Connection` """ # TODO(thowe): I proposed that service name defaults to None in OCC defaults = {} prof = profile.Profile() services = [service.service_type for service in prof.get_services()] for service in services: defaults[service + '_service_name'] = None # TODO(thowe): default is 2 which turns into v2 which doesn't work # this stuff needs to be fixed where we keep version and path separated. defaults['network_api_version'] = 'v2.0' # Get the cloud_config occ = os_client_config.OpenStackConfig(override_defaults=defaults) cloud_config = occ.get_one_cloud(opts.cloud, argparse=opts) if cloud_config.debug: utils.enable_logging(True, stream=sys.stdout) # TODO(mordred) we need to add service_type setting to openstacksdk. # Some clouds have type overridden as well as name. prof = profile.Profile() services = [service.service_type for service in prof.get_services()] for service in cloud_config.get_services(): if service in services: version = cloud_config.get_api_version(service) if version: version = str(version) if not version.startswith("v"): version = "v" + version prof.set_version(service, version) prof.set_name(service, cloud_config.get_service_name(service)) prof.set_interface(service, cloud_config.get_interface(service)) prof.set_region(service, cloud_config.get_region_name(service)) # Auth auth = cloud_config.config['auth'] # TODO(thowe) We should be using auth_type auth['auth_plugin'] = cloud_config.config['auth_type'] if 'cacert' in cloud_config.config: auth['verify'] = cloud_config.config['cacert'] if 'insecure' in cloud_config.config: auth['verify'] = not bool(cloud_config.config['insecure']) return Connection(profile=prof, **auth)
def setUpClass(cls): name = os.getenv('OS_CLOUD', 'test_cloud') test_cloud = os_client_config.OpenStackConfig().get_one_cloud(name) prof = profile.Profile() prof.set_region(prof.ALL, test_cloud.region) if test_cloud.debug: utils.enable_logging(True, stream=sys.stdout) auth = test_cloud.config['auth'] if 'insecure' in test_cloud.config: auth['verify'] = not bool(test_cloud.config['insecure']) cls.conn = connection.Connection(profile=prof, **auth)
def setUpClass(cls): name = os.getenv('OS_CLOUD', 'test_cloud') test_cloud = os_client_config.OpenStackConfig().get_one_cloud(name) prof = profile.Profile(extensions=hp.extensions) prof.set_region(prof.ALL, test_cloud.region) if test_cloud.debug: utils.enable_logging(True) auth = test_cloud.config['auth'] if 'insecure' in test_cloud.config: auth['verify'] = test_cloud.config['insecure'] cls.conn = connection.Connection(profile=prof, **auth)
def setup(): opts = option_parser().parse_args() utils.enable_logging(opts.debug) return opts
import openstack.config import sys, os, errno from openstack import utils openstack .enable_logging() utils.enable_logging(debug=True, path='openstack.log', stream=sys.stdout) """ cloud_regions = openstack.config.OpenStackConfig().get_all() for cloud_region in cloud_regions: print(cloud_region.name, cloud_region.region, cloud_region.config) """ def create_connection_from_config(): return openstack.connect(cloud='openstack') def list_servers(conn): print('List servers:') for server in conn.compute.servers(): print(server) def list_flavors(conn): print('List flavors:') for flavor in conn.compute.flavors(): print(flavor) def list_images(conn): print('List Images:') for image in conn.compute.images(): print(image)
def main(): utils.enable_logging(debug=True, stream=sys.stdout) conn = connect.create_connection() create_manageiq_instance(conn)
def _console_tests(self, level, debug, stream): utils.enable_logging(debug=debug, stream=stream) self.assertEqual(self.openstack_logger.addHandler.call_count, 1) self.openstack_logger.setLevel.assert_called_with(level)
# a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import six import sys from openstack.tests.functional import base from openstack import utils utils.enable_logging(debug=True,stream=sys.stdout) from openstack.compute.v2.volume_attachment import VolumeAttachment class TestDelVolumeAttachments(base.BaseFunctionalTest): @classmethod def setUpClass(cls): super(TestDelVolumeAttachments, cls).setUpClass() def test_create(self): self.conn.compute.create_volume_attachment("1658753f-8ab2-4650-be05-5ac8d353c56a", device = "/dev/sdb", volume_id = "fbacaf56-ec2f-4cbf-a9e2-06c36fc3b35a") # def test_update(self): # self.conn.compute.update_volume_attachment("fbacaf56-ec2f-4cbf-a9e2-06c36fc3b35a","1658753f-8ab2-4650-be05-5ac8d353c56a", device = "/dev/sdb") def test_del_volume(self): self.conn.compute.delete_volume_attachment("fbacaf56-ec2f-4cbf-a9e2-06c36fc3b35a", "1658753f-8ab2-4650-be05-5ac8d353c56a",force_del = True)
print(filter_stack_trace(frame, 'lib/python', 1)) #print("-- 1:") traceback.print_stack(frame, limit=1, file=sys.stdout) #print("-- FULL:") #traceback.print_stack(frame, file=sys.stdout) raise TimeoutException("OpenStack - timeout") dtstring = strftime("%Y-%m-%d %H:%M:%S", gmtime()) print("Starting at: " + dtstring) signal.signal(signal.SIGALRM, signalHandler) ''' Read connection information for specified cloud from clouds.yaml file ''' utils.enable_logging(False, stream=sys.stdout) def connectToCloud(cloudName): occ = os_client_config.OpenStackConfig() cloud = occ.get_one_cloud(cloudName) return connection.from_config(cloud_config=cloud) def getServerFields(server, fields, flavor_names, image_names): values = [] for field in fields: if field == 'name' or field == 'status':
traceback.print_stack(frame, limit=1, file=sys.stdout) #print("-- FULL:") #traceback.print_stack(frame, file=sys.stdout) raise TimeoutException("OpenStack - timeout") dtstring = strftime("%Y-%m-%d %H:%M:%S", gmtime()) print("Starting at: " + dtstring) signal.signal(signal.SIGALRM, signalHandler) ''' Read connection information for specified cloud from clouds.yaml file ''' utils.enable_logging(False, stream=sys.stdout) def connectToCloud(cloudName): occ = os_client_config.OpenStackConfig() cloud = occ.get_one_cloud(cloudName) return connection.from_config(cloud_config=cloud) def getServerFields(server, fields, flavor_names, image_names): values=[] for field in fields: if field == 'name' or field == 'status': values.append( server[field] ) elif field == 'addresses':
def setup(): opts = option_parser().parse_args() utils.enable_logging(opts.debug, stream=sys.stdout) return opts
def from_config(cloud_name=None, cloud_config=None, options=None): """Create a Connection using os-client-config :param str cloud_name: Use the `cloud_name` configuration details when creating the Connection instance. :param cloud_config: An instance of `os_client_config.config.OpenStackConfig` as returned from the os-client-config library. If no `config` is provided, `os_client_config.OpenStackConfig` will be called, and the provided `cloud_name` will be used in determining which cloud's configuration details will be used in creation of the `Connection` instance. :param options: A namespace object; allows direct passing in of options to be added to the cloud config. This does not have to be an instance of argparse.Namespace, despite the naming of the the `os_client_config.config.OpenStackConfig.get_one_cloud` argument to which it is passed. :rtype: :class:`~openstack.connection.Connection` """ # TODO(thowe): I proposed that service name defaults to None in OCC defaults = {} prof = _profile.Profile() services = [service.service_type for service in prof.get_services()] for service in services: defaults[service + '_service_name'] = None # TODO(thowe): default is 2 which turns into v2 which doesn't work # this stuff needs to be fixed where we keep version and path separated. defaults['network_api_version'] = 'v2.0' if cloud_config is None: occ = os_client_config.OpenStackConfig(override_defaults=defaults) cloud_config = occ.get_one_cloud(cloud=cloud_name, argparse=options) if cloud_config.debug: utils.enable_logging(True, stream=sys.stdout) # TODO(mordred) we need to add service_type setting to openstacksdk. # Some clouds have type overridden as well as name. services = [service.service_type for service in prof.get_services()] for service in cloud_config.get_services(): if service in services: version = cloud_config.get_api_version(service) if version: version = str(version) if not version.startswith("v"): version = "v" + version prof.set_version(service, version) name = cloud_config.get_service_name(service) if name: prof.set_name(service, name) interface = cloud_config.get_interface(service) if interface: prof.set_interface(service, interface) region = cloud_config.get_region_name(service) if region: for service in services: prof.set_region(service, region) # Auth auth = cloud_config.config['auth'] # TODO(thowe) We should be using auth_type auth['auth_plugin'] = cloud_config.config['auth_type'] if 'cacert' in auth: auth['verify'] = auth.pop('cacert') if 'cacert' in cloud_config.config: auth['verify'] = cloud_config.config['cacert'] insecure = cloud_config.config.get('insecure', False) if insecure: auth['verify'] = False cert = cloud_config.config.get('cert') if cert: key = cloud_config.config.get('key') auth['cert'] = (cert, key) if key else cert return Connection(profile=prof, **auth)
print (image.id) print (flavor.id) print (network.id) server = conn.bare_metal.create_node( name=SERVER_NAME, image_id=image.id, flavor_id=flavor.id, networks=[{"uuid": network.id}], key_name=keypair.name) # server = conn.compute.wait_for_server(server) # print("ssh -i {key} root@{ip}".format( # key=PRIVATE_KEYPAIR_FILE, # ip=server.access_ipv4)) conn = create_connection('https://chi.tacc.chameleoncloud.org:5000/v2.0', 'regionOne', 'CH-819321', 'cjy7117', 'Wait4aTrain7!') conn.authorize() utils.enable_logging(debug=True, stream=sys.stdout) #list_servers(conn) #list_images(conn) #list_flavors(conn) #list_networks(conn) create_server(conn)
def from_config(cloud_name=None, cloud_config=None, options=None): """Create a Connection using os-client-config :param str cloud_name: Use the `cloud_name` configuration details when creating the Connection instance. :param cloud_config: An instance of `os_client_config.config.OpenStackConfig` as returned from the os-client-config library. If no `config` is provided, `os_client_config.OpenStackConfig` will be called, and the provided `cloud_name` will be used in determining which cloud's configuration details will be used in creation of the `Connection` instance. :param options: An argparse Namespace object; allows direct passing in of argparse options to be added to the cloud config. This value is passed to the `argparse` argument of `os_client_config.config.OpenStackConfig.get_one_cloud`. :rtype: :class:`~openstack.connection.Connection` """ # TODO(thowe): I proposed that service name defaults to None in OCC defaults = {} prof = _profile.Profile() services = [service.service_type for service in prof.get_services()] for service in services: defaults[service + '_service_name'] = None # TODO(thowe): default is 2 which turns into v2 which doesn't work # this stuff needs to be fixed where we keep version and path separated. defaults['network_api_version'] = 'v2.0' if cloud_config is None: occ = os_client_config.OpenStackConfig(override_defaults=defaults) cloud_config = occ.get_one_cloud(cloud=cloud_name, argparse=options) if cloud_config.debug: utils.enable_logging(True, stream=sys.stdout) # TODO(mordred) we need to add service_type setting to openstacksdk. # Some clouds have type overridden as well as name. services = [service.service_type for service in prof.get_services()] for service in cloud_config.get_services(): if service in services: version = cloud_config.get_api_version(service) if version: version = str(version) if not version.startswith("v"): version = "v" + version prof.set_version(service, version) name = cloud_config.get_service_name(service) if name: prof.set_name(service, name) interface = cloud_config.get_interface(service) if interface: prof.set_interface(service, interface) region = cloud_config.get_region_name(service) if region: for service in services: prof.set_region(service, region) # Auth auth = cloud_config.config['auth'] # TODO(thowe) We should be using auth_type auth['auth_plugin'] = cloud_config.config['auth_type'] if 'cacert' in auth: auth['verify'] = auth.pop('cacert') if 'cacert' in cloud_config.config: auth['verify'] = cloud_config.config['cacert'] insecure = cloud_config.config.get('insecure', False) if insecure: auth['verify'] = False cert = cloud_config.config.get('cert') if cert: key = cloud_config.config.get('key') auth['cert'] = (cert, key) if key else cert return Connection(profile=prof, **auth)
from openstack import exceptions as sdk_exc from openstack import utils as sdk_utils from oslo_config import cfg from oslo_log import log as logging from oslo_serialization import jsonutils from requests import exceptions as req_exc import six from senlin.common import exception as senlin_exc from senlin import version USER_AGENT = 'senlin' exc = sdk_exc LOG = logging.getLogger(__name__) sdk_utils.enable_logging(debug=False, stream=sys.stdout) def parse_exception(ex): '''Parse exception code and yield useful information.''' code = 500 LOG.exception(ex) if isinstance(ex, sdk_exc.HttpException): # some exceptions don't contain status_code if hasattr(ex, "status_code") and ex.status_code is not None: code = ex.status_code elif hasattr(ex, "http_status") and ex.http_status is not None: code = ex.http_status message = six.text_type(ex)