Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
emerge aliases and functions
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
kris0
n00b
n00b


Joined: 10 Oct 2021
Posts: 16

PostPosted: Mon Oct 11, 2021 10:01 pm    Post subject: emerge aliases and functions Reply with quote

Got some good responses on my last emerge post so here is another one, as before I'm curious if you do something similar/better.

In my shell rc I have:

Code:

alias emerge="sudo systemd-inhibit --why 'emerging packages' emerge"

update() {
  emerge --ask n --update --oneshot sys-apps/portage
  emerge --alert --update --deep --newuse @world
}

supdate() {
  sudo systemd-inhibit --why updating emaint sync -a && update
}


If your not familiar the systemd-inhib it stops my computer from suspending while emerging.
Back to top
View user's profile Send private message
cboldt
Veteran
Veteran


Joined: 24 Aug 2005
Posts: 1046

PostPosted: Mon Oct 11, 2021 10:31 pm    Post subject: Reply with quote

I run a small LAN where sync on the master machine cascades to the others. These are functions in root's .bashrc

Code:
esync ()
{
  emerge --sync --quiet
  screen -p 2 -X stuff "date;emerge -p --update --deep --newuse --with-bdeps=y @world\n"
  if [ $ESYNC_HOST == $HOSTNAME ]; then
  for i in $ESYNC_SLAVES; do
    ping -c 1 $i > /dev/null 2>&1
    [ $? == 0 ] && ssh -x $i screen -S RootEnv-Client -p 1 -X 'stuff "esync\n"'
  done
  fi
}


I happen to use "screen" with a definite setup, but passing the "esync" command from ESYNC_HOST to ESYNC_SLAVE could be by other means. ESYNC_HOST is also the local mirror for the portage tree.

Window 2 of the screen session on each machine runs a pretend update. No two machines are the same, so same adjusting of USE, masking, etc. can intervene before commanding the actual update. Enter another function, also in .bashrc

Code:
emerge.new ()
{
  emerge -q --update --deep --newuse --with-bdeps=y @world
  emerge -q @preserved-rebuild
  CLEAN_DELAY=0 emerge -q --depclean
  revdep-rebuild --quiet --ignore
}


My sense is that by now the revdep-rebuild is not needed. Sometimes, I would say rarely, additional steps beyond an `etc-update` are needed to keep things straight. perl-cleaner comes to mind.

I have intrusion detection here, mostly I think as proof it works, not to do any actual work. Intrusion detection is tripwire and rkhunter. So one more set of functions to check for the changes caused by updating the system, and clear them. That set of functions also copies non-standard files in /etc and /var/db/repos/local into a tarball.

Once a week, just a few machines (three or four, depending on whether or not the reserve machine is powered up), and the system stays pretty tidy. No scripted but on cron jobs, monthly check for cruft files and deprecated USE flags.
Back to top
View user's profile Send private message
kris0
n00b
n00b


Joined: 10 Oct 2021
Posts: 16

PostPosted: Mon Oct 11, 2021 10:45 pm    Post subject: Reply with quote

cboldt wrote:
I happen to use "screen"


I wanted to use tmux in mine, but the only way to make it work like I thought it should was to send keys so I didn't do it. Maybe there is a way with disown/reptyr.
Back to top
View user's profile Send private message
cboldt
Veteran
Veteran


Joined: 24 Aug 2005
Posts: 1046

PostPosted: Mon Oct 11, 2021 11:29 pm    Post subject: Reply with quote

I think tmux "send-keys" is similar to screen "-X stuff". I don't know the exact syntax for tmux (maybe "send-keys -X"), just saying that I think the same function is available. On any given machine the "screen -X stuff" command operates inside a given instance of screen. But the ESYNC_HOST is using the screen function to "stuff" a command across machines. tmux likewise appears to have this function of instance ID.

It's easy to test the functions - took me a few tries to get the syntax working.

Not apparent from the bash functions, the systems here have a post sync hook to update eix, and also to enable `eix-diff` I use eix-diff to look for package names being added to the portage tree. My reasons for curiosity there are totally unrelated to maintenance of the system.
Back to top
View user's profile Send private message
teika
Apprentice
Apprentice


Joined: 19 Feb 2011
Posts: 155
Location: YYYY-MM-DD, period. Have you ever used the Internet?

PostPosted: Tue Oct 26, 2021 1:00 pm    Post subject: Reply with quote

I save the outputs of emerge. I also have a command to view these logs, which is a wrapper for "less". (Elog is the logger of ebuild (1), not of emerge.)

My .bashrc has this:
Code:
alias emerge="emergeWrapper.sh"
alias oneshot="emergeWrapper.sh -1"
function ELV(){
    # ELV = Emerge Log Viewer
    # The latest is shown first, and the oldest is shown last.
    find <emerge log dir> -type f | xargs ls -t |xargs less
}


The file "emergeWrapper.sh" looks:
Code:
#!/bin/bash

LOGDIR=<emerge log dir>

### Bug:
# * Don't catch the exit code of emerge
#   Because we rely on "| tee" (so it's a subshell),
#   Passing the exit code to the parent is cumbersome.

### Notes:

# Emerge uses the following console_codes. See man 4 console_codes.
# ESC [ <param> m (ansi color & bold)
# ESC [ A  (go upward)
# ESC ] 0; <title> ^G (set window title)
# 08 (BS), 09 (HT)

# Previously this script relied on script (1), but it was problematic, because
# * The newline was CR + LF (\r\n), rather than LF.
# * When viewed with less (1), the appearance was somewhat annoying.
#   probably due to the CR+LF newline.

{
    mkdir -p -m 777 $LOGDIR

    log="$(date '+%m-%d.%H.%M.%S')"
    log=${LOGDIR}/${log}

    echo -n "Logging began at: " > $log
    date >> $log
    echo "$ emerge ${@}" >> $log

    emerge "${@}" 2>&1 | tee -a $log
    echo -n "Logging done at:" >> $log
    date >> $log

    echo "Log is $log"

    exit
}


EDIT: Thanks Hu for a detailed comment. I deleted the last "rm -f $f", which is a leftover from older versions.

These days I use "set -u" to detect uninitialized variables. (I know some are against its use.)
_________________
Hack of easy Shift / Ctrl / AltGr etc; save your pinkies, type without drudgery: topic 865313

XPAT - Xi, Putin, Abe and Trump - are security holes of their own nations.


Last edited by teika on Wed Oct 27, 2021 2:09 am; edited 1 time in total
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21631

PostPosted: Tue Oct 26, 2021 4:12 pm    Post subject: Reply with quote

teika wrote:
Code:
function ELV(){    find <emerge log dir> -type f | xargs ls -t |xargs less
}
Using ls to find files for further processing is almost always wrong, although it can be convenient.
teika wrote:
Code:
### Bug:
# * Don't catch the exit code of emerge
#   Because we rely on "| tee" (so it's a subshell),
#   Passing the exit code to the parent is cumbersome.
Make the shell do it for you.
Code:
$ (false | tee; echo $?)
0
$ (set -o pipefail; false | tee; echo $?)
1
teika wrote:
Code:
{
    mkdir -p -m 777 $LOGDIR
To guard against users with crazy paths adapting this script, this should be quoted. Using mode 777 is generally a bad idea. It would be better to make it writable only to the Portage user.
teika wrote:
Code:
    log="$(date '+%m-%d.%H.%M.%S')"
Since you are already using /bin/bash (rather than /bin/sh), you can rely on bash-isms here.
Code:
$ (TZ=UTC printf -v a '%(%m-%d.%H.%M.%S)T' -1; echo "$a")
10-26.16.08.27
teika wrote:
Code:
    echo -n "Logging began at: " > $log
    date >> $log
    echo "$ emerge ${@}" >> $log
By the same principle, this could be a single printf.
Code:
printf "Logging began at %(%a %b %e %H:%M:%S %Z %Y)T\n$ emerge %s" -1 "$*" >> "$log"
You could also use %c to get a much shorter format string, but some locales omit the timezone name in that case.

If you want really accurate quoting of the emerge arguments, make bash do it for you.
Code:
(printf "Logging began at %(%a %b %e %H:%M:%S %Z %Y)T\n" -1; PS4=; BASH_XTRACEFD=1; set -x; : emerge "$@") >> "$log"
teika wrote:
Code:
    rm -f $f
What does this delete? I don't see an assignment to f anywhere above.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum