feat: CNS env variables (#675)

This commit is contained in:
Sérgio Ramos 2017-09-13 20:32:32 +01:00 committed by Wyatt Preul
parent e70d80425a
commit 3b427871cf
8 changed files with 139 additions and 63 deletions

View File

@ -83,51 +83,57 @@ const bootstrap = function ({ docker, rethink }, cb) {
return cb(); return cb();
} }
data.createDatacenter({ region, name: region }, (err, datacenter) => { data.createDatacenter({
region,
name: region
}, (err, datacenter) => {
if (err) { if (err) {
return cb(err); return cb(err);
} }
Triton.createClient( Triton.createClient({
{
profile: settings.triton profile: settings.triton
}, }, (err, { cloudapi }) => {
(err, { cloudapi }) => {
if (err) { if (err) {
return cb(err); return cb(err);
} }
cloudapi.getAccount((err, { firstName, lastName, email, login }) => { cloudapi.getAccount((err, {
id,
firstName,
lastName,
email,
login
}) => {
if (err) { if (err) {
return cb(err); return cb(err);
} }
data.createUser( data.createUser({
{ firstName, lastName, email, login }, tritonId: id,
(err, user) => { firstName,
lastName,
email,
login
}, (err, user) => {
if (err) { if (err) {
return cb(err); return cb(err);
} }
data.createPortal( data.createPortal({
{
user, user,
datacenter datacenter
}, }, (err, portal) => {
(err, portal) => {
if (err) { if (err) {
return cb(err); return cb(err);
} }
console.log('data bootstrapped'); console.log('data bootstrapped');
cb(); cb();
}
);
}
);
}); });
} });
); });
});
}); });
}); });
}); });

View File

@ -1,6 +1,6 @@
{ {
"name": "joyent-cp-frontend", "name": "joyent-cp-frontend",
"version": "1.3.2", "version": "1.4.0",
"license": "MPL-2.0", "license": "MPL-2.0",
"repository": "github:yldio/joyent-portal", "repository": "github:yldio/joyent-portal",
"main": "build/", "main": "build/",

View File

@ -5,6 +5,7 @@ import { withRouter } from 'react-router';
import { Redirect } from 'react-router-dom'; import { Redirect } from 'react-router-dom';
import intercept from 'apr-intercept'; import intercept from 'apr-intercept';
import paramCase from 'param-case'; import paramCase from 'param-case';
import get from 'lodash.get';
import remove from 'lodash.remove'; import remove from 'lodash.remove';
import flatten from 'lodash.flatten'; import flatten from 'lodash.flatten';
import uniq from 'lodash.uniq'; import uniq from 'lodash.uniq';
@ -17,12 +18,15 @@ import DeploymentGroupBySlugQuery from '@graphql/DeploymentGroupBySlug.gql';
import DeploymentGroupCreateMutation from '@graphql/DeploymentGroupCreate.gql'; import DeploymentGroupCreateMutation from '@graphql/DeploymentGroupCreate.gql';
import DeploymentGroupProvisionMutation from '@graphql/DeploymentGroupProvision.gql'; import DeploymentGroupProvisionMutation from '@graphql/DeploymentGroupProvision.gql';
import DeploymentGroupConfigQuery from '@graphql/DeploymentGroupConfig.gql'; import DeploymentGroupConfigQuery from '@graphql/DeploymentGroupConfig.gql';
import PortalQuery from '@graphql/Portal.gql';
import { client } from '@state/store'; import { client } from '@state/store';
import { ErrorMessage } from '@components/messaging'; import { ErrorMessage } from '@components/messaging';
import { Environment, Name, Review, Manifest } from '@components/manifest'; import { Environment, Name, Review, Manifest } from '@components/manifest';
const INTERPOLATE_REGEX = /\$([_a-z][_a-z0-9]*)/gi; 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 // TODO: move state to redux. why: because in redux we can cache transactional
// state between refreshes // state between refreshes
@ -166,15 +170,22 @@ class DeploymentGroupEditOrCreate extends Component {
return environment; return environment;
} }
const names = forceArray(manifest.match(INTERPOLATE_REGEX)).map(name => const searchDomain = [
name.replace(/^\$/, '') `${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) const vars = uniq(names)
.map(name => `\n${name}=`) .map(name => `\n${name}=`)
.join(''); .join('');
return `# define your interpolatable variables here\n${vars}`; return `${searchDomain}\n\n# define your interpolatable variables here\n${vars}`;
} }
getDefaultFile(name = '') { getDefaultFile(name = '') {
@ -317,7 +328,10 @@ class DeploymentGroupEditOrCreate extends Component {
}; };
this.setState( this.setState(
{ environment: environment || this.props.environment, loading: true }, {
environment: environment || this.getEnvironmentDefaultValue(),
loading: true
},
getConfig getConfig
); );
} }
@ -480,6 +494,12 @@ class DeploymentGroupEditOrCreate extends Component {
} }
export default compose( export default compose(
graphql(PortalQuery, {
props: ({ data: { portal = {} } }) => ({
dataCenter: get(portal, 'datacenter.region', ''),
tritonId: get(portal, 'user.tritonId', '')
})
}),
graphql(DeploymentGroupCreateMutation, { graphql(DeploymentGroupCreateMutation, {
props: ({ mutate }) => ({ props: ({ mutate }) => ({
createDeploymentGroup: variables => mutate({ variables }) createDeploymentGroup: variables => mutate({ variables })

View File

@ -2,6 +2,7 @@ query Portal {
portal { portal {
user { user {
firstName firstName
tritonId
} }
datacenter { datacenter {
id id

View File

@ -7,6 +7,7 @@ type Portal {
type User { type User {
id: ID! id: ID!
tritonId: ID!
firstName: String! firstName: String!
lastName: String! lastName: String!
email: String! email: String!

View File

@ -49,36 +49,77 @@ const ifError = function (err) {
console.error(err); console.error(err);
process.exit(1); process.exit(1);
} }
process.exit(0);
}; };
const bootstrap = function () { const bootstrap = function (cb) {
const data = new Data(settings); const data = new Data(settings);
const region = process.env.TRITON_DC || 'us-sw-1'; const region = process.env.TRITON_DC || 'us-sw-1';
data.connect((err) => { data.connect((err) => {
ifError(err); if (err) {
return cb(err);
}
data.createDatacenter({ region, name: region }, (err, datacenter) => { data.getDatacenters((err, datacenters) => {
ifError(err); if (err) {
return cb(err);
}
// Don't continue since data is already bootstrapped
if (datacenters && datacenters.length) {
return cb();
}
data.createDatacenter({
region,
name: region
}, (err, datacenter) => {
if (err) {
return cb(err);
}
Triton.createClient({ Triton.createClient({
profile: settings.triton profile: settings.triton
}, (err, { cloudapi }) => { }, (err, { cloudapi }) => {
ifError(err); if (err) {
return cb(err);
}
cloudapi.getAccount((err, { firstName, lastName, email, login }) => { cloudapi.getAccount((err, {
ifError(err); id,
firstName,
lastName,
email,
login
}) => {
if (err) {
return cb(err);
}
data.createUser({ firstName, lastName, email, login }, (err, user) => { data.createUser({
ifError(err); tritonId: id,
firstName,
lastName,
email,
login
}, (err, user) => {
if (err) {
return cb(err);
}
data.createPortal({ data.createPortal({
user, user,
datacenter datacenter
}, (err, portal) => { }, (err, portal) => {
ifError(err); if (err) {
return cb(err);
}
console.log('data bootstrapped'); console.log('data bootstrapped');
process.exit(0); cb();
});
}); });
}); });
}); });
@ -87,4 +128,4 @@ const bootstrap = function () {
}); });
}; };
bootstrap(); bootstrap(ifError);

View File

@ -201,6 +201,7 @@ exports.toInstance = function (clientInstance) {
exports.fromUser = function (user) { exports.fromUser = function (user) {
return { return {
id: user.id, id: user.id,
tritonId: user.tritonId,
firstName: user.first_name, firstName: user.first_name,
lastName: user.last_name, lastName: user.last_name,
email: user.email, email: user.email,
@ -211,6 +212,7 @@ exports.fromUser = function (user) {
exports.toUser = function (clientUser) { exports.toUser = function (clientUser) {
return { return {
id: clientUser.id, id: clientUser.id,
tritonId: clientUser.tritonId,
first_name: clientUser.firstName, first_name: clientUser.firstName,
last_name: clientUser.lastName, last_name: clientUser.lastName,
email: clientUser.email, email: clientUser.email,

View File

@ -23,7 +23,11 @@ const goodOptions = {
consoleReporter: [{ consoleReporter: [{
module: 'good-squeeze', module: 'good-squeeze',
name: 'Squeeze', name: 'Squeeze',
args: [{ log: '*', response: '*', error: '*' }] args: [{
log: '*',
response: '*',
error: '*'
}]
}, { }, {
module: 'good-console' module: 'good-console'
}, 'stdout'] }, 'stdout']
@ -39,17 +43,18 @@ server.register([
}, },
{ {
register: Toppsy, register: Toppsy,
options: { namespace: 'portal', subsystem: 'api' } options: {
namespace: 'portal',
subsystem: 'api'
} }
], }
(err) => { ], (err) => {
handlerError(err); handlerError(err);
server.start((err) => { server.start((err) => {
handlerError(err); handlerError(err);
console.log(`server started at http://localhost:${server.info.port}`); console.log(`server started at http://localhost:${server.info.port}`);
}); });
} });
);
function handlerError (error) { function handlerError (error) {
if (error) { if (error) {