<commit_before># Copyright 2015 Bracket Computing, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# https://github.com/brkt/brkt-sdk-java/blob/master/LICENSE
#
# or in the "license" file accompanying this file. This file 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.

"""
Create a bracket solo metavisor/guest AMI
complete with a encrypted root volume for a given guest AMI

Basic outline:
    create a guest AMI with the right permissions on root vol snapshot
    launch a metavisor AMI with
        *) unencrypted guest root volume as /dev/sda4
        *) (raw) guest root volume (2x size) as /dev/sda5
    wait for the metavisor to launch
        *) dd from /dev/sda4 to /dev/sda5 creating encrypted root for guest
    stop metavisor
    create new AMI from metavisor instance
        *) include ephemeral drives

At that point a new AMI contains the metavisor + encrypted guest root volume.
We should be able to auto-chain load the guest once this AMI is launched

Environment setup:
Either setup environment variables for:
export AWS_ACCESS_KEY_ID=XXXXXXX
export AWS_SECRET_KEY=XXXXXXX

or

export BOTO_CONFIG ~/.ec2/boto_config
------------
boto_config
------------
[Credentials]
aws_access_key_id = XXXXXXXXXXXXX
aws_secret_access_key = <KEY>
------------
"""
from __future__ import print_function

import argparse
import boto
import boto.ec2
import boto.vpc
import logging
import os
import re
import requests
import string
import sys
import time
import uuid
import warnings

from boto.exception import EC2ResponseError
from boto.ec2.blockdevicemapping import (
    BlockDeviceMapping,
    BlockDeviceType,
    EBSBlockDeviceType
)

from brkt_cli import service
from brkt_cli.util import Deadline, make_nonce

# End user-visible terminology.  These are resource names and descriptions
# that the user will see in his or her EC2 console.

# Snapshotter instance names.
NAME_SNAPSHOT_CREATOR = 'Bracket root snapshot creator'
DESCRIPTION_SNAPSHOT_CREATOR = \
    'Used for creating a snapshot of the root volume from %(image_id)s'

# Security group names
NAME_ENCRYPTOR_SECURITY_GROUP = 'Bracket Encryptor %(nonce)s'
DESCRIPTION_ENCRYPTOR_SECURITY_GROUP = (
    "Allows access to the encryptor's status server.")

# Encryptor instance names.
NAME_ENCRYPTOR = 'Bracket volume encryptor'
DESCRIPTION_ENCRYPTOR = \
    'Copies the root snapshot from %(image_id)s to a new encrypted volume'

# Snapshots names.
NAME_ORIGINAL_SNAPSHOT = 'Bracket encryptor original volume'
DESCRIPTION_ORIGINAL_SNAPSHOT = \
    'Original unencrypted root volume from %(image_id)s'
NAME_ENCRYPTED_ROOT_SNAPSHOT = 'Bracket encrypted root volume'
NAME_METAVISOR_ROOT_SNAPSHOT = 'Bracket system root'
NAME_METAVISOR_GRUB_SNAPSHOT = 'Bracket system GRUB'
NAME_METAVISOR_LOG_SNAPSHOT = 'Bracket system log'
DESCRIPTION_SNAPSHOT = 'Based on %(image_id)s'

# Tag names.
TAG_ENCRYPTOR = 'BrktEncryptor'
TAG_ENCRYPTOR_SESSION_ID = 'BrktEncryptorSessionID'
TAG_ENCRYPTOR_AMI = 'BrktEncryptorAMI'
TAG_DESCRIPTION = 'Description'<commit_msg>Fix security group description<commit_after># Copyright 2015 Bracket Computing, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# https://github.com/brkt/brkt-sdk-java/blob/master/LICENSE
#
# or in the "license" file accompanying this file. This file 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.

"""
Create a bracket solo metavisor/guest AMI
complete with a encrypted root volume for a given guest AMI

Basic outline:
    create a guest AMI with the right permissions on root vol snapshot
    launch a metavisor AMI with
        *) unencrypted guest root volume as /dev/sda4
        *) (raw) guest root volume (2x size) as /dev/sda5
    wait for the metavisor to launch
        *) dd from /dev/sda4 to /dev/sda5 creating encrypted root for guest
    stop metavisor
    create new AMI from metavisor instance
        *) include ephemeral drives

At that point a new AMI contains the metavisor + encrypted guest root volume.
We should be able to auto-chain load the guest once this AMI is launched

Environment setup:
Either setup environment variables for:
export AWS_ACCESS_KEY_ID=XXXXXXX
export AWS_SECRET_KEY=XXXXXXX

or

export BOTO_CONFIG ~/.ec2/boto_config
------------
boto_config
------------
[Credentials]
aws_access_key_id = XXXXXXXXXXXXX
aws_secret_access_key = <KEY>
------------
"""
from __future__ import print_function

import argparse
import boto
import boto.ec2
import boto.vpc
import logging
import os
import re
import requests
import string
import sys
import time
import uuid
import warnings

from boto.exception import EC2ResponseError
from boto.ec2.blockdevicemapping import (
    BlockDeviceMapping,
    BlockDeviceType,
    EBSBlockDeviceType
)

from brkt_cli import service
from brkt_cli.util import Deadline, make_nonce

# End user-visible terminology.  These are resource names and descriptions
# that the user will see in his or her EC2 console.

# Snapshotter instance names.
NAME_SNAPSHOT_CREATOR = 'Bracket root snapshot creator'
DESCRIPTION_SNAPSHOT_CREATOR = \
    'Used for creating a snapshot of the root volume from %(image_id)s'

# Security group names
NAME_ENCRYPTOR_SECURITY_GROUP = 'Bracket Encryptor %(nonce)s'
DESCRIPTION_ENCRYPTOR_SECURITY_GROUP = (
    "Allows access to the encryption service.")

# Encryptor instance names.
NAME_ENCRYPTOR = 'Bracket volume encryptor'
DESCRIPTION_ENCRYPTOR = \
    'Copies the root snapshot from %(image_id)s to a new encrypted volume'

# Snapshots names.
NAME_ORIGINAL_SNAPSHOT = 'Bracket encryptor original volume'
DESCRIPTION_ORIGINAL_SNAPSHOT = \
    'Original unencrypted root volume from %(image_id)s'
NAME_ENCRYPTED_ROOT_SNAPSHOT = 'Bracket encrypted root volume'
NAME_METAVISOR_ROOT_SNAPSHOT = 'Bracket system root'
NAME_METAVISOR_GRUB_SNAPSHOT = 'Bracket system GRUB'
NAME_METAVISOR_LOG_SNAPSHOT = 'Bracket system log'
DESCRIPTION_SNAPSHOT = 'Based on %(image_id)s'

# Tag names.
TAG_ENCRYPTOR = 'BrktEncryptor'
TAG_ENCRYPTOR_SESSION_ID = 'BrktEncryptorSessionID'
TAG_ENCRYPTOR_AMI = 'BrktEncryptorAMI'
TAG_DESCRIPTION = 'Description'
{'fraction_non_alphanumeric': 0.06751497005988023, 'fraction_numerical': 0.004790419161676647, 'mean_word_length': 4.480721903199344, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5589786', 'n_tokens_mistral': 2111, 'n_tokens_neox': 2054, 'n_words': 749}