Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified Script Warning #1809

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions modules/scripts/startup-script/files/running-script-warning.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh
# Copyright 2023 Google LLC
#
# 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.

WARNING=$(
cat <<EOF
** NOTICE **: System services may not be running until startup scripts complete.
The output of the command below will end with "Finished Google Compute Engine
Startup Scripts." when they are complete. Please review the output for any
errors which may indicate that services are unhealthy.

sudo journalctl -b 0 -u google-startup-scripts.service
EOF
)

echo
echo "${WARNING}"
echo
41 changes: 41 additions & 0 deletions modules/scripts/startup-script/files/startup-script-stdlib-head.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ readonly E_RUN_OR_DIE=5
readonly E_MISSING_MANDATORY_ARG=9
readonly E_UNKNOWN_ARG=10

SUCCESS_MESSAGE=$(
cat <<-EOF
** NOTICE **: The VM startup scripts have finished running successfully.
Systems are configured and running.
EOF
)
readonly SUCCESS_MESSAGE
ERROR_MESSAGE=$(
cat <<-EOF
** ERROR **: The VM startup scripts have finished running, but produced an error.
cdunbar13 marked this conversation as resolved.
Show resolved Hide resolved
Systems may be in an unhealthy state.
EOF
)
readonly ERROR_MESSAGE
WARNING_MESSAGE=$(
cat <<-EOF
** WARNING **: The VM startup scripts for this machine have started.
Systems may not be configured or running.
EOF
)
readonly WARNING_MESSAGE

stdlib::debug() {
[[ -z ${DEBUG:-} ]] && return 0
local ds msg
Expand Down Expand Up @@ -57,6 +79,25 @@ stdlib::error() {
echo -e "${RED}${ds}Error [$$]: ${msg}${NC}" >&2
}

stdlib::announce_runners_start() {
if [ -z "$recursive_proc" ]; then
wall -n "$WARNING_MESSAGE"
fi
export recursive_proc=$((${recursive_proc:=0} + 1))
}

stdlib::announce_runners_end() {
exit_code=$1
export recursive_proc=$((${recursive_proc:=0} - 1))
if [ "$recursive_proc" -le "0" ]; then
if [ "$exit_code" -ne "0" ]; then
wall -n "$ERROR_MESSAGE"
else
wall -n "$SUCCESS_MESSAGE"
fi
fi
}

# The main initialization function of this library. This should be kept to the
# minimum amount of work required for all functions to operate cleanly.
stdlib::init() {
Expand Down
11 changes: 9 additions & 2 deletions modules/scripts/startup-script/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ locals {
destination = "install_cloud_ops_agent_automatic.sh"
}] : []

warnings = [
{
type = "data"
content = file("${path.module}/files/running-script-warning.sh")
destination = "/etc/profile.d/99-running-script-warning.sh"
}
]

configure_ssh = length(var.configure_ssh_host_patterns) > 0
host_args = {
host_name_prefix = var.configure_ssh_host_patterns
Expand Down Expand Up @@ -57,7 +65,6 @@ locals {
}
] : []


has_ansible_runners = anytrue([for r in var.runners : r.type == "ansible-local"]) || local.configure_ssh
install_ansible = var.install_ansible == null ? local.has_ansible_runners : var.install_ansible
ansible_installer = local.install_ansible ? [{
Expand All @@ -67,7 +74,7 @@ locals {
args = var.ansible_virtualenv_path
}] : []

runners = concat(local.ops_agent_installer, local.ansible_installer, local.configure_ssh_runners, var.runners)
runners = concat(local.warnings, local.ops_agent_installer, local.ansible_installer, local.configure_ssh_runners, var.runners)

bucket_regex = "^gs://([^/]*)/*(.*)"
gcs_bucket_path_trimmed = var.gcs_bucket_path == null ? null : trimsuffix(var.gcs_bucket_path, "/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ stdlib::runner() {
stdlib::info "=== $object finished with exit_code=$exit_code ==="
if [ "$exit_code" -ne "0" ] ; then
stdlib::error "=== execution of $object failed, exiting ==="
stdlib::announce_runners_end "$exit_code"
exit $exit_code
fi
}
Expand All @@ -46,10 +47,12 @@ stdlib::load_runners(){
tmpdir="$(mktemp -d)"

stdlib::debug "=== BEGIN Running runners ==="
stdlib::announce_runners_start

%{for r in runners ~}
stdlib::runner "${r.type}" "${r.object}" "${r.destination}" $${tmpdir} "${r.args}"
%{endfor ~}

stdlib::announce_runners_end "0"
stdlib::debug "=== END Running runners ==="
}