Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
Signed-off-by: Waleed Malik <ahmedwaleedmalik@gmail.com>
  • Loading branch information
ahmedwaleedmalik committed Mar 7, 2023
1 parent f066e05 commit 6290196
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 1,146 deletions.
7 changes: 4 additions & 3 deletions image-builder/windows/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ endif

COMMON_WINDOWS_VAR_FILES := packer/common/ansible-args-windows.json \
packer/common/common.json \
packer/common/cloudbase-init.json \
packer/common/goss-args.json
packer/common/cloudbase-init.json

PACKER_WINDOWS_NODE_FLAGS := $(foreach f,$(abspath $(COMMON_WINDOWS_VAR_FILES)),-var-file="$(f)" )

azure-deps:
hack/ensure-goss.sh
hack/ensure-ansible.sh
hack/ensure-ansible-windows.sh
hack/ensure-packer.sh

build-azure-image: azure-deps
packer validate $(PACKER_WINDOWS_NODE_FLAGS) -var-file="$(abspath packer/azure/azure-config.json)" -var-file="$(abspath packer/azure/$(distribution))" $(abspath packer/azure/packer-windows.json)
Expand Down
1 change: 0 additions & 1 deletion image-builder/windows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ This is an indirect clone of [image-builder-capi](https://github.com/kubernetes-
## Prerequisites

- [Packer](https://www.packer.io/intro/getting-started/install.html) version >= 1.6.0
- [Goss plugin for Packer](https://github.com/YaleUniversity/packer-provisioner-goss) version >= 1.2.0
- [Ansible](http://docs.ansible.com/ansible/latest/intro_installation.html) version >= 2.10.0

## Azure
Expand Down
3 changes: 3 additions & 0 deletions image-builder/windows/ansible/ansible_winrm.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
# https://www.packer.io/docs/provisioners/ansible.html#winrm-communicator
# https://www.packer.io/docs/builders/amazon/ebs#connecting-to-windows-instances-using-winrm

Write-Host "Changing PS execution policy to Unrestricted" -ForegroundColor Cyan
Set-ExecutionPolicy Unrestricted -Scope LocalMachine -Force -ErrorAction Ignore

# Don't set this before Set-ExecutionPolicy as it throws an error
$ErrorActionPreference = "stop"

Write-Host "Execution Policy configured" -ForegroundColor Cyan

# Remove HTTP listener
Remove-Item -Path WSMan:\Localhost\listener\listener* -Recurse

Expand Down
45 changes: 45 additions & 0 deletions image-builder/windows/hack/ensure-ansible-windows.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash

# Copyright 2020 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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.

set -o errexit
set -o nounset
set -o pipefail

[[ -n ${DEBUG:-} ]] && set -o xtrace

source hack/utils.sh

_version="0.4.2"

if [[ ${HOSTOS} == "darwin" ]]; then
echo "IMPORTANT: Winrm connection plugin for Ansible on MacOS causes connection issues."
echo "See https://docs.ansible.com/ansible/latest/user_guide/windows_winrm.html#what-is-winrm for more details."
echo "To fix the issue provide the enviroment variable 'no_proxy=*'"
echo "Example call to build Windows images on MacOS: 'no_proxy=* make build-<target>'"
fi

# Change directories to the parent directory of the one in which this
# script is located.
cd "$(dirname "${BASH_SOURCE[0]}")/.."

# Disable pip's version check and root user warning
export PIP_DISABLE_PIP_VERSION_CHECK=1 PIP_ROOT_USER_ACTION=ignore

if pip3 show pywinrm >/dev/null 2>&1; then exit 0; fi

ensure_py3
pip3 install --user "pywinrm==${_version}"
if ! pip3 show pywinrm ; then exit 1; fi
45 changes: 45 additions & 0 deletions image-builder/windows/hack/ensure-ansible.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash

# Copyright 2020 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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.

set -o errexit
set -o nounset
set -o pipefail

[[ -n ${DEBUG:-} ]] && set -o xtrace

source hack/utils.sh

_version="2.11.5"

# Change directories to the parent directory of the one in which this
# script is located.
cd "$(dirname "${BASH_SOURCE[0]}")/.."

# Disable pip's version check and root user warning
export PIP_DISABLE_PIP_VERSION_CHECK=1 PIP_ROOT_USER_ACTION=ignore

if ! command -v ansible >/dev/null 2>&1; then
ensure_py3
pip3 install --user "ansible-core==${_version}"
ensure_py3_bin ansible
ensure_py3_bin ansible-playbook
fi

ansible-galaxy collection install \
community.general \
ansible.posix \
'ansible.windows:>=1.7.0' \
community.windows
73 changes: 0 additions & 73 deletions image-builder/windows/hack/ensure-goss.sh

This file was deleted.

54 changes: 54 additions & 0 deletions image-builder/windows/hack/ensure-packer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash

# Copyright 2019 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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.

set -o errexit
set -o nounset
set -o pipefail

[[ -n ${DEBUG:-} ]] && set -o xtrace

_version="1.8.6"

# Change directories to the parent directory of the one in which this
# script is located.
cd "$(dirname "${BASH_SOURCE[0]}")/.."

source hack/utils.sh

if command -v packer >/dev/null 2>&1; then exit 0; fi

mkdir -p .local/bin && cd .local/bin

SED="sed"
if command -v gsed >/dev/null; then
SED="gsed"
fi
if ! (${SED} --version 2>&1 | grep -q GNU); then
echo "!!! GNU sed is required. If on OS X, use 'brew install gnu-sed'." >&2
exit 1
fi

_chkfile="packer_${_version}_SHA256SUMS"
_chk_url="https://releases.hashicorp.com/packer/${_version}/${_chkfile}"
_zipfile="packer_${_version}_${HOSTOS}_${HOSTARCH}.zip"
_zip_url="https://releases.hashicorp.com/packer/${_version}/${_zipfile}"
curl -SsLO "${_chk_url}"
curl -SsLO "${_zip_url}"
${SED} -i -n "/${HOSTOS}_${HOSTARCH}/p" "${_chkfile}"
checksum_sha256 "${_chkfile}"
unzip -o "${_zipfile}"
rm -f "${_chkfile}" "${_zipfile}"
echo "'packer' has been installed to $(pwd), make sure this directory is in your \$PATH"
30 changes: 0 additions & 30 deletions image-builder/windows/packer/azure/packer-windows.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,36 +82,6 @@
"restart_timeout": "10m",
"type": "windows-restart"
},
{
"arch": "{{user `goss_arch`}}",
"download_path": "{{user `goss_download_path`}}",
"format": "{{user `goss_format`}}",
"format_options": "{{user `goss_format_options`}}",
"goss_file": "{{user `goss_entry_file`}}",
"inspect": "{{user `goss_inspect_mode`}}",
"remote_folder": "{{user `goss_remote_folder`}}",
"remote_path": "{{user `goss_remote_path`}}",
"skip_install": "{{user `goss_skip_install`}}",
"target_os": "Windows",
"tests": [
"{{user `goss_tests_dir`}}"
],
"type": "goss",
"url": "{{user `goss_url`}}",
"use_sudo": false,
"vars_env": {
"GOSS_MAX_CONCURRENT": "1",
"GOSS_USE_ALPHA": "1"
},
"vars_file": "{{user `goss_vars_file`}}",
"vars_inline": {
"OS": "{{user `distribution` | lower}}",
"PROVIDER": "azure",
"distribution_version": "{{user `distribution_version`}}",
"ssh_source_url": "{{user `ssh_source_url`}}"
},
"version": "{{user `goss_version`}}"
},
{
"elevated_password": "{{.WinRMPassword}}",
"elevated_user": "packer",
Expand Down
1 change: 1 addition & 0 deletions image-builder/windows/packer/azure/windows-2022.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"image_offer": "WindowsServer",
"image_publisher": "MicrosoftWindowsServer",
"image_sku": "2022-Datacenter-Core-smalldisk",
"image_version": "latest",
"vm_size": "Standard_D4s_v3",
"windows_updates_kbs": ""
}
15 changes: 0 additions & 15 deletions image-builder/windows/packer/common/goss-args.json

This file was deleted.

Loading

0 comments on commit 6290196

Please sign in to comment.