def _run_migration(migration, ansible_context, check_mode, no_stop): puts(color_summary('Give ansible user access to couchdb files:')) user_args = "user=ansible groups=couchdb append=yes" run_ansible_module(migration.source_environment, ansible_context, 'couchdb2', 'user', user_args, True, None, False) file_args = "path={} mode=0755".format(migration.couchdb2_data_dir) run_ansible_module(migration.source_environment, ansible_context, 'couchdb2', 'file', file_args, True, None, False) puts(color_summary('Copy file lists to nodes:')) rsync_files_by_host = prepare_to_sync_files(migration, ansible_context) if no_stop: stop_couch_context = noop_context() else: puts(color_summary('Stop couch and reallocate shards')) stop_couch_context = stop_couch(migration.all_environments, ansible_context, check_mode) with stop_couch_context: execute_file_copy_scripts(migration.target_environment, list(rsync_files_by_host), check_mode) return 0
def _run_migration(migration, ansible_context, check_mode, no_stop): puts(colored.blue('Give ansible user access to couchdb files:')) user_args = "user=ansible groups=couchdb append=yes" run_ansible_module( migration.source_environment, ansible_context, 'couchdb2', 'user', user_args, True, None, False ) file_args = "path={} mode=0755".format(migration.couchdb2_data_dir) run_ansible_module( migration.source_environment, ansible_context, 'couchdb2', 'file', file_args, True, None, False ) puts(colored.blue('Copy file lists to nodes:')) rsync_files_by_host = prepare_to_sync_files(migration, ansible_context) if no_stop: stop_couch_context = noop_context() else: puts(colored.blue('Stop couch and reallocate shards')) stop_couch_context = stop_couch(migration.all_environments, ansible_context, check_mode) with stop_couch_context: execute_file_copy_scripts(migration.target_environment, list(rsync_files_by_host), check_mode) return 0
def start_stop_service(environment, ansible_context, service_state, check_mode=False): extra_args = [] if check_mode: extra_args.append('--check') for service in ('monit', 'couchdb2'): args = 'name={} state={}'.format(service, service_state) run_ansible_module( environment, ansible_context, 'couchdb2', 'service', args, extra_args=extra_args )
def start_stop_service(environment, ansible_context, service_state, check_mode=False): extra_args = [] if check_mode: extra_args.append('--check') for service in ('monit', 'couchdb2'): args = 'name={} state={}'.format(service, service_state) run_ansible_module( environment, ansible_context, 'couchdb2', 'service', args, True, None, False, *extra_args )
def _set_auth_key(env, host, user, ansible_context, working_directory, remove=False): state = 'absent' if remove else 'present' key_path = os.path.join(working_directory, 'id_rsa.tmp') args = "user={} state={} key={{{{ lookup('file', '{}') }}}}".format( user, state, key_path) run_ansible_module(env, ansible_context, host, 'authorized_key', args, True, None, None)
def _genearate_and_fetch_key(env, host, user, ansible_context, working_directory): user_args = "name={} generate_ssh_key=yes".format(user) run_ansible_module(env, ansible_context, host, 'user', user_args, True, None, None) user_home_output = PrivilegedCommand( 'ansible', env.get_ansible_user_password(), "getent passwd {} | cut -d: -f6".format(user) ).run_command(host) user_home = user_home_output[host] fetch_args = "src={user_home}/.ssh/id_rsa.pub dest={key_tmp} flat=yes fail_on_missing=yes".format( user_home=user_home, key_tmp=os.path.join(working_directory, 'id_rsa.tmp') ) run_ansible_module(env, ansible_context, host, 'fetch', fetch_args, True, None, None)
def _genearate_and_fetch_key(env, host, user, ansible_context, working_directory): user_args = "name={} generate_ssh_key=yes".format(user) run_ansible_module(env, ansible_context, host, 'user', user_args, True, None, None) user_home_output = PrivilegedCommand( 'ansible', env.get_ansible_user_password(), "getent passwd {} | cut -d: -f6".format(user) ).run_command(host) user_home = user_home_output[host] fetch_args = "src={user_home}/.ssh/id_rsa.pub dest={key_tmp} flat=yes fail_on_missing=yes".format( user_home=user_home, key_tmp=os.path.join(working_directory, 'id_rsa.tmp') ) run_ansible_module(env, ansible_context, host, 'fetch', fetch_args, True, None, None)
def _run_ansible_module(self, host_pattern, module, module_args): return run_ansible_module( self.environment, self.ansible_context, host_pattern, module, module_args, )
def _run_migration(migration, ansible_context, check_mode): puts(colored.blue('Give ansible user access to couchdb files:')) user_args = "user=ansible groups=couchdb append=yes" run_ansible_module(migration.source_environment, ansible_context, 'couchdb2', 'user', user_args, True, None, False) file_args = "path=/opt/data/couchdb2 mode=0755" run_ansible_module(migration.source_environment, ansible_context, 'couchdb2', 'file', file_args, True, None, False) puts(colored.blue('Copy file lists to nodes:')) rsync_files_by_host = prepare_to_sync_files(migration, ansible_context) puts(colored.blue('Stop couch and reallocate shards')) with stop_couch(migration.all_environments, ansible_context, check_mode): sync_files_to_dest(migration, rsync_files_by_host, check_mode) return 0
def _run_ansible_module(self, host_pattern, module, module_args): return run_ansible_module( self.environment, self.ansible_context, host_pattern, module, module_args, True, None, False, )
def copy_scripts_to_target_host(target_host, script_root, environment, ansible_context): local_files_path = os.path.join(script_root, target_host) destination_path = os.path.join('/tmp', REMOTE_MIGRATION_ROOT) # remove destination path to ensure we're starting fresh file_args = "path={} state=absent".format(destination_path) run_ansible_module( environment, ansible_context, target_host, 'file', file_args, True, None, False ) # recursively copy all rsync file lists to destination copy_args = "src={src}/ dest={dest} mode={mode}".format( src=local_files_path, dest=destination_path, mode='0644' ) run_ansible_module( environment, ansible_context, target_host, 'copy', copy_args, True, None, False ) # make script executable file_args = "path={path} mode='0744'".format( path=os.path.join(destination_path, FILE_MIGRATION_RSYNC_SCRIPT) ) run_ansible_module( environment, ansible_context, target_host, 'file', file_args, True, None, False )
def prepare_to_sync_files(migration, ansible_context): rsync_files_by_host = generate_rsync_lists(migration) for host_ip in rsync_files_by_host: host_files_root = os.path.join(migration.rsync_files_path, host_ip) destination_path = os.path.join('/tmp', RSYNC_FILE_LIST_FOLDER_NAME) # remove destination path to ensure we're starting fresh file_args = "path={} state=absent".format(destination_path) run_ansible_module(migration.target_environment, ansible_context, host_ip, 'file', file_args, True, None, False) # recursively copy all rsync file lists to destination copy_args = "src={src}/ dest={dest} owner={owner} group={group} mode={mode}".format( src=host_files_root, dest=destination_path, owner='couchdb', # TODO: get from vars group='couchdb', mode='0644') run_ansible_module(migration.target_environment, ansible_context, host_ip, 'copy', copy_args, True, None, False) # make script executable file_args = "path={path} mode='0744'".format( path=os.path.join(destination_path, COUCHDB_RSYNC_SCRIPT)) run_ansible_module(migration.target_environment, ansible_context, host_ip, 'file', file_args, True, None, False) return rsync_files_by_host
def copy_scripts_to_target_host(target_host, script_root, environment, ansible_context): local_files_path = os.path.join(script_root, target_host) destination_path = os.path.join('/tmp', REMOTE_MIGRATION_ROOT) # remove destination path to ensure we're starting fresh file_args = "path={} state=absent".format(destination_path) run_ansible_module( environment, ansible_context, target_host, 'file', file_args, True, None, False ) # recursively copy all rsync file lists to destination copy_args = "src={src}/ dest={dest} mode={mode}".format( src=local_files_path, dest=destination_path, mode='0644' ) run_ansible_module( environment, ansible_context, target_host, 'copy', copy_args, True, None, False ) # make script executable file_args = "path={path} mode='0744'".format( path=os.path.join(destination_path, FILE_MIGRATION_RSYNC_SCRIPT) ) run_ansible_module( environment, ansible_context, target_host, 'file', file_args, True, None, False )
def _run_command(environment, ansible_context, command, become=False): return run_ansible_module(environment, ansible_context, HQ_PROCESSES_SCOPE, 'shell', command, become, None, False)
def _set_auth_key(env, host, user, ansible_context, working_directory, remove=False): state = 'absent' if remove else 'present' key_path = os.path.join(working_directory, 'id_rsa.tmp') args = "user={} state={} key={{{{ lookup('file', '{}') }}}}".format(user, state, key_path) run_ansible_module(env, ansible_context, host, 'authorized_key', args, True, None, None)
def _run_command(environment, ansible_context, command, become=False): return run_ansible_module( environment, ansible_context, ','.join(COMMCARE_INVENTORY_GROUPS), 'shell', command, become=become )
def _run_command(environment, ansible_context, command, become=False): return run_ansible_module( environment, ansible_context, HQ_PROCESSES_SCOPE, 'shell', command, become, None, False )