diff --git a/.drone.yml b/.drone.yml index 7a18b66..e0fbecc 100644 --- a/.drone.yml +++ b/.drone.yml @@ -4,56 +4,88 @@ name: BB_PIPELINE steps: -# - name: deploy-container-db -# image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest -# environment: -# IMAGENAME: mariadb:10 -# CONTAINERNAME: bigbrotherdb -# COMMAND: create -# OVERWRITE: false -# PRIVATE: true -# CODEFIRST_CLIENTDRONE_ENV_MARIADB_ROOT_PASSWORD: -# from_secret: db_root_password -# CODEFIRST_CLIENTDRONE_ENV_MARIADB_DATABASE: -# from_secret: db_database -# CODEFIRST_CLIENTDRONE_ENV_MARIADB_USER: -# from_secret: db_user -# CODEFIRST_CLIENTDRONE_ENV_MARIADB_PASSWORD: -# from_secret: db_password -# -# - name: create-bigbrother-database -# image: python:latest -# commands: -# - cd database/ -# - pip3 install mariadb -# - python3 db-creation.py -# environment: -# MARIADB_USER: -# from_secret: db_user -# MARIADB_PASSWORD: -# from_secret: db_password -# MARIADB_DATABASE: -# from_secret: db_database -# depends_on: [ deploy-container-db ] -# -# - name: build-image-server + - name: deploy-container-db + image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest + environment: + IMAGENAME: mariadb:10 + CONTAINERNAME: bigbrotherdb + COMMAND: create + OVERWRITE: false + PRIVATE: true + CODEFIRST_CLIENTDRONE_ENV_MARIADB_ROOT_PASSWORD: + from_secret: db_root_password + CODEFIRST_CLIENTDRONE_ENV_MARIADB_DATABASE: + from_secret: db_database + CODEFIRST_CLIENTDRONE_ENV_MARIADB_USER: + from_secret: db_user + CODEFIRST_CLIENTDRONE_ENV_MARIADB_PASSWORD: + from_secret: db_password + + - name: create-bigbrother-database + image: python:latest + commands: + - cd database/ + - pip3 install mariadb + - python3 db-creation.py + environment: + MARIADB_USER: + from_secret: db_user + MARIADB_PASSWORD: + from_secret: db_password + MARIADB_DATABASE: + from_secret: db_database + depends_on: [ deploy-container-db ] + + - name: build-image-server + image: plugins/docker + settings: + dockerfile: callback-server/Dockerfile + context: ./ + registry: hub.codefirst.iut.uca.fr + repo: hub.codefirst.iut.uca.fr/felix.mielcarek/big-brother-server + username: + from_secret: SECRET_REGISTRY_USERNAME + password: + from_secret: SECRET_REGISTRY_PASSWORD + depends_on: [ create-bigbrother-database ] + + - name: deploy-container-server + image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest + environment: + IMAGENAME: hub.codefirst.iut.uca.fr/felix.mielcarek/big-brother-server:latest + CONTAINERNAME: bigbrotherserver + COMMAND: create + OVERWRITE: true + MARIADB_USER: + from_secret: db_user + MARIADB_PASSWORD: + from_secret: db_password + MARIADB_DATABASE: + from_secret: db_database + CLIENT_ID: + from_secret: spotify_client_id + CLIENT_SECRET: + from_secret: spotify_client_id + depends_on: [ build-image-server ] + +# - name: build-image-script # image: plugins/docker # settings: -# dockerfile: callback-server/Dockerfile +# dockerfile: script/Dockerfile # context: ./ # registry: hub.codefirst.iut.uca.fr -# repo: hub.codefirst.iut.uca.fr/felix.mielcarek/big-brother-server +# repo: hub.codefirst.iut.uca.fr/felix.mielcarek/big-brother # username: # from_secret: SECRET_REGISTRY_USERNAME # password: # from_secret: SECRET_REGISTRY_PASSWORD # depends_on: [ create-bigbrother-database ] # -# - name: deploy-container-server +# - name: deploy-container-script # image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest # environment: -# IMAGENAME: hub.codefirst.iut.uca.fr/felix.mielcarek/big-brother-server:latest -# CONTAINERNAME: bigbrotherserver +# IMAGENAME: hub.codefirst.iut.uca.fr/felix.mielcarek/big-brother:latest +# CONTAINERNAME: bigbrotherscript # COMMAND: create # OVERWRITE: true # MARIADB_USER: @@ -66,36 +98,4 @@ steps: # from_secret: spotify_client_id # CLIENT_SECRET: # from_secret: spotify_client_id -# depends_on: [ build-image-server ] - - - name: build-image-script - image: plugins/docker - settings: - dockerfile: script/Dockerfile - context: ./ - registry: hub.codefirst.iut.uca.fr - repo: hub.codefirst.iut.uca.fr/felix.mielcarek/big-brother - username: - from_secret: SECRET_REGISTRY_USERNAME - password: - from_secret: SECRET_REGISTRY_PASSWORD - #depends_on: [ create-bigbrother-database ] - - - name: deploy-container-script - image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest - environment: - IMAGENAME: hub.codefirst.iut.uca.fr/felix.mielcarek/big-brother:latest - CONTAINERNAME: bigbrotherscript - COMMAND: create - OVERWRITE: true - MARIADB_USER: - from_secret: db_user - MARIADB_PASSWORD: - from_secret: db_password - MARIADB_DATABASE: - from_secret: db_database - CLIENT_ID: - from_secret: spotify_client_id - CLIENT_SECRET: - from_secret: spotify_client_id - depends_on: [ build-image-script ] \ No newline at end of file +# depends_on: [ build-image-script ] \ No newline at end of file diff --git a/callback-server/app.js b/callback-server/app.js index 6de6c33..39a6ccb 100644 --- a/callback-server/app.js +++ b/callback-server/app.js @@ -20,15 +20,15 @@ app.listen(port, () => { console.log(`Big brother is listening on port ${port}`) //#region ACCESS TOKEN app.get('/settings/deactivate', async (req,res) => { - const client = mariadb.createPool({ + const code = req.query.code; + + const pool = mariadb.createPool({ host: 'felixmielcarek-bigbrotherdb', user: process.env.MARIADB_USER, database: process.env.MARIADB_DATABASE, password: process.env.MARIADB_PASSWORD }); - const code = req.query.code; - var authOptions = { url: 'https://accounts.spotify.com/api/token', method: 'post', json: true, data: { code: code, redirect_uri: redirectUri, grant_type: 'authorization_code' }, @@ -41,29 +41,30 @@ app.get('/settings/deactivate', async (req,res) => { const response1 = await axios(authOptions); const accessToken = response1.data.access_token + let conn; + try { + conn = await pool.getConnection() const response2 = await axios.get(`https://api.spotify.com/v1/me`, { headers: { 'Authorization': 'Bearer ' + accessToken, } }); - await client.getConnection() - const sqlQuery = ` DELETE FROM users WHERE spotifyid = ?; `; - client.query(sqlQuery, [response2.data.SpotifyId], (err, res) => { + conn.query(sqlQuery, [response2.data.SpotifyId], (err, res) => { if (err) { console.error('Error executing query', err); return; } console.log('Data deleted successfully'); - client.end(); + conn.end(); }); } catch (error) { console.log('Error getting user Spotify id') } }) app.get('/', async (req, res) => { - const client = mariadb.createPool({ + const pool = mariadb.createPool({ host: 'felixmielcarek-bigbrotherdb', user: process.env.MARIADB_USER, database: process.env.MARIADB_DATABASE, @@ -85,7 +86,11 @@ app.get('/', async (req, res) => { const accessToken = response1.data.access_token const refreshToken = response1.data.refresh_token + let conn; + try { + conn = await pool.getConnection() + const response2 = await axios.get(`https://api.spotify.com/v1/me`, { headers: { 'Authorization': 'Bearer ' + accessToken, } }); const data = { @@ -94,8 +99,6 @@ app.get('/', async (req, res) => { RefreshToken: refreshToken }; - await client.getConnection() - const sqlQuery = ` INSERT INTO users (spotifyid, accesstoken, refreshtoken) VALUES (?, ?, ?) @@ -104,13 +107,13 @@ app.get('/', async (req, res) => { refreshtoken = VALUES(refreshtoken); `; - client.query(sqlQuery, [data.SpotifyId, data.AccessToken, data.RefreshToken], (err, res) => { + conn.query(sqlQuery, [data.SpotifyId, data.AccessToken, data.RefreshToken], (err, res) => { if (err) { console.error('Error executing query', err); return; } console.log('Data inserted/updated successfully'); - client.end(); + conn.end(); }); } catch (error) { console.log('Error getting user Spotify id') } }); diff --git a/script/app.js b/script/app.js index 49a55ff..e87ccdc 100644 --- a/script/app.js +++ b/script/app.js @@ -8,12 +8,6 @@ const spotifyRequestsLimit = 50; const thresholdLove = 0.6; const clientId = process.env.CLIENT_ID; const clientSecret = process.env.CLIENT_SECRET; -const client = mariadb.createPool({ - host: 'felixmielcarek-bigbrotherdb', - user: process.env.MARIADB_USER, - database: process.env.MARIADB_DATABASE, - password: process.env.MARIADB_PASSWORD -}); //#endregion //#region STRUCTURE @@ -176,11 +170,20 @@ async function mainAlgorithm(accessToken) { } async function main() { - await client.getConnection(); + const pool = mariadb.createPool({ + host: 'felixmielcarek-bigbrotherdb', + user: process.env.MARIADB_USER, + database: process.env.MARIADB_DATABASE, + password: process.env.MARIADB_PASSWORD + }); + + try { + conn = await pool.getConnection(); + const selectQuery = 'SELECT * FROM users'; - const selectResult = await client.query(selectQuery); + const selectResult = await conn.query(selectQuery); for(let row of selectResult.rows) { const spotifyId = row.spotifyid; @@ -209,11 +212,11 @@ async function main() { SET accesstoken = ?, refreshtoken = ? WHERE spotifyid = ?; `; - await client.query(updateQuery, [spotifyId, newAccessToken, newRefreshToken]) + await conn.query(updateQuery, [spotifyId, newAccessToken, newRefreshToken]) await mainAlgorithm(newAccessToken); } - } catch (error) { console.error('Error executing select query:', error) } finally { await client.end() } + } catch (error) { console.error('Error executing select query:', error) } finally { await conn.end() } } //#endregion