# Local setup on ubuntu To get the app running locally following things need to be set. ### Install dependencies 1. install [rvm](https://github.com/rvm/ubuntu_rvm): ```bash sudo apt-get install software-properties-common sudo apt-add-repository -y ppa:rael-gc/rvm sudo apt-get update sudo apt-get install rvm echo 'source "/etc/profile.d/rvm.sh"' >> ~/.bashrc ``` 2. reboot after rvm install 3. install correct ruby version: `rvm install ruby-2.6.3` 4. install rails: `gem install rails` 5. clone autograder repository:`git clone git@github.com:user/project.git` 5. Install postgresql: `sudo apt install postgresql-contrib libpq-dev` 7. Install gems. In the directory where project was cloned run: `bundle install` ### Setup the database 1. Create new postgres user: ``` sudo -u postgres -i createuser --pwprompt tdd_lti ``` Username must be "tdd_lti" and also when asked for password use "tdd_lti" 2. Add "Create DB" role for tdd_lti user: ``` psql ALTER USER tdd_lti CREATEDB; ``` 3. Check location for pg_hba.conf file: ``` SHOW hba_file; ``` 4. Change authentication from the hba_file: Change the line: ``` local all all peer ``` to: ``` local all all md5 ``` make sure to restart database ``` sudo service postgresql restart ``` 5. Setup the database: Make sure `config/database.yml` file has both username, and password setup as "tdd_lti" on all modes(development, test, production) and then run: ``` rails db:setup rails db:migrate ``` ### Start the app To start the rails server run: ``` rails s ``` App should now be running at localhost:3000 --- # Deploying app to the server with capistrano ## Setup the server With SSH connection to the server set the following things. Also note that the user is "deploy" in this case. ### Install dependencies 1. nodeJS and redis: ``` curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list sudo add-apt-repository ppa:chris-lea/redis-server sudo apt-get update sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev dirmngr gnupg apt-transport-https ca-certificates redis-server redis-tools nodejs yarn ``` 2. rvm and ruby-2.6.3: Install them in the same way as for the local app [(steps 1-3)](#Local-setup-on-ubuntu). 3. Install bundler: ``` gem install bundler ``` 4. Install NGINX: ``` sudo apt-get install curl git-core nginx -y ``` 5. install docker: ``` sudo apt-get update sudo apt-get remove docker docker-engine docker.io sudo apt install docker.io sudo chown $USER /var/run/docker.sock sudo systemctl start docker sudo systemctl enable docker ``` 6. install cypress dependencies: ``` sudo apt-get install libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb ``` ### setup postgresql 1. Create user the same way as for the local version, but also create a new postgres database: ``` sudo -u postgres -i createuser --pwprompt tdd_lti createdb -O tdd_lti tdd_lti ``` 2. Change the hba_file [in the same way](#Setup-the-database) as with local app ### Configure NGINX 1. Edit the default file: ``` sudo nano /etc/nginx/sites-available/default ``` replace everything with: ```less upstream app { server unix:/home/deploy/tdd_lti/shared/tmp/sockets/puma.sock fail_timeout=0; } server { listen 80; server_name localhost; root /home/deploy/tdd_lti/current/public; try_files $uri/index.html $uri @app; location @app { proxy_pass http://app; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; } error_page 500 502 503 504 /500.html; client_max_body_size 4G; keepalive_timeout 10; } ``` 2. start nginx: ``` sudo service nginx start ``` ## Setup on client ### Source code setup If the server is setup just like in this document, then the couple things need to be set from the source code. to *config/deploy.rb* file, set the repository where the to be deployed app is and also make sure the other 2 lines are in the file: ```vscode set :repo_url, 'git@github.com:user/project.git' set :linked_dirs, fetch(:linked_dirs, []) + %w{public/system} + %w{public/ckeditor_assets} + %w{public/images} + %w{public/videos} + %w{log tmp/backup tmp/pids tmp/cache tmp/sockets vendor/bundle} set :rvm_custom_path, "/usr/share/rvm" ``` to *config/deploy/production.rb* file, set the ip of the server and the deployment username. "deploy" is the user in this case: ```vscode role :app, %w{deploy@ip} role :web, %w{deploy@ip} role :db, %w{deploy@ip} set :deploy_to, '/home/deploy/tdd_lti' ``` To get the cypress test videos working with this setup, the video path from *views/lti/show.html.haml* and *views/submissions/show.html.haml* needs to be: ```vscode %source{src: "/videos/#{@sub.user_id}_#{@sub.assignment_id}.mp4", type:"video/mp4"} ``` ### Deploying the app from local machine to the server 1. Make sure the changes are committed to github 2. Also make sure that the gems of the app are installed, from the folder of the where the app is run: `bundle install` 3. To start deploying the app to the server run: ` cap production deploy ` # Creating admin user for the app to create user for local app open console with: `rails c` for the app running on the server open console with: `cap production rails:console` In the rails console make a new user and save it to the database: ```bash @user = User.new(:email => '123@gmail.com', :password => 'password', :password_confirmation => 'password', :activated => true, :confirmed_at => '2020-09-25 06:58:39.88024', :role => 1) @user.save ``` notice that `confirmed_at` can be any date, `email` can be any email, `activated` needs to be **true** and `role` needs to be **1**. Logging in with the added credentials should now work. # Adding new tests 1. Login to the app with the created user 2. Go to "My courses" and add new course 3. Add new assignment for the newly created course 4. Set name, cooldown time and the script as follows: * "./lib/sand_tester.sh" for Tic Tac Toe, Rahti assignment * "./lib/cypress_tester.sh" for all the other assignments 6. Add test repository to the assignment(repositorys can be found from: https://github.com/skayred?tab=repositories) # Adding the tests to moodle ### Tool URL Every assignment has its own tool URL that can be found like this: ![](https://i.imgur.com/Klkp8av.png) ### Secret LTI key Shared secret key needs to be set for adding the tool to moodle. To find this key SSH into the server and check the entry for the admin user: ``` sudo -u postgres -i psql \c tdd_lti SELECT * FROM users; ``` The field `secret` contains the LTI shared secret key. ### Configure external tool on moodle Add external tools for each of the tests at moodle by navigating to: `site administration -> plugins -> external tool -> manage tools -> configure a tool manually` Set the secret and tool URL also LTI version is set to **1.0/1.1** --- # Troubleshooting To view NGINX logs:`sudo less /var/log/nginx/error.log` Other log files are located at: `/home/deploy/tdd_lti/shared/log/`