Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Hashfyre committed Nov 24, 2023
1 parent fae6948 commit 4b1ff1c
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 1 deletion.
45 changes: 45 additions & 0 deletions app/robot-shop/helm/templates/mongodb-seeder-job.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ spec:
spec:
containers:
- name: mysql-seeder
image: hashfyre/robotshop-seeder
image: hashfyre/robotshop-mysql-seeder
args:
- /bin/bash
- -c
Expand Down
33 changes: 33 additions & 0 deletions app/robot-shop/seeder/mongo/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions app/robot-shop/seeder/mongo/bin/seeder.sh
Original file line number Diff line number Diff line change
@@ -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"
113 changes: 113 additions & 0 deletions app/robot-shop/seeder/mongo/seeds/catalogue.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
]
63 changes: 63 additions & 0 deletions app/robot-shop/seeder/mongo/seeds/users.js
Original file line number Diff line number Diff line change
@@ -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: '[email protected]'},
{name: 'stan', password: 'bigbrain', email: '[email protected]'},
{name: 'partner-57', password: 'worktogether', email: '[email protected]'}
]);
// unique index on the name
db.users.createIndex(
{name: 1},
{unique: true}
);
} else {
logger.error('MongoDB not connected', mongoClient);
}
}

mongoLoop();
seedUsers();
17 changes: 17 additions & 0 deletions app/robot-shop/seeder/mongo/seeds/users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"name": "user",
"password": "password",
"email": "[email protected]"
},
{
"name": "stan",
"password": "bigbrain",
"email": "[email protected]"
},
{
"name": "partner-57",
"password": "worktogether",
"email": "[email protected]"
}
]
55 changes: 55 additions & 0 deletions app/robot-shop/seeder/mongo/seeds/usersv2.js
Original file line number Diff line number Diff line change
@@ -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: '[email protected]'},
{name: 'stan', password: 'bigbrain', email: '[email protected]'},
{name: 'partner-57', password: 'worktogether', email: '[email protected]'}
];

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();
}
)

0 comments on commit 4b1ff1c

Please sign in to comment.