diff --git a/docker/api/bootstrap-data.js b/docker/api/bootstrap-data.js index c4073638..4478472d 100644 --- a/docker/api/bootstrap-data.js +++ b/docker/api/bootstrap-data.js @@ -83,51 +83,57 @@ const bootstrap = function ({ docker, rethink }, cb) { return cb(); } - data.createDatacenter({ region, name: region }, (err, datacenter) => { + data.createDatacenter({ + region, + name: region + }, (err, datacenter) => { if (err) { return cb(err); } - Triton.createClient( - { - profile: settings.triton - }, - (err, { cloudapi }) => { + Triton.createClient({ + profile: settings.triton + }, (err, { cloudapi }) => { + if (err) { + return cb(err); + } + + cloudapi.getAccount((err, { + id, + firstName, + lastName, + email, + login + }) => { if (err) { return cb(err); } - cloudapi.getAccount((err, { firstName, lastName, email, login }) => { + data.createUser({ + tritonId: id, + firstName, + lastName, + email, + login + }, (err, user) => { if (err) { return cb(err); } - data.createUser( - { firstName, lastName, email, login }, - (err, user) => { - if (err) { - return cb(err); - } - - data.createPortal( - { - user, - datacenter - }, - (err, portal) => { - if (err) { - return cb(err); - } - - console.log('data bootstrapped'); - cb(); - } - ); + data.createPortal({ + user, + datacenter + }, (err, portal) => { + if (err) { + return cb(err); } - ); + + console.log('data bootstrapped'); + cb(); + }); }); - } - ); + }); + }); }); }); }); diff --git a/packages/cp-frontend/package.json b/packages/cp-frontend/package.json index fb535635..74b01c09 100644 --- a/packages/cp-frontend/package.json +++ b/packages/cp-frontend/package.json @@ -1,6 +1,6 @@ { "name": "joyent-cp-frontend", - "version": "1.3.2", + "version": "1.4.0", "license": "MPL-2.0", "repository": "github:yldio/joyent-portal", "main": "build/", diff --git a/packages/cp-frontend/src/containers/manifest/edit-or-create.js b/packages/cp-frontend/src/containers/manifest/edit-or-create.js index 13883f8c..d034666b 100644 --- a/packages/cp-frontend/src/containers/manifest/edit-or-create.js +++ b/packages/cp-frontend/src/containers/manifest/edit-or-create.js @@ -5,6 +5,7 @@ import { withRouter } from 'react-router'; import { Redirect } from 'react-router-dom'; import intercept from 'apr-intercept'; import paramCase from 'param-case'; +import get from 'lodash.get'; import remove from 'lodash.remove'; import flatten from 'lodash.flatten'; import uniq from 'lodash.uniq'; @@ -17,12 +18,15 @@ import DeploymentGroupBySlugQuery from '@graphql/DeploymentGroupBySlug.gql'; import DeploymentGroupCreateMutation from '@graphql/DeploymentGroupCreate.gql'; import DeploymentGroupProvisionMutation from '@graphql/DeploymentGroupProvision.gql'; import DeploymentGroupConfigQuery from '@graphql/DeploymentGroupConfig.gql'; +import PortalQuery from '@graphql/Portal.gql'; import { client } from '@state/store'; import { ErrorMessage } from '@components/messaging'; import { Environment, Name, Review, Manifest } from '@components/manifest'; const INTERPOLATE_REGEX = /\$([_a-z][_a-z0-9]*)/gi; +const CNS_PRIVATE = 'TRITON_CNS_SEARCH_DOMAIN_PRIVATE'; +const CNS_PUBLIC = 'TRITON_CNS_SEARCH_DOMAIN_PUBLIC'; // TODO: move state to redux. why: because in redux we can cache transactional // state between refreshes @@ -166,15 +170,22 @@ class DeploymentGroupEditOrCreate extends Component { return environment; } - const names = forceArray(manifest.match(INTERPOLATE_REGEX)).map(name => - name.replace(/^\$/, '') - ); + const searchDomain = [ + `${CNS_PRIVATE}=${this.props.tritonId}.${this.props + .dataCenter}.cns.joyent.com`, + `${CNS_PUBLIC}=${this.props.tritonId}.${this.props + .dataCenter}.triton.zone` + ].join('\n'); + + const names = forceArray(manifest.match(INTERPOLATE_REGEX)) + .map(name => name.replace(/^\$/, '')) + .filter(name => [CNS_PRIVATE, CNS_PUBLIC].indexOf(name) < 0); const vars = uniq(names) .map(name => `\n${name}=`) .join(''); - return `# define your interpolatable variables here\n${vars}`; + return `${searchDomain}\n\n# define your interpolatable variables here\n${vars}`; } getDefaultFile(name = '') { @@ -317,7 +328,10 @@ class DeploymentGroupEditOrCreate extends Component { }; this.setState( - { environment: environment || this.props.environment, loading: true }, + { + environment: environment || this.getEnvironmentDefaultValue(), + loading: true + }, getConfig ); } @@ -480,6 +494,12 @@ class DeploymentGroupEditOrCreate extends Component { } export default compose( + graphql(PortalQuery, { + props: ({ data: { portal = {} } }) => ({ + dataCenter: get(portal, 'datacenter.region', ''), + tritonId: get(portal, 'user.tritonId', '') + }) + }), graphql(DeploymentGroupCreateMutation, { props: ({ mutate }) => ({ createDeploymentGroup: variables => mutate({ variables }) diff --git a/packages/cp-frontend/src/graphql/Portal.gql b/packages/cp-frontend/src/graphql/Portal.gql index ceeb4497..f734864a 100644 --- a/packages/cp-frontend/src/graphql/Portal.gql +++ b/packages/cp-frontend/src/graphql/Portal.gql @@ -2,6 +2,7 @@ query Portal { portal { user { firstName + tritonId } datacenter { id diff --git a/packages/cp-gql-schema/schema.gql b/packages/cp-gql-schema/schema.gql index b307e7bc..4377fc62 100644 --- a/packages/cp-gql-schema/schema.gql +++ b/packages/cp-gql-schema/schema.gql @@ -7,6 +7,7 @@ type Portal { type User { id: ID! + tritonId: ID! firstName: String! lastName: String! email: String! diff --git a/packages/portal-api/bootstrap-data.js b/packages/portal-api/bootstrap-data.js index de06d4fe..7fa1d3c9 100644 --- a/packages/portal-api/bootstrap-data.js +++ b/packages/portal-api/bootstrap-data.js @@ -49,36 +49,77 @@ const ifError = function (err) { console.error(err); process.exit(1); } + + process.exit(0); }; -const bootstrap = function () { +const bootstrap = function (cb) { const data = new Data(settings); const region = process.env.TRITON_DC || 'us-sw-1'; data.connect((err) => { - ifError(err); + if (err) { + return cb(err); + } - data.createDatacenter({ region, name: region }, (err, datacenter) => { - ifError(err); + data.getDatacenters((err, datacenters) => { + if (err) { + return cb(err); + } - Triton.createClient({ - profile: settings.triton - }, (err, { cloudapi }) => { - ifError(err); + // Don't continue since data is already bootstrapped + if (datacenters && datacenters.length) { + return cb(); + } - cloudapi.getAccount((err, { firstName, lastName, email, login }) => { - ifError(err); + data.createDatacenter({ + region, + name: region + }, (err, datacenter) => { + if (err) { + return cb(err); + } - data.createUser({ firstName, lastName, email, login }, (err, user) => { - ifError(err); + Triton.createClient({ + profile: settings.triton + }, (err, { cloudapi }) => { + if (err) { + return cb(err); + } - data.createPortal({ - user, - datacenter - }, (err, portal) => { - ifError(err); - console.log('data bootstrapped'); - process.exit(0); + cloudapi.getAccount((err, { + id, + firstName, + lastName, + email, + login + }) => { + if (err) { + return cb(err); + } + + data.createUser({ + tritonId: id, + firstName, + lastName, + email, + login + }, (err, user) => { + if (err) { + return cb(err); + } + + data.createPortal({ + user, + datacenter + }, (err, portal) => { + if (err) { + return cb(err); + } + + console.log('data bootstrapped'); + cb(); + }); }); }); }); @@ -87,4 +128,4 @@ const bootstrap = function () { }); }; -bootstrap(); +bootstrap(ifError); diff --git a/packages/portal-api/lib/data/transform.js b/packages/portal-api/lib/data/transform.js index a8704237..5aecbec3 100644 --- a/packages/portal-api/lib/data/transform.js +++ b/packages/portal-api/lib/data/transform.js @@ -201,6 +201,7 @@ exports.toInstance = function (clientInstance) { exports.fromUser = function (user) { return { id: user.id, + tritonId: user.tritonId, firstName: user.first_name, lastName: user.last_name, email: user.email, @@ -211,6 +212,7 @@ exports.fromUser = function (user) { exports.toUser = function (clientUser) { return { id: clientUser.id, + tritonId: clientUser.tritonId, first_name: clientUser.firstName, last_name: clientUser.lastName, email: clientUser.email, diff --git a/packages/portal-api/server.js b/packages/portal-api/server.js index 0b1861da..13900383 100644 --- a/packages/portal-api/server.js +++ b/packages/portal-api/server.js @@ -23,7 +23,11 @@ const goodOptions = { consoleReporter: [{ module: 'good-squeeze', name: 'Squeeze', - args: [{ log: '*', response: '*', error: '*' }] + args: [{ + log: '*', + response: '*', + error: '*' + }] }, { module: 'good-console' }, 'stdout'] @@ -39,17 +43,18 @@ server.register([ }, { register: Toppsy, - options: { namespace: 'portal', subsystem: 'api' } + options: { + namespace: 'portal', + subsystem: 'api' + } } -], -(err) => { +], (err) => { handlerError(err); server.start((err) => { handlerError(err); console.log(`server started at http://localhost:${server.info.port}`); }); -} -); +}); function handlerError (error) { if (error) {