Skip to content

Commit 3ef0788

Browse files
Merge pull request #306 from smortex/freebsd
Add Compatibility with FreeBSD
2 parents 27e9ad8 + ab97a56 commit 3ef0788

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

files/pkg.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# $1 = string literal to feed to pkg-query. include json formatting
2+
# No error check here because querying the status of uninstalled packages will return non-zero
3+
# Use the format string to make a json object with status and version
4+
# This will print nothing to stdout if the package is not installed
5+
pkg_status() {
6+
pkg query "$1" "${name}"
7+
}
8+
9+
# Determine if newer package is available to mirror the ruby/puppet implementation
10+
pkg_check_latest() {
11+
installed="$(pkg_status '%v')"
12+
[ -z "$installed" ] && success '{ "status": "uninstalled", "version": ""}'
13+
14+
candidate=$(pkg rquery '%v' "$name")
15+
16+
if [ "$installed" != "$candidate" ]; then
17+
cmd_status="{ \"status\":\"installed\", \"version\":\"${installed}\", \"latest\":\"${candidate}\" }"
18+
else
19+
cmd_status="$(pkg_status '{ "status":"installed", "version":"%v" }')"
20+
fi
21+
22+
success "$cmd_status"
23+
}

tasks/init.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"implementations": [
2727
{"name": "init.rb", "requirements": ["puppet-agent"]},
2828
{"name": "windows.ps1", "requirements": ["powershell"], "input_method": "powershell"},
29-
{"name": "linux.sh", "requirements": ["shell"], "input_method": "environment", "files": ["package/files/common.sh", "package/files/apt.sh", "package/files/yum.sh", "package/files/zypper.sh"]}
29+
{"name": "linux.sh", "requirements": ["shell"], "input_method": "environment", "files": ["package/files/common.sh", "package/files/apt.sh", "package/files/pkg.sh", "package/files/yum.sh", "package/files/zypper.sh"]}
3030
],
3131
"extensions": {
3232
"discovery": {

tasks/linux.sh

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ for f in "$PT__installdir"/package/files/*sh; do
77
source "$f"
88
done
99

10-
package_managers=("apt-get" "yum" "zypper")
10+
package_managers=("apt-get" "pkg" "yum" "zypper")
1111
options=()
1212

1313
for p in "${package_managers[@]}"; do
@@ -18,7 +18,7 @@ for p in "${package_managers[@]}"; do
1818
done
1919

2020
[[ $available_manager ]] || {
21-
validation_error "No package managers found: Must be one of: [apt, yum, zypper]"
21+
validation_error "No package managers found: Must be one of: [apt, pkg, yum, zypper]"
2222
}
2323

2424
# For any package manager, check if the action is "status". If so, only run a status command
@@ -65,6 +65,42 @@ case "$available_manager" in
6565
esac
6666
;;
6767

68+
"pkg")
69+
# quiet and assume yes
70+
options+=("-yq")
71+
72+
[ ! -z "$manager_options" ] && options+=("$manager_options")
73+
74+
# For Ruby compatability, 'status' and 'install' will check if the package is upgradable
75+
case "$action" in
76+
"status")
77+
pkg_check_latest
78+
;;
79+
80+
"install")
81+
pkg install "${options[@]}" "$name" >/dev/null || fail
82+
pkg_check_latest
83+
;;
84+
85+
# uninstall: do not print any version information
86+
"uninstall")
87+
pkg remove "${options[@]}" "$name" >/dev/null || fail
88+
success '{ "status": "uninstalled" }'
89+
;;
90+
91+
"upgrade")
92+
action="install"
93+
# Get the currently installed version to compare with the upgraded version
94+
old_version="$(pkg_status '%v')"
95+
96+
pkg upgrade "${options[@]}" "$name" >/dev/null || fail
97+
# For Ruby compatability, this command uses a different output format
98+
cmd_status="$(pkg_status "{ \"old_version\": ${old_version}, \"version\": \"%v\" }")"
99+
success "$cmd_status"
100+
;;
101+
esac
102+
;;
103+
68104
"yum")
69105
# assume yes
70106
options+=("-y")

0 commit comments

Comments
 (0)