11#!/usr/bin/env python3
2- # -*- coding: utf-8 -*-
32"""
43Deploys nbviewer on helm
54
65"""
76
8- from __future__ import print_function
9-
10- from functools import lru_cache
11- import json
12- import os
13- import pipes
14- import re
15- import socket
16- import sys
17- import time
18-
19- join = os .path .join
20-
21- from invoke import run , task
227import requests
23-
8+ from invoke import task
249
2510creds = {}
26- with open (' creds' ) as f :
11+ with open (" creds" ) as f :
2712 exec (f .read (), creds )
2813
2914
3015@task
3116def trigger_build (ctx ):
3217 url_base = "https://hub.docker.com/api/build/v1/source/579ab043-912f-425b-8b3f-765ee6143b53/trigger/{}/call/"
33- r = requests .post (url = url_base .format (creds [' DOCKER_TRIGGER_TOKEN' ]))
18+ r = requests .post (url = url_base .format (creds [" DOCKER_TRIGGER_TOKEN" ]))
3419 r .raise_for_status ()
3520 print (r .text )
3621
@@ -45,42 +30,40 @@ def doitall(ctx):
4530 2. upgrade on all machines
4631 """
4732 # make sure current repo is up to date
48- ctx .run (' git pull' , echo = True )
33+ ctx .run (" git pull" , echo = True )
4934 upgrade (ctx )
5035 fastly (ctx )
5136
5237
5338@task
5439def upgrade (ctx , yes = False ):
55- """Update helm deployment
56-
57- """
40+ """Update helm deployment"""
5841 raise NotImplementedError ("Not implemented yet for helm" )
5942
6043
6144# ------- Fastly commands for updating the CDN --------
6245
63- FASTLY_API = ' https://api.fastly.com'
46+ FASTLY_API = " https://api.fastly.com"
6447
6548
6649class FastlyService :
6750 def __init__ (self , api_key , service_id ):
6851 self .session = requests .Session ()
69- self .session .headers [' Fastly-Key' ] = api_key
52+ self .session .headers [" Fastly-Key" ] = api_key
7053 self .service_id = service_id
7154 latest_version = self .versions ()[- 1 ]
72- self .version = latest_version [' number' ]
73- if latest_version [' active' ]:
55+ self .version = latest_version [" number" ]
56+ if latest_version [" active" ]:
7457 # don't have an inactive version yet
75- self .api_request (' /clone' , method = ' PUT' )
58+ self .api_request (" /clone" , method = " PUT" )
7659 latest_version = self .versions ()[- 1 ]
77- self .version = latest_version [' number' ]
60+ self .version = latest_version [" number" ]
7861
79- def api_request (self , path , include_version = True , method = ' GET' , ** kwargs ):
62+ def api_request (self , path , include_version = True , method = " GET" , ** kwargs ):
8063 url = "{api}/service/{service_id}{v}{path}" .format (
8164 api = FASTLY_API ,
8265 service_id = self .service_id ,
83- v = ' /version/%i' % self .version if include_version else '' ,
66+ v = f" /version/{ self .version } " if include_version else "" ,
8467 path = path ,
8568 )
8669 r = self .session .request (method , url , ** kwargs )
@@ -92,78 +75,78 @@ def api_request(self, path, include_version=True, method='GET', **kwargs):
9275 return r .json ()
9376
9477 def backends (self ):
95- return self .api_request (' /backend' )
78+ return self .api_request (" /backend" )
9679
9780 def versions (self ):
98- return self .api_request (' /version' , include_version = False )
81+ return self .api_request (" /version" , include_version = False )
9982
10083 def add_backend (self , name , hostname , port , copy_backend = None ):
10184 if copy_backend is None :
10285 copy_backend = self .backends ()[0 ]
10386 data = {
10487 key : copy_backend [key ]
10588 for key in [
106- ' healthcheck' ,
107- ' max_conn' ,
108- ' weight' ,
109- ' error_threshold' ,
110- ' connect_timeout' ,
111- ' between_bytes_timeout' ,
112- ' first_byte_timeout' ,
113- ' auto_loadbalance' ,
89+ " healthcheck" ,
90+ " max_conn" ,
91+ " weight" ,
92+ " error_threshold" ,
93+ " connect_timeout" ,
94+ " between_bytes_timeout" ,
95+ " first_byte_timeout" ,
96+ " auto_loadbalance" ,
11497 ]
11598 }
116- data .update ({' address' : hostname , ' name' : name , ' port' : port })
117- self .api_request (' /backend' , method = ' POST' , data = data )
99+ data .update ({" address" : hostname , " name" : name , " port" : port })
100+ self .api_request (" /backend" , method = " POST" , data = data )
118101
119102 def remove_backend (self , name ):
120- self .api_request (' /backend/%s' % name , method = ' DELETE' )
103+ self .api_request (f" /backend/{ name } " , method = " DELETE" )
121104
122105 def deploy (self ):
123106 # activate the current version
124- self .api_request (' /activate' , method = ' PUT' )
107+ self .api_request (" /activate" , method = " PUT" )
125108 # clone to a new version
126- self .api_request (' /clone' , method = ' PUT' )
127- self .version = self .versions ()[- 1 ][' number' ]
109+ self .api_request (" /clone" , method = " PUT" )
110+ self .version = self .versions ()[- 1 ][" number" ]
128111
129112
130113def all_instances ():
131114 """Return {(ip, port) : name} for all running nbviewer containers on all machines"""
132115 all_nbviewers = {}
133116 # add ovh by hand
134117 # TODO: get service from kubernetes
135- all_nbviewers [(' 135.125.83.237' , 80 )] = ' ovh'
118+ all_nbviewers [(" 135.125.83.237" , 80 )] = " ovh"
136119 return all_nbviewers
137120
138121
139122@task
140123def fastly (ctx ):
141124 """Update the fastly CDN"""
142125 print ("Checking fastly backends" )
143- f = FastlyService (creds [' FASTLY_KEY' ], creds [' FASTLY_SERVICE_ID' ])
126+ f = FastlyService (creds [" FASTLY_KEY" ], creds [" FASTLY_SERVICE_ID" ])
144127 changed = False
145128 backends = f .backends ()
146129 nbviewers = all_instances ()
147130 existing_backends = set ()
148131 # first, delete the backends we don't want
149132 copy_backend = backends [0 ]
150133 for backend in backends :
151- host = (backend [' address' ], backend [' port' ])
134+ host = (backend [" address" ], backend [" port" ])
152135 if host not in nbviewers :
153- print ("Deleting backend %s" % backend ['name' ])
154- f .remove_backend (backend [' name' ])
136+ print (f "Deleting backend { backend ['name' ]} " )
137+ f .remove_backend (backend [" name" ])
155138 changed = True
156139 else :
157140 existing_backends .add (host )
158141 for host , name in nbviewers .items ():
159142 if host not in existing_backends :
160143 ip , port = host
161- print ("Adding backend %s %s:%i" % ( name , ip , port ) )
144+ print (f "Adding backend { name } { ip } : { port } " )
162145 f .add_backend (name , ip , port , copy_backend )
163146 changed = True
164147
165148 if changed :
166- print ("Activating fastly configuration %s" % f .version )
149+ print (f "Activating fastly configuration { f .version } " )
167150 f .deploy ()
168151 else :
169152 print ("Fastly OK" )
0 commit comments