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.

122 lines
3.7KB

  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. unless( ($admin_un) = `tail /etc/passwd | grep -v "nobody:"` =~ /([^:\n]+):[^:]+:[0-9]{4,}/){
  25. `wget --quiet https://git.lain.church/gashapwn/lyadmin/raw/branch/master/perl-script/create_user.pl -O create_admin.pl`;
  26. printf("run create_admin.pl to create an admin user\n");
  27. die "or add with useradd and add an ssh key to ~/.ssh/authorizedkeys\n";
  28. }
  29. # install git
  30. system("pkg_add git");
  31. # Setup install dir
  32. system("mkdir $INST_DIR");
  33. system("useradd -d $INST_DIR -r 100..900 $SVC_ACCT");
  34. system("chown $SVC_ACCT:$SVC_ACCT $INST_DIR");
  35. chdir $INST_DIR;
  36. # clone repo
  37. system("su $SVC_ACCT -c 'git clone $GIT_REPO'");
  38. chdir $REPO_DIR;
  39. # Copy the skel directory
  40. system("mkdir ./skel/public_html/cgi");
  41. system("cp -r ./skel/* /etc/skel/");
  42. # Check /etc/passwd for the username created during
  43. # installation
  44. if( ($admin_un) = `tail /etc/passwd | grep -v "nobody:"` =~ /([^:\n]+):[^:]+:[0-9]{4,}/){
  45. # grant doas access to admin user
  46. system("echo 'permit $admin_un' > /etc/doas.conf");
  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. }
  52. # Setup the virtual environment
  53. system("pkg_add python3");
  54. printf("generating virtual enviornment...\n");
  55. system("su $SVC_ACCT -c 'python3 -m venv venv'");
  56. system("su $SVC_ACCT -c '. ./venv/bin/activate && pip3 install -r requirements.txt'");
  57. system("cp ./perl-script/conf/lingyin.rc /etc/rc.d/lingyind");
  58. system("chmod 755 /etc/rc.d/lingyind");
  59. system("rcctl enable lingyind");
  60. system("rcctl start lingyind");
  61. system("pkg_add p5-JSON");
  62. # Install apache
  63. system("pkg_add apache-httpd");
  64. printf("configuring apache\n");
  65. # enable the userdir module
  66. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule userdir_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  67. system("sed -i -e 's/^\\(.\\)*#\\(Include \\/etc\\/apache2\\/extra\\/httpd-userdir.conf\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  68. # Enable the CGI directory
  69. system("echo '<Directory \"/home/*/public_html/cgi/\">
  70. Require all granted
  71. Options +ExecCGI
  72. AddHandler cgi-script .cgi
  73. </Directory>' >> /etc/apache2/extra/httpd-userdir.conf");
  74. # Enable the CGI modules
  75. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule cgi_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  76. system("sed -i -e 's/^\\(.\\)*#\\(LoadModule cgid_module\\)/\\1\\2/' /etc/apache2/httpd2.conf");
  77. # Disable directory listing
  78. system("sed -i -e 's/\\(<\\/Directory>\\)/ Options -Indexes\\
  79. \\1/g' /etc/apache2/extra/httpd-userdir.conf");
  80. # Change the port to 5001
  81. system("sed -i -e 's/^\\(.\\)*Listen *80/\\1Listen 5001/' /etc/apache2/httpd2.conf");
  82. # rev up those apache processes!
  83. system("rcctl enable apache2");
  84. system("rcctl start apache2");
  85. # Install and config haproxy
  86. system("pkg_add haproxy");
  87. printf("configuring haproxy\n");
  88. system("cp ./perl-script/conf/haproxy.cfg /etc/haproxy/haproxy.cfg");
  89. system("rcctl enable haproxy");
  90. system("rcctl start haproxy");
  91. printf("\n\nInstall complete\n");
  92. printf("==================================================\n");
  93. printf("Protip: use doas instead of sudoo\n");
  94. printf("dont forget\n\ncreate yourself an user with: useradd -m\n");
  95. printf("setup your ssh pub key at ~/.ssh/authorized_keys\n");