From c17bac520729c337e52ec9e691e5f177af4ec4db Mon Sep 17 00:00:00 2001 From: Ferry Boender Date: Fri, 24 Apr 2015 08:08:49 +0200 Subject: [PATCH] MegaCorp example updated. --- examples/megacorp_acc/README.md | 6 + examples/megacorp_acc/job_clean_database.sh | 12 +- examples/megacorp_acc/job_download_db.sh | 11 ++ examples/megacorp_acc/job_enable_firewall.sh | 15 ++ examples/megacorp_acc/job_import_employees.sh | 11 ++ examples/megacorp_acc/job_list_employees.sh | 8 ++ examples/megacorp_acc/megacorp_acc.json | 133 +++++++++++------- examples/megacorp_acc/megacorp_employees.csv | 3 + examples/megacorp_acc/megacorp_empty.sql | 6 + 9 files changed, 146 insertions(+), 59 deletions(-) create mode 100644 examples/megacorp_acc/README.md create mode 100755 examples/megacorp_acc/job_download_db.sh create mode 100755 examples/megacorp_acc/job_enable_firewall.sh create mode 100755 examples/megacorp_acc/job_import_employees.sh create mode 100755 examples/megacorp_acc/job_list_employees.sh create mode 100644 examples/megacorp_acc/megacorp_employees.csv create mode 100644 examples/megacorp_acc/megacorp_empty.sql diff --git a/examples/megacorp_acc/README.md b/examples/megacorp_acc/README.md new file mode 100644 index 0000000..888326e --- /dev/null +++ b/examples/megacorp_acc/README.md @@ -0,0 +1,6 @@ +Extensive example of all kinds of jobs for the fictional Acceptance environment for Megacorp. + +Usernames: + +* username: admin +* password: password diff --git a/examples/megacorp_acc/job_clean_database.sh b/examples/megacorp_acc/job_clean_database.sh index 0d14471..5ce8763 100755 --- a/examples/megacorp_acc/job_clean_database.sh +++ b/examples/megacorp_acc/job_clean_database.sh @@ -1,7 +1,9 @@ #!/bin/sh -MYSQL_DEFAULTS_FILE="my.cnf" -MYSQL="mysql --defaults-file=$MYSQL_DEFAULTS_FILE" - -echo "echo 'DROP DATABASE scriptform_acc' | $MYSQL" -echo "$MYSQL < dbs/${sample_db}.sql" +if [ "$source_sql" = "empty" ]; then + echo "Loading empty database" + rm megacorp.db + sqlite3 megacorp.db < megacorp_empty.sql && echo "Succesfully loaded" +else + echo "Not Implemented" +fi diff --git a/examples/megacorp_acc/job_download_db.sh b/examples/megacorp_acc/job_download_db.sh new file mode 100755 index 0000000..a5b96b7 --- /dev/null +++ b/examples/megacorp_acc/job_download_db.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +FILESIZE=$(stat -c "%s" megacorp.db) +cat << EOF +HTTP/1.0 200 Ok +Content-Type: application/octet-stream +Content-Disposition: attachment; filename="megacorp.db" +Content-Length: $FILESIZE + +EOF +cat megacorp.db diff --git a/examples/megacorp_acc/job_enable_firewall.sh b/examples/megacorp_acc/job_enable_firewall.sh new file mode 100755 index 0000000..0a22524 --- /dev/null +++ b/examples/megacorp_acc/job_enable_firewall.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +# FIXME: Validate ip address + +if [ "$network" = "intra" ]; then + NETWORK="192.168.1.0/24" +elif [ "$network" = "machine" ]; then + NETWORK="192.168.1.12" +else + echo "Invalid network >&2" + exit 1 +fi + +echo "iptables -A INPUT -p tcp --source $ip_address --dest $NETWORK -j ACCEPT" +echo "echo \"iptables -A INPUT -p tcp --source $ip_address --dest $NETWORK -j ACCEPT\" | at now + $expire_days days" diff --git a/examples/megacorp_acc/job_import_employees.sh b/examples/megacorp_acc/job_import_employees.sh new file mode 100755 index 0000000..85263ad --- /dev/null +++ b/examples/megacorp_acc/job_import_employees.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +{ + echo ".separator ," + echo ".import $csv_file employee" +} | sqlite3 megacorp.db +if [ $? -eq 0 ]; then + echo "Succesfully loaded employees" +else + echo "Failed to load employees. Maybe you should clean the database first?" +fi diff --git a/examples/megacorp_acc/job_list_employees.sh b/examples/megacorp_acc/job_list_employees.sh new file mode 100755 index 0000000..5c26515 --- /dev/null +++ b/examples/megacorp_acc/job_list_employees.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +echo "" +{ + echo ".mode html" + echo "SELECT * FROM employee;" +} | sqlite3 megacorp.db || exit 1 +echo "
" diff --git a/examples/megacorp_acc/megacorp_acc.json b/examples/megacorp_acc/megacorp_acc.json index 823727b..f20b1bb 100644 --- a/examples/megacorp_acc/megacorp_acc.json +++ b/examples/megacorp_acc/megacorp_acc.json @@ -1,56 +1,20 @@ { "title": "MegaCorp acceptance jobs", - "forms": { - "enable_firewall": { - "title": "Enable firewall", - "description": "Enable access to the acceptance environment from the entered IP", - "submit_title": "Enable access", - "script": "job_clean_database.sh", - "fields": [ - { - "name": "ip_address", - "title": "IP Address", - "type": "string", - "required": true - }, - { - "name": "expire_days", - "title": "Expire (days)", - "type": "integer", - "max": 31, - "min": 2 - }, - { - "name": "expire_date", - "title": "Expire (date)", - "type": "date", - "required": true - }, - { - "name": "network", - "title": "Which network", - "type": "radio", - "options": [ - ["intra", "Whole intranet"], - ["machine", "Acceptance machine"] - ] - }, - { - "name": "comment", - "title": "Comment", - "type": "text" - } - ] - }, - "clean_database": { + "users": { + "jjohnson": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", + "admin": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8" + }, + "forms": [ + { + "name": "clean_database", "title": "Load clean database", "description": "Recreate the acceptance database from scratch. This deletes all the information in the database", "submit_title": "Run", "script": "job_clean_database.sh", "fields": [ { - "name": "sample_db", - "title": "Sample database", + "name": "source_sql", + "title": "Database type", "type": "select", "options": [ ["empty", "Empty database"], @@ -60,11 +24,12 @@ } ] }, - "import_csv": { - "title": "Import CSV data into database", - "description": "Load a CSV with test data into the database", + { + "name": "import_employees", + "title": "Import employee data from CSV", + "description": "Load a CSV with employee data into the database. The employee table MUST be empty.", "submit_title": "Import CSV", - "script": "job_restart_acc.sh", + "script": "job_import_employees.sh", "fields": [ { "name": "csv_file", @@ -73,18 +38,78 @@ } ] }, - "restart_services": { + { + "name": "list_employees", + "title": "List employees", + "description": "List the employees currently in the database", + "submit_title": "List", + "script": "job_list_employees.sh", + "output": "html", + "fields": [ + ] + }, + { + "name": "download_db", + "title": "Download database", + "description": "Download the full binary database", + "submit_title": "Download", + "script": "job_download_db.sh", + "output": "raw", + "fields": [ + ] + }, + { + "name": "restart_services", "title": "Restart Acceptance services", - "description": "Restarts the acceptance services (web, db, cache). Consult with John Foo first!", + "description": "Restarts the acceptance services (web, db, cache). Consult with Karl Karlsön first!", "submit_title": "Restart", "script": "job_restart_acc.sh", "fields": [ { "name": "passwd", - "title": "John Foo gave you a password. What is it?", - "type": "password" + "title": "Karl Karlsön gave you a password. What is it?", + "type": "password", + "required": true + } + ] + }, + { + "name": "enable_firewall", + "allowed_users": ["admin"], + "title": "Enable firewall", + "description": "Enable access to the acceptance environment from the entered IP", + "submit_title": "Enable access", + "script": "job_enable_firewall.sh", + "fields": [ + { + "name": "ip_address", + "title": "From IP Address", + "type": "string", + "required": true, + "min_length": 7 + }, + { + "name": "expire_days", + "title": "Expire (days)", + "type": "integer", + "max": 31, + "min": 2 + }, + { + "name": "network", + "title": "To which network", + "type": "radio", + "options": [ + ["intra", "Whole intranet"], + ["machine", "Acceptance machine"] + ] + }, + { + "name": "comment", + "title": "Comment", + "type": "text" } ] } - } + ] } diff --git a/examples/megacorp_acc/megacorp_employees.csv b/examples/megacorp_acc/megacorp_employees.csv new file mode 100644 index 0000000..fd30252 --- /dev/null +++ b/examples/megacorp_acc/megacorp_employees.csv @@ -0,0 +1,3 @@ +1, JJO, John, Johnson +2, KKA, Karl, Karlsön +3, PPE, Pete, Peterson diff --git a/examples/megacorp_acc/megacorp_empty.sql b/examples/megacorp_acc/megacorp_empty.sql new file mode 100644 index 0000000..aaa0b28 --- /dev/null +++ b/examples/megacorp_acc/megacorp_empty.sql @@ -0,0 +1,6 @@ +CREATE TABLE employee ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + code TEXT, + firstname TEXT, + lastname TEXT +);