shrub/base/ape/cloud/core.hook

451 lines
12 KiB
Plaintext
Raw Normal View History

2015-04-25 21:23:34 +03:00
:: digital ocean fleet management
::
::::
::
2015-05-13 16:14:36 +03:00
/? 314
/- *talk
/+ talk, sole, http
2015-04-25 21:23:34 +03:00
::
::
2015-05-21 04:34:45 +03:00
:::: sivtyv-barnel
2015-04-25 21:23:34 +03:00
::
!:
|%
2015-05-27 03:29:02 +03:00
++ instance
$: name=@t id=@ud status=@t created=@t region=@t snapshot=json disk=@u ip=(list ,@if)
2015-04-30 05:24:24 +03:00
==
2015-05-23 23:45:42 +03:00
++ create-req-do
2015-04-30 23:55:10 +03:00
$:
2015-05-23 23:45:42 +03:00
name=@t region=@t size=@t image=@t ssh=(list cord)
backups=(unit ,?) ipv6=(unit ,?)
private-networking=(unit ,?) user-data=(unit ,@t)
==
++ create-req-gce ,[project=@t zone=@t name=@t machine-type=@t]
++ axle
2015-05-26 05:28:08 +03:00
$: auth=[do=keys gce=keys] toke=[do=tokens gce=tokens]
2015-05-27 03:29:02 +03:00
insts=(list instance)
2015-04-30 23:55:10 +03:00
==
2015-04-25 21:23:34 +03:00
++ keys ,[authc=(unit ,@t) client-secret=(unit ,@t)]
++ tokens ,[access=@t refresh=@t]
2015-05-21 04:34:45 +03:00
++ move ,[bone card]
++ card
$% [%diff %json json]
[%wait wire @da]
[%send wire [ship term] %poke %talk-command command]
[%them wire (unit hiss)]
2015-05-13 16:14:36 +03:00
==
2015-04-30 00:02:54 +03:00
--
2015-04-30 23:55:10 +03:00
!:
2015-05-01 22:05:16 +03:00
|% :::
2015-04-30 05:24:24 +03:00
++ parse-img
2015-04-30 00:02:54 +03:00
=> jo
2015-04-30 05:24:24 +03:00
%- ot
2015-05-21 04:34:45 +03:00
:~ id/ni name/so distribution/so slug/(mu so) public/bo
2015-04-30 05:24:24 +03:00
regions/(ar so) 'created_at'^so type/so 'min_disk_size'^ni
2015-04-30 00:02:54 +03:00
==
2015-05-27 03:29:02 +03:00
++ parse-ip
2015-04-30 00:02:54 +03:00
=> jo
%- ot
2015-05-27 03:29:02 +03:00
:- v4/(ar (ot 'ip_address'^(su lip:ag) ~))
2015-04-30 00:02:54 +03:00
~
2015-04-30 23:55:10 +03:00
++ parse-region
=> jo
2015-05-27 03:29:02 +03:00
(ot name/so ~)
2015-05-23 23:45:42 +03:00
++ create-do-body
|= $: name=@t region=@t size=@t image=@t ssh-keys=(list cord)
2015-04-30 23:55:10 +03:00
backups=(unit ,?) ipv6=(unit ,?) private-networking=(unit ,?) user-data=(unit ,@t)
2015-04-30 05:24:24 +03:00
==
%- jobe
2015-05-23 23:45:42 +03:00
:~ name/s/name region/s/region size/s/size image/s/image ::(jone image)
2015-05-13 16:14:36 +03:00
backups/?~(backups ~ b/u.backups) ipv6/?~(ipv6 ~ b/u.ipv6)
2015-04-30 23:55:10 +03:00
'user_data'^?~(user-data ~ s/u.user-data) 'private_networking'^?~(private-networking ~ b/u.private-networking)
2015-04-30 05:24:24 +03:00
==
2015-05-01 06:22:05 +03:00
++ state-to-json
2015-05-27 03:29:02 +03:00
|= a=(list instance)
2015-05-01 06:22:05 +03:00
:- %a
%+ turn a
2015-05-27 03:29:02 +03:00
|= instance
2015-05-01 06:22:05 +03:00
^- json
%- jobe
2015-05-27 03:29:02 +03:00
:~ name/`json`s/name
id/`json`(jone id)
2015-05-01 06:22:05 +03:00
status/s/status
2015-05-27 03:29:02 +03:00
created/s/created
region/s/region
snapshot/s/'xx replace'
disk/`json`(jone disk)
ip/a/(turn ip |=(a=@if s/(rsh 3 1 (scot %if a))))
2015-05-01 06:22:05 +03:00
==
2015-04-25 21:23:34 +03:00
--
!:
|_ [hid=hide vat=axle]
2015-04-30 00:02:54 +03:00
::
2015-05-27 03:29:02 +03:00
++ prep ,_`.
2015-05-13 16:14:36 +03:00
:::
++ spam
|= jon=json
%+ turn (~(tap by sup.hid))
|= [sub=bone @ pax=path]
^- move
2015-05-21 04:34:45 +03:00
[sub %diff %json jon]
2015-05-13 16:14:36 +03:00
++ auth-queries
2015-05-21 04:34:45 +03:00
|= code=cord
2015-04-25 21:23:34 +03:00
:~ 'grant_type'^'authorization_code'
2015-05-21 04:34:45 +03:00
'code'^code
2015-04-25 21:23:34 +03:00
:- 'client_id'
'd8f46b95af38c1ab3d78ad34c2157a6959c23eb0eb5d8e393f650f08e6a75c6f'
2015-05-22 03:22:56 +03:00
'redirect_uri'^'http://localhost:8443/home/pub/cloud/fab'
2015-04-25 21:23:34 +03:00
==
++ httpreq
|= $: ost=bone pour-path=wire
domain=(list cord) end-point=path
2015-04-30 05:24:24 +03:00
req-type=$?(%get [%post json]) headers=math
2015-04-25 21:23:34 +03:00
queries=quay
==
^- move
=+ ^- parsed-url=purl
:+ ^= host-port :: ++hart
:+ security=%.y
port=~
host=[%.y [path=[%com domain]]]
endpoint=[extensions=~ point=end-point] :: ++pork,
q-strings=queries :: ++quay
=+ ^- request=hiss :: cast to hiss
:- parsed-url
2015-04-30 05:24:24 +03:00
?@ req-type
[%get headers ~]
[%post headers ~ (tact (pojo +.req-type))]
2015-05-21 04:34:45 +03:00
:^ ost %them pour-path
2015-04-25 21:23:34 +03:00
`(unit hiss)`[~ request]
::
++ peer
2015-05-21 04:34:45 +03:00
|= [[ost=bone you=ship] pax=path]
2015-04-25 21:23:34 +03:00
^- [(list move) _+>.$]
:_ +>.$
2015-05-27 03:29:02 +03:00
[ost %diff %json (state-to-json insts.vat)]~
2015-04-25 21:23:34 +03:00
::
2015-05-22 03:22:56 +03:00
++ poke-cloud-auth
2015-05-21 04:34:45 +03:00
|= [[ost=bone you=ship] [cde=cord typ=cord]]
2015-04-25 21:23:34 +03:00
^- [(list move) _+>.$]
2015-05-21 04:34:45 +03:00
~& [cde typ]
?: =(%do typ)
=. authc.do.auth.vat
[~ cde]
:_ +>.$
~
2015-05-26 05:28:08 +03:00
=. access.gce.toke.vat
2015-05-23 23:45:42 +03:00
cde
2015-04-25 21:23:34 +03:00
:_ +>.$
2015-05-27 03:29:02 +03:00
:- (list-instances-gce ost)
2015-05-21 04:34:45 +03:00
~
2015-04-25 21:23:34 +03:00
::
2015-05-22 03:22:56 +03:00
++ poke-cloud-secret
2015-05-21 04:34:45 +03:00
|= [[ost=bone you=ship] secret=cord typ=cord]
2015-04-25 21:23:34 +03:00
^- [(list move) _+>.$]
2015-05-23 23:45:42 +03:00
~& [secret typ]
2015-05-21 04:34:45 +03:00
::=+ [newvat code path]=[vat(auth auth.vat) ...]
::=. vat newvat
?+ typ ~|(missing-platform=typ !!)
%do
=. client-secret.do.auth.vat
[~ secret]
:_ +>.$
:_ ~
2015-05-23 23:45:42 +03:00
%^ httpreq ost /auth-do
2015-05-21 04:34:45 +03:00
:^ ~[%digitalocean %cloud] `path`/v1/oauth/token
[%post ~]
:- ~ `quay`['client_secret'^secret (auth-queries (need authc.do.auth.vat))]
%gce
2015-05-26 05:28:08 +03:00
=. client-secret.gce.auth.vat
2015-05-21 04:34:45 +03:00
[~ secret]
:_ +>.$
:_ ~
2015-05-23 23:45:42 +03:00
%^ httpreq ost /auth-gce
2015-05-21 04:34:45 +03:00
:^ ~[%google %cloud] `path`/v1/oauth/token
[%post ~]
2015-05-23 23:45:42 +03:00
:- (mo ['Content-Type' 'application/json' ~] ['Authorization' (cat 3 'Bearer ' access.do.toke.vat) ~] ~)
2015-05-26 05:28:08 +03:00
`quay`['client_secret'^secret %'access_token'^(need authc.gce.auth.vat) ~] ::(auth-queries (need authc.gcp.auth.vat))]
2015-05-21 04:34:45 +03:00
==
2015-04-25 21:23:34 +03:00
::
2015-04-30 00:02:54 +03:00
++ receive-auth
2015-05-23 23:45:42 +03:00
|= [ost=bone pour-path=cord resp=httr]
2015-05-13 16:14:36 +03:00
^- [(list move) _+>.$]
~| resp
2015-05-21 04:34:45 +03:00
=+ body=(rash q:(need r.resp) apex:poja)
~| recieve-auth/resp(r body)
2015-05-23 23:45:42 +03:00
?+ pour-path !!
%auth-do
2015-04-30 00:02:54 +03:00
=+ [ac re]=(need ((ot 'access_token'^so 'refresh_token'^so ~):jo body))
2015-05-23 23:45:42 +03:00
=: access.do.toke.vat ac
refresh.do.toke.vat re
2015-04-30 00:02:54 +03:00
==
:_ +>.$
2015-05-27 03:29:02 +03:00
:~ (list-instances-do ost)
2015-05-13 16:14:36 +03:00
(publish ost our.hid [%lin & 'successfully authenticated']~)
==
2015-05-23 23:45:42 +03:00
%auth-gce
::=+ ac=(need ((ot ~):jo
~& [body resp]
:_ +>.$
~
==
2015-04-30 00:02:54 +03:00
::
++ poke-json
2015-05-21 04:34:45 +03:00
|= [[ost=bone you=ship] act=json]
2015-05-01 22:05:16 +03:00
^- [(list move) _+>.$]
2015-04-30 05:24:24 +03:00
=+ do=(need ((ot action/so ~):jo act))
2015-05-27 03:29:02 +03:00
:_ +>.$
:_ ~
2015-04-30 00:02:54 +03:00
?+ do !!
2015-05-13 16:14:36 +03:00
%list
2015-05-27 03:29:02 +03:00
^- move (list-instances-do ost)
2015-05-13 16:14:36 +03:00
::
2015-05-23 23:45:42 +03:00
%create-do
2015-05-27 03:29:02 +03:00
^- move (create-do ost act)
2015-05-26 05:28:08 +03:00
::
%create-gce-disk
2015-05-27 03:29:02 +03:00
^- move (create-gce-disk ost act)
2015-05-26 05:28:08 +03:00
2015-05-23 23:45:42 +03:00
::
%create-gce
2015-05-27 03:29:02 +03:00
^- move (create-gce ost act)
2015-05-13 16:14:36 +03:00
::
2015-05-27 03:29:02 +03:00
?(%start %stop %reboot %delete) ::%'power_on' %'power_off' %reboot %'power_cycle'
^- move
2015-05-13 16:14:36 +03:00
=+ id=(need ((ot id/no ~):jo act))
2015-05-27 03:29:02 +03:00
(instance-action ost id do)
2015-04-25 21:23:34 +03:00
==
2015-05-13 16:14:36 +03:00
::
2015-05-27 03:29:02 +03:00
++ instance-action
2015-05-13 16:14:36 +03:00
|= $: os=bone id=@t
$= action $?
2015-05-27 03:29:02 +03:00
%start %stop %reboot %delete
2015-05-13 16:14:36 +03:00
== ==
:: restore, resize, rebuild, change_kernelm, retrieve droplet action
2015-05-27 03:29:02 +03:00
^- move
2015-05-13 16:14:36 +03:00
=+ ^= req
2015-05-01 22:05:16 +03:00
%- httpreq :*
2015-05-13 16:14:36 +03:00
os /reboot
~[%digitalocean %api] /v2/droplets/[id]/actions
[%post `json`(jobe type/s/action ~)]
2015-05-23 23:45:42 +03:00
(mo ['Content-Type' 'application/json' ~] ['Authorization' (cat 3 'Bearer ' access.do.toke.vat) ~] ~)
2015-05-01 22:05:16 +03:00
*quay
==
2015-05-13 16:14:36 +03:00
req
2015-05-01 22:05:16 +03:00
2015-05-27 03:29:02 +03:00
++ list-instances-gce
2015-05-26 05:28:08 +03:00
|= os=bone
=+ ^= lis
:* os /list-gce
~[%googleapis %www] /compute/v1/projects/urbcloud/zones/['us-central1-a']/'instances'
%get ~
^- quay
[%'access_token' access.gce.toke.vat]~
==
(httpreq lis)
2015-05-27 03:29:02 +03:00
++ list-instances-do
2015-04-25 21:23:34 +03:00
|= os=bone
2015-05-01 06:22:05 +03:00
=+ ^= lis
2015-05-26 05:28:08 +03:00
:~ os /list-do
2015-04-25 21:23:34 +03:00
~[%digitalocean %api] /v2/droplets
%get
2015-05-23 23:45:42 +03:00
(mo ['Content-Type' 'application/json' ~] ['Authorization' (cat 3 'Bearer ' access.do.toke.vat) ~] ~)
2015-04-25 21:23:34 +03:00
==
2015-05-13 16:14:36 +03:00
(httpreq lis)
2015-04-30 00:02:54 +03:00
::
2015-05-27 03:29:02 +03:00
++ receive-list-do
|= [ost=bone resp=httr]
2015-05-13 16:14:36 +03:00
^- [(list move) _+>.$]
2015-05-27 03:29:02 +03:00
=+ parsed=(rash q:(need r.resp) apex:poja) :: parse httr to json
2015-05-13 16:14:36 +03:00
~| recieve-list/parsed
2015-05-27 03:29:02 +03:00
=+ dar=(need ((ot droplets/(ar some) ~):jo parsed)) :: reparse ar of insts
=+ ^- dropz=(list instance)
2015-04-30 05:24:24 +03:00
~| bad-json/-.dar
2015-04-30 23:55:10 +03:00
%+ turn dar
|= drp=json
2015-04-30 05:24:24 +03:00
%- need
2015-04-30 23:55:10 +03:00
%. drp =+ jo
2015-04-30 05:24:24 +03:00
%- ot
2015-05-27 03:29:02 +03:00
:~ name/so id/ni status/so 'created_at'^so region/parse-region
image/some disk/ni networks/parse-ip
2015-04-30 05:24:24 +03:00
==
2015-05-27 03:29:02 +03:00
=. insts.vat
2015-04-30 23:55:10 +03:00
dropz
2015-05-13 16:14:36 +03:00
=+ buf=`@da`(add ~s10 lat.hid)
2015-04-30 05:24:24 +03:00
:_ +>.$
2015-05-27 03:29:02 +03:00
:_ (spam (state-to-json insts.vat))
2015-05-26 05:28:08 +03:00
[ost %wait /refresh-do buf]
2015-05-27 03:29:02 +03:00
2015-05-23 23:45:42 +03:00
++ create-do
2015-05-27 03:29:02 +03:00
|= [os=bone act=json]
=+ ^- deets=create-req-do
%- need
%. act
=> jo
%- ot
:~ name/so region/so size/so image/so :: id key:img object
ssh/(ar so) backups/(mu bo)
'ipv6'^(mu bo) 'priv_networking'^(mu bo) 'user_data'^(mu so)
==
2015-05-21 04:34:45 +03:00
=+ ^- body=json
2015-05-23 23:45:42 +03:00
%- create-do-body :*
2015-05-27 03:29:02 +03:00
name.deets region.deets size.deets image.deets ssh.deets backups.deets
ipv6.deets private-networking.deets user-data.deets
2015-04-30 23:55:10 +03:00
==
2015-05-21 04:34:45 +03:00
%- httpreq :*
2015-05-23 23:45:42 +03:00
os /create-do
2015-05-21 04:34:45 +03:00
~[%digitalocean %api] /v2/droplets
[%post body]
2015-05-26 05:28:08 +03:00
%^ mo ['Content-Type' 'application/json; charset=utf-8' ~]
2015-05-23 23:45:42 +03:00
['Authorization' (cat 3 'Bearer ' access.do.toke.vat) ~]
2015-05-21 04:34:45 +03:00
~
~
==
2015-05-26 05:28:08 +03:00
::++ batch-request
::|= [os=bone name=@t number=@ud snap=@t]
::(list httr) ^- httr
++ create-gce-disk
2015-05-27 03:29:02 +03:00
|= [os=bone act=json] :: num=(unit ,@u)
2015-05-26 05:28:08 +03:00
:: =. name ?~(num name ...
2015-05-27 03:29:02 +03:00
=+ :- name=(need ((ot name/so ~):jo act))
snap=(need ((ot snap/so ~):jo act))
2015-05-26 05:28:08 +03:00
=+ ^- body=json
(jobe name/s/name %'sourceSnapshot'^s/'compute/v1/projects/urbcloud/global/snapshots/snapshot-1' ~) ::^so/snap ~)
%- httpreq
:* os /create-gce-disk
~['googleapis' 'www'] /compute/v1/projects/urbcloud/zones/us-central1-b/disks
[%post body]
%^ mo ['Content-Type' 'application/json' ~]
['Authorization' (cat 3 'Bearer ' access.gce.toke.vat) ~]
~
~
==
++ ask-disk-status
|= [os=bone pax=path] ^- move
=+ safe=(slav %uv ?~(pax !! -.pax))
=+ link=(need (epur ?~(pax !! safe)))
=. r.link ['access_token'^access.gce.toke.vat r.link]
:^ os %them `wire`/disk-status
`(unit hiss)`[~ [link [%get ~ ~]]]
++ disk-status ::receive
|= [ost=bone resp=httr]
^- [(list move) _+>.$]
2015-05-26 23:47:57 +03:00
=+ hcode=p.resp
?: =('200' hcode)
~| 'did not receive 200' !!
=+ :-(parsed=(rash q:(need r.resp) apex:poja) jo)
=+ :- status=(need ((ot status/so ~) parsed))
lin=(need ((ot 'selfLink'^so ~) parsed))
=+ link=(scot %uv lin)
~& lin
?: =('DONE' status)
~& resp
~& 'boot disk now running, now starting instance'
=+ target=(need ((ot 'targetLink'^so ~):jo parsed))
=+ nam=-:(flop q.q:(need (epur target)))
~& nam
::(create-gce-disk ost nam 'tbd')
:- ~ +>.$
2015-05-26 05:28:08 +03:00
:_ +>.$
2015-05-27 03:29:02 +03:00
[ost %wait `path`[%check-status link ~] `@da`(add ~s3 lat.hid)]~ :: refesh every 10 sec
2015-05-26 05:28:08 +03:00
2015-05-23 23:45:42 +03:00
++ create-gce
2015-05-27 03:29:02 +03:00
|= [os=bone act=json]
=+ ^- deets=create-req-gce
%- need
%. act
=> jo
%- ot
:~ project/so zone/so name/so %'machine_type'^so
==
=+ src=(cat 3 'compute/v1/projects/urbcloud/zones/us-central1-b/disks/' name.deets)
2015-05-23 23:45:42 +03:00
=+ ^- body=json
%- jobe
2015-05-26 05:28:08 +03:00
:~ name/s/'name-provided' 'machineType'^s/'zones/us-central1-b/machineTypes/n1-standard-1'
:- %disks :- %a :_ ~
(jobe boot/b/%.y type/s/'persistent' source/s/src ~)
:- 'networkInterfaces' :- %a :_ ~
(joba 'network' `json`[%s 'global/networks/default'])
==
2015-05-23 23:45:42 +03:00
%- httpreq
:* `bone`os `path`/create-gce
2015-05-26 05:28:08 +03:00
`(list cord)`~['googleapis' 'www'] `path`/compute/v1/projects/urbcloud/zones/us-central1-b/'instances'
2015-05-23 23:45:42 +03:00
[%post `json`body]
2015-05-24 00:40:20 +03:00
%^ mo ['Content-Type' 'application/json' ~]
2015-05-26 05:28:08 +03:00
['Authorization' (cat 3 'Bearer ' access.gce.toke.vat) ~]
2015-05-24 00:40:20 +03:00
~
2015-05-26 05:28:08 +03:00
`quay`[%key access.gce.toke.vat]~
2015-05-23 23:45:42 +03:00
==
2015-05-21 04:34:45 +03:00
++ wake
|= [[ost=bone him=ship pour-path=path] ~]
?+ -.pour-path !!
2015-05-26 05:28:08 +03:00
%refresh-do
2015-05-21 04:34:45 +03:00
:_ +>.$
2015-05-27 03:29:02 +03:00
[(list-instances-do ost)]~
2015-05-26 05:28:08 +03:00
%check-status
:_ +>.$
[(ask-disk-status ost +.pour-path)]~
2015-05-21 04:34:45 +03:00
==
++ thou
|= [[ost=bone him=ship pour-path=path] resp=httr]
2015-04-30 00:02:54 +03:00
^- [(list move) _+>.$]
2015-05-26 05:28:08 +03:00
?+ -.pour-path ~& pour-path !!
2015-05-23 23:45:42 +03:00
%auth-do
(receive-auth ost -.pour-path resp)
::
%auth-gce
(receive-auth ost -.pour-path resp)
2015-04-30 00:02:54 +03:00
::
2015-05-26 05:28:08 +03:00
%list-do
2015-05-27 03:29:02 +03:00
(receive-list-do ost resp)
2015-05-26 05:28:08 +03:00
%list-gce
~& resp
:_ +>.$
~
2015-04-30 23:55:10 +03:00
::
2015-05-13 16:14:36 +03:00
$?
%delete %reboot %'power_cycle' %shutdown %'power_off'
%'power_on' %'password_reset' %'enable_ipv6' %'enable_private_networking'
%snapshot %upgrade :: add retrieve droplet action
2015-05-23 23:45:42 +03:00
%create-do %create-gce
2015-05-13 16:14:36 +03:00
==
~& resp
:_ +>.$ ~
2015-05-26 05:28:08 +03:00
::
?(%create-gce-disk %disk-status)
(disk-status ost resp)
::
%check-status
:_ +>.$ ~[(ask-disk-status ost +.pour-path)]
2015-05-01 06:22:05 +03:00
::
2015-05-13 16:14:36 +03:00
%pub
:_ +>.$ ~
::
2015-04-30 00:02:54 +03:00
==
2015-05-13 16:14:36 +03:00
++ publish
|= [ost=bone you=ship act=(list speech)]
^- move
=+ ^= spchz
%+ turn act
|= sp=speech
=+ ^= tail
:- ^- audience
:+ :- `partner`[%& our.hid ?+((clan our.hid) !! %czar %court, %duke %porch)]
^- (pair envelope delivery)
[`envelope`[& ~] %pending]
~
~
`statement`[lat.hid ~ sp]
^- thought
:- `@`(sham eny.hid tail)
tail
2015-05-21 04:34:45 +03:00
=+ mez=[%talk-command [%publish `(list thought)`spchz]]
[ost %send /pub [our.hid %talk] %poke mez]
2015-04-25 21:23:34 +03:00
--