@@ -7,10 +7,15 @@ import { getEnv } from './env.js';
77const serviceId = argv [ 2 ] ;
88const serviceName = argv [ 3 ] ;
99
10- const { DICTIONARY_NAME , CONFIG_STORE_NAME , KV_STORE_NAME , SECRET_STORE_NAME } =
11- getEnv ( serviceName ) ;
10+ const {
11+ DICTIONARY_NAME ,
12+ CONFIG_STORE_NAME ,
13+ KV_STORE_NAME ,
14+ SECRET_STORE_NAME ,
15+ ACL_NAME ,
16+ } = getEnv ( serviceName ) ;
1217
13- function existingStoreId ( stores , existingName ) {
18+ function existingListId ( stores , existingName ) {
1419 const existing = stores . find (
1520 ( { Name, name } ) => name === existingName || Name === existingName ,
1621 ) ;
@@ -34,108 +39,112 @@ if (process.env.FASTLY_API_TOKEN === undefined) {
3439 }
3540 zx . verbose = true ;
3641}
37- const FASTLY_API_TOKEN = process . env . FASTLY_API_TOKEN ;
3842
3943async function setupConfigStores ( ) {
40- let stores = await ( async function ( ) {
41- try {
42- return JSON . parse (
43- await zx `fastly config-store list --quiet --json --token $FASTLY_API_TOKEN` ,
44- ) ;
45- } catch {
46- return [ ] ;
47- }
48- } ) ( ) ;
44+ const stores = JSON . parse (
45+ await zx `fastly config-store list --quiet --json --token $FASTLY_API_TOKEN` ,
46+ ) ;
4947
50- let STORE_ID = existingStoreId ( stores , DICTIONARY_NAME ) ;
48+ let STORE_ID = existingListId ( stores , DICTIONARY_NAME ) ;
5149 if ( ! STORE_ID ) {
5250 console . log ( `Creating new config store ${ DICTIONARY_NAME } ` ) ;
53- process . env . STORE_ID = JSON . parse (
51+ STORE_ID = JSON . parse (
5452 await zx `fastly config-store create --quiet --name=${ DICTIONARY_NAME } --json --token $FASTLY_API_TOKEN` ,
5553 ) . id ;
5654 } else {
5755 console . log ( `Using existing config store ${ DICTIONARY_NAME } ` ) ;
58- process . env . STORE_ID = STORE_ID ;
56+ STORE_ID = STORE_ID ;
5957 }
60- await zx `echo -n 'https://twitter.com/fastly' | fastly config-store-entry update --upsert --key twitter --store-id=$STORE_ID --stdin --token $FASTLY_API_TOKEN` ;
58+ await zx `echo -n 'https://twitter.com/fastly' | fastly config-store-entry update --upsert --key twitter --store-id=${ STORE_ID } --stdin --token $FASTLY_API_TOKEN` ;
6159 try {
62- await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id $STORE_ID --token $FASTLY_API_TOKEN --autoclone` ;
60+ await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id ${ STORE_ID } --token $FASTLY_API_TOKEN --autoclone` ;
6361 } catch ( e ) {
6462 if ( ! e . message . includes ( 'Duplicate record' ) ) throw e ;
6563 }
6664
67- STORE_ID = existingStoreId ( stores , CONFIG_STORE_NAME ) ;
65+ STORE_ID = existingListId ( stores , CONFIG_STORE_NAME ) ;
6866 if ( ! STORE_ID ) {
6967 console . log ( `Creating new config store ${ CONFIG_STORE_NAME } ` ) ;
70- process . env . STORE_ID = JSON . parse (
68+ STORE_ID = JSON . parse (
7169 await zx `fastly config-store create --quiet --name=${ CONFIG_STORE_NAME } --json --token $FASTLY_API_TOKEN` ,
7270 ) . id ;
7371 } else {
7472 console . log ( `Using existing config store ${ CONFIG_STORE_NAME } ` ) ;
75- process . env . STORE_ID = STORE_ID ;
73+ STORE_ID = STORE_ID ;
7674 }
77- await zx `echo -n 'https://twitter.com/fastly' | fastly config-store-entry update --upsert --key twitter --store-id=$STORE_ID --stdin --token $FASTLY_API_TOKEN` ;
75+ await zx `echo -n 'https://twitter.com/fastly' | fastly config-store-entry update --upsert --key twitter --store-id=${ STORE_ID } --stdin --token $FASTLY_API_TOKEN` ;
7876 try {
79- await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id $STORE_ID --token $FASTLY_API_TOKEN --autoclone` ;
77+ await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id ${ STORE_ID } --token $FASTLY_API_TOKEN --autoclone` ;
8078 } catch ( e ) {
8179 if ( ! e . message . includes ( 'Duplicate record' ) ) throw e ;
8280 }
8381}
8482
8583async function setupKVStore ( ) {
86- let stores = await ( async function ( ) {
87- try {
88- return JSON . parse (
89- await zx `fastly kv-store list --quiet --json --token $FASTLY_API_TOKEN` ,
90- ) . Data ;
91- } catch {
92- return [ ] ;
93- }
94- } ) ( ) ;
84+ let stores = JSON . parse (
85+ await zx `fastly kv-store list --quiet --json --token $FASTLY_API_TOKEN` ,
86+ ) . Data ;
9587
96- const STORE_ID = existingStoreId ( stores , KV_STORE_NAME ) ;
88+ let STORE_ID = existingListId ( stores , KV_STORE_NAME ) ;
9789 if ( ! STORE_ID ) {
9890 console . log ( `Creating new KV store ${ KV_STORE_NAME } ` ) ;
99- process . env . STORE_ID = JSON . parse (
91+ STORE_ID = JSON . parse (
10092 await zx `fastly kv-store create --quiet --name=${ KV_STORE_NAME } --json --token $FASTLY_API_TOKEN` ,
10193 ) . StoreID ;
10294 } else {
10395 console . log ( `Using existing KV store ${ KV_STORE_NAME } ` ) ;
104- process . env . STORE_ID = STORE_ID ;
96+ STORE_ID = STORE_ID ;
10597 }
10698 try {
107- await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id $STORE_ID --token $FASTLY_API_TOKEN --autoclone` ;
99+ await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id ${ STORE_ID } --token $FASTLY_API_TOKEN --autoclone` ;
108100 } catch ( e ) {
109101 if ( ! e . message . includes ( 'Duplicate record' ) ) throw e ;
110102 }
111103}
112104
113105async function setupSecretStore ( ) {
114- let stores = await ( async function ( ) {
115- try {
116- return JSON . parse (
117- await zx `fastly secret-store list --quiet --json --token $FASTLY_API_TOKEN` ,
118- ) ;
119- } catch ( e ) {
120- throw e ;
121- }
122- } ) ( ) ;
123- const STORE_ID = existingStoreId ( stores , SECRET_STORE_NAME ) ;
106+ const stores = JSON . parse (
107+ await zx `fastly secret-store list --quiet --json --token $FASTLY_API_TOKEN` ,
108+ ) ;
109+ let STORE_ID = existingListId ( stores , SECRET_STORE_NAME ) ;
124110 if ( ! STORE_ID ) {
125111 console . log ( `Creating new secret store ${ SECRET_STORE_NAME } ` ) ;
126- process . env . STORE_ID = JSON . parse (
112+ STORE_ID = JSON . parse (
127113 await zx `fastly secret-store create --quiet --name=${ SECRET_STORE_NAME } --json --token $FASTLY_API_TOKEN` ,
128114 ) . id ;
129115 } else {
130116 console . log ( `Using existing secret store ${ SECRET_STORE_NAME } ` ) ;
131- process . env . STORE_ID = STORE_ID ;
132117 }
133- await zx `echo -n 'This is also some secret data' | fastly secret-store-entry create --recreate-allow --name first --store-id=$STORE_ID --stdin --token $FASTLY_API_TOKEN` ;
118+ await zx `echo -n 'This is also some secret data' | fastly secret-store-entry create --recreate-allow --name first --store-id=${ STORE_ID } --stdin --token $FASTLY_API_TOKEN` ;
134119 let key =
135120 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ;
136- await zx `echo -n 'This is some secret data' | fastly secret-store-entry create --recreate-allow --name ${ key } --store-id=$STORE_ID --stdin --token $FASTLY_API_TOKEN` ;
121+ await zx `echo -n 'This is some secret data' | fastly secret-store-entry create --recreate-allow --name ${ key } --store-id=${ STORE_ID } --stdin --token $FASTLY_API_TOKEN` ;
122+ try {
123+ await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id ${ STORE_ID } --token $FASTLY_API_TOKEN --autoclone` ;
124+ } catch ( e ) {
125+ if ( ! e . message . includes ( 'Duplicate record' ) ) throw e ;
126+ }
127+ }
128+
129+ async function setupAcl ( ) {
130+ let ACL_ID = existingListId (
131+ JSON . parse (
132+ await zx `fastly compute acl list-acls --quiet --json --token $FASTLY_API_TOKEN` ,
133+ ) . data ,
134+ ACL_NAME ,
135+ ) ;
136+ if ( ! ACL_ID ) {
137+ console . log ( `Creating ACL ${ ACL_NAME } ` ) ;
138+ ACL_ID = JSON . parse (
139+ await zx `fastly compute acl create --name=${ ACL_NAME } --token $FASTLY_API_TOKEN --json` ,
140+ ) . id ;
141+ await zx `fastly compute acl update --acl-id=${ ACL_ID } --operation=create --prefix=100.100.0.0/16 --action=BLOCK --token $FASTLY_API_TOKEN` ;
142+ await zx `fastly compute acl update --acl-id=${ ACL_ID } --operation=create --prefix=2a03:4b80::/32 --action=ALLOW --token $FASTLY_API_TOKEN` ;
143+ } else {
144+ console . log ( `Using existing ACL ${ ACL_NAME } ` ) ;
145+ }
137146 try {
138- await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id $STORE_ID --token $FASTLY_API_TOKEN --autoclone` ;
147+ await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id ${ ACL_ID } --token $FASTLY_API_TOKEN --autoclone` ;
139148 } catch ( e ) {
140149 if ( ! e . message . includes ( 'Duplicate record' ) ) throw e ;
141150 }
@@ -144,5 +153,6 @@ async function setupSecretStore() {
144153await setupConfigStores ( ) ;
145154await setupKVStore ( ) ;
146155await setupSecretStore ( ) ;
156+ await setupAcl ( ) ;
147157
148158await zx `fastly service-version activate --service-id ${ serviceId } --version latest --token $FASTLY_API_TOKEN` ;
0 commit comments