Skip to content

Commit 38ce178

Browse files
Merge pull request #9644 from habitat-sh/agadgil/no-install-deps-fix
Fixes failure when `NO_INSTALL_DEPS` is set
2 parents 84f6848 + 17a6de1 commit 38ce178

File tree

2 files changed

+132
-17
lines changed

2 files changed

+132
-17
lines changed

components/plan-build/bin/hab-plan-build.sh

+84-17
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ _ensure_origin_key_present() {
606606
# If the commands are not found, `exit_with` is called and the program is
607607
# terminated.
608608
_find_system_commands() {
609-
if exists stat; then
609+
if exists stat; then
610610
if stat -f '%Su:%g' . 2>/dev/null 1>/dev/null; then
611611
_stat_variant="bsd"
612612
elif stat -c '%u:%g' . 2>/dev/null 1>/dev/null; then
@@ -675,6 +675,66 @@ _find_system_commands() {
675675
fi
676676
}
677677

678+
# **Internal** Return the path to the latest release of a package on stdout.
679+
#
680+
# ```
681+
# _latest_installed_package acme/nginx
682+
# # /hab/pkgs/acme/nginx/1.8.0/20150911120000
683+
# _latest_installed_package acme/nginx/1.8.0
684+
# # /hab/pkgs/acme/nginx/1.8.0/20150911120000
685+
# _latest_installed_package acme/nginx/1.8.0/20150911120000
686+
# # /hab/pkgs/acme/nginx/1.8.0/20150911120000
687+
# ```
688+
#
689+
# Will return 0 if a package was found on disk, and 1 if a package cannot be
690+
# found. A message will be printed to stderr explaining that no package was
691+
# found.
692+
_latest_installed_package() {
693+
local result
694+
if result="$($HAB_BIN pkg path "$1" 2> /dev/null)"; then
695+
echo "$result"
696+
return 0
697+
else
698+
warn "Could not find a suitable installed package for '$1'"
699+
return 1
700+
fi
701+
}
702+
703+
# **Internal** Returns the path to the desired package on stdout, using the
704+
# constraints specified in `$pkg_deps` or `$pkg_build_deps`. If a package
705+
# cannot be found locally on disk, and the `hab` CLI package is present,
706+
# this program will attempt to install the package from a remote repository.
707+
#
708+
# ```
709+
# _resolve_dependency acme/zlib
710+
# # /hab/pkgs/acme/zlib/1.2.8/20151216221001
711+
# _resolve_dependency acme/zlib/1.2.8
712+
# # /hab/pkgs/acme/zlib/1.2.8/20151216221001
713+
# _resolve_dependency acme/zlib/1.2.8/20151216221001
714+
# # /hab/pkgs/acme/zlib/1.2.8/20151216221001
715+
# ```
716+
#
717+
# Will return 0 if a package was found or installed on disk, and 1 if a package
718+
# cannot be found or remotely installed. A message will be printed to stderr to
719+
# provide context.
720+
_resolve_dependency() {
721+
local dep="$1"
722+
local dep_path
723+
if ! echo "$dep" | grep -q '\/' > /dev/null; then
724+
warn "Origin required for '$dep' in plan '$pkg_origin/$pkg_name' (example: acme/$dep)"
725+
return 1
726+
fi
727+
728+
if dep_path=$(_latest_installed_package "$dep"); then
729+
echo "${dep_path}"
730+
return 0
731+
else
732+
return 1
733+
fi
734+
}
735+
736+
# **Internal** Attempts to download a package dependency. If the value of the
737+
678738
# **Internal** Attempts to download a package dependency. If the value of the
679739
# `$NO_INSTALL_DEPS` variable is set, then no package installation will occur.
680740
# If an installation is attempted but there is an error, this function will
@@ -689,22 +749,29 @@ _install_dependency() {
689749
local dep="${1}"
690750
local origin
691751
local channel="$HAB_BLDR_CHANNEL"
692-
if [[ -z "${NO_INSTALL_DEPS:-}" ]]; then
693-
origin="$(echo "$dep" | cut -d "/" -f 1)"
694-
if [[ $origin == "core" ]]; then
695-
channel="$HAB_REFRESH_CHANNEL"
696-
if [[ $HAB_PREFER_LOCAL_CHEF_DEPS == "false" ]]; then
697-
IGNORE_LOCAL="--ignore-local"
698-
fi
699-
fi
752+
if [[ -z "${NO_INSTALL_DEPS:-}" || ${NO_INSTALL_DEPS} == "false" ]]; then
753+
origin="$(echo "$dep" | cut -d "/" -f 1)"
754+
if [[ $origin == "core" ]]; then
755+
channel="$HAB_REFRESH_CHANNEL"
756+
if [[ $HAB_PREFER_LOCAL_CHEF_DEPS == "false" ]]; then
757+
IGNORE_LOCAL="--ignore-local"
758+
fi
759+
fi
700760

701-
$HAB_BIN pkg install -u $HAB_BLDR_URL --channel $channel ${IGNORE_LOCAL:-} "$@" || {
702-
if [[ "$channel" != "$HAB_FALLBACK_CHANNEL" ]]; then
703-
build_line "Trying to install '$dep' from '$HAB_FALLBACK_CHANNEL'"
704-
$HAB_BIN pkg install -u $HAB_BLDR_URL --channel "$HAB_FALLBACK_CHANNEL" ${IGNORE_LOCAL:-} "$@" || true
705-
fi
706-
}
707-
fi
761+
$HAB_BIN pkg install -u $HAB_BLDR_URL --channel $channel ${IGNORE_LOCAL:-} "$@" || {
762+
if [[ "$channel" != "$HAB_FALLBACK_CHANNEL" ]]; then
763+
build_line "Trying to install '$dep' from '$HAB_FALLBACK_CHANNEL'"
764+
$HAB_BIN pkg install -u $HAB_BLDR_URL --channel "$HAB_FALLBACK_CHANNEL" ${IGNORE_LOCAL:-} "$@" || true
765+
fi
766+
}
767+
else
768+
if resolved=$(_resolve_dependency "${dep}" | sed "s|${HAB_PKG_PATH}[/]\?||g"); then
769+
echo "${resolved}"
770+
return 0
771+
else
772+
return 1
773+
fi
774+
fi
708775
return 0
709776
}
710777

@@ -921,7 +988,7 @@ _attach_whereami() {
921988
# * Otherwise `$_hab_cmd` is used, set in the `_find_system_commands()`
922989
# function
923990
_determine_hab_bin() {
924-
if [[ -n "${NO_INSTALL_DEPS:-}" ]]; then
991+
if [[ -n "${NO_INSTALL_DEPS:-}" && "${NO_INSTALL_DEPS}" != "false" ]]; then
925992
build_line "NO_INSTALL_DEPS set: no package dependencies will be installed"
926993
fi
927994

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
hab origin key generate $env:HAB_ORIGIN
2+
3+
Context "NO_INSTALL_DEPS env variable is set" {
4+
BeforeEach {
5+
hab pkg uninstall core/redis
6+
}
7+
8+
Describe "NO_INSTALL_DEPS Environment is Set to True" {
9+
$env:HAB_STUDIO_SECRET_NO_INSTALL_DEPS = "true"
10+
11+
It "When Package Dependency is already installed" {
12+
hab pkg install core/redis
13+
14+
hab pkg build test/fixtures/no-install-deps
15+
$LASTEXITCODE | Should -Be 0
16+
}
17+
18+
It "When Package Dependency is not installed" {
19+
20+
hab pkg build test/fixtures/no-install-deps
21+
$LASTEXITCODE | Should -Not -Be 0
22+
}
23+
}
24+
25+
Describe "NO_INSTALL_DEPS Environment is set to False" {
26+
$env:HAB_STUDIO_SECRET_NO_INSTALL_DEPS = "false"
27+
28+
It "When Package Dependency is already installed" {
29+
hab pkg install core/redis
30+
hab pkg build test/fixtures/no-install-deps
31+
$LASTEXITCODE | Should -Be 0
32+
}
33+
34+
It "When Package Dependency is not installed" {
35+
hab pkg build test/fixtures/no-install-deps
36+
$LASTEXITCODE | Should -Be 0
37+
}
38+
}
39+
40+
Describe "NO_INSTALL_DEPS Environment is not set" {
41+
$env:HAB_STUDIO_SECRET_NO_INSTALL_DEPS = ""
42+
43+
It "When Package Dependency is not installed" {
44+
hab pkg build test/fixtures/no-install-deps
45+
$LASTEXITCODE | Should -Be 0
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)