def copy_startup_script()

in community/front-end/ofe/website/ghpcfe/cluster_manager/workbenchinfo.py [0:0]


    def copy_startup_script(self):
        user = self.workbench.trusted_user

        # pylint: disable=line-too-long
        startup_script_vars = f"""
USER=$(curl -s http://metadata.google.internal/computeMetadata/v1/oslogin/users?pagesize=1024 \
            -H 'Metadata-Flavor: Google' | \
        jq '.[][] | \
        select ( .name == "{user.socialaccount_set.first().uid}") | \
        .posixAccounts | \
        .[].username' 2>&- | tr -d '"')
"""

        slurm_config_segment = ""
        try:
            cid = self.workbench.attached_cluster.cloud_id
            slurm_config_segment=f"""\
apt-get install -y munge libmunge-dev


mkdir -p /mnt/clustermunge
mkdir -p /etc/munge
mount slurm-{cid}-controller:/etc/munge /mnt/clustermunge
cp /mnt/clustermunge/munge.key /etc/munge/munge.key
chmod 400 /etc/munge/munge.key
chown munge:munge /etc/munge/munge.key
umount /mnt/clustermunge
rmdir /mnt/clustermunge
systemctl restart munge

useradd --system -u981 -U -m -d /var/lib/slurm -s /bin/bash slurm
echo "N" > /sys/module/nfs/parameters/nfs4_disable_idmapping

tmpdir=$(mktemp -d)
currdir=$PWD
cd $tmpdir
wget https://download.schedmd.com/slurm/slurm-21.08-latest.tar.bz2
tar xf slurm-21.08-latest.tar.bz2
cd slurm-21.08*/


#wget https://download.schedmd.com/slurm/slurm-22.05-latest.tar.bz2
#tar xf slurm-22.05-latest.tar.bz2
#cd slurm-22.05*/

./configure --prefix=/usr/local --sysconfdir=/etc/slurm
make -j $(nproc)
make install
# Throw an error if the slurm install fails
if [ "$?" -ne "0" ]; then
    echo "BRINGUP FAILED"
    exit 1
fi

cd $currdir
rm -r $tmpdir


mkdir -p /etc/slurm
mount slurm-{cid}-controller:/usr/local/etc/slurm /etc/slurm
"""
        except AttributeError:
            pass

        startup_script = self.workbench_dir / "startup_script.sh"
        with startup_script.open("w") as f:
            f.write(
                f"""#!/bin/bash
echo "starting startup script at `date`" | tee -a /tmp/startup.log
echo "Getting username..." | tee -a /tmp/startup.log
{startup_script_vars}

echo "Setting up storage" | tee -a /tmp/startup.log
sudo apt-get -y update && sudo apt-get install -y nfs-common

{slurm_config_segment}

mkdir /tmp/jupyterhome
chown $USER:$USER /tmp/jupyterhome

mkdir /home/$USER
chown $USER:$USER /home/$USER

cp /home/jupyter/.jupyter /tmp/jupyterhome/.jupyter -R
chown $USER:$USER /tmp/jupyterhome/.jupyter -R

cat << EOF > /tmp/jupyterhome/DATA_LOSS_WARNING.txt
DATA LOSS WARNING:

The data on this workbench instance is not automatically saved unless it is
saved in a shared filesystem that has been mounted.

All mounted shared filesystems are listed below. If none are listed then
all data on this instance will be deleted.

MOUNTED FILESYSTEMS:

EOF

"""
            )



            for mp in self.workbench.mount_points.order_by("mount_order"):
                if (
                    self.workbench.id == mp.workbench.id
                    and mp.export.filesystem.hostname_or_ip
                ):
                    f.write("mkdir -p " + mp.mount_path + "\n")
                    f.write(
                        "mkdir -p /tmp/jupyterhome`dirname "
                        + mp.mount_path
                        + "`\n"
                    )
                    f.write(
                        "mount "
                        + mp.export.filesystem.hostname_or_ip
                        + ":"
                        + mp.export.export_name
                        + " "
                        + mp.mount_path
                        + "\n"
                    )
                    f.write("chmod 777 " + mp.mount_path + "\n")
                    f.write(
                        "ln -s "
                        + mp.mount_path
                        + " /tmp/jupyterhome`dirname "
                        + mp.mount_path
                        + "` \n"
                    )
                    f.write(
                        'echo "'
                        + mp.export.filesystem.hostname_or_ip
                        + ":"
                        + mp.export.export_name
                        + " is mounted at "
                        + mp.mount_path
                        + '" >> /tmp/jupyterhome/DATA_LOSS_WARNING.txt\n'
                    )

            logger.debug("Writing workbench startup script")
            with open(
                self.config["baseDir"]
                / "infrastructure_files"
                / "gcs_bucket"
                / "workbench"
                / "startup_script_template.sh",
                encoding="utf-8",
            ) as infile:
                for line in infile:
                    print(line)
                    f.write(line)

                f.write("\n")