Add shared library: lib/ with modular bash helpers#237
Open
jash90 wants to merge 1 commit into
Open
Conversation
Introduces lib/noobs_lib.sh as the main entry point and 16 focused modules (each under 200 lines) covering: messages, permissions, packages, repos, services, ui, utils, files, config, users, errors, mysql, php, apache, nginx, systemd. Security fixes applied (from code review): - escape sed metacharacters in config/php helpers - use --defaults-file temp file instead of -p for mysql passwords - validate nginx block_name against path traversal - fix is_port_free false positive when ss/netstat missing - propagate module load failures with || exit 1 - replace eval with argv-based run_or_die - use sudo tee for writes to system directories - anchor grep patterns in sshd_config helpers Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is this?
lib/is a shared helper library for scripts in this repository. Instead of copy-pasting the same boilerplate (root check, package installation, service management, colored output) into every script — justsourceit once.How to use
Add at the top of your script:
What's inside
messages.shmsg_info,msg_ok,msg_error,msg_warn,msg_debug— colored outputpermissions.shrequire_root,check_root,command_exists,is_port_free,user_existspackages.shpkg_install,pkg_remove,pkg_update,pkg_is_installed,detect_package_managerservices.shservice_start/stop/restart/reload/enable/disable,service_exists,service_is_activerepos.shimport_gpg_key,add_ppa_repo,add_repository_with_keyusers.shcreate_web_user,create_system_user,set_web_permissionsconfig.shconfig_set_value,config_append_if_missing,config_remove_linemysql.shmysql_query,mysql_create_db_user,mysql_drop_db_userphp.shphp_install_packages,php_configure,php_fpm_create_poolapache.shapache_create_vhost,apache_create_alias,webserver_enable/disable_sitenginx.shnginx_create_server_block(generic / drupal / moodle / wordpress)systemd.shcreate_systemd_service,create_systemd_timer,delete_systemd_serviceerrors.shdie,run_or_die,trap_error,show_help_templatefiles.shbackup_file,create_temp_dir,cleanup_tempui.shask_input,ask_password,ask_yes_no,show_progressutils.shgenerate_password,get_ip_address,url_encodeBefore and after
Before (without library):
After (with library):
Scripts that can be improved
Every script in
scripts/that repeats the patterns above can be simplified using this library:chce_LAMP.shrequire_root, apt package install, Apache/PHP-FPM setup →pkg_install,apache_create_vhost,php_fpm_create_poolchce_LEMP.shrequire_root, nginx + php install, vhost creation →pkg_install,nginx_create_server_blockchce_wordpress.shmysql_create_db_user,nginx_create_server_blockchce_drupal.shphp_install_packages,mysql_create_db_user,apache_create_vhostchce_moodle.shphp_configure,mysql_create_db_userchce_fail2ban.shrequire_root, package install, config editing →pkg_install,config_set_valuechce_dockera.shrequire_root, GPG repo import, package install →import_gpg_key,add_repository_with_key,pkg_installchce_mariadb_binary.shcreate_system_user,create_systemd_servicechce_postgresql.shadd_repository_with_key,pkg_installchce_wireguard.sh/chce_openvpn.shrequire_root,is_port_free,service_enable_nowchce_hardening.shconfig_set_value,config_append_if_missingchce_backup.shcreate_systemd_service,create_systemd_timerchce_ftp.shcreate_web_user(withchroot_ssh=true)Security
All modules were reviewed and hardened against common shell scripting vulnerabilities:
eval—run_or_dieuses argv array instead--defaults-file, never via-p(not visible inps)sedmetacharacters properly escaped when editing config filessudo teeused instead of direct writes to/etc/from unprivileged contextis_port_freereturns an error when neitherssnornetstatis available, instead of a false positive🤖 Generated with Claude Code