Skip to content

Commit

Permalink
add packer logging
Browse files Browse the repository at this point in the history
  • Loading branch information
colbylwilliams committed Oct 18, 2022
1 parent 4f94bb5 commit 88c3d35
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Microsoft Azure CLI Custom Image Helper 'az' Extension adds useful "utilities" f
To install the Azure CLI Custom Image Helper extension, simply run the following command:

```sh
az extension add --source https://github.com/colbylwilliams/az-bake/releases/latest/download/bake-0.0.28-py3-none-any.whl -y
az extension add --source https://github.com/colbylwilliams/az-bake/releases/latest/download/bake-0.0.29-py3-none-any.whl -y
```

### Update
Expand Down
4 changes: 4 additions & 0 deletions bake/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
Release History
===============

0.0.29
++++++
+ Create new directory for the builer logs

0.0.28
++++++
+ Add another windows restart to packer build file
Expand Down
27 changes: 14 additions & 13 deletions bake/azext_bake/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,37 @@
from xml.etree.ElementTree import Element, tostring

import yaml
from azure.cli.core.azclierror import (ClientRequestError, FileOperationError,
MutuallyExclusiveArgumentError,
ResourceNotFoundError, ValidationError)
from azure.cli.core.util import should_disable_connection_verify
from azure.cli.core.azclierror import FileOperationError, ValidationError
from knack.log import get_logger as knack_get_logger

from ._constants import (AZ_BAKE_IMAGE_BUILDER, AZ_BAKE_REPO_VOLUME,
AZ_BAKE_STORAGE_VOLUME)

timestamp = datetime.now(timezone.utc).strftime('%Y%m%d%H%M%S')

IN_BUILDER = os.environ.get(AZ_BAKE_IMAGE_BUILDER)
IN_BUILDER = True if IN_BUILDER else False

def get_logger(name):
'''Get the logger for the extension'''
in_builder = os.environ.get(AZ_BAKE_IMAGE_BUILDER)
in_builder = True if in_builder else False
REPO_DIR = Path(AZ_BAKE_REPO_VOLUME) if IN_BUILDER else Path(__file__).resolve().parent.parent.parent
STORAGE_DIR = Path(AZ_BAKE_STORAGE_VOLUME) if IN_BUILDER else REPO_DIR / '.local' / 'storage'

OUTPUT_DIR = STORAGE_DIR / timestamp

if IN_BUILDER:
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

repo = Path(AZ_BAKE_REPO_VOLUME) if in_builder else Path(__file__).resolve().parent.parent.parent
storage = Path(AZ_BAKE_STORAGE_VOLUME) if in_builder else repo / '.local' / 'storage'

def get_logger(name):
'''Get the logger for the extension'''
_logger = knack_get_logger(name)

if in_builder and os.path.isdir(storage):
if IN_BUILDER and os.path.isdir(STORAGE_DIR):
import logging
log_file = storage / f'log_{timestamp}.txt'
log_file = OUTPUT_DIR / f'builder.txt'
formatter = logging.Formatter('{asctime} [{name:^28}] {levelname:<8}: {message}', datefmt='%m/%d/%Y %I:%M:%S %p', style='{',)
fh = logging.FileHandler(log_file)
fh.setLevel(level=_logger.level)
fh.setFormatter(formatter)

_logger.addHandler(fh)

return _logger
Expand Down
41 changes: 19 additions & 22 deletions bake/azext_bake/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,28 @@
from pathlib import Path
from re import match

from azure.cli.core.azclierror import (ArgumentUsageError, FileOperationError,
from azure.cli.core.azclierror import (ArgumentUsageError,
InvalidArgumentValueError,
MutuallyExclusiveArgumentError,
RequiredArgumentMissingError,
ValidationError)
from azure.cli.core.commands.validators import validate_tags
from azure.cli.core.extension import get_extension
from azure.cli.core.util import is_guid
from azure.mgmt.core.tools import (is_valid_resource_id, parse_resource_id,
resource_id)
from azure.mgmt.core.tools import is_valid_resource_id, parse_resource_id

from ._completers import get_default_location_from_sandbox_resource_group
from ._constants import (AZ_BAKE_BUILD_IMAGE_NAME, AZ_BAKE_IMAGE_BUILDER,
AZ_BAKE_IMAGE_BUILDER_VERSION, AZ_BAKE_REPO_VOLUME,
AZ_BAKE_STORAGE_VOLUME, BAKE_PROPERTIES,
GALLERY_PROPERTIES, IMAGE_PROPERTIES, KEY_ALLOWED,
KEY_REQUIRED, SANDBOX_PROPERTIES, tag_key)
AZ_BAKE_IMAGE_BUILDER_VERSION, BAKE_PROPERTIES,
IMAGE_PROPERTIES, KEY_ALLOWED, KEY_REQUIRED,
SANDBOX_PROPERTIES, tag_key)
from ._github import (get_github_latest_release_version,
github_release_version_exists)
from ._packer import check_packer_install
from ._repos import get_repo, is_ci, parse_repo_url
from ._sandbox import get_sandbox_from_group
from ._utils import get_logger, get_yaml_file_contents, get_yaml_file_path
from ._utils import (IN_BUILDER, REPO_DIR, STORAGE_DIR, get_logger,
get_yaml_file_contents, get_yaml_file_path)

logger = get_logger(__name__)

Expand Down Expand Up @@ -98,37 +97,35 @@ def process_bake_repo_validate_namespace(cmd, ns):
def builder_validator(cmd, ns):
check_packer_install(raise_error=True)

in_builder = os.environ.get(AZ_BAKE_IMAGE_BUILDER)
in_builder = True if in_builder else False
ns.in_builder = in_builder
ns.in_builder = IN_BUILDER

builder_version = os.environ.get(AZ_BAKE_IMAGE_BUILDER_VERSION, 'unknown') if in_builder else 'local'
builder_version = os.environ.get(AZ_BAKE_IMAGE_BUILDER_VERSION, 'unknown') if IN_BUILDER else 'local'

logger.info(f'{AZ_BAKE_IMAGE_BUILDER}: {in_builder}')
logger.info(f'{AZ_BAKE_IMAGE_BUILDER}: {IN_BUILDER}')
logger.info(f'{AZ_BAKE_IMAGE_BUILDER_VERSION}: {builder_version}')

if not in_builder:
if not IN_BUILDER:
logger.warning('WARNING: Running outside of the builder container. This should only be done during testing.')

# if not in_builder:
# raise CLIError('Not in builder')

repo = Path(AZ_BAKE_REPO_VOLUME) if in_builder else Path(__file__).resolve().parent.parent.parent
storage = Path(AZ_BAKE_STORAGE_VOLUME) if in_builder else repo / '.local' / 'storage'
# repo = REPO_DIR
# storage = STORAGE_DIR

_validate_dir_path(repo, 'repo')
_validate_dir_path(storage, 'storage')
_validate_dir_path(REPO_DIR, 'repo')
_validate_dir_path(STORAGE_DIR, 'storage')

ns.repo = repo
ns.storage = storage
ns.repo = REPO_DIR
ns.storage = STORAGE_DIR

# check for required environment variables
for env in [AZ_BAKE_BUILD_IMAGE_NAME]:
if not os.environ.get(env, False):
raise ValidationError(f'Missing environment variable: {env}')

image_name = os.environ[AZ_BAKE_BUILD_IMAGE_NAME]
image_path = repo / 'images' / image_name
image_path = REPO_DIR / 'images' / image_name

_validate_dir_path(image_path, image_name)

Expand All @@ -140,7 +137,7 @@ def builder_validator(cmd, ns):

logger.info(f'Build suffix: {ns.suffix}')

bake_yaml = get_yaml_file_path(repo, 'bake', required=True)
bake_yaml = get_yaml_file_path(REPO_DIR, 'bake', required=True)

bake_obj = bake_yaml_content_validator(cmd, ns, bake_yaml)

Expand Down
11 changes: 10 additions & 1 deletion bake/azext_bake/templates/packer/build.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ build {
# So we enable AutoAdminLogon and use packer's windows-restart provisioner to get the system into a good state to allow scheduled tasks to run.
provisioner "powershell" {
inline = [
"Write-Host 'Enabling AutoAdminLogon to allow packers scheduled task created by elevated_user to run...'",
"Set-ItemProperty 'HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon' -Name AutoAdminLogon -Value 1 -type String -ErrorAction Stop",
"Set-ItemProperty 'HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon' -Name DefaultUsername -Value ${build.User} -type String -ErrorAction Stop",
"Set-ItemProperty 'HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon' -Name DefaultPassword -Value ${build.Password} -type String -ErrorAction Stop"
Expand All @@ -92,6 +93,7 @@ build {
# Disable Auto-Logon that was enabled above
provisioner "powershell" {
inline = [
"Write-Host 'Disabling AutoAdminLogon...'",
"Remove-ItemProperty 'HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon' -Name AutoAdminLogon -ErrorAction Stop",
"Remove-ItemProperty 'HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon' -Name DefaultUserName -ErrorAction Stop",
"Remove-ItemProperty 'HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon' -Name DefaultPassword -ErrorAction Stop",
Expand All @@ -102,13 +104,20 @@ build {
provisioner "powershell" {
inline = [
# Generalize the image
"Write-Host '>>> Waiting for GA Service (RdAgent) to start ...'",
"while ((Get-Service RdAgent -ErrorAction SilentlyContinue) -and ((Get-Service RdAgent).Status -ne 'Running')) { Start-Sleep -s 5 }",
"Write-Host '>>> Waiting for GA Service (WindowsAzureTelemetryService) to start ...'",
"while ((Get-Service WindowsAzureTelemetryService -ErrorAction SilentlyContinue) -and ((Get-Service WindowsAzureTelemetryService).Status -ne 'Running')) { Start-Sleep -s 5 }",
"Write-Host '>>> Waiting for GA Service (WindowsAzureGuestAgent) to start ...'",
"while ((Get-Service WindowsAzureGuestAgent -ErrorAction SilentlyContinue) -and ((Get-Service WindowsAzureGuestAgent).Status -ne 'Running')) { Start-Sleep -s 5 }",
"Write-Host '>>> Sysprepping VM ...'",
"Remove-Item $Env:SystemRoot\\system32\\Sysprep\\unattend.xml -Force -ErrorAction SilentlyContinue",
# https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/sysprep-command-line-options?view=windows-11
"& $Env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /mode:vm /generalize /quiet /quit",
"while ($true) { $imageState = (Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State).ImageState; Write-Output $imageState; if ($imageState -eq 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { break }; Start-Sleep -s 5 }"
# "while ($true) { $imageState = (Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State).ImageState; Write-Output $imageState; if ($imageState -eq 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { break }; Start-Sleep -s 5 }",
"$imageStateCompleteCount = 0; while ($true) { if ($imageStateCompleteCount -gt 12) { Write-Host '===> SYSPREP ACTLOG'; Get-Content -Path 'C:\\windows\\system32\\sysprep\\panther\\setupact.log' -ErrorAction SilentlyContinue; Write-Host '===> SYSPREP ERRLOG'; Get-Content -Path 'C:\\windows\\system32\\sysprep\\panther\\setuperr.log' -ErrorAction SilentlyContinue; exit 1 }; $imageState = (Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State).ImageState; Write-Output $imageState; if ($imageState -eq 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { break }; if ($imageState -eq 'IMAGE_STATE_COMPLETE') { $imageStateCompleteCount += 1 }; Start-Sleep -s 5 }",
"Write-Host '>>> Sysprep complete ...'",
"Write-Host '>>> Shutting down VM ...'"
]
}
}
2 changes: 1 addition & 1 deletion bake/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
logger.warn("Wheel is not available, disabling bdist_wheel hook")

# Must match a HISTORY.rst entry.
VERSION = '0.0.28'
VERSION = '0.0.29'

# The full list of classifiers is available at
# https://pypi.python.org/pypi?%3Aaction=list_classifiers
Expand Down
2 changes: 1 addition & 1 deletion builder/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ LABEL maintainer="Microsoft" \
RUN apk add --no-cache packer --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community

# install az-bake
RUN az extension add --source https://github.com/colbylwilliams/az-bake/releases/latest/download/bake-0.0.28-py3-none-any.whl -y
RUN az extension add --source https://github.com/colbylwilliams/az-bake/releases/latest/download/bake-0.0.29-py3-none-any.whl -y

# Terminate container on stop
STOPSIGNAL SIGTERM
Expand Down

0 comments on commit 88c3d35

Please sign in to comment.