UNPKG

3.98 kBtext/coffeescriptView Raw
1# This is the client's interface to the Huxley API. Objects are transported from
2# here to the server and the results are interpreted. The interface functions
3# are grouped based on which resource they affect.
4#===============================================================================
5# Modules
6#===============================================================================
7# Core Libraries
8{resolve} = require "path"
9
10# PandaStrike Libraries
11{sleep} = require "fairmont" # utility helpers
12Configurator = require "panda-config" # config file parsing
13{discover} = require "./client" # PBX component
14
15# Third Party Libraries
16async = (require "when/generator").lift # promise library
17
18
19#===============================================================================
20# Helpers
21#===============================================================================
22# Prepare a basic error object to return to the UI.
23build_error = (message, value) ->
24 return {
25 message: message
26 value: value
27 }
28
29
30
31#===============================================================================
32# Module Definition
33#===============================================================================
34module.exports =
35
36 #--------------------------------
37 # Cluster
38 #--------------------------------
39 create_cluster: async (spec) ->
40 try
41 api = yield discover spec.url
42 clusters = api.clusters
43 {response: {headers: {cluster_id}}} = yield clusters.create spec
44 console.log "*****Creation In Progress. \nName: #{spec.cluster_name} \nCluster ID: #{cluster_id}"
45 return {cluster_id: cluster_id}
46 catch error
47 throw build_error "Unable to construct Huxley cluster.", error
48
49 delete_cluster: async (spec) ->
50 try
51 api = yield discover spec.url
52 cluster = api.cluster spec.cluster_id
53 yield cluster.delete()
54 console.log "****Deletion In Progress."
55 catch error
56 throw build_error "Unable to delete Huxley cluster.", error
57
58 poll_cluster: async (spec) ->
59 api = yield discover spec.url
60 cluster = api.cluster spec.cluster_id
61 while true
62 response = yield cluster.get()
63 data = yield response.data
64 console.log "*****Cluster status: ", data.cluster_status
65 if data.cluster_status == "The cluster is confirmed to be online and ready."
66 return data.cluster_status # The cluster formation complete.
67 else
68 yield sleep 5000 # Not complete, keep going.
69
70
71 #--------------------------------
72 # Remote
73 #--------------------------------
74 add_remote: async (spec) ->
75 try
76 api = yield discover spec.url
77 remotes = api.remotes
78 {response: {headers: {remote_id}}} = yield remotes.create spec
79 console.log "*****Githook installed on cluster #{spec.cluster_name} \nID: #{remote_id}"
80 return {remote_id: remote_id}
81
82 catch error
83 throw build_error "Unable to install remote githook.", error
84
85 rm_remote: async (spec) ->
86 try
87 api = yield discover spec.url
88 remote = api.remote spec.remote_id
89 result = yield remote.delete()
90 console.log yield result.data
91
92 catch error
93 throw build_error "Unable to remove remote githook.", error
94
95 #-------------------------------------------------
96 # code related to "user" functionality
97 #-------------------------------------------------
98 # # FIXME: filter out secret keys in response
99 # create_user: async ({aws, email, url, key_pair, public_keys}) ->
100 #
101 # api = (yield discover url)
102 # users = (api.users)
103 # {data} = (yield users.create {aws, email, key_pair, public_keys})
104 # data = (yield data)
105 # secret_token = (JSON.parse data).user.secret_token
106 #
107 # configurator = Configurator.make
108 # prefix: "."
109 # paths: [ process.env.HOME ]
110 # configuration = configurator.make name: "huxley"
111 # yield configuration.load()
112 # configuration.data.secret_token = secret_token
113 # configuration.save()
114 #
115 # secret_token