Skip to content

Commit bd91823

Browse files
committedSep 15, 2023
update convert from sql -> sqlite scripts, for later reference
1 parent a0e0e43 commit bd91823

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed
 

‎operational/scripts/convert.sh

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
socket_dir=$(mktemp -d)
4+
connection_name="nais-io:europe-north1:naisdevice-e7bdb702"
5+
project="$(cut -d ':' -f 1 <<< "$connection_name")"
6+
7+
cloud_sql_proxy "${connection_name}?unix-socket-path=${socket_dir}/${connection_name}" &
8+
export PGPASSWORD=$(gcloud --project "$project" compute ssh --tunnel-through-iap naisdevice-apiserver -- sudo grep "DBCONNDSN" /etc/default/apiserver | cut -d ' ' -f 4 | cut -d '=' -f 2)
9+
pid=$!
10+
sleep 2
11+
12+
trap 'kill "$pid"; rm -rf "$socket_dir"' EXIT
13+
14+
rm -f new.db
15+
rm -r data/
16+
mkdir -p data/
17+
18+
psql "sslmode=disable host=${socket_dir}/${connection_name} user=apiserver dbname=naisdevice" -o data/devices.csv --csv -c "select * from device;"
19+
psql "sslmode=disable host=${socket_dir}/${connection_name} user=apiserver dbname=naisdevice" -o data/gateways.csv --csv -c "select * from gateway;"
20+
psql "sslmode=disable host=${socket_dir}/${connection_name} user=apiserver dbname=naisdevice" -o data/sessions.csv --csv -c "select * from session;"
21+
22+
go run ./convert_to_sqlite.go > data/inserts.sql
23+
sqlite3 new.db < ../../pkg/apiserver/database/schema/0001_schema.up.sql
24+
sqlite3 new.db < data/inserts.sql
25+
sqlite3 new.db <<< "CREATE TABLE schema_migrations (version uint64, dirty bool); INSERT INTO schema_migrations VALUES (1, false);"
26+
27+
gcloud --project "$project" compute scp --tunnel-through-iap new.db naisdevice-apiserver:.
28+
29+
cat <<EOF | gcloud --project "$project" compute ssh --zone=europe-north1-a --tunnel-through-iap naisdevice-apiserver
30+
sudo mkdir /var/lib/naisdevice/
31+
sudo install -o root -g root -m 640 ./new.db /var/lib/naisdevice/apiserver.db
32+
sudo grep -q APISERVER_DBPATH /etc/default/apiserver || sudo tee -a /etc/default/apiserver <<< 'APISERVER_DBPATH="/var/lib/naisdevice/apiserver.db"'
33+
sudo apt-get update
34+
sudo apt-get install apiserver
35+
EOF

‎operational/scripts/convert_to_sqlite.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ sqlite> PRAGMA table_info(devices);
6666
*/
6767
func convertDevice(device map[string]string) {
6868
if device["last_updated"] == "" {
69-
// device never checked in, ingore it
70-
return
69+
device["last_updated"] = "2006-01-02 15:04:05.999999+00"
7170
}
7271

7372
fmt.Printf("INSERT INTO devices(id, username, serial, platform, healthy, last_updated, public_key, ip) VALUES(%s, '%s', '%s', '%s', %s, '%s', '%s', '%s');\n",
@@ -140,6 +139,7 @@ func convertGateway(gateway map[string]string) {
140139
}
141140

142141
/*
142+
"cat - > backupfile.tar"
143143
# source csv
144144
key,device_id,groups,object_id,expiry
145145

‎operational/scripts/refetch_data.sh

-14
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.