Skip to content
This repository has been archived by the owner on Apr 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #16 from dgraph-io/arpan/dedicated-deployment
Browse files Browse the repository at this point in the history
feat: Dedicated deployments
  • Loading branch information
gja authored Feb 16, 2021
2 parents a26ac03 + f935225 commit 5ced8ab
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -563,4 +563,4 @@ EXAMPLE
```

_See code: [src/commands/update-schema.ts](https://github.com/dgraph-io/slash-graphql-cli/blob/v1.16.7/src/commands/update-schema.ts)_
<!-- commandsstop -->
<!-- commandsstop -->
65 changes: 48 additions & 17 deletions src/commands/deploy-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@ import sleep = require('sleep-promise')

const defaultRegion: Record<string, string> = {dev: 'us-test-1', stg: 'us-east-1', prod: 'us-west-2'}

const CREATE_DEPLOYMENT = `
mutation CreateDeployment($dep: NewDeployment!) {
createDeployment(input: $dep) {
uid
name
url
owner
jwtToken
deploymentMode
lambdaScript
}
}
`;

export default class DeployBackend extends BaseCommand {
static description = 'Launch a new Backend'

static examples = [
'$ slash-graphql deploy-backend "My New Backend"',
'$ slash-graphql deploy-backend "My New Backend"',
]

static aliases = ['create-backend', 'launch-backend']
Expand All @@ -21,47 +36,63 @@ export default class DeployBackend extends BaseCommand {
organizationId: flags.string({char: 'o', description: 'Organization ID', default: ''}),
subdomain: flags.string({char: 's', description: 'Subdomain'}),
mode: flags.string({char: 'm', description: 'Backend Mode', default: 'graphql', options: ['readonly', 'graphql', 'flexible']}),
type: flags.string({char: 'T', description: 'Backend Type', default: 'slash-graphql', options: ['slash-graphql', 'dedicated']}),
jaeger: flags.string({description: 'Enable Jaeger (Only works for dedicated backends)', default: 'false', options: ['true', 'false']}),
acl: flags.string({description: 'Enable ACL (Only works for dedicated backends)', default: 'false', options: ['true', 'false']}),
dgraphHA: flags.string({description: 'Enable High Availability (Only works for dedicated backends)', default: 'false', options: ['true', 'false']}),
size: flags.string({description: 'Backend Size (Only Works for dedicated backends)', default: 'small', options: ['small', 'medium', 'large', 'xlarge']}),
storage: flags.integer({description: 'Alpha Storage in GBs - Accepts Only Integers (Only Works for dedicated backends)', default: 10}),
dataFile: flags.string({description: 'Data File Path for Bulk Loader (Only works for dedicated backends)', default: ''}),
schemaFile: flags.string({description: 'Dgraph Schema File Path for Bulk Loader (Only works for dedicated backends)', default: ''}),
gqlSchemaFile: flags.string({description: 'GQL Schema File Path for Bulk Loader (Only works for dedicated backends)', default: ''}),
}

static args = [{name: 'name', description: 'Backend Name', required: true}]

async run() {
const opts = this.parse(DeployBackend)
const {apiServer, authFile} = getEnvironment(opts.flags.environment)
const {apiServer, authFile, deploymentProtocol} = getEnvironment(opts.flags.environment)

const token = await this.getAccessToken(apiServer, authFile)

if (!token) {
this.error('Please login with `slash-graphql login` before creating a backend')
}

const response = await fetch(`${apiServer}/deployments/create`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
const {errors, data} = await this.sendGraphQLRequest(apiServer, token, CREATE_DEPLOYMENT, {
dep: {
name: opts.args.name,
zone: opts.flags.region || defaultRegion[opts.flags.environment],
subdomain: opts.flags.subdomain,
deploymentMode: opts.flags.mode,
organizationId: opts.flags.organizationId,
}),
organizationUID: opts.flags.organizationId === "" ? null : opts.flags.organizationId,
enterprise: opts.flags.type === "dedicated" ? "true" : "false",
size: opts.flags.size,
storage: opts.flags.storage,
aclEnabled: opts.flags.acl,
jaegerEnabled: opts.flags.jaeger,
dgraphHA: opts.flags.dgraphHA,
bulkLoadSchemaFilePath: opts.flags.schemaFile,
bulkLoadGQLSchemaFilePath: opts.flags.gqlSchemaFile,
bulkLoadDataFilePath: opts.flags.dataFile,
},
})
if (response.status !== 200) {
this.error(`Unable to create backend. ${response.status} ${await response.text()}`)
if (errors) {
for (const {message} of errors) {
this.error("Unable to create backend. " + message)
}
return
}
const deployment = await response.json() as APIBackend
const endpoint = `https://${deployment.url}/graphql`

const deployment = data.createDeployment as APIBackend
const endpoint = `${deploymentProtocol}://${deployment.url}/graphql`

if (!opts.flags.quiet) {
this.log(`Waiting for backend to come up at ${endpoint}`)
}

await this.pollForEndpoint(endpoint)

this.log(endpoint)
this.log(`Deployment Launched at: ${endpoint}`)
}

async pollForEndpoint(endpoint: string, endTime = new Date(new Date().getTime() + (120 * 1000))) {
Expand All @@ -79,6 +110,6 @@ export default class DeployBackend extends BaseCommand {

sleep(5000)
}
this.error('Gave up waiting for backend after 2 minutes')
this.error("Looks like your backend is taking longer than usual to come up. If you are bulk loading, then it might take a little more time based on your data size.")
}
}
4 changes: 2 additions & 2 deletions src/commands/list-backends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class ListBackends extends BaseCommand {

async run() {
const opts = this.parse(ListBackends)
const {apiServer, authFile} = getEnvironment(opts.flags.environment)
const {apiServer, authFile, deploymentProtocol} = getEnvironment(opts.flags.environment)

const token = await this.getAccessToken(apiServer, authFile)
if (!token) {
Expand Down Expand Up @@ -50,7 +50,7 @@ export default class ListBackends extends BaseCommand {
extended: true,
},
endpoint: {
get: ({url}) => `https://${url}/graphql`,
get: ({url}) => `${deploymentProtocol}://${url}/graphql`,
},
}, {
printLine: this.log,
Expand Down
3 changes: 3 additions & 0 deletions src/lib/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ const environments = {
stg: {
apiServer: 'https://api.thegaas.com',
authFile: 'auth-stg.yml',
deploymentProtocol: 'https',
},
dev: {
apiServer: 'http://localhost:8070',
authFile: 'auth-stg.yml',
deploymentProtocol: 'http',
},
prod: {
apiServer: 'https://api.cloud.dgraph.io',
authFile: 'auth.yml',
deploymentProtocol: 'https',
},
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export abstract class BaseCommand extends Command {
}

async backendFromOpts(opts: Output<{ endpoint: string | undefined; token: string | undefined; environment: string }, any>): Promise<Backend> {
const {apiServer, authFile} = getEnvironment(opts.flags.environment)
const endpoint = await this.convertToGraphQLEndpoint(apiServer, authFile, opts.flags.endpoint)
const {apiServer, authFile, deploymentProtocol} = getEnvironment(opts.flags.environment)
const endpoint = await this.convertToGraphQLEndpoint(apiServer, authFile, deploymentProtocol, opts.flags.endpoint)
if (!endpoint) {
this.error('Please pass an endpoint or cluster id with the -e flag')
}
Expand Down Expand Up @@ -207,7 +207,7 @@ export abstract class BaseCommand extends Command {
return backends.find(backend => backend.url === hotname) || null
}

async convertToGraphQLEndpoint(apiServer: string, authFile: string, endpoint: string | undefined): Promise<string | null> {
async convertToGraphQLEndpoint(apiServer: string, authFile: string, deploymentProtocol: string, endpoint: string | undefined): Promise<string | null> {
if (!endpoint) {
return null
}
Expand All @@ -227,7 +227,7 @@ export abstract class BaseCommand extends Command {
this.error(`Cannot find backend ${endpoint}`)
}

return `https://${backend.url}/graphql`
return `${deploymentProtocol}://${backend.url}/graphql`
}

async convertToGraphQLUid(apiServer: string, authFile: string, endpoint: string | undefined): Promise<string | null> {
Expand Down

0 comments on commit 5ced8ab

Please sign in to comment.