Exemple #1
0
def _download_logs_for_all_instances(instance_id_list, logs_location):
    for instance_id, url in iteritems(instance_id_list):
        zip_location = utils.save_file_from_url(url, logs_location,
                                                instance_id + '.zip')
        instance_folder = os.path.join(logs_location, instance_id)
        fileoperations.unzip_folder(zip_location, instance_folder)
        fileoperations.delete_file(zip_location)
Exemple #2
0
def _download_logs_for_all_instances(instance_id_list, logs_location):
    for instance_id, url in iteritems(instance_id_list):
        zip_location = utils.save_file_from_url(
            url,
            logs_location,
            instance_id + '.zip'
        )
        instance_folder = os.path.join(logs_location, instance_id)
        fileoperations.unzip_folder(zip_location, instance_folder)
        fileoperations.delete_file(zip_location)
Exemple #3
0
    def test_zip_up_project(self, _validate_file_for_archive_mock):
        _validate_file_for_archive_mock.side_effect = lambda f: not f.endswith(
            '.sock')
        shutil.rmtree('home', ignore_errors=True)
        os.mkdir('src')
        os.mkdir(os.path.join('src', 'lib'))
        open(os.path.join('src', 'lib', 'app.py'), 'w').write('import os')
        open(os.path.join('src', 'lib', 'app.py~'), 'w').write('import os')
        open(os.path.join('src', 'lib', 'ignore-this-file.py'),
             'w').write('import os')
        open(os.path.join('src', 'lib', 'test.sock'),
             'w').write('mock socket file')

        os.symlink(os.path.join('src', 'lib', 'app.py'),
                   os.path.join('src', 'lib', 'app.py-copy'))

        os.mkdir(os.path.join('src', 'lib', 'api'))

        if sys.version_info > (3, 0):
            os.symlink(os.path.join('src', 'lib', 'api'),
                       os.path.join('src', 'lib', 'api-copy'),
                       target_is_directory=True)
        else:
            os.symlink(os.path.join('src', 'lib', 'api'),
                       os.path.join('src', 'lib', 'api-copy'))

        open(os.path.join('src', 'lib', 'api', 'api.py'),
             'w').write('import unittest')

        fileoperations.zip_up_project(
            'app.zip',
            ignore_list=[os.path.join('src', 'lib', 'ignore-this-file.py')])

        os.mkdir('tmp')
        fileoperations.unzip_folder('app.zip', 'tmp')

        self.assertTrue(
            os.path.exists(os.path.join('tmp', 'src', 'lib', 'app.py')))
        self.assertTrue(
            os.path.exists(os.path.join('tmp', 'src', 'lib', 'api')))
        self.assertTrue(
            os.path.exists(os.path.join('tmp', 'src', 'lib', 'app.py-copy')))
        self.assertTrue(
            os.path.exists(os.path.join('tmp', 'src', 'lib', 'api-copy')))
        self.assertFalse(
            os.path.exists(os.path.join('tmp', 'src', 'lib', 'app.py~')))
        self.assertFalse(
            os.path.exists(
                os.path.join('tmp', 'src', 'lib', 'ignore-this-file.py')))
        self.assertFalse(
            os.path.exists(os.path.join('tmp', 'src', 'lib', 'test.sock')))
Exemple #4
0
    def test_zip_up_project(self):
        shutil.rmtree('home', ignore_errors=True)
        os.mkdir('src')
        os.mkdir(os.path.join('src', 'lib'))
        open(os.path.join('src', 'lib', 'app.py'), 'w').write('import os')
        open(os.path.join('src', 'lib', 'app.py~'), 'w').write('import os')
        open(os.path.join('src', 'lib', 'ignore-this-file.py'),
             'w').write('import os')

        os.symlink(os.path.join('src', 'lib', 'app.py'),
                   os.path.join('src', 'lib', 'app.py-copy'))

        os.mkdir(os.path.join('src', 'lib', 'api'))

        if sys.version_info > (3, 0):
            os.symlink(os.path.join('src', 'lib', 'api'),
                       os.path.join('src', 'lib', 'api-copy'),
                       target_is_directory=True)
        else:
            os.symlink(os.path.join('src', 'lib', 'api'),
                       os.path.join('src', 'lib', 'api-copy'))

        open(os.path.join('src', 'lib', 'api', 'api.py'),
             'w').write('import unittest')

        fileoperations.zip_up_project(
            'app.zip',
            ignore_list=[os.path.join('src', 'lib', 'ignore-this-file.py')])

        os.mkdir('tmp')
        fileoperations.unzip_folder('app.zip', 'tmp')

        self.assertTrue(
            os.path.exists(os.path.join('tmp', 'src', 'lib', 'app.py')))
        self.assertTrue(
            os.path.exists(os.path.join('tmp', 'src', 'lib', 'api')))
        self.assertTrue(
            os.path.exists(os.path.join('tmp', 'src', 'lib', 'app.py-copy')))
        self.assertTrue(
            os.path.exists(os.path.join('tmp', 'src', 'lib', 'api-copy')))
        self.assertFalse(
            os.path.exists(os.path.join('tmp', 'src', 'lib', 'app.py~')))
        self.assertFalse(
            os.path.exists(
                os.path.join('tmp', 'src', 'lib', 'ignore-this-file.py')))
Exemple #5
0
def download_source_bundle(app_name, env_name):
    env = elasticbeanstalk.get_environment(app_name=app_name,
                                           env_name=env_name)
    if env.version_label and env.version_label != 'Sample Application':
        app_version = elasticbeanstalk.get_application_versions(
            app_name,
            version_labels=[env.version_label])['ApplicationVersions'][0]

        source_bundle = app_version['SourceBundle']
        bucket_name = source_bundle['S3Bucket']
        key_name = source_bundle['S3Key']
        io.echo('Downloading application version...')
        data = s3.get_object(bucket_name, key_name)
        filename = get_filename(key_name)
    else:
        # sample app
        template = cloudformation.get_template('awseb-' + env.id + '-stack')
        try:
            url = template['TemplateBody']['Parameters']['AppSource'][
                'Default']
        except KeyError:
            raise NotFoundError('Can not find app source for environment')
        utils.get_data_from_url(url)
        io.echo('Downloading application version...')
        data = utils.get_data_from_url(url, timeout=30)
        filename = 'sample.zip'

    fileoperations.make_eb_dir('downloads/')
    location = fileoperations.get_eb_file_full_location('downloads/' +
                                                        filename)
    fileoperations.write_to_data_file(location, data)
    io.echo('Application version downloaded to:', location)

    cwd = os.getcwd()
    try:
        fileoperations._traverse_to_project_root()
        if heuristics.directory_is_empty():
            # If we dont have any project code, unzip as current project
            io.echo('Unzipping application version as project files.')
            fileoperations.unzip_folder(location, os.getcwd())
            io.echo('Done.')
    finally:
        os.chdir(cwd)
Exemple #6
0
def get_logs(env_name, info_type, do_zip=False, instance_id=None):
    # Get logs
    result = elasticbeanstalk.retrieve_environment_info(env_name, info_type)
    """
        Results are ordered with latest last, we just want the latest
    """
    log_list = {}
    for log in result['EnvironmentInfo']:
        i_id = log['Ec2InstanceId']
        url = log['Message']
        log_list[i_id] = url

    if instance_id:
        try:
            log_list = {instance_id: log_list[instance_id]}
        except KeyError:
            raise NotFoundError(strings['beanstalk-logs.badinstance'].replace(
                '{instance_id}', instance_id))

    if info_type == 'bundle':
        # save file, unzip, place in logs directory
        logs_folder_name = datetime.now().strftime("%y%m%d_%H%M%S")
        logs_location = fileoperations.get_logs_location(logs_folder_name)
        #get logs for each instance
        for i_id, url in iteritems(log_list):
            zip_location = utils.save_file_from_url(url, logs_location,
                                                    i_id + '.zip')
            instance_folder = os.path.join(logs_location, i_id)
            fileoperations.unzip_folder(zip_location, instance_folder)
            fileoperations.delete_file(zip_location)

        fileoperations.set_user_only_permissions(logs_location)
        if do_zip:
            fileoperations.zip_up_folder(logs_location, logs_location + '.zip')
            fileoperations.delete_directory(logs_location)

            logs_location += '.zip'
            fileoperations.set_user_only_permissions(logs_location)
            io.echo(strings['logs.location'].replace('{location}',
                                                     logs_location))
        else:
            io.echo(strings['logs.location'].replace('{location}',
                                                     logs_location))
            # create symlink to logs/latest
            latest_location = fileoperations.get_logs_location('latest')
            try:
                os.unlink(latest_location)
            except OSError:
                # doesn't exist. Ignore
                pass
            try:
                os.symlink(logs_location, latest_location)
                io.echo('Updated symlink at', latest_location)
            except OSError:
                #Oh well.. we tried.
                ## Probably on windows, or logs/latest is not a symlink
                pass

    else:
        # print logs
        data = []
        for i_id, url in iteritems(log_list):
            data.append('============= ' + str(i_id) + ' ==============')
            log_result = utils.get_data_from_url(url)
            data.append(utils.decode_bytes(log_result))
        io.echo_with_pager(os.linesep.join(data))