Skip to content

Commit 04f1ed5

Browse files
committed
Initial commit
0 parents  commit 04f1ed5

18 files changed

+582
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
core.Microsoft*
2+
core.mongo*
3+
core.python*
4+
env.py
5+
__pycache__/
6+
*.py[cod]
7+
node_modules/
8+
.github/

.gitpod.dockerfile

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
FROM gitpod/workspace-base:latest
2+
3+
RUN echo "CI version from base"
4+
5+
### NodeJS ###
6+
USER gitpod
7+
ENV NODE_VERSION=16.13.0
8+
ENV TRIGGER_REBUILD=1
9+
RUN curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | PROFILE=/dev/null bash \
10+
&& bash -c ". .nvm/nvm.sh \
11+
&& nvm install $NODE_VERSION \
12+
&& nvm alias default $NODE_VERSION \
13+
&& npm install -g typescript yarn node-gyp" \
14+
&& echo ". ~/.nvm/nvm.sh" >> /home/gitpod/.bashrc.d/50-node
15+
ENV PATH=$PATH:/home/gitpod/.nvm/versions/node/v${NODE_VERSION}/bin
16+
17+
### Python ###
18+
USER gitpod
19+
RUN sudo install-packages python3-pip
20+
21+
ENV PATH=$HOME/.pyenv/bin:$HOME/.pyenv/shims:$PATH
22+
RUN curl -fsSL https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash \
23+
&& { echo; \
24+
echo 'eval "$(pyenv init -)"'; \
25+
echo 'eval "$(pyenv virtualenv-init -)"'; } >> /home/gitpod/.bashrc.d/60-python \
26+
&& pyenv update \
27+
&& pyenv install 3.8.11 \
28+
&& pyenv global 3.8.11 \
29+
&& python3 -m pip install --no-cache-dir --upgrade pip \
30+
&& python3 -m pip install --no-cache-dir --upgrade \
31+
setuptools wheel virtualenv pipenv pylint rope flake8 \
32+
mypy autopep8 pep8 pylama pydocstyle bandit notebook \
33+
twine \
34+
&& sudo rm -rf /tmp/*USER gitpod
35+
ENV PYTHONUSERBASE=/workspace/.pip-modules \
36+
PIP_USER=yes
37+
ENV PATH=$PYTHONUSERBASE/bin:$PATH
38+
39+
40+
# Setup Heroku CLI
41+
RUN curl https://cli-assets.heroku.com/install.sh | sh
42+
43+
# Setup PostgreSQL
44+
45+
RUN sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list' && \
46+
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8 && \
47+
sudo apt-get update -y && \
48+
sudo apt-get install -y postgresql-12
49+
50+
ENV PGDATA="/workspace/.pgsql/data"
51+
52+
RUN mkdir -p ~/.pg_ctl/bin ~/.pg_ctl/sockets \
53+
&& echo '#!/bin/bash\n[ ! -d $PGDATA ] && mkdir -p $PGDATA && initdb --auth=trust -D $PGDATA\npg_ctl -D $PGDATA -l ~/.pg_ctl/log -o "-k ~/.pg_ctl/sockets" start\n' > ~/.pg_ctl/bin/pg_start \
54+
&& echo '#!/bin/bash\npg_ctl -D $PGDATA -l ~/.pg_ctl/log -o "-k ~/.pg_ctl/sockets" stop\n' > ~/.pg_ctl/bin/pg_stop \
55+
&& chmod +x ~/.pg_ctl/bin/*
56+
57+
# ENV DATABASE_URL="postgresql://gitpod@localhost"
58+
ENV PGHOSTADDR="127.0.0.1"
59+
ENV PGDATABASE="postgres"
60+
61+
ENV PATH="/usr/lib/postgresql/12/bin:/home/gitpod/.nvm/versions/node/v${NODE_VERSION}/bin:$HOME/.pg_ctl/bin:$PATH"
62+
63+
# Create our own config files
64+
65+
# Add aliases
66+
67+
RUN echo 'alias heroku_config=". $GITPOD_REPO_ROOT/.vscode/heroku_config.sh"' >> ~/.bashrc && \
68+
echo 'alias python=python3' >> ~/.bashrc && \
69+
echo 'alias pip=pip3' >> ~/.bashrc && \
70+
echo 'alias font_fix="python3 $GITPOD_REPO_ROOT/.vscode/font_fix.py"' >> ~/.bashrc
71+
72+
# Local environment variables
73+
74+
ENV PORT="8080"
75+
ENV IP="0.0.0.0"

.gitpod.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
image:
2+
file: .gitpod.dockerfile
3+
tasks:
4+
- init: . ${GITPOD_REPO_ROOT}/.vscode/init_tasks.sh
5+
command: /home/gitpod/.pg_ctl/bin/pg_start > /dev/null
6+
- command: . ${GITPOD_REPO_ROOT}/.vscode/uptime.sh &
7+
vscode:
8+
extensions:
9+
- ms-python.python
10+
- formulahendry.auto-close-tag
11+
- eventyret.bootstrap-4-cdn-snippet
12+
- hookyqr.beautify
13+
- matt-rudge.auto-open-preview-panel

.vscode/font_fix.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Fixes the font issue on Brave browser
2+
# Matt Rudge
3+
# August 2021
4+
5+
import json
6+
import os
7+
8+
BASE_PATH = os.environ.get("GITPOD_REPO_ROOT")
9+
10+
with open(f"{BASE_PATH}/.vscode/settings.json", "r+") as f:
11+
content = json.loads(f.read())
12+
13+
if "terminal.integrated.fontFamily" not in content:
14+
print("Adding wider and higher font settings")
15+
content["terminal.integrated.lineHeight"] = 1.2
16+
content["terminal.integrated.letterSpacing"] = 2
17+
else:
18+
print("Wider and higher font settings already added!")
19+
20+
f.seek(0, os.SEEK_SET)
21+
f.write(json.dumps(content))
22+
f.truncate()

.vscode/heroku_config.sh

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
# Script to allow Heroku API key to be pasted
3+
# exported as an environment variable
4+
#
5+
# Matt Rudge, May 2021
6+
7+
echo Heroku authentication configuration script
8+
echo Code Institute, 2021
9+
echo
10+
echo Get your Heroku API key by going to https://dashboard.heroku.com
11+
echo Go to Account Settings and click on Reveal to view your Heroku API key
12+
echo
13+
14+
if [[ -z "${HEROKU_API_KEY}" ]]; then
15+
echo Paste your Heroku API key here or press Enter to quit:
16+
read apikey
17+
if [[ -z "${apikey}" ]]; then
18+
return 0
19+
fi
20+
echo export HEROKU_API_KEY=${apikey} >> ~/.bashrc
21+
echo Added the export. Refreshing the terminal.
22+
. ~/.bashrc > /dev/null
23+
echo Done!
24+
else
25+
echo API key is already set. Exiting
26+
fi

.vscode/init_tasks.sh

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
# Creates a user record for the current Cloud9 user
4+
# Gives a personalised greeting
5+
# Adds configuration options for SQLite
6+
# Creates run aliases
7+
# Author: Matt Rudge
8+
9+
echo "Setting the greeting"
10+
sed -i "s/USER_NAME/$GITPOD_GIT_USER_NAME/g" ${GITPOD_REPO_ROOT}/README.md
11+
echo "Creating the gitpod user in MySQL"
12+
RESULT="$(mysql -sse "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'gitpod')")"
13+
if [ "$RESULT" = 1 ]; then
14+
echo "gitpod already exists"
15+
else
16+
mysql -e "CREATE USER 'gitpod'@'%' IDENTIFIED BY '';" -u root
17+
echo "Granting privileges"
18+
mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'gitpod'@'%' WITH GRANT OPTION;" -u root
19+
fi
20+
echo "Creating .sqliterc file"
21+
echo ".headers on" > ~/.sqliterc
22+
echo ".mode column" >> ~/.sqliterc
23+
echo "Your workspace is ready to use. Happy coding!"
24+
25+
# Open README.md file
26+
code README.md

.vscode/launch.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
"version": "0.2.0",
5+
"configurations": [
6+
{
7+
"name": "Python: Current File (Integrated Terminal)",
8+
"type": "python",
9+
"request": "launch",
10+
"program": "${file}",
11+
"console": "internalConsole"
12+
}
13+
]
14+
}

.vscode/settings.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"python.linting.pylintEnabled": true,
3+
"python.linting.enabled": true,
4+
"python.linting.pycodestyleEnabled": false,
5+
"python.linting.flake8Enabled": true,
6+
"python.terminal.activateEnvironment": false,
7+
"python.formatting.autopep8Path": "/home/gitpod/.pyenv/shims/autopep8",
8+
"python.linting.flake8Path": "/home/gitpod/.pyenv/shims/flake8",
9+
"cornflakes.linter.executablePath": "/home/gitpod/.pyenv/shims/flake8",
10+
"files.exclude": {
11+
"**/.DS_Store": true,
12+
"**/.git": true,
13+
"**/.github": true,
14+
"**/.gitp*": true,
15+
"**/.hg": true,
16+
"**/.svn": true,
17+
"**/.vscode": true,
18+
"**/core.Microsoft*": true,
19+
"**/core.mongo*": true,
20+
"**/core.python*": true,
21+
"**/CVS": true
22+
},
23+
"files.autoSave": "off",
24+
"workbench.colorTheme": "Visual Studio Dark",
25+
"editor.defaultFormatter": "HookyQR.beautify"
26+
}

.vscode/uptime.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
# Pings the webhook so that we can gather
4+
# basic usage stats. No personally identifiable
5+
# data is captured here, and it is impossible to
6+
# identify an individual user from the captured data.
7+
# Matt Rudge, April 2021
8+
9+
UUID=$(cat /proc/sys/kernel/random/uuid)
10+
URL=https://1xthkmzwg3.execute-api.eu-west-1.amazonaws.com/prod/lrsapi/
11+
API_KEY=jceBCdeGZP9RDeUNCfM4jIQ39Cx0jtG51QgcwDwc
12+
VERB="started"
13+
14+
clear
15+
16+
while true; do
17+
18+
DATA="{\"activity_time\":\"$(date +%Y-%m-%dT%H:%M:%S).000Z\",\"actor\":\"${UUID}\",\"verb\":\"${VERB}\",\"activity_object\":\"Gitpod Workspace\",\"extra_data\":\"{}\"}"
19+
curl -s -X POST -H "x-api-key: ${API_KEY}" -d "${DATA}" ${URL} 1> /dev/null
20+
VERB="running"
21+
sleep 300
22+
23+
done

Procfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: node index.js

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
![CI logo](https://codeinstitute.s3.amazonaws.com/fullstack/ci_logo_small.png)
2+
3+
Welcome USER_NAME,
4+
5+
This is the Code Institute student template for deploying your third portfolio project, the Python command-line project. The last update to this file was: **August 17, 2021**
6+
7+
## Reminders
8+
9+
* Your code must be placed in the `run.py` file
10+
* Your dependencies must be placed in the `requirements.txt` file
11+
* Do not edit any of the other files or your code may not deploy properly
12+
13+
## Creating the Heroku app
14+
15+
When you create the app, you will need to add two buildpacks from the _Settings_ tab. The ordering is as follows:
16+
17+
1. `heroku/python`
18+
2. `heroku/nodejs`
19+
20+
You must then create a _Config Var_ called `PORT`. Set this to `8000`
21+
22+
If you have credentials, such as in the Love Sandwiches project, you must create another _Config Var_ called `CREDS` and paste the JSON into the value field.
23+
24+
Connect your GitHub repository and deploy as normal.
25+
26+
## Constraints
27+
28+
The deployment terminal is set to 80 columns by 24 rows. That means that each line of text needs to be 80 characters or less otherwise it will be wrapped onto a second line.
29+
30+
-----
31+
Happy coding!

controllers/default.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const Pty = require('node-pty');
2+
const fs = require('fs');
3+
4+
exports.install = function () {
5+
6+
ROUTE('/');
7+
WEBSOCKET('/', socket, ['raw']);
8+
9+
};
10+
11+
function socket() {
12+
13+
this.encodedecode = false;
14+
this.autodestroy();
15+
16+
this.on('open', function (client) {
17+
18+
// Spawn terminal
19+
client.tty = Pty.spawn('python3', ['run.py'], {
20+
name: 'xterm-color',
21+
cols: 80,
22+
rows: 24,
23+
cwd: process.env.PWD,
24+
env: process.env
25+
});
26+
27+
client.tty.on('exit', function (code, signal) {
28+
client.tty = null;
29+
client.close();
30+
console.log("Process killed");
31+
});
32+
33+
client.tty.on('data', function (data) {
34+
client.send(data);
35+
});
36+
37+
});
38+
39+
this.on('close', function (client) {
40+
if (client.tty) {
41+
client.tty.kill(9);
42+
client.tty = null;
43+
console.log("Process killed and terminal unloaded");
44+
}
45+
});
46+
47+
this.on('message', function (client, msg) {
48+
client.tty && client.tty.write(msg);
49+
});
50+
}
51+
52+
if (process.env.CREDS != null) {
53+
console.log("Creating creds.json file.");
54+
fs.writeFile('creds.json', process.env.CREDS, 'utf8', function (err) {
55+
if (err) {
56+
console.log('Error writing file: ', err);
57+
socket.emit("console_output", "Error saving credentials: " + err);
58+
}
59+
});
60+
}

index.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// ===================================================
2+
// Total.js start script
3+
// https://www.totaljs.com
4+
// ===================================================
5+
6+
const options = {};
7+
8+
// options.ip = '127.0.0.1';
9+
options.port = parseInt(process.env.PORT);
10+
// options.unixsocket = require('path').join(require('os').tmpdir(), 'app_name');
11+
// options.config = { name: 'Total.js' };
12+
// options.sleep = 3000;
13+
// options.inspector = 9229;
14+
// options.watch = ['private'];
15+
// options.livereload = 'https://yourhostname';
16+
17+
// Enables cluster:
18+
// options.cluster = 'auto';
19+
// options.cluster_limit = 10; // max 10. threads (works only with "auto" scaling)
20+
21+
// Enables threads:
22+
// options.cluster = 'auto';
23+
// options.cluster_limit = 10; // max 10. threads (works only with "auto" scaling)
24+
// options.timeout = 5000;
25+
// options.threads = '/api/';
26+
// options.logs = 'isolated';
27+
28+
var type = process.argv.indexOf('--release', 1) !== -1 || process.argv.indexOf('release', 1) !== -1 ? 'release' : 'debug';
29+
// require('total4/' + type)(options);
30+
require('total4').http('release', options);

package.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "terminal",
3+
"version": "1.0.0",
4+
"main": "server.js",
5+
"scripts": {
6+
"test": "echo \"Error: no test specified\" && exit 1"
7+
},
8+
"repository": {
9+
"type": "git",
10+
"url": "git+https://github.com/lechien73/terminal.git"
11+
},
12+
"author": "",
13+
"license": "ISC",
14+
"bugs": {
15+
"url": "https://github.com/lechien73/terminal/issues"
16+
},
17+
"homepage": "https://github.com/lechien73/terminal#readme",
18+
"dependencies": {
19+
"node-static": "^0.7.11",
20+
"node-pty": "^0.10.1",
21+
"total4": "^0.0.45"
22+
}
23+
}

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Your requirements go here

0 commit comments

Comments
 (0)