Commit 47ab20de authored by Ahmad Nemati's avatar Ahmad Nemati

init

parent 17f328ff
......@@ -31,14 +31,17 @@ app.get('/ip/:ip/', (req, res) => {
})
async function addBaship(ip) {
return shell.exec(' ip addr add ' + ip + ' dev eth0').stderr;
let command='sudo ifconfig eth0:' + randomInt(0, 99999) + ' ' + ip + ' netmask 255.255.255.0 up'
console.log(command)
return shell.exec(command).stderr;
}
function randomInt(low, high) {
return Math.floor(Math.random() * (high - low) + low)
}
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
......@@ -69,18 +72,25 @@ init()
async function init() {
await initToken()
// postRequest()
// postRequest()
checkIps()
}
async function addIPs(ip) {
await aruba.attachedToServerByIp(dc, ip)
await aruba.attachedToServerByIp( ip)
addBaship(ip)
await postFloatingIps([ip])
}
async function justDoIp(ip)
{
addBaship(ip)
await postFloatingIps([ip])
}
async function checkIps() {
let ips = await aruba.getAllIps(dc)
let ips = await aruba.getAllIps()
for (let i = 0; i < ips.length; i++) {
let status = await getWorkFloating(ips[i].Value)
......@@ -94,10 +104,10 @@ async function checkIps() {
await addIPs(ips[i].Value)
break
case 0:
await aruba.RemovedByIp(ips[i].Value)
await aruba.RemovedByIp(ips[i].Value)
break
case 1:
// await addIPs(ips[i].Value)
await justDoIp(ips[i].Value)
break
}
......@@ -112,7 +122,6 @@ async function sleep(millis) {
async function initToken() {
await aruba.init()
dc = aruba.getDc()
// remover()
......@@ -121,7 +130,7 @@ async function initToken() {
async function addNewIp(req, res) {
res.send('Done')
await aruba.createFloatingIp(dc)
await aruba.createFloatingIp()
checkIps()
}
......@@ -151,8 +160,6 @@ function postFloatingIps(data) {
}
function getWork() {
const request = require('request');
let url = 'http://admin.fcfglobal.co:3000/work/'
......
......@@ -92,7 +92,7 @@ async function getDetailsofServer(dc, id) {
return d
}
async function createFloatingIp(dc) {
async function createFloatingIp() {
let url = generateUrl(foundDc, 'SetPurchaseIpAddress')
let d = await doRequest(url, auth)
......@@ -101,8 +101,8 @@ async function createFloatingIp(dc) {
return d
}
async function getAllIps(dc) {
let url = generateUrl(dc, 'GetPurchasedIpAddresses')
async function getAllIps() {
let url = generateUrl(foundDc, 'GetPurchasedIpAddresses')
let d = await doRequest(url, auth)
......@@ -115,7 +115,9 @@ async function getAllIps(dc) {
return arr
}
async function deleteAllIps(dc) {
async function deleteAllIps() {
masterIp=await getIp()
let ips = await getAllIps(foundDc)
for (let i = 0; i < ips.length; i++) {
if (ips[i].Value !== masterIp) {
......@@ -130,7 +132,7 @@ async function deleteAllIps(dc) {
}
async function attachedToServer(dc, resourceId) {
async function attachedToServer( resourceId) {
let url = generateUrl(foundDc, 'SetEnqueueAssociateIpAddress')
let body = auth
......@@ -143,7 +145,7 @@ async function attachedToServer(dc, resourceId) {
}
async function unAttachedToServer(dc, resourceId) {
async function unAttachedToServer( resourceId) {
let url = generateUrl(foundDc, 'SetEnqueueDeassociateIpAddress')
let body = auth
......@@ -156,7 +158,7 @@ async function unAttachedToServer(dc, resourceId) {
}
async function attachedToServerByIp(dc, ip) {
async function attachedToServerByIp( ip) {
let ips = await getAllIps(foundDc)
for (let i = 0; i < ips.length; i++) {
......@@ -173,9 +175,9 @@ async function attachedToServerByIp(dc, ip) {
}
async function RemovedByIp(dc, ip) {
let ips = await getAllIps(dc)
async function RemovedByIp( ip) {
let ips = await getAllIps(foundDc)
masterIp=await getIp()
for (let i = 0; i < ips.length; i++) {
if (ips[i].Value === ip && ip !== masterIp) {
await deleteIp(foundDc, ips[i].ResourceId)
......@@ -189,7 +191,7 @@ async function RemovedByIp(dc, ip) {
}
async function deleteIp(dc, id) {
async function deleteIp(id) {
await unAttachedToServer(foundDc, id)
let url = generateUrl(foundDc, 'SetRemoveIpAddress')
let body = auth
......@@ -201,9 +203,6 @@ async function deleteIp(dc, id) {
}
function getDc() {
return foundDc
}
async function doRequest(url, body) {
......@@ -302,7 +301,6 @@ module.exports =
{
init: init,
getAllIps: getAllIps,
getDc: getDc,
attachedToServerByIp: attachedToServerByIp,
RemovedByIp: RemovedByIp,
createFloatingIp: createFloatingIp
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment