def template(provider, provisioning, setup_provider): template_type = provisioning['stack_provisioning']['template_type'] template_name = fauxfactory.gen_alphanumeric() template = OrchestrationTemplate(template_type=template_type, template_name=template_name) if provider.type == "ec2": data_file = load_data_file( str(orchestration_path.join('aws_vm_template.json'))) elif provider.type == "openstack": data_file = load_data_file( str(orchestration_path.join('openstack_vm_template.data'))) elif provider.type == "azure": data_file = load_data_file( str(orchestration_path.join('azure_vm_template.json'))) template.create(data_file.read().replace('CFMETemplateName', template_name)) if provider.type == "azure": dialog_name = "azure-single-vm-from-user-image" else: dialog_name = "dialog_" + fauxfactory.gen_alphanumeric() if provider.type != "azure": template.create_service_dialog_from_template(dialog_name, template.template_name) yield template, dialog_name
def set_yaml_config(config_name, data_dict, hostname=None): """Given a yaml name, dictionary and hostname, set the configuration yaml on the server The configuration yamls must be inserted into the DB using the ruby console, so this function uses SSH, not the database. It makes sense to be included here as a counterpart to :py:func:`get_yaml_config` Args: config_name: Name of the yaml configuration file data_dict: Dictionary with data to set/change hostname: Hostname/address of the server that we want to set up (default ``None``) Note: If hostname is set to ``None``, the default server set up for this session will be used. See :py:class:``utils.ssh.SSHClient`` for details of the default setup. Warning: Manually editing the config yamls is potentially dangerous. Furthermore, the rails runner doesn't return useful information on the outcome of the set request, so errors that arise from the newly loading config file will go unreported. Usage: # Update the appliance name, for example vmbd_yaml = get_yaml_config('vmdb') vmdb_yaml['server']['name'] = 'EVM IS AWESOME' set_yaml_config('vmdb', vmdb_yaml, '1.2.3.4') """ # CFME does a lot of things when loading a configfile, so # let their native conf loader handle the job # If hostname is defined, connect to the specified server if hostname is not None: _ssh_client = SSHClient(hostname=hostname) # Else, connect to the default one set up for this session else: _ssh_client = store.current_appliance.ssh_client # Build & send new config temp_yaml = NamedTemporaryFile() dest_yaml = '/tmp/conf.yaml' yaml.dump(data_dict, temp_yaml, default_flow_style=False) _ssh_client.put_file(temp_yaml.name, dest_yaml) # Build and send ruby script dest_ruby = '/tmp/load_conf.rb' ruby_template = data_path.join('utils', 'cfmedb_load_config.rbt') ruby_replacements = { 'config_name': config_name, 'config_file': dest_yaml } temp_ruby = load_data_file(ruby_template.strpath, ruby_replacements) _ssh_client.put_file(temp_ruby.name, dest_ruby) # Run it _ssh_client.run_rails_command(dest_ruby) fire('server_details_changed') fire('server_config_changed')
def template(provider, provisioning, dialog_name, setup_provider): template_type = provisioning['stack_provisioning']['template_type'] template_name = fauxfactory.gen_alphanumeric() template = OrchestrationTemplate(template_type=template_type, template_name=template_name) if provider.type == "ec2": data_file = load_data_file(str(orchestration_path.join('aws_vm_template.json'))) elif provider.type == "openstack": data_file = load_data_file(str(orchestration_path.join('openstack_vm_template.data'))) elif provider.type == "azure": data_file = load_data_file(str(orchestration_path.join('azure_vm_template.json'))) template.create(data_file.read().replace('CFMETemplateName', template_name)) if provider.type != "azure": template.create_service_dialog_from_template(dialog_name, template.template_name) yield template
def main(): parser = argparse.ArgumentParser( epilog=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('address', help='hostname or ip address of target appliance') parser.add_argument('db_address', help='hostname or ip address of external database') parser.add_argument('--database', default='vmdb_production', help='name of the external database') parser.add_argument('--region', default=0, type=int, help='region to assign to the new DB') parser.add_argument('--username', default=credentials['database']['username'], help='username for external database') parser.add_argument('--password', default=credentials['database']['password'], help='password for external database') args = parser.parse_args() ssh_kwargs = { 'username': credentials['ssh']['username'], 'password': credentials['ssh']['password'], 'hostname': args.address } rbt_repl = { 'miq_lib': '/var/www/miq/lib', 'host': args.db_address, 'database': args.database, 'region': args.region, 'username': args.username, 'password': args.password } # Find and load our rb template with replacements base_path = os.path.dirname(__file__) rbt = datafile.data_path_for_filename('enable-external-db.rbt', base_path) rb = datafile.load_data_file(rbt, rbt_repl) # Init SSH client and sent rb file over to /tmp remote_file = '/tmp/%s' % generate_random_string() client = SSHClient(**ssh_kwargs) client.put_file(rb.name, remote_file) # Run the rb script, clean it up when done print 'Initializing Appliance External DB' status, out = client.run_command('ruby %s' % remote_file) client.run_command('rm %s' % remote_file) if status != 0: print 'Enabling DB failed with error:' print out sys.exit(1) else: print 'DB Enabled, evm watchdog should start the UI shortly.'
def enable_internal(self, region=0, key_address=None, db_password=None, ssh_password=None): """Enables internal database Args: region: Region number of the CFME appliance. key_address: Address of CFME appliance where key can be fetched. Note: If key_address is None, a new encryption key is generated for the appliance. """ self.logger.info('Enabling internal DB (region {}) on {}.'.format( region, self.address)) self.address = self.appliance.address clear_property_cache(self, 'client') client = self.ssh_client # Defaults db_password = db_password or conf.credentials['database']['password'] ssh_password = ssh_password or conf.credentials['ssh']['password'] if self.appliance.has_cli: # use the cli if key_address: status, out = client.run_command( 'appliance_console_cli --region {0} --internal --fetch-key {1} -p {2} -a {3}' .format(region, key_address, db_password, ssh_password)) else: status, out = client.run_command( 'appliance_console_cli --region {} --internal --force-key -p {}' .format(region, db_password)) else: # no cli, use the enable internal db script rbt_repl = { 'miq_lib': '/var/www/miq/lib', 'region': region, 'postgres_version': self.postgres_version } # Find and load our rb template with replacements rbt = datafile.data_path_for_filename('enable-internal-db.rbt', scripts_path.strpath) rb = datafile.load_data_file(rbt, rbt_repl) # sent rb file over to /tmp remote_file = '/tmp/{}'.format(fauxfactory.gen_alphanumeric()) client.put_file(rb.name, remote_file) # Run the rb script, clean it up when done status, out = client.run_command('ruby {}'.format(remote_file)) client.run_command('rm {}'.format(remote_file)) return status, out
def __call__(self, filename, replacements=None): if filename.startswith('/'): complete_path = data_path_for_filename(filename.strip('/'), self.base_path) else: complete_path = data_path_for_filename(filename, self.base_path, self.testmod_path) seen_data_files.add(complete_path) return load_data_file(complete_path, replacements)
def __call__(self, filename, replacements=None): if filename.startswith('/'): complete_path = data_path_for_filename( filename.strip('/'), self.base_path) else: complete_path = data_path_for_filename( filename, self.base_path, self.testmod_path) seen_data_files.add(complete_path) return load_data_file(complete_path, replacements)
def main(): parser = argparse.ArgumentParser(epilog=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('address', help='hostname or ip address of target appliance') parser.add_argument('db_address', help='hostname or ip address of external database') parser.add_argument('--database', default='vmdb_production', help='name of the external database') parser.add_argument('--region', default=0, type=int, help='region to assign to the new DB') parser.add_argument('--username', default=credentials['database']['username'], help='username for external database') parser.add_argument('--password', default=credentials['database']['password'], help='password for external database') args = parser.parse_args() ssh_kwargs = { 'username': credentials['ssh']['username'], 'password': credentials['ssh']['password'], 'hostname': args.address } rbt_repl = { 'miq_lib': '/var/www/miq/lib', 'host': args.db_address, 'database': args.database, 'region': args.region, 'username': args.username, 'password': args.password } # Find and load our rb template with replacements base_path = os.path.dirname(__file__) rbt = datafile.data_path_for_filename( 'enable-external-db.rbt', base_path) rb = datafile.load_data_file(rbt, rbt_repl) # Init SSH client and sent rb file over to /tmp remote_file = '/tmp/%s' % generate_random_string() client = SSHClient(**ssh_kwargs) client.put_file(rb.name, remote_file) # Run the rb script, clean it up when done print 'Initializing Appliance External DB' status, out = client.run_command('ruby %s' % remote_file) client.run_command('rm %s' % remote_file) if status != 0: print 'Enabling DB failed with error:' print out sys.exit(1) else: print 'DB Enabled, evm watchdog should start the UI shortly.'
def enable_internal(self, region=0, key_address=None, db_password=None, ssh_password=None): """Enables internal database Args: region: Region number of the CFME appliance. key_address: Address of CFME appliance where key can be fetched. Note: If key_address is None, a new encryption key is generated for the appliance. """ self.logger.info('Enabling internal DB (region {}) on {}.'.format(region, self.address)) self.address = self.appliance.address clear_property_cache(self, 'client') client = self.ssh_client # Defaults db_password = db_password or conf.credentials['database']['password'] ssh_password = ssh_password or conf.credentials['ssh']['password'] if self.appliance.has_cli: # use the cli if key_address: status, out = client.run_command( 'appliance_console_cli --region {0} --internal --fetch-key {1} -p {2} -a {3}' .format(region, key_address, db_password, ssh_password) ) else: status, out = client.run_command( 'appliance_console_cli --region {} --internal --force-key -p {}' .format(region, db_password) ) else: # no cli, use the enable internal db script rbt_repl = { 'miq_lib': '/var/www/miq/lib', 'region': region, 'postgres_version': self.postgres_version } # Find and load our rb template with replacements rbt = datafile.data_path_for_filename('enable-internal-db.rbt', scripts_path.strpath) rb = datafile.load_data_file(rbt, rbt_repl) # sent rb file over to /tmp remote_file = '/tmp/{}'.format(fauxfactory.gen_alphanumeric()) client.put_file(rb.name, remote_file) # Run the rb script, clean it up when done status, out = client.run_command('ruby {}'.format(remote_file)) client.run_command('rm {}'.format(remote_file)) return status, out
def get_template_from_config(template_config_name): """ Convenience function to grab the details for a template from the yamls. """ template_config = conf.cfme_data.get('customization_templates', {})[template_config_name] script_data = load_data_file(str(project_path.join(template_config['script_file'])), replacements=template_config['replacements']) script_data = script_data.read() return CustomizationTemplate(name=template_config['name'], description=template_config['description'], image_type=template_config['image_type'], script_type=template_config['script_type'], script_data=script_data)
def enable_external(self, db_address, region=0, db_name=None, db_username=None, db_password=None): """Enables external database Args: db_address: Address of the external database region: Number of region to join db_name: Name of the external DB db_username: Username to access the external DB db_password: Password to access the external DB Returns a tuple of (exitstatus, script_output) for reporting, if desired """ self.logger.info('Enabling external DB (db_address {}, region {}) on {}.' .format(db_address, region, self.address)) # reset the db address and clear the cached db object if we have one self.address = db_address clear_property_cache(self, 'client') # default db_name = db_name or 'vmdb_production' db_username = db_username or conf.credentials['database']['username'] db_password = db_password or conf.credentials['database']['password'] client = self.ssh_client if self.appliance.has_cli: # copy v2 key master_client = client(hostname=self.address) rand_filename = "/tmp/v2_key_{}".format(fauxfactory.gen_alphanumeric()) master_client.get_file("/var/www/miq/vmdb/certs/v2_key", rand_filename) client.put_file(rand_filename, "/var/www/miq/vmdb/certs/v2_key") # enable external DB with cli status, out = client.run_command( 'appliance_console_cli ' '--hostname {0} --region {1} --dbname {2} --username {3} --password {4}'.format( self.address, region, db_name, db_username, db_password ) ) else: # no cli, use the enable external db script rbt_repl = { 'miq_lib': '/var/www/miq/lib', 'host': self.address, 'region': region, 'database': db_name, 'username': db_username, 'password': db_password } # Find and load our rb template with replacements rbt = datafile.data_path_for_filename('enable-internal-db.rbt', scripts_path.strpath) rb = datafile.load_data_file(rbt, rbt_repl) # Init SSH client and sent rb file over to /tmp remote_file = '/tmp/{}'.format(fauxfactory.gen_alphanumeric()) client.put_file(rb.name, remote_file) # Run the rb script, clean it up when done status, out = client.run_command('ruby {}'.format(remote_file)) client.run_command('rm {}'.format(remote_file)) if status != 0: self.logger.error('error enabling external db') self.logger.error(out) msg = ('Appliance {} failed to enable external DB running on {}' .format(self.appliance.address, db_address)) self.logger.error(msg) from . import ApplianceException raise ApplianceException(msg) return status, out