<commit_before>
"""
Set up an ipython notebook profile for use over the web on GVL servers.

After running this script, change to your project directory and run

   ipython notebook --profile=nbserver

For security, you should not launch ipython notebook from your home directory, 
as it will have access to files in the directory tree it is run from.
Create a notebook or project directory and launch it from there.

This script will:
* Create a profile (nbserver) to use for the notebook server
* Configure password protection
* Configure HTTPS encryption
* Configure port settings
* Install the Table of Contents plugin
"""

##
# <NAME>, VLSCI, University of Melbourne
# Authored as part of the Genomics Virtual Laboratory project
##

# TODO: at end, tell user how to use
# TODO: add Table of Contents plugin
# TODO: set up readonly as well as interactive server

import os
import IPython.lib
import logging
import subprocess
import stat

profile_name = "nbserver"
interactive_port =  9510
readonly_port = 9560
interactive_location = "/ipython"

profile_config = \
"""
# Configuration file for ipython, intended for notebook server. 
# Generated by GVL setup script.

c = get_config()

# Kernel config
c.IPKernelApp.pylab = 'inline'  # plotting support by default

# Do not open local browser, just run as a server
c.NotebookApp.open_browser = False

# Require password access. 
c.NotebookApp.password = u'{<PASSWORD>}'

# Use a known port, which needs to be open in the security group
c.NotebookApp.port = {port}
c.NotebookApp.port_retries = 0

# Currently we are accessing the notebook directly via open ports.
# We need to do encryption and listen on external interfaces
c.NotebookApp.ip = '*'
c.NotebookApp.certfile = u'{certfile}'
c.NotebookApp.keyfile = u'{keyfile}'
"""

def main():
    """ The body of the script. """

    # Initialise logging to print info to screen
    logging.basicConfig(level=logging.INFO)

    # Get locations
    ipython_dir = IPython.utils.path.get_ipython_dir()
    nginx_dir = "/usr/nginx/conf"
    nginx_conf = os.path.join(nginx_dir, "nginx.conf")

    # Create the nbserver profile
    logging.info("Creating ipython profile for "+profile_name)
    profile_dir = os.path.join(ipython_dir, "profile_"+profile_name)
    run_cmd("ipython profile create "+profile_name)

    # Ask the user for a password; only store the hash
    logging.info("Configuring password")
    print "Enter a password to use for ipython notebook web access:"
    password_hash = IPython.lib.passwd()
    
    # Generate a self-signed certificate
    logging.info("Generating self-signed certificate for SSL encryption")
    certfile_tmp = "./instance_selfsigned_cert.pem.tmp"
    keyfile_tmp = "./instance_selfsigned_key.pem.tmp"
    certfile = os.path.join(profile_dir, "instance_selfsigned_cert.pem")
    keyfile = os.path.join(profile_dir, "instance_selfsigned_key.pem")
    run_cmd("yes '' | openssl req -x509 -nodes -days 3650 -newkey rsa:1024 -keyout "+keyfile_tmp+" -out "+certfile_tmp)
    run_cmd("sudo mv "+certfile_tmp+" "+certfile)
    run_cmd("sudo mv "+keyfile_tmp+" "+keyfile)
    run_cmd("sudo chmod 440 "+keyfile)
    
    # Overwrite the default profile config with ours
    config_file = os.path.join(profile_dir, "ipython_notebook_config.py")
    logging.info("Writing nbserver config file "+config_file)
    with open(config_file, 'wb') as f:
        f.write(profile_config.format(hash = password_hash, 
                                      port = interactive_port, 
                                      location = interactive_location,
                                      certfile = certfile,
                                      keyfile = keyfile))

def run_cmd(command):
    """ Run a shell command. """
    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return process.communicate()  

if __name__ == "__main__":
    main()
    <commit_msg>Added user instructions and IP address detection to notebook setup.<commit_after>
"""
Set up an ipython notebook profile for use over the web on GVL servers.

After running this script, change to your project directory and run

   ipython notebook --profile=nbserver

For security, you should not launch ipython notebook from your home directory, 
as it will have access to files in the directory tree it is run from.
Create a notebook or project directory and launch it from there.

This script will:
* Create a profile (nbserver) to use for the notebook server
* Configure password protection
* Configure HTTPS encryption
* Configure port settings
* Install the Table of Contents plugin
"""

##
# <NAME>, VLSCI, University of Melbourne
# Authored as part of the Genomics Virtual Laboratory project
##

# TODO: at end, tell user how to use
# TODO: add Table of Contents plugin
# TODO: set up readonly as well as interactive server

import os
import IPython.lib
import logging
import subprocess
import stat

profile_name = "nbserver"
interactive_port =  9510
readonly_port = 9560
interactive_location = "/ipython"

profile_config = \
"""
# Configuration file for ipython, intended for notebook server. 
# Generated by GVL setup script.

c = get_config()

# Kernel config
c.IPKernelApp.pylab = 'inline'  # plotting support by default

# Do not open local browser, just run as a server
c.NotebookApp.open_browser = False

# Require password access. 
c.NotebookApp.password = u'{<PASSWORD>}'

# Use a known port, which needs to be open in the security group
c.NotebookApp.port = {port}
c.NotebookApp.port_retries = 0

# Currently we are accessing the notebook directly via open ports.
# We need to do encryption and listen on external interfaces
c.NotebookApp.ip = '*'
c.NotebookApp.certfile = u'{certfile}'
c.NotebookApp.keyfile = u'{keyfile}'
"""

instruction_text =\
"""

================================================================================
ipython notebook is now configured to run as a server. To launch, change to your
working directory and run

    ipython notebook --profile=nbserver

Do not run this from your home directory as it will give the server access to 
all files. If you want the server to run while you are logged out, you may want 
to enter a screen session first by running `screen`. The next time you log in, 
you can reconnect to it using `screen -r`.

To access the running ipython notebook, point your browser to:

    https://{ip_address}:{port}/

Note the https in the URL! 
You will need the password you entered during setup.
================================================================================

"""

def main():
    """ The body of the script. """

    # Initialise logging to print info to screen
    logging.basicConfig(level=logging.INFO)

    # Get locations
    ipython_dir = IPython.utils.path.get_ipython_dir()
    nginx_dir = "/usr/nginx/conf"
    nginx_conf = os.path.join(nginx_dir, "nginx.conf")

    # Create the nbserver profile
    logging.info("Creating ipython profile for "+profile_name)
    profile_dir = os.path.join(ipython_dir, "profile_"+profile_name)
    run_cmd("ipython profile create "+profile_name)

    # Ask the user for a password; only store the hash
    logging.info("Configuring password")
    print "Enter a password to use for ipython notebook web access:"
    password_hash = IPython.lib.passwd()
    
    # Generate a self-signed certificate
    logging.info("Generating self-signed certificate for SSL encryption")
    certfile_tmp = "./instance_selfsigned_cert.pem.tmp"
    keyfile_tmp = "./instance_selfsigned_key.pem.tmp"
    certfile = os.path.join(profile_dir, "instance_selfsigned_cert.pem")
    keyfile = os.path.join(profile_dir, "instance_selfsigned_key.pem")
    run_cmd("yes '' | openssl req -x509 -nodes -days 3650 -newkey rsa:1024 -keyout "+keyfile_tmp+" -out "+certfile_tmp)
    run_cmd("sudo mv "+certfile_tmp+" "+certfile)
    run_cmd("sudo mv "+keyfile_tmp+" "+keyfile)
    run_cmd("sudo chmod 440 "+keyfile)
    
    # Overwrite the default profile config with ours
    config_file = os.path.join(profile_dir, "ipython_notebook_config.py")
    logging.info("Writing nbserver config file "+config_file)
    with open(config_file, 'wb') as f:
        f.write(profile_config.format(hash = password_hash, 
                                      port = interactive_port, 
                                      location = interactive_location,
                                      certfile = certfile,
                                      keyfile = keyfile))

    # Get our IP address and tell the user what to do
    ip_addr = cmd_output("ifconfig | grep -A 1 eth0 | grep inet | sed -nr 's/.*?addr:([0-9\\.]+).*/\\1/p'")
    print instruction_text.format(ip_address = ip_addr, port = interactive_port)

def cmd_output(command):
    """Run a shell command and get the standard output, ignoring stderr."""
    return run_cmd(command)[0].strip()

def run_cmd(command):
    """ Run a shell command. """
    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return process.communicate()  

if __name__ == "__main__":
    main()
    
{'fraction_non_alphanumeric': 0.1046282079403378, 'fraction_numerical': 0.005703005044966001, 'mean_word_length': 3.5595, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5360393', 'n_tokens_mistral': 2732, 'n_tokens_neox': 2624, 'n_words': 936}