Skip to content

Commit

Permalink
Address GoogleCloudPlatform#2120: fail on bad state and succeed on re…
Browse files Browse the repository at this point in the history
…install
  • Loading branch information
nick-stroud committed Jan 12, 2024
1 parent 3543ffa commit 854601e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 16 additions & 4 deletions modules/scripts/startup-script/files/install_monitoring_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# 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 -e -o pipefail

LEGACY_MONITORING_PACKAGE='stackdriver-agent'
LEGACY_MONITORING_SCRIPT_URL='https://dl.google.com/cloudagents/add-monitoring-agent-repo.sh'
Expand All @@ -21,7 +22,7 @@ LEGACY_LOGGING_SCRIPT_URL='https://dl.google.com/cloudagents/add-logging-agent-r
OPSAGENT_PACKAGE='google-cloud-ops-agent'
OPSAGENT_SCRIPT_URL='https://dl.google.com/cloudagents/add-google-cloud-ops-agent-repo.sh'

install_legacy="${1:-true}"
ops_or_legacy="${1:-legacy}"

fail() {
echo >&2 "[$(date +'%Y-%m-%dT%H:%M:%S%z')] $*"
Expand Down Expand Up @@ -113,11 +114,22 @@ main() {
fail "Unsupported platform."
fi

if is_legacy_installed || is_opsagent_installed; then
fail "Legacy (stackdriver) or Ops Agent is already installed."
# Handle cases that agent is already installed
if [[ -z "$(is_legacy_monitoring_installed)" && -n $(is_legacy_logging_installed) ]] ||
[[ -n "$(is_legacy_monitoring_installed)" && -z $(is_legacy_logging_installed) ]]; then
fail "Bad state: legacy agent is partially installed"
elif [[ "${ops_or_legacy}" == "legacy" ]] && is_legacy_installed; then
echo "Legacy agent is already installed"
exit 0
elif [[ "${ops_or_legacy}" != "legacy" ]] && is_opsagent_installed; then
echo "Ops agent is already installed"
exit 0
elif is_legacy_installed || is_opsagent_installed; then
fail "Agent is already installed but does not match requested agent of ${ops_or_legacy}"
fi

if [[ "${install_legacy}" == true ]]; then
# install agent
if [[ "${ops_or_legacy}" == "legacy" ]]; then
echo "Installing legacy monitoring agent (stackdriver)"
install_stackdriver_agent
else
Expand Down
2 changes: 1 addition & 1 deletion modules/scripts/startup-script/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ locals {
type = "shell"
source = "${path.module}/files/install_monitoring_agent.sh"
destination = "install_monitoring_agent_automatic.sh"
args = var.install_cloud_ops_agent ? "false" : "true" # install legacy (stackdriver)
args = var.install_cloud_ops_agent ? "ops" : "legacy" # install legacy (stackdriver)
}] :
[]
)
Expand Down

0 comments on commit 854601e

Please sign in to comment.