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.

124 lines
3.5KB

  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. printf("need a non-root user...\n");
  30. printf("want to create a user? Y/N\n");
  31. unless(<STDIN> eq "Y\n"){
  32. die "create a non-root user & set user passsword before running this script.";
  33. }
  34. printf("Username: ");
  35. $admin_un = <STDIN>;
  36. chomp $admin_un;
  37. `useradd -m $admin_un`;
  38. }
  39. $admin_home_dir = "/home/$admin_un";
  40. # grant doas access to admin user
  41. system("echo 'permit $admin_un' > /etc/doas.conf");
  42. # install git
  43. system("pkg_add git");
  44. # Setup install dir
  45. system("mkdir $INST_DIR");
  46. system("useradd -d $INST_DIR -r 100..900 $SVC_ACCT");
  47. system("chown $SVC_ACCT:$SVC_ACCT $INST_DIR");
  48. chdir $INST_DIR;
  49. # clone repo
  50. system("su $SVC_ACCT -c 'git clone $GIT_REPO'");
  51. chdir $REPO_DIR;
  52. # Copy the skel directory
  53. system("mkdir ./skel/public_html/cgi");
  54. system("cp -r ./skel/* /etc/skel/");
  55. # setup admin user
  56. system("cp -r ./skel/* /home/$admin_un/");
  57. system("chown -R $admin_un:$admin_un /home/$admin_un");
  58. system("echo $admin_un >> ./user_list.txt");
  59. # Setup the virtual environment
  60. system("pkg_add python3");
  61. printf("generating virtual enviornment...\n");
  62. system("su $SVC_ACCT -c 'python3 -m venv venv'");
  63. system("su $SVC_ACCT -c '. ./venv/bin/activate && pip3 install -r requirements.txt'");
  64. system("cp ./perl-script/conf/lingyin.rc /etc/rc.d/lingyind");
  65. system("chmod 755 /etc/rc.d/lingyind");
  66. system("rcctl enable lingyind");
  67. system("rcctl start lingyind");
  68. system("pkg_add p5-JSON");
  69. # Install apache
  70. system("pkg_add apache-httpd");
  71. printf("configuring apache\n");
  72. # enable the userdir module
  73. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule userdir_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  74. system("sed -i -e 's/^\\(.\\)*#\\(Include \\/etc\\/apache2\\/extra\\/httpd-userdir.conf\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  75. # Enable the CGI directory
  76. system("echo '<Directory \"/home/*/public_html/cgi/\">
  77. Require all granted
  78. Options +ExecCGI
  79. AddHandler cgi-script .cgi
  80. </Directory>' >> /etc/apache2/extra/httpd-userdir.conf");
  81. # Enable the CGI modules
  82. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule cgi_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  83. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule cgid_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  84. # Disable directory listing
  85. system("sed -i -e 's/\\(<\\/Directory>\\)/ Options -Indexes\\
  86. \\1/g' /etc/apache2/extra/httpd-userdir.conf");
  87. # Change the port to 5001
  88. system("sed -i -e 's/^\\(.\\)*Listen *80/\\1Listen 5001/' /etc/apache2/httpd2.conf");
  89. # rev up those apache processes!
  90. system("rcctl enable apache2");
  91. system("rcctl start apache2");
  92. # Install and config haproxy
  93. system("pkg_add haproxy");
  94. printf("configuring haproxy\n");
  95. system("cp ./perl-script/conf/haproxy.cfg /etc/haproxy/haproxy.cfg");
  96. system("rcctl enable haproxy");
  97. system("rcctl start haproxy");
  98. printf("dont forget to setup your ssh pub key at /home/$admin_un/.ssh/authorized_keys\n");