-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
342 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
) |