From 273400608ccc9ca4daf1b45f2737a71c1ddcdfde Mon Sep 17 00:00:00 2001 From: Henry Ault Date: Wed, 10 Jun 2015 13:53:43 -0700 Subject: [PATCH] cutting out create-disk --- base/ape/cloud.hoon | 177 +++++++++++++++++++++++-------------- base/mar/cloud-auth.hoon | 16 ++++ base/mar/cloud-secret.hoon | 9 ++ base/pub/cloud/src/main.js | 80 +++++++++-------- 4 files changed, 178 insertions(+), 104 deletions(-) create mode 100644 base/mar/cloud-auth.hoon create mode 100644 base/mar/cloud-secret.hoon diff --git a/base/ape/cloud.hoon b/base/ape/cloud.hoon index e171681e6..cf8c73f73 100644 --- a/base/ape/cloud.hoon +++ b/base/ape/cloud.hoon @@ -41,6 +41,18 @@ $% [%diff %json json] [%send wire [ship term] %poke %talk-command command] [%them wire (unit hiss)] == +++ droplet-action +$% [%start ~] + [%stop ~] + [%reboot ~] + [%delete ~] + [%snapshot p=@t] +== +++ cloud-command +$% [%action id=@t name=@t act=droplet-action] + [%create-do p=json] + [%create-gce p=json] +== -- !: |% ::: @@ -66,6 +78,23 @@ $% [%diff %json json] == ++ parse-zone-sign ;~(plug ;~(pose (cold & lus) (cold | hep))) -- +++ parse-cloud-command + =+ jo + %- of :~ + [%create-do some] + [%create-gce some] + :- %action + (ot id/so name/so act/parse-droplet-action ~) + == +++ parse-droplet-action + => jo + %- of :~ + [%start ul] + [%stop ul] + [%reboot ul] + [%delete ul] + [%snapshot so] + == ++ key-do (mo [%start 'power_on'] [%stop 'shutdown'] [%reboot 'power_cycle'] ~) ++ adapter-do @@ -100,7 +129,7 @@ $% [%diff %json json] 'user_data'^?~(user-data ~ s/u.user-data) 'private_networking'^?~(private-networking ~ b/u.private-networking) == ++ convert-do - |= a=?(%start %stop %reboot) + |= a=?(%start %stop %reboot %snapshot) ?- a %start 'power_on' @@ -108,9 +137,13 @@ $% [%diff %json json] 'shutdown' %reboot 'power_cycle' + %snapshot + 'snapshot' == ++ instance-to-json |= a=(list instance) + ^- json + %+ joba 'instances' :- %a %+ turn a |= instance @@ -132,6 +165,7 @@ $% [%diff %json json] |=(a=[[@t @t] image] `image`+.a) ++ image-to-json |= a=(list image) + %+ joba 'images' :- %a %+ turn a |= image @@ -231,72 +265,75 @@ $% [%diff %json json] list-do :: ++ poke-json - |= act=json + |= jon=json ^- [(list move) _+>.$] - =+ action=(need ((ot action/so ~):jo act)) + ~& receive-act/jon + =+ action=`cloud-command`(need (parse-cloud-command jon)) :_ +>.$ - ?+ action !! - :: + ?- -.action %create-do - [(create-do act)]~ + [(create-do p.action)]~ :: %create-gce - (create-gce-disks act) + ::(create-gce-disks p.action) + ~[(create-gce 'name')] :: - ?(%start %stop %reboot %delete) - =+ :- id=(need ((ot id/so ~):jo act)) - name=(need ((ot name/so ~):jo act)) - [(instance-action id name action)]~ + %action + [(instance-action [id name act]:action)]~ == :: ++ instance-action - |= $: id=@t name=@t - $= action $? - %start %stop %reboot %delete - == == + |= [id=@t name=@t action=droplet-action] =+ d=(~(got by insts.vat) id) ~| 'can\'t find id' =+ typ=?~(d !! -.d) ?- typ %do - =+ meth=?:(?=(%delete action) %delt [%post (jobe type/s/(convert-do action) ~)]) - ^- move - =+ ^= req - %- httpreq :* - /do/[action] - ~[%digitalocean %api] - ?:(?=(%delt meth) /v2/droplets/[id] /v2/droplets/[id]/actions) - meth - %^ mo ['Content-Type' 'application/json' ~] - ['Authorization' (cat 3 'Bearer ' access.do.toke.vat) ~] ~ - *quay - == - req + =+ ^= meth + ?: ?=(%delete -.action) + %delt + [%post (jobe type/s/(convert-do -.action) ?.(?=(%snapshot -.action) ~ [name/s/p.action ~]))] + ^- move + =+ ^= req + %- httpreq :* + /do/[-.action] + ~[%digitalocean %api] + ?:(?=(%delt meth) /v2/droplets/[id] /v2/droplets/[id]/actions) + meth + %^ mo ['Content-Type' 'application/json' ~] + ['Authorization' (cat 3 'Bearer ' access.do.toke.vat) ~] ~ + *quay + == + req %gce - =+ ^= head-query - :- %^ mo ['Content-Type' 'application/json' ~] - ['Authorization' (cat 3 'Bearer ' access.gce.toke.vat) ~] ~ - *quay - ?- action - ?(%start %stop %reboot) - =+ end=/compute/v1/projects/urbcloud/zones/us-central1-a/instances/[name] - %- httpreq :* - /gce-act/[action] ~['googleapis' 'www'] - (welp end [?:(?=(%reboot action) 'reset' action) ~]) - [%post ~] - head-query - == - %delete - =+ end=/compute/v1/projects/urbcloud/zones/us-central1-a/instances/[name] - ~& end - %- httpreq :* - /gce-act/[action] ~['googleapis' 'www'] - end - %delt - head-query - == - == + =+ ^= head-query + :- %^ mo ['Content-Type' 'application/json' ~] + ['Authorization' (cat 3 'Bearer ' access.gce.toke.vat) ~] ~ + *quay + ?- -.action + ?(%start %stop %reboot %'snapshot') + =+ end=/compute/v1/projects/urbcloud/zones/us-central1-a/instances/[name] + %- httpreq :* + /gce-act/[-.action] ~['googleapis' 'www'] + (welp end [?:(?=(%reboot -.action) 'reset' -.action) ~]) + [%post ~] + head-query + == + %delete + =+ end=/compute/v1/projects/urbcloud/zones/us-central1-a/instances/[name] + %- httpreq :* + /gce-act/[-.action] ~['googleapis' 'www'] + end + %delt + head-query + == + == == +++ thou-do-snapshot + |= [pax=path resp=httr] + ~& 'arrives' + ~& response/resp + :_ +>.$ ~ ++ thou-gce-act |= [pax=path resp=httr] ~& [resp act/pax] @@ -343,7 +380,7 @@ $% [%diff %json json] =+ ^- body=json (jobe name/s/name %'sourceSnapshot'^s/'compute/v1/projects/urbcloud/global/snapshots/snapshot-1' ~) ::^so/snap ~) %- httpreq - :* /create-gce-disk/[snapshot]/[name] + :* /create-gce-disk/[snap]/[name] ~['googleapis' 'www'] /compute/v1/projects/urbcloud/zones/us-central1-a/disks [%post body] %^ mo ['Content-Type' 'application/json' ~] @@ -370,6 +407,7 @@ $% [%diff %json json] ?: =('200' hcode) ~| 'did not receive 200' !! =+ :-(parsed=(rash q:(need r.resp) apex:poja) jo) + ~& parsed =+ :- status=(need ((ot status/so ~) parsed)) lin=(need ((ot 'selfLink'^so ~) parsed)) =+ link=(scot %uv lin) @@ -382,13 +420,16 @@ $% [%diff %json json] [ost %wait `path`[%check-status link ins-img ~] `@da`(add ~s3 now)]~ :: refesh every 10 sec :: ++ create-gce - |= [name=@t snap=@t] + |= name=@t ::snap=@t] + =+ ^= image + "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/". + "images/backports-debian-7-wheezy-v20150603" =+ src=(cat 3 'compute/v1/projects/urbcloud/zones/us-central1-a/disks/' name) =+ ^- body=json %- jobe :~ name/s/name 'machineType'^s/'zones/us-central1-a/machineTypes/n1-standard-1' :- %disks :- %a :_ ~ - (jobe boot/b/%.y type/s/'persistent' source/s/src ~) + ^- json (jobe 'initializeParams'^`json`(jobe 'sourceImage'^s/(crip image) ~) ~) ::boot/b/%.y type/s/'persistent' source/s/src ~) :- 'networkInterfaces' :- %a :_ ~ (joba 'network' `json`[%s 'global/networks/default']) == @@ -538,18 +579,12 @@ $% [%diff %json json] |= [pax=path resp=httr] =+ parsed=(rash q:(need r.resp) apex:poja) ~| crashed-do-images/parsed - =+ imgz=(need ((ot images/(ar some) ~):jo parsed)) + =+ imgz=(need ((ot images/(ar (ot [name/so distribution/so id/no ~])) ~):jo parsed)) =+ ^- images=(list ,[[@t @t] image]) %+ turn imgz - |= im=json - =< [[name %do] .] - ^- image - :- %do - %- need - %. im - =+ jo - %- ot - [name/so id/no ~] + |= [name=@t dist=@t id=@t] + =+ nom=(cat 3 name dist) + [[%do nom] `image`[%do nom id]] =. images.vat %- mo %+ weld images @@ -558,6 +593,14 @@ $% [%diff %json json] :_ +>.$ ~ ::=. :: +++ thou-do + |= [pax=path resp=httr] + ~& resp + :_ +>.$ ~ +++ thou-create + |= [pax=path resp=httr] + ~& resp + :_ +>.$ ~ ++ thou |= [pour-path=path resp=httr] ^- [(list move) _+>.$] @@ -566,10 +609,7 @@ $% [%diff %json json] (receive-auth +.pour-path resp) :: $? - %delete %reboot %'power_cycle' %shutdown %'power_off' - %'power_on' %'password_reset' %'enable_ipv6' %'enable_private_networking' - %snapshot %upgrade :: add retrieve droplet action - %create-do %create-gce %do-start %do-stop %do-reboot %do-delete + %create-do %create-gce == :_ +>.$ ~ :: @@ -604,3 +644,4 @@ $% [%diff %json json] =+ mez=[%talk-command [%publish `(list thought)`spchz]] [ost %send /pub [our %talk] %poke mez] -- + diff --git a/base/mar/cloud-auth.hoon b/base/mar/cloud-auth.hoon new file mode 100644 index 000000000..695a9b6b1 --- /dev/null +++ b/base/mar/cloud-auth.hoon @@ -0,0 +1,16 @@ +:: this mark is used to receive incoming oauth2 tokens that we use to poke our %gall server +:: +:::: /hook/door/do-auth/mar + :: +/? 310 +!: +|_ cod=cord +:: +++ grab :: converter arm + |% + ++ noun ,@t :: clam from noun + ++ json (cork (ot authcode/so platform/so ~):jo need) :: (need (so:jo jon)) + -- +-- + + diff --git a/base/mar/cloud-secret.hoon b/base/mar/cloud-secret.hoon new file mode 100644 index 000000000..3cdf63110 --- /dev/null +++ b/base/mar/cloud-secret.hoon @@ -0,0 +1,9 @@ +!: +|_ cod=cord +:: +++ grab :: converter am + |% + ++ noun ,@t :: clam from noun + ++ json (cork (ot secret/so platform/so ~):jo need) :: (need (so:jo jon)) + -- +-- diff --git a/base/pub/cloud/src/main.js b/base/pub/cloud/src/main.js index e23db01d6..7580a0f8e 100644 --- a/base/pub/cloud/src/main.js +++ b/base/pub/cloud/src/main.js @@ -24,19 +24,18 @@ function HashToJSON() { DOControls = React.createClass({ createDroplet: function(){ urb.send({appl: "cloud", - data: { - action:'create-do', - name:$('#name').val(), - region:$('#region').val(), - size:$('#size').val(), - image:$('#image').val(), - ssh:[], // $('#ssh').val()] - backups:null,//$('#backups').val(), - ipv6:null,//$('#ipv6').val(), - priv_networking:null,//$('#priv-networking').val(), - user_data:null//$('#user-data').val() - }, - mark: "json"}) + mark: "json", + data: {'create-do':{ + name:$('#name').val(), + region:$('#region').val(), + size:$('#size').val(), + image:$('#image').val(), + ssh:[], // $('#ssh').val()] + backups:null,//$('#backups').val(), + ipv6:null,//$('#ipv6').val(), + priv_networking:null,//$('#priv-networking').val(), + user_data:null//$('#user-data').val() + }}}) }, render: function(){ @@ -73,24 +72,25 @@ GCEControls = React.createClass({ createDroplet: function(){ urb.send({ appl: 'cloud', - data: {action:'create-gce', + mark: 'json', + data: {'create-gce':{ // project:$('#project').val(), // zone:$('#zone').val(), // name:$('#gname').val(), // machine_type:$('#machine_type').val() / - }, - mark: 'json'}) + }}}) }, createDisk: function(){ urb.send({ appl: 'cloud', - data: {action:'create-gce', + mark: 'json', + data: {'create-gce':{ snap:$('#gsnap').val(), - number:$('#number').val(), + number:parseInt($('#number').val()), name:$('#gcpName').val(), - instance_img:$('#instance_image').val()}, - mark: 'json'}) + instance_img:$('#instance_image').val() + }}}) }, render: function(){ @@ -118,30 +118,32 @@ GCEControls = React.createClass({ }) Droplet = React.createClass({ - dropletAction:function(id, action){ - urb.send({ - appl:"cloud", - data: {action:action, - id:id}}) + dropletAction: function(act){ + return function(){ + var action = {act:{}, id:this.props.id, name:this.props.name} + switch(act){ + case "snapshot": + action.act[act] = this.refs.snapname.getDOMNode().value + break; default: + action.act[act] = null + } + urb.send({appl: "cloud", data: {action:action}})} }, render: function() { var $this = this //local var, else it always points at second - var acts = ["start","stop","reboot","delete"] + var acts = ["start","stop","reboot","delete","snapshot"] var buttons = []; - var buttons = acts.map(function(act){ - return b({onClick:function(){ - $this.dropletAction($this.props.id, act) - }}, act) - }) + var buttons = acts.map(function(act){ return b({onClick:$this.dropletAction(act).bind($this)}, act)}) kay = Object.keys(this.props) kay = kay.filter(function(b){return b!="children"}) // XX individually adress props - return div({}, + return div({},[ buttons, + input({ref:'snapname',placeholder:'Name of Snapshot'}), table({}, tr({},kay.map(function(k){return th({},k)})), tr({},kay.map(function(k){return td({},JSON.stringify($this.props[k]))}))), - hr()) + hr()]) } }) @@ -181,10 +183,16 @@ Page = recl({ render: function(){ + var drops = [], imgs = [] + if(this.props.instances) drops = this.props.instances.map(Droplet) + if(this.props.images) imgs = this.props.images.map( + function(i){return div({},i.name)} + ) return (div({}, DOControls({handleClick:this.handleClick,sendSecret:this.sendSecret}), GCEControls({handleClick:this.handleClick,sendSecret:this.sendSecret}), - this.props.droplets.map(Droplet) + drops, + imgs )) } }) @@ -194,6 +202,6 @@ authcode.gce = hash.access_token mounted = React.render(Page({droplets:[]}), $("#container")[0]) urb.bind("/", function(err,d) { - - mounted.setProps({droplets:d.data}) + + mounted.setProps(d.data) return})