def test_get_ebignore_list__includes_files_with_unicode_names( self, get_ebignore_location_mock, get_project_root_mock ): get_project_root_mock.return_value = os.getcwd() get_ebignore_location_mock.return_value = os.path.join(os.getcwd(), '.ebignore') open('哈哈', 'w').close() open('昨夜のコンサートは最高でした。', 'w').close() open('ändrar något i databasen', 'w').close() with open('.ebignore', 'wb') as file: ebignore_file_contents = """ 哈哈 昨夜のコンサートは最高でした。 ändrar\ något\ i\ databasen """ file.write(ebignore_file_contents.encode("UTF-8")) file.close() paths_to_ignore = fileoperations.get_ebignore_list() self.assertEqual( {'哈哈', '昨夜のコンサートは最高でした。', 'ändrar något i databasen', '.ebignore'}, paths_to_ignore )
def test_get_ebignore_list__includes_regular_files_in_the_ebignore_list( self, get_ebignore_location_mock, get_project_root_mock ): get_project_root_mock.return_value = os.getcwd() get_ebignore_location_mock.return_value = os.path.join(os.getcwd(), '.ebignore') os.mkdir('directory_1') open('file_1', 'w').close() open('directory_1/file_2', 'w').close() open('directory_1/file_3', 'w').close() with open('.ebignore', 'w') as file: ebignore_file_contents = """ # ignore regular top-level file file_1 # ignore regular inner-level file' directory_1/file_2 """.format(os.path.sep) file.write(ebignore_file_contents) file.close() paths_to_ignore = fileoperations.get_ebignore_list() self.assertEqual( {'file_1', 'directory_1{}file_2'.format(os.path.sep), '.ebignore'}, paths_to_ignore )
def test_get_ebignore_list__includes_filenames_with_exclamations_in_ignore_list( self, get_ebignore_location_mock, get_project_root_mock ): get_project_root_mock.return_value = os.getcwd() get_ebignore_location_mock.return_value = os.path.join( os.getcwd(), '.ebignore') open('!file_1', 'w').close() with open('.ebignore', 'w') as file: ebignore_file_contents = """ \!file_1 """ file.write(ebignore_file_contents) file.close() paths_to_ignore = fileoperations.get_ebignore_list() self.assertEqual( {'!file_1', '.ebignore'}, paths_to_ignore )
def test_get_ebignore_list__includes_dotted_files_in_the_ebignore_list( self, get_ebignore_location_mock, get_project_root_mock): get_project_root_mock.return_value = os.getcwd() get_ebignore_location_mock.return_value = os.path.join( os.getcwd(), '.ebignore') os.mkdir('directory_1') open('.gitignore', 'w').close() open('.dotted_file', 'w').close() open('directory_1{}.dotted_file'.format(os.path.sep), 'w').close() with open('.ebignore', 'w') as file: ebignore_file_contents = """ # ignore special dot file .gitignore # ignore dot file .dotted_file # ignore inner-level dot file' directory_1/.dotted_file """ file.write(ebignore_file_contents) file.close() paths_to_ignore = fileoperations.get_ebignore_list() self.assertEqual( { '.gitignore', '.dotted_file', 'directory_1{}.dotted_file'.format(os.path.sep), '.ebignore' }, set(paths_to_ignore))
def make_version_file_with_ebignore(version_label, dockerrun=None, docker_compose=None, ebext=None): """ Making zip file to upload for ElasticBeanstalk based on ebigore :param version_label: will be name of the created zip file * Including :param dockerrun: file as Dockerrun.aws.json * Including :param docker-compose: file as docker-compose.yml (for Amazon linux2) * Including :param exext: directory as .ebextensions/ :return: File path to created zip file (current directory). """ ebext = ebext or DOCKEREXT_NAME tempd = tempfile.mkdtemp() try: ignore_files = fileoperations.get_ebignore_list() # Dockerrun, docker-compose and ebextentions files are added to zip file later. ignore_files |= {DOCKERRUN_NAME, DOCKER_COMPOSE_NAME} ignore_ebext_files = set() for file in os.listdir(DOCKEREXT_NAME): ebext_file_path = os.path.join(DOCKEREXT_NAME, file) if os.path.isfile(ebext_file_path): ignore_ebext_files.add(ebext_file_path) ignore_files |= ignore_ebext_files zip_filename = version_label + ".zip" temp_zip_path = os.path.join(tempd, zip_filename) fileoperations.zip_up_project(temp_zip_path, ignore_list=ignore_files) shutil.copyfile(temp_zip_path, zip_filename) logger.info( f'Adding {DOCKERRUN_NAME}, {DOCKER_COMPOSE_NAME}, {DOCKEREXT_NAME} to archive.' ) with zipfile.ZipFile(zip_filename, 'a', allowZip64=True) as f: for file in os.listdir(ebext): source_ebext_file_path = os.path.join(ebext, file) target_ebext_file_path = os.path.join(DOCKEREXT_NAME, file) if os.path.isfile(source_ebext_file_path): f.write(source_ebext_file_path, arcname=target_ebext_file_path) if docker_compose: f.write(docker_compose, arcname=DOCKER_COMPOSE_NAME) if dockerrun: f.write(dockerrun, arcname=DOCKERRUN_NAME) else: dockerrun = dockerrun or DOCKERRUN_NAME f.write(dockerrun, arcname=DOCKERRUN_NAME) return zip_filename finally: shutil.rmtree(tempd)
def build_bundle(version_label): if not os.path.exists('.elasticbeanstalk'): os.mkdir('.elasticbeanstalk') file_path = fileoperations.get_zip_location('{}.zip'.format(version_label)) logging.info('Packaging application to %s', file_path) ignore_files = fileoperations.get_ebignore_list() fileoperations.io.log_info = lambda message: logging.debug(message) fileoperations.zip_up_project(file_path, ignore_list=ignore_files) return '.elasticbeanstalk/app_versions/{}.zip'.format(version_label)
def _zip_up_project(version_label, source_control, staged=False): file_name = version_label + '.zip' file_path = fileoperations.get_zip_location(file_name) if not fileoperations.file_exists(file_path): io.echo(strings['appversion.create'].replace('{version}', version_label)) ignore_files = fileoperations.get_ebignore_list() if ignore_files is None: source_control.do_zip(file_path, staged) else: io.log_info('Found .ebignore, using system zip.') fileoperations.zip_up_project(file_path, ignore_list=ignore_files) return file_name, file_path
def _zip_up_project(version_label, source_control, staged=False): # Create zip file file_name = version_label + '.zip' file_path = fileoperations.get_zip_location(file_name) # Check to see if file already exists from previous attempt if not fileoperations.file_exists(file_path): # If it doesn't already exist, create it io.echo(strings['appversion.create'].replace('{version}', version_label)) ignore_files = fileoperations.get_ebignore_list() if ignore_files is None: source_control.do_zip(file_path, staged) else: io.log_info('Found .ebignore, using system zip.') fileoperations.zip_up_project(file_path, ignore_list=ignore_files) return file_name, file_path
def test_get_ebignore_list__excludes_paths_escaped_with_exclamation_from_the_ebignore_list( self, get_ebignore_location_mock, get_project_root_mock ): get_project_root_mock.return_value = os.getcwd() get_ebignore_location_mock.return_value = os.path.join(os.getcwd(), '.ebignore') os.mkdir('directory_1') os.mkdir(os.path.join('directory_1', 'directory_2')) open('directory_1{0}file_1'.format(os.path.sep), 'w').close() open('directory_1{0}file_2'.format(os.path.sep), 'w').close() open('directory_1{0}directory_2{0}file_1'.format(os.path.sep), 'w').close() open('directory_1{0}directory_2{0}file_2'.format(os.path.sep), 'w').close() with open('.ebignore', 'w') as file: ebignore_file_contents = """ # ignore top-level directory and all its contents directory_1/* # exclude a directory inside the above !directory_1/directory_2 # ignore the contents of the inner directory directory_1/directory_2/* # exclude the file of inside the inner directory that you want to commit to VC !directory_1/directory_2/file_2 """ file.write(ebignore_file_contents) file.close() paths_to_ignore = fileoperations.get_ebignore_list() self.assertEqual( { 'directory_1{0}file_1'.format(os.path.sep), 'directory_1{0}file_2'.format(os.path.sep), 'directory_1{0}directory_2{0}file_1'.format(os.path.sep), '.ebignore' }, paths_to_ignore )
#!/usr/bin/env python """ Helper for creating a zip file for consumption by elasticbeanstalk. Runs similar code to `eb deploy` from ebcli, but will just create the zipfile. This allows us to be specific about the name and bind it to our branch & build number for later deployment. """ import logging import sys import ebcli.core.fileoperations as fileoperations logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') if len(sys.argv) != 2: print("Usage: {} FILENAME".format(sys.argv[0])) print("Ex: {} myapp-BRANCH-BUILDNUMBER.zip".format(sys.argv[0])) sys.exit(1) file_name = sys.argv[1] file_path = fileoperations.get_zip_location(file_name) logging.info('Packaging application to %s', file_path) ignore_files = fileoperations.get_ebignore_list() fileoperations.io.log_info = lambda message: logging.debug(message) fileoperations.zip_up_project(file_path, ignore_list=ignore_files)