Commit e9bf7776 authored by Ahmad Nemati's avatar Ahmad Nemati

init

parent 2af272c5
const HetznerCloud = require('hcloud-js')
let client
let _ = require('lodash')
let tokens
let oldG = '#!/bin/bash\n' +
'cd /root\n' +
'git clone http://git.fcfglobal.co/root/mt2.git\n' +
' mv mt2 mt\n' +
'cd mt\n' +
'sudo chmod 777 *\n' +
'sudo ./install.sh\n' +
'cd ..\n' +
' git clone http://git.fcfglobal.co/root/newhet.git\n' +
' cd newhet\n' +
'echo ahmad > token.txt\n' +
'sudo chmod 777 *\n' +
'sudo ./run.sh\n' +
'sleep 20'
let newG = '#!/bin/bash\n' +
'cd /root\n' +
'git clone http://git.fcfglobal.co/root/Hetzner-Mt2-NewGeneration.git\n' +
' mv Hetzner-Mt2-NewGeneration mt\n' +
'cd mt\n' +
'sudo chmod 777 *\n' +
'sudo ./install.sh\n' +
'cd ..\n' +
' git clone http://git.fcfglobal.co/root/Hetzner-NewGeneration.git\n' +
' mv Hetzner-NewGeneration newhet\n' +
' cd newhet\n' +
'echo ahmad > token.txt\n' +
'sudo chmod 777 *\n' +
'sudo ./run.sh\n' +
'sleep 20'
class Hetzner {
......@@ -8,6 +41,7 @@ class Hetzner {
constructor(token) {
client = new HetznerCloud.Client(_.trim(token))
tokens = _.trim(token)
}
......@@ -81,7 +115,6 @@ class Hetzner {
}
async removeFloatingIpByIp(ip) {
try {
ip = _.trim(ip)
......@@ -114,7 +147,6 @@ class Hetzner {
await this.delFloatingIp(ips[i].id)
}
} catch (e) {
......@@ -147,31 +179,38 @@ class Hetzner {
}
createServer(region, newGeneration) {
let rand = this.randomInt(0, 100000)
rand = 'Server' + rand
return new Promise(function (resolve, reject) {
createServer(name) {
let command = newGeneration === true ? newG : oldG
// nuremberg nbg1
//fsn1
command=command.replace('ahmad',tokens)
return new Promise(function (resolve, reject) {
client.servers.build(name)
.serverType('cx51-ceph')
.location('fsn1')
.image('9541194')
.sshKey('ffgf')
client.servers.build(rand)
.serverType('cx51')
.location(region)
.userData(command)
.image('ubuntu-18.04')
.create()
.then(function (response) {
resolve(response)
})
.catch(function (error) {
console.log(error)
reject(error)
// Handle error...
})
})
}
randomInt(low, high) {
return Math.floor(Math.random() * (high - low) + low)
}
createFloatingIp(location, server) {
......
......@@ -35,6 +35,12 @@ app.get('/add/:num/', (req, res) => {
})
app.get('/createServer/:num/', (req, res) => {
// console.log(req.params.acc)
return newServerOrder(req, res)
})
app.get('/del/', (req, res) => {
// console.log(req.params.acc)
res.send('Done')
......@@ -73,6 +79,38 @@ async function newFloatingIpsOrder(req, res) {
// addBaship(req.params.ip)
}
async function newServerOrder(req, res) {
let order = parseInt(req.params.num)
res.send('Create Server from ' + (order === 1 ? 'Nuremberg' : order ===2 ?'Falkenstein' : 'Helsinki') + ' Requested')
let ips = await het.getAllServers()
if (ips.length === 10)
return
let floatNum = 10 - ips.length
let location
if (order === 1)
location = constant.nuremberg
else if (order === 2)
location = constant.falkenstein
else
location = constant.helsinki
let newIps = []
for (let i = 1; i <= floatNum; i++) {
let ip = await het.createServer(location, order === 3)
addBaship(ip)
newIps.push(ip)
}
// console.log(req.params.ip)
// addBaship(req.params.ip)
}
async function addBaship(ip) {
return shell.exec(' ip addr add ' + ip + ' dev eth0').stderr;
......@@ -105,10 +143,13 @@ cronRemover.schedule(' */1 * * * *', () => {
});
init()
async function init() {
await initToken()
addMaster()
postRequest()
checkIps()
}
......@@ -133,7 +174,6 @@ async function checkIps() {
}
}
}
......@@ -239,6 +279,19 @@ function getIp() {
}
function addMaster() {
const request = require('request');
let url = 'http://admin.fcfglobal.co:3000/addMaster/'
return new Promise(function (resolve, reject) {
request(url, {timeout: 15000}, function (error, res, body) {
resolve(body)
});
});
}
function postRequest() {
let data = {generation: generation, groups: groups}
data = JSON.stringify(data)
......
module.exports = Object.freeze({
falkenstein: 'fsn1',
nuremberg:'nbg1'
nuremberg:'nbg1',
helsinki:'hel1'
......
......@@ -15,13 +15,13 @@ const cryptLib = require('@skavinvarnan/cryptlib');
const key = "6*sN_rZxHD4!X$=T";
let Hetzner = require('./Hetzner')
let het
postFloatingIps(['127.0.0.2','127.0.0.3'])
tesT()
async function tesT()
{
await init()
// let masterid=await het.getMasterServerId()
let d=await het.removeAllFloatingIps('78.47.38.44')
console.log(d)
let d=await het.getAllServers('fsn1')
console.log(d.length)
}
......
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