scripts and tools to administer the lingy.in public unix / tilde
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.3KB

  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. # provision.pl
  5. # script to provision a tilde instance
  6. #
  7. # This script is intended to be run on a fresh
  8. # OpenBSD install
  9. #
  10. # gashapwn
  11. # Nov 2020
  12. my $GIT_REPO = 'https://git.lain.church/gashapwn/lyadmin.git';
  13. my ($REPO_DIR) = $GIT_REPO =~ /\/([^\/]*)\.git$/;
  14. my $INST_DIR = "/tilde";
  15. my $SVC_ACCT = "_lingyind";
  16. my $pwuid;
  17. my $admin_un;
  18. my $admin_home_dir;
  19. # Make sure we're running as root
  20. $pwuid = getpwuid( $< );
  21. if($pwuid ne "root"){
  22. die "script must be run as root";
  23. }
  24. # Check /etc/passwd for the username created during
  25. # installation
  26. if( ($admin_un) = `tail /etc/passwd | grep -v "nobody:"` =~ /([^:\n]+):[^:]+:[0-9]{4,}/){
  27. printf("admin user will be set to %s\n", $admin_un);
  28. }else{
  29. die "create a non-root user & set user passsword before running this script."
  30. }
  31. $admin_home_dir = "/home/$admin_un";
  32. # grant doas access to admin user
  33. system("echo 'permit $admin_un' > /etc/doas.conf");
  34. # install git
  35. system("pkg_add git");
  36. # Setup install dir
  37. system("mkdir $INST_DIR");
  38. system("useradd -d $INST_DIR -r 100..900 $SVC_ACCT");
  39. system("chown $SVC_ACCT:$SVC_ACCT $INST_DIR");
  40. chdir $INST_DIR;
  41. # clone repo
  42. system("su $SVC_ACCT -c 'git clone $GIT_REPO'");
  43. chdir $REPO_DIR;
  44. # Copy the skel directory
  45. system("mkdir ./skel/public_html/cgi");
  46. system("cp -r ./skel/* /etc/skel/");
  47. # setup admin user
  48. system("cp -r ./skel/* /home/$admin_un/");
  49. system("chown -R $admin_un:$admin_un /home/$admin_un");
  50. system("echo $admin_un >> ./user_list.txt");
  51. # Setup the virtual environment
  52. system("pkg_add python3");
  53. printf("generating virtual enviornment...\n");
  54. system("su $SVC_ACCT -c 'python3 -m venv venv'");
  55. system("su $SVC_ACCT -c '. ./venv/bin/activate && pip3 install -r requirements.txt'");
  56. system("cp ./perl-script/conf/lingyin.rc /etc/rc.d/lingyind");
  57. system("chmod 755 /etc/rc.d/lingyind");
  58. system("rcctl enable lingyind");
  59. system("rcctl start lingyind");
  60. system("pkg_add p5-JSON");
  61. # Install apache
  62. system("pkg_add apache-httpd");
  63. printf("configuring apache\n");
  64. # enable the userdir module
  65. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule userdir_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  66. system("sed -i -e 's/^\\(.\\)*#\\(Include \\/etc\\/apache2\\/extra\\/httpd-userdir.conf\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  67. # Enable the CGI directory
  68. system("echo '<Directory \"/home/*/public_html/cgi/\">
  69. Require all granted
  70. Options +ExecCGI
  71. AddHandler cgi-script .cgi
  72. </Directory>' >> /etc/apache2/extra/httpd-userdir.conf");
  73. # Enable the CGI modules
  74. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule cgi_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  75. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule cgid_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  76. # Disable directory listing
  77. system("sed -i -e 's/\\(<\\/Directory>\\)/ Options -Indexes\\
  78. \\1/g' /etc/apache2/extra/httpd-userdir.conf");
  79. # Change the port to 5001
  80. system("sed -i -e 's/^\\(.\\)*Listen *80/\\1Listen 5001/' /etc/apache2/httpd2.conf");
  81. # rev up those apache processes!
  82. system("rcctl enable apache2");
  83. system("rcctl start apache2");
  84. # Install and config haproxy
  85. system("pkg_add haproxy");
  86. printf("configuring haproxy\n");
  87. system("cp ./perl-script/conf/haproxy.cfg /etc/haproxy/haproxy.cfg");
  88. system("rcctl enable haproxy");
  89. system("rcctl start haproxy");
  90. printf("dont forget to setup your ssh pub key at /home/$admin_un/.ssh/authorized_keys\n");