Changed File Deployment

This commit is contained in:
Jan 2025-04-10 14:51:45 +02:00
parent fd27618915
commit 6c1c1b0ea5
8 changed files with 590 additions and 189 deletions

View file

@ -2,6 +2,8 @@ cd "$(dirname "$0")"
cp preseed/preseed.cfg source/preseed.cfg cp preseed/preseed.cfg source/preseed.cfg
cp preseed/gtk.cfg source/isolinux/gtk.cfg cp preseed/gtk.cfg source/isolinux/gtk.cfg
mkdir source/files
cp preseed/late_command.sh source/files/late_command.sh
cd source cd source

26
preseed/late_command.sh Normal file
View file

@ -0,0 +1,26 @@
#!/bin/bash
# Install and configure a Ubuntu-PC for the charity organization angestöpselt e.V. | angestoepselt.de
# created by matthias of angestöpselt e.V.
# created at 2022-02-17
version=0.2
set -o errexit
exec 100>/tmp/z31.lock || exit 1
flock 100 || exit 1
# Speed up script by not using unicode.
LC_ALL=C
LANG=C
# some static variables
DEBIAN_FRONTEND=noninteractive
current_user=$(id 1000 | awk -F '[()]' '{print $2}')
lockfile=/tmp/z31.lock
sysinfofile=$HOME/.config/systeminfo.json
random_tmpdir=$(mktemp)
SCRIPT_DIR="$(dirname "$0")"
TIME=$(date +%Y%m%d%H%M)
LOGTIME=$(date "+%Y-%m-%d %H:%M:%S")
LOGFILE="$SCRIPT_DIR/$TIME-z31.log"

View file

@ -100,17 +100,4 @@ d-i netcfg/choose_interface select auto
d-i netcfg/hostname string computerspende d-i netcfg/hostname string computerspende
d-i preseed/late_command string \ d-i preseed/late_command string \
in-target --pass-stdout bash -c "echo 'computerspende ALL=NOPASSWD:ALL' > /etc/sudoers.d/computerspende"; \ cp /cdrom/files/late_command.sh /home/computerspende/late_command.sh
in-target bash -c 'echo "#!/bin/bash" > /home/computerspende/echo_message.sh'; \
in-target bash -c 'echo "echo \"Hello, this is a message from late_command!\"" >> /home/computerspende/echo_message.sh'; \
in-target bash -c 'echo "read p" >> /home/computerspende/echo_message.sh '; \
in-target chmod +x /home/computerspende/post_hardware.sh; \
in-target bash -c 'mkdir -p /home/computerspende/.config/autostart'; \
in-target bash -c 'echo "[Desktop Entry]" > /home/computerspende/.config/autostart/post_hardware.desktop'; \
in-target bash -c 'echo "Type=Application" >> /home/computerspende/.config/autostart/post_hardware.desktop'; \
in-target bash -c 'echo "Terminal=true" >> /home/computerspende/.config/autostart/post_hardware.desktop'; \
in-target bash -c 'echo "Exec=gnome-terminal -- /home/computerspende/post_hardware.sh" >> /home/username/.config/autostart/post_hardware.desktop'; \
in-target bash -c 'echo "Name=Post Hardware Setup" >> /home/computerspende/.config/autostart/post_hardware.desktop'; \
in-target bash -c 'chown computerspende:computerspende /home/computerspende/post_hardware.sh'; \
in-target bash -c 'chown -R computerspende:computerspende /home/computerspende/.config'

View file

@ -1,4 +1,4 @@
def convert_to_in_target(bash_script_path, target_username): def convert_to_in_target(bash_script_path, target_username, output_file_path):
try: try:
with open(bash_script_path, 'r') as file: with open(bash_script_path, 'r') as file:
lines = file.readlines() lines = file.readlines()
@ -6,7 +6,7 @@ def convert_to_in_target(bash_script_path, target_username):
in_target_commands = [] in_target_commands = []
# Construct the script creation command # Construct the script creation command
script_creation_cmd = f"in-target bash -c 'echo \"#!/bin/bash\" > /home/{target_username}/post_hardware.sh'" script_creation_cmd = f"d-i preseed/late_command string \\"
in_target_commands.append(script_creation_cmd) in_target_commands.append(script_creation_cmd)
# Convert each script line into an in-target command # Convert each script line into an in-target command
@ -14,21 +14,33 @@ def convert_to_in_target(bash_script_path, target_username):
# Strip the line to remove leading/trailing whitespace # Strip the line to remove leading/trailing whitespace
stripped_line = line.strip() stripped_line = line.strip()
if stripped_line: # Ignore empty lines if stripped_line: # Ignore empty lines
escaped_line = stripped_line.replace('"', '\\"').replace('$', '\\$') escaped_line = (
cmd = f"in-target bash -c 'echo \"{escaped_line}\" >> /home/{target_username}/post_hardware.sh'" stripped_line.replace('"', '\\"')
.replace('$', '\\$')
.replace('(', '\\(')
.replace(')', '\\)')
.replace('[', '\\[')
.replace(']', '\\]')
.replace('#', '\\#')
)
cmd = f"in-target bash -c 'echo \"{escaped_line}\" >> /home/{target_username}/post_hardware.sh' \\"
in_target_commands.append(cmd) in_target_commands.append(cmd)
# Add command to make the script executable # Add command to make the script executable
in_target_commands.append(f"in-target chmod +x /home/{target_username}/post_hardware.sh") in_target_commands.append(f"in-target chmod +x /home/{target_username}/post_hardware.sh")
# Output the generated in-target commands # Write the generated in-target commands to the output file
with open(output_file_path, 'w') as output_file:
for command in in_target_commands: for command in in_target_commands:
print(command) output_file.write(command + "\n")
print(f"In-target commands successfully written to {output_file_path}")
except FileNotFoundError: except FileNotFoundError:
print("The specified bash script file does not exist.") print("The specified bash script file does not exist.")
# Usage # Usage
bash_script_path = 'post_hardware.sh' # Replace with the path to your bash script bash_script_path = 'late_command.sh' # Replace with the path to your bash script
target_username = 'computerspende' # Replace with the target user's username target_username = 'computerspende' # Replace with the target user's username
convert_to_in_target(bash_script_path, target_username) output_file_path = 'scripts.txt' # Replace with the desired path for the output file
convert_to_in_target(bash_script_path, target_username, output_file_path)

516
scripts/late_command.sh.bak Normal file
View file

@ -0,0 +1,516 @@
#!/bin/bash
# Install and configure a Ubuntu-PC for the charity organization angestöpselt e.V. | angestoepselt.de
# created by matthias of angestöpselt e.V.
# created at 2022-02-17
version=0.2
set -o errexit
exec 100>/tmp/z31.lock || exit 1
flock 100 || exit 1
# Speed up script by not using unicode.
LC_ALL=C
LANG=C
# some static variables
DEBIAN_FRONTEND=noninteractive
current_user=$(id 1000 | awk -F '[()]' '{print $2}')
lockfile=/tmp/z31.lock
sysinfofile=$HOME/.config/systeminfo.json
random_tmpdir=$(mktemp)
SCRIPT_DIR="$(dirname "$0")"
TIME=$(date +%Y%m%d%H%M)
LOGTIME=$(date "+%Y-%m-%d %H:%M:%S")
LOGFILE="$SCRIPT_DIR/$TIME-z31.log"
# Set variables for stdout print
COLOR_N='\e[0m'
COLOR_GREEN='\e[1;32m'
COLOR_RED='\e[1;31m'
COLOR_BLUE='\e[1;34m'
COLOR_WHITE='\e[1;97m'
OK="[${COLOR_GREEN}ok${COLOR_N}]"
FAIL="[${COLOR_RED}fail${COLOR_N}]"
INFO="[${COLOR_BLUE}info${COLOR_N}]"
SKIP="[${COLOR_BLUE}skip${COLOR_N}]"
PROGRESS="[${COLOR_BLUE}..${COLOR_N}]"
# log actual command and it's stderr to logfile in one go
runcmd_stdout() {
echo "+ $1" >>"$LOGFILE"
bash -c -o pipefail "$1" 2>>"$LOGFILE" | tee -a "$LOGFILE"
}
runcmd() {
echo "+ $1" >>"$LOGFILE"
bash -c -o pipefail "$1" >>"$LOGFILE" 2>&1
}
printprog() {
echo -ne "${PROGRESS} $*\r"
}
printok() {
echo -e "\r${OK} $*"
}
printfail() {
echo -e "\r${FAIL} $*"
}
printinfo() {
echo -e "${INFO} $*"
}
printskip() {
echo -e "\r${SKIP} $*"
}
ErrorHandling() {
set -eu
printfail "Something went wrong, exiting. Check $LOGFILE for more details."
}
system_update() {
set -o pipefail
trap ErrorHandling ERR INT
printprog "update cache"
runcmd "sudo /usr/bin/apt-get update -qq"
printok "update cache"
printprog "upgrade system"
runcmd "sudo /usr/bin/apt-get dist-upgrade -y -qq"
printok "upgrade system"
}
system_install_base() {
set -o pipefail
trap ErrorHandling ERR INT
printprog "add anydesk repo to source.list.d"
if [ -f "/etc/apt/sources.list.d/anydesk-stable.list" ]; then
printskip "add anydesk repo to source.list.d"
else
runcmd "sudo /usr/bin/wget -qO - https://keys.anydesk.com/repos/DEB-GPG-KEY | sudo apt-key add - && \
echo "deb http://deb.anydesk.com/ all main" > /etc/apt/sources.list.d/anydesk-stable.list"
printok "add anydesk repo to source.list.d"
fi
echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | sudo debconf-set-selections
printprog "install apt packages (this can take some time)"
runcmd "sudo /usr/bin/apt-get install --yes -qq -o Dpkg::Options::=--force-confdef --ignore-missing \
ubuntu-restricted-extras \
apt-transport-https \
vlc \
gdebi \
gparted \
tlp \
language-pack-de-base \
libreoffice \
libreoffice-l10n-de \
libreoffice-help-de \
libreoffice-help-en-gb \
libreoffice-l10n-en-gb \
firefox \
firefox-locale-de \
firefox-locale-en \
thunderbird \
thunderbird-locale-de \
thunderbird-locale-en \
thunderbird-locale-en-gb \
gimp \
anydesk \
libqt5qml5 \
language-selector-common \
openoffice.org-hyphenation \
language-pack-kde-ar \
language-pack-kde-uk \
language-pack-kde-ru \
language-pack-kde-fa"
printok "install apt packages"
}
system_install_edubuntu() {
set -o pipefail
trap ErrorHandling ERR INT
# Edubuntu packages (official support until 18.04) https://wiki.ubuntuusers.de/Edubuntu_Programme
printprog "install Edubuntu packages (this can take some time)"
runcmd "sudo /usr/bin/apt-get install --yes -qq -o Dpkg::Options::=--force-confdef --ignore-missing \
blinken \
calibre \
cantor \
chemtool \
dia \
einstein \
fritzing \
gamine \
gcompris \
goldendict \
inkscape \
kalgebra \
kalzium \
kanagram \
kbruch \
kgeography \
khangman \
kig \
klettres \
kmplot \
kstars \
ktouch \
ktuberling \
kturtle \
kwordquiz \
laby \
lightspeed \
lybniz \
marble \
melting \
parley \
pencil2d \
ri-li \
rocs \
step \
tuxmath \
tuxpaint \
tuxtype \
yorick"
printok "install Edubuntu packages"
}
system_clean() {
set -o pipefail
trap ErrorHandling ERR INT
printprog "clean up apt cache and packages"
runcmd "sudo /usr/bin/apt-get autoremove --yes"
printok "clean up apt cache and packages"
}
system_configure() {
set -o pipefail
trap ErrorHandling ERR INT
printprog "set timezone"
runcmd "timedatectl set-timezone Europe/Berlin"
printok "set timezone"
}
system_files_download() {
set -o pipefail
trap ErrorHandling ERR INT
printprog ""
runcmd ""
printok ""
}
system_modify_dm() {
set -o pipefail
trap ErrorHandling ERR INT
# TODO: detect dm of device and choose modification
# cat /etc/X11/default-display-manager > /usr/sbin/gdm3 (Gnome Desktop Manager)
# goals to cover
# - Wallpaper
# -
printprog ""
runcmd ""
printok ""
}
system_checkinfo() {
set -o pipefail
trap ErrorHandling ERR INT
# this part checks if the device is already registered in snipe-IT
# is already registered it returns 1
# this is a new device it returns 0
if [ ! -f $sysinfofile ]; then
printprog "write systeminfo to $sysinfofile"
runcmd "mkdir /home/$current_user/.config/angestoepselt && /bin/bash /usr/local/bin/angestoepselt-info > $sysinfofile"
printok "write systeminfo to $sysinfofile"
fi
printprog "read systeminfo"
runcmd "jq -e . $sysinfofile >/dev/null 2>&1"
printok "read systeminfo"
HW_ADDRESS=$(cat $sysinfofile | jq -r .HW_ADDRESS)
ASSET_TAG=$(cat $sysinfofile | jq -r .Asset_Tag)
## start localcheck
if [[ $ASSET_TAG =~ ^[0-9]+$ ]]; then
printinfo "already in Snipe $ASSET_TAG"
return 1
elif
[ -f $SCRIPT_DIR/mac_white.list ] && grep -Fxq "$HW_ADDRESS" mac_white.list
then
unset HW_ADDRESS
printinfo "Device is using a charity owned USB Ethernet-Adapter"
return 1
## end localcheck
else
## start snipe-it check
printprog "check presence of snipe-IT API"
runcmd "curl -o /dev/null --silent https://$SNIPEIT_DOMAIN"
printok "check presence of snipe-IT API"
printinfo "fetch info from Snipe-IT"
result=$(curl --silent --request GET \
--url "https://$SNIPEIT_DOMAIN/api/v1/hardware?limit=1&search=${HW_ADDRESS//[:]/%3A}" \
--header 'accept: application/json' \
--header 'authorization: Bearer '$SNIPEIT_APIKEY'')
[[ $(echo $result | jq '(has("error"))') == true ]] && { printinfo "$(echo $result | jq .error)"; return 1; }
system_check_result=$(echo $result | jq .total) # doesn't work
asset_tag=$(echo $result | jq -r .rows[].asset_tag)
if [ -n "$asset_tag" ]; then
printinfo "Device already present in Snipe-IT: RE$asset_tag"
printprog "add ASSET_TAG to $sysinfofile"
runcmd "jq '. |= . + {"Asset_Tag": \"'${asset_tag}'\"}' $sysinfofile > $random_tmpdir/systeminfo.json.tmp && mv $random_tmpdir/systeminfo.json.tmp $sysinfofile"
printok "add ASSET_TAG to $sysinfofile"
else
printok "Device not in Snipe-IT"
system_checkinfo_value="newdevice"
return 0
fi
## end snipe-it check
fi
}
system_register() {
set -o pipefail
trap ErrorHandling ERR INT
if [[ "$system_checkinfo_value" != "newdevice" ]]; then
printskip "register Device at Snipe-IT"
fi
# is snipe-IT reachable?
printprog "check presence of snipe-IT API"
runcmd "curl -o /dev/null --silent https://$SNIPEIT_DOMAIN"
printok "check presence of snipe-IT API"
# convert json to variables, like key=value
eval "$(jq -r '. | to_entries | .[] | .key + "=" + (.value | @sh)'< $sysinfofile)"
printinfo "fetching variables for register $Platform at Snipe-IT"
# get platform. at the moment this looks for a laptop in string. If ist not a laptop it is a desktop PC (model 2)
[[ "$Platform" == *"laptop"* ]] && { model=1; display="$Displaysize @ $Resolution"; } || model=2
# clean up anydesk variable. > room for improvement, only integer should be in the var. This can be handled in the angestoepselt-info script
[[ "$Anydesk_ID" == *"SERVICE_NOT_RUNNING"* ]] && unset Anydesk_ID
# cannont locate issue with true and false, simple workaround to fix it
[[ "$Optical_Drive" == "FALSE" ]] && Optical_Drive=nein || Optical_Drive=ja
# change this variables used in this post to your needs, read the README of this repo and read ahead in the Snipe-IT API docs for more Informations
# https://snipe-it.readme.io/reference/updating-custom-fields
post_data()
{
cat <<EOF
{
"status_id": "2",
"name": "$Model",
"model_id": "$model",
"serial": "$serialno",
"_snipeit_mac_address_1": "$HW_ADDRESS",
"_snipeit_betriebssystem_2": "$Distro",
"_snipeit_festplatte_4": "$HDD",
"_snipeit_prozessor_5": "$CPU",
"_snipeit_arbeitsspeicher_6": "$Memory",
"_snipeit_optisches_laufwerk_7": "$Optical_Drive",
"_snipeit_display_8": "$display",
"_snipeit_akku_9": "$Battery_life",
"_snipeit_anydeskid_10": "$Anydesk_ID"
}
EOF
}
printprog "register $Platform at Snipe-IT"
post_result=$(curl --silent --request POST \
--url 'https://'$SNIPEIT_DOMAIN'/api/v1/hardware' \
--header 'accept: application/json' \
--header 'authorization: Bearer '$SNIPEIT_APIKEY'' \
--header 'Content-Type: application/json' \
--data "$(post_data)")
post_status=$(echo $post_result |jq -r .status)
post_message=$(echo $post_result |jq -r .message)
asset_tag=$(echo $post_result |jq -r .payload.asset_tag)
if [ "$post_status" = "success" ]; then
printok "register $Platform at Snipe-IT"
printprog "add PC to $sysinfofile"
runcmd "jq '. |= . + {"Asset_Tag": \"'${asset_tag}'\"}' $sysinfofile > $random_tmpdir/systeminfo.json.tmp && mv $random_tmpdir/systeminfo.json.tmp $sysinfofile"
printok "add PC to $sysinfofile"
else
printfail "$post_message"
fi
}
script_check_root() {
set -o pipefail
trap ErrorHandling ERR INT
printprog "check root privileges"
runcmd "[ $(id -u) -eq 0 ]"
printok "check root privileges"
}
script_check_snipesecrets() {
set -o pipefail
trap ErrorHandling ERR INT
printprog "check .secrets.env"
runcmd "[ -f $SCRIPT_DIR/.secrets.env ] && export $(cat $SCRIPT_DIR/.secrets.env)"
printok "check .secrets.env"
}
script_prerequisites() {
set -o pipefail
trap ErrorHandling ERR INT
printinfo "install requirements"
printprog "install required packages"
runcmd "sudo /usr/bin/apt-get -qq --yes install jq moreutils dialog html2ps python3 python3-pip curl wkhtmltopdf"
printok "install required packages"
printprog "install angestoepselt-info"
runcmd "sudo /usr/bin/wget quiet $REPO/raw/branch/master/angestoepselt-info -O /usr/local/bin/angestoepselt-info && sudo chmod +x /usr/local/bin/angestoepselt-info"
printok "install angestoepselt-info"
}
usage() {
printf "%s" "\
Usage: $0 --option
Options:
INFO:
-h|--help print this info
-v|--version show version of script
-a|--all running all task below
-u|--update perform an basic system update
-i|--install install all packages for this system
-r|--register Register PC in snipe-IT Asset Management
-c|--configure running basic system config settings
-d|--files_download download useful files to your HomeDirectory
-m|--modify_dm paint your Desktop Manager with angestoepselt design
--checkinfo Check if PC is already in snipe-IT present
--clean clean up trash, like files and from system tasks like packagemanger
"
exit 1
}
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
;;
-u|--update)
script_check_root
system_update
shift
shift
;;
-i|--install)
script_check_root
system_install_base
system_install_edubuntu
shift
shift
;;
--clean)
script_check_root
system_clean
shift
shift
;;
--charity)
charity=${OPTARG:=angestoepselt}
shift
shift
;;
-c|--configure)
script_check_root
system_configure
shift
shift
;;
-d|--files_download)
system_files_download
shift
shift
;;
-m|--modify_dm)
script_check_root
system_modify_dm
shift
shift
;;
--checkinfo)
script_check_snipesecrets
system_checkinfo
shift
shift
;;
-r|--register)
script_check_snipesecrets
system_register
shift
shift
;;
-a|--all)
script_check_root
system_update
system_install
system_clean
system_configure
system_files_download
system_modify_dm
script_check_snipesecrets
system_checkinfo
system_register
exit 0
;;
-v|--version)
printf '%s\n' "$0 v$version"
exit 1
;;
-*|--*)
echo "Unknown option $1"
usage
exit 1
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
if [[ -n $1 ]]; then
echo "Last line of file specified as non-opt/last argument:"
tail -1 "$1"
fi

View file

@ -1,166 +0,0 @@
#!/bin/bash
# Script for hardware post on snipeIT and print label by angestöpselt e.V.
# v0.4
export $(curl --silent 10.200.4.10/.env | xargs)
echo '#####################################'
echo '####### exec post_hardware.sh #######'
echo '#####################################'
echo "looking for anydesk source list" >> ${LOG}
if [ -f etc/apt/sources.list.d/anydesk-stable.list ] ; then
echo "Anydesk source.list besteht" >> ${LOG}
else
sudo wget -qO - https://keys.anydesk.com/repos/DEB-GPG-KEY | apt-key add -
sudo echo "deb http://deb.anydesk.com/ all main" > /etc/apt/sources.list.d/anydesk-stable.list
fi
echo "update apt cache" >> ${LOG}
sudo apt-get update -qq
echo "install dependencies" >> ${LOG}
sudo apt-get install -y -qq jq curl wkhtmltopdf zenity anydesk
# prepare for publishing computer to snipe-IT
category=`hostnamectl | grep Chassis | cut -c 21-`
#hdd=`cat "/sys/class/block/sda/size" | awk '{print $1 / 1024 / 1024 }' | awk -F "." '{print $1}'`
hdd=$(lsblk -dno SIZE --fs /dev/sda | awk -F "," '{print $1}')
memory=`(grep MemTotal /proc/meminfo | awk '{print int($2 / 1000000 + 0.5)}')`
os=`hostname | awk -F- '{print $1}'`
version=`cat /etc/os-release | grep VERSION_ID | grep -oP '(?<=").*(?=")'`
cpu=`cat /proc/cpuinfo | grep 'model name' | uniq | cut -c14-39 | sed -e 's/([^()]*)//g' | tr -d ' ' | cut -c -12`
mac=`ip link | sed -n "/BROADCAST.*state UP/{n;p}" | tail -1 | tr -s " " | cut -d" " -f3`
anydeskid=`anydesk --get-id`
serialno=`sudo dmidecode -s system-serial-number`
# name is for a nice label with some data
name=`echo $os/$version/$cpu/$memory/$hdd`
# Check the category for required field: model_id and fill laptop variables
if [[ $category = "laptop" || $category = "notebook" ]]; then
model=2
battery=`acpi -V | grep '^Battery.*%$' | tail -c 4`
display=`xrandr | awk '/ connected/{print sqrt( ($(NF-2)/10)^2 + ($NF/10)^2 )/2.54}' | cut -c -2 | head -n 1`
else
model=1
fi
# Check if an optical drive is available
if [[ $(lsblk | grep sr0) ]]; then
odd=ja
else
odd=nein
fi
# check, based on mac address
curl --request GET \
--url 'https://orga.z31.it/api/v1/hardware?limit=5&search='$mac'' \
--header 'accept: application/json' \
--header 'authorization: Bearer '$API_KEY'' \
--header 'content-type: application/x-www-form-urlencoded' > $HOME/check_result.json
# look for asset tag in payload
asset_tag=$(jq -j .rows[].asset_tag $HOME/check_result.json)
if [ ! -z ${asset_tag} ]
then
zenity --info --text "Der Rechner ist ist bereits eingetragen RE${asset_tag}" --width=500 --height=200
#echo " Der Rechner ist bereits eingetragen RE${asset_tag}"
exit 0
else
echo "preparing asset data in json"
post_data()
{
cat <<EOF
{
"status_id": "2",
"name": "$name",
"model_id": "$model",
"serial": "$serialno",
"_snipeit_mac_address_1": "$mac",
"_snipeit_betriebssystem_2": "$os$version",
"_snipeit_festplatte_4": "$hddGB",
"_snipeit_prozessor_5": "$cpu",
"_snipeit_arbeitsspeicher_6": "$memory",
"_snipeit_optisches_laufwerk_7": "$odd",
"_snipeit_display_8": "$display",
"_snipeit_akku_9": "$battery",
"_snipeit_anydeskid_10": "$anydeskid"
}
EOF
}
echo "do curl request to snipe-IT"
curl --request POST \
--url https://orga.z31.it/api/v1/hardware \
--header 'accept: application/json' \
--header 'authorization: Bearer '$API_KEY'' \
--header 'Content-Type: application/json' \
--data "$(post_data)" > $HOME/result.json
# get missing information from payload
asset_tag=$(cat $HOME/result.json | jq -r '.payload.asset_tag')
result_jq=$(cat $HOME/result.json | jq -r '.status')
erstellt_am=$(cat $HOME/result.json | jq -r '.payload.created_at')
echo "checking if the asset has been created and display an error from the snipeIT api" >> ${LOG}
for result in $result_jq
do
case $result in
success)
echo "${category} RE${asset_tag}, erstellt am ${erstellt_am}" >> ${LOG}
echo "MAC-Adresse ${mac} | Anydesk-ID ${anydeskid}" >> ${LOG}
touch "/home/computerspende/RE${asset_tag}"
echo "erstellt am: ${erstellt_am}" >> /home/computerspende/RE${asset_tag}
## This part is creating a file with html content for the printer
cat >/tmp/rechner.html <<EOL
<div>
<p style="float: left;"><img src="http://${PXESERVER_IP}/angestoepselt.png" height="80px" width="80px"></p>
</div>
<div>
<p style="font-size:15px">angestoepselt e.V.<br>Passwort: csw</p>
<b> <p style="font-size:23px"><b>RE${asset_tag}</p></b>
<p style="margin-left:5%; font-size:12px;">${name}</p>
</div>
EOL
exec 2>/dev/null # vernichtet nervige Fehlermeldungen
zenity --question --title "Label drucken" --text "\nEs wurde folgender PC eingetragen: \n\nRechnernr: $asset_tag \nTyp: $category \nOS: $os $version \nCPU: $cpu \nRAM: $memory GB \n\nWillst du jetzt das Label drucken?" --no-wrap --ok-label "Ja" --cancel-label "Nein"
if [ "$?" -eq "0" ]; then
## wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb
# parsing the variables into html file and make a nice looking pdf for printing
# page-heigt and page-width are 99012 Large Adress Labels, you can set this by default in cups Print Options with "Media Size"
wkhtmltopdf --page-height 36mm --page-width 89mm /tmp/rechner.html /tmp/rechner.pdf
echo 'Label drucken' >> ${LOG}
## change the ip of the printer host
# -P is the printer name of cups -> https://CUPS_SERVER_IP:631/printers/
lpr -H ${PRINTER_IP}:631 -P DYMO -o landscape /tmp/rechner.pdf
zenity --info --text="<big>Das ist der Rechner\n\n<big>RE$asset_tag</big></big>" --width=500 --height=200
echo 'Label wurde gedruckt' >> ${LOG}
else
echo "es wurde nichts gedruckt" >> ${LOG}
fi
;;
null)
zenity --info --text "Das hat nicht geklappt bitte führe das Script von Hand aus.\nBenutze bitte im Terminal:\n\ncurl -sSL pxe.z31.it/post_hardware.sh | bash" --width=500 --height=200
zenity --error --text "Fehlermeldung: \n\n$(cat $HOME/result.json | jq -r '.error')" --width=500 --height=200
;;
esac
done
fi
echo '#####################################'
echo '##### finished post_hardware.sh #####' >> ${LOG}
echo '#####################################'

23
scripts/scripts.txt Normal file
View file

@ -0,0 +1,23 @@
d-i preseed/late_command string \
in-target bash -c 'echo "\#!/bin/bash" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "\# Install and configure a Ubuntu-PC for the charity organization angestöpselt e.V. | angestoepselt.de" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "\# created by matthias of angestöpselt e.V." >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "\# created at 2022-02-17" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "version=0.2" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "set -o errexit" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "exec 100>/tmp/z31.lock || exit 1" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "flock 100 || exit 1" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "\# Speed up script by not using unicode." >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "LC_ALL=C" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "LANG=C" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "\# some static variables" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "DEBIAN_FRONTEND=noninteractive" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "current_user=\$\(id 1000 | awk -F '\[\(\)\]' '{print \$2}'\)" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "lockfile=/tmp/z31.lock" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "sysinfofile=\$HOME/.config/systeminfo.json" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "random_tmpdir=\$\(mktemp\)" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "SCRIPT_DIR=\"\$\(dirname \"\$0\"\)\"" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "TIME=\$\(date +%Y%m%d%H%M\)" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "LOGTIME=\$\(date \"+%Y-%m-%d %H:%M:%S\"\)" >> /home/computerspende/post_hardware.sh' \
in-target bash -c 'echo "LOGFILE=\"\$SCRIPT_DIR/\$TIME-z31.log\"" >> /home/computerspende/post_hardware.sh' \
in-target chmod +x /home/computerspende/late_command.sh

View file

@ -4,6 +4,7 @@ pkgs.mkShell {
buildInputs = [ buildInputs = [
pkgs.xorriso pkgs.xorriso
pkgs.p7zip pkgs.p7zip
pkgs.python3
]; ];
} }