-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.py
More file actions
724 lines (642 loc) · 17.5 KB
/
commands.py
File metadata and controls
724 lines (642 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import json
from os import environ, EX_OK, EX_UNAVAILABLE
from argparse import ArgumentParser, RawTextHelpFormatter
from hypernode_api_python.client import HypernodeAPIPython
def get_client():
"""
Instantiates the HypernodeAPIPython client with the token from the environment.
:return obj HypernodeAPIPython: The instantiated client
"""
api_token = environ.get("HYPERNODE_API_TOKEN")
if not api_token:
raise ValueError(
"HYPERNODE_API_TOKEN environment variable not set. "
"Try running `export HYPERNODE_API_TOKEN=yourapitoken`"
)
client = HypernodeAPIPython(environ["HYPERNODE_API_TOKEN"])
return client
def get_app_name():
"""
Gets the app name from the environment.
:return str app_name: The app name
"""
app_name = environ.get("HYPERNODE_APP_NAME")
if not app_name:
raise ValueError(
"HYPERNODE_APP_NAME environment variable not set. "
"Try running `export HYPERNODE_APP_NAME=yourhypernodeappname`"
)
return app_name
def print_response(response):
"""
Pretty prints the JSON response.
:param obj response: The response object
:return NoneType None: None
"""
print(json.dumps(response.json(), indent=2))
def get_app_info(args=None):
parser = ArgumentParser(
description="""
Get information about the Hypernode app.
Example:
$ ./bin/get_app_info
{
"name": "yourhypernodeappname",
"type": "persistent",
"product": {
"code": "FALCON_M_202203",
"name": "Falcon M",
"backups_enabled": true,
"storage_size_in_gb": 75,
"price": 1234,
"is_development": false,
"provider": "combell",
"varnish_supported": true,
"supports_sla": true
},
"domainname": "yourhypernodeappname.hypernode.io",
...
}
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
app_name = get_app_name()
print_response(client.get_app_info_or_404(app_name))
def get_next_best_plan_for_app(args=None):
parser = ArgumentParser(
description="""
Get the plan that is the first bigger plan than the current plan for this Hypernode.
This is convenient for if you want to upgrade your Hypernode to a bigger plan using the
xgrade feature and you need to know which plan to upgrade to.
Example:
$ ./bin/get_next_best_plan_for_app
{
"code": "FALCON_L_202203",
"name": "Falcon L"
}
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
app_name = get_app_name()
print_response(client.get_next_best_plan_for_app_or_404(app_name))
def validate_app_name(args=None):
parser = ArgumentParser(
description="""
Check if the specified app_name is valid and available. An app name can not be
registered if it's already taken. Also there are certain restrictions on the
app name, like it can't contain certain characters or exceed a certain length.
Examples:
$ ./bin/validate_app_name hypernode
App name 'hypernode' is valid.
$ ./bin/validate_app_name hyper_node
App name 'hyper_node' is invalid: ["This value can only contain non-capital letters 'a' through 'z' or digits 0 through 9."]
""",
formatter_class=RawTextHelpFormatter,
)
parser.add_argument(
"app_name",
help="The app name to validate",
)
args = parser.parse_args(args=args)
client = get_client()
try:
client.validate_app_name(args.app_name)
print("App name '{}' is valid.".format(args.app_name))
exit(EX_OK)
except Exception as e:
print("App name '{}' is invalid: {}".format(args.app_name, e))
exit(EX_UNAVAILABLE)
def get_app_flavor(args=None):
parser = ArgumentParser(
description="""
Get the current flavor of the Hypernode app.
Example:
$ ./bin/get_app_flavor
{
"name": "3CPU/16GB/80GB (Falcon M 202202)",
"redis_size": "2048"
}
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
app_name = get_app_name()
print_response(client.get_app_flavor(app_name))
def get_flows(args=None):
parser = ArgumentParser(
description="""
Llist the flows for the Hypernode app. This is a history of all the
Hypernode automation jobs that have been ran for this Hypernode.
Example:
$ ./bin/get_flows
{
"count": 36,
"next": null,
"previous": null,
"results": [
{
"uuid": "ad3b520e-a424-4bcb-a6fb-7d1fc055c3fa",
"state": "success",
"name": "create_backup",
"created_at": "2024-05-25T10:01:25Z",
"updated_at": "2024-05-25T10:01:57Z",
"progress": {
"running": [],
"total": 2,
"completed": 2
},
"logbook": "myhypernodeappname",
"tracker": {
"uuid": null,
"description": null
}
},
...
}
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
app_name = get_app_name()
print_response(client.get_flows(app_name))
def get_slas(args=None):
parser = ArgumentParser(
description="""
List all available SLAs.
Example:
$ ./bin/get_slas
[
{
"id": 123,
"code": "sla-standard",
"name": "SLA Standard",
"price": 1234,
"billing_period": 1,
"billing_period_unit": "month"
},
...
]
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
print_response(client.get_slas())
def get_sla(args=None):
parser = ArgumentParser(
description="""
Get a specific SLA.
$ ./bin/get_sla sla-standard
{
"id": 123,
"code": "sla-standard",
"name": "SLA Standard",
"price": 1234,
"billing_period": 1,
"billing_period_unit": "month"
}
""",
formatter_class=RawTextHelpFormatter,
)
parser.add_argument("sla_code", help="The code of the SLA to get")
args = parser.parse_args(args=args)
client = get_client()
print_response(client.get_sla(args.sla_code))
def get_available_backups_for_app(args=None):
parser = ArgumentParser(
description="""
List the available backups for the Hypernode
Example:
$ ./bin/get_available_backups_for_app
{
"count": 10,
"next": null,
"previous": null,
"results": [
{
"backup_created_at": "2024-05-02T12:00:33+02:00",
"type": "periodic",
"backup_id": "1169e792-8b05-449c-a7b1-7d52cf43153a",
"expired_at": "2024-05-30T12:00:33+02:00"
},
...
]
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
app_name = get_app_name()
print_response(client.get_available_backups_for_app(app_name))
def get_eav_description(args=None):
parser = ArgumentParser(
description="""
List all available EAV settings and their descriptions.
Example:
$ ./bin/get_eav_description
{
"supervisor_enabled": [
true,
false
],
"redis_eviction_policy": [
"noeviction",
"allkeys-lru",
"allkeys-lfu",
"volatile-lru",
"volatile-lfu",
"allkeys-random",
"volatile-random",
"volatile-ttl"
],
...
}
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
print_response(client.get_app_eav_description())
def get_app_configurations(args=None):
parser = ArgumentParser(
description="""
List all the available configurations that can be selected when ordering a new Hypernode.
Example:
$ ./bin/get_app_configurations
{
"count": 7,
"next": null,
"previous": null,
"results": [
{
"name": "Akeneo 6.0",
"configuration_id": "akeneo_6_0",
"php_version": "8.0",
"mysql_version": "8.0",
...
},
...
}
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
print_response(client.get_app_configurations())
def get_cluster_relations(args=None):
parser = ArgumentParser(
description="""
List all the cluster relations for the Hypernode.
Example:
$ ./bin/get_cluster_relations
{
"parents": [
{
"id": 182,
"parent": "mytestappdb",
"child": "mytestappweb",
"relation_type": "mysql",
"cluster_description": null
},
{
"id": 180,
"parent": "mytestapp",
"child": "mytestappweb",
"relation_type": "loadbalancer",
"cluster_description": null
},
...
}
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
app_name = get_app_name()
print_response(client.get_cluster_relations(app_name))
def get_product_info(args=None):
parser = ArgumentParser(
description="""
Gets the product info for the specified product
$ ./bin/get_product_info FALCON_S_202203
{
"code": "FALCON_S_202203",
"name": "Falcon S",
"backups_enabled": true,
"storage_size_in_gb": 57,
"price": 1234,
...
}
""",
formatter_class=RawTextHelpFormatter,
)
client = get_client()
parser.add_argument(
"product_code",
help="The code of the product to get",
choices=[p["code"] for p in client.get_active_products().json()],
)
args = parser.parse_args(args=args)
print_response(client.get_product_info_with_price(args.product_code))
def get_block_attack_descriptions(args=None):
parser = ArgumentParser(
description="""
List all attack blocking strategies and their descriptions.
Example:
$ ./bin/get_block_attack_descriptions
{
"BlockSqliBruteForce": "Attempts to deploy NGINX rules to block suspected (blind) SQL injection attacks",
...
}
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
print_response(client.get_block_attack_descriptions())
def block_attack(args=None):
parser = ArgumentParser(
description="""
Block a specific attack based on a pre-defined attack blocking strategy.
$ ./bin/block_attack BlockSqliBruteForce
A job to block the 'BlockSqliBruteForce' attack has been posted.
""",
formatter_class=RawTextHelpFormatter,
)
client = get_client()
choices = client.get_block_attack_descriptions().json().keys()
parser.add_argument("attack_name", help="The attack to block", choices=choices)
args = parser.parse_args(args=args)
app_name = get_app_name()
output = client.block_attack(app_name, args.attack_name).content
if output:
print(output)
else:
print(
"A job to block the '{}' attack has been posted.".format(args.attack_name)
)
def get_whitelist_options(args=None):
parser = ArgumentParser(
description="""
List all available WAF whitelist options for the Hypernode.
Example:
$ ./bin/get_whitelist_options
{
"name": "Whitelist",
"description": "",
"renders": [
"application/json"
],
...
}
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
app_name = get_app_name()
print_response(client.get_whitelist_options(app_name))
def get_whitelist_rules(args=None):
parser = ArgumentParser(
description="""
List all currently configured WAF whitelist rules for the Hypernode.
Example:
$ ./bin/get_whitelist_rules
[
{
"id": 1234,
"created": "2024-05-25T13:39:48Z",
"domainname": "yourhypernodeappname.hypernode.io",
"ip": "1.2.3.4",
"type": "database",
"description": "my description"
},
...
]
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
app_name = get_app_name()
print_response(client.get_whitelist_rules(app_name))
def get_current_product_for_app(args=None):
parser = ArgumentParser(
description="""
Gets the current product for the specified app.
Example:
$ ./bin/get_current_product_for_app
{
"code": "FALCON_M_202203",
"name": "Falcon M",
"backups_enabled": true,
"is_development": false,
"varnish_supported": true,
"supports_sla": true,
"provider_flavors": [
{
"vcpus": 3,
"ram_in_mb": 16384,
...
},
...
}
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
app_name = get_app_name()
print_response(client.get_current_product_for_app(app_name))
def check_payment_information_for_app(args=None):
parser = ArgumentParser(
description="""
Shows the payment information for the specified app.
Example:
$ ./bin/check_payment_information_for_app
{
"has_valid_vat_number": true,
"has_valid_payment_method": true
}
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
app_name = get_app_name()
print_response(client.check_payment_information_for_app(app_name))
def get_active_products(args=None):
parser = ArgumentParser(
description="""
Lists all available products.
Example:
$ ./bin/get_active_products
[
{
"code": "JACKAL_S_202301",
"name": "Jackal S",
"backups_enabled": true,
"is_development": false,
...
},
...
]
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
print_response(client.get_active_products())
def check_xgrade(args=None):
parser = ArgumentParser(
description="""
Verify that the specified app can be upgraded to the specified product.
This checks if there is enough disk space available. The output will also
show whether or not there will be an IP change and if a volume swap xgrade
would be performed instead of an rsync xgrade.
Example:
$ ./bin/check_xgrade FALCON_L_202203
{
"has_valid_vat_number": true,
"has_valid_payment_method": true,
"will_change_ip": false,
"will_do_volswap": false,
"will_disk_fit": true
}
""",
formatter_class=RawTextHelpFormatter,
)
client = get_client()
parser.add_argument(
"product_code",
help="The code of the product to check",
choices=[p["code"] for p in client.get_active_products().json()],
)
args = parser.parse_args(args=args)
app_name = get_app_name()
print_response(client.check_xgrade(app_name, args.product_code))
def xgrade(args=None):
parser = ArgumentParser(
description="""
Change the plan of your Hypernode.
Example:
$ ./bin/xgrade FALCON_L_202203
The job to xgrade Hypernode 'yourappname' to product 'FALCON_L_202203' has been posted
""",
formatter_class=RawTextHelpFormatter,
)
client = get_client()
parser.add_argument(
"product_code",
help="The code of the product to check",
choices=[p["code"] for p in client.get_active_products().json()],
)
args = parser.parse_args(args=args)
app_name = get_app_name()
data = {"product": args.product_code}
output = client.xgrade(app_name, data=data).content
if output:
print(output)
else:
print(
"The job to xgrade Hypernode '{}' to product '{}' "
"has been posted".format(app_name, args.product_code)
)
def get_active_branchers(args=None):
parser = ArgumentParser(
description="""
List all active branchers
Example:
$ ./bin/get_active_branchers
{
"monthly_total_time": 0,
"total_minutes_elapsed": 0,
"actual_monthly_total_cost": 0,
"monthly_total_cost": 0,
"branchers": []
}
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
app_name = get_app_name()
print_response(client.get_active_branchers(app_name))
def create_brancher(args=None):
parser = ArgumentParser(
description="""
Create a Brancher Hypernode from the specified app.
Outputs the app_info of the brancher to be created..
Example:
$ ./bin/create_brancher
{
"name": "yourappname-ephoj82yb",
"parent": "yourappname",
"type": "brancher",
"product": "FALCON_M_202203",
"domainname": "yourappname-ephoj82yb.hypernode.io",
...
}
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
app_name = get_app_name()
data = {}
print_response(client.create_brancher(app_name, data=data))
def destroy_brancher(args=None):
parser = ArgumentParser(
description="""
Destroy a Brancher Hypernode.
Examples:
$ ./bin/destroy_brancher yourbrancherappname-eph12345
A job has been posted to cancel the 'yourbrancherappname-eph12345' brancher app.
""",
formatter_class=RawTextHelpFormatter,
)
parser.add_argument(
"brancher_app_name",
help="The name of the brancher to destroy. See ./bin/get_active_branchers",
)
args = parser.parse_args(args=args)
client = get_client()
try:
client.destroy_brancher(args.brancher_app_name)
print(
"A job has been posted to cancel the '{}' brancher app.".format(
args.brancher_app_name
)
)
exit(EX_OK)
except Exception as e:
print(
"Brancher app '{}' failed to be cancelled: {}".format(
args.brancher_app_name, e
)
)
exit(EX_UNAVAILABLE)
def get_fpm_status(args=None):
parser = ArgumentParser(
description="""
Show the status of the PHP-FPM workers.
Example:
$ ./bin/get_fpm_status
{
"message": null,
"data": "50570 IDLE 0.0s - phpfpm 127.0.0.1 GET magweb/status.php (python-requests/2.28.1)\n50571 IDLE 0.0s - phpfpm 127.0.0.1 GET magweb/status.php (python-requests/2.28.1)\n",
"status": 200
}
""",
formatter_class=RawTextHelpFormatter,
)
parser.parse_args(args=args)
client = get_client()
app_name = get_app_name()
print_response(client.get_fpm_status(app_name))