dev-setup

by tyuan
8 deployments · 6 still active · last rev. 11 days ago

set up development env: c++/node.js/jekyll.



Compatible with: Ubuntu 17.04
						#!/bin/bash

# <UDF name="host_name" Label="Host Name" default="dev" />
# <UDF name="user_name" Label="User Name" default="thomas" />
# <UDF name="user_password" Label="User Password" />
# <UDF name="user_public_key" Label="User Public Key" default="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCx8L5Lt1/+UN2V+nbE8Zh04/pjR3fq+mlvEDjAYOw0PZ5APp3mXqBHS6ywowBJu+0jh0dAiHDMUjj6PLmVQL3mcZr2tyfTMYxYwmcOiLmtPo3Sj7qFK797+gPnYPMVkFapVJklPekdkogt4aW0CZD/htsW/eKkG+vQih4OThem5LhGfbKjbss72mr8veGAPA8Opi7wRmIXm1AyHniB1kfc0+bjdKtoxPvXOvJGtmnu6nQOURKsk+7cMTlWpVjMAAO7CsDyOM9PNs0H7fzwAIbMoT6XsVQTWSvofGaE9z8xM3iqQ+RZAeYbmqn4xlEHHhRMl4m+xBvV72S7xF6m4QwH thomas@cppcoder.net" />

function system_set_hostname {
    # $1 - The hostname to define
    HOSTNAME="$1"
        
    if [ ! -n "$HOSTNAME" ]; then
        echo "Hostname undefined"
        return 1;
    fi
    
    echo "$HOSTNAME" > /etc/hostname
    hostname -F /etc/hostname
}

###########################################################
# Users and Authentication
###########################################################

function user_add_sudo {
    # Installs sudo if needed and creates a user in the sudo group.
    #
    # $1 - Required - username
    # $2 - Required - password
    USERNAME="$1"
    USERPASS="$2"

    if [ ! -n "$USERNAME" ] || [ ! -n "$USERPASS" ]; then
        echo "No new username and/or password entered"
        return 1;
    fi
    
    aptitude -y install sudo
    adduser $USERNAME --disabled-password --gecos ""
    echo "$USERNAME:$USERPASS" | chpasswd
    usermod -aG sudo $USERNAME
}

function user_add_pubkey {
    # Adds the users public key to authorized_keys for the specified user. Make sure you wrap your input variables in double quotes, or the key may not load properly.
    #
    #
    # $1 - Required - username
    # $2 - Required - public key
    USERNAME="$1"
    USERPUBKEY="$2"
    
    if [ ! -n "$USERNAME" ] || [ ! -n "$USERPUBKEY" ]; then
        echo "Must provide a username and the location of a pubkey"
        return 1;
    fi
    
    if [ "$USERNAME" == "root" ]; then
        mkdir /root/.ssh
        echo "$USERPUBKEY" >> /root/.ssh/authorized_keys
        return 1;
    fi
    
    mkdir -p /home/$USERNAME/.ssh
    echo "$USERPUBKEY" >> /home/$USERNAME/.ssh/authorized_keys
    chown -R "$USERNAME":"$USERNAME" /home/$USERNAME/.ssh
}

function ssh_disable_root_and_password {
    # Disables root SSH access.
    sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
    sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
    touch /tmp/restart-ssh
}

###########################################################
# Other niceties!
###########################################################

function goodstuff {
    # Installs the REAL vim, wget, less, and enables color root prompt and the "ll" list long alias

    aptitude -y install wget vim less
    sed -i -e 's/^#PS1=/PS1=/' /root/.bashrc # enable the colorful root bash prompt
    sed -i -e "s/^#alias ll='ls -l'/alias ll='ls -al'/" /root/.bashrc # enable ll list long alias <3
}


###########################################################
# utility functions
###########################################################

function restartServices {
    # restarts services that have a file in /tmp/needs-restart/

    for service in $(ls /tmp/restart-* | cut -d- -f2-10); do
        /etc/init.d/$service restart
        rm -f /tmp/restart-$service
    done
}


# set host name
system_set_hostname "$HOST_NAME"

# setup timezone to Toronto
ln -sf /usr/share/zoneinfo/America/Toronto /etc/localtime

# add user thomas
user_add_sudo "$USER_NAME" "$USER_PASSWORD"
user_add_pubkey "$USER_NAME" "$USER_PUBLIC_KEY"

ssh_disable_root_and_password
goodstuff
restartServices

# install latest node.js
apt-get install python-software-properties -y
curl -sL https://deb.nodesource.com/setup_7.x
apt-get install nodejs -y

# install Jekyll
apt-get install git gcc ruby ruby-dev make autoconf ruby -y
gem install jekyll