Skip to content

Commit

Permalink
Start in host CWD, autoconfigure ATMOS_BASE_PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuru committed Jan 21, 2022
1 parent 33b2b44 commit 1149442
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 21 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

<!-- markdownlint-disable -->
# Geodesic [![Build Status](https://github.com/cloudposse/geodesic/workflows/docker/badge.svg)](https://github.com/cloudposse/geodesic/actions?query=workflow%3Adocker) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fcloudposse%2Fgeodesic.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fcloudposse%2Fgeodesic?ref=badge_shield) [![Latest Release](https://img.shields.io/github/release/cloudposse/geodesic.svg)](https://github.com/cloudposse/geodesic/releases/latest) [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com) [![Slack Archive](https://img.shields.io/badge/slack-archive-blue.svg)](https://archive.sweetops.com/geodesic)
<!-- markdownlint-restore -->
Expand Down Expand Up @@ -41,7 +42,6 @@ It provides a fully customizable framework for defining and building cloud infra

It's works natively with Mac OSX, Linux, and [Windows 10 (WSL)](https://docs.microsoft.com/en-us/windows/wsl/install-win10).


---

This project is part of our comprehensive ["SweetOps"](https://cpco.io/sweetops) approach towards DevOps.
Expand All @@ -66,7 +66,6 @@ It's 100% Open Source and licensed under the [APACHE2](LICENSE).




## Screenshots


Expand Down Expand Up @@ -108,6 +107,10 @@ chip takes several years to establish; we hope we will not have to wait that lon

Want to learn more? [Check out our getting started with Geodesic guide!](https://docs.cloudposse.com/tutorials/geodesic-getting-started/)





## Usage


Expand Down Expand Up @@ -195,6 +198,7 @@ Like this project? Please give it a ★ on [our GitHub](https://github.com/cloud
Are you using this project or any of our other projects? Consider [leaving a testimonial][testimonial]. =)



## Related Projects

Check out these related projects.
Expand All @@ -203,8 +207,6 @@ Check out these related projects.
- [Build Harness](https://github.com/cloudposse/dev) - Collection of Makefiles to facilitate building Golang projects, Dockerfiles, Helm charts, and more
- [terraform-aws-components](https://github.com/cloudposse/terraform-aws-components) - Catalog of reusable Terraform components and blueprints for provisioning reference architectures



## Help

**Got a question?** We got answers.
Expand Down Expand Up @@ -276,7 +278,7 @@ In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow.

## Copyright

Copyright © 2017-2021 [Cloud Posse, LLC](https://cpco.io/copyright)
Copyright © 2017-2022 [Cloud Posse, LLC](https://cpco.io/copyright)



Expand Down
7 changes: 0 additions & 7 deletions rootfs/etc/init.d/atmos.sh

This file was deleted.

60 changes: 60 additions & 0 deletions rootfs/etc/profile.d/_workdir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Files in the profile.d directory are executed by the lexicographical order of their file names.
# This file sets the working directory inside Geodesic to match the host directory Geodesic
# was launched from, if possible. If the host directory is not accessible, it sets the working directory to `/`.
#
# This file is named _workdir.sh. The leading underscore is needed to ensure this file executes before
# other files that may depend on it. The "w" is needed to ensure it is loaded *after* _preferences.sh
#

function _file_device() {
df --output=source "$1" | tail -1
}

# file_on_host is true when the argument is a file or directory that appears to be on the Host file system.
# Intended to support files on user-defined bind mounts in addition to `/localhost`.
# This function is run by the command line prompt setup, so it should be very fast.
# Therefore we cache some info in the environment.
if df -a | grep -q /localhost; then
export GEODESIC_LOCALHOST_DEVICE=$(_file_device /localhost)
else
export GEODESIC_LOCALHOST_MISSING=true
fi

function file_on_host() {
[[ $GEODESIC_LOCALHOST_MISSING != "true" ]] && [[ $(_file_device "$1") == ${GEODESIC_LOCALHOST_DEVICE} ]]
}

function _default_initial_wd() {
if [[ -d /stacks ]]; then
# Newer default using `atmos` and stacks
export GEODESIC_WORKDIR="/"
else
# Older default working directory
export GEODESIC_WORKDIR="/conf"
fi
red "# Defaulting initial working directory to \"${GEODESIC_WORKDIR}\""
}

# You can set GEODESIC_WORKDIR in your Geodesic preferences to have full control of your starting working directory
if [[ -d $GEODESIC_WORKDIR ]]; then
[[ $SHLVL == 1 ]] && green "# Initial working directory configured as ${GEODESIC_WORKDIR}"
else
if [[ -d $GEODESIC_HOST_CWD ]]; then
if [[ -n $LOCAL_HOME ]] && $(file_on_host "$GEODESIC_HOST_CWD"); then
export GEODESIC_WORKDIR=$(readlink -e "${GEODESIC_HOST_CWD}")
green "# Initial working directory set from host CWD to ${GEODESIC_WORKDIR}"
else
red "# Host CWD \"${GEODESIC_HOST_CWD}\" does not appear to be accessible from this container"
_default_initial_wd
fi
else
red "# No configured working directory is accessible:"
red "# GEODESIC_WORKDIR is \"$GEODESIC_WORKDIR\""
red "# GEODESIC_HOST_CWD is \"$GEODESIC_HOST_CWD\""
_default_initial_wd
fi
fi

[[ $SHLVL == 1 ]] && cd "${GEODESIC_WORKDIR}"

unset -f _default_initial_wd
38 changes: 38 additions & 0 deletions rootfs/etc/profile.d/atmos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

function _configure_atmos_base_path() {
# Leave $ATMOS_BASE_PATH alone if it is already set
if [[ -n $ATMOS_BASE_PATH ]]; then
if [[ $SHLVL == 1 ]]; then
green "# Using configured $ATMOS_BASE_PATH of \"$ATMOS_BASE_PATH\""
fi
return
fi

# If $GEODESIC_WORKDIR contains both a "stacks" and "components" directory,
# use it as the $ATMOS_BASE_PATH
if [[ -d "${GEODESIC_WORKDIR}/stacks" ]] && [[ -d "${GEODESIC_WORKDIR}/components" ]]; then
export ATMOS_BASE_PATH="${GEODESIC_WORKDIR}"
green "# Setting ATMOS_BASE_PATH to \"$ATMOS_BASE_PATH\" based on children of workdir"
return
fi

# If $GEODESIC_WORKDIR is a descendent of either a "stacks" or "components" directory,
# use the parent of that directory as ATMOS_BASE_PATH
if [[ "${GEODESIC_WORKDIR}" =~ /(stacks|components)/ ]]; then
if [[ "${GEODESIC_WORKDIR}" =~ /stacks/ ]]; then
export ATMOS_BASE_PATH="${GEODESIC_WORKDIR%/stacks/*}"
else
export ATMOS_BASE_PATH="${GEODESIC_WORKDIR%/components/*}"
fi
green "# Setting ATMOS_BASE_PATH to \"$ATMOS_BASE_PATH\" based on parent of workdir"
return
fi
yellow "# No candidate for ATMOS_BASE_PATH found, leaving it unset"
}

# Only configure ATMOS_BASE_PATH if we find an `atmos` executable,
# but otherwise leave the function available for the user to call explicitly.
# NOTE: If we start shipping `atmos` with Geodesic by default, change this to
# [[ -f /usr/local/etc/atmos/atmos.yaml ]] && _configure_atmos_base_path
command -v atmos >/dev/null && _configure_atmos_base_path && unset -f _configure_atmos_base_path
2 changes: 1 addition & 1 deletion rootfs/etc/profile.d/prompt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function geodesic_prompt() {

local dir_prompt
dir_prompt="${STATUS}${level_prompt} "
if [[ $(pwd -P) =~ ^/localhost/ ]]; then
if file_on_host "$(pwd -P)"; then
dir_prompt+="${ROLE_PROMPT} ("$'\x01'$(tput bold)$(tput setaf 1)$'\x02HOST\x01'$(tput sgr0)$'\x02'") \W "
else
dir_prompt+="${ROLE_PROMPT} \W "
Expand Down
19 changes: 11 additions & 8 deletions rootfs/templates/wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ set -o pipefail
# Geodesic Settings
export GEODESIC_PORT=${GEODESIC_PORT:-$((30000 + $$ % 30000))}

# If this env var is unset, set it to the current directory without the home dir
export GEODESIC_WORKDIR=${GEODESIC_WORKDIR:-/localhost/${PWD#$HOME/}}
export GEODESIC_HOST_CWD=$(pwd -P 2>/dev/null || pwd)

readonly OS=$(uname -s)

Expand Down Expand Up @@ -156,20 +155,24 @@ function use() {
if [ "${local_home}" == "/localhost" ]; then
echo "WARNING: not mounting ${local_home} because it conflicts with geodesic"
else
echo "# Mounting ${local_home} into container"
DOCKER_ARGS+=(--volume=${local_home}:/localhost)
DOCKER_ARGS+=(--env LOCAL_HOME=${local_home})
echo "# Mounting ${local_home} into container with workdir ${GEODESIC_HOST_CWD}"
DOCKER_ARGS+=(
--volume="${local_home}:/localhost:"
--env LOCAL_HOME="${local_home}"
)
fi

DOCKER_ARGS+=(--privileged
DOCKER_ARGS+=(
--privileged
--publish ${GEODESIC_PORT}:${GEODESIC_PORT}
--name "${DOCKER_NAME}"
--rm
--env GEODESIC_PORT=${GEODESIC_PORT}
--env DOCKER_IMAGE="${DOCKER_IMAGE%:*}"
--env DOCKER_NAME="${DOCKER_NAME}"
--env DOCKER_TAG="${DOCKER_TAG}"
--workdir "${GEODESIC_WORKDIR}")
--env GEODESIC_HOST_CWD="${GEODESIC_HOST_CWD}"
)

trap run_exit_hooks EXIT
# the extra curly braces around .ID are because this file goes through go template substitution local before being installed as a shell script
Expand All @@ -179,7 +182,7 @@ function use() {
if [ $# -eq 0 ]; then
set -- "/bin/bash" "-l" "$@"
fi
docker exec -it "${DOCKER_NAME}" $*
docker exec -it --env GEODESIC_HOST_CWD="${GEODESIC_HOST_CWD}" "${DOCKER_NAME}" $*
else
echo "# Starting new ${DOCKER_NAME} session from ${DOCKER_IMAGE}"
echo "# Exposing port ${GEODESIC_PORT}"
Expand Down

0 comments on commit 1149442

Please sign in to comment.