Racksjs Demo

Here I show how to use racksjs to create a new cloud server in the Rackspace Cloud.

I assume you have a Rackspace Cloud account and know your username and apikey. We are running CentOS 6 as our operating system.

Firstly lets install nodejs

  
yum install nodejs
  

Now we have the engine to run our javascript.

About that javascript, I lied. I will be writing coffeescript in this article since I get tired of adding {} everywhere.

Our second step will to clone the repo of rackjs, a SDK that Seandon Mooy and I have been working on for the Rackspace Cloud.

racksjs will only works with the Rackspace Cloud and thus we have been able to add almost all of the features their API supports. Not getting bogged down in abstracting "cloudy" concepts away and giving the user direct access to the API.

  
git clone https://github.com/erulabs/racksjs.git
  

Now take a look in the examples/examples.coffee file and you will see a section for creating a new server.

  
# Create a new server!
rs.nextgen.servers.new {
   'name': 'newServerName'
   'flavorRef': 'performance1-1'
   'imageRef': '2318853e-f3b1-4cf4-b1a4-d7db71ca9b50'
}, (server) ->
   server.systemActive (details) ->
       console.log 'server object:', server, 'server details', details
  

But before we create the server we need to have racksjs create an authentication token.

  
RacksJS = require '../dist/racks.js'
new RacksJS {
  username: process.argv[3]
  apiKey: process.argv[4]
  verbosity: 1
}, (rs) ->
  

Also we need to select a datacenter:

  
rs.datacenter = 'ORD'
  

To put it all together create a newServer.coffee file.

  
vim newServer.coffee
  
  
RacksJS = require '../dist/racks.js'
new RacksJS {
  username: process.argv[3]
  apiKey: process.argv[4]
  verbosity: 1
}, (rs) ->
  rs.datacenter = 'ORD'
  # Create a new server!
  rs.nextgen.servers.new {
     'name': 'newServerName'
     'flavorRef': 'performance1-1'
     'imageRef': '2318853e-f3b1-4cf4-b1a4-d7db71ca9b50'
  }, (server) ->
     server.systemActive (details) ->
         console.log 'server object:', server, 'server details', details
  

We can have coffee run this script against our Rackspace Cloud account by passing our user name and api key to newServer.coffee.

  
coffee newServer.coffee -e rackspaceUsername rackspaceAPIkey
  

After about 5 minutes we will get output

  
2/2 00:03:31 POST: /v2.0/tokens
2/2 00:03:32 Reply: 200 OK
2/2 00:03:32 POST: /v2/xxxx/servers
2/2 00:03:33 Reply: 202 Accepted
2/2 00:03:48 GET: /v2/xxxx/servers/xxx-5ef8-xxx-aff8-xxxx
2/2 00:03:49 Reply: 200 OK
2/2 00:04:04 GET: /v2/xxxxx/servers/xxx-5ef8-4424-xxx-xxxx
2/2 00:04:04 Reply: 200 OK
2/2 00:04:19 GET: /v2/xxxxx/servers/xxx-xxx-4424-aff8-xxx
2/2 00:04:20 Reply: 200 OK
server object: { 'OS-DCF:diskConfig': 'AUTO',
  id: 'xxxxxxxx-x-x-x--x--xxx',
  links: 
   [ { href: 'https://ord.servers.api.rackspacecloud.com/v2/xxxxxxxxxx',
       rel: 'self' },
     { href: 'https://ord.servers.api.rackspacecloud.com/xxxxx',
       rel: 'bookmark' } ],
  adminPass: 'XXXXXXXXXXX',
  systemActive: [Function],
  details: [Function],
  addresses: [Function],
  delete: [Function],
  update: [Function],
  action: [Function],
  changePassword: [Function],
  reboot: [Function],
  rescue: [Function],
  unrescue: [Function],
  createImage: [Function],
  serverActions: [Function],
  showServerAction: [Function],
  resize: [Function],
  confirmResize: [Function],
  revertResize: [Function],
  rebuild: [Function],
  attachVolume: [Function],
  volumes: [Function],
  volumeDetails: [Function],
  detachVolume: [Function],
  metadata: [Function],
  setMetadata: [Function],
  updateMetadata: [Function],
  getMetadataItem: [Function],
  setMetadataItem: [Function],
  deleteMetadataItem: [Function],
  getVips: [Function],
  attachNetwork: [Function],
  _racksmeta: 
   { resource: 'servers',
     product: 'cloudServersOpenStack',
     target: [Function] } } server details { status: 'ACTIVE',
  updated: '201xxxxx:13Z',
  hostId: 'xxxxxxx',
  addresses: { public: [ [Object], [Object] ], private: [ [Object] ] },
  links: 
   [ { href: 'https://ord.servers.api.rackspacecloud.com/v2/xxxxxxx',
       rel: 'self' },
     { href: 'https://ord.servers.api.rackspacecloud.com/xxxxx',
       rel: 'bookmark' } ],
  key_name: null,
  image: 
   { id: 'xxxxx-x-x-x-x-xxxx-',
     links: [ [Object] ] },
  'RAX-PUBLIC-IP-ZONE-ID:publicIPZoneId': 'xxxxxxxxxxx',
  'OS-EXT-STS:task_state': null,
  'OS-EXT-STS:vm_state': 'active',
  flavor: { id: 'performance1-1', links: [ [Object] ] },
  id: 'xxx-xxxx-xxx-x--x-x--xx',
  user_id: 'xxxxxx',
  name: 'newServerName',
  created: '2015xxxx33Z',
  tenant_id: 'xxxxxxxx',
  'OS-DCF:diskConfig': 'AUTO',
  accessIPv4: 'xxx.xxx.xxx.xxx',
  accessIPv6: 'xxx:xxxx:xxxx:xxxx:xxxx:',
  progress: 100,
  'OS-EXT-STS:power_state': 1,
  config_drive: '',
  metadata: {} }
  

The two lines of this output that we care about are the adminPass and public accessIPv4

  
  adminPass: 'XXXXXXXXXXXX',
  
  
  accessIPv4: 'xxx.xxx.xxx.xxx',
  

Now you have a server that you used JavaScript to create!

Check out some of my other articles to see some cool things you can do with this server.