1 Setup of the project

1.1 Getting the project files

After creating many projects using bookdown, i was ready to turn a project into a template. In order to turn a repository to a template, i went to GitHub, entered my project, chose Settings and checked Template repository. For a new project, i went to New repository and chose my template as Repository template. Another way to do the latter thing is to go to the main page of the template repository and click Use this template.

For projects inside an organisation, the project must be set to public in order to not having to use a password for cloning: Settings -> General -> Danger Zone -> Change visibility.

In RStudio Server, i created the new project: File -> New Project… -> Version Control -> Git, entered the HTTPS version of Uniform Resource Locator (URL) of my template repository for Repository URL and clicked Create Project.

RStudio Server doesn’t rename the project metadata file *.Rproj automatically. i did the rename manually and reopened the project: File -> Open Project….

1.2 Automated actions

For automation the user-based actions after the user has an account, i created a script that should be run if the manual set-up is too overwhelming:

~/rstudio-projects/create-project.sh --project <subdomain> --user <user>  # 1
                                                                          # 2

This is the content of that file written in BouShell Command Language[2] using[3]:

#!/bin/bash                                                                                                                                                                                     # 1
programname=$0                                                                                                                                                                                  # 2
                                                                                                                                                                                                # 3
usage() {                                                                                                                                                                                       # 4
    echo ""                                                                                                                                                                                     # 5
    echo "This program automates creating a new RStudio Server project."                                                                                                                        # 6
    echo ""                                                                                                                                                                                     # 7
    echo "usage: $programname --project string --user string[ --existing]"                                                                                                                      # 8
    echo ""                                                                                                                                                                                     # 9
    echo "  --project string   name of the project"                                                                                                                                            # 10
    echo "                          (example: loodusteaduste-klubi)"                                                                                                                           # 11
    echo "  --user string            the user in GitHub"                                                                                                                                       # 12
    echo "                          (example: Tome-Kit)"                                                                                                                                       # 13
    echo "  --existing         Is it an old project instead of a brand-new one?"                                                                                                               # 14
    echo ""                                                                                                                                                                                    # 15
}                                                                                                                                                                                              # 16
                                                                                                                                                                                               # 17
die() {                                                                                                                                                                                        # 18
    printf "Script failed: %s\n\n" "$1"                                                                                                                                                        # 19
    exit 1                                                                                                                                                                                     # 20
}                                                                                                                                                                                              # 21
                                                                                                                                                                                               # 22
while [ $# -gt 0 ]; do                                                                                                                                                                         # 23
    if [[ $1 == "--help" ]]; then                                                                                                                                                              # 24
        usage                                                                                                                                                                                  # 25
        exit 0                                                                                                                                                                                 # 26
    elif [[ $1 == "--"* ]]; then                                                                                                                                                               # 27
        v="${1/--/}"                                                                                                                                                                           # 28
        declare "$v"="$2"                                                                                                                                                                      # 29
    shift                                                                                                                                                                                          # 30
    fi                                                                                                                                                                                         # 31
    shift                                                                                                                                                                                      # 32
done                                                                                                                                                                                           # 33
                                                                                                                                                                                               # 34
if [[ -z $project ]]; then                                                                                                                                                                     # 35
    usage                                                                                                                                                                                      # 36
    die "The name of the project is missing."                                                                                                                                                  # 37
elif [[ -z $user ]]; then                                                                                                                                                                      # 38
    usage                                                                                                                                                                                      # 39
    die "The name of the user in GitHub is missing."                                                                                                                                           # 40
fi                                                                                                                                                                                             # 41
                                                                                                                                                                                               # 42
folder_of_project="/home/kalmer/rstudio-projects/$project"                                                                                                                                     # 43
git clone https://github.com/$user/$project $folder_of_project                                                                                                                                 # 44
                                                                                                                                                                                               # 45
tee ~/.ssh/github.com/$project > /dev/null <<EOF                                                                                                                                               # 46
Host github.com-$project                                                                                                                                                                       # 47
  HostName github.com                                                                                                                                                                          # 48
  User git                                                                                                                                                                                     # 49
  IdentityFile ~/.ssh/$project                                                                                                                                                                 # 50
EOF                                                                                                                                                                                            # 51
                                                                                                                                                                                               # 52
configuration_file_for_git="$folder_of_project/.git/config"                                                                                                                                    # 53
sed -i "s/https:\/\//git@/" $configuration_file_for_git                                                                                                                                        # 54
sed -i "s/github.com\//github.com-$project:/" $configuration_file_for_git                                                                                                                      # 55
                                                                                                                                                                                               # 56
if [[ ! -z $existing ]]; then                                                                                                                                                                  # 57
    ssh-keygen -N '' -f ~/.ssh/$project -q                                                                                                                                                     # 58
                                                                                                                                                                                               # 59
    ## Configuration of the content                                                                                                                                                            # 60
                                                                                                                                                                                               # 61
    name_of_original_project="manual-for-rstudio"                                                                                                                                              # 62
    sed_for_renaming_project="s/$name_of_original_project/$project/"                                                                                                                           # 63
                                                                                                                                                                                               # 64
    path_to_bookdown_yml="$folder_of_project/_bookdown.yml"                                                                                                                                    # 65
    echo $sed_for_renaming_project                                                                                                                                                             # 66
    sed -i $sed_for_renaming_project $path_to_bookdown_yml                                                                                                                                     # 67
                                                                                                                                                                                               # 68
    sed -i "/\"rmd\//d" $path_to_bookdown_yml                                                                                                                                                  # 69
    rm $folder_of_project/rmd/*                                                                                                                                                                # 70
    rm $folder_of_project/washing-cycles.csv                                                                                                                                                   # 71
                                                                                                                                                                                               # 72
    path_to_output_yml="$folder_of_project/_output.yml"                                                                                                                                        # 73
    sed -i "s/piiskop/$user/" $path_to_output_yml                                                                                                                                              # 74
    sed -i $sed_for_renaming_project $path_to_output_yml                                                                                                                                       # 75
                                                                                                                                                                                               # 76
    sed -i $sed_for_renaming_project $folder_of_project/js.js                                                                                                                                  # 77
fi                                                                                                                                                                                             # 78
                                                                                                                                                                                               # 79
## Web output                                                                                                                                                                                  # 80
                                                                                                                                                                                               # 81
configuration_file="/etc/nginx/sites-available/$project.ons.ee"                                                                                                                                # 82
sudo cp /etc/nginx/sites-available/manual-for-rstudio.ons.ee $configuration_file                                                                                                               # 83
sudo sed -i "s/manual-for-rstudio/$project/g" $configuration_file                                                                                                                              # 84
sudo ln -s $configuration_file "/etc/nginx/sites-enabled/$project.ons.ee"                                                                                                                      # 85
sudo systemctl reload nginx                                                                                                                                                                    # 86
                                                                                                                                                                                               # 87

On the line 64 of the previous script, there is sed command which can be used for deleting unnecessary lines[4]. The command on the particular line deletes all the lines from the file *_bookdown.yml* in the project folder that contain the text “rmd/” because the files in the folder rmd are project-specific.

1.3 User-based actions

The following actions must be performed for every editor of the repository.

For a new user:

sudo adduser <username>  # 1
                         # 2

If instead of adduser the command useradd would be used then the password and home folder must be set manually.

In order to push into the remote repository i need to create a key that can be used to unlock the lock to the repository in GitHub. i let to generate the key:

ssh-keygen  # 1
            # 2

This command first generates the following output:

Generating public/private rsa key pair.
Enter file in which to save the key (/home//.ssh/id_rsa):

As i have many projects in GitHub i need to have many keys as one key can’t be used for many projects. Therefore, i also write a new file name for the key:

/home/<username>/.ssh/<name-of-project>

If instead of the full path only the file name would be set then that file would be created into the home folder and for further actions with Secure Shell (SSH) its folder must be created manually.

The content of the newly created public key must be copied into GitHub for the named project as deploying the key by also allowing write access. i also have to inform my server about the connections of the newly created key by adding the following lines into ~/.ssh/config:

Host github.com-<name-of-project>
 HostName github.com
 User git
 IdentityFile ~/.ssh/<name-of-project>

So far, git has no information on the newly cloned project about it’s credentials. i need to tell it this information in order to be able to push my changes into the remote repository by changing the file .git/config inside my project’s folder by setting a new value for url:

url = git@github.com-<name-of-project>:<username-in-github>/<name-of-project>.git

From now on, it’s possible to edit the newly created project and push its changes into GitHub.

Inside RStudio Server, i want to make some changes right at the beginning before the first building as it’s a new project. One of them is to change the name of the output file in _bookdown.yml:

book_filename: "<name-of-project>" 

The same filename must be entered into js.js:

  createLink(div, "book-print", "<name-of-project>.pdf", "View printable book");  # 1
                                                                                  # 2
                                                                                  # 3

i might want to delete all the files inside rmd subfolder and their references in rmd_files:

rmd_files: [
  "index.Rmd",
  "references.Rmd"
]

Samuti tasub koheselt muuta faili README.md sisu projektile vastavaks.

i need to update the repository Uniform Resource Locator (URL) in _output.yml for bookdown::bs4_book:

repo: https://github.com/<username-in-github>/<name-of-project>