Skip to content

Commit 4b1ff1c

Browse files
committed
WIP
1 parent fae6948 commit 4b1ff1c

8 files changed

Lines changed: 342 additions & 1 deletion

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
apiVersion: batch/v1
2+
kind: Job
3+
metadata:
4+
name: mongodb-seeder
5+
labels:
6+
service: mongodb-seeder
7+
spec:
8+
template:
9+
spec:
10+
containers:
11+
- name: mongodb-seeder
12+
image: hashfyre/robotshop-mongodb-seeder
13+
args:
14+
- /bin/bash
15+
- -c
16+
- /wait && /opt/seeds/seeder.sh
17+
env:
18+
- name: MONGO_URL
19+
value: {{ .Values.mongo_url }}
20+
- name: TIMEOUT
21+
value: "600"
22+
- name: WAIT_HOSTS
23+
value: {{ .Values.mongo_url }}:3306
24+
- name: WAIT_HOSTS_TIMEOUT
25+
value: "300"
26+
- name: WAIT_HOST_CONNECT_TIMEOUT
27+
value: "30"
28+
- name: WAIT_SLEEP_INTERVAL
29+
value: "30"
30+
resources:
31+
{{- toYaml .Values.mongodbseeder.resources | nindent 12 }}
32+
restartPolicy: OnFailure
33+
{{- with .Values.users.affinity }}
34+
affinity:
35+
{{- toYaml . | nindent 8 }}
36+
{{- end }}
37+
{{- with .Values.users.nodeSelector }}
38+
nodeSelector:
39+
{{- toYaml . | nindent 8 }}
40+
{{- end }}
41+
{{- with .Values.users.tolerations }}
42+
tolerations:
43+
{{- toYaml . | nindent 8 }}
44+
{{- end }}
45+
backoffLimit: 4

app/robot-shop/helm/templates/mysql-seeed-job.yaml renamed to app/robot-shop/helm/templates/mysql-seeder-job.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ spec:
99
spec:
1010
containers:
1111
- name: mysql-seeder
12-
image: hashfyre/robotshop-seeder
12+
image: hashfyre/robotshop-mysql-seeder
1313
args:
1414
- /bin/bash
1515
- -c
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# FROM mongo:7.0.3
2+
FROM debian:trixie-slim
3+
4+
# COPY *.js /docker-entrypoint-initdb.d/
5+
ENV NODE_MODULES=/usr/local/lib/node_modules
6+
## Add the wait script to the image
7+
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait
8+
9+
COPY ./seeds/* /opt/seeds/
10+
COPY --chmod=775 ./bin/seeder.sh /opt/seeds/seeder.sh
11+
12+
RUN mkdir -p /usr/share/man/man1 /usr/share/man/man2 \
13+
&& apt-get update \
14+
&& apt-get purge -y nodejs --auto-remove \
15+
&& apt-get purge -y npm --auto-remove \
16+
&& rm /usr/local/bin/node \
17+
&& apt-get install -y \
18+
software-properties-common \
19+
gnupg2 \
20+
apt-transport-https \
21+
apt-utils \
22+
ca-certificates \
23+
bash \
24+
curl \
25+
nodejs \
26+
npm \
27+
&& npm cache clean -f \
28+
&& npm install -g n \
29+
&& n stable \
30+
&& npm install -g mongodb \
31+
&& chmod +x /wait
32+
33+
# 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
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
echo "Seeding in progress"
4+
5+
SOURCE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6+
cd $SOURCE_DIR
7+
8+
echo "Importing all seed data into DB: $DB_DEV"
9+
for seed_file in *.sql; do
10+
echo "Adding file: $seed_file"
11+
#MONGO_PWD=$MONGO_PASSWORD MONGO -h "${MONGO_HOST:-localhost}" -u $MONGO_USER < $seed_file
12+
mongo < $seed_file
13+
done
14+
15+
echo "Seeding is complete"
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
[
2+
{
3+
"sku": "Watson",
4+
"name": "Watson",
5+
"description": "Probably the smartest AI on the planet",
6+
"price": 2001,
7+
"instock": 2,
8+
"categories": [
9+
"Artificial Intelligence"
10+
]
11+
},
12+
{
13+
"sku": "Ewooid",
14+
"name": "Ewooid",
15+
"description": "Fully sentient assistant",
16+
"price": 200,
17+
"instock": 0,
18+
"categories": [
19+
"Artificial Intelligence"
20+
]
21+
},
22+
{
23+
"sku": "HPTD",
24+
"name": "High-Powered Travel Droid",
25+
"description": "Traveling to the far reaches of the Galaxy? You need this for protection. Comes in handy when you are lost in space",
26+
"price": 1200,
27+
"instock": 12,
28+
"categories": [
29+
"Robot"
30+
]
31+
},
32+
{
33+
"sku": "UHJ",
34+
"name": "Ultimate Harvesting Juggernaut",
35+
"description": "Extraterrestrial vegetation harvester",
36+
"price": 5000,
37+
"instock": 10,
38+
"categories": [
39+
"Robot"
40+
]
41+
},
42+
{
43+
"sku": "EPE",
44+
"name": "Extreme Probe Emulator",
45+
"description": "Versatile interface adapter for hacking into systems",
46+
"price": 953,
47+
"instock": 1,
48+
"categories": [
49+
"Robot"
50+
]
51+
},
52+
{
53+
"sku": "EMM",
54+
"name": "Exceptional Medical Machine",
55+
"description": "Fully automatic surgery droid with exceptional bedside manner",
56+
"price": 1024,
57+
"instock": 1,
58+
"categories": [
59+
"Robot"
60+
]
61+
},
62+
{
63+
"sku": "SHCE",
64+
"name": "Strategic Human Control Emulator",
65+
"description": "Diplomatic protocol assistant",
66+
"price": 300,
67+
"instock": 12,
68+
"categories": [
69+
"Robot"
70+
]
71+
},
72+
{
73+
"sku": "RED",
74+
"name": "Responsive Enforcer Droid",
75+
"description": "Security detail, will gaurd anything",
76+
"price": 700,
77+
"instock": 5,
78+
"categories": [
79+
"Robot"
80+
]
81+
},
82+
{
83+
"sku": "RMC",
84+
"name": "Robotic Mining Cyborg",
85+
"description": "Excellent tunneling capability to get those rare minerals",
86+
"price": 42,
87+
"instock": 48,
88+
"categories": [
89+
"Robot"
90+
]
91+
},
92+
{
93+
"sku": "STAN-1",
94+
"name": "Stan",
95+
"description": "Observability guru",
96+
"price": 67,
97+
"instock": 1000,
98+
"categories": [
99+
"Robot",
100+
"Artificial Intelligence"
101+
]
102+
},
103+
{
104+
"sku": "CNA",
105+
"name": "Cybernated Neutralization Android",
106+
"description": "Is your spaceship a bit whiffy? This little fellow will bring a breath of fresh air",
107+
"price": 1000,
108+
"instock": 0,
109+
"categories": [
110+
"Robot"
111+
]
112+
}
113+
]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const mongoClient = require('mongodb').MongoClient;
2+
const mongoObjectID = require('mongodb').ObjectID;
3+
const pino = require('pino');
4+
5+
const logger = pino({
6+
level: 'error',
7+
prettyPrint: true,
8+
useLevelLabels: true
9+
});
10+
11+
// MongoDB
12+
var db;
13+
var usersCollection;
14+
var mongoConnected = false;
15+
16+
function mongoConnect() {
17+
return new Promise((resolve, reject) => {
18+
// var mongoURL = process.env.MONGO_URL || 'mongodb://mongodb:27017/users';
19+
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';
20+
mongoClient.connect(mongoURL, (error, client) => {
21+
if(error) {
22+
reject(error);
23+
logger.error('Mongodb connection error', error)
24+
} else {
25+
db = client.db('users');
26+
usersCollection = db.collection('users');
27+
resolve('connected');
28+
}
29+
});
30+
});
31+
}
32+
33+
function mongoLoop() {
34+
mongoConnect().then((r) => {
35+
mongoConnected = true;
36+
logger.info('MongoDB connected');
37+
}).catch((e) => {
38+
logger.error('ERROR', e);
39+
setTimeout(mongoLoop, 5);
40+
});
41+
}
42+
43+
// seed
44+
function seedUsers(){
45+
if (mongoConnected) {
46+
db = db.getSiblingDB('users');
47+
db.users.insertMany([
48+
{name: 'user', password: 'password', email: 'user@me.com'},
49+
{name: 'stan', password: 'bigbrain', email: 'stan@instana.com'},
50+
{name: 'partner-57', password: 'worktogether', email: 'howdy@partner.com'}
51+
]);
52+
// unique index on the name
53+
db.users.createIndex(
54+
{name: 1},
55+
{unique: true}
56+
);
57+
} else {
58+
logger.error('MongoDB not connected', mongoClient);
59+
}
60+
}
61+
62+
mongoLoop();
63+
seedUsers();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"name": "user",
4+
"password": "password",
5+
"email": "user@me.com"
6+
},
7+
{
8+
"name": "stan",
9+
"password": "bigbrain",
10+
"email": "stan@instana.com"
11+
},
12+
{
13+
"name": "partner-57",
14+
"password": "worktogether",
15+
"email": "howdy@partner.com"
16+
}
17+
]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const mongoose = require('mongoose')
2+
3+
const { Schema } = mongoose;
4+
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';
5+
var usersSchema = new Schema({ any: {} });
6+
var usersSchema = new Schema({ any: Schema.Types.Mixed });
7+
var users = mongoose.model('users', usersSchema)
8+
9+
mongoose.connect(
10+
mongoURL,
11+
{
12+
useNewUrlParser: true,
13+
}
14+
).then(
15+
()=> {
16+
console.log('MongoDb connected')
17+
}
18+
).catch(
19+
(err) => {
20+
console.log(err)
21+
}
22+
);
23+
24+
const seedUsers = [
25+
{name: 'user', password: 'password', email: 'user@me.com'},
26+
{name: 'stan', password: 'bigbrain', email: 'stan@instana.com'},
27+
{name: 'partner-57', password: 'worktogether', email: 'howdy@partner.com'}
28+
];
29+
30+
const seedDB = async () => {
31+
await users.insertMany(seedUsers).then(
32+
function(){
33+
console.log('user data inserted')
34+
}
35+
).catch(
36+
function(error){
37+
console.log(error)
38+
}
39+
)
40+
// unique index on the name
41+
await usersSchema.index(
42+
{name: 1},
43+
{unique: true}
44+
)
45+
var usersData = await users.find({"name" : "stan"});
46+
if (usersData) {
47+
console.log(JSON.stringify(usersData, null, 4))
48+
}
49+
};
50+
51+
seedDB().then(
52+
() => {
53+
mongoose.connection.close();
54+
}
55+
)

0 commit comments

Comments
 (0)