shrub/base/ape/cloud/core.hook
2015-05-23 14:21:47 -07:00

448 lines
12 KiB
Plaintext

:: digital ocean fleet management
::
::::
::
/? 314
/- *talk
/+ talk, sole, http
::
::
:::: sivtyv-barnel
::
!:
|%
++ droplet
$: id=@ud name=@t memory=@ud vcpus=@ud disk=@ud locked=?
created=@t status=@t backup-ids=(list ,@t) snapshot-ids=(list ,@ud)
features=(list ,@t) region=regi image=img
size=syze size-slug=@t networks=ntwrks kernel=kern
next-backup-window=json
==
++ regi ,[name=@t slug=@t sizes=(list ,@t)]
++ kern ,[id=@ud name=@t version=@t]
++ img
$: id=@ud name=@t distribution=@t slug=(unit ,@t) public=? regions=(list ,@t)
created-at=@t type=@t min-disk-size=@ud
==
++ ntwrks ,[v4=(list v) v6=(list v)]
++ v ,[ip-address=@t netmask=@t gateway=@t type=@t]
++ syze
$: memory=@ud price-monthly=@t price-hourly=@t disk=@ud vcpus=@ud
slug=@t transfer=@t available=? regions=(list ,@t)
==
++ create-req-do
$:
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
$: auth=[do=keys gcp=keys] toke=[do=tokens gcp=tokens]
drops=(list droplet)
==
++ keys ,[authc=(unit ,@t) client-secret=(unit ,@t)]
++ tokens ,[access=@t refresh=@t]
++ move ,[bone card]
++ card
$% [%diff %json json]
[%wait wire @da]
[%send wire [ship term] %poke %talk-command command]
[%them wire (unit hiss)]
==
--
!:
|% :::
++ parse-img
=> jo
%- ot
:~ id/ni name/so distribution/so slug/(mu so) public/bo
regions/(ar so) 'created_at'^so type/so 'min_disk_size'^ni
==
++ parse-ntwrks
=> jo
%- ot
:+ v4/(ar (ot 'ip_address'^so netmask/so gateway/so type/so ~))
v6/(ar (ot 'ip_address'^so netmask/so gateway/so type/so ~))
~
++ parse-size
=> jo
%- ot
:~ memory/ni 'price_monthly'^no 'price_hourly'^no disk/ni
vcpus/ni slug/so transfer/no available/bo regions/(ar so)
==
++ parse-region
=> jo
(ot name/so slug/so sizes/(ar so) ~)
++ create-do-body
|= $: name=@t region=@t size=@t image=@t ssh-keys=(list cord)
backups=(unit ,?) ipv6=(unit ,?) private-networking=(unit ,?) user-data=(unit ,@t)
==
%- jobe
:~ name/s/name region/s/region size/s/size image/s/image ::(jone image)
backups/?~(backups ~ b/u.backups) ipv6/?~(ipv6 ~ b/u.ipv6)
'user_data'^?~(user-data ~ s/u.user-data) 'private_networking'^?~(private-networking ~ b/u.private-networking)
==
++ state-to-json
|= a=(list droplet)
:- %a
%+ turn a
|= droplet
^- json
%- jobe
:~ id/`json`(jone id)
name/`json`s/name
vcpus/`json`(jone vcpus)
disk/`json`(jone disk)
locked/b/locked
created/s/created
status/s/status
backup-ids/a/(turn backup-ids |=(el=cord s/el))
snapshot-ids/a/(turn snapshot-ids jone)
features/a/(turn features |=(el=cord s/el))
=+ ^= reg
:~ name/s/name.region slug/s/slug.region
sizes/a/(turn sizes.region |=(el=cord s/el))
==
region/(jobe reg)
=+ ^= im
:~ id/(jone id.image) name/s/name.image distribution/s/distribution.image
slug/?~(slug.image ~ s/u.slug.image) public/b/public.image
regions/a/(turn regions.image |=(el=cord s/el))
created-at/s/created-at.image type/s/type.image
min-disk-size/(jone min-disk-size.image)
==
image/(jobe im)
=+ ^= sz
:~ memory/(jone memory.size) price-monthly/s/price-monthly.size
price-hourly/s/price-hourly.size disk/(jone disk.size) vcpus/(jone vcpus.size)
slug/s/slug.size transfer/s/transfer.size available/b/available.size
regions/a/(turn regions.size |=(el=cord s/el))
==
size/(jobe sz)
size-slug/s/size-slug
=+ ^= nt
=+ ^= vobj
|= v
%- jobe :~
ip-address/s/ip-address netmask/s/netmask
gateway/s/gateway type/s/type
==
:~ v4/[%a (turn v4.networks vobj)]
v6/[%a (turn v4.networks vobj)]
==
networks/(jobe nt)
kernel/s/'kernel'
next-backup-window/s/'valid'
==
--
!:
|_ [hid=hide vat=axle]
::
::++ prep ,_`.
:::
++ spam
|= jon=json
%+ turn (~(tap by sup.hid))
|= [sub=bone @ pax=path]
^- move
[sub %diff %json jon]
++ auth-queries
|= code=cord
:~ 'grant_type'^'authorization_code'
'code'^code
:- 'client_id'
'd8f46b95af38c1ab3d78ad34c2157a6959c23eb0eb5d8e393f650f08e6a75c6f'
'redirect_uri'^'http://localhost:8443/home/pub/cloud/fab'
==
++ httpreq
|= $: ost=bone pour-path=wire
domain=(list cord) end-point=path
req-type=$?(%get [%post json]) headers=math
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
?@ req-type
[%get headers ~]
[%post headers ~ (tact (pojo +.req-type))]
:^ ost %them pour-path
`(unit hiss)`[~ request]
::
++ peer
|= [[ost=bone you=ship] pax=path]
^- [(list move) _+>.$]
:_ +>.$
[ost %diff %json (state-to-json drops.vat)]~
::
++ poke-cloud-auth
|= [[ost=bone you=ship] [cde=cord typ=cord]]
^- [(list move) _+>.$]
~& [cde typ]
?: =(%do typ)
=. authc.do.auth.vat
[~ cde]
:_ +>.$
~
=. access.gcp.toke.vat
cde
:_ +>.$
~
::
++ poke-cloud-secret
|= [[ost=bone you=ship] secret=cord typ=cord]
^- [(list move) _+>.$]
~& [secret typ]
::=+ [newvat code path]=[vat(auth auth.vat) ...]
::=. vat newvat
?+ typ ~|(missing-platform=typ !!)
%do
=. client-secret.do.auth.vat
[~ secret]
:_ +>.$
:_ ~
%^ httpreq ost /auth-do
:^ ~[%digitalocean %cloud] `path`/v1/oauth/token
[%post ~]
:- ~ `quay`['client_secret'^secret (auth-queries (need authc.do.auth.vat))]
%gce
=. client-secret.gcp.auth.vat
[~ secret]
:_ +>.$
:_ ~
%^ httpreq ost /auth-gce
:^ ~[%google %cloud] `path`/v1/oauth/token
[%post ~]
:- (mo ['Content-Type' 'application/json' ~] ['Authorization' (cat 3 'Bearer ' access.do.toke.vat) ~] ~)
`quay`['client_secret'^secret %'access_token'^(need authc.gcp.auth.vat) ~] ::(auth-queries (need authc.gcp.auth.vat))]
==
::
++ receive-auth
|= [ost=bone pour-path=cord resp=httr]
^- [(list move) _+>.$]
~| resp
=+ body=(rash q:(need r.resp) apex:poja)
~| recieve-auth/resp(r body)
?+ pour-path !!
%auth-do
=+ [ac re]=(need ((ot 'access_token'^so 'refresh_token'^so ~):jo body))
=: access.do.toke.vat ac
refresh.do.toke.vat re
==
:_ +>.$
:~ (list-droplets ost)
(publish ost our.hid [%lin & 'successfully authenticated']~)
==
%auth-gce
::=+ ac=(need ((ot ~):jo
~& [body resp]
:_ +>.$
~
==
::
++ poke-json
|= [[ost=bone you=ship] act=json]
^- [(list move) _+>.$]
=+ do=(need ((ot action/so ~):jo act))
?+ do !!
%list
:_ +>.$
:- (list-droplets ost)
~
::
%create-do
:_ +>.$
=+ ^- 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)
==
:- (create-do ost deets)
~
::
%create-gce
~& 'chamado'
:_ +>.$
=+ ^- deets=create-req-gce
%- need
%. act
=> jo
%- ot
:~ project/so zone/so name/so %'machine_type'^so
==
~& 'dois'
:- (create-gce ost deets)
~
::
$? %reboot %'power_cycle' %shutdown %'power_off' %'power_on'
%'password_reset' %'enable_ipv6' %'enable_private_networking'
%snapshot %upgrade
==
=+ id=(need ((ot id/no ~):jo act))
(droplet-action ost id do)
==
::
++ droplet-action
|= $: os=bone id=@t
$= action $?
%delete %reboot %'power_cycle' %shutdown %'power_off'
%'power_on' %'password_reset' %'enable_ipv6' %'enable_private_networking'
%snapshot %upgrade :: add retrieve droplet action
== ==
:: restore, resize, rebuild, change_kernelm, retrieve droplet action
:_ +>.$
^- (list move)
:_ ~
=+ ^= req
%- httpreq :*
os /reboot
~[%digitalocean %api] /v2/droplets/[id]/actions
[%post `json`(jobe type/s/action ~)]
(mo ['Content-Type' 'application/json' ~] ['Authorization' (cat 3 'Bearer ' access.do.toke.vat) ~] ~)
*quay
==
req
++ list-droplets
|= os=bone
=+ ^= lis
:~ os /list
~[%digitalocean %api] /v2/droplets
%get
(mo ['Content-Type' 'application/json' ~] ['Authorization' (cat 3 'Bearer ' access.do.toke.vat) ~] ~)
==
(httpreq lis)
::
++ receive-list
|= [ost=bone plat=cord resp=httr]
^- [(list move) _+>.$]
=+ parsed=(rash q:(need r.resp) apex:poja)
~| recieve-list/parsed
=+ dar=(need ((ot droplets/(ar some) ~):jo parsed))
=+ ^- dropz=(list droplet)
~| bad-json/-.dar
%+ turn dar
|= drp=json
%- need
%. drp =+ jo
%- ot
:~ id/ni name/so
memory/ni vcpus/ni disk/ni
locked/bo 'created_at'^so status/so
'backup_ids'^(ar so) 'snapshot_ids'^(ar ni) features/(ar so)
region/parse-region image/parse-img size/parse-size
'size_slug'^so networks/parse-ntwrks kernel/(ot id/ni name/so version/so ~)
'next_backup_window'^some
==
=. drops.vat
dropz
=+ buf=`@da`(add ~s10 lat.hid)
:_ +>.$
:_ (spam (state-to-json drops.vat))
[ost %wait /refresh buf]
::
++ create-do
|= [os=bone create-req-do]
=- ~& - -
=+ ^- body=json
%- create-do-body :*
name region size image ssh backups
ipv6 private-networking user-data
==
%- httpreq :*
os /create-do
~[%digitalocean %api] /v2/droplets
[%post body]
%^ mo ['Content-Type' 'application/json' ~]
['Authorization' (cat 3 'Bearer ' access.do.toke.vat) ~]
~
~
==
++ create-gce
|= [os=bone create-req-gce]
~& 'called'
=+ ^- body=json
%- jobe
~[name/s/name %'machine-type'^s/machine-type]
~& body
=+ ^= req
%- httpreq
:* `bone`os `path`/create-gce
`(list cord)`~['googleapis' 'www'] `path`/v1/[project]/zones/[zone]/'instances'
~& 'here'
[%post `json`body]
~& 'here'
`math`%^(mo ['Authorization' (cat 3 'Bearer ' access.gcp.toke.vat) ~] ~ ~)
~& 'no'
`quay`~
==
~& req req
++ wake
|= [[ost=bone him=ship pour-path=path] ~]
?+ -.pour-path !!
%refresh
:_ +>.$
[(list-droplets ost)]~
==
++ thou
|= [[ost=bone him=ship pour-path=path] resp=httr]
^- [(list move) _+>.$]
?+ -.pour-path !!
%auth-do
(receive-auth ost -.pour-path resp)
::
%auth-gce
(receive-auth ost -.pour-path resp)
::
%list
(receive-list ost -.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
==
~& resp
:_ +>.$ ~
::
%pub
:_ +>.$ ~
::
==
++ 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
=+ mez=[%talk-command [%publish `(list thought)`spchz]]
[ost %send /pub [our.hid %talk] %poke mez]
--