Page MenuHomePhabricator

Terminal configuration for cookbooks
Open, LowPublic

Description

I made a cookbook to disable Puppet merges temporarily (sre.puppet.disable-merges) which eventually executes "puppet-merge --lockout-tagout". This however fails since puppet-merge attempts to use ANSI colour codes and Cumin (or clustershell?) doesn't set a modern terminal and thus fails:

Acquired lock for key /spicerack/locks/cookbooks/sre.puppet.disable-merges: {'concurrency': 20, 'created': '2026-06-12 06:11:56.163789', 'owner': 'jmm@cumin2002 [2264380]', 'ttl': 1800}
START - Cookbook sre.puppet.disable-merges
===== NODE GROUP =====
(6) puppetserver[2001-2002,2004].codfw.wmnet,puppetserver[1001-1003].eqiad.wmnet
----- OUTPUT for command #1: 'sudo /usr/local/...bled for a test"' -----
tput: unknown terminal "unknown"
tput: unknown terminal "unknown"
================

I'm wondering about the correct place to fix this? Should a 21st century terminal be made the default in Spicerack or rather on a per cookbook basis where it's needed?

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald Transcript

It seems to be a mix of things, from one side sudo is executing without bash that sets TERM, with:

sudo cumin 'A:puppetserver' 'echo $TERM'

you get dumb, that is expected in non interactive shells, when instead you run:

sudo cumin 'A:cumin' 'sudo sh -c '"'"'echo $TERM'"'"''

you get unknown, that makes tput fail because there is no terminfo entry for it.

On the other side it's not in the env:

$ sudo cumin 'A:puppetserver' 'env | grep TERM || echo "missing"'
...
missing

that I think is what makes sudo unset/strip it.

Clustershells doesn't allocate a PTY, and I think we shouldn't modify that behavior.

Change #1302262 had a related patch set uploaded (by JHathaway; author: JHathaway):

[operations/puppet@production] puppet-merge: disable colors if we don't have a tty

https://gerrit.wikimedia.org/r/1302262

Since we don't have a PTY, I would disable colors. Patch sent!

@MoritzMuehlenhoff what do you think of the patch? Or do you want to find a way to retain the colors?

One quick fix should be to just remove sudo AFAIUI.
That said, if in the longer term we still aim to go towards unprivileged cumin that will use sudo when needed this is probably something that needs a more proper general fix.

One quick fix should be to just remove sudo AFAIUI.
That said, if in the longer term we still aim to go towards unprivileged cumin that will use sudo when needed this is probably something that needs a more proper general fix.

Relying on TERM=dumb would probably work, as most terminals use the same escape codes for colors. However, the ideal would be for a PTY to be allocated and for the users TERM value to be plumbed all the way through, since tput looks up the escape codes for a specific terminal.

When using ssh, if you don't ask for a PTY with -t then TERM is unset, which I think should be spicerack's behavior as well.

My understanding is that it's set to dumb when not in a PTY:

mylaptop $ ssh cumin1003.eqiad.wmnet 'echo $TERM'
dumb
mylaptop $ ssh -t cumin1003.eqiad.wmnet 'echo $TERM'
xterm-256color
Connection to cumin1003.eqiad.wmnet closed.

but sudo changes things to unknown:

mylaptop $ ssh cumin1003.eqiad.wmnet 'sudo sh -c '"'"'echo $TERM'"'"''
unknown
mylaptop $ ssh -t cumin1003.eqiad.wmnet 'sudo sh -c '"'"'echo $TERM'"'"''
xterm-256color
Connection to cumin1003.eqiad.wmnet closed.

@MoritzMuehlenhoff what do you think of the patch? Or do you want to find a way to retain the colors?

The patch sounds great, let's take that route. There's no need for colour output for the command to disable Puppet merges.

My understanding is that it's set to dumb when not in a PTY:

mylaptop $ ssh cumin1003.eqiad.wmnet 'echo $TERM'
dumb
mylaptop $ ssh -t cumin1003.eqiad.wmnet 'echo $TERM'
xterm-256color
Connection to cumin1003.eqiad.wmnet closed.

I did a bit more investigation, unsurprisingly, its weird.

ssh doesn't set the TERM variable normally, but if TERM is not set in the environment. Bash sets a local variable to TERM=dumb

$ ssh cumin1003.eqiad.wmnet 'declare -p TERM'
declare -- TERM="dumb"

But the variable is not exported as an environment variable, other shells don't set it at all:

$ echo $(unset TERM; bash -c 'echo $TERM')
dumb
$ echo $(unset TERM; sh -c 'echo $TERM')
$ echo $(unset TERM; zsh -c 'echo $TERM')

If you force PTY allocation with -t then ssh sets the environment variable`TERM` to match your terminal:

$ ssh cumin1003.eqiad.wmnet -t 'declare -p TERM'
declare -x TERM="xterm"

but sudo changes things to unknown:

mylaptop $ ssh cumin1003.eqiad.wmnet 'sudo sh -c '"'"'echo $TERM'"'"''
unknown
mylaptop $ ssh -t cumin1003.eqiad.wmnet 'sudo sh -c '"'"'echo $TERM'"'"''
xterm-256color
Connection to cumin1003.eqiad.wmnet closed.

Since bash doesn't export TERM=dumb the environment variable is unset when sudo executes. Sudo then makes the questionable decision to set it to TERM=unknown rather than just leaving it unset.

Change #1302262 merged by JHathaway:

[operations/puppet@production] puppet-merge: disable colors if we don't have a tty

https://gerrit.wikimedia.org/r/1302262

@MoritzMuehlenhoff what do you think of the patch? Or do you want to find a way to retain the colors?

The patch sounds great, let's take that route. There's no need for colour output for the command to disable Puppet merges.

sounds good, merged

@MoritzMuehlenhoff what do you think of the patch? Or do you want to find a way to retain the colors?

The patch sounds great, let's take that route. There's no need for colour output for the command to disable Puppet merges.

sounds good, merged

Thanks! I'll retest the cookbook tomorrow.

Thanks! I'll retest the cookbook tomorrow.

That didn't work, puppet-merge leaves some "Internal error". But let's not bother with this further; puppet-merge can be kept for interactive use and for the cookbook I've instead made https://gerrit.wikimedia.org/r/c/operations/cookbooks/+/1303997

(1) puppetserver1001.eqiad.wmnet
----- OUTPUT for command #1: 'sudo /usr/local/...erges as a test"' -----
Internal error!
===== NODE GROUP =====
(5) puppetserver[2001-2002,2004].codfw.wmnet,puppetserver[1002-1003].eqiad.wmnet
----- OUTPUT for command #1: 'sudo /usr/local/...erges as a test"' -----
To ensure consistent locking please run puppet-merge from: puppetserver1001.eqiad.wmnet
================

I think that makes sense, "Internal error!" is the helpful message that the args to the script were wrong. However, the whole dual python and shell script setup is a mess. Touching the file directly does have a small risk of breaking, if the script changes, but I think that is acceptable.