From 4b1ff1c4692cd0738359c65098829c94c1dd1cbe Mon Sep 17 00:00:00 2001 From: Hashfyre Date: Fri, 24 Nov 2023 18:57:57 +0530 Subject: [PATCH] WIP --- .../helm/templates/mongodb-seeder-job.yaml | 45 +++++++ ...l-seeed-job.yaml => mysql-seeder-job.yaml} | 2 +- app/robot-shop/seeder/mongo/Dockerfile | 33 +++++ app/robot-shop/seeder/mongo/bin/seeder.sh | 15 +++ .../seeder/mongo/seeds/catalogue.json | 113 ++++++++++++++++++ app/robot-shop/seeder/mongo/seeds/users.js | 63 ++++++++++ app/robot-shop/seeder/mongo/seeds/users.json | 17 +++ app/robot-shop/seeder/mongo/seeds/usersv2.js | 55 +++++++++ 8 files changed, 342 insertions(+), 1 deletion(-) create mode 100644 app/robot-shop/helm/templates/mongodb-seeder-job.yaml rename app/robot-shop/helm/templates/{mysql-seeed-job.yaml => mysql-seeder-job.yaml} (96%) create mode 100644 app/robot-shop/seeder/mongo/Dockerfile create mode 100644 app/robot-shop/seeder/mongo/bin/seeder.sh create mode 100644 app/robot-shop/seeder/mongo/seeds/catalogue.json create mode 100644 app/robot-shop/seeder/mongo/seeds/users.js create mode 100644 app/robot-shop/seeder/mongo/seeds/users.json create mode 100644 app/robot-shop/seeder/mongo/seeds/usersv2.js diff --git a/app/robot-shop/helm/templates/mongodb-seeder-job.yaml b/app/robot-shop/helm/templates/mongodb-seeder-job.yaml new file mode 100644 index 0000000..6e25154 --- /dev/null +++ b/app/robot-shop/helm/templates/mongodb-seeder-job.yaml @@ -0,0 +1,45 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: mongodb-seeder + labels: + service: mongodb-seeder +spec: + template: + spec: + containers: + - name: mongodb-seeder + image: hashfyre/robotshop-mongodb-seeder + args: + - /bin/bash + - -c + - /wait && /opt/seeds/seeder.sh + env: + - name: MONGO_URL + value: {{ .Values.mongo_url }} + - name: TIMEOUT + value: "600" + - name: WAIT_HOSTS + value: {{ .Values.mongo_url }}:3306 + - name: WAIT_HOSTS_TIMEOUT + value: "300" + - name: WAIT_HOST_CONNECT_TIMEOUT + value: "30" + - name: WAIT_SLEEP_INTERVAL + value: "30" + resources: + {{- toYaml .Values.mongodbseeder.resources | nindent 12 }} + restartPolicy: OnFailure + {{- with .Values.users.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.users.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.users.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} + backoffLimit: 4 \ No newline at end of file diff --git a/app/robot-shop/helm/templates/mysql-seeed-job.yaml b/app/robot-shop/helm/templates/mysql-seeder-job.yaml similarity index 96% rename from app/robot-shop/helm/templates/mysql-seeed-job.yaml rename to app/robot-shop/helm/templates/mysql-seeder-job.yaml index a2826d4..99cb45c 100644 --- a/app/robot-shop/helm/templates/mysql-seeed-job.yaml +++ b/app/robot-shop/helm/templates/mysql-seeder-job.yaml @@ -9,7 +9,7 @@ spec: spec: containers: - name: mysql-seeder - image: hashfyre/robotshop-seeder + image: hashfyre/robotshop-mysql-seeder args: - /bin/bash - -c diff --git a/app/robot-shop/seeder/mongo/Dockerfile b/app/robot-shop/seeder/mongo/Dockerfile new file mode 100644 index 0000000..8ab665f --- /dev/null +++ b/app/robot-shop/seeder/mongo/Dockerfile @@ -0,0 +1,33 @@ +# FROM mongo:7.0.3 +FROM debian:trixie-slim + +# COPY *.js /docker-entrypoint-initdb.d/ +ENV NODE_MODULES=/usr/local/lib/node_modules +## Add the wait script to the image +ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait + +COPY ./seeds/* /opt/seeds/ +COPY --chmod=775 ./bin/seeder.sh /opt/seeds/seeder.sh + +RUN mkdir -p /usr/share/man/man1 /usr/share/man/man2 \ + && apt-get update \ + && apt-get purge -y nodejs --auto-remove \ + && apt-get purge -y npm --auto-remove \ + && rm /usr/local/bin/node \ + && apt-get install -y \ + software-properties-common \ + gnupg2 \ + apt-transport-https \ + apt-utils \ + ca-certificates \ + bash \ + curl \ + nodejs \ + npm \ + && npm cache clean -f \ + && npm install -g n \ + && n stable \ + && npm install -g mongodb \ + && chmod +x /wait + +# seed -u 'mongodb://roboadmin:docdb3421z@robotshopdocdb-cluster.cluster-chh4lgwsalzi.us-east-1.docdb.amazonaws.com:27017/?replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false/users' --drop-database ./users.json \ No newline at end of file diff --git a/app/robot-shop/seeder/mongo/bin/seeder.sh b/app/robot-shop/seeder/mongo/bin/seeder.sh new file mode 100644 index 0000000..8e7c43c --- /dev/null +++ b/app/robot-shop/seeder/mongo/bin/seeder.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +echo "Seeding in progress" + +SOURCE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +cd $SOURCE_DIR + +echo "Importing all seed data into DB: $DB_DEV" +for seed_file in *.sql; do + echo "Adding file: $seed_file" + #MONGO_PWD=$MONGO_PASSWORD MONGO -h "${MONGO_HOST:-localhost}" -u $MONGO_USER < $seed_file + mongo < $seed_file +done + +echo "Seeding is complete" \ No newline at end of file diff --git a/app/robot-shop/seeder/mongo/seeds/catalogue.json b/app/robot-shop/seeder/mongo/seeds/catalogue.json new file mode 100644 index 0000000..473a8bb --- /dev/null +++ b/app/robot-shop/seeder/mongo/seeds/catalogue.json @@ -0,0 +1,113 @@ +[ + { + "sku": "Watson", + "name": "Watson", + "description": "Probably the smartest AI on the planet", + "price": 2001, + "instock": 2, + "categories": [ + "Artificial Intelligence" + ] + }, + { + "sku": "Ewooid", + "name": "Ewooid", + "description": "Fully sentient assistant", + "price": 200, + "instock": 0, + "categories": [ + "Artificial Intelligence" + ] + }, + { + "sku": "HPTD", + "name": "High-Powered Travel Droid", + "description": "Traveling to the far reaches of the Galaxy? You need this for protection. Comes in handy when you are lost in space", + "price": 1200, + "instock": 12, + "categories": [ + "Robot" + ] + }, + { + "sku": "UHJ", + "name": "Ultimate Harvesting Juggernaut", + "description": "Extraterrestrial vegetation harvester", + "price": 5000, + "instock": 10, + "categories": [ + "Robot" + ] + }, + { + "sku": "EPE", + "name": "Extreme Probe Emulator", + "description": "Versatile interface adapter for hacking into systems", + "price": 953, + "instock": 1, + "categories": [ + "Robot" + ] + }, + { + "sku": "EMM", + "name": "Exceptional Medical Machine", + "description": "Fully automatic surgery droid with exceptional bedside manner", + "price": 1024, + "instock": 1, + "categories": [ + "Robot" + ] + }, + { + "sku": "SHCE", + "name": "Strategic Human Control Emulator", + "description": "Diplomatic protocol assistant", + "price": 300, + "instock": 12, + "categories": [ + "Robot" + ] + }, + { + "sku": "RED", + "name": "Responsive Enforcer Droid", + "description": "Security detail, will gaurd anything", + "price": 700, + "instock": 5, + "categories": [ + "Robot" + ] + }, + { + "sku": "RMC", + "name": "Robotic Mining Cyborg", + "description": "Excellent tunneling capability to get those rare minerals", + "price": 42, + "instock": 48, + "categories": [ + "Robot" + ] + }, + { + "sku": "STAN-1", + "name": "Stan", + "description": "Observability guru", + "price": 67, + "instock": 1000, + "categories": [ + "Robot", + "Artificial Intelligence" + ] + }, + { + "sku": "CNA", + "name": "Cybernated Neutralization Android", + "description": "Is your spaceship a bit whiffy? This little fellow will bring a breath of fresh air", + "price": 1000, + "instock": 0, + "categories": [ + "Robot" + ] + } +] \ No newline at end of file diff --git a/app/robot-shop/seeder/mongo/seeds/users.js b/app/robot-shop/seeder/mongo/seeds/users.js new file mode 100644 index 0000000..7dc0d0e --- /dev/null +++ b/app/robot-shop/seeder/mongo/seeds/users.js @@ -0,0 +1,63 @@ +const mongoClient = require('mongodb').MongoClient; +const mongoObjectID = require('mongodb').ObjectID; +const pino = require('pino'); + +const logger = pino({ + level: 'error', + prettyPrint: true, + useLevelLabels: true +}); + +// MongoDB +var db; +var usersCollection; +var mongoConnected = false; + +function mongoConnect() { + return new Promise((resolve, reject) => { + // var mongoURL = process.env.MONGO_URL || 'mongodb://mongodb:27017/users'; + var mongoURL = process.env.MONGO_URL || 'mongodb://roboadmin:docdb3421z@robotshopdocdb-cluster.cluster-chh4lgwsalzi.us-east-1.docdb.amazonaws.com:27017/?replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'; + mongoClient.connect(mongoURL, (error, client) => { + if(error) { + reject(error); + logger.error('Mongodb connection error', error) + } else { + db = client.db('users'); + usersCollection = db.collection('users'); + resolve('connected'); + } + }); + }); +} + +function mongoLoop() { + mongoConnect().then((r) => { + mongoConnected = true; + logger.info('MongoDB connected'); + }).catch((e) => { + logger.error('ERROR', e); + setTimeout(mongoLoop, 5); + }); +} + +// seed +function seedUsers(){ + if (mongoConnected) { + db = db.getSiblingDB('users'); + db.users.insertMany([ + {name: 'user', password: 'password', email: 'user@me.com'}, + {name: 'stan', password: 'bigbrain', email: 'stan@instana.com'}, + {name: 'partner-57', password: 'worktogether', email: 'howdy@partner.com'} + ]); + // unique index on the name + db.users.createIndex( + {name: 1}, + {unique: true} + ); + } else { + logger.error('MongoDB not connected', mongoClient); + } +} + +mongoLoop(); +seedUsers(); \ No newline at end of file diff --git a/app/robot-shop/seeder/mongo/seeds/users.json b/app/robot-shop/seeder/mongo/seeds/users.json new file mode 100644 index 0000000..8be15e6 --- /dev/null +++ b/app/robot-shop/seeder/mongo/seeds/users.json @@ -0,0 +1,17 @@ +[ + { + "name": "user", + "password": "password", + "email": "user@me.com" + }, + { + "name": "stan", + "password": "bigbrain", + "email": "stan@instana.com" + }, + { + "name": "partner-57", + "password": "worktogether", + "email": "howdy@partner.com" + } +] \ No newline at end of file diff --git a/app/robot-shop/seeder/mongo/seeds/usersv2.js b/app/robot-shop/seeder/mongo/seeds/usersv2.js new file mode 100644 index 0000000..879688e --- /dev/null +++ b/app/robot-shop/seeder/mongo/seeds/usersv2.js @@ -0,0 +1,55 @@ +const mongoose = require('mongoose') + +const { Schema } = mongoose; +var mongoURL= process.env.MONGO_URL || 'mongodb://roboadmin:docdb3421z@robotshopdocdb-cluster.cluster-chh4lgwsalzi.us-east-1.docdb.amazonaws.com:27017/?replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'; +var usersSchema = new Schema({ any: {} }); +var usersSchema = new Schema({ any: Schema.Types.Mixed }); +var users = mongoose.model('users', usersSchema) + +mongoose.connect( + mongoURL, + { + useNewUrlParser: true, + } +).then( + ()=> { + console.log('MongoDb connected') + } +).catch( + (err) => { + console.log(err) + } +); + +const seedUsers = [ + {name: 'user', password: 'password', email: 'user@me.com'}, + {name: 'stan', password: 'bigbrain', email: 'stan@instana.com'}, + {name: 'partner-57', password: 'worktogether', email: 'howdy@partner.com'} +]; + +const seedDB = async () => { + await users.insertMany(seedUsers).then( + function(){ + console.log('user data inserted') + } + ).catch( + function(error){ + console.log(error) + } + ) + // unique index on the name + await usersSchema.index( + {name: 1}, + {unique: true} + ) + var usersData = await users.find({"name" : "stan"}); + if (usersData) { + console.log(JSON.stringify(usersData, null, 4)) + } +}; + +seedDB().then( + () => { + mongoose.connection.close(); + } +) \ No newline at end of file