Skip to content

Commit

Permalink
Merge pull request #191 from devops-works/global_mode
Browse files Browse the repository at this point in the history
initial system-wide (i.e. global) mode
  • Loading branch information
leucos authored May 3, 2022
2 parents 3420f38 + adf4faf commit 460f16d
Show file tree
Hide file tree
Showing 15 changed files with 527 additions and 156 deletions.
36 changes: 25 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The last binary you'll ever install.
- [FreeBSD (bash/zsh)](#freebsd-bashzsh)
- [OpenBSD (bash/zsh)](#openbsd-bashzsh)
- [Install](#install)
- [User install](#user-install)
- [Updating binenv](#updating-binenv)
- [Supported "distributions"](#supported-distributions)
- [Usage](#usage)
Expand All @@ -36,6 +37,7 @@ The last binary you'll ever install.
- [Example](#example-1)
- [Selecting versions using environment variables](#selecting-versions-using-environment-variables)
- [Example](#example-2)
- [Environment variables](#environment-variables)
- [Removing binenv stuff](#removing-binenv-stuff)
- [Status](#status)
- [FAQ](#faq)
Expand Down Expand Up @@ -63,11 +65,14 @@ typing `binenv install something`.

## Quick start

See [System-wide installation](./SYSTEM.md) for system-wide installations
(a.k.a. global mode).

### Linux (bash/zsh)

```
wget -q https://github.com/devops-works/binenv/releases/download/v0.18.0/binenv_linux_amd64
wget -q https://github.com/devops-works/binenv/releases/download/v0.18.0/checksums.txt
wget -q https://github.com/devops-works/binenv/releases/download/v0.19.0/binenv_linux_amd64
wget -q https://github.com/devops-works/binenv/releases/download/v0.19.0/checksums.txt
sha256sum --check --ignore-missing checksums.txt
mv binenv_linux_amd64 binenv
chmod +x binenv
Expand All @@ -85,8 +90,8 @@ exec $SHELL
### MacOS (with bash)

```
wget -q https://github.com/devops-works/binenv/releases/download/v0.18.0/binenv_darwin_amd64
wget -q https://github.com/devops-works/binenv/releases/download/v0.18.0/checksums.txt
wget -q https://github.com/devops-works/binenv/releases/download/v0.19.0/binenv_darwin_amd64
wget -q https://github.com/devops-works/binenv/releases/download/v0.19.0/checksums.txt
sha256sum --check --ignore-missing checksums.txt
mv binenv_darwin_amd64 binenv
chmod +x binenv
Expand All @@ -105,8 +110,8 @@ binenv does not support windows.
### FreeBSD (bash/zsh)

```
fetch https://github.com/devops-works/binenv/releases/download/v0.18.0/binenv_freebsd_amd64
fetch https://github.com/devops-works/binenv/releases/download/v0.18.0/checksums.txt
fetch https://github.com/devops-works/binenv/releases/download/v0.19.0/binenv_freebsd_amd64
fetch https://github.com/devops-works/binenv/releases/download/v0.19.0/checksums.txt
shasum --ignore-missing -a 512 -c checksums.txt
mv binenv_freebsd_amd64 binenv
chmod +x binenv
Expand All @@ -128,8 +133,8 @@ To be able to verify checksums, you have to install the `p5-Digest-SHA` package.
### OpenBSD (bash/zsh)

```
ftp https://github.com/devops-works/binenv/releases/download/v0.18.0/binenv_openbsd_amd64
ftp https://github.com/devops-works/binenv/releases/download/v0.18.0/checksums.txt
ftp https://github.com/devops-works/binenv/releases/download/v0.19.0/binenv_openbsd_amd64
ftp https://github.com/devops-works/binenv/releases/download/v0.19.0/checksums.txt
cksum -a sha256 -C checksums.txt binenv_openbsd_amd64
mv binenv_openbsd_amd64 binenv
chmod +x binenv
Expand All @@ -148,11 +153,13 @@ If you are using a different shell, skip adding completion to your `.${SHELL}rc`

## Install

### User install

- download a suitable `binenv` (yes, but wait !) for your architecture/OS at
http://github.com/devops-works/binenv/releases.

```
wget -q https://github.com/devops-works/binenv/releases/download/v0.18.0/binenv-<OS>-<ARCH>
wget -q https://github.com/devops-works/binenv/releases/download/v0.19.0/binenv-<OS>-<ARCH>
```

- rename it
Expand Down Expand Up @@ -416,7 +423,7 @@ info.

### Expanding binary absolute path

To get the obsolute path of the binary installed by a distribution you need to
To get the absolute path of the binary installed by a distribution you need to
invoke the command `expand`.

This can be useful when you need to use binenv in conjunction with other tools
Expand Down Expand Up @@ -525,7 +532,6 @@ When an environment variable with this name exists, binenv will use the `=`
operator to look for an exact match for that constraint and will ignore the
contents of the `.binenv.lock` file if it exists.


### Example

```bash
Expand All @@ -539,6 +545,14 @@ $ BINENV_HELM_VERSION=3.6.3 helm version
version.BuildInfo{Version:"v3.6.3", GitCommit:"d506314abfb5d21419df8c7e7e68012379db2354", GitTreeState:"clean", GoVersion:"go1.16.5"}
```

## Environment variables

Two other environment variables exist:

- `BINENV_GLOBAL`: forces `binenv` to run un global mode (same as `-g`); see
[SYSTEM.md](./SYSTEM.md) for more information on this mode.
- `BINENV_VERBOSE`: same as `-v`

## Removing binenv stuff

`binenv` stores downloaded binaries in `~/.binenv/binaries`, and a cache in
Expand Down
150 changes: 150 additions & 0 deletions SYSTEM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# System-wide installation

**This feature is currently in alpha state.**

`binenv` can be installed system-wide, so binaries are managed for the entire
system.

In this mode, `binenv` is installed in several system-wide places:

- `cachedir`, holding the distributions cache, in `/var/cache/binenv`
- `confdir`, holding the distributions list, in `/var/lib/binenv`
- `linkdir`, holding symlinks to the shim, in `/usr/local/bin/`
- `bindir`, holding shim and distributions binaries

Users in the system will be able to use any binary installed this way. However,
only `root` will be able to manage binaries.

To use `binenv` in this global mode, the `-g` flag is required before each
command, e.g.:

```bash
sudo binenv -g install terraform
```

To run in global mode, you can either install from [scratch](#installation), or
[add](#add-system-wide-installation) a system wide installation.

Note that operations that mutate state (i.e. `install`, `uninstall`, `update`,
`upgrade`) will have to be executed as `root`.

**Note that it is mandatory to set `BINENV_GLOBAL=true` to use binaries
installed in system-wide mode (see [Caveat](#caveat)).**

## Installation

Note: instructions for BSD systems have not been tested.

### Linux (bash/zsh)

```
wget -q https://github.com/devops-works/binenv/releases/latest/download/binenv_linux_amd64
wget -q https://github.com/devops-works/binenv/releases/latest/download/checksums.txt
sha256sum --check --ignore-missing checksums.txt
mv binenv_linux_amd64 binenv
chmod +x ./binenv
sudo ./binenv -g update
sudo ./binenv -g install binenv 0.19.0
rm ./binenv
if [[ -n $BASH ]]; then ZESHELL=bash; fi
if [[ -n $ZSH_NAME ]]; then ZESHELL=zsh; fi
echo $ZESHELL
echo "source <(binenv completion ${ZESHELL})" >> ~/.${ZESHELL}rc
exec $SHELL
```

### MacOS (with bash)

```
wget -q https://github.com/devops-works/binenv/releases/latest/download/binenv_darwin_amd64
wget -q https://github.com/devops-works/binenv/releases/latest/download/checksums.txt
sha256sum --check --ignore-missing checksums.txt
mv binenv_darwin_amd64 binenv
chmod +x binenv
sudo ./binenv -g update
sudo ./binenv -g install binenv 0.19.0
rm ./binenv
echo 'source <(binenv completion bash)' >> ~/.bashrc
exec $SHELL
```

### Windows

binenv does not support windows.

### FreeBSD (bash/zsh)

```
fetch https://github.com/devops-works/binenv/releases/latest/download/binenv_freebsd_amd64
fetch https://github.com/devops-works/binenv/releases/latest/download/checksums.txt
shasum --ignore-missing -a 512 -c checksums.txt
mv binenv_freebsd_amd64 binenv
chmod +x binenv
sudo ./binenv -g update
sudo ./binenv -g install binenv 0.19.0
rm ./binenv
if [[ -n $BASH ]]; then ZESHELL=bash; fi
if [[ -n $ZSH_NAME ]]; then ZESHELL=zsh; fi
echo $ZESHELL
echo "source <(binenv completion ${ZESHELL})" >> ~/.${ZESHELL}rc
exec $SHELL
```

If you are using a different shell, skip adding completion to your `.${SHELL}rc` file.

To be able to verify checksums, you have to install the `p5-Digest-SHA` package.

### OpenBSD (bash/zsh)

```
ftp https://github.com/devops-works/binenv/releases/latest/download/binenv_openbsd_amd64
ftp https://github.com/devops-works/binenv/releases/latest/download/checksums.txt
cksum -a sha256 -C checksums.txt binenv_openbsd_amd64
mv binenv_openbsd_amd64 binenv
chmod +x binenv
sudo ./binenv -g update
sudo ./binenv -g install binenv 0.19.0
rm ./binenv
if [[ -n $BASH ]]; then ZESHELL=bash; fi
if [[ -n $ZSH_NAME ]]; then ZESHELL=zsh; fi
echo $ZESHELL
echo "source <(binenv completion ${ZESHELL})" >> ~/.${ZESHELL}rc
exec $SHELL
```

If you are using a different shell, skip adding completion to your `.${SHELL}rc` file.

## Add system-wide installation

- Ensure you are using at least `binenv` version v0.19.0.

```bash
binenv version
```

- install `binenv` system-wide

```bash
binenv -g install binenv 0.19.0-rc4
```

- adjust path in your rcfiles according to your preferences

## Caveat

To use system-wide installed binaries, you have to set the `BINENV_GLOBAL=true`
environment variable.

This is required since binenv shim will look in user paths by default if this
is not set.

## FAQ

### Why do I have to use `-g` ? Can't binenv autodetect global mode ?

Yes it could. But what if you want to use a user installation along a global
one ? Say, for instance, when your sysadmin does not want to install a specific
binary.

The best way to achieve this is to `export BINENV_GLOBAL=true` in your shell
rc-file so binenv will always run in global mode.
3 changes: 0 additions & 3 deletions cmd/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ func expandCmd(a *app.App) *cobra.Command {
Long: `Return full installed distribution binary path.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
verbose, _ := cmd.Flags().GetBool("verbose")
a.SetVerbose(verbose)

a.SetFlag("justExpand", true)
a.Execute(args)
return nil
Expand Down
9 changes: 3 additions & 6 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@ import (

// localCmd represents the local command
func installCmd(a *app.App) *cobra.Command {
var bindir string
var fromlock, dryrun, verbose bool
var fromlock, dryrun bool

cmd := &cobra.Command{
Use: "install [--lock] [--dry-run] [<distribution> <version> [<distribution> <version>]]",
Short: "Install a version for the package",
Long: `This command will install one or several distributions with the specified versions.
If --lock is used, versions from the .binenv.lock file in the current directory will be installed.`,
Run: func(cmd *cobra.Command, args []string) {
a.SetVerbose(verbose)
a.SetDryRun(dryrun)
a.SetBinDir(bindir)

if fromlock {
a.InstallFromLock()
}

if len(args) > 0 {
a.Install(args...)
}
Expand All @@ -41,10 +40,8 @@ If --lock is used, versions from the .binenv.lock file in the current directory
},
}

cmd.Flags().StringVarP(&bindir, "bindir", "b", app.GetDefaultBinDir(), "Binaries directory")
cmd.Flags().BoolVarP(&fromlock, "lock", "l", false, "Install versions specified in ./.binenv.lock")
cmd.Flags().BoolVarP(&dryrun, "dry-run", "n", false, "Do not install, just simulate")
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose operations")

return cmd
}
2 changes: 0 additions & 2 deletions cmd/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ func localCmd(a *app.App) *cobra.Command {
Any previously constraint used in this file for the distribution will be removed, and an exact match ('=') will be used.`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
verbose, _ := cmd.Flags().GetBool("verbose")
a.SetVerbose(verbose)
a.Local(args[0], args[1])
},
}
Expand Down
Loading

0 comments on commit 460f16d

Please sign in to comment.