Superalgos node v1

This commit is contained in:
Matias committed 2019-06-22 17:19:42 -03:00
1 parent c3412f9894
commit b7cf6dcad6
13 files changed
+599 -397

No files matched your search

+1 -3
View File
@@ -4,15 +4,13 @@ FROM mhart/alpine-node:12
ARG API_DIR=/usr/src/clones-api
# Prepare required directories
RUN mkdir -p ${API_DIR} && \
mkdir -p /root/.kube
RUN mkdir -p ${API_DIR}
# Use app directory as current workdir
WORKDIR ${API_DIR}
# Copy package.json to workdir
COPY . ${API_DIR}/
COPY ./config /root/.kube
# Install dependencies
RUN npm install --production
+494 -357
View File
File diff suppressed because it is too large. Load diff
+1 -1
View File
@@ -27,7 +27,7 @@
"express": "4.16.3",
"express-graphql": "0.6.12",
"graphql": "14.0.0",
"kubernetes-client": "6.5.1",
"kubernetes-client": "8.2.0",
"luxon": "^1.13.2",
"mongoose": "5.2.12",
"winston": "3.2.1"
+3 -1
View File
@@ -19,7 +19,9 @@
"env": [
]
}
]
],
"hostNetwork": true,
"dnsPolicy": "ClusterFirstWithHostNet"
},
"metadata": {
"labels": {
+16 -8
View File
@@ -5,12 +5,18 @@ import { Client, config } from 'kubernetes-client'
import deploymentManifest from '../config/clone-deployment.json'
import { BACKTEST, COMPETITION, LIVE, NO_TIME } from '../enums/CloneMode'
import { Trading, Indicator, Sensor } from '../enums/BotTypes'
import kubeconfig from './kubeConfig'
const createClone = async (clone) => {
try {
logger.debug('createClone %s', clone.id)
const client = new Client({ config: config.fromKubeconfig(), version: '1.9' })
const Request = require('kubernetes-client/backends/request')
const backend = new Request(Request.config.fromKubeconfig(kubeconfig()))
const client = new Client({ backend, version: '1.9' })
deploymentManifest.metadata.name = clone.id
deploymentManifest.spec.template.spec.containers[0].image = process.env.CLONE_EXECUTOR_IMAGE
logger.debug('createClone Environment and Storage Configuration.')
let env = []
@@ -19,17 +25,18 @@ const createClone = async (clone) => {
'value': process.env.GATEWAY_ENDPOINT
})
env.push({
'name': 'HOST_URL',
'value': clone.host.url
'name': 'GATEWAY_ENDPOINT_K8S',
'value': process.env.GATEWAY_ENDPOINT_K8S
})
env.push({
'name': 'HOST_STORAGE',
'value': clone.host.storage
})
env.push({
'name': 'HOST_ACCESS_KEY',
'value': clone.host.accessKey
'value': 'localStorage'
})
// TODO Pending setting private bots
// env.push({
// 'name': 'HOST_ACCESS_KEY',
// 'value': clone.host.accessKey
// })
env.push({
'name': 'ACCESS_TOKEN_STRATEGY',
'value': clone.accessTokenStrategy
@@ -176,6 +183,7 @@ const createClone = async (clone) => {
logger.debug('createClone %s on Kubernates success.', clone.id)
} catch (err) {
logger.error('Error creating clone: %s', err)
throw new KubernateError(err)
}
}
+5 -2
View File
@@ -1,11 +1,14 @@
import logger from '../config/logger'
import { KubernateError } from '../errors'
import { Client, config } from 'kubernetes-client'
import kubeconfig from './kubeConfig'
const getClonePodLogs = async (cloneName) => {
try {
logger.debug('getClonePodLogs on kubernates started.')
const client = new Client({config: config.fromKubeconfig(), version: '1.9'})
const Request = require('kubernetes-client/backends/request')
const backend = new Request(Request.config.fromKubeconfig(kubeconfig()))
const client = new Client({ backend, version: '1.9' })
let query = {
'qs': {
+5 -2
View File
@@ -1,11 +1,14 @@
import logger from '../config/logger'
import { KubernateError } from '../errors'
import { Client, config } from 'kubernetes-client'
import kubeconfig from './kubeConfig'
const getClonePodStatus = async (cloneName) => {
try {
logger.debug('getClonePodStatus on kubernates started.')
const client = new Client({ config: config.fromKubeconfig(), version: '1.9' })
const Request = require('kubernetes-client/backends/request')
const backend = new Request(Request.config.fromKubeconfig(kubeconfig()))
const client = new Client({ backend, version: '1.9' })
let query = {
'qs': {
+49
View File
@@ -0,0 +1,49 @@
import logger from '../config/logger'
const kubeConfig = () => {
let clusterConfig = {}
clusterConfig.name = process.env.CLUSTER_NAME
let cluster = {}
cluster.server = process.env.CLUSTER_SERVER
if (process.env.CERTIFICATE_AUTHORITY_DATA) {
cluster['certificate-authority-data'] = process.env.CERTIFICATE_AUTHORITY_DATA
}
if (process.env.INSECURE_SKIP_TLS_VERIFY) {
cluster['insecure-skip-tls-verify'] = JSON.parse(process.env.INSECURE_SKIP_TLS_VERIFY)
}
clusterConfig.cluster = cluster
let contextConfig = {}
contextConfig.name = process.env.CONTEXT_NAME
let context = {}
context.cluster = process.env.CLUSTER_NAME
context.user = process.env.USER_NAME
contextConfig.context = context
let userConfig = {}
userConfig.name = process.env.USER_NAME
let user = {}
if (process.env.USER_PASSWORD) {
user.password = process.env.USER_PASSWORD
user.username = process.env.USER_NAME
}
if (process.env.CLIENT_CERTIFICATE_DATA) {
user['client-certificate-data'] = process.env.CLIENT_CERTIFICATE_DATA
user['client-key-data'] = process.env.CLIENT_KEY_DATA
}
userConfig.user = user
const config = {
apiVersion: 'v1',
clusters: [clusterConfig],
contexts: [contextConfig],
'current-context': process.env.CONTEXT_NAME,
kind: 'Config',
users: [userConfig]
}
return config
}
export default kubeConfig
+5 -1
View File
@@ -1,10 +1,14 @@
import logger from '../config/logger'
import { Client, config } from 'kubernetes-client'
import kubeconfig from './kubeConfig'
const removeClone = async (cloneId) => {
try {
logger.debug('removeClone %s', cloneId)
const client = new Client({config: config.fromKubeconfig(), version: '1.9'})
const Request = require('kubernetes-client/backends/request')
const backend = new Request(Request.config.fromKubeconfig(kubeconfig()))
const client = new Client({ backend, version: '1.9' })
let query = {
'qs': {
@@ -76,19 +76,6 @@ const resolve = async (parent, { clone }, context) => {
newClone.authId = context.userId
clone.id = newClone._id
let ecosystem = await ecosystemQuery(context.authorization)
for (let i = 0; i < ecosystem.devTeams.length; i++) {
const devTeam = ecosystem.devTeams[i];
if (devTeam.codeName === clone.teamSlug) {
clone.host = devTeam.host
break
}
}
if (!clone.host) {
throw new WrongArgumentsError('Team not found at the user ecosystem.')
}
logger.debug('addClone -> Creating the clone on the Database.')
let savedClone = await newClone.save()
@@ -97,8 +84,7 @@ const resolve = async (parent, { clone }, context) => {
if (isDefined(strategyResponse.data.data.strategizer_AuthorizeStrategy)) {
clone.accessTokenStrategy = strategyResponse.data.data.strategizer_AuthorizeStrategy
} else {
logger.debug('addClone -> Authorizing clone to use the strategy fail.')
throw new AuthorizationError()
logger.warn('addClone -> Authorizing clone to use the strategy fail.')
}
if (clone.mode === LIVE || clone.mode === COMPETITION) {
@@ -35,10 +35,10 @@ const resolve = async (parent, { id }, context) => {
let bot = allUserBotsResponse.data.data.teams_FbByOwner.edges[0].node
clone = cloneDetails(bot, clone)
await authorizeStrategy(context.authorization, clone.botSlug, clone.id, true)
if ((clone.mode === LIVE || clone.mode === COMPETITION) && isDefined(clone.keyId)) {
logger.debug('removeClone -> Release the clone key.')
await authorizeKey(context.authorization, clone.keyId, clone.id, true)
await authorizeStrategy(context.authorization, clone.botSlug, clone.id, true)
}
logger.debug('removeClone -> Removing Clone from Kubernates.')
+17 -5
View File
@@ -12,6 +12,8 @@ import {
} from '../../errors'
import logger from '../../config/logger'
import createKubernetesClone from '../../kubernetes/createClone'
import authorizeStrategy from '../../graphQLCalls/authorizeStrategy'
import { isDefined } from '../../config/utils'
const args = {
simulation: { type: SimulationInputType }
@@ -49,6 +51,8 @@ const resolve = async (parent, { simulation }, context) => {
logger.debug('runSimulation -> Stop running clones.')
await authorizeStrategy(context.authorization, bot.slug, clones[i]._id, true)
await removeKuberneteClone(clones[i]._id)
logger.debug('runSimulation -> Clone Removed from Kubernetes: %s.', clones[i]._id)
@@ -73,20 +77,20 @@ const resolve = async (parent, { simulation }, context) => {
if (simulatorBot !== undefined) {
if (simulation.timePeriodDailyArray !== undefined && simulation.timePeriodDailyArray.length > 0) {
simulation.timePeriod = simulation.timePeriodDailyArray
await createSimulatorClone(simulation, "Multi-Period-Daily", context.userId, simulatorBot)
await createSimulatorClone(simulation, "Multi-Period-Daily", context.userId, simulatorBot, context.authorization)
}
if (simulation.timePeriodMarketArray !== undefined && simulation.timePeriodMarketArray.length > 0) {
simulation.timePeriod = simulation.timePeriodMarketArray
await createSimulatorClone(simulation, "Multi-Period-Market", context.userId, simulatorBot)
await createSimulatorClone(simulation, "Multi-Period-Market", context.userId, simulatorBot, context.authorization)
}
if (simulation.timePeriodDailyArray === undefined && simulation.timePeriodMarketArray === undefined) {
simulation.timePeriod = ["15-min", "10-min", "05-min"]
await createSimulatorClone(simulation, "Multi-Period-Daily", context.userId, simulatorBot)
await createSimulatorClone(simulation, "Multi-Period-Daily", context.userId, simulatorBot, context.authorization)
simulation.timePeriod = ["24-hs", "12-hs", "08-hs", "06-hs", "04-hs", "03-hs", "02-hs", "01-hs"]
await createSimulatorClone(simulation, "Multi-Period-Market", context.userId, simulatorBot)
await createSimulatorClone(simulation, "Multi-Period-Market", context.userId, simulatorBot, context.authorization)
}
return "Simulation Clones Recreated."
@@ -99,7 +103,7 @@ const resolve = async (parent, { simulation }, context) => {
}
}
async function createSimulatorClone(simulation, processName, userId, simulatorBot) {
async function createSimulatorClone(simulation, processName, userId, simulatorBot, authorization) {
let clone = {
teamId: simulatorBot.team.id,
botId: simulatorBot.id,
@@ -123,6 +127,14 @@ async function createSimulatorClone(simulation, processName, userId, simulatorBo
logger.debug('runSimulation -> Creating %s clone on the Database.', processName)
await newClone.save()
logger.debug('runSimulation -> Authorizing clone to use the strategy.')
let strategyResponse = await authorizeStrategy(authorization, clone.botSlug, clone.id, false)
if (isDefined(strategyResponse.data.data.strategizer_AuthorizeStrategy)) {
clone.accessTokenStrategy = strategyResponse.data.data.strategizer_AuthorizeStrategy
} else {
logger.warn('runSimulation -> Authorizing clone to use the strategy fail.')
}
clone = cloneDetails(simulatorBot, clone)
await createKubernetesClone(clone)
+1 -1
View File
@@ -1,6 +1,6 @@
var assert = require('chai').assert
const axios = require('axios')
const { activeCloneList } = require('./activeCloneListDemo')
const { activeCloneList } = require('./activeCloneList')
require('dotenv').config()
describe('Admin clones Clones', function () {